xref: /openbmc/openbmc/poky/scripts/contrib/serdevtry (revision 12fc939c)
1#!/bin/sh
2
3# Copyright (C) 2014 Intel Corporation
4#
5# SPDX-License-Identifier: MIT
6#
7
8if [ "$1" = "" -o "$1" = "--help" ] ; then
9    echo "Usage: $0 <serial terminal command>"
10    echo
11    echo "Simple script to handle maintaining a terminal for serial devices that"
12    echo "disappear when a device is powered down or reset, such as the USB"
13    echo "serial console on the original BeagleBone (white version)."
14    echo
15    echo "e.g. $0 picocom -b 115200 /dev/ttyUSB0"
16    echo
17    exit
18fi
19
20args="$@"
21DEVICE=""
22while [ "$1" != "" ]; do
23    case "$1" in
24        /dev/*)
25            DEVICE=$1
26            break;;
27    esac
28    shift
29done
30
31if [ "$DEVICE" != "" ] ; then
32    while true; do
33        if [ ! -e $DEVICE ] ; then
34            echo "serdevtry: waiting for $DEVICE to exist..."
35            while [ ! -e $DEVICE ]; do
36                sleep 0.1
37            done
38        fi
39        if [ ! -w $DEVICE ] ; then
40            # Sometimes (presumably because of a race with udev) we get to
41            # the device before its permissions have been set up
42            RETRYNUM=0
43            while [ ! -w $DEVICE ]; do
44                if [ "$RETRYNUM" = "2" ] ; then
45                    echo "Device $DEVICE exists but is not writable!"
46                    exit 1
47                fi
48                RETRYNUM=$((RETRYNUM+1))
49                sleep 0.1
50            done
51        fi
52        $args
53        if [ -e $DEVICE ] ; then
54            break
55        fi
56    done
57else
58    echo "Unable to determine device node from command: $args"
59    exit 1
60fi
61
62