blob: db89041576ffa3976c52042ff7776c13c22f149d [file] [log] [blame]
#!/bin/bash
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
### attach to a serial console
## usage: fx serial [device-path]
##
## If no device path is given, a list of options will be presented.
##
## If the selected device is not readable by the active user account, it will
## first be chmod'd to provide access to the current user.
##
## Exit the session with CTRL+o
##
## In order to have arrow keys work, execute `export TERM=xterm; /boot/bin/sh`
## on in the console.
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $?
fx-config-read
if ! which socat > /dev/null 2>&1; then
fx-error "The command \`socat\` was not found!"
if [[ "$(uname)" == "Linux" ]]; then
fx-error " maybe \`apt install socat\`"
else
fx-error " maybe \`brew install socat\`"
fi
exit 1
fi
DEVICE="$1"
if [[ -z "$DEVICE" ]]; then
echo >&2 "Select a serial device from the following list:"
select device in /dev/ttyUSB*; do
DEVICE="$device"
break
done
fi
if [[ ! -e "$DEVICE" ]]; then
fx-error "$DEVICE not found"
exit 1
fi
if [[ ! -r "$DEVICE" ]]; then
if [[ "$(uname)" == "Linux" ]]; then
owninggroup=$(stat "$DEVICE" --printf="%G")
if [[ ! -r "$DEVICE" ]]; then
fx-error "$DEVICE is not readable by $USER"
fx-error " fix: sudo usermod -a -G "$owninggroup" $USER"
fx-error "You need to start a new login session for a group change to take effect"
exit 1
fi
else
fx-warn "$DEVICE is not readable by $USER"
fx-warn "Fix the permissions on $DEVICE or group membership of $USER"
fi
fi
echo >&2 "SERIAL: Connecting to $DEVICE..."
echo >&2 "SERIAL: Use CTRL-o to exit"
echo >&2 "SERIAL: Run: \`export TERM=xterm; /boot/bin/sh\` for improved key bindings"
socat -,sane,raw,echo=0,icanon=0,escape=0x0f "file:${DEVICE}",b115200,ospeed=115200,ispeed=115200,cs8,clocal=0,cstopb=0,parenb=0,nonblock=1,raw,echo=0