blob: b583ac6a42641a67f316fb1764d38269cc00cc63 [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>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020016#include <pe.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010017#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
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020030/* List of all events registered by RegisterProtocolNotify() */
31LIST_HEAD(efi_register_notify_events);
32
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010033/* Handle of the currently executing image */
34static efi_handle_t current_image;
35
Alexander Graffa5246b2018-11-15 21:23:47 +010036/*
37 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
38 * we need to do trickery with caches. Since we don't want to break the EFI
39 * aware boot path, only apply hacks when loading exiting directly (breaking
40 * direct Linux EFI booting along the way - oh well).
41 */
42static bool efi_is_direct_boot = true;
43
Simon Glasscdfe6962016-09-25 15:27:35 -060044#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010045/*
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
50 */
51static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060052#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010053
Heinrich Schuchardt72506232019-02-09 14:10:39 +010054/* 1 if inside U-Boot code, 0 if inside EFI payload code */
55static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040056static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010057/* GUID of the device tree table */
58const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010059/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040062
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010063/* event group ExitBootServices() invoked */
64const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66/* event group SetVirtualAddressMap() invoked */
67const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69/* event group memory map changed */
70const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72/* event group boot manager about to boot */
73const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75/* event group ResetSystem() invoked (before ExitBootServices) */
76const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
78
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010079static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010083
Rob Clark86789d52017-07-27 08:04:18 -040084/* Called on every callback entry */
85int __efi_entry_check(void)
86{
87 int ret = entry_count++ == 0;
88#ifdef CONFIG_ARM
89 assert(efi_gd);
90 app_gd = gd;
91 gd = efi_gd;
92#endif
93 return ret;
94}
95
96/* Called on every callback exit */
97int __efi_exit_check(void)
98{
99 int ret = --entry_count == 0;
100#ifdef CONFIG_ARM
101 gd = app_gd;
102#endif
103 return ret;
104}
105
Alexander Grafc15d9212016-03-04 01:09:59 +0100106/* Called from do_bootefi_exec() */
107void efi_save_gd(void)
108{
Simon Glasscdfe6962016-09-25 15:27:35 -0600109#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100110 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600111#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100112}
113
Rob Clark86789d52017-07-27 08:04:18 -0400114/*
Mario Six8fac2912018-07-10 08:40:17 +0200115 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400118 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100119void efi_restore_gd(void)
120{
Simon Glasscdfe6962016-09-25 15:27:35 -0600121#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100122 /* Only restore if we're already in EFI context */
123 if (!efi_gd)
124 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600126#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100127}
128
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200129/**
Mario Six8fac2912018-07-10 08:40:17 +0200130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100132 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200133 * A maximum of ten indent levels is supported. Higher indent levels will be
134 * truncated.
135 *
Mario Six8fac2912018-07-10 08:40:17 +0200136 * Return: A string for indenting with two spaces per level is
137 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400138 */
139static const char *indent_string(int level)
140{
141 const char *indent = " ";
142 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100143
Rob Clarke7896c32017-07-27 08:04:19 -0400144 level = min(max, level * 2);
145 return &indent[max - level];
146}
147
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200148const char *__efi_nesting(void)
149{
150 return indent_string(nesting_level);
151}
152
Rob Clarke7896c32017-07-27 08:04:19 -0400153const char *__efi_nesting_inc(void)
154{
155 return indent_string(nesting_level++);
156}
157
158const char *__efi_nesting_dec(void)
159{
160 return indent_string(--nesting_level);
161}
162
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200163/**
Mario Six8fac2912018-07-10 08:40:17 +0200164 * efi_queue_event() - queue an EFI event
165 * @event: event to signal
166 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200167 *
168 * This function queues the notification function of the event for future
169 * execution.
170 *
Mario Six8fac2912018-07-10 08:40:17 +0200171 * The notification function is called if the task priority level of the event
172 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200173 *
174 * For the SignalEvent service see efi_signal_event_ext.
175 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200176 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100177static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200178{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200179 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200180 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200181 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100182 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200183 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200184 EFI_CALL_VOID(event->notify_function(event,
185 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200186 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200187 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200188}
189
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200190/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200191 * is_valid_tpl() - check if the task priority level is valid
192 *
193 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200194 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200195 */
196efi_status_t is_valid_tpl(efi_uintn_t tpl)
197{
198 switch (tpl) {
199 case TPL_APPLICATION:
200 case TPL_CALLBACK:
201 case TPL_NOTIFY:
202 case TPL_HIGH_LEVEL:
203 return EFI_SUCCESS;
204 default:
205 return EFI_INVALID_PARAMETER;
206 }
207}
208
209/**
Mario Six8fac2912018-07-10 08:40:17 +0200210 * efi_signal_event() - signal an EFI event
211 * @event: event to signal
212 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100213 *
Mario Six8fac2912018-07-10 08:40:17 +0200214 * This function signals an event. If the event belongs to an event group all
215 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100216 * their notification function is queued.
217 *
218 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100219 */
220void efi_signal_event(struct efi_event *event, bool check_tpl)
221{
222 if (event->group) {
223 struct efi_event *evt;
224
225 /*
226 * The signaled state has to set before executing any
227 * notification function
228 */
229 list_for_each_entry(evt, &efi_events, link) {
230 if (!evt->group || guidcmp(evt->group, event->group))
231 continue;
232 if (evt->is_signaled)
233 continue;
234 evt->is_signaled = true;
235 if (evt->type & EVT_NOTIFY_SIGNAL &&
236 evt->notify_function)
237 evt->is_queued = true;
238 }
239 list_for_each_entry(evt, &efi_events, link) {
240 if (!evt->group || guidcmp(evt->group, event->group))
241 continue;
242 if (evt->is_queued)
243 efi_queue_event(evt, check_tpl);
244 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200245 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100246 event->is_signaled = true;
247 if (event->type & EVT_NOTIFY_SIGNAL)
248 efi_queue_event(event, check_tpl);
249 }
250}
251
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200252/**
Mario Six8fac2912018-07-10 08:40:17 +0200253 * efi_raise_tpl() - raise the task priority level
254 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200255 *
256 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200257 *
258 * See the Unified Extensible Firmware Interface (UEFI) specification for
259 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200260 *
Mario Six8fac2912018-07-10 08:40:17 +0200261 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200262 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100263static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100264{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100265 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200266
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200267 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200268
269 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200270 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200271 efi_tpl = new_tpl;
272 if (efi_tpl > TPL_HIGH_LEVEL)
273 efi_tpl = TPL_HIGH_LEVEL;
274
275 EFI_EXIT(EFI_SUCCESS);
276 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100277}
278
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200279/**
Mario Six8fac2912018-07-10 08:40:17 +0200280 * efi_restore_tpl() - lower the task priority level
281 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200282 *
283 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200284 *
Mario Six8fac2912018-07-10 08:40:17 +0200285 * See the Unified Extensible Firmware Interface (UEFI) specification for
286 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200287 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100288static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100289{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200290 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200291
292 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200293 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200294 efi_tpl = old_tpl;
295 if (efi_tpl > TPL_HIGH_LEVEL)
296 efi_tpl = TPL_HIGH_LEVEL;
297
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100298 /*
299 * Lowering the TPL may have made queued events eligible for execution.
300 */
301 efi_timer_check();
302
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200303 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100304}
305
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200306/**
Mario Six8fac2912018-07-10 08:40:17 +0200307 * efi_allocate_pages_ext() - allocate memory pages
308 * @type: type of allocation to be performed
309 * @memory_type: usage type of the allocated memory
310 * @pages: number of pages to be allocated
311 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200312 *
313 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200314 *
Mario Six8fac2912018-07-10 08:40:17 +0200315 * See the Unified Extensible Firmware Interface (UEFI) specification for
316 * details.
317 *
318 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200319 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900320static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100321 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900322 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100323{
324 efi_status_t r;
325
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100326 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100327 r = efi_allocate_pages(type, memory_type, pages, memory);
328 return EFI_EXIT(r);
329}
330
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200331/**
Mario Six8fac2912018-07-10 08:40:17 +0200332 * efi_free_pages_ext() - Free memory pages.
333 * @memory: start of the memory area to be freed
334 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200335 *
336 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200337 *
338 * See the Unified Extensible Firmware Interface (UEFI) specification for
339 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200340 *
Mario Six8fac2912018-07-10 08:40:17 +0200341 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200342 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900343static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100344 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100345{
346 efi_status_t r;
347
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900348 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100349 r = efi_free_pages(memory, pages);
350 return EFI_EXIT(r);
351}
352
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200353/**
Mario Six8fac2912018-07-10 08:40:17 +0200354 * efi_get_memory_map_ext() - get map describing memory usage
355 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
356 * on exit the size of the copied memory map
357 * @memory_map: buffer to which the memory map is written
358 * @map_key: key for the memory map
359 * @descriptor_size: size of an individual memory descriptor
360 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200361 *
362 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200363 *
Mario Six8fac2912018-07-10 08:40:17 +0200364 * See the Unified Extensible Firmware Interface (UEFI) specification for
365 * details.
366 *
367 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200368 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900369static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100370 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900371 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100372 efi_uintn_t *map_key,
373 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900374 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100375{
376 efi_status_t r;
377
378 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
379 map_key, descriptor_size, descriptor_version);
380 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
381 descriptor_size, descriptor_version);
382 return EFI_EXIT(r);
383}
384
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200385/**
Mario Six8fac2912018-07-10 08:40:17 +0200386 * efi_allocate_pool_ext() - allocate memory from pool
387 * @pool_type: type of the pool from which memory is to be allocated
388 * @size: number of bytes to be allocated
389 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200390 *
391 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200392 *
393 * See the Unified Extensible Firmware Interface (UEFI) specification for
394 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
Mario Six8fac2912018-07-10 08:40:17 +0200396 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200397 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200398static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100399 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200400 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100401{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100402 efi_status_t r;
403
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100404 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200405 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100406 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100407}
408
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200409/**
Mario Six8fac2912018-07-10 08:40:17 +0200410 * efi_free_pool_ext() - free memory from pool
411 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200412 *
413 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200414 *
Mario Six8fac2912018-07-10 08:40:17 +0200415 * See the Unified Extensible Firmware Interface (UEFI) specification for
416 * details.
417 *
418 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200419 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200420static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100421{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100422 efi_status_t r;
423
424 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200425 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100426 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100427}
428
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200429/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200430 * efi_add_handle() - add a new handle to the object list
431 *
432 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100433 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200434 * The protocols list is initialized. The handle is added to the list of known
435 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100436 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200437void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100438{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200439 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100440 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200441 INIT_LIST_HEAD(&handle->protocols);
442 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100443}
444
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200445/**
Mario Six8fac2912018-07-10 08:40:17 +0200446 * efi_create_handle() - create handle
447 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200448 *
Mario Six8fac2912018-07-10 08:40:17 +0200449 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200450 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100451efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200452{
453 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200454
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200455 obj = calloc(1, sizeof(struct efi_object));
456 if (!obj)
457 return EFI_OUT_OF_RESOURCES;
458
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100459 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200460 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200461
462 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200463}
464
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200465/**
Mario Six8fac2912018-07-10 08:40:17 +0200466 * efi_search_protocol() - find a protocol on a handle.
467 * @handle: handle
468 * @protocol_guid: GUID of the protocol
469 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100470 *
Mario Six8fac2912018-07-10 08:40:17 +0200471 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100472 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100473efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100474 const efi_guid_t *protocol_guid,
475 struct efi_handler **handler)
476{
477 struct efi_object *efiobj;
478 struct list_head *lhandle;
479
480 if (!handle || !protocol_guid)
481 return EFI_INVALID_PARAMETER;
482 efiobj = efi_search_obj(handle);
483 if (!efiobj)
484 return EFI_INVALID_PARAMETER;
485 list_for_each(lhandle, &efiobj->protocols) {
486 struct efi_handler *protocol;
487
488 protocol = list_entry(lhandle, struct efi_handler, link);
489 if (!guidcmp(protocol->guid, protocol_guid)) {
490 if (handler)
491 *handler = protocol;
492 return EFI_SUCCESS;
493 }
494 }
495 return EFI_NOT_FOUND;
496}
497
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200498/**
Mario Six8fac2912018-07-10 08:40:17 +0200499 * efi_remove_protocol() - delete protocol from a handle
500 * @handle: handle from which the protocol shall be deleted
501 * @protocol: GUID of the protocol to be deleted
502 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100503 *
Mario Six8fac2912018-07-10 08:40:17 +0200504 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100505 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100506efi_status_t efi_remove_protocol(const efi_handle_t handle,
507 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100508 void *protocol_interface)
509{
510 struct efi_handler *handler;
511 efi_status_t ret;
512
513 ret = efi_search_protocol(handle, protocol, &handler);
514 if (ret != EFI_SUCCESS)
515 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200516 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200517 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100518 list_del(&handler->link);
519 free(handler);
520 return EFI_SUCCESS;
521}
522
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200523/**
Mario Six8fac2912018-07-10 08:40:17 +0200524 * efi_remove_all_protocols() - delete all protocols from a handle
525 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526 *
Mario Six8fac2912018-07-10 08:40:17 +0200527 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100528 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100529efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100530{
531 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100532 struct efi_handler *protocol;
533 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534
535 efiobj = efi_search_obj(handle);
536 if (!efiobj)
537 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100538 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100539 efi_status_t ret;
540
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100541 ret = efi_remove_protocol(handle, protocol->guid,
542 protocol->protocol_interface);
543 if (ret != EFI_SUCCESS)
544 return ret;
545 }
546 return EFI_SUCCESS;
547}
548
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200549/**
Mario Six8fac2912018-07-10 08:40:17 +0200550 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100551 *
Mario Six8fac2912018-07-10 08:40:17 +0200552 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100553 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200554void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100555{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200556 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200558 efi_remove_all_protocols(handle);
559 list_del(&handle->link);
560 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100561}
562
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200563/**
Mario Six8fac2912018-07-10 08:40:17 +0200564 * efi_is_event() - check if a pointer is a valid event
565 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100566 *
Mario Six8fac2912018-07-10 08:40:17 +0200567 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100568 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100569static efi_status_t efi_is_event(const struct efi_event *event)
570{
571 const struct efi_event *evt;
572
573 if (!event)
574 return EFI_INVALID_PARAMETER;
575 list_for_each_entry(evt, &efi_events, link) {
576 if (evt == event)
577 return EFI_SUCCESS;
578 }
579 return EFI_INVALID_PARAMETER;
580}
Alexander Grafc15d9212016-03-04 01:09:59 +0100581
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200582/**
Mario Six8fac2912018-07-10 08:40:17 +0200583 * efi_create_event() - create an event
584 * @type: type of the event to create
585 * @notify_tpl: task priority level of the event
586 * @notify_function: notification function of the event
587 * @notify_context: pointer passed to the notification function
588 * @group: event group
589 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200590 *
591 * This function is used inside U-Boot code to create an event.
592 *
593 * For the API function implementing the CreateEvent service see
594 * efi_create_event_ext.
595 *
Mario Six8fac2912018-07-10 08:40:17 +0200596 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200597 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100598efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200599 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200600 struct efi_event *event,
601 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100602 void *notify_context, efi_guid_t *group,
603 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100604{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100605 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200606
Jonathan Gray7758b212017-03-12 19:26:07 +1100607 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200608 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100609
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200610 switch (type) {
611 case 0:
612 case EVT_TIMER:
613 case EVT_NOTIFY_SIGNAL:
614 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
615 case EVT_NOTIFY_WAIT:
616 case EVT_TIMER | EVT_NOTIFY_WAIT:
617 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
618 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
619 break;
620 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200621 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200622 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100623
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900624 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200625 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200626 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100627
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100628 evt = calloc(1, sizeof(struct efi_event));
629 if (!evt)
630 return EFI_OUT_OF_RESOURCES;
631 evt->type = type;
632 evt->notify_tpl = notify_tpl;
633 evt->notify_function = notify_function;
634 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100635 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200636 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100637 evt->trigger_next = -1ULL;
638 evt->is_queued = false;
639 evt->is_signaled = false;
640 list_add_tail(&evt->link, &efi_events);
641 *event = evt;
642 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100643}
644
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200645/*
Mario Six8fac2912018-07-10 08:40:17 +0200646 * efi_create_event_ex() - create an event in a group
647 * @type: type of the event to create
648 * @notify_tpl: task priority level of the event
649 * @notify_function: notification function of the event
650 * @notify_context: pointer passed to the notification function
651 * @event: created event
652 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100653 *
654 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100655 *
Mario Six8fac2912018-07-10 08:40:17 +0200656 * See the Unified Extensible Firmware Interface (UEFI) specification for
657 * details.
658 *
659 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100660 */
661efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
662 void (EFIAPI *notify_function) (
663 struct efi_event *event,
664 void *context),
665 void *notify_context,
666 efi_guid_t *event_group,
667 struct efi_event **event)
668{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200669 efi_status_t ret;
670
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100671 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
672 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200673
674 /*
675 * The allowable input parameters are the same as in CreateEvent()
676 * except for the following two disallowed event types.
677 */
678 switch (type) {
679 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
680 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
681 ret = EFI_INVALID_PARAMETER;
682 goto out;
683 }
684
685 ret = efi_create_event(type, notify_tpl, notify_function,
686 notify_context, event_group, event);
687out:
688 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100689}
690
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200691/**
Mario Six8fac2912018-07-10 08:40:17 +0200692 * efi_create_event_ext() - create an event
693 * @type: type of the event to create
694 * @notify_tpl: task priority level of the event
695 * @notify_function: notification function of the event
696 * @notify_context: pointer passed to the notification function
697 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200698 *
699 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200700 *
701 * See the Unified Extensible Firmware Interface (UEFI) specification for
702 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703 *
Mario Six8fac2912018-07-10 08:40:17 +0200704 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200705 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200706static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100707 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200708 void (EFIAPI *notify_function) (
709 struct efi_event *event,
710 void *context),
711 void *notify_context, struct efi_event **event)
712{
713 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
714 notify_context);
715 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100716 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200717}
718
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200719/**
Mario Six8fac2912018-07-10 08:40:17 +0200720 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200721 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200722 * Check if a timer event has occurred or a queued notification function should
723 * be called.
724 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100725 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200726 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100727 */
728void efi_timer_check(void)
729{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100730 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100731 u64 now = timer_get_us();
732
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100733 list_for_each_entry(evt, &efi_events, link) {
734 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100735 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100736 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200737 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100738 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200739 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100740 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200741 break;
742 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100743 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200744 break;
745 default:
746 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200747 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100748 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100749 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100750 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100751 WATCHDOG_RESET();
752}
753
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200754/**
Mario Six8fac2912018-07-10 08:40:17 +0200755 * efi_set_timer() - set the trigger time for a timer event or stop the event
756 * @event: event for which the timer is set
757 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200758 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200759 *
760 * This is the function for internal usage in U-Boot. For the API function
761 * implementing the SetTimer service see efi_set_timer_ext.
762 *
Mario Six8fac2912018-07-10 08:40:17 +0200763 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200764 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200765efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200766 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100767{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100768 /* Check that the event is valid */
769 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
770 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100771
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200772 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200773 * The parameter defines a multiple of 100 ns.
774 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200775 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200776 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100777
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100778 switch (type) {
779 case EFI_TIMER_STOP:
780 event->trigger_next = -1ULL;
781 break;
782 case EFI_TIMER_PERIODIC:
783 case EFI_TIMER_RELATIVE:
784 event->trigger_next = timer_get_us() + trigger_time;
785 break;
786 default:
787 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100788 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100789 event->trigger_type = type;
790 event->trigger_time = trigger_time;
791 event->is_signaled = false;
792 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100793}
794
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200795/**
Mario Six8fac2912018-07-10 08:40:17 +0200796 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
797 * event
798 * @event: event for which the timer is set
799 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200800 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200801 *
802 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200803 *
Mario Six8fac2912018-07-10 08:40:17 +0200804 * See the Unified Extensible Firmware Interface (UEFI) specification for
805 * details.
806 *
807 *
808 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200809 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200810static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
811 enum efi_timer_delay type,
812 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200813{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900814 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200815 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
816}
817
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200818/**
Mario Six8fac2912018-07-10 08:40:17 +0200819 * efi_wait_for_event() - wait for events to be signaled
820 * @num_events: number of events to be waited for
821 * @event: events to be waited for
822 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200823 *
824 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200825 *
Mario Six8fac2912018-07-10 08:40:17 +0200826 * See the Unified Extensible Firmware Interface (UEFI) specification for
827 * details.
828 *
829 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200830 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100831static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200832 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100833 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100834{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100835 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100836
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100837 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100838
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200839 /* Check parameters */
840 if (!num_events || !event)
841 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200842 /* Check TPL */
843 if (efi_tpl != TPL_APPLICATION)
844 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200845 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100846 if (efi_is_event(event[i]) != EFI_SUCCESS)
847 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200848 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
849 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200850 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100851 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200852 }
853
854 /* Wait for signal */
855 for (;;) {
856 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200857 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200858 goto out;
859 }
860 /* Allow events to occur. */
861 efi_timer_check();
862 }
863
864out:
865 /*
866 * Reset the signal which is passed to the caller to allow periodic
867 * events to occur.
868 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200869 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200870 if (index)
871 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100872
873 return EFI_EXIT(EFI_SUCCESS);
874}
875
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200876/**
Mario Six8fac2912018-07-10 08:40:17 +0200877 * efi_signal_event_ext() - signal an EFI event
878 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200879 *
880 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200881 *
882 * See the Unified Extensible Firmware Interface (UEFI) specification for
883 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 *
885 * This functions sets the signaled state of the event and queues the
886 * notification function for execution.
887 *
Mario Six8fac2912018-07-10 08:40:17 +0200888 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200889 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200890static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100891{
892 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100893 if (efi_is_event(event) != EFI_SUCCESS)
894 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100895 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100896 return EFI_EXIT(EFI_SUCCESS);
897}
898
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200899/**
Mario Six8fac2912018-07-10 08:40:17 +0200900 * efi_close_event() - close an EFI event
901 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200902 *
903 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200904 *
Mario Six8fac2912018-07-10 08:40:17 +0200905 * See the Unified Extensible Firmware Interface (UEFI) specification for
906 * details.
907 *
908 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200909 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200910static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100911{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200912 struct efi_register_notify_event *item, *next;
913
Alexander Grafc15d9212016-03-04 01:09:59 +0100914 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100915 if (efi_is_event(event) != EFI_SUCCESS)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200917
918 /* Remove protocol notify registrations for the event */
919 list_for_each_entry_safe(item, next, &efi_register_notify_events,
920 link) {
921 if (event == item->event) {
922 list_del(&item->link);
923 free(item);
924 }
925 }
926
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100927 list_del(&event->link);
928 free(event);
929 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100930}
931
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200932/**
Mario Six8fac2912018-07-10 08:40:17 +0200933 * efi_check_event() - check if an event is signaled
934 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200935 *
936 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200937 *
938 * See the Unified Extensible Firmware Interface (UEFI) specification for
939 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200940 *
Mario Six8fac2912018-07-10 08:40:17 +0200941 * If an event is not signaled yet, the notification function is queued. The
942 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200943 *
Mario Six8fac2912018-07-10 08:40:17 +0200944 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200945 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200946static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100947{
948 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200949 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100950 if (efi_is_event(event) != EFI_SUCCESS ||
951 event->type & EVT_NOTIFY_SIGNAL)
952 return EFI_EXIT(EFI_INVALID_PARAMETER);
953 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100954 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100955 if (event->is_signaled) {
956 event->is_signaled = false;
957 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200958 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100959 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100960}
961
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200962/**
Mario Six8fac2912018-07-10 08:40:17 +0200963 * efi_search_obj() - find the internal EFI object for a handle
964 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200965 *
Mario Six8fac2912018-07-10 08:40:17 +0200966 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200967 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100968struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200969{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100970 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200971
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +0200972 if (!handle)
973 return NULL;
974
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100975 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200976 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200977 return efiobj;
978 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200979 return NULL;
980}
981
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200982/**
Mario Six8fac2912018-07-10 08:40:17 +0200983 * efi_open_protocol_info_entry() - create open protocol info entry and add it
984 * to a protocol
985 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100986 *
Mario Six8fac2912018-07-10 08:40:17 +0200987 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100988 */
989static struct efi_open_protocol_info_entry *efi_create_open_info(
990 struct efi_handler *handler)
991{
992 struct efi_open_protocol_info_item *item;
993
994 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
995 if (!item)
996 return NULL;
997 /* Append the item to the open protocol info list. */
998 list_add_tail(&item->link, &handler->open_infos);
999
1000 return &item->info;
1001}
1002
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001003/**
Mario Six8fac2912018-07-10 08:40:17 +02001004 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1005 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001006 *
Mario Six8fac2912018-07-10 08:40:17 +02001007 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001008 */
1009static efi_status_t efi_delete_open_info(
1010 struct efi_open_protocol_info_item *item)
1011{
1012 list_del(&item->link);
1013 free(item);
1014 return EFI_SUCCESS;
1015}
1016
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001017/**
Mario Six8fac2912018-07-10 08:40:17 +02001018 * efi_add_protocol() - install new protocol on a handle
1019 * @handle: handle on which the protocol shall be installed
1020 * @protocol: GUID of the protocol to be installed
1021 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001022 *
Mario Six8fac2912018-07-10 08:40:17 +02001023 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001024 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001025efi_status_t efi_add_protocol(const efi_handle_t handle,
1026 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001027 void *protocol_interface)
1028{
1029 struct efi_object *efiobj;
1030 struct efi_handler *handler;
1031 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001032 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001033
1034 efiobj = efi_search_obj(handle);
1035 if (!efiobj)
1036 return EFI_INVALID_PARAMETER;
1037 ret = efi_search_protocol(handle, protocol, NULL);
1038 if (ret != EFI_NOT_FOUND)
1039 return EFI_INVALID_PARAMETER;
1040 handler = calloc(1, sizeof(struct efi_handler));
1041 if (!handler)
1042 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001043 handler->guid = protocol;
1044 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001045 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001046 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001047
1048 /* Notify registered events */
1049 list_for_each_entry(event, &efi_register_notify_events, link) {
1050 if (!guidcmp(protocol, &event->protocol))
1051 efi_signal_event(event->event, true);
1052 }
1053
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001054 if (!guidcmp(&efi_guid_device_path, protocol))
1055 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001056 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001057}
1058
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001059/**
Mario Six8fac2912018-07-10 08:40:17 +02001060 * efi_install_protocol_interface() - install protocol interface
1061 * @handle: handle on which the protocol shall be installed
1062 * @protocol: GUID of the protocol to be installed
1063 * @protocol_interface_type: type of the interface to be installed,
1064 * always EFI_NATIVE_INTERFACE
1065 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001066 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001067 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001068 *
Mario Six8fac2912018-07-10 08:40:17 +02001069 * See the Unified Extensible Firmware Interface (UEFI) specification for
1070 * details.
1071 *
1072 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001073 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001074static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001075 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001076 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001077{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001078 efi_status_t r;
1079
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001080 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1081 protocol_interface);
1082
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001083 if (!handle || !protocol ||
1084 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1085 r = EFI_INVALID_PARAMETER;
1086 goto out;
1087 }
1088
1089 /* Create new handle if requested. */
1090 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001091 r = efi_create_handle(handle);
1092 if (r != EFI_SUCCESS)
1093 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001094 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001095 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001096 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001097 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001098 /* Add new protocol */
1099 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001100out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001101 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001102}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001103
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001104/**
Mario Six8fac2912018-07-10 08:40:17 +02001105 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001106 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001107 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001108 * @number_of_drivers: number of child controllers
1109 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001110 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001111 * The allocated buffer has to be freed with free().
1112 *
Mario Six8fac2912018-07-10 08:40:17 +02001113 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001114 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001115static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001116 const efi_guid_t *protocol,
1117 efi_uintn_t *number_of_drivers,
1118 efi_handle_t **driver_handle_buffer)
1119{
1120 struct efi_handler *handler;
1121 struct efi_open_protocol_info_item *item;
1122 efi_uintn_t count = 0, i;
1123 bool duplicate;
1124
1125 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001126 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001127 if (protocol && guidcmp(handler->guid, protocol))
1128 continue;
1129 list_for_each_entry(item, &handler->open_infos, link) {
1130 if (item->info.attributes &
1131 EFI_OPEN_PROTOCOL_BY_DRIVER)
1132 ++count;
1133 }
1134 }
1135 /*
1136 * Create buffer. In case of duplicate driver assignments the buffer
1137 * will be too large. But that does not harm.
1138 */
1139 *number_of_drivers = 0;
1140 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1141 if (!*driver_handle_buffer)
1142 return EFI_OUT_OF_RESOURCES;
1143 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001144 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001145 if (protocol && guidcmp(handler->guid, protocol))
1146 continue;
1147 list_for_each_entry(item, &handler->open_infos, link) {
1148 if (item->info.attributes &
1149 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1150 /* Check this is a new driver */
1151 duplicate = false;
1152 for (i = 0; i < *number_of_drivers; ++i) {
1153 if ((*driver_handle_buffer)[i] ==
1154 item->info.agent_handle)
1155 duplicate = true;
1156 }
1157 /* Copy handle to buffer */
1158 if (!duplicate) {
1159 i = (*number_of_drivers)++;
1160 (*driver_handle_buffer)[i] =
1161 item->info.agent_handle;
1162 }
1163 }
1164 }
1165 }
1166 return EFI_SUCCESS;
1167}
1168
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001169/**
Mario Six8fac2912018-07-10 08:40:17 +02001170 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001171 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001172 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001173 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001174 *
1175 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001176 *
1177 * See the Unified Extensible Firmware Interface (UEFI) specification for
1178 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001179 *
Mario Six8fac2912018-07-10 08:40:17 +02001180 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001181 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001182static efi_status_t efi_disconnect_all_drivers
1183 (efi_handle_t handle,
1184 const efi_guid_t *protocol,
1185 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001186{
1187 efi_uintn_t number_of_drivers;
1188 efi_handle_t *driver_handle_buffer;
1189 efi_status_t r, ret;
1190
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001191 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001192 &driver_handle_buffer);
1193 if (ret != EFI_SUCCESS)
1194 return ret;
1195
1196 ret = EFI_NOT_FOUND;
1197 while (number_of_drivers) {
1198 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001199 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001200 driver_handle_buffer[--number_of_drivers],
1201 child_handle));
1202 if (r == EFI_SUCCESS)
1203 ret = r;
1204 }
1205 free(driver_handle_buffer);
1206 return ret;
1207}
1208
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001209/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001210 * efi_uninstall_protocol() - uninstall protocol interface
1211 *
Mario Six8fac2912018-07-10 08:40:17 +02001212 * @handle: handle from which the protocol shall be removed
1213 * @protocol: GUID of the protocol to be removed
1214 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001215 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001216 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001217 *
1218 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001219 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001220static efi_status_t efi_uninstall_protocol
1221 (efi_handle_t handle, const efi_guid_t *protocol,
1222 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001223{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001224 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001225 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001226 struct efi_open_protocol_info_item *item;
1227 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001228 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001229
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001230 /* Check handle */
1231 efiobj = efi_search_obj(handle);
1232 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001233 r = EFI_INVALID_PARAMETER;
1234 goto out;
1235 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001236 /* Find the protocol on the handle */
1237 r = efi_search_protocol(handle, protocol, &handler);
1238 if (r != EFI_SUCCESS)
1239 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001240 /* Disconnect controllers */
1241 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1242 if (!list_empty(&handler->open_infos)) {
1243 r = EFI_ACCESS_DENIED;
1244 goto out;
1245 }
1246 /* Close protocol */
1247 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1248 if (item->info.attributes ==
1249 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1250 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1251 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1252 list_del(&item->link);
1253 }
1254 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001255 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001256 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001257 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001258 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001259out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001260 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001261}
1262
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001263/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001264 * efi_uninstall_protocol_interface() - uninstall protocol interface
1265 * @handle: handle from which the protocol shall be removed
1266 * @protocol: GUID of the protocol to be removed
1267 * @protocol_interface: interface to be removed
1268 *
1269 * This function implements the UninstallProtocolInterface service.
1270 *
1271 * See the Unified Extensible Firmware Interface (UEFI) specification for
1272 * details.
1273 *
1274 * Return: status code
1275 */
1276static efi_status_t EFIAPI efi_uninstall_protocol_interface
1277 (efi_handle_t handle, const efi_guid_t *protocol,
1278 void *protocol_interface)
1279{
1280 efi_status_t ret;
1281
1282 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1283
1284 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1285 if (ret != EFI_SUCCESS)
1286 goto out;
1287
1288 /* If the last protocol has been removed, delete the handle. */
1289 if (list_empty(&handle->protocols)) {
1290 list_del(&handle->link);
1291 free(handle);
1292 }
1293out:
1294 return EFI_EXIT(ret);
1295}
1296
1297/**
Mario Six8fac2912018-07-10 08:40:17 +02001298 * efi_register_protocol_notify() - register an event for notification when a
1299 * protocol is installed.
1300 * @protocol: GUID of the protocol whose installation shall be notified
1301 * @event: event to be signaled upon installation of the protocol
1302 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001303 *
1304 * This function implements the RegisterProtocolNotify service.
1305 * See the Unified Extensible Firmware Interface (UEFI) specification
1306 * for details.
1307 *
Mario Six8fac2912018-07-10 08:40:17 +02001308 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001309 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001310static efi_status_t EFIAPI efi_register_protocol_notify(
1311 const efi_guid_t *protocol,
1312 struct efi_event *event,
1313 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001314{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001315 struct efi_register_notify_event *item;
1316 efi_status_t ret = EFI_SUCCESS;
1317
Rob Clark238f88c2017-09-13 18:05:41 -04001318 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001319
1320 if (!protocol || !event || !registration) {
1321 ret = EFI_INVALID_PARAMETER;
1322 goto out;
1323 }
1324
1325 item = calloc(1, sizeof(struct efi_register_notify_event));
1326 if (!item) {
1327 ret = EFI_OUT_OF_RESOURCES;
1328 goto out;
1329 }
1330
1331 item->event = event;
1332 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
1333
1334 list_add_tail(&item->link, &efi_register_notify_events);
1335
1336 *registration = item;
1337out:
1338 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001339}
1340
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001341/**
Mario Six8fac2912018-07-10 08:40:17 +02001342 * efi_search() - determine if an EFI handle implements a protocol
1343 * @search_type: selection criterion
1344 * @protocol: GUID of the protocol
1345 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001346 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001347 *
1348 * See the documentation of the LocateHandle service in the UEFI specification.
1349 *
Mario Six8fac2912018-07-10 08:40:17 +02001350 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001351 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001352static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001353 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001354{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001355 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001356
1357 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001358 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001359 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001360 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001361 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001362 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001363 return (ret != EFI_SUCCESS);
1364 default:
1365 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001366 return -1;
1367 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001368}
1369
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001370/**
Mario Six8fac2912018-07-10 08:40:17 +02001371 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001372 *
1373 * @search_type: selection criterion
1374 * @protocol: GUID of the protocol
1375 * @search_key: registration key
1376 * @buffer_size: size of the buffer to receive the handles in bytes
1377 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001378 *
1379 * This function is meant for U-Boot internal calls. For the API implementation
1380 * of the LocateHandle service see efi_locate_handle_ext.
1381 *
Mario Six8fac2912018-07-10 08:40:17 +02001382 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001383 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001384static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001385 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001386 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001387 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001388{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001389 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001390 efi_uintn_t size = 0;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001391 struct efi_register_notify_event *item, *event = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001392
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001393 /* Check parameters */
1394 switch (search_type) {
1395 case ALL_HANDLES:
1396 break;
1397 case BY_REGISTER_NOTIFY:
1398 if (!search_key)
1399 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001400 /* Check that the registration key is valid */
1401 list_for_each_entry(item, &efi_register_notify_events, link) {
1402 if (item ==
1403 (struct efi_register_notify_event *)search_key) {
1404 event = item;
1405 break;
1406 }
1407 }
1408 if (!event)
1409 return EFI_INVALID_PARAMETER;
1410
1411 protocol = &event->protocol;
1412 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001413 case BY_PROTOCOL:
1414 if (!protocol)
1415 return EFI_INVALID_PARAMETER;
1416 break;
1417 default:
1418 return EFI_INVALID_PARAMETER;
1419 }
1420
Alexander Grafc15d9212016-03-04 01:09:59 +01001421 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001422 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001423 if (!efi_search(search_type, protocol, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001424 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001425 }
1426
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001427 if (size == 0)
1428 return EFI_NOT_FOUND;
1429
1430 if (!buffer_size)
1431 return EFI_INVALID_PARAMETER;
1432
Alexander Grafc15d9212016-03-04 01:09:59 +01001433 if (*buffer_size < size) {
1434 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001435 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001436 }
1437
Rob Clarkcdee3372017-08-06 14:10:07 -04001438 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001439
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001440 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001441 if (!buffer)
1442 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001443
Alexander Grafc15d9212016-03-04 01:09:59 +01001444 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001445 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001446 if (!efi_search(search_type, protocol, efiobj))
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001447 *buffer++ = efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01001448 }
1449
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001450 return EFI_SUCCESS;
1451}
1452
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001453/**
Mario Six8fac2912018-07-10 08:40:17 +02001454 * efi_locate_handle_ext() - locate handles implementing a protocol.
1455 * @search_type: selection criterion
1456 * @protocol: GUID of the protocol
1457 * @search_key: registration key
1458 * @buffer_size: size of the buffer to receive the handles in bytes
1459 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001460 *
1461 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001462 *
Mario Six8fac2912018-07-10 08:40:17 +02001463 * See the Unified Extensible Firmware Interface (UEFI) specification for
1464 * details.
1465 *
1466 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001467 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001468static efi_status_t EFIAPI efi_locate_handle_ext(
1469 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001470 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001471 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001472{
Rob Clark238f88c2017-09-13 18:05:41 -04001473 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001474 buffer_size, buffer);
1475
1476 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1477 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001478}
1479
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001480/**
Mario Six8fac2912018-07-10 08:40:17 +02001481 * efi_remove_configuration_table() - collapses configuration table entries,
1482 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001483 *
Mario Six8fac2912018-07-10 08:40:17 +02001484 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001485 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001486static void efi_remove_configuration_table(int i)
1487{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001488 struct efi_configuration_table *this = &systab.tables[i];
1489 struct efi_configuration_table *next = &systab.tables[i + 1];
1490 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001491
1492 memmove(this, next, (ulong)end - (ulong)next);
1493 systab.nr_tables--;
1494}
1495
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001496/**
Mario Six8fac2912018-07-10 08:40:17 +02001497 * efi_install_configuration_table() - adds, updates, or removes a
1498 * configuration table
1499 * @guid: GUID of the installed table
1500 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001501 *
1502 * This function is used for internal calls. For the API implementation of the
1503 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1504 *
Mario Six8fac2912018-07-10 08:40:17 +02001505 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001506 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001507efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1508 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001509{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001510 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001511 int i;
1512
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001513 if (!guid)
1514 return EFI_INVALID_PARAMETER;
1515
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001516 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001517 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001518 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001519 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001520 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001521 else
1522 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001523 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001524 }
1525 }
1526
Alexander Graffe3366f2017-07-26 13:41:04 +02001527 if (!table)
1528 return EFI_NOT_FOUND;
1529
Alexander Grafc15d9212016-03-04 01:09:59 +01001530 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001531 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001532 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001533
1534 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001535 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1536 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001537 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001538
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001539out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001540 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001541 efi_update_table_header_crc32(&systab.hdr);
1542
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001543 /* Notify that the configuration table was changed */
1544 list_for_each_entry(evt, &efi_events, link) {
1545 if (evt->group && !guidcmp(evt->group, guid)) {
1546 efi_signal_event(evt, false);
1547 break;
1548 }
1549 }
1550
Alexander Grafc5c11632016-08-19 01:23:24 +02001551 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001552}
1553
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001554/**
Mario Six8fac2912018-07-10 08:40:17 +02001555 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1556 * configuration table.
1557 * @guid: GUID of the installed table
1558 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001559 *
1560 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001561 *
Mario Six8fac2912018-07-10 08:40:17 +02001562 * See the Unified Extensible Firmware Interface (UEFI) specification for
1563 * details.
1564 *
1565 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001566 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001567static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1568 void *table)
1569{
Rob Clark238f88c2017-09-13 18:05:41 -04001570 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001571 return EFI_EXIT(efi_install_configuration_table(guid, table));
1572}
1573
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001574/**
Mario Six8fac2912018-07-10 08:40:17 +02001575 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001576 *
1577 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001578 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001579 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001580 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1581 * code is returned.
1582 *
1583 * @device_path: device path of the loaded image
1584 * @file_path: file path of the loaded image
1585 * @handle_ptr: handle of the loaded image
1586 * @info_ptr: loaded image protocol
1587 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001588 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001589efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1590 struct efi_device_path *file_path,
1591 struct efi_loaded_image_obj **handle_ptr,
1592 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001593{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001594 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001595 struct efi_loaded_image *info = NULL;
1596 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001597 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001598
1599 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1600 *handle_ptr = NULL;
1601 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001602
1603 info = calloc(1, sizeof(*info));
1604 if (!info)
1605 return EFI_OUT_OF_RESOURCES;
1606 obj = calloc(1, sizeof(*obj));
1607 if (!obj) {
1608 free(info);
1609 return EFI_OUT_OF_RESOURCES;
1610 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001611 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001612
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001613 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001614 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001615
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001616 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001617 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001618 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001619
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001620 if (device_path) {
1621 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001622
1623 dp = efi_dp_append(device_path, file_path);
1624 if (!dp) {
1625 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001626 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001627 }
1628 } else {
1629 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001630 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001631 ret = efi_add_protocol(&obj->header,
1632 &efi_guid_loaded_image_device_path, dp);
1633 if (ret != EFI_SUCCESS)
1634 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001635
1636 /*
1637 * When asking for the loaded_image interface, just
1638 * return handle which points to loaded_image_info
1639 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001640 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001641 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001642 if (ret != EFI_SUCCESS)
1643 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001644
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001645 *info_ptr = info;
1646 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001647
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001648 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001649failure:
1650 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001651 efi_delete_handle(&obj->header);
1652 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001653 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001654}
1655
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001656/**
Mario Six8fac2912018-07-10 08:40:17 +02001657 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001658 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001659 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1660 * callers obligation to update the memory type as needed.
1661 *
1662 * @file_path: the path of the image to load
1663 * @buffer: buffer containing the loaded image
1664 * @size: size of the loaded image
1665 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001666 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001667static
Rob Clarkc84c1102017-09-13 18:05:38 -04001668efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001669 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001670{
1671 struct efi_file_info *info = NULL;
1672 struct efi_file_handle *f;
1673 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001674 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001675 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001676
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001677 /* In case of failure nothing is returned */
1678 *buffer = NULL;
1679 *size = 0;
1680
1681 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001682 f = efi_file_from_path(file_path);
1683 if (!f)
1684 return EFI_DEVICE_ERROR;
1685
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001686 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001687 bs = 0;
1688 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1689 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001690 if (ret != EFI_BUFFER_TOO_SMALL) {
1691 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001692 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001693 }
Rob Clark857a1222017-09-13 18:05:35 -04001694
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001695 info = malloc(bs);
1696 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1697 info));
1698 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001699 goto error;
1700
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001701 /*
1702 * When reading the file we do not yet know if it contains an
1703 * application, a boottime driver, or a runtime driver. So here we
1704 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1705 * update the reservation according to the image type.
1706 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001707 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001708 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1709 EFI_BOOT_SERVICES_DATA,
1710 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001711 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001712 ret = EFI_OUT_OF_RESOURCES;
1713 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001714 }
1715
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001716 /* Read file */
1717 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1718 if (ret != EFI_SUCCESS)
1719 efi_free_pages(addr, efi_size_in_pages(bs));
1720 *buffer = (void *)(uintptr_t)addr;
1721 *size = bs;
1722error:
1723 EFI_CALL(f->close(f));
1724 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001725 return ret;
1726}
1727
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001728/**
Mario Six8fac2912018-07-10 08:40:17 +02001729 * efi_load_image() - load an EFI image into memory
1730 * @boot_policy: true for request originating from the boot manager
1731 * @parent_image: the caller's image handle
1732 * @file_path: the path of the image to load
1733 * @source_buffer: memory location from which the image is installed
1734 * @source_size: size of the memory area from which the image is installed
1735 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001736 *
1737 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001738 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001739 * See the Unified Extensible Firmware Interface (UEFI) specification
1740 * for details.
1741 *
Mario Six8fac2912018-07-10 08:40:17 +02001742 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001743 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001744efi_status_t EFIAPI efi_load_image(bool boot_policy,
1745 efi_handle_t parent_image,
1746 struct efi_device_path *file_path,
1747 void *source_buffer,
1748 efi_uintn_t source_size,
1749 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001750{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001751 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001752 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001753 struct efi_loaded_image_obj **image_obj =
1754 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001755 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001756 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001757
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001758 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001759 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001760
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001761 if (!image_handle || !efi_search_obj(parent_image)) {
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001762 ret = EFI_INVALID_PARAMETER;
1763 goto error;
1764 }
1765
1766 if (!source_buffer && !file_path) {
1767 ret = EFI_NOT_FOUND;
1768 goto error;
1769 }
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001770 /* The parent image handle must refer to a loaded image */
1771 if (!parent_image->type) {
1772 ret = EFI_INVALID_PARAMETER;
1773 goto error;
1774 }
Rob Clark857a1222017-09-13 18:05:35 -04001775
1776 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001777 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001778 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001779 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001780 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001781 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001782 if (!source_size) {
1783 ret = EFI_LOAD_ERROR;
1784 goto error;
1785 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001786 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001787 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001788 /* split file_path which contains both the device and file parts */
1789 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001790 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001791 if (ret == EFI_SUCCESS)
1792 ret = efi_load_pe(*image_obj, dest_buffer, info);
1793 if (!source_buffer)
1794 /* Release buffer to which file was loaded */
1795 efi_free_pages((uintptr_t)dest_buffer,
1796 efi_size_in_pages(source_size));
1797 if (ret == EFI_SUCCESS) {
1798 info->system_table = &systab;
1799 info->parent_handle = parent_image;
1800 } else {
1801 /* The image is invalid. Release all associated resources. */
1802 efi_delete_handle(*image_handle);
1803 *image_handle = NULL;
1804 free(info);
1805 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001806error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001807 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001808}
1809
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001810/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001811 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1812 */
1813static void efi_exit_caches(void)
1814{
1815#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1816 /*
1817 * Grub on 32bit ARM needs to have caches disabled before jumping into
1818 * a zImage, but does not know of all cache layers. Give it a hand.
1819 */
1820 if (efi_is_direct_boot)
1821 cleanup_before_linux();
1822#endif
1823}
1824
1825/**
Mario Six8fac2912018-07-10 08:40:17 +02001826 * efi_exit_boot_services() - stop all boot services
1827 * @image_handle: handle of the loaded image
1828 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001829 *
1830 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001831 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001832 * See the Unified Extensible Firmware Interface (UEFI) specification
1833 * for details.
1834 *
Mario Six8fac2912018-07-10 08:40:17 +02001835 * All timer events are disabled. For exit boot services events the
1836 * notification function is called. The boot services are disabled in the
1837 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001838 *
Mario Six8fac2912018-07-10 08:40:17 +02001839 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001840 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001841static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001842 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001843{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001844 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001845
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001846 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001847
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001848 /* Check that the caller has read the current memory map */
1849 if (map_key != efi_memory_map_key)
1850 return EFI_INVALID_PARAMETER;
1851
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001852 /* Make sure that notification functions are not called anymore */
1853 efi_tpl = TPL_HIGH_LEVEL;
1854
1855 /* Check if ExitBootServices has already been called */
1856 if (!systab.boottime)
1857 return EFI_EXIT(EFI_SUCCESS);
1858
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001859 /* Add related events to the event group */
1860 list_for_each_entry(evt, &efi_events, link) {
1861 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1862 evt->group = &efi_guid_event_group_exit_boot_services;
1863 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001864 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001865 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001866 if (evt->group &&
1867 !guidcmp(evt->group,
1868 &efi_guid_event_group_exit_boot_services)) {
1869 efi_signal_event(evt, false);
1870 break;
1871 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001872 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001873
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001874 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001875
Alexander Graf2ebeb442016-11-17 01:02:57 +01001876 board_quiesce_devices();
1877
Alexander Graffa5246b2018-11-15 21:23:47 +01001878 /* Fix up caches for EFI payloads if necessary */
1879 efi_exit_caches();
1880
Alexander Grafc15d9212016-03-04 01:09:59 +01001881 /* This stops all lingering devices */
1882 bootm_disable_interrupts();
1883
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001884 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001885 systab.con_in_handle = NULL;
1886 systab.con_in = NULL;
1887 systab.con_out_handle = NULL;
1888 systab.con_out = NULL;
1889 systab.stderr_handle = NULL;
1890 systab.std_err = NULL;
1891 systab.boottime = NULL;
1892
1893 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001894 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001895
Alexander Grafc15d9212016-03-04 01:09:59 +01001896 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001897 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001898 WATCHDOG_RESET();
1899
1900 return EFI_EXIT(EFI_SUCCESS);
1901}
1902
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001903/**
Mario Six8fac2912018-07-10 08:40:17 +02001904 * efi_get_next_monotonic_count() - get next value of the counter
1905 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001906 *
1907 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001908 *
1909 * See the Unified Extensible Firmware Interface (UEFI) specification for
1910 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001911 *
Mario Six8fac2912018-07-10 08:40:17 +02001912 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001913 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001914static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1915{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001916 static uint64_t mono;
1917
Alexander Grafc15d9212016-03-04 01:09:59 +01001918 EFI_ENTRY("%p", count);
1919 *count = mono++;
1920 return EFI_EXIT(EFI_SUCCESS);
1921}
1922
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001923/**
Mario Six8fac2912018-07-10 08:40:17 +02001924 * efi_stall() - sleep
1925 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001926 *
Mario Six8fac2912018-07-10 08:40:17 +02001927 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001928 *
Mario Six8fac2912018-07-10 08:40:17 +02001929 * See the Unified Extensible Firmware Interface (UEFI) specification for
1930 * details.
1931 *
1932 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001933 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001934static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1935{
1936 EFI_ENTRY("%ld", microseconds);
1937 udelay(microseconds);
1938 return EFI_EXIT(EFI_SUCCESS);
1939}
1940
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001941/**
Mario Six8fac2912018-07-10 08:40:17 +02001942 * efi_set_watchdog_timer() - reset the watchdog timer
1943 * @timeout: seconds before reset by watchdog
1944 * @watchdog_code: code to be logged when resetting
1945 * @data_size: size of buffer in bytes
1946 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001947 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001948 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001949 *
Mario Six8fac2912018-07-10 08:40:17 +02001950 * See the Unified Extensible Firmware Interface (UEFI) specification for
1951 * details.
1952 *
1953 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001954 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001955static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1956 uint64_t watchdog_code,
1957 unsigned long data_size,
1958 uint16_t *watchdog_data)
1959{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001960 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001961 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001962 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001963}
1964
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001965/**
Mario Six8fac2912018-07-10 08:40:17 +02001966 * efi_close_protocol() - close a protocol
1967 * @handle: handle on which the protocol shall be closed
1968 * @protocol: GUID of the protocol to close
1969 * @agent_handle: handle of the driver
1970 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001971 *
1972 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001973 *
Mario Six8fac2912018-07-10 08:40:17 +02001974 * See the Unified Extensible Firmware Interface (UEFI) specification for
1975 * details.
1976 *
1977 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001978 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001979static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001980 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001981 efi_handle_t agent_handle,
1982 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001983{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001984 struct efi_handler *handler;
1985 struct efi_open_protocol_info_item *item;
1986 struct efi_open_protocol_info_item *pos;
1987 efi_status_t r;
1988
Rob Clark238f88c2017-09-13 18:05:41 -04001989 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001990 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001991
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001992 if (!efi_search_obj(agent_handle) ||
1993 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001994 r = EFI_INVALID_PARAMETER;
1995 goto out;
1996 }
1997 r = efi_search_protocol(handle, protocol, &handler);
1998 if (r != EFI_SUCCESS)
1999 goto out;
2000
2001 r = EFI_NOT_FOUND;
2002 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2003 if (item->info.agent_handle == agent_handle &&
2004 item->info.controller_handle == controller_handle) {
2005 efi_delete_open_info(item);
2006 r = EFI_SUCCESS;
2007 break;
2008 }
2009 }
2010out:
2011 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002012}
2013
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002014/**
Mario Six8fac2912018-07-10 08:40:17 +02002015 * efi_open_protocol_information() - provide information about then open status
2016 * of a protocol on a handle
2017 * @handle: handle for which the information shall be retrieved
2018 * @protocol: GUID of the protocol
2019 * @entry_buffer: buffer to receive the open protocol information
2020 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002021 *
2022 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002023 *
2024 * See the Unified Extensible Firmware Interface (UEFI) specification for
2025 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002026 *
Mario Six8fac2912018-07-10 08:40:17 +02002027 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002028 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002029static efi_status_t EFIAPI efi_open_protocol_information(
2030 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002031 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002032 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002033{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002034 unsigned long buffer_size;
2035 unsigned long count;
2036 struct efi_handler *handler;
2037 struct efi_open_protocol_info_item *item;
2038 efi_status_t r;
2039
Rob Clark238f88c2017-09-13 18:05:41 -04002040 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002041 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002042
2043 /* Check parameters */
2044 if (!entry_buffer) {
2045 r = EFI_INVALID_PARAMETER;
2046 goto out;
2047 }
2048 r = efi_search_protocol(handle, protocol, &handler);
2049 if (r != EFI_SUCCESS)
2050 goto out;
2051
2052 /* Count entries */
2053 count = 0;
2054 list_for_each_entry(item, &handler->open_infos, link) {
2055 if (item->info.open_count)
2056 ++count;
2057 }
2058 *entry_count = count;
2059 *entry_buffer = NULL;
2060 if (!count) {
2061 r = EFI_SUCCESS;
2062 goto out;
2063 }
2064
2065 /* Copy entries */
2066 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002067 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002068 (void **)entry_buffer);
2069 if (r != EFI_SUCCESS)
2070 goto out;
2071 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2072 if (item->info.open_count)
2073 (*entry_buffer)[--count] = item->info;
2074 }
2075out:
2076 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002077}
2078
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002079/**
Mario Six8fac2912018-07-10 08:40:17 +02002080 * efi_protocols_per_handle() - get protocols installed on a handle
2081 * @handle: handle for which the information is retrieved
2082 * @protocol_buffer: buffer with protocol GUIDs
2083 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002084 *
2085 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002086 *
Mario Six8fac2912018-07-10 08:40:17 +02002087 * See the Unified Extensible Firmware Interface (UEFI) specification for
2088 * details.
2089 *
2090 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002091 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002092static efi_status_t EFIAPI efi_protocols_per_handle(
2093 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002094 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002095{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002096 unsigned long buffer_size;
2097 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002098 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002099 efi_status_t r;
2100
Alexander Grafc15d9212016-03-04 01:09:59 +01002101 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2102 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002103
2104 if (!handle || !protocol_buffer || !protocol_buffer_count)
2105 return EFI_EXIT(EFI_INVALID_PARAMETER);
2106
2107 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002108 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002109
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002110 efiobj = efi_search_obj(handle);
2111 if (!efiobj)
2112 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002113
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002114 /* Count protocols */
2115 list_for_each(protocol_handle, &efiobj->protocols) {
2116 ++*protocol_buffer_count;
2117 }
2118
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002119 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002120 if (*protocol_buffer_count) {
2121 size_t j = 0;
2122
2123 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002124 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002125 (void **)protocol_buffer);
2126 if (r != EFI_SUCCESS)
2127 return EFI_EXIT(r);
2128 list_for_each(protocol_handle, &efiobj->protocols) {
2129 struct efi_handler *protocol;
2130
2131 protocol = list_entry(protocol_handle,
2132 struct efi_handler, link);
2133 (*protocol_buffer)[j] = (void *)protocol->guid;
2134 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002135 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002136 }
2137
2138 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002139}
2140
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002141/**
Mario Six8fac2912018-07-10 08:40:17 +02002142 * efi_locate_handle_buffer() - locate handles implementing a protocol
2143 * @search_type: selection criterion
2144 * @protocol: GUID of the protocol
2145 * @search_key: registration key
2146 * @no_handles: number of returned handles
2147 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002148 *
2149 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002150 *
Mario Six8fac2912018-07-10 08:40:17 +02002151 * See the Unified Extensible Firmware Interface (UEFI) specification for
2152 * details.
2153 *
2154 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002155 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002156static efi_status_t EFIAPI efi_locate_handle_buffer(
2157 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002158 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002159 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002160{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002161 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002162 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002163
Rob Clark238f88c2017-09-13 18:05:41 -04002164 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002165 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002166
2167 if (!no_handles || !buffer) {
2168 r = EFI_INVALID_PARAMETER;
2169 goto out;
2170 }
2171 *no_handles = 0;
2172 *buffer = NULL;
2173 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2174 *buffer);
2175 if (r != EFI_BUFFER_TOO_SMALL)
2176 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002177 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002178 (void **)buffer);
2179 if (r != EFI_SUCCESS)
2180 goto out;
2181 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2182 *buffer);
2183 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002184 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002185out:
2186 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002187}
2188
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002189/**
Mario Six8fac2912018-07-10 08:40:17 +02002190 * efi_locate_protocol() - find an interface implementing a protocol
2191 * @protocol: GUID of the protocol
2192 * @registration: registration key passed to the notification function
2193 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002194 *
2195 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002196 *
2197 * See the Unified Extensible Firmware Interface (UEFI) specification for
2198 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002199 *
Mario Six8fac2912018-07-10 08:40:17 +02002200 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002201 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002202static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002203 void *registration,
2204 void **protocol_interface)
2205{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002206 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002207 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002208
Rob Clark238f88c2017-09-13 18:05:41 -04002209 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002210
2211 if (!protocol || !protocol_interface)
2212 return EFI_EXIT(EFI_INVALID_PARAMETER);
2213
2214 list_for_each(lhandle, &efi_obj_list) {
2215 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002216 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002217
2218 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002219
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002220 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002221 if (ret == EFI_SUCCESS) {
2222 *protocol_interface = handler->protocol_interface;
2223 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002224 }
2225 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002226 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002227
2228 return EFI_EXIT(EFI_NOT_FOUND);
2229}
2230
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002231/**
Mario Six8fac2912018-07-10 08:40:17 +02002232 * efi_locate_device_path() - Get the device path and handle of an device
2233 * implementing a protocol
2234 * @protocol: GUID of the protocol
2235 * @device_path: device path
2236 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002237 *
2238 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002239 *
Mario Six8fac2912018-07-10 08:40:17 +02002240 * See the Unified Extensible Firmware Interface (UEFI) specification for
2241 * details.
2242 *
2243 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002244 */
2245static efi_status_t EFIAPI efi_locate_device_path(
2246 const efi_guid_t *protocol,
2247 struct efi_device_path **device_path,
2248 efi_handle_t *device)
2249{
2250 struct efi_device_path *dp;
2251 size_t i;
2252 struct efi_handler *handler;
2253 efi_handle_t *handles;
2254 size_t len, len_dp;
2255 size_t len_best = 0;
2256 efi_uintn_t no_handles;
2257 u8 *remainder;
2258 efi_status_t ret;
2259
2260 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2261
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002262 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002263 ret = EFI_INVALID_PARAMETER;
2264 goto out;
2265 }
2266
2267 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002268 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002269
2270 /* Get all handles implementing the protocol */
2271 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2272 &no_handles, &handles));
2273 if (ret != EFI_SUCCESS)
2274 goto out;
2275
2276 for (i = 0; i < no_handles; ++i) {
2277 /* Find the device path protocol */
2278 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2279 &handler);
2280 if (ret != EFI_SUCCESS)
2281 continue;
2282 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002283 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002284 /*
2285 * This handle can only be a better fit
2286 * if its device path length is longer than the best fit and
2287 * if its device path length is shorter of equal the searched
2288 * device path.
2289 */
2290 if (len_dp <= len_best || len_dp > len)
2291 continue;
2292 /* Check if dp is a subpath of device_path */
2293 if (memcmp(*device_path, dp, len_dp))
2294 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002295 if (!device) {
2296 ret = EFI_INVALID_PARAMETER;
2297 goto out;
2298 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002299 *device = handles[i];
2300 len_best = len_dp;
2301 }
2302 if (len_best) {
2303 remainder = (u8 *)*device_path + len_best;
2304 *device_path = (struct efi_device_path *)remainder;
2305 ret = EFI_SUCCESS;
2306 } else {
2307 ret = EFI_NOT_FOUND;
2308 }
2309out:
2310 return EFI_EXIT(ret);
2311}
2312
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002313/**
Mario Six8fac2912018-07-10 08:40:17 +02002314 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2315 * interfaces
2316 * @handle: handle on which the protocol interfaces shall be installed
2317 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2318 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002319 *
2320 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002321 *
2322 * See the Unified Extensible Firmware Interface (UEFI) specification for
2323 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002324 *
Mario Six8fac2912018-07-10 08:40:17 +02002325 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002326 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002327efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002328 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002329{
2330 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002331
Alexander Graf404a7c82018-06-18 17:23:05 +02002332 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002333 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002334 void *protocol_interface;
2335 efi_status_t r = EFI_SUCCESS;
2336 int i = 0;
2337
2338 if (!handle)
2339 return EFI_EXIT(EFI_INVALID_PARAMETER);
2340
Alexander Graf404a7c82018-06-18 17:23:05 +02002341 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002342 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002343 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002344 if (!protocol)
2345 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002346 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002347 r = EFI_CALL(efi_install_protocol_interface(
2348 handle, protocol,
2349 EFI_NATIVE_INTERFACE,
2350 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002351 if (r != EFI_SUCCESS)
2352 break;
2353 i++;
2354 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002355 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002356 if (r == EFI_SUCCESS)
2357 return EFI_EXIT(r);
2358
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002359 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002360 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002361 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002362 protocol = efi_va_arg(argptr, efi_guid_t*);
2363 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002364 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002365 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002366 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002367 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002368
2369 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002370}
2371
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002372/**
Mario Six8fac2912018-07-10 08:40:17 +02002373 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2374 * interfaces
2375 * @handle: handle from which the protocol interfaces shall be removed
2376 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2377 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002378 *
2379 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002380 *
Mario Six8fac2912018-07-10 08:40:17 +02002381 * See the Unified Extensible Firmware Interface (UEFI) specification for
2382 * details.
2383 *
2384 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002385 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002386static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002387 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002388{
2389 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002390
Alexander Graf404a7c82018-06-18 17:23:05 +02002391 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002392 const efi_guid_t *protocol;
2393 void *protocol_interface;
2394 efi_status_t r = EFI_SUCCESS;
2395 size_t i = 0;
2396
2397 if (!handle)
2398 return EFI_EXIT(EFI_INVALID_PARAMETER);
2399
Alexander Graf404a7c82018-06-18 17:23:05 +02002400 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002401 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002402 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002403 if (!protocol)
2404 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002405 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002406 r = efi_uninstall_protocol(handle, protocol,
2407 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002408 if (r != EFI_SUCCESS)
2409 break;
2410 i++;
2411 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002412 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002413 if (r == EFI_SUCCESS) {
2414 /* If the last protocol has been removed, delete the handle. */
2415 if (list_empty(&handle->protocols)) {
2416 list_del(&handle->link);
2417 free(handle);
2418 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002419 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002420 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002421
2422 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002423 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002424 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002425 protocol = efi_va_arg(argptr, efi_guid_t*);
2426 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002427 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2428 EFI_NATIVE_INTERFACE,
2429 protocol_interface));
2430 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002431 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002432
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002433 /* In case of an error always return EFI_INVALID_PARAMETER */
2434 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002435}
2436
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002437/**
Mario Six8fac2912018-07-10 08:40:17 +02002438 * efi_calculate_crc32() - calculate cyclic redundancy code
2439 * @data: buffer with data
2440 * @data_size: size of buffer in bytes
2441 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002442 *
2443 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002444 *
2445 * See the Unified Extensible Firmware Interface (UEFI) specification for
2446 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002447 *
Mario Six8fac2912018-07-10 08:40:17 +02002448 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002449 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002450static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2451 efi_uintn_t data_size,
2452 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002453{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002454 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002455 *crc32_p = crc32(0, data, data_size);
2456 return EFI_EXIT(EFI_SUCCESS);
2457}
2458
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002459/**
Mario Six8fac2912018-07-10 08:40:17 +02002460 * efi_copy_mem() - copy memory
2461 * @destination: destination of the copy operation
2462 * @source: source of the copy operation
2463 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002464 *
2465 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002466 *
Mario Six8fac2912018-07-10 08:40:17 +02002467 * See the Unified Extensible Firmware Interface (UEFI) specification for
2468 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002469 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002470static void EFIAPI efi_copy_mem(void *destination, const void *source,
2471 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002472{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002473 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002474 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002475 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002476}
2477
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002478/**
Mario Six8fac2912018-07-10 08:40:17 +02002479 * efi_set_mem() - Fill memory with a byte value.
2480 * @buffer: buffer to fill
2481 * @size: size of buffer in bytes
2482 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002483 *
2484 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002485 *
Mario Six8fac2912018-07-10 08:40:17 +02002486 * See the Unified Extensible Firmware Interface (UEFI) specification for
2487 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002488 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002489static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002490{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002491 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002492 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002493 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002494}
2495
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002496/**
Mario Six8fac2912018-07-10 08:40:17 +02002497 * efi_protocol_open() - open protocol interface on a handle
2498 * @handler: handler of a protocol
2499 * @protocol_interface: interface implementing the protocol
2500 * @agent_handle: handle of the driver
2501 * @controller_handle: handle of the controller
2502 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002503 *
Mario Six8fac2912018-07-10 08:40:17 +02002504 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002505 */
2506static efi_status_t efi_protocol_open(
2507 struct efi_handler *handler,
2508 void **protocol_interface, void *agent_handle,
2509 void *controller_handle, uint32_t attributes)
2510{
2511 struct efi_open_protocol_info_item *item;
2512 struct efi_open_protocol_info_entry *match = NULL;
2513 bool opened_by_driver = false;
2514 bool opened_exclusive = false;
2515
2516 /* If there is no agent, only return the interface */
2517 if (!agent_handle)
2518 goto out;
2519
2520 /* For TEST_PROTOCOL ignore interface attribute */
2521 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2522 *protocol_interface = NULL;
2523
2524 /*
2525 * Check if the protocol is already opened by a driver with the same
2526 * attributes or opened exclusively
2527 */
2528 list_for_each_entry(item, &handler->open_infos, link) {
2529 if (item->info.agent_handle == agent_handle) {
2530 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2531 (item->info.attributes == attributes))
2532 return EFI_ALREADY_STARTED;
2533 }
2534 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2535 opened_exclusive = true;
2536 }
2537
2538 /* Only one controller can open the protocol exclusively */
2539 if (opened_exclusive && attributes &
2540 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2541 return EFI_ACCESS_DENIED;
2542
2543 /* Prepare exclusive opening */
2544 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2545 /* Try to disconnect controllers */
2546 list_for_each_entry(item, &handler->open_infos, link) {
2547 if (item->info.attributes ==
2548 EFI_OPEN_PROTOCOL_BY_DRIVER)
2549 EFI_CALL(efi_disconnect_controller(
2550 item->info.controller_handle,
2551 item->info.agent_handle,
2552 NULL));
2553 }
2554 opened_by_driver = false;
2555 /* Check if all controllers are disconnected */
2556 list_for_each_entry(item, &handler->open_infos, link) {
2557 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2558 opened_by_driver = true;
2559 }
Heinrich Schuchardt8d0b45d2018-09-30 13:40:43 +02002560 /* Only one controller can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002561 if (opened_by_driver)
2562 return EFI_ACCESS_DENIED;
2563 }
2564
2565 /* Find existing entry */
2566 list_for_each_entry(item, &handler->open_infos, link) {
2567 if (item->info.agent_handle == agent_handle &&
2568 item->info.controller_handle == controller_handle)
2569 match = &item->info;
2570 }
2571 /* None found, create one */
2572 if (!match) {
2573 match = efi_create_open_info(handler);
2574 if (!match)
2575 return EFI_OUT_OF_RESOURCES;
2576 }
2577
2578 match->agent_handle = agent_handle;
2579 match->controller_handle = controller_handle;
2580 match->attributes = attributes;
2581 match->open_count++;
2582
2583out:
2584 /* For TEST_PROTOCOL ignore interface attribute. */
2585 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2586 *protocol_interface = handler->protocol_interface;
2587
2588 return EFI_SUCCESS;
2589}
2590
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002591/**
Mario Six8fac2912018-07-10 08:40:17 +02002592 * efi_open_protocol() - open protocol interface on a handle
2593 * @handle: handle on which the protocol shall be opened
2594 * @protocol: GUID of the protocol
2595 * @protocol_interface: interface implementing the protocol
2596 * @agent_handle: handle of the driver
2597 * @controller_handle: handle of the controller
2598 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002599 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002600 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002601 *
Mario Six8fac2912018-07-10 08:40:17 +02002602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2603 * details.
2604 *
2605 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002606 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002607static efi_status_t EFIAPI efi_open_protocol
2608 (efi_handle_t handle, const efi_guid_t *protocol,
2609 void **protocol_interface, efi_handle_t agent_handle,
2610 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002611{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002612 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002613 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002614
Rob Clark238f88c2017-09-13 18:05:41 -04002615 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002616 protocol_interface, agent_handle, controller_handle,
2617 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002618
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002619 if (!handle || !protocol ||
2620 (!protocol_interface && attributes !=
2621 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002622 goto out;
2623 }
2624
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002625 switch (attributes) {
2626 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2627 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2628 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2629 break;
2630 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2631 if (controller_handle == handle)
2632 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002633 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002634 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2635 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002636 /* Check that the controller handle is valid */
2637 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002638 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002639 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002640 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002641 /* Check that the agent handle is valid */
2642 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002643 goto out;
2644 break;
2645 default:
2646 goto out;
2647 }
2648
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002649 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002650 switch (r) {
2651 case EFI_SUCCESS:
2652 break;
2653 case EFI_NOT_FOUND:
2654 r = EFI_UNSUPPORTED;
2655 goto out;
2656 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002657 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002658 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002659
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002660 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2661 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002662out:
2663 return EFI_EXIT(r);
2664}
2665
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002666/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002667 * efi_start_image() - call the entry point of an image
2668 * @image_handle: handle of the image
2669 * @exit_data_size: size of the buffer
2670 * @exit_data: buffer to receive the exit data of the called image
2671 *
2672 * This function implements the StartImage service.
2673 *
2674 * See the Unified Extensible Firmware Interface (UEFI) specification for
2675 * details.
2676 *
2677 * Return: status code
2678 */
2679efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2680 efi_uintn_t *exit_data_size,
2681 u16 **exit_data)
2682{
2683 struct efi_loaded_image_obj *image_obj =
2684 (struct efi_loaded_image_obj *)image_handle;
2685 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002686 void *info;
2687 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002688
2689 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2690
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002691 /* Check parameters */
2692 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2693 &info, NULL, NULL,
2694 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2695 if (ret != EFI_SUCCESS)
2696 return EFI_EXIT(EFI_INVALID_PARAMETER);
2697
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002698 efi_is_direct_boot = false;
2699
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002700 image_obj->exit_data_size = exit_data_size;
2701 image_obj->exit_data = exit_data;
2702
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002703 /* call the image! */
2704 if (setjmp(&image_obj->exit_jmp)) {
2705 /*
2706 * We called the entry point of the child image with EFI_CALL
2707 * in the lines below. The child image called the Exit() boot
2708 * service efi_exit() which executed the long jump that brought
2709 * us to the current line. This implies that the second half
2710 * of the EFI_CALL macro has not been executed.
2711 */
2712#ifdef CONFIG_ARM
2713 /*
2714 * efi_exit() called efi_restore_gd(). We have to undo this
2715 * otherwise __efi_entry_check() will put the wrong value into
2716 * app_gd.
2717 */
2718 gd = app_gd;
2719#endif
2720 /*
2721 * To get ready to call EFI_EXIT below we have to execute the
2722 * missed out steps of EFI_CALL.
2723 */
2724 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002725 EFI_PRINT("%lu returned by started image\n",
2726 (unsigned long)((uintptr_t)image_obj->exit_status &
2727 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002728 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002729 return EFI_EXIT(image_obj->exit_status);
2730 }
2731
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002732 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002733 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002734 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002735 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2736
2737 /*
2738 * Usually UEFI applications call Exit() instead of returning.
2739 * But because the world doesn't consist of ponies and unicorns,
2740 * we're happy to emulate that behavior on behalf of a payload
2741 * that forgot.
2742 */
2743 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2744}
2745
2746/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002747 * efi_delete_image() - delete loaded image from memory)
2748 *
2749 * @image_obj: handle of the loaded image
2750 * @loaded_image_protocol: loaded image protocol
2751 */
2752static void efi_delete_image(struct efi_loaded_image_obj *image_obj,
2753 struct efi_loaded_image *loaded_image_protocol)
2754{
2755 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2756 efi_size_in_pages(loaded_image_protocol->image_size));
2757 efi_delete_handle(&image_obj->header);
2758}
2759
2760/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002761 * efi_unload_image() - unload an EFI image
2762 * @image_handle: handle of the image to be unloaded
2763 *
2764 * This function implements the UnloadImage service.
2765 *
2766 * See the Unified Extensible Firmware Interface (UEFI) specification for
2767 * details.
2768 *
2769 * Return: status code
2770 */
2771efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2772{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002773 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002774 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002775 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002776
2777 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002778
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002779 efiobj = efi_search_obj(image_handle);
2780 if (!efiobj) {
2781 ret = EFI_INVALID_PARAMETER;
2782 goto out;
2783 }
2784 /* Find the loaded image protocol */
2785 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2786 (void **)&loaded_image_protocol,
2787 NULL, NULL,
2788 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2789 if (ret != EFI_SUCCESS) {
2790 ret = EFI_INVALID_PARAMETER;
2791 goto out;
2792 }
2793 switch (efiobj->type) {
2794 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2795 /* Call the unload function */
2796 if (!loaded_image_protocol->unload) {
2797 ret = EFI_UNSUPPORTED;
2798 goto out;
2799 }
2800 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2801 if (ret != EFI_SUCCESS)
2802 goto out;
2803 break;
2804 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2805 break;
2806 default:
2807 ret = EFI_INVALID_PARAMETER;
2808 goto out;
2809 }
2810 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2811 loaded_image_protocol);
2812out:
2813 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002814}
2815
2816/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002817 * efi_update_exit_data() - fill exit data parameters of StartImage()
2818 *
2819 * @image_obj image handle
2820 * @exit_data_size size of the exit data buffer
2821 * @exit_data buffer with data returned by UEFI payload
2822 * Return: status code
2823 */
2824static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2825 efi_uintn_t exit_data_size,
2826 u16 *exit_data)
2827{
2828 efi_status_t ret;
2829
2830 /*
2831 * If exit_data is not provided to StartImage(), exit_data_size must be
2832 * ignored.
2833 */
2834 if (!image_obj->exit_data)
2835 return EFI_SUCCESS;
2836 if (image_obj->exit_data_size)
2837 *image_obj->exit_data_size = exit_data_size;
2838 if (exit_data_size && exit_data) {
2839 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
2840 exit_data_size,
2841 (void **)image_obj->exit_data);
2842 if (ret != EFI_SUCCESS)
2843 return ret;
2844 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
2845 } else {
2846 image_obj->exit_data = NULL;
2847 }
2848 return EFI_SUCCESS;
2849}
2850
2851/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002852 * efi_exit() - leave an EFI application or driver
2853 * @image_handle: handle of the application or driver that is exiting
2854 * @exit_status: status code
2855 * @exit_data_size: size of the buffer in bytes
2856 * @exit_data: buffer with data describing an error
2857 *
2858 * This function implements the Exit service.
2859 *
2860 * See the Unified Extensible Firmware Interface (UEFI) specification for
2861 * details.
2862 *
2863 * Return: status code
2864 */
2865static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2866 efi_status_t exit_status,
2867 efi_uintn_t exit_data_size,
2868 u16 *exit_data)
2869{
2870 /*
2871 * TODO: We should call the unload procedure of the loaded
2872 * image protocol.
2873 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002874 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002875 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002876 struct efi_loaded_image_obj *image_obj =
2877 (struct efi_loaded_image_obj *)image_handle;
2878
2879 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2880 exit_data_size, exit_data);
2881
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002882 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002883 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002884 (void **)&loaded_image_protocol,
2885 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002886 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002887 if (ret != EFI_SUCCESS) {
2888 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002889 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002890 }
2891
2892 /* Unloading of unstarted images */
2893 switch (image_obj->header.type) {
2894 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2895 break;
2896 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2897 efi_delete_image(image_obj, loaded_image_protocol);
2898 ret = EFI_SUCCESS;
2899 goto out;
2900 default:
2901 /* Handle does not refer to loaded image */
2902 ret = EFI_INVALID_PARAMETER;
2903 goto out;
2904 }
2905 /* A started image can only be unloaded it is the last one started. */
2906 if (image_handle != current_image) {
2907 ret = EFI_INVALID_PARAMETER;
2908 goto out;
2909 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002910
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002911 /* Exit data is only foreseen in case of failure. */
2912 if (exit_status != EFI_SUCCESS) {
2913 ret = efi_update_exit_data(image_obj, exit_data_size,
2914 exit_data);
2915 /* Exiting has priority. Don't return error to caller. */
2916 if (ret != EFI_SUCCESS)
2917 EFI_PRINT("%s: out of memory\n", __func__);
2918 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002919 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
2920 exit_status != EFI_SUCCESS)
2921 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002922
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002923 /* Make sure entry/exit counts for EFI world cross-overs match */
2924 EFI_EXIT(exit_status);
2925
2926 /*
2927 * But longjmp out with the U-Boot gd, not the application's, as
2928 * the other end is a setjmp call inside EFI context.
2929 */
2930 efi_restore_gd();
2931
2932 image_obj->exit_status = exit_status;
2933 longjmp(&image_obj->exit_jmp, 1);
2934
2935 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002936out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02002937 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002938}
2939
2940/**
Mario Six8fac2912018-07-10 08:40:17 +02002941 * efi_handle_protocol() - get interface of a protocol on a handle
2942 * @handle: handle on which the protocol shall be opened
2943 * @protocol: GUID of the protocol
2944 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002945 *
2946 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002947 *
2948 * See the Unified Extensible Firmware Interface (UEFI) specification for
2949 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002950 *
Mario Six8fac2912018-07-10 08:40:17 +02002951 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002952 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002953static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002954 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002955 void **protocol_interface)
2956{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002957 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2958 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002959}
2960
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002961/**
Mario Six8fac2912018-07-10 08:40:17 +02002962 * efi_bind_controller() - bind a single driver to a controller
2963 * @controller_handle: controller handle
2964 * @driver_image_handle: driver handle
2965 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002966 *
Mario Six8fac2912018-07-10 08:40:17 +02002967 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002968 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002969static efi_status_t efi_bind_controller(
2970 efi_handle_t controller_handle,
2971 efi_handle_t driver_image_handle,
2972 struct efi_device_path *remain_device_path)
2973{
2974 struct efi_driver_binding_protocol *binding_protocol;
2975 efi_status_t r;
2976
2977 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2978 &efi_guid_driver_binding_protocol,
2979 (void **)&binding_protocol,
2980 driver_image_handle, NULL,
2981 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2982 if (r != EFI_SUCCESS)
2983 return r;
2984 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2985 controller_handle,
2986 remain_device_path));
2987 if (r == EFI_SUCCESS)
2988 r = EFI_CALL(binding_protocol->start(binding_protocol,
2989 controller_handle,
2990 remain_device_path));
2991 EFI_CALL(efi_close_protocol(driver_image_handle,
2992 &efi_guid_driver_binding_protocol,
2993 driver_image_handle, NULL));
2994 return r;
2995}
2996
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002997/**
Mario Six8fac2912018-07-10 08:40:17 +02002998 * efi_connect_single_controller() - connect a single driver to a controller
2999 * @controller_handle: controller
3000 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003001 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003002 *
Mario Six8fac2912018-07-10 08:40:17 +02003003 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003004 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003005static efi_status_t efi_connect_single_controller(
3006 efi_handle_t controller_handle,
3007 efi_handle_t *driver_image_handle,
3008 struct efi_device_path *remain_device_path)
3009{
3010 efi_handle_t *buffer;
3011 size_t count;
3012 size_t i;
3013 efi_status_t r;
3014 size_t connected = 0;
3015
3016 /* Get buffer with all handles with driver binding protocol */
3017 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3018 &efi_guid_driver_binding_protocol,
3019 NULL, &count, &buffer));
3020 if (r != EFI_SUCCESS)
3021 return r;
3022
3023 /* Context Override */
3024 if (driver_image_handle) {
3025 for (; *driver_image_handle; ++driver_image_handle) {
3026 for (i = 0; i < count; ++i) {
3027 if (buffer[i] == *driver_image_handle) {
3028 buffer[i] = NULL;
3029 r = efi_bind_controller(
3030 controller_handle,
3031 *driver_image_handle,
3032 remain_device_path);
3033 /*
3034 * For drivers that do not support the
3035 * controller or are already connected
3036 * we receive an error code here.
3037 */
3038 if (r == EFI_SUCCESS)
3039 ++connected;
3040 }
3041 }
3042 }
3043 }
3044
3045 /*
3046 * TODO: Some overrides are not yet implemented:
3047 * - Platform Driver Override
3048 * - Driver Family Override Search
3049 * - Bus Specific Driver Override
3050 */
3051
3052 /* Driver Binding Search */
3053 for (i = 0; i < count; ++i) {
3054 if (buffer[i]) {
3055 r = efi_bind_controller(controller_handle,
3056 buffer[i],
3057 remain_device_path);
3058 if (r == EFI_SUCCESS)
3059 ++connected;
3060 }
3061 }
3062
3063 efi_free_pool(buffer);
3064 if (!connected)
3065 return EFI_NOT_FOUND;
3066 return EFI_SUCCESS;
3067}
3068
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003069/**
Mario Six8fac2912018-07-10 08:40:17 +02003070 * efi_connect_controller() - connect a controller to a driver
3071 * @controller_handle: handle of the controller
3072 * @driver_image_handle: handle of the driver
3073 * @remain_device_path: device path of a child controller
3074 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003075 *
3076 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003077 *
3078 * See the Unified Extensible Firmware Interface (UEFI) specification for
3079 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003080 *
3081 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003082 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003083 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3084 *
Mario Six8fac2912018-07-10 08:40:17 +02003085 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003086 */
3087static efi_status_t EFIAPI efi_connect_controller(
3088 efi_handle_t controller_handle,
3089 efi_handle_t *driver_image_handle,
3090 struct efi_device_path *remain_device_path,
3091 bool recursive)
3092{
3093 efi_status_t r;
3094 efi_status_t ret = EFI_NOT_FOUND;
3095 struct efi_object *efiobj;
3096
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003097 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003098 remain_device_path, recursive);
3099
3100 efiobj = efi_search_obj(controller_handle);
3101 if (!efiobj) {
3102 ret = EFI_INVALID_PARAMETER;
3103 goto out;
3104 }
3105
3106 r = efi_connect_single_controller(controller_handle,
3107 driver_image_handle,
3108 remain_device_path);
3109 if (r == EFI_SUCCESS)
3110 ret = EFI_SUCCESS;
3111 if (recursive) {
3112 struct efi_handler *handler;
3113 struct efi_open_protocol_info_item *item;
3114
3115 list_for_each_entry(handler, &efiobj->protocols, link) {
3116 list_for_each_entry(item, &handler->open_infos, link) {
3117 if (item->info.attributes &
3118 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3119 r = EFI_CALL(efi_connect_controller(
3120 item->info.controller_handle,
3121 driver_image_handle,
3122 remain_device_path,
3123 recursive));
3124 if (r == EFI_SUCCESS)
3125 ret = EFI_SUCCESS;
3126 }
3127 }
3128 }
3129 }
3130 /* Check for child controller specified by end node */
3131 if (ret != EFI_SUCCESS && remain_device_path &&
3132 remain_device_path->type == DEVICE_PATH_TYPE_END)
3133 ret = EFI_SUCCESS;
3134out:
3135 return EFI_EXIT(ret);
3136}
3137
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003138/**
Mario Six8fac2912018-07-10 08:40:17 +02003139 * efi_reinstall_protocol_interface() - reinstall protocol interface
3140 * @handle: handle on which the protocol shall be reinstalled
3141 * @protocol: GUID of the protocol to be installed
3142 * @old_interface: interface to be removed
3143 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003144 *
3145 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003146 *
3147 * See the Unified Extensible Firmware Interface (UEFI) specification for
3148 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003149 *
3150 * The old interface is uninstalled. The new interface is installed.
3151 * Drivers are connected.
3152 *
Mario Six8fac2912018-07-10 08:40:17 +02003153 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003154 */
3155static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3156 efi_handle_t handle, const efi_guid_t *protocol,
3157 void *old_interface, void *new_interface)
3158{
3159 efi_status_t ret;
3160
3161 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3162 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003163
3164 /* Uninstall protocol but do not delete handle */
3165 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003166 if (ret != EFI_SUCCESS)
3167 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003168
3169 /* Install the new protocol */
3170 ret = efi_add_protocol(handle, protocol, new_interface);
3171 /*
3172 * The UEFI spec does not specify what should happen to the handle
3173 * if in case of an error no protocol interface remains on the handle.
3174 * So let's do nothing here.
3175 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003176 if (ret != EFI_SUCCESS)
3177 goto out;
3178 /*
3179 * The returned status code has to be ignored.
3180 * Do not create an error if no suitable driver for the handle exists.
3181 */
3182 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3183out:
3184 return EFI_EXIT(ret);
3185}
3186
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003187/**
Mario Six8fac2912018-07-10 08:40:17 +02003188 * efi_get_child_controllers() - get all child controllers associated to a driver
3189 * @efiobj: handle of the controller
3190 * @driver_handle: handle of the driver
3191 * @number_of_children: number of child controllers
3192 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003193 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003194 * The allocated buffer has to be freed with free().
3195 *
Mario Six8fac2912018-07-10 08:40:17 +02003196 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003197 */
3198static efi_status_t efi_get_child_controllers(
3199 struct efi_object *efiobj,
3200 efi_handle_t driver_handle,
3201 efi_uintn_t *number_of_children,
3202 efi_handle_t **child_handle_buffer)
3203{
3204 struct efi_handler *handler;
3205 struct efi_open_protocol_info_item *item;
3206 efi_uintn_t count = 0, i;
3207 bool duplicate;
3208
3209 /* Count all child controller associations */
3210 list_for_each_entry(handler, &efiobj->protocols, link) {
3211 list_for_each_entry(item, &handler->open_infos, link) {
3212 if (item->info.agent_handle == driver_handle &&
3213 item->info.attributes &
3214 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3215 ++count;
3216 }
3217 }
3218 /*
3219 * Create buffer. In case of duplicate child controller assignments
3220 * the buffer will be too large. But that does not harm.
3221 */
3222 *number_of_children = 0;
3223 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3224 if (!*child_handle_buffer)
3225 return EFI_OUT_OF_RESOURCES;
3226 /* Copy unique child handles */
3227 list_for_each_entry(handler, &efiobj->protocols, link) {
3228 list_for_each_entry(item, &handler->open_infos, link) {
3229 if (item->info.agent_handle == driver_handle &&
3230 item->info.attributes &
3231 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3232 /* Check this is a new child controller */
3233 duplicate = false;
3234 for (i = 0; i < *number_of_children; ++i) {
3235 if ((*child_handle_buffer)[i] ==
3236 item->info.controller_handle)
3237 duplicate = true;
3238 }
3239 /* Copy handle to buffer */
3240 if (!duplicate) {
3241 i = (*number_of_children)++;
3242 (*child_handle_buffer)[i] =
3243 item->info.controller_handle;
3244 }
3245 }
3246 }
3247 }
3248 return EFI_SUCCESS;
3249}
3250
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003251/**
Mario Six8fac2912018-07-10 08:40:17 +02003252 * efi_disconnect_controller() - disconnect a controller from a driver
3253 * @controller_handle: handle of the controller
3254 * @driver_image_handle: handle of the driver
3255 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003256 *
3257 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003258 *
3259 * See the Unified Extensible Firmware Interface (UEFI) specification for
3260 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003261 *
Mario Six8fac2912018-07-10 08:40:17 +02003262 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003263 */
3264static efi_status_t EFIAPI efi_disconnect_controller(
3265 efi_handle_t controller_handle,
3266 efi_handle_t driver_image_handle,
3267 efi_handle_t child_handle)
3268{
3269 struct efi_driver_binding_protocol *binding_protocol;
3270 efi_handle_t *child_handle_buffer = NULL;
3271 size_t number_of_children = 0;
3272 efi_status_t r;
3273 size_t stop_count = 0;
3274 struct efi_object *efiobj;
3275
3276 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3277 child_handle);
3278
3279 efiobj = efi_search_obj(controller_handle);
3280 if (!efiobj) {
3281 r = EFI_INVALID_PARAMETER;
3282 goto out;
3283 }
3284
3285 if (child_handle && !efi_search_obj(child_handle)) {
3286 r = EFI_INVALID_PARAMETER;
3287 goto out;
3288 }
3289
3290 /* If no driver handle is supplied, disconnect all drivers */
3291 if (!driver_image_handle) {
3292 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3293 goto out;
3294 }
3295
3296 /* Create list of child handles */
3297 if (child_handle) {
3298 number_of_children = 1;
3299 child_handle_buffer = &child_handle;
3300 } else {
3301 efi_get_child_controllers(efiobj,
3302 driver_image_handle,
3303 &number_of_children,
3304 &child_handle_buffer);
3305 }
3306
3307 /* Get the driver binding protocol */
3308 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3309 &efi_guid_driver_binding_protocol,
3310 (void **)&binding_protocol,
3311 driver_image_handle, NULL,
3312 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3313 if (r != EFI_SUCCESS)
3314 goto out;
3315 /* Remove the children */
3316 if (number_of_children) {
3317 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3318 controller_handle,
3319 number_of_children,
3320 child_handle_buffer));
3321 if (r == EFI_SUCCESS)
3322 ++stop_count;
3323 }
3324 /* Remove the driver */
3325 if (!child_handle)
3326 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3327 controller_handle,
3328 0, NULL));
3329 if (r == EFI_SUCCESS)
3330 ++stop_count;
3331 EFI_CALL(efi_close_protocol(driver_image_handle,
3332 &efi_guid_driver_binding_protocol,
3333 driver_image_handle, NULL));
3334
3335 if (stop_count)
3336 r = EFI_SUCCESS;
3337 else
3338 r = EFI_NOT_FOUND;
3339out:
3340 if (!child_handle)
3341 free(child_handle_buffer);
3342 return EFI_EXIT(r);
3343}
3344
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003345static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003346 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003347 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3348 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003349 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003350 },
3351 .raise_tpl = efi_raise_tpl,
3352 .restore_tpl = efi_restore_tpl,
3353 .allocate_pages = efi_allocate_pages_ext,
3354 .free_pages = efi_free_pages_ext,
3355 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003356 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003357 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003358 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003359 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003360 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003361 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003362 .close_event = efi_close_event,
3363 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003364 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003365 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003366 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003367 .handle_protocol = efi_handle_protocol,
3368 .reserved = NULL,
3369 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003370 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003371 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003372 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003373 .load_image = efi_load_image,
3374 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003375 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003376 .unload_image = efi_unload_image,
3377 .exit_boot_services = efi_exit_boot_services,
3378 .get_next_monotonic_count = efi_get_next_monotonic_count,
3379 .stall = efi_stall,
3380 .set_watchdog_timer = efi_set_watchdog_timer,
3381 .connect_controller = efi_connect_controller,
3382 .disconnect_controller = efi_disconnect_controller,
3383 .open_protocol = efi_open_protocol,
3384 .close_protocol = efi_close_protocol,
3385 .open_protocol_information = efi_open_protocol_information,
3386 .protocols_per_handle = efi_protocols_per_handle,
3387 .locate_handle_buffer = efi_locate_handle_buffer,
3388 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003389 .install_multiple_protocol_interfaces =
3390 efi_install_multiple_protocol_interfaces,
3391 .uninstall_multiple_protocol_interfaces =
3392 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003393 .calculate_crc32 = efi_calculate_crc32,
3394 .copy_mem = efi_copy_mem,
3395 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003396 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003397};
3398
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003399static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003400
Alexander Graf393dd912016-10-14 13:45:30 +02003401struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003402 .hdr = {
3403 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003404 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003405 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003406 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003407 .fw_vendor = firmware_vendor,
3408 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003409 .con_in = (void *)&efi_con_in,
3410 .con_out = (void *)&efi_con_out,
3411 .std_err = (void *)&efi_con_out,
3412 .runtime = (void *)&efi_runtime_services,
3413 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003414 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003415 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003416};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003417
3418/**
3419 * efi_initialize_system_table() - Initialize system table
3420 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003421 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003422 */
3423efi_status_t efi_initialize_system_table(void)
3424{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003425 efi_status_t ret;
3426
3427 /* Allocate configuration table array */
3428 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3429 EFI_MAX_CONFIGURATION_TABLES *
3430 sizeof(struct efi_configuration_table),
3431 (void **)&systab.tables);
3432
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003433 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003434 efi_update_table_header_crc32(&systab.hdr);
3435 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3436 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003437
3438 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003439}