blob: eeefe0bdfe3bb1a813318a19143571748cc54bcb [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
16#include <inttypes.h>
17#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029
Alexander Grafc15d9212016-03-04 01:09:59 +010030/*
31 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
32 * we need to do trickery with caches. Since we don't want to break the EFI
33 * aware boot path, only apply hacks when loading exiting directly (breaking
34 * direct Linux EFI booting along the way - oh well).
35 */
36static bool efi_is_direct_boot = true;
37
38/*
39 * EFI can pass arbitrary additional "tables" containing vendor specific
40 * information to the payload. One such table is the FDT table which contains
41 * a pointer to a flattened device tree blob.
42 *
43 * In most cases we want to pass an FDT to the payload, so reserve one slot of
44 * config table space for it. The pointer gets populated by do_bootefi_exec().
45 */
Bin Mengc4d777b2018-06-27 20:38:02 -070046static struct efi_configuration_table __efi_runtime_data efi_conf_table[16];
Alexander Grafc15d9212016-03-04 01:09:59 +010047
Simon Glasscdfe6962016-09-25 15:27:35 -060048#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010049/*
50 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
51 * fixed when compiling U-Boot. However, the payload does not know about that
52 * restriction so we need to manually swap its and our view of that register on
53 * EFI callback entry/exit.
54 */
55static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060056#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010057
Rob Clark86789d52017-07-27 08:04:18 -040058static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040059static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010060/* GUID of the device tree table */
61const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010062/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
63const efi_guid_t efi_guid_driver_binding_protocol =
64 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040065
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010066/* event group ExitBootServices() invoked */
67const efi_guid_t efi_guid_event_group_exit_boot_services =
68 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
69/* event group SetVirtualAddressMap() invoked */
70const efi_guid_t efi_guid_event_group_virtual_address_change =
71 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
72/* event group memory map changed */
73const efi_guid_t efi_guid_event_group_memory_map_change =
74 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
75/* event group boot manager about to boot */
76const efi_guid_t efi_guid_event_group_ready_to_boot =
77 EFI_EVENT_GROUP_READY_TO_BOOT;
78/* event group ResetSystem() invoked (before ExitBootServices) */
79const efi_guid_t efi_guid_event_group_reset_system =
80 EFI_EVENT_GROUP_RESET_SYSTEM;
81
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010082static efi_status_t EFIAPI efi_disconnect_controller(
83 efi_handle_t controller_handle,
84 efi_handle_t driver_image_handle,
85 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010086
Rob Clark86789d52017-07-27 08:04:18 -040087/* Called on every callback entry */
88int __efi_entry_check(void)
89{
90 int ret = entry_count++ == 0;
91#ifdef CONFIG_ARM
92 assert(efi_gd);
93 app_gd = gd;
94 gd = efi_gd;
95#endif
96 return ret;
97}
98
99/* Called on every callback exit */
100int __efi_exit_check(void)
101{
102 int ret = --entry_count == 0;
103#ifdef CONFIG_ARM
104 gd = app_gd;
105#endif
106 return ret;
107}
108
Alexander Grafc15d9212016-03-04 01:09:59 +0100109/* Called from do_bootefi_exec() */
110void efi_save_gd(void)
111{
Simon Glasscdfe6962016-09-25 15:27:35 -0600112#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100113 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100115}
116
Rob Clark86789d52017-07-27 08:04:18 -0400117/*
Mario Six8fac2912018-07-10 08:40:17 +0200118 * Special case handler for error/abort that just forces things back to u-boot
119 * world so we can dump out an abort msg, without any care about returning back
120 * to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400121 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100122void efi_restore_gd(void)
123{
Simon Glasscdfe6962016-09-25 15:27:35 -0600124#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 /* Only restore if we're already in EFI context */
126 if (!efi_gd)
127 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100128 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600129#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100130}
131
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200132/**
Mario Six8fac2912018-07-10 08:40:17 +0200133 * indent_string() - returns a string for indenting with two spaces per level
134 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100135 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200136 * A maximum of ten indent levels is supported. Higher indent levels will be
137 * truncated.
138 *
Mario Six8fac2912018-07-10 08:40:17 +0200139 * Return: A string for indenting with two spaces per level is
140 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400141 */
142static const char *indent_string(int level)
143{
144 const char *indent = " ";
145 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100146
Rob Clarke7896c32017-07-27 08:04:19 -0400147 level = min(max, level * 2);
148 return &indent[max - level];
149}
150
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200151const char *__efi_nesting(void)
152{
153 return indent_string(nesting_level);
154}
155
Rob Clarke7896c32017-07-27 08:04:19 -0400156const char *__efi_nesting_inc(void)
157{
158 return indent_string(nesting_level++);
159}
160
161const char *__efi_nesting_dec(void)
162{
163 return indent_string(--nesting_level);
164}
165
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200166/**
Mario Six8fac2912018-07-10 08:40:17 +0200167 * efi_queue_event() - queue an EFI event
168 * @event: event to signal
169 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200170 *
171 * This function queues the notification function of the event for future
172 * execution.
173 *
Mario Six8fac2912018-07-10 08:40:17 +0200174 * The notification function is called if the task priority level of the event
175 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200176 *
177 * For the SignalEvent service see efi_signal_event_ext.
178 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200179 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100180static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200181{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200182 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200183 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200184 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100185 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200186 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200187 EFI_CALL_VOID(event->notify_function(event,
188 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200189 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200190 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200191}
192
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200193/**
Mario Six8fac2912018-07-10 08:40:17 +0200194 * efi_signal_event() - signal an EFI event
195 * @event: event to signal
196 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100197 *
Mario Six8fac2912018-07-10 08:40:17 +0200198 * This function signals an event. If the event belongs to an event group all
199 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100200 * their notification function is queued.
201 *
202 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100203 */
204void efi_signal_event(struct efi_event *event, bool check_tpl)
205{
206 if (event->group) {
207 struct efi_event *evt;
208
209 /*
210 * The signaled state has to set before executing any
211 * notification function
212 */
213 list_for_each_entry(evt, &efi_events, link) {
214 if (!evt->group || guidcmp(evt->group, event->group))
215 continue;
216 if (evt->is_signaled)
217 continue;
218 evt->is_signaled = true;
219 if (evt->type & EVT_NOTIFY_SIGNAL &&
220 evt->notify_function)
221 evt->is_queued = true;
222 }
223 list_for_each_entry(evt, &efi_events, link) {
224 if (!evt->group || guidcmp(evt->group, event->group))
225 continue;
226 if (evt->is_queued)
227 efi_queue_event(evt, check_tpl);
228 }
229 } else if (!event->is_signaled) {
230 event->is_signaled = true;
231 if (event->type & EVT_NOTIFY_SIGNAL)
232 efi_queue_event(event, check_tpl);
233 }
234}
235
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200236/**
Mario Six8fac2912018-07-10 08:40:17 +0200237 * efi_raise_tpl() - raise the task priority level
238 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200239 *
240 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200241 *
242 * See the Unified Extensible Firmware Interface (UEFI) specification for
243 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200244 *
Mario Six8fac2912018-07-10 08:40:17 +0200245 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200246 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100247static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100248{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100249 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200250
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200251 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200252
253 if (new_tpl < efi_tpl)
254 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
255 efi_tpl = new_tpl;
256 if (efi_tpl > TPL_HIGH_LEVEL)
257 efi_tpl = TPL_HIGH_LEVEL;
258
259 EFI_EXIT(EFI_SUCCESS);
260 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100261}
262
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200263/**
Mario Six8fac2912018-07-10 08:40:17 +0200264 * efi_restore_tpl() - lower the task priority level
265 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200266 *
267 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200268 *
Mario Six8fac2912018-07-10 08:40:17 +0200269 * See the Unified Extensible Firmware Interface (UEFI) specification for
270 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200271 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100272static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100273{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200274 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200275
276 if (old_tpl > efi_tpl)
277 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
278 efi_tpl = old_tpl;
279 if (efi_tpl > TPL_HIGH_LEVEL)
280 efi_tpl = TPL_HIGH_LEVEL;
281
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100282 /*
283 * Lowering the TPL may have made queued events eligible for execution.
284 */
285 efi_timer_check();
286
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200287 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100288}
289
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200290/**
Mario Six8fac2912018-07-10 08:40:17 +0200291 * efi_allocate_pages_ext() - allocate memory pages
292 * @type: type of allocation to be performed
293 * @memory_type: usage type of the allocated memory
294 * @pages: number of pages to be allocated
295 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200296 *
297 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200298 *
Mario Six8fac2912018-07-10 08:40:17 +0200299 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * details.
301 *
302 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200303 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900304static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100305 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900306 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100307{
308 efi_status_t r;
309
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100310 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100311 r = efi_allocate_pages(type, memory_type, pages, memory);
312 return EFI_EXIT(r);
313}
314
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200315/**
Mario Six8fac2912018-07-10 08:40:17 +0200316 * efi_free_pages_ext() - Free memory pages.
317 * @memory: start of the memory area to be freed
318 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200319 *
320 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200321 *
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 *
Mario Six8fac2912018-07-10 08:40:17 +0200325 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200326 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900327static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100328 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100329{
330 efi_status_t r;
331
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100332 EFI_ENTRY("%" PRIx64 ", 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100333 r = efi_free_pages(memory, pages);
334 return EFI_EXIT(r);
335}
336
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200337/**
Mario Six8fac2912018-07-10 08:40:17 +0200338 * efi_get_memory_map_ext() - get map describing memory usage
339 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map: buffer to which the memory map is written
342 * @map_key: key for the memory map
343 * @descriptor_size: size of an individual memory descriptor
344 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200345 *
346 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 *
Mario Six8fac2912018-07-10 08:40:17 +0200348 * See the Unified Extensible Firmware Interface (UEFI) specification for
349 * details.
350 *
351 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200352 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900353static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100354 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900355 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100356 efi_uintn_t *map_key,
357 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900358 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100359{
360 efi_status_t r;
361
362 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
363 map_key, descriptor_size, descriptor_version);
364 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
365 descriptor_size, descriptor_version);
366 return EFI_EXIT(r);
367}
368
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200369/**
Mario Six8fac2912018-07-10 08:40:17 +0200370 * efi_allocate_pool_ext() - allocate memory from pool
371 * @pool_type: type of the pool from which memory is to be allocated
372 * @size: number of bytes to be allocated
373 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200374 *
375 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200376 *
377 * See the Unified Extensible Firmware Interface (UEFI) specification for
378 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200379 *
Mario Six8fac2912018-07-10 08:40:17 +0200380 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200381 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200382static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100383 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200384 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100385{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100386 efi_status_t r;
387
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100388 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200389 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100390 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100391}
392
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200393/**
Mario Six8fac2912018-07-10 08:40:17 +0200394 * efi_free_pool_ext() - free memory from pool
395 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 *
397 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200398 *
Mario Six8fac2912018-07-10 08:40:17 +0200399 * See the Unified Extensible Firmware Interface (UEFI) specification for
400 * details.
401 *
402 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200403 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200404static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100406 efi_status_t r;
407
408 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200409 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100410 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100411}
412
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200413/**
Mario Six8fac2912018-07-10 08:40:17 +0200414 * efi_add_handle() - add a new object to the object list
415 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100416 *
Mario Six8fac2912018-07-10 08:40:17 +0200417 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100418 */
419void efi_add_handle(struct efi_object *obj)
420{
421 if (!obj)
422 return;
423 INIT_LIST_HEAD(&obj->protocols);
424 obj->handle = obj;
425 list_add_tail(&obj->link, &efi_obj_list);
426}
427
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200428/**
Mario Six8fac2912018-07-10 08:40:17 +0200429 * efi_create_handle() - create handle
430 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200431 *
Mario Six8fac2912018-07-10 08:40:17 +0200432 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200433 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100434efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200435{
436 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200437
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200438 obj = calloc(1, sizeof(struct efi_object));
439 if (!obj)
440 return EFI_OUT_OF_RESOURCES;
441
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100442 efi_add_handle(obj);
443 *handle = obj->handle;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200444
445 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200446}
447
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200448/**
Mario Six8fac2912018-07-10 08:40:17 +0200449 * efi_search_protocol() - find a protocol on a handle.
450 * @handle: handle
451 * @protocol_guid: GUID of the protocol
452 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100453 *
Mario Six8fac2912018-07-10 08:40:17 +0200454 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100455 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100456efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100457 const efi_guid_t *protocol_guid,
458 struct efi_handler **handler)
459{
460 struct efi_object *efiobj;
461 struct list_head *lhandle;
462
463 if (!handle || !protocol_guid)
464 return EFI_INVALID_PARAMETER;
465 efiobj = efi_search_obj(handle);
466 if (!efiobj)
467 return EFI_INVALID_PARAMETER;
468 list_for_each(lhandle, &efiobj->protocols) {
469 struct efi_handler *protocol;
470
471 protocol = list_entry(lhandle, struct efi_handler, link);
472 if (!guidcmp(protocol->guid, protocol_guid)) {
473 if (handler)
474 *handler = protocol;
475 return EFI_SUCCESS;
476 }
477 }
478 return EFI_NOT_FOUND;
479}
480
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200481/**
Mario Six8fac2912018-07-10 08:40:17 +0200482 * efi_remove_protocol() - delete protocol from a handle
483 * @handle: handle from which the protocol shall be deleted
484 * @protocol: GUID of the protocol to be deleted
485 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100486 *
Mario Six8fac2912018-07-10 08:40:17 +0200487 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100488 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100489efi_status_t efi_remove_protocol(const efi_handle_t handle,
490 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100491 void *protocol_interface)
492{
493 struct efi_handler *handler;
494 efi_status_t ret;
495
496 ret = efi_search_protocol(handle, protocol, &handler);
497 if (ret != EFI_SUCCESS)
498 return ret;
499 if (guidcmp(handler->guid, protocol))
500 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200501 if (handler->protocol_interface != protocol_interface)
502 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100503 list_del(&handler->link);
504 free(handler);
505 return EFI_SUCCESS;
506}
507
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200508/**
Mario Six8fac2912018-07-10 08:40:17 +0200509 * efi_remove_all_protocols() - delete all protocols from a handle
510 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100511 *
Mario Six8fac2912018-07-10 08:40:17 +0200512 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100513 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100514efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100515{
516 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100517 struct efi_handler *protocol;
518 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100519
520 efiobj = efi_search_obj(handle);
521 if (!efiobj)
522 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100523 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 efi_status_t ret;
525
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526 ret = efi_remove_protocol(handle, protocol->guid,
527 protocol->protocol_interface);
528 if (ret != EFI_SUCCESS)
529 return ret;
530 }
531 return EFI_SUCCESS;
532}
533
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200534/**
Mario Six8fac2912018-07-10 08:40:17 +0200535 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100536 *
Mario Six8fac2912018-07-10 08:40:17 +0200537 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100538 */
539void efi_delete_handle(struct efi_object *obj)
540{
541 if (!obj)
542 return;
543 efi_remove_all_protocols(obj->handle);
544 list_del(&obj->link);
545 free(obj);
546}
547
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200548/**
Mario Six8fac2912018-07-10 08:40:17 +0200549 * efi_is_event() - check if a pointer is a valid event
550 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100551 *
Mario Six8fac2912018-07-10 08:40:17 +0200552 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100553 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100554static efi_status_t efi_is_event(const struct efi_event *event)
555{
556 const struct efi_event *evt;
557
558 if (!event)
559 return EFI_INVALID_PARAMETER;
560 list_for_each_entry(evt, &efi_events, link) {
561 if (evt == event)
562 return EFI_SUCCESS;
563 }
564 return EFI_INVALID_PARAMETER;
565}
Alexander Grafc15d9212016-03-04 01:09:59 +0100566
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200567/**
Mario Six8fac2912018-07-10 08:40:17 +0200568 * efi_create_event() - create an event
569 * @type: type of the event to create
570 * @notify_tpl: task priority level of the event
571 * @notify_function: notification function of the event
572 * @notify_context: pointer passed to the notification function
573 * @group: event group
574 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200575 *
576 * This function is used inside U-Boot code to create an event.
577 *
578 * For the API function implementing the CreateEvent service see
579 * efi_create_event_ext.
580 *
Mario Six8fac2912018-07-10 08:40:17 +0200581 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200582 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100583efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200584 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200585 struct efi_event *event,
586 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100587 void *notify_context, efi_guid_t *group,
588 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100589{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100590 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200591
Jonathan Gray7758b212017-03-12 19:26:07 +1100592 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200593 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100594
595 if ((type & EVT_NOTIFY_SIGNAL) && (type & EVT_NOTIFY_WAIT))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200596 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100597
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100598 if ((type & (EVT_NOTIFY_SIGNAL | EVT_NOTIFY_WAIT)) &&
Jonathan Gray7758b212017-03-12 19:26:07 +1100599 notify_function == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200600 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100601
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100602 evt = calloc(1, sizeof(struct efi_event));
603 if (!evt)
604 return EFI_OUT_OF_RESOURCES;
605 evt->type = type;
606 evt->notify_tpl = notify_tpl;
607 evt->notify_function = notify_function;
608 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100609 evt->group = group;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100610 /* Disable timers on bootup */
611 evt->trigger_next = -1ULL;
612 evt->is_queued = false;
613 evt->is_signaled = false;
614 list_add_tail(&evt->link, &efi_events);
615 *event = evt;
616 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100617}
618
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200619/*
Mario Six8fac2912018-07-10 08:40:17 +0200620 * efi_create_event_ex() - create an event in a group
621 * @type: type of the event to create
622 * @notify_tpl: task priority level of the event
623 * @notify_function: notification function of the event
624 * @notify_context: pointer passed to the notification function
625 * @event: created event
626 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100627 *
628 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100629 *
Mario Six8fac2912018-07-10 08:40:17 +0200630 * See the Unified Extensible Firmware Interface (UEFI) specification for
631 * details.
632 *
633 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100634 */
635efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
636 void (EFIAPI *notify_function) (
637 struct efi_event *event,
638 void *context),
639 void *notify_context,
640 efi_guid_t *event_group,
641 struct efi_event **event)
642{
643 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
644 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100645 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100646 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100647}
648
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200649/**
Mario Six8fac2912018-07-10 08:40:17 +0200650 * efi_create_event_ext() - create an event
651 * @type: type of the event to create
652 * @notify_tpl: task priority level of the event
653 * @notify_function: notification function of the event
654 * @notify_context: pointer passed to the notification function
655 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200656 *
657 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200658 *
659 * See the Unified Extensible Firmware Interface (UEFI) specification for
660 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200661 *
Mario Six8fac2912018-07-10 08:40:17 +0200662 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200663 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200664static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100665 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200666 void (EFIAPI *notify_function) (
667 struct efi_event *event,
668 void *context),
669 void *notify_context, struct efi_event **event)
670{
671 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
672 notify_context);
673 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100674 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200675}
676
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200677/**
Mario Six8fac2912018-07-10 08:40:17 +0200678 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200679 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200680 * Check if a timer event has occurred or a queued notification function should
681 * be called.
682 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100683 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200684 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100685 */
686void efi_timer_check(void)
687{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100688 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100689 u64 now = timer_get_us();
690
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100691 list_for_each_entry(evt, &efi_events, link) {
692 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100693 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100694 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200695 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100696 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200697 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100698 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200699 break;
700 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100701 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200702 break;
703 default:
704 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200705 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100706 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100707 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100708 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100709 WATCHDOG_RESET();
710}
711
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200712/**
Mario Six8fac2912018-07-10 08:40:17 +0200713 * efi_set_timer() - set the trigger time for a timer event or stop the event
714 * @event: event for which the timer is set
715 * @type: type of the timer
716 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200717 *
718 * This is the function for internal usage in U-Boot. For the API function
719 * implementing the SetTimer service see efi_set_timer_ext.
720 *
Mario Six8fac2912018-07-10 08:40:17 +0200721 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200722 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200723efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200724 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100725{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100726 /* Check that the event is valid */
727 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
728 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100729
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200730 /*
731 * The parameter defines a multiple of 100ns.
732 * We use multiples of 1000ns. So divide by 10.
733 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200734 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100735
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100736 switch (type) {
737 case EFI_TIMER_STOP:
738 event->trigger_next = -1ULL;
739 break;
740 case EFI_TIMER_PERIODIC:
741 case EFI_TIMER_RELATIVE:
742 event->trigger_next = timer_get_us() + trigger_time;
743 break;
744 default:
745 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100746 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100747 event->trigger_type = type;
748 event->trigger_time = trigger_time;
749 event->is_signaled = false;
750 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100751}
752
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200753/**
Mario Six8fac2912018-07-10 08:40:17 +0200754 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
755 * event
756 * @event: event for which the timer is set
757 * @type: type of the timer
758 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200759 *
760 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200761 *
Mario Six8fac2912018-07-10 08:40:17 +0200762 * See the Unified Extensible Firmware Interface (UEFI) specification for
763 * details.
764 *
765 *
766 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200767 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200768static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
769 enum efi_timer_delay type,
770 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200771{
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100772 EFI_ENTRY("%p, %d, %" PRIx64, event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200773 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
774}
775
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200776/**
Mario Six8fac2912018-07-10 08:40:17 +0200777 * efi_wait_for_event() - wait for events to be signaled
778 * @num_events: number of events to be waited for
779 * @event: events to be waited for
780 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200781 *
782 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200783 *
Mario Six8fac2912018-07-10 08:40:17 +0200784 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * details.
786 *
787 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200788 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100789static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200790 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100791 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100792{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100793 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100794
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100795 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100796
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200797 /* Check parameters */
798 if (!num_events || !event)
799 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200800 /* Check TPL */
801 if (efi_tpl != TPL_APPLICATION)
802 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200803 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100804 if (efi_is_event(event[i]) != EFI_SUCCESS)
805 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200806 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
807 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200808 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100809 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200810 }
811
812 /* Wait for signal */
813 for (;;) {
814 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200815 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200816 goto out;
817 }
818 /* Allow events to occur. */
819 efi_timer_check();
820 }
821
822out:
823 /*
824 * Reset the signal which is passed to the caller to allow periodic
825 * events to occur.
826 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200827 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200828 if (index)
829 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100830
831 return EFI_EXIT(EFI_SUCCESS);
832}
833
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200834/**
Mario Six8fac2912018-07-10 08:40:17 +0200835 * efi_signal_event_ext() - signal an EFI event
836 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200837 *
838 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200839 *
840 * See the Unified Extensible Firmware Interface (UEFI) specification for
841 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200842 *
843 * This functions sets the signaled state of the event and queues the
844 * notification function for execution.
845 *
Mario Six8fac2912018-07-10 08:40:17 +0200846 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200847 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200848static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100849{
850 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100851 if (efi_is_event(event) != EFI_SUCCESS)
852 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100853 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100854 return EFI_EXIT(EFI_SUCCESS);
855}
856
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200857/**
Mario Six8fac2912018-07-10 08:40:17 +0200858 * efi_close_event() - close an EFI event
859 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200860 *
861 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200862 *
Mario Six8fac2912018-07-10 08:40:17 +0200863 * See the Unified Extensible Firmware Interface (UEFI) specification for
864 * details.
865 *
866 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200867 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200868static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100869{
870 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100871 if (efi_is_event(event) != EFI_SUCCESS)
872 return EFI_EXIT(EFI_INVALID_PARAMETER);
873 list_del(&event->link);
874 free(event);
875 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100876}
877
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200878/**
Mario Six8fac2912018-07-10 08:40:17 +0200879 * efi_check_event() - check if an event is signaled
880 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200881 *
882 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200883 *
884 * See the Unified Extensible Firmware Interface (UEFI) specification for
885 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 *
Mario Six8fac2912018-07-10 08:40:17 +0200887 * If an event is not signaled yet, the notification function is queued. The
888 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200889 *
Mario Six8fac2912018-07-10 08:40:17 +0200890 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200892static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100893{
894 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200895 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100896 if (efi_is_event(event) != EFI_SUCCESS ||
897 event->type & EVT_NOTIFY_SIGNAL)
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
899 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100900 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100901 if (event->is_signaled) {
902 event->is_signaled = false;
903 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200904 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100905 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100906}
907
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200908/**
Mario Six8fac2912018-07-10 08:40:17 +0200909 * efi_search_obj() - find the internal EFI object for a handle
910 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200911 *
Mario Six8fac2912018-07-10 08:40:17 +0200912 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200913 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100914struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200915{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100916 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200917
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100918 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200919 if (efiobj->handle == handle)
920 return efiobj;
921 }
922
923 return NULL;
924}
925
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200926/**
Mario Six8fac2912018-07-10 08:40:17 +0200927 * efi_open_protocol_info_entry() - create open protocol info entry and add it
928 * to a protocol
929 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100930 *
Mario Six8fac2912018-07-10 08:40:17 +0200931 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100932 */
933static struct efi_open_protocol_info_entry *efi_create_open_info(
934 struct efi_handler *handler)
935{
936 struct efi_open_protocol_info_item *item;
937
938 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
939 if (!item)
940 return NULL;
941 /* Append the item to the open protocol info list. */
942 list_add_tail(&item->link, &handler->open_infos);
943
944 return &item->info;
945}
946
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200947/**
Mario Six8fac2912018-07-10 08:40:17 +0200948 * efi_delete_open_info() - remove an open protocol info entry from a protocol
949 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100950 *
Mario Six8fac2912018-07-10 08:40:17 +0200951 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100952 */
953static efi_status_t efi_delete_open_info(
954 struct efi_open_protocol_info_item *item)
955{
956 list_del(&item->link);
957 free(item);
958 return EFI_SUCCESS;
959}
960
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200961/**
Mario Six8fac2912018-07-10 08:40:17 +0200962 * efi_add_protocol() - install new protocol on a handle
963 * @handle: handle on which the protocol shall be installed
964 * @protocol: GUID of the protocol to be installed
965 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200966 *
Mario Six8fac2912018-07-10 08:40:17 +0200967 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200968 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100969efi_status_t efi_add_protocol(const efi_handle_t handle,
970 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200971 void *protocol_interface)
972{
973 struct efi_object *efiobj;
974 struct efi_handler *handler;
975 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200976
977 efiobj = efi_search_obj(handle);
978 if (!efiobj)
979 return EFI_INVALID_PARAMETER;
980 ret = efi_search_protocol(handle, protocol, NULL);
981 if (ret != EFI_NOT_FOUND)
982 return EFI_INVALID_PARAMETER;
983 handler = calloc(1, sizeof(struct efi_handler));
984 if (!handler)
985 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100986 handler->guid = protocol;
987 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100988 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100989 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +0100990 if (!guidcmp(&efi_guid_device_path, protocol))
991 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100992 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200993}
994
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200995/**
Mario Six8fac2912018-07-10 08:40:17 +0200996 * efi_install_protocol_interface() - install protocol interface
997 * @handle: handle on which the protocol shall be installed
998 * @protocol: GUID of the protocol to be installed
999 * @protocol_interface_type: type of the interface to be installed,
1000 * always EFI_NATIVE_INTERFACE
1001 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001002 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001003 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001004 *
Mario Six8fac2912018-07-10 08:40:17 +02001005 * See the Unified Extensible Firmware Interface (UEFI) specification for
1006 * details.
1007 *
1008 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001009 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001010static efi_status_t EFIAPI efi_install_protocol_interface(
1011 void **handle, const efi_guid_t *protocol,
1012 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001013{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001014 efi_status_t r;
1015
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001016 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1017 protocol_interface);
1018
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001019 if (!handle || !protocol ||
1020 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1021 r = EFI_INVALID_PARAMETER;
1022 goto out;
1023 }
1024
1025 /* Create new handle if requested. */
1026 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001027 r = efi_create_handle(handle);
1028 if (r != EFI_SUCCESS)
1029 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001030 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1031 *handle);
1032 } else {
1033 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1034 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001035 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001036 /* Add new protocol */
1037 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001038out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001039 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001040}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001041
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001042/**
Mario Six8fac2912018-07-10 08:40:17 +02001043 * efi_get_drivers() - get all drivers associated to a controller
1044 * @efiobj: handle of the controller
1045 * @protocol: protocol guid (optional)
1046 * @number_of_drivers: number of child controllers
1047 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001048 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001049 * The allocated buffer has to be freed with free().
1050 *
Mario Six8fac2912018-07-10 08:40:17 +02001051 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001052 */
1053static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1054 const efi_guid_t *protocol,
1055 efi_uintn_t *number_of_drivers,
1056 efi_handle_t **driver_handle_buffer)
1057{
1058 struct efi_handler *handler;
1059 struct efi_open_protocol_info_item *item;
1060 efi_uintn_t count = 0, i;
1061 bool duplicate;
1062
1063 /* Count all driver associations */
1064 list_for_each_entry(handler, &efiobj->protocols, link) {
1065 if (protocol && guidcmp(handler->guid, protocol))
1066 continue;
1067 list_for_each_entry(item, &handler->open_infos, link) {
1068 if (item->info.attributes &
1069 EFI_OPEN_PROTOCOL_BY_DRIVER)
1070 ++count;
1071 }
1072 }
1073 /*
1074 * Create buffer. In case of duplicate driver assignments the buffer
1075 * will be too large. But that does not harm.
1076 */
1077 *number_of_drivers = 0;
1078 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1079 if (!*driver_handle_buffer)
1080 return EFI_OUT_OF_RESOURCES;
1081 /* Collect unique driver handles */
1082 list_for_each_entry(handler, &efiobj->protocols, link) {
1083 if (protocol && guidcmp(handler->guid, protocol))
1084 continue;
1085 list_for_each_entry(item, &handler->open_infos, link) {
1086 if (item->info.attributes &
1087 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1088 /* Check this is a new driver */
1089 duplicate = false;
1090 for (i = 0; i < *number_of_drivers; ++i) {
1091 if ((*driver_handle_buffer)[i] ==
1092 item->info.agent_handle)
1093 duplicate = true;
1094 }
1095 /* Copy handle to buffer */
1096 if (!duplicate) {
1097 i = (*number_of_drivers)++;
1098 (*driver_handle_buffer)[i] =
1099 item->info.agent_handle;
1100 }
1101 }
1102 }
1103 }
1104 return EFI_SUCCESS;
1105}
1106
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001107/**
Mario Six8fac2912018-07-10 08:40:17 +02001108 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1109 * @efiobj: handle of the controller
1110 * @protocol: protocol guid (optional)
1111 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001112 *
1113 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001114 *
1115 * See the Unified Extensible Firmware Interface (UEFI) specification for
1116 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001117 *
Mario Six8fac2912018-07-10 08:40:17 +02001118 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001119 */
1120static efi_status_t efi_disconnect_all_drivers(
1121 struct efi_object *efiobj,
1122 const efi_guid_t *protocol,
1123 efi_handle_t child_handle)
1124{
1125 efi_uintn_t number_of_drivers;
1126 efi_handle_t *driver_handle_buffer;
1127 efi_status_t r, ret;
1128
1129 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1130 &driver_handle_buffer);
1131 if (ret != EFI_SUCCESS)
1132 return ret;
1133
1134 ret = EFI_NOT_FOUND;
1135 while (number_of_drivers) {
1136 r = EFI_CALL(efi_disconnect_controller(
1137 efiobj->handle,
1138 driver_handle_buffer[--number_of_drivers],
1139 child_handle));
1140 if (r == EFI_SUCCESS)
1141 ret = r;
1142 }
1143 free(driver_handle_buffer);
1144 return ret;
1145}
1146
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001147/**
Mario Six8fac2912018-07-10 08:40:17 +02001148 * efi_uninstall_protocol_interface() - uninstall protocol interface
1149 * @handle: handle from which the protocol shall be removed
1150 * @protocol: GUID of the protocol to be removed
1151 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001152 *
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001153 * This function implements the UninstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001154 *
Mario Six8fac2912018-07-10 08:40:17 +02001155 * See the Unified Extensible Firmware Interface (UEFI) specification for
1156 * details.
1157 *
1158 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001159 */
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001160static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001161 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001162 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001163{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001164 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001165 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001166 struct efi_open_protocol_info_item *item;
1167 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001168 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001169
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001170 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1171
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001172 /* Check handle */
1173 efiobj = efi_search_obj(handle);
1174 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001175 r = EFI_INVALID_PARAMETER;
1176 goto out;
1177 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001178 /* Find the protocol on the handle */
1179 r = efi_search_protocol(handle, protocol, &handler);
1180 if (r != EFI_SUCCESS)
1181 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001182 /* Disconnect controllers */
1183 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1184 if (!list_empty(&handler->open_infos)) {
1185 r = EFI_ACCESS_DENIED;
1186 goto out;
1187 }
1188 /* Close protocol */
1189 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1190 if (item->info.attributes ==
1191 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1192 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1193 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1194 list_del(&item->link);
1195 }
1196 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001197 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001198 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001199 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001200 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001201out:
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01001202 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001203}
1204
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001205/**
Mario Six8fac2912018-07-10 08:40:17 +02001206 * efi_register_protocol_notify() - register an event for notification when a
1207 * protocol is installed.
1208 * @protocol: GUID of the protocol whose installation shall be notified
1209 * @event: event to be signaled upon installation of the protocol
1210 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001211 *
1212 * This function implements the RegisterProtocolNotify service.
1213 * See the Unified Extensible Firmware Interface (UEFI) specification
1214 * for details.
1215 *
Mario Six8fac2912018-07-10 08:40:17 +02001216 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001217 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001218static efi_status_t EFIAPI efi_register_protocol_notify(
1219 const efi_guid_t *protocol,
1220 struct efi_event *event,
1221 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001222{
Rob Clark238f88c2017-09-13 18:05:41 -04001223 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001224 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1225}
1226
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001227/**
Mario Six8fac2912018-07-10 08:40:17 +02001228 * efi_search() - determine if an EFI handle implements a protocol
1229 * @search_type: selection criterion
1230 * @protocol: GUID of the protocol
1231 * @search_key: registration key
1232 * @efiobj: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001233 *
1234 * See the documentation of the LocateHandle service in the UEFI specification.
1235 *
Mario Six8fac2912018-07-10 08:40:17 +02001236 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001237 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001238static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001239 const efi_guid_t *protocol, void *search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01001240 struct efi_object *efiobj)
1241{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001242 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001243
1244 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001245 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001246 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001247 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001248 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001249 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001250 case BY_PROTOCOL:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001251 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1252 return (ret != EFI_SUCCESS);
1253 default:
1254 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001255 return -1;
1256 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001257}
1258
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001259/**
Mario Six8fac2912018-07-10 08:40:17 +02001260 * efi_locate_handle() - locate handles implementing a protocol
1261 * @search_type: selection criterion
1262 * @protocol: GUID of the protocol
1263 * @search_key: registration key
1264 * @buffer_size: size of the buffer to receive the handles in bytes
1265 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001266 *
1267 * This function is meant for U-Boot internal calls. For the API implementation
1268 * of the LocateHandle service see efi_locate_handle_ext.
1269 *
Mario Six8fac2912018-07-10 08:40:17 +02001270 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001271 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001272static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001273 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001274 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001275 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001276{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001277 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001278 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001279
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001280 /* Check parameters */
1281 switch (search_type) {
1282 case ALL_HANDLES:
1283 break;
1284 case BY_REGISTER_NOTIFY:
1285 if (!search_key)
1286 return EFI_INVALID_PARAMETER;
1287 /* RegisterProtocolNotify is not implemented yet */
1288 return EFI_UNSUPPORTED;
1289 case BY_PROTOCOL:
1290 if (!protocol)
1291 return EFI_INVALID_PARAMETER;
1292 break;
1293 default:
1294 return EFI_INVALID_PARAMETER;
1295 }
1296
1297 /*
1298 * efi_locate_handle_buffer uses this function for
1299 * the calculation of the necessary buffer size.
1300 * So do not require a buffer for buffersize == 0.
1301 */
1302 if (!buffer_size || (*buffer_size && !buffer))
1303 return EFI_INVALID_PARAMETER;
1304
Alexander Grafc15d9212016-03-04 01:09:59 +01001305 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001306 list_for_each_entry(efiobj, &efi_obj_list, link) {
1307 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001308 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001309 }
1310
1311 if (*buffer_size < size) {
1312 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001313 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001314 }
1315
Rob Clarkcdee3372017-08-06 14:10:07 -04001316 *buffer_size = size;
1317 if (size == 0)
1318 return EFI_NOT_FOUND;
1319
Alexander Grafc15d9212016-03-04 01:09:59 +01001320 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001321 list_for_each_entry(efiobj, &efi_obj_list, link) {
1322 if (!efi_search(search_type, protocol, search_key, efiobj))
1323 *buffer++ = efiobj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001324 }
1325
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001326 return EFI_SUCCESS;
1327}
1328
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001329/**
Mario Six8fac2912018-07-10 08:40:17 +02001330 * efi_locate_handle_ext() - 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 Schuchardta40db6e2017-09-21 18:30:11 +02001336 *
1337 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001338 *
Mario Six8fac2912018-07-10 08:40:17 +02001339 * See the Unified Extensible Firmware Interface (UEFI) specification for
1340 * details.
1341 *
1342 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001343 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001344static efi_status_t EFIAPI efi_locate_handle_ext(
1345 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001346 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001347 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001348{
Rob Clark238f88c2017-09-13 18:05:41 -04001349 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001350 buffer_size, buffer);
1351
1352 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1353 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001354}
1355
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001356/**
Mario Six8fac2912018-07-10 08:40:17 +02001357 * efi_remove_configuration_table() - collapses configuration table entries,
1358 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001359 *
Mario Six8fac2912018-07-10 08:40:17 +02001360 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001361 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001362static void efi_remove_configuration_table(int i)
1363{
1364 struct efi_configuration_table *this = &efi_conf_table[i];
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001365 struct efi_configuration_table *next = &efi_conf_table[i + 1];
Alexander Graffe3366f2017-07-26 13:41:04 +02001366 struct efi_configuration_table *end = &efi_conf_table[systab.nr_tables];
1367
1368 memmove(this, next, (ulong)end - (ulong)next);
1369 systab.nr_tables--;
1370}
1371
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001372/**
Mario Six8fac2912018-07-10 08:40:17 +02001373 * efi_install_configuration_table() - adds, updates, or removes a
1374 * configuration table
1375 * @guid: GUID of the installed table
1376 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001377 *
1378 * This function is used for internal calls. For the API implementation of the
1379 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1380 *
Mario Six8fac2912018-07-10 08:40:17 +02001381 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001382 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001383efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1384 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001385{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001386 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001387 int i;
1388
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001389 if (!guid)
1390 return EFI_INVALID_PARAMETER;
1391
Alexander Grafc15d9212016-03-04 01:09:59 +01001392 /* Check for guid override */
1393 for (i = 0; i < systab.nr_tables; i++) {
1394 if (!guidcmp(guid, &efi_conf_table[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001395 if (table)
1396 efi_conf_table[i].table = table;
1397 else
1398 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001399 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001400 }
1401 }
1402
Alexander Graffe3366f2017-07-26 13:41:04 +02001403 if (!table)
1404 return EFI_NOT_FOUND;
1405
Alexander Grafc15d9212016-03-04 01:09:59 +01001406 /* No override, check for overflow */
1407 if (i >= ARRAY_SIZE(efi_conf_table))
Alexander Grafc5c11632016-08-19 01:23:24 +02001408 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001409
1410 /* Add a new entry */
1411 memcpy(&efi_conf_table[i].guid, guid, sizeof(*guid));
1412 efi_conf_table[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001413 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001414
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001415out:
1416 /* Notify that the configuration table was changed */
1417 list_for_each_entry(evt, &efi_events, link) {
1418 if (evt->group && !guidcmp(evt->group, guid)) {
1419 efi_signal_event(evt, false);
1420 break;
1421 }
1422 }
1423
Alexander Grafc5c11632016-08-19 01:23:24 +02001424 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001425}
1426
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001427/**
Mario Six8fac2912018-07-10 08:40:17 +02001428 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1429 * configuration table.
1430 * @guid: GUID of the installed table
1431 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001432 *
1433 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001434 *
Mario Six8fac2912018-07-10 08:40:17 +02001435 * See the Unified Extensible Firmware Interface (UEFI) specification for
1436 * details.
1437 *
1438 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001439 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001440static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1441 void *table)
1442{
Rob Clark238f88c2017-09-13 18:05:41 -04001443 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001444 return EFI_EXIT(efi_install_configuration_table(guid, table));
1445}
1446
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001447/**
Mario Six8fac2912018-07-10 08:40:17 +02001448 * efi_setup_loaded_image() - initialize a loaded image
1449 * @info: loaded image info to be passed to the entry point of the image
1450 * @obj: internal object associated with the loaded image
1451 * @device_path: device path of the loaded image
1452 * @file_path: file path of the loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001453 *
1454 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001455 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001456 *
Mario Six8fac2912018-07-10 08:40:17 +02001457 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001458 */
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001459efi_status_t efi_setup_loaded_image(
1460 struct efi_loaded_image *info, struct efi_object *obj,
1461 struct efi_device_path *device_path,
1462 struct efi_device_path *file_path)
Rob Clarkf8db9222017-09-13 18:05:33 -04001463{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001464 efi_status_t ret;
1465
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001466 /* Add internal object to object list */
1467 efi_add_handle(obj);
1468 /* efi_exit() assumes that the handle points to the info */
Rob Clarkf8db9222017-09-13 18:05:33 -04001469 obj->handle = info;
1470
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001471 info->file_path = file_path;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001472
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001473 if (device_path) {
1474 info->device_handle = efi_dp_find_obj(device_path, NULL);
1475 /*
1476 * When asking for the device path interface, return
1477 * bootefi_device_path
1478 */
1479 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1480 device_path);
1481 if (ret != EFI_SUCCESS)
1482 goto failure;
1483 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001484
1485 /*
1486 * When asking for the loaded_image interface, just
1487 * return handle which points to loaded_image_info
1488 */
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001489 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1490 if (ret != EFI_SUCCESS)
1491 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001492
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001493 ret = efi_add_protocol(obj->handle,
1494 &efi_guid_device_path_to_text_protocol,
1495 (void *)&efi_device_path_to_text);
1496 if (ret != EFI_SUCCESS)
1497 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001498
Leif Lindholmb6e6fdc2018-03-09 17:43:21 +01001499 ret = efi_add_protocol(obj->handle,
1500 &efi_guid_device_path_utilities_protocol,
1501 (void *)&efi_device_path_utilities);
1502 if (ret != EFI_SUCCESS)
1503 goto failure;
1504
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001505 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001506failure:
1507 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001508 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001509}
1510
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001511/**
Mario Six8fac2912018-07-10 08:40:17 +02001512 * efi_load_image_from_path() - load an image using a file path
1513 * @file_path: the path of the image to load
1514 * @buffer: buffer containing the loaded image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001515 *
Mario Six8fac2912018-07-10 08:40:17 +02001516 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001517 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001518efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1519 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001520{
1521 struct efi_file_info *info = NULL;
1522 struct efi_file_handle *f;
1523 static efi_status_t ret;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001524 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001525
1526 f = efi_file_from_path(file_path);
1527 if (!f)
1528 return EFI_DEVICE_ERROR;
1529
1530 bs = 0;
1531 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1532 &bs, info));
1533 if (ret == EFI_BUFFER_TOO_SMALL) {
1534 info = malloc(bs);
1535 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1536 &bs, info));
1537 }
1538 if (ret != EFI_SUCCESS)
1539 goto error;
1540
1541 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1542 if (ret)
1543 goto error;
1544
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001545 bs = info->file_size;
1546 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark857a1222017-09-13 18:05:35 -04001547
1548error:
1549 free(info);
1550 EFI_CALL(f->close(f));
1551
1552 if (ret != EFI_SUCCESS) {
1553 efi_free_pool(*buffer);
1554 *buffer = NULL;
1555 }
1556
1557 return ret;
1558}
1559
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001560/**
Mario Six8fac2912018-07-10 08:40:17 +02001561 * efi_load_image() - load an EFI image into memory
1562 * @boot_policy: true for request originating from the boot manager
1563 * @parent_image: the caller's image handle
1564 * @file_path: the path of the image to load
1565 * @source_buffer: memory location from which the image is installed
1566 * @source_size: size of the memory area from which the image is installed
1567 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001568 *
1569 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001570 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001571 * See the Unified Extensible Firmware Interface (UEFI) specification
1572 * for details.
1573 *
Mario Six8fac2912018-07-10 08:40:17 +02001574 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001575 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001576static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1577 efi_handle_t parent_image,
1578 struct efi_device_path *file_path,
1579 void *source_buffer,
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001580 efi_uintn_t source_size,
Alexander Grafc15d9212016-03-04 01:09:59 +01001581 efi_handle_t *image_handle)
1582{
Alexander Grafc15d9212016-03-04 01:09:59 +01001583 struct efi_loaded_image *info;
1584 struct efi_object *obj;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001585 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001586
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001587 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001588 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001589
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001590 if (!image_handle || !parent_image) {
1591 ret = EFI_INVALID_PARAMETER;
1592 goto error;
1593 }
1594
1595 if (!source_buffer && !file_path) {
1596 ret = EFI_NOT_FOUND;
1597 goto error;
1598 }
1599
Rob Clark857a1222017-09-13 18:05:35 -04001600 info = calloc(1, sizeof(*info));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001601 if (!info) {
1602 ret = EFI_OUT_OF_RESOURCES;
1603 goto error;
1604 }
Rob Clark857a1222017-09-13 18:05:35 -04001605 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001606 if (!obj) {
1607 free(info);
1608 ret = EFI_OUT_OF_RESOURCES;
1609 goto error;
1610 }
Rob Clark857a1222017-09-13 18:05:35 -04001611
1612 if (!source_buffer) {
1613 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001614
Rob Clarkc84c1102017-09-13 18:05:38 -04001615 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001616 if (ret != EFI_SUCCESS)
1617 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001618 /*
1619 * split file_path which contains both the device and
1620 * file parts:
1621 */
1622 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001623 ret = efi_setup_loaded_image(info, obj, dp, fp);
1624 if (ret != EFI_SUCCESS)
1625 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001626 } else {
1627 /* In this case, file_path is the "device" path, ie.
1628 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1629 */
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001630 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1631 if (ret != EFI_SUCCESS)
1632 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001633 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001634 info->reserved = efi_load_pe(source_buffer, info);
1635 if (!info->reserved) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001636 ret = EFI_UNSUPPORTED;
1637 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001638 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001639 info->system_table = &systab;
1640 info->parent_handle = parent_image;
Heinrich Schuchardt754d4972017-11-26 14:05:22 +01001641 *image_handle = obj->handle;
Alexander Grafc15d9212016-03-04 01:09:59 +01001642 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001643failure:
1644 free(info);
1645 efi_delete_handle(obj);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001646error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001647 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001648}
1649
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001650/**
Mario Six8fac2912018-07-10 08:40:17 +02001651 * efi_start_image() - dall the entry point of an image
1652 * @image_handle: handle of the image
1653 * @exit_data_size: size of the buffer
1654 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001655 *
1656 * This function implements the StartImage service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001657 *
Mario Six8fac2912018-07-10 08:40:17 +02001658 * See the Unified Extensible Firmware Interface (UEFI) specification for
1659 * details.
1660 *
1661 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001662 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001663static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1664 unsigned long *exit_data_size,
1665 s16 **exit_data)
1666{
Alexander Graf54d8cdf2018-01-24 00:18:08 +01001667 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1668 struct efi_system_table *st);
Alexander Grafc15d9212016-03-04 01:09:59 +01001669 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001670 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001671
1672 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1673 entry = info->reserved;
1674
1675 efi_is_direct_boot = false;
1676
1677 /* call the image! */
Alexander Graf988c0662016-05-20 23:28:23 +02001678 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001679 /*
1680 * We called the entry point of the child image with EFI_CALL
1681 * in the lines below. The child image called the Exit() boot
1682 * service efi_exit() which executed the long jump that brought
1683 * us to the current line. This implies that the second half
1684 * of the EFI_CALL macro has not been executed.
1685 */
1686#ifdef CONFIG_ARM
1687 /*
1688 * efi_exit() called efi_restore_gd(). We have to undo this
1689 * otherwise __efi_entry_check() will put the wrong value into
1690 * app_gd.
1691 */
1692 gd = app_gd;
1693#endif
1694 /*
1695 * To get ready to call EFI_EXIT below we have to execute the
1696 * missed out steps of EFI_CALL.
1697 */
1698 assert(__efi_entry_check());
1699 debug("%sEFI: %lu returned by started image\n",
1700 __efi_nesting_dec(),
1701 (unsigned long)((uintptr_t)info->exit_status &
1702 ~EFI_ERROR_MASK));
Alexander Graf988c0662016-05-20 23:28:23 +02001703 return EFI_EXIT(info->exit_status);
1704 }
1705
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001706 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001707
Alexander Grafeff317c2018-01-26 00:47:53 +01001708 /*
1709 * Usually UEFI applications call Exit() instead of returning.
1710 * But because the world doesn not consist of ponies and unicorns,
1711 * we're happy to emulate that behavior on behalf of a payload
1712 * that forgot.
1713 */
1714 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001715}
1716
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001717/**
Mario Six8fac2912018-07-10 08:40:17 +02001718 * efi_exit() - leave an EFI application or driver
1719 * @image_handle: handle of the application or driver that is exiting
1720 * @exit_status: status code
1721 * @exit_data_size: size of the buffer in bytes
1722 * @exit_data: buffer with data describing an error
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001723 *
1724 * This function implements the Exit service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001725 *
Mario Six8fac2912018-07-10 08:40:17 +02001726 * See the Unified Extensible Firmware Interface (UEFI) specification for
1727 * details.
1728 *
1729 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001730 */
Alexander Graf988c0662016-05-20 23:28:23 +02001731static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001732 efi_status_t exit_status,
1733 unsigned long exit_data_size,
1734 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001735{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001736 /*
1737 * We require that the handle points to the original loaded
1738 * image protocol interface.
1739 *
1740 * For getting the longjmp address this is safer than locating
1741 * the protocol because the protocol may have been reinstalled
1742 * pointing to another memory location.
1743 *
1744 * TODO: We should call the unload procedure of the loaded
1745 * image protocol.
1746 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001747 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001748
Alexander Grafc15d9212016-03-04 01:09:59 +01001749 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1750 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001751
Alexander Graf87e787a2017-09-03 14:14:17 +02001752 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001753 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001754
Alexander Graf87e787a2017-09-03 14:14:17 +02001755 /*
1756 * But longjmp out with the U-Boot gd, not the application's, as
1757 * the other end is a setjmp call inside EFI context.
1758 */
1759 efi_restore_gd();
1760
Alexander Graf988c0662016-05-20 23:28:23 +02001761 loaded_image_info->exit_status = exit_status;
Alexander Graf9d006b72016-09-27 09:30:32 +02001762 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001763
1764 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001765}
1766
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001767/**
Mario Six8fac2912018-07-10 08:40:17 +02001768 * efi_unload_image() - unload an EFI image
1769 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001770 *
1771 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001772 *
1773 * See the Unified Extensible Firmware Interface (UEFI) specification for
1774 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001775 *
Mario Six8fac2912018-07-10 08:40:17 +02001776 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001777 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001778static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001779{
1780 struct efi_object *efiobj;
1781
1782 EFI_ENTRY("%p", image_handle);
1783 efiobj = efi_search_obj(image_handle);
1784 if (efiobj)
1785 list_del(&efiobj->link);
1786
1787 return EFI_EXIT(EFI_SUCCESS);
1788}
1789
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001790/**
Mario Six8fac2912018-07-10 08:40:17 +02001791 * efi_exit_caches() - fix up caches for EFI payloads if necessary
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001792 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001793static void efi_exit_caches(void)
1794{
1795#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1796 /*
1797 * Grub on 32bit ARM needs to have caches disabled before jumping into
1798 * a zImage, but does not know of all cache layers. Give it a hand.
1799 */
1800 if (efi_is_direct_boot)
1801 cleanup_before_linux();
1802#endif
1803}
1804
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001805/**
Mario Six8fac2912018-07-10 08:40:17 +02001806 * efi_exit_boot_services() - stop all boot services
1807 * @image_handle: handle of the loaded image
1808 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001809 *
1810 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001811 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001812 * See the Unified Extensible Firmware Interface (UEFI) specification
1813 * for details.
1814 *
Mario Six8fac2912018-07-10 08:40:17 +02001815 * All timer events are disabled. For exit boot services events the
1816 * notification function is called. The boot services are disabled in the
1817 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001818 *
Mario Six8fac2912018-07-10 08:40:17 +02001819 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001820 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001821static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001822 unsigned long map_key)
1823{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001824 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001825
Alexander Grafc15d9212016-03-04 01:09:59 +01001826 EFI_ENTRY("%p, %ld", image_handle, map_key);
1827
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001828 /* Make sure that notification functions are not called anymore */
1829 efi_tpl = TPL_HIGH_LEVEL;
1830
1831 /* Check if ExitBootServices has already been called */
1832 if (!systab.boottime)
1833 return EFI_EXIT(EFI_SUCCESS);
1834
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001835 /* Add related events to the event group */
1836 list_for_each_entry(evt, &efi_events, link) {
1837 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1838 evt->group = &efi_guid_event_group_exit_boot_services;
1839 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001840 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001841 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001842 if (evt->group &&
1843 !guidcmp(evt->group,
1844 &efi_guid_event_group_exit_boot_services)) {
1845 efi_signal_event(evt, false);
1846 break;
1847 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001848 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001849
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001850 /* TODO Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001851
Alexander Graf2ebeb442016-11-17 01:02:57 +01001852 board_quiesce_devices();
1853
Alexander Grafc15d9212016-03-04 01:09:59 +01001854 /* Fix up caches for EFI payloads if necessary */
1855 efi_exit_caches();
1856
1857 /* This stops all lingering devices */
1858 bootm_disable_interrupts();
1859
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001860 /* Disable boottime services */
1861 systab.con_in_handle = NULL;
1862 systab.con_in = NULL;
1863 systab.con_out_handle = NULL;
1864 systab.con_out = NULL;
1865 systab.stderr_handle = NULL;
1866 systab.std_err = NULL;
1867 systab.boottime = NULL;
1868
1869 /* Recalculate CRC32 */
1870 systab.hdr.crc32 = 0;
1871 systab.hdr.crc32 = crc32(0, (const unsigned char *)&systab,
1872 sizeof(struct efi_system_table));
1873
Alexander Grafc15d9212016-03-04 01:09:59 +01001874 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001875 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001876 WATCHDOG_RESET();
1877
1878 return EFI_EXIT(EFI_SUCCESS);
1879}
1880
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001881/**
Mario Six8fac2912018-07-10 08:40:17 +02001882 * efi_get_next_monotonic_count() - get next value of the counter
1883 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001884 *
1885 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001886 *
1887 * See the Unified Extensible Firmware Interface (UEFI) specification for
1888 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001889 *
Mario Six8fac2912018-07-10 08:40:17 +02001890 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001891 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001892static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1893{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001894 static uint64_t mono;
1895
Alexander Grafc15d9212016-03-04 01:09:59 +01001896 EFI_ENTRY("%p", count);
1897 *count = mono++;
1898 return EFI_EXIT(EFI_SUCCESS);
1899}
1900
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001901/**
Mario Six8fac2912018-07-10 08:40:17 +02001902 * efi_stall() - sleep
1903 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001904 *
Mario Six8fac2912018-07-10 08:40:17 +02001905 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001906 *
Mario Six8fac2912018-07-10 08:40:17 +02001907 * See the Unified Extensible Firmware Interface (UEFI) specification for
1908 * details.
1909 *
1910 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001911 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001912static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1913{
1914 EFI_ENTRY("%ld", microseconds);
1915 udelay(microseconds);
1916 return EFI_EXIT(EFI_SUCCESS);
1917}
1918
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001919/**
Mario Six8fac2912018-07-10 08:40:17 +02001920 * efi_set_watchdog_timer() - reset the watchdog timer
1921 * @timeout: seconds before reset by watchdog
1922 * @watchdog_code: code to be logged when resetting
1923 * @data_size: size of buffer in bytes
1924 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001925 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001926 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001927 *
Mario Six8fac2912018-07-10 08:40:17 +02001928 * See the Unified Extensible Firmware Interface (UEFI) specification for
1929 * details.
1930 *
1931 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001932 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001933static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1934 uint64_t watchdog_code,
1935 unsigned long data_size,
1936 uint16_t *watchdog_data)
1937{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001938 EFI_ENTRY("%ld, 0x%" PRIx64 ", %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001939 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001940 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001941}
1942
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001943/**
Mario Six8fac2912018-07-10 08:40:17 +02001944 * efi_close_protocol() - close a protocol
1945 * @handle: handle on which the protocol shall be closed
1946 * @protocol: GUID of the protocol to close
1947 * @agent_handle: handle of the driver
1948 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001949 *
1950 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001951 *
Mario Six8fac2912018-07-10 08:40:17 +02001952 * See the Unified Extensible Firmware Interface (UEFI) specification for
1953 * details.
1954 *
1955 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001956 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001957static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001958 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001959 efi_handle_t agent_handle,
1960 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001961{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001962 struct efi_handler *handler;
1963 struct efi_open_protocol_info_item *item;
1964 struct efi_open_protocol_info_item *pos;
1965 efi_status_t r;
1966
Rob Clark238f88c2017-09-13 18:05:41 -04001967 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001968 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001969
1970 if (!agent_handle) {
1971 r = EFI_INVALID_PARAMETER;
1972 goto out;
1973 }
1974 r = efi_search_protocol(handle, protocol, &handler);
1975 if (r != EFI_SUCCESS)
1976 goto out;
1977
1978 r = EFI_NOT_FOUND;
1979 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1980 if (item->info.agent_handle == agent_handle &&
1981 item->info.controller_handle == controller_handle) {
1982 efi_delete_open_info(item);
1983 r = EFI_SUCCESS;
1984 break;
1985 }
1986 }
1987out:
1988 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001989}
1990
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001991/**
Mario Six8fac2912018-07-10 08:40:17 +02001992 * efi_open_protocol_information() - provide information about then open status
1993 * of a protocol on a handle
1994 * @handle: handle for which the information shall be retrieved
1995 * @protocol: GUID of the protocol
1996 * @entry_buffer: buffer to receive the open protocol information
1997 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001998 *
1999 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002000 *
2001 * See the Unified Extensible Firmware Interface (UEFI) specification for
2002 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002003 *
Mario Six8fac2912018-07-10 08:40:17 +02002004 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002005 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002006static efi_status_t EFIAPI efi_open_protocol_information(
2007 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002008 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002009 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002010{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002011 unsigned long buffer_size;
2012 unsigned long count;
2013 struct efi_handler *handler;
2014 struct efi_open_protocol_info_item *item;
2015 efi_status_t r;
2016
Rob Clark238f88c2017-09-13 18:05:41 -04002017 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002018 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002019
2020 /* Check parameters */
2021 if (!entry_buffer) {
2022 r = EFI_INVALID_PARAMETER;
2023 goto out;
2024 }
2025 r = efi_search_protocol(handle, protocol, &handler);
2026 if (r != EFI_SUCCESS)
2027 goto out;
2028
2029 /* Count entries */
2030 count = 0;
2031 list_for_each_entry(item, &handler->open_infos, link) {
2032 if (item->info.open_count)
2033 ++count;
2034 }
2035 *entry_count = count;
2036 *entry_buffer = NULL;
2037 if (!count) {
2038 r = EFI_SUCCESS;
2039 goto out;
2040 }
2041
2042 /* Copy entries */
2043 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2044 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2045 (void **)entry_buffer);
2046 if (r != EFI_SUCCESS)
2047 goto out;
2048 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2049 if (item->info.open_count)
2050 (*entry_buffer)[--count] = item->info;
2051 }
2052out:
2053 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002054}
2055
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002056/**
Mario Six8fac2912018-07-10 08:40:17 +02002057 * efi_protocols_per_handle() - get protocols installed on a handle
2058 * @handle: handle for which the information is retrieved
2059 * @protocol_buffer: buffer with protocol GUIDs
2060 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002061 *
2062 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002063 *
Mario Six8fac2912018-07-10 08:40:17 +02002064 * See the Unified Extensible Firmware Interface (UEFI) specification for
2065 * details.
2066 *
2067 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002068 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002069static efi_status_t EFIAPI efi_protocols_per_handle(
2070 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002071 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002072{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002073 unsigned long buffer_size;
2074 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002075 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002076 efi_status_t r;
2077
Alexander Grafc15d9212016-03-04 01:09:59 +01002078 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2079 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002080
2081 if (!handle || !protocol_buffer || !protocol_buffer_count)
2082 return EFI_EXIT(EFI_INVALID_PARAMETER);
2083
2084 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002085 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002086
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002087 efiobj = efi_search_obj(handle);
2088 if (!efiobj)
2089 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002090
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002091 /* Count protocols */
2092 list_for_each(protocol_handle, &efiobj->protocols) {
2093 ++*protocol_buffer_count;
2094 }
2095
2096 /* Copy guids */
2097 if (*protocol_buffer_count) {
2098 size_t j = 0;
2099
2100 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2101 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2102 (void **)protocol_buffer);
2103 if (r != EFI_SUCCESS)
2104 return EFI_EXIT(r);
2105 list_for_each(protocol_handle, &efiobj->protocols) {
2106 struct efi_handler *protocol;
2107
2108 protocol = list_entry(protocol_handle,
2109 struct efi_handler, link);
2110 (*protocol_buffer)[j] = (void *)protocol->guid;
2111 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002112 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002113 }
2114
2115 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002116}
2117
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002118/**
Mario Six8fac2912018-07-10 08:40:17 +02002119 * efi_locate_handle_buffer() - locate handles implementing a protocol
2120 * @search_type: selection criterion
2121 * @protocol: GUID of the protocol
2122 * @search_key: registration key
2123 * @no_handles: number of returned handles
2124 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002125 *
2126 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002127 *
Mario Six8fac2912018-07-10 08:40:17 +02002128 * See the Unified Extensible Firmware Interface (UEFI) specification for
2129 * details.
2130 *
2131 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002132 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002133static efi_status_t EFIAPI efi_locate_handle_buffer(
2134 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002135 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002136 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002137{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002138 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002139 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002140
Rob Clark238f88c2017-09-13 18:05:41 -04002141 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002142 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002143
2144 if (!no_handles || !buffer) {
2145 r = EFI_INVALID_PARAMETER;
2146 goto out;
2147 }
2148 *no_handles = 0;
2149 *buffer = NULL;
2150 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2151 *buffer);
2152 if (r != EFI_BUFFER_TOO_SMALL)
2153 goto out;
2154 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2155 (void **)buffer);
2156 if (r != EFI_SUCCESS)
2157 goto out;
2158 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2159 *buffer);
2160 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002161 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002162out:
2163 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002164}
2165
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002166/**
Mario Six8fac2912018-07-10 08:40:17 +02002167 * efi_locate_protocol() - find an interface implementing a protocol
2168 * @protocol: GUID of the protocol
2169 * @registration: registration key passed to the notification function
2170 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002171 *
2172 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002173 *
2174 * See the Unified Extensible Firmware Interface (UEFI) specification for
2175 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002176 *
Mario Six8fac2912018-07-10 08:40:17 +02002177 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002178 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002179static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002180 void *registration,
2181 void **protocol_interface)
2182{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002183 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002184 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002185
Rob Clark238f88c2017-09-13 18:05:41 -04002186 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002187
2188 if (!protocol || !protocol_interface)
2189 return EFI_EXIT(EFI_INVALID_PARAMETER);
2190
2191 list_for_each(lhandle, &efi_obj_list) {
2192 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002193 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002194
2195 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002196
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002197 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2198 if (ret == EFI_SUCCESS) {
2199 *protocol_interface = handler->protocol_interface;
2200 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002201 }
2202 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002203 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002204
2205 return EFI_EXIT(EFI_NOT_FOUND);
2206}
2207
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002208/**
Mario Six8fac2912018-07-10 08:40:17 +02002209 * efi_locate_device_path() - Get the device path and handle of an device
2210 * implementing a protocol
2211 * @protocol: GUID of the protocol
2212 * @device_path: device path
2213 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002214 *
2215 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002216 *
Mario Six8fac2912018-07-10 08:40:17 +02002217 * See the Unified Extensible Firmware Interface (UEFI) specification for
2218 * details.
2219 *
2220 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002221 */
2222static efi_status_t EFIAPI efi_locate_device_path(
2223 const efi_guid_t *protocol,
2224 struct efi_device_path **device_path,
2225 efi_handle_t *device)
2226{
2227 struct efi_device_path *dp;
2228 size_t i;
2229 struct efi_handler *handler;
2230 efi_handle_t *handles;
2231 size_t len, len_dp;
2232 size_t len_best = 0;
2233 efi_uintn_t no_handles;
2234 u8 *remainder;
2235 efi_status_t ret;
2236
2237 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2238
2239 if (!protocol || !device_path || !*device_path || !device) {
2240 ret = EFI_INVALID_PARAMETER;
2241 goto out;
2242 }
2243
2244 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002245 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002246
2247 /* Get all handles implementing the protocol */
2248 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2249 &no_handles, &handles));
2250 if (ret != EFI_SUCCESS)
2251 goto out;
2252
2253 for (i = 0; i < no_handles; ++i) {
2254 /* Find the device path protocol */
2255 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2256 &handler);
2257 if (ret != EFI_SUCCESS)
2258 continue;
2259 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002260 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002261 /*
2262 * This handle can only be a better fit
2263 * if its device path length is longer than the best fit and
2264 * if its device path length is shorter of equal the searched
2265 * device path.
2266 */
2267 if (len_dp <= len_best || len_dp > len)
2268 continue;
2269 /* Check if dp is a subpath of device_path */
2270 if (memcmp(*device_path, dp, len_dp))
2271 continue;
2272 *device = handles[i];
2273 len_best = len_dp;
2274 }
2275 if (len_best) {
2276 remainder = (u8 *)*device_path + len_best;
2277 *device_path = (struct efi_device_path *)remainder;
2278 ret = EFI_SUCCESS;
2279 } else {
2280 ret = EFI_NOT_FOUND;
2281 }
2282out:
2283 return EFI_EXIT(ret);
2284}
2285
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002286/**
Mario Six8fac2912018-07-10 08:40:17 +02002287 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2288 * interfaces
2289 * @handle: handle on which the protocol interfaces shall be installed
2290 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2291 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002292 *
2293 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002294 *
2295 * See the Unified Extensible Firmware Interface (UEFI) specification for
2296 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002297 *
Mario Six8fac2912018-07-10 08:40:17 +02002298 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002299 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002300static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2301 void **handle, ...)
2302{
2303 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002304
2305 va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002306 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002307 void *protocol_interface;
2308 efi_status_t r = EFI_SUCCESS;
2309 int i = 0;
2310
2311 if (!handle)
2312 return EFI_EXIT(EFI_INVALID_PARAMETER);
2313
2314 va_start(argptr, handle);
2315 for (;;) {
2316 protocol = va_arg(argptr, efi_guid_t*);
2317 if (!protocol)
2318 break;
2319 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002320 r = EFI_CALL(efi_install_protocol_interface(
2321 handle, protocol,
2322 EFI_NATIVE_INTERFACE,
2323 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002324 if (r != EFI_SUCCESS)
2325 break;
2326 i++;
2327 }
2328 va_end(argptr);
2329 if (r == EFI_SUCCESS)
2330 return EFI_EXIT(r);
2331
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002332 /* If an error occurred undo all changes. */
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002333 va_start(argptr, handle);
2334 for (; i; --i) {
2335 protocol = va_arg(argptr, efi_guid_t*);
2336 protocol_interface = va_arg(argptr, void*);
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002337 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2338 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002339 }
2340 va_end(argptr);
2341
2342 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002343}
2344
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002345/**
Mario Six8fac2912018-07-10 08:40:17 +02002346 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2347 * interfaces
2348 * @handle: handle from which the protocol interfaces shall be removed
2349 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2350 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002351 *
2352 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002353 *
Mario Six8fac2912018-07-10 08:40:17 +02002354 * See the Unified Extensible Firmware Interface (UEFI) specification for
2355 * details.
2356 *
2357 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002358 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002359static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2360 void *handle, ...)
2361{
2362 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002363
2364 va_list argptr;
2365 const efi_guid_t *protocol;
2366 void *protocol_interface;
2367 efi_status_t r = EFI_SUCCESS;
2368 size_t i = 0;
2369
2370 if (!handle)
2371 return EFI_EXIT(EFI_INVALID_PARAMETER);
2372
2373 va_start(argptr, handle);
2374 for (;;) {
2375 protocol = va_arg(argptr, efi_guid_t*);
2376 if (!protocol)
2377 break;
2378 protocol_interface = va_arg(argptr, void*);
2379 r = EFI_CALL(efi_uninstall_protocol_interface(
2380 handle, protocol,
2381 protocol_interface));
2382 if (r != EFI_SUCCESS)
2383 break;
2384 i++;
2385 }
2386 va_end(argptr);
2387 if (r == EFI_SUCCESS)
2388 return EFI_EXIT(r);
2389
2390 /* If an error occurred undo all changes. */
2391 va_start(argptr, handle);
2392 for (; i; --i) {
2393 protocol = va_arg(argptr, efi_guid_t*);
2394 protocol_interface = va_arg(argptr, void*);
2395 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2396 EFI_NATIVE_INTERFACE,
2397 protocol_interface));
2398 }
2399 va_end(argptr);
2400
2401 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002402}
2403
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002404/**
Mario Six8fac2912018-07-10 08:40:17 +02002405 * efi_calculate_crc32() - calculate cyclic redundancy code
2406 * @data: buffer with data
2407 * @data_size: size of buffer in bytes
2408 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002409 *
2410 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002411 *
2412 * See the Unified Extensible Firmware Interface (UEFI) specification for
2413 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002414 *
Mario Six8fac2912018-07-10 08:40:17 +02002415 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002416 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002417static efi_status_t EFIAPI efi_calculate_crc32(void *data,
2418 unsigned long data_size,
2419 uint32_t *crc32_p)
2420{
2421 EFI_ENTRY("%p, %ld", data, data_size);
2422 *crc32_p = crc32(0, data, data_size);
2423 return EFI_EXIT(EFI_SUCCESS);
2424}
2425
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002426/**
Mario Six8fac2912018-07-10 08:40:17 +02002427 * efi_copy_mem() - copy memory
2428 * @destination: destination of the copy operation
2429 * @source: source of the copy operation
2430 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002431 *
2432 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002433 *
Mario Six8fac2912018-07-10 08:40:17 +02002434 * See the Unified Extensible Firmware Interface (UEFI) specification for
2435 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002436 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002437static void EFIAPI efi_copy_mem(void *destination, const void *source,
2438 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002439{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002440 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002441 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002442 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002443}
2444
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002445/**
Mario Six8fac2912018-07-10 08:40:17 +02002446 * efi_set_mem() - Fill memory with a byte value.
2447 * @buffer: buffer to fill
2448 * @size: size of buffer in bytes
2449 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002450 *
2451 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002452 *
Mario Six8fac2912018-07-10 08:40:17 +02002453 * See the Unified Extensible Firmware Interface (UEFI) specification for
2454 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002455 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002456static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002457{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002458 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002459 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002460 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002461}
2462
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002463/**
Mario Six8fac2912018-07-10 08:40:17 +02002464 * efi_protocol_open() - open protocol interface on a handle
2465 * @handler: handler of a protocol
2466 * @protocol_interface: interface implementing the protocol
2467 * @agent_handle: handle of the driver
2468 * @controller_handle: handle of the controller
2469 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002470 *
Mario Six8fac2912018-07-10 08:40:17 +02002471 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002472 */
2473static efi_status_t efi_protocol_open(
2474 struct efi_handler *handler,
2475 void **protocol_interface, void *agent_handle,
2476 void *controller_handle, uint32_t attributes)
2477{
2478 struct efi_open_protocol_info_item *item;
2479 struct efi_open_protocol_info_entry *match = NULL;
2480 bool opened_by_driver = false;
2481 bool opened_exclusive = false;
2482
2483 /* If there is no agent, only return the interface */
2484 if (!agent_handle)
2485 goto out;
2486
2487 /* For TEST_PROTOCOL ignore interface attribute */
2488 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2489 *protocol_interface = NULL;
2490
2491 /*
2492 * Check if the protocol is already opened by a driver with the same
2493 * attributes or opened exclusively
2494 */
2495 list_for_each_entry(item, &handler->open_infos, link) {
2496 if (item->info.agent_handle == agent_handle) {
2497 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2498 (item->info.attributes == attributes))
2499 return EFI_ALREADY_STARTED;
2500 }
2501 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2502 opened_exclusive = true;
2503 }
2504
2505 /* Only one controller can open the protocol exclusively */
2506 if (opened_exclusive && attributes &
2507 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2508 return EFI_ACCESS_DENIED;
2509
2510 /* Prepare exclusive opening */
2511 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2512 /* Try to disconnect controllers */
2513 list_for_each_entry(item, &handler->open_infos, link) {
2514 if (item->info.attributes ==
2515 EFI_OPEN_PROTOCOL_BY_DRIVER)
2516 EFI_CALL(efi_disconnect_controller(
2517 item->info.controller_handle,
2518 item->info.agent_handle,
2519 NULL));
2520 }
2521 opened_by_driver = false;
2522 /* Check if all controllers are disconnected */
2523 list_for_each_entry(item, &handler->open_infos, link) {
2524 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2525 opened_by_driver = true;
2526 }
2527 /* Only one controller can be conncected */
2528 if (opened_by_driver)
2529 return EFI_ACCESS_DENIED;
2530 }
2531
2532 /* Find existing entry */
2533 list_for_each_entry(item, &handler->open_infos, link) {
2534 if (item->info.agent_handle == agent_handle &&
2535 item->info.controller_handle == controller_handle)
2536 match = &item->info;
2537 }
2538 /* None found, create one */
2539 if (!match) {
2540 match = efi_create_open_info(handler);
2541 if (!match)
2542 return EFI_OUT_OF_RESOURCES;
2543 }
2544
2545 match->agent_handle = agent_handle;
2546 match->controller_handle = controller_handle;
2547 match->attributes = attributes;
2548 match->open_count++;
2549
2550out:
2551 /* For TEST_PROTOCOL ignore interface attribute. */
2552 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2553 *protocol_interface = handler->protocol_interface;
2554
2555 return EFI_SUCCESS;
2556}
2557
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002558/**
Mario Six8fac2912018-07-10 08:40:17 +02002559 * efi_open_protocol() - open protocol interface on a handle
2560 * @handle: handle on which the protocol shall be opened
2561 * @protocol: GUID of the protocol
2562 * @protocol_interface: interface implementing the protocol
2563 * @agent_handle: handle of the driver
2564 * @controller_handle: handle of the controller
2565 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002566 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002567 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002568 *
Mario Six8fac2912018-07-10 08:40:17 +02002569 * See the Unified Extensible Firmware Interface (UEFI) specification for
2570 * details.
2571 *
2572 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002573 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002574static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002575 void *handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002576 void **protocol_interface, void *agent_handle,
2577 void *controller_handle, uint32_t attributes)
2578{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002579 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002580 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002581
Rob Clark238f88c2017-09-13 18:05:41 -04002582 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002583 protocol_interface, agent_handle, controller_handle,
2584 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002585
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002586 if (!handle || !protocol ||
2587 (!protocol_interface && attributes !=
2588 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002589 goto out;
2590 }
2591
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002592 switch (attributes) {
2593 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2594 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2595 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2596 break;
2597 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2598 if (controller_handle == handle)
2599 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002600 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002601 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2602 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002603 /* Check that the controller handle is valid */
2604 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002605 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002606 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002607 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002608 /* Check that the agent handle is valid */
2609 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002610 goto out;
2611 break;
2612 default:
2613 goto out;
2614 }
2615
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002616 r = efi_search_protocol(handle, protocol, &handler);
2617 if (r != EFI_SUCCESS)
2618 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002619
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002620 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2621 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002622out:
2623 return EFI_EXIT(r);
2624}
2625
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002626/**
Mario Six8fac2912018-07-10 08:40:17 +02002627 * efi_handle_protocol() - get interface of a protocol on a handle
2628 * @handle: handle on which the protocol shall be opened
2629 * @protocol: GUID of the protocol
2630 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002631 *
2632 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002633 *
2634 * See the Unified Extensible Firmware Interface (UEFI) specification for
2635 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002636 *
Mario Six8fac2912018-07-10 08:40:17 +02002637 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002638 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002639static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002640 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002641 void **protocol_interface)
2642{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002643 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2644 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002645}
2646
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002647/**
Mario Six8fac2912018-07-10 08:40:17 +02002648 * efi_bind_controller() - bind a single driver to a controller
2649 * @controller_handle: controller handle
2650 * @driver_image_handle: driver handle
2651 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002652 *
Mario Six8fac2912018-07-10 08:40:17 +02002653 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002654 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002655static efi_status_t efi_bind_controller(
2656 efi_handle_t controller_handle,
2657 efi_handle_t driver_image_handle,
2658 struct efi_device_path *remain_device_path)
2659{
2660 struct efi_driver_binding_protocol *binding_protocol;
2661 efi_status_t r;
2662
2663 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2664 &efi_guid_driver_binding_protocol,
2665 (void **)&binding_protocol,
2666 driver_image_handle, NULL,
2667 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2668 if (r != EFI_SUCCESS)
2669 return r;
2670 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2671 controller_handle,
2672 remain_device_path));
2673 if (r == EFI_SUCCESS)
2674 r = EFI_CALL(binding_protocol->start(binding_protocol,
2675 controller_handle,
2676 remain_device_path));
2677 EFI_CALL(efi_close_protocol(driver_image_handle,
2678 &efi_guid_driver_binding_protocol,
2679 driver_image_handle, NULL));
2680 return r;
2681}
2682
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002683/**
Mario Six8fac2912018-07-10 08:40:17 +02002684 * efi_connect_single_controller() - connect a single driver to a controller
2685 * @controller_handle: controller
2686 * @driver_image_handle: driver
2687 * @remain_device_path: remainting path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002688 *
Mario Six8fac2912018-07-10 08:40:17 +02002689 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002690 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002691static efi_status_t efi_connect_single_controller(
2692 efi_handle_t controller_handle,
2693 efi_handle_t *driver_image_handle,
2694 struct efi_device_path *remain_device_path)
2695{
2696 efi_handle_t *buffer;
2697 size_t count;
2698 size_t i;
2699 efi_status_t r;
2700 size_t connected = 0;
2701
2702 /* Get buffer with all handles with driver binding protocol */
2703 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2704 &efi_guid_driver_binding_protocol,
2705 NULL, &count, &buffer));
2706 if (r != EFI_SUCCESS)
2707 return r;
2708
2709 /* Context Override */
2710 if (driver_image_handle) {
2711 for (; *driver_image_handle; ++driver_image_handle) {
2712 for (i = 0; i < count; ++i) {
2713 if (buffer[i] == *driver_image_handle) {
2714 buffer[i] = NULL;
2715 r = efi_bind_controller(
2716 controller_handle,
2717 *driver_image_handle,
2718 remain_device_path);
2719 /*
2720 * For drivers that do not support the
2721 * controller or are already connected
2722 * we receive an error code here.
2723 */
2724 if (r == EFI_SUCCESS)
2725 ++connected;
2726 }
2727 }
2728 }
2729 }
2730
2731 /*
2732 * TODO: Some overrides are not yet implemented:
2733 * - Platform Driver Override
2734 * - Driver Family Override Search
2735 * - Bus Specific Driver Override
2736 */
2737
2738 /* Driver Binding Search */
2739 for (i = 0; i < count; ++i) {
2740 if (buffer[i]) {
2741 r = efi_bind_controller(controller_handle,
2742 buffer[i],
2743 remain_device_path);
2744 if (r == EFI_SUCCESS)
2745 ++connected;
2746 }
2747 }
2748
2749 efi_free_pool(buffer);
2750 if (!connected)
2751 return EFI_NOT_FOUND;
2752 return EFI_SUCCESS;
2753}
2754
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002755/**
Mario Six8fac2912018-07-10 08:40:17 +02002756 * efi_connect_controller() - connect a controller to a driver
2757 * @controller_handle: handle of the controller
2758 * @driver_image_handle: handle of the driver
2759 * @remain_device_path: device path of a child controller
2760 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002761 *
2762 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002763 *
2764 * See the Unified Extensible Firmware Interface (UEFI) specification for
2765 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002766 *
2767 * First all driver binding protocol handles are tried for binding drivers.
2768 * Afterwards all handles that have openened a protocol of the controller
2769 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2770 *
Mario Six8fac2912018-07-10 08:40:17 +02002771 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002772 */
2773static efi_status_t EFIAPI efi_connect_controller(
2774 efi_handle_t controller_handle,
2775 efi_handle_t *driver_image_handle,
2776 struct efi_device_path *remain_device_path,
2777 bool recursive)
2778{
2779 efi_status_t r;
2780 efi_status_t ret = EFI_NOT_FOUND;
2781 struct efi_object *efiobj;
2782
2783 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2784 remain_device_path, recursive);
2785
2786 efiobj = efi_search_obj(controller_handle);
2787 if (!efiobj) {
2788 ret = EFI_INVALID_PARAMETER;
2789 goto out;
2790 }
2791
2792 r = efi_connect_single_controller(controller_handle,
2793 driver_image_handle,
2794 remain_device_path);
2795 if (r == EFI_SUCCESS)
2796 ret = EFI_SUCCESS;
2797 if (recursive) {
2798 struct efi_handler *handler;
2799 struct efi_open_protocol_info_item *item;
2800
2801 list_for_each_entry(handler, &efiobj->protocols, link) {
2802 list_for_each_entry(item, &handler->open_infos, link) {
2803 if (item->info.attributes &
2804 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2805 r = EFI_CALL(efi_connect_controller(
2806 item->info.controller_handle,
2807 driver_image_handle,
2808 remain_device_path,
2809 recursive));
2810 if (r == EFI_SUCCESS)
2811 ret = EFI_SUCCESS;
2812 }
2813 }
2814 }
2815 }
2816 /* Check for child controller specified by end node */
2817 if (ret != EFI_SUCCESS && remain_device_path &&
2818 remain_device_path->type == DEVICE_PATH_TYPE_END)
2819 ret = EFI_SUCCESS;
2820out:
2821 return EFI_EXIT(ret);
2822}
2823
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002824/**
Mario Six8fac2912018-07-10 08:40:17 +02002825 * efi_reinstall_protocol_interface() - reinstall protocol interface
2826 * @handle: handle on which the protocol shall be reinstalled
2827 * @protocol: GUID of the protocol to be installed
2828 * @old_interface: interface to be removed
2829 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002830 *
2831 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002832 *
2833 * See the Unified Extensible Firmware Interface (UEFI) specification for
2834 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002835 *
2836 * The old interface is uninstalled. The new interface is installed.
2837 * Drivers are connected.
2838 *
Mario Six8fac2912018-07-10 08:40:17 +02002839 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002840 */
2841static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2842 efi_handle_t handle, const efi_guid_t *protocol,
2843 void *old_interface, void *new_interface)
2844{
2845 efi_status_t ret;
2846
2847 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2848 new_interface);
2849 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2850 old_interface));
2851 if (ret != EFI_SUCCESS)
2852 goto out;
2853 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2854 EFI_NATIVE_INTERFACE,
2855 new_interface));
2856 if (ret != EFI_SUCCESS)
2857 goto out;
2858 /*
2859 * The returned status code has to be ignored.
2860 * Do not create an error if no suitable driver for the handle exists.
2861 */
2862 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2863out:
2864 return EFI_EXIT(ret);
2865}
2866
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002867/**
Mario Six8fac2912018-07-10 08:40:17 +02002868 * efi_get_child_controllers() - get all child controllers associated to a driver
2869 * @efiobj: handle of the controller
2870 * @driver_handle: handle of the driver
2871 * @number_of_children: number of child controllers
2872 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002873 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002874 * The allocated buffer has to be freed with free().
2875 *
Mario Six8fac2912018-07-10 08:40:17 +02002876 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002877 */
2878static efi_status_t efi_get_child_controllers(
2879 struct efi_object *efiobj,
2880 efi_handle_t driver_handle,
2881 efi_uintn_t *number_of_children,
2882 efi_handle_t **child_handle_buffer)
2883{
2884 struct efi_handler *handler;
2885 struct efi_open_protocol_info_item *item;
2886 efi_uintn_t count = 0, i;
2887 bool duplicate;
2888
2889 /* Count all child controller associations */
2890 list_for_each_entry(handler, &efiobj->protocols, link) {
2891 list_for_each_entry(item, &handler->open_infos, link) {
2892 if (item->info.agent_handle == driver_handle &&
2893 item->info.attributes &
2894 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2895 ++count;
2896 }
2897 }
2898 /*
2899 * Create buffer. In case of duplicate child controller assignments
2900 * the buffer will be too large. But that does not harm.
2901 */
2902 *number_of_children = 0;
2903 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2904 if (!*child_handle_buffer)
2905 return EFI_OUT_OF_RESOURCES;
2906 /* Copy unique child handles */
2907 list_for_each_entry(handler, &efiobj->protocols, link) {
2908 list_for_each_entry(item, &handler->open_infos, link) {
2909 if (item->info.agent_handle == driver_handle &&
2910 item->info.attributes &
2911 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2912 /* Check this is a new child controller */
2913 duplicate = false;
2914 for (i = 0; i < *number_of_children; ++i) {
2915 if ((*child_handle_buffer)[i] ==
2916 item->info.controller_handle)
2917 duplicate = true;
2918 }
2919 /* Copy handle to buffer */
2920 if (!duplicate) {
2921 i = (*number_of_children)++;
2922 (*child_handle_buffer)[i] =
2923 item->info.controller_handle;
2924 }
2925 }
2926 }
2927 }
2928 return EFI_SUCCESS;
2929}
2930
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002931/**
Mario Six8fac2912018-07-10 08:40:17 +02002932 * efi_disconnect_controller() - disconnect a controller from a driver
2933 * @controller_handle: handle of the controller
2934 * @driver_image_handle: handle of the driver
2935 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002936 *
2937 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002938 *
2939 * See the Unified Extensible Firmware Interface (UEFI) specification for
2940 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002941 *
Mario Six8fac2912018-07-10 08:40:17 +02002942 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002943 */
2944static efi_status_t EFIAPI efi_disconnect_controller(
2945 efi_handle_t controller_handle,
2946 efi_handle_t driver_image_handle,
2947 efi_handle_t child_handle)
2948{
2949 struct efi_driver_binding_protocol *binding_protocol;
2950 efi_handle_t *child_handle_buffer = NULL;
2951 size_t number_of_children = 0;
2952 efi_status_t r;
2953 size_t stop_count = 0;
2954 struct efi_object *efiobj;
2955
2956 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2957 child_handle);
2958
2959 efiobj = efi_search_obj(controller_handle);
2960 if (!efiobj) {
2961 r = EFI_INVALID_PARAMETER;
2962 goto out;
2963 }
2964
2965 if (child_handle && !efi_search_obj(child_handle)) {
2966 r = EFI_INVALID_PARAMETER;
2967 goto out;
2968 }
2969
2970 /* If no driver handle is supplied, disconnect all drivers */
2971 if (!driver_image_handle) {
2972 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2973 goto out;
2974 }
2975
2976 /* Create list of child handles */
2977 if (child_handle) {
2978 number_of_children = 1;
2979 child_handle_buffer = &child_handle;
2980 } else {
2981 efi_get_child_controllers(efiobj,
2982 driver_image_handle,
2983 &number_of_children,
2984 &child_handle_buffer);
2985 }
2986
2987 /* Get the driver binding protocol */
2988 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2989 &efi_guid_driver_binding_protocol,
2990 (void **)&binding_protocol,
2991 driver_image_handle, NULL,
2992 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2993 if (r != EFI_SUCCESS)
2994 goto out;
2995 /* Remove the children */
2996 if (number_of_children) {
2997 r = EFI_CALL(binding_protocol->stop(binding_protocol,
2998 controller_handle,
2999 number_of_children,
3000 child_handle_buffer));
3001 if (r == EFI_SUCCESS)
3002 ++stop_count;
3003 }
3004 /* Remove the driver */
3005 if (!child_handle)
3006 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3007 controller_handle,
3008 0, NULL));
3009 if (r == EFI_SUCCESS)
3010 ++stop_count;
3011 EFI_CALL(efi_close_protocol(driver_image_handle,
3012 &efi_guid_driver_binding_protocol,
3013 driver_image_handle, NULL));
3014
3015 if (stop_count)
3016 r = EFI_SUCCESS;
3017 else
3018 r = EFI_NOT_FOUND;
3019out:
3020 if (!child_handle)
3021 free(child_handle_buffer);
3022 return EFI_EXIT(r);
3023}
3024
Alexander Grafc15d9212016-03-04 01:09:59 +01003025static const struct efi_boot_services efi_boot_services = {
3026 .hdr = {
3027 .headersize = sizeof(struct efi_table_hdr),
3028 },
3029 .raise_tpl = efi_raise_tpl,
3030 .restore_tpl = efi_restore_tpl,
3031 .allocate_pages = efi_allocate_pages_ext,
3032 .free_pages = efi_free_pages_ext,
3033 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003034 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003035 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003036 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003037 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003038 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003039 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003040 .close_event = efi_close_event,
3041 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003042 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003043 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003044 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003045 .handle_protocol = efi_handle_protocol,
3046 .reserved = NULL,
3047 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003048 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003049 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003050 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003051 .load_image = efi_load_image,
3052 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003053 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003054 .unload_image = efi_unload_image,
3055 .exit_boot_services = efi_exit_boot_services,
3056 .get_next_monotonic_count = efi_get_next_monotonic_count,
3057 .stall = efi_stall,
3058 .set_watchdog_timer = efi_set_watchdog_timer,
3059 .connect_controller = efi_connect_controller,
3060 .disconnect_controller = efi_disconnect_controller,
3061 .open_protocol = efi_open_protocol,
3062 .close_protocol = efi_close_protocol,
3063 .open_protocol_information = efi_open_protocol_information,
3064 .protocols_per_handle = efi_protocols_per_handle,
3065 .locate_handle_buffer = efi_locate_handle_buffer,
3066 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003067 .install_multiple_protocol_interfaces =
3068 efi_install_multiple_protocol_interfaces,
3069 .uninstall_multiple_protocol_interfaces =
3070 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003071 .calculate_crc32 = efi_calculate_crc32,
3072 .copy_mem = efi_copy_mem,
3073 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003074 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003075};
3076
Heinrich Schuchardt3579b692017-12-11 20:10:20 +01003077static uint16_t __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003078
Alexander Graf393dd912016-10-14 13:45:30 +02003079struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003080 .hdr = {
3081 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt7d495df2018-02-05 18:04:21 +01003082 .revision = 2 << 16 | 70, /* 2.7 */
Alexander Grafc15d9212016-03-04 01:09:59 +01003083 .headersize = sizeof(struct efi_table_hdr),
3084 },
3085 .fw_vendor = (long)firmware_vendor,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003086 .con_in = (void *)&efi_con_in,
3087 .con_out = (void *)&efi_con_out,
3088 .std_err = (void *)&efi_con_out,
3089 .runtime = (void *)&efi_runtime_services,
3090 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003091 .nr_tables = 0,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003092 .tables = (void *)efi_conf_table,
Alexander Grafc15d9212016-03-04 01:09:59 +01003093};