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