3 _SCRIPT_NAME=efi-boot-update
6 . /etc/rc.d/init.d/functions
10 echo "Usage: $0 OPTIONS"
11 echo "Update EFI boot loaders"
13 echo " --version Show version number"
14 echo " --help, -h This help message"
15 echo " --mount, -m Try to mount /boot/efi first"
16 echo " --verbose, -v Verbose output"
17 echo " --force Force file updates"
21 echo "efi-boot-update: $*" >&2
25 if is_yes "$VERBOSE" ; then
26 echo "efi-boot-update: $*" >&2
31 if is_yes "$VERBOSE" ; then
41 if [ "$item" = "$2" ] ; then
52 while [ -n "$1" ] ; do
56 [ -e "$1" ] || return 0
59 msg "update_file: ignoring unknown option: $1"
70 msg "update_file: unexpected arguments: $*"
73 if [ "${dst#/}" = "${dst}" ] ; then
77 if is_yes "$FORCE_UPDATES" ; then
78 is_yes "$VERBOSE" && echo +cp --force --preserve=timestamps "$src" "$dst"
79 cp --force --preserve=timestamps "$src" "$dst"
81 is_yes "$VERBOSE" && echo +cp --update --preserve=timestamps "$src" "$dst"
82 cp --update --preserve=timestamps "$src" "$dst"
86 get_efibootmgr_opts() {
89 efi_disk=$(mount | awk '$3=="/boot/efi" {print $1}' 2>/dev/null)
90 EFIBOOTMGR_OPTS="--gpt"
91 if [ -n "$efi_disk" ] ; then
92 efi_partnum="$(echo $efi_disk|sed -e's;^.*[^0-9]\([0-9]\+\)$;\1;')"
93 efi_disk="$(echo $efi_disk|sed -e's;^\(.*\)[0-9]\+$;\1;')"
94 if [ -b "$efi_disk" -a -n "$efi_partnum" ] ; then
95 EFIBOOTMGR_OPTS="$EFIBOOTMGR_OPTS --disk $efi_disk"
96 EFIBOOTMGR_OPTS="$EFIBOOTMGR_OPTS --part $efi_partnum"
99 echo -n $EFIBOOTMGR_OPTS
102 find_bootmgr_entry () {
104 $EFIBOOTMGR | awk -v find="$1" '
105 /^Boot[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\*?/ {
110 gsub(/^[ \t]+|[ \t]+$/,"");
111 if ($0 == find) print num
115 remove_bootmgr_entry () {
117 bootnum=$(find_bootmgr_entry "$1")
118 [ -n "$bootnum" ] || return 0
119 verbose_cmd $EFIBOOTMGR $EFIBOOTMGR_OPTS --quiet --delete-bootnum -b "$bootnum"
123 add_bootmgr_entry () {
128 bootnum=$(find_bootmgr_entry "$label")
130 if [ "${binary#/}" = "${binary}" ] ; then
132 binary="$DESTDIR/$binary"
134 binary="${binary#/boot/efi}"
135 binary="$(echo -n "$binary"|sed -e's;/;\\;g')"
137 if [ -n "$bootnum" ] ; then
138 echo -n "$args" | verbose_cmd $EFIBOOTMGR $EFIBOOTMGR_OPTS --quiet \
139 --bootnum "$bootnum" --loader "$binary" -@ -
141 echo -n "$args" | verbose_cmd $EFIBOOTMGR $EFIBOOTMGR_OPTS --create \
142 --quiet --label "$label" --loader "$binary" -@ -
143 bootnum="$(find_bootmgr_entry "$label")"
149 echo -n "$*" | tr -c '[a-zA-Z0-9_]' '_'
153 MOUNT_EFI_PARTITION="no"
158 [ -e /etc/efi-boot/update.conf ] && . /etc/efi-boot/update.conf
160 while [ -n "$*" ] ; do
170 echo "$_SCRIPT_NAME $_SCRIPT_VERSION"
174 MOUNT_EFI_PARTITION=yes
189 if ! mountpoint -q /boot/efi ; then
191 if is_yes "$MOUNT_EFI_PARTITION" ; then
192 # first try via fstab
193 if ! mount /boot/efi 2>/dev/null ; then
195 efi_device="$(/sbin/blkid -o device -l -t PARTUUID="54f69bcc-954d-4f97-8fef-80b359f9e4aa")"
196 if [ -z "$efi_device" ] ; then
197 msg "EFI system partition not found."
200 mount -t vfat "$efi_device" /boot/efi
203 if ! mountpoint -q /boot/efi ; then
204 msg "EFI system partition not mounted."
209 if [ -x /usr/sbin/efibootmgr ] ; then
211 EFIBOOTMGR=/usr/sbin/efibootmgr
212 if ! $EFIBOOTMGR >/dev/null 2>&1 ; then
213 msg "efibootmgr does not work (efivars interface not available?)"
214 msg "won't update boot manager configuration"
217 EFIBOOTMGR_OPTS="$(get_efibootmgr_opts)"
220 msg "efibootmgr missing, won't update the boot manager configuration"
224 for bootloader_conf in /etc/efi-boot/update.d/*.conf ; do
225 if [ ! -e "$bootloader_conf" ] ; then
229 CONFIG_NAME="$(basename "$bootloader_conf" .conf)"
238 . "$bootloader_conf" || continue
240 LABEL="$LABEL_PREFIX$LABEL"
242 if ! is_yes "$ENABLED" ; then
243 remove_bootmgr_entry "$LABEL" >/dev/null
248 if [[ "$ARCH" = i?86 || "$ARCH" = pentium[45] || "$ARCH" = "athlon" ]] ; then
251 elif [[ "$ARCH" = "x86_64" || "$ARCH" = "amd64" || "$ARCH" = "ia32e" ]] ; then
257 DESTDIR="/boot/efi/EFI/$(echo -n "$PLATFORM_DIR"|sed -e's/@ARCH@/'"$efi_arch"'/')"
260 verbose "Updating $LABEL..."
262 if [ -n "$BINARY" ] ; then
263 bootnum="$(add_bootmgr_entry "$LABEL" "$BINARY" "$ARGS")"
264 eval "_$(safe_string ${CONFIG_NAME})_bootnum=\"$bootnum\""
268 if [ -n "$ORDER" -a "$EFIBOOTMGR" != "/bin/true" ] ; then
269 # set up the configured boot order, not removing any existing entries
270 tail="$($EFIBOOTMGR | awk '/^BootOrder:/ {gsub(/,/," ",$2); print $2}')"
272 for config_name in $ORDER ; do
273 eval "bootnum=\$_$(safe_string ${config_name})_bootnum"
274 if [ -z "$bootnum" ] ; then
275 verbose "Cannot find '$config_name' config - won't add to boot order."
278 tail="$(list_remove "$tail" "$bootnum")"
279 head="$head $bootnum"
281 bootorder="$(echo -n $head $tail | sed -e's/ /,/g')"
282 if [ -n "$bootorder" ] ; then
283 verbose_cmd $EFIBOOTMGR $EFIBOOTMGR_OPTS --quiet --bootorder "$bootorder"
287 # vi: ft=sh sw=4 sts=4 et