]>
Commit | Line | Data |
---|---|---|
f739fcd8 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
bee91169 | 2 | /* |
359a699a | 3 | * EFI application boot time services |
bee91169 | 4 | * |
359a699a | 5 | * Copyright (c) 2016 Alexander Graf |
bee91169 AG |
6 | */ |
7 | ||
bee91169 | 8 | #include <common.h> |
7d963323 | 9 | #include <div64.h> |
bee91169 | 10 | #include <efi_loader.h> |
ad644e7c | 11 | #include <environment.h> |
bee91169 | 12 | #include <malloc.h> |
b08c8c48 | 13 | #include <linux/libfdt_env.h> |
bee91169 AG |
14 | #include <u-boot/crc.h> |
15 | #include <bootm.h> | |
126a43f1 | 16 | #include <pe.h> |
bee91169 AG |
17 | #include <watchdog.h> |
18 | ||
19 | DECLARE_GLOBAL_DATA_PTR; | |
20 | ||
32f4b2f8 | 21 | /* Task priority level */ |
152cade3 | 22 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
32f4b2f8 | 23 | |
bee91169 AG |
24 | /* This list contains all the EFI objects our payload has access to */ |
25 | LIST_HEAD(efi_obj_list); | |
26 | ||
43bce442 | 27 | /* List of all events */ |
b095f3c8 | 28 | LIST_HEAD(efi_events); |
43bce442 | 29 | |
7a69e97b HS |
30 | /* List of queued events */ |
31 | LIST_HEAD(efi_event_queue); | |
32 | ||
7eaa900e HS |
33 | /* Flag to disable timer activity in ExitBootServices() */ |
34 | static bool timers_enabled = true; | |
35 | ||
ab15d414 HS |
36 | /* List of all events registered by RegisterProtocolNotify() */ |
37 | LIST_HEAD(efi_register_notify_events); | |
38 | ||
bb31c3f7 HS |
39 | /* Handle of the currently executing image */ |
40 | static efi_handle_t current_image; | |
41 | ||
f31239ac AG |
42 | /* |
43 | * If we're running on nasty systems (32bit ARM booting into non-EFI Linux) | |
44 | * we need to do trickery with caches. Since we don't want to break the EFI | |
45 | * aware boot path, only apply hacks when loading exiting directly (breaking | |
46 | * direct Linux EFI booting along the way - oh well). | |
47 | */ | |
48 | static bool efi_is_direct_boot = true; | |
49 | ||
65e4c0b1 | 50 | #ifdef CONFIG_ARM |
bee91169 AG |
51 | /* |
52 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare | |
53 | * fixed when compiling U-Boot. However, the payload does not know about that | |
54 | * restriction so we need to manually swap its and our view of that register on | |
55 | * EFI callback entry/exit. | |
56 | */ | |
57 | static volatile void *efi_gd, *app_gd; | |
65e4c0b1 | 58 | #endif |
bee91169 | 59 | |
914df75b HS |
60 | /* 1 if inside U-Boot code, 0 if inside EFI payload code */ |
61 | static int entry_count = 1; | |
af65db85 | 62 | static int nesting_level; |
bc4f9133 HS |
63 | /* GUID of the device tree table */ |
64 | const efi_guid_t efi_guid_fdt = EFI_FDT_GUID; | |
f0959dbe HS |
65 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
66 | const efi_guid_t efi_guid_driver_binding_protocol = | |
67 | EFI_DRIVER_BINDING_PROTOCOL_GUID; | |
c160d2f5 | 68 | |
a3a28f5f HS |
69 | /* event group ExitBootServices() invoked */ |
70 | const efi_guid_t efi_guid_event_group_exit_boot_services = | |
71 | EFI_EVENT_GROUP_EXIT_BOOT_SERVICES; | |
72 | /* event group SetVirtualAddressMap() invoked */ | |
73 | const efi_guid_t efi_guid_event_group_virtual_address_change = | |
74 | EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE; | |
75 | /* event group memory map changed */ | |
76 | const efi_guid_t efi_guid_event_group_memory_map_change = | |
77 | EFI_EVENT_GROUP_MEMORY_MAP_CHANGE; | |
78 | /* event group boot manager about to boot */ | |
79 | const efi_guid_t efi_guid_event_group_ready_to_boot = | |
80 | EFI_EVENT_GROUP_READY_TO_BOOT; | |
81 | /* event group ResetSystem() invoked (before ExitBootServices) */ | |
82 | const efi_guid_t efi_guid_event_group_reset_system = | |
83 | EFI_EVENT_GROUP_RESET_SYSTEM; | |
84 | ||
2074f700 HS |
85 | static efi_status_t EFIAPI efi_disconnect_controller( |
86 | efi_handle_t controller_handle, | |
87 | efi_handle_t driver_image_handle, | |
88 | efi_handle_t child_handle); | |
3f9b0042 | 89 | |
c160d2f5 RC |
90 | /* Called on every callback entry */ |
91 | int __efi_entry_check(void) | |
92 | { | |
93 | int ret = entry_count++ == 0; | |
94 | #ifdef CONFIG_ARM | |
95 | assert(efi_gd); | |
96 | app_gd = gd; | |
97 | gd = efi_gd; | |
98 | #endif | |
99 | return ret; | |
100 | } | |
101 | ||
102 | /* Called on every callback exit */ | |
103 | int __efi_exit_check(void) | |
104 | { | |
105 | int ret = --entry_count == 0; | |
106 | #ifdef CONFIG_ARM | |
107 | gd = app_gd; | |
108 | #endif | |
109 | return ret; | |
110 | } | |
111 | ||
bee91169 AG |
112 | /* Called from do_bootefi_exec() */ |
113 | void efi_save_gd(void) | |
114 | { | |
65e4c0b1 | 115 | #ifdef CONFIG_ARM |
bee91169 | 116 | efi_gd = gd; |
65e4c0b1 | 117 | #endif |
bee91169 AG |
118 | } |
119 | ||
c160d2f5 | 120 | /* |
78a88f79 | 121 | * Special case handler for error/abort that just forces things back to u-boot |
b72aaa87 HS |
122 | * world so we can dump out an abort message, without any care about returning |
123 | * back to UEFI world. | |
c160d2f5 | 124 | */ |
bee91169 AG |
125 | void efi_restore_gd(void) |
126 | { | |
65e4c0b1 | 127 | #ifdef CONFIG_ARM |
bee91169 AG |
128 | /* Only restore if we're already in EFI context */ |
129 | if (!efi_gd) | |
130 | return; | |
bee91169 | 131 | gd = efi_gd; |
65e4c0b1 | 132 | #endif |
bee91169 AG |
133 | } |
134 | ||
6b03cd10 | 135 | /** |
78a88f79 MS |
136 | * indent_string() - returns a string for indenting with two spaces per level |
137 | * @level: indent level | |
6b03cd10 HS |
138 | * |
139 | * A maximum of ten indent levels is supported. Higher indent levels will be | |
140 | * truncated. | |
c8df80c5 | 141 | * |
78a88f79 MS |
142 | * Return: A string for indenting with two spaces per level is |
143 | * returned. | |
af65db85 RC |
144 | */ |
145 | static const char *indent_string(int level) | |
146 | { | |
147 | const char *indent = " "; | |
148 | const int max = strlen(indent); | |
ab9efa97 | 149 | |
af65db85 RC |
150 | level = min(max, level * 2); |
151 | return &indent[max - level]; | |
152 | } | |
153 | ||
ae0bd3a9 HS |
154 | const char *__efi_nesting(void) |
155 | { | |
156 | return indent_string(nesting_level); | |
157 | } | |
158 | ||
af65db85 RC |
159 | const char *__efi_nesting_inc(void) |
160 | { | |
161 | return indent_string(nesting_level++); | |
162 | } | |
163 | ||
164 | const char *__efi_nesting_dec(void) | |
165 | { | |
166 | return indent_string(--nesting_level); | |
167 | } | |
168 | ||
7a69e97b HS |
169 | /** |
170 | * efi_event_is_queued() - check if an event is queued | |
171 | * | |
172 | * @event: event | |
173 | * Return: true if event is queued | |
174 | */ | |
175 | static bool efi_event_is_queued(struct efi_event *event) | |
176 | { | |
177 | return !!event->queue_link.next; | |
178 | } | |
179 | ||
180 | /** | |
181 | * efi_process_event_queue() - process event queue | |
182 | */ | |
183 | static void efi_process_event_queue(void) | |
184 | { | |
185 | while (!list_empty(&efi_event_queue)) { | |
186 | struct efi_event *event; | |
187 | efi_uintn_t old_tpl; | |
188 | ||
189 | event = list_first_entry(&efi_event_queue, struct efi_event, | |
190 | queue_link); | |
191 | if (efi_tpl >= event->notify_tpl) | |
192 | return; | |
193 | list_del(&event->queue_link); | |
194 | event->queue_link.next = NULL; | |
195 | event->queue_link.prev = NULL; | |
196 | /* Events must be executed at the event's TPL */ | |
197 | old_tpl = efi_tpl; | |
198 | efi_tpl = event->notify_tpl; | |
199 | EFI_CALL_VOID(event->notify_function(event, | |
200 | event->notify_context)); | |
201 | efi_tpl = old_tpl; | |
202 | if (event->type == EVT_NOTIFY_SIGNAL) | |
203 | event->is_signaled = 0; | |
204 | } | |
205 | } | |
206 | ||
6b03cd10 | 207 | /** |
78a88f79 MS |
208 | * efi_queue_event() - queue an EFI event |
209 | * @event: event to signal | |
332468f7 HS |
210 | * |
211 | * This function queues the notification function of the event for future | |
212 | * execution. | |
213 | * | |
332468f7 | 214 | */ |
7eaa900e | 215 | static void efi_queue_event(struct efi_event *event) |
c6841592 | 216 | { |
7a69e97b HS |
217 | struct efi_event *item = NULL; |
218 | ||
219 | if (!event->notify_function) | |
220 | return; | |
221 | ||
222 | if (!efi_event_is_queued(event)) { | |
223 | /* | |
224 | * Events must be notified in order of decreasing task priority | |
225 | * level. Insert the new event accordingly. | |
226 | */ | |
227 | list_for_each_entry(item, &efi_event_queue, queue_link) { | |
228 | if (item->notify_tpl < event->notify_tpl) { | |
229 | list_add_tail(&event->queue_link, | |
230 | &item->queue_link); | |
231 | event = NULL; | |
232 | break; | |
233 | } | |
234 | } | |
235 | if (event) | |
236 | list_add_tail(&event->queue_link, &efi_event_queue); | |
c6841592 | 237 | } |
7a69e97b | 238 | efi_process_event_queue(); |
c6841592 HS |
239 | } |
240 | ||
21b3edfc HS |
241 | /** |
242 | * is_valid_tpl() - check if the task priority level is valid | |
243 | * | |
244 | * @tpl: TPL level to check | |
b72aaa87 | 245 | * Return: status code |
21b3edfc HS |
246 | */ |
247 | efi_status_t is_valid_tpl(efi_uintn_t tpl) | |
248 | { | |
249 | switch (tpl) { | |
250 | case TPL_APPLICATION: | |
251 | case TPL_CALLBACK: | |
252 | case TPL_NOTIFY: | |
253 | case TPL_HIGH_LEVEL: | |
254 | return EFI_SUCCESS; | |
255 | default: | |
256 | return EFI_INVALID_PARAMETER; | |
257 | } | |
258 | } | |
259 | ||
6b03cd10 | 260 | /** |
78a88f79 MS |
261 | * efi_signal_event() - signal an EFI event |
262 | * @event: event to signal | |
b095f3c8 | 263 | * |
78a88f79 MS |
264 | * This function signals an event. If the event belongs to an event group all |
265 | * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL | |
b095f3c8 HS |
266 | * their notification function is queued. |
267 | * | |
268 | * For the SignalEvent service see efi_signal_event_ext. | |
b095f3c8 | 269 | */ |
7eaa900e | 270 | void efi_signal_event(struct efi_event *event) |
b095f3c8 | 271 | { |
dfa306e4 HS |
272 | if (event->is_signaled) |
273 | return; | |
b095f3c8 HS |
274 | if (event->group) { |
275 | struct efi_event *evt; | |
276 | ||
277 | /* | |
278 | * The signaled state has to set before executing any | |
279 | * notification function | |
280 | */ | |
281 | list_for_each_entry(evt, &efi_events, link) { | |
282 | if (!evt->group || guidcmp(evt->group, event->group)) | |
283 | continue; | |
284 | if (evt->is_signaled) | |
285 | continue; | |
286 | evt->is_signaled = true; | |
b095f3c8 HS |
287 | } |
288 | list_for_each_entry(evt, &efi_events, link) { | |
289 | if (!evt->group || guidcmp(evt->group, event->group)) | |
290 | continue; | |
7a69e97b | 291 | efi_queue_event(evt); |
b095f3c8 | 292 | } |
3626e539 | 293 | } else { |
b095f3c8 | 294 | event->is_signaled = true; |
7a69e97b | 295 | efi_queue_event(event); |
b095f3c8 HS |
296 | } |
297 | } | |
298 | ||
6b03cd10 | 299 | /** |
78a88f79 MS |
300 | * efi_raise_tpl() - raise the task priority level |
301 | * @new_tpl: new value of the task priority level | |
332468f7 HS |
302 | * |
303 | * This function implements the RaiseTpl service. | |
332468f7 | 304 | * |
78a88f79 MS |
305 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
306 | * details. | |
307 | * | |
308 | * Return: old value of the task priority level | |
332468f7 | 309 | */ |
152cade3 | 310 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
bee91169 | 311 | { |
152cade3 | 312 | efi_uintn_t old_tpl = efi_tpl; |
32f4b2f8 | 313 | |
503f2695 | 314 | EFI_ENTRY("0x%zx", new_tpl); |
32f4b2f8 HS |
315 | |
316 | if (new_tpl < efi_tpl) | |
529886a0 | 317 | EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__); |
32f4b2f8 HS |
318 | efi_tpl = new_tpl; |
319 | if (efi_tpl > TPL_HIGH_LEVEL) | |
320 | efi_tpl = TPL_HIGH_LEVEL; | |
321 | ||
322 | EFI_EXIT(EFI_SUCCESS); | |
323 | return old_tpl; | |
bee91169 AG |
324 | } |
325 | ||
6b03cd10 | 326 | /** |
78a88f79 MS |
327 | * efi_restore_tpl() - lower the task priority level |
328 | * @old_tpl: value of the task priority level to be restored | |
332468f7 HS |
329 | * |
330 | * This function implements the RestoreTpl service. | |
332468f7 | 331 | * |
78a88f79 MS |
332 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
333 | * details. | |
332468f7 | 334 | */ |
152cade3 | 335 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
bee91169 | 336 | { |
503f2695 | 337 | EFI_ENTRY("0x%zx", old_tpl); |
32f4b2f8 HS |
338 | |
339 | if (old_tpl > efi_tpl) | |
529886a0 | 340 | EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__); |
32f4b2f8 HS |
341 | efi_tpl = old_tpl; |
342 | if (efi_tpl > TPL_HIGH_LEVEL) | |
343 | efi_tpl = TPL_HIGH_LEVEL; | |
344 | ||
0f7fcc72 HS |
345 | /* |
346 | * Lowering the TPL may have made queued events eligible for execution. | |
347 | */ | |
348 | efi_timer_check(); | |
349 | ||
32f4b2f8 | 350 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
351 | } |
352 | ||
6b03cd10 | 353 | /** |
78a88f79 MS |
354 | * efi_allocate_pages_ext() - allocate memory pages |
355 | * @type: type of allocation to be performed | |
356 | * @memory_type: usage type of the allocated memory | |
357 | * @pages: number of pages to be allocated | |
358 | * @memory: allocated memory | |
332468f7 HS |
359 | * |
360 | * This function implements the AllocatePages service. | |
332468f7 | 361 | * |
78a88f79 MS |
362 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
363 | * details. | |
364 | * | |
365 | * Return: status code | |
332468f7 | 366 | */ |
6e0bf8d8 | 367 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
f5a2a938 | 368 | efi_uintn_t pages, |
6e0bf8d8 | 369 | uint64_t *memory) |
bee91169 AG |
370 | { |
371 | efi_status_t r; | |
372 | ||
f5a2a938 | 373 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
bee91169 AG |
374 | r = efi_allocate_pages(type, memory_type, pages, memory); |
375 | return EFI_EXIT(r); | |
376 | } | |
377 | ||
6b03cd10 | 378 | /** |
78a88f79 MS |
379 | * efi_free_pages_ext() - Free memory pages. |
380 | * @memory: start of the memory area to be freed | |
381 | * @pages: number of pages to be freed | |
332468f7 HS |
382 | * |
383 | * This function implements the FreePages service. | |
332468f7 | 384 | * |
78a88f79 MS |
385 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
386 | * details. | |
387 | * | |
388 | * Return: status code | |
332468f7 | 389 | */ |
6e0bf8d8 | 390 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
f5a2a938 | 391 | efi_uintn_t pages) |
bee91169 AG |
392 | { |
393 | efi_status_t r; | |
394 | ||
dee37fc9 | 395 | EFI_ENTRY("%llx, 0x%zx", memory, pages); |
bee91169 AG |
396 | r = efi_free_pages(memory, pages); |
397 | return EFI_EXIT(r); | |
398 | } | |
399 | ||
6b03cd10 | 400 | /** |
78a88f79 MS |
401 | * efi_get_memory_map_ext() - get map describing memory usage |
402 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, | |
403 | * on exit the size of the copied memory map | |
404 | * @memory_map: buffer to which the memory map is written | |
405 | * @map_key: key for the memory map | |
406 | * @descriptor_size: size of an individual memory descriptor | |
407 | * @descriptor_version: version number of the memory descriptor structure | |
332468f7 HS |
408 | * |
409 | * This function implements the GetMemoryMap service. | |
332468f7 | 410 | * |
78a88f79 MS |
411 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
412 | * details. | |
413 | * | |
414 | * Return: status code | |
332468f7 | 415 | */ |
6e0bf8d8 | 416 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
f5a2a938 | 417 | efi_uintn_t *memory_map_size, |
6e0bf8d8 | 418 | struct efi_mem_desc *memory_map, |
f5a2a938 HS |
419 | efi_uintn_t *map_key, |
420 | efi_uintn_t *descriptor_size, | |
6e0bf8d8 | 421 | uint32_t *descriptor_version) |
bee91169 AG |
422 | { |
423 | efi_status_t r; | |
424 | ||
425 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, | |
426 | map_key, descriptor_size, descriptor_version); | |
427 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, | |
428 | descriptor_size, descriptor_version); | |
429 | return EFI_EXIT(r); | |
430 | } | |
431 | ||
6b03cd10 | 432 | /** |
78a88f79 MS |
433 | * efi_allocate_pool_ext() - allocate memory from pool |
434 | * @pool_type: type of the pool from which memory is to be allocated | |
435 | * @size: number of bytes to be allocated | |
436 | * @buffer: allocated memory | |
332468f7 HS |
437 | * |
438 | * This function implements the AllocatePool service. | |
332468f7 | 439 | * |
78a88f79 MS |
440 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
441 | * details. | |
442 | * | |
443 | * Return: status code | |
332468f7 | 444 | */ |
ead1274b | 445 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
f5a2a938 | 446 | efi_uintn_t size, |
ead1274b | 447 | void **buffer) |
bee91169 | 448 | { |
1cd29f0a AG |
449 | efi_status_t r; |
450 | ||
f5a2a938 | 451 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
ead1274b | 452 | r = efi_allocate_pool(pool_type, size, buffer); |
1cd29f0a | 453 | return EFI_EXIT(r); |
bee91169 AG |
454 | } |
455 | ||
6b03cd10 | 456 | /** |
78a88f79 MS |
457 | * efi_free_pool_ext() - free memory from pool |
458 | * @buffer: start of memory to be freed | |
332468f7 HS |
459 | * |
460 | * This function implements the FreePool service. | |
332468f7 | 461 | * |
78a88f79 MS |
462 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
463 | * details. | |
464 | * | |
465 | * Return: status code | |
332468f7 | 466 | */ |
42417bc8 | 467 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
bee91169 | 468 | { |
1cd29f0a AG |
469 | efi_status_t r; |
470 | ||
471 | EFI_ENTRY("%p", buffer); | |
42417bc8 | 472 | r = efi_free_pool(buffer); |
1cd29f0a | 473 | return EFI_EXIT(r); |
bee91169 AG |
474 | } |
475 | ||
6b03cd10 | 476 | /** |
e6023be4 | 477 | * efi_add_handle() - add a new handle to the object list |
44549d62 | 478 | * |
e6023be4 HS |
479 | * @handle: handle to be added |
480 | * | |
481 | * The protocols list is initialized. The handle is added to the list of known | |
482 | * UEFI objects. | |
44549d62 | 483 | */ |
fae0118e | 484 | void efi_add_handle(efi_handle_t handle) |
44549d62 | 485 | { |
fae0118e | 486 | if (!handle) |
44549d62 | 487 | return; |
fae0118e HS |
488 | INIT_LIST_HEAD(&handle->protocols); |
489 | list_add_tail(&handle->link, &efi_obj_list); | |
44549d62 HS |
490 | } |
491 | ||
6b03cd10 | 492 | /** |
78a88f79 MS |
493 | * efi_create_handle() - create handle |
494 | * @handle: new handle | |
2edab5e2 | 495 | * |
78a88f79 | 496 | * Return: status code |
2edab5e2 | 497 | */ |
2074f700 | 498 | efi_status_t efi_create_handle(efi_handle_t *handle) |
3cc6e3fe HS |
499 | { |
500 | struct efi_object *obj; | |
3cc6e3fe | 501 | |
d29e7824 HS |
502 | obj = calloc(1, sizeof(struct efi_object)); |
503 | if (!obj) | |
504 | return EFI_OUT_OF_RESOURCES; | |
505 | ||
44549d62 | 506 | efi_add_handle(obj); |
fae0118e | 507 | *handle = obj; |
d29e7824 HS |
508 | |
509 | return EFI_SUCCESS; | |
3cc6e3fe HS |
510 | } |
511 | ||
6b03cd10 | 512 | /** |
78a88f79 MS |
513 | * efi_search_protocol() - find a protocol on a handle. |
514 | * @handle: handle | |
515 | * @protocol_guid: GUID of the protocol | |
516 | * @handler: reference to the protocol | |
678e03a0 | 517 | * |
78a88f79 | 518 | * Return: status code |
678e03a0 | 519 | */ |
2074f700 | 520 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
678e03a0 HS |
521 | const efi_guid_t *protocol_guid, |
522 | struct efi_handler **handler) | |
523 | { | |
524 | struct efi_object *efiobj; | |
525 | struct list_head *lhandle; | |
526 | ||
527 | if (!handle || !protocol_guid) | |
528 | return EFI_INVALID_PARAMETER; | |
529 | efiobj = efi_search_obj(handle); | |
530 | if (!efiobj) | |
531 | return EFI_INVALID_PARAMETER; | |
532 | list_for_each(lhandle, &efiobj->protocols) { | |
533 | struct efi_handler *protocol; | |
534 | ||
535 | protocol = list_entry(lhandle, struct efi_handler, link); | |
536 | if (!guidcmp(protocol->guid, protocol_guid)) { | |
537 | if (handler) | |
538 | *handler = protocol; | |
539 | return EFI_SUCCESS; | |
540 | } | |
541 | } | |
542 | return EFI_NOT_FOUND; | |
543 | } | |
544 | ||
6b03cd10 | 545 | /** |
78a88f79 MS |
546 | * efi_remove_protocol() - delete protocol from a handle |
547 | * @handle: handle from which the protocol shall be deleted | |
548 | * @protocol: GUID of the protocol to be deleted | |
549 | * @protocol_interface: interface of the protocol implementation | |
678e03a0 | 550 | * |
78a88f79 | 551 | * Return: status code |
678e03a0 | 552 | */ |
2074f700 HS |
553 | efi_status_t efi_remove_protocol(const efi_handle_t handle, |
554 | const efi_guid_t *protocol, | |
678e03a0 HS |
555 | void *protocol_interface) |
556 | { | |
557 | struct efi_handler *handler; | |
558 | efi_status_t ret; | |
559 | ||
560 | ret = efi_search_protocol(handle, protocol, &handler); | |
561 | if (ret != EFI_SUCCESS) | |
562 | return ret; | |
1f470e17 | 563 | if (handler->protocol_interface != protocol_interface) |
96aa99cd | 564 | return EFI_NOT_FOUND; |
678e03a0 HS |
565 | list_del(&handler->link); |
566 | free(handler); | |
567 | return EFI_SUCCESS; | |
568 | } | |
569 | ||
6b03cd10 | 570 | /** |
78a88f79 MS |
571 | * efi_remove_all_protocols() - delete all protocols from a handle |
572 | * @handle: handle from which the protocols shall be deleted | |
678e03a0 | 573 | * |
78a88f79 | 574 | * Return: status code |
678e03a0 | 575 | */ |
2074f700 | 576 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
678e03a0 HS |
577 | { |
578 | struct efi_object *efiobj; | |
32e6fed6 HS |
579 | struct efi_handler *protocol; |
580 | struct efi_handler *pos; | |
678e03a0 HS |
581 | |
582 | efiobj = efi_search_obj(handle); | |
583 | if (!efiobj) | |
584 | return EFI_INVALID_PARAMETER; | |
32e6fed6 | 585 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
678e03a0 HS |
586 | efi_status_t ret; |
587 | ||
678e03a0 HS |
588 | ret = efi_remove_protocol(handle, protocol->guid, |
589 | protocol->protocol_interface); | |
590 | if (ret != EFI_SUCCESS) | |
591 | return ret; | |
592 | } | |
593 | return EFI_SUCCESS; | |
594 | } | |
595 | ||
6b03cd10 | 596 | /** |
78a88f79 | 597 | * efi_delete_handle() - delete handle |
678e03a0 | 598 | * |
78a88f79 | 599 | * @obj: handle to delete |
678e03a0 | 600 | */ |
fae0118e | 601 | void efi_delete_handle(efi_handle_t handle) |
678e03a0 | 602 | { |
fae0118e | 603 | if (!handle) |
678e03a0 | 604 | return; |
fae0118e HS |
605 | efi_remove_all_protocols(handle); |
606 | list_del(&handle->link); | |
607 | free(handle); | |
678e03a0 HS |
608 | } |
609 | ||
6b03cd10 | 610 | /** |
78a88f79 MS |
611 | * efi_is_event() - check if a pointer is a valid event |
612 | * @event: pointer to check | |
43bce442 | 613 | * |
78a88f79 | 614 | * Return: status code |
bee91169 | 615 | */ |
43bce442 HS |
616 | static efi_status_t efi_is_event(const struct efi_event *event) |
617 | { | |
618 | const struct efi_event *evt; | |
619 | ||
620 | if (!event) | |
621 | return EFI_INVALID_PARAMETER; | |
622 | list_for_each_entry(evt, &efi_events, link) { | |
623 | if (evt == event) | |
624 | return EFI_SUCCESS; | |
625 | } | |
626 | return EFI_INVALID_PARAMETER; | |
627 | } | |
bee91169 | 628 | |
6b03cd10 | 629 | /** |
78a88f79 MS |
630 | * efi_create_event() - create an event |
631 | * @type: type of the event to create | |
632 | * @notify_tpl: task priority level of the event | |
633 | * @notify_function: notification function of the event | |
634 | * @notify_context: pointer passed to the notification function | |
635 | * @group: event group | |
636 | * @event: created event | |
332468f7 HS |
637 | * |
638 | * This function is used inside U-Boot code to create an event. | |
639 | * | |
640 | * For the API function implementing the CreateEvent service see | |
641 | * efi_create_event_ext. | |
642 | * | |
78a88f79 | 643 | * Return: status code |
332468f7 | 644 | */ |
152cade3 | 645 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
49deb455 | 646 | void (EFIAPI *notify_function) ( |
2fd945fe HS |
647 | struct efi_event *event, |
648 | void *context), | |
b095f3c8 HS |
649 | void *notify_context, efi_guid_t *group, |
650 | struct efi_event **event) | |
bee91169 | 651 | { |
43bce442 | 652 | struct efi_event *evt; |
c6841592 | 653 | |
a95343b8 | 654 | if (event == NULL) |
49deb455 | 655 | return EFI_INVALID_PARAMETER; |
a95343b8 | 656 | |
21b3edfc HS |
657 | switch (type) { |
658 | case 0: | |
659 | case EVT_TIMER: | |
660 | case EVT_NOTIFY_SIGNAL: | |
661 | case EVT_TIMER | EVT_NOTIFY_SIGNAL: | |
662 | case EVT_NOTIFY_WAIT: | |
663 | case EVT_TIMER | EVT_NOTIFY_WAIT: | |
664 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: | |
665 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: | |
666 | break; | |
667 | default: | |
49deb455 | 668 | return EFI_INVALID_PARAMETER; |
21b3edfc | 669 | } |
a95343b8 | 670 | |
3748ed90 | 671 | if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) && |
751e928d | 672 | (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS)) |
49deb455 | 673 | return EFI_INVALID_PARAMETER; |
a95343b8 | 674 | |
43bce442 HS |
675 | evt = calloc(1, sizeof(struct efi_event)); |
676 | if (!evt) | |
677 | return EFI_OUT_OF_RESOURCES; | |
678 | evt->type = type; | |
679 | evt->notify_tpl = notify_tpl; | |
680 | evt->notify_function = notify_function; | |
681 | evt->notify_context = notify_context; | |
b095f3c8 | 682 | evt->group = group; |
b72aaa87 | 683 | /* Disable timers on boot up */ |
43bce442 | 684 | evt->trigger_next = -1ULL; |
43bce442 HS |
685 | list_add_tail(&evt->link, &efi_events); |
686 | *event = evt; | |
687 | return EFI_SUCCESS; | |
bee91169 AG |
688 | } |
689 | ||
9f0930e5 | 690 | /* |
78a88f79 MS |
691 | * efi_create_event_ex() - create an event in a group |
692 | * @type: type of the event to create | |
693 | * @notify_tpl: task priority level of the event | |
694 | * @notify_function: notification function of the event | |
695 | * @notify_context: pointer passed to the notification function | |
696 | * @event: created event | |
697 | * @event_group: event group | |
9f0930e5 HS |
698 | * |
699 | * This function implements the CreateEventEx service. | |
9f0930e5 | 700 | * |
78a88f79 MS |
701 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
702 | * details. | |
703 | * | |
704 | * Return: status code | |
9f0930e5 HS |
705 | */ |
706 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, | |
707 | void (EFIAPI *notify_function) ( | |
708 | struct efi_event *event, | |
709 | void *context), | |
710 | void *notify_context, | |
711 | efi_guid_t *event_group, | |
712 | struct efi_event **event) | |
713 | { | |
1884512b HS |
714 | efi_status_t ret; |
715 | ||
9f0930e5 HS |
716 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function, |
717 | notify_context, event_group); | |
1884512b HS |
718 | |
719 | /* | |
720 | * The allowable input parameters are the same as in CreateEvent() | |
721 | * except for the following two disallowed event types. | |
722 | */ | |
723 | switch (type) { | |
724 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: | |
725 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: | |
726 | ret = EFI_INVALID_PARAMETER; | |
727 | goto out; | |
728 | } | |
729 | ||
730 | ret = efi_create_event(type, notify_tpl, notify_function, | |
731 | notify_context, event_group, event); | |
732 | out: | |
733 | return EFI_EXIT(ret); | |
9f0930e5 HS |
734 | } |
735 | ||
6b03cd10 | 736 | /** |
78a88f79 MS |
737 | * efi_create_event_ext() - create an event |
738 | * @type: type of the event to create | |
739 | * @notify_tpl: task priority level of the event | |
740 | * @notify_function: notification function of the event | |
741 | * @notify_context: pointer passed to the notification function | |
742 | * @event: created event | |
332468f7 HS |
743 | * |
744 | * This function implements the CreateEvent service. | |
332468f7 | 745 | * |
78a88f79 MS |
746 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
747 | * details. | |
748 | * | |
749 | * Return: status code | |
332468f7 | 750 | */ |
49deb455 | 751 | static efi_status_t EFIAPI efi_create_event_ext( |
152cade3 | 752 | uint32_t type, efi_uintn_t notify_tpl, |
49deb455 HS |
753 | void (EFIAPI *notify_function) ( |
754 | struct efi_event *event, | |
755 | void *context), | |
756 | void *notify_context, struct efi_event **event) | |
757 | { | |
758 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, | |
759 | notify_context); | |
760 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, | |
b095f3c8 | 761 | notify_context, NULL, event)); |
49deb455 HS |
762 | } |
763 | ||
6b03cd10 | 764 | /** |
78a88f79 | 765 | * efi_timer_check() - check if a timer event has occurred |
6b03cd10 | 766 | * |
332468f7 HS |
767 | * Check if a timer event has occurred or a queued notification function should |
768 | * be called. | |
769 | * | |
bee91169 | 770 | * Our timers have to work without interrupts, so we check whenever keyboard |
332468f7 | 771 | * input or disk accesses happen if enough time elapsed for them to fire. |
bee91169 AG |
772 | */ |
773 | void efi_timer_check(void) | |
774 | { | |
43bce442 | 775 | struct efi_event *evt; |
bee91169 AG |
776 | u64 now = timer_get_us(); |
777 | ||
43bce442 | 778 | list_for_each_entry(evt, &efi_events, link) { |
7eaa900e HS |
779 | if (!timers_enabled) |
780 | continue; | |
43bce442 | 781 | if (!(evt->type & EVT_TIMER) || now < evt->trigger_next) |
ca62a4f5 | 782 | continue; |
43bce442 | 783 | switch (evt->trigger_type) { |
ca62a4f5 | 784 | case EFI_TIMER_RELATIVE: |
43bce442 | 785 | evt->trigger_type = EFI_TIMER_STOP; |
ca62a4f5 HS |
786 | break; |
787 | case EFI_TIMER_PERIODIC: | |
43bce442 | 788 | evt->trigger_next += evt->trigger_time; |
ca62a4f5 HS |
789 | break; |
790 | default: | |
791 | continue; | |
c6841592 | 792 | } |
b095f3c8 | 793 | evt->is_signaled = false; |
7eaa900e | 794 | efi_signal_event(evt); |
bee91169 | 795 | } |
7a69e97b | 796 | efi_process_event_queue(); |
bee91169 AG |
797 | WATCHDOG_RESET(); |
798 | } | |
799 | ||
6b03cd10 | 800 | /** |
78a88f79 MS |
801 | * efi_set_timer() - set the trigger time for a timer event or stop the event |
802 | * @event: event for which the timer is set | |
803 | * @type: type of the timer | |
b72aaa87 | 804 | * @trigger_time: trigger period in multiples of 100 ns |
332468f7 HS |
805 | * |
806 | * This is the function for internal usage in U-Boot. For the API function | |
807 | * implementing the SetTimer service see efi_set_timer_ext. | |
808 | * | |
78a88f79 | 809 | * Return: status code |
332468f7 | 810 | */ |
b521d29e | 811 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
bfc72462 | 812 | uint64_t trigger_time) |
bee91169 | 813 | { |
43bce442 HS |
814 | /* Check that the event is valid */ |
815 | if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER)) | |
816 | return EFI_INVALID_PARAMETER; | |
bee91169 | 817 | |
8787b02e | 818 | /* |
b72aaa87 HS |
819 | * The parameter defines a multiple of 100 ns. |
820 | * We use multiples of 1000 ns. So divide by 10. | |
8787b02e | 821 | */ |
7d963323 | 822 | do_div(trigger_time, 10); |
bee91169 | 823 | |
43bce442 HS |
824 | switch (type) { |
825 | case EFI_TIMER_STOP: | |
826 | event->trigger_next = -1ULL; | |
827 | break; | |
828 | case EFI_TIMER_PERIODIC: | |
829 | case EFI_TIMER_RELATIVE: | |
830 | event->trigger_next = timer_get_us() + trigger_time; | |
831 | break; | |
832 | default: | |
833 | return EFI_INVALID_PARAMETER; | |
bee91169 | 834 | } |
43bce442 HS |
835 | event->trigger_type = type; |
836 | event->trigger_time = trigger_time; | |
837 | event->is_signaled = false; | |
838 | return EFI_SUCCESS; | |
bfc72462 HS |
839 | } |
840 | ||
6b03cd10 | 841 | /** |
78a88f79 MS |
842 | * efi_set_timer_ext() - Set the trigger time for a timer event or stop the |
843 | * event | |
844 | * @event: event for which the timer is set | |
845 | * @type: type of the timer | |
b72aaa87 | 846 | * @trigger_time: trigger period in multiples of 100 ns |
332468f7 HS |
847 | * |
848 | * This function implements the SetTimer service. | |
332468f7 | 849 | * |
78a88f79 MS |
850 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
851 | * details. | |
852 | * | |
853 | * | |
854 | * Return: status code | |
332468f7 | 855 | */ |
b521d29e HS |
856 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
857 | enum efi_timer_delay type, | |
858 | uint64_t trigger_time) | |
bfc72462 | 859 | { |
dee37fc9 | 860 | EFI_ENTRY("%p, %d, %llx", event, type, trigger_time); |
bfc72462 | 861 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); |
bee91169 AG |
862 | } |
863 | ||
6b03cd10 | 864 | /** |
78a88f79 MS |
865 | * efi_wait_for_event() - wait for events to be signaled |
866 | * @num_events: number of events to be waited for | |
867 | * @event: events to be waited for | |
868 | * @index: index of the event that was signaled | |
332468f7 HS |
869 | * |
870 | * This function implements the WaitForEvent service. | |
332468f7 | 871 | * |
78a88f79 MS |
872 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
873 | * details. | |
874 | * | |
875 | * Return: status code | |
332468f7 | 876 | */ |
f5a2a938 | 877 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
2fd945fe | 878 | struct efi_event **event, |
f5a2a938 | 879 | efi_uintn_t *index) |
bee91169 | 880 | { |
43bce442 | 881 | int i; |
bee91169 | 882 | |
f5a2a938 | 883 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
bee91169 | 884 | |
c6841592 HS |
885 | /* Check parameters */ |
886 | if (!num_events || !event) | |
887 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
32f4b2f8 HS |
888 | /* Check TPL */ |
889 | if (efi_tpl != TPL_APPLICATION) | |
890 | return EFI_EXIT(EFI_UNSUPPORTED); | |
c6841592 | 891 | for (i = 0; i < num_events; ++i) { |
43bce442 HS |
892 | if (efi_is_event(event[i]) != EFI_SUCCESS) |
893 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c6841592 HS |
894 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) |
895 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
e190e897 | 896 | if (!event[i]->is_signaled) |
7eaa900e | 897 | efi_queue_event(event[i]); |
c6841592 HS |
898 | } |
899 | ||
900 | /* Wait for signal */ | |
901 | for (;;) { | |
902 | for (i = 0; i < num_events; ++i) { | |
e190e897 | 903 | if (event[i]->is_signaled) |
c6841592 HS |
904 | goto out; |
905 | } | |
906 | /* Allow events to occur. */ | |
907 | efi_timer_check(); | |
908 | } | |
909 | ||
910 | out: | |
911 | /* | |
912 | * Reset the signal which is passed to the caller to allow periodic | |
913 | * events to occur. | |
914 | */ | |
e190e897 | 915 | event[i]->is_signaled = false; |
c6841592 HS |
916 | if (index) |
917 | *index = i; | |
bee91169 AG |
918 | |
919 | return EFI_EXIT(EFI_SUCCESS); | |
920 | } | |
921 | ||
6b03cd10 | 922 | /** |
78a88f79 MS |
923 | * efi_signal_event_ext() - signal an EFI event |
924 | * @event: event to signal | |
332468f7 HS |
925 | * |
926 | * This function implements the SignalEvent service. | |
78a88f79 MS |
927 | * |
928 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
929 | * details. | |
332468f7 HS |
930 | * |
931 | * This functions sets the signaled state of the event and queues the | |
932 | * notification function for execution. | |
933 | * | |
78a88f79 | 934 | * Return: status code |
332468f7 | 935 | */ |
c6841592 | 936 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
bee91169 AG |
937 | { |
938 | EFI_ENTRY("%p", event); | |
43bce442 HS |
939 | if (efi_is_event(event) != EFI_SUCCESS) |
940 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
7eaa900e | 941 | efi_signal_event(event); |
bee91169 AG |
942 | return EFI_EXIT(EFI_SUCCESS); |
943 | } | |
944 | ||
6b03cd10 | 945 | /** |
78a88f79 MS |
946 | * efi_close_event() - close an EFI event |
947 | * @event: event to close | |
332468f7 HS |
948 | * |
949 | * This function implements the CloseEvent service. | |
332468f7 | 950 | * |
78a88f79 MS |
951 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
952 | * details. | |
953 | * | |
954 | * Return: status code | |
332468f7 | 955 | */ |
2fd945fe | 956 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
bee91169 | 957 | { |
ab15d414 HS |
958 | struct efi_register_notify_event *item, *next; |
959 | ||
bee91169 | 960 | EFI_ENTRY("%p", event); |
43bce442 HS |
961 | if (efi_is_event(event) != EFI_SUCCESS) |
962 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
ab15d414 HS |
963 | |
964 | /* Remove protocol notify registrations for the event */ | |
965 | list_for_each_entry_safe(item, next, &efi_register_notify_events, | |
966 | link) { | |
967 | if (event == item->event) { | |
f09cea36 HS |
968 | struct efi_protocol_notification *hitem, *hnext; |
969 | ||
970 | /* Remove signaled handles */ | |
971 | list_for_each_entry_safe(hitem, hnext, &item->handles, | |
972 | link) { | |
973 | list_del(&hitem->link); | |
974 | free(hitem); | |
975 | } | |
ab15d414 HS |
976 | list_del(&item->link); |
977 | free(item); | |
978 | } | |
979 | } | |
7a69e97b HS |
980 | /* Remove event from queue */ |
981 | if (efi_event_is_queued(event)) | |
982 | list_del(&event->queue_link); | |
ab15d414 | 983 | |
43bce442 HS |
984 | list_del(&event->link); |
985 | free(event); | |
986 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
987 | } |
988 | ||
6b03cd10 | 989 | /** |
78a88f79 MS |
990 | * efi_check_event() - check if an event is signaled |
991 | * @event: event to check | |
332468f7 HS |
992 | * |
993 | * This function implements the CheckEvent service. | |
332468f7 | 994 | * |
78a88f79 MS |
995 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
996 | * details. | |
997 | * | |
998 | * If an event is not signaled yet, the notification function is queued. The | |
999 | * signaled state is cleared. | |
332468f7 | 1000 | * |
78a88f79 | 1001 | * Return: status code |
332468f7 | 1002 | */ |
2fd945fe | 1003 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
bee91169 AG |
1004 | { |
1005 | EFI_ENTRY("%p", event); | |
c6841592 | 1006 | efi_timer_check(); |
43bce442 HS |
1007 | if (efi_is_event(event) != EFI_SUCCESS || |
1008 | event->type & EVT_NOTIFY_SIGNAL) | |
1009 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
1010 | if (!event->is_signaled) | |
7eaa900e | 1011 | efi_queue_event(event); |
43bce442 HS |
1012 | if (event->is_signaled) { |
1013 | event->is_signaled = false; | |
1014 | return EFI_EXIT(EFI_SUCCESS); | |
c6841592 | 1015 | } |
43bce442 | 1016 | return EFI_EXIT(EFI_NOT_READY); |
bee91169 AG |
1017 | } |
1018 | ||
6b03cd10 | 1019 | /** |
78a88f79 MS |
1020 | * efi_search_obj() - find the internal EFI object for a handle |
1021 | * @handle: handle to find | |
7b9f8ad7 | 1022 | * |
78a88f79 | 1023 | * Return: EFI object |
7b9f8ad7 | 1024 | */ |
2074f700 | 1025 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
7b9f8ad7 | 1026 | { |
1b68153a | 1027 | struct efi_object *efiobj; |
7b9f8ad7 | 1028 | |
ec163faa HS |
1029 | if (!handle) |
1030 | return NULL; | |
1031 | ||
1b68153a | 1032 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
fae0118e | 1033 | if (efiobj == handle) |
7b9f8ad7 HS |
1034 | return efiobj; |
1035 | } | |
7b9f8ad7 HS |
1036 | return NULL; |
1037 | } | |
1038 | ||
6b03cd10 | 1039 | /** |
78a88f79 MS |
1040 | * efi_open_protocol_info_entry() - create open protocol info entry and add it |
1041 | * to a protocol | |
1042 | * @handler: handler of a protocol | |
fe1599da | 1043 | * |
78a88f79 | 1044 | * Return: open protocol info entry |
fe1599da HS |
1045 | */ |
1046 | static struct efi_open_protocol_info_entry *efi_create_open_info( | |
1047 | struct efi_handler *handler) | |
1048 | { | |
1049 | struct efi_open_protocol_info_item *item; | |
1050 | ||
1051 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); | |
1052 | if (!item) | |
1053 | return NULL; | |
1054 | /* Append the item to the open protocol info list. */ | |
1055 | list_add_tail(&item->link, &handler->open_infos); | |
1056 | ||
1057 | return &item->info; | |
1058 | } | |
1059 | ||
6b03cd10 | 1060 | /** |
78a88f79 MS |
1061 | * efi_delete_open_info() - remove an open protocol info entry from a protocol |
1062 | * @item: open protocol info entry to delete | |
fe1599da | 1063 | * |
78a88f79 | 1064 | * Return: status code |
fe1599da HS |
1065 | */ |
1066 | static efi_status_t efi_delete_open_info( | |
1067 | struct efi_open_protocol_info_item *item) | |
1068 | { | |
1069 | list_del(&item->link); | |
1070 | free(item); | |
1071 | return EFI_SUCCESS; | |
1072 | } | |
1073 | ||
6b03cd10 | 1074 | /** |
78a88f79 MS |
1075 | * efi_add_protocol() - install new protocol on a handle |
1076 | * @handle: handle on which the protocol shall be installed | |
1077 | * @protocol: GUID of the protocol to be installed | |
1078 | * @protocol_interface: interface of the protocol implementation | |
3f79a2b5 | 1079 | * |
78a88f79 | 1080 | * Return: status code |
3f79a2b5 | 1081 | */ |
2074f700 HS |
1082 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
1083 | const efi_guid_t *protocol, | |
3f79a2b5 HS |
1084 | void *protocol_interface) |
1085 | { | |
1086 | struct efi_object *efiobj; | |
1087 | struct efi_handler *handler; | |
1088 | efi_status_t ret; | |
ab15d414 | 1089 | struct efi_register_notify_event *event; |
3f79a2b5 HS |
1090 | |
1091 | efiobj = efi_search_obj(handle); | |
1092 | if (!efiobj) | |
1093 | return EFI_INVALID_PARAMETER; | |
1094 | ret = efi_search_protocol(handle, protocol, NULL); | |
1095 | if (ret != EFI_NOT_FOUND) | |
1096 | return EFI_INVALID_PARAMETER; | |
1097 | handler = calloc(1, sizeof(struct efi_handler)); | |
1098 | if (!handler) | |
1099 | return EFI_OUT_OF_RESOURCES; | |
69fb6b1a HS |
1100 | handler->guid = protocol; |
1101 | handler->protocol_interface = protocol_interface; | |
fe1599da | 1102 | INIT_LIST_HEAD(&handler->open_infos); |
69fb6b1a | 1103 | list_add_tail(&handler->link, &efiobj->protocols); |
ab15d414 HS |
1104 | |
1105 | /* Notify registered events */ | |
1106 | list_for_each_entry(event, &efi_register_notify_events, link) { | |
f09cea36 HS |
1107 | if (!guidcmp(protocol, &event->protocol)) { |
1108 | struct efi_protocol_notification *notif; | |
1109 | ||
1110 | notif = calloc(1, sizeof(*notif)); | |
1111 | if (!notif) { | |
1112 | list_del(&handler->link); | |
1113 | free(handler); | |
1114 | return EFI_OUT_OF_RESOURCES; | |
1115 | } | |
1116 | notif->handle = handle; | |
1117 | list_add_tail(¬if->link, &event->handles); | |
daa3f847 | 1118 | event->event->is_signaled = false; |
7eaa900e | 1119 | efi_signal_event(event->event); |
f09cea36 | 1120 | } |
ab15d414 HS |
1121 | } |
1122 | ||
d5504144 HS |
1123 | if (!guidcmp(&efi_guid_device_path, protocol)) |
1124 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); | |
69fb6b1a | 1125 | return EFI_SUCCESS; |
3f79a2b5 HS |
1126 | } |
1127 | ||
6b03cd10 | 1128 | /** |
78a88f79 MS |
1129 | * efi_install_protocol_interface() - install protocol interface |
1130 | * @handle: handle on which the protocol shall be installed | |
1131 | * @protocol: GUID of the protocol to be installed | |
1132 | * @protocol_interface_type: type of the interface to be installed, | |
1133 | * always EFI_NATIVE_INTERFACE | |
1134 | * @protocol_interface: interface of the protocol implementation | |
332468f7 | 1135 | * |
1760ef57 | 1136 | * This function implements the InstallProtocolInterface service. |
332468f7 | 1137 | * |
78a88f79 MS |
1138 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1139 | * details. | |
1140 | * | |
1141 | * Return: status code | |
332468f7 | 1142 | */ |
1760ef57 | 1143 | static efi_status_t EFIAPI efi_install_protocol_interface( |
faea1041 | 1144 | efi_handle_t *handle, const efi_guid_t *protocol, |
1760ef57 | 1145 | int protocol_interface_type, void *protocol_interface) |
bee91169 | 1146 | { |
e0549f8a HS |
1147 | efi_status_t r; |
1148 | ||
1760ef57 HS |
1149 | EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type, |
1150 | protocol_interface); | |
1151 | ||
e0549f8a HS |
1152 | if (!handle || !protocol || |
1153 | protocol_interface_type != EFI_NATIVE_INTERFACE) { | |
1154 | r = EFI_INVALID_PARAMETER; | |
1155 | goto out; | |
1156 | } | |
1157 | ||
1158 | /* Create new handle if requested. */ | |
1159 | if (!*handle) { | |
3cc6e3fe HS |
1160 | r = efi_create_handle(handle); |
1161 | if (r != EFI_SUCCESS) | |
1162 | goto out; | |
529886a0 | 1163 | EFI_PRINT("new handle %p\n", *handle); |
af1408e0 | 1164 | } else { |
529886a0 | 1165 | EFI_PRINT("handle %p\n", *handle); |
e0549f8a | 1166 | } |
1202530d HS |
1167 | /* Add new protocol */ |
1168 | r = efi_add_protocol(*handle, protocol, protocol_interface); | |
e0549f8a | 1169 | out: |
1760ef57 | 1170 | return EFI_EXIT(r); |
bee91169 | 1171 | } |
e0549f8a | 1172 | |
6b03cd10 | 1173 | /** |
78a88f79 | 1174 | * efi_get_drivers() - get all drivers associated to a controller |
fae0118e | 1175 | * @handle: handle of the controller |
b72aaa87 | 1176 | * @protocol: protocol GUID (optional) |
78a88f79 MS |
1177 | * @number_of_drivers: number of child controllers |
1178 | * @driver_handle_buffer: handles of the the drivers | |
6b03cd10 | 1179 | * |
3f9b0042 HS |
1180 | * The allocated buffer has to be freed with free(). |
1181 | * | |
78a88f79 | 1182 | * Return: status code |
3f9b0042 | 1183 | */ |
fae0118e | 1184 | static efi_status_t efi_get_drivers(efi_handle_t handle, |
3f9b0042 HS |
1185 | const efi_guid_t *protocol, |
1186 | efi_uintn_t *number_of_drivers, | |
1187 | efi_handle_t **driver_handle_buffer) | |
1188 | { | |
1189 | struct efi_handler *handler; | |
1190 | struct efi_open_protocol_info_item *item; | |
1191 | efi_uintn_t count = 0, i; | |
1192 | bool duplicate; | |
1193 | ||
1194 | /* Count all driver associations */ | |
fae0118e | 1195 | list_for_each_entry(handler, &handle->protocols, link) { |
3f9b0042 HS |
1196 | if (protocol && guidcmp(handler->guid, protocol)) |
1197 | continue; | |
1198 | list_for_each_entry(item, &handler->open_infos, link) { | |
1199 | if (item->info.attributes & | |
1200 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
1201 | ++count; | |
1202 | } | |
1203 | } | |
66ca24a9 HS |
1204 | *number_of_drivers = 0; |
1205 | if (!count) { | |
1206 | *driver_handle_buffer = NULL; | |
1207 | return EFI_SUCCESS; | |
1208 | } | |
3f9b0042 HS |
1209 | /* |
1210 | * Create buffer. In case of duplicate driver assignments the buffer | |
1211 | * will be too large. But that does not harm. | |
1212 | */ | |
3f9b0042 HS |
1213 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
1214 | if (!*driver_handle_buffer) | |
1215 | return EFI_OUT_OF_RESOURCES; | |
1216 | /* Collect unique driver handles */ | |
fae0118e | 1217 | list_for_each_entry(handler, &handle->protocols, link) { |
3f9b0042 HS |
1218 | if (protocol && guidcmp(handler->guid, protocol)) |
1219 | continue; | |
1220 | list_for_each_entry(item, &handler->open_infos, link) { | |
1221 | if (item->info.attributes & | |
1222 | EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
1223 | /* Check this is a new driver */ | |
1224 | duplicate = false; | |
1225 | for (i = 0; i < *number_of_drivers; ++i) { | |
1226 | if ((*driver_handle_buffer)[i] == | |
1227 | item->info.agent_handle) | |
1228 | duplicate = true; | |
1229 | } | |
1230 | /* Copy handle to buffer */ | |
1231 | if (!duplicate) { | |
1232 | i = (*number_of_drivers)++; | |
1233 | (*driver_handle_buffer)[i] = | |
1234 | item->info.agent_handle; | |
1235 | } | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | return EFI_SUCCESS; | |
1240 | } | |
1241 | ||
6b03cd10 | 1242 | /** |
78a88f79 | 1243 | * efi_disconnect_all_drivers() - disconnect all drivers from a controller |
fae0118e | 1244 | * @handle: handle of the controller |
b72aaa87 | 1245 | * @protocol: protocol GUID (optional) |
78a88f79 | 1246 | * @child_handle: handle of the child to destroy |
3f9b0042 HS |
1247 | * |
1248 | * This function implements the DisconnectController service. | |
3f9b0042 | 1249 | * |
78a88f79 MS |
1250 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1251 | * details. | |
1252 | * | |
1253 | * Return: status code | |
3f9b0042 | 1254 | */ |
fae0118e HS |
1255 | static efi_status_t efi_disconnect_all_drivers |
1256 | (efi_handle_t handle, | |
1257 | const efi_guid_t *protocol, | |
1258 | efi_handle_t child_handle) | |
3f9b0042 HS |
1259 | { |
1260 | efi_uintn_t number_of_drivers; | |
1261 | efi_handle_t *driver_handle_buffer; | |
1262 | efi_status_t r, ret; | |
1263 | ||
fae0118e | 1264 | ret = efi_get_drivers(handle, protocol, &number_of_drivers, |
3f9b0042 HS |
1265 | &driver_handle_buffer); |
1266 | if (ret != EFI_SUCCESS) | |
1267 | return ret; | |
66ca24a9 HS |
1268 | if (!number_of_drivers) |
1269 | return EFI_SUCCESS; | |
3f9b0042 HS |
1270 | ret = EFI_NOT_FOUND; |
1271 | while (number_of_drivers) { | |
1272 | r = EFI_CALL(efi_disconnect_controller( | |
fae0118e | 1273 | handle, |
3f9b0042 HS |
1274 | driver_handle_buffer[--number_of_drivers], |
1275 | child_handle)); | |
1276 | if (r == EFI_SUCCESS) | |
1277 | ret = r; | |
1278 | } | |
1279 | free(driver_handle_buffer); | |
1280 | return ret; | |
1281 | } | |
1282 | ||
6b03cd10 | 1283 | /** |
9b47f13b HS |
1284 | * efi_uninstall_protocol() - uninstall protocol interface |
1285 | * | |
78a88f79 MS |
1286 | * @handle: handle from which the protocol shall be removed |
1287 | * @protocol: GUID of the protocol to be removed | |
1288 | * @protocol_interface: interface to be removed | |
332468f7 | 1289 | * |
9b47f13b | 1290 | * This function DOES NOT delete a handle without installed protocol. |
78a88f79 MS |
1291 | * |
1292 | * Return: status code | |
332468f7 | 1293 | */ |
9b47f13b HS |
1294 | static efi_status_t efi_uninstall_protocol |
1295 | (efi_handle_t handle, const efi_guid_t *protocol, | |
1296 | void *protocol_interface) | |
bee91169 | 1297 | { |
ad97373b | 1298 | struct efi_object *efiobj; |
5810511d | 1299 | struct efi_handler *handler; |
ad97373b HS |
1300 | struct efi_open_protocol_info_item *item; |
1301 | struct efi_open_protocol_info_item *pos; | |
5810511d | 1302 | efi_status_t r; |
4b6ed0d7 | 1303 | |
ad97373b HS |
1304 | /* Check handle */ |
1305 | efiobj = efi_search_obj(handle); | |
1306 | if (!efiobj) { | |
4b6ed0d7 HS |
1307 | r = EFI_INVALID_PARAMETER; |
1308 | goto out; | |
1309 | } | |
5810511d HS |
1310 | /* Find the protocol on the handle */ |
1311 | r = efi_search_protocol(handle, protocol, &handler); | |
1312 | if (r != EFI_SUCCESS) | |
1313 | goto out; | |
ad97373b HS |
1314 | /* Disconnect controllers */ |
1315 | efi_disconnect_all_drivers(efiobj, protocol, NULL); | |
ad97373b HS |
1316 | /* Close protocol */ |
1317 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
1318 | if (item->info.attributes == | |
1319 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || | |
1320 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || | |
1321 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
1322 | list_del(&item->link); | |
1323 | } | |
1324 | if (!list_empty(&handler->open_infos)) { | |
1325 | r = EFI_ACCESS_DENIED; | |
1326 | goto out; | |
4b6ed0d7 | 1327 | } |
ad97373b | 1328 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
4b6ed0d7 | 1329 | out: |
9b47f13b HS |
1330 | return r; |
1331 | } | |
1332 | ||
1333 | /** | |
1334 | * efi_uninstall_protocol_interface() - uninstall protocol interface | |
1335 | * @handle: handle from which the protocol shall be removed | |
1336 | * @protocol: GUID of the protocol to be removed | |
1337 | * @protocol_interface: interface to be removed | |
1338 | * | |
1339 | * This function implements the UninstallProtocolInterface service. | |
1340 | * | |
1341 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
1342 | * details. | |
1343 | * | |
1344 | * Return: status code | |
1345 | */ | |
1346 | static efi_status_t EFIAPI efi_uninstall_protocol_interface | |
1347 | (efi_handle_t handle, const efi_guid_t *protocol, | |
1348 | void *protocol_interface) | |
1349 | { | |
1350 | efi_status_t ret; | |
1351 | ||
1352 | EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface); | |
1353 | ||
1354 | ret = efi_uninstall_protocol(handle, protocol, protocol_interface); | |
1355 | if (ret != EFI_SUCCESS) | |
1356 | goto out; | |
1357 | ||
1358 | /* If the last protocol has been removed, delete the handle. */ | |
1359 | if (list_empty(&handle->protocols)) { | |
1360 | list_del(&handle->link); | |
1361 | free(handle); | |
1362 | } | |
1363 | out: | |
1364 | return EFI_EXIT(ret); | |
bee91169 AG |
1365 | } |
1366 | ||
6b03cd10 | 1367 | /** |
78a88f79 MS |
1368 | * efi_register_protocol_notify() - register an event for notification when a |
1369 | * protocol is installed. | |
1370 | * @protocol: GUID of the protocol whose installation shall be notified | |
1371 | * @event: event to be signaled upon installation of the protocol | |
1372 | * @registration: key for retrieving the registration information | |
332468f7 HS |
1373 | * |
1374 | * This function implements the RegisterProtocolNotify service. | |
1375 | * See the Unified Extensible Firmware Interface (UEFI) specification | |
1376 | * for details. | |
1377 | * | |
78a88f79 | 1378 | * Return: status code |
332468f7 | 1379 | */ |
5a9682d0 HS |
1380 | static efi_status_t EFIAPI efi_register_protocol_notify( |
1381 | const efi_guid_t *protocol, | |
1382 | struct efi_event *event, | |
1383 | void **registration) | |
bee91169 | 1384 | { |
ab15d414 HS |
1385 | struct efi_register_notify_event *item; |
1386 | efi_status_t ret = EFI_SUCCESS; | |
1387 | ||
778e6af8 | 1388 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
ab15d414 HS |
1389 | |
1390 | if (!protocol || !event || !registration) { | |
1391 | ret = EFI_INVALID_PARAMETER; | |
1392 | goto out; | |
1393 | } | |
1394 | ||
1395 | item = calloc(1, sizeof(struct efi_register_notify_event)); | |
1396 | if (!item) { | |
1397 | ret = EFI_OUT_OF_RESOURCES; | |
1398 | goto out; | |
1399 | } | |
1400 | ||
1401 | item->event = event; | |
1402 | memcpy(&item->protocol, protocol, sizeof(efi_guid_t)); | |
f09cea36 | 1403 | INIT_LIST_HEAD(&item->handles); |
ab15d414 HS |
1404 | |
1405 | list_add_tail(&item->link, &efi_register_notify_events); | |
1406 | ||
1407 | *registration = item; | |
1408 | out: | |
1409 | return EFI_EXIT(ret); | |
bee91169 AG |
1410 | } |
1411 | ||
6b03cd10 | 1412 | /** |
78a88f79 MS |
1413 | * efi_search() - determine if an EFI handle implements a protocol |
1414 | * @search_type: selection criterion | |
1415 | * @protocol: GUID of the protocol | |
1416 | * @search_key: registration key | |
fae0118e | 1417 | * @handle: handle |
332468f7 HS |
1418 | * |
1419 | * See the documentation of the LocateHandle service in the UEFI specification. | |
1420 | * | |
78a88f79 | 1421 | * Return: 0 if the handle implements the protocol |
332468f7 | 1422 | */ |
bee91169 | 1423 | static int efi_search(enum efi_locate_search_type search_type, |
ab15d414 | 1424 | const efi_guid_t *protocol, efi_handle_t handle) |
bee91169 | 1425 | { |
42cf1242 | 1426 | efi_status_t ret; |
bee91169 AG |
1427 | |
1428 | switch (search_type) { | |
9f0770ff | 1429 | case ALL_HANDLES: |
bee91169 | 1430 | return 0; |
9f0770ff | 1431 | case BY_PROTOCOL: |
fae0118e | 1432 | ret = efi_search_protocol(handle, protocol, NULL); |
42cf1242 HS |
1433 | return (ret != EFI_SUCCESS); |
1434 | default: | |
1435 | /* Invalid search type */ | |
bee91169 AG |
1436 | return -1; |
1437 | } | |
bee91169 AG |
1438 | } |
1439 | ||
b8abd743 HS |
1440 | /** |
1441 | * efi_check_register_notify_event() - check if registration key is valid | |
1442 | * | |
1443 | * Check that a pointer is a valid registration key as returned by | |
1444 | * RegisterProtocolNotify(). | |
1445 | * | |
1446 | * @key: registration key | |
1447 | * Return: valid registration key or NULL | |
1448 | */ | |
1449 | static struct efi_register_notify_event *efi_check_register_notify_event | |
1450 | (void *key) | |
1451 | { | |
1452 | struct efi_register_notify_event *event; | |
1453 | ||
1454 | list_for_each_entry(event, &efi_register_notify_events, link) { | |
1455 | if (event == (struct efi_register_notify_event *)key) | |
1456 | return event; | |
1457 | } | |
1458 | return NULL; | |
1459 | } | |
1460 | ||
6b03cd10 | 1461 | /** |
78a88f79 | 1462 | * efi_locate_handle() - locate handles implementing a protocol |
ab15d414 HS |
1463 | * |
1464 | * @search_type: selection criterion | |
1465 | * @protocol: GUID of the protocol | |
1466 | * @search_key: registration key | |
1467 | * @buffer_size: size of the buffer to receive the handles in bytes | |
1468 | * @buffer: buffer to receive the relevant handles | |
332468f7 HS |
1469 | * |
1470 | * This function is meant for U-Boot internal calls. For the API implementation | |
1471 | * of the LocateHandle service see efi_locate_handle_ext. | |
1472 | * | |
78a88f79 | 1473 | * Return: status code |
332468f7 | 1474 | */ |
ebf199b9 | 1475 | static efi_status_t efi_locate_handle( |
bee91169 | 1476 | enum efi_locate_search_type search_type, |
5a9682d0 | 1477 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1478 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
bee91169 | 1479 | { |
caf864e4 | 1480 | struct efi_object *efiobj; |
f5a2a938 | 1481 | efi_uintn_t size = 0; |
b8abd743 | 1482 | struct efi_register_notify_event *event; |
f09cea36 | 1483 | struct efi_protocol_notification *handle = NULL; |
bee91169 | 1484 | |
caf864e4 HS |
1485 | /* Check parameters */ |
1486 | switch (search_type) { | |
1487 | case ALL_HANDLES: | |
1488 | break; | |
1489 | case BY_REGISTER_NOTIFY: | |
1490 | if (!search_key) | |
1491 | return EFI_INVALID_PARAMETER; | |
ab15d414 | 1492 | /* Check that the registration key is valid */ |
b8abd743 | 1493 | event = efi_check_register_notify_event(search_key); |
ab15d414 HS |
1494 | if (!event) |
1495 | return EFI_INVALID_PARAMETER; | |
ab15d414 | 1496 | break; |
caf864e4 HS |
1497 | case BY_PROTOCOL: |
1498 | if (!protocol) | |
1499 | return EFI_INVALID_PARAMETER; | |
1500 | break; | |
1501 | default: | |
1502 | return EFI_INVALID_PARAMETER; | |
1503 | } | |
1504 | ||
bee91169 | 1505 | /* Count how much space we need */ |
f09cea36 HS |
1506 | if (search_type == BY_REGISTER_NOTIFY) { |
1507 | if (list_empty(&event->handles)) | |
1508 | return EFI_NOT_FOUND; | |
1509 | handle = list_first_entry(&event->handles, | |
1510 | struct efi_protocol_notification, | |
1511 | link); | |
1512 | efiobj = handle->handle; | |
1513 | size += sizeof(void *); | |
1514 | } else { | |
1515 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
1516 | if (!efi_search(search_type, protocol, efiobj)) | |
1517 | size += sizeof(void *); | |
1518 | } | |
1519 | if (size == 0) | |
1520 | return EFI_NOT_FOUND; | |
bee91169 AG |
1521 | } |
1522 | ||
8dfb5e6e HS |
1523 | if (!buffer_size) |
1524 | return EFI_INVALID_PARAMETER; | |
1525 | ||
bee91169 AG |
1526 | if (*buffer_size < size) { |
1527 | *buffer_size = size; | |
26329584 | 1528 | return EFI_BUFFER_TOO_SMALL; |
bee91169 AG |
1529 | } |
1530 | ||
796a78cb | 1531 | *buffer_size = size; |
8dfb5e6e | 1532 | |
0a843196 | 1533 | /* The buffer size is sufficient but there is no buffer */ |
8dfb5e6e HS |
1534 | if (!buffer) |
1535 | return EFI_INVALID_PARAMETER; | |
796a78cb | 1536 | |
bee91169 | 1537 | /* Then fill the array */ |
f09cea36 HS |
1538 | if (search_type == BY_REGISTER_NOTIFY) { |
1539 | *buffer = efiobj; | |
1540 | list_del(&handle->link); | |
1541 | } else { | |
1542 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
1543 | if (!efi_search(search_type, protocol, efiobj)) | |
1544 | *buffer++ = efiobj; | |
1545 | } | |
bee91169 AG |
1546 | } |
1547 | ||
26329584 HS |
1548 | return EFI_SUCCESS; |
1549 | } | |
1550 | ||
6b03cd10 | 1551 | /** |
78a88f79 MS |
1552 | * efi_locate_handle_ext() - locate handles implementing a protocol. |
1553 | * @search_type: selection criterion | |
1554 | * @protocol: GUID of the protocol | |
1555 | * @search_key: registration key | |
1556 | * @buffer_size: size of the buffer to receive the handles in bytes | |
1557 | * @buffer: buffer to receive the relevant handles | |
332468f7 HS |
1558 | * |
1559 | * This function implements the LocateHandle service. | |
332468f7 | 1560 | * |
78a88f79 MS |
1561 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1562 | * details. | |
1563 | * | |
1564 | * Return: 0 if the handle implements the protocol | |
332468f7 | 1565 | */ |
26329584 HS |
1566 | static efi_status_t EFIAPI efi_locate_handle_ext( |
1567 | enum efi_locate_search_type search_type, | |
5a9682d0 | 1568 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 1569 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
26329584 | 1570 | { |
778e6af8 | 1571 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
26329584 HS |
1572 | buffer_size, buffer); |
1573 | ||
1574 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, | |
1575 | buffer_size, buffer)); | |
bee91169 AG |
1576 | } |
1577 | ||
6b03cd10 | 1578 | /** |
78a88f79 MS |
1579 | * efi_remove_configuration_table() - collapses configuration table entries, |
1580 | * removing index i | |
6b03cd10 | 1581 | * |
78a88f79 | 1582 | * @i: index of the table entry to be removed |
6b03cd10 | 1583 | */ |
d98cdf6a AG |
1584 | static void efi_remove_configuration_table(int i) |
1585 | { | |
4182a129 HS |
1586 | struct efi_configuration_table *this = &systab.tables[i]; |
1587 | struct efi_configuration_table *next = &systab.tables[i + 1]; | |
1588 | struct efi_configuration_table *end = &systab.tables[systab.nr_tables]; | |
d98cdf6a AG |
1589 | |
1590 | memmove(this, next, (ulong)end - (ulong)next); | |
1591 | systab.nr_tables--; | |
1592 | } | |
1593 | ||
6b03cd10 | 1594 | /** |
78a88f79 MS |
1595 | * efi_install_configuration_table() - adds, updates, or removes a |
1596 | * configuration table | |
1597 | * @guid: GUID of the installed table | |
1598 | * @table: table to be installed | |
332468f7 HS |
1599 | * |
1600 | * This function is used for internal calls. For the API implementation of the | |
1601 | * InstallConfigurationTable service see efi_install_configuration_table_ext. | |
1602 | * | |
78a88f79 | 1603 | * Return: status code |
332468f7 | 1604 | */ |
ab9efa97 HS |
1605 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, |
1606 | void *table) | |
bee91169 | 1607 | { |
b095f3c8 | 1608 | struct efi_event *evt; |
bee91169 AG |
1609 | int i; |
1610 | ||
eb68b4ef HS |
1611 | if (!guid) |
1612 | return EFI_INVALID_PARAMETER; | |
1613 | ||
b72aaa87 | 1614 | /* Check for GUID override */ |
bee91169 | 1615 | for (i = 0; i < systab.nr_tables; i++) { |
4182a129 | 1616 | if (!guidcmp(guid, &systab.tables[i].guid)) { |
d98cdf6a | 1617 | if (table) |
4182a129 | 1618 | systab.tables[i].table = table; |
d98cdf6a AG |
1619 | else |
1620 | efi_remove_configuration_table(i); | |
b095f3c8 | 1621 | goto out; |
bee91169 AG |
1622 | } |
1623 | } | |
1624 | ||
d98cdf6a AG |
1625 | if (!table) |
1626 | return EFI_NOT_FOUND; | |
1627 | ||
bee91169 | 1628 | /* No override, check for overflow */ |
4182a129 | 1629 | if (i >= EFI_MAX_CONFIGURATION_TABLES) |
488bf12d | 1630 | return EFI_OUT_OF_RESOURCES; |
bee91169 AG |
1631 | |
1632 | /* Add a new entry */ | |
4182a129 HS |
1633 | memcpy(&systab.tables[i].guid, guid, sizeof(*guid)); |
1634 | systab.tables[i].table = table; | |
aba5e919 | 1635 | systab.nr_tables = i + 1; |
bee91169 | 1636 | |
b095f3c8 | 1637 | out: |
b72aaa87 | 1638 | /* systab.nr_tables may have changed. So we need to update the CRC32 */ |
55d8ee3b HS |
1639 | efi_update_table_header_crc32(&systab.hdr); |
1640 | ||
b095f3c8 HS |
1641 | /* Notify that the configuration table was changed */ |
1642 | list_for_each_entry(evt, &efi_events, link) { | |
1643 | if (evt->group && !guidcmp(evt->group, guid)) { | |
7eaa900e | 1644 | efi_signal_event(evt); |
b095f3c8 HS |
1645 | break; |
1646 | } | |
1647 | } | |
1648 | ||
488bf12d AG |
1649 | return EFI_SUCCESS; |
1650 | } | |
1651 | ||
6b03cd10 | 1652 | /** |
78a88f79 MS |
1653 | * efi_install_configuration_table_ex() - Adds, updates, or removes a |
1654 | * configuration table. | |
1655 | * @guid: GUID of the installed table | |
1656 | * @table: table to be installed | |
332468f7 HS |
1657 | * |
1658 | * This function implements the InstallConfigurationTable service. | |
332468f7 | 1659 | * |
78a88f79 MS |
1660 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
1661 | * details. | |
1662 | * | |
1663 | * Return: status code | |
332468f7 | 1664 | */ |
488bf12d AG |
1665 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
1666 | void *table) | |
1667 | { | |
778e6af8 | 1668 | EFI_ENTRY("%pUl, %p", guid, table); |
488bf12d | 1669 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
bee91169 AG |
1670 | } |
1671 | ||
6b03cd10 | 1672 | /** |
78a88f79 | 1673 | * efi_setup_loaded_image() - initialize a loaded image |
6b03cd10 HS |
1674 | * |
1675 | * Initialize a loaded_image_info and loaded_image_info object with correct | |
95c5553e | 1676 | * protocols, boot-device, etc. |
332468f7 | 1677 | * |
16112f9f HS |
1678 | * In case of an error *handle_ptr and *info_ptr are set to NULL and an error |
1679 | * code is returned. | |
1680 | * | |
1681 | * @device_path: device path of the loaded image | |
1682 | * @file_path: file path of the loaded image | |
1683 | * @handle_ptr: handle of the loaded image | |
1684 | * @info_ptr: loaded image protocol | |
1685 | * Return: status code | |
95c5553e | 1686 | */ |
c982874e HS |
1687 | efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path, |
1688 | struct efi_device_path *file_path, | |
1689 | struct efi_loaded_image_obj **handle_ptr, | |
1690 | struct efi_loaded_image **info_ptr) | |
95c5553e | 1691 | { |
48b66230 | 1692 | efi_status_t ret; |
16112f9f HS |
1693 | struct efi_loaded_image *info = NULL; |
1694 | struct efi_loaded_image_obj *obj = NULL; | |
bc8fc328 | 1695 | struct efi_device_path *dp; |
16112f9f HS |
1696 | |
1697 | /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */ | |
1698 | *handle_ptr = NULL; | |
1699 | *info_ptr = NULL; | |
c982874e HS |
1700 | |
1701 | info = calloc(1, sizeof(*info)); | |
1702 | if (!info) | |
1703 | return EFI_OUT_OF_RESOURCES; | |
1704 | obj = calloc(1, sizeof(*obj)); | |
1705 | if (!obj) { | |
1706 | free(info); | |
1707 | return EFI_OUT_OF_RESOURCES; | |
1708 | } | |
cd73aba6 | 1709 | obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE; |
48b66230 | 1710 | |
44549d62 | 1711 | /* Add internal object to object list */ |
d39646a3 | 1712 | efi_add_handle(&obj->header); |
c982874e | 1713 | |
95147313 | 1714 | info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; |
48b66230 | 1715 | info->file_path = file_path; |
7e1effce | 1716 | info->system_table = &systab; |
48b66230 | 1717 | |
7df5af6f HS |
1718 | if (device_path) { |
1719 | info->device_handle = efi_dp_find_obj(device_path, NULL); | |
bc8fc328 AT |
1720 | |
1721 | dp = efi_dp_append(device_path, file_path); | |
1722 | if (!dp) { | |
1723 | ret = EFI_OUT_OF_RESOURCES; | |
7df5af6f | 1724 | goto failure; |
bc8fc328 AT |
1725 | } |
1726 | } else { | |
1727 | dp = NULL; | |
7df5af6f | 1728 | } |
bc8fc328 AT |
1729 | ret = efi_add_protocol(&obj->header, |
1730 | &efi_guid_loaded_image_device_path, dp); | |
1731 | if (ret != EFI_SUCCESS) | |
1732 | goto failure; | |
95c5553e RC |
1733 | |
1734 | /* | |
1735 | * When asking for the loaded_image interface, just | |
1736 | * return handle which points to loaded_image_info | |
1737 | */ | |
d39646a3 | 1738 | ret = efi_add_protocol(&obj->header, |
c982874e | 1739 | &efi_guid_loaded_image, info); |
48b66230 HS |
1740 | if (ret != EFI_SUCCESS) |
1741 | goto failure; | |
95c5553e | 1742 | |
d5974af7 HS |
1743 | *info_ptr = info; |
1744 | *handle_ptr = obj; | |
16112f9f | 1745 | |
56d92888 | 1746 | return ret; |
48b66230 HS |
1747 | failure: |
1748 | printf("ERROR: Failure to install protocols for loaded image\n"); | |
16112f9f HS |
1749 | efi_delete_handle(&obj->header); |
1750 | free(info); | |
56d92888 | 1751 | return ret; |
95c5553e RC |
1752 | } |
1753 | ||
6b03cd10 | 1754 | /** |
78a88f79 | 1755 | * efi_load_image_from_path() - load an image using a file path |
332468f7 | 1756 | * |
0e18f584 HS |
1757 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
1758 | * callers obligation to update the memory type as needed. | |
1759 | * | |
1760 | * @file_path: the path of the image to load | |
1761 | * @buffer: buffer containing the loaded image | |
1762 | * @size: size of the loaded image | |
1763 | * Return: status code | |
332468f7 | 1764 | */ |
6b95b38c | 1765 | static |
9975fe96 | 1766 | efi_status_t efi_load_image_from_path(struct efi_device_path *file_path, |
0e18f584 | 1767 | void **buffer, efi_uintn_t *size) |
838ee4b4 RC |
1768 | { |
1769 | struct efi_file_info *info = NULL; | |
1770 | struct efi_file_handle *f; | |
1771 | static efi_status_t ret; | |
0e18f584 | 1772 | u64 addr; |
b6dd5777 | 1773 | efi_uintn_t bs; |
838ee4b4 | 1774 | |
0e18f584 HS |
1775 | /* In case of failure nothing is returned */ |
1776 | *buffer = NULL; | |
1777 | *size = 0; | |
1778 | ||
1779 | /* Open file */ | |
838ee4b4 RC |
1780 | f = efi_file_from_path(file_path); |
1781 | if (!f) | |
20000038 | 1782 | return EFI_NOT_FOUND; |
838ee4b4 | 1783 | |
0e18f584 | 1784 | /* Get file size */ |
838ee4b4 RC |
1785 | bs = 0; |
1786 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, | |
1787 | &bs, info)); | |
0e18f584 HS |
1788 | if (ret != EFI_BUFFER_TOO_SMALL) { |
1789 | ret = EFI_DEVICE_ERROR; | |
838ee4b4 | 1790 | goto error; |
0e18f584 | 1791 | } |
838ee4b4 | 1792 | |
0e18f584 HS |
1793 | info = malloc(bs); |
1794 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs, | |
1795 | info)); | |
1796 | if (ret != EFI_SUCCESS) | |
838ee4b4 RC |
1797 | goto error; |
1798 | ||
0e18f584 HS |
1799 | /* |
1800 | * When reading the file we do not yet know if it contains an | |
1801 | * application, a boottime driver, or a runtime driver. So here we | |
1802 | * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to | |
1803 | * update the reservation according to the image type. | |
1804 | */ | |
b6dd5777 | 1805 | bs = info->file_size; |
0e18f584 HS |
1806 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
1807 | EFI_BOOT_SERVICES_DATA, | |
1808 | efi_size_in_pages(bs), &addr); | |
838ee4b4 | 1809 | if (ret != EFI_SUCCESS) { |
0e18f584 HS |
1810 | ret = EFI_OUT_OF_RESOURCES; |
1811 | goto error; | |
838ee4b4 RC |
1812 | } |
1813 | ||
0e18f584 HS |
1814 | /* Read file */ |
1815 | EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr)); | |
1816 | if (ret != EFI_SUCCESS) | |
1817 | efi_free_pages(addr, efi_size_in_pages(bs)); | |
1818 | *buffer = (void *)(uintptr_t)addr; | |
1819 | *size = bs; | |
1820 | error: | |
1821 | EFI_CALL(f->close(f)); | |
1822 | free(info); | |
838ee4b4 RC |
1823 | return ret; |
1824 | } | |
1825 | ||
6b03cd10 | 1826 | /** |
78a88f79 MS |
1827 | * efi_load_image() - load an EFI image into memory |
1828 | * @boot_policy: true for request originating from the boot manager | |
1829 | * @parent_image: the caller's image handle | |
1830 | * @file_path: the path of the image to load | |
1831 | * @source_buffer: memory location from which the image is installed | |
1832 | * @source_size: size of the memory area from which the image is installed | |
1833 | * @image_handle: handle for the newly installed image | |
332468f7 HS |
1834 | * |
1835 | * This function implements the LoadImage service. | |
78a88f79 | 1836 | * |
332468f7 HS |
1837 | * See the Unified Extensible Firmware Interface (UEFI) specification |
1838 | * for details. | |
1839 | * | |
78a88f79 | 1840 | * Return: status code |
332468f7 | 1841 | */ |
d7e0b010 AT |
1842 | efi_status_t EFIAPI efi_load_image(bool boot_policy, |
1843 | efi_handle_t parent_image, | |
1844 | struct efi_device_path *file_path, | |
1845 | void *source_buffer, | |
1846 | efi_uintn_t source_size, | |
1847 | efi_handle_t *image_handle) | |
bee91169 | 1848 | { |
0e18f584 | 1849 | struct efi_device_path *dp, *fp; |
1c3b2f4a | 1850 | struct efi_loaded_image *info = NULL; |
c982874e HS |
1851 | struct efi_loaded_image_obj **image_obj = |
1852 | (struct efi_loaded_image_obj **)image_handle; | |
b9b17598 | 1853 | efi_status_t ret; |
b0c3c346 | 1854 | void *dest_buffer; |
bee91169 | 1855 | |
7fb96a10 | 1856 | EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image, |
bee91169 | 1857 | file_path, source_buffer, source_size, image_handle); |
838ee4b4 | 1858 | |
e4afcb28 HS |
1859 | if (!image_handle || (!source_buffer && !file_path) || |
1860 | !efi_search_obj(parent_image) || | |
1861 | /* The parent image handle must refer to a loaded image */ | |
1862 | !parent_image->type) { | |
84a918e8 HS |
1863 | ret = EFI_INVALID_PARAMETER; |
1864 | goto error; | |
1865 | } | |
28a4fd46 | 1866 | |
838ee4b4 | 1867 | if (!source_buffer) { |
b0c3c346 | 1868 | ret = efi_load_image_from_path(file_path, &dest_buffer, |
0e18f584 | 1869 | &source_size); |
b9b17598 | 1870 | if (ret != EFI_SUCCESS) |
0e18f584 | 1871 | goto error; |
838ee4b4 | 1872 | } else { |
470dfa50 HS |
1873 | if (!source_size) { |
1874 | ret = EFI_LOAD_ERROR; | |
1875 | goto error; | |
1876 | } | |
b0c3c346 | 1877 | dest_buffer = source_buffer; |
838ee4b4 | 1878 | } |
1e15a9cb HS |
1879 | /* split file_path which contains both the device and file parts */ |
1880 | efi_dp_split_file_path(file_path, &dp, &fp); | |
0e18f584 | 1881 | ret = efi_setup_loaded_image(dp, fp, image_obj, &info); |
b0c3c346 HS |
1882 | if (ret == EFI_SUCCESS) |
1883 | ret = efi_load_pe(*image_obj, dest_buffer, info); | |
1884 | if (!source_buffer) | |
1885 | /* Release buffer to which file was loaded */ | |
1886 | efi_free_pages((uintptr_t)dest_buffer, | |
1887 | efi_size_in_pages(source_size)); | |
1888 | if (ret == EFI_SUCCESS) { | |
1889 | info->system_table = &systab; | |
1890 | info->parent_handle = parent_image; | |
1891 | } else { | |
1892 | /* The image is invalid. Release all associated resources. */ | |
1893 | efi_delete_handle(*image_handle); | |
1894 | *image_handle = NULL; | |
1895 | free(info); | |
1896 | } | |
28a4fd46 | 1897 | error: |
b9b17598 | 1898 | return EFI_EXIT(ret); |
bee91169 AG |
1899 | } |
1900 | ||
f31239ac AG |
1901 | /** |
1902 | * efi_exit_caches() - fix up caches for EFI payloads if necessary | |
1903 | */ | |
1904 | static void efi_exit_caches(void) | |
1905 | { | |
1906 | #if defined(CONFIG_ARM) && !defined(CONFIG_ARM64) | |
1907 | /* | |
1908 | * Grub on 32bit ARM needs to have caches disabled before jumping into | |
1909 | * a zImage, but does not know of all cache layers. Give it a hand. | |
1910 | */ | |
1911 | if (efi_is_direct_boot) | |
1912 | cleanup_before_linux(); | |
1913 | #endif | |
1914 | } | |
1915 | ||
6b03cd10 | 1916 | /** |
78a88f79 MS |
1917 | * efi_exit_boot_services() - stop all boot services |
1918 | * @image_handle: handle of the loaded image | |
1919 | * @map_key: key of the memory map | |
332468f7 HS |
1920 | * |
1921 | * This function implements the ExitBootServices service. | |
78a88f79 | 1922 | * |
332468f7 HS |
1923 | * See the Unified Extensible Firmware Interface (UEFI) specification |
1924 | * for details. | |
1925 | * | |
78a88f79 MS |
1926 | * All timer events are disabled. For exit boot services events the |
1927 | * notification function is called. The boot services are disabled in the | |
1928 | * system table. | |
cc20ed03 | 1929 | * |
78a88f79 | 1930 | * Return: status code |
332468f7 | 1931 | */ |
2074f700 | 1932 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
b015ab57 | 1933 | efi_uintn_t map_key) |
bee91169 | 1934 | { |
43bce442 | 1935 | struct efi_event *evt; |
98967379 | 1936 | efi_status_t ret = EFI_SUCCESS; |
152a263c | 1937 | |
b015ab57 | 1938 | EFI_ENTRY("%p, %zx", image_handle, map_key); |
bee91169 | 1939 | |
1fcb7ea2 | 1940 | /* Check that the caller has read the current memory map */ |
98967379 HS |
1941 | if (map_key != efi_memory_map_key) { |
1942 | ret = EFI_INVALID_PARAMETER; | |
1943 | goto out; | |
1944 | } | |
1fcb7ea2 | 1945 | |
cc20ed03 HS |
1946 | /* Check if ExitBootServices has already been called */ |
1947 | if (!systab.boottime) | |
98967379 | 1948 | goto out; |
cc20ed03 | 1949 | |
7eaa900e HS |
1950 | /* Stop all timer related activities */ |
1951 | timers_enabled = false; | |
1952 | ||
b095f3c8 HS |
1953 | /* Add related events to the event group */ |
1954 | list_for_each_entry(evt, &efi_events, link) { | |
1955 | if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES) | |
1956 | evt->group = &efi_guid_event_group_exit_boot_services; | |
1957 | } | |
152a263c | 1958 | /* Notify that ExitBootServices is invoked. */ |
43bce442 | 1959 | list_for_each_entry(evt, &efi_events, link) { |
b095f3c8 HS |
1960 | if (evt->group && |
1961 | !guidcmp(evt->group, | |
1962 | &efi_guid_event_group_exit_boot_services)) { | |
7eaa900e | 1963 | efi_signal_event(evt); |
b095f3c8 HS |
1964 | break; |
1965 | } | |
152a263c | 1966 | } |
152a263c | 1967 | |
7eaa900e HS |
1968 | /* Make sure that notification functions are not called anymore */ |
1969 | efi_tpl = TPL_HIGH_LEVEL; | |
1970 | ||
29018abb HS |
1971 | /* Notify variable services */ |
1972 | efi_variables_boot_exit_notify(); | |
ad644e7c | 1973 | |
b7b8410a AG |
1974 | board_quiesce_devices(); |
1975 | ||
f31239ac AG |
1976 | /* Fix up caches for EFI payloads if necessary */ |
1977 | efi_exit_caches(); | |
1978 | ||
bee91169 AG |
1979 | /* This stops all lingering devices */ |
1980 | bootm_disable_interrupts(); | |
1981 | ||
b72aaa87 | 1982 | /* Disable boot time services */ |
cc20ed03 HS |
1983 | systab.con_in_handle = NULL; |
1984 | systab.con_in = NULL; | |
1985 | systab.con_out_handle = NULL; | |
1986 | systab.con_out = NULL; | |
1987 | systab.stderr_handle = NULL; | |
1988 | systab.std_err = NULL; | |
1989 | systab.boottime = NULL; | |
1990 | ||
1991 | /* Recalculate CRC32 */ | |
640adadf | 1992 | efi_update_table_header_crc32(&systab.hdr); |
cc20ed03 | 1993 | |
bee91169 | 1994 | /* Give the payload some time to boot */ |
b3d60900 | 1995 | efi_set_watchdog(0); |
bee91169 | 1996 | WATCHDOG_RESET(); |
98967379 HS |
1997 | out: |
1998 | return EFI_EXIT(ret); | |
bee91169 AG |
1999 | } |
2000 | ||
6b03cd10 | 2001 | /** |
78a88f79 MS |
2002 | * efi_get_next_monotonic_count() - get next value of the counter |
2003 | * @count: returned value of the counter | |
332468f7 HS |
2004 | * |
2005 | * This function implements the NextMonotonicCount service. | |
332468f7 | 2006 | * |
78a88f79 MS |
2007 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2008 | * details. | |
2009 | * | |
2010 | * Return: status code | |
332468f7 | 2011 | */ |
bee91169 AG |
2012 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
2013 | { | |
ab9efa97 | 2014 | static uint64_t mono; |
1344f7d0 | 2015 | efi_status_t ret; |
ab9efa97 | 2016 | |
bee91169 | 2017 | EFI_ENTRY("%p", count); |
1344f7d0 HS |
2018 | if (!count) { |
2019 | ret = EFI_INVALID_PARAMETER; | |
2020 | goto out; | |
2021 | } | |
bee91169 | 2022 | *count = mono++; |
1344f7d0 HS |
2023 | ret = EFI_SUCCESS; |
2024 | out: | |
2025 | return EFI_EXIT(ret); | |
bee91169 AG |
2026 | } |
2027 | ||
6b03cd10 | 2028 | /** |
78a88f79 MS |
2029 | * efi_stall() - sleep |
2030 | * @microseconds: period to sleep in microseconds | |
332468f7 | 2031 | * |
78a88f79 | 2032 | * This function implements the Stall service. |
332468f7 | 2033 | * |
78a88f79 MS |
2034 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2035 | * details. | |
2036 | * | |
2037 | * Return: status code | |
332468f7 | 2038 | */ |
bee91169 AG |
2039 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
2040 | { | |
22f23db4 HS |
2041 | u64 end_tick; |
2042 | ||
bee91169 | 2043 | EFI_ENTRY("%ld", microseconds); |
22f23db4 HS |
2044 | |
2045 | end_tick = get_ticks() + usec_to_tick(microseconds); | |
2046 | while (get_ticks() < end_tick) | |
2047 | efi_timer_check(); | |
2048 | ||
bee91169 AG |
2049 | return EFI_EXIT(EFI_SUCCESS); |
2050 | } | |
2051 | ||
6b03cd10 | 2052 | /** |
78a88f79 MS |
2053 | * efi_set_watchdog_timer() - reset the watchdog timer |
2054 | * @timeout: seconds before reset by watchdog | |
2055 | * @watchdog_code: code to be logged when resetting | |
2056 | * @data_size: size of buffer in bytes | |
2057 | * @watchdog_data: buffer with data describing the reset reason | |
332468f7 | 2058 | * |
b3d60900 | 2059 | * This function implements the SetWatchdogTimer service. |
332468f7 | 2060 | * |
78a88f79 MS |
2061 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2062 | * details. | |
2063 | * | |
2064 | * Return: status code | |
332468f7 | 2065 | */ |
bee91169 AG |
2066 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
2067 | uint64_t watchdog_code, | |
2068 | unsigned long data_size, | |
2069 | uint16_t *watchdog_data) | |
2070 | { | |
dee37fc9 | 2071 | EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code, |
bee91169 | 2072 | data_size, watchdog_data); |
b3d60900 | 2073 | return EFI_EXIT(efi_set_watchdog(timeout)); |
bee91169 AG |
2074 | } |
2075 | ||
6b03cd10 | 2076 | /** |
78a88f79 MS |
2077 | * efi_close_protocol() - close a protocol |
2078 | * @handle: handle on which the protocol shall be closed | |
2079 | * @protocol: GUID of the protocol to close | |
2080 | * @agent_handle: handle of the driver | |
2081 | * @controller_handle: handle of the controller | |
332468f7 HS |
2082 | * |
2083 | * This function implements the CloseProtocol service. | |
332468f7 | 2084 | * |
78a88f79 MS |
2085 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2086 | * details. | |
2087 | * | |
2088 | * Return: status code | |
332468f7 | 2089 | */ |
2074f700 | 2090 | static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
5a9682d0 | 2091 | const efi_guid_t *protocol, |
2074f700 HS |
2092 | efi_handle_t agent_handle, |
2093 | efi_handle_t controller_handle) | |
bee91169 | 2094 | { |
3b8a489c HS |
2095 | struct efi_handler *handler; |
2096 | struct efi_open_protocol_info_item *item; | |
2097 | struct efi_open_protocol_info_item *pos; | |
2098 | efi_status_t r; | |
2099 | ||
778e6af8 | 2100 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
bee91169 | 2101 | controller_handle); |
3b8a489c | 2102 | |
ec163faa HS |
2103 | if (!efi_search_obj(agent_handle) || |
2104 | (controller_handle && !efi_search_obj(controller_handle))) { | |
3b8a489c HS |
2105 | r = EFI_INVALID_PARAMETER; |
2106 | goto out; | |
2107 | } | |
2108 | r = efi_search_protocol(handle, protocol, &handler); | |
2109 | if (r != EFI_SUCCESS) | |
2110 | goto out; | |
2111 | ||
2112 | r = EFI_NOT_FOUND; | |
2113 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { | |
2114 | if (item->info.agent_handle == agent_handle && | |
2115 | item->info.controller_handle == controller_handle) { | |
2116 | efi_delete_open_info(item); | |
2117 | r = EFI_SUCCESS; | |
3b8a489c HS |
2118 | } |
2119 | } | |
2120 | out: | |
2121 | return EFI_EXIT(r); | |
bee91169 AG |
2122 | } |
2123 | ||
6b03cd10 | 2124 | /** |
78a88f79 MS |
2125 | * efi_open_protocol_information() - provide information about then open status |
2126 | * of a protocol on a handle | |
2127 | * @handle: handle for which the information shall be retrieved | |
2128 | * @protocol: GUID of the protocol | |
2129 | * @entry_buffer: buffer to receive the open protocol information | |
2130 | * @entry_count: number of entries available in the buffer | |
332468f7 HS |
2131 | * |
2132 | * This function implements the OpenProtocolInformation service. | |
332468f7 | 2133 | * |
78a88f79 MS |
2134 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2135 | * details. | |
2136 | * | |
2137 | * Return: status code | |
332468f7 | 2138 | */ |
ab9efa97 HS |
2139 | static efi_status_t EFIAPI efi_open_protocol_information( |
2140 | efi_handle_t handle, const efi_guid_t *protocol, | |
bee91169 | 2141 | struct efi_open_protocol_info_entry **entry_buffer, |
f5a2a938 | 2142 | efi_uintn_t *entry_count) |
bee91169 | 2143 | { |
e3fbbc36 HS |
2144 | unsigned long buffer_size; |
2145 | unsigned long count; | |
2146 | struct efi_handler *handler; | |
2147 | struct efi_open_protocol_info_item *item; | |
2148 | efi_status_t r; | |
2149 | ||
778e6af8 | 2150 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
bee91169 | 2151 | entry_count); |
e3fbbc36 HS |
2152 | |
2153 | /* Check parameters */ | |
2154 | if (!entry_buffer) { | |
2155 | r = EFI_INVALID_PARAMETER; | |
2156 | goto out; | |
2157 | } | |
2158 | r = efi_search_protocol(handle, protocol, &handler); | |
2159 | if (r != EFI_SUCCESS) | |
2160 | goto out; | |
2161 | ||
2162 | /* Count entries */ | |
2163 | count = 0; | |
2164 | list_for_each_entry(item, &handler->open_infos, link) { | |
2165 | if (item->info.open_count) | |
2166 | ++count; | |
2167 | } | |
2168 | *entry_count = count; | |
2169 | *entry_buffer = NULL; | |
2170 | if (!count) { | |
2171 | r = EFI_SUCCESS; | |
2172 | goto out; | |
2173 | } | |
2174 | ||
2175 | /* Copy entries */ | |
2176 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); | |
eee6530e | 2177 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
e3fbbc36 HS |
2178 | (void **)entry_buffer); |
2179 | if (r != EFI_SUCCESS) | |
2180 | goto out; | |
2181 | list_for_each_entry_reverse(item, &handler->open_infos, link) { | |
2182 | if (item->info.open_count) | |
2183 | (*entry_buffer)[--count] = item->info; | |
2184 | } | |
2185 | out: | |
2186 | return EFI_EXIT(r); | |
bee91169 AG |
2187 | } |
2188 | ||
6b03cd10 | 2189 | /** |
78a88f79 MS |
2190 | * efi_protocols_per_handle() - get protocols installed on a handle |
2191 | * @handle: handle for which the information is retrieved | |
2192 | * @protocol_buffer: buffer with protocol GUIDs | |
2193 | * @protocol_buffer_count: number of entries in the buffer | |
332468f7 HS |
2194 | * |
2195 | * This function implements the ProtocolsPerHandleService. | |
332468f7 | 2196 | * |
78a88f79 MS |
2197 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2198 | * details. | |
2199 | * | |
2200 | * Return: status code | |
332468f7 | 2201 | */ |
2074f700 HS |
2202 | static efi_status_t EFIAPI efi_protocols_per_handle( |
2203 | efi_handle_t handle, efi_guid_t ***protocol_buffer, | |
f5a2a938 | 2204 | efi_uintn_t *protocol_buffer_count) |
bee91169 | 2205 | { |
c0ebfc86 HS |
2206 | unsigned long buffer_size; |
2207 | struct efi_object *efiobj; | |
69fb6b1a | 2208 | struct list_head *protocol_handle; |
c0ebfc86 HS |
2209 | efi_status_t r; |
2210 | ||
bee91169 AG |
2211 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
2212 | protocol_buffer_count); | |
c0ebfc86 HS |
2213 | |
2214 | if (!handle || !protocol_buffer || !protocol_buffer_count) | |
2215 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2216 | ||
2217 | *protocol_buffer = NULL; | |
661c8327 | 2218 | *protocol_buffer_count = 0; |
c0ebfc86 | 2219 | |
69fb6b1a HS |
2220 | efiobj = efi_search_obj(handle); |
2221 | if (!efiobj) | |
2222 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
c0ebfc86 | 2223 | |
69fb6b1a HS |
2224 | /* Count protocols */ |
2225 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2226 | ++*protocol_buffer_count; | |
2227 | } | |
2228 | ||
b72aaa87 | 2229 | /* Copy GUIDs */ |
69fb6b1a HS |
2230 | if (*protocol_buffer_count) { |
2231 | size_t j = 0; | |
2232 | ||
2233 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; | |
eee6530e | 2234 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
69fb6b1a HS |
2235 | (void **)protocol_buffer); |
2236 | if (r != EFI_SUCCESS) | |
2237 | return EFI_EXIT(r); | |
2238 | list_for_each(protocol_handle, &efiobj->protocols) { | |
2239 | struct efi_handler *protocol; | |
2240 | ||
2241 | protocol = list_entry(protocol_handle, | |
2242 | struct efi_handler, link); | |
2243 | (*protocol_buffer)[j] = (void *)protocol->guid; | |
2244 | ++j; | |
c0ebfc86 | 2245 | } |
c0ebfc86 HS |
2246 | } |
2247 | ||
2248 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2249 | } |
2250 | ||
6b03cd10 | 2251 | /** |
78a88f79 MS |
2252 | * efi_locate_handle_buffer() - locate handles implementing a protocol |
2253 | * @search_type: selection criterion | |
2254 | * @protocol: GUID of the protocol | |
2255 | * @search_key: registration key | |
2256 | * @no_handles: number of returned handles | |
2257 | * @buffer: buffer with the returned handles | |
332468f7 HS |
2258 | * |
2259 | * This function implements the LocateHandleBuffer service. | |
332468f7 | 2260 | * |
78a88f79 MS |
2261 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2262 | * details. | |
2263 | * | |
2264 | * Return: status code | |
332468f7 | 2265 | */ |
bee91169 AG |
2266 | static efi_status_t EFIAPI efi_locate_handle_buffer( |
2267 | enum efi_locate_search_type search_type, | |
5a9682d0 | 2268 | const efi_guid_t *protocol, void *search_key, |
f5a2a938 | 2269 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
bee91169 | 2270 | { |
c2e703f9 | 2271 | efi_status_t r; |
f5a2a938 | 2272 | efi_uintn_t buffer_size = 0; |
c2e703f9 | 2273 | |
778e6af8 | 2274 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
bee91169 | 2275 | no_handles, buffer); |
c2e703f9 HS |
2276 | |
2277 | if (!no_handles || !buffer) { | |
2278 | r = EFI_INVALID_PARAMETER; | |
2279 | goto out; | |
2280 | } | |
2281 | *no_handles = 0; | |
2282 | *buffer = NULL; | |
2283 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2284 | *buffer); | |
2285 | if (r != EFI_BUFFER_TOO_SMALL) | |
2286 | goto out; | |
eee6530e | 2287 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
c2e703f9 HS |
2288 | (void **)buffer); |
2289 | if (r != EFI_SUCCESS) | |
2290 | goto out; | |
2291 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, | |
2292 | *buffer); | |
2293 | if (r == EFI_SUCCESS) | |
2074f700 | 2294 | *no_handles = buffer_size / sizeof(efi_handle_t); |
c2e703f9 HS |
2295 | out: |
2296 | return EFI_EXIT(r); | |
bee91169 AG |
2297 | } |
2298 | ||
6b03cd10 | 2299 | /** |
78a88f79 MS |
2300 | * efi_locate_protocol() - find an interface implementing a protocol |
2301 | * @protocol: GUID of the protocol | |
2302 | * @registration: registration key passed to the notification function | |
2303 | * @protocol_interface: interface implementing the protocol | |
332468f7 HS |
2304 | * |
2305 | * This function implements the LocateProtocol service. | |
332468f7 | 2306 | * |
78a88f79 MS |
2307 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2308 | * details. | |
2309 | * | |
2310 | * Return: status code | |
332468f7 | 2311 | */ |
5a9682d0 | 2312 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
bee91169 AG |
2313 | void *registration, |
2314 | void **protocol_interface) | |
2315 | { | |
e31b3b16 | 2316 | struct efi_handler *handler; |
9172cd91 | 2317 | efi_status_t ret; |
e31b3b16 | 2318 | struct efi_object *efiobj; |
bee91169 | 2319 | |
778e6af8 | 2320 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
88adae5e | 2321 | |
e31b3b16 HS |
2322 | /* |
2323 | * The UEFI spec explicitly requires a protocol even if a registration | |
2324 | * key is provided. This differs from the logic in LocateHandle(). | |
2325 | */ | |
88adae5e HS |
2326 | if (!protocol || !protocol_interface) |
2327 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2328 | ||
e31b3b16 HS |
2329 | if (registration) { |
2330 | struct efi_register_notify_event *event; | |
2331 | struct efi_protocol_notification *handle; | |
9172cd91 | 2332 | |
e31b3b16 HS |
2333 | event = efi_check_register_notify_event(registration); |
2334 | if (!event) | |
2335 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2336 | /* | |
2337 | * The UEFI spec requires to return EFI_NOT_FOUND if no | |
2338 | * protocol instance matches protocol and registration. | |
2339 | * So let's do the same for a mismatch between protocol and | |
2340 | * registration. | |
2341 | */ | |
2342 | if (guidcmp(&event->protocol, protocol)) | |
2343 | goto not_found; | |
2344 | if (list_empty(&event->handles)) | |
2345 | goto not_found; | |
2346 | handle = list_first_entry(&event->handles, | |
2347 | struct efi_protocol_notification, | |
2348 | link); | |
2349 | efiobj = handle->handle; | |
2350 | list_del(&handle->link); | |
2351 | free(handle); | |
fae0118e | 2352 | ret = efi_search_protocol(efiobj, protocol, &handler); |
e31b3b16 HS |
2353 | if (ret == EFI_SUCCESS) |
2354 | goto found; | |
2355 | } else { | |
2356 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
2357 | ret = efi_search_protocol(efiobj, protocol, &handler); | |
2358 | if (ret == EFI_SUCCESS) | |
2359 | goto found; | |
bee91169 AG |
2360 | } |
2361 | } | |
e31b3b16 | 2362 | not_found: |
88adae5e | 2363 | *protocol_interface = NULL; |
bee91169 | 2364 | return EFI_EXIT(EFI_NOT_FOUND); |
e31b3b16 HS |
2365 | found: |
2366 | *protocol_interface = handler->protocol_interface; | |
2367 | return EFI_EXIT(EFI_SUCCESS); | |
bee91169 AG |
2368 | } |
2369 | ||
6b03cd10 | 2370 | /** |
78a88f79 MS |
2371 | * efi_locate_device_path() - Get the device path and handle of an device |
2372 | * implementing a protocol | |
2373 | * @protocol: GUID of the protocol | |
2374 | * @device_path: device path | |
2375 | * @device: handle of the device | |
ae2c85c1 HS |
2376 | * |
2377 | * This function implements the LocateDevicePath service. | |
ae2c85c1 | 2378 | * |
78a88f79 MS |
2379 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2380 | * details. | |
2381 | * | |
2382 | * Return: status code | |
ae2c85c1 HS |
2383 | */ |
2384 | static efi_status_t EFIAPI efi_locate_device_path( | |
2385 | const efi_guid_t *protocol, | |
2386 | struct efi_device_path **device_path, | |
2387 | efi_handle_t *device) | |
2388 | { | |
2389 | struct efi_device_path *dp; | |
2390 | size_t i; | |
2391 | struct efi_handler *handler; | |
2392 | efi_handle_t *handles; | |
2393 | size_t len, len_dp; | |
2394 | size_t len_best = 0; | |
2395 | efi_uintn_t no_handles; | |
2396 | u8 *remainder; | |
2397 | efi_status_t ret; | |
2398 | ||
2399 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); | |
2400 | ||
ab557141 | 2401 | if (!protocol || !device_path || !*device_path) { |
ae2c85c1 HS |
2402 | ret = EFI_INVALID_PARAMETER; |
2403 | goto out; | |
2404 | } | |
2405 | ||
2406 | /* Find end of device path */ | |
f6dd3f35 | 2407 | len = efi_dp_instance_size(*device_path); |
ae2c85c1 HS |
2408 | |
2409 | /* Get all handles implementing the protocol */ | |
2410 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, | |
2411 | &no_handles, &handles)); | |
2412 | if (ret != EFI_SUCCESS) | |
2413 | goto out; | |
2414 | ||
2415 | for (i = 0; i < no_handles; ++i) { | |
2416 | /* Find the device path protocol */ | |
2417 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, | |
2418 | &handler); | |
2419 | if (ret != EFI_SUCCESS) | |
2420 | continue; | |
2421 | dp = (struct efi_device_path *)handler->protocol_interface; | |
f6dd3f35 | 2422 | len_dp = efi_dp_instance_size(dp); |
ae2c85c1 HS |
2423 | /* |
2424 | * This handle can only be a better fit | |
2425 | * if its device path length is longer than the best fit and | |
2426 | * if its device path length is shorter of equal the searched | |
2427 | * device path. | |
2428 | */ | |
2429 | if (len_dp <= len_best || len_dp > len) | |
2430 | continue; | |
2431 | /* Check if dp is a subpath of device_path */ | |
2432 | if (memcmp(*device_path, dp, len_dp)) | |
2433 | continue; | |
ab557141 HS |
2434 | if (!device) { |
2435 | ret = EFI_INVALID_PARAMETER; | |
2436 | goto out; | |
2437 | } | |
ae2c85c1 HS |
2438 | *device = handles[i]; |
2439 | len_best = len_dp; | |
2440 | } | |
2441 | if (len_best) { | |
2442 | remainder = (u8 *)*device_path + len_best; | |
2443 | *device_path = (struct efi_device_path *)remainder; | |
2444 | ret = EFI_SUCCESS; | |
2445 | } else { | |
2446 | ret = EFI_NOT_FOUND; | |
2447 | } | |
2448 | out: | |
2449 | return EFI_EXIT(ret); | |
2450 | } | |
2451 | ||
6b03cd10 | 2452 | /** |
78a88f79 MS |
2453 | * efi_install_multiple_protocol_interfaces() - Install multiple protocol |
2454 | * interfaces | |
2455 | * @handle: handle on which the protocol interfaces shall be installed | |
2456 | * @...: NULL terminated argument list with pairs of protocol GUIDS and | |
2457 | * interfaces | |
332468f7 HS |
2458 | * |
2459 | * This function implements the MultipleProtocolInterfaces service. | |
332468f7 | 2460 | * |
78a88f79 MS |
2461 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2462 | * details. | |
2463 | * | |
2464 | * Return: status code | |
332468f7 | 2465 | */ |
7657ae12 | 2466 | efi_status_t EFIAPI efi_install_multiple_protocol_interfaces |
faea1041 | 2467 | (efi_handle_t *handle, ...) |
bee91169 AG |
2468 | { |
2469 | EFI_ENTRY("%p", handle); | |
58b83586 | 2470 | |
beb077a2 | 2471 | efi_va_list argptr; |
5a9682d0 | 2472 | const efi_guid_t *protocol; |
58b83586 | 2473 | void *protocol_interface; |
226cddbe | 2474 | efi_handle_t old_handle; |
58b83586 HS |
2475 | efi_status_t r = EFI_SUCCESS; |
2476 | int i = 0; | |
2477 | ||
2478 | if (!handle) | |
2479 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2480 | ||
beb077a2 | 2481 | efi_va_start(argptr, handle); |
58b83586 | 2482 | for (;;) { |
beb077a2 | 2483 | protocol = efi_va_arg(argptr, efi_guid_t*); |
58b83586 HS |
2484 | if (!protocol) |
2485 | break; | |
beb077a2 | 2486 | protocol_interface = efi_va_arg(argptr, void*); |
226cddbe HS |
2487 | /* Check that a device path has not been installed before */ |
2488 | if (!guidcmp(protocol, &efi_guid_device_path)) { | |
2489 | struct efi_device_path *dp = protocol_interface; | |
2490 | ||
2491 | r = EFI_CALL(efi_locate_device_path(protocol, &dp, | |
2492 | &old_handle)); | |
20562893 HS |
2493 | if (r == EFI_SUCCESS && |
2494 | dp->type == DEVICE_PATH_TYPE_END) { | |
2495 | EFI_PRINT("Path %pD already installed\n", | |
2496 | protocol_interface); | |
226cddbe HS |
2497 | r = EFI_ALREADY_STARTED; |
2498 | break; | |
2499 | } | |
2500 | } | |
1760ef57 HS |
2501 | r = EFI_CALL(efi_install_protocol_interface( |
2502 | handle, protocol, | |
2503 | EFI_NATIVE_INTERFACE, | |
2504 | protocol_interface)); | |
58b83586 HS |
2505 | if (r != EFI_SUCCESS) |
2506 | break; | |
2507 | i++; | |
2508 | } | |
beb077a2 | 2509 | efi_va_end(argptr); |
58b83586 HS |
2510 | if (r == EFI_SUCCESS) |
2511 | return EFI_EXIT(r); | |
2512 | ||
62471e46 | 2513 | /* If an error occurred undo all changes. */ |
beb077a2 | 2514 | efi_va_start(argptr, handle); |
58b83586 | 2515 | for (; i; --i) { |
beb077a2 AG |
2516 | protocol = efi_va_arg(argptr, efi_guid_t*); |
2517 | protocol_interface = efi_va_arg(argptr, void*); | |
faea1041 | 2518 | EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol, |
cd534083 | 2519 | protocol_interface)); |
58b83586 | 2520 | } |
beb077a2 | 2521 | efi_va_end(argptr); |
58b83586 HS |
2522 | |
2523 | return EFI_EXIT(r); | |
bee91169 AG |
2524 | } |
2525 | ||
6b03cd10 | 2526 | /** |
78a88f79 MS |
2527 | * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol |
2528 | * interfaces | |
2529 | * @handle: handle from which the protocol interfaces shall be removed | |
2530 | * @...: NULL terminated argument list with pairs of protocol GUIDS and | |
2531 | * interfaces | |
332468f7 HS |
2532 | * |
2533 | * This function implements the UninstallMultipleProtocolInterfaces service. | |
332468f7 | 2534 | * |
78a88f79 MS |
2535 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2536 | * details. | |
2537 | * | |
2538 | * Return: status code | |
332468f7 | 2539 | */ |
bee91169 | 2540 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
faea1041 | 2541 | efi_handle_t handle, ...) |
bee91169 AG |
2542 | { |
2543 | EFI_ENTRY("%p", handle); | |
843ce54c | 2544 | |
beb077a2 | 2545 | efi_va_list argptr; |
843ce54c HS |
2546 | const efi_guid_t *protocol; |
2547 | void *protocol_interface; | |
2548 | efi_status_t r = EFI_SUCCESS; | |
2549 | size_t i = 0; | |
2550 | ||
2551 | if (!handle) | |
2552 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2553 | ||
beb077a2 | 2554 | efi_va_start(argptr, handle); |
843ce54c | 2555 | for (;;) { |
beb077a2 | 2556 | protocol = efi_va_arg(argptr, efi_guid_t*); |
843ce54c HS |
2557 | if (!protocol) |
2558 | break; | |
beb077a2 | 2559 | protocol_interface = efi_va_arg(argptr, void*); |
9b47f13b HS |
2560 | r = efi_uninstall_protocol(handle, protocol, |
2561 | protocol_interface); | |
843ce54c HS |
2562 | if (r != EFI_SUCCESS) |
2563 | break; | |
2564 | i++; | |
2565 | } | |
beb077a2 | 2566 | efi_va_end(argptr); |
9b47f13b HS |
2567 | if (r == EFI_SUCCESS) { |
2568 | /* If the last protocol has been removed, delete the handle. */ | |
2569 | if (list_empty(&handle->protocols)) { | |
2570 | list_del(&handle->link); | |
2571 | free(handle); | |
2572 | } | |
843ce54c | 2573 | return EFI_EXIT(r); |
9b47f13b | 2574 | } |
843ce54c HS |
2575 | |
2576 | /* If an error occurred undo all changes. */ | |
beb077a2 | 2577 | efi_va_start(argptr, handle); |
843ce54c | 2578 | for (; i; --i) { |
beb077a2 AG |
2579 | protocol = efi_va_arg(argptr, efi_guid_t*); |
2580 | protocol_interface = efi_va_arg(argptr, void*); | |
843ce54c HS |
2581 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, |
2582 | EFI_NATIVE_INTERFACE, | |
2583 | protocol_interface)); | |
2584 | } | |
beb077a2 | 2585 | efi_va_end(argptr); |
843ce54c | 2586 | |
e2373021 HS |
2587 | /* In case of an error always return EFI_INVALID_PARAMETER */ |
2588 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
bee91169 AG |
2589 | } |
2590 | ||
6b03cd10 | 2591 | /** |
78a88f79 MS |
2592 | * efi_calculate_crc32() - calculate cyclic redundancy code |
2593 | * @data: buffer with data | |
2594 | * @data_size: size of buffer in bytes | |
2595 | * @crc32_p: cyclic redundancy code | |
332468f7 HS |
2596 | * |
2597 | * This function implements the CalculateCrc32 service. | |
332468f7 | 2598 | * |
78a88f79 MS |
2599 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2600 | * details. | |
2601 | * | |
2602 | * Return: status code | |
332468f7 | 2603 | */ |
8aa8360e HS |
2604 | static efi_status_t EFIAPI efi_calculate_crc32(const void *data, |
2605 | efi_uintn_t data_size, | |
2606 | u32 *crc32_p) | |
bee91169 | 2607 | { |
db80fe38 HS |
2608 | efi_status_t ret = EFI_SUCCESS; |
2609 | ||
8aa8360e | 2610 | EFI_ENTRY("%p, %zu", data, data_size); |
db80fe38 HS |
2611 | if (!data || !data_size || !crc32_p) { |
2612 | ret = EFI_INVALID_PARAMETER; | |
2613 | goto out; | |
2614 | } | |
bee91169 | 2615 | *crc32_p = crc32(0, data, data_size); |
db80fe38 HS |
2616 | out: |
2617 | return EFI_EXIT(ret); | |
bee91169 AG |
2618 | } |
2619 | ||
6b03cd10 | 2620 | /** |
78a88f79 MS |
2621 | * efi_copy_mem() - copy memory |
2622 | * @destination: destination of the copy operation | |
2623 | * @source: source of the copy operation | |
2624 | * @length: number of bytes to copy | |
332468f7 HS |
2625 | * |
2626 | * This function implements the CopyMem service. | |
332468f7 | 2627 | * |
78a88f79 MS |
2628 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2629 | * details. | |
332468f7 | 2630 | */ |
fc05a959 HS |
2631 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
2632 | size_t length) | |
bee91169 | 2633 | { |
fc05a959 | 2634 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
0bc81a71 | 2635 | memmove(destination, source, length); |
f7c78176 | 2636 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2637 | } |
2638 | ||
6b03cd10 | 2639 | /** |
78a88f79 MS |
2640 | * efi_set_mem() - Fill memory with a byte value. |
2641 | * @buffer: buffer to fill | |
2642 | * @size: size of buffer in bytes | |
2643 | * @value: byte to copy to the buffer | |
332468f7 HS |
2644 | * |
2645 | * This function implements the SetMem service. | |
332468f7 | 2646 | * |
78a88f79 MS |
2647 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2648 | * details. | |
332468f7 | 2649 | */ |
fc05a959 | 2650 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
bee91169 | 2651 | { |
fc05a959 | 2652 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
bee91169 | 2653 | memset(buffer, value, size); |
f7c78176 | 2654 | EFI_EXIT(EFI_SUCCESS); |
bee91169 AG |
2655 | } |
2656 | ||
6b03cd10 | 2657 | /** |
78a88f79 MS |
2658 | * efi_protocol_open() - open protocol interface on a handle |
2659 | * @handler: handler of a protocol | |
2660 | * @protocol_interface: interface implementing the protocol | |
2661 | * @agent_handle: handle of the driver | |
2662 | * @controller_handle: handle of the controller | |
2663 | * @attributes: attributes indicating how to open the protocol | |
191a41cc | 2664 | * |
78a88f79 | 2665 | * Return: status code |
191a41cc HS |
2666 | */ |
2667 | static efi_status_t efi_protocol_open( | |
2668 | struct efi_handler *handler, | |
2669 | void **protocol_interface, void *agent_handle, | |
2670 | void *controller_handle, uint32_t attributes) | |
2671 | { | |
2672 | struct efi_open_protocol_info_item *item; | |
2673 | struct efi_open_protocol_info_entry *match = NULL; | |
2674 | bool opened_by_driver = false; | |
2675 | bool opened_exclusive = false; | |
2676 | ||
2677 | /* If there is no agent, only return the interface */ | |
2678 | if (!agent_handle) | |
2679 | goto out; | |
2680 | ||
2681 | /* For TEST_PROTOCOL ignore interface attribute */ | |
2682 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2683 | *protocol_interface = NULL; | |
2684 | ||
2685 | /* | |
2686 | * Check if the protocol is already opened by a driver with the same | |
2687 | * attributes or opened exclusively | |
2688 | */ | |
2689 | list_for_each_entry(item, &handler->open_infos, link) { | |
2690 | if (item->info.agent_handle == agent_handle) { | |
2691 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && | |
2692 | (item->info.attributes == attributes)) | |
2693 | return EFI_ALREADY_STARTED; | |
399a39e3 HS |
2694 | } else { |
2695 | if (item->info.attributes & | |
2696 | EFI_OPEN_PROTOCOL_BY_DRIVER) | |
2697 | opened_by_driver = true; | |
191a41cc HS |
2698 | } |
2699 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) | |
2700 | opened_exclusive = true; | |
2701 | } | |
2702 | ||
2703 | /* Only one controller can open the protocol exclusively */ | |
399a39e3 HS |
2704 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
2705 | if (opened_exclusive) | |
2706 | return EFI_ACCESS_DENIED; | |
2707 | } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) { | |
2708 | if (opened_exclusive || opened_by_driver) | |
2709 | return EFI_ACCESS_DENIED; | |
2710 | } | |
191a41cc HS |
2711 | |
2712 | /* Prepare exclusive opening */ | |
2713 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { | |
2714 | /* Try to disconnect controllers */ | |
dae7ce45 HS |
2715 | disconnect_next: |
2716 | opened_by_driver = false; | |
191a41cc | 2717 | list_for_each_entry(item, &handler->open_infos, link) { |
dae7ce45 HS |
2718 | efi_status_t ret; |
2719 | ||
191a41cc | 2720 | if (item->info.attributes == |
dae7ce45 HS |
2721 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
2722 | ret = EFI_CALL(efi_disconnect_controller( | |
191a41cc HS |
2723 | item->info.controller_handle, |
2724 | item->info.agent_handle, | |
2725 | NULL)); | |
dae7ce45 HS |
2726 | if (ret == EFI_SUCCESS) |
2727 | /* | |
2728 | * Child controllers may have been | |
2729 | * removed from the open_infos list. So | |
2730 | * let's restart the loop. | |
2731 | */ | |
2732 | goto disconnect_next; | |
2733 | else | |
2734 | opened_by_driver = true; | |
2735 | } | |
191a41cc | 2736 | } |
dae7ce45 | 2737 | /* Only one driver can be connected */ |
191a41cc HS |
2738 | if (opened_by_driver) |
2739 | return EFI_ACCESS_DENIED; | |
2740 | } | |
2741 | ||
2742 | /* Find existing entry */ | |
2743 | list_for_each_entry(item, &handler->open_infos, link) { | |
2744 | if (item->info.agent_handle == agent_handle && | |
b4863baa HS |
2745 | item->info.controller_handle == controller_handle && |
2746 | item->info.attributes == attributes) | |
191a41cc HS |
2747 | match = &item->info; |
2748 | } | |
2749 | /* None found, create one */ | |
2750 | if (!match) { | |
2751 | match = efi_create_open_info(handler); | |
2752 | if (!match) | |
2753 | return EFI_OUT_OF_RESOURCES; | |
2754 | } | |
2755 | ||
2756 | match->agent_handle = agent_handle; | |
2757 | match->controller_handle = controller_handle; | |
2758 | match->attributes = attributes; | |
2759 | match->open_count++; | |
2760 | ||
2761 | out: | |
2762 | /* For TEST_PROTOCOL ignore interface attribute. */ | |
2763 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) | |
2764 | *protocol_interface = handler->protocol_interface; | |
2765 | ||
2766 | return EFI_SUCCESS; | |
2767 | } | |
2768 | ||
6b03cd10 | 2769 | /** |
78a88f79 MS |
2770 | * efi_open_protocol() - open protocol interface on a handle |
2771 | * @handle: handle on which the protocol shall be opened | |
2772 | * @protocol: GUID of the protocol | |
2773 | * @protocol_interface: interface implementing the protocol | |
2774 | * @agent_handle: handle of the driver | |
2775 | * @controller_handle: handle of the controller | |
2776 | * @attributes: attributes indicating how to open the protocol | |
332468f7 HS |
2777 | * |
2778 | * This function implements the OpenProtocol interface. | |
332468f7 | 2779 | * |
78a88f79 MS |
2780 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
2781 | * details. | |
2782 | * | |
2783 | * Return: status code | |
332468f7 | 2784 | */ |
faea1041 HS |
2785 | static efi_status_t EFIAPI efi_open_protocol |
2786 | (efi_handle_t handle, const efi_guid_t *protocol, | |
2787 | void **protocol_interface, efi_handle_t agent_handle, | |
2788 | efi_handle_t controller_handle, uint32_t attributes) | |
bee91169 | 2789 | { |
80286e8f | 2790 | struct efi_handler *handler; |
69baec67 | 2791 | efi_status_t r = EFI_INVALID_PARAMETER; |
bee91169 | 2792 | |
778e6af8 | 2793 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
bee91169 AG |
2794 | protocol_interface, agent_handle, controller_handle, |
2795 | attributes); | |
b5349f74 | 2796 | |
69baec67 HS |
2797 | if (!handle || !protocol || |
2798 | (!protocol_interface && attributes != | |
2799 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { | |
2800 | goto out; | |
2801 | } | |
2802 | ||
2803 | switch (attributes) { | |
2804 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: | |
2805 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: | |
2806 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: | |
2807 | break; | |
2808 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: | |
2809 | if (controller_handle == handle) | |
2810 | goto out; | |
191a41cc | 2811 | /* fall-through */ |
69baec67 HS |
2812 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
2813 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: | |
191a41cc HS |
2814 | /* Check that the controller handle is valid */ |
2815 | if (!efi_search_obj(controller_handle)) | |
69baec67 | 2816 | goto out; |
191a41cc | 2817 | /* fall-through */ |
69baec67 | 2818 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
191a41cc HS |
2819 | /* Check that the agent handle is valid */ |
2820 | if (!efi_search_obj(agent_handle)) | |
69baec67 HS |
2821 | goto out; |
2822 | break; | |
2823 | default: | |
b5349f74 HS |
2824 | goto out; |
2825 | } | |
2826 | ||
80286e8f | 2827 | r = efi_search_protocol(handle, protocol, &handler); |
e7c3cd6b HS |
2828 | switch (r) { |
2829 | case EFI_SUCCESS: | |
2830 | break; | |
2831 | case EFI_NOT_FOUND: | |
2832 | r = EFI_UNSUPPORTED; | |
80286e8f | 2833 | goto out; |
e7c3cd6b HS |
2834 | default: |
2835 | goto out; | |
2836 | } | |
bee91169 | 2837 | |
191a41cc HS |
2838 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
2839 | controller_handle, attributes); | |
bee91169 AG |
2840 | out: |
2841 | return EFI_EXIT(r); | |
2842 | } | |
2843 | ||
a115d565 HS |
2844 | /** |
2845 | * efi_start_image() - call the entry point of an image | |
2846 | * @image_handle: handle of the image | |
2847 | * @exit_data_size: size of the buffer | |
2848 | * @exit_data: buffer to receive the exit data of the called image | |
2849 | * | |
2850 | * This function implements the StartImage service. | |
2851 | * | |
2852 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
2853 | * details. | |
2854 | * | |
2855 | * Return: status code | |
2856 | */ | |
2857 | efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, | |
2858 | efi_uintn_t *exit_data_size, | |
2859 | u16 **exit_data) | |
2860 | { | |
2861 | struct efi_loaded_image_obj *image_obj = | |
2862 | (struct efi_loaded_image_obj *)image_handle; | |
2863 | efi_status_t ret; | |
bb31c3f7 HS |
2864 | void *info; |
2865 | efi_handle_t parent_image = current_image; | |
a115d565 HS |
2866 | |
2867 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); | |
2868 | ||
bb31c3f7 | 2869 | /* Check parameters */ |
1d3e8dc7 HS |
2870 | if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE) |
2871 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2872 | ||
bb31c3f7 HS |
2873 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
2874 | &info, NULL, NULL, | |
2875 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
2876 | if (ret != EFI_SUCCESS) | |
2877 | return EFI_EXIT(EFI_INVALID_PARAMETER); | |
2878 | ||
a115d565 HS |
2879 | efi_is_direct_boot = false; |
2880 | ||
556d8dc9 HS |
2881 | image_obj->exit_data_size = exit_data_size; |
2882 | image_obj->exit_data = exit_data; | |
2883 | ||
a115d565 HS |
2884 | /* call the image! */ |
2885 | if (setjmp(&image_obj->exit_jmp)) { | |
2886 | /* | |
2887 | * We called the entry point of the child image with EFI_CALL | |
2888 | * in the lines below. The child image called the Exit() boot | |
2889 | * service efi_exit() which executed the long jump that brought | |
2890 | * us to the current line. This implies that the second half | |
2891 | * of the EFI_CALL macro has not been executed. | |
2892 | */ | |
2893 | #ifdef CONFIG_ARM | |
2894 | /* | |
2895 | * efi_exit() called efi_restore_gd(). We have to undo this | |
2896 | * otherwise __efi_entry_check() will put the wrong value into | |
2897 | * app_gd. | |
2898 | */ | |
2899 | gd = app_gd; | |
2900 | #endif | |
2901 | /* | |
2902 | * To get ready to call EFI_EXIT below we have to execute the | |
2903 | * missed out steps of EFI_CALL. | |
2904 | */ | |
2905 | assert(__efi_entry_check()); | |
529886a0 HS |
2906 | EFI_PRINT("%lu returned by started image\n", |
2907 | (unsigned long)((uintptr_t)image_obj->exit_status & | |
2908 | ~EFI_ERROR_MASK)); | |
bb31c3f7 | 2909 | current_image = parent_image; |
a115d565 HS |
2910 | return EFI_EXIT(image_obj->exit_status); |
2911 | } | |
2912 | ||
bb31c3f7 | 2913 | current_image = image_handle; |
cd73aba6 | 2914 | image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE; |
6b95b38c | 2915 | EFI_PRINT("Jumping into 0x%p\n", image_obj->entry); |
a115d565 HS |
2916 | ret = EFI_CALL(image_obj->entry(image_handle, &systab)); |
2917 | ||
2918 | /* | |
2919 | * Usually UEFI applications call Exit() instead of returning. | |
2920 | * But because the world doesn't consist of ponies and unicorns, | |
2921 | * we're happy to emulate that behavior on behalf of a payload | |
2922 | * that forgot. | |
2923 | */ | |
2924 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); | |
2925 | } | |
2926 | ||
df116e81 HS |
2927 | /** |
2928 | * efi_delete_image() - delete loaded image from memory) | |
2929 | * | |
2930 | * @image_obj: handle of the loaded image | |
2931 | * @loaded_image_protocol: loaded image protocol | |
2932 | */ | |
120ff7ba HS |
2933 | static efi_status_t efi_delete_image |
2934 | (struct efi_loaded_image_obj *image_obj, | |
2935 | struct efi_loaded_image *loaded_image_protocol) | |
df116e81 | 2936 | { |
120ff7ba HS |
2937 | struct efi_object *efiobj; |
2938 | efi_status_t r, ret = EFI_SUCCESS; | |
2939 | ||
2940 | close_next: | |
2941 | list_for_each_entry(efiobj, &efi_obj_list, link) { | |
2942 | struct efi_handler *protocol; | |
2943 | ||
2944 | list_for_each_entry(protocol, &efiobj->protocols, link) { | |
2945 | struct efi_open_protocol_info_item *info; | |
2946 | ||
2947 | list_for_each_entry(info, &protocol->open_infos, link) { | |
2948 | if (info->info.agent_handle != | |
2949 | (efi_handle_t)image_obj) | |
2950 | continue; | |
2951 | r = EFI_CALL(efi_close_protocol | |
2952 | (efiobj, protocol->guid, | |
2953 | info->info.agent_handle, | |
2954 | info->info.controller_handle | |
2955 | )); | |
2956 | if (r != EFI_SUCCESS) | |
2957 | ret = r; | |
2958 | /* | |
2959 | * Closing protocols may results in further | |
2960 | * items being deleted. To play it safe loop | |
2961 | * over all elements again. | |
2962 | */ | |
2963 | goto close_next; | |
2964 | } | |
2965 | } | |
2966 | } | |
2967 | ||
df116e81 HS |
2968 | efi_free_pages((uintptr_t)loaded_image_protocol->image_base, |
2969 | efi_size_in_pages(loaded_image_protocol->image_size)); | |
2970 | efi_delete_handle(&image_obj->header); | |
120ff7ba HS |
2971 | |
2972 | return ret; | |
df116e81 HS |
2973 | } |
2974 | ||
46e99a9c HS |
2975 | /** |
2976 | * efi_unload_image() - unload an EFI image | |
2977 | * @image_handle: handle of the image to be unloaded | |
2978 | * | |
2979 | * This function implements the UnloadImage service. | |
2980 | * | |
2981 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
2982 | * details. | |
2983 | * | |
2984 | * Return: status code | |
2985 | */ | |
2986 | efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) | |
2987 | { | |
df116e81 | 2988 | efi_status_t ret = EFI_SUCCESS; |
46e99a9c | 2989 | struct efi_object *efiobj; |
df116e81 | 2990 | struct efi_loaded_image *loaded_image_protocol; |
46e99a9c HS |
2991 | |
2992 | EFI_ENTRY("%p", image_handle); | |
46e99a9c | 2993 | |
df116e81 HS |
2994 | efiobj = efi_search_obj(image_handle); |
2995 | if (!efiobj) { | |
2996 | ret = EFI_INVALID_PARAMETER; | |
2997 | goto out; | |
2998 | } | |
2999 | /* Find the loaded image protocol */ | |
3000 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, | |
3001 | (void **)&loaded_image_protocol, | |
3002 | NULL, NULL, | |
3003 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
3004 | if (ret != EFI_SUCCESS) { | |
3005 | ret = EFI_INVALID_PARAMETER; | |
3006 | goto out; | |
3007 | } | |
3008 | switch (efiobj->type) { | |
3009 | case EFI_OBJECT_TYPE_STARTED_IMAGE: | |
3010 | /* Call the unload function */ | |
3011 | if (!loaded_image_protocol->unload) { | |
3012 | ret = EFI_UNSUPPORTED; | |
3013 | goto out; | |
3014 | } | |
3015 | ret = EFI_CALL(loaded_image_protocol->unload(image_handle)); | |
3016 | if (ret != EFI_SUCCESS) | |
3017 | goto out; | |
3018 | break; | |
3019 | case EFI_OBJECT_TYPE_LOADED_IMAGE: | |
3020 | break; | |
3021 | default: | |
3022 | ret = EFI_INVALID_PARAMETER; | |
3023 | goto out; | |
3024 | } | |
3025 | efi_delete_image((struct efi_loaded_image_obj *)efiobj, | |
3026 | loaded_image_protocol); | |
3027 | out: | |
3028 | return EFI_EXIT(ret); | |
46e99a9c HS |
3029 | } |
3030 | ||
556d8dc9 HS |
3031 | /** |
3032 | * efi_update_exit_data() - fill exit data parameters of StartImage() | |
3033 | * | |
3034 | * @image_obj image handle | |
3035 | * @exit_data_size size of the exit data buffer | |
3036 | * @exit_data buffer with data returned by UEFI payload | |
3037 | * Return: status code | |
3038 | */ | |
3039 | static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj, | |
3040 | efi_uintn_t exit_data_size, | |
3041 | u16 *exit_data) | |
3042 | { | |
3043 | efi_status_t ret; | |
3044 | ||
3045 | /* | |
3046 | * If exit_data is not provided to StartImage(), exit_data_size must be | |
3047 | * ignored. | |
3048 | */ | |
3049 | if (!image_obj->exit_data) | |
3050 | return EFI_SUCCESS; | |
3051 | if (image_obj->exit_data_size) | |
3052 | *image_obj->exit_data_size = exit_data_size; | |
3053 | if (exit_data_size && exit_data) { | |
3054 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, | |
3055 | exit_data_size, | |
3056 | (void **)image_obj->exit_data); | |
3057 | if (ret != EFI_SUCCESS) | |
3058 | return ret; | |
3059 | memcpy(*image_obj->exit_data, exit_data, exit_data_size); | |
3060 | } else { | |
3061 | image_obj->exit_data = NULL; | |
3062 | } | |
3063 | return EFI_SUCCESS; | |
3064 | } | |
3065 | ||
a115d565 HS |
3066 | /** |
3067 | * efi_exit() - leave an EFI application or driver | |
3068 | * @image_handle: handle of the application or driver that is exiting | |
3069 | * @exit_status: status code | |
3070 | * @exit_data_size: size of the buffer in bytes | |
3071 | * @exit_data: buffer with data describing an error | |
3072 | * | |
3073 | * This function implements the Exit service. | |
3074 | * | |
3075 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3076 | * details. | |
3077 | * | |
3078 | * Return: status code | |
3079 | */ | |
3080 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, | |
3081 | efi_status_t exit_status, | |
3082 | efi_uintn_t exit_data_size, | |
3083 | u16 *exit_data) | |
3084 | { | |
3085 | /* | |
3086 | * TODO: We should call the unload procedure of the loaded | |
3087 | * image protocol. | |
3088 | */ | |
bb31c3f7 | 3089 | efi_status_t ret; |
126a43f1 | 3090 | struct efi_loaded_image *loaded_image_protocol; |
a115d565 HS |
3091 | struct efi_loaded_image_obj *image_obj = |
3092 | (struct efi_loaded_image_obj *)image_handle; | |
3093 | ||
3094 | EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status, | |
3095 | exit_data_size, exit_data); | |
3096 | ||
bb31c3f7 | 3097 | /* Check parameters */ |
bb31c3f7 | 3098 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
126a43f1 HS |
3099 | (void **)&loaded_image_protocol, |
3100 | NULL, NULL, | |
bb31c3f7 | 3101 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
126a43f1 HS |
3102 | if (ret != EFI_SUCCESS) { |
3103 | ret = EFI_INVALID_PARAMETER; | |
bb31c3f7 | 3104 | goto out; |
126a43f1 HS |
3105 | } |
3106 | ||
3107 | /* Unloading of unstarted images */ | |
3108 | switch (image_obj->header.type) { | |
3109 | case EFI_OBJECT_TYPE_STARTED_IMAGE: | |
3110 | break; | |
3111 | case EFI_OBJECT_TYPE_LOADED_IMAGE: | |
3112 | efi_delete_image(image_obj, loaded_image_protocol); | |
3113 | ret = EFI_SUCCESS; | |
3114 | goto out; | |
3115 | default: | |
3116 | /* Handle does not refer to loaded image */ | |
3117 | ret = EFI_INVALID_PARAMETER; | |
3118 | goto out; | |
3119 | } | |
3120 | /* A started image can only be unloaded it is the last one started. */ | |
3121 | if (image_handle != current_image) { | |
3122 | ret = EFI_INVALID_PARAMETER; | |
3123 | goto out; | |
3124 | } | |
bb31c3f7 | 3125 | |
556d8dc9 HS |
3126 | /* Exit data is only foreseen in case of failure. */ |
3127 | if (exit_status != EFI_SUCCESS) { | |
3128 | ret = efi_update_exit_data(image_obj, exit_data_size, | |
3129 | exit_data); | |
3130 | /* Exiting has priority. Don't return error to caller. */ | |
3131 | if (ret != EFI_SUCCESS) | |
3132 | EFI_PRINT("%s: out of memory\n", __func__); | |
3133 | } | |
126a43f1 HS |
3134 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION || |
3135 | exit_status != EFI_SUCCESS) | |
3136 | efi_delete_image(image_obj, loaded_image_protocol); | |
556d8dc9 | 3137 | |
a115d565 HS |
3138 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
3139 | EFI_EXIT(exit_status); | |
3140 | ||
3141 | /* | |
3142 | * But longjmp out with the U-Boot gd, not the application's, as | |
3143 | * the other end is a setjmp call inside EFI context. | |
3144 | */ | |
3145 | efi_restore_gd(); | |
3146 | ||
3147 | image_obj->exit_status = exit_status; | |
3148 | longjmp(&image_obj->exit_jmp, 1); | |
3149 | ||
3150 | panic("EFI application exited"); | |
bb31c3f7 | 3151 | out: |
126a43f1 | 3152 | return EFI_EXIT(ret); |
a115d565 HS |
3153 | } |
3154 | ||
6b03cd10 | 3155 | /** |
78a88f79 MS |
3156 | * efi_handle_protocol() - get interface of a protocol on a handle |
3157 | * @handle: handle on which the protocol shall be opened | |
3158 | * @protocol: GUID of the protocol | |
3159 | * @protocol_interface: interface implementing the protocol | |
332468f7 HS |
3160 | * |
3161 | * This function implements the HandleProtocol service. | |
332468f7 | 3162 | * |
78a88f79 MS |
3163 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
3164 | * details. | |
3165 | * | |
3166 | * Return: status code | |
332468f7 | 3167 | */ |
2074f700 | 3168 | static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
5a9682d0 | 3169 | const efi_guid_t *protocol, |
bee91169 AG |
3170 | void **protocol_interface) |
3171 | { | |
755d42d4 | 3172 | return efi_open_protocol(handle, protocol, protocol_interface, efi_root, |
8e1d329f | 3173 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
bee91169 AG |
3174 | } |
3175 | ||
6b03cd10 | 3176 | /** |
78a88f79 MS |
3177 | * efi_bind_controller() - bind a single driver to a controller |
3178 | * @controller_handle: controller handle | |
3179 | * @driver_image_handle: driver handle | |
3180 | * @remain_device_path: remaining path | |
6b03cd10 | 3181 | * |
78a88f79 | 3182 | * Return: status code |
6b03cd10 | 3183 | */ |
f0959dbe HS |
3184 | static efi_status_t efi_bind_controller( |
3185 | efi_handle_t controller_handle, | |
3186 | efi_handle_t driver_image_handle, | |
3187 | struct efi_device_path *remain_device_path) | |
3188 | { | |
3189 | struct efi_driver_binding_protocol *binding_protocol; | |
3190 | efi_status_t r; | |
3191 | ||
3192 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
3193 | &efi_guid_driver_binding_protocol, | |
3194 | (void **)&binding_protocol, | |
3195 | driver_image_handle, NULL, | |
3196 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
3197 | if (r != EFI_SUCCESS) | |
3198 | return r; | |
3199 | r = EFI_CALL(binding_protocol->supported(binding_protocol, | |
3200 | controller_handle, | |
3201 | remain_device_path)); | |
3202 | if (r == EFI_SUCCESS) | |
3203 | r = EFI_CALL(binding_protocol->start(binding_protocol, | |
3204 | controller_handle, | |
3205 | remain_device_path)); | |
3206 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
3207 | &efi_guid_driver_binding_protocol, | |
3208 | driver_image_handle, NULL)); | |
3209 | return r; | |
3210 | } | |
3211 | ||
6b03cd10 | 3212 | /** |
78a88f79 MS |
3213 | * efi_connect_single_controller() - connect a single driver to a controller |
3214 | * @controller_handle: controller | |
3215 | * @driver_image_handle: driver | |
b72aaa87 | 3216 | * @remain_device_path: remaining path |
6b03cd10 | 3217 | * |
78a88f79 | 3218 | * Return: status code |
6b03cd10 | 3219 | */ |
f0959dbe HS |
3220 | static efi_status_t efi_connect_single_controller( |
3221 | efi_handle_t controller_handle, | |
3222 | efi_handle_t *driver_image_handle, | |
3223 | struct efi_device_path *remain_device_path) | |
3224 | { | |
3225 | efi_handle_t *buffer; | |
3226 | size_t count; | |
3227 | size_t i; | |
3228 | efi_status_t r; | |
3229 | size_t connected = 0; | |
3230 | ||
3231 | /* Get buffer with all handles with driver binding protocol */ | |
3232 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, | |
3233 | &efi_guid_driver_binding_protocol, | |
3234 | NULL, &count, &buffer)); | |
3235 | if (r != EFI_SUCCESS) | |
3236 | return r; | |
3237 | ||
359a699a | 3238 | /* Context Override */ |
f0959dbe HS |
3239 | if (driver_image_handle) { |
3240 | for (; *driver_image_handle; ++driver_image_handle) { | |
3241 | for (i = 0; i < count; ++i) { | |
3242 | if (buffer[i] == *driver_image_handle) { | |
3243 | buffer[i] = NULL; | |
3244 | r = efi_bind_controller( | |
3245 | controller_handle, | |
3246 | *driver_image_handle, | |
3247 | remain_device_path); | |
3248 | /* | |
3249 | * For drivers that do not support the | |
3250 | * controller or are already connected | |
3251 | * we receive an error code here. | |
3252 | */ | |
3253 | if (r == EFI_SUCCESS) | |
3254 | ++connected; | |
3255 | } | |
3256 | } | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | /* | |
3261 | * TODO: Some overrides are not yet implemented: | |
3262 | * - Platform Driver Override | |
3263 | * - Driver Family Override Search | |
3264 | * - Bus Specific Driver Override | |
3265 | */ | |
3266 | ||
3267 | /* Driver Binding Search */ | |
3268 | for (i = 0; i < count; ++i) { | |
3269 | if (buffer[i]) { | |
3270 | r = efi_bind_controller(controller_handle, | |
3271 | buffer[i], | |
3272 | remain_device_path); | |
3273 | if (r == EFI_SUCCESS) | |
3274 | ++connected; | |
3275 | } | |
3276 | } | |
3277 | ||
3278 | efi_free_pool(buffer); | |
3279 | if (!connected) | |
3280 | return EFI_NOT_FOUND; | |
3281 | return EFI_SUCCESS; | |
3282 | } | |
3283 | ||
6b03cd10 | 3284 | /** |
78a88f79 MS |
3285 | * efi_connect_controller() - connect a controller to a driver |
3286 | * @controller_handle: handle of the controller | |
3287 | * @driver_image_handle: handle of the driver | |
3288 | * @remain_device_path: device path of a child controller | |
3289 | * @recursive: true to connect all child controllers | |
f0959dbe HS |
3290 | * |
3291 | * This function implements the ConnectController service. | |
78a88f79 MS |
3292 | * |
3293 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3294 | * details. | |
f0959dbe HS |
3295 | * |
3296 | * First all driver binding protocol handles are tried for binding drivers. | |
b72aaa87 | 3297 | * Afterwards all handles that have opened a protocol of the controller |
f0959dbe HS |
3298 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. |
3299 | * | |
78a88f79 | 3300 | * Return: status code |
f0959dbe HS |
3301 | */ |
3302 | static efi_status_t EFIAPI efi_connect_controller( | |
3303 | efi_handle_t controller_handle, | |
3304 | efi_handle_t *driver_image_handle, | |
3305 | struct efi_device_path *remain_device_path, | |
3306 | bool recursive) | |
3307 | { | |
3308 | efi_status_t r; | |
3309 | efi_status_t ret = EFI_NOT_FOUND; | |
3310 | struct efi_object *efiobj; | |
3311 | ||
d178836b | 3312 | EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle, |
f0959dbe HS |
3313 | remain_device_path, recursive); |
3314 | ||
3315 | efiobj = efi_search_obj(controller_handle); | |
3316 | if (!efiobj) { | |
3317 | ret = EFI_INVALID_PARAMETER; | |
3318 | goto out; | |
3319 | } | |
3320 | ||
3321 | r = efi_connect_single_controller(controller_handle, | |
3322 | driver_image_handle, | |
3323 | remain_device_path); | |
3324 | if (r == EFI_SUCCESS) | |
3325 | ret = EFI_SUCCESS; | |
3326 | if (recursive) { | |
3327 | struct efi_handler *handler; | |
3328 | struct efi_open_protocol_info_item *item; | |
3329 | ||
3330 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3331 | list_for_each_entry(item, &handler->open_infos, link) { | |
3332 | if (item->info.attributes & | |
3333 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
3334 | r = EFI_CALL(efi_connect_controller( | |
3335 | item->info.controller_handle, | |
3336 | driver_image_handle, | |
3337 | remain_device_path, | |
3338 | recursive)); | |
3339 | if (r == EFI_SUCCESS) | |
3340 | ret = EFI_SUCCESS; | |
3341 | } | |
3342 | } | |
3343 | } | |
3344 | } | |
359a699a | 3345 | /* Check for child controller specified by end node */ |
f0959dbe HS |
3346 | if (ret != EFI_SUCCESS && remain_device_path && |
3347 | remain_device_path->type == DEVICE_PATH_TYPE_END) | |
3348 | ret = EFI_SUCCESS; | |
3349 | out: | |
3350 | return EFI_EXIT(ret); | |
3351 | } | |
3352 | ||
6b03cd10 | 3353 | /** |
78a88f79 MS |
3354 | * efi_reinstall_protocol_interface() - reinstall protocol interface |
3355 | * @handle: handle on which the protocol shall be reinstalled | |
3356 | * @protocol: GUID of the protocol to be installed | |
3357 | * @old_interface: interface to be removed | |
3358 | * @new_interface: interface to be installed | |
e861a120 HS |
3359 | * |
3360 | * This function implements the ReinstallProtocolInterface service. | |
78a88f79 MS |
3361 | * |
3362 | * See the Unified Extensible Firmware Interface (UEFI) specification for | |
3363 | * details. | |
e861a120 HS |
3364 | * |
3365 | * The old interface is uninstalled. The new interface is installed. | |
3366 | * Drivers are connected. | |
3367 | * | |
78a88f79 | 3368 | * Return: status code |
e861a120 HS |
3369 | */ |
3370 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( | |
3371 | efi_handle_t handle, const efi_guid_t *protocol, | |
3372 | void *old_interface, void *new_interface) | |
3373 | { | |
3374 | efi_status_t ret; | |
3375 | ||
3376 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface, | |
3377 | new_interface); | |
9b47f13b HS |
3378 | |
3379 | /* Uninstall protocol but do not delete handle */ | |
3380 | ret = efi_uninstall_protocol(handle, protocol, old_interface); | |
e861a120 HS |
3381 | if (ret != EFI_SUCCESS) |
3382 | goto out; | |
9b47f13b HS |
3383 | |
3384 | /* Install the new protocol */ | |
3385 | ret = efi_add_protocol(handle, protocol, new_interface); | |
3386 | /* | |
3387 | * The UEFI spec does not specify what should happen to the handle | |
3388 | * if in case of an error no protocol interface remains on the handle. | |
3389 | * So let's do nothing here. | |
3390 | */ | |
e861a120 HS |
3391 | if (ret != EFI_SUCCESS) |
3392 | goto out; | |
3393 | /* | |
3394 | * The returned status code has to be ignored. | |
3395 | * Do not create an error if no suitable driver for the handle exists. | |
3396 | */ | |
3397 | EFI_CALL(efi_connect_controller(handle, NULL, NULL, true)); | |
3398 | out: | |
3399 | return EFI_EXIT(ret); | |
3400 | } | |
3401 | ||
6b03cd10 | 3402 | /** |
78a88f79 MS |
3403 | * efi_get_child_controllers() - get all child controllers associated to a driver |
3404 | * @efiobj: handle of the controller | |
3405 | * @driver_handle: handle of the driver | |
3406 | * @number_of_children: number of child controllers | |
3407 | * @child_handle_buffer: handles of the the child controllers | |
6b03cd10 | 3408 | * |
3f9b0042 HS |
3409 | * The allocated buffer has to be freed with free(). |
3410 | * | |
78a88f79 | 3411 | * Return: status code |
3f9b0042 HS |
3412 | */ |
3413 | static efi_status_t efi_get_child_controllers( | |
3414 | struct efi_object *efiobj, | |
3415 | efi_handle_t driver_handle, | |
3416 | efi_uintn_t *number_of_children, | |
3417 | efi_handle_t **child_handle_buffer) | |
3418 | { | |
3419 | struct efi_handler *handler; | |
3420 | struct efi_open_protocol_info_item *item; | |
3421 | efi_uintn_t count = 0, i; | |
3422 | bool duplicate; | |
3423 | ||
3424 | /* Count all child controller associations */ | |
3425 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3426 | list_for_each_entry(item, &handler->open_infos, link) { | |
3427 | if (item->info.agent_handle == driver_handle && | |
3428 | item->info.attributes & | |
3429 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) | |
3430 | ++count; | |
3431 | } | |
3432 | } | |
3433 | /* | |
3434 | * Create buffer. In case of duplicate child controller assignments | |
3435 | * the buffer will be too large. But that does not harm. | |
3436 | */ | |
3437 | *number_of_children = 0; | |
3438 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); | |
3439 | if (!*child_handle_buffer) | |
3440 | return EFI_OUT_OF_RESOURCES; | |
3441 | /* Copy unique child handles */ | |
3442 | list_for_each_entry(handler, &efiobj->protocols, link) { | |
3443 | list_for_each_entry(item, &handler->open_infos, link) { | |
3444 | if (item->info.agent_handle == driver_handle && | |
3445 | item->info.attributes & | |
3446 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { | |
3447 | /* Check this is a new child controller */ | |
3448 | duplicate = false; | |
3449 | for (i = 0; i < *number_of_children; ++i) { | |
3450 | if ((*child_handle_buffer)[i] == | |
3451 | item->info.controller_handle) | |
3452 | duplicate = true; | |
3453 | } | |
3454 | /* Copy handle to buffer */ | |
3455 | if (!duplicate) { | |
3456 | i = (*number_of_children)++; | |
3457 | (*child_handle_buffer)[i] = | |
3458 | item->info.controller_handle; | |
3459 | } | |
3460 | } | |
3461 | } | |
3462 | } | |
3463 | return EFI_SUCCESS; | |
3464 | } | |
3465 | ||
6b03cd10 | 3466 | /** |
78a88f79 MS |
3467 | * efi_disconnect_controller() - disconnect a controller from a driver |
3468 | * @controller_handle: handle of the controller | |
3469 | * @driver_image_handle: handle of the driver | |
3470 | * @child_handle: handle of the child to destroy | |
3f9b0042 HS |
3471 | * |
3472 | * This function implements the DisconnectController service. | |
3f9b0042 | 3473 | * |
78a88f79 MS |
3474 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
3475 | * details. | |
3476 | * | |
3477 | * Return: status code | |
3f9b0042 HS |
3478 | */ |
3479 | static efi_status_t EFIAPI efi_disconnect_controller( | |
3480 | efi_handle_t controller_handle, | |
3481 | efi_handle_t driver_image_handle, | |
3482 | efi_handle_t child_handle) | |
3483 | { | |
3484 | struct efi_driver_binding_protocol *binding_protocol; | |
3485 | efi_handle_t *child_handle_buffer = NULL; | |
3486 | size_t number_of_children = 0; | |
3487 | efi_status_t r; | |
3488 | size_t stop_count = 0; | |
3489 | struct efi_object *efiobj; | |
3490 | ||
3491 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, | |
3492 | child_handle); | |
3493 | ||
3494 | efiobj = efi_search_obj(controller_handle); | |
3495 | if (!efiobj) { | |
3496 | r = EFI_INVALID_PARAMETER; | |
3497 | goto out; | |
3498 | } | |
3499 | ||
3500 | if (child_handle && !efi_search_obj(child_handle)) { | |
3501 | r = EFI_INVALID_PARAMETER; | |
3502 | goto out; | |
3503 | } | |
3504 | ||
3505 | /* If no driver handle is supplied, disconnect all drivers */ | |
3506 | if (!driver_image_handle) { | |
3507 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); | |
3508 | goto out; | |
3509 | } | |
3510 | ||
3511 | /* Create list of child handles */ | |
3512 | if (child_handle) { | |
3513 | number_of_children = 1; | |
3514 | child_handle_buffer = &child_handle; | |
3515 | } else { | |
3516 | efi_get_child_controllers(efiobj, | |
3517 | driver_image_handle, | |
3518 | &number_of_children, | |
3519 | &child_handle_buffer); | |
3520 | } | |
3521 | ||
3522 | /* Get the driver binding protocol */ | |
3523 | r = EFI_CALL(efi_open_protocol(driver_image_handle, | |
3524 | &efi_guid_driver_binding_protocol, | |
3525 | (void **)&binding_protocol, | |
3526 | driver_image_handle, NULL, | |
3527 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); | |
3528 | if (r != EFI_SUCCESS) | |
3529 | goto out; | |
3530 | /* Remove the children */ | |
3531 | if (number_of_children) { | |
3532 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
3533 | controller_handle, | |
3534 | number_of_children, | |
3535 | child_handle_buffer)); | |
3536 | if (r == EFI_SUCCESS) | |
3537 | ++stop_count; | |
3538 | } | |
3539 | /* Remove the driver */ | |
3540 | if (!child_handle) | |
3541 | r = EFI_CALL(binding_protocol->stop(binding_protocol, | |
3542 | controller_handle, | |
3543 | 0, NULL)); | |
3544 | if (r == EFI_SUCCESS) | |
3545 | ++stop_count; | |
3546 | EFI_CALL(efi_close_protocol(driver_image_handle, | |
3547 | &efi_guid_driver_binding_protocol, | |
3548 | driver_image_handle, NULL)); | |
3549 | ||
3550 | if (stop_count) | |
3551 | r = EFI_SUCCESS; | |
3552 | else | |
3553 | r = EFI_NOT_FOUND; | |
3554 | out: | |
3555 | if (!child_handle) | |
3556 | free(child_handle_buffer); | |
3557 | return EFI_EXIT(r); | |
3558 | } | |
3559 | ||
640adadf | 3560 | static struct efi_boot_services efi_boot_services = { |
bee91169 | 3561 | .hdr = { |
112f2430 HS |
3562 | .signature = EFI_BOOT_SERVICES_SIGNATURE, |
3563 | .revision = EFI_SPECIFICATION_VERSION, | |
71c846ab | 3564 | .headersize = sizeof(struct efi_boot_services), |
bee91169 AG |
3565 | }, |
3566 | .raise_tpl = efi_raise_tpl, | |
3567 | .restore_tpl = efi_restore_tpl, | |
3568 | .allocate_pages = efi_allocate_pages_ext, | |
3569 | .free_pages = efi_free_pages_ext, | |
3570 | .get_memory_map = efi_get_memory_map_ext, | |
ead1274b | 3571 | .allocate_pool = efi_allocate_pool_ext, |
42417bc8 | 3572 | .free_pool = efi_free_pool_ext, |
49deb455 | 3573 | .create_event = efi_create_event_ext, |
bfc72462 | 3574 | .set_timer = efi_set_timer_ext, |
bee91169 | 3575 | .wait_for_event = efi_wait_for_event, |
c6841592 | 3576 | .signal_event = efi_signal_event_ext, |
bee91169 AG |
3577 | .close_event = efi_close_event, |
3578 | .check_event = efi_check_event, | |
1760ef57 | 3579 | .install_protocol_interface = efi_install_protocol_interface, |
bee91169 | 3580 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
cd534083 | 3581 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
bee91169 AG |
3582 | .handle_protocol = efi_handle_protocol, |
3583 | .reserved = NULL, | |
3584 | .register_protocol_notify = efi_register_protocol_notify, | |
26329584 | 3585 | .locate_handle = efi_locate_handle_ext, |
bee91169 | 3586 | .locate_device_path = efi_locate_device_path, |
488bf12d | 3587 | .install_configuration_table = efi_install_configuration_table_ext, |
bee91169 AG |
3588 | .load_image = efi_load_image, |
3589 | .start_image = efi_start_image, | |
a86aeaf2 | 3590 | .exit = efi_exit, |
bee91169 AG |
3591 | .unload_image = efi_unload_image, |
3592 | .exit_boot_services = efi_exit_boot_services, | |
3593 | .get_next_monotonic_count = efi_get_next_monotonic_count, | |
3594 | .stall = efi_stall, | |
3595 | .set_watchdog_timer = efi_set_watchdog_timer, | |
3596 | .connect_controller = efi_connect_controller, | |
3597 | .disconnect_controller = efi_disconnect_controller, | |
3598 | .open_protocol = efi_open_protocol, | |
3599 | .close_protocol = efi_close_protocol, | |
3600 | .open_protocol_information = efi_open_protocol_information, | |
3601 | .protocols_per_handle = efi_protocols_per_handle, | |
3602 | .locate_handle_buffer = efi_locate_handle_buffer, | |
3603 | .locate_protocol = efi_locate_protocol, | |
ab9efa97 HS |
3604 | .install_multiple_protocol_interfaces = |
3605 | efi_install_multiple_protocol_interfaces, | |
3606 | .uninstall_multiple_protocol_interfaces = | |
3607 | efi_uninstall_multiple_protocol_interfaces, | |
bee91169 AG |
3608 | .calculate_crc32 = efi_calculate_crc32, |
3609 | .copy_mem = efi_copy_mem, | |
3610 | .set_mem = efi_set_mem, | |
9f0930e5 | 3611 | .create_event_ex = efi_create_event_ex, |
bee91169 AG |
3612 | }; |
3613 | ||
0b386537 | 3614 | static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
bee91169 | 3615 | |
3c63db9c | 3616 | struct efi_system_table __efi_runtime_data systab = { |
bee91169 AG |
3617 | .hdr = { |
3618 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, | |
112f2430 | 3619 | .revision = EFI_SPECIFICATION_VERSION, |
71c846ab | 3620 | .headersize = sizeof(struct efi_system_table), |
bee91169 | 3621 | }, |
0b386537 HS |
3622 | .fw_vendor = firmware_vendor, |
3623 | .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8, | |
3c783bfb | 3624 | .runtime = &efi_runtime_services, |
bee91169 | 3625 | .nr_tables = 0, |
4182a129 | 3626 | .tables = NULL, |
bee91169 | 3627 | }; |
640adadf HS |
3628 | |
3629 | /** | |
3630 | * efi_initialize_system_table() - Initialize system table | |
3631 | * | |
0414359a | 3632 | * Return: status code |
640adadf HS |
3633 | */ |
3634 | efi_status_t efi_initialize_system_table(void) | |
3635 | { | |
4182a129 HS |
3636 | efi_status_t ret; |
3637 | ||
3638 | /* Allocate configuration table array */ | |
3639 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, | |
3640 | EFI_MAX_CONFIGURATION_TABLES * | |
3641 | sizeof(struct efi_configuration_table), | |
3642 | (void **)&systab.tables); | |
3643 | ||
93148eba HS |
3644 | /* |
3645 | * These entries will be set to NULL in ExitBootServices(). To avoid | |
3646 | * relocation in SetVirtualAddressMap(), set them dynamically. | |
3647 | */ | |
3648 | systab.con_in = &efi_con_in; | |
3649 | systab.con_out = &efi_con_out; | |
3650 | systab.std_err = &efi_con_out; | |
3651 | systab.boottime = &efi_boot_services; | |
3652 | ||
b72aaa87 | 3653 | /* Set CRC32 field in table headers */ |
640adadf HS |
3654 | efi_update_table_header_crc32(&systab.hdr); |
3655 | efi_update_table_header_crc32(&efi_runtime_services.hdr); | |
3656 | efi_update_table_header_crc32(&efi_boot_services.hdr); | |
4182a129 HS |
3657 | |
3658 | return ret; | |
640adadf | 3659 | } |