xref: /kvm-unit-tests/riscv/efi/run (revision f3f338619e4938c2509f5c691adc1f331b07c203)
1#!/bin/bash
2
3if [ $# -eq 0 ]; then
4	echo "Usage $0 TEST_CASE [QEMU_ARGS]"
5	exit 2
6fi
7
8if [ ! -f config.mak ]; then
9	echo "run './configure --enable-efi && make' first. See ./configure -h"
10	exit 2
11fi
12source config.mak
13source scripts/arch-run.bash
14source scripts/vmm.bash
15
16if [[ $(vmm_get_target) == "kvmtool" ]]; then
17	echo "kvmtool does not support EFI tests."
18	exit 2
19fi
20
21if [ -f RISCV_VIRT_CODE.fd ]; then
22	DEFAULT_UEFI=RISCV_VIRT_CODE.fd
23fi
24
25KERNEL_NAME=$1
26
27: "${EFI_SRC:=$TEST_DIR}"
28: "${EFI_UEFI:=$DEFAULT_UEFI}"
29: "${EFI_TEST:=efi-tests}"
30: "${EFI_CASE:=$(basename $KERNEL_NAME .efi)}"
31: "${EFI_TESTNAME:=$TESTNAME}"
32: "${EFI_TESTNAME:=$EFI_CASE}"
33: "${EFI_CASE_DIR:="$EFI_TEST/$EFI_TESTNAME"}"
34: "${EFI_VAR_GUID:=97ef3e03-7329-4a6a-b9ba-6c1fdcc5f823}"
35
36if [ ! -f "$EFI_UEFI" ]; then
37	echo "UEFI firmware not found."
38	echo "Please specify the path with the env variable EFI_UEFI"
39	exit 2
40fi
41
42if [ "$EFI_USE_ACPI" = "y" ]; then
43	echo "ACPI not available"
44	exit 2
45fi
46
47# Remove the TEST_CASE from $@
48shift 1
49
50# Fish out the arguments for the test, they should be the next string
51# after the "-append" option
52qemu_args=()
53cmd_args=()
54while (( "$#" )); do
55	if [ "$1" = "-append" ]; then
56		cmd_args=("$2")
57		shift 2
58	else
59		qemu_args+=("$1")
60		shift 1
61	fi
62done
63
64if [ "$EFI_CASE" = "_NO_FILE_4Uhere_" ]; then
65	EFI_CASE_DIR="$EFI_TEST/dummy"
66	mkdir -p "$EFI_CASE_DIR"
67	$TEST_DIR/run \
68		$EFI_CASE \
69		-machine pflash0=pflash0 \
70		-blockdev node-name=pflash0,driver=file,read-only=on,filename="$EFI_UEFI" \
71		-drive file.dir="$EFI_CASE_DIR/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \
72		"${qemu_args[@]}"
73	exit
74fi
75
76uefi_shell_run()
77{
78	mkdir -p "$EFI_CASE_DIR"
79	cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_DIR/"
80	echo "@echo -off" > "$EFI_CASE_DIR/startup.nsh"
81	if [ "$EFI_USE_ACPI" != "y" ]; then
82		qemu_args+=(-machine acpi=off)
83		FDT_BASENAME="dtb"
84		UEFI_SHELL_RUN=y $TEST_DIR/run \
85			-machine pflash0=pflash0 \
86			-blockdev node-name=pflash0,driver=file,read-only=on,filename="$EFI_UEFI" \
87			-machine dumpdtb="$EFI_CASE_DIR/$FDT_BASENAME" \
88			"${qemu_args[@]}"
89		echo "setvar fdtfile -guid $EFI_VAR_GUID -rt =L\"$FDT_BASENAME\""  >> "$EFI_CASE_DIR/startup.nsh"
90	fi
91	echo "$EFI_CASE.efi" "${cmd_args[@]}" >> "$EFI_CASE_DIR/startup.nsh"
92
93	UEFI_SHELL_RUN=y $TEST_DIR/run \
94		-machine pflash0=pflash0 \
95		-blockdev node-name=pflash0,driver=file,read-only=on,filename="$EFI_UEFI" \
96		-drive file.dir="$EFI_CASE_DIR/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \
97		"${qemu_args[@]}"
98}
99
100if [ "$EFI_DIRECT" = "y" ]; then
101	if [ "$EFI_USE_ACPI" != "y" ]; then
102		qemu_args+=(-machine acpi=off)
103	fi
104	$TEST_DIR/run \
105		$KERNEL_NAME \
106		-append "$(basename $KERNEL_NAME) ${cmd_args[*]}" \
107		-machine pflash0=pflash0 \
108		-blockdev node-name=pflash0,driver=file,read-only=on,filename="$EFI_UEFI" \
109		"${qemu_args[@]}"
110else
111	uefi_shell_run
112fi
113