blob: 9e56389ddb5241682732bffc7a512e72186d2d0d [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Provides alert messages in boot stage, called by chromeos_startup.
# Since this script only provides messages, never abort.
set +e
# Prints usage help for commands supports
usage_help() {
echo "Usage: $0 mode terminal [arg ...]
warn_dev: Message for warning about developer mode on normal firmware.
Arg #1: (optional, default=30) Countdown in seconds before continue
enter_dev: Message for entering developer mode from non-dev.
Arg #1: (optional, default=30) Countdown in seconds before continue
leave_dev: Message when leaving developer mode.
Arg: none
update_firmware: Message before starting firmware update.
Arg: none
wipe: Message when starting to erase stateful partition.
Arg #1: (optional) Image file to show.
self_repair: Message when starting to rebuild stateful partition.
"
}
# Prints the two byte hex code of the matched char or
# exists non-zero on timeout. It reads from the current tty.
# To override, call it as follows,
# match_char_timeout args < "$STDOUT"
#
# Arguments: time_in_seconds two_byte_hex_match_1 two_byte_hex_match_2 ...
match_char_timeout() {
local delay_secs="$1"
shift
local input=''
local match=''
local start_time=$(date +%s)
local stop_time=$((start_time + delay_secs))
local tty_config=$(stty -g)
stty raw -echo
while [ $delay_secs -gt 0 ]; do
input=$(timeout -s KILL ${delay_secs}s head -c 1)
[ $? -eq 137 ] && break # Timed out.
input=$(printf "%02x" "'$input")
for char in "$@"; do
if [ "$input" = "$char" ]; then
match="$input"
break
fi
done
[ -n "$match" ] && break
delay_secs=$((stop_time - $(date +%s) ))
done
# Restores the tty's settings.
stty $tty_config
[ -z "$match" ] && return 1
printf "$match"
return 0
}
# Prints message when in developer mode
# Argument: time to countdown (in seconds)
mode_warn_dev() {
local delay_secs="${1:-30}"
# Don't show a broken/partial background image.
tput clear > "$STDOUT"
echo '
__________________________
|.------------------------.|
|| ||
|| (_) (_) ||
|| ||
|| __________ ||
|| / \ ||
||________________________||
/ __/___/_____________\__\__ \
/ _ \
/ /_______\ \_____\ \
/__________________\ \_____________\
\___________________\ \____________/
\ \_-_-_-.
/| |
| | |
\ |
\_______/
Chrome OS verification is turned off.
Press space to begin recovery.
' >"$STDOUT"
# TODO(wad) Use the developer mode firmware image
# Read a space bar or Ctrl+D or timeout.
local input=$(match_char_timeout "$delay_secs" 04 20 < "$STDOUT")
local exit_code=$?
tput clear > "$STDOUT"
# If we timed out, we're done.
[ $exit_code -ne 0 ] && return 0
case "$input" in
"04") # Ctrl+D
# Done.
;;
"20") # Spacebar
crossystem recovery_request=1
# TODO(rspangler,vb) remove when reboot_mode is gone.
reboot_mode --recovery=1
reboot
;;
esac
return 0
}
# Prints message when entering developer mode
# Argument: time to countdown (in seconds)
mode_enter_dev() {
local delay_secs="${1:-30}"
echo "
Your system is transitioning to Developer Mode.
If you do not want to enter Developer Mode, turn your computer off now
and toggle the developer switch back to Normal (Verified) Mode.
" >"$STDOUT"
local echo_opt='-n'
if [ -n "$BASH_VERSION" ]; then
# bash needs -e to interpret \r, but dash does not.
echo_opt='-n -e'
fi
for dev_count_down in $(seq $delay_secs -1 1); do
# Trailing spaces must exist to clear previous message when the printed
# counter width changed (ex, 100->99).
echo $echo_opt "\r Starting in $dev_count_down second(s)... " >"$STDOUT"
sleep 1
done
echo $echo_opt "\r " >"$STDOUT"
# Count-down
echo "
Preparing your system for developer mode.
Please wait 5 - 10 minutes and DO NOT POWER OFF . . .
The system will reboot when complete.
On reboot, press Ctrl+Alt+[->] (F2) for further instruction.
" >"$STDOUT"
# TODO(wad,wfrichar) Request a root password here.
# TODO(wad,reinauer) Inform the user of chromeos-firmwareupdate --mode=todev
}
# Prints message when leaving developer mode
mode_leave_dev() {
echo "
Your system last booted in developer mode.
Attempting to return to normal...
Upon completion, the machine will reboot.
" >"$STDOUT"
}
# Prints messages before starting firmware update
mode_update_firmware() {
echo "
Your system is updating firmware, please DO NOT POWER OFF.
Upon completion, the machine may reboot or continue for login.
" >"$STDOUT"
}
# Prints message before starting to erase stateful partition (wipe).
# Argument: (optional) file for splash image to show
mode_wipe() {
local splash_file="$1"
if [ -s "$splash_file" ] && type ply-image >/dev/null 2>&1; then
# Do not use background execution because this file is going to be wiped.
ply-image "$splash_file"
else
echo " Erasing stateful partition..." >"$STDOUT"
fi
}
# Prints message before starting to rebuild a corrupted stateful partition.
mode_self_repair() {
echo "
Your system is performing a self-repair process, please wait...
" >"$STDOUT"
}
# Main initialization and dispatcher
main() {
# process args
if [ $# -lt 2 ]; then
usage_help
exit 1
fi
local mode="$1"
# global
STDOUT="$2"
shift
shift
# light-up the screen if possible
if [ -x /usr/sbin/lightup_screen ]; then
/usr/sbin/lightup_screen >/dev/null 2>&1
fi
case "$mode" in
"warn_dev" | "enter_dev" | "leave_dev" | \
"update_firmware" | "wipe" |"self_repair" )
mode_"$mode" "$@"
;;
* )
usage_help
exit 1
;;
esac
}
# Main Entry
main "$@"