grub-shell.in 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. # Usage: usage
  32. # Print the usage.
  33. usage () {
  34. cat <<EOF
  35. Usage: $0 [OPTION] [SOURCE]
  36. Run GRUB script in a Qemu instance.
  37. -h, --help print this message and exit
  38. -v, --version print the version information and exit
  39. --boot=[fd|hd|cd|net] boot method for Qemu instance
  40. --modules=MODULES pre-load specified modules MODULES
  41. --qemu=FILE Name of qemu binary
  42. --disk=FILE Attach FILE as a disk
  43. --qemu-opts=OPTIONS extra options to pass to Qemu instance
  44. --files=FILES add files to the image
  45. --mkrescue-arg=ARGS additional arguments to grub-mkrescue
  46. --timeout=SECONDS set timeout
  47. --trim trim firmware output
  48. $0 runs input GRUB script or SOURCE file in a Qemu instance and prints
  49. its output.
  50. Report bugs to <bug-grub@gnu.org>.
  51. EOF
  52. }
  53. . "${builddir}/grub-core/modinfo.sh"
  54. qemuopts="${GRUB_QEMU_OPTS}"
  55. serial_port=com0
  56. serial_null=
  57. halt_cmd=halt
  58. pseries=n
  59. disk="hda "
  60. case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
  61. *-emu)
  62. device_map=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  63. boot=emu
  64. console=console
  65. disk=0
  66. # To skip "Welcome to GRUB" and color setttings
  67. trim=1
  68. serial_port=
  69. ;;
  70. powerpc-ieee1275)
  71. boot=hd
  72. qemu=qemu-system-ppc
  73. console=console
  74. serial_port=escc-ch-b
  75. serial_null="-serial null"
  76. netbootext=elf
  77. trim=1
  78. ;;
  79. sparc64-ieee1275)
  80. boot=cd
  81. qemu=qemu-system-sparc64
  82. console=
  83. serial_port=ieee1275/ttya
  84. trim=1
  85. qemuopts="$qemuopts -no-reboot"
  86. halt_cmd=reboot
  87. ;;
  88. mips-qemu_mips)
  89. boot=mips_qemu
  90. qemu=qemu-system-mips
  91. qemuopts="-M mips $qemuopts"
  92. console=vga_text
  93. ;;
  94. mips-arc)
  95. boot=cd
  96. qemu=qemu-system-mips64
  97. qemuopts="-M indy $qemuopts"
  98. serial_port=arc/serial0/line0
  99. console=
  100. trim=1
  101. ;;
  102. mipsel-arc)
  103. boot=cd
  104. qemu=qemu-system-mips64el
  105. qemuopts="-M magnum $qemuopts -no-reboot"
  106. serial_port=arc/multi0/serial0
  107. console=console
  108. halt_cmd=reboot
  109. trim=1
  110. ;;
  111. mipsel-qemu_mips)
  112. boot=mipsel_qemu
  113. qemu=qemu-system-mipsel
  114. qemuopts="-M mips $qemuopts"
  115. console=vga_text
  116. ;;
  117. mipsel-loongson)
  118. boot=mipsel_fulong2e
  119. qemu=qemu-system-mips64el
  120. qemuopts="-M fulong2e $qemuopts"
  121. console=
  122. trim=1
  123. ;;
  124. i386-coreboot)
  125. boot=coreboot
  126. qemu=qemu-system-i386
  127. console=vga_text
  128. ;;
  129. i386-multiboot)
  130. boot=cd
  131. qemu=qemu-system-i386
  132. console=vga_text;;
  133. i386-ieee1275)
  134. boot=hd
  135. qemu=qemu-system-i386
  136. console=console
  137. trim=1
  138. disk="hdb "
  139. ;;
  140. i386-qemu)
  141. boot=qemu
  142. qemu=qemu-system-i386
  143. console=vga_text;;
  144. i386-pc)
  145. boot=cd
  146. qemu=qemu-system-i386
  147. console=console
  148. netbootext=0
  149. ;;
  150. i386-efi)
  151. qemu=qemu-system-i386
  152. boot=cd
  153. console=console
  154. trim=1
  155. qemuopts="-bios OVMF-ia32.fd $qemuopts"
  156. ;;
  157. x86_64-efi)
  158. qemu=qemu-system-x86_64
  159. boot=cd
  160. console=console
  161. trim=1
  162. qemuopts="-bios OVMF.fd $qemuopts"
  163. ;;
  164. arm64-efi)
  165. qemu=qemu-system-aarch64
  166. boot=hd
  167. console=console
  168. trim=1
  169. qemuopts="-machine virt -cpu cortex-a57 -bios /usr/share/qemu-efi/QEMU_EFI.fd $qemuopts"
  170. disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
  171. serial_port=
  172. ;;
  173. arm-efi)
  174. qemu=qemu-system-arm
  175. boot=hd
  176. console=console
  177. trim=1
  178. qemuopts="-machine virt -bios /usr/share/ovmf-arm/QEMU_EFI.fd $qemuopts"
  179. disk="device virtio-blk-device,drive=hd1 -drive if=none,id=hd1,file="
  180. serial_port=efi0
  181. ;;
  182. *)
  183. boot=hd
  184. qemu=qemu-system-i386
  185. console=console;;
  186. esac
  187. timeout=60
  188. mkimage_extra_arg=
  189. # Check the arguments.
  190. for option in "$@"; do
  191. case "$option" in
  192. -h | --help)
  193. usage
  194. exit 0 ;;
  195. -v | --version)
  196. echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
  197. exit 0 ;;
  198. --trim)
  199. trim=1
  200. ;;
  201. --debug)
  202. debug=1 ;;
  203. --modules=*)
  204. ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
  205. modules="$modules $ms" ;;
  206. --files=*)
  207. fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'`
  208. files="$files $fls" ;;
  209. --mkrescue-arg=*)
  210. mkr=`echo "$option" | sed -e 's/--mkrescue-arg=//' -e 's/,/ /g'`
  211. mkrescue_args="$mkrescue_args $mkr" ;;
  212. --qemu=*)
  213. qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;;
  214. --pseries)
  215. qemu=qemu-system-ppc64
  216. serial_port=ieee1275/hvterm
  217. serial_null=
  218. qemuopts="$qemuopts -M pseries -no-reboot"
  219. trim=1
  220. pseries=y
  221. ;;
  222. --qemu-opts=*)
  223. qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
  224. qemuopts="$qemuopts $qs" ;;
  225. --disk=*)
  226. dsk=`echo "$option" | sed -e 's/--disk=//'`
  227. if [ ${grub_modinfo_platform} = emu ]; then
  228. echo "(hd$disk) $dsk" >> "$device_map"
  229. disk="$((disk+1))"
  230. else
  231. if [ "$disk" = error ]; then
  232. echo "Too many disks" 1>&2
  233. exit 1;
  234. fi
  235. qemuopts="$qemuopts -$disk$dsk"
  236. if [ "$disk" = "hda " ]; then
  237. disk="hdb ";
  238. elif [ "$disk" = "hdb " ]; then
  239. # CDROM is hdc
  240. disk="hdd "
  241. elif [ "$disk" = "hdd " ]; then
  242. # CDROM is hdc
  243. disk=error
  244. fi
  245. fi
  246. ;;
  247. --timeout=*)
  248. timeout=`echo "$option" | sed -e 's/--timeout=//'`
  249. ;;
  250. # Intentionally undocumented
  251. --grub-mkimage-extra)
  252. mkimage_extra_arg="$mkimage_extra_arg `argument $option "$@"`"; shift ;;
  253. --grub-mkimage-extra=*)
  254. mkimage_extra_arg="$mkimage_extra_arg `echo "$option" | sed 's/--grub-mkimage-extra=//'`" ;;
  255. --boot=*)
  256. dev=`echo "$option" | sed -e 's/--boot=//'`
  257. if [ "$dev" = "fd" ] ; then boot=fd;
  258. elif [ "$dev" = "hd" ] ; then boot=hd;
  259. elif [ "$dev" = "cd" ] ; then boot=cd;
  260. elif [ "$dev" = "net" ] ; then boot=net;
  261. elif [ "$dev" = "qemu" ] ; then boot=qemu;
  262. elif [ "$dev" = "coreboot" ] ; then boot=coreboot;
  263. elif [ "$dev" = "mips_qemu" ] ; then boot=mips_qemu;
  264. elif [ "$dev" = "mipsel_qemu" ] ; then boot=mipsel_qemu;
  265. elif [ "$dev" = "mipsel_fulong2e" ] ; then boot=mipsel_fulong2e;
  266. else
  267. echo "Unrecognized boot method \`$dev'" 1>&2
  268. usage
  269. exit 1
  270. fi ;;
  271. -*)
  272. echo "Unrecognized option \`$option'" 1>&2
  273. usage
  274. exit 1 ;;
  275. *)
  276. if [ "x${source}" != x ] ; then
  277. echo "too many parameters at the end" 1>&2
  278. usage
  279. exit 1
  280. fi
  281. source="${option}" ;;
  282. esac
  283. done
  284. if [ "x${source}" = x ] ; then
  285. tmpfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  286. while read REPLY; do
  287. echo "$REPLY" >> ${tmpfile}
  288. done
  289. source=${tmpfile}
  290. fi
  291. cfgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  292. cat <<EOF >${cfgfile}
  293. grubshell=yes
  294. enable_progress_indicator=0
  295. export enable_progress_indicator
  296. EOF
  297. if [ "${grub_modinfo_platform}" != emu ]; then
  298. echo insmod serial >>${cfgfile}
  299. fi
  300. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ]; then
  301. echo insmod escc >>${cfgfile}
  302. fi
  303. if [ "${serial_port}" != "" ]; then
  304. echo "serial ${serial_port}" >>${cfgfile}
  305. term="serial_${serial_port}"
  306. else
  307. term=console
  308. fi
  309. cat <<EOF >>${cfgfile}
  310. terminfo -g 1024x1024 ${term} dumb
  311. terminal_input ${term}
  312. terminal_output ${term}
  313. EOF
  314. trim_head=664cbea8-132f-4770-8aa4-1696d59ac35c
  315. if [ $trim = 1 ]; then
  316. echo "echo $trim_head" >>${cfgfile}
  317. fi
  318. rom_directory=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  319. for mod in ${modules}
  320. do
  321. echo "insmod ${mod}" >> ${cfgfile}
  322. done
  323. cat <<EOF >>${cfgfile}
  324. source "\$prefix/testcase.cfg"
  325. # Stop serial output to suppress "ACPI shutdown failed" error.
  326. EOF
  327. # Attempt to switch to console on i386-ieee1275 causes "screen not found" message
  328. if [ x$console != x ] && [ x"${grub_modinfo_target_cpu}-${grub_modinfo_platform}" != xi386-ieee1275 ]; then
  329. echo "terminal_output $console" >>${cfgfile}
  330. fi
  331. echo "${halt_cmd}" >>${cfgfile}
  332. test -z "$debug" || echo "GRUB script: ${cfgfile}" >&2
  333. test -z "$debug" || echo "GRUB testcase script: ${tmpfile}" >&2
  334. test -z "$debug" || echo "Boot device: ${boot}" >&2
  335. isofile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  336. test -z "$debug" || echo "GRUB ISO file: ${isofile}" >&2
  337. test -z "$debug" || echo "GRUB ROM directory: ${rom_directory}" >&2
  338. if test -z "$debug"; then
  339. qemuopts="${qemuopts} -nographic -monitor file:/dev/null"
  340. fi
  341. if [ x$boot != xnet ] && [ x$boot != xemu ]; then
  342. pkgdatadir="@builddir@" "@builddir@/grub-mkrescue" "--output=${isofile}" "--override-directory=${builddir}/grub-core" \
  343. --rom-directory="${rom_directory}" \
  344. --locale-directory="@srcdir@/po" \
  345. --themes-directory="@srcdir@/themes" \
  346. $mkimage_extra_arg ${mkrescue_args} \
  347. "/boot/grub/grub.cfg=${cfgfile}" "/boot/grub/testcase.cfg=${source}" \
  348. ${files} >/dev/null 2>&1
  349. fi
  350. if [ x$boot = xhd ]; then
  351. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
  352. device="device virtio-blk-device,drive=hd0 -drive if=none,id=hd0,file="
  353. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; then
  354. device="hdb "
  355. else
  356. device="hda "
  357. fi
  358. bootdev="-boot c"
  359. fi
  360. if [ x$boot = xcd ]; then
  361. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm64-efi ] || [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = arm-efi ]; then
  362. device="device virtio-blk-device,drive=cd0 -drive if=none,id=cd0,media=cdrom,file="
  363. elif [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ] && [ x$pseries != xy ] ; then
  364. device="-drive if=ide,media=cdrom,file="
  365. else
  366. device="cdrom "
  367. fi
  368. bootdev="-boot d"
  369. fi
  370. if [ x$boot = xfd ]; then
  371. device="fda "
  372. bootdev="-boot a"
  373. fi
  374. if [ x$boot = xqemu ]; then
  375. bootdev="-bios ${rom_directory}/qemu.img"
  376. device="cdrom "
  377. fi
  378. if [ x$boot = xmipsel_qemu ]; then
  379. bootdev="-kernel ${rom_directory}/mipsel-qemu_mips.elf"
  380. device="cdrom "
  381. fi
  382. if [ x$boot = xmipsel_fulong2e ]; then
  383. bootdev="-kernel ${rom_directory}/mipsel-loongson.elf -append machtype=lemote-fuloong-2e"
  384. device="cdrom "
  385. fi
  386. if [ x$boot = xmips_qemu ]; then
  387. bootdev="-kernel ${rom_directory}/mips-qemu_mips.elf"
  388. device="cdrom "
  389. fi
  390. if [ x$boot = xcoreboot ]; then
  391. imgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  392. cp "${GRUB_COREBOOT_ROM}" "${imgfile}"
  393. "${GRUB_CBFSTOOL}" "${imgfile}" add-payload -f "${rom_directory}/coreboot.elf" -n fallback/payload
  394. bootdev="-bios ${imgfile}"
  395. device="cdrom "
  396. test -z "$debug" || echo "Coreboot image: ${imgfile}" >&2
  397. fi
  398. if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mipsel-arc ]; then
  399. case "$boot" in
  400. hd)
  401. bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/disk" ;;
  402. *)
  403. bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/cdrom";;
  404. esac
  405. fi
  406. do_trim ()
  407. {
  408. if [ $trim = 1 ]; then
  409. awk '{ if (have_head == 1) print $0; } /664cbea8-132f-4770-8aa4-1696d59ac35c/ { have_head=1; }'
  410. else
  411. cat
  412. fi
  413. }
  414. copy_extra_files() {
  415. _destdir="$1"
  416. shift
  417. # FIXME support '=' in file names
  418. for _file in "$@"; do
  419. _target="${_file%=*}"
  420. _source="${_file#*=}"
  421. [ -n "$_source" ] || _source="$_target"
  422. _target="$_destdir/$_target"
  423. _targetdir="$(dirname "$_target")"
  424. [ -d "$_targetdir" ] || mkdir -p "$_targetdir"
  425. cp "$_source" "$_target"
  426. done
  427. }
  428. if [ x$boot = xnet ]; then
  429. netdir=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
  430. pkgdatadir="@builddir@" "@builddir@/grub-mknetdir" "--grub-mkimage=${builddir}/grub-mkimage" "--directory=${builddir}/grub-core" "--net-directory=$netdir" ${mkrescue_args} > /dev/null
  431. cp "${cfgfile}" "$netdir/boot/grub/grub.cfg"
  432. cp "${source}" "$netdir/boot/grub/testcase.cfg"
  433. [ -z "$files" ] || copy_extra_files "$netdir" $files
  434. timeout -s KILL $timeout "${qemu}" ${qemuopts} ${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 | cat | tr -d "\r" | do_trim
  435. elif [ x$boot = xemu ]; then
  436. rootdir="$(mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")"
  437. grubdir="$rootdir/boot/grub"
  438. mkdir -p "$grubdir/fonts"
  439. mkdir -p "$grubdir/themes"
  440. mkdir -p "$grubdir/locale"
  441. test -f "@builddir@/"unicode.pf2 && cp "@builddir@/"unicode.pf2 "$grubdir/fonts/unicode.pf2"
  442. cp -R "@srcdir@/themes/starfield" "$grubdir/themes/starfield"
  443. for file in "@srcdir@/po/"*.gmo; do
  444. if [ -f "$file" ]; then
  445. cp "$file" "$grubdir/locale/"
  446. fi
  447. done
  448. cp "${cfgfile}" "$grubdir/grub.cfg"
  449. cp "${source}" "$grubdir/testcase.cfg"
  450. [ -z "$files" ] || copy_extra_files "$rootdir" $files
  451. roottar="$(mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")"
  452. (cd "$rootdir"; tar cf "$roottar" .)
  453. @builddir@/grub-core/grub-emu -m "$device_map" --memdisk "$roottar" -r memdisk -d "/boot/grub" | tr -d "\r" | do_trim
  454. test -n "$debug" || rm -rf "$rootdir"
  455. test -n "$debug" || rm -f "$roottar"
  456. else
  457. timeout -s KILL $timeout "${qemu}" ${qemuopts} ${serial_null} -serial file:/dev/stdout -${device}"${isofile}" ${bootdev} | cat | tr -d "\r" | do_trim
  458. fi
  459. if [ x$boot = xcoreboot ]; then
  460. test -n "$debug" || rm -f "${imgfile}"
  461. fi
  462. test -n "$debug" || rm -f "${isofile}"
  463. test -n "$debug" || rm -rf "${rom_directory}"
  464. test -n "$debug" || rm -f "${tmpfile}" "${cfgfile}"
  465. exit 0