xref: /freebsd/tools/boot/full-test.sh (revision c27f7d6b9cf6d4ab01cb3d0972726c14e0aca146)
1#!/bin/sh
2
3# STAND_ROOT is the root of a tree:
4# cache - Cached binaries that we have downloaded
5# trees - binary trees that we use to make image
6#      trees/${ARCH}/$thing
7# images - bootable images that we use to test
8#	images/${ARCH}/$thing
9# bios - cached bios images (as well as 'vars' files when we start testing
10#	different booting scenarios in the precense / absence of variables).
11# scripts - generated scripts that uses images to run the tests.
12#
13# Strategy:
14#	Download FreeBSD release isos, Linux kernels (for the kboot tests) and
15#	other misc things. We use these to generate dozens of test images that we
16#	use qemu-system-XXXX to boot. They all boot the same thing at the moment:
17#	an /etc/rc script that prints the boot method, echos success and then
18#	halts.
19
20# What version of FreeBSD to we snag the ISOs from to extract the binaries
21# we are testing
22FREEBSD_VERSION=14.2
23# eg https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/14.2/FreeBSD-14.2-RELEASE-amd64-bootonly.iso.xz
24URLBASE="https://download.freebsd.org/releases"
25: ${STAND_ROOT:="${HOME}/stand-test-root"}
26CACHE=${STAND_ROOT}/cache
27TREES=${STAND_ROOT}/trees
28IMAGES=${STAND_ROOT}/images
29BIOS=${STAND_ROOT}/bios
30SCRIPTS=${STAND_ROOT}/scripts
31OVERRIDE=${STAND_ROOT}/override
32
33# Find make
34case $(uname) in
35    Darwin)
36	t=$(realpath $(dirname $0)/../..)
37	# Use the python wrapper to find make
38	if [ -f ${t}/tools/build/make.py ]; then
39	    MAKE="${t}/tools/build/make.py"
40	    case $(uname -m) in
41		arm64)
42		    DEFARCH="TARGET_ARCH=aarch64 TARGET=arm64"
43		    ;;
44		x86_64)
45		    DEFARCH="TARGET_ARCH=amd64 TARGET=amd64"
46		    ;;
47		*)
48		    die "Do not know about $(uanme -p)"
49		    ;;
50	    esac
51	else
52	    die "Can't find the make wrapper"
53	fi
54	qemu_bin=/opt/homebrew/bin
55	;;
56    FreeBSD)
57	MAKE=make
58	qemu_bin=/usr/local/bin
59	;;
60    # linux) not yet
61    *)
62	die "Do not know about system $(uname)"
63	;;
64esac
65
66SRCTOP=$(${MAKE} ${DEFARCH} -v SRCTOP)
67echo $SRCTOP
68
69# Find makefs and mkimg
70MAKEFS=$(SHELL="which makefs" ${MAKE} ${DEFARCH} buildenv | tail -1) || die "No makefs try WITH_DISK_IMAGE_TOOLS_BOOTSTRAP=y"
71MKIMG=$(SHELL="which mkimg" ${MAKE} ${DEFARCH} buildenv | tail -1) || die "No mkimg, try buildworld first"
72MTREE=$(SHELL="which mtree" ${MAKE} ${DEFARCH} buildenv | tail -1) || die "No mtree, try buildworld first"
73
74# MAKE=$(SHELL="which make" ${MAKE} ${DEFARCH} buildenv | tail -1) || die "No make, try buildworld first"
75
76
77# All the architectures under test
78# Note: we can't yet do armv7 because we don't have a good iso for it and would
79# need root to extract the files.
80#ARCHES="amd64:amd64 i386:i386 powerpc:powerpc powerpc:powerpc64 powerpc:powerpc64le powerpc:powerpcspe arm64:aarch64 riscv:riscv64"
81ARCHES="amd64:amd64 arm64:aarch64"
82
83# The smallest FAT32 filesystem is 33292 KB
84espsize=33292
85
86mkdir -p ${CACHE} ${TREES} ${IMAGES} ${BIOS}
87
88die()
89{
90    echo Fatal Error: $*
91    exit 1
92}
93
94ma_combo()
95{
96    local m=$1
97    local ma=$2
98    local ma_combo="${m}"
99
100    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
101    echo ${ma_combo}
102}
103
104fetch_one()
105{
106    local m=$1
107    local ma=$2
108    local v=$3
109    local flavor=$4
110    local ma_combo=$(ma_combo $m $ma)
111    local file="FreeBSD-${v}-RELEASE-${ma_combo}-${flavor}"
112    local url="${URLBASE}/${m}/${ma}/ISO-IMAGES/${v}/${file}.xz"
113
114    mkdir -p ${CACHE}
115    [ -r ${CACHE}/${file} ] && echo "Using cached ${file}" && return
116    cd ${CACHE}
117    echo "Fetching ${url}"
118    fetch ${url} || die "Can't fetch ${file} from ${url}"
119    xz -d ${file}.xz || die "Can't uncompress ${file}.xz"
120    cd ..
121}
122
123update_freebsd_img_cache()
124{
125    local a m ma
126
127    for a in $ARCHES; do
128	m=${a%%:*}
129	ma=${a##*:}
130	fetch_one $m $ma ${FREEBSD_VERSION} bootonly.iso
131    done
132
133    fetch_one arm armv7 ${FREEBSD_VERSION} GENERICSD.img
134}
135
136make_minimal_freebsd_tree()
137{
138    local m=$1
139    local ma=$2
140    local v=$3
141    local flavor=$4
142    local file d
143    local ma_combo="${m}"
144    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
145
146    file="FreeBSD-${v}-RELEASE-${ma_combo}-${flavor}"
147    dir=${TREES}/${ma_combo}/freebsd
148    rm -rf ${dir}
149
150    # Make a super simple userland. It has just enough to print a santiy value,
151    # then say test succeeded, and then halt the system. We assume that /bin/sh
152    # has all the library prereqs for the rest...
153    mkdir -p ${dir}
154    # Make required dirs
155    for d in boot/kernel boot/defaults boot/lua boot/loader.conf.d \
156			 sbin bin lib libexec etc dev; do
157	mkdir -p ${dir}/${d}
158    done
159    # Pretend we don't have a separate /usr
160    ln -s . ${dir}/usr
161    # snag the binaries for my simple /etc/rc file
162    tar -C ${dir} -xf ${CACHE}/$file sbin/fastboot sbin/reboot sbin/halt sbin/init bin/sh sbin/sysctl \
163	lib/libtinfow.so.9 lib/libncursesw.so.9 lib/libc.so.7 lib/libedit.so.8 libexec/ld-elf.so.1
164    # My simple etc/rc
165    cat > ${dir}/etc/rc <<EOF
166#!/bin/sh
167
168sysctl machdep.bootmethod
169echo "RC COMMAND RUNNING -- SUCCESS!!!!!"
170halt -p
171EOF
172    chmod +x ${dir}/etc/rc
173
174    # Check to see if we have overrides here... So we can insert our own kernel
175    # instead of the one from the release.
176    echo "CHECKING ${OVERRIDE}/${ma_combo}/boot"
177    if [ -d ${OVERRIDE}/${ma_combo}/boot ]; then
178	o=${OVERRIDE}/${ma_combo}
179	for i in \
180	    boot/device.hints \
181	    boot/kernel/kernel \
182	    boot/kernel/acl_nfs4.ko \
183	    boot/kernel/cryptodev.ko \
184	    boot/kernel/zfs.ko \
185	    boot/kernel/geom_eli.ko; do
186	    [ -r $o/$i ] && echo Copying override $i && cp $o/$i ${dir}/$i
187	done
188    else
189	# Copy the kernel (but not the boot loader, we'll add the one to test later)
190	# This will take care of both UFS and ZFS boots as well as geli
191	# Note: It's OK for device.hints to be missing. It's mostly for legacy platforms.
192	tar -C ${dir} -xf ${CACHE}/$file \
193	    boot/device.hints \
194	    boot/kernel/kernel \
195	    boot/kernel/acl_nfs4.ko \
196	    boot/kernel/cryptodev.ko \
197	    boot/kernel/zfs.ko \
198	    boot/kernel/geom_eli.ko || true
199	# XXX WHAT TO DO ABOUT LINKER HINTS -- PUNT FOR NOW
200	# XXX also, ZFS not supported on 32-bit powerpc platforms
201    fi
202
203    # Setup some common settings for serial console, etc
204    echo -h -D -S115200 > ${dir}/boot.config
205    cat > ${dir}/boot/loader.conf <<EOF
206comconsole_speed=115200
207autoboot_delay=2
208zfs_load="YES"
209boot_verbose=yes
210kern.cfg.order="acpi,fdt"
211EOF
212}
213
214make_freebsd_minimal_trees()
215{
216    for a in $ARCHES; do
217	m=${a%%:*}
218	ma=${a##*:}
219	make_minimal_freebsd_tree $m $ma ${FREEBSD_VERSION} bootonly.iso
220    done
221    # Note: armv7 isn't done yet as its the odd-man out -- we need to extract things
222    # in a special way, so punt for the moment
223}
224
225make_freebsd_test_trees()
226{
227    for a in $ARCHES; do
228	m=${a%%:*}
229	ma=${a##*:}
230	ma_combo="${m}"
231	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
232	dir=${TREES}/${ma_combo}/test-stand
233	mkdir -p ${dir}
234	${MTREE} -deUW -f ${SRCTOP}/etc/mtree/BSD.root.dist -p ${dir}
235	echo "Creating tree for ${m}:${ma}"
236	cd ${SRCTOP}
237	# Indirection needed because our build system is too complex
238	# Also, bare make for 'inside' the buildenv ${MAKE} for outside
239#	SHELL="make clean" ${MAKE} buildenv TARGET=${m} TARGET_ARCH=${ma}
240	SHELL="sh -c 'cd stand ; make -j 100 all'" ${MAKE} TARGET=${m} TARGET_ARCH=${ma} buildenv
241	DESTDIR=${dir} SHELL="sh -c 'cd stand ; make install MK_MAN=no MK_INSTALL_AS_USER=yes WITHOUT_DEBUG_FILES=yes'" \
242	     ${MAKE} buildenv TARGET=${m} TARGET_ARCH=${ma}
243	rm -rf ${dir}/bin ${dir}/[ac-z]*	# Don't care about anything here
244    done
245}
246
247make_linux_initrds()
248{
249    # At the moment, we have just two
250    for a in amd64:amd64 arm64:aarch64; do
251	m=${a%%:*}
252	ma=${a##*:}
253	ma_combo="${m}"
254	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
255	dir=${TREES}/${ma_combo}/linuxboot
256	dir2=${TREES}/${ma_combo}/test-stand
257	dir3=${TREES}/${ma_combo}/freebsd
258	initrd=${TREES}/${ma_combo}/initrd.img
259	rm -rf ${dir}
260	mkdir -p ${dir}
261	cp ${dir2}/boot/loader.kboot ${dir}/init
262	# Copy the boot loader
263	tar -c -f - -C ${dir2} boot | tar -xf - -C ${dir}
264	# Copy the boot kernel
265	tar -c -f - -C ${dir3} boot | tar -xf - -C ${dir}
266	(cd ${dir} ; find . | LC_ALL=C sort | cpio -o -H newc | gzip > ${initrd})
267    done
268}
269
270make_linux_esps()
271{
272    # At the moment, we have just two
273    for a in amd64:amd64 arm64:aarch64; do
274	m=${a%%:*}
275	ma=${a##*:}
276	ma_combo="${m}"
277	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
278	dir=${TREES}/${ma_combo}/linuxboot-esp
279	initrd=${TREES}/${ma_combo}/initrd.img
280	mkdir -p ${dir}
281	case ${ma} in
282	    amd64) bin=x64 cons="console=ttyS0,115200" ;;
283	    aarch64) bin=aa64 ;;
284	esac
285	mkdir -p ${dir}/efi/boot
286	cp ${CACHE}/linux/linux${bin}.efi ${dir}
287	cp ${CACHE}/linux/shell${bin}.efi ${dir}/efi/boot/boot${bin}.efi
288	cat > ${dir}/startup.nsh <<EOF
289# Run linux
290# Tell it to run with out special initrd that then boot FreeBSD
291
292\linux${bin} ${cons} initrd=\initrd.img
293EOF
294	cp $initrd ${dir}
295    done
296}
297
298make_linuxboot_images()
299{
300    # ESP variant: In this variant, amd64 and arm64 are both created more or
301    # less the same way. Both are EFI + ACPI implementations
302    for a in amd64:amd64 arm64:aarch64; do
303	m=${a%%:*}
304	ma=${a##*:}
305	ma_combo="${m}"
306	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
307	src=${TREES}/${ma_combo}/linuxboot-esp
308	dir=${TREES}/${ma_combo}/freebsd
309	dir2=${TREES}/${ma_combo}/test-stand
310	esp=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}.esp
311	ufs=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}.ufs
312	zfs=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}.zfs
313	img=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}.img
314	img2=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}-zfs.img
315	pool="linuxboot"
316	mkdir -p ${IMAGES}/${ma_combo}
317	${MAKEFS} -t msdos -o fat_type=32 -o sectors_per_cluster=1 \
318	       -o volume_label=EFISYS -s80m ${esp} ${src}
319	${MAKEFS} -t ffs -B little -s 200m -o label=root ${ufs} ${dir} ${dir2}
320	${MKIMG} -s gpt -p efi:=${esp} -p freebsd-ufs:=${ufs} -o ${img}
321	${MAKEFS} -t zfs -s 200m \
322	       -o poolname=${pool} -o bootfs=${pool} -o rootpath=/ \
323		${zfs} ${dir} ${dir2}
324	${MKIMG} -s gpt \
325	      -p efi:=${esp} \
326	      -p freebsd-zfs:=${zfs} -o ${img2}
327	rm -f ${esp}	# Don't need to keep this around
328    done
329
330    # The raw variant, currently used only on arm64. It boots with the raw interface of qemu
331    # for testing purposes.  This means it makes a good test for the DTB variation, but not ACPI
332    # since qemu doesn't currently provide that...
333    for a in arm64:aarch64; do
334	m=${a%%:*}
335	ma=${a##*:}
336	ma_combo="${m}"
337	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
338	linux="${CACHE}/linux/vmlinux-${m}*"
339	initrd=${TREES}/${ma_combo}/initrd.img
340	img=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}-raw
341	cp ${linux} ${img}.kernel
342	cp ${initrd} ${img}.initrd
343    done
344}
345
346make_linuxboot_scripts()
347{
348    # At the moment, we have just two -- and the images we've built so far are just
349    # the hostfs boot. The boot off anything more complex isn't here.
350    for a in amd64:amd64 arm64:aarch64; do
351	m=${a%%:*}
352	ma=${a##*:}
353	ma_combo="${m}"
354	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
355
356	# First off, update the edk firmware
357	bios_code=${BIOS}/edk2-${ma_combo}-code.fd
358	bios_vars=${BIOS}/edk2-${ma_combo}-vars.fd
359	case ${ma} in
360	    amd64)
361		if [ ${bios_code} -ot /usr/local/share/qemu/edk2-x86_64-code.fd ]; then
362		    cp /usr/local/share/qemu/edk2-x86_64-code.fd ${bios_code}
363		    # vars file works on both 32 and 64 bit x86
364#		    cp /usr/local/share/qemu/edk2-i386-vars.fd ${bios_vars}
365		fi
366		;;
367	    aarch64)
368		if [ ${bios_code} -ot /usr/local/share/qemu/edk2-aarch64-code.fd ]; then
369		    # aarch64 vars starts as an empty file
370		    dd if=/dev/zero of=${bios_code} bs=1M count=64
371		    dd if=/dev/zero of=${bios_vars} bs=1M count=64
372		    dd if=/usr/local/share/qemu/edk2-aarch64-code.fd of=${bios_code} conv=notrunc
373		fi
374		;;
375	esac
376
377	# Now make me a script
378	img=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}.img
379	img2=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}-raw
380	img3=${IMAGES}/${ma_combo}/linuxboot-${ma_combo}-zfs.img
381	out=${SCRIPTS}/${ma_combo}/linuxboot-test.sh
382	out2=${SCRIPTS}/${ma_combo}/linuxboot-test-raw.sh
383	out3=${SCRIPTS}/${ma_combo}/linuxboot-test-zfs.sh
384	cd=${CACHE}/FreeBSD-13.1-RELEASE-arm64-aarch64-bootonly.iso
385	mkdir -p ${SCRIPTS}/${ma_combo}
386	case ${ma} in
387	    amd64)
388		cat > ${out} <<EOF
389${qemu_bin}/qemu-system-x86_64 -nographic -m 512M \\
390        -drive file=${img},if=none,id=drive0,cache=writeback,format=raw \\
391        -device virtio-blk,drive=drive0,bootindex=0 \\
392        -drive file=${bios_code},format=raw,if=pflash \\
393        -drive file=${bios_vars},format=raw,if=pflash \\
394        -monitor telnet::4444,server,nowait \\
395        -serial stdio \$*
396EOF
397		;;
398	    aarch64)
399		# ESP version
400		raw=${IMAGES}/${ma_combo}/freebsd-arm64-aarch64.img
401		cat > ${out} <<EOF
402${qemu_bin}/qemu-system-aarch64 -nographic -machine virt,gic-version=3 -m 512M -smp 4 \\
403        -cpu cortex-a57 \\
404	-drive file=${img},if=none,id=drive0,cache=writeback \\
405	-device virtio-blk,drive=drive0,bootindex=0 \\
406        -drive file=${raw},if=none,id=drive1,cache=writeback \\
407	-device nvme,serial=fboot,drive=drive1,bootindex=1 \\
408        -drive file=${bios_code},format=raw,if=pflash \\
409        -drive file=${bios_vars},format=raw,if=pflash \\
410        -monitor telnet::4444,server,nowait \\
411        -serial stdio \$*
412EOF
413		# RAW version
414		# Note: We have to use cortex-a57 for raw mode because the
415		# kernel we use has issues with max.
416		cat > ${out2} <<EOF
417${qemu_bin}/qemu-system-aarch64 -m 1024 -cpu cortex-a57 -M virt \\
418	-kernel ${img2}.kernel -initrd ${img2}.initrd \\
419	-append "console=ttyAMA0" \\
420        -drive file=${cd},if=none,id=drive0,cache=writeback,format=raw \\
421        -device virtio-blk,drive=drive0,bootindex=0 \\
422	-nographic -monitor telnet::4444,server,nowait \\
423	-serial stdio \$*
424EOF
425		# ZFS version
426		# Note: We have to use cortex-a57 for raw mode because the
427		# kernel we use has issues with max.
428		cat > ${out3} <<EOF
429${qemu_bin}/qemu-system-aarch64 -nographic -machine virt,gic-version=3 -m 512M -smp 4 \\
430        -cpu cortex-a57 \\
431	-drive file=${img3},if=none,id=drive0,cache=writeback \\
432	-device virtio-blk,drive=drive0,bootindex=0 \\
433        -drive file=${bios_code},format=raw,if=pflash \\
434        -drive file=${bios_vars},format=raw,if=pflash \\
435        -monitor telnet::4444,server,nowait \\
436        -serial stdio \$*
437EOF
438		;;
439	esac
440    done
441}
442
443make_freebsd_esps()
444{
445    # At the moment, we have just three (armv7 could also be here too, but we're not doing that)
446#   for a in amd64:amd64 arm64:aarch64 riscv:riscv64; do
447    for a in amd64:amd64 arm64:aarch64; do
448	m=${a%%:*}
449	ma=${a##*:}
450	ma_combo="${m}"
451	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
452	dir=${TREES}/${ma_combo}/freebsd-esp
453	dir2=${TREES}/${ma_combo}/test-stand
454	rm -rf ${dir}
455	mkdir -p ${dir}
456	case ${ma} in
457	    amd64) bin=x64 ;;
458	    aarch64) bin=aa64 ;;
459	esac
460	mkdir -p ${dir}/efi/boot
461	cp ${dir2}/boot/loader.efi ${dir}/efi/boot/boot${bin}.efi
462    done
463}
464
465make_freebsd_images()
466{
467    # ESP variant: In this variant, riscv, amd64 and arm64 are created more or
468    # less the same way. UEFI + ACPI implementations
469#   for a in amd64:amd64 arm64:aarch64 riscv:riscv64; do
470    for a in amd64:amd64 arm64:aarch64; do
471	m=${a%%:*}
472	ma=${a##*:}
473	ma_combo="${m}"
474	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
475	src=${TREES}/${ma_combo}/freebsd-esp
476	dir=${TREES}/${ma_combo}/freebsd
477	dir2=${TREES}/${ma_combo}/test-stand
478	esp=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.esp
479	ufs=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.ufs
480	img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
481	mkdir -p ${IMAGES}/${ma_combo}
482	mkdir -p ${dir2}/etc
483	cat > ${dir2}/etc/fstab <<EOF
484/dev/ufs/root	/		ufs	rw	1	1
485EOF
486	${MAKEFS} -t msdos -o fat_type=32 -o sectors_per_cluster=1 \
487	       -o volume_label=EFISYS -s100m ${esp} ${src}
488	${MAKEFS} -t ffs -B little -s 200m -o label=root ${ufs} ${dir} ${dir2}
489	${MKIMG} -s gpt -p efi:=${esp} -p freebsd-ufs:=${ufs} -o ${img}
490	# rm -f ${esp} ${ufs}	# Don't need to keep this around
491    done
492
493    set -x
494
495if false; then
496    # BIOS i386
497    a=i386:i386
498    m=${a%%:*}
499    ma=${a##*:}
500    ma_combo="${m}"
501    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
502    dir=${TREES}/${ma_combo}/freebsd
503    dir2=${TREES}/${ma_combo}/test-stand
504    ufs=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.ufs
505    img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
506    mkdir -p ${IMAGES}/${ma_combo}
507    mkdir -p ${dir2}/etc
508    cat > ${dir2}/etc/fstab <<EOF
509/dev/ufs/root	/		ufs	rw	1	1
510EOF
511    ${MAKEFS} -t ffs -B little -s 200m \
512	   -o label=root,version=2,bsize=32768,fsize=4096,density=16384 \
513	   ${ufs} ${dir} ${dir2}
514    ${MKIMG} -s gpt -b ${dir2}/boot/pmbr \
515	  -p freebsd-boot:=${dir2}/boot/gptboot \
516	  -p freebsd-ufs:=${ufs} \
517	  -o ${img}
518    rm -f ${src}/etc/fstab
519
520    # PowerPC for 32-bit mac
521    a=powerpc:powerpc
522    m=${a%%:*}
523    ma=${a##*:}
524    ma_combo="${m}"
525    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
526    dir=${TREES}/${ma_combo}/freebsd
527    dir2=${TREES}/${ma_combo}/test-stand
528    ufs=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.ufs
529    img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
530    mkdir -p ${IMAGES}/${ma_combo}
531    mkdir -p ${dir2}/etc
532    cat > ${dir2}/etc/fstab <<EOF
533/dev/ufs/root	/		ufs	rw	1	1
534EOF
535    ${MAKEFS} -t ffs -B big -s 200m \
536	   -o label=root,version=2,bsize=32768,fsize=4096,density=16384 \
537	   ${ufs} ${dir} ${dir2}
538    ${MKIMG} -a 1 -s apm \
539        -p freebsd-boot:=${dir2}/boot/boot1.hfs \
540        -p freebsd-ufs:=${ufs} \
541        -o ${img}
542fi
543
544    set +x
545}
546
547make_freebsd_scripts()
548{
549    # At the moment, we have just two
550    for a in amd64:amd64 arm64:aarch64; do
551	m=${a%%:*}
552	ma=${a##*:}
553	ma_combo="${m}"
554	[ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
555
556	# First off, update the edk firmware
557	bios_code=${BIOS}/edk2-${ma_combo}-code.fd
558	bios_vars=${BIOS}/edk2-${ma_combo}-vars.fd
559	case ${ma} in
560	    amd64)
561		if [ ${bios_code} -ot /usr/local/share/qemu/edk2-x86_64-code.fd ]; then
562		    cp /usr/local/share/qemu/edk2-x86_64-code.fd ${bios_code}
563		    # vars file works on both 32 and 64 bit x86
564#		    cp /usr/local/share/qemu/edk2-i386-vars.fd ${bios_vars}
565		fi
566		;;
567	    aarch64)
568		if [ ${bios_code} -ot /usr/local/share/qemu/edk2-aarch64-code.fd ]; then
569		    # aarch64 vars starts as an empty file
570		    dd if=/dev/zero of=${bios_code} bs=1M count=64
571		    dd if=/dev/zero of=${bios_vars} bs=1M count=64
572		    dd if=/usr/local/share/qemu/edk2-aarch64-code.fd of=${bios_code} conv=notrunc
573		fi
574		;;
575	esac
576
577	# Now make me a script
578	img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
579	out=${SCRIPTS}/${ma_combo}/freebsd-test.sh
580	mkdir -p ${SCRIPTS}/${ma_combo}
581	case ${ma} in
582	    amd64)
583		cat > ${out} <<EOF
584${qemu_bin}/qemu-system-x86_64 -nographic -m 512M \\
585        -drive file=${img},if=none,id=drive0,cache=writeback,format=raw \\
586        -device virtio-blk,drive=drive0,bootindex=0 \\
587        -drive file=${bios_code},format=raw,if=pflash \\
588        -drive file=${bios_vars},format=raw,if=pflash \\
589        -monitor telnet::4444,server,nowait \\
590        -serial stdio \$*
591EOF
592		;;
593	    aarch64)
594		# ESP version
595		raw=${IMAGES}/${ma_combo}/nvme-test-empty.raw
596		cat > ${out} <<EOF
597${qemu_bin}/qemu-system-aarch64 -nographic -machine virt,gic-version=3 -m 512M \\
598        -cpu cortex-a57 -drive file=${img},if=none,id=drive0,cache=writeback -smp 4 \\
599        -device virtio-blk,drive=drive0,bootindex=0 \\
600        -drive file=${bios_code},format=raw,if=pflash \\
601        -drive file=${bios_vars},format=raw,if=pflash \\
602        -drive file=${raw},if=none,id=drive1,cache=writeback,format=raw \\
603	-device nvme,serial=deadbeef,drive=drive1 \\
604        -monitor telnet::4444,server,nowait \\
605        -serial stdio \$*
606EOF
607		;;
608	esac
609    done
610
611if false; then
612    set -x
613    a=powerpc:powerpc
614    m=${a%%:*}
615    ma=${a##*:}
616    ma_combo="${m}"
617    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
618    img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
619    out=${SCRIPTS}/${ma_combo}/freebsd-test.sh
620    mkdir -p ${SCRIPTS}/${ma_combo}
621    cat > ${out} <<EOF
622${qemu_bin}/qemu-system-ppc -m 1g -M mac99,via=pmu \\
623	-vga none -nographic \\
624	-drive file=${img},if=virtio \\
625	-prom-env "boot-device=/pci@f2000000/scsi/disk@0:,\\\\\\:tbxi" \\
626        -monitor telnet::4444,server,nowait \\
627        -serial stdio \$*
628EOF
629
630    set -x
631    a=i386:i386
632    m=${a%%:*}
633    ma=${a##*:}
634    ma_combo="${m}"
635    [ "${m}" != "${ma}" ] && ma_combo="${m}-${ma}"
636    img=${IMAGES}/${ma_combo}/freebsd-${ma_combo}.img
637    out=${SCRIPTS}/${ma_combo}/freebsd-test.sh
638    mkdir -p ${SCRIPTS}/${ma_combo}
639    cat > ${out} <<EOF
640${qemu_bin}/qemu-system-i386 -m 1g \\
641	-vga none -nographic \\
642	-drive file=${img},format=raw \\
643	-nographic \\
644        -monitor telnet::4444,server,nowait \\
645        -serial stdio \$*
646EOF
647fi
648}
649
650# The smallest FAT32 filesystem is 33292 KB
651espsize=33292
652
653set -e
654echo "src/stand test in ${STAND_ROOT}"
655update_freebsd_img_cache
656make_freebsd_minimal_trees
657make_freebsd_test_trees
658make_linux_initrds
659make_linux_esps
660make_freebsd_esps
661make_freebsd_images
662make_freebsd_scripts
663make_linuxboot_images
664make_linuxboot_scripts
665