blob: 8d119f054c5f74ee79041f0e4ec5feb7315a10cc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +01002/*
3 * efi_selftest_start_image
4 *
5 * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
6 *
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +01007 * This test checks the StartImage boot service.
8 * The efi_selftest_miniapp_exit.efi application is loaded into memory
9 * and started.
10 */
11
12#include <efi_selftest.h>
13/* Include containing the miniapp.efi application */
14#include "efi_miniapp_file_image_exit.h"
15
16/* Block size of compressed disk image */
17#define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
18
19/* Binary logarithm of the block size */
20#define LB_BLOCK_SIZE 9
21
22static efi_handle_t image_handle;
23static struct efi_boot_services *boottime;
24
25/* One 8 byte block of the compressed disk image */
26struct line {
27 size_t addr;
28 char *line;
29};
30
31/* Compressed file image */
32struct compressed_file_image {
33 size_t length;
34 struct line lines[];
35};
36
37static struct compressed_file_image img = EFI_ST_DISK_IMG;
38
39/* Decompressed file image */
40static u8 *image;
41
42/*
43 * Decompress the disk image.
44 *
45 * @image decompressed disk image
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010046 * Return: status code
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +010047 */
48static efi_status_t decompress(u8 **image)
49{
50 u8 *buf;
51 size_t i;
52 size_t addr;
53 size_t len;
54 efi_status_t ret;
55
56 ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
57 (void **)&buf);
58 if (ret != EFI_SUCCESS) {
59 efi_st_error("Out of memory\n");
60 return ret;
61 }
62 boottime->set_mem(buf, img.length, 0);
63
64 for (i = 0; ; ++i) {
65 if (!img.lines[i].line)
66 break;
67 addr = img.lines[i].addr;
68 len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
69 if (addr + len > img.length)
70 len = img.length - addr;
71 boottime->copy_mem(buf + addr, img.lines[i].line, len);
72 }
73 *image = buf;
74 return ret;
75}
76
77/*
78 * Setup unit test.
79 *
80 * @handle: handle of the loaded image
81 * @systable: system table
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +010082 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +010083 */
84static int setup(const efi_handle_t handle,
85 const struct efi_system_table *systable)
86{
Simon Glass182ef1b2024-12-01 08:24:25 -070087 efi_status_t ret;
88
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +010089 image_handle = handle;
90 boottime = systable->boottime;
91
92 /* Load the application image into memory */
Simon Glass182ef1b2024-12-01 08:24:25 -070093 ret = decompress(&image);
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +010094
Simon Glass182ef1b2024-12-01 08:24:25 -070095 return ret;
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +010096}
97
98/*
99 * Tear down unit test.
100 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100101 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100102 */
103static int teardown(void)
104{
105 efi_status_t r = EFI_ST_SUCCESS;
106
107 if (image) {
Heinrich Schuchardt77ff4d82019-02-12 21:50:45 +0100108 r = boottime->free_pool(image);
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100109 if (r != EFI_SUCCESS) {
110 efi_st_error("Failed to free image\n");
111 return EFI_ST_FAILURE;
112 }
113 }
114 return r;
115}
116
117/*
118 * Execute unit test.
119 *
120 * Load and start the application image.
121 *
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100122 * Return: EFI_ST_SUCCESS for success
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100123 */
124static int execute(void)
125{
126 efi_status_t ret;
127 efi_handle_t handle;
Heinrich Schuchardt1696dd02018-09-30 13:26:36 +0200128 efi_uintn_t exit_data_size = 0;
129 u16 *exit_data = NULL;
130 u16 expected_text[] = EFI_ST_SUCCESS_STR;
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100131
132 ret = boottime->load_image(false, image_handle, NULL, image,
133 img.length, &handle);
134 if (ret != EFI_SUCCESS) {
135 efi_st_error("Failed to load image\n");
136 return EFI_ST_FAILURE;
137 }
Heinrich Schuchardt1696dd02018-09-30 13:26:36 +0200138 ret = boottime->start_image(handle, &exit_data_size, &exit_data);
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100139 if (ret != EFI_UNSUPPORTED) {
140 efi_st_error("Wrong return value from application\n");
141 return EFI_ST_FAILURE;
142 }
Heinrich Schuchardt1696dd02018-09-30 13:26:36 +0200143 if (!exit_data || exit_data_size != sizeof(expected_text) ||
Heinrich Schuchardt5f946662019-05-04 19:48:38 +0200144 memcmp(exit_data, expected_text, sizeof(expected_text))) {
Heinrich Schuchardt1696dd02018-09-30 13:26:36 +0200145 efi_st_error("Incorrect exit data\n");
146 return EFI_ST_FAILURE;
147 }
148 ret = boottime->free_pool(exit_data);
149 if (ret != EFI_SUCCESS) {
150 efi_st_error("Failed to free exit data\n");
151 return EFI_ST_FAILURE;
152 }
Heinrich Schuchardte3cde3a2018-01-19 19:01:05 +0100153
154 return EFI_ST_SUCCESS;
155}
156
157EFI_UNIT_TEST(startimage_exit) = {
158 .name = "start image exit",
159 .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
160 .setup = setup,
161 .execute = execute,
162 .teardown = teardown,
163};