|
| 1 | +#!/bin/bash -eu |
| 2 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +. sh-functions |
| 5 | +. shell-error |
| 6 | + |
| 7 | +BOOTDIR="${BOOTDIR:-/boot}" |
| 8 | +PROCFS_PATH="${PROCFS_PATH:-/proc}" |
| 9 | +arch="${ARCH:-$(uname -m)}" |
| 10 | + |
| 11 | +uefi_stub="${UKI_UEFI_STUB-}" |
| 12 | +kernel="${UKI_KERNEL-}" |
| 13 | +initrd="$workdir/initrd.img" |
| 14 | +outfile="${UKI_EFI_INTERNAL_IMAGE:-$workdir/linux.efi}" |
| 15 | + |
| 16 | + |
| 17 | +get_offset_value() |
| 18 | +{ |
| 19 | + local idx name size vma lma file_off algn |
| 20 | + |
| 21 | + printf -v "$1" '0' |
| 22 | + |
| 23 | + while read -r idx name size vma lma file_off algn; do |
| 24 | + [ -z "$idx" ] || [ -z "${idx##*[!0-9]*}" ] || |
| 25 | + printf -v "$1" '%s' "$(( 16#${size} + 16#${vma} ))" |
| 26 | + done < <(objdump -h "$2" 2>/dev/null) |
| 27 | +} |
| 28 | + |
| 29 | +filter_objdump() |
| 30 | +{ |
| 31 | + local k a b c d e; |
| 32 | + |
| 33 | + eval "$1=('' '' '' '' '')" |
| 34 | + |
| 35 | + while read -r k a b c d e; do |
| 36 | + [ "$k" != "$3" ] || |
| 37 | + eval "$1=(\"\$a\" \"\$b\" \"\$c\" \"\$d\" \"\$e\")" |
| 38 | + done < <(objdump -p "$2" 2>/dev/null) |
| 39 | +} |
| 40 | + |
| 41 | +pe_get_int_value() |
| 42 | +{ |
| 43 | + local values |
| 44 | + filter_objdump values "$2" "$3" |
| 45 | + |
| 46 | + [ -n "${values[0]}" ] && |
| 47 | + printf -v "$1" '%s' "$(( 16#${values[0]} ))" || |
| 48 | + return 1 |
| 49 | +} |
| 50 | + |
| 51 | +pe_file_format() |
| 52 | +{ |
| 53 | + local magic=0 |
| 54 | + pe_get_int_value magic "$1" "Magic" |
| 55 | + |
| 56 | + case "$magic" in |
| 57 | + 267|523) # 010b (PE32) | 020b (PE32+) |
| 58 | + return 0 |
| 59 | + ;; |
| 60 | + esac |
| 61 | + return 1 |
| 62 | +} |
| 63 | + |
| 64 | +section_align=0 |
| 65 | +offset=0 |
| 66 | +update_section_offset() |
| 67 | +{ |
| 68 | + [ "$#" -eq 0 ] || |
| 69 | + offset=$(( $offset + $(stat -Lc%s "$1") )) |
| 70 | + offset=$(( $offset + $section_align - $offset % $section_align )) |
| 71 | +} |
| 72 | + |
| 73 | +args=() |
| 74 | +add_section() |
| 75 | +{ |
| 76 | + local offs |
| 77 | + printf -v offs '0x%x' "$offset" |
| 78 | + args+=( --add-section "$1=$2" --change-section-vma "$1=$offs" ) |
| 79 | + update_section_offset "$2" |
| 80 | +} |
| 81 | + |
| 82 | +[ -n "$kernel" ] || |
| 83 | + kernel="$("$FEATURESDIR"/uki/bin/find-kernel)" |
| 84 | + |
| 85 | +[ -f "$kernel" ] || |
| 86 | + fatal "Can't find a kernel image to create a UEFI executable" |
| 87 | + |
| 88 | +verbose "Kernel image: $kernel" |
| 89 | + |
| 90 | +if [ -z "$uefi_stub" ]; then |
| 91 | + case "$arch" in |
| 92 | + x86_64) efi_type=x64 ;; |
| 93 | + i?86) efi_type=ia32 ;; |
| 94 | + aarch64) efi_type=aa64 ;; |
| 95 | + *) |
| 96 | + fatal "Architecture '$arch' not supported to create a UEFI executable" |
| 97 | + ;; |
| 98 | + esac |
| 99 | + |
| 100 | + uefi_stub="/usr/lib/systemd/boot/efi/linux${efi_type}.efi.stub" |
| 101 | +fi |
| 102 | + |
| 103 | +pe_file_format "$uefi_stub" || |
| 104 | + fatal "wrong file format: $uefi_stub" |
| 105 | + |
| 106 | +verbose "UEFI stub file: $uefi_stub" |
| 107 | + |
| 108 | +get_offset_value offset "$uefi_stub" |
| 109 | + |
| 110 | +[ $offset -gt 0 ] || |
| 111 | + fatal "failed to get the size of $uefi_stub to create UEFI image file" |
| 112 | + |
| 113 | +pe_get_int_value section_align "$uefi_stub" SectionAlignment || |
| 114 | + fatal "failed to get the SectionAlignment of the stub PE header to create the UEFI image file" |
| 115 | + |
| 116 | +image_base=0 |
| 117 | +pe_get_int_value image_base "$uefi_stub" ImageBase || |
| 118 | + fatal "failed to get the ImageBase of the stub PE header to create the UEFI image file" |
| 119 | + |
| 120 | +printf -v image_base '0x%x' "$image_base" |
| 121 | + |
| 122 | +args=( --image-base="$image_base" ) |
| 123 | + |
| 124 | +update_section_offset |
| 125 | + |
| 126 | +for f in /usr/lib/os-release /etc/os-release; do |
| 127 | + if [ -s "$f" ]; then |
| 128 | + add_section ".osrel" "$f" |
| 129 | + break |
| 130 | + fi |
| 131 | +done |
| 132 | + |
| 133 | +# shellcheck disable=SC2206 |
| 134 | +cmdline=( ${UKI_CMDLINE-} ) |
| 135 | + |
| 136 | +if [ "${#cmdline[@]}" -gt 0 ]; then |
| 137 | + :; |
| 138 | +elif [ -d /etc/cmdline.d ]; then |
| 139 | + for conf in /etc/cmdline.d/*.conf; do |
| 140 | + [ -e "$conf" ] || |
| 141 | + continue |
| 142 | + while read -r l; do |
| 143 | + # shellcheck disable=SC2206 |
| 144 | + cmdline+=( $l ) |
| 145 | + done < "$conf" |
| 146 | + done |
| 147 | +elif [ -e "$PROCFS_PATH/cmdline" ]; then |
| 148 | + while read -r l; do |
| 149 | + # shellcheck disable=SC2206 |
| 150 | + cmdline+=( $l ) |
| 151 | + done < "$PROCFS_PATH/cmdline" |
| 152 | +fi |
| 153 | + |
| 154 | +verbose "UEFI kernel cmdline: ${cmdline[*]}" |
| 155 | + |
| 156 | +uefi_cmdline="$workdir/cmdline.txt" |
| 157 | +printf >"$uefi_cmdline" '%s\0' "${cmdline[*]}" |
| 158 | + |
| 159 | +add_section ".cmdline" "$uefi_cmdline" |
| 160 | + |
| 161 | +if [ -n "${UKI_SPLASH_IMAGE-}" ] && [ -s "$UKI_SPLASH_IMAGE" ]; then |
| 162 | + add_section ".splash" "$UKI_SPLASH_IMAGE" |
| 163 | + verbose "UEFI splash image: $UKI_SPLASH_IMAGE" |
| 164 | +fi |
| 165 | + |
| 166 | +uefi_sbat="$workdir/uki.sbat" |
| 167 | +printf >"$uefi_sbat" '%s\n' \ |
| 168 | + "sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md" \ |
| 169 | + ${UKI_SBAT:+"$UKI_SBAT"} |
| 170 | + |
| 171 | +add_section ".sbat" "$uefi_sbat" |
| 172 | +add_section ".linux" "$kernel" |
| 173 | +add_section ".initrd" "$initrd" |
| 174 | + |
| 175 | +uefi_stub_file="$workdir/stub.efi" |
| 176 | + |
| 177 | +cp $verbose -f -- "$uefi_stub" "$uefi_stub_file" |
| 178 | +objcopy --remove-section ".sbat" "$uefi_stub_file" >/dev/null 2>&1 |
| 179 | +objcopy $verbose "${args[@]}" "$uefi_stub_file" "$outfile" |
| 180 | + |
| 181 | +rm -f -- "$uefi_cmdline" "$uefi_sbat" "$uefi_stub_file" "$initrd" |
0 commit comments