1 // SPDX-License-Identifier: GPL-2.0+
3 * efi_selftest_loadimage
5 * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
7 * This test checks the LoadImage and StartImage boot service.
9 * The efi_selftest_miniapp_exit.efi application is loaded via a file device
13 #include <efi_selftest.h>
14 /* Include containing the efi_selftest_miniapp_exit.efi application */
15 #include "efi_miniapp_file_image_exit.h"
17 /* Block size of compressed disk image */
18 #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
20 /* Binary logarithm of the block size */
21 #define LB_BLOCK_SIZE 9
23 #define FILE_NAME u"app.efi"
24 #define VOLUME_NAME u"EfiDisk"
26 static struct efi_boot_services *boottime;
27 static efi_handle_t handle_image;
28 static efi_handle_t handle_volume;
30 static const efi_guid_t guid_device_path = EFI_DEVICE_PATH_PROTOCOL_GUID;
31 static const efi_guid_t guid_simple_file_system_protocol =
32 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
33 static const efi_guid_t guid_file_info = EFI_FILE_INFO_GUID;
34 static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
36 /* One 8 byte block of the compressed disk image */
42 /* Compressed file image */
43 struct compressed_file_image {
48 /* File info including file name */
50 struct efi_file_info info;
51 u16 file_name[sizeof(FILE_NAME)];
54 /* File system info including volume name */
55 struct file_system_info {
56 struct efi_file_system_info info;
57 u16 file_name[sizeof(VOLUME_NAME)];
60 /* Compressed file image */
61 static struct compressed_file_image img = EFI_ST_DISK_IMG;
63 /* Pointer to decompressed file image */
67 static struct file_info priv_file_info = {
69 .size = sizeof(struct file_info),
70 .attribute = EFI_FILE_READ_ONLY,
75 /* Pointer to file info */
76 struct efi_file_info *file_info = &priv_file_info.info;
78 /* Volume device path */
80 struct efi_device_path_vendor vendor;
81 struct efi_device_path end;
82 } __packed dp_volume = {
85 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
86 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
87 .length = sizeof(struct efi_device_path_vendor),
89 .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
90 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
93 .type = DEVICE_PATH_TYPE_END,
94 .sub_type = DEVICE_PATH_SUB_TYPE_END,
95 .length = sizeof(struct efi_device_path),
99 /* File device path */
101 struct efi_device_path_vendor vendor;
102 struct efi_device_path path;
103 u16 file[sizeof(FILE_NAME)];
104 struct efi_device_path end;
105 } __packed dp_file = {
108 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
109 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
110 .length = sizeof(struct efi_device_path_vendor),
112 .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
113 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
116 .type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
117 .sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
118 .length = sizeof(struct efi_device_path) + sizeof(dp_file.file),
122 .type = DEVICE_PATH_TYPE_END,
123 .sub_type = DEVICE_PATH_SUB_TYPE_END,
124 .length = sizeof(struct efi_device_path),
128 /* File system info */
129 static struct file_system_info priv_file_system_info = {
131 .size = sizeof(struct file_system_info),
133 .volume_size = 0x100000,
140 /* Pointer to file system info */
141 static struct efi_file_system_info *file_system_info =
142 &priv_file_system_info.info;
144 /* Forward definitions of file and file system functions */
145 static efi_status_t EFIAPI efi_st_open_volume
146 (struct efi_simple_file_system_protocol *this,
147 struct efi_file_handle **root);
149 static efi_status_t EFIAPI efi_st_open
150 (struct efi_file_handle *this,
151 struct efi_file_handle **new_handle,
152 u16 *file_name, u64 open_mode, u64 attributes);
154 static efi_status_t EFIAPI efi_st_close(struct efi_file_handle *this);
156 static efi_status_t EFIAPI efi_st_delete(struct efi_file_handle *this);
158 static efi_status_t EFIAPI efi_st_read
159 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
161 static efi_status_t EFIAPI efi_st_write
162 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
164 static efi_status_t EFIAPI efi_st_getpos(struct efi_file_handle *this,
167 static efi_status_t EFIAPI efi_st_setpos(struct efi_file_handle *this, u64 pos);
169 static efi_status_t EFIAPI efi_st_getinfo
170 (struct efi_file_handle *this, const efi_guid_t *info_type,
171 efi_uintn_t *buffer_size, void *buffer);
173 static efi_status_t EFIAPI efi_st_setinfo
174 (struct efi_file_handle *this, const efi_guid_t *info_type,
175 efi_uintn_t buffer_size, void *buffer);
177 static efi_status_t EFIAPI efi_st_flush(struct efi_file_handle *this);
179 /* Internal information about status of file system */
181 /* Difference of volume open count minus volume close count */
182 int volume_open_count;
183 /* Difference of file open count minus file close count */
187 /* Current position in file */
191 /* EFI_FILE_PROTOCOL for file */
192 static struct efi_file_handle file = {
195 .close = efi_st_close,
196 .delete = efi_st_delete,
198 .write = efi_st_write,
199 .getpos = efi_st_getpos,
200 .setpos = efi_st_setpos,
201 .getinfo = efi_st_getinfo,
202 .setinfo = efi_st_setinfo,
203 .flush = efi_st_flush,
206 /* EFI_FILE_PROTOCOL for root directory */
207 static struct efi_file_handle volume = {
210 .close = efi_st_close,
211 .delete = efi_st_delete,
213 .write = efi_st_write,
214 .getpos = efi_st_getpos,
215 .setpos = efi_st_setpos,
216 .getinfo = efi_st_getinfo,
217 .setinfo = efi_st_setinfo,
218 .flush = efi_st_flush,
221 /* EFI_SIMPLE_FILE_SYSTEM_PROTOCOL of the block device */
222 struct efi_simple_file_system_protocol file_system = {
224 .open_volume = efi_st_open_volume,
227 static efi_status_t EFIAPI efi_st_open_volume
228 (struct efi_simple_file_system_protocol *this,
229 struct efi_file_handle **root)
231 if (this != &file_system || !root)
232 return EFI_INVALID_PARAMETER;
235 priv.volume_open_count++;
240 static efi_status_t EFIAPI efi_st_open
241 (struct efi_file_handle *this,
242 struct efi_file_handle **new_handle,
243 u16 *file_name, u64 open_mode, u64 attributes)
246 return EFI_INVALID_PARAMETER;
250 priv.file_open_count++;
255 static efi_status_t EFIAPI efi_st_close(struct efi_file_handle *this)
258 priv.file_open_count--;
259 else if (this == &volume)
260 priv.volume_open_count--;
262 return EFI_INVALID_PARAMETER;
267 static efi_status_t EFIAPI efi_st_delete(struct efi_file_handle *this)
270 return EFI_INVALID_PARAMETER;
272 return EFI_UNSUPPORTED;
275 static efi_status_t EFIAPI efi_st_read
276 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
279 return EFI_INVALID_PARAMETER;
281 if (priv.file_pos >= img.length)
283 else if (priv.file_pos + *buffer_size > img.length)
284 *buffer_size = img.length - priv.file_pos;
286 boottime->copy_mem(buffer, &image[priv.file_pos], *buffer_size);
287 priv.file_pos += *buffer_size;
292 static efi_status_t EFIAPI efi_st_write
293 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
296 return EFI_INVALID_PARAMETER;
298 return EFI_UNSUPPORTED;
301 static efi_status_t EFIAPI efi_st_getpos(struct efi_file_handle *this, u64 *pos)
304 return EFI_INVALID_PARAMETER;
306 *pos = priv.file_pos;
311 static efi_status_t EFIAPI efi_st_setpos(struct efi_file_handle *this, u64 pos)
314 return EFI_INVALID_PARAMETER;
321 static efi_status_t EFIAPI efi_st_getinfo
322 (struct efi_file_handle *this, const efi_guid_t *info_type,
323 efi_uintn_t *buffer_size, void *buffer)
326 if (memcmp(info_type, &guid_file_info, sizeof(efi_guid_t)))
327 return EFI_INVALID_PARAMETER;
328 if (*buffer_size >= sizeof(struct file_info)) {
329 boottime->copy_mem(buffer, file_info,
330 sizeof(struct file_info));
332 *buffer_size = sizeof(struct file_info);
333 return EFI_BUFFER_TOO_SMALL;
335 } else if (this == &volume) {
336 if (memcmp(info_type, &guid_file_system_info,
338 return EFI_INVALID_PARAMETER;
339 if (*buffer_size >= sizeof(struct file_system_info)) {
340 boottime->copy_mem(buffer, file_system_info,
341 sizeof(struct file_system_info));
343 *buffer_size = sizeof(struct file_system_info);
344 return EFI_BUFFER_TOO_SMALL;
347 return EFI_INVALID_PARAMETER;
352 static efi_status_t EFIAPI efi_st_setinfo
353 (struct efi_file_handle *this, const efi_guid_t *info_type,
354 efi_uintn_t buffer_size, void *buffer)
357 return EFI_INVALID_PARAMETER;
359 return EFI_UNSUPPORTED;
362 static efi_status_t EFIAPI efi_st_flush(struct efi_file_handle *this)
365 return EFI_INVALID_PARAMETER;
367 return EFI_UNSUPPORTED;
371 * Decompress the disk image.
373 * @image decompressed disk image
374 * Return: status code
376 static efi_status_t decompress(u8 **image)
384 ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
386 if (ret != EFI_SUCCESS) {
387 efi_st_error("Out of memory\n");
390 boottime->set_mem(buf, img.length, 0);
393 if (!img.lines[i].line)
395 addr = img.lines[i].addr;
396 len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
397 if (addr + len > img.length)
398 len = img.length - addr;
399 boottime->copy_mem(buf + addr, img.lines[i].line, len);
402 priv.file_size = img.length;
403 file_info->file_size = img.length;
410 * Decompress application image and provide a handle for the in memory block
413 * @handle: handle of the loaded image
414 * @systable: system table
415 * Return: EFI_ST_SUCCESS for success
417 static int setup(const efi_handle_t handle,
418 const struct efi_system_table *systable)
422 handle_image = handle;
423 boottime = systable->boottime;
425 /* Load the application image into memory */
428 ret = boottime->install_protocol_interface
429 (&handle_volume, &guid_device_path, EFI_NATIVE_INTERFACE,
431 if (ret != EFI_SUCCESS) {
432 efi_st_error("Failed to install device path\n");
433 return EFI_ST_FAILURE;
435 ret = boottime->install_protocol_interface
436 (&handle_volume, &guid_simple_file_system_protocol,
437 EFI_NATIVE_INTERFACE, &file_system);
438 if (ret != EFI_SUCCESS) {
439 efi_st_error("Failed to install simple file system protocol\n");
440 return EFI_ST_FAILURE;
443 return EFI_ST_SUCCESS;
447 * Tear down unit test.
449 * Uninstall protocols and free memory.
451 * Return: EFI_ST_SUCCESS for success
453 static int teardown(void)
455 efi_status_t ret = EFI_ST_SUCCESS;
458 ret = boottime->uninstall_protocol_interface
459 (handle_volume, &guid_simple_file_system_protocol,
461 if (ret != EFI_SUCCESS) {
463 ("Failed to uninstall simple file system protocol\n");
464 return EFI_ST_FAILURE;
466 ret = boottime->uninstall_protocol_interface
467 (handle_volume, &guid_device_path, &dp_volume);
468 if (ret != EFI_SUCCESS) {
470 ("Failed to uninstall device path protocol\n");
471 return EFI_ST_FAILURE;
476 ret = boottime->free_pool(image);
477 if (ret != EFI_SUCCESS) {
478 efi_st_error("Failed to free image\n");
479 return EFI_ST_FAILURE;
488 * Load and start the application image.
490 * Return: EFI_ST_SUCCESS for success
492 static int execute(void)
497 ret = boottime->load_image(false, handle_image, &dp_file.vendor.dp,
499 if (ret != EFI_SUCCESS) {
500 efi_st_error("Failed to load image\n");
501 return EFI_ST_FAILURE;
503 ret = boottime->start_image(handle, NULL, NULL);
504 if (ret != EFI_UNSUPPORTED) {
505 efi_st_error("Wrong return value from application\n");
506 return EFI_ST_FAILURE;
509 if (priv.file_open_count) {
510 efi_st_error("File open count = %d, expected 0\n",
511 priv.file_open_count);
512 return EFI_ST_FAILURE;
514 if (priv.volume_open_count) {
515 efi_st_error("Volume open count = %d, expected 0\n",
516 priv.volume_open_count);
517 return EFI_ST_FAILURE;
520 return EFI_ST_SUCCESS;
523 EFI_UNIT_TEST(loadimage) = {
524 .name = "load image from file",
525 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
528 .teardown = teardown,