Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 5abe1f5

Browse files
libefiwrapper: support LoadImage() and StartImage() Boot Services
The PE/COFF loader was only available in the host section of the project. This patch moves this support to the libefiwrapper making the LoadImage() and StartImage() Boot Services widely available. Change-Id: I5f3b0925d5075578a292b83f2da4ed92d8857184 Signed-off-by: Jeremy Compostella <[email protected]>
1 parent 9aea75b commit 5abe1f5

File tree

10 files changed

+64
-37
lines changed

10 files changed

+64
-37
lines changed

host/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ OBJS := main.o \
99
tcp4.o \
1010
fileio.o \
1111
gop.o \
12-
image.o \
13-
pe.o \
1412
host_time.o \
1513
terminal_conin.o
1614

host/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -54,7 +54,6 @@
5454
#include "tcp4.h"
5555
#include "fileio.h"
5656
#include "gop.h"
57-
#include "image.h"
5857
#include "host_time.h"
5958
#include "terminal_conin.h"
6059

@@ -64,7 +63,6 @@ static ewdrv_t *host_drivers[] = {
6463
&tcp4_drv,
6564
&fileio_drv,
6665
&gop_drv,
67-
&image_drv,
6866
&time_drv,
6967
&terminal_conin_drv,
7068
NULL

include/libefiwrapper/external.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -61,6 +61,9 @@ long int strtol(const char *s, char **nptr, int base);
6161
unsigned long long int strtoull(const char *ptr, char **endptr, int base);
6262
long atol(const char *nptr);
6363

64+
/* malloc.h */
65+
void *memalign(size_t align, size_t size);
66+
6467
/* string.h */
6568
int memcmp(const void *s1, const void *s2, size_t n);
6669
void *memcpy(void *dest, const void *src, size_t n);

libefiwrapper/Android.mk

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ LIBEFIWRAPPER_SRC_FILES := \
2222
ewarg.c \
2323
sdio.c \
2424
ewlib.c \
25-
eraseblk.c
25+
eraseblk.c \
26+
image.c \
27+
pe.c
2628

2729
include $(CLEAR_VARS)
2830
LOCAL_MODULE := libefiwrapper-$(TARGET_BUILD_VARIANT)

libefiwrapper/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ OBJS := ewvar.o \
2525
ewarg.o \
2626
sdio.o \
2727
ewlib.o \
28-
eraseblk.o
28+
eraseblk.o \
29+
image.o \
30+
pe.o
2931

3032
$(EW_LIB): $(OBJS)
3133
$(AR) rcs $@ $^

libefiwrapper/core.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -35,6 +35,7 @@
3535
#include "ewarg.h"
3636
#include "ewlog.h"
3737
#include "ewvar.h"
38+
#include "image.h"
3839
#include "lib.h"
3940
#include "rs.h"
4041
#include "serialio.h"
@@ -90,7 +91,8 @@ static struct component {
9091
{ "console in", conin_init, conin_free },
9192
{ "console out", conout_init, conout_free },
9293
{ "serial", serialio_init, serialio_free },
93-
{ "smbios", smbios_init, smbios_free }
94+
{ "smbios", smbios_init, smbios_free },
95+
{ "image", image_init, image_free }
9496
};
9597

9698
EFI_STATUS set_load_options(int argc, char **argv)

host/image.c renamed to libefiwrapper/image.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -83,7 +83,9 @@ start_image(EFI_HANDLE ImageHandle,
8383
{
8484
EFI_STATUS ret;
8585
image_t *image;
86+
#if defined(HOST)
8687
int setjmpret;
88+
#endif
8789

8890
if (ExitDataSize || ExitData)
8991
return EFI_UNSUPPORTED;
@@ -92,9 +94,11 @@ start_image(EFI_HANDLE ImageHandle,
9294
if (EFI_ERROR(ret))
9395
return ret;
9496

97+
#if defined(HOST)
9598
setjmpret = setjmp(image->jmp);
9699
if (setjmpret != 0)
97100
return image->exit_status;
101+
#endif
98102

99103
return image->entry(ImageHandle, saved_st);
100104
}
@@ -116,8 +120,9 @@ efi_exit(EFI_HANDLE ImageHandle,
116120
return ret;
117121

118122
image->exit_status = ExitStatus;
123+
#if defined(HOST)
119124
longjmp(image->jmp, 1);
120-
125+
#endif
121126
return EFI_SUCCESS;
122127
}
123128

@@ -141,7 +146,7 @@ static EFI_IMAGE_START saved_start_image;
141146
static EFI_EXIT saved_efi_exit;
142147
static EFI_IMAGE_UNLOAD saved_unload_image;
143148

144-
static EFI_STATUS image_init(EFI_SYSTEM_TABLE *st)
149+
EFI_STATUS image_init(EFI_SYSTEM_TABLE *st)
145150
{
146151
if (!st)
147152
return EFI_INVALID_PARAMETER;
@@ -161,7 +166,7 @@ static EFI_STATUS image_init(EFI_SYSTEM_TABLE *st)
161166
return EFI_SUCCESS;
162167
}
163168

164-
static EFI_STATUS image_exit(EFI_SYSTEM_TABLE *st)
169+
EFI_STATUS image_free(EFI_SYSTEM_TABLE *st)
165170
{
166171
if (!st)
167172
return EFI_INVALID_PARAMETER;
@@ -173,10 +178,3 @@ static EFI_STATUS image_exit(EFI_SYSTEM_TABLE *st)
173178

174179
return EFI_SUCCESS;
175180
}
176-
177-
ewdrv_t image_drv = {
178-
.name = "image",
179-
.description = "PE/COFF image",
180-
.init = image_init,
181-
.exit = image_exit
182-
};

host/image.h renamed to libefiwrapper/image.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -32,17 +32,24 @@
3232
#ifndef _IMAGE_H_
3333
#define _IMAGE_H_
3434

35+
#if defined(HOST)
3536
#include <setjmp.h>
37+
#endif
3638
#include <ewdrv.h>
3739

3840
typedef struct image {
3941
EFI_LOADED_IMAGE prot;
4042
void *data;
43+
#if defined(HOST)
4144
jmp_buf jmp;
45+
#endif
4246
EFI_IMAGE_ENTRY_POINT entry;
4347
EFI_STATUS exit_status;
4448
} image_t;
4549

4650
extern ewdrv_t image_drv;
4751

52+
EFI_STATUS image_init(EFI_SYSTEM_TABLE *st);
53+
EFI_STATUS image_free(EFI_SYSTEM_TABLE *st);
54+
4855
#endif /* _IMAGE_H_ */

host/pe.c renamed to libefiwrapper/pe.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>
@@ -29,12 +29,16 @@
2929
* OF THE POSSIBILITY OF SUCH DAMAGE.
3030
*/
3131

32-
#include <stdlib.h>
32+
#include <stddef.h>
3333
#include <stdint.h>
3434
#include <ewlog.h>
3535
#include <ewlib.h>
36+
37+
#if defined(HOST)
3638
#include <sys/mman.h>
39+
#endif
3740

41+
#include "lib.h"
3842
#include "pe.h"
3943

4044
/* The following structure definitions come from:
@@ -99,7 +103,7 @@ typedef struct {
99103
} std_coff_t;
100104

101105
typedef struct {
102-
UINTN image_base;
106+
UINT32 image_base;
103107
UINT32 section_alignment;
104108
UINT32 file_alignment;
105109
UINT16 major_operating_system_version;
@@ -114,14 +118,16 @@ typedef struct {
114118
UINT32 checksum;
115119
UINT16 subsystem;
116120
UINT16 dll_characteristics;
117-
UINTN size_of_stack_reserve;
118-
UINTN size_of_stack_commit;
119-
UINTN size_of_heap_reserve;
120-
UINTN size_of_heap_commit;
121+
UINT32 size_of_stack_reserve;
122+
UINT32 size_of_stack_commit;
123+
UINT32 size_of_heap_reserve;
124+
UINT32 size_of_heap_commit;
121125
UINT32 loader_flags;
122126
UINT32 number_of_rva_and_sizes;
123127
} win_t;
124128

129+
#define BASERELOC_DIRECTORY_ENTRY 5
130+
125131
typedef struct {
126132
coff_header_t hdr;
127133
struct {
@@ -165,14 +171,17 @@ void get_section_boundaries(section_header_t *section, UINT16 nb,
165171
*end = 0;
166172

167173
for (i = 0; i < nb; i++) {
174+
UINT32 size = section[i].virtual_size;
175+
if (section[i].size_of_raw_data)
176+
size = section[i].size_of_raw_data;
168177
*start = min(*start, section[i].virtual_address);
169-
*end = max(*end, section[i].virtual_address +
170-
section[i].virtual_size);
178+
*end = max(*end, section[i].virtual_address + size);
171179
}
172180
}
173181

174-
EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
175-
void *base, image_t *image)
182+
static EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
183+
void *base, image_t *image,
184+
size_t alignment)
176185
{
177186
loaded_section_t *data;
178187
UINT16 i;
@@ -188,9 +197,13 @@ EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
188197
get_section_boundaries(section, nb, &data->start, &data->end);
189198

190199
data->size = data->end - data->start;
200+
#if defined(HOST)
191201
data->addr = mmap(NULL, data->size, PROT_EXEC | PROT_READ | PROT_WRITE,
192202
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
193-
if (!data->addr) {
203+
#else
204+
data->addr = memalign(alignment, data->size);
205+
#endif
206+
if (data->addr == (void *) -1 || data->addr == NULL) {
194207
ewerr("Failed to allocate sections memory");
195208
free(data);
196209
return EFI_OUT_OF_RESOURCES;
@@ -205,7 +218,7 @@ EFI_STATUS load_sections(section_header_t *section, UINT16 nb,
205218
data->start;
206219
size = section[i].virtual_size;
207220

208-
if (!size && section[i].size_of_raw_data)
221+
if (section[i].size_of_raw_data)
209222
size = section[i].size_of_raw_data;
210223
memcpy(dst, src, size);
211224

@@ -248,7 +261,7 @@ EFI_STATUS pe_load(void *data, UINTN size, image_t *image)
248261

249262
ret = load_sections((section_header_t *)(pe + 1),
250263
pe->hdr.number_of_sections,
251-
data, image);
264+
data, image, pe->opt.win.section_alignment);
252265
if (EFI_ERROR(ret))
253266
return ret;
254267

@@ -268,7 +281,11 @@ EFI_STATUS pe_unload(image_t *image)
268281
return EFI_INVALID_PARAMETER;
269282

270283
data = image->data;
284+
#if defined(HOST)
271285
munmap(data->addr, data->size);
286+
#else
287+
free(data->addr);
288+
#endif
272289
free(data);
273290
image->data = NULL;
274291

host/pe.h renamed to libefiwrapper/pe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Intel Corporation
2+
* Copyright (c) 2016-2020, Intel Corporation
33
* All rights reserved.
44
*
55
* Author: Jérémy Compostella <[email protected]>

0 commit comments

Comments
 (0)