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