Skip to content

Instantly share code, notes, and snippets.

@rany2
Created April 10, 2023 14:02
Show Gist options
  • Save rany2/520e3edcf819937951cecaee86648741 to your computer and use it in GitHub Desktop.
Save rany2/520e3edcf819937951cecaee86648741 to your computer and use it in GitHub Desktop.
Get bootloader type and (if available) the bootloader version. Only works on x86/x86-64 Linux.
#!/bin/bash
set -euf -o pipefail
# Copyright (C) rany <[email protected]>. All rights reserved.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
# Where 0x15 is the bootloader type and 0x234 is the version,
# it is derived by doing: 0x15 << 4 | 0x234 & 0xf = 0x154.
# We use kernel.bootloader_version to get the full version type
# because bootloader_type only provides the last four bits.
bootloader_type=$(($(</proc/sys/kernel/bootloader_type) >> 4))
# Get the full bootloader version. If not written, it is zero.
# Source: https://www.kernel.org/doc/Documentation/x86/boot.rst
# (ext_loader_ver section)
bootloader_version=$(</proc/sys/kernel/bootloader_version)
case $bootloader_type in
# Source: https://www.kernel.org/doc/Documentation/x86/boot.rst
# (type_of_loader section)
$((0x00)))
if ((bootloader_version == 0)); then
bootloader_type='pre-2.00 bootloader'
else
bootloader_type='LILO'
fi
;;
$((0x01))) bootloader_type='Loadlin';;
$((0x02)))
# Used by EFI Boot Stub:
# https://github.com/torvalds/linux/blob/09a9639e56c01c7a00d6c0ca63f4c7c41abe075d/drivers/firmware/efi/libstub/x86-stub.c#L506
if ((bootloader_version == 1)); then
bootloader_type='EFI Boot Stub'
bootloader_version='0'
elif ((bootloader_version != 0)); then
echo >&2 "Invalid type_of_loader and version combination. Only 0x20 and 0x21 are known to be used."
exit 1
else
bootloader_type='bootsect-loader'
fi
;;
$((0x03))) bootloader_type='Syslinux';;
$((0x04))) bootloader_type='Etherboot/gPXE/iPXE';;
$((0x05))) bootloader_type='ELILO';;
$((0x07))) bootloader_type='GRUB';;
$((0x08))) bootloader_type='U-Boot';;
$((0x09))) bootloader_type='Xen';;
$((0x0A))) bootloader_type='Gujin';;
$((0x0B))) bootloader_type='Qemu';;
$((0x0C))) bootloader_type='Arcturus Networks uCbootloader';;
$((0x0D))) bootloader_type='kexec-tools';;
$((0x11))) bootloader_type='Minimal Linux Bootloader <http://sebastian-plotz.blogspot.de>';;
$((0x12))) bootloader_type='OVMF UEFI virtualization stack';;
$((0x13))) bootloader_type='barebox';;
*) echo >&2 "Unknown bootloader type ($bootloader_type). Please update this script."; exit 1;;
esac
printf %s "$bootloader_type"
(( bootloader_version > 0 )) && printf %s " (v$bootloader_version)"
printf '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment