Commit 1db561e11f2b6b6cd8f64035cc12b067dd6c1f91
1 parent
1504bb0d96
Exists in
smarc_8mq_lf_v2020.04
and in
11 other branches
efi_loader: documentation of image loader
- Add missing function descriptions. - Update existing function descriptions to match Sphinx style. - Add lib/efi_loader/efi_image_loader.c to the input files for Sphinx generated documentation. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Showing 2 changed files with 25 additions and 9 deletions Inline Diff
Documentation/efi.rst
1 | .. SPDX-License-Identifier: GPL-2.0+ | 1 | .. SPDX-License-Identifier: GPL-2.0+ |
2 | 2 | ||
3 | EFI subsystem | 3 | EFI subsystem |
4 | ============= | 4 | ============= |
5 | 5 | ||
6 | Boot services | 6 | Boot services |
7 | ------------- | 7 | ------------- |
8 | 8 | ||
9 | .. kernel-doc:: lib/efi_loader/efi_boottime.c | 9 | .. kernel-doc:: lib/efi_loader/efi_boottime.c |
10 | :internal: | 10 | :internal: |
11 | 11 | ||
12 | Image relocation | ||
13 | ~~~~~~~~~~~~~~~~ | ||
14 | |||
15 | .. kernel-doc:: lib/efi_loader/efi_image_loader.c | ||
16 | :internal: | ||
17 | |||
12 | Runtime services | 18 | Runtime services |
13 | ---------------- | 19 | ---------------- |
14 | 20 | ||
15 | .. kernel-doc:: lib/efi_loader/efi_runtime.c | 21 | .. kernel-doc:: lib/efi_loader/efi_runtime.c |
16 | :internal: | 22 | :internal: |
17 | 23 |
lib/efi_loader/efi_image_loader.c
1 | // SPDX-License-Identifier: GPL-2.0+ | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | 2 | /* |
3 | * EFI image loader | 3 | * EFI image loader |
4 | * | 4 | * |
5 | * based partly on wine code | 5 | * based partly on wine code |
6 | * | 6 | * |
7 | * Copyright (c) 2016 Alexander Graf | 7 | * Copyright (c) 2016 Alexander Graf |
8 | */ | 8 | */ |
9 | 9 | ||
10 | #include <common.h> | 10 | #include <common.h> |
11 | #include <efi_loader.h> | 11 | #include <efi_loader.h> |
12 | #include <pe.h> | 12 | #include <pe.h> |
13 | 13 | ||
14 | const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID; | 14 | const efi_guid_t efi_global_variable_guid = EFI_GLOBAL_VARIABLE_GUID; |
15 | const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID; | 15 | const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID; |
16 | const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID; | 16 | const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID; |
17 | const efi_guid_t efi_simple_file_system_protocol_guid = | 17 | const efi_guid_t efi_simple_file_system_protocol_guid = |
18 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; | 18 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; |
19 | const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; | 19 | const efi_guid_t efi_file_info_guid = EFI_FILE_INFO_GUID; |
20 | 20 | ||
21 | static int machines[] = { | 21 | static int machines[] = { |
22 | #if defined(__aarch64__) | 22 | #if defined(__aarch64__) |
23 | IMAGE_FILE_MACHINE_ARM64, | 23 | IMAGE_FILE_MACHINE_ARM64, |
24 | #elif defined(__arm__) | 24 | #elif defined(__arm__) |
25 | IMAGE_FILE_MACHINE_ARM, | 25 | IMAGE_FILE_MACHINE_ARM, |
26 | IMAGE_FILE_MACHINE_THUMB, | 26 | IMAGE_FILE_MACHINE_THUMB, |
27 | IMAGE_FILE_MACHINE_ARMNT, | 27 | IMAGE_FILE_MACHINE_ARMNT, |
28 | #endif | 28 | #endif |
29 | 29 | ||
30 | #if defined(__x86_64__) | 30 | #if defined(__x86_64__) |
31 | IMAGE_FILE_MACHINE_AMD64, | 31 | IMAGE_FILE_MACHINE_AMD64, |
32 | #elif defined(__i386__) | 32 | #elif defined(__i386__) |
33 | IMAGE_FILE_MACHINE_I386, | 33 | IMAGE_FILE_MACHINE_I386, |
34 | #endif | 34 | #endif |
35 | 35 | ||
36 | #if defined(__riscv) && (__riscv_xlen == 32) | 36 | #if defined(__riscv) && (__riscv_xlen == 32) |
37 | IMAGE_FILE_MACHINE_RISCV32, | 37 | IMAGE_FILE_MACHINE_RISCV32, |
38 | #endif | 38 | #endif |
39 | 39 | ||
40 | #if defined(__riscv) && (__riscv_xlen == 64) | 40 | #if defined(__riscv) && (__riscv_xlen == 64) |
41 | IMAGE_FILE_MACHINE_RISCV64, | 41 | IMAGE_FILE_MACHINE_RISCV64, |
42 | #endif | 42 | #endif |
43 | 0 }; | 43 | 0 }; |
44 | 44 | ||
45 | /* | 45 | /** |
46 | * Print information about a loaded image. | 46 | * efi_print_image_info() - print information about a loaded image |
47 | * | 47 | * |
48 | * If the program counter is located within the image the offset to the base | 48 | * If the program counter is located within the image the offset to the base |
49 | * address is shown. | 49 | * address is shown. |
50 | * | 50 | * |
51 | * @obj: EFI object | 51 | * @obj: EFI object |
52 | * @image: loaded image | 52 | * @image: loaded image |
53 | * @pc: program counter (use NULL to suppress offset output) | 53 | * @pc: program counter (use NULL to suppress offset output) |
54 | * @return: status code | 54 | * Return: status code |
55 | */ | 55 | */ |
56 | static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj, | 56 | static efi_status_t efi_print_image_info(struct efi_loaded_image_obj *obj, |
57 | struct efi_loaded_image *image, | 57 | struct efi_loaded_image *image, |
58 | void *pc) | 58 | void *pc) |
59 | { | 59 | { |
60 | printf("UEFI image"); | 60 | printf("UEFI image"); |
61 | printf(" [0x%p:0x%p]", | 61 | printf(" [0x%p:0x%p]", |
62 | obj->reloc_base, obj->reloc_base + obj->reloc_size - 1); | 62 | obj->reloc_base, obj->reloc_base + obj->reloc_size - 1); |
63 | if (pc && pc >= obj->reloc_base && | 63 | if (pc && pc >= obj->reloc_base && |
64 | pc < obj->reloc_base + obj->reloc_size) | 64 | pc < obj->reloc_base + obj->reloc_size) |
65 | printf(" pc=0x%zx", pc - obj->reloc_base); | 65 | printf(" pc=0x%zx", pc - obj->reloc_base); |
66 | if (image->file_path) | 66 | if (image->file_path) |
67 | printf(" '%pD'", image->file_path); | 67 | printf(" '%pD'", image->file_path); |
68 | printf("\n"); | 68 | printf("\n"); |
69 | return EFI_SUCCESS; | 69 | return EFI_SUCCESS; |
70 | } | 70 | } |
71 | 71 | ||
72 | /* | 72 | /** |
73 | * Print information about all loaded images. | 73 | * efi_print_image_infos() - print information about all loaded images |
74 | * | 74 | * |
75 | * @pc: program counter (use NULL to suppress offset output) | 75 | * @pc: program counter (use NULL to suppress offset output) |
76 | */ | 76 | */ |
77 | void efi_print_image_infos(void *pc) | 77 | void efi_print_image_infos(void *pc) |
78 | { | 78 | { |
79 | struct efi_object *efiobj; | 79 | struct efi_object *efiobj; |
80 | struct efi_handler *handler; | 80 | struct efi_handler *handler; |
81 | 81 | ||
82 | list_for_each_entry(efiobj, &efi_obj_list, link) { | 82 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
83 | list_for_each_entry(handler, &efiobj->protocols, link) { | 83 | list_for_each_entry(handler, &efiobj->protocols, link) { |
84 | if (!guidcmp(handler->guid, &efi_guid_loaded_image)) { | 84 | if (!guidcmp(handler->guid, &efi_guid_loaded_image)) { |
85 | efi_print_image_info( | 85 | efi_print_image_info( |
86 | (struct efi_loaded_image_obj *)efiobj, | 86 | (struct efi_loaded_image_obj *)efiobj, |
87 | handler->protocol_interface, pc); | 87 | handler->protocol_interface, pc); |
88 | } | 88 | } |
89 | } | 89 | } |
90 | } | 90 | } |
91 | } | 91 | } |
92 | 92 | ||
93 | /** | ||
94 | * efi_loader_relocate() - relocate UEFI binary | ||
95 | * | ||
96 | * @rel: pointer to the relocation table | ||
97 | * @rel_size: size of the relocation table in bytes | ||
98 | * @efi_reloc: actual load address of the image | ||
99 | * @pref_address: preferred load address of the image | ||
100 | * Return: status code | ||
101 | */ | ||
93 | static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, | 102 | static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel, |
94 | unsigned long rel_size, void *efi_reloc, | 103 | unsigned long rel_size, void *efi_reloc, |
95 | unsigned long pref_address) | 104 | unsigned long pref_address) |
96 | { | 105 | { |
97 | unsigned long delta = (unsigned long)efi_reloc - pref_address; | 106 | unsigned long delta = (unsigned long)efi_reloc - pref_address; |
98 | const IMAGE_BASE_RELOCATION *end; | 107 | const IMAGE_BASE_RELOCATION *end; |
99 | int i; | 108 | int i; |
100 | 109 | ||
101 | if (delta == 0) | 110 | if (delta == 0) |
102 | return EFI_SUCCESS; | 111 | return EFI_SUCCESS; |
103 | 112 | ||
104 | end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); | 113 | end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size); |
105 | while (rel < end - 1 && rel->SizeOfBlock) { | 114 | while (rel < end - 1 && rel->SizeOfBlock) { |
106 | const uint16_t *relocs = (const uint16_t *)(rel + 1); | 115 | const uint16_t *relocs = (const uint16_t *)(rel + 1); |
107 | i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); | 116 | i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t); |
108 | while (i--) { | 117 | while (i--) { |
109 | uint32_t offset = (uint32_t)(*relocs & 0xfff) + | 118 | uint32_t offset = (uint32_t)(*relocs & 0xfff) + |
110 | rel->VirtualAddress; | 119 | rel->VirtualAddress; |
111 | int type = *relocs >> EFI_PAGE_SHIFT; | 120 | int type = *relocs >> EFI_PAGE_SHIFT; |
112 | uint64_t *x64 = efi_reloc + offset; | 121 | uint64_t *x64 = efi_reloc + offset; |
113 | uint32_t *x32 = efi_reloc + offset; | 122 | uint32_t *x32 = efi_reloc + offset; |
114 | uint16_t *x16 = efi_reloc + offset; | 123 | uint16_t *x16 = efi_reloc + offset; |
115 | 124 | ||
116 | switch (type) { | 125 | switch (type) { |
117 | case IMAGE_REL_BASED_ABSOLUTE: | 126 | case IMAGE_REL_BASED_ABSOLUTE: |
118 | break; | 127 | break; |
119 | case IMAGE_REL_BASED_HIGH: | 128 | case IMAGE_REL_BASED_HIGH: |
120 | *x16 += ((uint32_t)delta) >> 16; | 129 | *x16 += ((uint32_t)delta) >> 16; |
121 | break; | 130 | break; |
122 | case IMAGE_REL_BASED_LOW: | 131 | case IMAGE_REL_BASED_LOW: |
123 | *x16 += (uint16_t)delta; | 132 | *x16 += (uint16_t)delta; |
124 | break; | 133 | break; |
125 | case IMAGE_REL_BASED_HIGHLOW: | 134 | case IMAGE_REL_BASED_HIGHLOW: |
126 | *x32 += (uint32_t)delta; | 135 | *x32 += (uint32_t)delta; |
127 | break; | 136 | break; |
128 | case IMAGE_REL_BASED_DIR64: | 137 | case IMAGE_REL_BASED_DIR64: |
129 | *x64 += (uint64_t)delta; | 138 | *x64 += (uint64_t)delta; |
130 | break; | 139 | break; |
131 | #ifdef __riscv | 140 | #ifdef __riscv |
132 | case IMAGE_REL_BASED_RISCV_HI20: | 141 | case IMAGE_REL_BASED_RISCV_HI20: |
133 | *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) | | 142 | *x32 = ((*x32 & 0xfffff000) + (uint32_t)delta) | |
134 | (*x32 & 0x00000fff); | 143 | (*x32 & 0x00000fff); |
135 | break; | 144 | break; |
136 | case IMAGE_REL_BASED_RISCV_LOW12I: | 145 | case IMAGE_REL_BASED_RISCV_LOW12I: |
137 | case IMAGE_REL_BASED_RISCV_LOW12S: | 146 | case IMAGE_REL_BASED_RISCV_LOW12S: |
138 | /* We know that we're 4k aligned */ | 147 | /* We know that we're 4k aligned */ |
139 | if (delta & 0xfff) { | 148 | if (delta & 0xfff) { |
140 | printf("Unsupported reloc offset\n"); | 149 | printf("Unsupported reloc offset\n"); |
141 | return EFI_LOAD_ERROR; | 150 | return EFI_LOAD_ERROR; |
142 | } | 151 | } |
143 | break; | 152 | break; |
144 | #endif | 153 | #endif |
145 | default: | 154 | default: |
146 | printf("Unknown Relocation off %x type %x\n", | 155 | printf("Unknown Relocation off %x type %x\n", |
147 | offset, type); | 156 | offset, type); |
148 | return EFI_LOAD_ERROR; | 157 | return EFI_LOAD_ERROR; |
149 | } | 158 | } |
150 | relocs++; | 159 | relocs++; |
151 | } | 160 | } |
152 | rel = (const IMAGE_BASE_RELOCATION *)relocs; | 161 | rel = (const IMAGE_BASE_RELOCATION *)relocs; |
153 | } | 162 | } |
154 | return EFI_SUCCESS; | 163 | return EFI_SUCCESS; |
155 | } | 164 | } |
156 | 165 | ||
157 | void __weak invalidate_icache_all(void) | 166 | void __weak invalidate_icache_all(void) |
158 | { | 167 | { |
159 | /* If the system doesn't support icache_all flush, cross our fingers */ | 168 | /* If the system doesn't support icache_all flush, cross our fingers */ |
160 | } | 169 | } |
161 | 170 | ||
162 | /* | 171 | /** |
163 | * Determine the memory types to be used for code and data. | 172 | * efi_set_code_and_data_type() - determine the memory types to be used for code |
173 | * and data. | ||
164 | * | 174 | * |
165 | * @loaded_image_info image descriptor | 175 | * @loaded_image_info: image descriptor |
166 | * @image_type field Subsystem of the optional header for | 176 | * @image_type: field Subsystem of the optional header for |
167 | * Windows specific field | 177 | * Windows specific field |
168 | */ | 178 | */ |
169 | static void efi_set_code_and_data_type( | 179 | static void efi_set_code_and_data_type( |
170 | struct efi_loaded_image *loaded_image_info, | 180 | struct efi_loaded_image *loaded_image_info, |
171 | uint16_t image_type) | 181 | uint16_t image_type) |
172 | { | 182 | { |
173 | switch (image_type) { | 183 | switch (image_type) { |
174 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: | 184 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
175 | loaded_image_info->image_code_type = EFI_LOADER_CODE; | 185 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
176 | loaded_image_info->image_data_type = EFI_LOADER_DATA; | 186 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
177 | break; | 187 | break; |
178 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: | 188 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
179 | loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE; | 189 | loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE; |
180 | loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA; | 190 | loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA; |
181 | break; | 191 | break; |
182 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: | 192 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
183 | case IMAGE_SUBSYSTEM_EFI_ROM: | 193 | case IMAGE_SUBSYSTEM_EFI_ROM: |
184 | loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE; | 194 | loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE; |
185 | loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA; | 195 | loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA; |
186 | break; | 196 | break; |
187 | default: | 197 | default: |
188 | printf("%s: invalid image type: %u\n", __func__, image_type); | 198 | printf("%s: invalid image type: %u\n", __func__, image_type); |
189 | /* Let's assume it is an application */ | 199 | /* Let's assume it is an application */ |
190 | loaded_image_info->image_code_type = EFI_LOADER_CODE; | 200 | loaded_image_info->image_code_type = EFI_LOADER_CODE; |
191 | loaded_image_info->image_data_type = EFI_LOADER_DATA; | 201 | loaded_image_info->image_data_type = EFI_LOADER_DATA; |
192 | break; | 202 | break; |
193 | } | 203 | } |
194 | } | 204 | } |
195 | 205 | ||
196 | /** | 206 | /** |
197 | * efi_load_pe() - relocate EFI binary | 207 | * efi_load_pe() - relocate EFI binary |
198 | * | 208 | * |
199 | * This function loads all sections from a PE binary into a newly reserved | 209 | * This function loads all sections from a PE binary into a newly reserved |
200 | * piece of memory. On success the entry point is returned as handle->entry. | 210 | * piece of memory. On success the entry point is returned as handle->entry. |
201 | * | 211 | * |
202 | * @handle: loaded image handle | 212 | * @handle: loaded image handle |
203 | * @efi: pointer to the EFI binary | 213 | * @efi: pointer to the EFI binary |
204 | * @loaded_image_info: loaded image protocol | 214 | * @loaded_image_info: loaded image protocol |
205 | * Return: status code | 215 | * Return: status code |
206 | */ | 216 | */ |
207 | efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, | 217 | efi_status_t efi_load_pe(struct efi_loaded_image_obj *handle, void *efi, |
208 | struct efi_loaded_image *loaded_image_info) | 218 | struct efi_loaded_image *loaded_image_info) |
209 | { | 219 | { |
210 | IMAGE_NT_HEADERS32 *nt; | 220 | IMAGE_NT_HEADERS32 *nt; |
211 | IMAGE_DOS_HEADER *dos; | 221 | IMAGE_DOS_HEADER *dos; |
212 | IMAGE_SECTION_HEADER *sections; | 222 | IMAGE_SECTION_HEADER *sections; |
213 | int num_sections; | 223 | int num_sections; |
214 | void *efi_reloc; | 224 | void *efi_reloc; |
215 | int i; | 225 | int i; |
216 | const IMAGE_BASE_RELOCATION *rel; | 226 | const IMAGE_BASE_RELOCATION *rel; |
217 | unsigned long rel_size; | 227 | unsigned long rel_size; |
218 | int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; | 228 | int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC; |
219 | uint64_t image_base; | 229 | uint64_t image_base; |
220 | uint64_t image_size; | 230 | uint64_t image_size; |
221 | unsigned long virt_size = 0; | 231 | unsigned long virt_size = 0; |
222 | int supported = 0; | 232 | int supported = 0; |
223 | 233 | ||
224 | dos = efi; | 234 | dos = efi; |
225 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) { | 235 | if (dos->e_magic != IMAGE_DOS_SIGNATURE) { |
226 | printf("%s: Invalid DOS Signature\n", __func__); | 236 | printf("%s: Invalid DOS Signature\n", __func__); |
227 | return EFI_LOAD_ERROR; | 237 | return EFI_LOAD_ERROR; |
228 | } | 238 | } |
229 | 239 | ||
230 | nt = (void *) ((char *)efi + dos->e_lfanew); | 240 | nt = (void *) ((char *)efi + dos->e_lfanew); |
231 | if (nt->Signature != IMAGE_NT_SIGNATURE) { | 241 | if (nt->Signature != IMAGE_NT_SIGNATURE) { |
232 | printf("%s: Invalid NT Signature\n", __func__); | 242 | printf("%s: Invalid NT Signature\n", __func__); |
233 | return EFI_LOAD_ERROR; | 243 | return EFI_LOAD_ERROR; |
234 | } | 244 | } |
235 | 245 | ||
236 | for (i = 0; machines[i]; i++) | 246 | for (i = 0; machines[i]; i++) |
237 | if (machines[i] == nt->FileHeader.Machine) { | 247 | if (machines[i] == nt->FileHeader.Machine) { |
238 | supported = 1; | 248 | supported = 1; |
239 | break; | 249 | break; |
240 | } | 250 | } |
241 | 251 | ||
242 | if (!supported) { | 252 | if (!supported) { |
243 | printf("%s: Machine type 0x%04x is not supported\n", | 253 | printf("%s: Machine type 0x%04x is not supported\n", |
244 | __func__, nt->FileHeader.Machine); | 254 | __func__, nt->FileHeader.Machine); |
245 | return EFI_LOAD_ERROR; | 255 | return EFI_LOAD_ERROR; |
246 | } | 256 | } |
247 | 257 | ||
248 | /* Calculate upper virtual address boundary */ | 258 | /* Calculate upper virtual address boundary */ |
249 | num_sections = nt->FileHeader.NumberOfSections; | 259 | num_sections = nt->FileHeader.NumberOfSections; |
250 | sections = (void *)&nt->OptionalHeader + | 260 | sections = (void *)&nt->OptionalHeader + |
251 | nt->FileHeader.SizeOfOptionalHeader; | 261 | nt->FileHeader.SizeOfOptionalHeader; |
252 | 262 | ||
253 | for (i = num_sections - 1; i >= 0; i--) { | 263 | for (i = num_sections - 1; i >= 0; i--) { |
254 | IMAGE_SECTION_HEADER *sec = §ions[i]; | 264 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
255 | virt_size = max_t(unsigned long, virt_size, | 265 | virt_size = max_t(unsigned long, virt_size, |
256 | sec->VirtualAddress + sec->Misc.VirtualSize); | 266 | sec->VirtualAddress + sec->Misc.VirtualSize); |
257 | } | 267 | } |
258 | 268 | ||
259 | /* Read 32/64bit specific header bits */ | 269 | /* Read 32/64bit specific header bits */ |
260 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { | 270 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
261 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; | 271 | IMAGE_NT_HEADERS64 *nt64 = (void *)nt; |
262 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; | 272 | IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader; |
263 | image_base = opt->ImageBase; | 273 | image_base = opt->ImageBase; |
264 | image_size = opt->SizeOfImage; | 274 | image_size = opt->SizeOfImage; |
265 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); | 275 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
266 | efi_reloc = efi_alloc(virt_size, | 276 | efi_reloc = efi_alloc(virt_size, |
267 | loaded_image_info->image_code_type); | 277 | loaded_image_info->image_code_type); |
268 | if (!efi_reloc) { | 278 | if (!efi_reloc) { |
269 | printf("%s: Could not allocate %lu bytes\n", | 279 | printf("%s: Could not allocate %lu bytes\n", |
270 | __func__, virt_size); | 280 | __func__, virt_size); |
271 | return EFI_OUT_OF_RESOURCES; | 281 | return EFI_OUT_OF_RESOURCES; |
272 | } | 282 | } |
273 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; | 283 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
274 | rel_size = opt->DataDirectory[rel_idx].Size; | 284 | rel_size = opt->DataDirectory[rel_idx].Size; |
275 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; | 285 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
276 | virt_size = ALIGN(virt_size, opt->SectionAlignment); | 286 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
277 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { | 287 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
278 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; | 288 | IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader; |
279 | image_base = opt->ImageBase; | 289 | image_base = opt->ImageBase; |
280 | image_size = opt->SizeOfImage; | 290 | image_size = opt->SizeOfImage; |
281 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); | 291 | efi_set_code_and_data_type(loaded_image_info, opt->Subsystem); |
282 | efi_reloc = efi_alloc(virt_size, | 292 | efi_reloc = efi_alloc(virt_size, |
283 | loaded_image_info->image_code_type); | 293 | loaded_image_info->image_code_type); |
284 | if (!efi_reloc) { | 294 | if (!efi_reloc) { |
285 | printf("%s: Could not allocate %lu bytes\n", | 295 | printf("%s: Could not allocate %lu bytes\n", |
286 | __func__, virt_size); | 296 | __func__, virt_size); |
287 | return EFI_OUT_OF_RESOURCES; | 297 | return EFI_OUT_OF_RESOURCES; |
288 | } | 298 | } |
289 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; | 299 | handle->entry = efi_reloc + opt->AddressOfEntryPoint; |
290 | rel_size = opt->DataDirectory[rel_idx].Size; | 300 | rel_size = opt->DataDirectory[rel_idx].Size; |
291 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; | 301 | rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress; |
292 | virt_size = ALIGN(virt_size, opt->SectionAlignment); | 302 | virt_size = ALIGN(virt_size, opt->SectionAlignment); |
293 | } else { | 303 | } else { |
294 | printf("%s: Invalid optional header magic %x\n", __func__, | 304 | printf("%s: Invalid optional header magic %x\n", __func__, |
295 | nt->OptionalHeader.Magic); | 305 | nt->OptionalHeader.Magic); |
296 | return EFI_LOAD_ERROR; | 306 | return EFI_LOAD_ERROR; |
297 | } | 307 | } |
298 | 308 | ||
299 | /* Load sections into RAM */ | 309 | /* Load sections into RAM */ |
300 | for (i = num_sections - 1; i >= 0; i--) { | 310 | for (i = num_sections - 1; i >= 0; i--) { |
301 | IMAGE_SECTION_HEADER *sec = §ions[i]; | 311 | IMAGE_SECTION_HEADER *sec = §ions[i]; |
302 | memset(efi_reloc + sec->VirtualAddress, 0, | 312 | memset(efi_reloc + sec->VirtualAddress, 0, |
303 | sec->Misc.VirtualSize); | 313 | sec->Misc.VirtualSize); |
304 | memcpy(efi_reloc + sec->VirtualAddress, | 314 | memcpy(efi_reloc + sec->VirtualAddress, |
305 | efi + sec->PointerToRawData, | 315 | efi + sec->PointerToRawData, |
306 | sec->SizeOfRawData); | 316 | sec->SizeOfRawData); |
307 | } | 317 | } |
308 | 318 | ||
309 | /* Run through relocations */ | 319 | /* Run through relocations */ |
310 | if (efi_loader_relocate(rel, rel_size, efi_reloc, | 320 | if (efi_loader_relocate(rel, rel_size, efi_reloc, |
311 | (unsigned long)image_base) != EFI_SUCCESS) { | 321 | (unsigned long)image_base) != EFI_SUCCESS) { |
312 | efi_free_pages((uintptr_t) efi_reloc, | 322 | efi_free_pages((uintptr_t) efi_reloc, |
313 | (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); | 323 | (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT); |
314 | return EFI_LOAD_ERROR; | 324 | return EFI_LOAD_ERROR; |
315 | } | 325 | } |
316 | 326 | ||
317 | /* Flush cache */ | 327 | /* Flush cache */ |
318 | flush_cache((ulong)efi_reloc, | 328 | flush_cache((ulong)efi_reloc, |
319 | ALIGN(virt_size, EFI_CACHELINE_SIZE)); | 329 | ALIGN(virt_size, EFI_CACHELINE_SIZE)); |
320 | invalidate_icache_all(); | 330 | invalidate_icache_all(); |
321 | 331 | ||
322 | /* Populate the loaded image interface bits */ | 332 | /* Populate the loaded image interface bits */ |
323 | loaded_image_info->image_base = efi; | 333 | loaded_image_info->image_base = efi; |
324 | loaded_image_info->image_size = image_size; | 334 | loaded_image_info->image_size = image_size; |
325 | handle->reloc_base = efi_reloc; | 335 | handle->reloc_base = efi_reloc; |
326 | handle->reloc_size = virt_size; | 336 | handle->reloc_size = virt_size; |
327 | 337 | ||
328 | return EFI_SUCCESS; | 338 | return EFI_SUCCESS; |
329 | } | 339 | } |
330 | 340 |