#!/bin/bash # Build a bootable UEFI rescue shell image # Copyright 2014 TJ # Licensed on the terms of the GNU General Public License version 3 # # Creates an image file named "UEFI-GPT-recovery-shell.img" with a default # size of 64MB containing a bootable UEFI image that starts the # Tianocore Shell v2, and also contains GRUB EFI boot-manager and supporting files # set +x ARCH=$(uname -m) ARCH_U=${ARCH^^} # Default values (can be set in the environment prior to executing this script) SIZE=${SIZE:-64} IMG=${IMG:-UEFI-GPT-recovery-shell.img} URL_UEFI_SHELL=${UEL_UEFI_SHELL:-https://svn.code.sf.net/p/edk2/code/trunk/edk2/ShellBinPkg/UefiShell/X64/Shell.efi} GRUB_EFI=${GRUB_EFI:-/usr/lib/grub/${ARCH}-efi/} GRUB_EFI_SIGNED=${GRUB_EFI_SIGNED:-/usr/lib/grub/${ARCH}-efi-signed/} SUDO="" if [ $(id -u) -ne 0 ]; then SUDO="sudo" echo "warning: sudo privileges will be required for some operations" fi dd if=/dev/zero bs=1M "count=$SIZE" "of=$IMG" || exit 1 DEV=$($SUDO losetup --show -f "$IMG") || exit 2 PART=${DEV}p1 $SUDO parted -s $DEV "unit s mklabel gpt mkpart primary fat32 2048s -34s set 1 boot on print" || exit 3 $SUDO mkfs.fat -F 32 -M 0xF8 -n 'EFI.ESP' ${PART} || exit 4 MOUNTPOINT="/mnt/uefi-$RANDOM" MP_BOOT=${MOUNTPOINT}/EFI/BOOT MP_GRUB=${MOUNTPOINT}/EFI/GRUB $SUDO mkdir -p $MOUNTPOINT || exit 5 $SUDO mount -t msdos $PART $MOUNTPOINT || exit 6 $SUDO mkdir -p ${MP_BOOT} ${MP_GRUB} || exit 7 $SUDO wget $URL_UEFI_SHELL -O ${MP_BOOT}/BOOTx64.EFI || exit 8 if [ -d "$GRUB_EFI" ]; then $SUDO grub-install --no-nvram --bootloader-id=grub "--target=${ARCH}-efi" --uefi-secure-boot --efi-directory=${MOUNTPOINT} --boot-directory=${MP_GRUB%/*} $DEV || exit 9 fi $SUDO parted $DEV unit s print echo "info: UEFI System Partition with FAT32 file-system contains these UEFI boot managers:" find ${MOUNTPOINT} -type f -name '*.efi' | sed "s,^${MOUNTPOINT}\(.*\),\1," sync $SUDO umount ${MOUNTPOINT} $SUDO rmdir ${MOUNTPOINT} $SUDO losetup -d $DEV echo "info: Successfully created UEFI bootable Shell in $IMG" exit 0