grub-shell.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #! @BUILD_SHEBANG@
  2. set -e
  3. # Run GRUB script in a Qemu instance
  4. # Copyright (C) 2009,2010 Free Software Foundation, Inc.
  5. #
  6. # GRUB is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # GRUB is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. # Initialize some variables.
  19. prefix="@prefix@"
  20. exec_prefix="@exec_prefix@"
  21. datarootdir="@datarootdir@"
  22. builddir="@builddir@"
  23. srcdir="@srcdir@"
  24. PACKAGE_NAME=@PACKAGE_NAME@
  25. PACKAGE_TARNAME=@PACKAGE_TARNAME@
  26. PACKAGE_VERSION=@PACKAGE_VERSION@
  27. # Force build directory components
  28. PATH="${builddir}:$PATH"
  29. export PATH
  30. trim=0
  31. trim_head=664cbea8-132f-4770-8aa4-1696d59ac35c
  32. # Usage: usage
  33. # Print the usage.
  34. usage () {
  35. cat <<EOF
  36. Usage: $0 [OPTION] [SOURCE]
  37. Run GRUB script in a Qemu instance.
  38. -h, --help print this message and exit
  39. -v, --version print the version information and exit
  40. --boot=[fd|hd|cd|net] boot method for Qemu instance
  41. --modules=MODULES pre-load specified modules MODULES
  42. --qemu=FILE Name of qemu binary
  43. --disk=FILE Attach FILE as a disk
  44. --qemu-opts=OPTIONS extra options to pass to Qemu instance
  45. --files=FILES add files to the image
  46. --mkrescue-arg=ARGS additional arguments to grub-mkrescue
  47. --timeout=SECONDS set timeout
  48. --trim trim firmware output
  49. $0 runs input GRUB script or SOURCE file in a Qemu instance and prints
  50. its output.
  51. Report bugs to <bug-grub@gnu.org>.
  52. EOF
  53. }
  54. # Exec given argv and only show its output on STDERR if it returns an
  55. # error status.
  56. exec_show_error () {
  57. v=`$@ 2>&1`
  58. ret=$?
  59. if [ "$ret" != 0 ]; then
  60. echo "$v" >&2
  61. exit $ret
  62. fi
  63. }
  64. work_directory=${WORKDIR:-`mktemp -d "${TMPDIR:-/tmp}/grub-shell.XXXXXXXXXX"`} || exit 1
  65. . "${builddir}/grub-core/modinfo.sh"
  66. qemuopts=
  67. serial_port=com0
  68. serial_null=
  69. halt_cmd=halt
  70. pseries=n
  71. disk="hda "
  72. case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
  73. *-emu)
  74. device_map="$work_directory/device.map"
  75. boot=emu
  76. console=console
  77. disk=0
  78. # To skip "Welcome to GRUB" and color setttings
  79. trim=1
  80. serial_port=
  81. ;;
  82. powerpc-ieee1275)
  83. boot=hd
  84. qemu=qemu-system-ppc
  85. console=console
  86. serial_port=escc-ch-b
  87. serial_null="-serial null"
  88. netbootext=elf
  89. trim=1
  90. qemuopts="-M mac99,via=pmu $qemuopts"
  91. ;;
  92. sparc64-ieee1275)
  93. boot=cd
  94. qemu=qemu-system-sparc64
  95. console=
  96. serial_port=ieee1275/ttya
  97. trim=1
  98. qemuopts="$qemuopts -no-reboot"
  99. halt_cmd=reboot
  100. ;;
  101. mips-qemu_mips)
  102. boot=mips_qemu
  103. qemu=qemu-system-mips
  104. qemuopts="-M mips $qemuopts"
  105. console=vga_text
  106. ;;
  107. mips-arc)
  108. boot=cd
  109. qemu=qemu-system-mips64
  110. qemuopts="-M indy $qemuopts"
  111. serial_port=arc/serial0/line0
  112. console=
  113. trim=1
  114. ;;
  115. mipsel-arc)
  116. boot=cd
  117. qemu=qemu-system-mips64el
  118. qemuopts="-M magnum $qemuopts -no-reboot"
  119. serial_port=arc/multi0/serial0
  120. console=console
  121. halt_cmd=reboot
  122. trim=1
  123. ;;
  124. mipsel-qemu_mips)
  125. boot=mipsel_qemu
  126. qemu=qemu-system-mipsel
  127. qemuopts="-M mips $qemuopts"
  128. console=vga_text
  129. ;;
  130. mipsel-loongson)
  131. boot=mipsel_fulong2e
  132. qemu=qemu-system-mips64el
  133. qemuopts="-M fulong2e $qemuopts"
  134. console=
  135. trim=1
  136. ;;
  137. i386-coreboot)
  138. boot=coreboot
  139. qemu=qemu-system-i386
  140. console=vga_text
  141. ;;
  142. i386-multiboot)
  143. boot=cd
  144. qemu=qemu-system-i386
  145. console=vga_text;;
  146. i386-ieee1275)
  147. boot=hd
  148. qemu=qemu-system-i386
  149. console=console
  150. trim=1
  151. disk="hdb "
  152. ;;
  153. i386-qemu)
  154. boot=qemu
  155. qemu=qemu-system-i386
  156. console=vga_text;;
  157. i386-pc)
  158. boot=cd
  159. qemu=qemu-system-i386
  160. console=console
  161. netbootext=0
  162. ;;
  163. i386-efi)
  164. qemu=qemu-system-i386
  165. boot=cd
  166. console=console
  167. trim=1
  168. pflash=${srcdir}/OVMF32.fd
  169. pflash_code=${srcdir}/OVMF32_CODE.fd
  170. pflash_vars=${srcdir}/OVMF32_VARS.fd
  171. if [ -f "$pflash" ]; then
  172. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  173. elif [ -f "$pflash_code" ]; then
  174. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  175. if [ -f "$pflash_vars" ]; then
  176. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  177. fi
  178. else
  179. pflash=/usr/share/qemu/OVMF32.fd
  180. pflash_code=/usr/share/OVMF/OVMF32_CODE_4M.secboot.fd
  181. pflash_vars=/usr/share/OVMF/OVMF32_VARS_4M.fd
  182. if [ -f "$pflash" ]; then
  183. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  184. elif [ -f "$pflash_code" ]; then
  185. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  186. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  187. else
  188. echo "Firmware not found, please install either the appropriate Debian package or an appropriately named copy in the source directory." >&2
  189. exit 1
  190. fi
  191. fi
  192. qemuopts="-machine q35 $qemuopts"
  193. ;;
  194. x86_64-efi)
  195. qemu=qemu-system-x86_64
  196. boot=cd
  197. console=console
  198. trim=1
  199. pflash=${srcdir}/OVMF.fd
  200. pflash_code=${srcdir}/OVMF_CODE.fd
  201. pflash_vars=${srcdir}/OVMF_VARS.fd
  202. if [ -f "$pflash" ]; then
  203. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  204. elif [ -f "$pflash_code" ]; then
  205. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  206. if [ -f "$pflash_vars" ]; then
  207. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  208. fi
  209. else
  210. pflash=/usr/share/qemu/OVMF.fd
  211. pflash_code=/usr/share/OVMF/OVMF_CODE.fd
  212. pflash_vars=/usr/share/OVMF/OVMF_VARS.fd
  213. if [ -f "$pflash" ]; then
  214. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  215. elif [ -f "$pflash_code" ]; then
  216. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  217. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  218. else
  219. echo "Firmware not found, please install either the appropriate Debian package or an appropriately named copy in the source directory." >&2
  220. exit 1
  221. fi
  222. fi
  223. ;;
  224. arm64-efi)
  225. qemu=qemu-system-aarch64
  226. boot=hd
  227. console=console
  228. trim=1
  229. pflash=${srcdir}/AAVMF.fd
  230. pflash_code=${srcdir}/AAVMF_CODE.fd
  231. pflash_vars=${srcdir}/AAVMF_VARS.fd
  232. if [ -f "$pflash" ]; then
  233. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  234. elif [ -f "$pflash_code" ]; then
  235. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  236. if [ -f "$pflash_vars" ]; then
  237. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  238. fi
  239. else
  240. pflash=/usr/share/qemu-efi-aarch64/QEMU_EFI.fd
  241. pflash_code=/usr/share/AAVMF/AAVMF_CODE.fd
  242. pflash_vars=/usr/share/AAVMF/AAVMF_VARS.fd
  243. if [ -f "$pflash" ]; then
  244. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  245. elif [ -f "$pflash_code" ]; then
  246. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  247. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  248. else
  249. echo "Firmware not found, please install either the appropriate Debian package or an appropriately named copy in the source directory." >&2
  250. exit 1
  251. fi
  252. fi
  253. qemuopts="-machine virt -cpu cortex-a57 $qemuopts"
  254. disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
  255. serial_port=
  256. ;;
  257. arm-efi)
  258. qemu=qemu-system-arm
  259. boot=hd
  260. console=console
  261. trim=1
  262. pflash=${srcdir}/AAVMF32.fd
  263. pflash_code=${srcdir}/AAVMF32_CODE.fd
  264. pflash_vars=${srcdir}/AAVMF32_VARS.fd
  265. if [ -f "$pflash" ]; then
  266. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  267. elif [ -f "$pflash_code" ]; then
  268. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  269. if [ -f "$pflash_vars" ]; then
  270. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  271. fi
  272. else
  273. pflash=/usr/share/AAVMF/AAVMF32.fd
  274. pflash_code=/usr/share/AAVMF/AAVMF32_CODE.fd
  275. pflash_vars=/usr/share/AAVMF/AAVMF32_VARS.fd
  276. if [ -f "$pflash" ]; then
  277. qemuopts="-drive if=pflash,format=raw,unit=0,snapshot=on,file=$pflash $qemuopts"
  278. elif [ -f "$pflash_code" ]; then
  279. qemuopts="-drive if=pflash,format=raw,unit=0,readonly=on,file=$pflash_code $qemuopts"
  280. qemuopts="-drive if=pflash,format=raw,unit=1,snapshot=on,file=$pflash_vars $qemuopts"
  281. else
  282. echo "Firmware not found, please install either the appropriate Debian package or an appropriately named copy in the source directory." >&2
  283. exit 1
  284. fi
  285. fi
  286. qemuopts="-machine virt $qemuopts"
  287. disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
  288. serial_port=efi0
  289. ;;
  290. loongarch64-efi)
  291. qemu=qemu-system-loongarch64
  292. boot=hd
  293. console=console
  294. trim=1
  295. qemuopts="-machine virt -cpu la464-loongarch-cpu -smp 4 -nographic -m 3G \
  296. -bios QEMU_EFI.fd -L ${srcdir} -L /usr/share/edk2/loongarch64 \
  297. -L /usr/share/qemu-efi-loongarch64 $qemuopts"
  298. disk="device virtio-blk-pci,drive=hd1 -drive if=none,id=hd1,file="
  299. serial_port=
  300. ;;
  301. *)
  302. boot=hd
  303. qemu=qemu-system-i386
  304. console=console;;
  305. esac
  306. case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
  307. # Only add the RNG device for EFI platforms because we currently only
  308. # support Stack Smashing protection on EFI.
  309. *-efi)
  310. qemuopts="$qemuopts -device virtio-rng-pci" ;;
  311. esac
  312. timeout=${GRUB_SHELL_DEFAULT_TIMEOUT:-60}
  313. mkimage_extra_arg=
  314. debug=${GRUB_SHELL_DEFAULT_DEBUG:-$GRUB_TEST_DEFAULT_DEBUG}
  315. # Check the arguments.
  316. for option in "$@"; do
  317. case "$option" in
  318. -h | --help)
  319. usage
  320. exit 0 ;;
  321. -v | --version)
  322. echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
  323. exit 0 ;;
  324. --trim)
  325. trim=1 ;;
  326. --trim=*)
  327. trim=2
  328. trim_head=`echo "$option" | sed -e 's/--trim=//' -e 's/,/ /g'`
  329. ;;
  330. --no-trim)
  331. trim=0 ;;
  332. --debug)
  333. debug=$((debug+1)) ;;
  334. --debug=*)
  335. debug=$((`echo "$option" | sed -e 's/--debug=//'`)) ;;
  336. --modules=*)
  337. ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
  338. modules="$modules $ms" ;;
  339. --files=*)
  340. fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'`
  341. files="$files $fls" ;;
  342. --mkrescue-arg=*)
  343. mkr=`echo "$option" | sed -e 's/--mkrescue-arg=//' -e 's/,/ /g'`
  344. mkrescue_args="$mkrescue_args $mkr" ;;
  345. --pseries)
  346. qemu=qemu-system-ppc64
  347. serial_port=ieee1275/hvterm
  348. serial_null=
  349. qemuopts="$(echo $qemuopts | sed -E 's/-M [^ ]+//') -M pseries -no-reboot"
  350. trim=1
  351. pseries=y
  352. ;;
  353. --serial=*)
  354. serial_port=`echo "$option" | sed -e 's/--serial=//'`;;
  355. --qemu=*)
  356. qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;;
  357. --qemu-opts=*)
  358. qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
  359. qemuopts="$qemuopts $qs" ;;
  360. --disk=*)
  361. dsk=`echo "$option" | sed -e 's/--disk=//'`
  362. if [ ${grub_modinfo_platform} = emu ]; then
  363. echo "(hd$disk) $dsk" >> "$device_map"
  364. disk="$((disk+1))"
  365. else
  366. if [ "$disk" = error ]; then
  367. echo "Too many disks" 1>&2
  368. exit 1;
  369. fi
  370. qemuopts="$qemuopts -$disk$dsk"
  371. if [ "$disk" = "hda " ]; then
  372. disk="hdb ";
  373. elif [ "$disk" = "hdb " ]; then
  374. # CDROM is hdc
  375. disk="hdd "
  376. elif [ "$disk" = "hdd " ]; then
  377. # CDROM is hdc
  378. disk=error
  379. fi
  380. fi
  381. ;;
  382. --timeout=*)
  383. timeout=`echo "$option" | sed -e 's/--timeout=//'`
  384. ;;
  385. # Intentionally undocumented
  386. --grub-mkimage-extra)
  387. mkimage_extra_arg="$mkimage_extra_arg `argument $option "$@"`"; shift ;;
  388. --grub-mkimage-extra=*)
  389. mkimage_extra_arg="$mkimage_extra_arg `echo "$option" | sed 's/--grub-mkimage-extra=//'`" ;;
  390. --boot=*)
  391. dev=`echo "$option" | sed -e 's/--boot=//'`
  392. if [ "$dev" = "fd" ] ; then boot=fd;
  393. elif [ "$dev" = "hd" ] ; then boot=hd;
  394. elif [ "$dev" = "cd" ] ; then boot=cd;
  395. elif [ "$dev" = "net" ] ; then boot=net;
  396. elif [ "$dev" = "qemu" ] ; then boot=qemu;
  397. elif [ "$dev" = "coreboot" ] ; then boot=coreboot;
  398. elif [ "$dev" = "mips_qemu" ] ; then boot=mips_qemu;
  399. elif [ "$dev" = "mipsel_qemu" ] ; then boot=mipsel_qemu;
  400. elif [ "$dev" = "mipsel_fulong2e" ] ; then boot=mipsel_fulong2e;
  401. else
  402. echo "Unrecognized boot method \`$dev'" 1>&2
  403. usage
  404. exit 1
  405. fi ;;
  406. -*)
  407. echo "Unrecognized option \`$option'" 1>&2
  408. usage
  409. exit 1 ;;
  410. *)
  411. if [ "x${source}" != x ] ; then
  412. echo "too many parameters at the end" 1>&2
  413. usage
  414. exit 1
  415. fi
  416. source="${option}" ;;
  417. esac
  418. done
  419. [ "${debug:-0}" -gt 1 ] && set -x
  420. if [ "x${source}" = x ] ; then
  421. tmpfile="$work_directory/testcase.cfg"
  422. while read REPLY; do
  423. echo "$REPLY" >> ${tmpfile}
  424. done
  425. source=${tmpfile}
  426. fi
  427. cfgfile="$work_directory/grub.cfg"
  428. cat <<EOF >${cfgfile}
  429. grubshell=yes
  430. halt_cmd=${halt_cmd}
  431. export halt_cmd
  432. enable_progress_indicator=0
  433. export enable_progress_indicator
  434. if [ "${grub_modinfo_platform}" == efi ]; then
  435. gdbinfo
  436. fi
  437. EOF
  438. if [ "${grub_modinfo_platform}" != emu ]; then
  439. echo insmod serial >>${cfgfile}
  440. fi
  441. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ]; then
  442. echo insmod escc >>${cfgfile}
  443. fi
  444. if [ "${serial_port}" != "" ]; then
  445. echo "serial ${serial_port}" >>${cfgfile}
  446. term="serial_${serial_port}"
  447. else
  448. term=console
  449. fi
  450. cat <<EOF >>${cfgfile}
  451. terminfo -g 1024x1024 ${term} dumb
  452. terminal_input ${term}
  453. terminal_output ${term}
  454. EOF
  455. if [ $trim = 1 ]; then
  456. echo "echo; echo $trim_head" >>${cfgfile}
  457. fi
  458. rom_directory="$work_directory/rom_directory"
  459. mkdir -p "$rom_directory"
  460. for mod in ${modules}
  461. do
  462. echo "insmod ${mod}" >> ${cfgfile}
  463. done
  464. cat <<EOF >>${cfgfile}
  465. source "\$prefix/testcase.cfg"
  466. # Stop serial output to suppress "ACPI shutdown failed" error.
  467. EOF
  468. # Attempt to switch to console on i386-ieee1275 causes "screen not found" message
  469. if [ x$console != x ] && [ x"${grub_modinfo_target_cpu}-${grub_modinfo_platform}" != xi386-ieee1275 ]; then
  470. echo "terminal_output $console" >>${cfgfile}
  471. fi
  472. echo "${halt_cmd}" >>${cfgfile}
  473. test -z "$debug" || echo "GRUB script: ${cfgfile}" >&2
  474. test -z "$debug" || echo "GRUB testcase script: ${tmpfile}" >&2
  475. goutfile="$work_directory/grub-qemu.log"
  476. test -z "$debug" || echo "GRUB output log: ${goutfile}" >&2
  477. test -z "$debug" || echo "Boot device: ${boot}" >&2
  478. isofile="$work_directory/grub.iso"
  479. test -z "$debug" || echo "GRUB ISO file: ${isofile}" >&2
  480. test -z "$debug" || echo "GRUB ROM directory: ${rom_directory}" >&2
  481. if test -z "$debug"; then
  482. qemuopts="${qemuopts} -nographic -monitor file:/dev/null"
  483. # SeaBIOS 1.11.0 added support for VGA emulation over a serial port. If
  484. # this is configured in SeaBIOS, then -nographic causes some extra junk to
  485. # end up on the serial console, which interferes with our tests. This
  486. # workaround unfortunately causes qemu to issue a warning 'externally
  487. # provided fw_cfg item names should be prefixed with "opt/"', but there
  488. # doesn't seem to be a better option.
  489. #
  490. # SeaBIOS is used for i386, except on EFI.
  491. if [ ${grub_modinfo_target_cpu} == 'i386' ] && [ ${grub_modinfo_platform} != 'efi' ]; then
  492. qemuopts="${qemuopts} -fw_cfg name=etc/sercon-port,string=0"
  493. fi
  494. fi
  495. if [ x$boot != xnet ] && [ x$boot != xemu ]; then
  496. pkgdatadir="${builddir}" \
  497. exec_show_error "${builddir}/grub-mkrescue" \
  498. ${debug:+$([ "$debug" -gt 2 ] && echo -n "--verbose")} \
  499. "--output=${isofile}" \
  500. "--override-directory=${builddir}/grub-core" \
  501. --rom-directory="${rom_directory}" \
  502. --locale-directory="${srcdir}/po" \
  503. --themes-directory="${srcdir}/themes" \
  504. $mkimage_extra_arg ${mkrescue_args} \
  505. "/boot/grub/grub.cfg=${cfgfile}" "/boot/grub/testcase.cfg=${source}" \
  506. ${files} || exit $?
  507. fi
  508. if [ x$boot = xhd ]; then
  509. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
  510. device="device virtio-blk-device,drive=hd0 -drive if=none,id=hd0,file="
  511. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = loongarch64-efi ]; then
  512. device="device virtio-blk-pci,drive=grubdisk -drive if=none,id=grubdisk,file="
  513. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; then
  514. device="hdb "
  515. else
  516. device="hda "
  517. fi
  518. bootdev="-boot c"
  519. fi
  520. if [ x$boot = xcd ]; then
  521. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
  522. device="device virtio-blk-device,drive=cd0 -drive if=none,id=cd0,media=cdrom,file="
  523. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = loongarch64-efi ]; then
  524. device="device virtio-blk-pci,drive=grubcd -drive if=none,id=grubcd,media=cdrom,file="
  525. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ] && [ x$pseries != xy ] ; then
  526. device="-drive if=ide,media=cdrom,file="
  527. else
  528. device="cdrom "
  529. fi
  530. bootdev="-boot d"
  531. fi
  532. if [ x$boot = xfd ]; then
  533. device="fda "
  534. bootdev="-boot a"
  535. fi
  536. if [ x$boot = xqemu ]; then
  537. bootdev="-bios ${rom_directory}/qemu.img"
  538. device="cdrom "
  539. fi
  540. if [ x$boot = xmipsel_qemu ]; then
  541. bootdev="-kernel ${rom_directory}/mipsel-qemu_mips.elf"
  542. device="cdrom "
  543. fi
  544. if [ x$boot = xmipsel_fulong2e ]; then
  545. bootdev="-kernel ${rom_directory}/mipsel-loongson.elf -append machtype=lemote-fuloong-2e"
  546. device="cdrom "
  547. fi
  548. if [ x$boot = xmips_qemu ]; then
  549. bootdev="-kernel ${rom_directory}/mips-qemu_mips.elf"
  550. device="cdrom "
  551. fi
  552. if [ x$boot = xcoreboot ]; then
  553. imgfile="$work_directory/coreboot.img"
  554. cp "${GRUB_COREBOOT_ROM}" "${imgfile}"
  555. "${GRUB_CBFSTOOL}" "${imgfile}" add-payload -f "${rom_directory}/coreboot.elf" -n fallback/payload
  556. bootdev="-bios ${imgfile}"
  557. device="cdrom "
  558. test -z "$debug" || echo "Coreboot image: ${imgfile}" >&2
  559. fi
  560. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mipsel-arc ]; then
  561. case "$boot" in
  562. hd)
  563. bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/disk" ;;
  564. *)
  565. bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/cdrom";;
  566. esac
  567. fi
  568. do_trim ()
  569. {
  570. if [ $trim = 1 ] || [ $trim = 2 ]; then
  571. awk '{ if (have_head == 1) print $0; } /^'"$trim_head"'/ { have_head=1; }'
  572. else
  573. cat
  574. fi
  575. }
  576. copy_extra_files() {
  577. _destdir="$1"
  578. shift
  579. # FIXME support '=' in file names
  580. for _file in "$@"; do
  581. _target="${_file%=*}"
  582. _source="${_file#*=}"
  583. [ -n "$_source" ] || _source="$_target"
  584. _target="$_destdir/$_target"
  585. _targetdir="$(dirname "$_target")"
  586. [ -d "$_targetdir" ] || mkdir -p "$_targetdir"
  587. cp "$_source" "$_target"
  588. done
  589. }
  590. setup_qemu_logger() {
  591. cat < "$work_directory/qemu-pipe" | tr -d "\r" | tee "${goutfile}" | do_trim &
  592. }
  593. ret=0
  594. mkfifo "$work_directory/qemu-pipe"
  595. if [ x$boot = xnet ]; then
  596. netdir="$work_directory/netdir"
  597. mkdir -p "$netdir"
  598. pkgdatadir="${builddir}" "${builddir}/grub-mknetdir" "--grub-mkimage=${builddir}/grub-mkimage" "--directory=${builddir}/grub-core" "--net-directory=$netdir" ${mkrescue_args} > /dev/null
  599. cp "${cfgfile}" "$netdir/boot/grub/grub.cfg"
  600. cp "${source}" "$netdir/boot/grub/testcase.cfg"
  601. [ -z "$files" ] || copy_extra_files "$netdir" $files
  602. cat >"$work_directory/run.sh" <<EOF
  603. #! @BUILD_SHEBANG@
  604. GRUB_QEMU_OPTS=\${GRUB_QEMU_OPTS:-"$GRUB_QEMU_OPTS"}
  605. qemuopts="${qemuopts}"
  606. exec "${qemu}" \${qemuopts} \${GRUB_QEMU_OPTS} ${serial_null} -serial file:/dev/stdout -boot n -net "user,tftp=$netdir,bootfile=/boot/grub/${grub_modinfo_target_cpu}-${grub_modinfo_platform}/core.$netbootext" -net nic "\$@"
  607. EOF
  608. elif [ x$boot = xemu ]; then
  609. rootdir="$work_directory/rootdir"
  610. grubdir="$rootdir/boot/grub"
  611. mkdir -p "$grubdir/fonts"
  612. mkdir -p "$grubdir/themes"
  613. mkdir -p "$grubdir/locale"
  614. test -f "${builddir}/"unicode.pf2 && cp "${builddir}/"unicode.pf2 "$grubdir/fonts/unicode.pf2"
  615. cp -R "${srcdir}/themes/starfield" "$grubdir/themes/starfield"
  616. for file in "${srcdir}/po/"*.gmo; do
  617. if [ -f "$file" ]; then
  618. cp "$file" "$grubdir/locale/"
  619. fi
  620. done
  621. cp "${cfgfile}" "$grubdir/grub.cfg"
  622. cp "${source}" "$grubdir/testcase.cfg"
  623. [ -z "$files" ] || copy_extra_files "$rootdir" $files
  624. roottar="$work_directory/root.tar"
  625. (cd "$rootdir"; tar cf "$roottar" .)
  626. cat >"$work_directory/run.sh" <<EOF
  627. #! @BUILD_SHEBANG@
  628. SDIR=\$(realpath -e \${0%/*})
  629. exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub"
  630. EOF
  631. else
  632. cat >"$work_directory/run.sh" <<EOF
  633. #! @BUILD_SHEBANG@
  634. SDIR=\$(realpath -e \${0%/*})
  635. GRUB_QEMU_OPTS=\${GRUB_QEMU_OPTS:-"$GRUB_QEMU_OPTS"}
  636. qemuopts="${qemuopts}"
  637. cd "\$SDIR"
  638. exec "${qemu}" \${qemuopts} \${GRUB_QEMU_OPTS} ${serial_null} -serial file:/dev/stdout -${device}"\${SDIR}/${isofile##*/}" ${bootdev} "\$@"
  639. EOF
  640. fi
  641. if [ -f "$work_directory/run.sh" ]; then
  642. setup_qemu_logger
  643. chmod +x "$work_directory/run.sh"
  644. timeout -s KILL $timeout "$work_directory/run.sh" > "$work_directory/qemu-pipe" || ret=$?
  645. fi
  646. wait
  647. rm -f "$work_directory/qemu-pipe"
  648. if [ "$ret" -ne 0 ]; then
  649. # If QEMU failure, keep generated files to reproduce
  650. exit $ret
  651. fi
  652. if [ x$boot = xcoreboot ]; then
  653. test -n "$debug" || rm -f "${imgfile}"
  654. elif [ x$boot = xemu ]; then
  655. test -n "$debug" || rm -rf "$rootdir"
  656. test -n "$debug" || rm -f "$roottar"
  657. fi
  658. test -n "$debug" || rm -f "${isofile}"
  659. test -n "$debug" || rm -rf "${rom_directory}"
  660. test -n "$debug" || rm -f "${tmpfile}" "${cfgfile}" "${goutfile}"
  661. test -n "$debug" || rm -f "$work_directory/run.sh"
  662. exit $ret