4 # Run GRUB script in a Qemu instance
5 # Copyright (C) 2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 # Initialize some variables.
22 exec_prefix
="@exec_prefix@"
23 datarootdir
="@datarootdir@"
25 PACKAGE_NAME
=@PACKAGE_NAME@
26 PACKAGE_TARNAME
=@PACKAGE_TARNAME@
27 PACKAGE_VERSION
=@PACKAGE_VERSION@
29 # Force build directory components
30 PATH
="${builddir}:$PATH"
39 Usage: $0 [OPTION] [SOURCE]
40 Run GRUB script in a Qemu instance.
42 -h, --help print this message and exit
43 -v, --version print the version information and exit
44 --boot=[fd|hd|cd|net] boot method for Qemu instance
45 --modules=MODULES pre-load specified modules MODULES
46 --qemu=FILE Name of qemu binary
47 --disk=FILE Attach FILE as a disk
48 --qemu-opts=OPTIONS extra options to pass to Qemu instance
49 --files=FILES add files to the image
50 --mkrescue-arg=ARGS additional arguments to grub-mkrescue
51 --timeout=SECONDS set timeout
52 --trim trim firmware output
54 $0 runs input GRUB script or SOURCE file in a Qemu instance and prints
57 Report bugs to <bug-grub@gnu.org>.
61 .
"${builddir}/grub-core/modinfo.sh"
62 qemuopts
="${GRUB_QEMU_OPTS}"
68 case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
70 device_map
=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
74 # To skip "Welcome to GRUB" and color setttings
82 serial_null
="-serial null"
88 qemu
=qemu-system-sparc64
90 serial_port
=ieee1275
/ttya
92 qemuopts
="$qemuopts -no-reboot"
99 qemuopts
="$qemuopts -M mips"
104 qemu
=qemu-system-mips64
105 qemuopts
="$qemuopts -M indy"
106 serial_port
=arc
/serial
0/line0
112 qemu
=qemu-system-mips64el
113 qemuopts
="$qemuopts -M magnum -no-reboot"
114 serial_port
=arc
/multi
0/serial0
121 qemu
=qemu-system-mipsel
122 qemuopts
="$qemuopts -M mips"
127 qemu
=qemu-system-mips64el
128 qemuopts
="$qemuopts -M fulong2e"
134 qemu
=qemu-system-i386
139 qemu
=qemu-system-i386
144 qemu
=qemu-system-i386
151 qemu
=qemu-system-i386
156 qemu
=qemu-system-i386
162 qemu
=qemu-system-i386
168 qemu
=qemu-system-x86_64
175 qemu
=qemu-system-i386
182 # Check the arguments.
183 for option
in "$@"; do
189 echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
195 ms
=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
196 modules
="$modules $ms" ;;
198 fls
=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'`
199 files
="$files $fls" ;;
201 mkr
=`echo "$option" | sed -e 's/--mkrescue-arg=//' -e 's/,/ /g'`
202 mkrescue_args
="$mkrescue_args $mkr" ;;
204 qemu
=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;;
206 qemu
=qemu-system-ppc64
207 serial_port
=ieee1275
/hvterm
209 qemuopts
="$qemuopts -M pseries -no-reboot"
213 qs
=`echo "$option" | sed -e 's/--qemu-opts=//'`
214 qemuopts
="$qemuopts $qs" ;;
216 dsk
=`echo "$option" | sed -e 's/--disk=//'`
217 if [ ${grub_modinfo_platform} = emu
]; then
218 echo "(hd$disk) $dsk" >> "$device_map"
221 if [ $disk = error
]; then
222 echo "Too many disks" 1>&2
225 qemuopts
="$qemuopts -$disk $dsk"
226 if [ "$disk" = hda
]; then
228 elif [ "$disk" = hdb
]; then
231 elif [ "$disk" = hdd
]; then
238 timeout
=`echo "$option" | sed -e 's/--timeout=//'`
241 # Intentionally undocumented
242 --grub-mkimage-extra)
243 mkimage_extra_arg
="$mkimage_extra_arg `argument $option "$@
"`"; shift ;;
244 --grub-mkimage-extra=*)
245 mkimage_extra_arg
="$mkimage_extra_arg `echo "$option" | sed 's/--grub-mkimage-extra=//'`" ;;
248 dev
=`echo "$option" | sed -e 's/--boot=//'`
249 if [ "$dev" = "fd" ] ; then boot
=fd
;
250 elif [ "$dev" = "hd" ] ; then boot
=hd
;
251 elif [ "$dev" = "cd" ] ; then boot
=cd;
252 elif [ "$dev" = "net" ] ; then boot
=net
;
253 elif [ "$dev" = "qemu" ] ; then boot
=qemu
;
254 elif [ "$dev" = "coreboot" ] ; then boot
=coreboot
;
255 elif [ "$dev" = "mips_qemu" ] ; then boot
=mips_qemu
;
256 elif [ "$dev" = "mipsel_qemu" ] ; then boot
=mipsel_qemu
;
257 elif [ "$dev" = "mipsel_fulong2e" ] ; then boot
=mipsel_fulong2e
;
259 echo "Unrecognized boot method \`$dev'" 1>&2
264 echo "Unrecognized option \`$option'" 1>&2
268 if [ "x${source}" != x
] ; then
269 echo "too many parameters at the end" 1>&2
273 source="${option}" ;;
277 if [ "x${source}" = x
] ; then
278 tmpfile
=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
280 echo "$REPLY" >> ${tmpfile}
285 cfgfile
=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
286 cat <<EOF >${cfgfile}
291 if [ "${grub_modinfo_platform}" != emu
]; then
292 echo insmod serial
>>${cfgfile}
295 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275
]; then
296 echo insmod escc
>>${cfgfile}
298 if [ "${grub_modinfo_platform}" != emu
]; then
299 echo "serial ${serial_port}" >>${cfgfile}
300 term
="serial_${serial_port}"
305 cat <<EOF >>${cfgfile}
306 terminfo -g 255x255 ${term} dumb
307 terminal_input ${term}
308 terminal_output ${term}
311 trim_head
=664cbea8-132f-4770-8aa4-1696d59ac35c
313 if [ $trim = 1 ]; then
314 echo "echo $trim_head" >>${cfgfile}
317 rom_directory
=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
319 for mod
in ${modules}
321 echo "insmod ${mod}" >> ${cfgfile}
324 cat <<EOF >>${cfgfile}
325 source "\$prefix/testcase.cfg"
326 # Stop serial output to suppress "ACPI shutdown failed" error.
328 if [ x
$console != x
]; then
329 echo "terminal_output $console" >>${cfgfile}
331 echo "${halt_cmd}" >>${cfgfile}
333 isofile
=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
334 if [ x
$boot != xnet
] && [ x
$boot != xemu
]; then
335 pkgdatadir
="@builddir@" sh
"@builddir@/grub-mkrescue" "--grub-mkimage=${builddir}/grub-mkimage" "--grub-render-label=${builddir}/grub-render-label" "--output=${isofile}" "--override-directory=${builddir}/grub-core" \
336 --rom-directory="${rom_directory}" "--grub-mkimage-extra=$mkimage_extra_arg" ${mkrescue_args} \
337 "/boot/grub/grub.cfg=${cfgfile}" "/boot/grub/testcase.cfg=${source}" \
338 ${files} >/dev
/null
2>&1
340 if [ x
$boot = xhd
]; then
341 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc
]; then
348 if [ x
$boot = xcd
]; then
352 if [ x
$boot = xfd
]; then
357 if [ x
$boot = xqemu
]; then
358 bootdev
="-bios ${rom_directory}/qemu.img"
362 if [ x
$boot = xmipsel_qemu
]; then
363 bootdev
="-kernel ${rom_directory}/mipsel-qemu_mips.elf"
367 if [ x
$boot = xmipsel_fulong2e
]; then
368 bootdev
="-kernel ${rom_directory}/mipsel-loongson.elf -append machtype=lemote-fuloong-2e"
372 if [ x
$boot = xmips_qemu
]; then
373 bootdev
="-kernel ${rom_directory}/mips-qemu_mips.elf"
377 if [ x
$boot = xcoreboot
]; then
378 imgfile
=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
379 cp "${GRUB_COREBOOT_ROM}" "${imgfile}"
380 "${GRUB_CBFSTOOL}" "${imgfile}" add-payload "${rom_directory}/coreboot.elf" fallback
/payload
381 bootdev
="-bios ${imgfile}"
385 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mipsel-arc
]; then
388 bootdev
="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/disk" ;;
390 bootdev
="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/cdrom";;
396 if [ $trim = 1 ]; then
397 awk '{ if (have_head == 1) print $0; } /664cbea8-132f-4770-8aa4-1696d59ac35c/ { have_head=1; }'
403 if [ x
$boot = xnet
]; then
404 netdir
=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ||
exit 1
405 pkgdatadir
="@builddir@" sh
"@builddir@/grub-mknetdir" "--grub-mkimage=${builddir}/grub-mkimage" "--directory=${builddir}/grub-core" "--net-directory=$netdir" ${mkrescue_args} > /dev/null
406 cp "${cfgfile}" "$netdir/boot
/grub
/grub.cfg
"
407 cp "${source}" "$netdir/boot
/grub
/testcase.cfg
"
408 timeout -s KILL $timeout "${qemu}" ${qemuopts} -nographic ${serial_null} -serial file:/dev/stdout -monitor file:/dev/null -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
409 elif [ x$boot = xemu ]; then
410 grubdir="$
(mktemp
-d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX")"
411 mkdir -p "$grubdir/fonts
"
412 cp "@builddir@
/"unicode.pf2 "$grubdir/fonts
/unicode.pf2
"
413 cp "${cfgfile}" "$grubdir/grub.cfg
"
414 cp "${source}" "$grubdir/testcase.cfg
"
415 @builddir@/grub-core/grub-emu -m "$device_map" -d "$grubdir" | tr -d "\r" | do_trim
417 timeout -s KILL $timeout "${qemu}" ${qemuopts} -nographic ${serial_null} -serial file:/dev/stdout -monitor file:/dev/null -${device} ${isofile} ${bootdev} |
cat |
tr -d "\r" | do_trim
419 rm -f "${isofile}" "${imgfile}"
420 rm -rf "${rom_directory}"
421 if [ x
$boot = xcoreboot
]; then
425 rm -f "${tmpfile}" "${cfgfile}"