blob: f591dcd2115effe908a4c0f13e9be7ab212f8cda [file] [log] [blame]
Ilias Apalodimasc52067c2020-02-21 09:55:46 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * efi_selftest_load_initrd
4 *
5 * Copyright (c) 2020 Ilias Apalodimas <ilias.apalodimas@linaro.org>
6 *
7 * This test checks the FileLoad2 protocol.
8 * A known file is read from the file system and verified.
9 *
10 * An example usage - given a file image with a file system in partition 1
11 * holding file initrd - is:
12 *
13 * * Configure the sandbox with
14 *
15 * CONFIG_EFI_SELFTEST=y
16 * CONFIG_EFI_LOAD_FILE2_INITRD=y
17 * CONFIG_EFI_INITRD_FILESPEC="host 0:1 initrd"
18 *
19 * * Run ./u-boot and execute
20 *
21 * host bind 0 image
22 * setenv efi_selftest load initrd
23 * bootefi selftest
24 *
25 * This would provide a test output like:
26 *
27 * Testing EFI API implementation
28 *
29 * Selected test: 'load initrd'
30 *
31 * Setting up 'load initrd'
32 * Setting up 'load initrd' succeeded
33 *
34 * Executing 'load initrd'
35 * Loaded 12378613 bytes
36 * CRC32 2997478465
37 *
38 * Now the size and CRC32 can be compared to the provided file.
39 */
40
41#include <efi_selftest.h>
42#include <efi_loader.h>
43#include <efi_load_initrd.h>
44
45static struct efi_boot_services *boottime;
46
47static struct efi_initrd_dp dp = {
48 .vendor = {
49 {
50 DEVICE_PATH_TYPE_MEDIA_DEVICE,
51 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
52 sizeof(dp.vendor),
53 },
54 EFI_INITRD_MEDIA_GUID,
55 },
56 .end = {
57 DEVICE_PATH_TYPE_END,
58 DEVICE_PATH_SUB_TYPE_END,
59 sizeof(dp.end),
60 }
61};
62
63static struct efi_initrd_dp dp_invalid = {
64 .vendor = {
65 {
66 DEVICE_PATH_TYPE_MEDIA_DEVICE,
67 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
68 sizeof(dp.vendor),
69 },
70 EFI_INITRD_MEDIA_GUID,
71 },
72 .end = {
73 0x8f, /* invalid */
74 0xfe, /* invalid */
75 sizeof(dp.end),
76 }
77};
78
79static int setup(const efi_handle_t handle,
80 const struct efi_system_table *systable)
81{
82 boottime = systable->boottime;
83
84 return EFI_ST_SUCCESS;
85}
86
87static int execute(void)
88{
Ilias Apalodimasc52067c2020-02-21 09:55:46 +020089 struct efi_load_file_protocol *lf2;
90 struct efi_device_path *dp2, *dp2_invalid;
91 efi_status_t status;
92 efi_handle_t handle;
93 char buffer[64];
94 efi_uintn_t buffer_size;
95 void *buf;
96 u32 crc32;
97
98 memset(buffer, 0, sizeof(buffer));
99
100 dp2 = (struct efi_device_path *)&dp;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +0100101 status = boottime->locate_device_path(&efi_guid_load_file2_protocol,
102 &dp2, &handle);
Ilias Apalodimasc52067c2020-02-21 09:55:46 +0200103 if (status != EFI_SUCCESS) {
104 efi_st_error("Unable to locate device path\n");
105 return EFI_ST_FAILURE;
106 }
107
Heinrich Schuchardt485956d2020-12-04 03:33:41 +0100108 status = boottime->handle_protocol(handle,
109 &efi_guid_load_file2_protocol,
Ilias Apalodimasc52067c2020-02-21 09:55:46 +0200110 (void **)&lf2);
111 if (status != EFI_SUCCESS) {
112 efi_st_error("Unable to locate protocol\n");
113 return EFI_ST_FAILURE;
114 }
115
116 /* Case 1:
117 * buffer_size can't be NULL
118 * protocol can't be NULL
119 */
120 status = lf2->load_file(lf2, dp2, false, NULL, &buffer);
121 if (status != EFI_INVALID_PARAMETER) {
122 efi_st_error("Buffer size can't be NULL\n");
123 return EFI_ST_FAILURE;
124 }
125 buffer_size = sizeof(buffer);
126 status = lf2->load_file(NULL, dp2, false, &buffer_size, &buffer);
127 if (status != EFI_INVALID_PARAMETER) {
128 efi_st_error("Protocol can't be NULL\n");
129 return EFI_ST_FAILURE;
130 }
131
132 /*
133 * Case 2: Match end node type/sub-type on device path
134 */
135 dp2_invalid = (struct efi_device_path *)&dp_invalid;
136 buffer_size = sizeof(buffer);
137 status = lf2->load_file(lf2, dp2_invalid, false, &buffer_size, &buffer);
138 if (status != EFI_INVALID_PARAMETER) {
139 efi_st_error("Invalid device path type must return EFI_INVALID_PARAMETER\n");
140 return EFI_ST_FAILURE;
141 }
142
143 status = lf2->load_file(lf2, dp2_invalid, false, &buffer_size, &buffer);
144 if (status != EFI_INVALID_PARAMETER) {
145 efi_st_error("Invalid device path sub-type must return EFI_INVALID_PARAMETER\n");
146 return EFI_ST_FAILURE;
147 }
148
149 /*
150 * Case 3:
151 * BootPolicy 'true' must return EFI_UNSUPPORTED
152 */
153 buffer_size = sizeof(buffer);
154 status = lf2->load_file(lf2, dp2, true, &buffer_size, &buffer);
155 if (status != EFI_UNSUPPORTED) {
156 efi_st_error("BootPolicy true must return EFI_UNSUPPORTED\n");
157 return EFI_ST_FAILURE;
158 }
159
160 /*
161 * Case: Pass buffer size as zero, firmware must return
162 * EFI_BUFFER_TOO_SMALL and an appropriate size
163 */
164 buffer_size = 0;
165 status = lf2->load_file(lf2, dp2, false, &buffer_size, NULL);
166 if (status != EFI_BUFFER_TOO_SMALL || !buffer_size) {
167 efi_st_printf("buffer_size: %u\n", (unsigned int)buffer_size);
168 efi_st_printf("status: %x\n", (unsigned int)status);
169 efi_st_error("Buffer size not updated\n");
170 return EFI_ST_FAILURE;
171 }
172
173 /*
174 * Case: Pass buffer size as smaller than the file_size,
175 * firmware must return * EFI_BUFFER_TOO_SMALL and an appropriate size
176 */
177 buffer_size = 1;
178 status = lf2->load_file(lf2, dp2, false, &buffer_size, &buffer);
179 if (status != EFI_BUFFER_TOO_SMALL || buffer_size <= 1) {
180 efi_st_error("Buffer size not updated\n");
181 return EFI_ST_FAILURE;
182 }
183
184 status = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
185 &buf);
186 if (status != EFI_SUCCESS) {
187 efi_st_error("Cannot allocate buffer\n");
188 return EFI_ST_FAILURE;
189 }
190
191 /* Case: Pass correct buffer, load the file and verify checksum*/
192 status = lf2->load_file(lf2, dp2, false, &buffer_size, buf);
193 if (status != EFI_SUCCESS) {
194 efi_st_error("Loading initrd failed\n");
195 return EFI_ST_FAILURE;
196 }
197
198 efi_st_printf("Loaded %u bytes\n", (unsigned int)buffer_size);
199 status = boottime->calculate_crc32(buf, buffer_size, &crc32);
200 if (status != EFI_SUCCESS) {
201 efi_st_error("Could not determine CRC32\n");
202 return EFI_ST_FAILURE;
203 }
Heinrich Schuchardt38a5c472020-10-03 13:43:45 +0200204 efi_st_printf("CRC32 %.8x\n", (unsigned int)crc32);
Ilias Apalodimasc52067c2020-02-21 09:55:46 +0200205
206 status = boottime->free_pool(buf);
207 if (status != EFI_SUCCESS) {
208 efi_st_error("Cannot free buffer\n");
209 return EFI_ST_FAILURE;
210 }
211
212 return EFI_ST_SUCCESS;
213}
214
215EFI_UNIT_TEST(load_initrd) = {
216 .name = "load initrd",
217 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
218 .setup = setup,
219 .execute = execute,
220 .on_request = true,
221};