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