make_grub_efi_image() {
local path="${1}"
- local label="${2}"
+ local efiboot="${2}"
+ local label="${3}"
- local grub_efi_arch="${arch}"
+ local config="$(mktemp)"
- case "${arch}" in
- aarch64)
- grub_efi_arch="a64"
- ;;
- x86_64)
- grub_efi_arch="x64"
- ;;
- esac
+ # Write early GRUB configuration
+ make_grub_efi_config "${label}" > "${config}"
+
+ # Generate a GRUB image
+ if ! in_buildsystem --bind="${tempdir}" --bind="${config}" \
+ grub-mkimage \
+ --format="${arch}-efi" \
+ --prefix="/EFI/BOOT" \
+ --output="${path}" \
+ --config="${config}" \
+ "${GRUB_MODULES[@]}"; then
+ echo "Could not generate GRUB EFI image" >&2
+ rm -f "${config}"
+ return 1
+ fi
+
+ # Remove configuration
+ rm -f "${config}"
# Allocate file
- if ! allocate_file "${path}" 1440; then
+ if ! allocate_file "${efiboot}" 1440; then
return 1
fi
# Format it with FAT
- if ! mkdosfs -F 12 -n "BRICKLAYER_EFI" "${path}"; then
+ if ! mkdosfs -F 12 -n "BRICKLAYER_EFI" "${efiboot}"; then
echo "Could not format the EFI filesystem" >&2
+ rm -f "${efiboot}"
return 1
fi
local tempdir="$(mktemp -d)"
- local config="$(mktemp)"
# Mount the disk
- if ! mount -o loop "${path}" "${tempdir}"; then
+ if ! mount -o loop "${efiboot}" "${tempdir}"; then
echo "Could not mount EFI filesystem" >&2
+ rm -rf "${efiboot}" "${tempdir}"
return 1
fi
mkdir -p "${tempdir}/EFI/BOOT"
- # Write early GRUB configuration
- make_grub_efi_config "${label}" > "${config}"
-
- # Generate a GRUB image
- if ! in_buildsystem --bind="${tempdir}" --bind="${config}" \
- grub-mkimage \
- --format="${arch}-efi" \
- --prefix="/EFI/BOOT" \
- --output="${tempdir}/EFI/BOOT/boot${grub_efi_arch}.efi" \
- --config="${config}" \
- "${GRUB_MODULES[@]}"; then
- echo "Could not generate GRUB EFI image" >&2
- umount "${tempdir}"
- rm -rf "${tempdir}" "${config}"
+ # Copy GRUB EFI image onto the FAT partition
+ if ! cp -- "${path}" "${tempdir}/EFI/BOOT/${path##*/}"; then
+ echo "Could not copy GRUB EFI image onto the FAT filesystem" >&2
+ rm -rf "${efiboot}" "${tempdir}"
return 1
fi
+ # Umount the filesystem
if ! umount "${tempdir}"; then
- rm -rf "${tempdir}" "${config}"
+ rm -rf "${efiboot}" "${tempdir}"
return 1
fi
- rm -rf "${tempdir}" "${config}"
+ # Cleanup
+ rm -rf "${tempdir}"
return 0
}
# Generate GRUB EFI image
if [ "${has_efi_boot}" = "true" ]; then
- if ! make_grub_efi_image "${tempdir}/boot/efiboot.img" "${label}"; then
+ local grub_efi_arch="${arch}"
+
+ case "${arch}" in
+ aarch64)
+ grub_efi_arch="a64"
+ ;;
+ x86_64)
+ grub_efi_arch="x64"
+ ;;
+ esac
+
+ # Create a directory for EFI stuff
+ mkdir -p "${tempdir}/EFI/BOOT"
+
+ # Generate a GRUB EFI image
+ if ! make_grub_efi_image \
+ "${tempdir}/EFI/BOOT/boot${grub_efi_arch}.efi" \
+ "${tempdir}/boot/efiboot.img" \
+ "${label}"; then
rm -rf "${tempdir}"
return 1
fi