From d9647b14a361e0582ff6c96eabcea22ed40cf98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 10 Jan 2024 12:54:01 +0100 Subject: [PATCH 1/4] firmware-util.sh: fold together the three stale-file deletions --- firmware-util.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/firmware-util.sh b/firmware-util.sh index 78f460a..be83664 100644 --- a/firmware-util.sh +++ b/firmware-util.sh @@ -39,9 +39,7 @@ fi #get support scripts echo -e "\nDownloading supporting files..." -rm -rf firmware.sh >/dev/null 2>&1 -rm -rf functions.sh >/dev/null 2>&1 -rm -rf sources.sh >/dev/null 2>&1 +rm -rf firmware.sh functions.sh sources.sh >/dev/null 2>&1 $CURL -sLO ${script_url}firmware.sh rc0=$? $CURL -sLO ${script_url}functions.sh From c7c982947a12a14860ad811546902f0dda8a4489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 10 Jan 2024 12:53:35 +0100 Subject: [PATCH 2/4] firmware-util.sh: add no-network (-n) support --- firmware-util.sh | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/firmware-util.sh b/firmware-util.sh index be83664..5bb7ebe 100644 --- a/firmware-util.sh +++ b/firmware-util.sh @@ -13,6 +13,7 @@ #where the stuff is script_url="https://raw.githubusercontent.com/MrChromebox/scripts/master/" +scriptdir="$(realpath "$(dirname "$0")")" #ensure output of system tools in en-us for parsing export LC_ALL=C @@ -30,8 +31,35 @@ fi printf "\ec" echo -e "\nMrChromebox Firmware Utility Script starting up" +if [ "$1" = '-n' ]; then + # Emulate curl by copying files from the directory with the script, or log the URLs to download & fail. + # Intended for no-network (=> no-OOBE on ChromeOS) and reproduction scenarios. + # Logs the curl cmdlines to stdout (for script(1) use) and to ./curls; + # this means that you can use a reusable medium on the chromebook and do + # mount /dev/sda /media + # bash /media/firmware-utl.sh -n + # umount /media + # on the target machine, then `sh curls` on a trusted network-connected machine, + # and repeat this twice to get all the artifacts for your system. + curl() { + { printf '%q ' curl "$@"; echo; } | tee -a "$scriptdir/curls" + OPTIND=1 + curl_file= + while getopts sLOo: curl_flag; do + case "$curl_flag" in + [sLO]) ;; + o) curl_file="$OPTARG" ;; + *) echo "unknown curl flag -$curl_flag" >&2; return 1 ;; + esac + done + shift "$((OPTIND - 1))" + [ -n "$curl_file" ] || curl_file="${1##*/}" + [ -s "$scriptdir/$curl_file" ] && cp -v "$scriptdir/$curl_file" . + } + export CURL=curl + #check for cmd line param, expired CrOS certs -if ! curl -sLo /dev/null https://mrchromebox.tech/index.html || [[ "$1" = "-k" ]]; then +elif ! curl -sLo /dev/null https://mrchromebox.tech/index.html || [[ "$1" = "-k" ]]; then export CURL="curl -k" else export CURL="curl" From 1bc14a18277619b2bb2ab4edf2f3bbdbee497b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 10 Jan 2024 13:19:08 +0100 Subject: [PATCH 3/4] Lift out /tmp/usb mounting. NFC --- firmware.sh | 37 ++++++------------------------------- functions.sh | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/firmware.sh b/firmware.sh index a38a503..546c433 100644 --- a/firmware.sh +++ b/firmware.sh @@ -593,22 +593,9 @@ function restore_fw_from_usb() { read -rep " Connect the USB/SD device which contains the backed-up stock firmware and press [Enter] to continue. " - + list_usb_devices || { exit_red "No USB devices available to read firmware backup."; return 1; } - usb_dev_index="" - while [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; do - read -rep "Enter the number for the device which contains the stock firmware backup: " usb_dev_index - if [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; then - echo -e "Error: Invalid option selected; enter a number from the list above." - fi - done - usb_device="${usb_devs[${usb_dev_index}-1]}" - mkdir /tmp/usb > /dev/null 2>&1 - mount "${usb_device}" /tmp/usb > /dev/null 2>&1 - if [ $? -ne 0 ]; then - mount "${usb_device}1" /tmp/usb - fi - if [ $? -ne 0 ]; then + if ! mount_usb_device; then echo_red "USB device failed to mount; cannot proceed." read -rep "Press [Enter] to return to the main menu." umount /tmp/usb > /dev/null 2>&1 @@ -789,23 +776,11 @@ USB/SD devices are connected. " backup_fail "No USB devices available to store firmware backup." return 1 fi - - usb_dev_index="" - while [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; do - read -rep "Enter the number for the device to be used for firmware backup: " usb_dev_index - if [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; then - echo -e "Error: Invalid option selected; enter a number from the list above." - fi - done - - usb_device="${usb_devs[${usb_dev_index}-1]}" - mkdir /tmp/usb > /dev/null 2>&1 - if ! mount "${usb_device}" /tmp/usb > /dev/null 2>&1; then - if ! mount "${usb_device}1" /tmp/usb > /dev/null 2>&1; then - backup_fail "USB backup device failed to mount; cannot proceed." - return 1 - fi + if ! mount_usb_device; then + backup_fail "USB backup device failed to mount; cannot proceed." + return 1 fi + backupname="stock-firmware-${boardName}-$(date +%Y%m%d).rom" echo_yellow "\nSaving firmware backup as ${backupname}" if ! cp /tmp/bios.bin /tmp/usb/${backupname}; then diff --git a/functions.sh b/functions.sh index 1cbfec0..dea03cf 100644 --- a/functions.sh +++ b/functions.sh @@ -188,6 +188,25 @@ function list_usb_devices() return 0 } +########################################## +# Mount the found USB device on /tmp/usb # +########################################## +function mount_usb_device() +{ + usb_dev_index="" + while [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; do + read -rep "Enter the number for the device to be used for firmware backup: " usb_dev_index + if [[ "$usb_dev_index" = "" || ($usb_dev_index -le 0 && $usb_dev_index -gt $num_usb_devs) ]]; then + echo -e "Error: Invalid option selected; enter a number from the list above." + fi + done + + usb_device="${usb_devs[${usb_dev_index}-1]}" + mkdir /tmp/usb > /dev/null 2>&1 + mount "${usb_device}" /tmp/usb > /dev/null 2>&1 || + mount "${usb_device}1" /tmp/usb > /dev/null 2>&1 +} + ################ # Get cbfstool # From 3c794bf1e04dca6ec098cd78d7d2d4516e0a9dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=BD=D0=B0=D0=B1?= Date: Wed, 10 Jan 2024 13:21:12 +0100 Subject: [PATCH 4/4] Also handle running from a mounted USB device by bind-mounting /tmp/usb from an existing mountpoint --- functions.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions.sh b/functions.sh index dea03cf..d1cd9c6 100644 --- a/functions.sh +++ b/functions.sh @@ -204,7 +204,8 @@ function mount_usb_device() usb_device="${usb_devs[${usb_dev_index}-1]}" mkdir /tmp/usb > /dev/null 2>&1 mount "${usb_device}" /tmp/usb > /dev/null 2>&1 || - mount "${usb_device}1" /tmp/usb > /dev/null 2>&1 + mount "${usb_device}1" /tmp/usb > /dev/null 2>&1 || + { mounted="$(df -x devtmpfs "${usb_device}" || df -x devtmpfs "${usb_device}1")" 2>/dev/null && mount --bind "${mounted}" /tmp/usb > /dev/null 2>&1; } }