blob: c2f89805c76776fe4a2bff2f9d6aea1a9b882469 [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafbee91162016-03-04 01:09:59 +01004 *
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02005 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +020016#include <pe.h>
Alexander Grafbee91162016-03-04 01:09:59 +010017#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020023
Alexander Grafbee91162016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010029
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +020030/* List of queued events */
31LIST_HEAD(efi_event_queue);
32
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +020033/* Flag to disable timer activity in ExitBootServices() */
34static bool timers_enabled = true;
35
Heinrich Schuchardtab15d412019-05-04 17:27:54 +020036/* List of all events registered by RegisterProtocolNotify() */
37LIST_HEAD(efi_register_notify_events);
38
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010039/* Handle of the currently executing image */
40static efi_handle_t current_image;
41
Alexander Graff31239a2018-11-15 21:23:47 +010042/*
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 */
48static bool efi_is_direct_boot = true;
49
Simon Glass65e4c0b2016-09-25 15:27:35 -060050#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010051/*
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 */
57static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060058#endif
Alexander Grafbee91162016-03-04 01:09:59 +010059
Heinrich Schuchardt914df752019-02-09 14:10:39 +010060/* 1 if inside U-Boot code, 0 if inside EFI payload code */
61static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040062static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010063/* GUID of the device tree table */
64const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010065/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
66const efi_guid_t efi_guid_driver_binding_protocol =
67 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040068
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010069/* event group ExitBootServices() invoked */
70const efi_guid_t efi_guid_event_group_exit_boot_services =
71 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
72/* event group SetVirtualAddressMap() invoked */
73const efi_guid_t efi_guid_event_group_virtual_address_change =
74 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
75/* event group memory map changed */
76const 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 */
79const efi_guid_t efi_guid_event_group_ready_to_boot =
80 EFI_EVENT_GROUP_READY_TO_BOOT;
81/* event group ResetSystem() invoked (before ExitBootServices) */
82const efi_guid_t efi_guid_event_group_reset_system =
83 EFI_EVENT_GROUP_RESET_SYSTEM;
84
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010085static 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);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010089
Rob Clarkc160d2f2017-07-27 08:04:18 -040090/* Called on every callback entry */
91int __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 */
103int __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
Alexander Grafbee91162016-03-04 01:09:59 +0100112/* Called from do_bootefi_exec() */
113void efi_save_gd(void)
114{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600115#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100116 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600117#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100118}
119
Rob Clarkc160d2f2017-07-27 08:04:18 -0400120/*
Mario Six78a88f72018-07-10 08:40:17 +0200121 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200122 * world so we can dump out an abort message, without any care about returning
123 * back to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400124 */
Alexander Grafbee91162016-03-04 01:09:59 +0100125void efi_restore_gd(void)
126{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600127#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100128 /* Only restore if we're already in EFI context */
129 if (!efi_gd)
130 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100131 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600132#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100133}
134
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200135/**
Mario Six78a88f72018-07-10 08:40:17 +0200136 * indent_string() - returns a string for indenting with two spaces per level
137 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100138 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200139 * A maximum of ten indent levels is supported. Higher indent levels will be
140 * truncated.
141 *
Mario Six78a88f72018-07-10 08:40:17 +0200142 * Return: A string for indenting with two spaces per level is
143 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400144 */
145static const char *indent_string(int level)
146{
147 const char *indent = " ";
148 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100149
Rob Clarkaf65db82017-07-27 08:04:19 -0400150 level = min(max, level * 2);
151 return &indent[max - level];
152}
153
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200154const char *__efi_nesting(void)
155{
156 return indent_string(nesting_level);
157}
158
Rob Clarkaf65db82017-07-27 08:04:19 -0400159const char *__efi_nesting_inc(void)
160{
161 return indent_string(nesting_level++);
162}
163
164const char *__efi_nesting_dec(void)
165{
166 return indent_string(--nesting_level);
167}
168
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200169/**
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200170 * efi_event_is_queued() - check if an event is queued
171 *
172 * @event: event
173 * Return: true if event is queued
174 */
175static 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 */
183static 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
207/**
Mario Six78a88f72018-07-10 08:40:17 +0200208 * efi_queue_event() - queue an EFI event
209 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200210 *
211 * This function queues the notification function of the event for future
212 * execution.
213 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200214 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200215static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200216{
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200217 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);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200237 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200238 efi_process_event_queue();
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200239}
240
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200241/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200242 * is_valid_tpl() - check if the task priority level is valid
243 *
244 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200245 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200246 */
247efi_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
260/**
Mario Six78a88f72018-07-10 08:40:17 +0200261 * efi_signal_event() - signal an EFI event
262 * @event: event to signal
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100263 *
Mario Six78a88f72018-07-10 08:40:17 +0200264 * 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
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100266 * their notification function is queued.
267 *
268 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100269 */
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200270void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100271{
Heinrich Schuchardtdfa306e2019-06-06 01:51:50 +0200272 if (event->is_signaled)
273 return;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100274 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;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100287 }
288 list_for_each_entry(evt, &efi_events, link) {
289 if (!evt->group || guidcmp(evt->group, event->group))
290 continue;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200291 efi_queue_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100292 }
Heinrich Schuchardt3626e532019-05-05 00:07:34 +0200293 } else {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100294 event->is_signaled = true;
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200295 efi_queue_event(event);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100296 }
297}
298
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200299/**
Mario Six78a88f72018-07-10 08:40:17 +0200300 * efi_raise_tpl() - raise the task priority level
301 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200302 *
303 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200304 *
Mario Six78a88f72018-07-10 08:40:17 +0200305 * See the Unified Extensible Firmware Interface (UEFI) specification for
306 * details.
307 *
308 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200309 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100310static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100311{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100312 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200313
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200314 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200315
316 if (new_tpl < efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200317 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200318 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;
Alexander Grafbee91162016-03-04 01:09:59 +0100324}
325
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200326/**
Mario Six78a88f72018-07-10 08:40:17 +0200327 * efi_restore_tpl() - lower the task priority level
328 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200329 *
330 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200331 *
Mario Six78a88f72018-07-10 08:40:17 +0200332 * See the Unified Extensible Firmware Interface (UEFI) specification for
333 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200334 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100335static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100336{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200337 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200338
339 if (old_tpl > efi_tpl)
Heinrich Schuchardt529886a2019-05-05 11:56:23 +0200340 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200341 efi_tpl = old_tpl;
342 if (efi_tpl > TPL_HIGH_LEVEL)
343 efi_tpl = TPL_HIGH_LEVEL;
344
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100345 /*
346 * Lowering the TPL may have made queued events eligible for execution.
347 */
348 efi_timer_check();
349
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200350 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100351}
352
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200353/**
Mario Six78a88f72018-07-10 08:40:17 +0200354 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200359 *
360 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200361 *
Mario Six78a88f72018-07-10 08:40:17 +0200362 * See the Unified Extensible Firmware Interface (UEFI) specification for
363 * details.
364 *
365 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200366 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900367static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100368 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900369 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100370{
371 efi_status_t r;
372
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100373 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100374 r = efi_allocate_pages(type, memory_type, pages, memory);
375 return EFI_EXIT(r);
376}
377
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200378/**
Mario Six78a88f72018-07-10 08:40:17 +0200379 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200382 *
383 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200384 *
Mario Six78a88f72018-07-10 08:40:17 +0200385 * See the Unified Extensible Firmware Interface (UEFI) specification for
386 * details.
387 *
388 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200389 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900390static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100391 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100392{
393 efi_status_t r;
394
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900395 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100396 r = efi_free_pages(memory, pages);
397 return EFI_EXIT(r);
398}
399
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200400/**
Mario Six78a88f72018-07-10 08:40:17 +0200401 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200408 *
409 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200410 *
Mario Six78a88f72018-07-10 08:40:17 +0200411 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * details.
413 *
414 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200415 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900416static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100417 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900418 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100419 efi_uintn_t *map_key,
420 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900421 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100422{
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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200432/**
Mario Six78a88f72018-07-10 08:40:17 +0200433 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200437 *
438 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200439 *
Mario Six78a88f72018-07-10 08:40:17 +0200440 * See the Unified Extensible Firmware Interface (UEFI) specification for
441 * details.
442 *
443 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200444 */
Stefan Brünsead12742016-10-09 22:17:18 +0200445static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100446 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200447 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100448{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100449 efi_status_t r;
450
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100451 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200452 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100453 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100454}
455
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200456/**
Mario Six78a88f72018-07-10 08:40:17 +0200457 * efi_free_pool_ext() - free memory from pool
458 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200459 *
460 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200461 *
Mario Six78a88f72018-07-10 08:40:17 +0200462 * See the Unified Extensible Firmware Interface (UEFI) specification for
463 * details.
464 *
465 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200466 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200467static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100468{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100469 efi_status_t r;
470
471 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200472 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100473 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100474}
475
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200476/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200477 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100478 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200479 * @handle: handle to be added
480 *
481 * The protocols list is initialized. The handle is added to the list of known
482 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100483 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200484void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100485{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200486 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100487 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200488 INIT_LIST_HEAD(&handle->protocols);
489 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100490}
491
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200492/**
Mario Six78a88f72018-07-10 08:40:17 +0200493 * efi_create_handle() - create handle
494 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200495 *
Mario Six78a88f72018-07-10 08:40:17 +0200496 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200497 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100498efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200499{
500 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200501
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200502 obj = calloc(1, sizeof(struct efi_object));
503 if (!obj)
504 return EFI_OUT_OF_RESOURCES;
505
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100506 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200507 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200508
509 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200510}
511
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200512/**
Mario Six78a88f72018-07-10 08:40:17 +0200513 * 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
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100517 *
Mario Six78a88f72018-07-10 08:40:17 +0200518 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100519 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100520efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100521 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200545/**
Mario Six78a88f72018-07-10 08:40:17 +0200546 * 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
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100550 *
Mario Six78a88f72018-07-10 08:40:17 +0200551 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100552 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100553efi_status_t efi_remove_protocol(const efi_handle_t handle,
554 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100555 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;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200563 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardt96aa99c2019-05-10 20:06:48 +0200564 return EFI_NOT_FOUND;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100565 list_del(&handler->link);
566 free(handler);
567 return EFI_SUCCESS;
568}
569
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200570/**
Mario Six78a88f72018-07-10 08:40:17 +0200571 * efi_remove_all_protocols() - delete all protocols from a handle
572 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100573 *
Mario Six78a88f72018-07-10 08:40:17 +0200574 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100575 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100576efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100577{
578 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100579 struct efi_handler *protocol;
580 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100581
582 efiobj = efi_search_obj(handle);
583 if (!efiobj)
584 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100585 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100586 efi_status_t ret;
587
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100588 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200596/**
Mario Six78a88f72018-07-10 08:40:17 +0200597 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100598 *
Mario Six78a88f72018-07-10 08:40:17 +0200599 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100600 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200601void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100602{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200603 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100604 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200605 efi_remove_all_protocols(handle);
606 list_del(&handle->link);
607 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100608}
609
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200610/**
Mario Six78a88f72018-07-10 08:40:17 +0200611 * efi_is_event() - check if a pointer is a valid event
612 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100613 *
Mario Six78a88f72018-07-10 08:40:17 +0200614 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100615 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100616static 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}
Alexander Grafbee91162016-03-04 01:09:59 +0100628
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200629/**
Mario Six78a88f72018-07-10 08:40:17 +0200630 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200637 *
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 *
Mario Six78a88f72018-07-10 08:40:17 +0200643 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200644 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100645efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200646 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200647 struct efi_event *event,
648 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100649 void *notify_context, efi_guid_t *group,
650 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100651{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100652 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200653
Jonathan Graya95343b2017-03-12 19:26:07 +1100654 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200655 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100656
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200657 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:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200668 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200669 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100670
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900671 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt751e9282019-04-25 07:30:41 +0200672 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200673 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100674
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100675 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;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100682 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200683 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100684 evt->trigger_next = -1ULL;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100685 list_add_tail(&evt->link, &efi_events);
686 *event = evt;
687 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100688}
689
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200690/*
Mario Six78a88f72018-07-10 08:40:17 +0200691 * 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
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100698 *
699 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100700 *
Mario Six78a88f72018-07-10 08:40:17 +0200701 * See the Unified Extensible Firmware Interface (UEFI) specification for
702 * details.
703 *
704 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100705 */
706efi_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{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200714 efi_status_t ret;
715
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100716 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
717 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200718
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);
732out:
733 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100734}
735
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200736/**
Mario Six78a88f72018-07-10 08:40:17 +0200737 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200743 *
744 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200745 *
Mario Six78a88f72018-07-10 08:40:17 +0200746 * See the Unified Extensible Firmware Interface (UEFI) specification for
747 * details.
748 *
749 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200750 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200751static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100752 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200753 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,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100761 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200762}
763
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200764/**
Mario Six78a88f72018-07-10 08:40:17 +0200765 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200766 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200767 * Check if a timer event has occurred or a queued notification function should
768 * be called.
769 *
Alexander Grafbee91162016-03-04 01:09:59 +0100770 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200771 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100772 */
773void efi_timer_check(void)
774{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100775 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100776 u64 now = timer_get_us();
777
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100778 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200779 if (!timers_enabled)
780 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100781 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200782 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100783 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200784 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100785 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200786 break;
787 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100788 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200789 break;
790 default:
791 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200792 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100793 evt->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200794 efi_signal_event(evt);
Alexander Grafbee91162016-03-04 01:09:59 +0100795 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200796 efi_process_event_queue();
Alexander Grafbee91162016-03-04 01:09:59 +0100797 WATCHDOG_RESET();
798}
799
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200800/**
Mario Six78a88f72018-07-10 08:40:17 +0200801 * 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
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200804 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200805 *
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 *
Mario Six78a88f72018-07-10 08:40:17 +0200809 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200810 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200811efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200812 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100813{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100814 /* Check that the event is valid */
815 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
816 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100817
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200818 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200819 * The parameter defines a multiple of 100 ns.
820 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200821 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200822 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100823
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100824 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;
Alexander Grafbee91162016-03-04 01:09:59 +0100834 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100835 event->trigger_type = type;
836 event->trigger_time = trigger_time;
837 event->is_signaled = false;
838 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200839}
840
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200841/**
Mario Six78a88f72018-07-10 08:40:17 +0200842 * 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
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200846 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200847 *
848 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200849 *
Mario Six78a88f72018-07-10 08:40:17 +0200850 * See the Unified Extensible Firmware Interface (UEFI) specification for
851 * details.
852 *
853 *
854 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200855 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200856static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
857 enum efi_timer_delay type,
858 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200859{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900860 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200861 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100862}
863
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200864/**
Mario Six78a88f72018-07-10 08:40:17 +0200865 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200869 *
870 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200871 *
Mario Six78a88f72018-07-10 08:40:17 +0200872 * See the Unified Extensible Firmware Interface (UEFI) specification for
873 * details.
874 *
875 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200876 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100877static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200878 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100879 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100880{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100881 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100882
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100883 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100884
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200885 /* Check parameters */
886 if (!num_events || !event)
887 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200888 /* Check TPL */
889 if (efi_tpl != TPL_APPLICATION)
890 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200891 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100892 if (efi_is_event(event[i]) != EFI_SUCCESS)
893 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200894 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
895 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200896 if (!event[i]->is_signaled)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200897 efi_queue_event(event[i]);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200898 }
899
900 /* Wait for signal */
901 for (;;) {
902 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200903 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200904 goto out;
905 }
906 /* Allow events to occur. */
907 efi_timer_check();
908 }
909
910out:
911 /*
912 * Reset the signal which is passed to the caller to allow periodic
913 * events to occur.
914 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200915 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200916 if (index)
917 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100918
919 return EFI_EXIT(EFI_SUCCESS);
920}
921
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200922/**
Mario Six78a88f72018-07-10 08:40:17 +0200923 * efi_signal_event_ext() - signal an EFI event
924 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200925 *
926 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200927 *
928 * See the Unified Extensible Firmware Interface (UEFI) specification for
929 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200930 *
931 * This functions sets the signaled state of the event and queues the
932 * notification function for execution.
933 *
Mario Six78a88f72018-07-10 08:40:17 +0200934 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200935 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200936static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100937{
938 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100939 if (efi_is_event(event) != EFI_SUCCESS)
940 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +0200941 efi_signal_event(event);
Alexander Grafbee91162016-03-04 01:09:59 +0100942 return EFI_EXIT(EFI_SUCCESS);
943}
944
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200945/**
Mario Six78a88f72018-07-10 08:40:17 +0200946 * efi_close_event() - close an EFI event
947 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200948 *
949 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200950 *
Mario Six78a88f72018-07-10 08:40:17 +0200951 * See the Unified Extensible Firmware Interface (UEFI) specification for
952 * details.
953 *
954 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200955 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200956static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100957{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200958 struct efi_register_notify_event *item, *next;
959
Alexander Grafbee91162016-03-04 01:09:59 +0100960 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100961 if (efi_is_event(event) != EFI_SUCCESS)
962 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200963
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) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +0200968 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 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200976 list_del(&item->link);
977 free(item);
978 }
979 }
Heinrich Schuchardt7a69e972019-06-05 21:00:39 +0200980 /* Remove event from queue */
981 if (efi_event_is_queued(event))
982 list_del(&event->queue_link);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +0200983
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100984 list_del(&event->link);
985 free(event);
986 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100987}
988
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200989/**
Mario Six78a88f72018-07-10 08:40:17 +0200990 * efi_check_event() - check if an event is signaled
991 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200992 *
993 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200994 *
Mario Six78a88f72018-07-10 08:40:17 +0200995 * See the Unified Extensible Firmware Interface (UEFI) specification for
996 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200997 *
Mario Six78a88f72018-07-10 08:40:17 +0200998 * If an event is not signaled yet, the notification function is queued. The
999 * signaled state is cleared.
1000 *
1001 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001002 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +02001003static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +01001004{
1005 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001006 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001007 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)
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001011 efi_queue_event(event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001012 if (event->is_signaled) {
1013 event->is_signaled = false;
1014 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02001015 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001016 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +01001017}
1018
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001019/**
Mario Six78a88f72018-07-10 08:40:17 +02001020 * efi_search_obj() - find the internal EFI object for a handle
1021 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001022 *
Mario Six78a88f72018-07-10 08:40:17 +02001023 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001024 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001025struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001026{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001027 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001028
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02001029 if (!handle)
1030 return NULL;
1031
Heinrich Schuchardt1b681532017-11-06 21:17:50 +01001032 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001033 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001034 return efiobj;
1035 }
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +02001036 return NULL;
1037}
1038
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001039/**
Mario Six78a88f72018-07-10 08:40:17 +02001040 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1041 * to a protocol
1042 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001043 *
Mario Six78a88f72018-07-10 08:40:17 +02001044 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001045 */
1046static 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001060/**
Mario Six78a88f72018-07-10 08:40:17 +02001061 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1062 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001063 *
Mario Six78a88f72018-07-10 08:40:17 +02001064 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001065 */
1066static 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001074/**
Mario Six78a88f72018-07-10 08:40:17 +02001075 * 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
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001079 *
Mario Six78a88f72018-07-10 08:40:17 +02001080 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001081 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001082efi_status_t efi_add_protocol(const efi_handle_t handle,
1083 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001084 void *protocol_interface)
1085{
1086 struct efi_object *efiobj;
1087 struct efi_handler *handler;
1088 efi_status_t ret;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001089 struct efi_register_notify_event *event;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001090
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;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001100 handler->guid = protocol;
1101 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001102 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001103 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001104
1105 /* Notify registered events */
1106 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001107 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(&notif->link, &event->handles);
Heinrich Schuchardtdaa3f842019-06-07 07:43:24 +02001118 event->event->is_signaled = false;
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001119 efi_signal_event(event->event);
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001120 }
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001121 }
1122
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001123 if (!guidcmp(&efi_guid_device_path, protocol))
1124 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001125 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001126}
1127
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001128/**
Mario Six78a88f72018-07-10 08:40:17 +02001129 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001135 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001136 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001137 *
Mario Six78a88f72018-07-10 08:40:17 +02001138 * See the Unified Extensible Firmware Interface (UEFI) specification for
1139 * details.
1140 *
1141 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001142 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001143static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001144 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001145 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001146{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001147 efi_status_t r;
1148
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001149 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1150 protocol_interface);
1151
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001152 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) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001160 r = efi_create_handle(handle);
1161 if (r != EFI_SUCCESS)
1162 goto out;
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001163 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001164 } else {
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02001165 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001166 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001167 /* Add new protocol */
1168 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001169out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001170 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001171}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001172
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001173/**
Mario Six78a88f72018-07-10 08:40:17 +02001174 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001175 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001176 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001177 * @number_of_drivers: number of child controllers
1178 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001179 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001180 * The allocated buffer has to be freed with free().
1181 *
Mario Six78a88f72018-07-10 08:40:17 +02001182 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001183 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001184static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001185 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 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001195 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001196 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 }
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001204 *number_of_drivers = 0;
1205 if (!count) {
1206 *driver_handle_buffer = NULL;
1207 return EFI_SUCCESS;
1208 }
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001209 /*
1210 * Create buffer. In case of duplicate driver assignments the buffer
1211 * will be too large. But that does not harm.
1212 */
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001213 *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 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001217 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001218 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001242/**
Mario Six78a88f72018-07-10 08:40:17 +02001243 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001244 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001245 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001246 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001247 *
1248 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001249 *
Mario Six78a88f72018-07-10 08:40:17 +02001250 * See the Unified Extensible Firmware Interface (UEFI) specification for
1251 * details.
1252 *
1253 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001254 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001255static efi_status_t efi_disconnect_all_drivers
1256 (efi_handle_t handle,
1257 const efi_guid_t *protocol,
1258 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001259{
1260 efi_uintn_t number_of_drivers;
1261 efi_handle_t *driver_handle_buffer;
1262 efi_status_t r, ret;
1263
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001264 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001265 &driver_handle_buffer);
1266 if (ret != EFI_SUCCESS)
1267 return ret;
Heinrich Schuchardt66ca24a2019-06-02 01:43:33 +02001268 if (!number_of_drivers)
1269 return EFI_SUCCESS;
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001270 ret = EFI_NOT_FOUND;
1271 while (number_of_drivers) {
1272 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001273 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001274 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
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001283/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001284 * efi_uninstall_protocol() - uninstall protocol interface
1285 *
Mario Six78a88f72018-07-10 08:40:17 +02001286 * @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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001289 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001290 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001291 *
1292 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001293 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001294static efi_status_t efi_uninstall_protocol
1295 (efi_handle_t handle, const efi_guid_t *protocol,
1296 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001297{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001298 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001299 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001300 struct efi_open_protocol_info_item *item;
1301 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001302 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001303
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001304 /* Check handle */
1305 efiobj = efi_search_obj(handle);
1306 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001307 r = EFI_INVALID_PARAMETER;
1308 goto out;
1309 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001310 /* Find the protocol on the handle */
1311 r = efi_search_protocol(handle, protocol, &handler);
1312 if (r != EFI_SUCCESS)
1313 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001314 /* Disconnect controllers */
1315 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001316 /* 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;
1327 }
1328 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001329out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001330 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 */
1346static 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 }
1363out:
1364 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001365}
1366
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001367/**
Mario Six78a88f72018-07-10 08:40:17 +02001368 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001373 *
1374 * This function implements the RegisterProtocolNotify service.
1375 * See the Unified Extensible Firmware Interface (UEFI) specification
1376 * for details.
1377 *
Mario Six78a88f72018-07-10 08:40:17 +02001378 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001379 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001380static efi_status_t EFIAPI efi_register_protocol_notify(
1381 const efi_guid_t *protocol,
1382 struct efi_event *event,
1383 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001384{
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001385 struct efi_register_notify_event *item;
1386 efi_status_t ret = EFI_SUCCESS;
1387
Rob Clark778e6af2017-09-13 18:05:41 -04001388 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001389
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));
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001403 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001404
1405 list_add_tail(&item->link, &efi_register_notify_events);
1406
1407 *registration = item;
1408out:
1409 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001410}
1411
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001412/**
Mario Six78a88f72018-07-10 08:40:17 +02001413 * 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
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001417 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001418 *
1419 * See the documentation of the LocateHandle service in the UEFI specification.
1420 *
Mario Six78a88f72018-07-10 08:40:17 +02001421 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001422 */
Alexander Grafbee91162016-03-04 01:09:59 +01001423static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001424 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001425{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001426 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001427
1428 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001429 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001430 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001431 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001432 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001433 return (ret != EFI_SUCCESS);
1434 default:
1435 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001436 return -1;
1437 }
Alexander Grafbee91162016-03-04 01:09:59 +01001438}
1439
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001440/**
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001441 * 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 */
1449static 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
1461/**
Mario Six78a88f72018-07-10 08:40:17 +02001462 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001463 *
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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001469 *
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 *
Mario Six78a88f72018-07-10 08:40:17 +02001473 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001474 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001475static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001476 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001477 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001478 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001479{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001480 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001481 efi_uintn_t size = 0;
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001482 struct efi_register_notify_event *event;
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001483 struct efi_protocol_notification *handle = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01001484
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001485 /* 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;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001492 /* Check that the registration key is valid */
Heinrich Schuchardtb8abd742019-05-29 07:46:33 +02001493 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001494 if (!event)
1495 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtab15d412019-05-04 17:27:54 +02001496 break;
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001497 case BY_PROTOCOL:
1498 if (!protocol)
1499 return EFI_INVALID_PARAMETER;
1500 break;
1501 default:
1502 return EFI_INVALID_PARAMETER;
1503 }
1504
Alexander Grafbee91162016-03-04 01:09:59 +01001505 /* Count how much space we need */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001506 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;
Alexander Grafbee91162016-03-04 01:09:59 +01001521 }
1522
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001523 if (!buffer_size)
1524 return EFI_INVALID_PARAMETER;
1525
Alexander Grafbee91162016-03-04 01:09:59 +01001526 if (*buffer_size < size) {
1527 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001528 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001529 }
1530
Rob Clark796a78c2017-08-06 14:10:07 -04001531 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001532
Heinrich Schuchardt0a843192019-05-10 19:03:49 +02001533 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001534 if (!buffer)
1535 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001536
Alexander Grafbee91162016-03-04 01:09:59 +01001537 /* Then fill the array */
Heinrich Schuchardtf09cea32019-05-21 18:19:01 +02001538 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 }
Alexander Grafbee91162016-03-04 01:09:59 +01001546 }
1547
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001548 return EFI_SUCCESS;
1549}
1550
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001551/**
Mario Six78a88f72018-07-10 08:40:17 +02001552 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001558 *
1559 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001560 *
Mario Six78a88f72018-07-10 08:40:17 +02001561 * See the Unified Extensible Firmware Interface (UEFI) specification for
1562 * details.
1563 *
1564 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001565 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001566static efi_status_t EFIAPI efi_locate_handle_ext(
1567 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001568 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001569 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001570{
Rob Clark778e6af2017-09-13 18:05:41 -04001571 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001572 buffer_size, buffer);
1573
1574 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1575 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001576}
1577
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001578/**
Mario Six78a88f72018-07-10 08:40:17 +02001579 * efi_remove_configuration_table() - collapses configuration table entries,
1580 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001581 *
Mario Six78a88f72018-07-10 08:40:17 +02001582 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001583 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001584static void efi_remove_configuration_table(int i)
1585{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001586 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];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001589
1590 memmove(this, next, (ulong)end - (ulong)next);
1591 systab.nr_tables--;
1592}
1593
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001594/**
Mario Six78a88f72018-07-10 08:40:17 +02001595 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001599 *
1600 * This function is used for internal calls. For the API implementation of the
1601 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1602 *
Mario Six78a88f72018-07-10 08:40:17 +02001603 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001604 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001605efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1606 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001607{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001608 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001609 int i;
1610
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001611 if (!guid)
1612 return EFI_INVALID_PARAMETER;
1613
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001614 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001615 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001616 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001617 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001618 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001619 else
1620 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001621 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001622 }
1623 }
1624
Alexander Grafd98cdf62017-07-26 13:41:04 +02001625 if (!table)
1626 return EFI_NOT_FOUND;
1627
Alexander Grafbee91162016-03-04 01:09:59 +01001628 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001629 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001630 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001631
1632 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001633 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1634 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001635 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001636
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001637out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001638 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001639 efi_update_table_header_crc32(&systab.hdr);
1640
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001641 /* Notify that the configuration table was changed */
1642 list_for_each_entry(evt, &efi_events, link) {
1643 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001644 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001645 break;
1646 }
1647 }
1648
Alexander Graf488bf122016-08-19 01:23:24 +02001649 return EFI_SUCCESS;
1650}
1651
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001652/**
Mario Six78a88f72018-07-10 08:40:17 +02001653 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001657 *
1658 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001659 *
Mario Six78a88f72018-07-10 08:40:17 +02001660 * See the Unified Extensible Firmware Interface (UEFI) specification for
1661 * details.
1662 *
1663 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001664 */
Alexander Graf488bf122016-08-19 01:23:24 +02001665static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1666 void *table)
1667{
Rob Clark778e6af2017-09-13 18:05:41 -04001668 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001669 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001670}
1671
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001672/**
Mario Six78a88f72018-07-10 08:40:17 +02001673 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001674 *
1675 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001676 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001677 *
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001678 * 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
Rob Clark95c55532017-09-13 18:05:33 -04001686 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001687efi_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)
Rob Clark95c55532017-09-13 18:05:33 -04001691{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001692 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001693 struct efi_loaded_image *info = NULL;
1694 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001695 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001696
1697 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1698 *handle_ptr = NULL;
1699 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001700
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 }
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02001709 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001710
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001711 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001712 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001713
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001714 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001715 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001716 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001717
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001718 if (device_path) {
1719 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001720
1721 dp = efi_dp_append(device_path, file_path);
1722 if (!dp) {
1723 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001724 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001725 }
1726 } else {
1727 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001728 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001729 ret = efi_add_protocol(&obj->header,
1730 &efi_guid_loaded_image_device_path, dp);
1731 if (ret != EFI_SUCCESS)
1732 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001733
1734 /*
1735 * When asking for the loaded_image interface, just
1736 * return handle which points to loaded_image_info
1737 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001738 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001739 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001740 if (ret != EFI_SUCCESS)
1741 goto failure;
1742
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001743 *info_ptr = info;
1744 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001745
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001746 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001747failure:
1748 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001749 efi_delete_handle(&obj->header);
1750 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001751 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001752}
1753
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001754/**
Mario Six78a88f72018-07-10 08:40:17 +02001755 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001756 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001757 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001764 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001765static
Rob Clark9975fe92017-09-13 18:05:38 -04001766efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001767 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001768{
1769 struct efi_file_info *info = NULL;
1770 struct efi_file_handle *f;
1771 static efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001772 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001773 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001774
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001775 /* In case of failure nothing is returned */
1776 *buffer = NULL;
1777 *size = 0;
1778
1779 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001780 f = efi_file_from_path(file_path);
1781 if (!f)
Heinrich Schuchardt20000032019-06-11 19:00:56 +02001782 return EFI_NOT_FOUND;
Rob Clark838ee4b2017-09-13 18:05:35 -04001783
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001784 /* Get file size */
Rob Clark838ee4b2017-09-13 18:05:35 -04001785 bs = 0;
1786 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1787 &bs, info));
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001788 if (ret != EFI_BUFFER_TOO_SMALL) {
1789 ret = EFI_DEVICE_ERROR;
1790 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001791 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001792
1793 info = malloc(bs);
1794 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1795 info));
Rob Clark838ee4b2017-09-13 18:05:35 -04001796 if (ret != EFI_SUCCESS)
1797 goto error;
1798
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001799 /*
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 */
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001805 bs = info->file_size;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001806 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1807 EFI_BOOT_SERVICES_DATA,
1808 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001809 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001810 ret = EFI_OUT_OF_RESOURCES;
1811 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001812 }
1813
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001814 /* 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;
1820error:
1821 EFI_CALL(f->close(f));
1822 free(info);
Rob Clark838ee4b2017-09-13 18:05:35 -04001823 return ret;
1824}
1825
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001826/**
Mario Six78a88f72018-07-10 08:40:17 +02001827 * 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
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001834 *
1835 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001836 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001837 * See the Unified Extensible Firmware Interface (UEFI) specification
1838 * for details.
1839 *
Mario Six78a88f72018-07-10 08:40:17 +02001840 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001841 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09001842efi_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)
Alexander Grafbee91162016-03-04 01:09:59 +01001848{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001849 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04001850 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001851 struct efi_loaded_image_obj **image_obj =
1852 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001853 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001854 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01001855
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001856 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001857 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001858
Heinrich Schuchardte4afcb22019-06-11 18:52:33 +02001859 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) {
Heinrich Schuchardt84a918e2019-05-05 16:55:06 +02001863 ret = EFI_INVALID_PARAMETER;
1864 goto error;
1865 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001866
Rob Clark838ee4b2017-09-13 18:05:35 -04001867 if (!source_buffer) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001868 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001869 &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001870 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001871 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001872 } else {
Heinrich Schuchardt470dfa52019-05-05 16:55:06 +02001873 if (!source_size) {
1874 ret = EFI_LOAD_ERROR;
1875 goto error;
1876 }
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001877 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04001878 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00001879 /* split file_path which contains both the device and file parts */
1880 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001881 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001882 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 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001897error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001898 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001899}
1900
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001901/**
Alexander Graff31239a2018-11-15 21:23:47 +01001902 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1903 */
1904static 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
1916/**
Mario Six78a88f72018-07-10 08:40:17 +02001917 * efi_exit_boot_services() - stop all boot services
1918 * @image_handle: handle of the loaded image
1919 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001920 *
1921 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001922 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001923 * See the Unified Extensible Firmware Interface (UEFI) specification
1924 * for details.
1925 *
Mario Six78a88f72018-07-10 08:40:17 +02001926 * 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.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001929 *
Mario Six78a88f72018-07-10 08:40:17 +02001930 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001931 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001932static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02001933 efi_uintn_t map_key)
Alexander Grafbee91162016-03-04 01:09:59 +01001934{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001935 struct efi_event *evt;
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001936 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001937
Heinrich Schuchardtb015ab52019-05-05 21:58:35 +02001938 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafbee91162016-03-04 01:09:59 +01001939
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001940 /* Check that the caller has read the current memory map */
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001941 if (map_key != efi_memory_map_key) {
1942 ret = EFI_INVALID_PARAMETER;
1943 goto out;
1944 }
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001945
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001946 /* Check if ExitBootServices has already been called */
1947 if (!systab.boottime)
Heinrich Schuchardt98967372019-06-11 20:05:40 +02001948 goto out;
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001949
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001950 /* Stop all timer related activities */
1951 timers_enabled = false;
1952
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001953 /* 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 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001958 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001959 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001960 if (evt->group &&
1961 !guidcmp(evt->group,
1962 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001963 efi_signal_event(evt);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001964 break;
1965 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001966 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001967
Heinrich Schuchardt7eaa9002019-06-07 06:47:01 +02001968 /* Make sure that notification functions are not called anymore */
1969 efi_tpl = TPL_HIGH_LEVEL;
1970
Heinrich Schuchardt29018ab2019-06-20 15:25:48 +02001971 /* Notify variable services */
1972 efi_variables_boot_exit_notify();
Rob Clarkad644e72017-09-13 18:05:37 -04001973
Alexander Grafb7b84102016-11-17 01:02:57 +01001974 board_quiesce_devices();
1975
Heinrich Schuchardt7f951042019-07-05 17:42:16 +02001976 /* Patch out unsupported runtime function */
1977 efi_runtime_detach();
1978
Alexander Graff31239a2018-11-15 21:23:47 +01001979 /* Fix up caches for EFI payloads if necessary */
1980 efi_exit_caches();
1981
Alexander Grafbee91162016-03-04 01:09:59 +01001982 /* This stops all lingering devices */
1983 bootm_disable_interrupts();
1984
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001985 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001986 systab.con_in_handle = NULL;
1987 systab.con_in = NULL;
1988 systab.con_out_handle = NULL;
1989 systab.con_out = NULL;
1990 systab.stderr_handle = NULL;
1991 systab.std_err = NULL;
1992 systab.boottime = NULL;
1993
1994 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001995 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001996
Alexander Grafbee91162016-03-04 01:09:59 +01001997 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001998 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001999 WATCHDOG_RESET();
Heinrich Schuchardt98967372019-06-11 20:05:40 +02002000out:
2001 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002002}
2003
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002004/**
Mario Six78a88f72018-07-10 08:40:17 +02002005 * efi_get_next_monotonic_count() - get next value of the counter
2006 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002007 *
2008 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002009 *
Mario Six78a88f72018-07-10 08:40:17 +02002010 * See the Unified Extensible Firmware Interface (UEFI) specification for
2011 * details.
2012 *
2013 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002014 */
Alexander Grafbee91162016-03-04 01:09:59 +01002015static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2016{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002017 static uint64_t mono;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002018 efi_status_t ret;
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002019
Alexander Grafbee91162016-03-04 01:09:59 +01002020 EFI_ENTRY("%p", count);
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002021 if (!count) {
2022 ret = EFI_INVALID_PARAMETER;
2023 goto out;
2024 }
Alexander Grafbee91162016-03-04 01:09:59 +01002025 *count = mono++;
Heinrich Schuchardt1344f7d2019-05-17 12:47:17 +02002026 ret = EFI_SUCCESS;
2027out:
2028 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002029}
2030
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002031/**
Mario Six78a88f72018-07-10 08:40:17 +02002032 * efi_stall() - sleep
2033 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002034 *
Mario Six78a88f72018-07-10 08:40:17 +02002035 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002036 *
Mario Six78a88f72018-07-10 08:40:17 +02002037 * See the Unified Extensible Firmware Interface (UEFI) specification for
2038 * details.
2039 *
2040 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002041 */
Alexander Grafbee91162016-03-04 01:09:59 +01002042static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2043{
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002044 u64 end_tick;
2045
Alexander Grafbee91162016-03-04 01:09:59 +01002046 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt22f23db2019-06-02 21:12:17 +02002047
2048 end_tick = get_ticks() + usec_to_tick(microseconds);
2049 while (get_ticks() < end_tick)
2050 efi_timer_check();
2051
Alexander Grafbee91162016-03-04 01:09:59 +01002052 return EFI_EXIT(EFI_SUCCESS);
2053}
2054
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002055/**
Mario Six78a88f72018-07-10 08:40:17 +02002056 * efi_set_watchdog_timer() - reset the watchdog timer
2057 * @timeout: seconds before reset by watchdog
2058 * @watchdog_code: code to be logged when resetting
2059 * @data_size: size of buffer in bytes
2060 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002061 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002062 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002063 *
Mario Six78a88f72018-07-10 08:40:17 +02002064 * See the Unified Extensible Firmware Interface (UEFI) specification for
2065 * details.
2066 *
2067 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002068 */
Alexander Grafbee91162016-03-04 01:09:59 +01002069static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2070 uint64_t watchdog_code,
2071 unsigned long data_size,
2072 uint16_t *watchdog_data)
2073{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09002074 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01002075 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02002076 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01002077}
2078
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002079/**
Mario Six78a88f72018-07-10 08:40:17 +02002080 * efi_close_protocol() - close a protocol
2081 * @handle: handle on which the protocol shall be closed
2082 * @protocol: GUID of the protocol to close
2083 * @agent_handle: handle of the driver
2084 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002085 *
2086 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002087 *
Mario Six78a88f72018-07-10 08:40:17 +02002088 * See the Unified Extensible Firmware Interface (UEFI) specification for
2089 * details.
2090 *
2091 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002092 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002093static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002094 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002095 efi_handle_t agent_handle,
2096 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01002097{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002098 struct efi_handler *handler;
2099 struct efi_open_protocol_info_item *item;
2100 struct efi_open_protocol_info_item *pos;
2101 efi_status_t r;
2102
Rob Clark778e6af2017-09-13 18:05:41 -04002103 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01002104 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002105
Heinrich Schuchardtec163fa2019-05-05 10:37:51 +02002106 if (!efi_search_obj(agent_handle) ||
2107 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002108 r = EFI_INVALID_PARAMETER;
2109 goto out;
2110 }
2111 r = efi_search_protocol(handle, protocol, &handler);
2112 if (r != EFI_SUCCESS)
2113 goto out;
2114
2115 r = EFI_NOT_FOUND;
2116 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2117 if (item->info.agent_handle == agent_handle &&
2118 item->info.controller_handle == controller_handle) {
2119 efi_delete_open_info(item);
2120 r = EFI_SUCCESS;
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01002121 }
2122 }
2123out:
2124 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002125}
2126
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002127/**
Mario Six78a88f72018-07-10 08:40:17 +02002128 * efi_open_protocol_information() - provide information about then open status
2129 * of a protocol on a handle
2130 * @handle: handle for which the information shall be retrieved
2131 * @protocol: GUID of the protocol
2132 * @entry_buffer: buffer to receive the open protocol information
2133 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002134 *
2135 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002136 *
Mario Six78a88f72018-07-10 08:40:17 +02002137 * See the Unified Extensible Firmware Interface (UEFI) specification for
2138 * details.
2139 *
2140 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002141 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002142static efi_status_t EFIAPI efi_open_protocol_information(
2143 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002144 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002145 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002146{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002147 unsigned long buffer_size;
2148 unsigned long count;
2149 struct efi_handler *handler;
2150 struct efi_open_protocol_info_item *item;
2151 efi_status_t r;
2152
Rob Clark778e6af2017-09-13 18:05:41 -04002153 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002154 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002155
2156 /* Check parameters */
2157 if (!entry_buffer) {
2158 r = EFI_INVALID_PARAMETER;
2159 goto out;
2160 }
2161 r = efi_search_protocol(handle, protocol, &handler);
2162 if (r != EFI_SUCCESS)
2163 goto out;
2164
2165 /* Count entries */
2166 count = 0;
2167 list_for_each_entry(item, &handler->open_infos, link) {
2168 if (item->info.open_count)
2169 ++count;
2170 }
2171 *entry_count = count;
2172 *entry_buffer = NULL;
2173 if (!count) {
2174 r = EFI_SUCCESS;
2175 goto out;
2176 }
2177
2178 /* Copy entries */
2179 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002180 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002181 (void **)entry_buffer);
2182 if (r != EFI_SUCCESS)
2183 goto out;
2184 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2185 if (item->info.open_count)
2186 (*entry_buffer)[--count] = item->info;
2187 }
2188out:
2189 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002190}
2191
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002192/**
Mario Six78a88f72018-07-10 08:40:17 +02002193 * efi_protocols_per_handle() - get protocols installed on a handle
2194 * @handle: handle for which the information is retrieved
2195 * @protocol_buffer: buffer with protocol GUIDs
2196 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002197 *
2198 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002199 *
Mario Six78a88f72018-07-10 08:40:17 +02002200 * See the Unified Extensible Firmware Interface (UEFI) specification for
2201 * details.
2202 *
2203 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002204 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002205static efi_status_t EFIAPI efi_protocols_per_handle(
2206 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002207 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002208{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002209 unsigned long buffer_size;
2210 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002211 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002212 efi_status_t r;
2213
Alexander Grafbee91162016-03-04 01:09:59 +01002214 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2215 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002216
2217 if (!handle || !protocol_buffer || !protocol_buffer_count)
2218 return EFI_EXIT(EFI_INVALID_PARAMETER);
2219
2220 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002221 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002222
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002223 efiobj = efi_search_obj(handle);
2224 if (!efiobj)
2225 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002226
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002227 /* Count protocols */
2228 list_for_each(protocol_handle, &efiobj->protocols) {
2229 ++*protocol_buffer_count;
2230 }
2231
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002232 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002233 if (*protocol_buffer_count) {
2234 size_t j = 0;
2235
2236 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002237 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002238 (void **)protocol_buffer);
2239 if (r != EFI_SUCCESS)
2240 return EFI_EXIT(r);
2241 list_for_each(protocol_handle, &efiobj->protocols) {
2242 struct efi_handler *protocol;
2243
2244 protocol = list_entry(protocol_handle,
2245 struct efi_handler, link);
2246 (*protocol_buffer)[j] = (void *)protocol->guid;
2247 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002248 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002249 }
2250
2251 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002252}
2253
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002254/**
Mario Six78a88f72018-07-10 08:40:17 +02002255 * efi_locate_handle_buffer() - locate handles implementing a protocol
2256 * @search_type: selection criterion
2257 * @protocol: GUID of the protocol
2258 * @search_key: registration key
2259 * @no_handles: number of returned handles
2260 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002261 *
2262 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002263 *
Mario Six78a88f72018-07-10 08:40:17 +02002264 * See the Unified Extensible Firmware Interface (UEFI) specification for
2265 * details.
2266 *
2267 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002268 */
Alexander Grafbee91162016-03-04 01:09:59 +01002269static efi_status_t EFIAPI efi_locate_handle_buffer(
2270 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002271 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002272 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002273{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002274 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002275 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002276
Rob Clark778e6af2017-09-13 18:05:41 -04002277 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002278 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002279
2280 if (!no_handles || !buffer) {
2281 r = EFI_INVALID_PARAMETER;
2282 goto out;
2283 }
2284 *no_handles = 0;
2285 *buffer = NULL;
2286 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2287 *buffer);
2288 if (r != EFI_BUFFER_TOO_SMALL)
2289 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002290 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002291 (void **)buffer);
2292 if (r != EFI_SUCCESS)
2293 goto out;
2294 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2295 *buffer);
2296 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002297 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002298out:
2299 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002300}
2301
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002302/**
Mario Six78a88f72018-07-10 08:40:17 +02002303 * efi_locate_protocol() - find an interface implementing a protocol
2304 * @protocol: GUID of the protocol
2305 * @registration: registration key passed to the notification function
2306 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002307 *
2308 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002309 *
Mario Six78a88f72018-07-10 08:40:17 +02002310 * See the Unified Extensible Firmware Interface (UEFI) specification for
2311 * details.
2312 *
2313 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002314 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002315static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002316 void *registration,
2317 void **protocol_interface)
2318{
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002319 struct efi_handler *handler;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002320 efi_status_t ret;
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002321 struct efi_object *efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01002322
Rob Clark778e6af2017-09-13 18:05:41 -04002323 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002324
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002325 /*
2326 * The UEFI spec explicitly requires a protocol even if a registration
2327 * key is provided. This differs from the logic in LocateHandle().
2328 */
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002329 if (!protocol || !protocol_interface)
2330 return EFI_EXIT(EFI_INVALID_PARAMETER);
2331
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002332 if (registration) {
2333 struct efi_register_notify_event *event;
2334 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002335
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002336 event = efi_check_register_notify_event(registration);
2337 if (!event)
2338 return EFI_EXIT(EFI_INVALID_PARAMETER);
2339 /*
2340 * The UEFI spec requires to return EFI_NOT_FOUND if no
2341 * protocol instance matches protocol and registration.
2342 * So let's do the same for a mismatch between protocol and
2343 * registration.
2344 */
2345 if (guidcmp(&event->protocol, protocol))
2346 goto not_found;
2347 if (list_empty(&event->handles))
2348 goto not_found;
2349 handle = list_first_entry(&event->handles,
2350 struct efi_protocol_notification,
2351 link);
2352 efiobj = handle->handle;
2353 list_del(&handle->link);
2354 free(handle);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002355 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002356 if (ret == EFI_SUCCESS)
2357 goto found;
2358 } else {
2359 list_for_each_entry(efiobj, &efi_obj_list, link) {
2360 ret = efi_search_protocol(efiobj, protocol, &handler);
2361 if (ret == EFI_SUCCESS)
2362 goto found;
Alexander Grafbee91162016-03-04 01:09:59 +01002363 }
2364 }
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002365not_found:
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002366 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002367 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardte31b3b12019-05-29 18:06:46 +02002368found:
2369 *protocol_interface = handler->protocol_interface;
2370 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002371}
2372
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002373/**
Mario Six78a88f72018-07-10 08:40:17 +02002374 * efi_locate_device_path() - Get the device path and handle of an device
2375 * implementing a protocol
2376 * @protocol: GUID of the protocol
2377 * @device_path: device path
2378 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002379 *
2380 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002381 *
Mario Six78a88f72018-07-10 08:40:17 +02002382 * See the Unified Extensible Firmware Interface (UEFI) specification for
2383 * details.
2384 *
2385 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002386 */
2387static efi_status_t EFIAPI efi_locate_device_path(
2388 const efi_guid_t *protocol,
2389 struct efi_device_path **device_path,
2390 efi_handle_t *device)
2391{
2392 struct efi_device_path *dp;
2393 size_t i;
2394 struct efi_handler *handler;
2395 efi_handle_t *handles;
2396 size_t len, len_dp;
2397 size_t len_best = 0;
2398 efi_uintn_t no_handles;
2399 u8 *remainder;
2400 efi_status_t ret;
2401
2402 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2403
Heinrich Schuchardtab557142019-05-10 19:21:41 +02002404 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002405 ret = EFI_INVALID_PARAMETER;
2406 goto out;
2407 }
2408
2409 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002410 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002411
2412 /* Get all handles implementing the protocol */
2413 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2414 &no_handles, &handles));
2415 if (ret != EFI_SUCCESS)
2416 goto out;
2417
2418 for (i = 0; i < no_handles; ++i) {
2419 /* Find the device path protocol */
2420 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2421 &handler);
2422 if (ret != EFI_SUCCESS)
2423 continue;
2424 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002425 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002426 /*
2427 * This handle can only be a better fit
2428 * if its device path length is longer than the best fit and
2429 * if its device path length is shorter of equal the searched
2430 * device path.
2431 */
2432 if (len_dp <= len_best || len_dp > len)
2433 continue;
2434 /* Check if dp is a subpath of device_path */
2435 if (memcmp(*device_path, dp, len_dp))
2436 continue;
Heinrich Schuchardtab557142019-05-10 19:21:41 +02002437 if (!device) {
2438 ret = EFI_INVALID_PARAMETER;
2439 goto out;
2440 }
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002441 *device = handles[i];
2442 len_best = len_dp;
2443 }
2444 if (len_best) {
2445 remainder = (u8 *)*device_path + len_best;
2446 *device_path = (struct efi_device_path *)remainder;
2447 ret = EFI_SUCCESS;
2448 } else {
2449 ret = EFI_NOT_FOUND;
2450 }
2451out:
2452 return EFI_EXIT(ret);
2453}
2454
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002455/**
Mario Six78a88f72018-07-10 08:40:17 +02002456 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2457 * interfaces
2458 * @handle: handle on which the protocol interfaces shall be installed
2459 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2460 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002461 *
2462 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002463 *
Mario Six78a88f72018-07-10 08:40:17 +02002464 * See the Unified Extensible Firmware Interface (UEFI) specification for
2465 * details.
2466 *
2467 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002468 */
Heinrich Schuchardt7657ae12019-04-12 06:59:49 +02002469efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002470 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002471{
2472 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002473
Alexander Grafbeb077a2018-06-18 17:23:05 +02002474 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002475 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002476 void *protocol_interface;
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002477 efi_handle_t old_handle;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002478 efi_status_t r = EFI_SUCCESS;
2479 int i = 0;
2480
2481 if (!handle)
2482 return EFI_EXIT(EFI_INVALID_PARAMETER);
2483
Alexander Grafbeb077a2018-06-18 17:23:05 +02002484 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002485 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002486 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002487 if (!protocol)
2488 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002489 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002490 /* Check that a device path has not been installed before */
2491 if (!guidcmp(protocol, &efi_guid_device_path)) {
2492 struct efi_device_path *dp = protocol_interface;
2493
2494 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2495 &old_handle));
Heinrich Schuchardt20562892019-05-20 19:55:18 +00002496 if (r == EFI_SUCCESS &&
2497 dp->type == DEVICE_PATH_TYPE_END) {
2498 EFI_PRINT("Path %pD already installed\n",
2499 protocol_interface);
Heinrich Schuchardt226cddb2019-05-16 21:54:04 +02002500 r = EFI_ALREADY_STARTED;
2501 break;
2502 }
2503 }
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002504 r = EFI_CALL(efi_install_protocol_interface(
2505 handle, protocol,
2506 EFI_NATIVE_INTERFACE,
2507 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002508 if (r != EFI_SUCCESS)
2509 break;
2510 i++;
2511 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002512 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002513 if (r == EFI_SUCCESS)
2514 return EFI_EXIT(r);
2515
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002516 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002517 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002518 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002519 protocol = efi_va_arg(argptr, efi_guid_t*);
2520 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002521 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002522 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002523 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002524 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002525
2526 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002527}
2528
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002529/**
Mario Six78a88f72018-07-10 08:40:17 +02002530 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2531 * interfaces
2532 * @handle: handle from which the protocol interfaces shall be removed
2533 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2534 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002535 *
2536 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002537 *
Mario Six78a88f72018-07-10 08:40:17 +02002538 * See the Unified Extensible Firmware Interface (UEFI) specification for
2539 * details.
2540 *
2541 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002542 */
Alexander Grafbee91162016-03-04 01:09:59 +01002543static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002544 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002545{
2546 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002547
Alexander Grafbeb077a2018-06-18 17:23:05 +02002548 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002549 const efi_guid_t *protocol;
2550 void *protocol_interface;
2551 efi_status_t r = EFI_SUCCESS;
2552 size_t i = 0;
2553
2554 if (!handle)
2555 return EFI_EXIT(EFI_INVALID_PARAMETER);
2556
Alexander Grafbeb077a2018-06-18 17:23:05 +02002557 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002558 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002559 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002560 if (!protocol)
2561 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002562 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002563 r = efi_uninstall_protocol(handle, protocol,
2564 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002565 if (r != EFI_SUCCESS)
2566 break;
2567 i++;
2568 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002569 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002570 if (r == EFI_SUCCESS) {
2571 /* If the last protocol has been removed, delete the handle. */
2572 if (list_empty(&handle->protocols)) {
2573 list_del(&handle->link);
2574 free(handle);
2575 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002576 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002577 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002578
2579 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002580 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002581 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002582 protocol = efi_va_arg(argptr, efi_guid_t*);
2583 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002584 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2585 EFI_NATIVE_INTERFACE,
2586 protocol_interface));
2587 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002588 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002589
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002590 /* In case of an error always return EFI_INVALID_PARAMETER */
2591 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002592}
2593
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002594/**
Mario Six78a88f72018-07-10 08:40:17 +02002595 * efi_calculate_crc32() - calculate cyclic redundancy code
2596 * @data: buffer with data
2597 * @data_size: size of buffer in bytes
2598 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002599 *
2600 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002601 *
Mario Six78a88f72018-07-10 08:40:17 +02002602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2603 * details.
2604 *
2605 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002606 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002607static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2608 efi_uintn_t data_size,
2609 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002610{
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002611 efi_status_t ret = EFI_SUCCESS;
2612
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002613 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002614 if (!data || !data_size || !crc32_p) {
2615 ret = EFI_INVALID_PARAMETER;
2616 goto out;
2617 }
Alexander Grafbee91162016-03-04 01:09:59 +01002618 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardtdb80fe32019-05-16 23:31:29 +02002619out:
2620 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01002621}
2622
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002623/**
Mario Six78a88f72018-07-10 08:40:17 +02002624 * efi_copy_mem() - copy memory
2625 * @destination: destination of the copy operation
2626 * @source: source of the copy operation
2627 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002628 *
2629 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002630 *
Mario Six78a88f72018-07-10 08:40:17 +02002631 * See the Unified Extensible Firmware Interface (UEFI) specification for
2632 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002633 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002634static void EFIAPI efi_copy_mem(void *destination, const void *source,
2635 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002636{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002637 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002638 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002639 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002640}
2641
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002642/**
Mario Six78a88f72018-07-10 08:40:17 +02002643 * efi_set_mem() - Fill memory with a byte value.
2644 * @buffer: buffer to fill
2645 * @size: size of buffer in bytes
2646 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002647 *
2648 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002649 *
Mario Six78a88f72018-07-10 08:40:17 +02002650 * See the Unified Extensible Firmware Interface (UEFI) specification for
2651 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002652 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002653static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002654{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002655 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002656 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002657 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002658}
2659
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002660/**
Mario Six78a88f72018-07-10 08:40:17 +02002661 * efi_protocol_open() - open protocol interface on a handle
2662 * @handler: handler of a protocol
2663 * @protocol_interface: interface implementing the protocol
2664 * @agent_handle: handle of the driver
2665 * @controller_handle: handle of the controller
2666 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002667 *
Mario Six78a88f72018-07-10 08:40:17 +02002668 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002669 */
2670static efi_status_t efi_protocol_open(
2671 struct efi_handler *handler,
2672 void **protocol_interface, void *agent_handle,
2673 void *controller_handle, uint32_t attributes)
2674{
2675 struct efi_open_protocol_info_item *item;
2676 struct efi_open_protocol_info_entry *match = NULL;
2677 bool opened_by_driver = false;
2678 bool opened_exclusive = false;
2679
2680 /* If there is no agent, only return the interface */
2681 if (!agent_handle)
2682 goto out;
2683
2684 /* For TEST_PROTOCOL ignore interface attribute */
2685 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2686 *protocol_interface = NULL;
2687
2688 /*
2689 * Check if the protocol is already opened by a driver with the same
2690 * attributes or opened exclusively
2691 */
2692 list_for_each_entry(item, &handler->open_infos, link) {
2693 if (item->info.agent_handle == agent_handle) {
2694 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2695 (item->info.attributes == attributes))
2696 return EFI_ALREADY_STARTED;
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002697 } else {
2698 if (item->info.attributes &
2699 EFI_OPEN_PROTOCOL_BY_DRIVER)
2700 opened_by_driver = true;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002701 }
2702 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2703 opened_exclusive = true;
2704 }
2705
2706 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardt399a39e2019-05-30 14:16:31 +02002707 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2708 if (opened_exclusive)
2709 return EFI_ACCESS_DENIED;
2710 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2711 if (opened_exclusive || opened_by_driver)
2712 return EFI_ACCESS_DENIED;
2713 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002714
2715 /* Prepare exclusive opening */
2716 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2717 /* Try to disconnect controllers */
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002718disconnect_next:
2719 opened_by_driver = false;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002720 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002721 efi_status_t ret;
2722
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002723 if (item->info.attributes ==
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002724 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2725 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002726 item->info.controller_handle,
2727 item->info.agent_handle,
2728 NULL));
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002729 if (ret == EFI_SUCCESS)
2730 /*
2731 * Child controllers may have been
2732 * removed from the open_infos list. So
2733 * let's restart the loop.
2734 */
2735 goto disconnect_next;
2736 else
2737 opened_by_driver = true;
2738 }
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002739 }
Heinrich Schuchardtdae7ce42019-05-30 14:16:31 +02002740 /* Only one driver can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002741 if (opened_by_driver)
2742 return EFI_ACCESS_DENIED;
2743 }
2744
2745 /* Find existing entry */
2746 list_for_each_entry(item, &handler->open_infos, link) {
2747 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardtb4863ba2019-06-01 20:15:10 +02002748 item->info.controller_handle == controller_handle &&
2749 item->info.attributes == attributes)
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002750 match = &item->info;
2751 }
2752 /* None found, create one */
2753 if (!match) {
2754 match = efi_create_open_info(handler);
2755 if (!match)
2756 return EFI_OUT_OF_RESOURCES;
2757 }
2758
2759 match->agent_handle = agent_handle;
2760 match->controller_handle = controller_handle;
2761 match->attributes = attributes;
2762 match->open_count++;
2763
2764out:
2765 /* For TEST_PROTOCOL ignore interface attribute. */
2766 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2767 *protocol_interface = handler->protocol_interface;
2768
2769 return EFI_SUCCESS;
2770}
2771
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002772/**
Mario Six78a88f72018-07-10 08:40:17 +02002773 * efi_open_protocol() - open protocol interface on a handle
2774 * @handle: handle on which the protocol shall be opened
2775 * @protocol: GUID of the protocol
2776 * @protocol_interface: interface implementing the protocol
2777 * @agent_handle: handle of the driver
2778 * @controller_handle: handle of the controller
2779 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002780 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002781 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002782 *
Mario Six78a88f72018-07-10 08:40:17 +02002783 * See the Unified Extensible Firmware Interface (UEFI) specification for
2784 * details.
2785 *
2786 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002787 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002788static efi_status_t EFIAPI efi_open_protocol
2789 (efi_handle_t handle, const efi_guid_t *protocol,
2790 void **protocol_interface, efi_handle_t agent_handle,
2791 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002792{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002793 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002794 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002795
Rob Clark778e6af2017-09-13 18:05:41 -04002796 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002797 protocol_interface, agent_handle, controller_handle,
2798 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002799
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002800 if (!handle || !protocol ||
2801 (!protocol_interface && attributes !=
2802 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2803 goto out;
2804 }
2805
2806 switch (attributes) {
2807 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2808 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2809 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2810 break;
2811 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2812 if (controller_handle == handle)
2813 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002814 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002815 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2816 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002817 /* Check that the controller handle is valid */
2818 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002819 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002820 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002821 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002822 /* Check that the agent handle is valid */
2823 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002824 goto out;
2825 break;
2826 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002827 goto out;
2828 }
2829
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002830 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002831 switch (r) {
2832 case EFI_SUCCESS:
2833 break;
2834 case EFI_NOT_FOUND:
2835 r = EFI_UNSUPPORTED;
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002836 goto out;
Heinrich Schuchardte7c3cd62019-05-05 11:24:53 +02002837 default:
2838 goto out;
2839 }
Alexander Grafbee91162016-03-04 01:09:59 +01002840
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002841 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2842 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002843out:
2844 return EFI_EXIT(r);
2845}
2846
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002847/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002848 * efi_start_image() - call the entry point of an image
2849 * @image_handle: handle of the image
2850 * @exit_data_size: size of the buffer
2851 * @exit_data: buffer to receive the exit data of the called image
2852 *
2853 * This function implements the StartImage service.
2854 *
2855 * See the Unified Extensible Firmware Interface (UEFI) specification for
2856 * details.
2857 *
2858 * Return: status code
2859 */
2860efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2861 efi_uintn_t *exit_data_size,
2862 u16 **exit_data)
2863{
2864 struct efi_loaded_image_obj *image_obj =
2865 (struct efi_loaded_image_obj *)image_handle;
2866 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002867 void *info;
2868 efi_handle_t parent_image = current_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002869
2870 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2871
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002872 /* Check parameters */
Heinrich Schuchardt1d3e8dc2019-06-11 19:35:20 +02002873 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2874 return EFI_EXIT(EFI_INVALID_PARAMETER);
2875
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002876 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2877 &info, NULL, NULL,
2878 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2879 if (ret != EFI_SUCCESS)
2880 return EFI_EXIT(EFI_INVALID_PARAMETER);
2881
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002882 efi_is_direct_boot = false;
2883
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02002884 image_obj->exit_data_size = exit_data_size;
2885 image_obj->exit_data = exit_data;
2886
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002887 /* call the image! */
2888 if (setjmp(&image_obj->exit_jmp)) {
2889 /*
2890 * We called the entry point of the child image with EFI_CALL
2891 * in the lines below. The child image called the Exit() boot
2892 * service efi_exit() which executed the long jump that brought
2893 * us to the current line. This implies that the second half
2894 * of the EFI_CALL macro has not been executed.
2895 */
2896#ifdef CONFIG_ARM
2897 /*
2898 * efi_exit() called efi_restore_gd(). We have to undo this
2899 * otherwise __efi_entry_check() will put the wrong value into
2900 * app_gd.
2901 */
2902 gd = app_gd;
2903#endif
2904 /*
2905 * To get ready to call EFI_EXIT below we have to execute the
2906 * missed out steps of EFI_CALL.
2907 */
2908 assert(__efi_entry_check());
Heinrich Schuchardt529886a2019-05-05 11:56:23 +02002909 EFI_PRINT("%lu returned by started image\n",
2910 (unsigned long)((uintptr_t)image_obj->exit_status &
2911 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002912 current_image = parent_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002913 return EFI_EXIT(image_obj->exit_status);
2914 }
2915
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002916 current_image = image_handle;
Heinrich Schuchardtcd73aba2019-05-01 14:20:18 +02002917 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09002918 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002919 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2920
2921 /*
2922 * Usually UEFI applications call Exit() instead of returning.
2923 * But because the world doesn't consist of ponies and unicorns,
2924 * we're happy to emulate that behavior on behalf of a payload
2925 * that forgot.
2926 */
2927 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2928}
2929
2930/**
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002931 * efi_delete_image() - delete loaded image from memory)
2932 *
2933 * @image_obj: handle of the loaded image
2934 * @loaded_image_protocol: loaded image protocol
2935 */
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02002936static efi_status_t efi_delete_image
2937 (struct efi_loaded_image_obj *image_obj,
2938 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002939{
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02002940 struct efi_object *efiobj;
2941 efi_status_t r, ret = EFI_SUCCESS;
2942
2943close_next:
2944 list_for_each_entry(efiobj, &efi_obj_list, link) {
2945 struct efi_handler *protocol;
2946
2947 list_for_each_entry(protocol, &efiobj->protocols, link) {
2948 struct efi_open_protocol_info_item *info;
2949
2950 list_for_each_entry(info, &protocol->open_infos, link) {
2951 if (info->info.agent_handle !=
2952 (efi_handle_t)image_obj)
2953 continue;
2954 r = EFI_CALL(efi_close_protocol
2955 (efiobj, protocol->guid,
2956 info->info.agent_handle,
2957 info->info.controller_handle
2958 ));
2959 if (r != EFI_SUCCESS)
2960 ret = r;
2961 /*
2962 * Closing protocols may results in further
2963 * items being deleted. To play it safe loop
2964 * over all elements again.
2965 */
2966 goto close_next;
2967 }
2968 }
2969 }
2970
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002971 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2972 efi_size_in_pages(loaded_image_protocol->image_size));
2973 efi_delete_handle(&image_obj->header);
Heinrich Schuchardt120ff7b2019-06-02 20:02:32 +02002974
2975 return ret;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002976}
2977
2978/**
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02002979 * efi_unload_image() - unload an EFI image
2980 * @image_handle: handle of the image to be unloaded
2981 *
2982 * This function implements the UnloadImage service.
2983 *
2984 * See the Unified Extensible Firmware Interface (UEFI) specification for
2985 * details.
2986 *
2987 * Return: status code
2988 */
2989efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2990{
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002991 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02002992 struct efi_object *efiobj;
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002993 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02002994
2995 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02002996
Heinrich Schuchardtdf116e82019-05-01 18:25:45 +02002997 efiobj = efi_search_obj(image_handle);
2998 if (!efiobj) {
2999 ret = EFI_INVALID_PARAMETER;
3000 goto out;
3001 }
3002 /* Find the loaded image protocol */
3003 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3004 (void **)&loaded_image_protocol,
3005 NULL, NULL,
3006 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3007 if (ret != EFI_SUCCESS) {
3008 ret = EFI_INVALID_PARAMETER;
3009 goto out;
3010 }
3011 switch (efiobj->type) {
3012 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3013 /* Call the unload function */
3014 if (!loaded_image_protocol->unload) {
3015 ret = EFI_UNSUPPORTED;
3016 goto out;
3017 }
3018 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3019 if (ret != EFI_SUCCESS)
3020 goto out;
3021 break;
3022 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3023 break;
3024 default:
3025 ret = EFI_INVALID_PARAMETER;
3026 goto out;
3027 }
3028 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3029 loaded_image_protocol);
3030out:
3031 return EFI_EXIT(ret);
Heinrich Schuchardt46e99a92019-05-01 19:04:32 +02003032}
3033
3034/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003035 * efi_update_exit_data() - fill exit data parameters of StartImage()
3036 *
3037 * @image_obj image handle
3038 * @exit_data_size size of the exit data buffer
3039 * @exit_data buffer with data returned by UEFI payload
3040 * Return: status code
3041 */
3042static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3043 efi_uintn_t exit_data_size,
3044 u16 *exit_data)
3045{
3046 efi_status_t ret;
3047
3048 /*
3049 * If exit_data is not provided to StartImage(), exit_data_size must be
3050 * ignored.
3051 */
3052 if (!image_obj->exit_data)
3053 return EFI_SUCCESS;
3054 if (image_obj->exit_data_size)
3055 *image_obj->exit_data_size = exit_data_size;
3056 if (exit_data_size && exit_data) {
3057 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3058 exit_data_size,
3059 (void **)image_obj->exit_data);
3060 if (ret != EFI_SUCCESS)
3061 return ret;
3062 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3063 } else {
3064 image_obj->exit_data = NULL;
3065 }
3066 return EFI_SUCCESS;
3067}
3068
3069/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003070 * efi_exit() - leave an EFI application or driver
3071 * @image_handle: handle of the application or driver that is exiting
3072 * @exit_status: status code
3073 * @exit_data_size: size of the buffer in bytes
3074 * @exit_data: buffer with data describing an error
3075 *
3076 * This function implements the Exit service.
3077 *
3078 * See the Unified Extensible Firmware Interface (UEFI) specification for
3079 * details.
3080 *
3081 * Return: status code
3082 */
3083static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3084 efi_status_t exit_status,
3085 efi_uintn_t exit_data_size,
3086 u16 *exit_data)
3087{
3088 /*
3089 * TODO: We should call the unload procedure of the loaded
3090 * image protocol.
3091 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003092 efi_status_t ret;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003093 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003094 struct efi_loaded_image_obj *image_obj =
3095 (struct efi_loaded_image_obj *)image_handle;
3096
3097 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3098 exit_data_size, exit_data);
3099
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003100 /* Check parameters */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003101 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003102 (void **)&loaded_image_protocol,
3103 NULL, NULL,
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003104 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003105 if (ret != EFI_SUCCESS) {
3106 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003107 goto out;
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003108 }
3109
3110 /* Unloading of unstarted images */
3111 switch (image_obj->header.type) {
3112 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3113 break;
3114 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3115 efi_delete_image(image_obj, loaded_image_protocol);
3116 ret = EFI_SUCCESS;
3117 goto out;
3118 default:
3119 /* Handle does not refer to loaded image */
3120 ret = EFI_INVALID_PARAMETER;
3121 goto out;
3122 }
3123 /* A started image can only be unloaded it is the last one started. */
3124 if (image_handle != current_image) {
3125 ret = EFI_INVALID_PARAMETER;
3126 goto out;
3127 }
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003128
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003129 /* Exit data is only foreseen in case of failure. */
3130 if (exit_status != EFI_SUCCESS) {
3131 ret = efi_update_exit_data(image_obj, exit_data_size,
3132 exit_data);
3133 /* Exiting has priority. Don't return error to caller. */
3134 if (ret != EFI_SUCCESS)
3135 EFI_PRINT("%s: out of memory\n", __func__);
3136 }
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003137 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3138 exit_status != EFI_SUCCESS)
3139 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02003140
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003141 /* Make sure entry/exit counts for EFI world cross-overs match */
3142 EFI_EXIT(exit_status);
3143
3144 /*
3145 * But longjmp out with the U-Boot gd, not the application's, as
3146 * the other end is a setjmp call inside EFI context.
3147 */
3148 efi_restore_gd();
3149
3150 image_obj->exit_status = exit_status;
3151 longjmp(&image_obj->exit_jmp, 1);
3152
3153 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01003154out:
Heinrich Schuchardt126a43f2019-05-01 20:07:04 +02003155 return EFI_EXIT(ret);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01003156}
3157
3158/**
Mario Six78a88f72018-07-10 08:40:17 +02003159 * efi_handle_protocol() - get interface of a protocol on a handle
3160 * @handle: handle on which the protocol shall be opened
3161 * @protocol: GUID of the protocol
3162 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003163 *
3164 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003165 *
Mario Six78a88f72018-07-10 08:40:17 +02003166 * See the Unified Extensible Firmware Interface (UEFI) specification for
3167 * details.
3168 *
3169 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02003170 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01003171static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02003172 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01003173 void **protocol_interface)
3174{
Heinrich Schuchardt755d42d2019-06-01 19:29:39 +02003175 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02003176 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01003177}
3178
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003179/**
Mario Six78a88f72018-07-10 08:40:17 +02003180 * efi_bind_controller() - bind a single driver to a controller
3181 * @controller_handle: controller handle
3182 * @driver_image_handle: driver handle
3183 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003184 *
Mario Six78a88f72018-07-10 08:40:17 +02003185 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003186 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003187static efi_status_t efi_bind_controller(
3188 efi_handle_t controller_handle,
3189 efi_handle_t driver_image_handle,
3190 struct efi_device_path *remain_device_path)
3191{
3192 struct efi_driver_binding_protocol *binding_protocol;
3193 efi_status_t r;
3194
3195 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3196 &efi_guid_driver_binding_protocol,
3197 (void **)&binding_protocol,
3198 driver_image_handle, NULL,
3199 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3200 if (r != EFI_SUCCESS)
3201 return r;
3202 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3203 controller_handle,
3204 remain_device_path));
3205 if (r == EFI_SUCCESS)
3206 r = EFI_CALL(binding_protocol->start(binding_protocol,
3207 controller_handle,
3208 remain_device_path));
3209 EFI_CALL(efi_close_protocol(driver_image_handle,
3210 &efi_guid_driver_binding_protocol,
3211 driver_image_handle, NULL));
3212 return r;
3213}
3214
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003215/**
Mario Six78a88f72018-07-10 08:40:17 +02003216 * efi_connect_single_controller() - connect a single driver to a controller
3217 * @controller_handle: controller
3218 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003219 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003220 *
Mario Six78a88f72018-07-10 08:40:17 +02003221 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003222 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003223static efi_status_t efi_connect_single_controller(
3224 efi_handle_t controller_handle,
3225 efi_handle_t *driver_image_handle,
3226 struct efi_device_path *remain_device_path)
3227{
3228 efi_handle_t *buffer;
3229 size_t count;
3230 size_t i;
3231 efi_status_t r;
3232 size_t connected = 0;
3233
3234 /* Get buffer with all handles with driver binding protocol */
3235 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3236 &efi_guid_driver_binding_protocol,
3237 NULL, &count, &buffer));
3238 if (r != EFI_SUCCESS)
3239 return r;
3240
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003241 /* Context Override */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003242 if (driver_image_handle) {
3243 for (; *driver_image_handle; ++driver_image_handle) {
3244 for (i = 0; i < count; ++i) {
3245 if (buffer[i] == *driver_image_handle) {
3246 buffer[i] = NULL;
3247 r = efi_bind_controller(
3248 controller_handle,
3249 *driver_image_handle,
3250 remain_device_path);
3251 /*
3252 * For drivers that do not support the
3253 * controller or are already connected
3254 * we receive an error code here.
3255 */
3256 if (r == EFI_SUCCESS)
3257 ++connected;
3258 }
3259 }
3260 }
3261 }
3262
3263 /*
3264 * TODO: Some overrides are not yet implemented:
3265 * - Platform Driver Override
3266 * - Driver Family Override Search
3267 * - Bus Specific Driver Override
3268 */
3269
3270 /* Driver Binding Search */
3271 for (i = 0; i < count; ++i) {
3272 if (buffer[i]) {
3273 r = efi_bind_controller(controller_handle,
3274 buffer[i],
3275 remain_device_path);
3276 if (r == EFI_SUCCESS)
3277 ++connected;
3278 }
3279 }
3280
3281 efi_free_pool(buffer);
3282 if (!connected)
3283 return EFI_NOT_FOUND;
3284 return EFI_SUCCESS;
3285}
3286
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003287/**
Mario Six78a88f72018-07-10 08:40:17 +02003288 * efi_connect_controller() - connect a controller to a driver
3289 * @controller_handle: handle of the controller
3290 * @driver_image_handle: handle of the driver
3291 * @remain_device_path: device path of a child controller
3292 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003293 *
3294 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02003295 *
3296 * See the Unified Extensible Firmware Interface (UEFI) specification for
3297 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003298 *
3299 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003300 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003301 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3302 *
Mario Six78a88f72018-07-10 08:40:17 +02003303 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003304 */
3305static efi_status_t EFIAPI efi_connect_controller(
3306 efi_handle_t controller_handle,
3307 efi_handle_t *driver_image_handle,
3308 struct efi_device_path *remain_device_path,
3309 bool recursive)
3310{
3311 efi_status_t r;
3312 efi_status_t ret = EFI_NOT_FOUND;
3313 struct efi_object *efiobj;
3314
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01003315 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003316 remain_device_path, recursive);
3317
3318 efiobj = efi_search_obj(controller_handle);
3319 if (!efiobj) {
3320 ret = EFI_INVALID_PARAMETER;
3321 goto out;
3322 }
3323
3324 r = efi_connect_single_controller(controller_handle,
3325 driver_image_handle,
3326 remain_device_path);
3327 if (r == EFI_SUCCESS)
3328 ret = EFI_SUCCESS;
3329 if (recursive) {
3330 struct efi_handler *handler;
3331 struct efi_open_protocol_info_item *item;
3332
3333 list_for_each_entry(handler, &efiobj->protocols, link) {
3334 list_for_each_entry(item, &handler->open_infos, link) {
3335 if (item->info.attributes &
3336 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3337 r = EFI_CALL(efi_connect_controller(
3338 item->info.controller_handle,
3339 driver_image_handle,
3340 remain_device_path,
3341 recursive));
3342 if (r == EFI_SUCCESS)
3343 ret = EFI_SUCCESS;
3344 }
3345 }
3346 }
3347 }
Heinrich Schuchardt359a6992019-07-03 20:27:24 +02003348 /* Check for child controller specified by end node */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01003349 if (ret != EFI_SUCCESS && remain_device_path &&
3350 remain_device_path->type == DEVICE_PATH_TYPE_END)
3351 ret = EFI_SUCCESS;
3352out:
3353 return EFI_EXIT(ret);
3354}
3355
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003356/**
Mario Six78a88f72018-07-10 08:40:17 +02003357 * efi_reinstall_protocol_interface() - reinstall protocol interface
3358 * @handle: handle on which the protocol shall be reinstalled
3359 * @protocol: GUID of the protocol to be installed
3360 * @old_interface: interface to be removed
3361 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003362 *
3363 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003364 *
3365 * See the Unified Extensible Firmware Interface (UEFI) specification for
3366 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003367 *
3368 * The old interface is uninstalled. The new interface is installed.
3369 * Drivers are connected.
3370 *
Mario Six78a88f72018-07-10 08:40:17 +02003371 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003372 */
3373static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3374 efi_handle_t handle, const efi_guid_t *protocol,
3375 void *old_interface, void *new_interface)
3376{
3377 efi_status_t ret;
3378
3379 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3380 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003381
3382 /* Uninstall protocol but do not delete handle */
3383 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003384 if (ret != EFI_SUCCESS)
3385 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003386
3387 /* Install the new protocol */
3388 ret = efi_add_protocol(handle, protocol, new_interface);
3389 /*
3390 * The UEFI spec does not specify what should happen to the handle
3391 * if in case of an error no protocol interface remains on the handle.
3392 * So let's do nothing here.
3393 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003394 if (ret != EFI_SUCCESS)
3395 goto out;
3396 /*
3397 * The returned status code has to be ignored.
3398 * Do not create an error if no suitable driver for the handle exists.
3399 */
3400 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3401out:
3402 return EFI_EXIT(ret);
3403}
3404
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003405/**
Mario Six78a88f72018-07-10 08:40:17 +02003406 * efi_get_child_controllers() - get all child controllers associated to a driver
3407 * @efiobj: handle of the controller
3408 * @driver_handle: handle of the driver
3409 * @number_of_children: number of child controllers
3410 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003411 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003412 * The allocated buffer has to be freed with free().
3413 *
Mario Six78a88f72018-07-10 08:40:17 +02003414 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003415 */
3416static efi_status_t efi_get_child_controllers(
3417 struct efi_object *efiobj,
3418 efi_handle_t driver_handle,
3419 efi_uintn_t *number_of_children,
3420 efi_handle_t **child_handle_buffer)
3421{
3422 struct efi_handler *handler;
3423 struct efi_open_protocol_info_item *item;
3424 efi_uintn_t count = 0, i;
3425 bool duplicate;
3426
3427 /* Count all child controller associations */
3428 list_for_each_entry(handler, &efiobj->protocols, link) {
3429 list_for_each_entry(item, &handler->open_infos, link) {
3430 if (item->info.agent_handle == driver_handle &&
3431 item->info.attributes &
3432 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3433 ++count;
3434 }
3435 }
3436 /*
3437 * Create buffer. In case of duplicate child controller assignments
3438 * the buffer will be too large. But that does not harm.
3439 */
3440 *number_of_children = 0;
3441 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3442 if (!*child_handle_buffer)
3443 return EFI_OUT_OF_RESOURCES;
3444 /* Copy unique child handles */
3445 list_for_each_entry(handler, &efiobj->protocols, link) {
3446 list_for_each_entry(item, &handler->open_infos, link) {
3447 if (item->info.agent_handle == driver_handle &&
3448 item->info.attributes &
3449 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3450 /* Check this is a new child controller */
3451 duplicate = false;
3452 for (i = 0; i < *number_of_children; ++i) {
3453 if ((*child_handle_buffer)[i] ==
3454 item->info.controller_handle)
3455 duplicate = true;
3456 }
3457 /* Copy handle to buffer */
3458 if (!duplicate) {
3459 i = (*number_of_children)++;
3460 (*child_handle_buffer)[i] =
3461 item->info.controller_handle;
3462 }
3463 }
3464 }
3465 }
3466 return EFI_SUCCESS;
3467}
3468
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003469/**
Mario Six78a88f72018-07-10 08:40:17 +02003470 * efi_disconnect_controller() - disconnect a controller from a driver
3471 * @controller_handle: handle of the controller
3472 * @driver_image_handle: handle of the driver
3473 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003474 *
3475 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003476 *
Mario Six78a88f72018-07-10 08:40:17 +02003477 * See the Unified Extensible Firmware Interface (UEFI) specification for
3478 * details.
3479 *
3480 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003481 */
3482static efi_status_t EFIAPI efi_disconnect_controller(
3483 efi_handle_t controller_handle,
3484 efi_handle_t driver_image_handle,
3485 efi_handle_t child_handle)
3486{
3487 struct efi_driver_binding_protocol *binding_protocol;
3488 efi_handle_t *child_handle_buffer = NULL;
3489 size_t number_of_children = 0;
3490 efi_status_t r;
3491 size_t stop_count = 0;
3492 struct efi_object *efiobj;
3493
3494 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3495 child_handle);
3496
3497 efiobj = efi_search_obj(controller_handle);
3498 if (!efiobj) {
3499 r = EFI_INVALID_PARAMETER;
3500 goto out;
3501 }
3502
3503 if (child_handle && !efi_search_obj(child_handle)) {
3504 r = EFI_INVALID_PARAMETER;
3505 goto out;
3506 }
3507
3508 /* If no driver handle is supplied, disconnect all drivers */
3509 if (!driver_image_handle) {
3510 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3511 goto out;
3512 }
3513
3514 /* Create list of child handles */
3515 if (child_handle) {
3516 number_of_children = 1;
3517 child_handle_buffer = &child_handle;
3518 } else {
3519 efi_get_child_controllers(efiobj,
3520 driver_image_handle,
3521 &number_of_children,
3522 &child_handle_buffer);
3523 }
3524
3525 /* Get the driver binding protocol */
3526 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3527 &efi_guid_driver_binding_protocol,
3528 (void **)&binding_protocol,
3529 driver_image_handle, NULL,
3530 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3531 if (r != EFI_SUCCESS)
3532 goto out;
3533 /* Remove the children */
3534 if (number_of_children) {
3535 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3536 controller_handle,
3537 number_of_children,
3538 child_handle_buffer));
3539 if (r == EFI_SUCCESS)
3540 ++stop_count;
3541 }
3542 /* Remove the driver */
3543 if (!child_handle)
3544 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3545 controller_handle,
3546 0, NULL));
3547 if (r == EFI_SUCCESS)
3548 ++stop_count;
3549 EFI_CALL(efi_close_protocol(driver_image_handle,
3550 &efi_guid_driver_binding_protocol,
3551 driver_image_handle, NULL));
3552
3553 if (stop_count)
3554 r = EFI_SUCCESS;
3555 else
3556 r = EFI_NOT_FOUND;
3557out:
3558 if (!child_handle)
3559 free(child_handle_buffer);
3560 return EFI_EXIT(r);
3561}
3562
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003563static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003564 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003565 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3566 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003567 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003568 },
3569 .raise_tpl = efi_raise_tpl,
3570 .restore_tpl = efi_restore_tpl,
3571 .allocate_pages = efi_allocate_pages_ext,
3572 .free_pages = efi_free_pages_ext,
3573 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003574 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003575 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003576 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003577 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003578 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003579 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003580 .close_event = efi_close_event,
3581 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003582 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003583 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003584 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003585 .handle_protocol = efi_handle_protocol,
3586 .reserved = NULL,
3587 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003588 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003589 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003590 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003591 .load_image = efi_load_image,
3592 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003593 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003594 .unload_image = efi_unload_image,
3595 .exit_boot_services = efi_exit_boot_services,
3596 .get_next_monotonic_count = efi_get_next_monotonic_count,
3597 .stall = efi_stall,
3598 .set_watchdog_timer = efi_set_watchdog_timer,
3599 .connect_controller = efi_connect_controller,
3600 .disconnect_controller = efi_disconnect_controller,
3601 .open_protocol = efi_open_protocol,
3602 .close_protocol = efi_close_protocol,
3603 .open_protocol_information = efi_open_protocol_information,
3604 .protocols_per_handle = efi_protocols_per_handle,
3605 .locate_handle_buffer = efi_locate_handle_buffer,
3606 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003607 .install_multiple_protocol_interfaces =
3608 efi_install_multiple_protocol_interfaces,
3609 .uninstall_multiple_protocol_interfaces =
3610 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003611 .calculate_crc32 = efi_calculate_crc32,
3612 .copy_mem = efi_copy_mem,
3613 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003614 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003615};
3616
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003617static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003618
Alexander Graf3c63db92016-10-14 13:45:30 +02003619struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003620 .hdr = {
3621 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003622 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003623 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003624 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003625 .fw_vendor = firmware_vendor,
3626 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt3c783bf2019-06-15 14:51:06 +02003627 .runtime = &efi_runtime_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003628 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003629 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003630};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003631
3632/**
3633 * efi_initialize_system_table() - Initialize system table
3634 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003635 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003636 */
3637efi_status_t efi_initialize_system_table(void)
3638{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003639 efi_status_t ret;
3640
3641 /* Allocate configuration table array */
3642 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3643 EFI_MAX_CONFIGURATION_TABLES *
3644 sizeof(struct efi_configuration_table),
3645 (void **)&systab.tables);
3646
Heinrich Schuchardt93148eb2019-06-29 02:00:16 +02003647 /*
3648 * These entries will be set to NULL in ExitBootServices(). To avoid
3649 * relocation in SetVirtualAddressMap(), set them dynamically.
3650 */
3651 systab.con_in = &efi_con_in;
3652 systab.con_out = &efi_con_out;
3653 systab.std_err = &efi_con_out;
3654 systab.boottime = &efi_boot_services;
3655
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003656 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003657 efi_update_table_header_crc32(&systab.hdr);
3658 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3659 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003660
3661 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003662}