Skip to content

Commit

Permalink
zfsbootmenu/bin/firmware-setup: new reboot functionality
Browse files Browse the repository at this point in the history
reboots to UEFI firmware setup if supported

fixes #423
  • Loading branch information
classabbyamp committed Mar 18, 2024
1 parent 406cf45 commit 0d0372d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/online/recovery-shell.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ Common Commands
**reboot**

Reboot the system using a SysRq magic invocation.

**firmware-setup**

Reboot the system into the UEFI Firmware Setup interface (if available).
1 change: 1 addition & 0 deletions zfsbootmenu/bin/firmware-setup
50 changes: 49 additions & 1 deletion zfsbootmenu/bin/reboot
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,61 @@ done

unset src sources

check_fw_setup() {
if ! is_efi_system; then
zdebug "efivarfs unsupported"
return 1
elif [ ! -r /sys/firmware/efi/efivars/OsIndicationsSupported-8be4df61-93ca-11d2-aa0d-00e098032b8c ]; then
zdebug "OsIndicationsSupported unsupported"
return 1
elif [ ! -r /sys/firmware/efi/efivars/OsIndications-8be4df61-93ca-11d2-aa0d-00e098032b8c ]; then
zdebug "OsIndications unsupported"
return 1
fi

# Check if the EFI_OS_INDICATIONS_BOOT_TO_FW_UI = 0x01 bit is set
if ! (( $(od -An -t u1 -j4 -N1 \
/sys/firmware/efi/efivars/OsIndicationsSupported-8be4df61-93ca-11d2-aa0d-00e098032b8c \
| tr -dc '0-9') & 1 )); then
zdebug "EFI reboot to firmware setup unsupported"
return 1
fi
}

set_fw_setup() {
mount_efivarfs rw
if [ ! -w /sys/firmware/efi/efivars/OsIndications-8be4df61-93ca-11d2-aa0d-00e098032b8c ]; then
zdebug "OsIndications not writable"
return 1
fi

mapfile -t osindications < <(od -An -t x1 -v -w1 \
/sys/firmware/efi/efivars/OsIndications-8be4df61-93ca-11d2-aa0d-00e098032b8c | tr -dc '[:alnum:]\n')

# Set the EFI_OS_INDICATIONS_BOOT_TO_FW_UI = 0x01 bit if not already set
if ! (( "${osindications[4]}" & 0x01 )); then
printf -v osindications[4] '%02x' $(( 0x"${osindications[4]}" | 0x01 ))

printf -v bytes '\\x%02x' "${osindications[@]}"
printf '%b' "$bytes" \
> /sys/firmware/efi/efivars/OsIndications-8be4df61-93ca-11d2-aa0d-00e098032b8c
fi
}

case "${0##*/}" in
reboot)
trigger="b"
;;
shutdown|poweroff)
trigger="o"
;;
firmware-setup)
if ! check_fw_setup || ! set_fw_setup; then
echo -e "\033[0;31mWARNING: Reboot to UEFI firmware UI unsupported; unable to proceed\033[0m"
exit 1
fi
trigger="b"
;;
*)
exit
;;
Expand All @@ -34,7 +82,7 @@ while read -r _pool; do
done <<<"$( zpool list -H -o name )"

sysrq="/proc/sysrq-trigger"
if [ -e "${sysrq}" ] && [ -w "${sysrq}" ]; then
if [ -e "${sysrq}" ] && [ -w "${sysrq}" ]; then
echo "${trigger}" > /proc/sysrq-trigger
else
echo "${sysrq} does not exist, hard reset system"
Expand Down

0 comments on commit 0d0372d

Please sign in to comment.