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 L"app.efi"
24 #define VOLUME_NAME L"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 open_volume
146 (struct efi_simple_file_system_protocol *this,
147 struct efi_file_handle **root);
149 static efi_status_t EFIAPI 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 close(struct efi_file_handle *this);
156 static efi_status_t EFIAPI delete(struct efi_file_handle *this);
158 static efi_status_t EFIAPI read
159 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
161 static efi_status_t EFIAPI write
162 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
164 static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos);
166 static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos);
168 static efi_status_t EFIAPI getinfo
169 (struct efi_file_handle *this, const efi_guid_t *info_type,
170 efi_uintn_t *buffer_size, void *buffer);
172 static efi_status_t EFIAPI setinfo
173 (struct efi_file_handle *this, const efi_guid_t *info_type,
174 efi_uintn_t buffer_size, void *buffer);
176 static efi_status_t EFIAPI flush(struct efi_file_handle *this);
178 /* Internal information about status of file system */
180 /* Difference of volume open count minus volume close count */
181 int volume_open_count;
182 /* Difference of file open count minus file close count */
186 /* Current position in file */
190 /* EFI_FILE_PROTOCOL for file */
191 static struct efi_file_handle file = {
205 /* EFI_FILE_PROTOCOL for root directory */
206 static struct efi_file_handle volume = {
220 /* EFI_SIMPLE_FILE_SYSTEM_PROTOCOL of the block device */
221 struct efi_simple_file_system_protocol file_system = {
223 .open_volume = open_volume,
226 static efi_status_t EFIAPI open_volume
227 (struct efi_simple_file_system_protocol *this,
228 struct efi_file_handle **root)
230 if (this != &file_system || !root)
231 return EFI_INVALID_PARAMETER;
234 priv.volume_open_count++;
239 static efi_status_t EFIAPI open
240 (struct efi_file_handle *this,
241 struct efi_file_handle **new_handle,
242 u16 *file_name, u64 open_mode, u64 attributes)
245 return EFI_INVALID_PARAMETER;
249 priv.file_open_count++;
254 static efi_status_t EFIAPI close(struct efi_file_handle *this)
257 priv.file_open_count--;
258 else if (this == &volume)
259 priv.volume_open_count--;
261 return EFI_INVALID_PARAMETER;
266 static efi_status_t EFIAPI delete(struct efi_file_handle *this)
269 return EFI_INVALID_PARAMETER;
271 return EFI_UNSUPPORTED;
274 static efi_status_t EFIAPI read
275 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
278 return EFI_INVALID_PARAMETER;
280 if (priv.file_pos >= img.length)
282 else if (priv.file_pos + *buffer_size > img.length)
283 *buffer_size = img.length - priv.file_pos;
285 boottime->copy_mem(buffer, &image[priv.file_pos], *buffer_size);
286 priv.file_pos += *buffer_size;
291 static efi_status_t EFIAPI write
292 (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
295 return EFI_INVALID_PARAMETER;
297 return EFI_UNSUPPORTED;
300 static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos)
303 return EFI_INVALID_PARAMETER;
305 *pos = priv.file_pos;
310 static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos)
313 return EFI_INVALID_PARAMETER;
320 static efi_status_t EFIAPI getinfo
321 (struct efi_file_handle *this, const efi_guid_t *info_type,
322 efi_uintn_t *buffer_size, void *buffer)
325 if (memcmp(info_type, &guid_file_info, sizeof(efi_guid_t)))
326 return EFI_INVALID_PARAMETER;
327 if (*buffer_size >= sizeof(struct file_info)) {
328 boottime->copy_mem(buffer, file_info,
329 sizeof(struct file_info));
331 *buffer_size = sizeof(struct file_info);
332 return EFI_BUFFER_TOO_SMALL;
334 } else if (this == &volume) {
335 if (memcmp(info_type, &guid_file_system_info,
337 return EFI_INVALID_PARAMETER;
338 if (*buffer_size >= sizeof(struct file_system_info)) {
339 boottime->copy_mem(buffer, file_system_info,
340 sizeof(struct file_system_info));
342 *buffer_size = sizeof(struct file_system_info);
343 return EFI_BUFFER_TOO_SMALL;
346 return EFI_INVALID_PARAMETER;
351 static efi_status_t EFIAPI setinfo
352 (struct efi_file_handle *this, const efi_guid_t *info_type,
353 efi_uintn_t buffer_size, void *buffer)
356 return EFI_INVALID_PARAMETER;
358 return EFI_UNSUPPORTED;
361 static efi_status_t EFIAPI flush(struct efi_file_handle *this)
364 return EFI_INVALID_PARAMETER;
366 return EFI_UNSUPPORTED;
370 * Decompress the disk image.
372 * @image decompressed disk image
373 * @return status code
375 static efi_status_t decompress(u8 **image)
383 ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
385 if (ret != EFI_SUCCESS) {
386 efi_st_error("Out of memory\n");
389 boottime->set_mem(buf, img.length, 0);
392 if (!img.lines[i].line)
394 addr = img.lines[i].addr;
395 len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
396 if (addr + len > img.length)
397 len = img.length - addr;
398 boottime->copy_mem(buf + addr, img.lines[i].line, len);
401 priv.file_size = img.length;
402 file_info->file_size = img.length;
409 * Decompress application image and provide a handle for the in memory block
412 * @handle: handle of the loaded image
413 * @systable: system table
414 * @return: EFI_ST_SUCCESS for success
416 static int setup(const efi_handle_t handle,
417 const struct efi_system_table *systable)
421 handle_image = handle;
422 boottime = systable->boottime;
424 /* Load the application image into memory */
427 ret = boottime->install_protocol_interface
428 (&handle_volume, &guid_device_path, EFI_NATIVE_INTERFACE,
430 if (ret != EFI_SUCCESS) {
431 efi_st_error("Failed to install device path\n");
432 return EFI_ST_FAILURE;
434 ret = boottime->install_protocol_interface
435 (&handle_volume, &guid_simple_file_system_protocol,
436 EFI_NATIVE_INTERFACE, &file_system);
437 if (ret != EFI_SUCCESS) {
438 efi_st_error("Failed to install simple file system protocol\n");
439 return EFI_ST_FAILURE;
442 return EFI_ST_SUCCESS;
446 * Tear down unit test.
448 * Uninstall protocols and free memory.
450 * @return: EFI_ST_SUCCESS for success
452 static int teardown(void)
454 efi_status_t ret = EFI_ST_SUCCESS;
457 ret = boottime->uninstall_protocol_interface
458 (handle_volume, &guid_simple_file_system_protocol,
460 if (ret != EFI_SUCCESS) {
462 ("Failed to uninstall simple file system protocol\n");
463 return EFI_ST_FAILURE;
465 ret = boottime->uninstall_protocol_interface
466 (handle_volume, &guid_device_path, &dp_volume);
467 if (ret != EFI_SUCCESS) {
469 ("Failed to uninstall device path protocol\n");
470 return EFI_ST_FAILURE;
475 ret = boottime->free_pool(image);
476 if (ret != EFI_SUCCESS) {
477 efi_st_error("Failed to free image\n");
478 return EFI_ST_FAILURE;
487 * Load and start the application image.
489 * @return: EFI_ST_SUCCESS for success
491 static int execute(void)
496 ret = boottime->load_image(false, handle_image, &dp_file.vendor.dp,
498 if (ret != EFI_SUCCESS) {
499 efi_st_error("Failed to load image\n");
500 return EFI_ST_FAILURE;
502 ret = boottime->start_image(handle, NULL, NULL);
503 if (ret != EFI_UNSUPPORTED) {
504 efi_st_error("Wrong return value from application\n");
505 return EFI_ST_FAILURE;
508 if (priv.file_open_count) {
509 efi_st_error("File open count = %d, expected 0\n",
510 priv.file_open_count);
511 return EFI_ST_FAILURE;
513 if (priv.volume_open_count) {
514 efi_st_error("Volume open count = %d, expected 0\n",
515 priv.volume_open_count);
516 return EFI_ST_FAILURE;
519 return EFI_ST_SUCCESS;
522 EFI_UNIT_TEST(loadimage) = {
523 .name = "load image from file",
524 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
527 .teardown = teardown,