build-efi.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Script to build an EFI thing suitable for booting with QEMU, possibly running
  5. # it also.
  6. # This just an example. It assumes that
  7. # - you build U-Boot in ${ubdir}/<name> where <name> is the U-Boot board config
  8. # - /mnt/x is a directory used for mounting
  9. # - you have access to the 'pure UEFI' builds for QEMU
  10. #
  11. # UEFI binaries for QEMU used for testing this script:
  12. #
  13. # OVMF-pure-efi.i386.fd at
  14. # https://drive.google.com/file/d/1jWzOAZfQqMmS2_dAK2G518GhIgj9r2RY/view?usp=sharing
  15. # OVMF-pure-efi.x64.fd at
  16. # https://drive.google.com/file/d/1c39YI9QtpByGQ4V0UNNQtGqttEzS-eFV/view?usp=sharing
  17. set -e
  18. usage() {
  19. echo "Usage: $0 [-a | -p] [other opts]" 1>&2
  20. echo 1>&2
  21. echo " -a - Package up the app" 1>&2
  22. echo " -o - Use old EFI app build (before 32/64 split)" 1>&2
  23. echo " -p - Package up the payload" 1>&2
  24. echo " -P - Create a partition table" 1>&2
  25. echo " -r - Run QEMU with the image" 1>&2
  26. echo " -s - Run QEMU with serial only (no display)" 1>&2
  27. echo " -w - Use word version (32-bit)" 1>&2
  28. exit 1
  29. }
  30. # 32- or 64-bit EFI
  31. bitness=64
  32. # app or payload ?
  33. type=app
  34. # create a partition table and put the filesystem in that (otherwise put the
  35. # filesystem in the raw device)
  36. part=
  37. # run the image with QEMU
  38. run=
  39. # run QEMU without a display (U-Boot must be set to stdout=serial)
  40. serial=
  41. # before the 32/64 split of the app
  42. old=
  43. # Set ubdir to the build directory where you build U-Boot out-of-tree
  44. # We avoid in-tree build because it gets confusing trying different builds
  45. ubdir=/tmp/b/
  46. while getopts "aopPrsw" opt; do
  47. case "${opt}" in
  48. a)
  49. type=app
  50. ;;
  51. p)
  52. type=payload
  53. ;;
  54. r)
  55. run=1
  56. ;;
  57. s)
  58. serial=1
  59. ;;
  60. w)
  61. bitness=32
  62. ;;
  63. o)
  64. old=1
  65. ;;
  66. P)
  67. part=1
  68. ;;
  69. *)
  70. usage
  71. ;;
  72. esac
  73. done
  74. run_qemu() {
  75. extra=
  76. if [[ "${bitness}" = "64" ]]; then
  77. qemu=qemu-system-x86_64
  78. bios=OVMF-pure-efi.x64.fd
  79. else
  80. qemu=qemu-system-i386
  81. bios=OVMF-pure-efi.i386.fd
  82. fi
  83. if [[ -n "${serial}" ]]; then
  84. extra="-display none -serial mon:stdio"
  85. fi
  86. echo "Running ${qemu}"
  87. # Use 512MB since U-Boot EFI likes to have 256MB to play with
  88. "${qemu}" -bios "${bios}" \
  89. -m 512 \
  90. -drive id=disk,file="${IMG}",if=none,format=raw \
  91. -nic none -device ahci,id=ahci \
  92. -device ide-hd,drive=disk,bus=ahci.0 ${extra}
  93. }
  94. setup_files() {
  95. echo "Packaging ${BUILD}"
  96. mkdir -p $TMP
  97. cat >$TMP/startup.nsh <<EOF
  98. fs0:u-boot-${type}.efi
  99. EOF
  100. sudo cp ${ubdir}/${BUILD}/u-boot-${type}.efi $TMP
  101. # Can copy in other files here:
  102. #sudo cp ${ubdir}/$BUILD/image.bin $TMP/chromeos.rom
  103. #sudo cp /boot/vmlinuz-5.4.0-77-generic $TMP/vmlinuz
  104. }
  105. # Copy files into the filesystem
  106. copy_files() {
  107. sudo cp $TMP/* $MNT
  108. }
  109. # Create a filesystem on a raw device and copy in the files
  110. setup_raw() {
  111. mkfs.vfat "${IMG}" >/dev/null
  112. sudo mount -o loop "${IMG}" $MNT
  113. copy_files
  114. sudo umount $MNT
  115. }
  116. # Create a partition table and put the filesystem in the first partition
  117. # then copy in the files
  118. setup_part() {
  119. # Create a gpt partition table with one partition
  120. parted "${IMG}" mklabel gpt 2>/dev/null
  121. # This doesn't work correctly. It creates:
  122. # Number Start End Size File system Name Flags
  123. # 1 1049kB 24.1MB 23.1MB boot msftdata
  124. # Odd if the same is entered interactively it does set the FS type
  125. parted -s -a optimal -- "${IMG}" mkpart boot fat32 1MiB 23MiB
  126. # Map this partition to a loop device
  127. kp="$(sudo kpartx -av ${IMG})"
  128. read boot_dev<<<$(grep -o 'loop.*p.' <<< "${kp}")
  129. test "${boot_dev}"
  130. dev="/dev/mapper/${boot_dev}"
  131. mkfs.vfat "${dev}" >/dev/null
  132. sudo mount -o loop "${dev}" $MNT
  133. copy_files
  134. # Sync here since this makes kpartx more likely to work the first time
  135. sync
  136. sudo umount $MNT
  137. # For some reason this needs a sleep or it sometimes fails, if it was
  138. # run recently (in the last few seconds)
  139. if ! sudo kpartx -d "${IMG}" > /dev/null; then
  140. sleep .5
  141. sudo kpartx -d "${IMG}" > /dev/null || \
  142. echo "Failed to remove ${boot_dev}, use: sudo kpartx -d ${IMG}"
  143. fi
  144. }
  145. TMP="/tmp/efi${bitness}${type}"
  146. MNT=/mnt/x
  147. BUILD="efi-x86_${type}${bitness}"
  148. IMG=try.img
  149. if [[ -n "${old}" && "${bitness}" = "32" ]]; then
  150. BUILD="efi-x86_${type}"
  151. fi
  152. setup_files
  153. qemu-img create "${IMG}" 24M >/dev/null
  154. if [[ -n "${part}" ]]; then
  155. setup_part
  156. else
  157. setup_raw
  158. fi
  159. if [[ -n "${run}" ]]; then
  160. run_qemu
  161. fi