blob: 0c92cc1807cba5ea09fdfecc9227f7948a50c339 [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/**
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200426 * efi_add_handle() - add a new handle to the object list
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100427 *
Heinrich Schuchardte6023be2019-05-01 09:42:39 +0200428 * @handle: handle to be added
429 *
430 * The protocols list is initialized. The handle is added to the list of known
431 * UEFI objects.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100432 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200433void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100434{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200435 if (!handle)
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100436 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200437 INIT_LIST_HEAD(&handle->protocols);
438 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100439}
440
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200441/**
Mario Six78a88f72018-07-10 08:40:17 +0200442 * efi_create_handle() - create handle
443 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200444 *
Mario Six78a88f72018-07-10 08:40:17 +0200445 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200446 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100447efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200448{
449 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200450
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200451 obj = calloc(1, sizeof(struct efi_object));
452 if (!obj)
453 return EFI_OUT_OF_RESOURCES;
454
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100455 efi_add_handle(obj);
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200456 *handle = obj;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200457
458 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200459}
460
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200461/**
Mario Six78a88f72018-07-10 08:40:17 +0200462 * efi_search_protocol() - find a protocol on a handle.
463 * @handle: handle
464 * @protocol_guid: GUID of the protocol
465 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100466 *
Mario Six78a88f72018-07-10 08:40:17 +0200467 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100468 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100469efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100470 const efi_guid_t *protocol_guid,
471 struct efi_handler **handler)
472{
473 struct efi_object *efiobj;
474 struct list_head *lhandle;
475
476 if (!handle || !protocol_guid)
477 return EFI_INVALID_PARAMETER;
478 efiobj = efi_search_obj(handle);
479 if (!efiobj)
480 return EFI_INVALID_PARAMETER;
481 list_for_each(lhandle, &efiobj->protocols) {
482 struct efi_handler *protocol;
483
484 protocol = list_entry(lhandle, struct efi_handler, link);
485 if (!guidcmp(protocol->guid, protocol_guid)) {
486 if (handler)
487 *handler = protocol;
488 return EFI_SUCCESS;
489 }
490 }
491 return EFI_NOT_FOUND;
492}
493
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200494/**
Mario Six78a88f72018-07-10 08:40:17 +0200495 * efi_remove_protocol() - delete protocol from a handle
496 * @handle: handle from which the protocol shall be deleted
497 * @protocol: GUID of the protocol to be deleted
498 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100499 *
Mario Six78a88f72018-07-10 08:40:17 +0200500 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100501 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100502efi_status_t efi_remove_protocol(const efi_handle_t handle,
503 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100504 void *protocol_interface)
505{
506 struct efi_handler *handler;
507 efi_status_t ret;
508
509 ret = efi_search_protocol(handle, protocol, &handler);
510 if (ret != EFI_SUCCESS)
511 return ret;
512 if (guidcmp(handler->guid, protocol))
513 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200514 if (handler->protocol_interface != protocol_interface)
515 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100516 list_del(&handler->link);
517 free(handler);
518 return EFI_SUCCESS;
519}
520
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200521/**
Mario Six78a88f72018-07-10 08:40:17 +0200522 * efi_remove_all_protocols() - delete all protocols from a handle
523 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100524 *
Mario Six78a88f72018-07-10 08:40:17 +0200525 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100526 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100527efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100528{
529 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100530 struct efi_handler *protocol;
531 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100532
533 efiobj = efi_search_obj(handle);
534 if (!efiobj)
535 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100536 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100537 efi_status_t ret;
538
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100539 ret = efi_remove_protocol(handle, protocol->guid,
540 protocol->protocol_interface);
541 if (ret != EFI_SUCCESS)
542 return ret;
543 }
544 return EFI_SUCCESS;
545}
546
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200547/**
Mario Six78a88f72018-07-10 08:40:17 +0200548 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100549 *
Mario Six78a88f72018-07-10 08:40:17 +0200550 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100551 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200552void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100553{
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200554 if (!handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100555 return;
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200556 efi_remove_all_protocols(handle);
557 list_del(&handle->link);
558 free(handle);
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100559}
560
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200561/**
Mario Six78a88f72018-07-10 08:40:17 +0200562 * efi_is_event() - check if a pointer is a valid event
563 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100564 *
Mario Six78a88f72018-07-10 08:40:17 +0200565 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100566 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100567static efi_status_t efi_is_event(const struct efi_event *event)
568{
569 const struct efi_event *evt;
570
571 if (!event)
572 return EFI_INVALID_PARAMETER;
573 list_for_each_entry(evt, &efi_events, link) {
574 if (evt == event)
575 return EFI_SUCCESS;
576 }
577 return EFI_INVALID_PARAMETER;
578}
Alexander Grafbee91162016-03-04 01:09:59 +0100579
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200580/**
Mario Six78a88f72018-07-10 08:40:17 +0200581 * efi_create_event() - create an event
582 * @type: type of the event to create
583 * @notify_tpl: task priority level of the event
584 * @notify_function: notification function of the event
585 * @notify_context: pointer passed to the notification function
586 * @group: event group
587 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200588 *
589 * This function is used inside U-Boot code to create an event.
590 *
591 * For the API function implementing the CreateEvent service see
592 * efi_create_event_ext.
593 *
Mario Six78a88f72018-07-10 08:40:17 +0200594 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200595 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100596efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200597 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200598 struct efi_event *event,
599 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100600 void *notify_context, efi_guid_t *group,
601 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100602{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100603 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200604
Jonathan Graya95343b2017-03-12 19:26:07 +1100605 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200606 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100607
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200608 switch (type) {
609 case 0:
610 case EVT_TIMER:
611 case EVT_NOTIFY_SIGNAL:
612 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
613 case EVT_NOTIFY_WAIT:
614 case EVT_TIMER | EVT_NOTIFY_WAIT:
615 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
616 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
617 break;
618 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200619 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200620 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100621
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900622 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt751e9282019-04-25 07:30:41 +0200623 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200624 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100625
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100626 evt = calloc(1, sizeof(struct efi_event));
627 if (!evt)
628 return EFI_OUT_OF_RESOURCES;
629 evt->type = type;
630 evt->notify_tpl = notify_tpl;
631 evt->notify_function = notify_function;
632 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100633 evt->group = group;
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200634 /* Disable timers on boot up */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100635 evt->trigger_next = -1ULL;
636 evt->is_queued = false;
637 evt->is_signaled = false;
638 list_add_tail(&evt->link, &efi_events);
639 *event = evt;
640 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100641}
642
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200643/*
Mario Six78a88f72018-07-10 08:40:17 +0200644 * efi_create_event_ex() - create an event in a group
645 * @type: type of the event to create
646 * @notify_tpl: task priority level of the event
647 * @notify_function: notification function of the event
648 * @notify_context: pointer passed to the notification function
649 * @event: created event
650 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100651 *
652 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100653 *
Mario Six78a88f72018-07-10 08:40:17 +0200654 * See the Unified Extensible Firmware Interface (UEFI) specification for
655 * details.
656 *
657 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100658 */
659efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
660 void (EFIAPI *notify_function) (
661 struct efi_event *event,
662 void *context),
663 void *notify_context,
664 efi_guid_t *event_group,
665 struct efi_event **event)
666{
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200667 efi_status_t ret;
668
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100669 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
670 notify_context, event_group);
Heinrich Schuchardt18845122019-05-04 10:12:50 +0200671
672 /*
673 * The allowable input parameters are the same as in CreateEvent()
674 * except for the following two disallowed event types.
675 */
676 switch (type) {
677 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
678 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
679 ret = EFI_INVALID_PARAMETER;
680 goto out;
681 }
682
683 ret = efi_create_event(type, notify_tpl, notify_function,
684 notify_context, event_group, event);
685out:
686 return EFI_EXIT(ret);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100687}
688
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200689/**
Mario Six78a88f72018-07-10 08:40:17 +0200690 * efi_create_event_ext() - create an event
691 * @type: type of the event to create
692 * @notify_tpl: task priority level of the event
693 * @notify_function: notification function of the event
694 * @notify_context: pointer passed to the notification function
695 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200696 *
697 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200698 *
Mario Six78a88f72018-07-10 08:40:17 +0200699 * See the Unified Extensible Firmware Interface (UEFI) specification for
700 * details.
701 *
702 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200703 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200704static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100705 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200706 void (EFIAPI *notify_function) (
707 struct efi_event *event,
708 void *context),
709 void *notify_context, struct efi_event **event)
710{
711 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
712 notify_context);
713 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100714 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200715}
716
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200717/**
Mario Six78a88f72018-07-10 08:40:17 +0200718 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200719 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200720 * Check if a timer event has occurred or a queued notification function should
721 * be called.
722 *
Alexander Grafbee91162016-03-04 01:09:59 +0100723 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200724 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100725 */
726void efi_timer_check(void)
727{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100728 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100729 u64 now = timer_get_us();
730
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100731 list_for_each_entry(evt, &efi_events, link) {
732 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100733 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100734 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200735 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100736 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200737 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100738 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200739 break;
740 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100741 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200742 break;
743 default:
744 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200745 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100746 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100747 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100748 }
Alexander Grafbee91162016-03-04 01:09:59 +0100749 WATCHDOG_RESET();
750}
751
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200752/**
Mario Six78a88f72018-07-10 08:40:17 +0200753 * efi_set_timer() - set the trigger time for a timer event or stop the event
754 * @event: event for which the timer is set
755 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200756 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200757 *
758 * This is the function for internal usage in U-Boot. For the API function
759 * implementing the SetTimer service see efi_set_timer_ext.
760 *
Mario Six78a88f72018-07-10 08:40:17 +0200761 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200762 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200763efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200764 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100765{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100766 /* Check that the event is valid */
767 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
768 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100769
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200770 /*
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200771 * The parameter defines a multiple of 100 ns.
772 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200773 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200774 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100775
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100776 switch (type) {
777 case EFI_TIMER_STOP:
778 event->trigger_next = -1ULL;
779 break;
780 case EFI_TIMER_PERIODIC:
781 case EFI_TIMER_RELATIVE:
782 event->trigger_next = timer_get_us() + trigger_time;
783 break;
784 default:
785 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100786 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100787 event->trigger_type = type;
788 event->trigger_time = trigger_time;
789 event->is_signaled = false;
790 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200791}
792
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200793/**
Mario Six78a88f72018-07-10 08:40:17 +0200794 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
795 * event
796 * @event: event for which the timer is set
797 * @type: type of the timer
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +0200798 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200799 *
800 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200801 *
Mario Six78a88f72018-07-10 08:40:17 +0200802 * See the Unified Extensible Firmware Interface (UEFI) specification for
803 * details.
804 *
805 *
806 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200807 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200808static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
809 enum efi_timer_delay type,
810 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200811{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900812 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200813 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100814}
815
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200816/**
Mario Six78a88f72018-07-10 08:40:17 +0200817 * efi_wait_for_event() - wait for events to be signaled
818 * @num_events: number of events to be waited for
819 * @event: events to be waited for
820 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200821 *
822 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200823 *
Mario Six78a88f72018-07-10 08:40:17 +0200824 * See the Unified Extensible Firmware Interface (UEFI) specification for
825 * details.
826 *
827 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200828 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100829static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200830 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100831 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100832{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100833 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100834
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100835 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100836
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200837 /* Check parameters */
838 if (!num_events || !event)
839 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200840 /* Check TPL */
841 if (efi_tpl != TPL_APPLICATION)
842 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200843 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100844 if (efi_is_event(event[i]) != EFI_SUCCESS)
845 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200846 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
847 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200848 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100849 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200850 }
851
852 /* Wait for signal */
853 for (;;) {
854 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200855 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200856 goto out;
857 }
858 /* Allow events to occur. */
859 efi_timer_check();
860 }
861
862out:
863 /*
864 * Reset the signal which is passed to the caller to allow periodic
865 * events to occur.
866 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200867 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200868 if (index)
869 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100870
871 return EFI_EXIT(EFI_SUCCESS);
872}
873
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200874/**
Mario Six78a88f72018-07-10 08:40:17 +0200875 * efi_signal_event_ext() - signal an EFI event
876 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200877 *
878 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200879 *
880 * See the Unified Extensible Firmware Interface (UEFI) specification for
881 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200882 *
883 * This functions sets the signaled state of the event and queues the
884 * notification function for execution.
885 *
Mario Six78a88f72018-07-10 08:40:17 +0200886 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200887 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200888static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100889{
890 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100891 if (efi_is_event(event) != EFI_SUCCESS)
892 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100893 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100894 return EFI_EXIT(EFI_SUCCESS);
895}
896
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200897/**
Mario Six78a88f72018-07-10 08:40:17 +0200898 * efi_close_event() - close an EFI event
899 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200900 *
901 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200902 *
Mario Six78a88f72018-07-10 08:40:17 +0200903 * See the Unified Extensible Firmware Interface (UEFI) specification for
904 * details.
905 *
906 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200907 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200908static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100909{
910 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100911 if (efi_is_event(event) != EFI_SUCCESS)
912 return EFI_EXIT(EFI_INVALID_PARAMETER);
913 list_del(&event->link);
914 free(event);
915 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100916}
917
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200918/**
Mario Six78a88f72018-07-10 08:40:17 +0200919 * efi_check_event() - check if an event is signaled
920 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200921 *
922 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200923 *
Mario Six78a88f72018-07-10 08:40:17 +0200924 * See the Unified Extensible Firmware Interface (UEFI) specification for
925 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200926 *
Mario Six78a88f72018-07-10 08:40:17 +0200927 * If an event is not signaled yet, the notification function is queued. The
928 * signaled state is cleared.
929 *
930 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200931 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200932static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100933{
934 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200935 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100936 if (efi_is_event(event) != EFI_SUCCESS ||
937 event->type & EVT_NOTIFY_SIGNAL)
938 return EFI_EXIT(EFI_INVALID_PARAMETER);
939 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100940 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100941 if (event->is_signaled) {
942 event->is_signaled = false;
943 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200944 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100945 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100946}
947
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200948/**
Mario Six78a88f72018-07-10 08:40:17 +0200949 * efi_search_obj() - find the internal EFI object for a handle
950 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200951 *
Mario Six78a88f72018-07-10 08:40:17 +0200952 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200953 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100954struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200955{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100956 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200957
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100958 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtfae01182018-09-26 05:27:55 +0200959 if (efiobj == handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200960 return efiobj;
961 }
962
963 return NULL;
964}
965
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200966/**
Mario Six78a88f72018-07-10 08:40:17 +0200967 * efi_open_protocol_info_entry() - create open protocol info entry and add it
968 * to a protocol
969 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100970 *
Mario Six78a88f72018-07-10 08:40:17 +0200971 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100972 */
973static struct efi_open_protocol_info_entry *efi_create_open_info(
974 struct efi_handler *handler)
975{
976 struct efi_open_protocol_info_item *item;
977
978 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
979 if (!item)
980 return NULL;
981 /* Append the item to the open protocol info list. */
982 list_add_tail(&item->link, &handler->open_infos);
983
984 return &item->info;
985}
986
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200987/**
Mario Six78a88f72018-07-10 08:40:17 +0200988 * efi_delete_open_info() - remove an open protocol info entry from a protocol
989 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100990 *
Mario Six78a88f72018-07-10 08:40:17 +0200991 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100992 */
993static efi_status_t efi_delete_open_info(
994 struct efi_open_protocol_info_item *item)
995{
996 list_del(&item->link);
997 free(item);
998 return EFI_SUCCESS;
999}
1000
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001001/**
Mario Six78a88f72018-07-10 08:40:17 +02001002 * efi_add_protocol() - install new protocol on a handle
1003 * @handle: handle on which the protocol shall be installed
1004 * @protocol: GUID of the protocol to be installed
1005 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001006 *
Mario Six78a88f72018-07-10 08:40:17 +02001007 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001008 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001009efi_status_t efi_add_protocol(const efi_handle_t handle,
1010 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001011 void *protocol_interface)
1012{
1013 struct efi_object *efiobj;
1014 struct efi_handler *handler;
1015 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001016
1017 efiobj = efi_search_obj(handle);
1018 if (!efiobj)
1019 return EFI_INVALID_PARAMETER;
1020 ret = efi_search_protocol(handle, protocol, NULL);
1021 if (ret != EFI_NOT_FOUND)
1022 return EFI_INVALID_PARAMETER;
1023 handler = calloc(1, sizeof(struct efi_handler));
1024 if (!handler)
1025 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001026 handler->guid = protocol;
1027 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001028 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001029 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001030 if (!guidcmp(&efi_guid_device_path, protocol))
1031 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001032 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001033}
1034
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001035/**
Mario Six78a88f72018-07-10 08:40:17 +02001036 * efi_install_protocol_interface() - install protocol interface
1037 * @handle: handle on which the protocol shall be installed
1038 * @protocol: GUID of the protocol to be installed
1039 * @protocol_interface_type: type of the interface to be installed,
1040 * always EFI_NATIVE_INTERFACE
1041 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001042 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001043 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001044 *
Mario Six78a88f72018-07-10 08:40:17 +02001045 * See the Unified Extensible Firmware Interface (UEFI) specification for
1046 * details.
1047 *
1048 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001049 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001050static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02001051 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001052 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001053{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001054 efi_status_t r;
1055
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001056 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1057 protocol_interface);
1058
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001059 if (!handle || !protocol ||
1060 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1061 r = EFI_INVALID_PARAMETER;
1062 goto out;
1063 }
1064
1065 /* Create new handle if requested. */
1066 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001067 r = efi_create_handle(handle);
1068 if (r != EFI_SUCCESS)
1069 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001070 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1071 *handle);
1072 } else {
1073 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1074 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001075 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001076 /* Add new protocol */
1077 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001078out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001079 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001080}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001081
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001082/**
Mario Six78a88f72018-07-10 08:40:17 +02001083 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001084 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001085 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001086 * @number_of_drivers: number of child controllers
1087 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001088 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001089 * The allocated buffer has to be freed with free().
1090 *
Mario Six78a88f72018-07-10 08:40:17 +02001091 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001092 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001093static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001094 const efi_guid_t *protocol,
1095 efi_uintn_t *number_of_drivers,
1096 efi_handle_t **driver_handle_buffer)
1097{
1098 struct efi_handler *handler;
1099 struct efi_open_protocol_info_item *item;
1100 efi_uintn_t count = 0, i;
1101 bool duplicate;
1102
1103 /* Count all driver associations */
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 ++count;
1111 }
1112 }
1113 /*
1114 * Create buffer. In case of duplicate driver assignments the buffer
1115 * will be too large. But that does not harm.
1116 */
1117 *number_of_drivers = 0;
1118 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1119 if (!*driver_handle_buffer)
1120 return EFI_OUT_OF_RESOURCES;
1121 /* Collect unique driver handles */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001122 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001123 if (protocol && guidcmp(handler->guid, protocol))
1124 continue;
1125 list_for_each_entry(item, &handler->open_infos, link) {
1126 if (item->info.attributes &
1127 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1128 /* Check this is a new driver */
1129 duplicate = false;
1130 for (i = 0; i < *number_of_drivers; ++i) {
1131 if ((*driver_handle_buffer)[i] ==
1132 item->info.agent_handle)
1133 duplicate = true;
1134 }
1135 /* Copy handle to buffer */
1136 if (!duplicate) {
1137 i = (*number_of_drivers)++;
1138 (*driver_handle_buffer)[i] =
1139 item->info.agent_handle;
1140 }
1141 }
1142 }
1143 }
1144 return EFI_SUCCESS;
1145}
1146
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001147/**
Mario Six78a88f72018-07-10 08:40:17 +02001148 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001149 * @handle: handle of the controller
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001150 * @protocol: protocol GUID (optional)
Mario Six78a88f72018-07-10 08:40:17 +02001151 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001152 *
1153 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001154 *
Mario Six78a88f72018-07-10 08:40:17 +02001155 * See the Unified Extensible Firmware Interface (UEFI) specification for
1156 * details.
1157 *
1158 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001159 */
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001160static efi_status_t efi_disconnect_all_drivers
1161 (efi_handle_t handle,
1162 const efi_guid_t *protocol,
1163 efi_handle_t child_handle)
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001164{
1165 efi_uintn_t number_of_drivers;
1166 efi_handle_t *driver_handle_buffer;
1167 efi_status_t r, ret;
1168
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001169 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001170 &driver_handle_buffer);
1171 if (ret != EFI_SUCCESS)
1172 return ret;
1173
1174 ret = EFI_NOT_FOUND;
1175 while (number_of_drivers) {
1176 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001177 handle,
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001178 driver_handle_buffer[--number_of_drivers],
1179 child_handle));
1180 if (r == EFI_SUCCESS)
1181 ret = r;
1182 }
1183 free(driver_handle_buffer);
1184 return ret;
1185}
1186
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001187/**
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001188 * efi_uninstall_protocol() - uninstall protocol interface
1189 *
Mario Six78a88f72018-07-10 08:40:17 +02001190 * @handle: handle from which the protocol shall be removed
1191 * @protocol: GUID of the protocol to be removed
1192 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001193 *
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001194 * This function DOES NOT delete a handle without installed protocol.
Mario Six78a88f72018-07-10 08:40:17 +02001195 *
1196 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001197 */
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001198static efi_status_t efi_uninstall_protocol
1199 (efi_handle_t handle, const efi_guid_t *protocol,
1200 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001201{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001202 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001203 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001204 struct efi_open_protocol_info_item *item;
1205 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001206 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001207
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001208 /* Check handle */
1209 efiobj = efi_search_obj(handle);
1210 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001211 r = EFI_INVALID_PARAMETER;
1212 goto out;
1213 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001214 /* Find the protocol on the handle */
1215 r = efi_search_protocol(handle, protocol, &handler);
1216 if (r != EFI_SUCCESS)
1217 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001218 /* Disconnect controllers */
1219 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1220 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001221 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001222 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001223 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001224 /* Close protocol */
1225 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1226 if (item->info.attributes ==
1227 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1228 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1229 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1230 list_del(&item->link);
1231 }
1232 if (!list_empty(&handler->open_infos)) {
1233 r = EFI_ACCESS_DENIED;
1234 goto out;
1235 }
1236 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001237out:
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02001238 return r;
1239}
1240
1241/**
1242 * efi_uninstall_protocol_interface() - uninstall protocol interface
1243 * @handle: handle from which the protocol shall be removed
1244 * @protocol: GUID of the protocol to be removed
1245 * @protocol_interface: interface to be removed
1246 *
1247 * This function implements the UninstallProtocolInterface service.
1248 *
1249 * See the Unified Extensible Firmware Interface (UEFI) specification for
1250 * details.
1251 *
1252 * Return: status code
1253 */
1254static efi_status_t EFIAPI efi_uninstall_protocol_interface
1255 (efi_handle_t handle, const efi_guid_t *protocol,
1256 void *protocol_interface)
1257{
1258 efi_status_t ret;
1259
1260 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1261
1262 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1263 if (ret != EFI_SUCCESS)
1264 goto out;
1265
1266 /* If the last protocol has been removed, delete the handle. */
1267 if (list_empty(&handle->protocols)) {
1268 list_del(&handle->link);
1269 free(handle);
1270 }
1271out:
1272 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001273}
1274
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001275/**
Mario Six78a88f72018-07-10 08:40:17 +02001276 * efi_register_protocol_notify() - register an event for notification when a
1277 * protocol is installed.
1278 * @protocol: GUID of the protocol whose installation shall be notified
1279 * @event: event to be signaled upon installation of the protocol
1280 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001281 *
1282 * This function implements the RegisterProtocolNotify service.
1283 * See the Unified Extensible Firmware Interface (UEFI) specification
1284 * for details.
1285 *
Mario Six78a88f72018-07-10 08:40:17 +02001286 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001287 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001288static efi_status_t EFIAPI efi_register_protocol_notify(
1289 const efi_guid_t *protocol,
1290 struct efi_event *event,
1291 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001292{
Rob Clark778e6af2017-09-13 18:05:41 -04001293 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001294 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1295}
1296
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001297/**
Mario Six78a88f72018-07-10 08:40:17 +02001298 * efi_search() - determine if an EFI handle implements a protocol
1299 * @search_type: selection criterion
1300 * @protocol: GUID of the protocol
1301 * @search_key: registration key
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001302 * @handle: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001303 *
1304 * See the documentation of the LocateHandle service in the UEFI specification.
1305 *
Mario Six78a88f72018-07-10 08:40:17 +02001306 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001307 */
Alexander Grafbee91162016-03-04 01:09:59 +01001308static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001309 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001310 efi_handle_t handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001311{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001312 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001313
1314 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001315 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001316 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001317 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001318 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001319 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001320 case BY_PROTOCOL:
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001321 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001322 return (ret != EFI_SUCCESS);
1323 default:
1324 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001325 return -1;
1326 }
Alexander Grafbee91162016-03-04 01:09:59 +01001327}
1328
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001329/**
Mario Six78a88f72018-07-10 08:40:17 +02001330 * efi_locate_handle() - locate handles implementing a protocol
1331 * @search_type: selection criterion
1332 * @protocol: GUID of the protocol
1333 * @search_key: registration key
1334 * @buffer_size: size of the buffer to receive the handles in bytes
1335 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001336 *
1337 * This function is meant for U-Boot internal calls. For the API implementation
1338 * of the LocateHandle service see efi_locate_handle_ext.
1339 *
Mario Six78a88f72018-07-10 08:40:17 +02001340 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001341 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001342static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001343 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001344 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001345 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001346{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001347 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001348 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001349
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001350 /* Check parameters */
1351 switch (search_type) {
1352 case ALL_HANDLES:
1353 break;
1354 case BY_REGISTER_NOTIFY:
1355 if (!search_key)
1356 return EFI_INVALID_PARAMETER;
1357 /* RegisterProtocolNotify is not implemented yet */
1358 return EFI_UNSUPPORTED;
1359 case BY_PROTOCOL:
1360 if (!protocol)
1361 return EFI_INVALID_PARAMETER;
1362 break;
1363 default:
1364 return EFI_INVALID_PARAMETER;
1365 }
1366
Alexander Grafbee91162016-03-04 01:09:59 +01001367 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001368 list_for_each_entry(efiobj, &efi_obj_list, link) {
1369 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001370 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001371 }
1372
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001373 if (size == 0)
1374 return EFI_NOT_FOUND;
1375
1376 if (!buffer_size)
1377 return EFI_INVALID_PARAMETER;
1378
Alexander Grafbee91162016-03-04 01:09:59 +01001379 if (*buffer_size < size) {
1380 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001381 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001382 }
1383
Rob Clark796a78c2017-08-06 14:10:07 -04001384 *buffer_size = size;
Heinrich Schuchardt8dfb5e62019-05-04 17:37:32 +02001385
1386 /* The buffer size is sufficient but there is not buffer */
1387 if (!buffer)
1388 return EFI_INVALID_PARAMETER;
Rob Clark796a78c2017-08-06 14:10:07 -04001389
Alexander Grafbee91162016-03-04 01:09:59 +01001390 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001391 list_for_each_entry(efiobj, &efi_obj_list, link) {
1392 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02001393 *buffer++ = efiobj;
Alexander Grafbee91162016-03-04 01:09:59 +01001394 }
1395
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001396 return EFI_SUCCESS;
1397}
1398
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001399/**
Mario Six78a88f72018-07-10 08:40:17 +02001400 * efi_locate_handle_ext() - locate handles implementing a protocol.
1401 * @search_type: selection criterion
1402 * @protocol: GUID of the protocol
1403 * @search_key: registration key
1404 * @buffer_size: size of the buffer to receive the handles in bytes
1405 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001406 *
1407 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001408 *
Mario Six78a88f72018-07-10 08:40:17 +02001409 * See the Unified Extensible Firmware Interface (UEFI) specification for
1410 * details.
1411 *
1412 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001413 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001414static efi_status_t EFIAPI efi_locate_handle_ext(
1415 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001416 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001417 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001418{
Rob Clark778e6af2017-09-13 18:05:41 -04001419 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001420 buffer_size, buffer);
1421
1422 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1423 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001424}
1425
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001426/**
Mario Six78a88f72018-07-10 08:40:17 +02001427 * efi_remove_configuration_table() - collapses configuration table entries,
1428 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001429 *
Mario Six78a88f72018-07-10 08:40:17 +02001430 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001431 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001432static void efi_remove_configuration_table(int i)
1433{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001434 struct efi_configuration_table *this = &systab.tables[i];
1435 struct efi_configuration_table *next = &systab.tables[i + 1];
1436 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001437
1438 memmove(this, next, (ulong)end - (ulong)next);
1439 systab.nr_tables--;
1440}
1441
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001442/**
Mario Six78a88f72018-07-10 08:40:17 +02001443 * efi_install_configuration_table() - adds, updates, or removes a
1444 * configuration table
1445 * @guid: GUID of the installed table
1446 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001447 *
1448 * This function is used for internal calls. For the API implementation of the
1449 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1450 *
Mario Six78a88f72018-07-10 08:40:17 +02001451 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001452 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001453efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1454 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001455{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001456 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001457 int i;
1458
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001459 if (!guid)
1460 return EFI_INVALID_PARAMETER;
1461
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001462 /* Check for GUID override */
Alexander Grafbee91162016-03-04 01:09:59 +01001463 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001464 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001465 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001466 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001467 else
1468 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001469 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001470 }
1471 }
1472
Alexander Grafd98cdf62017-07-26 13:41:04 +02001473 if (!table)
1474 return EFI_NOT_FOUND;
1475
Alexander Grafbee91162016-03-04 01:09:59 +01001476 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001477 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001478 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001479
1480 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001481 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1482 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001483 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001484
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001485out:
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001486 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001487 efi_update_table_header_crc32(&systab.hdr);
1488
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001489 /* Notify that the configuration table was changed */
1490 list_for_each_entry(evt, &efi_events, link) {
1491 if (evt->group && !guidcmp(evt->group, guid)) {
1492 efi_signal_event(evt, false);
1493 break;
1494 }
1495 }
1496
Alexander Graf488bf122016-08-19 01:23:24 +02001497 return EFI_SUCCESS;
1498}
1499
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001500/**
Mario Six78a88f72018-07-10 08:40:17 +02001501 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1502 * configuration table.
1503 * @guid: GUID of the installed table
1504 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001505 *
1506 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001507 *
Mario Six78a88f72018-07-10 08:40:17 +02001508 * See the Unified Extensible Firmware Interface (UEFI) specification for
1509 * details.
1510 *
1511 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001512 */
Alexander Graf488bf122016-08-19 01:23:24 +02001513static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1514 void *table)
1515{
Rob Clark778e6af2017-09-13 18:05:41 -04001516 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001517 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001518}
1519
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001520/**
Mario Six78a88f72018-07-10 08:40:17 +02001521 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001522 *
1523 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001524 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001525 *
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001526 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1527 * code is returned.
1528 *
1529 * @device_path: device path of the loaded image
1530 * @file_path: file path of the loaded image
1531 * @handle_ptr: handle of the loaded image
1532 * @info_ptr: loaded image protocol
1533 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001534 */
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001535efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1536 struct efi_device_path *file_path,
1537 struct efi_loaded_image_obj **handle_ptr,
1538 struct efi_loaded_image **info_ptr)
Rob Clark95c55532017-09-13 18:05:33 -04001539{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001540 efi_status_t ret;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001541 struct efi_loaded_image *info = NULL;
1542 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001543 struct efi_device_path *dp;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001544
1545 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1546 *handle_ptr = NULL;
1547 *info_ptr = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001548
1549 info = calloc(1, sizeof(*info));
1550 if (!info)
1551 return EFI_OUT_OF_RESOURCES;
1552 obj = calloc(1, sizeof(*obj));
1553 if (!obj) {
1554 free(info);
1555 return EFI_OUT_OF_RESOURCES;
1556 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001557
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001558 /* Add internal object to object list */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001559 efi_add_handle(&obj->header);
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001560
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001561 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001562 info->file_path = file_path;
Heinrich Schuchardt7e1effc2018-08-26 15:31:52 +02001563 info->system_table = &systab;
Rob Clark95c55532017-09-13 18:05:33 -04001564
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001565 if (device_path) {
1566 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001567
1568 dp = efi_dp_append(device_path, file_path);
1569 if (!dp) {
1570 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001571 goto failure;
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001572 }
1573 } else {
1574 dp = NULL;
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001575 }
AKASHI Takahirobc8fc322019-03-27 13:40:32 +09001576 ret = efi_add_protocol(&obj->header,
1577 &efi_guid_loaded_image_device_path, dp);
1578 if (ret != EFI_SUCCESS)
1579 goto failure;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001580
1581 /*
1582 * When asking for the loaded_image interface, just
1583 * return handle which points to loaded_image_info
1584 */
Heinrich Schuchardtd39646a2018-09-26 05:27:56 +02001585 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001586 &efi_guid_loaded_image, info);
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001587 if (ret != EFI_SUCCESS)
1588 goto failure;
1589
Heinrich Schuchardtd5974af2019-03-19 18:58:58 +01001590 *info_ptr = info;
1591 *handle_ptr = obj;
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001592
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001593 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001594failure:
1595 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt16112f92019-02-06 19:41:29 +01001596 efi_delete_handle(&obj->header);
1597 free(info);
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001598 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001599}
1600
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001601/**
Mario Six78a88f72018-07-10 08:40:17 +02001602 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001603 *
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001604 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1605 * callers obligation to update the memory type as needed.
1606 *
1607 * @file_path: the path of the image to load
1608 * @buffer: buffer containing the loaded image
1609 * @size: size of the loaded image
1610 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001611 */
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09001612static
Rob Clark9975fe92017-09-13 18:05:38 -04001613efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001614 void **buffer, efi_uintn_t *size)
Rob Clark838ee4b2017-09-13 18:05:35 -04001615{
1616 struct efi_file_info *info = NULL;
1617 struct efi_file_handle *f;
1618 static efi_status_t ret;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001619 u64 addr;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001620 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001621
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001622 /* In case of failure nothing is returned */
1623 *buffer = NULL;
1624 *size = 0;
1625
1626 /* Open file */
Rob Clark838ee4b2017-09-13 18:05:35 -04001627 f = efi_file_from_path(file_path);
1628 if (!f)
1629 return EFI_DEVICE_ERROR;
1630
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001631 /* Get file size */
Rob Clark838ee4b2017-09-13 18:05:35 -04001632 bs = 0;
1633 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1634 &bs, info));
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001635 if (ret != EFI_BUFFER_TOO_SMALL) {
1636 ret = EFI_DEVICE_ERROR;
1637 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001638 }
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001639
1640 info = malloc(bs);
1641 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1642 info));
Rob Clark838ee4b2017-09-13 18:05:35 -04001643 if (ret != EFI_SUCCESS)
1644 goto error;
1645
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001646 /*
1647 * When reading the file we do not yet know if it contains an
1648 * application, a boottime driver, or a runtime driver. So here we
1649 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1650 * update the reservation according to the image type.
1651 */
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001652 bs = info->file_size;
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001653 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1654 EFI_BOOT_SERVICES_DATA,
1655 efi_size_in_pages(bs), &addr);
Rob Clark838ee4b2017-09-13 18:05:35 -04001656 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001657 ret = EFI_OUT_OF_RESOURCES;
1658 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001659 }
1660
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001661 /* Read file */
1662 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1663 if (ret != EFI_SUCCESS)
1664 efi_free_pages(addr, efi_size_in_pages(bs));
1665 *buffer = (void *)(uintptr_t)addr;
1666 *size = bs;
1667error:
1668 EFI_CALL(f->close(f));
1669 free(info);
Rob Clark838ee4b2017-09-13 18:05:35 -04001670 return ret;
1671}
1672
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001673/**
Mario Six78a88f72018-07-10 08:40:17 +02001674 * efi_load_image() - load an EFI image into memory
1675 * @boot_policy: true for request originating from the boot manager
1676 * @parent_image: the caller's image handle
1677 * @file_path: the path of the image to load
1678 * @source_buffer: memory location from which the image is installed
1679 * @source_size: size of the memory area from which the image is installed
1680 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001681 *
1682 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001683 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001684 * See the Unified Extensible Firmware Interface (UEFI) specification
1685 * for details.
1686 *
Mario Six78a88f72018-07-10 08:40:17 +02001687 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001688 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09001689efi_status_t EFIAPI efi_load_image(bool boot_policy,
1690 efi_handle_t parent_image,
1691 struct efi_device_path *file_path,
1692 void *source_buffer,
1693 efi_uintn_t source_size,
1694 efi_handle_t *image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001695{
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001696 struct efi_device_path *dp, *fp;
Tom Rini1c3b2f42018-09-30 10:38:15 -04001697 struct efi_loaded_image *info = NULL;
Heinrich Schuchardtc9828742018-09-23 17:21:51 +02001698 struct efi_loaded_image_obj **image_obj =
1699 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001700 efi_status_t ret;
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001701 void *dest_buffer;
Alexander Grafbee91162016-03-04 01:09:59 +01001702
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001703 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001704 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001705
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001706 if (!image_handle || !parent_image) {
1707 ret = EFI_INVALID_PARAMETER;
1708 goto error;
1709 }
1710
1711 if (!source_buffer && !file_path) {
1712 ret = EFI_NOT_FOUND;
1713 goto error;
1714 }
1715
Rob Clark838ee4b2017-09-13 18:05:35 -04001716 if (!source_buffer) {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001717 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001718 &source_size);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001719 if (ret != EFI_SUCCESS)
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001720 goto error;
Rob Clark838ee4b2017-09-13 18:05:35 -04001721 } else {
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001722 dest_buffer = source_buffer;
Rob Clark838ee4b2017-09-13 18:05:35 -04001723 }
Heinrich Schuchardt1e15a9c2019-04-20 19:24:43 +00001724 /* split file_path which contains both the device and file parts */
1725 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt0e18f582018-12-24 09:19:07 +01001726 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtb0c3c342019-03-04 17:50:05 +01001727 if (ret == EFI_SUCCESS)
1728 ret = efi_load_pe(*image_obj, dest_buffer, info);
1729 if (!source_buffer)
1730 /* Release buffer to which file was loaded */
1731 efi_free_pages((uintptr_t)dest_buffer,
1732 efi_size_in_pages(source_size));
1733 if (ret == EFI_SUCCESS) {
1734 info->system_table = &systab;
1735 info->parent_handle = parent_image;
1736 } else {
1737 /* The image is invalid. Release all associated resources. */
1738 efi_delete_handle(*image_handle);
1739 *image_handle = NULL;
1740 free(info);
1741 }
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001742error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001743 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001744}
1745
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001746/**
Mario Six78a88f72018-07-10 08:40:17 +02001747 * efi_unload_image() - unload an EFI image
1748 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001749 *
1750 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001751 *
Mario Six78a88f72018-07-10 08:40:17 +02001752 * See the Unified Extensible Firmware Interface (UEFI) specification for
1753 * details.
1754 *
1755 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001756 */
AKASHI Takahirod7e0b012019-03-05 14:53:31 +09001757efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001758{
1759 struct efi_object *efiobj;
1760
1761 EFI_ENTRY("%p", image_handle);
1762 efiobj = efi_search_obj(image_handle);
1763 if (efiobj)
1764 list_del(&efiobj->link);
1765
1766 return EFI_EXIT(EFI_SUCCESS);
1767}
1768
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001769/**
Alexander Graff31239a2018-11-15 21:23:47 +01001770 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1771 */
1772static void efi_exit_caches(void)
1773{
1774#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1775 /*
1776 * Grub on 32bit ARM needs to have caches disabled before jumping into
1777 * a zImage, but does not know of all cache layers. Give it a hand.
1778 */
1779 if (efi_is_direct_boot)
1780 cleanup_before_linux();
1781#endif
1782}
1783
1784/**
Mario Six78a88f72018-07-10 08:40:17 +02001785 * efi_exit_boot_services() - stop all boot services
1786 * @image_handle: handle of the loaded image
1787 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001788 *
1789 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001790 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001791 * See the Unified Extensible Firmware Interface (UEFI) specification
1792 * for details.
1793 *
Mario Six78a88f72018-07-10 08:40:17 +02001794 * All timer events are disabled. For exit boot services events the
1795 * notification function is called. The boot services are disabled in the
1796 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001797 *
Mario Six78a88f72018-07-10 08:40:17 +02001798 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001799 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001800static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001801 unsigned long map_key)
1802{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001803 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001804
Alexander Grafbee91162016-03-04 01:09:59 +01001805 EFI_ENTRY("%p, %ld", image_handle, map_key);
1806
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001807 /* Check that the caller has read the current memory map */
1808 if (map_key != efi_memory_map_key)
1809 return EFI_INVALID_PARAMETER;
1810
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001811 /* Make sure that notification functions are not called anymore */
1812 efi_tpl = TPL_HIGH_LEVEL;
1813
1814 /* Check if ExitBootServices has already been called */
1815 if (!systab.boottime)
1816 return EFI_EXIT(EFI_SUCCESS);
1817
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001818 /* Add related events to the event group */
1819 list_for_each_entry(evt, &efi_events, link) {
1820 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1821 evt->group = &efi_guid_event_group_exit_boot_services;
1822 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001823 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001824 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001825 if (evt->group &&
1826 !guidcmp(evt->group,
1827 &efi_guid_event_group_exit_boot_services)) {
1828 efi_signal_event(evt, false);
1829 break;
1830 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001831 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001832
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001833 /* TODO: Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001834
Alexander Grafb7b84102016-11-17 01:02:57 +01001835 board_quiesce_devices();
1836
Alexander Graff31239a2018-11-15 21:23:47 +01001837 /* Fix up caches for EFI payloads if necessary */
1838 efi_exit_caches();
1839
Alexander Grafbee91162016-03-04 01:09:59 +01001840 /* This stops all lingering devices */
1841 bootm_disable_interrupts();
1842
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02001843 /* Disable boot time services */
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001844 systab.con_in_handle = NULL;
1845 systab.con_in = NULL;
1846 systab.con_out_handle = NULL;
1847 systab.con_out = NULL;
1848 systab.stderr_handle = NULL;
1849 systab.std_err = NULL;
1850 systab.boottime = NULL;
1851
1852 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001853 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001854
Alexander Grafbee91162016-03-04 01:09:59 +01001855 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001856 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001857 WATCHDOG_RESET();
1858
1859 return EFI_EXIT(EFI_SUCCESS);
1860}
1861
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001862/**
Mario Six78a88f72018-07-10 08:40:17 +02001863 * efi_get_next_monotonic_count() - get next value of the counter
1864 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001865 *
1866 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001867 *
Mario Six78a88f72018-07-10 08:40:17 +02001868 * See the Unified Extensible Firmware Interface (UEFI) specification for
1869 * details.
1870 *
1871 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001872 */
Alexander Grafbee91162016-03-04 01:09:59 +01001873static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1874{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001875 static uint64_t mono;
1876
Alexander Grafbee91162016-03-04 01:09:59 +01001877 EFI_ENTRY("%p", count);
1878 *count = mono++;
1879 return EFI_EXIT(EFI_SUCCESS);
1880}
1881
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001882/**
Mario Six78a88f72018-07-10 08:40:17 +02001883 * efi_stall() - sleep
1884 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001885 *
Mario Six78a88f72018-07-10 08:40:17 +02001886 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001887 *
Mario Six78a88f72018-07-10 08:40:17 +02001888 * See the Unified Extensible Firmware Interface (UEFI) specification for
1889 * details.
1890 *
1891 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001892 */
Alexander Grafbee91162016-03-04 01:09:59 +01001893static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1894{
1895 EFI_ENTRY("%ld", microseconds);
1896 udelay(microseconds);
1897 return EFI_EXIT(EFI_SUCCESS);
1898}
1899
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001900/**
Mario Six78a88f72018-07-10 08:40:17 +02001901 * efi_set_watchdog_timer() - reset the watchdog timer
1902 * @timeout: seconds before reset by watchdog
1903 * @watchdog_code: code to be logged when resetting
1904 * @data_size: size of buffer in bytes
1905 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001906 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001907 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001908 *
Mario Six78a88f72018-07-10 08:40:17 +02001909 * See the Unified Extensible Firmware Interface (UEFI) specification for
1910 * details.
1911 *
1912 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001913 */
Alexander Grafbee91162016-03-04 01:09:59 +01001914static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1915 uint64_t watchdog_code,
1916 unsigned long data_size,
1917 uint16_t *watchdog_data)
1918{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001919 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001920 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001921 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001922}
1923
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001924/**
Mario Six78a88f72018-07-10 08:40:17 +02001925 * efi_close_protocol() - close a protocol
1926 * @handle: handle on which the protocol shall be closed
1927 * @protocol: GUID of the protocol to close
1928 * @agent_handle: handle of the driver
1929 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001930 *
1931 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001932 *
Mario Six78a88f72018-07-10 08:40:17 +02001933 * See the Unified Extensible Firmware Interface (UEFI) specification for
1934 * details.
1935 *
1936 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001937 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001938static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001939 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001940 efi_handle_t agent_handle,
1941 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001942{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001943 struct efi_handler *handler;
1944 struct efi_open_protocol_info_item *item;
1945 struct efi_open_protocol_info_item *pos;
1946 efi_status_t r;
1947
Rob Clark778e6af2017-09-13 18:05:41 -04001948 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001949 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001950
1951 if (!agent_handle) {
1952 r = EFI_INVALID_PARAMETER;
1953 goto out;
1954 }
1955 r = efi_search_protocol(handle, protocol, &handler);
1956 if (r != EFI_SUCCESS)
1957 goto out;
1958
1959 r = EFI_NOT_FOUND;
1960 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1961 if (item->info.agent_handle == agent_handle &&
1962 item->info.controller_handle == controller_handle) {
1963 efi_delete_open_info(item);
1964 r = EFI_SUCCESS;
1965 break;
1966 }
1967 }
1968out:
1969 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001970}
1971
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001972/**
Mario Six78a88f72018-07-10 08:40:17 +02001973 * efi_open_protocol_information() - provide information about then open status
1974 * of a protocol on a handle
1975 * @handle: handle for which the information shall be retrieved
1976 * @protocol: GUID of the protocol
1977 * @entry_buffer: buffer to receive the open protocol information
1978 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001979 *
1980 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001981 *
Mario Six78a88f72018-07-10 08:40:17 +02001982 * See the Unified Extensible Firmware Interface (UEFI) specification for
1983 * details.
1984 *
1985 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001986 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001987static efi_status_t EFIAPI efi_open_protocol_information(
1988 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01001989 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001990 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01001991{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01001992 unsigned long buffer_size;
1993 unsigned long count;
1994 struct efi_handler *handler;
1995 struct efi_open_protocol_info_item *item;
1996 efi_status_t r;
1997
Rob Clark778e6af2017-09-13 18:05:41 -04001998 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01001999 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002000
2001 /* Check parameters */
2002 if (!entry_buffer) {
2003 r = EFI_INVALID_PARAMETER;
2004 goto out;
2005 }
2006 r = efi_search_protocol(handle, protocol, &handler);
2007 if (r != EFI_SUCCESS)
2008 goto out;
2009
2010 /* Count entries */
2011 count = 0;
2012 list_for_each_entry(item, &handler->open_infos, link) {
2013 if (item->info.open_count)
2014 ++count;
2015 }
2016 *entry_count = count;
2017 *entry_buffer = NULL;
2018 if (!count) {
2019 r = EFI_SUCCESS;
2020 goto out;
2021 }
2022
2023 /* Copy entries */
2024 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002025 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002026 (void **)entry_buffer);
2027 if (r != EFI_SUCCESS)
2028 goto out;
2029 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2030 if (item->info.open_count)
2031 (*entry_buffer)[--count] = item->info;
2032 }
2033out:
2034 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002035}
2036
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002037/**
Mario Six78a88f72018-07-10 08:40:17 +02002038 * efi_protocols_per_handle() - get protocols installed on a handle
2039 * @handle: handle for which the information is retrieved
2040 * @protocol_buffer: buffer with protocol GUIDs
2041 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002042 *
2043 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002044 *
Mario Six78a88f72018-07-10 08:40:17 +02002045 * See the Unified Extensible Firmware Interface (UEFI) specification for
2046 * details.
2047 *
2048 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002049 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002050static efi_status_t EFIAPI efi_protocols_per_handle(
2051 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002052 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002053{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002054 unsigned long buffer_size;
2055 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002056 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002057 efi_status_t r;
2058
Alexander Grafbee91162016-03-04 01:09:59 +01002059 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2060 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002061
2062 if (!handle || !protocol_buffer || !protocol_buffer_count)
2063 return EFI_EXIT(EFI_INVALID_PARAMETER);
2064
2065 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002066 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002067
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002068 efiobj = efi_search_obj(handle);
2069 if (!efiobj)
2070 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002071
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002072 /* Count protocols */
2073 list_for_each(protocol_handle, &efiobj->protocols) {
2074 ++*protocol_buffer_count;
2075 }
2076
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002077 /* Copy GUIDs */
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002078 if (*protocol_buffer_count) {
2079 size_t j = 0;
2080
2081 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002082 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002083 (void **)protocol_buffer);
2084 if (r != EFI_SUCCESS)
2085 return EFI_EXIT(r);
2086 list_for_each(protocol_handle, &efiobj->protocols) {
2087 struct efi_handler *protocol;
2088
2089 protocol = list_entry(protocol_handle,
2090 struct efi_handler, link);
2091 (*protocol_buffer)[j] = (void *)protocol->guid;
2092 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002093 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002094 }
2095
2096 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002097}
2098
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002099/**
Mario Six78a88f72018-07-10 08:40:17 +02002100 * efi_locate_handle_buffer() - locate handles implementing a protocol
2101 * @search_type: selection criterion
2102 * @protocol: GUID of the protocol
2103 * @search_key: registration key
2104 * @no_handles: number of returned handles
2105 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002106 *
2107 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002108 *
Mario Six78a88f72018-07-10 08:40:17 +02002109 * See the Unified Extensible Firmware Interface (UEFI) specification for
2110 * details.
2111 *
2112 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002113 */
Alexander Grafbee91162016-03-04 01:09:59 +01002114static efi_status_t EFIAPI efi_locate_handle_buffer(
2115 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002116 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002117 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002118{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002119 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002120 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002121
Rob Clark778e6af2017-09-13 18:05:41 -04002122 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002123 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002124
2125 if (!no_handles || !buffer) {
2126 r = EFI_INVALID_PARAMETER;
2127 goto out;
2128 }
2129 *no_handles = 0;
2130 *buffer = NULL;
2131 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2132 *buffer);
2133 if (r != EFI_BUFFER_TOO_SMALL)
2134 goto out;
Heinrich Schuchardteee65302018-10-03 20:02:29 +02002135 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002136 (void **)buffer);
2137 if (r != EFI_SUCCESS)
2138 goto out;
2139 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2140 *buffer);
2141 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002142 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002143out:
2144 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002145}
2146
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002147/**
Mario Six78a88f72018-07-10 08:40:17 +02002148 * efi_locate_protocol() - find an interface implementing a protocol
2149 * @protocol: GUID of the protocol
2150 * @registration: registration key passed to the notification function
2151 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002152 *
2153 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002154 *
Mario Six78a88f72018-07-10 08:40:17 +02002155 * See the Unified Extensible Firmware Interface (UEFI) specification for
2156 * details.
2157 *
2158 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002159 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002160static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002161 void *registration,
2162 void **protocol_interface)
2163{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002164 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002165 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002166
Rob Clark778e6af2017-09-13 18:05:41 -04002167 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002168
2169 if (!protocol || !protocol_interface)
2170 return EFI_EXIT(EFI_INVALID_PARAMETER);
2171
2172 list_for_each(lhandle, &efi_obj_list) {
2173 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002174 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002175
2176 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002177
Heinrich Schuchardtfae01182018-09-26 05:27:55 +02002178 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002179 if (ret == EFI_SUCCESS) {
2180 *protocol_interface = handler->protocol_interface;
2181 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002182 }
2183 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002184 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002185
2186 return EFI_EXIT(EFI_NOT_FOUND);
2187}
2188
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002189/**
Mario Six78a88f72018-07-10 08:40:17 +02002190 * efi_locate_device_path() - Get the device path and handle of an device
2191 * implementing a protocol
2192 * @protocol: GUID of the protocol
2193 * @device_path: device path
2194 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002195 *
2196 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002197 *
Mario Six78a88f72018-07-10 08:40:17 +02002198 * See the Unified Extensible Firmware Interface (UEFI) specification for
2199 * details.
2200 *
2201 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002202 */
2203static efi_status_t EFIAPI efi_locate_device_path(
2204 const efi_guid_t *protocol,
2205 struct efi_device_path **device_path,
2206 efi_handle_t *device)
2207{
2208 struct efi_device_path *dp;
2209 size_t i;
2210 struct efi_handler *handler;
2211 efi_handle_t *handles;
2212 size_t len, len_dp;
2213 size_t len_best = 0;
2214 efi_uintn_t no_handles;
2215 u8 *remainder;
2216 efi_status_t ret;
2217
2218 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2219
2220 if (!protocol || !device_path || !*device_path || !device) {
2221 ret = EFI_INVALID_PARAMETER;
2222 goto out;
2223 }
2224
2225 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002226 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002227
2228 /* Get all handles implementing the protocol */
2229 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2230 &no_handles, &handles));
2231 if (ret != EFI_SUCCESS)
2232 goto out;
2233
2234 for (i = 0; i < no_handles; ++i) {
2235 /* Find the device path protocol */
2236 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2237 &handler);
2238 if (ret != EFI_SUCCESS)
2239 continue;
2240 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002241 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002242 /*
2243 * This handle can only be a better fit
2244 * if its device path length is longer than the best fit and
2245 * if its device path length is shorter of equal the searched
2246 * device path.
2247 */
2248 if (len_dp <= len_best || len_dp > len)
2249 continue;
2250 /* Check if dp is a subpath of device_path */
2251 if (memcmp(*device_path, dp, len_dp))
2252 continue;
2253 *device = handles[i];
2254 len_best = len_dp;
2255 }
2256 if (len_best) {
2257 remainder = (u8 *)*device_path + len_best;
2258 *device_path = (struct efi_device_path *)remainder;
2259 ret = EFI_SUCCESS;
2260 } else {
2261 ret = EFI_NOT_FOUND;
2262 }
2263out:
2264 return EFI_EXIT(ret);
2265}
2266
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002267/**
Mario Six78a88f72018-07-10 08:40:17 +02002268 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2269 * interfaces
2270 * @handle: handle on which the protocol interfaces shall be installed
2271 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2272 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002273 *
2274 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002275 *
Mario Six78a88f72018-07-10 08:40:17 +02002276 * See the Unified Extensible Firmware Interface (UEFI) specification for
2277 * details.
2278 *
2279 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002280 */
Heinrich Schuchardt7657ae12019-04-12 06:59:49 +02002281efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002282 (efi_handle_t *handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002283{
2284 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002285
Alexander Grafbeb077a2018-06-18 17:23:05 +02002286 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002287 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002288 void *protocol_interface;
2289 efi_status_t r = EFI_SUCCESS;
2290 int i = 0;
2291
2292 if (!handle)
2293 return EFI_EXIT(EFI_INVALID_PARAMETER);
2294
Alexander Grafbeb077a2018-06-18 17:23:05 +02002295 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002296 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002297 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002298 if (!protocol)
2299 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002300 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002301 r = EFI_CALL(efi_install_protocol_interface(
2302 handle, protocol,
2303 EFI_NATIVE_INTERFACE,
2304 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002305 if (r != EFI_SUCCESS)
2306 break;
2307 i++;
2308 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002309 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002310 if (r == EFI_SUCCESS)
2311 return EFI_EXIT(r);
2312
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002313 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002314 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002315 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002316 protocol = efi_va_arg(argptr, efi_guid_t*);
2317 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002318 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002319 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002320 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002321 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002322
2323 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002324}
2325
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002326/**
Mario Six78a88f72018-07-10 08:40:17 +02002327 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2328 * interfaces
2329 * @handle: handle from which the protocol interfaces shall be removed
2330 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2331 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002332 *
2333 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002334 *
Mario Six78a88f72018-07-10 08:40:17 +02002335 * See the Unified Extensible Firmware Interface (UEFI) specification for
2336 * details.
2337 *
2338 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002339 */
Alexander Grafbee91162016-03-04 01:09:59 +01002340static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002341 efi_handle_t handle, ...)
Alexander Grafbee91162016-03-04 01:09:59 +01002342{
2343 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002344
Alexander Grafbeb077a2018-06-18 17:23:05 +02002345 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002346 const efi_guid_t *protocol;
2347 void *protocol_interface;
2348 efi_status_t r = EFI_SUCCESS;
2349 size_t i = 0;
2350
2351 if (!handle)
2352 return EFI_EXIT(EFI_INVALID_PARAMETER);
2353
Alexander Grafbeb077a2018-06-18 17:23:05 +02002354 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002355 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002356 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002357 if (!protocol)
2358 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002359 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002360 r = efi_uninstall_protocol(handle, protocol,
2361 protocol_interface);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002362 if (r != EFI_SUCCESS)
2363 break;
2364 i++;
2365 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002366 efi_va_end(argptr);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002367 if (r == EFI_SUCCESS) {
2368 /* If the last protocol has been removed, delete the handle. */
2369 if (list_empty(&handle->protocols)) {
2370 list_del(&handle->link);
2371 free(handle);
2372 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002373 return EFI_EXIT(r);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02002374 }
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002375
2376 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002377 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002378 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002379 protocol = efi_va_arg(argptr, efi_guid_t*);
2380 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002381 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2382 EFI_NATIVE_INTERFACE,
2383 protocol_interface));
2384 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002385 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002386
Heinrich Schuchardte2373022018-09-24 19:57:27 +02002387 /* In case of an error always return EFI_INVALID_PARAMETER */
2388 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafbee91162016-03-04 01:09:59 +01002389}
2390
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002391/**
Mario Six78a88f72018-07-10 08:40:17 +02002392 * efi_calculate_crc32() - calculate cyclic redundancy code
2393 * @data: buffer with data
2394 * @data_size: size of buffer in bytes
2395 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002396 *
2397 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002398 *
Mario Six78a88f72018-07-10 08:40:17 +02002399 * See the Unified Extensible Firmware Interface (UEFI) specification for
2400 * details.
2401 *
2402 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002403 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002404static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2405 efi_uintn_t data_size,
2406 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002407{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002408 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002409 *crc32_p = crc32(0, data, data_size);
2410 return EFI_EXIT(EFI_SUCCESS);
2411}
2412
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002413/**
Mario Six78a88f72018-07-10 08:40:17 +02002414 * efi_copy_mem() - copy memory
2415 * @destination: destination of the copy operation
2416 * @source: source of the copy operation
2417 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002418 *
2419 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002420 *
Mario Six78a88f72018-07-10 08:40:17 +02002421 * See the Unified Extensible Firmware Interface (UEFI) specification for
2422 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002423 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002424static void EFIAPI efi_copy_mem(void *destination, const void *source,
2425 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002426{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002427 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardt0bc81a72019-01-09 21:41:13 +01002428 memmove(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002429 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002430}
2431
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002432/**
Mario Six78a88f72018-07-10 08:40:17 +02002433 * efi_set_mem() - Fill memory with a byte value.
2434 * @buffer: buffer to fill
2435 * @size: size of buffer in bytes
2436 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002437 *
2438 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002439 *
Mario Six78a88f72018-07-10 08:40:17 +02002440 * See the Unified Extensible Firmware Interface (UEFI) specification for
2441 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002442 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002443static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002444{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002445 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002446 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002447 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002448}
2449
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002450/**
Mario Six78a88f72018-07-10 08:40:17 +02002451 * efi_protocol_open() - open protocol interface on a handle
2452 * @handler: handler of a protocol
2453 * @protocol_interface: interface implementing the protocol
2454 * @agent_handle: handle of the driver
2455 * @controller_handle: handle of the controller
2456 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002457 *
Mario Six78a88f72018-07-10 08:40:17 +02002458 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002459 */
2460static efi_status_t efi_protocol_open(
2461 struct efi_handler *handler,
2462 void **protocol_interface, void *agent_handle,
2463 void *controller_handle, uint32_t attributes)
2464{
2465 struct efi_open_protocol_info_item *item;
2466 struct efi_open_protocol_info_entry *match = NULL;
2467 bool opened_by_driver = false;
2468 bool opened_exclusive = false;
2469
2470 /* If there is no agent, only return the interface */
2471 if (!agent_handle)
2472 goto out;
2473
2474 /* For TEST_PROTOCOL ignore interface attribute */
2475 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2476 *protocol_interface = NULL;
2477
2478 /*
2479 * Check if the protocol is already opened by a driver with the same
2480 * attributes or opened exclusively
2481 */
2482 list_for_each_entry(item, &handler->open_infos, link) {
2483 if (item->info.agent_handle == agent_handle) {
2484 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2485 (item->info.attributes == attributes))
2486 return EFI_ALREADY_STARTED;
2487 }
2488 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2489 opened_exclusive = true;
2490 }
2491
2492 /* Only one controller can open the protocol exclusively */
2493 if (opened_exclusive && attributes &
2494 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2495 return EFI_ACCESS_DENIED;
2496
2497 /* Prepare exclusive opening */
2498 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2499 /* Try to disconnect controllers */
2500 list_for_each_entry(item, &handler->open_infos, link) {
2501 if (item->info.attributes ==
2502 EFI_OPEN_PROTOCOL_BY_DRIVER)
2503 EFI_CALL(efi_disconnect_controller(
2504 item->info.controller_handle,
2505 item->info.agent_handle,
2506 NULL));
2507 }
2508 opened_by_driver = false;
2509 /* Check if all controllers are disconnected */
2510 list_for_each_entry(item, &handler->open_infos, link) {
2511 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2512 opened_by_driver = true;
2513 }
Heinrich Schuchardt4f37fa42018-09-30 13:40:43 +02002514 /* Only one controller can be connected */
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002515 if (opened_by_driver)
2516 return EFI_ACCESS_DENIED;
2517 }
2518
2519 /* Find existing entry */
2520 list_for_each_entry(item, &handler->open_infos, link) {
2521 if (item->info.agent_handle == agent_handle &&
2522 item->info.controller_handle == controller_handle)
2523 match = &item->info;
2524 }
2525 /* None found, create one */
2526 if (!match) {
2527 match = efi_create_open_info(handler);
2528 if (!match)
2529 return EFI_OUT_OF_RESOURCES;
2530 }
2531
2532 match->agent_handle = agent_handle;
2533 match->controller_handle = controller_handle;
2534 match->attributes = attributes;
2535 match->open_count++;
2536
2537out:
2538 /* For TEST_PROTOCOL ignore interface attribute. */
2539 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2540 *protocol_interface = handler->protocol_interface;
2541
2542 return EFI_SUCCESS;
2543}
2544
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002545/**
Mario Six78a88f72018-07-10 08:40:17 +02002546 * efi_open_protocol() - open protocol interface on a handle
2547 * @handle: handle on which the protocol shall be opened
2548 * @protocol: GUID of the protocol
2549 * @protocol_interface: interface implementing the protocol
2550 * @agent_handle: handle of the driver
2551 * @controller_handle: handle of the controller
2552 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002553 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002554 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002555 *
Mario Six78a88f72018-07-10 08:40:17 +02002556 * See the Unified Extensible Firmware Interface (UEFI) specification for
2557 * details.
2558 *
2559 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002560 */
Heinrich Schuchardtfaea1042018-09-26 05:27:54 +02002561static efi_status_t EFIAPI efi_open_protocol
2562 (efi_handle_t handle, const efi_guid_t *protocol,
2563 void **protocol_interface, efi_handle_t agent_handle,
2564 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafbee91162016-03-04 01:09:59 +01002565{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002566 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002567 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002568
Rob Clark778e6af2017-09-13 18:05:41 -04002569 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002570 protocol_interface, agent_handle, controller_handle,
2571 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002572
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002573 if (!handle || !protocol ||
2574 (!protocol_interface && attributes !=
2575 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2576 goto out;
2577 }
2578
2579 switch (attributes) {
2580 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2581 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2582 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2583 break;
2584 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2585 if (controller_handle == handle)
2586 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002587 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002588 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2589 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002590 /* Check that the controller handle is valid */
2591 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002592 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002593 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002594 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002595 /* Check that the agent handle is valid */
2596 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002597 goto out;
2598 break;
2599 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002600 goto out;
2601 }
2602
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002603 r = efi_search_protocol(handle, protocol, &handler);
2604 if (r != EFI_SUCCESS)
2605 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002606
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002607 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2608 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002609out:
2610 return EFI_EXIT(r);
2611}
2612
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002613/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002614 * efi_start_image() - call the entry point of an image
2615 * @image_handle: handle of the image
2616 * @exit_data_size: size of the buffer
2617 * @exit_data: buffer to receive the exit data of the called image
2618 *
2619 * This function implements the StartImage service.
2620 *
2621 * See the Unified Extensible Firmware Interface (UEFI) specification for
2622 * details.
2623 *
2624 * Return: status code
2625 */
2626efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2627 efi_uintn_t *exit_data_size,
2628 u16 **exit_data)
2629{
2630 struct efi_loaded_image_obj *image_obj =
2631 (struct efi_loaded_image_obj *)image_handle;
2632 efi_status_t ret;
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002633 void *info;
2634 efi_handle_t parent_image = current_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002635
2636 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2637
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002638 /* Check parameters */
2639 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2640 &info, NULL, NULL,
2641 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2642 if (ret != EFI_SUCCESS)
2643 return EFI_EXIT(EFI_INVALID_PARAMETER);
2644
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002645 efi_is_direct_boot = false;
2646
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02002647 image_obj->exit_data_size = exit_data_size;
2648 image_obj->exit_data = exit_data;
2649
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002650 /* call the image! */
2651 if (setjmp(&image_obj->exit_jmp)) {
2652 /*
2653 * We called the entry point of the child image with EFI_CALL
2654 * in the lines below. The child image called the Exit() boot
2655 * service efi_exit() which executed the long jump that brought
2656 * us to the current line. This implies that the second half
2657 * of the EFI_CALL macro has not been executed.
2658 */
2659#ifdef CONFIG_ARM
2660 /*
2661 * efi_exit() called efi_restore_gd(). We have to undo this
2662 * otherwise __efi_entry_check() will put the wrong value into
2663 * app_gd.
2664 */
2665 gd = app_gd;
2666#endif
2667 /*
2668 * To get ready to call EFI_EXIT below we have to execute the
2669 * missed out steps of EFI_CALL.
2670 */
2671 assert(__efi_entry_check());
2672 debug("%sEFI: %lu returned by started image\n",
2673 __efi_nesting_dec(),
2674 (unsigned long)((uintptr_t)image_obj->exit_status &
2675 ~EFI_ERROR_MASK));
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002676 current_image = parent_image;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002677 return EFI_EXIT(image_obj->exit_status);
2678 }
2679
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002680 current_image = image_handle;
AKASHI Takahiro6b95b382019-04-19 12:22:35 +09002681 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
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/**
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02002694 * efi_update_exit_data() - fill exit data parameters of StartImage()
2695 *
2696 * @image_obj image handle
2697 * @exit_data_size size of the exit data buffer
2698 * @exit_data buffer with data returned by UEFI payload
2699 * Return: status code
2700 */
2701static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2702 efi_uintn_t exit_data_size,
2703 u16 *exit_data)
2704{
2705 efi_status_t ret;
2706
2707 /*
2708 * If exit_data is not provided to StartImage(), exit_data_size must be
2709 * ignored.
2710 */
2711 if (!image_obj->exit_data)
2712 return EFI_SUCCESS;
2713 if (image_obj->exit_data_size)
2714 *image_obj->exit_data_size = exit_data_size;
2715 if (exit_data_size && exit_data) {
2716 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2717 exit_data_size,
2718 (void **)image_obj->exit_data);
2719 if (ret != EFI_SUCCESS)
2720 return ret;
2721 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2722 } else {
2723 image_obj->exit_data = NULL;
2724 }
2725 return EFI_SUCCESS;
2726}
2727
2728/**
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002729 * efi_exit() - leave an EFI application or driver
2730 * @image_handle: handle of the application or driver that is exiting
2731 * @exit_status: status code
2732 * @exit_data_size: size of the buffer in bytes
2733 * @exit_data: buffer with data describing an error
2734 *
2735 * This function implements the Exit service.
2736 *
2737 * See the Unified Extensible Firmware Interface (UEFI) specification for
2738 * details.
2739 *
2740 * Return: status code
2741 */
2742static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2743 efi_status_t exit_status,
2744 efi_uintn_t exit_data_size,
2745 u16 *exit_data)
2746{
2747 /*
2748 * TODO: We should call the unload procedure of the loaded
2749 * image protocol.
2750 */
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002751 efi_status_t ret;
2752 void *info;
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002753 struct efi_loaded_image_obj *image_obj =
2754 (struct efi_loaded_image_obj *)image_handle;
2755
2756 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2757 exit_data_size, exit_data);
2758
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002759 /* Check parameters */
2760 if (image_handle != current_image)
2761 goto out;
2762 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2763 &info, NULL, NULL,
2764 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2765 if (ret != EFI_SUCCESS)
2766 goto out;
2767
Heinrich Schuchardt556d8dc2019-04-30 17:57:30 +02002768 /* Exit data is only foreseen in case of failure. */
2769 if (exit_status != EFI_SUCCESS) {
2770 ret = efi_update_exit_data(image_obj, exit_data_size,
2771 exit_data);
2772 /* Exiting has priority. Don't return error to caller. */
2773 if (ret != EFI_SUCCESS)
2774 EFI_PRINT("%s: out of memory\n", __func__);
2775 }
2776
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002777 /* Make sure entry/exit counts for EFI world cross-overs match */
2778 EFI_EXIT(exit_status);
2779
2780 /*
2781 * But longjmp out with the U-Boot gd, not the application's, as
2782 * the other end is a setjmp call inside EFI context.
2783 */
2784 efi_restore_gd();
2785
2786 image_obj->exit_status = exit_status;
2787 longjmp(&image_obj->exit_jmp, 1);
2788
2789 panic("EFI application exited");
Heinrich Schuchardtbb31c3f2019-03-26 19:03:17 +01002790out:
2791 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardta115d562019-03-26 19:02:05 +01002792}
2793
2794/**
Mario Six78a88f72018-07-10 08:40:17 +02002795 * efi_handle_protocol() - get interface of a protocol on a handle
2796 * @handle: handle on which the protocol shall be opened
2797 * @protocol: GUID of the protocol
2798 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002799 *
2800 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002801 *
Mario Six78a88f72018-07-10 08:40:17 +02002802 * See the Unified Extensible Firmware Interface (UEFI) specification for
2803 * details.
2804 *
2805 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002806 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002807static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002808 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002809 void **protocol_interface)
2810{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002811 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2812 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002813}
2814
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002815/**
Mario Six78a88f72018-07-10 08:40:17 +02002816 * efi_bind_controller() - bind a single driver to a controller
2817 * @controller_handle: controller handle
2818 * @driver_image_handle: driver handle
2819 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002820 *
Mario Six78a88f72018-07-10 08:40:17 +02002821 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002822 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002823static efi_status_t efi_bind_controller(
2824 efi_handle_t controller_handle,
2825 efi_handle_t driver_image_handle,
2826 struct efi_device_path *remain_device_path)
2827{
2828 struct efi_driver_binding_protocol *binding_protocol;
2829 efi_status_t r;
2830
2831 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2832 &efi_guid_driver_binding_protocol,
2833 (void **)&binding_protocol,
2834 driver_image_handle, NULL,
2835 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2836 if (r != EFI_SUCCESS)
2837 return r;
2838 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2839 controller_handle,
2840 remain_device_path));
2841 if (r == EFI_SUCCESS)
2842 r = EFI_CALL(binding_protocol->start(binding_protocol,
2843 controller_handle,
2844 remain_device_path));
2845 EFI_CALL(efi_close_protocol(driver_image_handle,
2846 &efi_guid_driver_binding_protocol,
2847 driver_image_handle, NULL));
2848 return r;
2849}
2850
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002851/**
Mario Six78a88f72018-07-10 08:40:17 +02002852 * efi_connect_single_controller() - connect a single driver to a controller
2853 * @controller_handle: controller
2854 * @driver_image_handle: driver
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002855 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002856 *
Mario Six78a88f72018-07-10 08:40:17 +02002857 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002858 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002859static efi_status_t efi_connect_single_controller(
2860 efi_handle_t controller_handle,
2861 efi_handle_t *driver_image_handle,
2862 struct efi_device_path *remain_device_path)
2863{
2864 efi_handle_t *buffer;
2865 size_t count;
2866 size_t i;
2867 efi_status_t r;
2868 size_t connected = 0;
2869
2870 /* Get buffer with all handles with driver binding protocol */
2871 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2872 &efi_guid_driver_binding_protocol,
2873 NULL, &count, &buffer));
2874 if (r != EFI_SUCCESS)
2875 return r;
2876
2877 /* Context Override */
2878 if (driver_image_handle) {
2879 for (; *driver_image_handle; ++driver_image_handle) {
2880 for (i = 0; i < count; ++i) {
2881 if (buffer[i] == *driver_image_handle) {
2882 buffer[i] = NULL;
2883 r = efi_bind_controller(
2884 controller_handle,
2885 *driver_image_handle,
2886 remain_device_path);
2887 /*
2888 * For drivers that do not support the
2889 * controller or are already connected
2890 * we receive an error code here.
2891 */
2892 if (r == EFI_SUCCESS)
2893 ++connected;
2894 }
2895 }
2896 }
2897 }
2898
2899 /*
2900 * TODO: Some overrides are not yet implemented:
2901 * - Platform Driver Override
2902 * - Driver Family Override Search
2903 * - Bus Specific Driver Override
2904 */
2905
2906 /* Driver Binding Search */
2907 for (i = 0; i < count; ++i) {
2908 if (buffer[i]) {
2909 r = efi_bind_controller(controller_handle,
2910 buffer[i],
2911 remain_device_path);
2912 if (r == EFI_SUCCESS)
2913 ++connected;
2914 }
2915 }
2916
2917 efi_free_pool(buffer);
2918 if (!connected)
2919 return EFI_NOT_FOUND;
2920 return EFI_SUCCESS;
2921}
2922
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002923/**
Mario Six78a88f72018-07-10 08:40:17 +02002924 * efi_connect_controller() - connect a controller to a driver
2925 * @controller_handle: handle of the controller
2926 * @driver_image_handle: handle of the driver
2927 * @remain_device_path: device path of a child controller
2928 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002929 *
2930 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002931 *
2932 * See the Unified Extensible Firmware Interface (UEFI) specification for
2933 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002934 *
2935 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02002936 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002937 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2938 *
Mario Six78a88f72018-07-10 08:40:17 +02002939 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002940 */
2941static efi_status_t EFIAPI efi_connect_controller(
2942 efi_handle_t controller_handle,
2943 efi_handle_t *driver_image_handle,
2944 struct efi_device_path *remain_device_path,
2945 bool recursive)
2946{
2947 efi_status_t r;
2948 efi_status_t ret = EFI_NOT_FOUND;
2949 struct efi_object *efiobj;
2950
Heinrich Schuchardtd1788362018-12-09 16:39:20 +01002951 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002952 remain_device_path, recursive);
2953
2954 efiobj = efi_search_obj(controller_handle);
2955 if (!efiobj) {
2956 ret = EFI_INVALID_PARAMETER;
2957 goto out;
2958 }
2959
2960 r = efi_connect_single_controller(controller_handle,
2961 driver_image_handle,
2962 remain_device_path);
2963 if (r == EFI_SUCCESS)
2964 ret = EFI_SUCCESS;
2965 if (recursive) {
2966 struct efi_handler *handler;
2967 struct efi_open_protocol_info_item *item;
2968
2969 list_for_each_entry(handler, &efiobj->protocols, link) {
2970 list_for_each_entry(item, &handler->open_infos, link) {
2971 if (item->info.attributes &
2972 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2973 r = EFI_CALL(efi_connect_controller(
2974 item->info.controller_handle,
2975 driver_image_handle,
2976 remain_device_path,
2977 recursive));
2978 if (r == EFI_SUCCESS)
2979 ret = EFI_SUCCESS;
2980 }
2981 }
2982 }
2983 }
2984 /* Check for child controller specified by end node */
2985 if (ret != EFI_SUCCESS && remain_device_path &&
2986 remain_device_path->type == DEVICE_PATH_TYPE_END)
2987 ret = EFI_SUCCESS;
2988out:
2989 return EFI_EXIT(ret);
2990}
2991
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002992/**
Mario Six78a88f72018-07-10 08:40:17 +02002993 * efi_reinstall_protocol_interface() - reinstall protocol interface
2994 * @handle: handle on which the protocol shall be reinstalled
2995 * @protocol: GUID of the protocol to be installed
2996 * @old_interface: interface to be removed
2997 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002998 *
2999 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02003000 *
3001 * See the Unified Extensible Firmware Interface (UEFI) specification for
3002 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003003 *
3004 * The old interface is uninstalled. The new interface is installed.
3005 * Drivers are connected.
3006 *
Mario Six78a88f72018-07-10 08:40:17 +02003007 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003008 */
3009static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3010 efi_handle_t handle, const efi_guid_t *protocol,
3011 void *old_interface, void *new_interface)
3012{
3013 efi_status_t ret;
3014
3015 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3016 new_interface);
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003017
3018 /* Uninstall protocol but do not delete handle */
3019 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003020 if (ret != EFI_SUCCESS)
3021 goto out;
Heinrich Schuchardt9b47f132018-09-28 22:14:17 +02003022
3023 /* Install the new protocol */
3024 ret = efi_add_protocol(handle, protocol, new_interface);
3025 /*
3026 * The UEFI spec does not specify what should happen to the handle
3027 * if in case of an error no protocol interface remains on the handle.
3028 * So let's do nothing here.
3029 */
Heinrich Schuchardte861a122018-05-11 12:09:22 +02003030 if (ret != EFI_SUCCESS)
3031 goto out;
3032 /*
3033 * The returned status code has to be ignored.
3034 * Do not create an error if no suitable driver for the handle exists.
3035 */
3036 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3037out:
3038 return EFI_EXIT(ret);
3039}
3040
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003041/**
Mario Six78a88f72018-07-10 08:40:17 +02003042 * efi_get_child_controllers() - get all child controllers associated to a driver
3043 * @efiobj: handle of the controller
3044 * @driver_handle: handle of the driver
3045 * @number_of_children: number of child controllers
3046 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003047 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003048 * The allocated buffer has to be freed with free().
3049 *
Mario Six78a88f72018-07-10 08:40:17 +02003050 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003051 */
3052static efi_status_t efi_get_child_controllers(
3053 struct efi_object *efiobj,
3054 efi_handle_t driver_handle,
3055 efi_uintn_t *number_of_children,
3056 efi_handle_t **child_handle_buffer)
3057{
3058 struct efi_handler *handler;
3059 struct efi_open_protocol_info_item *item;
3060 efi_uintn_t count = 0, i;
3061 bool duplicate;
3062
3063 /* Count all child controller associations */
3064 list_for_each_entry(handler, &efiobj->protocols, link) {
3065 list_for_each_entry(item, &handler->open_infos, link) {
3066 if (item->info.agent_handle == driver_handle &&
3067 item->info.attributes &
3068 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3069 ++count;
3070 }
3071 }
3072 /*
3073 * Create buffer. In case of duplicate child controller assignments
3074 * the buffer will be too large. But that does not harm.
3075 */
3076 *number_of_children = 0;
3077 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3078 if (!*child_handle_buffer)
3079 return EFI_OUT_OF_RESOURCES;
3080 /* Copy unique child handles */
3081 list_for_each_entry(handler, &efiobj->protocols, link) {
3082 list_for_each_entry(item, &handler->open_infos, link) {
3083 if (item->info.agent_handle == driver_handle &&
3084 item->info.attributes &
3085 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3086 /* Check this is a new child controller */
3087 duplicate = false;
3088 for (i = 0; i < *number_of_children; ++i) {
3089 if ((*child_handle_buffer)[i] ==
3090 item->info.controller_handle)
3091 duplicate = true;
3092 }
3093 /* Copy handle to buffer */
3094 if (!duplicate) {
3095 i = (*number_of_children)++;
3096 (*child_handle_buffer)[i] =
3097 item->info.controller_handle;
3098 }
3099 }
3100 }
3101 }
3102 return EFI_SUCCESS;
3103}
3104
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02003105/**
Mario Six78a88f72018-07-10 08:40:17 +02003106 * efi_disconnect_controller() - disconnect a controller from a driver
3107 * @controller_handle: handle of the controller
3108 * @driver_image_handle: handle of the driver
3109 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003110 *
3111 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003112 *
Mario Six78a88f72018-07-10 08:40:17 +02003113 * See the Unified Extensible Firmware Interface (UEFI) specification for
3114 * details.
3115 *
3116 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01003117 */
3118static efi_status_t EFIAPI efi_disconnect_controller(
3119 efi_handle_t controller_handle,
3120 efi_handle_t driver_image_handle,
3121 efi_handle_t child_handle)
3122{
3123 struct efi_driver_binding_protocol *binding_protocol;
3124 efi_handle_t *child_handle_buffer = NULL;
3125 size_t number_of_children = 0;
3126 efi_status_t r;
3127 size_t stop_count = 0;
3128 struct efi_object *efiobj;
3129
3130 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3131 child_handle);
3132
3133 efiobj = efi_search_obj(controller_handle);
3134 if (!efiobj) {
3135 r = EFI_INVALID_PARAMETER;
3136 goto out;
3137 }
3138
3139 if (child_handle && !efi_search_obj(child_handle)) {
3140 r = EFI_INVALID_PARAMETER;
3141 goto out;
3142 }
3143
3144 /* If no driver handle is supplied, disconnect all drivers */
3145 if (!driver_image_handle) {
3146 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3147 goto out;
3148 }
3149
3150 /* Create list of child handles */
3151 if (child_handle) {
3152 number_of_children = 1;
3153 child_handle_buffer = &child_handle;
3154 } else {
3155 efi_get_child_controllers(efiobj,
3156 driver_image_handle,
3157 &number_of_children,
3158 &child_handle_buffer);
3159 }
3160
3161 /* Get the driver binding protocol */
3162 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3163 &efi_guid_driver_binding_protocol,
3164 (void **)&binding_protocol,
3165 driver_image_handle, NULL,
3166 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3167 if (r != EFI_SUCCESS)
3168 goto out;
3169 /* Remove the children */
3170 if (number_of_children) {
3171 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3172 controller_handle,
3173 number_of_children,
3174 child_handle_buffer));
3175 if (r == EFI_SUCCESS)
3176 ++stop_count;
3177 }
3178 /* Remove the driver */
3179 if (!child_handle)
3180 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3181 controller_handle,
3182 0, NULL));
3183 if (r == EFI_SUCCESS)
3184 ++stop_count;
3185 EFI_CALL(efi_close_protocol(driver_image_handle,
3186 &efi_guid_driver_binding_protocol,
3187 driver_image_handle, NULL));
3188
3189 if (stop_count)
3190 r = EFI_SUCCESS;
3191 else
3192 r = EFI_NOT_FOUND;
3193out:
3194 if (!child_handle)
3195 free(child_handle_buffer);
3196 return EFI_EXIT(r);
3197}
3198
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003199static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003200 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003201 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3202 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003203 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003204 },
3205 .raise_tpl = efi_raise_tpl,
3206 .restore_tpl = efi_restore_tpl,
3207 .allocate_pages = efi_allocate_pages_ext,
3208 .free_pages = efi_free_pages_ext,
3209 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003210 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003211 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003212 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003213 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003214 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003215 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003216 .close_event = efi_close_event,
3217 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003218 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003219 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003220 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003221 .handle_protocol = efi_handle_protocol,
3222 .reserved = NULL,
3223 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003224 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003225 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003226 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003227 .load_image = efi_load_image,
3228 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003229 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003230 .unload_image = efi_unload_image,
3231 .exit_boot_services = efi_exit_boot_services,
3232 .get_next_monotonic_count = efi_get_next_monotonic_count,
3233 .stall = efi_stall,
3234 .set_watchdog_timer = efi_set_watchdog_timer,
3235 .connect_controller = efi_connect_controller,
3236 .disconnect_controller = efi_disconnect_controller,
3237 .open_protocol = efi_open_protocol,
3238 .close_protocol = efi_close_protocol,
3239 .open_protocol_information = efi_open_protocol_information,
3240 .protocols_per_handle = efi_protocols_per_handle,
3241 .locate_handle_buffer = efi_locate_handle_buffer,
3242 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003243 .install_multiple_protocol_interfaces =
3244 efi_install_multiple_protocol_interfaces,
3245 .uninstall_multiple_protocol_interfaces =
3246 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003247 .calculate_crc32 = efi_calculate_crc32,
3248 .copy_mem = efi_copy_mem,
3249 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003250 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003251};
3252
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003253static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003254
Alexander Graf3c63db92016-10-14 13:45:30 +02003255struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003256 .hdr = {
3257 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003258 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003259 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003260 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003261 .fw_vendor = firmware_vendor,
3262 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003263 .con_in = (void *)&efi_con_in,
3264 .con_out = (void *)&efi_con_out,
3265 .std_err = (void *)&efi_con_out,
3266 .runtime = (void *)&efi_runtime_services,
3267 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003268 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003269 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003270};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003271
3272/**
3273 * efi_initialize_system_table() - Initialize system table
3274 *
Heinrich Schuchardt04143592018-09-03 05:00:43 +02003275 * Return: status code
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003276 */
3277efi_status_t efi_initialize_system_table(void)
3278{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003279 efi_status_t ret;
3280
3281 /* Allocate configuration table array */
3282 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3283 EFI_MAX_CONFIGURATION_TABLES *
3284 sizeof(struct efi_configuration_table),
3285 (void **)&systab.tables);
3286
Heinrich Schuchardtb72aaa82018-09-03 19:12:24 +02003287 /* Set CRC32 field in table headers */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003288 efi_update_table_header_crc32(&systab.hdr);
3289 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3290 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003291
3292 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003293}