blob: 970c01614e2a523ab7c31e7450c2106dbf89060e [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/*
3 * EFI application boot time services
4 *
5 * 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>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020022
Alexander Grafbee91162016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010028
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +010029/* Handle of the currently executing image */
30static efi_handle_t current_image;
31
Alexander Graff31239a2018-11-15 21:23:47 +010032/*
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
37 */
38static bool efi_is_direct_boot = true;
39
Simon Glass65e4c0b2016-09-25 15:27:35 -060040#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010041/*
42 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
43 * fixed when compiling U-Boot. However, the payload does not know about that
44 * restriction so we need to manually swap its and our view of that register on
45 * EFI callback entry/exit.
46 */
47static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060048#endif
Alexander Grafbee91162016-03-04 01:09:59 +010049
Heinrich Schuchardt914df752019-02-09 14:10:39 +010050/* 1 if inside U-Boot code, 0 if inside EFI payload code */
51static int entry_count = 1;
Rob Clarkaf65db82017-07-27 08:04:19 -040052static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010053/* GUID of the device tree table */
54const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010055/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
56const efi_guid_t efi_guid_driver_binding_protocol =
57 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040058
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010059/* event group ExitBootServices() invoked */
60const efi_guid_t efi_guid_event_group_exit_boot_services =
61 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
62/* event group SetVirtualAddressMap() invoked */
63const efi_guid_t efi_guid_event_group_virtual_address_change =
64 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
65/* event group memory map changed */
66const efi_guid_t efi_guid_event_group_memory_map_change =
67 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
68/* event group boot manager about to boot */
69const efi_guid_t efi_guid_event_group_ready_to_boot =
70 EFI_EVENT_GROUP_READY_TO_BOOT;
71/* event group ResetSystem() invoked (before ExitBootServices) */
72const efi_guid_t efi_guid_event_group_reset_system =
73 EFI_EVENT_GROUP_RESET_SYSTEM;
74
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010075static efi_status_t EFIAPI efi_disconnect_controller(
76 efi_handle_t controller_handle,
77 efi_handle_t driver_image_handle,
78 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010079
Rob Clarkc160d2f2017-07-27 08:04:18 -040080/* Called on every callback entry */
81int __efi_entry_check(void)
82{
83 int ret = entry_count++ == 0;
84#ifdef CONFIG_ARM
85 assert(efi_gd);
86 app_gd = gd;
87 gd = efi_gd;
88#endif
89 return ret;
90}
91
92/* Called on every callback exit */
93int __efi_exit_check(void)
94{
95 int ret = --entry_count == 0;
96#ifdef CONFIG_ARM
97 gd = app_gd;
98#endif
99 return ret;
100}
101
Alexander Grafbee91162016-03-04 01:09:59 +0100102/* Called from do_bootefi_exec() */
103void efi_save_gd(void)
104{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100106 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600107#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100108}
109
Rob Clarkc160d2f2017-07-27 08:04:18 -0400110/*
Mario Six78a88f72018-07-10 08:40:17 +0200111 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200112 * world so we can dump out an abort message, without any care about returning
113 * back to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400114 */
Alexander Grafbee91162016-03-04 01:09:59 +0100115void efi_restore_gd(void)
116{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600117#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100118 /* Only restore if we're already in EFI context */
119 if (!efi_gd)
120 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100121 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600122#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100123}
124
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200125/**
Mario Six78a88f72018-07-10 08:40:17 +0200126 * indent_string() - returns a string for indenting with two spaces per level
127 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100128 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200129 * A maximum of ten indent levels is supported. Higher indent levels will be
130 * truncated.
131 *
Mario Six78a88f72018-07-10 08:40:17 +0200132 * Return: A string for indenting with two spaces per level is
133 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400134 */
135static const char *indent_string(int level)
136{
137 const char *indent = " ";
138 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100139
Rob Clarkaf65db82017-07-27 08:04:19 -0400140 level = min(max, level * 2);
141 return &indent[max - level];
142}
143
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200144const char *__efi_nesting(void)
145{
146 return indent_string(nesting_level);
147}
148
Rob Clarkaf65db82017-07-27 08:04:19 -0400149const char *__efi_nesting_inc(void)
150{
151 return indent_string(nesting_level++);
152}
153
154const char *__efi_nesting_dec(void)
155{
156 return indent_string(--nesting_level);
157}
158
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200159/**
Mario Six78a88f72018-07-10 08:40:17 +0200160 * efi_queue_event() - queue an EFI event
161 * @event: event to signal
162 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200163 *
164 * This function queues the notification function of the event for future
165 * execution.
166 *
Mario Six78a88f72018-07-10 08:40:17 +0200167 * The notification function is called if the task priority level of the event
168 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200169 *
170 * For the SignalEvent service see efi_signal_event_ext.
171 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200172 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100173static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200174{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200175 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200176 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200177 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100178 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200179 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200180 EFI_CALL_VOID(event->notify_function(event,
181 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200182 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200183 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200184}
185
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200186/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200187 * is_valid_tpl() - check if the task priority level is valid
188 *
189 * @tpl: TPL level to check
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200190 * Return: status code
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200191 */
192efi_status_t is_valid_tpl(efi_uintn_t tpl)
193{
194 switch (tpl) {
195 case TPL_APPLICATION:
196 case TPL_CALLBACK:
197 case TPL_NOTIFY:
198 case TPL_HIGH_LEVEL:
199 return EFI_SUCCESS;
200 default:
201 return EFI_INVALID_PARAMETER;
202 }
203}
204
205/**
Mario Six78a88f72018-07-10 08:40:17 +0200206 * efi_signal_event() - signal an EFI event
207 * @event: event to signal
208 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100209 *
Mario Six78a88f72018-07-10 08:40:17 +0200210 * This function signals an event. If the event belongs to an event group all
211 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100212 * their notification function is queued.
213 *
214 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100215 */
216void efi_signal_event(struct efi_event *event, bool check_tpl)
217{
218 if (event->group) {
219 struct efi_event *evt;
220
221 /*
222 * The signaled state has to set before executing any
223 * notification function
224 */
225 list_for_each_entry(evt, &efi_events, link) {
226 if (!evt->group || guidcmp(evt->group, event->group))
227 continue;
228 if (evt->is_signaled)
229 continue;
230 evt->is_signaled = true;
231 if (evt->type & EVT_NOTIFY_SIGNAL &&
232 evt->notify_function)
233 evt->is_queued = true;
234 }
235 list_for_each_entry(evt, &efi_events, link) {
236 if (!evt->group || guidcmp(evt->group, event->group))
237 continue;
238 if (evt->is_queued)
239 efi_queue_event(evt, check_tpl);
240 }
241 } else if (!event->is_signaled) {
242 event->is_signaled = true;
243 if (event->type & EVT_NOTIFY_SIGNAL)
244 efi_queue_event(event, check_tpl);
245 }
246}
247
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200248/**
Mario Six78a88f72018-07-10 08:40:17 +0200249 * efi_raise_tpl() - raise the task priority level
250 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200251 *
252 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200253 *
Mario Six78a88f72018-07-10 08:40:17 +0200254 * See the Unified Extensible Firmware Interface (UEFI) specification for
255 * details.
256 *
257 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200258 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100259static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100260{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100261 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200262
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200263 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200264
265 if (new_tpl < efi_tpl)
266 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
267 efi_tpl = new_tpl;
268 if (efi_tpl > TPL_HIGH_LEVEL)
269 efi_tpl = TPL_HIGH_LEVEL;
270
271 EFI_EXIT(EFI_SUCCESS);
272 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100273}
274
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200275/**
Mario Six78a88f72018-07-10 08:40:17 +0200276 * efi_restore_tpl() - lower the task priority level
277 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200278 *
279 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200280 *
Mario Six78a88f72018-07-10 08:40:17 +0200281 * See the Unified Extensible Firmware Interface (UEFI) specification for
282 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200283 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100284static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100285{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200286 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200287
288 if (old_tpl > efi_tpl)
289 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
290 efi_tpl = old_tpl;
291 if (efi_tpl > TPL_HIGH_LEVEL)
292 efi_tpl = TPL_HIGH_LEVEL;
293
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100294 /*
295 * Lowering the TPL may have made queued events eligible for execution.
296 */
297 efi_timer_check();
298
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200299 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100300}
301
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200302/**
Mario Six78a88f72018-07-10 08:40:17 +0200303 * efi_allocate_pages_ext() - allocate memory pages
304 * @type: type of allocation to be performed
305 * @memory_type: usage type of the allocated memory
306 * @pages: number of pages to be allocated
307 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200308 *
309 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200310 *
Mario Six78a88f72018-07-10 08:40:17 +0200311 * See the Unified Extensible Firmware Interface (UEFI) specification for
312 * details.
313 *
314 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200315 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900316static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100317 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900318 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100319{
320 efi_status_t r;
321
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100322 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100323 r = efi_allocate_pages(type, memory_type, pages, memory);
324 return EFI_EXIT(r);
325}
326
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200327/**
Mario Six78a88f72018-07-10 08:40:17 +0200328 * efi_free_pages_ext() - Free memory pages.
329 * @memory: start of the memory area to be freed
330 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200331 *
332 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200333 *
Mario Six78a88f72018-07-10 08:40:17 +0200334 * See the Unified Extensible Firmware Interface (UEFI) specification for
335 * details.
336 *
337 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200338 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900339static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100340 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100341{
342 efi_status_t r;
343
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900344 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100345 r = efi_free_pages(memory, pages);
346 return EFI_EXIT(r);
347}
348
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200349/**
Mario Six78a88f72018-07-10 08:40:17 +0200350 * efi_get_memory_map_ext() - get map describing memory usage
351 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
352 * on exit the size of the copied memory map
353 * @memory_map: buffer to which the memory map is written
354 * @map_key: key for the memory map
355 * @descriptor_size: size of an individual memory descriptor
356 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200357 *
358 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200359 *
Mario Six78a88f72018-07-10 08:40:17 +0200360 * See the Unified Extensible Firmware Interface (UEFI) specification for
361 * details.
362 *
363 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200364 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900365static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100366 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900367 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100368 efi_uintn_t *map_key,
369 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900370 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100371{
372 efi_status_t r;
373
374 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
375 map_key, descriptor_size, descriptor_version);
376 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
377 descriptor_size, descriptor_version);
378 return EFI_EXIT(r);
379}
380
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200381/**
Mario Six78a88f72018-07-10 08:40:17 +0200382 * efi_allocate_pool_ext() - allocate memory from pool
383 * @pool_type: type of the pool from which memory is to be allocated
384 * @size: number of bytes to be allocated
385 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200386 *
387 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200388 *
Mario Six78a88f72018-07-10 08:40:17 +0200389 * See the Unified Extensible Firmware Interface (UEFI) specification for
390 * details.
391 *
392 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200393 */
Stefan Brünsead12742016-10-09 22:17:18 +0200394static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100395 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200396 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100397{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100398 efi_status_t r;
399
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100400 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200401 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100402 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100403}
404
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200405/**
Mario Six78a88f72018-07-10 08:40:17 +0200406 * efi_free_pool_ext() - free memory from pool
407 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200408 *
409 * This function implements the FreePool 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 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200416static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100417{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100418 efi_status_t r;
419
420 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200421 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100422 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100423}
424
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200425/**
Mario Six78a88f72018-07-10 08:40:17 +0200426 * efi_add_handle() - add a new object to the object list
427 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100428 *
Mario Six78a88f72018-07-10 08:40:17 +0200429 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100430 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200431void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100432{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200433 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100434 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200435 INIT_LIST_HEAD(&handle->protocols);
436 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100437}
438
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200439/**
Mario Six78a88f72018-07-10 08:40:17 +0200440 * efi_create_handle() - create handle
441 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200442 *
Mario Six78a88f72018-07-10 08:40:17 +0200443 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200444 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100445efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200446{
447 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200448
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200449 obj = calloc(1, sizeof(struct efi_object));
450 if (!obj)
451 return EFI_OUT_OF_RESOURCES;
452
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100453 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200454 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200455
456 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200457}
458
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200459/**
Mario Six78a88f72018-07-10 08:40:17 +0200460 * efi_search_protocol() - find a protocol on a handle.
461 * @handle: handle
462 * @protocol_guid: GUID of the protocol
463 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100464 *
Mario Six78a88f72018-07-10 08:40:17 +0200465 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100466 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100467efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100468 const efi_guid_t *protocol_guid,
469 struct efi_handler **handler)
470{
471 struct efi_object *efiobj;
472 struct list_head *lhandle;
473
474 if (!handle || !protocol_guid)
475 return EFI_INVALID_PARAMETER;
476 efiobj = efi_search_obj(handle);
477 if (!efiobj)
478 return EFI_INVALID_PARAMETER;
479 list_for_each(lhandle, &efiobj->protocols) {
480 struct efi_handler *protocol;
481
482 protocol = list_entry(lhandle, struct efi_handler, link);
483 if (!guidcmp(protocol->guid, protocol_guid)) {
484 if (handler)
485 *handler = protocol;
486 return EFI_SUCCESS;
487 }
488 }
489 return EFI_NOT_FOUND;
490}
491
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200492/**
Mario Six78a88f72018-07-10 08:40:17 +0200493 * efi_remove_protocol() - delete protocol from a handle
494 * @handle: handle from which the protocol shall be deleted
495 * @protocol: GUID of the protocol to be deleted
496 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100497 *
Mario Six78a88f72018-07-10 08:40:17 +0200498 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100499 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100500efi_status_t efi_remove_protocol(const efi_handle_t handle,
501 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100502 void *protocol_interface)
503{
504 struct efi_handler *handler;
505 efi_status_t ret;
506
507 ret = efi_search_protocol(handle, protocol, &handler);
508 if (ret != EFI_SUCCESS)
509 return ret;
510 if (guidcmp(handler->guid, protocol))
511 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200512 if (handler->protocol_interface != protocol_interface)
513 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100514 list_del(&handler->link);
515 free(handler);
516 return EFI_SUCCESS;
517}
518
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200519/**
Mario Six78a88f72018-07-10 08:40:17 +0200520 * efi_remove_all_protocols() - delete all protocols from a handle
521 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100522 *
Mario Six78a88f72018-07-10 08:40:17 +0200523 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100524 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100525efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100526{
527 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100528 struct efi_handler *protocol;
529 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100530
531 efiobj = efi_search_obj(handle);
532 if (!efiobj)
533 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100534 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100535 efi_status_t ret;
536
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100537 ret = efi_remove_protocol(handle, protocol->guid,
538 protocol->protocol_interface);
539 if (ret != EFI_SUCCESS)
540 return ret;
541 }
542 return EFI_SUCCESS;
543}
544
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200545/**
Mario Six78a88f72018-07-10 08:40:17 +0200546 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100547 *
Mario Six78a88f72018-07-10 08:40:17 +0200548 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100549 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200550void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100551{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200552 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100553 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200554 efi_remove_all_protocols(handle);
555 list_del(&handle->link);
556 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100557}
558
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200559/**
Mario Six78a88f72018-07-10 08:40:17 +0200560 * efi_is_event() - check if a pointer is a valid event
561 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100562 *
Mario Six78a88f72018-07-10 08:40:17 +0200563 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100564 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100565static efi_status_t efi_is_event(const struct efi_event *event)
566{
567 const struct efi_event *evt;
568
569 if (!event)
570 return EFI_INVALID_PARAMETER;
571 list_for_each_entry(evt, &efi_events, link) {
572 if (evt == event)
573 return EFI_SUCCESS;
574 }
575 return EFI_INVALID_PARAMETER;
576}
Alexander Grafbee91162016-03-04 01:09:59 +0100577
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200578/**
Mario Six78a88f72018-07-10 08:40:17 +0200579 * efi_create_event() - create an event
580 * @type: type of the event to create
581 * @notify_tpl: task priority level of the event
582 * @notify_function: notification function of the event
583 * @notify_context: pointer passed to the notification function
584 * @group: event group
585 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200586 *
587 * This function is used inside U-Boot code to create an event.
588 *
589 * For the API function implementing the CreateEvent service see
590 * efi_create_event_ext.
591 *
Mario Six78a88f72018-07-10 08:40:17 +0200592 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200593 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100594efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200595 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200596 struct efi_event *event,
597 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100598 void *notify_context, efi_guid_t *group,
599 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100600{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100601 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200602
Jonathan Graya95343b2017-03-12 19:26:07 +1100603 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200604 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100605
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200606 switch (type) {
607 case 0:
608 case EVT_TIMER:
609 case EVT_NOTIFY_SIGNAL:
610 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
611 case EVT_NOTIFY_WAIT:
612 case EVT_TIMER | EVT_NOTIFY_WAIT:
613 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
614 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
615 break;
616 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200617 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200618 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100619
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900620 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
621 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200622 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100623
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100624 evt = calloc(1, sizeof(struct efi_event));
625 if (!evt)
626 return EFI_OUT_OF_RESOURCES;
627 evt->type = type;
628 evt->notify_tpl = notify_tpl;
629 evt->notify_function = notify_function;
630 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100631 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200632 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100633 evt->trigger_next = -1ULL;
634 evt->is_queued = false;
635 evt->is_signaled = false;
636 list_add_tail(&evt->link, &efi_events);
637 *event = evt;
638 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100639}
640
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200641/*
Mario Six78a88f72018-07-10 08:40:17 +0200642 * efi_create_event_ex() - create an event in a group
643 * @type: type of the event to create
644 * @notify_tpl: task priority level of the event
645 * @notify_function: notification function of the event
646 * @notify_context: pointer passed to the notification function
647 * @event: created event
648 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100649 *
650 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100651 *
Mario Six78a88f72018-07-10 08:40:17 +0200652 * See the Unified Extensible Firmware Interface (UEFI) specification for
653 * details.
654 *
655 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100656 */
657efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
658 void (EFIAPI *notify_function) (
659 struct efi_event *event,
660 void *context),
661 void *notify_context,
662 efi_guid_t *event_group,
663 struct efi_event **event)
664{
665 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
666 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100667 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100668 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100669}
670
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200671/**
Mario Six78a88f72018-07-10 08:40:17 +0200672 * efi_create_event_ext() - create an event
673 * @type: type of the event to create
674 * @notify_tpl: task priority level of the event
675 * @notify_function: notification function of the event
676 * @notify_context: pointer passed to the notification function
677 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200678 *
679 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200680 *
Mario Six78a88f72018-07-10 08:40:17 +0200681 * See the Unified Extensible Firmware Interface (UEFI) specification for
682 * details.
683 *
684 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200685 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200686static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100687 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200688 void (EFIAPI *notify_function) (
689 struct efi_event *event,
690 void *context),
691 void *notify_context, struct efi_event **event)
692{
693 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
694 notify_context);
695 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100696 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200697}
698
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200699/**
Mario Six78a88f72018-07-10 08:40:17 +0200700 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200701 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200702 * Check if a timer event has occurred or a queued notification function should
703 * be called.
704 *
Alexander Grafbee91162016-03-04 01:09:59 +0100705 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200706 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100707 */
708void efi_timer_check(void)
709{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100710 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100711 u64 now = timer_get_us();
712
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100713 list_for_each_entry(evt, &efi_events, link) {
714 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100715 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100716 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200717 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100718 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200719 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100720 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200721 break;
722 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100723 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200724 break;
725 default:
726 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200727 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100728 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100729 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100730 }
Alexander Grafbee91162016-03-04 01:09:59 +0100731 WATCHDOG_RESET();
732}
733
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200734/**
Mario Six78a88f72018-07-10 08:40:17 +0200735 * efi_set_timer() - set the trigger time for a timer event or stop the event
736 * @event: event for which the timer is set
737 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200738 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200739 *
740 * This is the function for internal usage in U-Boot. For the API function
741 * implementing the SetTimer service see efi_set_timer_ext.
742 *
Mario Six78a88f72018-07-10 08:40:17 +0200743 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200744 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200745efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200746 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100747{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100748 /* Check that the event is valid */
749 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
750 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100751
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200752 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200753 * The parameter defines a multiple of 100 ns.
754 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200755 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200756 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100757
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100758 switch (type) {
759 case EFI_TIMER_STOP:
760 event->trigger_next = -1ULL;
761 break;
762 case EFI_TIMER_PERIODIC:
763 case EFI_TIMER_RELATIVE:
764 event->trigger_next = timer_get_us() + trigger_time;
765 break;
766 default:
767 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100768 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100769 event->trigger_type = type;
770 event->trigger_time = trigger_time;
771 event->is_signaled = false;
772 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200773}
774
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200775/**
Mario Six78a88f72018-07-10 08:40:17 +0200776 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
777 * event
778 * @event: event for which the timer is set
779 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200780 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200781 *
782 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200783 *
Mario Six78a88f72018-07-10 08:40:17 +0200784 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * details.
786 *
787 *
788 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200789 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200790static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
791 enum efi_timer_delay type,
792 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200793{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900794 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200795 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100796}
797
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200798/**
Mario Six78a88f72018-07-10 08:40:17 +0200799 * efi_wait_for_event() - wait for events to be signaled
800 * @num_events: number of events to be waited for
801 * @event: events to be waited for
802 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200803 *
804 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200805 *
Mario Six78a88f72018-07-10 08:40:17 +0200806 * See the Unified Extensible Firmware Interface (UEFI) specification for
807 * details.
808 *
809 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200810 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100811static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200812 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100813 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100814{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100815 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100816
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100817 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100818
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200819 /* Check parameters */
820 if (!num_events || !event)
821 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200822 /* Check TPL */
823 if (efi_tpl != TPL_APPLICATION)
824 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200825 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100826 if (efi_is_event(event[i]) != EFI_SUCCESS)
827 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200828 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
829 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200830 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100831 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200832 }
833
834 /* Wait for signal */
835 for (;;) {
836 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200837 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200838 goto out;
839 }
840 /* Allow events to occur. */
841 efi_timer_check();
842 }
843
844out:
845 /*
846 * Reset the signal which is passed to the caller to allow periodic
847 * events to occur.
848 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200849 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200850 if (index)
851 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100852
853 return EFI_EXIT(EFI_SUCCESS);
854}
855
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200856/**
Mario Six78a88f72018-07-10 08:40:17 +0200857 * efi_signal_event_ext() - signal an EFI event
858 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200859 *
860 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200861 *
862 * See the Unified Extensible Firmware Interface (UEFI) specification for
863 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200864 *
865 * This functions sets the signaled state of the event and queues the
866 * notification function for execution.
867 *
Mario Six78a88f72018-07-10 08:40:17 +0200868 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200869 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200870static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100871{
872 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100873 if (efi_is_event(event) != EFI_SUCCESS)
874 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100875 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100876 return EFI_EXIT(EFI_SUCCESS);
877}
878
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200879/**
Mario Six78a88f72018-07-10 08:40:17 +0200880 * efi_close_event() - close an EFI event
881 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200882 *
883 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200884 *
Mario Six78a88f72018-07-10 08:40:17 +0200885 * See the Unified Extensible Firmware Interface (UEFI) specification for
886 * details.
887 *
888 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200889 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200890static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100891{
892 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100893 if (efi_is_event(event) != EFI_SUCCESS)
894 return EFI_EXIT(EFI_INVALID_PARAMETER);
895 list_del(&event->link);
896 free(event);
897 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100898}
899
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200900/**
Mario Six78a88f72018-07-10 08:40:17 +0200901 * efi_check_event() - check if an event is signaled
902 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200903 *
904 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200905 *
Mario Six78a88f72018-07-10 08:40:17 +0200906 * See the Unified Extensible Firmware Interface (UEFI) specification for
907 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200908 *
Mario Six78a88f72018-07-10 08:40:17 +0200909 * If an event is not signaled yet, the notification function is queued. The
910 * signaled state is cleared.
911 *
912 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200913 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200914static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100915{
916 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200917 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100918 if (efi_is_event(event) != EFI_SUCCESS ||
919 event->type & EVT_NOTIFY_SIGNAL)
920 return EFI_EXIT(EFI_INVALID_PARAMETER);
921 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100922 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100923 if (event->is_signaled) {
924 event->is_signaled = false;
925 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200926 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100927 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100928}
929
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200930/**
Mario Six78a88f72018-07-10 08:40:17 +0200931 * efi_search_obj() - find the internal EFI object for a handle
932 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200933 *
Mario Six78a88f72018-07-10 08:40:17 +0200934 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200935 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100936struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200937{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100938 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200939
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100940 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200941 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200942 return efiobj;
943 }
944
945 return NULL;
946}
947
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200948/**
Mario Six78a88f72018-07-10 08:40:17 +0200949 * efi_open_protocol_info_entry() - create open protocol info entry and add it
950 * to a protocol
951 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100952 *
Mario Six78a88f72018-07-10 08:40:17 +0200953 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100954 */
955static struct efi_open_protocol_info_entry *efi_create_open_info(
956 struct efi_handler *handler)
957{
958 struct efi_open_protocol_info_item *item;
959
960 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
961 if (!item)
962 return NULL;
963 /* Append the item to the open protocol info list. */
964 list_add_tail(&item->link, &handler->open_infos);
965
966 return &item->info;
967}
968
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200969/**
Mario Six78a88f72018-07-10 08:40:17 +0200970 * efi_delete_open_info() - remove an open protocol info entry from a protocol
971 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100972 *
Mario Six78a88f72018-07-10 08:40:17 +0200973 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100974 */
975static efi_status_t efi_delete_open_info(
976 struct efi_open_protocol_info_item *item)
977{
978 list_del(&item->link);
979 free(item);
980 return EFI_SUCCESS;
981}
982
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200983/**
Mario Six78a88f72018-07-10 08:40:17 +0200984 * efi_add_protocol() - install new protocol on a handle
985 * @handle: handle on which the protocol shall be installed
986 * @protocol: GUID of the protocol to be installed
987 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200988 *
Mario Six78a88f72018-07-10 08:40:17 +0200989 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200990 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100991efi_status_t efi_add_protocol(const efi_handle_t handle,
992 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200993 void *protocol_interface)
994{
995 struct efi_object *efiobj;
996 struct efi_handler *handler;
997 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200998
999 efiobj = efi_search_obj(handle);
1000 if (!efiobj)
1001 return EFI_INVALID_PARAMETER;
1002 ret = efi_search_protocol(handle, protocol, NULL);
1003 if (ret != EFI_NOT_FOUND)
1004 return EFI_INVALID_PARAMETER;
1005 handler = calloc(1, sizeof(struct efi_handler));
1006 if (!handler)
1007 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001008 handler->guid = protocol;
1009 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001010 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001011 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001012 if (!guidcmp(&efi_guid_device_path, protocol))
1013 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001014 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001015}
1016
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001017/**
Mario Six78a88f72018-07-10 08:40:17 +02001018 * efi_install_protocol_interface() - install protocol interface
1019 * @handle: handle on which the protocol shall be installed
1020 * @protocol: GUID of the protocol to be installed
1021 * @protocol_interface_type: type of the interface to be installed,
1022 * always EFI_NATIVE_INTERFACE
1023 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001024 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001025 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001026 *
Mario Six78a88f72018-07-10 08:40:17 +02001027 * See the Unified Extensible Firmware Interface (UEFI) specification for
1028 * details.
1029 *
1030 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001031 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001032static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001033 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001034 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001035{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001036 efi_status_t r;
1037
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001038 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1039 protocol_interface);
1040
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001041 if (!handle || !protocol ||
1042 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1043 r = EFI_INVALID_PARAMETER;
1044 goto out;
1045 }
1046
1047 /* Create new handle if requested. */
1048 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001049 r = efi_create_handle(handle);
1050 if (r != EFI_SUCCESS)
1051 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001052 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1053 *handle);
1054 } else {
1055 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1056 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001057 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001058 /* Add new protocol */
1059 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001060out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001061 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001062}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001063
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001064/**
Mario Six78a88f72018-07-10 08:40:17 +02001065 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001066 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001067 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001068 * @number_of_drivers: number of child controllers
1069 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001070 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001071 * The allocated buffer has to be freed with free().
1072 *
Mario Six78a88f72018-07-10 08:40:17 +02001073 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001074 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001075static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001076 const efi_guid_t *protocol,
1077 efi_uintn_t *number_of_drivers,
1078 efi_handle_t **driver_handle_buffer)
1079{
1080 struct efi_handler *handler;
1081 struct efi_open_protocol_info_item *item;
1082 efi_uintn_t count = 0, i;
1083 bool duplicate;
1084
1085 /* Count all driver associations */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001086 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001087 if (protocol && guidcmp(handler->guid, protocol))
1088 continue;
1089 list_for_each_entry(item, &handler->open_infos, link) {
1090 if (item->info.attributes &
1091 EFI_OPEN_PROTOCOL_BY_DRIVER)
1092 ++count;
1093 }
1094 }
1095 /*
1096 * Create buffer. In case of duplicate driver assignments the buffer
1097 * will be too large. But that does not harm.
1098 */
1099 *number_of_drivers = 0;
1100 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1101 if (!*driver_handle_buffer)
1102 return EFI_OUT_OF_RESOURCES;
1103 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001104 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001105 if (protocol && guidcmp(handler->guid, protocol))
1106 continue;
1107 list_for_each_entry(item, &handler->open_infos, link) {
1108 if (item->info.attributes &
1109 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1110 /* Check this is a new driver */
1111 duplicate = false;
1112 for (i = 0; i < *number_of_drivers; ++i) {
1113 if ((*driver_handle_buffer)[i] ==
1114 item->info.agent_handle)
1115 duplicate = true;
1116 }
1117 /* Copy handle to buffer */
1118 if (!duplicate) {
1119 i = (*number_of_drivers)++;
1120 (*driver_handle_buffer)[i] =
1121 item->info.agent_handle;
1122 }
1123 }
1124 }
1125 }
1126 return EFI_SUCCESS;
1127}
1128
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001129/**
Mario Six78a88f72018-07-10 08:40:17 +02001130 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001131 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001132 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001133 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001134 *
1135 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001136 *
Mario Six78a88f72018-07-10 08:40:17 +02001137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1138 * details.
1139 *
1140 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001141 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001142static efi_status_t efi_disconnect_all_drivers
1143 (efi_handle_t handle,
1144 const efi_guid_t *protocol,
1145 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001146{
1147 efi_uintn_t number_of_drivers;
1148 efi_handle_t *driver_handle_buffer;
1149 efi_status_t r, ret;
1150
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001151 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001152 &driver_handle_buffer);
1153 if (ret != EFI_SUCCESS)
1154 return ret;
1155
1156 ret = EFI_NOT_FOUND;
1157 while (number_of_drivers) {
1158 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001159 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001160 driver_handle_buffer[--number_of_drivers],
1161 child_handle));
1162 if (r == EFI_SUCCESS)
1163 ret = r;
1164 }
1165 free(driver_handle_buffer);
1166 return ret;
1167}
1168
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001169/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001170 * efi_uninstall_protocol() - uninstall protocol interface
1171 *
Mario Six78a88f72018-07-10 08:40:17 +02001172 * @handle: handle from which the protocol shall be removed
1173 * @protocol: GUID of the protocol to be removed
1174 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001175 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001176 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001177 *
1178 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001179 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001180static efi_status_t efi_uninstall_protocol
1181 (efi_handle_t handle, const efi_guid_t *protocol,
1182 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001183{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001184 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001185 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001186 struct efi_open_protocol_info_item *item;
1187 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001188 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001189
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001190 /* Check handle */
1191 efiobj = efi_search_obj(handle);
1192 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001193 r = EFI_INVALID_PARAMETER;
1194 goto out;
1195 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001196 /* Find the protocol on the handle */
1197 r = efi_search_protocol(handle, protocol, &handler);
1198 if (r != EFI_SUCCESS)
1199 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001200 /* Disconnect controllers */
1201 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1202 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001203 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001204 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001205 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001206 /* Close protocol */
1207 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1208 if (item->info.attributes ==
1209 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1210 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1211 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1212 list_del(&item->link);
1213 }
1214 if (!list_empty(&handler->open_infos)) {
1215 r = EFI_ACCESS_DENIED;
1216 goto out;
1217 }
1218 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001219out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001220 return r;
1221}
1222
1223/**
1224 * efi_uninstall_protocol_interface() - uninstall protocol interface
1225 * @handle: handle from which the protocol shall be removed
1226 * @protocol: GUID of the protocol to be removed
1227 * @protocol_interface: interface to be removed
1228 *
1229 * This function implements the UninstallProtocolInterface service.
1230 *
1231 * See the Unified Extensible Firmware Interface (UEFI) specification for
1232 * details.
1233 *
1234 * Return: status code
1235 */
1236static efi_status_t EFIAPI efi_uninstall_protocol_interface
1237 (efi_handle_t handle, const efi_guid_t *protocol,
1238 void *protocol_interface)
1239{
1240 efi_status_t ret;
1241
1242 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1243
1244 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1245 if (ret != EFI_SUCCESS)
1246 goto out;
1247
1248 /* If the last protocol has been removed, delete the handle. */
1249 if (list_empty(&handle->protocols)) {
1250 list_del(&handle->link);
1251 free(handle);
1252 }
1253out:
1254 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001255}
1256
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001257/**
Mario Six78a88f72018-07-10 08:40:17 +02001258 * efi_register_protocol_notify() - register an event for notification when a
1259 * protocol is installed.
1260 * @protocol: GUID of the protocol whose installation shall be notified
1261 * @event: event to be signaled upon installation of the protocol
1262 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001263 *
1264 * This function implements the RegisterProtocolNotify service.
1265 * See the Unified Extensible Firmware Interface (UEFI) specification
1266 * for details.
1267 *
Mario Six78a88f72018-07-10 08:40:17 +02001268 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001269 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001270static efi_status_t EFIAPI efi_register_protocol_notify(
1271 const efi_guid_t *protocol,
1272 struct efi_event *event,
1273 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001274{
Rob Clark778e6af2017-09-13 18:05:41 -04001275 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001276 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1277}
1278
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001279/**
Mario Six78a88f72018-07-10 08:40:17 +02001280 * efi_search() - determine if an EFI handle implements a protocol
1281 * @search_type: selection criterion
1282 * @protocol: GUID of the protocol
1283 * @search_key: registration key
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001284 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001285 *
1286 * See the documentation of the LocateHandle service in the UEFI specification.
1287 *
Mario Six78a88f72018-07-10 08:40:17 +02001288 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001289 */
Alexander Grafbee91162016-03-04 01:09:59 +01001290static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001291 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001292 efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001293{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001294 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001295
1296 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001297 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001298 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001299 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001300 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001301 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001302 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001303 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001304 return (ret != EFI_SUCCESS);
1305 default:
1306 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001307 return -1;
1308 }
Alexander Grafbee91162016-03-04 01:09:59 +01001309}
1310
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001311/**
Mario Six78a88f72018-07-10 08:40:17 +02001312 * efi_locate_handle() - locate handles implementing a protocol
1313 * @search_type: selection criterion
1314 * @protocol: GUID of the protocol
1315 * @search_key: registration key
1316 * @buffer_size: size of the buffer to receive the handles in bytes
1317 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001318 *
1319 * This function is meant for U-Boot internal calls. For the API implementation
1320 * of the LocateHandle service see efi_locate_handle_ext.
1321 *
Mario Six78a88f72018-07-10 08:40:17 +02001322 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001323 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001324static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001325 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001326 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001327 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001328{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001329 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001330 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001331
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001332 /* Check parameters */
1333 switch (search_type) {
1334 case ALL_HANDLES:
1335 break;
1336 case BY_REGISTER_NOTIFY:
1337 if (!search_key)
1338 return EFI_INVALID_PARAMETER;
1339 /* RegisterProtocolNotify is not implemented yet */
1340 return EFI_UNSUPPORTED;
1341 case BY_PROTOCOL:
1342 if (!protocol)
1343 return EFI_INVALID_PARAMETER;
1344 break;
1345 default:
1346 return EFI_INVALID_PARAMETER;
1347 }
1348
1349 /*
1350 * efi_locate_handle_buffer uses this function for
1351 * the calculation of the necessary buffer size.
1352 * So do not require a buffer for buffersize == 0.
1353 */
1354 if (!buffer_size || (*buffer_size && !buffer))
1355 return EFI_INVALID_PARAMETER;
1356
Alexander Grafbee91162016-03-04 01:09:59 +01001357 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001358 list_for_each_entry(efiobj, &efi_obj_list, link) {
1359 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001360 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001361 }
1362
1363 if (*buffer_size < size) {
1364 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001365 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001366 }
1367
Rob Clark796a78c2017-08-06 14:10:07 -04001368 *buffer_size = size;
1369 if (size == 0)
1370 return EFI_NOT_FOUND;
1371
Alexander Grafbee91162016-03-04 01:09:59 +01001372 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001373 list_for_each_entry(efiobj, &efi_obj_list, link) {
1374 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001375 *buffer++ = efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01001376 }
1377
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001378 return EFI_SUCCESS;
1379}
1380
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001381/**
Mario Six78a88f72018-07-10 08:40:17 +02001382 * efi_locate_handle_ext() - locate handles implementing a protocol.
1383 * @search_type: selection criterion
1384 * @protocol: GUID of the protocol
1385 * @search_key: registration key
1386 * @buffer_size: size of the buffer to receive the handles in bytes
1387 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001388 *
1389 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001390 *
Mario Six78a88f72018-07-10 08:40:17 +02001391 * See the Unified Extensible Firmware Interface (UEFI) specification for
1392 * details.
1393 *
1394 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001395 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001396static efi_status_t EFIAPI efi_locate_handle_ext(
1397 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001398 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001399 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001400{
Rob Clark778e6af2017-09-13 18:05:41 -04001401 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001402 buffer_size, buffer);
1403
1404 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1405 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001406}
1407
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001408/**
Mario Six78a88f72018-07-10 08:40:17 +02001409 * efi_remove_configuration_table() - collapses configuration table entries,
1410 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001411 *
Mario Six78a88f72018-07-10 08:40:17 +02001412 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001413 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001414static void efi_remove_configuration_table(int i)
1415{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001416 struct efi_configuration_table *this = &systab.tables[i];
1417 struct efi_configuration_table *next = &systab.tables[i + 1];
1418 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001419
1420 memmove(this, next, (ulong)end - (ulong)next);
1421 systab.nr_tables--;
1422}
1423
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001424/**
Mario Six78a88f72018-07-10 08:40:17 +02001425 * efi_install_configuration_table() - adds, updates, or removes a
1426 * configuration table
1427 * @guid: GUID of the installed table
1428 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001429 *
1430 * This function is used for internal calls. For the API implementation of the
1431 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1432 *
Mario Six78a88f72018-07-10 08:40:17 +02001433 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001434 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001435efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1436 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001437{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001438 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001439 int i;
1440
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001441 if (!guid)
1442 return EFI_INVALID_PARAMETER;
1443
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001444 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001445 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001446 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001447 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001448 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001449 else
1450 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001451 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001452 }
1453 }
1454
Alexander Grafd98cdf62017-07-26 13:41:04 +02001455 if (!table)
1456 return EFI_NOT_FOUND;
1457
Alexander Grafbee91162016-03-04 01:09:59 +01001458 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001459 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001460 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001461
1462 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001463 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1464 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001465 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001466
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001467out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001468 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001469 efi_update_table_header_crc32(&systab.hdr);
1470
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001471 /* Notify that the configuration table was changed */
1472 list_for_each_entry(evt, &efi_events, link) {
1473 if (evt->group && !guidcmp(evt->group, guid)) {
1474 efi_signal_event(evt, false);
1475 break;
1476 }
1477 }
1478
Alexander Graf488bf122016-08-19 01:23:24 +02001479 return EFI_SUCCESS;
1480}
1481
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001482/**
Mario Six78a88f72018-07-10 08:40:17 +02001483 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1484 * configuration table.
1485 * @guid: GUID of the installed table
1486 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001487 *
1488 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001489 *
Mario Six78a88f72018-07-10 08:40:17 +02001490 * See the Unified Extensible Firmware Interface (UEFI) specification for
1491 * details.
1492 *
1493 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001494 */
Alexander Graf488bf122016-08-19 01:23:24 +02001495static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1496 void *table)
1497{
Rob Clark778e6af2017-09-13 18:05:41 -04001498 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001499 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001500}
1501
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001502/**
Mario Six78a88f72018-07-10 08:40:17 +02001503 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001504 *
1505 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001506 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001507 *
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001508 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1509 * code is returned.
1510 *
1511 * @device_path: device path of the loaded image
1512 * @file_path: file path of the loaded image
1513 * @handle_ptr: handle of the loaded image
1514 * @info_ptr: loaded image protocol
1515 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001516 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001517efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1518 struct efi_device_path *file_path,
1519 struct efi_loaded_image_obj **handle_ptr,
1520 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001521{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001522 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001523 struct efi_loaded_image *info = NULL;
1524 struct efi_loaded_image_obj *obj = NULL;
1525
1526 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1527 *handle_ptr = NULL;
1528 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001529
1530 info = calloc(1, sizeof(*info));
1531 if (!info)
1532 return EFI_OUT_OF_RESOURCES;
1533 obj = calloc(1, sizeof(*obj));
1534 if (!obj) {
1535 free(info);
1536 return EFI_OUT_OF_RESOURCES;
1537 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001538
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001539 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001540 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001541
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001542 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001543 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001544 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001545
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001546 if (device_path) {
1547 info->device_handle = efi_dp_find_obj(device_path, NULL);
1548 /*
1549 * When asking for the device path interface, return
1550 * bootefi_device_path
1551 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001552 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001553 &efi_guid_device_path, device_path);
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001554 if (ret != EFI_SUCCESS)
1555 goto failure;
1556 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001557
1558 /*
1559 * When asking for the loaded_image interface, just
1560 * return handle which points to loaded_image_info
1561 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001562 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001563 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001564 if (ret != EFI_SUCCESS)
1565 goto failure;
1566
Alexander Graf5fbb2892019-02-11 15:24:00 +01001567#if CONFIG_IS_ENABLED(EFI_LOADER_HII)
Leif Lindholmc9bfb222019-01-21 12:12:57 +09001568 ret = efi_add_protocol(&obj->header,
1569 &efi_guid_hii_string_protocol,
1570 (void *)&efi_hii_string);
1571 if (ret != EFI_SUCCESS)
1572 goto failure;
1573
1574 ret = efi_add_protocol(&obj->header,
1575 &efi_guid_hii_database_protocol,
1576 (void *)&efi_hii_database);
1577 if (ret != EFI_SUCCESS)
1578 goto failure;
1579
AKASHI Takahirocb728e52019-01-21 12:13:00 +09001580 ret = efi_add_protocol(&obj->header,
1581 &efi_guid_hii_config_routing_protocol,
1582 (void *)&efi_hii_config_routing);
1583 if (ret != EFI_SUCCESS)
1584 goto failure;
Alexander Graf5fbb2892019-02-11 15:24:00 +01001585#endif
AKASHI Takahirocb728e52019-01-21 12:13:00 +09001586
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001587 *info_ptr = info;
1588 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001589
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001590 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001591failure:
1592 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001593 efi_delete_handle(&obj->header);
1594 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001595 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001596}
1597
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001598/**
Mario Six78a88f72018-07-10 08:40:17 +02001599 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001600 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001601 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1602 * callers obligation to update the memory type as needed.
1603 *
1604 * @file_path: the path of the image to load
1605 * @buffer: buffer containing the loaded image
1606 * @size: size of the loaded image
1607 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001608 */
Rob Clark9975fe92017-09-13 18:05:38 -04001609efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001610 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001611{
1612 struct efi_file_info *info = NULL;
1613 struct efi_file_handle *f;
1614 static efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001615 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001616 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001617
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001618 /* In case of failure nothing is returned */
1619 *buffer = NULL;
1620 *size = 0;
1621
1622 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001623 f = efi_file_from_path(file_path);
1624 if (!f)
1625 return EFI_DEVICE_ERROR;
1626
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001627 /* Get file size */
Rob Clark838ee4b2017-09-13 18:05:35 -04001628 bs = 0;
1629 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1630 &bs, info));
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001631 if (ret != EFI_BUFFER_TOO_SMALL) {
1632 ret = EFI_DEVICE_ERROR;
1633 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001634 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001635
1636 info = malloc(bs);
1637 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1638 info));
Rob Clark838ee4b2017-09-13 18:05:35 -04001639 if (ret != EFI_SUCCESS)
1640 goto error;
1641
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001642 /*
1643 * When reading the file we do not yet know if it contains an
1644 * application, a boottime driver, or a runtime driver. So here we
1645 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1646 * update the reservation according to the image type.
1647 */
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001648 bs = info->file_size;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001649 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1650 EFI_BOOT_SERVICES_DATA,
1651 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001652 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001653 ret = EFI_OUT_OF_RESOURCES;
1654 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001655 }
1656
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001657 /* Read file */
1658 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1659 if (ret != EFI_SUCCESS)
1660 efi_free_pages(addr, efi_size_in_pages(bs));
1661 *buffer = (void *)(uintptr_t)addr;
1662 *size = bs;
1663error:
1664 EFI_CALL(f->close(f));
1665 free(info);
Rob Clark838ee4b2017-09-13 18:05:35 -04001666 return ret;
1667}
1668
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001669/**
Mario Six78a88f72018-07-10 08:40:17 +02001670 * efi_load_image() - load an EFI image into memory
1671 * @boot_policy: true for request originating from the boot manager
1672 * @parent_image: the caller's image handle
1673 * @file_path: the path of the image to load
1674 * @source_buffer: memory location from which the image is installed
1675 * @source_size: size of the memory area from which the image is installed
1676 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001677 *
1678 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001679 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001680 * See the Unified Extensible Firmware Interface (UEFI) specification
1681 * for details.
1682 *
Mario Six78a88f72018-07-10 08:40:17 +02001683 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001684 */
Alexander Grafbee91162016-03-04 01:09:59 +01001685static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1686 efi_handle_t parent_image,
1687 struct efi_device_path *file_path,
1688 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001689 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001690 efi_handle_t *image_handle)
1691{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001692 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04001693 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001694 struct efi_loaded_image_obj **image_obj =
1695 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001696 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001697 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01001698
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001699 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001700 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001701
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001702 if (!image_handle || !parent_image) {
1703 ret = EFI_INVALID_PARAMETER;
1704 goto error;
1705 }
1706
1707 if (!source_buffer && !file_path) {
1708 ret = EFI_NOT_FOUND;
1709 goto error;
1710 }
1711
Rob Clark838ee4b2017-09-13 18:05:35 -04001712 if (!source_buffer) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001713 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001714 &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001715 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001716 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001717 /*
1718 * split file_path which contains both the device and
1719 * file parts:
1720 */
1721 efi_dp_split_file_path(file_path, &dp, &fp);
Rob Clark838ee4b2017-09-13 18:05:35 -04001722 } else {
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001723 /* In this case, file_path is the "device" path, i.e.
Rob Clark838ee4b2017-09-13 18:05:35 -04001724 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1725 */
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001726 dest_buffer = source_buffer;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001727 dp = file_path;
1728 fp = NULL;
Rob Clark838ee4b2017-09-13 18:05:35 -04001729 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001730 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001731 if (ret == EFI_SUCCESS)
1732 ret = efi_load_pe(*image_obj, dest_buffer, info);
1733 if (!source_buffer)
1734 /* Release buffer to which file was loaded */
1735 efi_free_pages((uintptr_t)dest_buffer,
1736 efi_size_in_pages(source_size));
1737 if (ret == EFI_SUCCESS) {
1738 info->system_table = &systab;
1739 info->parent_handle = parent_image;
1740 } else {
1741 /* The image is invalid. Release all associated resources. */
1742 efi_delete_handle(*image_handle);
1743 *image_handle = NULL;
1744 free(info);
1745 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001746error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001747 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001748}
1749
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001750/**
Mario Six78a88f72018-07-10 08:40:17 +02001751 * efi_unload_image() - unload an EFI image
1752 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001753 *
1754 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001755 *
Mario Six78a88f72018-07-10 08:40:17 +02001756 * See the Unified Extensible Firmware Interface (UEFI) specification for
1757 * details.
1758 *
1759 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001760 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001761static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001762{
1763 struct efi_object *efiobj;
1764
1765 EFI_ENTRY("%p", image_handle);
1766 efiobj = efi_search_obj(image_handle);
1767 if (efiobj)
1768 list_del(&efiobj->link);
1769
1770 return EFI_EXIT(EFI_SUCCESS);
1771}
1772
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001773/**
Alexander Graff31239a2018-11-15 21:23:47 +01001774 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1775 */
1776static void efi_exit_caches(void)
1777{
1778#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1779 /*
1780 * Grub on 32bit ARM needs to have caches disabled before jumping into
1781 * a zImage, but does not know of all cache layers. Give it a hand.
1782 */
1783 if (efi_is_direct_boot)
1784 cleanup_before_linux();
1785#endif
1786}
1787
1788/**
Mario Six78a88f72018-07-10 08:40:17 +02001789 * efi_exit_boot_services() - stop all boot services
1790 * @image_handle: handle of the loaded image
1791 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001792 *
1793 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001794 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001795 * See the Unified Extensible Firmware Interface (UEFI) specification
1796 * for details.
1797 *
Mario Six78a88f72018-07-10 08:40:17 +02001798 * All timer events are disabled. For exit boot services events the
1799 * notification function is called. The boot services are disabled in the
1800 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001801 *
Mario Six78a88f72018-07-10 08:40:17 +02001802 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001803 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001804static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001805 unsigned long map_key)
1806{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001807 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001808
Alexander Grafbee91162016-03-04 01:09:59 +01001809 EFI_ENTRY("%p, %ld", image_handle, map_key);
1810
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001811 /* Check that the caller has read the current memory map */
1812 if (map_key != efi_memory_map_key)
1813 return EFI_INVALID_PARAMETER;
1814
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001815 /* Make sure that notification functions are not called anymore */
1816 efi_tpl = TPL_HIGH_LEVEL;
1817
1818 /* Check if ExitBootServices has already been called */
1819 if (!systab.boottime)
1820 return EFI_EXIT(EFI_SUCCESS);
1821
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001822 /* Add related events to the event group */
1823 list_for_each_entry(evt, &efi_events, link) {
1824 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1825 evt->group = &efi_guid_event_group_exit_boot_services;
1826 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001827 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001828 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001829 if (evt->group &&
1830 !guidcmp(evt->group,
1831 &efi_guid_event_group_exit_boot_services)) {
1832 efi_signal_event(evt, false);
1833 break;
1834 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001835 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001836
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001837 /* TODO: Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001838
Alexander Grafb7b84102016-11-17 01:02:57 +01001839 board_quiesce_devices();
1840
Alexander Graff31239a2018-11-15 21:23:47 +01001841 /* Fix up caches for EFI payloads if necessary */
1842 efi_exit_caches();
1843
Alexander Grafbee91162016-03-04 01:09:59 +01001844 /* This stops all lingering devices */
1845 bootm_disable_interrupts();
1846
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001847 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001848 systab.con_in_handle = NULL;
1849 systab.con_in = NULL;
1850 systab.con_out_handle = NULL;
1851 systab.con_out = NULL;
1852 systab.stderr_handle = NULL;
1853 systab.std_err = NULL;
1854 systab.boottime = NULL;
1855
1856 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001857 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001858
Alexander Grafbee91162016-03-04 01:09:59 +01001859 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001860 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001861 WATCHDOG_RESET();
1862
1863 return EFI_EXIT(EFI_SUCCESS);
1864}
1865
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001866/**
Mario Six78a88f72018-07-10 08:40:17 +02001867 * efi_get_next_monotonic_count() - get next value of the counter
1868 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001869 *
1870 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001871 *
Mario Six78a88f72018-07-10 08:40:17 +02001872 * See the Unified Extensible Firmware Interface (UEFI) specification for
1873 * details.
1874 *
1875 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001876 */
Alexander Grafbee91162016-03-04 01:09:59 +01001877static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1878{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001879 static uint64_t mono;
1880
Alexander Grafbee91162016-03-04 01:09:59 +01001881 EFI_ENTRY("%p", count);
1882 *count = mono++;
1883 return EFI_EXIT(EFI_SUCCESS);
1884}
1885
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001886/**
Mario Six78a88f72018-07-10 08:40:17 +02001887 * efi_stall() - sleep
1888 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001889 *
Mario Six78a88f72018-07-10 08:40:17 +02001890 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001891 *
Mario Six78a88f72018-07-10 08:40:17 +02001892 * See the Unified Extensible Firmware Interface (UEFI) specification for
1893 * details.
1894 *
1895 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001896 */
Alexander Grafbee91162016-03-04 01:09:59 +01001897static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1898{
1899 EFI_ENTRY("%ld", microseconds);
1900 udelay(microseconds);
1901 return EFI_EXIT(EFI_SUCCESS);
1902}
1903
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001904/**
Mario Six78a88f72018-07-10 08:40:17 +02001905 * efi_set_watchdog_timer() - reset the watchdog timer
1906 * @timeout: seconds before reset by watchdog
1907 * @watchdog_code: code to be logged when resetting
1908 * @data_size: size of buffer in bytes
1909 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001910 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001911 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001912 *
Mario Six78a88f72018-07-10 08:40:17 +02001913 * See the Unified Extensible Firmware Interface (UEFI) specification for
1914 * details.
1915 *
1916 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001917 */
Alexander Grafbee91162016-03-04 01:09:59 +01001918static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1919 uint64_t watchdog_code,
1920 unsigned long data_size,
1921 uint16_t *watchdog_data)
1922{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001923 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001924 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001925 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001926}
1927
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001928/**
Mario Six78a88f72018-07-10 08:40:17 +02001929 * efi_close_protocol() - close a protocol
1930 * @handle: handle on which the protocol shall be closed
1931 * @protocol: GUID of the protocol to close
1932 * @agent_handle: handle of the driver
1933 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001934 *
1935 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001936 *
Mario Six78a88f72018-07-10 08:40:17 +02001937 * See the Unified Extensible Firmware Interface (UEFI) specification for
1938 * details.
1939 *
1940 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001941 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001942static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001943 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001944 efi_handle_t agent_handle,
1945 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001946{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001947 struct efi_handler *handler;
1948 struct efi_open_protocol_info_item *item;
1949 struct efi_open_protocol_info_item *pos;
1950 efi_status_t r;
1951
Rob Clark778e6af2017-09-13 18:05:41 -04001952 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001953 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001954
1955 if (!agent_handle) {
1956 r = EFI_INVALID_PARAMETER;
1957 goto out;
1958 }
1959 r = efi_search_protocol(handle, protocol, &handler);
1960 if (r != EFI_SUCCESS)
1961 goto out;
1962
1963 r = EFI_NOT_FOUND;
1964 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1965 if (item->info.agent_handle == agent_handle &&
1966 item->info.controller_handle == controller_handle) {
1967 efi_delete_open_info(item);
1968 r = EFI_SUCCESS;
1969 break;
1970 }
1971 }
1972out:
1973 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001974}
1975
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001976/**
Mario Six78a88f72018-07-10 08:40:17 +02001977 * efi_open_protocol_information() - provide information about then open status
1978 * of a protocol on a handle
1979 * @handle: handle for which the information shall be retrieved
1980 * @protocol: GUID of the protocol
1981 * @entry_buffer: buffer to receive the open protocol information
1982 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001983 *
1984 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001985 *
Mario Six78a88f72018-07-10 08:40:17 +02001986 * See the Unified Extensible Firmware Interface (UEFI) specification for
1987 * details.
1988 *
1989 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001990 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001991static efi_status_t EFIAPI efi_open_protocol_information(
1992 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001993 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001994 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001995{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001996 unsigned long buffer_size;
1997 unsigned long count;
1998 struct efi_handler *handler;
1999 struct efi_open_protocol_info_item *item;
2000 efi_status_t r;
2001
Rob Clark778e6af2017-09-13 18:05:41 -04002002 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002003 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002004
2005 /* Check parameters */
2006 if (!entry_buffer) {
2007 r = EFI_INVALID_PARAMETER;
2008 goto out;
2009 }
2010 r = efi_search_protocol(handle, protocol, &handler);
2011 if (r != EFI_SUCCESS)
2012 goto out;
2013
2014 /* Count entries */
2015 count = 0;
2016 list_for_each_entry(item, &handler->open_infos, link) {
2017 if (item->info.open_count)
2018 ++count;
2019 }
2020 *entry_count = count;
2021 *entry_buffer = NULL;
2022 if (!count) {
2023 r = EFI_SUCCESS;
2024 goto out;
2025 }
2026
2027 /* Copy entries */
2028 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002029 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002030 (void **)entry_buffer);
2031 if (r != EFI_SUCCESS)
2032 goto out;
2033 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2034 if (item->info.open_count)
2035 (*entry_buffer)[--count] = item->info;
2036 }
2037out:
2038 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002039}
2040
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002041/**
Mario Six78a88f72018-07-10 08:40:17 +02002042 * efi_protocols_per_handle() - get protocols installed on a handle
2043 * @handle: handle for which the information is retrieved
2044 * @protocol_buffer: buffer with protocol GUIDs
2045 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002046 *
2047 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002048 *
Mario Six78a88f72018-07-10 08:40:17 +02002049 * See the Unified Extensible Firmware Interface (UEFI) specification for
2050 * details.
2051 *
2052 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002053 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002054static efi_status_t EFIAPI efi_protocols_per_handle(
2055 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002056 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002057{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002058 unsigned long buffer_size;
2059 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002060 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002061 efi_status_t r;
2062
Alexander Grafbee91162016-03-04 01:09:59 +01002063 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2064 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002065
2066 if (!handle || !protocol_buffer || !protocol_buffer_count)
2067 return EFI_EXIT(EFI_INVALID_PARAMETER);
2068
2069 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002070 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002071
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002072 efiobj = efi_search_obj(handle);
2073 if (!efiobj)
2074 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002075
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002076 /* Count protocols */
2077 list_for_each(protocol_handle, &efiobj->protocols) {
2078 ++*protocol_buffer_count;
2079 }
2080
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002081 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002082 if (*protocol_buffer_count) {
2083 size_t j = 0;
2084
2085 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002086 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002087 (void **)protocol_buffer);
2088 if (r != EFI_SUCCESS)
2089 return EFI_EXIT(r);
2090 list_for_each(protocol_handle, &efiobj->protocols) {
2091 struct efi_handler *protocol;
2092
2093 protocol = list_entry(protocol_handle,
2094 struct efi_handler, link);
2095 (*protocol_buffer)[j] = (void *)protocol->guid;
2096 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002097 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002098 }
2099
2100 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002101}
2102
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002103/**
Mario Six78a88f72018-07-10 08:40:17 +02002104 * efi_locate_handle_buffer() - locate handles implementing a protocol
2105 * @search_type: selection criterion
2106 * @protocol: GUID of the protocol
2107 * @search_key: registration key
2108 * @no_handles: number of returned handles
2109 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002110 *
2111 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002112 *
Mario Six78a88f72018-07-10 08:40:17 +02002113 * See the Unified Extensible Firmware Interface (UEFI) specification for
2114 * details.
2115 *
2116 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002117 */
Alexander Grafbee91162016-03-04 01:09:59 +01002118static efi_status_t EFIAPI efi_locate_handle_buffer(
2119 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002120 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002121 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002122{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002123 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002124 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002125
Rob Clark778e6af2017-09-13 18:05:41 -04002126 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002127 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002128
2129 if (!no_handles || !buffer) {
2130 r = EFI_INVALID_PARAMETER;
2131 goto out;
2132 }
2133 *no_handles = 0;
2134 *buffer = NULL;
2135 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2136 *buffer);
2137 if (r != EFI_BUFFER_TOO_SMALL)
2138 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002139 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002140 (void **)buffer);
2141 if (r != EFI_SUCCESS)
2142 goto out;
2143 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2144 *buffer);
2145 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002146 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002147out:
2148 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002149}
2150
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002151/**
Mario Six78a88f72018-07-10 08:40:17 +02002152 * efi_locate_protocol() - find an interface implementing a protocol
2153 * @protocol: GUID of the protocol
2154 * @registration: registration key passed to the notification function
2155 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002156 *
2157 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002158 *
Mario Six78a88f72018-07-10 08:40:17 +02002159 * See the Unified Extensible Firmware Interface (UEFI) specification for
2160 * details.
2161 *
2162 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002163 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002164static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002165 void *registration,
2166 void **protocol_interface)
2167{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002168 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002169 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002170
Rob Clark778e6af2017-09-13 18:05:41 -04002171 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002172
2173 if (!protocol || !protocol_interface)
2174 return EFI_EXIT(EFI_INVALID_PARAMETER);
2175
2176 list_for_each(lhandle, &efi_obj_list) {
2177 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002178 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002179
2180 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002181
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002182 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002183 if (ret == EFI_SUCCESS) {
2184 *protocol_interface = handler->protocol_interface;
2185 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002186 }
2187 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002188 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002189
2190 return EFI_EXIT(EFI_NOT_FOUND);
2191}
2192
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002193/**
Mario Six78a88f72018-07-10 08:40:17 +02002194 * efi_locate_device_path() - Get the device path and handle of an device
2195 * implementing a protocol
2196 * @protocol: GUID of the protocol
2197 * @device_path: device path
2198 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002199 *
2200 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002201 *
Mario Six78a88f72018-07-10 08:40:17 +02002202 * See the Unified Extensible Firmware Interface (UEFI) specification for
2203 * details.
2204 *
2205 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002206 */
2207static efi_status_t EFIAPI efi_locate_device_path(
2208 const efi_guid_t *protocol,
2209 struct efi_device_path **device_path,
2210 efi_handle_t *device)
2211{
2212 struct efi_device_path *dp;
2213 size_t i;
2214 struct efi_handler *handler;
2215 efi_handle_t *handles;
2216 size_t len, len_dp;
2217 size_t len_best = 0;
2218 efi_uintn_t no_handles;
2219 u8 *remainder;
2220 efi_status_t ret;
2221
2222 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2223
2224 if (!protocol || !device_path || !*device_path || !device) {
2225 ret = EFI_INVALID_PARAMETER;
2226 goto out;
2227 }
2228
2229 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002230 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002231
2232 /* Get all handles implementing the protocol */
2233 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2234 &no_handles, &handles));
2235 if (ret != EFI_SUCCESS)
2236 goto out;
2237
2238 for (i = 0; i < no_handles; ++i) {
2239 /* Find the device path protocol */
2240 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2241 &handler);
2242 if (ret != EFI_SUCCESS)
2243 continue;
2244 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002245 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002246 /*
2247 * This handle can only be a better fit
2248 * if its device path length is longer than the best fit and
2249 * if its device path length is shorter of equal the searched
2250 * device path.
2251 */
2252 if (len_dp <= len_best || len_dp > len)
2253 continue;
2254 /* Check if dp is a subpath of device_path */
2255 if (memcmp(*device_path, dp, len_dp))
2256 continue;
2257 *device = handles[i];
2258 len_best = len_dp;
2259 }
2260 if (len_best) {
2261 remainder = (u8 *)*device_path + len_best;
2262 *device_path = (struct efi_device_path *)remainder;
2263 ret = EFI_SUCCESS;
2264 } else {
2265 ret = EFI_NOT_FOUND;
2266 }
2267out:
2268 return EFI_EXIT(ret);
2269}
2270
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002271/**
Mario Six78a88f72018-07-10 08:40:17 +02002272 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2273 * interfaces
2274 * @handle: handle on which the protocol interfaces shall be installed
2275 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2276 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002277 *
2278 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002279 *
Mario Six78a88f72018-07-10 08:40:17 +02002280 * See the Unified Extensible Firmware Interface (UEFI) specification for
2281 * details.
2282 *
2283 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002284 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002285static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2286 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002287{
2288 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002289
Alexander Grafbeb077a2018-06-18 17:23:05 +02002290 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002291 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002292 void *protocol_interface;
2293 efi_status_t r = EFI_SUCCESS;
2294 int i = 0;
2295
2296 if (!handle)
2297 return EFI_EXIT(EFI_INVALID_PARAMETER);
2298
Alexander Grafbeb077a2018-06-18 17:23:05 +02002299 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002300 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002301 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002302 if (!protocol)
2303 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002304 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002305 r = EFI_CALL(efi_install_protocol_interface(
2306 handle, protocol,
2307 EFI_NATIVE_INTERFACE,
2308 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002309 if (r != EFI_SUCCESS)
2310 break;
2311 i++;
2312 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002313 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002314 if (r == EFI_SUCCESS)
2315 return EFI_EXIT(r);
2316
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002317 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002318 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002319 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002320 protocol = efi_va_arg(argptr, efi_guid_t*);
2321 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002322 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002323 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002324 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002325 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002326
2327 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002328}
2329
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002330/**
Mario Six78a88f72018-07-10 08:40:17 +02002331 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2332 * interfaces
2333 * @handle: handle from which the protocol interfaces shall be removed
2334 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2335 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002336 *
2337 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002338 *
Mario Six78a88f72018-07-10 08:40:17 +02002339 * See the Unified Extensible Firmware Interface (UEFI) specification for
2340 * details.
2341 *
2342 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002343 */
Alexander Grafbee91162016-03-04 01:09:59 +01002344static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002345 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002346{
2347 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002348
Alexander Grafbeb077a2018-06-18 17:23:05 +02002349 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002350 const efi_guid_t *protocol;
2351 void *protocol_interface;
2352 efi_status_t r = EFI_SUCCESS;
2353 size_t i = 0;
2354
2355 if (!handle)
2356 return EFI_EXIT(EFI_INVALID_PARAMETER);
2357
Alexander Grafbeb077a2018-06-18 17:23:05 +02002358 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002359 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002360 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002361 if (!protocol)
2362 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002363 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002364 r = efi_uninstall_protocol(handle, protocol,
2365 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002366 if (r != EFI_SUCCESS)
2367 break;
2368 i++;
2369 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002370 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002371 if (r == EFI_SUCCESS) {
2372 /* If the last protocol has been removed, delete the handle. */
2373 if (list_empty(&handle->protocols)) {
2374 list_del(&handle->link);
2375 free(handle);
2376 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002377 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002378 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002379
2380 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002381 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002382 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002383 protocol = efi_va_arg(argptr, efi_guid_t*);
2384 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002385 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2386 EFI_NATIVE_INTERFACE,
2387 protocol_interface));
2388 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002389 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002390
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002391 /* In case of an error always return EFI_INVALID_PARAMETER */
2392 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002393}
2394
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002395/**
Mario Six78a88f72018-07-10 08:40:17 +02002396 * efi_calculate_crc32() - calculate cyclic redundancy code
2397 * @data: buffer with data
2398 * @data_size: size of buffer in bytes
2399 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002400 *
2401 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002402 *
Mario Six78a88f72018-07-10 08:40:17 +02002403 * See the Unified Extensible Firmware Interface (UEFI) specification for
2404 * details.
2405 *
2406 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002407 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002408static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2409 efi_uintn_t data_size,
2410 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002411{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002412 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002413 *crc32_p = crc32(0, data, data_size);
2414 return EFI_EXIT(EFI_SUCCESS);
2415}
2416
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002417/**
Mario Six78a88f72018-07-10 08:40:17 +02002418 * efi_copy_mem() - copy memory
2419 * @destination: destination of the copy operation
2420 * @source: source of the copy operation
2421 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002422 *
2423 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002424 *
Mario Six78a88f72018-07-10 08:40:17 +02002425 * See the Unified Extensible Firmware Interface (UEFI) specification for
2426 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002427 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002428static void EFIAPI efi_copy_mem(void *destination, const void *source,
2429 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002430{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002431 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002432 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002433 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002434}
2435
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002436/**
Mario Six78a88f72018-07-10 08:40:17 +02002437 * efi_set_mem() - Fill memory with a byte value.
2438 * @buffer: buffer to fill
2439 * @size: size of buffer in bytes
2440 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002441 *
2442 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002443 *
Mario Six78a88f72018-07-10 08:40:17 +02002444 * See the Unified Extensible Firmware Interface (UEFI) specification for
2445 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002446 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002447static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002448{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002449 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002450 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002451 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002452}
2453
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002454/**
Mario Six78a88f72018-07-10 08:40:17 +02002455 * efi_protocol_open() - open protocol interface on a handle
2456 * @handler: handler of a protocol
2457 * @protocol_interface: interface implementing the protocol
2458 * @agent_handle: handle of the driver
2459 * @controller_handle: handle of the controller
2460 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002461 *
Mario Six78a88f72018-07-10 08:40:17 +02002462 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002463 */
2464static efi_status_t efi_protocol_open(
2465 struct efi_handler *handler,
2466 void **protocol_interface, void *agent_handle,
2467 void *controller_handle, uint32_t attributes)
2468{
2469 struct efi_open_protocol_info_item *item;
2470 struct efi_open_protocol_info_entry *match = NULL;
2471 bool opened_by_driver = false;
2472 bool opened_exclusive = false;
2473
2474 /* If there is no agent, only return the interface */
2475 if (!agent_handle)
2476 goto out;
2477
2478 /* For TEST_PROTOCOL ignore interface attribute */
2479 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2480 *protocol_interface = NULL;
2481
2482 /*
2483 * Check if the protocol is already opened by a driver with the same
2484 * attributes or opened exclusively
2485 */
2486 list_for_each_entry(item, &handler->open_infos, link) {
2487 if (item->info.agent_handle == agent_handle) {
2488 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2489 (item->info.attributes == attributes))
2490 return EFI_ALREADY_STARTED;
2491 }
2492 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2493 opened_exclusive = true;
2494 }
2495
2496 /* Only one controller can open the protocol exclusively */
2497 if (opened_exclusive && attributes &
2498 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2499 return EFI_ACCESS_DENIED;
2500
2501 /* Prepare exclusive opening */
2502 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2503 /* Try to disconnect controllers */
2504 list_for_each_entry(item, &handler->open_infos, link) {
2505 if (item->info.attributes ==
2506 EFI_OPEN_PROTOCOL_BY_DRIVER)
2507 EFI_CALL(efi_disconnect_controller(
2508 item->info.controller_handle,
2509 item->info.agent_handle,
2510 NULL));
2511 }
2512 opened_by_driver = false;
2513 /* Check if all controllers are disconnected */
2514 list_for_each_entry(item, &handler->open_infos, link) {
2515 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2516 opened_by_driver = true;
2517 }
Heinrich Schuchardt4f37fa42018-09-30 13:40:43 +02002518 /* Only one controller can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002519 if (opened_by_driver)
2520 return EFI_ACCESS_DENIED;
2521 }
2522
2523 /* Find existing entry */
2524 list_for_each_entry(item, &handler->open_infos, link) {
2525 if (item->info.agent_handle == agent_handle &&
2526 item->info.controller_handle == controller_handle)
2527 match = &item->info;
2528 }
2529 /* None found, create one */
2530 if (!match) {
2531 match = efi_create_open_info(handler);
2532 if (!match)
2533 return EFI_OUT_OF_RESOURCES;
2534 }
2535
2536 match->agent_handle = agent_handle;
2537 match->controller_handle = controller_handle;
2538 match->attributes = attributes;
2539 match->open_count++;
2540
2541out:
2542 /* For TEST_PROTOCOL ignore interface attribute. */
2543 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2544 *protocol_interface = handler->protocol_interface;
2545
2546 return EFI_SUCCESS;
2547}
2548
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002549/**
Mario Six78a88f72018-07-10 08:40:17 +02002550 * efi_open_protocol() - open protocol interface on a handle
2551 * @handle: handle on which the protocol shall be opened
2552 * @protocol: GUID of the protocol
2553 * @protocol_interface: interface implementing the protocol
2554 * @agent_handle: handle of the driver
2555 * @controller_handle: handle of the controller
2556 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002557 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002558 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002559 *
Mario Six78a88f72018-07-10 08:40:17 +02002560 * See the Unified Extensible Firmware Interface (UEFI) specification for
2561 * details.
2562 *
2563 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002564 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002565static efi_status_t EFIAPI efi_open_protocol
2566 (efi_handle_t handle, const efi_guid_t *protocol,
2567 void **protocol_interface, efi_handle_t agent_handle,
2568 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002569{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002570 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002571 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002572
Rob Clark778e6af2017-09-13 18:05:41 -04002573 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002574 protocol_interface, agent_handle, controller_handle,
2575 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002576
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002577 if (!handle || !protocol ||
2578 (!protocol_interface && attributes !=
2579 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2580 goto out;
2581 }
2582
2583 switch (attributes) {
2584 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2585 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2586 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2587 break;
2588 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2589 if (controller_handle == handle)
2590 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002591 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002592 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2593 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002594 /* Check that the controller handle is valid */
2595 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002596 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002597 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002598 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002599 /* Check that the agent handle is valid */
2600 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002601 goto out;
2602 break;
2603 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002604 goto out;
2605 }
2606
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002607 r = efi_search_protocol(handle, protocol, &handler);
2608 if (r != EFI_SUCCESS)
2609 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002610
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002611 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2612 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002613out:
2614 return EFI_EXIT(r);
2615}
2616
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002617/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002618 * efi_start_image() - call the entry point of an image
2619 * @image_handle: handle of the image
2620 * @exit_data_size: size of the buffer
2621 * @exit_data: buffer to receive the exit data of the called image
2622 *
2623 * This function implements the StartImage service.
2624 *
2625 * See the Unified Extensible Firmware Interface (UEFI) specification for
2626 * details.
2627 *
2628 * Return: status code
2629 */
2630efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2631 efi_uintn_t *exit_data_size,
2632 u16 **exit_data)
2633{
2634 struct efi_loaded_image_obj *image_obj =
2635 (struct efi_loaded_image_obj *)image_handle;
2636 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002637 void *info;
2638 efi_handle_t parent_image = current_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002639
2640 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2641
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002642 /* Check parameters */
2643 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2644 &info, NULL, NULL,
2645 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2646 if (ret != EFI_SUCCESS)
2647 return EFI_EXIT(EFI_INVALID_PARAMETER);
2648
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002649 efi_is_direct_boot = false;
2650
2651 /* call the image! */
2652 if (setjmp(&image_obj->exit_jmp)) {
2653 /*
2654 * We called the entry point of the child image with EFI_CALL
2655 * in the lines below. The child image called the Exit() boot
2656 * service efi_exit() which executed the long jump that brought
2657 * us to the current line. This implies that the second half
2658 * of the EFI_CALL macro has not been executed.
2659 */
2660#ifdef CONFIG_ARM
2661 /*
2662 * efi_exit() called efi_restore_gd(). We have to undo this
2663 * otherwise __efi_entry_check() will put the wrong value into
2664 * app_gd.
2665 */
2666 gd = app_gd;
2667#endif
2668 /*
2669 * To get ready to call EFI_EXIT below we have to execute the
2670 * missed out steps of EFI_CALL.
2671 */
2672 assert(__efi_entry_check());
2673 debug("%sEFI: %lu returned by started image\n",
2674 __efi_nesting_dec(),
2675 (unsigned long)((uintptr_t)image_obj->exit_status &
2676 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002677 current_image = parent_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002678 return EFI_EXIT(image_obj->exit_status);
2679 }
2680
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002681 current_image = image_handle;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002682 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2683
2684 /*
2685 * Usually UEFI applications call Exit() instead of returning.
2686 * But because the world doesn't consist of ponies and unicorns,
2687 * we're happy to emulate that behavior on behalf of a payload
2688 * that forgot.
2689 */
2690 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2691}
2692
2693/**
2694 * efi_exit() - leave an EFI application or driver
2695 * @image_handle: handle of the application or driver that is exiting
2696 * @exit_status: status code
2697 * @exit_data_size: size of the buffer in bytes
2698 * @exit_data: buffer with data describing an error
2699 *
2700 * This function implements the Exit service.
2701 *
2702 * See the Unified Extensible Firmware Interface (UEFI) specification for
2703 * details.
2704 *
2705 * Return: status code
2706 */
2707static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2708 efi_status_t exit_status,
2709 efi_uintn_t exit_data_size,
2710 u16 *exit_data)
2711{
2712 /*
2713 * TODO: We should call the unload procedure of the loaded
2714 * image protocol.
2715 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002716 efi_status_t ret;
2717 void *info;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002718 struct efi_loaded_image_obj *image_obj =
2719 (struct efi_loaded_image_obj *)image_handle;
2720
2721 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2722 exit_data_size, exit_data);
2723
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002724 /* Check parameters */
2725 if (image_handle != current_image)
2726 goto out;
2727 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2728 &info, NULL, NULL,
2729 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2730 if (ret != EFI_SUCCESS)
2731 goto out;
2732
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002733 /* Make sure entry/exit counts for EFI world cross-overs match */
2734 EFI_EXIT(exit_status);
2735
2736 /*
2737 * But longjmp out with the U-Boot gd, not the application's, as
2738 * the other end is a setjmp call inside EFI context.
2739 */
2740 efi_restore_gd();
2741
2742 image_obj->exit_status = exit_status;
2743 longjmp(&image_obj->exit_jmp, 1);
2744
2745 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002746out:
2747 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002748}
2749
2750/**
Mario Six78a88f72018-07-10 08:40:17 +02002751 * efi_handle_protocol() - get interface of a protocol on a handle
2752 * @handle: handle on which the protocol shall be opened
2753 * @protocol: GUID of the protocol
2754 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002755 *
2756 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002757 *
Mario Six78a88f72018-07-10 08:40:17 +02002758 * See the Unified Extensible Firmware Interface (UEFI) specification for
2759 * details.
2760 *
2761 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002762 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002763static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002764 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002765 void **protocol_interface)
2766{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002767 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2768 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002769}
2770
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002771/**
Mario Six78a88f72018-07-10 08:40:17 +02002772 * efi_bind_controller() - bind a single driver to a controller
2773 * @controller_handle: controller handle
2774 * @driver_image_handle: driver handle
2775 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002776 *
Mario Six78a88f72018-07-10 08:40:17 +02002777 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002778 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002779static efi_status_t efi_bind_controller(
2780 efi_handle_t controller_handle,
2781 efi_handle_t driver_image_handle,
2782 struct efi_device_path *remain_device_path)
2783{
2784 struct efi_driver_binding_protocol *binding_protocol;
2785 efi_status_t r;
2786
2787 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2788 &efi_guid_driver_binding_protocol,
2789 (void **)&binding_protocol,
2790 driver_image_handle, NULL,
2791 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2792 if (r != EFI_SUCCESS)
2793 return r;
2794 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2795 controller_handle,
2796 remain_device_path));
2797 if (r == EFI_SUCCESS)
2798 r = EFI_CALL(binding_protocol->start(binding_protocol,
2799 controller_handle,
2800 remain_device_path));
2801 EFI_CALL(efi_close_protocol(driver_image_handle,
2802 &efi_guid_driver_binding_protocol,
2803 driver_image_handle, NULL));
2804 return r;
2805}
2806
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002807/**
Mario Six78a88f72018-07-10 08:40:17 +02002808 * efi_connect_single_controller() - connect a single driver to a controller
2809 * @controller_handle: controller
2810 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002811 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002812 *
Mario Six78a88f72018-07-10 08:40:17 +02002813 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002814 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002815static efi_status_t efi_connect_single_controller(
2816 efi_handle_t controller_handle,
2817 efi_handle_t *driver_image_handle,
2818 struct efi_device_path *remain_device_path)
2819{
2820 efi_handle_t *buffer;
2821 size_t count;
2822 size_t i;
2823 efi_status_t r;
2824 size_t connected = 0;
2825
2826 /* Get buffer with all handles with driver binding protocol */
2827 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2828 &efi_guid_driver_binding_protocol,
2829 NULL, &count, &buffer));
2830 if (r != EFI_SUCCESS)
2831 return r;
2832
2833 /* Context Override */
2834 if (driver_image_handle) {
2835 for (; *driver_image_handle; ++driver_image_handle) {
2836 for (i = 0; i < count; ++i) {
2837 if (buffer[i] == *driver_image_handle) {
2838 buffer[i] = NULL;
2839 r = efi_bind_controller(
2840 controller_handle,
2841 *driver_image_handle,
2842 remain_device_path);
2843 /*
2844 * For drivers that do not support the
2845 * controller or are already connected
2846 * we receive an error code here.
2847 */
2848 if (r == EFI_SUCCESS)
2849 ++connected;
2850 }
2851 }
2852 }
2853 }
2854
2855 /*
2856 * TODO: Some overrides are not yet implemented:
2857 * - Platform Driver Override
2858 * - Driver Family Override Search
2859 * - Bus Specific Driver Override
2860 */
2861
2862 /* Driver Binding Search */
2863 for (i = 0; i < count; ++i) {
2864 if (buffer[i]) {
2865 r = efi_bind_controller(controller_handle,
2866 buffer[i],
2867 remain_device_path);
2868 if (r == EFI_SUCCESS)
2869 ++connected;
2870 }
2871 }
2872
2873 efi_free_pool(buffer);
2874 if (!connected)
2875 return EFI_NOT_FOUND;
2876 return EFI_SUCCESS;
2877}
2878
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002879/**
Mario Six78a88f72018-07-10 08:40:17 +02002880 * efi_connect_controller() - connect a controller to a driver
2881 * @controller_handle: handle of the controller
2882 * @driver_image_handle: handle of the driver
2883 * @remain_device_path: device path of a child controller
2884 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002885 *
2886 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002887 *
2888 * See the Unified Extensible Firmware Interface (UEFI) specification for
2889 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002890 *
2891 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002892 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002893 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2894 *
Mario Six78a88f72018-07-10 08:40:17 +02002895 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002896 */
2897static efi_status_t EFIAPI efi_connect_controller(
2898 efi_handle_t controller_handle,
2899 efi_handle_t *driver_image_handle,
2900 struct efi_device_path *remain_device_path,
2901 bool recursive)
2902{
2903 efi_status_t r;
2904 efi_status_t ret = EFI_NOT_FOUND;
2905 struct efi_object *efiobj;
2906
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01002907 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002908 remain_device_path, recursive);
2909
2910 efiobj = efi_search_obj(controller_handle);
2911 if (!efiobj) {
2912 ret = EFI_INVALID_PARAMETER;
2913 goto out;
2914 }
2915
2916 r = efi_connect_single_controller(controller_handle,
2917 driver_image_handle,
2918 remain_device_path);
2919 if (r == EFI_SUCCESS)
2920 ret = EFI_SUCCESS;
2921 if (recursive) {
2922 struct efi_handler *handler;
2923 struct efi_open_protocol_info_item *item;
2924
2925 list_for_each_entry(handler, &efiobj->protocols, link) {
2926 list_for_each_entry(item, &handler->open_infos, link) {
2927 if (item->info.attributes &
2928 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2929 r = EFI_CALL(efi_connect_controller(
2930 item->info.controller_handle,
2931 driver_image_handle,
2932 remain_device_path,
2933 recursive));
2934 if (r == EFI_SUCCESS)
2935 ret = EFI_SUCCESS;
2936 }
2937 }
2938 }
2939 }
2940 /* Check for child controller specified by end node */
2941 if (ret != EFI_SUCCESS && remain_device_path &&
2942 remain_device_path->type == DEVICE_PATH_TYPE_END)
2943 ret = EFI_SUCCESS;
2944out:
2945 return EFI_EXIT(ret);
2946}
2947
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002948/**
Mario Six78a88f72018-07-10 08:40:17 +02002949 * efi_reinstall_protocol_interface() - reinstall protocol interface
2950 * @handle: handle on which the protocol shall be reinstalled
2951 * @protocol: GUID of the protocol to be installed
2952 * @old_interface: interface to be removed
2953 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002954 *
2955 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002956 *
2957 * See the Unified Extensible Firmware Interface (UEFI) specification for
2958 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002959 *
2960 * The old interface is uninstalled. The new interface is installed.
2961 * Drivers are connected.
2962 *
Mario Six78a88f72018-07-10 08:40:17 +02002963 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002964 */
2965static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2966 efi_handle_t handle, const efi_guid_t *protocol,
2967 void *old_interface, void *new_interface)
2968{
2969 efi_status_t ret;
2970
2971 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2972 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002973
2974 /* Uninstall protocol but do not delete handle */
2975 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002976 if (ret != EFI_SUCCESS)
2977 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002978
2979 /* Install the new protocol */
2980 ret = efi_add_protocol(handle, protocol, new_interface);
2981 /*
2982 * The UEFI spec does not specify what should happen to the handle
2983 * if in case of an error no protocol interface remains on the handle.
2984 * So let's do nothing here.
2985 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002986 if (ret != EFI_SUCCESS)
2987 goto out;
2988 /*
2989 * The returned status code has to be ignored.
2990 * Do not create an error if no suitable driver for the handle exists.
2991 */
2992 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2993out:
2994 return EFI_EXIT(ret);
2995}
2996
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002997/**
Mario Six78a88f72018-07-10 08:40:17 +02002998 * efi_get_child_controllers() - get all child controllers associated to a driver
2999 * @efiobj: handle of the controller
3000 * @driver_handle: handle of the driver
3001 * @number_of_children: number of child controllers
3002 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003003 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003004 * The allocated buffer has to be freed with free().
3005 *
Mario Six78a88f72018-07-10 08:40:17 +02003006 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003007 */
3008static efi_status_t efi_get_child_controllers(
3009 struct efi_object *efiobj,
3010 efi_handle_t driver_handle,
3011 efi_uintn_t *number_of_children,
3012 efi_handle_t **child_handle_buffer)
3013{
3014 struct efi_handler *handler;
3015 struct efi_open_protocol_info_item *item;
3016 efi_uintn_t count = 0, i;
3017 bool duplicate;
3018
3019 /* Count all child controller associations */
3020 list_for_each_entry(handler, &efiobj->protocols, link) {
3021 list_for_each_entry(item, &handler->open_infos, link) {
3022 if (item->info.agent_handle == driver_handle &&
3023 item->info.attributes &
3024 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3025 ++count;
3026 }
3027 }
3028 /*
3029 * Create buffer. In case of duplicate child controller assignments
3030 * the buffer will be too large. But that does not harm.
3031 */
3032 *number_of_children = 0;
3033 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3034 if (!*child_handle_buffer)
3035 return EFI_OUT_OF_RESOURCES;
3036 /* Copy unique child handles */
3037 list_for_each_entry(handler, &efiobj->protocols, link) {
3038 list_for_each_entry(item, &handler->open_infos, link) {
3039 if (item->info.agent_handle == driver_handle &&
3040 item->info.attributes &
3041 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3042 /* Check this is a new child controller */
3043 duplicate = false;
3044 for (i = 0; i < *number_of_children; ++i) {
3045 if ((*child_handle_buffer)[i] ==
3046 item->info.controller_handle)
3047 duplicate = true;
3048 }
3049 /* Copy handle to buffer */
3050 if (!duplicate) {
3051 i = (*number_of_children)++;
3052 (*child_handle_buffer)[i] =
3053 item->info.controller_handle;
3054 }
3055 }
3056 }
3057 }
3058 return EFI_SUCCESS;
3059}
3060
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003061/**
Mario Six78a88f72018-07-10 08:40:17 +02003062 * efi_disconnect_controller() - disconnect a controller from a driver
3063 * @controller_handle: handle of the controller
3064 * @driver_image_handle: handle of the driver
3065 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003066 *
3067 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003068 *
Mario Six78a88f72018-07-10 08:40:17 +02003069 * See the Unified Extensible Firmware Interface (UEFI) specification for
3070 * details.
3071 *
3072 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003073 */
3074static efi_status_t EFIAPI efi_disconnect_controller(
3075 efi_handle_t controller_handle,
3076 efi_handle_t driver_image_handle,
3077 efi_handle_t child_handle)
3078{
3079 struct efi_driver_binding_protocol *binding_protocol;
3080 efi_handle_t *child_handle_buffer = NULL;
3081 size_t number_of_children = 0;
3082 efi_status_t r;
3083 size_t stop_count = 0;
3084 struct efi_object *efiobj;
3085
3086 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3087 child_handle);
3088
3089 efiobj = efi_search_obj(controller_handle);
3090 if (!efiobj) {
3091 r = EFI_INVALID_PARAMETER;
3092 goto out;
3093 }
3094
3095 if (child_handle && !efi_search_obj(child_handle)) {
3096 r = EFI_INVALID_PARAMETER;
3097 goto out;
3098 }
3099
3100 /* If no driver handle is supplied, disconnect all drivers */
3101 if (!driver_image_handle) {
3102 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3103 goto out;
3104 }
3105
3106 /* Create list of child handles */
3107 if (child_handle) {
3108 number_of_children = 1;
3109 child_handle_buffer = &child_handle;
3110 } else {
3111 efi_get_child_controllers(efiobj,
3112 driver_image_handle,
3113 &number_of_children,
3114 &child_handle_buffer);
3115 }
3116
3117 /* Get the driver binding protocol */
3118 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3119 &efi_guid_driver_binding_protocol,
3120 (void **)&binding_protocol,
3121 driver_image_handle, NULL,
3122 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3123 if (r != EFI_SUCCESS)
3124 goto out;
3125 /* Remove the children */
3126 if (number_of_children) {
3127 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3128 controller_handle,
3129 number_of_children,
3130 child_handle_buffer));
3131 if (r == EFI_SUCCESS)
3132 ++stop_count;
3133 }
3134 /* Remove the driver */
3135 if (!child_handle)
3136 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3137 controller_handle,
3138 0, NULL));
3139 if (r == EFI_SUCCESS)
3140 ++stop_count;
3141 EFI_CALL(efi_close_protocol(driver_image_handle,
3142 &efi_guid_driver_binding_protocol,
3143 driver_image_handle, NULL));
3144
3145 if (stop_count)
3146 r = EFI_SUCCESS;
3147 else
3148 r = EFI_NOT_FOUND;
3149out:
3150 if (!child_handle)
3151 free(child_handle_buffer);
3152 return EFI_EXIT(r);
3153}
3154
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003155static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003156 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003157 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3158 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003159 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003160 },
3161 .raise_tpl = efi_raise_tpl,
3162 .restore_tpl = efi_restore_tpl,
3163 .allocate_pages = efi_allocate_pages_ext,
3164 .free_pages = efi_free_pages_ext,
3165 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003166 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003167 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003168 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003169 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003170 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003171 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003172 .close_event = efi_close_event,
3173 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003174 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003175 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003176 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003177 .handle_protocol = efi_handle_protocol,
3178 .reserved = NULL,
3179 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003180 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003181 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003182 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003183 .load_image = efi_load_image,
3184 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003185 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003186 .unload_image = efi_unload_image,
3187 .exit_boot_services = efi_exit_boot_services,
3188 .get_next_monotonic_count = efi_get_next_monotonic_count,
3189 .stall = efi_stall,
3190 .set_watchdog_timer = efi_set_watchdog_timer,
3191 .connect_controller = efi_connect_controller,
3192 .disconnect_controller = efi_disconnect_controller,
3193 .open_protocol = efi_open_protocol,
3194 .close_protocol = efi_close_protocol,
3195 .open_protocol_information = efi_open_protocol_information,
3196 .protocols_per_handle = efi_protocols_per_handle,
3197 .locate_handle_buffer = efi_locate_handle_buffer,
3198 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003199 .install_multiple_protocol_interfaces =
3200 efi_install_multiple_protocol_interfaces,
3201 .uninstall_multiple_protocol_interfaces =
3202 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003203 .calculate_crc32 = efi_calculate_crc32,
3204 .copy_mem = efi_copy_mem,
3205 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003206 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003207};
3208
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003209static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003210
Alexander Graf3c63db92016-10-14 13:45:30 +02003211struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003212 .hdr = {
3213 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003214 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003215 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003216 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003217 .fw_vendor = firmware_vendor,
3218 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003219 .con_in = (void *)&efi_con_in,
3220 .con_out = (void *)&efi_con_out,
3221 .std_err = (void *)&efi_con_out,
3222 .runtime = (void *)&efi_runtime_services,
3223 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003224 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003225 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003226};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003227
3228/**
3229 * efi_initialize_system_table() - Initialize system table
3230 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003231 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003232 */
3233efi_status_t efi_initialize_system_table(void)
3234{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003235 efi_status_t ret;
3236
3237 /* Allocate configuration table array */
3238 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3239 EFI_MAX_CONFIGURATION_TABLES *
3240 sizeof(struct efi_configuration_table),
3241 (void **)&systab.tables);
3242
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003243 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003244 efi_update_table_header_crc32(&systab.hdr);
3245 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3246 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003247
3248 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003249}