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