]>
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 | 9 | #include <common.h> |
7d963323 | 10 | #include <div64.h> |
bee91169 | 11 | #include <efi_loader.h> |
ad644e7c | 12 | #include <environment.h> |
bee91169 AG |
13 | #include <malloc.h> |
14 | #include <asm/global_data.h> | |
15 | #include <libfdt_env.h> | |
16 | #include <u-boot/crc.h> | |
17 | #include <bootm.h> | |
18 | #include <inttypes.h> | |
19 | #include <watchdog.h> | |
20 | ||
21 | DECLARE_GLOBAL_DATA_PTR; | |
22 | ||
32f4b2f8 | 23 | /* Task priority level */ |
152cade3 | 24 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
32f4b2f8 | 25 | |
bee91169 AG |
26 | /* This list contains all the EFI objects our payload has access to */ |
27 | LIST_HEAD(efi_obj_list); | |
28 | ||
29 | /* | |
30 | * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) | |
31 | * we need to do trickery with caches. Since we don't want to break the EFI | |
32 | * aware boot path, only apply hacks when loading exiting directly (breaking | |
33 | * direct Linux EFI booting along the way - oh well). | |
34 | */ | |
35 | static bool efi_is_direct_boot = true; | |
36 | ||
37 | /* | |
38 | * EFI can pass arbitrary additional "tables" containing vendor specific | |
39 | * information to the payload. One such table is the FDT table which contains | |
40 | * a pointer to a flattened device tree blob. | |
41 | * | |
42 | * In most cases we want to pass an FDT to the payload, so reserve one slot of | |
43 | * config table space for it. The pointer gets populated by do_bootefi_exec(). | |
44 | */ | |
3c63db9c | 45 | static struct efi_configuration_table __efi_runtime_data efi_conf_table[2]; |
bee91169 | 46 | |
65e4c0b1 | 47 | #ifdef CONFIG_ARM |
bee91169 AG |
48 | /* |
49 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare | |
50 | * fixed when compiling U-Boot. However, the payload does not know about that | |
51 | * restriction so we need to manually swap its and our view of that register on | |
52 | * EFI callback entry/exit. | |
53 | */ | |
54 | static volatile void *efi_gd, *app_gd; | |
65e4c0b1 | 55 | #endif |
bee91169 | 56 | |
c160d2f5 | 57 | static int entry_count; |
af65db85 | 58 | static int nesting_level; |
f0959dbe HS |
59 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
60 | const efi_guid_t efi_guid_driver_binding_protocol = | |
61 | EFI_DRIVER_BINDING_PROTOCOL_GUID; | |
c160d2f5 | 62 | |
2074f700 HS |
63 | static efi_status_t EFIAPI efi_disconnect_controller( |
64 | efi_handle_t controller_handle, | |
65 | efi_handle_t driver_image_handle, | |
66 | efi_handle_t child_handle); | |
3f9b0042 | 67 | |
c160d2f5 RC |
68 | /* Called on every callback entry */ |
69 | int __efi_entry_check(void) | |
70 | { | |
71 | int ret = entry_count++ == 0; | |
72 | #ifdef CONFIG_ARM | |
73 | assert(efi_gd); | |
74 | app_gd = gd; | |
75 | gd = efi_gd; | |
76 | #endif | |
77 | return ret; | |
78 | } | |
79 | ||
80 | /* Called on every callback exit */ | |
81 | int __efi_exit_check(void) | |
82 | { | |
83 | int ret = --entry_count == 0; | |
84 | #ifdef CONFIG_ARM | |
85 | gd = app_gd; | |
86 | #endif | |
87 | return ret; | |
88 | } | |
89 | ||
bee91169 AG |
90 | /* Called from do_bootefi_exec() */ |
91 | void efi_save_gd(void) | |
92 | { | |
65e4c0b1 | 93 | #ifdef CONFIG_ARM |
bee91169 | 94 | efi_gd = gd; |
65e4c0b1 | 95 | #endif |
bee91169 AG |
96 | } |
97 | ||
c160d2f5 RC |
98 | /* |
99 | * Special case handler for error/abort that just forces things back | |
100 | * to u-boot world so we can dump out an abort msg, without any care | |
101 | * about returning back to UEFI world. | |
102 | */ | |
bee91169 AG |
103 | void efi_restore_gd(void) |
104 | { | |
65e4c0b1 | 105 | #ifdef CONFIG_ARM |
bee91169 AG |
106 | /* Only restore if we're already in EFI context */ |
107 | if (!efi_gd) | |
108 | return; | |
bee91169 | 109 | gd = efi_gd; |
65e4c0b1 | 110 | #endif |
bee91169 AG |
111 | } |
112 | ||
af65db85 | 113 | /* |
c8df80c5 HS |
114 | * Return a string for indenting with two spaces per level. A maximum of ten |
115 | * indent levels is supported. Higher indent levels will be truncated. | |
116 | * | |
117 | * @level indent level | |
118 | * @return indent string | |
af65db85 RC |
119 | */ |
120 | static const char *indent_string(int level) | |
121 | { | |
122 | const char *indent = " "; | |
123 | const int max = strlen(indent); | |
124 | level = min(max, level * 2); | |
125 | return &indent[max - level]; | |
126 | } | |
127 | ||
ae0bd3a9 HS |
128 | const char *__efi_nesting(void) |
129 | { | |
130 | return indent_string(nesting_level); | |
131 | } | |
132 | ||
af65db85 RC |
133 | const char *__efi_nesting_inc(void) |
134 | { | |
135 | return indent_string(nesting_level++); | |
136 | } | |
137 | ||
138 | const char *__efi_nesting_dec(void) | |
139 | { | |
140 | return indent_string(--nesting_level); | |
141 | } | |
142 | ||
332468f7 HS |
143 | /* |
144 | * Queue an EFI event. | |
145 | * | |
146 | * This function queues the notification function of the event for future | |
147 | * execution. | |
148 | * | |
149 | * The notification function is called if the task priority level of the | |
150 | * event is higher than the current task priority level. | |
151 | * | |
152 | * For the SignalEvent service see efi_signal_event_ext. | |
153 | * | |
154 | * @event event to signal | |
9bc9664d | 155 | * @check_tpl check the TPL level |
332468f7 | 156 | */ |
9bc9664d | 157 | void efi_signal_event(struct efi_event *event, bool check_tpl) |
c6841592 | 158 | { |
ca62a4f5 | 159 | if (event->notify_function) { |
e190e897 | 160 | event->is_queued = true; |
32f4b2f8 | 161 | /* Check TPL */ |
9bc9664d | 162 | if (check_tpl && efi_tpl >= event->notify_tpl) |
32f4b2f8 | 163 | return; |
ea630ce9 HS |
164 | EFI_CALL_VOID(event->notify_function(event, |
165 | event->notify_context)); | |
c6841592 | 166 | } |
e190e897 | 167 | event->is_queued = false; |
c6841592 | 168 | } |
169 | ||
332468f7 HS |
170 | /* |
171 | * Raise the task priority level. | |
172 | * | |
173 | * This function implements the RaiseTpl service. | |
174 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
175 | * for details. | |
176 | * | |
177 | * @new_tpl new value of the task priority level | |
178 | * @return old value of the task priority level | |
179 | */ | |
152cade3 | 180 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
bee91169 | 181 | { |
152cade3 | 182 | efi_uintn_t old_tpl = efi_tpl; |
32f4b2f8 | 183 | |
503f2695 | 184 | EFI_ENTRY("0x%zx", new_tpl); |
32f4b2f8 HS |
185 | |
186 | if (new_tpl < efi_tpl) | |
187 | debug("WARNING: new_tpl < current_tpl in %s\n", __func__); | |
188 | efi_tpl = new_tpl; | |
189 | if (efi_tpl > TPL_HIGH_LEVEL) | |
190 | efi_tpl = TPL_HIGH_LEVEL; | |
191 | ||
192 | EFI_EXIT(EFI_SUCCESS); | |
193 | return old_tpl; | |
bee91169 AG |
194 | } |
195 | ||
332468f7 HS |
196 | /* |
197 | * Lower the task priority level. | |
198 | * | |
199 | * This function implements the RestoreTpl service. | |
200 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
201 | * for details. | |
202 | * | |
203 | * @old_tpl value of the task priority level to be restored | |
204 | */ | |
152cade3 | 205 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
bee91169 | 206 | { |
503f2695 | 207 | EFI_ENTRY("0x%zx", old_tpl); |
32f4b2f8 HS |
208 | |
209 | if (old_tpl > efi_tpl) | |
210 | debug("WARNING: old_tpl > current_tpl in %s\n", __func__); | |
211 | efi_tpl = old_tpl; | |
212 | if (efi_tpl > TPL_HIGH_LEVEL) | |
213 | efi_tpl = TPL_HIGH_LEVEL; | |
214 | ||
215 | EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
216 | } |
217 | ||
332468f7 HS |
218 | /* |
219 | * Allocate memory pages. | |
220 | * | |
221 | * This function implements the AllocatePages service. | |
222 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
223 | * for details. | |
224 | * | |
225 | * @type type of allocation to be performed | |
226 | * @memory_type usage type of the allocated memory | |
227 | * @pages number of pages to be allocated | |
228 | * @memory allocated memory | |
229 | * @return status code | |
230 | */ | |
6e0bf8d8 | 231 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
f5a2a938 | 232 | efi_uintn_t pages, |
6e0bf8d8 | 233 | uint64_t *memory) |
bee91169 AG |
234 | { |
235 | efi_status_t r; | |
236 | ||
f5a2a938 | 237 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
bee91169 AG |
238 | r = efi_allocate_pages(type, memory_type, pages, memory); |
239 | return EFI_EXIT(r); | |
240 | } | |
241 | ||
332468f7 HS |
242 | /* |
243 | * Free memory pages. | |
244 | * | |
245 | * This function implements the FreePages service. | |
246 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
247 | * for details. | |
248 | * | |
249 | * @memory start of the memory area to be freed | |
250 | * @pages number of pages to be freed | |
251 | * @return status code | |
252 | */ | |
6e0bf8d8 | 253 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
f5a2a938 | 254 | efi_uintn_t pages) |
bee91169 AG |
255 | { |
256 | efi_status_t r; | |
257 | ||
f5a2a938 | 258 | EFI_ENTRY("%"PRIx64", 0x%zx", memory, pages); |
bee91169 AG |
259 | r = efi_free_pages(memory, pages); |
260 | return EFI_EXIT(r); | |
261 | } | |
262 | ||
332468f7 HS |
263 | /* |
264 | * Get map describing memory usage. | |
265 | * | |
266 | * This function implements the GetMemoryMap service. | |
267 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
268 | * for details. | |
269 | * | |
270 | * @memory_map_size on entry the size, in bytes, of the memory map buffer, | |
271 | * on exit the size of the copied memory map | |
272 | * @memory_map buffer to which the memory map is written | |
273 | * @map_key key for the memory map | |
274 | * @descriptor_size size of an individual memory descriptor | |
275 | * @descriptor_version version number of the memory descriptor structure | |
276 | * @return status code | |
277 | */ | |
6e0bf8d8 | 278 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
f5a2a938 | 279 | efi_uintn_t *memory_map_size, |
6e0bf8d8 | 280 | struct efi_mem_desc *memory_map, |
f5a2a938 HS |
281 | efi_uintn_t *map_key, |
282 | efi_uintn_t *descriptor_size, | |
6e0bf8d8 | 283 | uint32_t *descriptor_version) |
bee91169 AG |
284 | { |
285 | efi_status_t r; | |
286 | ||
287 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, | |
288 | map_key, descriptor_size, descriptor_version); | |
289 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, | |
290 | descriptor_size, descriptor_version); | |
291 | return EFI_EXIT(r); | |
292 | } | |
293 | ||
332468f7 HS |
294 | /* |
295 | * Allocate memory from pool. | |
296 | * | |
297 | * This function implements the AllocatePool service. | |
298 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
299 | * for details. | |
300 | * | |
301 | * @pool_type type of the pool from which memory is to be allocated | |
302 | * @size number of bytes to be allocated | |
303 | * @buffer allocated memory | |
304 | * @return status code | |
305 | */ | |
ead1274b | 306 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
f5a2a938 | 307 | efi_uintn_t size, |
ead1274b | 308 | void **buffer) |
bee91169 | 309 | { |
1cd29f0a AG |
310 | efi_status_t r; |
311 | ||
f5a2a938 | 312 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
ead1274b | 313 | r = efi_allocate_pool(pool_type, size, buffer); |
1cd29f0a | 314 | return EFI_EXIT(r); |
bee91169 AG |
315 | } |
316 | ||
332468f7 HS |
317 | /* |
318 | * Free memory from pool. | |
319 | * | |
320 | * This function implements the FreePool service. | |
321 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
322 | * for details. | |
323 | * | |
324 | * @buffer start of memory to be freed | |
325 | * @return status code | |
326 | */ | |
42417bc8 | 327 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
bee91169 | 328 | { |
1cd29f0a AG |
329 | efi_status_t r; |
330 | ||
331 | EFI_ENTRY("%p", buffer); | |
42417bc8 | 332 | r = efi_free_pool(buffer); |
1cd29f0a | 333 | return EFI_EXIT(r); |
bee91169 AG |
334 | } |
335 | ||
44549d62 HS |
336 | /* |
337 | * Add a new object to the object list. | |
338 | * | |
339 | * The protocols list is initialized. | |
340 | * The object handle is set. | |
341 | * | |
342 | * @obj object to be added | |
343 | */ | |
344 | void efi_add_handle(struct efi_object *obj) | |
345 | { | |
346 | if (!obj) | |
347 | return; | |
348 | INIT_LIST_HEAD(&obj->protocols); | |
349 | obj->handle = obj; | |
350 | list_add_tail(&obj->link, &efi_obj_list); | |
351 | } | |
352 | ||
2edab5e2 HS |
353 | /* |
354 | * Create handle. | |
355 | * | |
356 | * @handle new handle | |
357 | * @return status code | |
358 | */ | |
2074f700 | 359 | efi_status_t efi_create_handle(efi_handle_t *handle) |
3cc6e3fe HS |
360 | { |
361 | struct efi_object *obj; | |
362 | efi_status_t r; | |
363 | ||
364 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, | |
365 | sizeof(struct efi_object), | |
366 | (void **)&obj); | |
367 | if (r != EFI_SUCCESS) | |
368 | return r; | |
44549d62 HS |
369 | efi_add_handle(obj); |
370 | *handle = obj->handle; | |
3cc6e3fe HS |
371 | return r; |
372 | } | |
373 | ||
678e03a0 HS |
374 | /* |
375 | * Find a protocol on a handle. | |
376 | * | |
377 | * @handle handle | |
378 | * @protocol_guid GUID of the protocol | |
379 | * @handler reference to the protocol | |
380 | * @return status code | |
381 | */ | |
2074f700 | 382 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
678e03a0 HS |
383 | const efi_guid_t *protocol_guid, |
384 | struct efi_handler **handler) | |
385 | { | |
386 | struct efi_object *efiobj; | |
387 | struct list_head *lhandle; | |
388 | ||
389 | if (!handle || !protocol_guid) | |
390 | return EFI_INVALID_PARAMETER; | |
391 | efiobj = efi_search_obj(handle); | |
392 | if (!efiobj) | |
393 | return EFI_INVALID_PARAMETER; | |
394 | list_for_each(lhandle, &efiobj->protocols) { | |
395 | struct efi_handler *protocol; | |
396 | ||
397 | protocol = list_entry(lhandle, struct efi_handler, link); | |
398 | if (!guidcmp(protocol->guid, protocol_guid)) { | |
399 | if (handler) | |
400 | *handler = protocol; | |
401 | return EFI_SUCCESS; | |
402 | } | |
403 | } | |
404 | return EFI_NOT_FOUND; | |
405 | } | |
406 | ||
407 | /* | |
408 | * Delete protocol from a handle. | |
409 | * | |
410 | * @handle handle from which the protocol shall be deleted | |
411 | * @protocol GUID of the protocol to be deleted | |
412 | * @protocol_interface interface of the protocol implementation | |
413 | * @return status code | |
414 | */ | |
2074f700 HS |
415 | efi_status_t efi_remove_protocol(const efi_handle_t handle, |
416 | const efi_guid_t *protocol, | |
678e03a0 HS |
417 | void *protocol_interface) |
418 | { | |
419 | struct efi_handler *handler; | |
420 | efi_status_t ret; | |
421 | ||
422 | ret = efi_search_protocol(handle, protocol, &handler); | |
423 | if (ret != EFI_SUCCESS) | |
424 | return ret; | |
425 | if (guidcmp(handler->guid, protocol)) | |
426 | return EFI_INVALID_PARAMETER; | |
427 | list_del(&handler->link); | |
428 | free(handler); | |
429 | return EFI_SUCCESS; | |
430 | } | |
431 | ||
432 | /* | |
433 | * Delete all protocols from a handle. | |
434 | * | |
435 | * @handle handle from which the protocols shall be deleted | |
436 | * @return status code | |
437 | */ | |
2074f700 | 438 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
678e03a0 HS |
439 | { |
440 | struct efi_object *efiobj; | |
32e6fed6 HS |
441 | struct efi_handler *protocol; |
442 | struct efi_handler *pos; | |
678e03a0 HS |
443 | |
444 | efiobj = efi_search_obj(handle); | |
445 | if (!efiobj) | |
446 | return EFI_INVALID_PARAMETER; | |
32e6fed6 | 447 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
678e03a0 HS |
448 | efi_status_t ret; |
449 | ||
678e03a0 HS |
450 | ret = efi_remove_protocol(handle, protocol->guid, |
451 | protocol->protocol_interface); | |
452 | if (ret != EFI_SUCCESS) | |
453 | return ret; | |
454 | } | |
455 | return EFI_SUCCESS; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Delete handle. | |
460 | * | |
461 | * @handle handle to delete | |
462 | */ | |
463 | void efi_delete_handle(struct efi_object *obj) | |
464 | { | |
465 | if (!obj) | |
466 | return; | |
467 | efi_remove_all_protocols(obj->handle); | |
468 | list_del(&obj->link); | |
469 | free(obj); | |
470 | } | |
471 | ||
bee91169 | 472 | /* |
c6841592 | 473 | * Our event capabilities are very limited. Only a small limited |
474 | * number of events is allowed to coexist. | |
bee91169 | 475 | */ |
c6841592 | 476 | static struct efi_event efi_events[16]; |
bee91169 | 477 | |
332468f7 HS |
478 | /* |
479 | * Create an event. | |
480 | * | |
481 | * This function is used inside U-Boot code to create an event. | |
482 | * | |
483 | * For the API function implementing the CreateEvent service see | |
484 | * efi_create_event_ext. | |
485 | * | |
486 | * @type type of the event to create | |
487 | * @notify_tpl task priority level of the event | |
488 | * @notify_function notification function of the event | |
489 | * @notify_context pointer passed to the notification function | |
490 | * @event created event | |
491 | * @return status code | |
492 | */ | |
152cade3 | 493 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
49deb455 | 494 | void (EFIAPI *notify_function) ( |
2fd945fe | 495 | struct efi_event *event, |
496 | void *context), | |
49deb455 | 497 | void *notify_context, struct efi_event **event) |
bee91169 | 498 | { |
c6841592 | 499 | int i; |
500 | ||
a95343b8 | 501 | if (event == NULL) |
49deb455 | 502 | return EFI_INVALID_PARAMETER; |
a95343b8 JG |
503 | |
504 | if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT)) | |
49deb455 | 505 | return EFI_INVALID_PARAMETER; |
a95343b8 JG |
506 | |
507 | if ((type & (EVT_NOTIFY_SIGNAL|EVT_NOTIFY_WAIT)) && | |
508 | notify_function == NULL) | |
49deb455 | 509 | return EFI_INVALID_PARAMETER; |
a95343b8 | 510 | |
c6841592 | 511 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
512 | if (efi_events[i].type) | |
513 | continue; | |
514 | efi_events[i].type = type; | |
515 | efi_events[i].notify_tpl = notify_tpl; | |
516 | efi_events[i].notify_function = notify_function; | |
517 | efi_events[i].notify_context = notify_context; | |
518 | /* Disable timers on bootup */ | |
519 | efi_events[i].trigger_next = -1ULL; | |
e190e897 HS |
520 | efi_events[i].is_queued = false; |
521 | efi_events[i].is_signaled = false; | |
c6841592 | 522 | *event = &efi_events[i]; |
49deb455 | 523 | return EFI_SUCCESS; |
c6841592 | 524 | } |
49deb455 | 525 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
526 | } |
527 | ||
9f0930e5 HS |
528 | /* |
529 | * Create an event in a group. | |
530 | * | |
531 | * This function implements the CreateEventEx service. | |
532 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
533 | * for details. | |
534 | * TODO: Support event groups | |
535 | * | |
536 | * @type type of the event to create | |
537 | * @notify_tpl task priority level of the event | |
538 | * @notify_function notification function of the event | |
539 | * @notify_context pointer passed to the notification function | |
540 | * @event created event | |
541 | * @event_group event group | |
542 | * @return status code | |
543 | */ | |
544 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, | |
545 | void (EFIAPI *notify_function) ( | |
546 | struct efi_event *event, | |
547 | void *context), | |
548 | void *notify_context, | |
549 | efi_guid_t *event_group, | |
550 | struct efi_event **event) | |
551 | { | |
552 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function, | |
553 | notify_context, event_group); | |
554 | if (event_group) | |
555 | return EFI_EXIT(EFI_UNSUPPORTED); | |
556 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
557 | notify_context, event)); | |
558 | } | |
559 | ||
332468f7 HS |
560 | /* |
561 | * Create an event. | |
562 | * | |
563 | * This function implements the CreateEvent service. | |
564 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
565 | * for details. | |
566 | * | |
567 | * @type type of the event to create | |
568 | * @notify_tpl task priority level of the event | |
569 | * @notify_function notification function of the event | |
570 | * @notify_context pointer passed to the notification function | |
571 | * @event created event | |
572 | * @return status code | |
573 | */ | |
49deb455 | 574 | static efi_status_t EFIAPI efi_create_event_ext( |
152cade3 | 575 | uint32_t type, efi_uintn_t notify_tpl, |
49deb455 | 576 | void (EFIAPI *notify_function) ( |
577 | struct efi_event *event, | |
578 | void *context), | |
579 | void *notify_context, struct efi_event **event) | |
580 | { | |
581 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, | |
582 | notify_context); | |
583 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
584 | notify_context, event)); | |
585 | } | |
586 | ||
587 | ||
bee91169 | 588 | /* |
332468f7 HS |
589 | * Check if a timer event has occurred or a queued notification function should |
590 | * be called. | |
591 | * | |
bee91169 | 592 | * Our timers have to work without interrupts, so we check whenever keyboard |
332468f7 | 593 | * input or disk accesses happen if enough time elapsed for them to fire. |
bee91169 AG |
594 | */ |
595 | void efi_timer_check(void) | |
596 | { | |
c6841592 | 597 | int i; |
bee91169 AG |
598 | u64 now = timer_get_us(); |
599 | ||
c6841592 | 600 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
ca62a4f5 HS |
601 | if (!efi_events[i].type) |
602 | continue; | |
e190e897 | 603 | if (efi_events[i].is_queued) |
9bc9664d | 604 | efi_signal_event(&efi_events[i], true); |
ca62a4f5 | 605 | if (!(efi_events[i].type & EVT_TIMER) || |
c6841592 | 606 | now < efi_events[i].trigger_next) |
607 | continue; | |
ca62a4f5 HS |
608 | switch (efi_events[i].trigger_type) { |
609 | case EFI_TIMER_RELATIVE: | |
610 | efi_events[i].trigger_type = EFI_TIMER_STOP; | |
611 | break; | |
612 | case EFI_TIMER_PERIODIC: | |
c6841592 | 613 | efi_events[i].trigger_next += |
8787b02e | 614 | efi_events[i].trigger_time; |
ca62a4f5 HS |
615 | break; |
616 | default: | |
617 | continue; | |
c6841592 | 618 | } |
e190e897 | 619 | efi_events[i].is_signaled = true; |
9bc9664d | 620 | efi_signal_event(&efi_events[i], true); |
bee91169 | 621 | } |
bee91169 AG |
622 | WATCHDOG_RESET(); |
623 | } | |
624 | ||
332468f7 HS |
625 | /* |
626 | * Set the trigger time for a timer event or stop the event. | |
627 | * | |
628 | * This is the function for internal usage in U-Boot. For the API function | |
629 | * implementing the SetTimer service see efi_set_timer_ext. | |
630 | * | |
631 | * @event event for which the timer is set | |
632 | * @type type of the timer | |
633 | * @trigger_time trigger period in multiples of 100ns | |
634 | * @return status code | |
635 | */ | |
b521d29e | 636 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
bfc72462 | 637 | uint64_t trigger_time) |
bee91169 | 638 | { |
c6841592 | 639 | int i; |
bee91169 | 640 | |
8787b02e | 641 | /* |
642 | * The parameter defines a multiple of 100ns. | |
643 | * We use multiples of 1000ns. So divide by 10. | |
644 | */ | |
7d963323 | 645 | do_div(trigger_time, 10); |
bee91169 | 646 | |
c6841592 | 647 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
648 | if (event != &efi_events[i]) | |
649 | continue; | |
bee91169 | 650 | |
c6841592 | 651 | if (!(event->type & EVT_TIMER)) |
652 | break; | |
653 | switch (type) { | |
654 | case EFI_TIMER_STOP: | |
655 | event->trigger_next = -1ULL; | |
656 | break; | |
657 | case EFI_TIMER_PERIODIC: | |
658 | case EFI_TIMER_RELATIVE: | |
659 | event->trigger_next = | |
8787b02e | 660 | timer_get_us() + trigger_time; |
c6841592 | 661 | break; |
662 | default: | |
bfc72462 | 663 | return EFI_INVALID_PARAMETER; |
c6841592 | 664 | } |
665 | event->trigger_type = type; | |
666 | event->trigger_time = trigger_time; | |
e190e897 | 667 | event->is_signaled = false; |
bfc72462 | 668 | return EFI_SUCCESS; |
bee91169 | 669 | } |
bfc72462 | 670 | return EFI_INVALID_PARAMETER; |
671 | } | |
672 | ||
332468f7 HS |
673 | /* |
674 | * Set the trigger time for a timer event or stop the event. | |
675 | * | |
676 | * This function implements the SetTimer service. | |
677 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
678 | * for details. | |
679 | * | |
680 | * @event event for which the timer is set | |
681 | * @type type of the timer | |
682 | * @trigger_time trigger period in multiples of 100ns | |
683 | * @return status code | |
684 | */ | |
b521d29e | 685 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
686 | enum efi_timer_delay type, | |
687 | uint64_t trigger_time) | |
bfc72462 | 688 | { |
689 | EFI_ENTRY("%p, %d, %"PRIx64, event, type, trigger_time); | |
690 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); | |
bee91169 AG |
691 | } |
692 | ||
332468f7 HS |
693 | /* |
694 | * Wait for events to be signaled. | |
695 | * | |
696 | * This function implements the WaitForEvent service. | |
697 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
698 | * for details. | |
699 | * | |
700 | * @num_events number of events to be waited for | |
701 | * @events events to be waited for | |
702 | * @index index of the event that was signaled | |
703 | * @return status code | |
704 | */ | |
f5a2a938 | 705 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
2fd945fe | 706 | struct efi_event **event, |
f5a2a938 | 707 | efi_uintn_t *index) |
bee91169 | 708 | { |
c6841592 | 709 | int i, j; |
bee91169 | 710 | |
f5a2a938 | 711 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
bee91169 | 712 | |
c6841592 | 713 | /* Check parameters */ |
714 | if (!num_events || !event) | |
715 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
32f4b2f8 HS |
716 | /* Check TPL */ |
717 | if (efi_tpl != TPL_APPLICATION) | |
718 | return EFI_EXIT(EFI_UNSUPPORTED); | |
c6841592 | 719 | for (i = 0; i < num_events; ++i) { |
720 | for (j = 0; j < ARRAY_SIZE(efi_events); ++j) { | |
721 | if (event[i] == &efi_events[j]) | |
722 | goto known_event; | |
723 | } | |
724 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
725 | known_event: | |
726 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) | |
727 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
e190e897 | 728 | if (!event[i]->is_signaled) |
9bc9664d | 729 | efi_signal_event(event[i], true); |
c6841592 | 730 | } |
731 | ||
732 | /* Wait for signal */ | |
733 | for (;;) { | |
734 | for (i = 0; i < num_events; ++i) { | |
e190e897 | 735 | if (event[i]->is_signaled) |
c6841592 | 736 | goto out; |
737 | } | |
738 | /* Allow events to occur. */ | |
739 | efi_timer_check(); | |
740 | } | |
741 | ||
742 | out: | |
743 | /* | |
744 | * Reset the signal which is passed to the caller to allow periodic | |
745 | * events to occur. | |
746 | */ | |
e190e897 | 747 | event[i]->is_signaled = false; |
c6841592 | 748 | if (index) |
749 | *index = i; | |
bee91169 AG |
750 | |
751 | return EFI_EXIT(EFI_SUCCESS); | |
752 | } | |
753 | ||
332468f7 HS |
754 | /* |
755 | * Signal an EFI event. | |
756 | * | |
757 | * This function implements the SignalEvent service. | |
758 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
759 | * for details. | |
760 | * | |
761 | * This functions sets the signaled state of the event and queues the | |
762 | * notification function for execution. | |
763 | * | |
764 | * @event event to signal | |
10a08c4e | 765 | * @return status code |
332468f7 | 766 | */ |
c6841592 | 767 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
bee91169 | 768 | { |
c6841592 | 769 | int i; |
770 | ||
bee91169 | 771 | EFI_ENTRY("%p", event); |
c6841592 | 772 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
773 | if (event != &efi_events[i]) | |
774 | continue; | |
e190e897 | 775 | if (event->is_signaled) |
ca62a4f5 | 776 | break; |
e190e897 | 777 | event->is_signaled = true; |
ca62a4f5 | 778 | if (event->type & EVT_NOTIFY_SIGNAL) |
9bc9664d | 779 | efi_signal_event(event, true); |
c6841592 | 780 | break; |
781 | } | |
bee91169 AG |
782 | return EFI_EXIT(EFI_SUCCESS); |
783 | } | |
784 | ||
332468f7 HS |
785 | /* |
786 | * Close an EFI event. | |
787 | * | |
788 | * This function implements the CloseEvent service. | |
789 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
790 | * for details. | |
791 | * | |
792 | * @event event to close | |
793 | * @return status code | |
794 | */ | |
2fd945fe | 795 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
bee91169 | 796 | { |
c6841592 | 797 | int i; |
798 | ||
bee91169 | 799 | EFI_ENTRY("%p", event); |
c6841592 | 800 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { |
801 | if (event == &efi_events[i]) { | |
802 | event->type = 0; | |
803 | event->trigger_next = -1ULL; | |
e190e897 HS |
804 | event->is_queued = false; |
805 | event->is_signaled = false; | |
c6841592 | 806 | return EFI_EXIT(EFI_SUCCESS); |
807 | } | |
808 | } | |
809 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
810 | } |
811 | ||
332468f7 HS |
812 | /* |
813 | * Check if an event is signaled. | |
814 | * | |
815 | * This function implements the CheckEvent service. | |
816 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
817 | * for details. | |
818 | * | |
819 | * If an event is not signaled yet the notification function is queued. | |
820 | * | |
821 | * @event event to check | |
822 | * @return status code | |
823 | */ | |
2fd945fe | 824 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
bee91169 | 825 | { |
c6841592 | 826 | int i; |
827 | ||
bee91169 | 828 | EFI_ENTRY("%p", event); |
c6841592 | 829 | efi_timer_check(); |
830 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { | |
831 | if (event != &efi_events[i]) | |
832 | continue; | |
833 | if (!event->type || event->type & EVT_NOTIFY_SIGNAL) | |
834 | break; | |
e190e897 | 835 | if (!event->is_signaled) |
9bc9664d | 836 | efi_signal_event(event, true); |
e190e897 | 837 | if (event->is_signaled) |
c6841592 | 838 | return EFI_EXIT(EFI_SUCCESS); |
839 | return EFI_EXIT(EFI_NOT_READY); | |
840 | } | |
841 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
842 | } |
843 | ||
7b9f8ad7 HS |
844 | /* |
845 | * Find the internal EFI object for a handle. | |
846 | * | |
847 | * @handle handle to find | |
848 | * @return EFI object | |
849 | */ | |
2074f700 | 850 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
7b9f8ad7 | 851 | { |
1b68153a | 852 | struct efi_object *efiobj; |
7b9f8ad7 | 853 | |
1b68153a | 854 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
7b9f8ad7 HS |
855 | if (efiobj->handle == handle) |
856 | return efiobj; | |
857 | } | |
858 | ||
859 | return NULL; | |
860 | } | |
861 | ||
fe1599da HS |
862 | /* |
863 | * Create open protocol info entry and add it to a protocol. | |
864 | * | |
865 | * @handler handler of a protocol | |
866 | * @return open protocol info entry | |
867 | */ | |
868 | static struct efi_open_protocol_info_entry *efi_create_open_info( | |
869 | struct efi_handler *handler) | |
870 | { | |
871 | struct efi_open_protocol_info_item *item; | |
872 | ||
873 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); | |
874 | if (!item) | |
875 | return NULL; | |
876 | /* Append the item to the open protocol info list. */ | |
877 | list_add_tail(&item->link, &handler->open_infos); | |
878 | ||
879 | return &item->info; | |
880 | } | |
881 | ||
882 | /* | |
883 | * Remove an open protocol info entry from a protocol. | |
884 | * | |
885 | * @handler handler of a protocol | |
886 | * @return status code | |
887 | */ | |
888 | static efi_status_t efi_delete_open_info( | |
889 | struct efi_open_protocol_info_item *item) | |
890 | { | |
891 | list_del(&item->link); | |
892 | free(item); | |
893 | return EFI_SUCCESS; | |
894 | } | |
895 | ||
3f79a2b5 HS |
896 | /* |
897 | * Install new protocol on a handle. | |
898 | * | |
899 | * @handle handle on which the protocol shall be installed | |
900 | * @protocol GUID of the protocol to be installed | |
901 | * @protocol_interface interface of the protocol implementation | |
902 | * @return status code | |
903 | */ | |
2074f700 HS |
904 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
905 | const efi_guid_t *protocol, | |
3f79a2b5 HS |
906 | void *protocol_interface) |
907 | { | |
908 | struct efi_object *efiobj; | |
909 | struct efi_handler *handler; | |
910 | efi_status_t ret; | |
3f79a2b5 HS |
911 | |
912 | efiobj = efi_search_obj(handle); | |
913 | if (!efiobj) | |
914 | return EFI_INVALID_PARAMETER; | |
915 | ret = efi_search_protocol(handle, protocol, NULL); | |
916 | if (ret != EFI_NOT_FOUND) | |
917 | return EFI_INVALID_PARAMETER; | |
918 | handler = calloc(1, sizeof(struct efi_handler)); | |
919 | if (!handler) | |
920 | return EFI_OUT_OF_RESOURCES; | |
69fb6b1a HS |
921 | handler->guid = protocol; |
922 | handler->protocol_interface = protocol_interface; | |
fe1599da | 923 | INIT_LIST_HEAD(&handler->open_infos); |
69fb6b1a | 924 | list_add_tail(&handler->link, &efiobj->protocols); |
d5504144 HS |
925 | if (!guidcmp(&efi_guid_device_path, protocol)) |
926 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); | |
69fb6b1a | 927 | return EFI_SUCCESS; |
3f79a2b5 HS |
928 | } |
929 | ||
332468f7 HS |
930 | /* |
931 | * Install protocol interface. | |
932 | * | |
1760ef57 HS |
933 | * This function implements the InstallProtocolInterface service. |
934 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
935 | * for details. | |
332468f7 HS |
936 | * |
937 | * @handle handle on which the protocol shall be installed | |
938 | * @protocol GUID of the protocol to be installed | |
939 | * @protocol_interface_type type of the interface to be installed, | |
940 | * always EFI_NATIVE_INTERFACE | |
941 | * @protocol_interface interface of the protocol implementation | |
942 | * @return status code | |
943 | */ | |
1760ef57 HS |
944 | static efi_status_t EFIAPI efi_install_protocol_interface( |
945 | void **handle, const efi_guid_t *protocol, | |
946 | int protocol_interface_type, void *protocol_interface) | |
bee91169 | 947 | { |
e0549f8a | 948 | efi_status_t r; |
949 | ||
1760ef57 HS |
950 | EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type, |
951 | protocol_interface); | |
952 | ||
e0549f8a | 953 | if (!handle || !protocol || |
954 | protocol_interface_type != EFI_NATIVE_INTERFACE) { | |
955 | r = EFI_INVALID_PARAMETER; | |
956 | goto out; | |
957 | } | |
958 | ||
959 | /* Create new handle if requested. */ | |
960 | if (!*handle) { | |
3cc6e3fe HS |
961 | r = efi_create_handle(handle); |
962 | if (r != EFI_SUCCESS) | |
963 | goto out; | |
af1408e0 HS |
964 | debug("%sEFI: new handle %p\n", indent_string(nesting_level), |
965 | *handle); | |
966 | } else { | |
967 | debug("%sEFI: handle %p\n", indent_string(nesting_level), | |
968 | *handle); | |
e0549f8a | 969 | } |
1202530d HS |
970 | /* Add new protocol */ |
971 | r = efi_add_protocol(*handle, protocol, protocol_interface); | |
e0549f8a | 972 | out: |
1760ef57 | 973 | return EFI_EXIT(r); |
bee91169 | 974 | } |
e0549f8a | 975 | |
332468f7 HS |
976 | /* |
977 | * Reinstall protocol interface. | |
978 | * | |
979 | * This function implements the ReinstallProtocolInterface service. | |
980 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
981 | * for details. | |
982 | * | |
983 | * @handle handle on which the protocol shall be | |
984 | * reinstalled | |
985 | * @protocol GUID of the protocol to be installed | |
986 | * @old_interface interface to be removed | |
987 | * @new_interface interface to be installed | |
988 | * @return status code | |
989 | */ | |
2074f700 HS |
990 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( |
991 | efi_handle_t handle, const efi_guid_t *protocol, | |
992 | void *old_interface, void *new_interface) | |
bee91169 | 993 | { |
778e6af8 | 994 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface, |
bee91169 AG |
995 | new_interface); |
996 | return EFI_EXIT(EFI_ACCESS_DENIED); | |
997 | } | |
998 | ||
3f9b0042 HS |
999 | /* |
1000 | * Get all drivers associated to a controller. | |
1001 | * The allocated buffer has to be freed with free(). | |
1002 | * | |
1003 | * @efiobj handle of the controller | |
1004 | * @protocol protocol guid (optional) | |
1005 | * @number_of_drivers number of child controllers | |
1006 | * @driver_handle_buffer handles of the the drivers | |
1007 | * @return status code | |
1008 | */ | |
1009 | static efi_status_t efi_get_drivers(struct efi_object *efiobj, | |
1010 | const efi_guid_t *protocol, | |
1011 | efi_uintn_t *number_of_drivers, | |
1012 | efi_handle_t **driver_handle_buffer) | |
1013 | { | |
1014 | struct efi_handler *handler; | |
1015 | struct efi_open_protocol_info_item *item; | |
1016 | efi_uintn_t count = 0, i; | |
1017 | bool duplicate; | |
1018 | ||
1019 | /* Count all driver associations */ | |
1020 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
1021 | if (protocol && guidcmp(handler->guid, protocol)) | |
1022 | continue; | |
1023 | list_for_each_entry(item, &handler->open_infos, link) { | |
1024 | if (item->info.attributes & | |
1025 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
1026 | ++count; | |
1027 | } | |
1028 | } | |
1029 | /* | |
1030 | * Create buffer. In case of duplicate driver assignments the buffer | |
1031 | * will be too large. But that does not harm. | |
1032 | */ | |
1033 | *number_of_drivers = 0; | |
1034 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); | |
1035 | if (!*driver_handle_buffer) | |
1036 | return EFI_OUT_OF_RESOURCES; | |
1037 | /* Collect unique driver handles */ | |
1038 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
1039 | if (protocol && guidcmp(handler->guid, protocol)) | |
1040 | continue; | |
1041 | list_for_each_entry(item, &handler->open_infos, link) { | |
1042 | if (item->info.attributes & | |
1043 | EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
1044 | /* Check this is a new driver */ | |
1045 | duplicate = false; | |
1046 | for (i = 0; i < *number_of_drivers; ++i) { | |
1047 | if ((*driver_handle_buffer)[i] == | |
1048 | item->info.agent_handle) | |
1049 | duplicate = true; | |
1050 | } | |
1051 | /* Copy handle to buffer */ | |
1052 | if (!duplicate) { | |
1053 | i = (*number_of_drivers)++; | |
1054 | (*driver_handle_buffer)[i] = | |
1055 | item->info.agent_handle; | |
1056 | } | |
1057 | } | |
1058 | } | |
1059 | } | |
1060 | return EFI_SUCCESS; | |
1061 | } | |
1062 | ||
1063 | /* | |
1064 | * Disconnect all drivers from a controller. | |
1065 | * | |
1066 | * This function implements the DisconnectController service. | |
1067 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1068 | * for details. | |
1069 | * | |
1070 | * @efiobj handle of the controller | |
1071 | * @protocol protocol guid (optional) | |
1072 | * @child_handle handle of the child to destroy | |
1073 | * @return status code | |
1074 | */ | |
1075 | static efi_status_t efi_disconnect_all_drivers( | |
1076 | struct efi_object *efiobj, | |
1077 | const efi_guid_t *protocol, | |
1078 | efi_handle_t child_handle) | |
1079 | { | |
1080 | efi_uintn_t number_of_drivers; | |
1081 | efi_handle_t *driver_handle_buffer; | |
1082 | efi_status_t r, ret; | |
1083 | ||
1084 | ret = efi_get_drivers(efiobj, protocol, &number_of_drivers, | |
1085 | &driver_handle_buffer); | |
1086 | if (ret != EFI_SUCCESS) | |
1087 | return ret; | |
1088 | ||
1089 | ret = EFI_NOT_FOUND; | |
1090 | while (number_of_drivers) { | |
1091 | r = EFI_CALL(efi_disconnect_controller( | |
1092 | efiobj->handle, | |
1093 | driver_handle_buffer[--number_of_drivers], | |
1094 | child_handle)); | |
1095 | if (r == EFI_SUCCESS) | |
1096 | ret = r; | |
1097 | } | |
1098 | free(driver_handle_buffer); | |
1099 | return ret; | |
1100 | } | |
1101 | ||
332468f7 HS |
1102 | /* |
1103 | * Uninstall protocol interface. | |
1104 | * | |
cd534083 HS |
1105 | * This function implements the UninstallProtocolInterface service. |
1106 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1107 | * for details. | |
332468f7 HS |
1108 | * |
1109 | * @handle handle from which the protocol shall be removed | |
1110 | * @protocol GUID of the protocol to be removed | |
1111 | * @protocol_interface interface to be removed | |
1112 | * @return status code | |
1113 | */ | |
cd534083 | 1114 | static efi_status_t EFIAPI efi_uninstall_protocol_interface( |
2074f700 | 1115 | efi_handle_t handle, const efi_guid_t *protocol, |
cd534083 | 1116 | void *protocol_interface) |
bee91169 | 1117 | { |
ad97373b | 1118 | struct efi_object *efiobj; |
5810511d | 1119 | struct efi_handler *handler; |
ad97373b HS |
1120 | struct efi_open_protocol_info_item *item; |
1121 | struct efi_open_protocol_info_item *pos; | |
5810511d | 1122 | efi_status_t r; |
4b6ed0d7 | 1123 | |
cd534083 HS |
1124 | EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface); |
1125 | ||
ad97373b HS |
1126 | /* Check handle */ |
1127 | efiobj = efi_search_obj(handle); | |
1128 | if (!efiobj) { | |
4b6ed0d7 | 1129 | r = EFI_INVALID_PARAMETER; |
1130 | goto out; | |
1131 | } | |
5810511d HS |
1132 | /* Find the protocol on the handle */ |
1133 | r = efi_search_protocol(handle, protocol, &handler); | |
1134 | if (r != EFI_SUCCESS) | |
1135 | goto out; | |
ad97373b HS |
1136 | /* Disconnect controllers */ |
1137 | efi_disconnect_all_drivers(efiobj, protocol, NULL); | |
1138 | if (!list_empty(&handler->open_infos)) { | |
5810511d | 1139 | r = EFI_ACCESS_DENIED; |
ad97373b HS |
1140 | goto out; |
1141 | } | |
1142 | /* Close protocol */ | |
1143 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1144 | if (item->info.attributes == | |
1145 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || | |
1146 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || | |
1147 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
1148 | list_del(&item->link); | |
1149 | } | |
1150 | if (!list_empty(&handler->open_infos)) { | |
1151 | r = EFI_ACCESS_DENIED; | |
1152 | goto out; | |
4b6ed0d7 | 1153 | } |
ad97373b | 1154 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
4b6ed0d7 | 1155 | out: |
cd534083 | 1156 | return EFI_EXIT(r); |
bee91169 AG |
1157 | } |
1158 | ||
332468f7 HS |
1159 | /* |
1160 | * Register an event for notification when a protocol is installed. | |
1161 | * | |
1162 | * This function implements the RegisterProtocolNotify service. | |
1163 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1164 | * for details. | |
1165 | * | |
1166 | * @protocol GUID of the protocol whose installation shall be | |
1167 | * notified | |
1168 | * @event event to be signaled upon installation of the protocol | |
1169 | * @registration key for retrieving the registration information | |
1170 | * @return status code | |
1171 | */ | |
5a9682d0 HS |
1172 | static efi_status_t EFIAPI efi_register_protocol_notify( |
1173 | const efi_guid_t *protocol, | |
1174 | struct efi_event *event, | |
1175 | void **registration) | |
bee91169 | 1176 | { |
778e6af8 | 1177 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
bee91169 AG |
1178 | return EFI_EXIT(EFI_OUT_OF_RESOURCES); |
1179 | } | |
1180 | ||
332468f7 HS |
1181 | /* |
1182 | * Determine if an EFI handle implements a protocol. | |
1183 | * | |
1184 | * See the documentation of the LocateHandle service in the UEFI specification. | |
1185 | * | |
1186 | * @search_type selection criterion | |
1187 | * @protocol GUID of the protocol | |
1188 | * @search_key registration key | |
1189 | * @efiobj handle | |
1190 | * @return 0 if the handle implements the protocol | |
1191 | */ | |
bee91169 | 1192 | static int efi_search(enum efi_locate_search_type search_type, |
5a9682d0 | 1193 | const efi_guid_t *protocol, void *search_key, |
bee91169 AG |
1194 | struct efi_object *efiobj) |
1195 | { | |
42cf1242 | 1196 | efi_status_t ret; |
bee91169 AG |
1197 | |
1198 | switch (search_type) { | |
9f0770ff | 1199 | case ALL_HANDLES: |
bee91169 | 1200 | return 0; |
9f0770ff | 1201 | case BY_REGISTER_NOTIFY: |
42cf1242 | 1202 | /* TODO: RegisterProtocolNotify is not implemented yet */ |
bee91169 | 1203 | return -1; |
9f0770ff | 1204 | case BY_PROTOCOL: |
42cf1242 HS |
1205 | ret = efi_search_protocol(efiobj->handle, protocol, NULL); |
1206 | return (ret != EFI_SUCCESS); | |
1207 | default: | |
1208 | /* Invalid search type */ | |
bee91169 AG |
1209 | return -1; |
1210 | } | |
bee91169 AG |
1211 | } |
1212 | ||
332468f7 HS |
1213 | /* |
1214 | * Locate handles implementing a protocol. | |
1215 | * | |
1216 | * This function is meant for U-Boot internal calls. For the API implementation | |
1217 | * of the LocateHandle service see efi_locate_handle_ext. | |
1218 | * | |
1219 | * @search_type selection criterion | |
1220 | * @protocol GUID of the protocol | |
1221 | * @search_key registration key | |
1222 | * @buffer_size size of the buffer to receive the handles in bytes | |
1223 | * @buffer buffer to receive the relevant handles | |
1224 | * @return status code | |
1225 | */ | |
ebf199b9 | 1226 | static efi_status_t efi_locate_handle( |
bee91169 | 1227 | enum efi_locate_search_type search_type, |
5a9682d0 | 1228 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1229 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
bee91169 | 1230 | { |
caf864e4 | 1231 | struct efi_object *efiobj; |
f5a2a938 | 1232 | efi_uintn_t size = 0; |
bee91169 | 1233 | |
caf864e4 HS |
1234 | /* Check parameters */ |
1235 | switch (search_type) { | |
1236 | case ALL_HANDLES: | |
1237 | break; | |
1238 | case BY_REGISTER_NOTIFY: | |
1239 | if (!search_key) | |
1240 | return EFI_INVALID_PARAMETER; | |
1241 | /* RegisterProtocolNotify is not implemented yet */ | |
1242 | return EFI_UNSUPPORTED; | |
1243 | case BY_PROTOCOL: | |
1244 | if (!protocol) | |
1245 | return EFI_INVALID_PARAMETER; | |
1246 | break; | |
1247 | default: | |
1248 | return EFI_INVALID_PARAMETER; | |
1249 | } | |
1250 | ||
1251 | /* | |
1252 | * efi_locate_handle_buffer uses this function for | |
1253 | * the calculation of the necessary buffer size. | |
1254 | * So do not require a buffer for buffersize == 0. | |
1255 | */ | |
1256 | if (!buffer_size || (*buffer_size && !buffer)) | |
1257 | return EFI_INVALID_PARAMETER; | |
1258 | ||
bee91169 | 1259 | /* Count how much space we need */ |
caf864e4 HS |
1260 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
1261 | if (!efi_search(search_type, protocol, search_key, efiobj)) | |
bee91169 | 1262 | size += sizeof(void*); |
bee91169 AG |
1263 | } |
1264 | ||
1265 | if (*buffer_size < size) { | |
1266 | *buffer_size = size; | |
26329584 | 1267 | return EFI_BUFFER_TOO_SMALL; |
bee91169 AG |
1268 | } |
1269 | ||
796a78cb RC |
1270 | *buffer_size = size; |
1271 | if (size == 0) | |
1272 | return EFI_NOT_FOUND; | |
1273 | ||
bee91169 | 1274 | /* Then fill the array */ |
caf864e4 HS |
1275 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
1276 | if (!efi_search(search_type, protocol, search_key, efiobj)) | |
1277 | *buffer++ = efiobj->handle; | |
bee91169 AG |
1278 | } |
1279 | ||
26329584 | 1280 | return EFI_SUCCESS; |
1281 | } | |
1282 | ||
332468f7 HS |
1283 | /* |
1284 | * Locate handles implementing a protocol. | |
1285 | * | |
1286 | * This function implements the LocateHandle service. | |
1287 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1288 | * for details. | |
1289 | * | |
1290 | * @search_type selection criterion | |
1291 | * @protocol GUID of the protocol | |
1292 | * @search_key registration key | |
1293 | * @buffer_size size of the buffer to receive the handles in bytes | |
1294 | * @buffer buffer to receive the relevant handles | |
1295 | * @return 0 if the handle implements the protocol | |
1296 | */ | |
26329584 | 1297 | static efi_status_t EFIAPI efi_locate_handle_ext( |
1298 | enum efi_locate_search_type search_type, | |
5a9682d0 | 1299 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1300 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
26329584 | 1301 | { |
778e6af8 | 1302 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
26329584 | 1303 | buffer_size, buffer); |
1304 | ||
1305 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, | |
1306 | buffer_size, buffer)); | |
bee91169 AG |
1307 | } |
1308 | ||
d98cdf6a AG |
1309 | /* Collapses configuration table entries, removing index i */ |
1310 | static void efi_remove_configuration_table(int i) | |
1311 | { | |
1312 | struct efi_configuration_table *this = &efi_conf_table[i]; | |
1313 | struct efi_configuration_table *next = &efi_conf_table[i+1]; | |
1314 | struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables]; | |
1315 | ||
1316 | memmove(this, next, (ulong)end - (ulong)next); | |
1317 | systab.nr_tables--; | |
1318 | } | |
1319 | ||
332468f7 HS |
1320 | /* |
1321 | * Adds, updates, or removes a configuration table. | |
1322 | * | |
1323 | * This function is used for internal calls. For the API implementation of the | |
1324 | * InstallConfigurationTable service see efi_install_configuration_table_ext. | |
1325 | * | |
1326 | * @guid GUID of the installed table | |
1327 | * @table table to be installed | |
1328 | * @return status code | |
1329 | */ | |
488bf12d | 1330 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, void *table) |
bee91169 AG |
1331 | { |
1332 | int i; | |
1333 | ||
bee91169 AG |
1334 | /* Check for guid override */ |
1335 | for (i = 0; i < systab.nr_tables; i++) { | |
1336 | if (!guidcmp(guid, &efi_conf_table[i].guid)) { | |
d98cdf6a AG |
1337 | if (table) |
1338 | efi_conf_table[i].table = table; | |
1339 | else | |
1340 | efi_remove_configuration_table(i); | |
488bf12d | 1341 | return EFI_SUCCESS; |
bee91169 AG |
1342 | } |
1343 | } | |
1344 | ||
d98cdf6a AG |
1345 | if (!table) |
1346 | return EFI_NOT_FOUND; | |
1347 | ||
bee91169 AG |
1348 | /* No override, check for overflow */ |
1349 | if (i >= ARRAY_SIZE(efi_conf_table)) | |
488bf12d | 1350 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
1351 | |
1352 | /* Add a new entry */ | |
1353 | memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid)); | |
1354 | efi_conf_table[i].table = table; | |
aba5e919 | 1355 | systab.nr_tables = i + 1; |
bee91169 | 1356 | |
488bf12d AG |
1357 | return EFI_SUCCESS; |
1358 | } | |
1359 | ||
332468f7 HS |
1360 | /* |
1361 | * Adds, updates, or removes a configuration table. | |
1362 | * | |
1363 | * This function implements the InstallConfigurationTable service. | |
1364 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1365 | * for details. | |
1366 | * | |
1367 | * @guid GUID of the installed table | |
1368 | * @table table to be installed | |
1369 | * @return status code | |
1370 | */ | |
488bf12d AG |
1371 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
1372 | void *table) | |
1373 | { | |
778e6af8 | 1374 | EFI_ENTRY("%pUl, %p", guid, table); |
488bf12d | 1375 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
bee91169 AG |
1376 | } |
1377 | ||
332468f7 HS |
1378 | /* |
1379 | * Initialize a loaded_image_info + loaded_image_info object with correct | |
95c5553e | 1380 | * protocols, boot-device, etc. |
332468f7 | 1381 | * |
10a08c4e | 1382 | * @info loaded image info to be passed to the entry point of the |
332468f7 HS |
1383 | * image |
1384 | * @obj internal object associated with the loaded image | |
1385 | * @device_path device path of the loaded image | |
1386 | * @file_path file path of the loaded image | |
56d92888 | 1387 | * @return status code |
95c5553e | 1388 | */ |
56d92888 HS |
1389 | efi_status_t efi_setup_loaded_image( |
1390 | struct efi_loaded_image *info, struct efi_object *obj, | |
1391 | struct efi_device_path *device_path, | |
1392 | struct efi_device_path *file_path) | |
95c5553e | 1393 | { |
48b66230 HS |
1394 | efi_status_t ret; |
1395 | ||
44549d62 HS |
1396 | /* Add internal object to object list */ |
1397 | efi_add_handle(obj); | |
1398 | /* efi_exit() assumes that the handle points to the info */ | |
95c5553e RC |
1399 | obj->handle = info; |
1400 | ||
48b66230 | 1401 | info->file_path = file_path; |
48b66230 | 1402 | |
7df5af6f HS |
1403 | if (device_path) { |
1404 | info->device_handle = efi_dp_find_obj(device_path, NULL); | |
1405 | /* | |
1406 | * When asking for the device path interface, return | |
1407 | * bootefi_device_path | |
1408 | */ | |
1409 | ret = efi_add_protocol(obj->handle, &efi_guid_device_path, | |
1410 | device_path); | |
1411 | if (ret != EFI_SUCCESS) | |
1412 | goto failure; | |
1413 | } | |
95c5553e RC |
1414 | |
1415 | /* | |
1416 | * When asking for the loaded_image interface, just | |
1417 | * return handle which points to loaded_image_info | |
1418 | */ | |
48b66230 HS |
1419 | ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info); |
1420 | if (ret != EFI_SUCCESS) | |
1421 | goto failure; | |
95c5553e | 1422 | |
48b66230 HS |
1423 | ret = efi_add_protocol(obj->handle, &efi_guid_console_control, |
1424 | (void *)&efi_console_control); | |
1425 | if (ret != EFI_SUCCESS) | |
1426 | goto failure; | |
95c5553e | 1427 | |
48b66230 HS |
1428 | ret = efi_add_protocol(obj->handle, |
1429 | &efi_guid_device_path_to_text_protocol, | |
1430 | (void *)&efi_device_path_to_text); | |
1431 | if (ret != EFI_SUCCESS) | |
1432 | goto failure; | |
95c5553e | 1433 | |
56d92888 | 1434 | return ret; |
48b66230 HS |
1435 | failure: |
1436 | printf("ERROR: Failure to install protocols for loaded image\n"); | |
56d92888 | 1437 | return ret; |
95c5553e RC |
1438 | } |
1439 | ||
332468f7 HS |
1440 | /* |
1441 | * Load an image using a file path. | |
1442 | * | |
1443 | * @file_path the path of the image to load | |
1444 | * @buffer buffer containing the loaded image | |
10a08c4e | 1445 | * @return status code |
332468f7 | 1446 | */ |
9975fe96 RC |
1447 | efi_status_t efi_load_image_from_path(struct efi_device_path *file_path, |
1448 | void **buffer) | |
838ee4b4 RC |
1449 | { |
1450 | struct efi_file_info *info = NULL; | |
1451 | struct efi_file_handle *f; | |
1452 | static efi_status_t ret; | |
1453 | uint64_t bs; | |
1454 | ||
1455 | f = efi_file_from_path(file_path); | |
1456 | if (!f) | |
1457 | return EFI_DEVICE_ERROR; | |
1458 | ||
1459 | bs = 0; | |
1460 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, | |
1461 | &bs, info)); | |
1462 | if (ret == EFI_BUFFER_TOO_SMALL) { | |
1463 | info = malloc(bs); | |
1464 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, | |
1465 | &bs, info)); | |
1466 | } | |
1467 | if (ret != EFI_SUCCESS) | |
1468 | goto error; | |
1469 | ||
1470 | ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer); | |
1471 | if (ret) | |
1472 | goto error; | |
1473 | ||
1474 | EFI_CALL(ret = f->read(f, &info->file_size, *buffer)); | |
1475 | ||
1476 | error: | |
1477 | free(info); | |
1478 | EFI_CALL(f->close(f)); | |
1479 | ||
1480 | if (ret != EFI_SUCCESS) { | |
1481 | efi_free_pool(*buffer); | |
1482 | *buffer = NULL; | |
1483 | } | |
1484 | ||
1485 | return ret; | |
1486 | } | |
1487 | ||
332468f7 HS |
1488 | /* |
1489 | * Load an EFI image into memory. | |
1490 | * | |
1491 | * This function implements the LoadImage service. | |
1492 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1493 | * for details. | |
1494 | * | |
1495 | * @boot_policy true for request originating from the boot manager | |
c8df80c5 | 1496 | * @parent_image the caller's image handle |
332468f7 HS |
1497 | * @file_path the path of the image to load |
1498 | * @source_buffer memory location from which the image is installed | |
1499 | * @source_size size of the memory area from which the image is | |
1500 | * installed | |
1501 | * @image_handle handle for the newly installed image | |
1502 | * @return status code | |
1503 | */ | |
bee91169 AG |
1504 | static efi_status_t EFIAPI efi_load_image(bool boot_policy, |
1505 | efi_handle_t parent_image, | |
1506 | struct efi_device_path *file_path, | |
1507 | void *source_buffer, | |
1508 | unsigned long source_size, | |
1509 | efi_handle_t *image_handle) | |
1510 | { | |
bee91169 AG |
1511 | struct efi_loaded_image *info; |
1512 | struct efi_object *obj; | |
b9b17598 | 1513 | efi_status_t ret; |
bee91169 | 1514 | |
476ed96e | 1515 | EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image, |
bee91169 | 1516 | file_path, source_buffer, source_size, image_handle); |
838ee4b4 RC |
1517 | |
1518 | info = calloc(1, sizeof(*info)); | |
1519 | obj = calloc(1, sizeof(*obj)); | |
1520 | ||
1521 | if (!source_buffer) { | |
1522 | struct efi_device_path *dp, *fp; | |
838ee4b4 | 1523 | |
9975fe96 | 1524 | ret = efi_load_image_from_path(file_path, &source_buffer); |
b9b17598 HS |
1525 | if (ret != EFI_SUCCESS) |
1526 | goto failure; | |
838ee4b4 RC |
1527 | /* |
1528 | * split file_path which contains both the device and | |
1529 | * file parts: | |
1530 | */ | |
1531 | efi_dp_split_file_path(file_path, &dp, &fp); | |
b9b17598 HS |
1532 | ret = efi_setup_loaded_image(info, obj, dp, fp); |
1533 | if (ret != EFI_SUCCESS) | |
1534 | goto failure; | |
838ee4b4 RC |
1535 | } else { |
1536 | /* In this case, file_path is the "device" path, ie. | |
1537 | * something like a HARDWARE_DEVICE:MEMORY_MAPPED | |
1538 | */ | |
b9b17598 HS |
1539 | ret = efi_setup_loaded_image(info, obj, file_path, NULL); |
1540 | if (ret != EFI_SUCCESS) | |
1541 | goto failure; | |
838ee4b4 | 1542 | } |
bee91169 AG |
1543 | info->reserved = efi_load_pe(source_buffer, info); |
1544 | if (!info->reserved) { | |
b9b17598 HS |
1545 | ret = EFI_UNSUPPORTED; |
1546 | goto failure; | |
bee91169 | 1547 | } |
32fc2ac3 HS |
1548 | info->system_table = &systab; |
1549 | info->parent_handle = parent_image; | |
ea54ad59 | 1550 | *image_handle = obj->handle; |
bee91169 | 1551 | return EFI_EXIT(EFI_SUCCESS); |
b9b17598 HS |
1552 | failure: |
1553 | free(info); | |
1554 | efi_delete_handle(obj); | |
1555 | return EFI_EXIT(ret); | |
bee91169 AG |
1556 | } |
1557 | ||
332468f7 HS |
1558 | /* |
1559 | * Call the entry point of an image. | |
1560 | * | |
1561 | * This function implements the StartImage service. | |
1562 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1563 | * for details. | |
1564 | * | |
1565 | * @image_handle handle of the image | |
1566 | * @exit_data_size size of the buffer | |
1567 | * @exit_data buffer to receive the exit data of the called image | |
10a08c4e | 1568 | * @return status code |
332468f7 | 1569 | */ |
bee91169 AG |
1570 | static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, |
1571 | unsigned long *exit_data_size, | |
1572 | s16 **exit_data) | |
1573 | { | |
c6fa5df6 AG |
1574 | EFIAPI efi_status_t (*entry)(efi_handle_t image_handle, |
1575 | struct efi_system_table *st); | |
bee91169 | 1576 | struct efi_loaded_image *info = image_handle; |
727a1afb | 1577 | efi_status_t ret; |
bee91169 AG |
1578 | |
1579 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); | |
1580 | entry = info->reserved; | |
1581 | ||
1582 | efi_is_direct_boot = false; | |
1583 | ||
1584 | /* call the image! */ | |
a86aeaf2 | 1585 | if (setjmp(&info->exit_jmp)) { |
727a1afb HS |
1586 | /* |
1587 | * We called the entry point of the child image with EFI_CALL | |
1588 | * in the lines below. The child image called the Exit() boot | |
1589 | * service efi_exit() which executed the long jump that brought | |
1590 | * us to the current line. This implies that the second half | |
1591 | * of the EFI_CALL macro has not been executed. | |
1592 | */ | |
1593 | #ifdef CONFIG_ARM | |
1594 | /* | |
1595 | * efi_exit() called efi_restore_gd(). We have to undo this | |
1596 | * otherwise __efi_entry_check() will put the wrong value into | |
1597 | * app_gd. | |
1598 | */ | |
1599 | gd = app_gd; | |
1600 | #endif | |
1601 | /* | |
1602 | * To get ready to call EFI_EXIT below we have to execute the | |
1603 | * missed out steps of EFI_CALL. | |
1604 | */ | |
1605 | assert(__efi_entry_check()); | |
1606 | debug("%sEFI: %lu returned by started image\n", | |
1607 | __efi_nesting_dec(), | |
1608 | (unsigned long)((uintptr_t)info->exit_status & | |
1609 | ~EFI_ERROR_MASK)); | |
a86aeaf2 AG |
1610 | return EFI_EXIT(info->exit_status); |
1611 | } | |
1612 | ||
727a1afb | 1613 | ret = EFI_CALL(entry(image_handle, &systab)); |
bee91169 | 1614 | |
56672bf5 AG |
1615 | /* |
1616 | * Usually UEFI applications call Exit() instead of returning. | |
1617 | * But because the world doesn not consist of ponies and unicorns, | |
1618 | * we're happy to emulate that behavior on behalf of a payload | |
1619 | * that forgot. | |
1620 | */ | |
1621 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); | |
bee91169 AG |
1622 | } |
1623 | ||
332468f7 HS |
1624 | /* |
1625 | * Leave an EFI application or driver. | |
1626 | * | |
1627 | * This function implements the Exit service. | |
1628 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1629 | * for details. | |
1630 | * | |
1631 | * @image_handle handle of the application or driver that is exiting | |
1632 | * @exit_status status code | |
1633 | * @exit_data_size size of the buffer in bytes | |
1634 | * @exit_data buffer with data describing an error | |
10a08c4e | 1635 | * @return status code |
332468f7 | 1636 | */ |
a86aeaf2 AG |
1637 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
1638 | efi_status_t exit_status, unsigned long exit_data_size, | |
1639 | int16_t *exit_data) | |
bee91169 | 1640 | { |
44549d62 HS |
1641 | /* |
1642 | * We require that the handle points to the original loaded | |
1643 | * image protocol interface. | |
1644 | * | |
1645 | * For getting the longjmp address this is safer than locating | |
1646 | * the protocol because the protocol may have been reinstalled | |
1647 | * pointing to another memory location. | |
1648 | * | |
1649 | * TODO: We should call the unload procedure of the loaded | |
1650 | * image protocol. | |
1651 | */ | |
a86aeaf2 AG |
1652 | struct efi_loaded_image *loaded_image_info = (void*)image_handle; |
1653 | ||
bee91169 AG |
1654 | EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status, |
1655 | exit_data_size, exit_data); | |
a86aeaf2 | 1656 | |
a148920e | 1657 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
727a1afb | 1658 | EFI_EXIT(exit_status); |
da94073b | 1659 | |
a148920e AG |
1660 | /* |
1661 | * But longjmp out with the U-Boot gd, not the application's, as | |
1662 | * the other end is a setjmp call inside EFI context. | |
1663 | */ | |
1664 | efi_restore_gd(); | |
1665 | ||
a86aeaf2 | 1666 | loaded_image_info->exit_status = exit_status; |
692fcdd8 | 1667 | longjmp(&loaded_image_info->exit_jmp, 1); |
a86aeaf2 AG |
1668 | |
1669 | panic("EFI application exited"); | |
bee91169 AG |
1670 | } |
1671 | ||
332468f7 HS |
1672 | /* |
1673 | * Unload an EFI image. | |
1674 | * | |
1675 | * This function implements the UnloadImage service. | |
1676 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1677 | * for details. | |
1678 | * | |
1679 | * @image_handle handle of the image to be unloaded | |
1680 | * @return status code | |
1681 | */ | |
2074f700 | 1682 | static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) |
bee91169 AG |
1683 | { |
1684 | struct efi_object *efiobj; | |
1685 | ||
1686 | EFI_ENTRY("%p", image_handle); | |
1687 | efiobj = efi_search_obj(image_handle); | |
1688 | if (efiobj) | |
1689 | list_del(&efiobj->link); | |
1690 | ||
1691 | return EFI_EXIT(EFI_SUCCESS); | |
1692 | } | |
1693 | ||
332468f7 HS |
1694 | /* |
1695 | * Fix up caches for EFI payloads if necessary. | |
1696 | */ | |
bee91169 AG |
1697 | static void efi_exit_caches(void) |
1698 | { | |
1699 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) | |
1700 | /* | |
1701 | * Grub on 32bit ARM needs to have caches disabled before jumping into | |
1702 | * a zImage, but does not know of all cache layers. Give it a hand. | |
1703 | */ | |
1704 | if (efi_is_direct_boot) | |
1705 | cleanup_before_linux(); | |
1706 | #endif | |
1707 | } | |
1708 | ||
332468f7 | 1709 | /* |
cc20ed03 | 1710 | * Stop all boot services. |
332468f7 HS |
1711 | * |
1712 | * This function implements the ExitBootServices service. | |
1713 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1714 | * for details. | |
1715 | * | |
cc20ed03 HS |
1716 | * All timer events are disabled. |
1717 | * For exit boot services events the notification function is called. | |
1718 | * The boot services are disabled in the system table. | |
1719 | * | |
332468f7 HS |
1720 | * @image_handle handle of the loaded image |
1721 | * @map_key key of the memory map | |
1722 | * @return status code | |
1723 | */ | |
2074f700 | 1724 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
bee91169 AG |
1725 | unsigned long map_key) |
1726 | { | |
152a263c HS |
1727 | int i; |
1728 | ||
bee91169 AG |
1729 | EFI_ENTRY("%p, %ld", image_handle, map_key); |
1730 | ||
cc20ed03 HS |
1731 | /* Make sure that notification functions are not called anymore */ |
1732 | efi_tpl = TPL_HIGH_LEVEL; | |
1733 | ||
1734 | /* Check if ExitBootServices has already been called */ | |
1735 | if (!systab.boottime) | |
1736 | return EFI_EXIT(EFI_SUCCESS); | |
1737 | ||
152a263c HS |
1738 | /* Notify that ExitBootServices is invoked. */ |
1739 | for (i = 0; i < ARRAY_SIZE(efi_events); ++i) { | |
1740 | if (efi_events[i].type != EVT_SIGNAL_EXIT_BOOT_SERVICES) | |
1741 | continue; | |
cc20ed03 HS |
1742 | efi_events[i].is_signaled = true; |
1743 | efi_signal_event(&efi_events[i], false); | |
152a263c | 1744 | } |
152a263c | 1745 | |
cc20ed03 | 1746 | /* TODO Should persist EFI variables here */ |
ad644e7c | 1747 | |
b7b8410a AG |
1748 | board_quiesce_devices(); |
1749 | ||
bee91169 AG |
1750 | /* Fix up caches for EFI payloads if necessary */ |
1751 | efi_exit_caches(); | |
1752 | ||
1753 | /* This stops all lingering devices */ | |
1754 | bootm_disable_interrupts(); | |
1755 | ||
cc20ed03 HS |
1756 | /* Disable boottime services */ |
1757 | systab.con_in_handle = NULL; | |
1758 | systab.con_in = NULL; | |
1759 | systab.con_out_handle = NULL; | |
1760 | systab.con_out = NULL; | |
1761 | systab.stderr_handle = NULL; | |
1762 | systab.std_err = NULL; | |
1763 | systab.boottime = NULL; | |
1764 | ||
1765 | /* Recalculate CRC32 */ | |
1766 | systab.hdr.crc32 = 0; | |
1767 | systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab, | |
1768 | sizeof(struct efi_system_table)); | |
1769 | ||
bee91169 | 1770 | /* Give the payload some time to boot */ |
b3d60900 | 1771 | efi_set_watchdog(0); |
bee91169 AG |
1772 | WATCHDOG_RESET(); |
1773 | ||
1774 | return EFI_EXIT(EFI_SUCCESS); | |
1775 | } | |
1776 | ||
332468f7 HS |
1777 | /* |
1778 | * Get next value of the counter. | |
1779 | * | |
1780 | * This function implements the NextMonotonicCount service. | |
1781 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1782 | * for details. | |
1783 | * | |
1784 | * @count returned value of the counter | |
1785 | * @return status code | |
1786 | */ | |
bee91169 AG |
1787 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
1788 | { | |
1789 | static uint64_t mono = 0; | |
1790 | EFI_ENTRY("%p", count); | |
1791 | *count = mono++; | |
1792 | return EFI_EXIT(EFI_SUCCESS); | |
1793 | } | |
1794 | ||
332468f7 HS |
1795 | /* |
1796 | * Sleep. | |
1797 | * | |
1798 | * This function implements the Stall sercive. | |
1799 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1800 | * for details. | |
1801 | * | |
1802 | * @microseconds period to sleep in microseconds | |
1803 | * @return status code | |
1804 | */ | |
bee91169 AG |
1805 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
1806 | { | |
1807 | EFI_ENTRY("%ld", microseconds); | |
1808 | udelay(microseconds); | |
1809 | return EFI_EXIT(EFI_SUCCESS); | |
1810 | } | |
1811 | ||
332468f7 HS |
1812 | /* |
1813 | * Reset the watchdog timer. | |
1814 | * | |
b3d60900 | 1815 | * This function implements the SetWatchdogTimer service. |
332468f7 HS |
1816 | * See the Unified Extensible Firmware Interface (UEFI) specification |
1817 | * for details. | |
1818 | * | |
1819 | * @timeout seconds before reset by watchdog | |
1820 | * @watchdog_code code to be logged when resetting | |
1821 | * @data_size size of buffer in bytes | |
1822 | * @watchdog_data buffer with data describing the reset reason | |
10a08c4e | 1823 | * @return status code |
332468f7 | 1824 | */ |
bee91169 AG |
1825 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
1826 | uint64_t watchdog_code, | |
1827 | unsigned long data_size, | |
1828 | uint16_t *watchdog_data) | |
1829 | { | |
1830 | EFI_ENTRY("%ld, 0x%"PRIx64", %ld, %p", timeout, watchdog_code, | |
1831 | data_size, watchdog_data); | |
b3d60900 | 1832 | return EFI_EXIT(efi_set_watchdog(timeout)); |
bee91169 AG |
1833 | } |
1834 | ||
332468f7 HS |
1835 | /* |
1836 | * Close a protocol. | |
1837 | * | |
1838 | * This function implements the CloseProtocol service. | |
1839 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1840 | * for details. | |
1841 | * | |
1842 | * @handle handle on which the protocol shall be closed | |
1843 | * @protocol GUID of the protocol to close | |
1844 | * @agent_handle handle of the driver | |
1845 | * @controller_handle handle of the controller | |
1846 | * @return status code | |
1847 | */ | |
2074f700 | 1848 | static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
5a9682d0 | 1849 | const efi_guid_t *protocol, |
2074f700 HS |
1850 | efi_handle_t agent_handle, |
1851 | efi_handle_t controller_handle) | |
bee91169 | 1852 | { |
3b8a489c HS |
1853 | struct efi_handler *handler; |
1854 | struct efi_open_protocol_info_item *item; | |
1855 | struct efi_open_protocol_info_item *pos; | |
1856 | efi_status_t r; | |
1857 | ||
778e6af8 | 1858 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
bee91169 | 1859 | controller_handle); |
3b8a489c HS |
1860 | |
1861 | if (!agent_handle) { | |
1862 | r = EFI_INVALID_PARAMETER; | |
1863 | goto out; | |
1864 | } | |
1865 | r = efi_search_protocol(handle, protocol, &handler); | |
1866 | if (r != EFI_SUCCESS) | |
1867 | goto out; | |
1868 | ||
1869 | r = EFI_NOT_FOUND; | |
1870 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1871 | if (item->info.agent_handle == agent_handle && | |
1872 | item->info.controller_handle == controller_handle) { | |
1873 | efi_delete_open_info(item); | |
1874 | r = EFI_SUCCESS; | |
1875 | break; | |
1876 | } | |
1877 | } | |
1878 | out: | |
1879 | return EFI_EXIT(r); | |
bee91169 AG |
1880 | } |
1881 | ||
332468f7 HS |
1882 | /* |
1883 | * Provide information about then open status of a protocol on a handle | |
1884 | * | |
1885 | * This function implements the OpenProtocolInformation service. | |
1886 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1887 | * for details. | |
1888 | * | |
1889 | * @handle handle for which the information shall be retrieved | |
1890 | * @protocol GUID of the protocol | |
1891 | * @entry_buffer buffer to receive the open protocol information | |
1892 | * @entry_count number of entries available in the buffer | |
1893 | * @return status code | |
1894 | */ | |
bee91169 | 1895 | static efi_status_t EFIAPI efi_open_protocol_information(efi_handle_t handle, |
5a9682d0 | 1896 | const efi_guid_t *protocol, |
bee91169 | 1897 | struct efi_open_protocol_info_entry **entry_buffer, |
f5a2a938 | 1898 | efi_uintn_t *entry_count) |
bee91169 | 1899 | { |
e3fbbc36 HS |
1900 | unsigned long buffer_size; |
1901 | unsigned long count; | |
1902 | struct efi_handler *handler; | |
1903 | struct efi_open_protocol_info_item *item; | |
1904 | efi_status_t r; | |
1905 | ||
778e6af8 | 1906 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
bee91169 | 1907 | entry_count); |
e3fbbc36 HS |
1908 | |
1909 | /* Check parameters */ | |
1910 | if (!entry_buffer) { | |
1911 | r = EFI_INVALID_PARAMETER; | |
1912 | goto out; | |
1913 | } | |
1914 | r = efi_search_protocol(handle, protocol, &handler); | |
1915 | if (r != EFI_SUCCESS) | |
1916 | goto out; | |
1917 | ||
1918 | /* Count entries */ | |
1919 | count = 0; | |
1920 | list_for_each_entry(item, &handler->open_infos, link) { | |
1921 | if (item->info.open_count) | |
1922 | ++count; | |
1923 | } | |
1924 | *entry_count = count; | |
1925 | *entry_buffer = NULL; | |
1926 | if (!count) { | |
1927 | r = EFI_SUCCESS; | |
1928 | goto out; | |
1929 | } | |
1930 | ||
1931 | /* Copy entries */ | |
1932 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); | |
1933 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
1934 | (void **)entry_buffer); | |
1935 | if (r != EFI_SUCCESS) | |
1936 | goto out; | |
1937 | list_for_each_entry_reverse(item, &handler->open_infos, link) { | |
1938 | if (item->info.open_count) | |
1939 | (*entry_buffer)[--count] = item->info; | |
1940 | } | |
1941 | out: | |
1942 | return EFI_EXIT(r); | |
bee91169 AG |
1943 | } |
1944 | ||
332468f7 HS |
1945 | /* |
1946 | * Get protocols installed on a handle. | |
1947 | * | |
1948 | * This function implements the ProtocolsPerHandleService. | |
1949 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1950 | * for details. | |
1951 | * | |
1952 | * @handle handle for which the information is retrieved | |
1953 | * @protocol_buffer buffer with protocol GUIDs | |
1954 | * @protocol_buffer_count number of entries in the buffer | |
10a08c4e | 1955 | * @return status code |
332468f7 | 1956 | */ |
2074f700 HS |
1957 | static efi_status_t EFIAPI efi_protocols_per_handle( |
1958 | efi_handle_t handle, efi_guid_t ***protocol_buffer, | |
f5a2a938 | 1959 | efi_uintn_t *protocol_buffer_count) |
bee91169 | 1960 | { |
c0ebfc86 | 1961 | unsigned long buffer_size; |
1962 | struct efi_object *efiobj; | |
69fb6b1a | 1963 | struct list_head *protocol_handle; |
c0ebfc86 | 1964 | efi_status_t r; |
1965 | ||
bee91169 AG |
1966 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
1967 | protocol_buffer_count); | |
c0ebfc86 | 1968 | |
1969 | if (!handle || !protocol_buffer || !protocol_buffer_count) | |
1970 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
1971 | ||
1972 | *protocol_buffer = NULL; | |
661c8327 | 1973 | *protocol_buffer_count = 0; |
c0ebfc86 | 1974 | |
69fb6b1a HS |
1975 | efiobj = efi_search_obj(handle); |
1976 | if (!efiobj) | |
1977 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c0ebfc86 | 1978 | |
69fb6b1a HS |
1979 | /* Count protocols */ |
1980 | list_for_each(protocol_handle, &efiobj->protocols) { | |
1981 | ++*protocol_buffer_count; | |
1982 | } | |
1983 | ||
1984 | /* Copy guids */ | |
1985 | if (*protocol_buffer_count) { | |
1986 | size_t j = 0; | |
1987 | ||
1988 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; | |
1989 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
1990 | (void **)protocol_buffer); | |
1991 | if (r != EFI_SUCCESS) | |
1992 | return EFI_EXIT(r); | |
1993 | list_for_each(protocol_handle, &efiobj->protocols) { | |
1994 | struct efi_handler *protocol; | |
1995 | ||
1996 | protocol = list_entry(protocol_handle, | |
1997 | struct efi_handler, link); | |
1998 | (*protocol_buffer)[j] = (void *)protocol->guid; | |
1999 | ++j; | |
c0ebfc86 | 2000 | } |
c0ebfc86 | 2001 | } |
2002 | ||
2003 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2004 | } |
2005 | ||
332468f7 HS |
2006 | /* |
2007 | * Locate handles implementing a protocol. | |
2008 | * | |
2009 | * This function implements the LocateHandleBuffer service. | |
2010 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2011 | * for details. | |
2012 | * | |
2013 | * @search_type selection criterion | |
2014 | * @protocol GUID of the protocol | |
2015 | * @search_key registration key | |
2016 | * @no_handles number of returned handles | |
2017 | * @buffer buffer with the returned handles | |
2018 | * @return status code | |
2019 | */ | |
bee91169 AG |
2020 | static efi_status_t EFIAPI efi_locate_handle_buffer( |
2021 | enum efi_locate_search_type search_type, | |
5a9682d0 | 2022 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 2023 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
bee91169 | 2024 | { |
c2e703f9 | 2025 | efi_status_t r; |
f5a2a938 | 2026 | efi_uintn_t buffer_size = 0; |
c2e703f9 | 2027 | |
778e6af8 | 2028 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
bee91169 | 2029 | no_handles, buffer); |
c2e703f9 | 2030 | |
2031 | if (!no_handles || !buffer) { | |
2032 | r = EFI_INVALID_PARAMETER; | |
2033 | goto out; | |
2034 | } | |
2035 | *no_handles = 0; | |
2036 | *buffer = NULL; | |
2037 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2038 | *buffer); | |
2039 | if (r != EFI_BUFFER_TOO_SMALL) | |
2040 | goto out; | |
2041 | r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size, | |
2042 | (void **)buffer); | |
2043 | if (r != EFI_SUCCESS) | |
2044 | goto out; | |
2045 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2046 | *buffer); | |
2047 | if (r == EFI_SUCCESS) | |
2074f700 | 2048 | *no_handles = buffer_size / sizeof(efi_handle_t); |
c2e703f9 | 2049 | out: |
2050 | return EFI_EXIT(r); | |
bee91169 AG |
2051 | } |
2052 | ||
332468f7 HS |
2053 | /* |
2054 | * Find an interface implementing a protocol. | |
2055 | * | |
2056 | * This function implements the LocateProtocol service. | |
2057 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2058 | * for details. | |
2059 | * | |
2060 | * @protocol GUID of the protocol | |
2061 | * @registration registration key passed to the notification function | |
2062 | * @protocol_interface interface implementing the protocol | |
10a08c4e | 2063 | * @return status code |
332468f7 | 2064 | */ |
5a9682d0 | 2065 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
bee91169 AG |
2066 | void *registration, |
2067 | void **protocol_interface) | |
2068 | { | |
88adae5e | 2069 | struct list_head *lhandle; |
9172cd91 | 2070 | efi_status_t ret; |
bee91169 | 2071 | |
778e6af8 | 2072 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
88adae5e | 2073 | |
2074 | if (!protocol || !protocol_interface) | |
2075 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2076 | ||
2077 | list_for_each(lhandle, &efi_obj_list) { | |
2078 | struct efi_object *efiobj; | |
9172cd91 | 2079 | struct efi_handler *handler; |
88adae5e | 2080 | |
2081 | efiobj = list_entry(lhandle, struct efi_object, link); | |
9172cd91 HS |
2082 | |
2083 | ret = efi_search_protocol(efiobj->handle, protocol, &handler); | |
2084 | if (ret == EFI_SUCCESS) { | |
2085 | *protocol_interface = handler->protocol_interface; | |
2086 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2087 | } |
2088 | } | |
88adae5e | 2089 | *protocol_interface = NULL; |
bee91169 AG |
2090 | |
2091 | return EFI_EXIT(EFI_NOT_FOUND); | |
2092 | } | |
2093 | ||
ae2c85c1 HS |
2094 | /* |
2095 | * Get the device path and handle of an device implementing a protocol. | |
2096 | * | |
2097 | * This function implements the LocateDevicePath service. | |
2098 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2099 | * for details. | |
2100 | * | |
2101 | * @protocol GUID of the protocol | |
2102 | * @device_path device path | |
2103 | * @device handle of the device | |
2104 | * @return status code | |
2105 | */ | |
2106 | static efi_status_t EFIAPI efi_locate_device_path( | |
2107 | const efi_guid_t *protocol, | |
2108 | struct efi_device_path **device_path, | |
2109 | efi_handle_t *device) | |
2110 | { | |
2111 | struct efi_device_path *dp; | |
2112 | size_t i; | |
2113 | struct efi_handler *handler; | |
2114 | efi_handle_t *handles; | |
2115 | size_t len, len_dp; | |
2116 | size_t len_best = 0; | |
2117 | efi_uintn_t no_handles; | |
2118 | u8 *remainder; | |
2119 | efi_status_t ret; | |
2120 | ||
2121 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); | |
2122 | ||
2123 | if (!protocol || !device_path || !*device_path || !device) { | |
2124 | ret = EFI_INVALID_PARAMETER; | |
2125 | goto out; | |
2126 | } | |
2127 | ||
2128 | /* Find end of device path */ | |
2129 | len = efi_dp_size(*device_path); | |
2130 | ||
2131 | /* Get all handles implementing the protocol */ | |
2132 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, | |
2133 | &no_handles, &handles)); | |
2134 | if (ret != EFI_SUCCESS) | |
2135 | goto out; | |
2136 | ||
2137 | for (i = 0; i < no_handles; ++i) { | |
2138 | /* Find the device path protocol */ | |
2139 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, | |
2140 | &handler); | |
2141 | if (ret != EFI_SUCCESS) | |
2142 | continue; | |
2143 | dp = (struct efi_device_path *)handler->protocol_interface; | |
2144 | len_dp = efi_dp_size(dp); | |
2145 | /* | |
2146 | * This handle can only be a better fit | |
2147 | * if its device path length is longer than the best fit and | |
2148 | * if its device path length is shorter of equal the searched | |
2149 | * device path. | |
2150 | */ | |
2151 | if (len_dp <= len_best || len_dp > len) | |
2152 | continue; | |
2153 | /* Check if dp is a subpath of device_path */ | |
2154 | if (memcmp(*device_path, dp, len_dp)) | |
2155 | continue; | |
2156 | *device = handles[i]; | |
2157 | len_best = len_dp; | |
2158 | } | |
2159 | if (len_best) { | |
2160 | remainder = (u8 *)*device_path + len_best; | |
2161 | *device_path = (struct efi_device_path *)remainder; | |
2162 | ret = EFI_SUCCESS; | |
2163 | } else { | |
2164 | ret = EFI_NOT_FOUND; | |
2165 | } | |
2166 | out: | |
2167 | return EFI_EXIT(ret); | |
2168 | } | |
2169 | ||
332468f7 HS |
2170 | /* |
2171 | * Install multiple protocol interfaces. | |
2172 | * | |
2173 | * This function implements the MultipleProtocolInterfaces service. | |
2174 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2175 | * for details. | |
2176 | * | |
2177 | * @handle handle on which the protocol interfaces shall be installed | |
2178 | * @... NULL terminated argument list with pairs of protocol GUIDS and | |
2179 | * interfaces | |
2180 | * @return status code | |
2181 | */ | |
bee91169 AG |
2182 | static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces( |
2183 | void **handle, ...) | |
2184 | { | |
2185 | EFI_ENTRY("%p", handle); | |
58b83586 | 2186 | |
2187 | va_list argptr; | |
5a9682d0 | 2188 | const efi_guid_t *protocol; |
58b83586 | 2189 | void *protocol_interface; |
2190 | efi_status_t r = EFI_SUCCESS; | |
2191 | int i = 0; | |
2192 | ||
2193 | if (!handle) | |
2194 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2195 | ||
2196 | va_start(argptr, handle); | |
2197 | for (;;) { | |
2198 | protocol = va_arg(argptr, efi_guid_t*); | |
2199 | if (!protocol) | |
2200 | break; | |
2201 | protocol_interface = va_arg(argptr, void*); | |
1760ef57 HS |
2202 | r = EFI_CALL(efi_install_protocol_interface( |
2203 | handle, protocol, | |
2204 | EFI_NATIVE_INTERFACE, | |
2205 | protocol_interface)); | |
58b83586 | 2206 | if (r != EFI_SUCCESS) |
2207 | break; | |
2208 | i++; | |
2209 | } | |
2210 | va_end(argptr); | |
2211 | if (r == EFI_SUCCESS) | |
2212 | return EFI_EXIT(r); | |
2213 | ||
62471e46 | 2214 | /* If an error occurred undo all changes. */ |
58b83586 | 2215 | va_start(argptr, handle); |
2216 | for (; i; --i) { | |
2217 | protocol = va_arg(argptr, efi_guid_t*); | |
2218 | protocol_interface = va_arg(argptr, void*); | |
cd534083 HS |
2219 | EFI_CALL(efi_uninstall_protocol_interface(handle, protocol, |
2220 | protocol_interface)); | |
58b83586 | 2221 | } |
2222 | va_end(argptr); | |
2223 | ||
2224 | return EFI_EXIT(r); | |
bee91169 AG |
2225 | } |
2226 | ||
332468f7 HS |
2227 | /* |
2228 | * Uninstall multiple protocol interfaces. | |
2229 | * | |
2230 | * This function implements the UninstallMultipleProtocolInterfaces service. | |
2231 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2232 | * for details. | |
2233 | * | |
2234 | * @handle handle from which the protocol interfaces shall be removed | |
2235 | * @... NULL terminated argument list with pairs of protocol GUIDS and | |
2236 | * interfaces | |
2237 | * @return status code | |
2238 | */ | |
bee91169 AG |
2239 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
2240 | void *handle, ...) | |
2241 | { | |
2242 | EFI_ENTRY("%p", handle); | |
843ce54c HS |
2243 | |
2244 | va_list argptr; | |
2245 | const efi_guid_t *protocol; | |
2246 | void *protocol_interface; | |
2247 | efi_status_t r = EFI_SUCCESS; | |
2248 | size_t i = 0; | |
2249 | ||
2250 | if (!handle) | |
2251 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2252 | ||
2253 | va_start(argptr, handle); | |
2254 | for (;;) { | |
2255 | protocol = va_arg(argptr, efi_guid_t*); | |
2256 | if (!protocol) | |
2257 | break; | |
2258 | protocol_interface = va_arg(argptr, void*); | |
2259 | r = EFI_CALL(efi_uninstall_protocol_interface( | |
2260 | handle, protocol, | |
2261 | protocol_interface)); | |
2262 | if (r != EFI_SUCCESS) | |
2263 | break; | |
2264 | i++; | |
2265 | } | |
2266 | va_end(argptr); | |
2267 | if (r == EFI_SUCCESS) | |
2268 | return EFI_EXIT(r); | |
2269 | ||
2270 | /* If an error occurred undo all changes. */ | |
2271 | va_start(argptr, handle); | |
2272 | for (; i; --i) { | |
2273 | protocol = va_arg(argptr, efi_guid_t*); | |
2274 | protocol_interface = va_arg(argptr, void*); | |
2275 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, | |
2276 | EFI_NATIVE_INTERFACE, | |
2277 | protocol_interface)); | |
2278 | } | |
2279 | va_end(argptr); | |
2280 | ||
2281 | return EFI_EXIT(r); | |
bee91169 AG |
2282 | } |
2283 | ||
332468f7 HS |
2284 | /* |
2285 | * Calculate cyclic redundancy code. | |
2286 | * | |
2287 | * This function implements the CalculateCrc32 service. | |
2288 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2289 | * for details. | |
2290 | * | |
2291 | * @data buffer with data | |
2292 | * @data_size size of buffer in bytes | |
2293 | * @crc32_p cyclic redundancy code | |
2294 | * @return status code | |
2295 | */ | |
bee91169 AG |
2296 | static efi_status_t EFIAPI efi_calculate_crc32(void *data, |
2297 | unsigned long data_size, | |
2298 | uint32_t *crc32_p) | |
2299 | { | |
2300 | EFI_ENTRY("%p, %ld", data, data_size); | |
2301 | *crc32_p = crc32(0, data, data_size); | |
2302 | return EFI_EXIT(EFI_SUCCESS); | |
2303 | } | |
2304 | ||
332468f7 HS |
2305 | /* |
2306 | * Copy memory. | |
2307 | * | |
2308 | * This function implements the CopyMem service. | |
2309 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2310 | * for details. | |
2311 | * | |
2312 | * @destination destination of the copy operation | |
2313 | * @source source of the copy operation | |
2314 | * @length number of bytes to copy | |
2315 | */ | |
fc05a959 HS |
2316 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
2317 | size_t length) | |
bee91169 | 2318 | { |
fc05a959 | 2319 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
bee91169 | 2320 | memcpy(destination, source, length); |
f7c78176 | 2321 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2322 | } |
2323 | ||
332468f7 HS |
2324 | /* |
2325 | * Fill memory with a byte value. | |
2326 | * | |
2327 | * This function implements the SetMem service. | |
2328 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2329 | * for details. | |
2330 | * | |
2331 | * @buffer buffer to fill | |
2332 | * @size size of buffer in bytes | |
2333 | * @value byte to copy to the buffer | |
2334 | */ | |
fc05a959 | 2335 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
bee91169 | 2336 | { |
fc05a959 | 2337 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
bee91169 | 2338 | memset(buffer, value, size); |
f7c78176 | 2339 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2340 | } |
2341 | ||
191a41cc HS |
2342 | /* |
2343 | * Open protocol interface on a handle. | |
2344 | * | |
2345 | * @handler handler of a protocol | |
2346 | * @protocol_interface interface implementing the protocol | |
2347 | * @agent_handle handle of the driver | |
2348 | * @controller_handle handle of the controller | |
2349 | * @attributes attributes indicating how to open the protocol | |
2350 | * @return status code | |
2351 | */ | |
2352 | static efi_status_t efi_protocol_open( | |
2353 | struct efi_handler *handler, | |
2354 | void **protocol_interface, void *agent_handle, | |
2355 | void *controller_handle, uint32_t attributes) | |
2356 | { | |
2357 | struct efi_open_protocol_info_item *item; | |
2358 | struct efi_open_protocol_info_entry *match = NULL; | |
2359 | bool opened_by_driver = false; | |
2360 | bool opened_exclusive = false; | |
2361 | ||
2362 | /* If there is no agent, only return the interface */ | |
2363 | if (!agent_handle) | |
2364 | goto out; | |
2365 | ||
2366 | /* For TEST_PROTOCOL ignore interface attribute */ | |
2367 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2368 | *protocol_interface = NULL; | |
2369 | ||
2370 | /* | |
2371 | * Check if the protocol is already opened by a driver with the same | |
2372 | * attributes or opened exclusively | |
2373 | */ | |
2374 | list_for_each_entry(item, &handler->open_infos, link) { | |
2375 | if (item->info.agent_handle == agent_handle) { | |
2376 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && | |
2377 | (item->info.attributes == attributes)) | |
2378 | return EFI_ALREADY_STARTED; | |
2379 | } | |
2380 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) | |
2381 | opened_exclusive = true; | |
2382 | } | |
2383 | ||
2384 | /* Only one controller can open the protocol exclusively */ | |
2385 | if (opened_exclusive && attributes & | |
2386 | (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER)) | |
2387 | return EFI_ACCESS_DENIED; | |
2388 | ||
2389 | /* Prepare exclusive opening */ | |
2390 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { | |
2391 | /* Try to disconnect controllers */ | |
2392 | list_for_each_entry(item, &handler->open_infos, link) { | |
2393 | if (item->info.attributes == | |
2394 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2395 | EFI_CALL(efi_disconnect_controller( | |
2396 | item->info.controller_handle, | |
2397 | item->info.agent_handle, | |
2398 | NULL)); | |
2399 | } | |
2400 | opened_by_driver = false; | |
2401 | /* Check if all controllers are disconnected */ | |
2402 | list_for_each_entry(item, &handler->open_infos, link) { | |
2403 | if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2404 | opened_by_driver = true; | |
2405 | } | |
2406 | /* Only one controller can be conncected */ | |
2407 | if (opened_by_driver) | |
2408 | return EFI_ACCESS_DENIED; | |
2409 | } | |
2410 | ||
2411 | /* Find existing entry */ | |
2412 | list_for_each_entry(item, &handler->open_infos, link) { | |
2413 | if (item->info.agent_handle == agent_handle && | |
2414 | item->info.controller_handle == controller_handle) | |
2415 | match = &item->info; | |
2416 | } | |
2417 | /* None found, create one */ | |
2418 | if (!match) { | |
2419 | match = efi_create_open_info(handler); | |
2420 | if (!match) | |
2421 | return EFI_OUT_OF_RESOURCES; | |
2422 | } | |
2423 | ||
2424 | match->agent_handle = agent_handle; | |
2425 | match->controller_handle = controller_handle; | |
2426 | match->attributes = attributes; | |
2427 | match->open_count++; | |
2428 | ||
2429 | out: | |
2430 | /* For TEST_PROTOCOL ignore interface attribute. */ | |
2431 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2432 | *protocol_interface = handler->protocol_interface; | |
2433 | ||
2434 | return EFI_SUCCESS; | |
2435 | } | |
2436 | ||
332468f7 HS |
2437 | /* |
2438 | * Open protocol interface on a handle. | |
2439 | * | |
2440 | * This function implements the OpenProtocol interface. | |
2441 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2442 | * for details. | |
2443 | * | |
2444 | * @handle handle on which the protocol shall be opened | |
2445 | * @protocol GUID of the protocol | |
2446 | * @protocol_interface interface implementing the protocol | |
2447 | * @agent_handle handle of the driver | |
2448 | * @controller_handle handle of the controller | |
2449 | * @attributes attributes indicating how to open the protocol | |
2450 | * @return status code | |
2451 | */ | |
bee91169 | 2452 | static efi_status_t EFIAPI efi_open_protocol( |
5a9682d0 | 2453 | void *handle, const efi_guid_t *protocol, |
bee91169 AG |
2454 | void **protocol_interface, void *agent_handle, |
2455 | void *controller_handle, uint32_t attributes) | |
2456 | { | |
80286e8f | 2457 | struct efi_handler *handler; |
69baec67 | 2458 | efi_status_t r = EFI_INVALID_PARAMETER; |
bee91169 | 2459 | |
778e6af8 | 2460 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
bee91169 AG |
2461 | protocol_interface, agent_handle, controller_handle, |
2462 | attributes); | |
b5349f74 | 2463 | |
69baec67 | 2464 | if (!handle || !protocol || |
2465 | (!protocol_interface && attributes != | |
2466 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { | |
2467 | goto out; | |
2468 | } | |
2469 | ||
2470 | switch (attributes) { | |
2471 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: | |
2472 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: | |
2473 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: | |
2474 | break; | |
2475 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: | |
2476 | if (controller_handle == handle) | |
2477 | goto out; | |
191a41cc | 2478 | /* fall-through */ |
69baec67 | 2479 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
2480 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
191a41cc HS |
2481 | /* Check that the controller handle is valid */ |
2482 | if (!efi_search_obj(controller_handle)) | |
69baec67 | 2483 | goto out; |
191a41cc | 2484 | /* fall-through */ |
69baec67 | 2485 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
191a41cc HS |
2486 | /* Check that the agent handle is valid */ |
2487 | if (!efi_search_obj(agent_handle)) | |
69baec67 | 2488 | goto out; |
2489 | break; | |
2490 | default: | |
b5349f74 | 2491 | goto out; |
2492 | } | |
2493 | ||
80286e8f HS |
2494 | r = efi_search_protocol(handle, protocol, &handler); |
2495 | if (r != EFI_SUCCESS) | |
2496 | goto out; | |
bee91169 | 2497 | |
191a41cc HS |
2498 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
2499 | controller_handle, attributes); | |
bee91169 AG |
2500 | out: |
2501 | return EFI_EXIT(r); | |
2502 | } | |
2503 | ||
332468f7 HS |
2504 | /* |
2505 | * Get interface of a protocol on a handle. | |
2506 | * | |
2507 | * This function implements the HandleProtocol service. | |
2508 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2509 | * for details. | |
2510 | * | |
2511 | * @handle handle on which the protocol shall be opened | |
2512 | * @protocol GUID of the protocol | |
2513 | * @protocol_interface interface implementing the protocol | |
2514 | * @return status code | |
2515 | */ | |
2074f700 | 2516 | static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
5a9682d0 | 2517 | const efi_guid_t *protocol, |
bee91169 AG |
2518 | void **protocol_interface) |
2519 | { | |
8e1d329f | 2520 | return efi_open_protocol(handle, protocol, protocol_interface, NULL, |
2521 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); | |
bee91169 AG |
2522 | } |
2523 | ||
f0959dbe HS |
2524 | static efi_status_t efi_bind_controller( |
2525 | efi_handle_t controller_handle, | |
2526 | efi_handle_t driver_image_handle, | |
2527 | struct efi_device_path *remain_device_path) | |
2528 | { | |
2529 | struct efi_driver_binding_protocol *binding_protocol; | |
2530 | efi_status_t r; | |
2531 | ||
2532 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
2533 | &efi_guid_driver_binding_protocol, | |
2534 | (void **)&binding_protocol, | |
2535 | driver_image_handle, NULL, | |
2536 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2537 | if (r != EFI_SUCCESS) | |
2538 | return r; | |
2539 | r = EFI_CALL(binding_protocol->supported(binding_protocol, | |
2540 | controller_handle, | |
2541 | remain_device_path)); | |
2542 | if (r == EFI_SUCCESS) | |
2543 | r = EFI_CALL(binding_protocol->start(binding_protocol, | |
2544 | controller_handle, | |
2545 | remain_device_path)); | |
2546 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
2547 | &efi_guid_driver_binding_protocol, | |
2548 | driver_image_handle, NULL)); | |
2549 | return r; | |
2550 | } | |
2551 | ||
2552 | static efi_status_t efi_connect_single_controller( | |
2553 | efi_handle_t controller_handle, | |
2554 | efi_handle_t *driver_image_handle, | |
2555 | struct efi_device_path *remain_device_path) | |
2556 | { | |
2557 | efi_handle_t *buffer; | |
2558 | size_t count; | |
2559 | size_t i; | |
2560 | efi_status_t r; | |
2561 | size_t connected = 0; | |
2562 | ||
2563 | /* Get buffer with all handles with driver binding protocol */ | |
2564 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, | |
2565 | &efi_guid_driver_binding_protocol, | |
2566 | NULL, &count, &buffer)); | |
2567 | if (r != EFI_SUCCESS) | |
2568 | return r; | |
2569 | ||
2570 | /* Context Override */ | |
2571 | if (driver_image_handle) { | |
2572 | for (; *driver_image_handle; ++driver_image_handle) { | |
2573 | for (i = 0; i < count; ++i) { | |
2574 | if (buffer[i] == *driver_image_handle) { | |
2575 | buffer[i] = NULL; | |
2576 | r = efi_bind_controller( | |
2577 | controller_handle, | |
2578 | *driver_image_handle, | |
2579 | remain_device_path); | |
2580 | /* | |
2581 | * For drivers that do not support the | |
2582 | * controller or are already connected | |
2583 | * we receive an error code here. | |
2584 | */ | |
2585 | if (r == EFI_SUCCESS) | |
2586 | ++connected; | |
2587 | } | |
2588 | } | |
2589 | } | |
2590 | } | |
2591 | ||
2592 | /* | |
2593 | * TODO: Some overrides are not yet implemented: | |
2594 | * - Platform Driver Override | |
2595 | * - Driver Family Override Search | |
2596 | * - Bus Specific Driver Override | |
2597 | */ | |
2598 | ||
2599 | /* Driver Binding Search */ | |
2600 | for (i = 0; i < count; ++i) { | |
2601 | if (buffer[i]) { | |
2602 | r = efi_bind_controller(controller_handle, | |
2603 | buffer[i], | |
2604 | remain_device_path); | |
2605 | if (r == EFI_SUCCESS) | |
2606 | ++connected; | |
2607 | } | |
2608 | } | |
2609 | ||
2610 | efi_free_pool(buffer); | |
2611 | if (!connected) | |
2612 | return EFI_NOT_FOUND; | |
2613 | return EFI_SUCCESS; | |
2614 | } | |
2615 | ||
2616 | /* | |
2617 | * Connect a controller to a driver. | |
2618 | * | |
2619 | * This function implements the ConnectController service. | |
2620 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2621 | * for details. | |
2622 | * | |
2623 | * First all driver binding protocol handles are tried for binding drivers. | |
2624 | * Afterwards all handles that have openened a protocol of the controller | |
2625 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. | |
2626 | * | |
2627 | * @controller_handle handle of the controller | |
2628 | * @driver_image_handle handle of the driver | |
2629 | * @remain_device_path device path of a child controller | |
2630 | * @recursive true to connect all child controllers | |
2631 | * @return status code | |
2632 | */ | |
2633 | static efi_status_t EFIAPI efi_connect_controller( | |
2634 | efi_handle_t controller_handle, | |
2635 | efi_handle_t *driver_image_handle, | |
2636 | struct efi_device_path *remain_device_path, | |
2637 | bool recursive) | |
2638 | { | |
2639 | efi_status_t r; | |
2640 | efi_status_t ret = EFI_NOT_FOUND; | |
2641 | struct efi_object *efiobj; | |
2642 | ||
2643 | EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle, | |
2644 | remain_device_path, recursive); | |
2645 | ||
2646 | efiobj = efi_search_obj(controller_handle); | |
2647 | if (!efiobj) { | |
2648 | ret = EFI_INVALID_PARAMETER; | |
2649 | goto out; | |
2650 | } | |
2651 | ||
2652 | r = efi_connect_single_controller(controller_handle, | |
2653 | driver_image_handle, | |
2654 | remain_device_path); | |
2655 | if (r == EFI_SUCCESS) | |
2656 | ret = EFI_SUCCESS; | |
2657 | if (recursive) { | |
2658 | struct efi_handler *handler; | |
2659 | struct efi_open_protocol_info_item *item; | |
2660 | ||
2661 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2662 | list_for_each_entry(item, &handler->open_infos, link) { | |
2663 | if (item->info.attributes & | |
2664 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
2665 | r = EFI_CALL(efi_connect_controller( | |
2666 | item->info.controller_handle, | |
2667 | driver_image_handle, | |
2668 | remain_device_path, | |
2669 | recursive)); | |
2670 | if (r == EFI_SUCCESS) | |
2671 | ret = EFI_SUCCESS; | |
2672 | } | |
2673 | } | |
2674 | } | |
2675 | } | |
2676 | /* Check for child controller specified by end node */ | |
2677 | if (ret != EFI_SUCCESS && remain_device_path && | |
2678 | remain_device_path->type == DEVICE_PATH_TYPE_END) | |
2679 | ret = EFI_SUCCESS; | |
2680 | out: | |
2681 | return EFI_EXIT(ret); | |
2682 | } | |
2683 | ||
3f9b0042 HS |
2684 | /* |
2685 | * Get all child controllers associated to a driver. | |
2686 | * The allocated buffer has to be freed with free(). | |
2687 | * | |
2688 | * @efiobj handle of the controller | |
2689 | * @driver_handle handle of the driver | |
2690 | * @number_of_children number of child controllers | |
2691 | * @child_handle_buffer handles of the the child controllers | |
2692 | */ | |
2693 | static efi_status_t efi_get_child_controllers( | |
2694 | struct efi_object *efiobj, | |
2695 | efi_handle_t driver_handle, | |
2696 | efi_uintn_t *number_of_children, | |
2697 | efi_handle_t **child_handle_buffer) | |
2698 | { | |
2699 | struct efi_handler *handler; | |
2700 | struct efi_open_protocol_info_item *item; | |
2701 | efi_uintn_t count = 0, i; | |
2702 | bool duplicate; | |
2703 | ||
2704 | /* Count all child controller associations */ | |
2705 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2706 | list_for_each_entry(item, &handler->open_infos, link) { | |
2707 | if (item->info.agent_handle == driver_handle && | |
2708 | item->info.attributes & | |
2709 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) | |
2710 | ++count; | |
2711 | } | |
2712 | } | |
2713 | /* | |
2714 | * Create buffer. In case of duplicate child controller assignments | |
2715 | * the buffer will be too large. But that does not harm. | |
2716 | */ | |
2717 | *number_of_children = 0; | |
2718 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); | |
2719 | if (!*child_handle_buffer) | |
2720 | return EFI_OUT_OF_RESOURCES; | |
2721 | /* Copy unique child handles */ | |
2722 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
2723 | list_for_each_entry(item, &handler->open_infos, link) { | |
2724 | if (item->info.agent_handle == driver_handle && | |
2725 | item->info.attributes & | |
2726 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
2727 | /* Check this is a new child controller */ | |
2728 | duplicate = false; | |
2729 | for (i = 0; i < *number_of_children; ++i) { | |
2730 | if ((*child_handle_buffer)[i] == | |
2731 | item->info.controller_handle) | |
2732 | duplicate = true; | |
2733 | } | |
2734 | /* Copy handle to buffer */ | |
2735 | if (!duplicate) { | |
2736 | i = (*number_of_children)++; | |
2737 | (*child_handle_buffer)[i] = | |
2738 | item->info.controller_handle; | |
2739 | } | |
2740 | } | |
2741 | } | |
2742 | } | |
2743 | return EFI_SUCCESS; | |
2744 | } | |
2745 | ||
2746 | /* | |
2747 | * Disconnect a controller from a driver. | |
2748 | * | |
2749 | * This function implements the DisconnectController service. | |
2750 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
2751 | * for details. | |
2752 | * | |
2753 | * @controller_handle handle of the controller | |
2754 | * @driver_image_handle handle of the driver | |
2755 | * @child_handle handle of the child to destroy | |
2756 | * @return status code | |
2757 | */ | |
2758 | static efi_status_t EFIAPI efi_disconnect_controller( | |
2759 | efi_handle_t controller_handle, | |
2760 | efi_handle_t driver_image_handle, | |
2761 | efi_handle_t child_handle) | |
2762 | { | |
2763 | struct efi_driver_binding_protocol *binding_protocol; | |
2764 | efi_handle_t *child_handle_buffer = NULL; | |
2765 | size_t number_of_children = 0; | |
2766 | efi_status_t r; | |
2767 | size_t stop_count = 0; | |
2768 | struct efi_object *efiobj; | |
2769 | ||
2770 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, | |
2771 | child_handle); | |
2772 | ||
2773 | efiobj = efi_search_obj(controller_handle); | |
2774 | if (!efiobj) { | |
2775 | r = EFI_INVALID_PARAMETER; | |
2776 | goto out; | |
2777 | } | |
2778 | ||
2779 | if (child_handle && !efi_search_obj(child_handle)) { | |
2780 | r = EFI_INVALID_PARAMETER; | |
2781 | goto out; | |
2782 | } | |
2783 | ||
2784 | /* If no driver handle is supplied, disconnect all drivers */ | |
2785 | if (!driver_image_handle) { | |
2786 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); | |
2787 | goto out; | |
2788 | } | |
2789 | ||
2790 | /* Create list of child handles */ | |
2791 | if (child_handle) { | |
2792 | number_of_children = 1; | |
2793 | child_handle_buffer = &child_handle; | |
2794 | } else { | |
2795 | efi_get_child_controllers(efiobj, | |
2796 | driver_image_handle, | |
2797 | &number_of_children, | |
2798 | &child_handle_buffer); | |
2799 | } | |
2800 | ||
2801 | /* Get the driver binding protocol */ | |
2802 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
2803 | &efi_guid_driver_binding_protocol, | |
2804 | (void **)&binding_protocol, | |
2805 | driver_image_handle, NULL, | |
2806 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2807 | if (r != EFI_SUCCESS) | |
2808 | goto out; | |
2809 | /* Remove the children */ | |
2810 | if (number_of_children) { | |
2811 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
2812 | controller_handle, | |
2813 | number_of_children, | |
2814 | child_handle_buffer)); | |
2815 | if (r == EFI_SUCCESS) | |
2816 | ++stop_count; | |
2817 | } | |
2818 | /* Remove the driver */ | |
2819 | if (!child_handle) | |
2820 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
2821 | controller_handle, | |
2822 | 0, NULL)); | |
2823 | if (r == EFI_SUCCESS) | |
2824 | ++stop_count; | |
2825 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
2826 | &efi_guid_driver_binding_protocol, | |
2827 | driver_image_handle, NULL)); | |
2828 | ||
2829 | if (stop_count) | |
2830 | r = EFI_SUCCESS; | |
2831 | else | |
2832 | r = EFI_NOT_FOUND; | |
2833 | out: | |
2834 | if (!child_handle) | |
2835 | free(child_handle_buffer); | |
2836 | return EFI_EXIT(r); | |
2837 | } | |
2838 | ||
bee91169 AG |
2839 | static const struct efi_boot_services efi_boot_services = { |
2840 | .hdr = { | |
2841 | .headersize = sizeof(struct efi_table_hdr), | |
2842 | }, | |
2843 | .raise_tpl = efi_raise_tpl, | |
2844 | .restore_tpl = efi_restore_tpl, | |
2845 | .allocate_pages = efi_allocate_pages_ext, | |
2846 | .free_pages = efi_free_pages_ext, | |
2847 | .get_memory_map = efi_get_memory_map_ext, | |
ead1274b | 2848 | .allocate_pool = efi_allocate_pool_ext, |
42417bc8 | 2849 | .free_pool = efi_free_pool_ext, |
49deb455 | 2850 | .create_event = efi_create_event_ext, |
bfc72462 | 2851 | .set_timer = efi_set_timer_ext, |
bee91169 | 2852 | .wait_for_event = efi_wait_for_event, |
c6841592 | 2853 | .signal_event = efi_signal_event_ext, |
bee91169 AG |
2854 | .close_event = efi_close_event, |
2855 | .check_event = efi_check_event, | |
1760ef57 | 2856 | .install_protocol_interface = efi_install_protocol_interface, |
bee91169 | 2857 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
cd534083 | 2858 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
bee91169 AG |
2859 | .handle_protocol = efi_handle_protocol, |
2860 | .reserved = NULL, | |
2861 | .register_protocol_notify = efi_register_protocol_notify, | |
26329584 | 2862 | .locate_handle = efi_locate_handle_ext, |
bee91169 | 2863 | .locate_device_path = efi_locate_device_path, |
488bf12d | 2864 | .install_configuration_table = efi_install_configuration_table_ext, |
bee91169 AG |
2865 | .load_image = efi_load_image, |
2866 | .start_image = efi_start_image, | |
a86aeaf2 | 2867 | .exit = efi_exit, |
bee91169 AG |
2868 | .unload_image = efi_unload_image, |
2869 | .exit_boot_services = efi_exit_boot_services, | |
2870 | .get_next_monotonic_count = efi_get_next_monotonic_count, | |
2871 | .stall = efi_stall, | |
2872 | .set_watchdog_timer = efi_set_watchdog_timer, | |
2873 | .connect_controller = efi_connect_controller, | |
2874 | .disconnect_controller = efi_disconnect_controller, | |
2875 | .open_protocol = efi_open_protocol, | |
2876 | .close_protocol = efi_close_protocol, | |
2877 | .open_protocol_information = efi_open_protocol_information, | |
2878 | .protocols_per_handle = efi_protocols_per_handle, | |
2879 | .locate_handle_buffer = efi_locate_handle_buffer, | |
2880 | .locate_protocol = efi_locate_protocol, | |
2881 | .install_multiple_protocol_interfaces = efi_install_multiple_protocol_interfaces, | |
2882 | .uninstall_multiple_protocol_interfaces = efi_uninstall_multiple_protocol_interfaces, | |
2883 | .calculate_crc32 = efi_calculate_crc32, | |
2884 | .copy_mem = efi_copy_mem, | |
2885 | .set_mem = efi_set_mem, | |
9f0930e5 | 2886 | .create_event_ex = efi_create_event_ex, |
bee91169 AG |
2887 | }; |
2888 | ||
2889 | ||
05b6f56e | 2890 | static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
bee91169 | 2891 | |
3c63db9c | 2892 | struct efi_system_table __efi_runtime_data systab = { |
bee91169 AG |
2893 | .hdr = { |
2894 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, | |
f19a95a4 | 2895 | .revision = 2 << 16 | 70, /* 2.7 */ |
bee91169 AG |
2896 | .headersize = sizeof(struct efi_table_hdr), |
2897 | }, | |
2898 | .fw_vendor = (long)firmware_vendor, | |
2899 | .con_in = (void*)&efi_con_in, | |
2900 | .con_out = (void*)&efi_con_out, | |
2901 | .std_err = (void*)&efi_con_out, | |
2902 | .runtime = (void*)&efi_runtime_services, | |
2903 | .boottime = (void*)&efi_boot_services, | |
2904 | .nr_tables = 0, | |
2905 | .tables = (void*)efi_conf_table, | |
2906 | }; |