]> git.proxmox.com Git - grub2.git/blob - tests/util/grub-shell.in
First automated video test (running videotest and comparing results)
[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 PACKAGE_NAME=@PACKAGE_NAME@
26 PACKAGE_TARNAME=@PACKAGE_TARNAME@
27 PACKAGE_VERSION=@PACKAGE_VERSION@
28
29 # Force build directory components
30 PATH="${builddir}:$PATH"
31 export PATH
32
33 trim=0
34
35 # Usage: usage
36 # Print the usage.
37 usage () {
38 cat <<EOF
39 Usage: $0 [OPTION] [SOURCE]
40 Run GRUB script in a Qemu instance.
41
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
53
54 $0 runs input GRUB script or SOURCE file in a Qemu instance and prints
55 its output.
56
57 Report bugs to <bug-grub@gnu.org>.
58 EOF
59 }
60
61 . "${builddir}/grub-core/modinfo.sh"
62 qemuopts="${GRUB_QEMU_OPTS}"
63 serial_port=com0
64 serial_null=
65 halt_cmd=halt
66 pseries=n
67 disk=hda
68 case "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" in
69 *-emu)
70 device_map=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
71 boot=emu
72 console=console
73 disk=0
74 # To skip "Welcome to GRUB" and color setttings
75 trim=1
76 ;;
77 powerpc-ieee1275)
78 boot=cd
79 qemu=qemu-system-ppc
80 console=console
81 serial_port=escc-ch-a
82 serial_null="-serial null"
83 netbootext=elf
84 ;;
85
86 sparc64-ieee1275)
87 boot=cd
88 qemu=qemu-system-sparc64
89 console=
90 serial_port=ieee1275/ttya
91 trim=1
92 qemuopts="$qemuopts -no-reboot"
93 halt_cmd=reboot
94 ;;
95
96 mips-qemu_mips)
97 boot=mips_qemu
98 qemu=qemu-system-mips
99 qemuopts="$qemuopts -M mips"
100 console=vga_text
101 ;;
102 mips-arc)
103 boot=cd
104 qemu=qemu-system-mips64
105 qemuopts="$qemuopts -M indy"
106 serial_port=arc/serial0/line0
107 console=
108 trim=1
109 ;;
110 mipsel-arc)
111 boot=cd
112 qemu=qemu-system-mips64el
113 qemuopts="$qemuopts -M magnum -no-reboot"
114 serial_port=arc/multi0/serial0
115 console=console
116 halt_cmd=reboot
117 trim=1
118 ;;
119 mipsel-qemu_mips)
120 boot=mipsel_qemu
121 qemu=qemu-system-mipsel
122 qemuopts="$qemuopts -M mips"
123 console=vga_text
124 ;;
125 mipsel-loongson)
126 boot=mipsel_fulong2e
127 qemu=qemu-system-mips64el
128 qemuopts="$qemuopts -M fulong2e"
129 console=
130 trim=1
131 ;;
132 i386-coreboot)
133 boot=coreboot
134 qemu=qemu-system-i386
135 console=vga_text
136 ;;
137 i386-multiboot)
138 boot=cd
139 qemu=qemu-system-i386
140 console=vga_text;;
141
142 i386-ieee1275)
143 boot=hd
144 qemu=qemu-system-i386
145 console=console
146 trim=1
147 disk=hdb
148 ;;
149 i386-qemu)
150 boot=qemu
151 qemu=qemu-system-i386
152 console=vga_text;;
153
154 i386-pc)
155 boot=cd
156 qemu=qemu-system-i386
157 console=console
158 netbootext=0
159 ;;
160
161 i386-efi)
162 qemu=qemu-system-i386
163 boot=cd
164 console=console
165 trim=1
166 ;;
167 x86_64-efi)
168 qemu=qemu-system-x86_64
169 boot=cd
170 console=console
171 trim=1
172 ;;
173 *)
174 boot=hd
175 qemu=qemu-system-i386
176 console=console;;
177 esac
178
179 timeout=60
180 mkimage_extra_arg=
181
182 # Check the arguments.
183 for option in "$@"; do
184 case "$option" in
185 -h | --help)
186 usage
187 exit 0 ;;
188 -v | --version)
189 echo "$0 (GNU GRUB ${PACKAGE_VERSION})"
190 exit 0 ;;
191 --trim)
192 trim=1
193 ;;
194 --modules=*)
195 ms=`echo "$option" | sed -e 's/--modules=//' -e 's/,/ /g'`
196 modules="$modules $ms" ;;
197 --files=*)
198 fls=`echo "$option" | sed -e 's/--files=//' -e 's/,/ /g'`
199 files="$files $fls" ;;
200 --mkrescue-arg=*)
201 mkr=`echo "$option" | sed -e 's/--mkrescue-arg=//' -e 's/,/ /g'`
202 mkrescue_args="$mkrescue_args $mkr" ;;
203 --qemu=*)
204 qemu=`echo "$option" | sed -e 's/--qemu=//' -e 's/,/ /g'`;;
205 --pseries)
206 qemu=qemu-system-ppc64
207 serial_port=ieee1275/hvterm
208 serial_null=
209 qemuopts="$qemuopts -M pseries -no-reboot"
210 trim=1
211 ;;
212 --qemu-opts=*)
213 qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
214 qemuopts="$qemuopts $qs" ;;
215 --disk=*)
216 dsk=`echo "$option" | sed -e 's/--disk=//'`
217 if [ ${grub_modinfo_platform} = emu ]; then
218 echo "(hd$disk) $dsk" >> "$device_map"
219 disk="$((disk+1))"
220 else
221 if [ $disk = error ]; then
222 echo "Too many disks" 1>&2
223 exit 1;
224 fi
225 qemuopts="$qemuopts -$disk $dsk"
226 if [ "$disk" = hda ]; then
227 disk=hdb;
228 elif [ "$disk" = hdb ]; then
229 # CDROM is hdc
230 disk=hdd
231 elif [ "$disk" = hdd ]; then
232 # CDROM is hdc
233 disk=error
234 fi
235 fi
236 ;;
237 --timeout=*)
238 timeout=`echo "$option" | sed -e 's/--timeout=//'`
239 ;;
240
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=//'`" ;;
246
247 --boot=*)
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;
258 else
259 echo "Unrecognized boot method \`$dev'" 1>&2
260 usage
261 exit 1
262 fi ;;
263 -*)
264 echo "Unrecognized option \`$option'" 1>&2
265 usage
266 exit 1 ;;
267 *)
268 if [ "x${source}" != x ] ; then
269 echo "too many parameters at the end" 1>&2
270 usage
271 exit 1
272 fi
273 source="${option}" ;;
274 esac
275 done
276
277 if [ "x${source}" = x ] ; then
278 tmpfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
279 while read REPLY; do
280 echo "$REPLY" >> ${tmpfile}
281 done
282 source=${tmpfile}
283 fi
284
285 cfgfile=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
286 cat <<EOF >${cfgfile}
287 grubshell=yes
288 EOF
289
290
291 if [ "${grub_modinfo_platform}" != emu ]; then
292 echo insmod serial >>${cfgfile}
293 fi
294
295 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = powerpc-ieee1275 ]; then
296 echo insmod escc >>${cfgfile}
297 fi
298 if [ "${grub_modinfo_platform}" != emu ]; then
299 echo "serial ${serial_port}" >>${cfgfile}
300 term="serial_${serial_port}"
301 else
302 term=console
303 fi
304
305 cat <<EOF >>${cfgfile}
306 terminfo -g 255x255 ${term} dumb
307 terminal_input ${term}
308 terminal_output ${term}
309 EOF
310
311 trim_head=664cbea8-132f-4770-8aa4-1696d59ac35c
312
313 if [ $trim = 1 ]; then
314 echo "echo $trim_head" >>${cfgfile}
315 fi
316
317 rom_directory=`mktemp -d "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` || exit 1
318
319 for mod in ${modules}
320 do
321 echo "insmod ${mod}" >> ${cfgfile}
322 done
323
324 cat <<EOF >>${cfgfile}
325 source "\$prefix/testcase.cfg"
326 # Stop serial output to suppress "ACPI shutdown failed" error.
327 EOF
328 if [ x$console != x ]; then
329 echo "terminal_output $console" >>${cfgfile}
330 fi
331 echo "${halt_cmd}" >>${cfgfile}
332
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
339 fi
340 if [ x$boot = xhd ]; then
341 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mips-arc ]; then
342 device=hdb
343 else
344 device=hda
345 fi
346 bootdev="-boot c"
347 fi
348 if [ x$boot = xcd ]; then
349 device=cdrom
350 bootdev="-boot d"
351 fi
352 if [ x$boot = xfd ]; then
353 device=fda
354 bootdev="-boot a"
355 fi
356
357 if [ x$boot = xqemu ]; then
358 bootdev="-bios ${rom_directory}/qemu.img"
359 device=cdrom
360 fi
361
362 if [ x$boot = xmipsel_qemu ]; then
363 bootdev="-kernel ${rom_directory}/mipsel-qemu_mips.elf"
364 device=cdrom
365 fi
366
367 if [ x$boot = xmipsel_fulong2e ]; then
368 bootdev="-kernel ${rom_directory}/mipsel-loongson.elf -append machtype=lemote-fuloong-2e"
369 device=cdrom
370 fi
371
372 if [ x$boot = xmips_qemu ]; then
373 bootdev="-kernel ${rom_directory}/mips-qemu_mips.elf"
374 device=cdrom
375 fi
376
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}"
382 device=cdrom
383 fi
384
385 if [ "${grub_modinfo_target_cpu}-${grub_modinfo_platform}" = mipsel-arc ]; then
386 case "$boot" in
387 hd)
388 bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/disk" ;;
389 *)
390 bootdev="-global ds1225y.filename=$GRUB_QEMU_MAGNUM_NVRAM_DIR/cdrom";;
391 esac
392 fi
393
394 do_trim ()
395 {
396 if [ $trim = 1 ]; then
397 awk '{ if (have_head == 1) print $0; } /664cbea8-132f-4770-8aa4-1696d59ac35c/ { have_head=1; }'
398 else
399 cat
400 fi
401 }
402
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
416 else
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
418 fi
419 rm -f "${isofile}" "${imgfile}"
420 rm -rf "${rom_directory}"
421 if [ x$boot = xcoreboot ]; then
422 rm -f "${imgfile}"
423 fi
424
425 rm -f "${tmpfile}" "${cfgfile}"
426 exit 0
427
428