]>
Commit | Line | Data |
---|---|---|
bee91169 AG |
1 | /* |
2 | * EFI application boot time services | |
3 | * | |
4 | * Copyright (c) 2016 Alexander Graf | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0+ | |
7 | */ | |
8 | ||
bee91169 AG |
9 | #include <common.h> |
10 | #include <efi_loader.h> | |
11 | #include <malloc.h> | |
12 | #include <asm/global_data.h> | |
13 | #include <libfdt_env.h> | |
14 | #include <u-boot/crc.h> | |
15 | #include <bootm.h> | |
16 | #include <inttypes.h> | |
17 | #include <watchdog.h> | |
18 | ||
19 | DECLARE_GLOBAL_DATA_PTR; | |
20 | ||
21 | /* This list contains all the EFI objects our payload has access to */ | |
22 | LIST_HEAD(efi_obj_list); | |
23 | ||
24 | /* | |
25 | * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) | |
26 | * we need to do trickery with caches. Since we don't want to break the EFI | |
27 | * aware boot path, only apply hacks when loading exiting directly (breaking | |
28 | * direct Linux EFI booting along the way - oh well). | |
29 | */ | |
30 | static bool efi_is_direct_boot = true; | |
31 | ||
32 | /* | |
33 | * EFI can pass arbitrary additional "tables" containing vendor specific | |
34 | * information to the payload. One such table is the FDT table which contains | |
35 | * a pointer to a flattened device tree blob. | |
36 | * | |
37 | * In most cases we want to pass an FDT to the payload, so reserve one slot of | |
38 | * config table space for it. The pointer gets populated by do_bootefi_exec(). | |
39 | */ | |
3c63db9c | 40 | static struct efi_configuration_table __efi_runtime_data efi_conf_table[2]; |
bee91169 | 41 | |
65e4c0b1 | 42 | #ifdef CONFIG_ARM |
bee91169 AG |
43 | /* |
44 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare | |
45 | * fixed when compiling U-Boot. However, the payload does not know about that | |
46 | * restriction so we need to manually swap its and our view of that register on | |
47 | * EFI callback entry/exit. | |
48 | */ | |
49 | static volatile void *efi_gd, *app_gd; | |
65e4c0b1 | 50 | #endif |
bee91169 AG |
51 | |
52 | /* Called from do_bootefi_exec() */ | |
53 | void efi_save_gd(void) | |
54 | { | |
65e4c0b1 | 55 | #ifdef CONFIG_ARM |
bee91169 | 56 | efi_gd = gd; |
65e4c0b1 | 57 | #endif |
bee91169 AG |
58 | } |
59 | ||
60 | /* Called on every callback entry */ | |
61 | void efi_restore_gd(void) | |
62 | { | |
65e4c0b1 | 63 | #ifdef CONFIG_ARM |
bee91169 AG |
64 | /* Only restore if we're already in EFI context */ |
65 | if (!efi_gd) | |
66 | return; | |
67 | ||
68 | if (gd != efi_gd) | |
69 | app_gd = gd; | |
70 | gd = efi_gd; | |
65e4c0b1 | 71 | #endif |
bee91169 AG |
72 | } |
73 | ||
74 | /* Called on every callback exit */ | |
75 | efi_status_t efi_exit_func(efi_status_t ret) | |
76 | { | |
65e4c0b1 | 77 | #ifdef CONFIG_ARM |
bee91169 | 78 | gd = app_gd; |
65e4c0b1 SG |
79 | #endif |
80 | ||
bee91169 AG |
81 | return ret; |
82 | } | |
83 | ||
84 | static efi_status_t efi_unsupported(const char *funcname) | |
85 | { | |
edcef3ba | 86 | debug("EFI: App called into unimplemented function %s\n", funcname); |
bee91169 AG |
87 | return EFI_EXIT(EFI_UNSUPPORTED); |
88 | } | |
89 | ||
90 | static int guidcmp(const efi_guid_t *g1, const efi_guid_t *g2) | |
91 | { | |
92 | return memcmp(g1, g2, sizeof(efi_guid_t)); | |
93 | } | |
94 | ||
95 | static unsigned long EFIAPI efi_raise_tpl(unsigned long new_tpl) | |
96 | { | |
97 | EFI_ENTRY("0x%lx", new_tpl); | |
98 | return EFI_EXIT(0); | |
99 | } | |
100 | ||
101 | static void EFIAPI efi_restore_tpl(unsigned long old_tpl) | |
102 | { | |
103 | EFI_ENTRY("0x%lx", old_tpl); | |
104 | EFI_EXIT(efi_unsupported(__func__)); | |
105 | } | |
106 | ||
6e0bf8d8 MY |
107 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
108 | unsigned long pages, | |
109 | uint64_t *memory) | |
bee91169 AG |
110 | { |
111 | efi_status_t r; | |
112 | ||
113 | EFI_ENTRY("%d, %d, 0x%lx, %p", type, memory_type, pages, memory); | |
114 | r = efi_allocate_pages(type, memory_type, pages, memory); | |
115 | return EFI_EXIT(r); | |
116 | } | |
117 | ||
6e0bf8d8 MY |
118 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
119 | unsigned long pages) | |
bee91169 AG |
120 | { |
121 | efi_status_t r; | |
122 | ||
123 | EFI_ENTRY("%"PRIx64", 0x%lx", memory, pages); | |
124 | r = efi_free_pages(memory, pages); | |
125 | return EFI_EXIT(r); | |
126 | } | |
127 | ||
6e0bf8d8 MY |
128 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
129 | unsigned long *memory_map_size, | |
130 | struct efi_mem_desc *memory_map, | |
131 | unsigned long *map_key, | |
132 | unsigned long *descriptor_size, | |
133 | uint32_t *descriptor_version) | |
bee91169 AG |
134 | { |
135 | efi_status_t r; | |
136 | ||
137 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, | |
138 | map_key, descriptor_size, descriptor_version); | |
139 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, | |
140 | descriptor_size, descriptor_version); | |
141 | return EFI_EXIT(r); | |
142 | } | |
143 | ||
ead1274b SB |
144 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
145 | unsigned long size, | |
146 | void **buffer) | |
bee91169 | 147 | { |
1cd29f0a AG |
148 | efi_status_t r; |
149 | ||
150 | EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer); | |
ead1274b | 151 | r = efi_allocate_pool(pool_type, size, buffer); |
1cd29f0a | 152 | return EFI_EXIT(r); |
bee91169 AG |
153 | } |
154 | ||
42417bc8 | 155 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
bee91169 | 156 | { |
1cd29f0a AG |
157 | efi_status_t r; |
158 | ||
159 | EFI_ENTRY("%p", buffer); | |
42417bc8 | 160 | r = efi_free_pool(buffer); |
1cd29f0a | 161 | return EFI_EXIT(r); |
bee91169 AG |
162 | } |
163 | ||
164 | /* | |
165 | * Our event capabilities are very limited. Only support a single | |
166 | * event to exist, so we don't need to maintain lists. | |
167 | */ | |
168 | static struct { | |
169 | enum efi_event_type type; | |
170 | u32 trigger_type; | |
171 | u32 trigger_time; | |
172 | u64 trigger_next; | |
173 | unsigned long notify_tpl; | |
e275458c | 174 | void (EFIAPI *notify_function) (void *event, void *context); |
bee91169 AG |
175 | void *notify_context; |
176 | } efi_event = { | |
177 | /* Disable timers on bootup */ | |
178 | .trigger_next = -1ULL, | |
179 | }; | |
180 | ||
181 | static efi_status_t EFIAPI efi_create_event( | |
182 | enum efi_event_type type, ulong notify_tpl, | |
e275458c SG |
183 | void (EFIAPI *notify_function) (void *event, |
184 | void *context), | |
bee91169 AG |
185 | void *notify_context, void **event) |
186 | { | |
187 | EFI_ENTRY("%d, 0x%lx, %p, %p", type, notify_tpl, notify_function, | |
188 | notify_context); | |
189 | if (efi_event.notify_function) { | |
190 | /* We only support one event at a time */ | |
191 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); | |
192 | } | |
193 | ||
a95343b8 JG |
194 | if (event == NULL) |
195 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
196 | ||
197 | if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT)) | |
198 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
199 | ||
200 | if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) && | |
201 | notify_function == NULL) | |
202 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
203 | ||
bee91169 AG |
204 | efi_event.type = type; |
205 | efi_event.notify_tpl = notify_tpl; | |
206 | efi_event.notify_function = notify_function; | |
207 | efi_event.notify_context = notify_context; | |
208 | *event = &efi_event; | |
209 | ||
210 | return EFI_EXIT(EFI_SUCCESS); | |
211 | } | |
212 | ||
213 | /* | |
214 | * Our timers have to work without interrupts, so we check whenever keyboard | |
215 | * input or disk accesses happen if enough time elapsed for it to fire. | |
216 | */ | |
217 | void efi_timer_check(void) | |
218 | { | |
219 | u64 now = timer_get_us(); | |
220 | ||
221 | if (now >= efi_event.trigger_next) { | |
222 | /* Triggering! */ | |
223 | if (efi_event.trigger_type == EFI_TIMER_PERIODIC) | |
224 | efi_event.trigger_next += efi_event.trigger_time / 10; | |
37a980b3 JG |
225 | if (efi_event.type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) |
226 | efi_event.notify_function(&efi_event, | |
227 | efi_event.notify_context); | |
bee91169 AG |
228 | } |
229 | ||
230 | WATCHDOG_RESET(); | |
231 | } | |
232 | ||
233 | static efi_status_t EFIAPI efi_set_timer(void *event, int type, | |
234 | uint64_t trigger_time) | |
235 | { | |
236 | /* We don't have 64bit division available everywhere, so limit timer | |
237 | * distances to 32bit bits. */ | |
238 | u32 trigger32 = trigger_time; | |
239 | ||
240 | EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); | |
241 | ||
242 | if (trigger32 < trigger_time) { | |
243 | printf("WARNING: Truncating timer from %"PRIx64" to %x\n", | |
244 | trigger_time, trigger32); | |
245 | } | |
246 | ||
247 | if (event != &efi_event) { | |
248 | /* We only support one event at a time */ | |
249 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
250 | } | |
251 | ||
252 | switch (type) { | |
253 | case EFI_TIMER_STOP: | |
254 | efi_event.trigger_next = -1ULL; | |
255 | break; | |
256 | case EFI_TIMER_PERIODIC: | |
257 | case EFI_TIMER_RELATIVE: | |
258 | efi_event.trigger_next = timer_get_us() + (trigger32 / 10); | |
259 | break; | |
260 | default: | |
261 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
262 | } | |
263 | efi_event.trigger_type = type; | |
264 | efi_event.trigger_time = trigger_time; | |
265 | ||
266 | return EFI_EXIT(EFI_SUCCESS); | |
267 | } | |
268 | ||
269 | static efi_status_t EFIAPI efi_wait_for_event(unsigned long num_events, | |
270 | void *event, unsigned long *index) | |
271 | { | |
272 | u64 now; | |
273 | ||
274 | EFI_ENTRY("%ld, %p, %p", num_events, event, index); | |
275 | ||
276 | now = timer_get_us(); | |
277 | while (now < efi_event.trigger_next) { } | |
278 | efi_timer_check(); | |
279 | ||
280 | return EFI_EXIT(EFI_SUCCESS); | |
281 | } | |
282 | ||
283 | static efi_status_t EFIAPI efi_signal_event(void *event) | |
284 | { | |
285 | EFI_ENTRY("%p", event); | |
286 | return EFI_EXIT(EFI_SUCCESS); | |
287 | } | |
288 | ||
289 | static efi_status_t EFIAPI efi_close_event(void *event) | |
290 | { | |
291 | EFI_ENTRY("%p", event); | |
292 | efi_event.trigger_next = -1ULL; | |
293 | return EFI_EXIT(EFI_SUCCESS); | |
294 | } | |
295 | ||
296 | static efi_status_t EFIAPI efi_check_event(void *event) | |
297 | { | |
298 | EFI_ENTRY("%p", event); | |
299 | return EFI_EXIT(EFI_NOT_READY); | |
300 | } | |
301 | ||
302 | static efi_status_t EFIAPI efi_install_protocol_interface(void **handle, | |
303 | efi_guid_t *protocol, int protocol_interface_type, | |
304 | void *protocol_interface) | |
305 | { | |
e0549f8a | 306 | struct list_head *lhandle; |
307 | int i; | |
308 | efi_status_t r; | |
309 | ||
e0549f8a | 310 | if (!handle || !protocol || |
311 | protocol_interface_type != EFI_NATIVE_INTERFACE) { | |
312 | r = EFI_INVALID_PARAMETER; | |
313 | goto out; | |
314 | } | |
315 | ||
316 | /* Create new handle if requested. */ | |
317 | if (!*handle) { | |
318 | r = EFI_OUT_OF_RESOURCES; | |
319 | goto out; | |
320 | } | |
321 | /* Find object. */ | |
322 | list_for_each(lhandle, &efi_obj_list) { | |
323 | struct efi_object *efiobj; | |
324 | efiobj = list_entry(lhandle, struct efi_object, link); | |
325 | ||
326 | if (efiobj->handle != *handle) | |
327 | continue; | |
328 | /* Check if protocol is already installed on the handle. */ | |
329 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { | |
330 | struct efi_handler *handler = &efiobj->protocols[i]; | |
331 | ||
332 | if (!handler->guid) | |
333 | continue; | |
334 | if (!guidcmp(handler->guid, protocol)) { | |
335 | r = EFI_INVALID_PARAMETER; | |
336 | goto out; | |
337 | } | |
338 | } | |
339 | /* Install protocol in first empty slot. */ | |
340 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { | |
341 | struct efi_handler *handler = &efiobj->protocols[i]; | |
342 | ||
343 | if (handler->guid) | |
344 | continue; | |
345 | ||
346 | handler->guid = protocol; | |
347 | handler->protocol_interface = protocol_interface; | |
348 | r = EFI_SUCCESS; | |
349 | goto out; | |
350 | } | |
351 | r = EFI_OUT_OF_RESOURCES; | |
352 | goto out; | |
353 | } | |
354 | r = EFI_INVALID_PARAMETER; | |
355 | out: | |
8bee5a3c | 356 | return r; |
357 | } | |
358 | ||
359 | static efi_status_t EFIAPI efi_install_protocol_interface_ext(void **handle, | |
360 | efi_guid_t *protocol, int protocol_interface_type, | |
361 | void *protocol_interface) | |
362 | { | |
363 | EFI_ENTRY("%p, %p, %d, %p", handle, protocol, protocol_interface_type, | |
364 | protocol_interface); | |
365 | ||
366 | return EFI_EXIT(efi_install_protocol_interface(handle, protocol, | |
367 | protocol_interface_type, | |
368 | protocol_interface)); | |
bee91169 | 369 | } |
e0549f8a | 370 | |
bee91169 AG |
371 | static efi_status_t EFIAPI efi_reinstall_protocol_interface(void *handle, |
372 | efi_guid_t *protocol, void *old_interface, | |
373 | void *new_interface) | |
374 | { | |
375 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, old_interface, | |
376 | new_interface); | |
377 | return EFI_EXIT(EFI_ACCESS_DENIED); | |
378 | } | |
379 | ||
380 | static efi_status_t EFIAPI efi_uninstall_protocol_interface(void *handle, | |
381 | efi_guid_t *protocol, void *protocol_interface) | |
382 | { | |
4b6ed0d7 | 383 | struct list_head *lhandle; |
384 | int i; | |
385 | efi_status_t r = EFI_NOT_FOUND; | |
386 | ||
4b6ed0d7 | 387 | if (!handle || !protocol) { |
388 | r = EFI_INVALID_PARAMETER; | |
389 | goto out; | |
390 | } | |
391 | ||
392 | list_for_each(lhandle, &efi_obj_list) { | |
393 | struct efi_object *efiobj; | |
394 | efiobj = list_entry(lhandle, struct efi_object, link); | |
395 | ||
396 | if (efiobj->handle != handle) | |
397 | continue; | |
398 | ||
399 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { | |
400 | struct efi_handler *handler = &efiobj->protocols[i]; | |
401 | const efi_guid_t *hprotocol = handler->guid; | |
402 | ||
403 | if (!hprotocol) | |
404 | continue; | |
405 | if (!guidcmp(hprotocol, protocol)) { | |
406 | if (handler->protocol_interface) { | |
407 | r = EFI_ACCESS_DENIED; | |
408 | } else { | |
409 | handler->guid = 0; | |
410 | r = EFI_SUCCESS; | |
411 | } | |
412 | goto out; | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
417 | out: | |
3d8e1456 | 418 | return r; |
419 | } | |
420 | ||
421 | static efi_status_t EFIAPI efi_uninstall_protocol_interface_ext(void *handle, | |
422 | efi_guid_t *protocol, void *protocol_interface) | |
423 | { | |
424 | EFI_ENTRY("%p, %p, %p", handle, protocol, protocol_interface); | |
425 | ||
426 | return EFI_EXIT(efi_uninstall_protocol_interface(handle, protocol, | |
427 | protocol_interface)); | |
bee91169 AG |
428 | } |
429 | ||
430 | static efi_status_t EFIAPI efi_register_protocol_notify(efi_guid_t *protocol, | |
431 | void *event, | |
432 | void **registration) | |
433 | { | |
434 | EFI_ENTRY("%p, %p, %p", protocol, event, registration); | |
435 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); | |
436 | } | |
437 | ||
438 | static int efi_search(enum efi_locate_search_type search_type, | |
439 | efi_guid_t *protocol, void *search_key, | |
440 | struct efi_object *efiobj) | |
441 | { | |
442 | int i; | |
443 | ||
444 | switch (search_type) { | |
445 | case all_handles: | |
446 | return 0; | |
447 | case by_register_notify: | |
448 | return -1; | |
449 | case by_protocol: | |
450 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { | |
451 | const efi_guid_t *guid = efiobj->protocols[i].guid; | |
452 | if (guid && !guidcmp(guid, protocol)) | |
453 | return 0; | |
454 | } | |
455 | return -1; | |
456 | } | |
457 | ||
458 | return -1; | |
459 | } | |
460 | ||
461 | static efi_status_t EFIAPI efi_locate_handle( | |
462 | enum efi_locate_search_type search_type, | |
463 | efi_guid_t *protocol, void *search_key, | |
464 | unsigned long *buffer_size, efi_handle_t *buffer) | |
465 | { | |
466 | struct list_head *lhandle; | |
467 | unsigned long size = 0; | |
468 | ||
bee91169 AG |
469 | /* Count how much space we need */ |
470 | list_for_each(lhandle, &efi_obj_list) { | |
471 | struct efi_object *efiobj; | |
472 | efiobj = list_entry(lhandle, struct efi_object, link); | |
473 | if (!efi_search(search_type, protocol, search_key, efiobj)) { | |
474 | size += sizeof(void*); | |
475 | } | |
476 | } | |
477 | ||
478 | if (*buffer_size < size) { | |
479 | *buffer_size = size; | |
26329584 | 480 | return EFI_BUFFER_TOO_SMALL; |
bee91169 AG |
481 | } |
482 | ||
483 | /* Then fill the array */ | |
484 | list_for_each(lhandle, &efi_obj_list) { | |
485 | struct efi_object *efiobj; | |
486 | efiobj = list_entry(lhandle, struct efi_object, link); | |
487 | if (!efi_search(search_type, protocol, search_key, efiobj)) { | |
488 | *(buffer++) = efiobj->handle; | |
489 | } | |
490 | } | |
491 | ||
492 | *buffer_size = size; | |
26329584 | 493 | return EFI_SUCCESS; |
494 | } | |
495 | ||
496 | static efi_status_t EFIAPI efi_locate_handle_ext( | |
497 | enum efi_locate_search_type search_type, | |
498 | efi_guid_t *protocol, void *search_key, | |
499 | unsigned long *buffer_size, efi_handle_t *buffer) | |
500 | { | |
501 | EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, | |
502 | buffer_size, buffer); | |
503 | ||
504 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, | |
505 | buffer_size, buffer)); | |
bee91169 AG |
506 | } |
507 | ||
508 | static efi_status_t EFIAPI efi_locate_device_path(efi_guid_t *protocol, | |
509 | struct efi_device_path **device_path, | |
510 | efi_handle_t *device) | |
511 | { | |
512 | EFI_ENTRY("%p, %p, %p", protocol, device_path, device); | |
513 | return EFI_EXIT(EFI_NOT_FOUND); | |
514 | } | |
515 | ||
488bf12d | 516 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table) |
bee91169 AG |
517 | { |
518 | int i; | |
519 | ||
bee91169 AG |
520 | /* Check for guid override */ |
521 | for (i = 0; i < systab.nr_tables; i++) { | |
522 | if (!guidcmp(guid, &efi_conf_table[i].guid)) { | |
523 | efi_conf_table[i].table = table; | |
488bf12d | 524 | return EFI_SUCCESS; |
bee91169 AG |
525 | } |
526 | } | |
527 | ||
528 | /* No override, check for overflow */ | |
529 | if (i >= ARRAY_SIZE(efi_conf_table)) | |
488bf12d | 530 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
531 | |
532 | /* Add a new entry */ | |
533 | memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); | |
534 | efi_conf_table[i].table = table; | |
aba5e919 | 535 | systab.nr_tables = i + 1; |
bee91169 | 536 | |
488bf12d AG |
537 | return EFI_SUCCESS; |
538 | } | |
539 | ||
540 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, | |
541 | void *table) | |
542 | { | |
543 | EFI_ENTRY("%p, %p", guid, table); | |
544 | return EFI_EXIT(efi_install_configuration_table(guid, table)); | |
bee91169 AG |
545 | } |
546 | ||
547 | static efi_status_t EFIAPI efi_load_image(bool boot_policy, | |
548 | efi_handle_t parent_image, | |
549 | struct efi_device_path *file_path, | |
550 | void *source_buffer, | |
551 | unsigned long source_size, | |
552 | efi_handle_t *image_handle) | |
553 | { | |
554 | static struct efi_object loaded_image_info_obj = { | |
555 | .protocols = { | |
556 | { | |
557 | .guid = &efi_guid_loaded_image, | |
bee91169 AG |
558 | }, |
559 | }, | |
560 | }; | |
561 | struct efi_loaded_image *info; | |
562 | struct efi_object *obj; | |
563 | ||
564 | EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image, | |
565 | file_path, source_buffer, source_size, image_handle); | |
566 | info = malloc(sizeof(*info)); | |
b5349f74 | 567 | loaded_image_info_obj.protocols[0].protocol_interface = info; |
bee91169 AG |
568 | obj = malloc(sizeof(loaded_image_info_obj)); |
569 | memset(info, 0, sizeof(*info)); | |
570 | memcpy(obj, &loaded_image_info_obj, sizeof(loaded_image_info_obj)); | |
571 | obj->handle = info; | |
572 | info->file_path = file_path; | |
573 | info->reserved = efi_load_pe(source_buffer, info); | |
574 | if (!info->reserved) { | |
575 | free(info); | |
576 | free(obj); | |
577 | return EFI_EXIT(EFI_UNSUPPORTED); | |
578 | } | |
579 | ||
580 | *image_handle = info; | |
581 | list_add_tail(&obj->link, &efi_obj_list); | |
582 | ||
583 | return EFI_EXIT(EFI_SUCCESS); | |
584 | } | |
585 | ||
586 | static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, | |
587 | unsigned long *exit_data_size, | |
588 | s16 **exit_data) | |
589 | { | |
590 | ulong (*entry)(void *image_handle, struct efi_system_table *st); | |
591 | struct efi_loaded_image *info = image_handle; | |
592 | ||
593 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); | |
594 | entry = info->reserved; | |
595 | ||
596 | efi_is_direct_boot = false; | |
597 | ||
598 | /* call the image! */ | |
a86aeaf2 AG |
599 | if (setjmp(&info->exit_jmp)) { |
600 | /* We returned from the child image */ | |
601 | return EFI_EXIT(info->exit_status); | |
602 | } | |
603 | ||
bee91169 AG |
604 | entry(image_handle, &systab); |
605 | ||
606 | /* Should usually never get here */ | |
607 | return EFI_EXIT(EFI_SUCCESS); | |
608 | } | |
609 | ||
a86aeaf2 AG |
610 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
611 | efi_status_t exit_status, unsigned long exit_data_size, | |
612 | int16_t *exit_data) | |
bee91169 | 613 | { |
a86aeaf2 AG |
614 | struct efi_loaded_image *loaded_image_info = (void*)image_handle; |
615 | ||
bee91169 AG |
616 | EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, |
617 | exit_data_size, exit_data); | |
a86aeaf2 AG |
618 | |
619 | loaded_image_info->exit_status = exit_status; | |
692fcdd8 | 620 | longjmp(&loaded_image_info->exit_jmp, 1); |
a86aeaf2 AG |
621 | |
622 | panic("EFI application exited"); | |
bee91169 AG |
623 | } |
624 | ||
625 | static struct efi_object *efi_search_obj(void *handle) | |
626 | { | |
627 | struct list_head *lhandle; | |
628 | ||
629 | list_for_each(lhandle, &efi_obj_list) { | |
630 | struct efi_object *efiobj; | |
631 | efiobj = list_entry(lhandle, struct efi_object, link); | |
632 | if (efiobj->handle == handle) | |
633 | return efiobj; | |
634 | } | |
635 | ||
636 | return NULL; | |
637 | } | |
638 | ||
639 | static efi_status_t EFIAPI efi_unload_image(void *image_handle) | |
640 | { | |
641 | struct efi_object *efiobj; | |
642 | ||
643 | EFI_ENTRY("%p", image_handle); | |
644 | efiobj = efi_search_obj(image_handle); | |
645 | if (efiobj) | |
646 | list_del(&efiobj->link); | |
647 | ||
648 | return EFI_EXIT(EFI_SUCCESS); | |
649 | } | |
650 | ||
651 | static void efi_exit_caches(void) | |
652 | { | |
653 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) | |
654 | /* | |
655 | * Grub on 32bit ARM needs to have caches disabled before jumping into | |
656 | * a zImage, but does not know of all cache layers. Give it a hand. | |
657 | */ | |
658 | if (efi_is_direct_boot) | |
659 | cleanup_before_linux(); | |
660 | #endif | |
661 | } | |
662 | ||
663 | static efi_status_t EFIAPI efi_exit_boot_services(void *image_handle, | |
664 | unsigned long map_key) | |
665 | { | |
666 | EFI_ENTRY("%p, %ld", image_handle, map_key); | |
667 | ||
b7b8410a AG |
668 | board_quiesce_devices(); |
669 | ||
bee91169 AG |
670 | /* Fix up caches for EFI payloads if necessary */ |
671 | efi_exit_caches(); | |
672 | ||
673 | /* This stops all lingering devices */ | |
674 | bootm_disable_interrupts(); | |
675 | ||
676 | /* Give the payload some time to boot */ | |
677 | WATCHDOG_RESET(); | |
678 | ||
679 | return EFI_EXIT(EFI_SUCCESS); | |
680 | } | |
681 | ||
682 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) | |
683 | { | |
684 | static uint64_t mono = 0; | |
685 | EFI_ENTRY("%p", count); | |
686 | *count = mono++; | |
687 | return EFI_EXIT(EFI_SUCCESS); | |
688 | } | |
689 | ||
690 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) | |
691 | { | |
692 | EFI_ENTRY("%ld", microseconds); | |
693 | udelay(microseconds); | |
694 | return EFI_EXIT(EFI_SUCCESS); | |
695 | } | |
696 | ||
697 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, | |
698 | uint64_t watchdog_code, | |
699 | unsigned long data_size, | |
700 | uint16_t *watchdog_data) | |
701 | { | |
702 | EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code, | |
703 | data_size, watchdog_data); | |
704 | return EFI_EXIT(efi_unsupported(__func__)); | |
705 | } | |
706 | ||
707 | static efi_status_t EFIAPI efi_connect_controller( | |
708 | efi_handle_t controller_handle, | |
709 | efi_handle_t *driver_image_handle, | |
710 | struct efi_device_path *remain_device_path, | |
711 | bool recursive) | |
712 | { | |
713 | EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle, | |
714 | remain_device_path, recursive); | |
715 | return EFI_EXIT(EFI_NOT_FOUND); | |
716 | } | |
717 | ||
718 | static efi_status_t EFIAPI efi_disconnect_controller(void *controller_handle, | |
719 | void *driver_image_handle, | |
720 | void *child_handle) | |
721 | { | |
722 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, | |
723 | child_handle); | |
724 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
725 | } | |
726 | ||
727 | static efi_status_t EFIAPI efi_close_protocol(void *handle, | |
728 | efi_guid_t *protocol, | |
729 | void *agent_handle, | |
730 | void *controller_handle) | |
731 | { | |
732 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, agent_handle, | |
733 | controller_handle); | |
734 | return EFI_EXIT(EFI_NOT_FOUND); | |
735 | } | |
736 | ||
737 | static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle, | |
738 | efi_guid_t *protocol, | |
739 | struct efi_open_protocol_info_entry **entry_buffer, | |
740 | unsigned long *entry_count) | |
741 | { | |
742 | EFI_ENTRY("%p, %p, %p, %p", handle, protocol, entry_buffer, | |
743 | entry_count); | |
744 | return EFI_EXIT(EFI_NOT_FOUND); | |
745 | } | |
746 | ||
747 | static efi_status_t EFIAPI efi_protocols_per_handle(void *handle, | |
748 | efi_guid_t ***protocol_buffer, | |
749 | unsigned long *protocol_buffer_count) | |
750 | { | |
751 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, | |
752 | protocol_buffer_count); | |
753 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); | |
754 | } | |
755 | ||
756 | static efi_status_t EFIAPI efi_locate_handle_buffer( | |
757 | enum efi_locate_search_type search_type, | |
758 | efi_guid_t *protocol, void *search_key, | |
759 | unsigned long *no_handles, efi_handle_t **buffer) | |
760 | { | |
c2e703f9 | 761 | efi_status_t r; |
762 | unsigned long buffer_size = 0; | |
763 | ||
bee91169 AG |
764 | EFI_ENTRY("%d, %p, %p, %p, %p", search_type, protocol, search_key, |
765 | no_handles, buffer); | |
c2e703f9 | 766 | |
767 | if (!no_handles || !buffer) { | |
768 | r = EFI_INVALID_PARAMETER; | |
769 | goto out; | |
770 | } | |
771 | *no_handles = 0; | |
772 | *buffer = NULL; | |
773 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
774 | *buffer); | |
775 | if (r != EFI_BUFFER_TOO_SMALL) | |
776 | goto out; | |
777 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
778 | (void **)buffer); | |
779 | if (r != EFI_SUCCESS) | |
780 | goto out; | |
781 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
782 | *buffer); | |
783 | if (r == EFI_SUCCESS) | |
784 | *no_handles = buffer_size / sizeof(void *); | |
785 | out: | |
786 | return EFI_EXIT(r); | |
bee91169 AG |
787 | } |
788 | ||
789 | static struct efi_class_map efi_class_maps[] = { | |
790 | { | |
791 | .guid = &efi_guid_console_control, | |
792 | .interface = &efi_console_control | |
793 | }, | |
794 | }; | |
795 | ||
796 | static efi_status_t EFIAPI efi_locate_protocol(efi_guid_t *protocol, | |
797 | void *registration, | |
798 | void **protocol_interface) | |
799 | { | |
800 | int i; | |
801 | ||
802 | EFI_ENTRY("%p, %p, %p", protocol, registration, protocol_interface); | |
803 | for (i = 0; i < ARRAY_SIZE(efi_class_maps); i++) { | |
804 | struct efi_class_map *curmap = &efi_class_maps[i]; | |
805 | if (!guidcmp(protocol, curmap->guid)) { | |
806 | *protocol_interface = (void*)curmap->interface; | |
807 | return EFI_EXIT(EFI_SUCCESS); | |
808 | } | |
809 | } | |
810 | ||
811 | return EFI_EXIT(EFI_NOT_FOUND); | |
812 | } | |
813 | ||
814 | static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces( | |
815 | void **handle, ...) | |
816 | { | |
817 | EFI_ENTRY("%p", handle); | |
58b83586 | 818 | |
819 | va_list argptr; | |
820 | efi_guid_t *protocol; | |
821 | void *protocol_interface; | |
822 | efi_status_t r = EFI_SUCCESS; | |
823 | int i = 0; | |
824 | ||
825 | if (!handle) | |
826 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
827 | ||
828 | va_start(argptr, handle); | |
829 | for (;;) { | |
830 | protocol = va_arg(argptr, efi_guid_t*); | |
831 | if (!protocol) | |
832 | break; | |
833 | protocol_interface = va_arg(argptr, void*); | |
834 | r = efi_install_protocol_interface(handle, protocol, | |
835 | EFI_NATIVE_INTERFACE, | |
836 | protocol_interface); | |
837 | if (r != EFI_SUCCESS) | |
838 | break; | |
839 | i++; | |
840 | } | |
841 | va_end(argptr); | |
842 | if (r == EFI_SUCCESS) | |
843 | return EFI_EXIT(r); | |
844 | ||
845 | /* If an error occured undo all changes. */ | |
846 | va_start(argptr, handle); | |
847 | for (; i; --i) { | |
848 | protocol = va_arg(argptr, efi_guid_t*); | |
849 | protocol_interface = va_arg(argptr, void*); | |
850 | efi_uninstall_protocol_interface(handle, protocol, | |
851 | protocol_interface); | |
852 | } | |
853 | va_end(argptr); | |
854 | ||
855 | return EFI_EXIT(r); | |
bee91169 AG |
856 | } |
857 | ||
858 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( | |
859 | void *handle, ...) | |
860 | { | |
861 | EFI_ENTRY("%p", handle); | |
862 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
863 | } | |
864 | ||
865 | static efi_status_t EFIAPI efi_calculate_crc32(void *data, | |
866 | unsigned long data_size, | |
867 | uint32_t *crc32_p) | |
868 | { | |
869 | EFI_ENTRY("%p, %ld", data, data_size); | |
870 | *crc32_p = crc32(0, data, data_size); | |
871 | return EFI_EXIT(EFI_SUCCESS); | |
872 | } | |
873 | ||
874 | static void EFIAPI efi_copy_mem(void *destination, void *source, | |
875 | unsigned long length) | |
876 | { | |
877 | EFI_ENTRY("%p, %p, %ld", destination, source, length); | |
878 | memcpy(destination, source, length); | |
879 | } | |
880 | ||
881 | static void EFIAPI efi_set_mem(void *buffer, unsigned long size, uint8_t value) | |
882 | { | |
883 | EFI_ENTRY("%p, %ld, 0x%x", buffer, size, value); | |
884 | memset(buffer, value, size); | |
885 | } | |
886 | ||
887 | static efi_status_t EFIAPI efi_open_protocol( | |
888 | void *handle, efi_guid_t *protocol, | |
889 | void **protocol_interface, void *agent_handle, | |
890 | void *controller_handle, uint32_t attributes) | |
891 | { | |
892 | struct list_head *lhandle; | |
893 | int i; | |
69baec67 | 894 | efi_status_t r = EFI_INVALID_PARAMETER; |
bee91169 AG |
895 | |
896 | EFI_ENTRY("%p, %p, %p, %p, %p, 0x%x", handle, protocol, | |
897 | protocol_interface, agent_handle, controller_handle, | |
898 | attributes); | |
b5349f74 | 899 | |
69baec67 | 900 | if (!handle || !protocol || |
901 | (!protocol_interface && attributes != | |
902 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { | |
903 | goto out; | |
904 | } | |
905 | ||
906 | switch (attributes) { | |
907 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: | |
908 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: | |
909 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: | |
910 | break; | |
911 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: | |
912 | if (controller_handle == handle) | |
913 | goto out; | |
914 | case EFI_OPEN_PROTOCOL_BY_DRIVER: | |
915 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
916 | if (controller_handle == NULL) | |
917 | goto out; | |
918 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
919 | if (agent_handle == NULL) | |
920 | goto out; | |
921 | break; | |
922 | default: | |
b5349f74 | 923 | goto out; |
924 | } | |
925 | ||
bee91169 AG |
926 | list_for_each(lhandle, &efi_obj_list) { |
927 | struct efi_object *efiobj; | |
928 | efiobj = list_entry(lhandle, struct efi_object, link); | |
929 | ||
930 | if (efiobj->handle != handle) | |
931 | continue; | |
932 | ||
933 | for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) { | |
934 | struct efi_handler *handler = &efiobj->protocols[i]; | |
935 | const efi_guid_t *hprotocol = handler->guid; | |
936 | if (!hprotocol) | |
4b6ed0d7 | 937 | continue; |
bee91169 | 938 | if (!guidcmp(hprotocol, protocol)) { |
b5349f74 | 939 | if (attributes != |
940 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL) { | |
941 | *protocol_interface = | |
942 | handler->protocol_interface; | |
943 | } | |
944 | r = EFI_SUCCESS; | |
bee91169 AG |
945 | goto out; |
946 | } | |
947 | } | |
69baec67 | 948 | goto unsupported; |
bee91169 AG |
949 | } |
950 | ||
69baec67 | 951 | unsupported: |
952 | r = EFI_UNSUPPORTED; | |
bee91169 AG |
953 | out: |
954 | return EFI_EXIT(r); | |
955 | } | |
956 | ||
957 | static efi_status_t EFIAPI efi_handle_protocol(void *handle, | |
958 | efi_guid_t *protocol, | |
959 | void **protocol_interface) | |
960 | { | |
8e1d329f | 961 | return efi_open_protocol(handle, protocol, protocol_interface, NULL, |
962 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); | |
bee91169 AG |
963 | } |
964 | ||
965 | static const struct efi_boot_services efi_boot_services = { | |
966 | .hdr = { | |
967 | .headersize = sizeof(struct efi_table_hdr), | |
968 | }, | |
969 | .raise_tpl = efi_raise_tpl, | |
970 | .restore_tpl = efi_restore_tpl, | |
971 | .allocate_pages = efi_allocate_pages_ext, | |
972 | .free_pages = efi_free_pages_ext, | |
973 | .get_memory_map = efi_get_memory_map_ext, | |
ead1274b | 974 | .allocate_pool = efi_allocate_pool_ext, |
42417bc8 | 975 | .free_pool = efi_free_pool_ext, |
bee91169 AG |
976 | .create_event = efi_create_event, |
977 | .set_timer = efi_set_timer, | |
978 | .wait_for_event = efi_wait_for_event, | |
979 | .signal_event = efi_signal_event, | |
980 | .close_event = efi_close_event, | |
981 | .check_event = efi_check_event, | |
8bee5a3c | 982 | .install_protocol_interface = efi_install_protocol_interface_ext, |
bee91169 | 983 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
3d8e1456 | 984 | .uninstall_protocol_interface = efi_uninstall_protocol_interface_ext, |
bee91169 AG |
985 | .handle_protocol = efi_handle_protocol, |
986 | .reserved = NULL, | |
987 | .register_protocol_notify = efi_register_protocol_notify, | |
26329584 | 988 | .locate_handle = efi_locate_handle_ext, |
bee91169 | 989 | .locate_device_path = efi_locate_device_path, |
488bf12d | 990 | .install_configuration_table = efi_install_configuration_table_ext, |
bee91169 AG |
991 | .load_image = efi_load_image, |
992 | .start_image = efi_start_image, | |
a86aeaf2 | 993 | .exit = efi_exit, |
bee91169 AG |
994 | .unload_image = efi_unload_image, |
995 | .exit_boot_services = efi_exit_boot_services, | |
996 | .get_next_monotonic_count = efi_get_next_monotonic_count, | |
997 | .stall = efi_stall, | |
998 | .set_watchdog_timer = efi_set_watchdog_timer, | |
999 | .connect_controller = efi_connect_controller, | |
1000 | .disconnect_controller = efi_disconnect_controller, | |
1001 | .open_protocol = efi_open_protocol, | |
1002 | .close_protocol = efi_close_protocol, | |
1003 | .open_protocol_information = efi_open_protocol_information, | |
1004 | .protocols_per_handle = efi_protocols_per_handle, | |
1005 | .locate_handle_buffer = efi_locate_handle_buffer, | |
1006 | .locate_protocol = efi_locate_protocol, | |
1007 | .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces, | |
1008 | .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces, | |
1009 | .calculate_crc32 = efi_calculate_crc32, | |
1010 | .copy_mem = efi_copy_mem, | |
1011 | .set_mem = efi_set_mem, | |
1012 | }; | |
1013 | ||
1014 | ||
3c63db9c | 1015 | static uint16_t __efi_runtime_data firmware_vendor[] = |
bee91169 AG |
1016 | { 'D','a','s',' ','U','-','b','o','o','t',0 }; |
1017 | ||
3c63db9c | 1018 | struct efi_system_table __efi_runtime_data systab = { |
bee91169 AG |
1019 | .hdr = { |
1020 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, | |
1021 | .revision = 0x20005, /* 2.5 */ | |
1022 | .headersize = sizeof(struct efi_table_hdr), | |
1023 | }, | |
1024 | .fw_vendor = (long)firmware_vendor, | |
1025 | .con_in = (void*)&efi_con_in, | |
1026 | .con_out = (void*)&efi_con_out, | |
1027 | .std_err = (void*)&efi_con_out, | |
1028 | .runtime = (void*)&efi_runtime_services, | |
1029 | .boottime = (void*)&efi_boot_services, | |
1030 | .nr_tables = 0, | |
1031 | .tables = (void*)efi_conf_table, | |
1032 | }; |