1*d787caddSHeinrich Schuchardt // SPDX-License-Identifier: GPL-2.0+
2*d787caddSHeinrich Schuchardt /*
3*d787caddSHeinrich Schuchardt * efi_selftest_loadimage
4*d787caddSHeinrich Schuchardt *
5*d787caddSHeinrich Schuchardt * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
6*d787caddSHeinrich Schuchardt *
7*d787caddSHeinrich Schuchardt * This test checks the LoadImage and StartImage boot service.
8*d787caddSHeinrich Schuchardt *
9*d787caddSHeinrich Schuchardt * The efi_selftest_miniapp_exit.efi application is loaded via a file device
10*d787caddSHeinrich Schuchardt * path and started.
11*d787caddSHeinrich Schuchardt */
12*d787caddSHeinrich Schuchardt
13*d787caddSHeinrich Schuchardt #include <efi_selftest.h>
14*d787caddSHeinrich Schuchardt /* Include containing the efi_selftest_miniapp_exit.efi application */
15*d787caddSHeinrich Schuchardt #include "efi_miniapp_file_image_exit.h"
16*d787caddSHeinrich Schuchardt
17*d787caddSHeinrich Schuchardt /* Block size of compressed disk image */
18*d787caddSHeinrich Schuchardt #define COMPRESSED_DISK_IMAGE_BLOCK_SIZE 8
19*d787caddSHeinrich Schuchardt
20*d787caddSHeinrich Schuchardt /* Binary logarithm of the block size */
21*d787caddSHeinrich Schuchardt #define LB_BLOCK_SIZE 9
22*d787caddSHeinrich Schuchardt
23*d787caddSHeinrich Schuchardt #define FILE_NAME L"app.efi"
24*d787caddSHeinrich Schuchardt #define VOLUME_NAME L"EfiDisk"
25*d787caddSHeinrich Schuchardt
26*d787caddSHeinrich Schuchardt static struct efi_boot_services *boottime;
27*d787caddSHeinrich Schuchardt static efi_handle_t handle_image;
28*d787caddSHeinrich Schuchardt static efi_handle_t handle_volume;
29*d787caddSHeinrich Schuchardt
30*d787caddSHeinrich Schuchardt static const efi_guid_t guid_device_path = DEVICE_PATH_GUID;
31*d787caddSHeinrich Schuchardt static const efi_guid_t guid_simple_file_system_protocol =
32*d787caddSHeinrich Schuchardt EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
33*d787caddSHeinrich Schuchardt static const efi_guid_t guid_file_info = EFI_FILE_INFO_GUID;
34*d787caddSHeinrich Schuchardt static const efi_guid_t guid_file_system_info = EFI_FILE_SYSTEM_INFO_GUID;
35*d787caddSHeinrich Schuchardt
36*d787caddSHeinrich Schuchardt /* One 8 byte block of the compressed disk image */
37*d787caddSHeinrich Schuchardt struct line {
38*d787caddSHeinrich Schuchardt size_t addr;
39*d787caddSHeinrich Schuchardt char *line;
40*d787caddSHeinrich Schuchardt };
41*d787caddSHeinrich Schuchardt
42*d787caddSHeinrich Schuchardt /* Compressed file image */
43*d787caddSHeinrich Schuchardt struct compressed_file_image {
44*d787caddSHeinrich Schuchardt size_t length;
45*d787caddSHeinrich Schuchardt struct line lines[];
46*d787caddSHeinrich Schuchardt };
47*d787caddSHeinrich Schuchardt
48*d787caddSHeinrich Schuchardt /* File info including file name */
49*d787caddSHeinrich Schuchardt struct file_info {
50*d787caddSHeinrich Schuchardt struct efi_file_info info;
51*d787caddSHeinrich Schuchardt u16 file_name[sizeof(FILE_NAME)];
52*d787caddSHeinrich Schuchardt };
53*d787caddSHeinrich Schuchardt
54*d787caddSHeinrich Schuchardt /* File system info including volume name */
55*d787caddSHeinrich Schuchardt struct file_system_info {
56*d787caddSHeinrich Schuchardt struct efi_file_system_info info;
57*d787caddSHeinrich Schuchardt u16 file_name[sizeof(VOLUME_NAME)];
58*d787caddSHeinrich Schuchardt };
59*d787caddSHeinrich Schuchardt
60*d787caddSHeinrich Schuchardt /* Compressed file image */
61*d787caddSHeinrich Schuchardt static struct compressed_file_image img = EFI_ST_DISK_IMG;
62*d787caddSHeinrich Schuchardt
63*d787caddSHeinrich Schuchardt /* Pointer to decompressed file image */
64*d787caddSHeinrich Schuchardt static u8 *image;
65*d787caddSHeinrich Schuchardt
66*d787caddSHeinrich Schuchardt /* File info */
67*d787caddSHeinrich Schuchardt static struct file_info priv_file_info = {
68*d787caddSHeinrich Schuchardt {
69*d787caddSHeinrich Schuchardt .size = sizeof(struct file_info),
70*d787caddSHeinrich Schuchardt .attribute = EFI_FILE_READ_ONLY,
71*d787caddSHeinrich Schuchardt },
72*d787caddSHeinrich Schuchardt FILE_NAME,
73*d787caddSHeinrich Schuchardt };
74*d787caddSHeinrich Schuchardt
75*d787caddSHeinrich Schuchardt /* Pointer to file info */
76*d787caddSHeinrich Schuchardt struct efi_file_info *file_info = &priv_file_info.info;
77*d787caddSHeinrich Schuchardt
78*d787caddSHeinrich Schuchardt /* Volume device path */
79*d787caddSHeinrich Schuchardt static struct {
80*d787caddSHeinrich Schuchardt struct efi_device_path_vendor vendor;
81*d787caddSHeinrich Schuchardt struct efi_device_path end;
82*d787caddSHeinrich Schuchardt } __packed dp_volume = {
83*d787caddSHeinrich Schuchardt .vendor = {
84*d787caddSHeinrich Schuchardt .dp = {
85*d787caddSHeinrich Schuchardt .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
86*d787caddSHeinrich Schuchardt .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
87*d787caddSHeinrich Schuchardt .length = sizeof(struct efi_device_path_vendor),
88*d787caddSHeinrich Schuchardt },
89*d787caddSHeinrich Schuchardt .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
90*d787caddSHeinrich Schuchardt 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
91*d787caddSHeinrich Schuchardt },
92*d787caddSHeinrich Schuchardt .end = {
93*d787caddSHeinrich Schuchardt .type = DEVICE_PATH_TYPE_END,
94*d787caddSHeinrich Schuchardt .sub_type = DEVICE_PATH_SUB_TYPE_END,
95*d787caddSHeinrich Schuchardt .length = sizeof(struct efi_device_path),
96*d787caddSHeinrich Schuchardt }
97*d787caddSHeinrich Schuchardt };
98*d787caddSHeinrich Schuchardt
99*d787caddSHeinrich Schuchardt /* File device path */
100*d787caddSHeinrich Schuchardt static struct {
101*d787caddSHeinrich Schuchardt struct efi_device_path_vendor vendor;
102*d787caddSHeinrich Schuchardt struct efi_device_path path;
103*d787caddSHeinrich Schuchardt u16 file[sizeof(FILE_NAME)];
104*d787caddSHeinrich Schuchardt struct efi_device_path end;
105*d787caddSHeinrich Schuchardt } __packed dp_file = {
106*d787caddSHeinrich Schuchardt .vendor = {
107*d787caddSHeinrich Schuchardt .dp = {
108*d787caddSHeinrich Schuchardt .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
109*d787caddSHeinrich Schuchardt .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
110*d787caddSHeinrich Schuchardt .length = sizeof(struct efi_device_path_vendor),
111*d787caddSHeinrich Schuchardt },
112*d787caddSHeinrich Schuchardt .guid = EFI_GUID(0x4f9a0ebf, 0xa179, 0x88a6, 0x25, 0x68,
113*d787caddSHeinrich Schuchardt 0x10, 0x72, 0xb1, 0x93, 0x51, 0x71),
114*d787caddSHeinrich Schuchardt },
115*d787caddSHeinrich Schuchardt .path = {
116*d787caddSHeinrich Schuchardt .type = DEVICE_PATH_TYPE_MEDIA_DEVICE,
117*d787caddSHeinrich Schuchardt .sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH,
118*d787caddSHeinrich Schuchardt .length = sizeof(struct efi_device_path) + sizeof(dp_file.file),
119*d787caddSHeinrich Schuchardt },
120*d787caddSHeinrich Schuchardt .file = FILE_NAME,
121*d787caddSHeinrich Schuchardt .end = {
122*d787caddSHeinrich Schuchardt .type = DEVICE_PATH_TYPE_END,
123*d787caddSHeinrich Schuchardt .sub_type = DEVICE_PATH_SUB_TYPE_END,
124*d787caddSHeinrich Schuchardt .length = sizeof(struct efi_device_path),
125*d787caddSHeinrich Schuchardt }
126*d787caddSHeinrich Schuchardt };
127*d787caddSHeinrich Schuchardt
128*d787caddSHeinrich Schuchardt /* File system info */
129*d787caddSHeinrich Schuchardt static struct file_system_info priv_file_system_info = {
130*d787caddSHeinrich Schuchardt {
131*d787caddSHeinrich Schuchardt .size = sizeof(struct file_system_info),
132*d787caddSHeinrich Schuchardt .read_only = true,
133*d787caddSHeinrich Schuchardt .volume_size = 0x100000,
134*d787caddSHeinrich Schuchardt .free_space = 0x0,
135*d787caddSHeinrich Schuchardt .block_size = 0x200,
136*d787caddSHeinrich Schuchardt },
137*d787caddSHeinrich Schuchardt VOLUME_NAME
138*d787caddSHeinrich Schuchardt };
139*d787caddSHeinrich Schuchardt
140*d787caddSHeinrich Schuchardt /* Pointer to file system info */
141*d787caddSHeinrich Schuchardt static struct efi_file_system_info *file_system_info =
142*d787caddSHeinrich Schuchardt &priv_file_system_info.info;
143*d787caddSHeinrich Schuchardt
144*d787caddSHeinrich Schuchardt /* Forward definitions of file and file system functions */
145*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI open_volume
146*d787caddSHeinrich Schuchardt (struct efi_simple_file_system_protocol *this,
147*d787caddSHeinrich Schuchardt struct efi_file_handle **root);
148*d787caddSHeinrich Schuchardt
149*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI open
150*d787caddSHeinrich Schuchardt (struct efi_file_handle *this,
151*d787caddSHeinrich Schuchardt struct efi_file_handle **new_handle,
152*d787caddSHeinrich Schuchardt u16 *file_name, u64 open_mode, u64 attributes);
153*d787caddSHeinrich Schuchardt
154*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI close(struct efi_file_handle *this);
155*d787caddSHeinrich Schuchardt
156*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI delete(struct efi_file_handle *this);
157*d787caddSHeinrich Schuchardt
158*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI read
159*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
160*d787caddSHeinrich Schuchardt
161*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI write
162*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer);
163*d787caddSHeinrich Schuchardt
164*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos);
165*d787caddSHeinrich Schuchardt
166*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos);
167*d787caddSHeinrich Schuchardt
168*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI getinfo
169*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, const efi_guid_t *info_type,
170*d787caddSHeinrich Schuchardt efi_uintn_t *buffer_size, void *buffer);
171*d787caddSHeinrich Schuchardt
172*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI setinfo
173*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, const efi_guid_t *info_type,
174*d787caddSHeinrich Schuchardt efi_uintn_t buffer_size, void *buffer);
175*d787caddSHeinrich Schuchardt
176*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI flush(struct efi_file_handle *this);
177*d787caddSHeinrich Schuchardt
178*d787caddSHeinrich Schuchardt /* Internal information about status of file system */
179*d787caddSHeinrich Schuchardt static struct {
180*d787caddSHeinrich Schuchardt /* Difference of volume open count minus volume close count */
181*d787caddSHeinrich Schuchardt int volume_open_count;
182*d787caddSHeinrich Schuchardt /* Difference of file open count minus file close count */
183*d787caddSHeinrich Schuchardt int file_open_count;
184*d787caddSHeinrich Schuchardt /* File size */
185*d787caddSHeinrich Schuchardt u64 file_size;
186*d787caddSHeinrich Schuchardt /* Current position in file */
187*d787caddSHeinrich Schuchardt u64 file_pos;
188*d787caddSHeinrich Schuchardt } priv;
189*d787caddSHeinrich Schuchardt
190*d787caddSHeinrich Schuchardt /* EFI_FILE_PROTOCOL for file */
191*d787caddSHeinrich Schuchardt static struct efi_file_handle file = {
192*d787caddSHeinrich Schuchardt .rev = 0x00010000,
193*d787caddSHeinrich Schuchardt .open = open,
194*d787caddSHeinrich Schuchardt .close = close,
195*d787caddSHeinrich Schuchardt .delete = delete,
196*d787caddSHeinrich Schuchardt .read = read,
197*d787caddSHeinrich Schuchardt .write = write,
198*d787caddSHeinrich Schuchardt .getpos = getpos,
199*d787caddSHeinrich Schuchardt .setpos = setpos,
200*d787caddSHeinrich Schuchardt .getinfo = getinfo,
201*d787caddSHeinrich Schuchardt .setinfo = setinfo,
202*d787caddSHeinrich Schuchardt .flush = flush,
203*d787caddSHeinrich Schuchardt };
204*d787caddSHeinrich Schuchardt
205*d787caddSHeinrich Schuchardt /* EFI_FILE_PROTOCOL for root directory */
206*d787caddSHeinrich Schuchardt static struct efi_file_handle volume = {
207*d787caddSHeinrich Schuchardt .rev = 0x00010000,
208*d787caddSHeinrich Schuchardt .open = open,
209*d787caddSHeinrich Schuchardt .close = close,
210*d787caddSHeinrich Schuchardt .delete = delete,
211*d787caddSHeinrich Schuchardt .read = read,
212*d787caddSHeinrich Schuchardt .write = write,
213*d787caddSHeinrich Schuchardt .getpos = getpos,
214*d787caddSHeinrich Schuchardt .setpos = setpos,
215*d787caddSHeinrich Schuchardt .getinfo = getinfo,
216*d787caddSHeinrich Schuchardt .setinfo = setinfo,
217*d787caddSHeinrich Schuchardt .flush = flush,
218*d787caddSHeinrich Schuchardt };
219*d787caddSHeinrich Schuchardt
220*d787caddSHeinrich Schuchardt /* EFI_SIMPLE_FILE_SYSTEM_PROTOCOL of the block device */
221*d787caddSHeinrich Schuchardt struct efi_simple_file_system_protocol file_system = {
222*d787caddSHeinrich Schuchardt .rev = 0x00010000,
223*d787caddSHeinrich Schuchardt .open_volume = open_volume,
224*d787caddSHeinrich Schuchardt };
225*d787caddSHeinrich Schuchardt
open_volume(struct efi_simple_file_system_protocol * this,struct efi_file_handle ** root)226*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI open_volume
227*d787caddSHeinrich Schuchardt (struct efi_simple_file_system_protocol *this,
228*d787caddSHeinrich Schuchardt struct efi_file_handle **root)
229*d787caddSHeinrich Schuchardt {
230*d787caddSHeinrich Schuchardt if (this != &file_system || !root)
231*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
232*d787caddSHeinrich Schuchardt
233*d787caddSHeinrich Schuchardt *root = &volume;
234*d787caddSHeinrich Schuchardt priv.volume_open_count++;
235*d787caddSHeinrich Schuchardt
236*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
237*d787caddSHeinrich Schuchardt }
238*d787caddSHeinrich Schuchardt
open(struct efi_file_handle * this,struct efi_file_handle ** new_handle,u16 * file_name,u64 open_mode,u64 attributes)239*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI open
240*d787caddSHeinrich Schuchardt (struct efi_file_handle *this,
241*d787caddSHeinrich Schuchardt struct efi_file_handle **new_handle,
242*d787caddSHeinrich Schuchardt u16 *file_name, u64 open_mode, u64 attributes)
243*d787caddSHeinrich Schuchardt {
244*d787caddSHeinrich Schuchardt if (this != &volume)
245*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
246*d787caddSHeinrich Schuchardt
247*d787caddSHeinrich Schuchardt *new_handle = &file;
248*d787caddSHeinrich Schuchardt priv.file_pos = 0;
249*d787caddSHeinrich Schuchardt priv.file_open_count++;
250*d787caddSHeinrich Schuchardt
251*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
252*d787caddSHeinrich Schuchardt }
253*d787caddSHeinrich Schuchardt
close(struct efi_file_handle * this)254*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI close(struct efi_file_handle *this)
255*d787caddSHeinrich Schuchardt {
256*d787caddSHeinrich Schuchardt if (this == &file)
257*d787caddSHeinrich Schuchardt priv.file_open_count--;
258*d787caddSHeinrich Schuchardt else if (this == &volume)
259*d787caddSHeinrich Schuchardt priv.volume_open_count--;
260*d787caddSHeinrich Schuchardt else
261*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
262*d787caddSHeinrich Schuchardt
263*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
264*d787caddSHeinrich Schuchardt }
265*d787caddSHeinrich Schuchardt
delete(struct efi_file_handle * this)266*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI delete(struct efi_file_handle *this)
267*d787caddSHeinrich Schuchardt {
268*d787caddSHeinrich Schuchardt if (this != &file)
269*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
270*d787caddSHeinrich Schuchardt
271*d787caddSHeinrich Schuchardt return EFI_UNSUPPORTED;
272*d787caddSHeinrich Schuchardt }
273*d787caddSHeinrich Schuchardt
read(struct efi_file_handle * this,efi_uintn_t * buffer_size,void * buffer)274*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI read
275*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
276*d787caddSHeinrich Schuchardt {
277*d787caddSHeinrich Schuchardt if (this != &file)
278*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
279*d787caddSHeinrich Schuchardt
280*d787caddSHeinrich Schuchardt if (priv.file_pos >= img.length)
281*d787caddSHeinrich Schuchardt *buffer_size = 0;
282*d787caddSHeinrich Schuchardt else if (priv.file_pos + *buffer_size > img.length)
283*d787caddSHeinrich Schuchardt *buffer_size = img.length - priv.file_pos;
284*d787caddSHeinrich Schuchardt
285*d787caddSHeinrich Schuchardt boottime->copy_mem(buffer, &image[priv.file_pos], *buffer_size);
286*d787caddSHeinrich Schuchardt priv.file_pos += *buffer_size;
287*d787caddSHeinrich Schuchardt
288*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
289*d787caddSHeinrich Schuchardt }
290*d787caddSHeinrich Schuchardt
write(struct efi_file_handle * this,efi_uintn_t * buffer_size,void * buffer)291*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI write
292*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, efi_uintn_t *buffer_size, void *buffer)
293*d787caddSHeinrich Schuchardt {
294*d787caddSHeinrich Schuchardt if (this != &file)
295*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
296*d787caddSHeinrich Schuchardt
297*d787caddSHeinrich Schuchardt return EFI_UNSUPPORTED;
298*d787caddSHeinrich Schuchardt }
299*d787caddSHeinrich Schuchardt
getpos(struct efi_file_handle * this,u64 * pos)300*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI getpos(struct efi_file_handle *this, u64 *pos)
301*d787caddSHeinrich Schuchardt {
302*d787caddSHeinrich Schuchardt if (this != &file)
303*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
304*d787caddSHeinrich Schuchardt
305*d787caddSHeinrich Schuchardt *pos = priv.file_pos;
306*d787caddSHeinrich Schuchardt
307*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
308*d787caddSHeinrich Schuchardt }
309*d787caddSHeinrich Schuchardt
setpos(struct efi_file_handle * this,u64 pos)310*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI setpos(struct efi_file_handle *this, u64 pos)
311*d787caddSHeinrich Schuchardt {
312*d787caddSHeinrich Schuchardt if (this != &file)
313*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
314*d787caddSHeinrich Schuchardt
315*d787caddSHeinrich Schuchardt priv.file_pos = pos;
316*d787caddSHeinrich Schuchardt
317*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
318*d787caddSHeinrich Schuchardt }
319*d787caddSHeinrich Schuchardt
getinfo(struct efi_file_handle * this,const efi_guid_t * info_type,efi_uintn_t * buffer_size,void * buffer)320*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI getinfo
321*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, const efi_guid_t *info_type,
322*d787caddSHeinrich Schuchardt efi_uintn_t *buffer_size, void *buffer)
323*d787caddSHeinrich Schuchardt {
324*d787caddSHeinrich Schuchardt if (this == &file) {
325*d787caddSHeinrich Schuchardt if (efi_st_memcmp(info_type, &guid_file_info,
326*d787caddSHeinrich Schuchardt sizeof(efi_guid_t)))
327*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
328*d787caddSHeinrich Schuchardt if (*buffer_size >= sizeof(struct file_info)) {
329*d787caddSHeinrich Schuchardt boottime->copy_mem(buffer, file_info,
330*d787caddSHeinrich Schuchardt sizeof(struct file_info));
331*d787caddSHeinrich Schuchardt } else {
332*d787caddSHeinrich Schuchardt *buffer_size = sizeof(struct file_info);
333*d787caddSHeinrich Schuchardt return EFI_BUFFER_TOO_SMALL;
334*d787caddSHeinrich Schuchardt }
335*d787caddSHeinrich Schuchardt } else if (this == &volume) {
336*d787caddSHeinrich Schuchardt if (efi_st_memcmp(info_type, &guid_file_system_info,
337*d787caddSHeinrich Schuchardt sizeof(efi_guid_t)))
338*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
339*d787caddSHeinrich Schuchardt if (*buffer_size >= sizeof(struct file_system_info)) {
340*d787caddSHeinrich Schuchardt boottime->copy_mem(buffer, file_system_info,
341*d787caddSHeinrich Schuchardt sizeof(struct file_system_info));
342*d787caddSHeinrich Schuchardt } else {
343*d787caddSHeinrich Schuchardt *buffer_size = sizeof(struct file_system_info);
344*d787caddSHeinrich Schuchardt return EFI_BUFFER_TOO_SMALL;
345*d787caddSHeinrich Schuchardt }
346*d787caddSHeinrich Schuchardt } else {
347*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
348*d787caddSHeinrich Schuchardt }
349*d787caddSHeinrich Schuchardt return EFI_SUCCESS;
350*d787caddSHeinrich Schuchardt }
351*d787caddSHeinrich Schuchardt
setinfo(struct efi_file_handle * this,const efi_guid_t * info_type,efi_uintn_t buffer_size,void * buffer)352*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI setinfo
353*d787caddSHeinrich Schuchardt (struct efi_file_handle *this, const efi_guid_t *info_type,
354*d787caddSHeinrich Schuchardt efi_uintn_t buffer_size, void *buffer)
355*d787caddSHeinrich Schuchardt {
356*d787caddSHeinrich Schuchardt if (this != &file)
357*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
358*d787caddSHeinrich Schuchardt
359*d787caddSHeinrich Schuchardt return EFI_UNSUPPORTED;
360*d787caddSHeinrich Schuchardt }
361*d787caddSHeinrich Schuchardt
flush(struct efi_file_handle * this)362*d787caddSHeinrich Schuchardt static efi_status_t EFIAPI flush(struct efi_file_handle *this)
363*d787caddSHeinrich Schuchardt {
364*d787caddSHeinrich Schuchardt if (this != &file)
365*d787caddSHeinrich Schuchardt return EFI_INVALID_PARAMETER;
366*d787caddSHeinrich Schuchardt
367*d787caddSHeinrich Schuchardt return EFI_UNSUPPORTED;
368*d787caddSHeinrich Schuchardt }
369*d787caddSHeinrich Schuchardt
370*d787caddSHeinrich Schuchardt /*
371*d787caddSHeinrich Schuchardt * Decompress the disk image.
372*d787caddSHeinrich Schuchardt *
373*d787caddSHeinrich Schuchardt * @image decompressed disk image
374*d787caddSHeinrich Schuchardt * @return status code
375*d787caddSHeinrich Schuchardt */
decompress(u8 ** image)376*d787caddSHeinrich Schuchardt static efi_status_t decompress(u8 **image)
377*d787caddSHeinrich Schuchardt {
378*d787caddSHeinrich Schuchardt u8 *buf;
379*d787caddSHeinrich Schuchardt size_t i;
380*d787caddSHeinrich Schuchardt size_t addr;
381*d787caddSHeinrich Schuchardt size_t len;
382*d787caddSHeinrich Schuchardt efi_status_t ret;
383*d787caddSHeinrich Schuchardt
384*d787caddSHeinrich Schuchardt ret = boottime->allocate_pool(EFI_LOADER_DATA, img.length,
385*d787caddSHeinrich Schuchardt (void **)&buf);
386*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
387*d787caddSHeinrich Schuchardt efi_st_error("Out of memory\n");
388*d787caddSHeinrich Schuchardt return ret;
389*d787caddSHeinrich Schuchardt }
390*d787caddSHeinrich Schuchardt boottime->set_mem(buf, img.length, 0);
391*d787caddSHeinrich Schuchardt
392*d787caddSHeinrich Schuchardt for (i = 0; ; ++i) {
393*d787caddSHeinrich Schuchardt if (!img.lines[i].line)
394*d787caddSHeinrich Schuchardt break;
395*d787caddSHeinrich Schuchardt addr = img.lines[i].addr;
396*d787caddSHeinrich Schuchardt len = COMPRESSED_DISK_IMAGE_BLOCK_SIZE;
397*d787caddSHeinrich Schuchardt if (addr + len > img.length)
398*d787caddSHeinrich Schuchardt len = img.length - addr;
399*d787caddSHeinrich Schuchardt boottime->copy_mem(buf + addr, img.lines[i].line, len);
400*d787caddSHeinrich Schuchardt }
401*d787caddSHeinrich Schuchardt *image = buf;
402*d787caddSHeinrich Schuchardt priv.file_size = img.length;
403*d787caddSHeinrich Schuchardt file_info->file_size = img.length;
404*d787caddSHeinrich Schuchardt return ret;
405*d787caddSHeinrich Schuchardt }
406*d787caddSHeinrich Schuchardt
407*d787caddSHeinrich Schuchardt /*
408*d787caddSHeinrich Schuchardt * Setup unit test.
409*d787caddSHeinrich Schuchardt *
410*d787caddSHeinrich Schuchardt * Decompress application image and provide a handle for the in memory block
411*d787caddSHeinrich Schuchardt * device.
412*d787caddSHeinrich Schuchardt *
413*d787caddSHeinrich Schuchardt * @handle: handle of the loaded image
414*d787caddSHeinrich Schuchardt * @systable: system table
415*d787caddSHeinrich Schuchardt * @return: EFI_ST_SUCCESS for success
416*d787caddSHeinrich Schuchardt */
setup(const efi_handle_t handle,const struct efi_system_table * systable)417*d787caddSHeinrich Schuchardt static int setup(const efi_handle_t handle,
418*d787caddSHeinrich Schuchardt const struct efi_system_table *systable)
419*d787caddSHeinrich Schuchardt {
420*d787caddSHeinrich Schuchardt efi_status_t ret;
421*d787caddSHeinrich Schuchardt
422*d787caddSHeinrich Schuchardt handle_image = handle;
423*d787caddSHeinrich Schuchardt boottime = systable->boottime;
424*d787caddSHeinrich Schuchardt
425*d787caddSHeinrich Schuchardt /* Load the application image into memory */
426*d787caddSHeinrich Schuchardt decompress(&image);
427*d787caddSHeinrich Schuchardt
428*d787caddSHeinrich Schuchardt ret = boottime->install_protocol_interface
429*d787caddSHeinrich Schuchardt (&handle_volume, &guid_device_path, EFI_NATIVE_INTERFACE,
430*d787caddSHeinrich Schuchardt &dp_volume);
431*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
432*d787caddSHeinrich Schuchardt efi_st_error("Failed to install device path\n");
433*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
434*d787caddSHeinrich Schuchardt }
435*d787caddSHeinrich Schuchardt ret = boottime->install_protocol_interface
436*d787caddSHeinrich Schuchardt (&handle_volume, &guid_simple_file_system_protocol,
437*d787caddSHeinrich Schuchardt EFI_NATIVE_INTERFACE, &file_system);
438*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
439*d787caddSHeinrich Schuchardt efi_st_error("Failed to install simple file system protocol\n");
440*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
441*d787caddSHeinrich Schuchardt }
442*d787caddSHeinrich Schuchardt
443*d787caddSHeinrich Schuchardt return EFI_ST_SUCCESS;
444*d787caddSHeinrich Schuchardt }
445*d787caddSHeinrich Schuchardt
446*d787caddSHeinrich Schuchardt /*
447*d787caddSHeinrich Schuchardt * Tear down unit test.
448*d787caddSHeinrich Schuchardt *
449*d787caddSHeinrich Schuchardt * Uninstall protocols and free memory.
450*d787caddSHeinrich Schuchardt *
451*d787caddSHeinrich Schuchardt * @return: EFI_ST_SUCCESS for success
452*d787caddSHeinrich Schuchardt */
teardown(void)453*d787caddSHeinrich Schuchardt static int teardown(void)
454*d787caddSHeinrich Schuchardt {
455*d787caddSHeinrich Schuchardt efi_status_t ret = EFI_ST_SUCCESS;
456*d787caddSHeinrich Schuchardt
457*d787caddSHeinrich Schuchardt if (handle_volume) {
458*d787caddSHeinrich Schuchardt ret = boottime->uninstall_protocol_interface
459*d787caddSHeinrich Schuchardt (handle_volume, &guid_simple_file_system_protocol,
460*d787caddSHeinrich Schuchardt &file_system);
461*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
462*d787caddSHeinrich Schuchardt efi_st_error
463*d787caddSHeinrich Schuchardt ("Failed to uninstall simple file system protocol\n");
464*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
465*d787caddSHeinrich Schuchardt }
466*d787caddSHeinrich Schuchardt ret = boottime->uninstall_protocol_interface
467*d787caddSHeinrich Schuchardt (handle_volume, &guid_device_path, &dp_volume);
468*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
469*d787caddSHeinrich Schuchardt efi_st_error
470*d787caddSHeinrich Schuchardt ("Failed to uninstall device path protocol\n");
471*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
472*d787caddSHeinrich Schuchardt }
473*d787caddSHeinrich Schuchardt }
474*d787caddSHeinrich Schuchardt
475*d787caddSHeinrich Schuchardt if (image) {
476*d787caddSHeinrich Schuchardt ret = boottime->free_pool(image);
477*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
478*d787caddSHeinrich Schuchardt efi_st_error("Failed to free image\n");
479*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
480*d787caddSHeinrich Schuchardt }
481*d787caddSHeinrich Schuchardt }
482*d787caddSHeinrich Schuchardt return ret;
483*d787caddSHeinrich Schuchardt }
484*d787caddSHeinrich Schuchardt
485*d787caddSHeinrich Schuchardt /*
486*d787caddSHeinrich Schuchardt * Execute unit test.
487*d787caddSHeinrich Schuchardt *
488*d787caddSHeinrich Schuchardt * Load and start the application image.
489*d787caddSHeinrich Schuchardt *
490*d787caddSHeinrich Schuchardt * @return: EFI_ST_SUCCESS for success
491*d787caddSHeinrich Schuchardt */
execute(void)492*d787caddSHeinrich Schuchardt static int execute(void)
493*d787caddSHeinrich Schuchardt {
494*d787caddSHeinrich Schuchardt efi_status_t ret;
495*d787caddSHeinrich Schuchardt efi_handle_t handle;
496*d787caddSHeinrich Schuchardt
497*d787caddSHeinrich Schuchardt ret = boottime->load_image(false, handle_image, &dp_file.vendor.dp,
498*d787caddSHeinrich Schuchardt NULL, 0, &handle);
499*d787caddSHeinrich Schuchardt if (ret != EFI_SUCCESS) {
500*d787caddSHeinrich Schuchardt efi_st_error("Failed to load image\n");
501*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
502*d787caddSHeinrich Schuchardt }
503*d787caddSHeinrich Schuchardt ret = boottime->start_image(handle, NULL, NULL);
504*d787caddSHeinrich Schuchardt if (ret != EFI_UNSUPPORTED) {
505*d787caddSHeinrich Schuchardt efi_st_error("Wrong return value from application\n");
506*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
507*d787caddSHeinrich Schuchardt }
508*d787caddSHeinrich Schuchardt
509*d787caddSHeinrich Schuchardt if (priv.file_open_count) {
510*d787caddSHeinrich Schuchardt efi_st_error("File open count = %d, expected 0\n",
511*d787caddSHeinrich Schuchardt priv.file_open_count);
512*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
513*d787caddSHeinrich Schuchardt }
514*d787caddSHeinrich Schuchardt if (priv.volume_open_count) {
515*d787caddSHeinrich Schuchardt efi_st_error("Volume open count = %d, expected 0\n",
516*d787caddSHeinrich Schuchardt priv.volume_open_count);
517*d787caddSHeinrich Schuchardt return EFI_ST_FAILURE;
518*d787caddSHeinrich Schuchardt }
519*d787caddSHeinrich Schuchardt
520*d787caddSHeinrich Schuchardt return EFI_ST_SUCCESS;
521*d787caddSHeinrich Schuchardt }
522*d787caddSHeinrich Schuchardt
523*d787caddSHeinrich Schuchardt EFI_UNIT_TEST(loadimage) = {
524*d787caddSHeinrich Schuchardt .name = "load image from file",
525*d787caddSHeinrich Schuchardt .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
526*d787caddSHeinrich Schuchardt .setup = setup,
527*d787caddSHeinrich Schuchardt .execute = execute,
528*d787caddSHeinrich Schuchardt .teardown = teardown,
529*d787caddSHeinrich Schuchardt };
530