blob: b215bd7723da5a92865de778b34b9c3dafaec7fe [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>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020022
Alexander Grafc15d9212016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010028
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010029/* Handle of the currently executing image */
30static efi_handle_t current_image;
31
Alexander Graffa5246b2018-11-15 21:23:47 +010032/*
33 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
34 * we need to do trickery with caches. Since we don't want to break the EFI
35 * aware boot path, only apply hacks when loading exiting directly (breaking
36 * direct Linux EFI booting along the way - oh well).
37 */
38static bool efi_is_direct_boot = true;
39
Simon Glasscdfe6962016-09-25 15:27:35 -060040#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010041/*
42 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
43 * fixed when compiling U-Boot. However, the payload does not know about that
44 * restriction so we need to manually swap its and our view of that register on
45 * EFI callback entry/exit.
46 */
47static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060048#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010049
Heinrich Schuchardt72506232019-02-09 14:10:39 +010050/* 1 if inside U-Boot code, 0 if inside EFI payload code */
51static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040052static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010053/* GUID of the device tree table */
54const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010055/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
56const efi_guid_t efi_guid_driver_binding_protocol =
57 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040058
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010059/* event group ExitBootServices() invoked */
60const efi_guid_t efi_guid_event_group_exit_boot_services =
61 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
62/* event group SetVirtualAddressMap() invoked */
63const efi_guid_t efi_guid_event_group_virtual_address_change =
64 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
65/* event group memory map changed */
66const efi_guid_t efi_guid_event_group_memory_map_change =
67 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
68/* event group boot manager about to boot */
69const efi_guid_t efi_guid_event_group_ready_to_boot =
70 EFI_EVENT_GROUP_READY_TO_BOOT;
71/* event group ResetSystem() invoked (before ExitBootServices) */
72const efi_guid_t efi_guid_event_group_reset_system =
73 EFI_EVENT_GROUP_RESET_SYSTEM;
74
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010075static efi_status_t EFIAPI efi_disconnect_controller(
76 efi_handle_t controller_handle,
77 efi_handle_t driver_image_handle,
78 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010079
Rob Clark86789d52017-07-27 08:04:18 -040080/* Called on every callback entry */
81int __efi_entry_check(void)
82{
83 int ret = entry_count++ == 0;
84#ifdef CONFIG_ARM
85 assert(efi_gd);
86 app_gd = gd;
87 gd = efi_gd;
88#endif
89 return ret;
90}
91
92/* Called on every callback exit */
93int __efi_exit_check(void)
94{
95 int ret = --entry_count == 0;
96#ifdef CONFIG_ARM
97 gd = app_gd;
98#endif
99 return ret;
100}
101
Alexander Grafc15d9212016-03-04 01:09:59 +0100102/* Called from do_bootefi_exec() */
103void efi_save_gd(void)
104{
Simon Glasscdfe6962016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100106 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600107#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100108}
109
Rob Clark86789d52017-07-27 08:04:18 -0400110/*
Mario Six8fac2912018-07-10 08:40:17 +0200111 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200112 * world so we can dump out an abort message, without any care about returning
113 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400114 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100115void efi_restore_gd(void)
116{
Simon Glasscdfe6962016-09-25 15:27:35 -0600117#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100118 /* Only restore if we're already in EFI context */
119 if (!efi_gd)
120 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100121 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600122#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100123}
124
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200125/**
Mario Six8fac2912018-07-10 08:40:17 +0200126 * indent_string() - returns a string for indenting with two spaces per level
127 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100128 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200129 * A maximum of ten indent levels is supported. Higher indent levels will be
130 * truncated.
131 *
Mario Six8fac2912018-07-10 08:40:17 +0200132 * Return: A string for indenting with two spaces per level is
133 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400134 */
135static const char *indent_string(int level)
136{
137 const char *indent = " ";
138 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100139
Rob Clarke7896c32017-07-27 08:04:19 -0400140 level = min(max, level * 2);
141 return &indent[max - level];
142}
143
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200144const char *__efi_nesting(void)
145{
146 return indent_string(nesting_level);
147}
148
Rob Clarke7896c32017-07-27 08:04:19 -0400149const char *__efi_nesting_inc(void)
150{
151 return indent_string(nesting_level++);
152}
153
154const char *__efi_nesting_dec(void)
155{
156 return indent_string(--nesting_level);
157}
158
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200159/**
Mario Six8fac2912018-07-10 08:40:17 +0200160 * efi_queue_event() - queue an EFI event
161 * @event: event to signal
162 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200163 *
164 * This function queues the notification function of the event for future
165 * execution.
166 *
Mario Six8fac2912018-07-10 08:40:17 +0200167 * The notification function is called if the task priority level of the event
168 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200169 *
170 * For the SignalEvent service see efi_signal_event_ext.
171 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200172 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100173static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200174{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200175 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200176 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200177 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100178 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200179 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200180 EFI_CALL_VOID(event->notify_function(event,
181 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200182 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200183 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200184}
185
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200186/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200187 * is_valid_tpl() - check if the task priority level is valid
188 *
189 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200190 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200191 */
192efi_status_t is_valid_tpl(efi_uintn_t tpl)
193{
194 switch (tpl) {
195 case TPL_APPLICATION:
196 case TPL_CALLBACK:
197 case TPL_NOTIFY:
198 case TPL_HIGH_LEVEL:
199 return EFI_SUCCESS;
200 default:
201 return EFI_INVALID_PARAMETER;
202 }
203}
204
205/**
Mario Six8fac2912018-07-10 08:40:17 +0200206 * efi_signal_event() - signal an EFI event
207 * @event: event to signal
208 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100209 *
Mario Six8fac2912018-07-10 08:40:17 +0200210 * This function signals an event. If the event belongs to an event group all
211 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100212 * their notification function is queued.
213 *
214 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100215 */
216void efi_signal_event(struct efi_event *event, bool check_tpl)
217{
218 if (event->group) {
219 struct efi_event *evt;
220
221 /*
222 * The signaled state has to set before executing any
223 * notification function
224 */
225 list_for_each_entry(evt, &efi_events, link) {
226 if (!evt->group || guidcmp(evt->group, event->group))
227 continue;
228 if (evt->is_signaled)
229 continue;
230 evt->is_signaled = true;
231 if (evt->type & EVT_NOTIFY_SIGNAL &&
232 evt->notify_function)
233 evt->is_queued = true;
234 }
235 list_for_each_entry(evt, &efi_events, link) {
236 if (!evt->group || guidcmp(evt->group, event->group))
237 continue;
238 if (evt->is_queued)
239 efi_queue_event(evt, check_tpl);
240 }
241 } else if (!event->is_signaled) {
242 event->is_signaled = true;
243 if (event->type & EVT_NOTIFY_SIGNAL)
244 efi_queue_event(event, check_tpl);
245 }
246}
247
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200248/**
Mario Six8fac2912018-07-10 08:40:17 +0200249 * efi_raise_tpl() - raise the task priority level
250 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200251 *
252 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200253 *
254 * See the Unified Extensible Firmware Interface (UEFI) specification for
255 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200256 *
Mario Six8fac2912018-07-10 08:40:17 +0200257 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200258 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100259static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100260{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100261 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200262
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200263 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200264
265 if (new_tpl < efi_tpl)
266 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
267 efi_tpl = new_tpl;
268 if (efi_tpl > TPL_HIGH_LEVEL)
269 efi_tpl = TPL_HIGH_LEVEL;
270
271 EFI_EXIT(EFI_SUCCESS);
272 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100273}
274
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200275/**
Mario Six8fac2912018-07-10 08:40:17 +0200276 * efi_restore_tpl() - lower the task priority level
277 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200278 *
279 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200280 *
Mario Six8fac2912018-07-10 08:40:17 +0200281 * See the Unified Extensible Firmware Interface (UEFI) specification for
282 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200283 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100284static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100285{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200286 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200287
288 if (old_tpl > efi_tpl)
289 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
290 efi_tpl = old_tpl;
291 if (efi_tpl > TPL_HIGH_LEVEL)
292 efi_tpl = TPL_HIGH_LEVEL;
293
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100294 /*
295 * Lowering the TPL may have made queued events eligible for execution.
296 */
297 efi_timer_check();
298
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200299 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100300}
301
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200302/**
Mario Six8fac2912018-07-10 08:40:17 +0200303 * efi_allocate_pages_ext() - allocate memory pages
304 * @type: type of allocation to be performed
305 * @memory_type: usage type of the allocated memory
306 * @pages: number of pages to be allocated
307 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200308 *
309 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200310 *
Mario Six8fac2912018-07-10 08:40:17 +0200311 * See the Unified Extensible Firmware Interface (UEFI) specification for
312 * details.
313 *
314 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200315 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900316static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100317 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900318 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100319{
320 efi_status_t r;
321
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100322 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100323 r = efi_allocate_pages(type, memory_type, pages, memory);
324 return EFI_EXIT(r);
325}
326
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200327/**
Mario Six8fac2912018-07-10 08:40:17 +0200328 * efi_free_pages_ext() - Free memory pages.
329 * @memory: start of the memory area to be freed
330 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200331 *
332 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200333 *
334 * See the Unified Extensible Firmware Interface (UEFI) specification for
335 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200336 *
Mario Six8fac2912018-07-10 08:40:17 +0200337 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200338 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900339static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100340 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100341{
342 efi_status_t r;
343
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900344 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100345 r = efi_free_pages(memory, pages);
346 return EFI_EXIT(r);
347}
348
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200349/**
Mario Six8fac2912018-07-10 08:40:17 +0200350 * efi_get_memory_map_ext() - get map describing memory usage
351 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
352 * on exit the size of the copied memory map
353 * @memory_map: buffer to which the memory map is written
354 * @map_key: key for the memory map
355 * @descriptor_size: size of an individual memory descriptor
356 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200357 *
358 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200359 *
Mario Six8fac2912018-07-10 08:40:17 +0200360 * See the Unified Extensible Firmware Interface (UEFI) specification for
361 * details.
362 *
363 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200364 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900365static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100366 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900367 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100368 efi_uintn_t *map_key,
369 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900370 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100371{
372 efi_status_t r;
373
374 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
375 map_key, descriptor_size, descriptor_version);
376 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
377 descriptor_size, descriptor_version);
378 return EFI_EXIT(r);
379}
380
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200381/**
Mario Six8fac2912018-07-10 08:40:17 +0200382 * efi_allocate_pool_ext() - allocate memory from pool
383 * @pool_type: type of the pool from which memory is to be allocated
384 * @size: number of bytes to be allocated
385 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200386 *
387 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200388 *
389 * See the Unified Extensible Firmware Interface (UEFI) specification for
390 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200391 *
Mario Six8fac2912018-07-10 08:40:17 +0200392 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200393 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200394static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100395 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200396 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100397{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100398 efi_status_t r;
399
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100400 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200401 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100402 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100403}
404
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200405/**
Mario Six8fac2912018-07-10 08:40:17 +0200406 * efi_free_pool_ext() - free memory from pool
407 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200408 *
409 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200410 *
Mario Six8fac2912018-07-10 08:40:17 +0200411 * See the Unified Extensible Firmware Interface (UEFI) specification for
412 * details.
413 *
414 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200415 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200416static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100417{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100418 efi_status_t r;
419
420 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200421 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100422 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100423}
424
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200425/**
Mario Six8fac2912018-07-10 08:40:17 +0200426 * efi_add_handle() - add a new object to the object list
427 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100428 *
Mario Six8fac2912018-07-10 08:40:17 +0200429 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100430 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200431void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100432{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200433 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100434 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200435 INIT_LIST_HEAD(&handle->protocols);
436 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100437}
438
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200439/**
Mario Six8fac2912018-07-10 08:40:17 +0200440 * efi_create_handle() - create handle
441 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200442 *
Mario Six8fac2912018-07-10 08:40:17 +0200443 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200444 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100445efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200446{
447 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200448
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200449 obj = calloc(1, sizeof(struct efi_object));
450 if (!obj)
451 return EFI_OUT_OF_RESOURCES;
452
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100453 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200454 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200455
456 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200457}
458
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200459/**
Mario Six8fac2912018-07-10 08:40:17 +0200460 * efi_search_protocol() - find a protocol on a handle.
461 * @handle: handle
462 * @protocol_guid: GUID of the protocol
463 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100464 *
Mario Six8fac2912018-07-10 08:40:17 +0200465 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100466 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100467efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100468 const efi_guid_t *protocol_guid,
469 struct efi_handler **handler)
470{
471 struct efi_object *efiobj;
472 struct list_head *lhandle;
473
474 if (!handle || !protocol_guid)
475 return EFI_INVALID_PARAMETER;
476 efiobj = efi_search_obj(handle);
477 if (!efiobj)
478 return EFI_INVALID_PARAMETER;
479 list_for_each(lhandle, &efiobj->protocols) {
480 struct efi_handler *protocol;
481
482 protocol = list_entry(lhandle, struct efi_handler, link);
483 if (!guidcmp(protocol->guid, protocol_guid)) {
484 if (handler)
485 *handler = protocol;
486 return EFI_SUCCESS;
487 }
488 }
489 return EFI_NOT_FOUND;
490}
491
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200492/**
Mario Six8fac2912018-07-10 08:40:17 +0200493 * efi_remove_protocol() - delete protocol from a handle
494 * @handle: handle from which the protocol shall be deleted
495 * @protocol: GUID of the protocol to be deleted
496 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100497 *
Mario Six8fac2912018-07-10 08:40:17 +0200498 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100499 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100500efi_status_t efi_remove_protocol(const efi_handle_t handle,
501 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100502 void *protocol_interface)
503{
504 struct efi_handler *handler;
505 efi_status_t ret;
506
507 ret = efi_search_protocol(handle, protocol, &handler);
508 if (ret != EFI_SUCCESS)
509 return ret;
510 if (guidcmp(handler->guid, protocol))
511 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200512 if (handler->protocol_interface != protocol_interface)
513 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100514 list_del(&handler->link);
515 free(handler);
516 return EFI_SUCCESS;
517}
518
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200519/**
Mario Six8fac2912018-07-10 08:40:17 +0200520 * efi_remove_all_protocols() - delete all protocols from a handle
521 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100522 *
Mario Six8fac2912018-07-10 08:40:17 +0200523 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100524 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100525efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100526{
527 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100528 struct efi_handler *protocol;
529 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100530
531 efiobj = efi_search_obj(handle);
532 if (!efiobj)
533 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100534 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100535 efi_status_t ret;
536
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100537 ret = efi_remove_protocol(handle, protocol->guid,
538 protocol->protocol_interface);
539 if (ret != EFI_SUCCESS)
540 return ret;
541 }
542 return EFI_SUCCESS;
543}
544
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200545/**
Mario Six8fac2912018-07-10 08:40:17 +0200546 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100547 *
Mario Six8fac2912018-07-10 08:40:17 +0200548 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100549 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200550void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100551{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200552 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100553 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200554 efi_remove_all_protocols(handle);
555 list_del(&handle->link);
556 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557}
558
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200559/**
Mario Six8fac2912018-07-10 08:40:17 +0200560 * efi_is_event() - check if a pointer is a valid event
561 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100562 *
Mario Six8fac2912018-07-10 08:40:17 +0200563 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100564 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100565static efi_status_t efi_is_event(const struct efi_event *event)
566{
567 const struct efi_event *evt;
568
569 if (!event)
570 return EFI_INVALID_PARAMETER;
571 list_for_each_entry(evt, &efi_events, link) {
572 if (evt == event)
573 return EFI_SUCCESS;
574 }
575 return EFI_INVALID_PARAMETER;
576}
Alexander Grafc15d9212016-03-04 01:09:59 +0100577
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200578/**
Mario Six8fac2912018-07-10 08:40:17 +0200579 * efi_create_event() - create an event
580 * @type: type of the event to create
581 * @notify_tpl: task priority level of the event
582 * @notify_function: notification function of the event
583 * @notify_context: pointer passed to the notification function
584 * @group: event group
585 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200586 *
587 * This function is used inside U-Boot code to create an event.
588 *
589 * For the API function implementing the CreateEvent service see
590 * efi_create_event_ext.
591 *
Mario Six8fac2912018-07-10 08:40:17 +0200592 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200593 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100594efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200595 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200596 struct efi_event *event,
597 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100598 void *notify_context, efi_guid_t *group,
599 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100600{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100601 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200602
Jonathan Gray7758b212017-03-12 19:26:07 +1100603 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200604 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100605
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200606 switch (type) {
607 case 0:
608 case EVT_TIMER:
609 case EVT_NOTIFY_SIGNAL:
610 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
611 case EVT_NOTIFY_WAIT:
612 case EVT_TIMER | EVT_NOTIFY_WAIT:
613 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
614 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
615 break;
616 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200617 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200618 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100619
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900620 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
621 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200622 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100623
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100624 evt = calloc(1, sizeof(struct efi_event));
625 if (!evt)
626 return EFI_OUT_OF_RESOURCES;
627 evt->type = type;
628 evt->notify_tpl = notify_tpl;
629 evt->notify_function = notify_function;
630 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100631 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200632 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100633 evt->trigger_next = -1ULL;
634 evt->is_queued = false;
635 evt->is_signaled = false;
636 list_add_tail(&evt->link, &efi_events);
637 *event = evt;
638 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100639}
640
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200641/*
Mario Six8fac2912018-07-10 08:40:17 +0200642 * efi_create_event_ex() - create an event in a group
643 * @type: type of the event to create
644 * @notify_tpl: task priority level of the event
645 * @notify_function: notification function of the event
646 * @notify_context: pointer passed to the notification function
647 * @event: created event
648 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100649 *
650 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100651 *
Mario Six8fac2912018-07-10 08:40:17 +0200652 * See the Unified Extensible Firmware Interface (UEFI) specification for
653 * details.
654 *
655 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100656 */
657efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
658 void (EFIAPI *notify_function) (
659 struct efi_event *event,
660 void *context),
661 void *notify_context,
662 efi_guid_t *event_group,
663 struct efi_event **event)
664{
665 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
666 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100667 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100668 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100669}
670
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200671/**
Mario Six8fac2912018-07-10 08:40:17 +0200672 * efi_create_event_ext() - create an event
673 * @type: type of the event to create
674 * @notify_tpl: task priority level of the event
675 * @notify_function: notification function of the event
676 * @notify_context: pointer passed to the notification function
677 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200678 *
679 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200680 *
681 * See the Unified Extensible Firmware Interface (UEFI) specification for
682 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200683 *
Mario Six8fac2912018-07-10 08:40:17 +0200684 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200685 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200686static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100687 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200688 void (EFIAPI *notify_function) (
689 struct efi_event *event,
690 void *context),
691 void *notify_context, struct efi_event **event)
692{
693 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
694 notify_context);
695 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100696 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200697}
698
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200699/**
Mario Six8fac2912018-07-10 08:40:17 +0200700 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200701 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200702 * Check if a timer event has occurred or a queued notification function should
703 * be called.
704 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100705 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200706 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100707 */
708void efi_timer_check(void)
709{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100710 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100711 u64 now = timer_get_us();
712
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100713 list_for_each_entry(evt, &efi_events, link) {
714 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100715 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100716 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200717 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100718 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200719 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100720 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200721 break;
722 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100723 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200724 break;
725 default:
726 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200727 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100728 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100729 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100730 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100731 WATCHDOG_RESET();
732}
733
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200734/**
Mario Six8fac2912018-07-10 08:40:17 +0200735 * efi_set_timer() - set the trigger time for a timer event or stop the event
736 * @event: event for which the timer is set
737 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200738 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200739 *
740 * This is the function for internal usage in U-Boot. For the API function
741 * implementing the SetTimer service see efi_set_timer_ext.
742 *
Mario Six8fac2912018-07-10 08:40:17 +0200743 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200744 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200745efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200746 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100747{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100748 /* Check that the event is valid */
749 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
750 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100751
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200752 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200753 * The parameter defines a multiple of 100 ns.
754 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200755 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200756 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100757
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100758 switch (type) {
759 case EFI_TIMER_STOP:
760 event->trigger_next = -1ULL;
761 break;
762 case EFI_TIMER_PERIODIC:
763 case EFI_TIMER_RELATIVE:
764 event->trigger_next = timer_get_us() + trigger_time;
765 break;
766 default:
767 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100768 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100769 event->trigger_type = type;
770 event->trigger_time = trigger_time;
771 event->is_signaled = false;
772 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100773}
774
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200775/**
Mario Six8fac2912018-07-10 08:40:17 +0200776 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
777 * event
778 * @event: event for which the timer is set
779 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200780 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200781 *
782 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200783 *
Mario Six8fac2912018-07-10 08:40:17 +0200784 * See the Unified Extensible Firmware Interface (UEFI) specification for
785 * details.
786 *
787 *
788 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200789 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200790static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
791 enum efi_timer_delay type,
792 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200793{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900794 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200795 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
796}
797
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200798/**
Mario Six8fac2912018-07-10 08:40:17 +0200799 * efi_wait_for_event() - wait for events to be signaled
800 * @num_events: number of events to be waited for
801 * @event: events to be waited for
802 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200803 *
804 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200805 *
Mario Six8fac2912018-07-10 08:40:17 +0200806 * See the Unified Extensible Firmware Interface (UEFI) specification for
807 * details.
808 *
809 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200810 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100811static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200812 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100813 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100814{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100815 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100816
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100817 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100818
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200819 /* Check parameters */
820 if (!num_events || !event)
821 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200822 /* Check TPL */
823 if (efi_tpl != TPL_APPLICATION)
824 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200825 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100826 if (efi_is_event(event[i]) != EFI_SUCCESS)
827 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200828 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
829 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200830 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100831 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200832 }
833
834 /* Wait for signal */
835 for (;;) {
836 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200837 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200838 goto out;
839 }
840 /* Allow events to occur. */
841 efi_timer_check();
842 }
843
844out:
845 /*
846 * Reset the signal which is passed to the caller to allow periodic
847 * events to occur.
848 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200849 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200850 if (index)
851 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100852
853 return EFI_EXIT(EFI_SUCCESS);
854}
855
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200856/**
Mario Six8fac2912018-07-10 08:40:17 +0200857 * efi_signal_event_ext() - signal an EFI event
858 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200859 *
860 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200861 *
862 * See the Unified Extensible Firmware Interface (UEFI) specification for
863 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200864 *
865 * This functions sets the signaled state of the event and queues the
866 * notification function for execution.
867 *
Mario Six8fac2912018-07-10 08:40:17 +0200868 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200869 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200870static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100871{
872 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100873 if (efi_is_event(event) != EFI_SUCCESS)
874 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100875 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100876 return EFI_EXIT(EFI_SUCCESS);
877}
878
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200879/**
Mario Six8fac2912018-07-10 08:40:17 +0200880 * efi_close_event() - close an EFI event
881 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200882 *
883 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 *
Mario Six8fac2912018-07-10 08:40:17 +0200885 * See the Unified Extensible Firmware Interface (UEFI) specification for
886 * details.
887 *
888 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200889 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200890static efi_status_t EFIAPI efi_close_event(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);
895 list_del(&event->link);
896 free(event);
897 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100898}
899
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200900/**
Mario Six8fac2912018-07-10 08:40:17 +0200901 * efi_check_event() - check if an event is signaled
902 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200903 *
904 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200905 *
906 * See the Unified Extensible Firmware Interface (UEFI) specification for
907 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200908 *
Mario Six8fac2912018-07-10 08:40:17 +0200909 * If an event is not signaled yet, the notification function is queued. The
910 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200911 *
Mario Six8fac2912018-07-10 08:40:17 +0200912 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200913 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200914static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100915{
916 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200917 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100918 if (efi_is_event(event) != EFI_SUCCESS ||
919 event->type & EVT_NOTIFY_SIGNAL)
920 return EFI_EXIT(EFI_INVALID_PARAMETER);
921 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100922 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100923 if (event->is_signaled) {
924 event->is_signaled = false;
925 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200926 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100927 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100928}
929
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200930/**
Mario Six8fac2912018-07-10 08:40:17 +0200931 * efi_search_obj() - find the internal EFI object for a handle
932 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200933 *
Mario Six8fac2912018-07-10 08:40:17 +0200934 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200935 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100936struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200937{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100938 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200939
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100940 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200941 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200942 return efiobj;
943 }
944
945 return NULL;
946}
947
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200948/**
Mario Six8fac2912018-07-10 08:40:17 +0200949 * efi_open_protocol_info_entry() - create open protocol info entry and add it
950 * to a protocol
951 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100952 *
Mario Six8fac2912018-07-10 08:40:17 +0200953 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100954 */
955static struct efi_open_protocol_info_entry *efi_create_open_info(
956 struct efi_handler *handler)
957{
958 struct efi_open_protocol_info_item *item;
959
960 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
961 if (!item)
962 return NULL;
963 /* Append the item to the open protocol info list. */
964 list_add_tail(&item->link, &handler->open_infos);
965
966 return &item->info;
967}
968
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200969/**
Mario Six8fac2912018-07-10 08:40:17 +0200970 * efi_delete_open_info() - remove an open protocol info entry from a protocol
971 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100972 *
Mario Six8fac2912018-07-10 08:40:17 +0200973 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100974 */
975static efi_status_t efi_delete_open_info(
976 struct efi_open_protocol_info_item *item)
977{
978 list_del(&item->link);
979 free(item);
980 return EFI_SUCCESS;
981}
982
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200983/**
Mario Six8fac2912018-07-10 08:40:17 +0200984 * efi_add_protocol() - install new protocol on a handle
985 * @handle: handle on which the protocol shall be installed
986 * @protocol: GUID of the protocol to be installed
987 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200988 *
Mario Six8fac2912018-07-10 08:40:17 +0200989 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200990 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100991efi_status_t efi_add_protocol(const efi_handle_t handle,
992 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200993 void *protocol_interface)
994{
995 struct efi_object *efiobj;
996 struct efi_handler *handler;
997 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200998
999 efiobj = efi_search_obj(handle);
1000 if (!efiobj)
1001 return EFI_INVALID_PARAMETER;
1002 ret = efi_search_protocol(handle, protocol, NULL);
1003 if (ret != EFI_NOT_FOUND)
1004 return EFI_INVALID_PARAMETER;
1005 handler = calloc(1, sizeof(struct efi_handler));
1006 if (!handler)
1007 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001008 handler->guid = protocol;
1009 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001010 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001011 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001012 if (!guidcmp(&efi_guid_device_path, protocol))
1013 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001014 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001015}
1016
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001017/**
Mario Six8fac2912018-07-10 08:40:17 +02001018 * efi_install_protocol_interface() - install protocol interface
1019 * @handle: handle on which the protocol shall be installed
1020 * @protocol: GUID of the protocol to be installed
1021 * @protocol_interface_type: type of the interface to be installed,
1022 * always EFI_NATIVE_INTERFACE
1023 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001024 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001025 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001026 *
Mario Six8fac2912018-07-10 08:40:17 +02001027 * See the Unified Extensible Firmware Interface (UEFI) specification for
1028 * details.
1029 *
1030 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001031 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001032static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001033 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001034 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001035{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001036 efi_status_t r;
1037
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001038 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1039 protocol_interface);
1040
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001041 if (!handle || !protocol ||
1042 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1043 r = EFI_INVALID_PARAMETER;
1044 goto out;
1045 }
1046
1047 /* Create new handle if requested. */
1048 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001049 r = efi_create_handle(handle);
1050 if (r != EFI_SUCCESS)
1051 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001052 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1053 *handle);
1054 } else {
1055 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1056 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001057 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001058 /* Add new protocol */
1059 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001060out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001061 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001062}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001063
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001064/**
Mario Six8fac2912018-07-10 08:40:17 +02001065 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001066 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001067 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001068 * @number_of_drivers: number of child controllers
1069 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001070 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001071 * The allocated buffer has to be freed with free().
1072 *
Mario Six8fac2912018-07-10 08:40:17 +02001073 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001074 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001075static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001076 const efi_guid_t *protocol,
1077 efi_uintn_t *number_of_drivers,
1078 efi_handle_t **driver_handle_buffer)
1079{
1080 struct efi_handler *handler;
1081 struct efi_open_protocol_info_item *item;
1082 efi_uintn_t count = 0, i;
1083 bool duplicate;
1084
1085 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001086 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001087 if (protocol && guidcmp(handler->guid, protocol))
1088 continue;
1089 list_for_each_entry(item, &handler->open_infos, link) {
1090 if (item->info.attributes &
1091 EFI_OPEN_PROTOCOL_BY_DRIVER)
1092 ++count;
1093 }
1094 }
1095 /*
1096 * Create buffer. In case of duplicate driver assignments the buffer
1097 * will be too large. But that does not harm.
1098 */
1099 *number_of_drivers = 0;
1100 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1101 if (!*driver_handle_buffer)
1102 return EFI_OUT_OF_RESOURCES;
1103 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001104 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001105 if (protocol && guidcmp(handler->guid, protocol))
1106 continue;
1107 list_for_each_entry(item, &handler->open_infos, link) {
1108 if (item->info.attributes &
1109 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1110 /* Check this is a new driver */
1111 duplicate = false;
1112 for (i = 0; i < *number_of_drivers; ++i) {
1113 if ((*driver_handle_buffer)[i] ==
1114 item->info.agent_handle)
1115 duplicate = true;
1116 }
1117 /* Copy handle to buffer */
1118 if (!duplicate) {
1119 i = (*number_of_drivers)++;
1120 (*driver_handle_buffer)[i] =
1121 item->info.agent_handle;
1122 }
1123 }
1124 }
1125 }
1126 return EFI_SUCCESS;
1127}
1128
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001129/**
Mario Six8fac2912018-07-10 08:40:17 +02001130 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001131 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001132 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001133 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001134 *
1135 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001136 *
1137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1138 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001139 *
Mario Six8fac2912018-07-10 08:40:17 +02001140 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001141 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001142static efi_status_t efi_disconnect_all_drivers
1143 (efi_handle_t handle,
1144 const efi_guid_t *protocol,
1145 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001146{
1147 efi_uintn_t number_of_drivers;
1148 efi_handle_t *driver_handle_buffer;
1149 efi_status_t r, ret;
1150
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001151 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001152 &driver_handle_buffer);
1153 if (ret != EFI_SUCCESS)
1154 return ret;
1155
1156 ret = EFI_NOT_FOUND;
1157 while (number_of_drivers) {
1158 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001159 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001160 driver_handle_buffer[--number_of_drivers],
1161 child_handle));
1162 if (r == EFI_SUCCESS)
1163 ret = r;
1164 }
1165 free(driver_handle_buffer);
1166 return ret;
1167}
1168
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001169/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001170 * efi_uninstall_protocol() - uninstall protocol interface
1171 *
Mario Six8fac2912018-07-10 08:40:17 +02001172 * @handle: handle from which the protocol shall be removed
1173 * @protocol: GUID of the protocol to be removed
1174 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001175 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001176 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001177 *
1178 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001179 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001180static efi_status_t efi_uninstall_protocol
1181 (efi_handle_t handle, const efi_guid_t *protocol,
1182 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001183{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001184 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001185 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001186 struct efi_open_protocol_info_item *item;
1187 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001188 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001189
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001190 /* Check handle */
1191 efiobj = efi_search_obj(handle);
1192 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001193 r = EFI_INVALID_PARAMETER;
1194 goto out;
1195 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001196 /* Find the protocol on the handle */
1197 r = efi_search_protocol(handle, protocol, &handler);
1198 if (r != EFI_SUCCESS)
1199 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001200 /* Disconnect controllers */
1201 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1202 if (!list_empty(&handler->open_infos)) {
1203 r = EFI_ACCESS_DENIED;
1204 goto out;
1205 }
1206 /* Close protocol */
1207 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1208 if (item->info.attributes ==
1209 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1210 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1211 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1212 list_del(&item->link);
1213 }
1214 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001215 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001216 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001217 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001218 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001219out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001220 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001221}
1222
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001223/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001224 * efi_uninstall_protocol_interface() - uninstall protocol interface
1225 * @handle: handle from which the protocol shall be removed
1226 * @protocol: GUID of the protocol to be removed
1227 * @protocol_interface: interface to be removed
1228 *
1229 * This function implements the UninstallProtocolInterface service.
1230 *
1231 * See the Unified Extensible Firmware Interface (UEFI) specification for
1232 * details.
1233 *
1234 * Return: status code
1235 */
1236static efi_status_t EFIAPI efi_uninstall_protocol_interface
1237 (efi_handle_t handle, const efi_guid_t *protocol,
1238 void *protocol_interface)
1239{
1240 efi_status_t ret;
1241
1242 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1243
1244 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1245 if (ret != EFI_SUCCESS)
1246 goto out;
1247
1248 /* If the last protocol has been removed, delete the handle. */
1249 if (list_empty(&handle->protocols)) {
1250 list_del(&handle->link);
1251 free(handle);
1252 }
1253out:
1254 return EFI_EXIT(ret);
1255}
1256
1257/**
Mario Six8fac2912018-07-10 08:40:17 +02001258 * efi_register_protocol_notify() - register an event for notification when a
1259 * protocol is installed.
1260 * @protocol: GUID of the protocol whose installation shall be notified
1261 * @event: event to be signaled upon installation of the protocol
1262 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001263 *
1264 * This function implements the RegisterProtocolNotify service.
1265 * See the Unified Extensible Firmware Interface (UEFI) specification
1266 * for details.
1267 *
Mario Six8fac2912018-07-10 08:40:17 +02001268 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001269 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001270static efi_status_t EFIAPI efi_register_protocol_notify(
1271 const efi_guid_t *protocol,
1272 struct efi_event *event,
1273 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001274{
Rob Clark238f88c2017-09-13 18:05:41 -04001275 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001276 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1277}
1278
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001279/**
Mario Six8fac2912018-07-10 08:40:17 +02001280 * efi_search() - determine if an EFI handle implements a protocol
1281 * @search_type: selection criterion
1282 * @protocol: GUID of the protocol
1283 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001284 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001285 *
1286 * See the documentation of the LocateHandle service in the UEFI specification.
1287 *
Mario Six8fac2912018-07-10 08:40:17 +02001288 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001289 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001290static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001291 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001292 efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001293{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001294 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001295
1296 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001297 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001298 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001299 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001300 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001301 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001302 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001303 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001304 return (ret != EFI_SUCCESS);
1305 default:
1306 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001307 return -1;
1308 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001309}
1310
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001311/**
Mario Six8fac2912018-07-10 08:40:17 +02001312 * efi_locate_handle() - locate handles implementing a protocol
1313 * @search_type: selection criterion
1314 * @protocol: GUID of the protocol
1315 * @search_key: registration key
1316 * @buffer_size: size of the buffer to receive the handles in bytes
1317 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001318 *
1319 * This function is meant for U-Boot internal calls. For the API implementation
1320 * of the LocateHandle service see efi_locate_handle_ext.
1321 *
Mario Six8fac2912018-07-10 08:40:17 +02001322 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001323 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001324static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001325 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001326 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001327 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001328{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001329 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001330 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001331
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001332 /* Check parameters */
1333 switch (search_type) {
1334 case ALL_HANDLES:
1335 break;
1336 case BY_REGISTER_NOTIFY:
1337 if (!search_key)
1338 return EFI_INVALID_PARAMETER;
1339 /* RegisterProtocolNotify is not implemented yet */
1340 return EFI_UNSUPPORTED;
1341 case BY_PROTOCOL:
1342 if (!protocol)
1343 return EFI_INVALID_PARAMETER;
1344 break;
1345 default:
1346 return EFI_INVALID_PARAMETER;
1347 }
1348
1349 /*
1350 * efi_locate_handle_buffer uses this function for
1351 * the calculation of the necessary buffer size.
1352 * So do not require a buffer for buffersize == 0.
1353 */
1354 if (!buffer_size || (*buffer_size && !buffer))
1355 return EFI_INVALID_PARAMETER;
1356
Alexander Grafc15d9212016-03-04 01:09:59 +01001357 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001358 list_for_each_entry(efiobj, &efi_obj_list, link) {
1359 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001360 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001361 }
1362
1363 if (*buffer_size < size) {
1364 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001365 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001366 }
1367
Rob Clarkcdee3372017-08-06 14:10:07 -04001368 *buffer_size = size;
1369 if (size == 0)
1370 return EFI_NOT_FOUND;
1371
Alexander Grafc15d9212016-03-04 01:09:59 +01001372 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001373 list_for_each_entry(efiobj, &efi_obj_list, link) {
1374 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001375 *buffer++ = efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01001376 }
1377
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001378 return EFI_SUCCESS;
1379}
1380
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001381/**
Mario Six8fac2912018-07-10 08:40:17 +02001382 * efi_locate_handle_ext() - locate handles implementing a protocol.
1383 * @search_type: selection criterion
1384 * @protocol: GUID of the protocol
1385 * @search_key: registration key
1386 * @buffer_size: size of the buffer to receive the handles in bytes
1387 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001388 *
1389 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001390 *
Mario Six8fac2912018-07-10 08:40:17 +02001391 * See the Unified Extensible Firmware Interface (UEFI) specification for
1392 * details.
1393 *
1394 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001395 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001396static efi_status_t EFIAPI efi_locate_handle_ext(
1397 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001398 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001399 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001400{
Rob Clark238f88c2017-09-13 18:05:41 -04001401 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001402 buffer_size, buffer);
1403
1404 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1405 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001406}
1407
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001408/**
Mario Six8fac2912018-07-10 08:40:17 +02001409 * efi_remove_configuration_table() - collapses configuration table entries,
1410 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001411 *
Mario Six8fac2912018-07-10 08:40:17 +02001412 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001413 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001414static void efi_remove_configuration_table(int i)
1415{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001416 struct efi_configuration_table *this = &systab.tables[i];
1417 struct efi_configuration_table *next = &systab.tables[i + 1];
1418 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001419
1420 memmove(this, next, (ulong)end - (ulong)next);
1421 systab.nr_tables--;
1422}
1423
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001424/**
Mario Six8fac2912018-07-10 08:40:17 +02001425 * efi_install_configuration_table() - adds, updates, or removes a
1426 * configuration table
1427 * @guid: GUID of the installed table
1428 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001429 *
1430 * This function is used for internal calls. For the API implementation of the
1431 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1432 *
Mario Six8fac2912018-07-10 08:40:17 +02001433 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001434 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001435efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1436 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001437{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001438 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001439 int i;
1440
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001441 if (!guid)
1442 return EFI_INVALID_PARAMETER;
1443
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001444 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001445 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001446 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001447 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001448 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001449 else
1450 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001451 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001452 }
1453 }
1454
Alexander Graffe3366f2017-07-26 13:41:04 +02001455 if (!table)
1456 return EFI_NOT_FOUND;
1457
Alexander Grafc15d9212016-03-04 01:09:59 +01001458 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001459 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001460 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001461
1462 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001463 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1464 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001465 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001466
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001467out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001468 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001469 efi_update_table_header_crc32(&systab.hdr);
1470
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001471 /* Notify that the configuration table was changed */
1472 list_for_each_entry(evt, &efi_events, link) {
1473 if (evt->group && !guidcmp(evt->group, guid)) {
1474 efi_signal_event(evt, false);
1475 break;
1476 }
1477 }
1478
Alexander Grafc5c11632016-08-19 01:23:24 +02001479 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001480}
1481
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001482/**
Mario Six8fac2912018-07-10 08:40:17 +02001483 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1484 * configuration table.
1485 * @guid: GUID of the installed table
1486 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001487 *
1488 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001489 *
Mario Six8fac2912018-07-10 08:40:17 +02001490 * See the Unified Extensible Firmware Interface (UEFI) specification for
1491 * details.
1492 *
1493 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001494 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001495static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1496 void *table)
1497{
Rob Clark238f88c2017-09-13 18:05:41 -04001498 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001499 return EFI_EXIT(efi_install_configuration_table(guid, table));
1500}
1501
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001502/**
Mario Six8fac2912018-07-10 08:40:17 +02001503 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001504 *
1505 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001506 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001507 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001508 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1509 * code is returned.
1510 *
1511 * @device_path: device path of the loaded image
1512 * @file_path: file path of the loaded image
1513 * @handle_ptr: handle of the loaded image
1514 * @info_ptr: loaded image protocol
1515 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001516 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001517efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1518 struct efi_device_path *file_path,
1519 struct efi_loaded_image_obj **handle_ptr,
1520 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001521{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001522 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001523 struct efi_loaded_image *info = NULL;
1524 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001525 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001526
1527 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1528 *handle_ptr = NULL;
1529 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001530
1531 info = calloc(1, sizeof(*info));
1532 if (!info)
1533 return EFI_OUT_OF_RESOURCES;
1534 obj = calloc(1, sizeof(*obj));
1535 if (!obj) {
1536 free(info);
1537 return EFI_OUT_OF_RESOURCES;
1538 }
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001539
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001540 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001541 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001542
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001543 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001544 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001545 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001546
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001547 if (device_path) {
1548 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001549
1550 dp = efi_dp_append(device_path, file_path);
1551 if (!dp) {
1552 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001553 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001554 }
1555 } else {
1556 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001557 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001558 ret = efi_add_protocol(&obj->header,
1559 &efi_guid_loaded_image_device_path, dp);
1560 if (ret != EFI_SUCCESS)
1561 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001562
1563 /*
1564 * When asking for the loaded_image interface, just
1565 * return handle which points to loaded_image_info
1566 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001567 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001568 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001569 if (ret != EFI_SUCCESS)
1570 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001571
Alexander Graf44be5da2019-02-11 15:24:00 +01001572#if CONFIG_IS_ENABLED(EFI_LOADER_HII)
Leif Lindholm5aab1b82019-01-21 12:12:57 +09001573 ret = efi_add_protocol(&obj->header,
1574 &efi_guid_hii_string_protocol,
1575 (void *)&efi_hii_string);
1576 if (ret != EFI_SUCCESS)
1577 goto failure;
1578
1579 ret = efi_add_protocol(&obj->header,
1580 &efi_guid_hii_database_protocol,
1581 (void *)&efi_hii_database);
1582 if (ret != EFI_SUCCESS)
1583 goto failure;
1584
AKASHI Takahiroca8aa712019-01-21 12:13:00 +09001585 ret = efi_add_protocol(&obj->header,
1586 &efi_guid_hii_config_routing_protocol,
1587 (void *)&efi_hii_config_routing);
1588 if (ret != EFI_SUCCESS)
1589 goto failure;
Alexander Graf44be5da2019-02-11 15:24:00 +01001590#endif
AKASHI Takahiroca8aa712019-01-21 12:13:00 +09001591
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001592 *info_ptr = info;
1593 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001594
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001595 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001596failure:
1597 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001598 efi_delete_handle(&obj->header);
1599 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001600 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001601}
1602
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001603/**
Mario Six8fac2912018-07-10 08:40:17 +02001604 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001605 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001606 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1607 * callers obligation to update the memory type as needed.
1608 *
1609 * @file_path: the path of the image to load
1610 * @buffer: buffer containing the loaded image
1611 * @size: size of the loaded image
1612 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001613 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001614efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001615 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001616{
1617 struct efi_file_info *info = NULL;
1618 struct efi_file_handle *f;
1619 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001620 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001621 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001622
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001623 /* In case of failure nothing is returned */
1624 *buffer = NULL;
1625 *size = 0;
1626
1627 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001628 f = efi_file_from_path(file_path);
1629 if (!f)
1630 return EFI_DEVICE_ERROR;
1631
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001632 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001633 bs = 0;
1634 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1635 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001636 if (ret != EFI_BUFFER_TOO_SMALL) {
1637 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001638 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001639 }
Rob Clark857a1222017-09-13 18:05:35 -04001640
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001641 info = malloc(bs);
1642 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1643 info));
1644 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001645 goto error;
1646
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001647 /*
1648 * When reading the file we do not yet know if it contains an
1649 * application, a boottime driver, or a runtime driver. So here we
1650 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1651 * update the reservation according to the image type.
1652 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001653 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001654 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1655 EFI_BOOT_SERVICES_DATA,
1656 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001657 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001658 ret = EFI_OUT_OF_RESOURCES;
1659 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001660 }
1661
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001662 /* Read file */
1663 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1664 if (ret != EFI_SUCCESS)
1665 efi_free_pages(addr, efi_size_in_pages(bs));
1666 *buffer = (void *)(uintptr_t)addr;
1667 *size = bs;
1668error:
1669 EFI_CALL(f->close(f));
1670 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001671 return ret;
1672}
1673
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001674/**
Mario Six8fac2912018-07-10 08:40:17 +02001675 * efi_load_image() - load an EFI image into memory
1676 * @boot_policy: true for request originating from the boot manager
1677 * @parent_image: the caller's image handle
1678 * @file_path: the path of the image to load
1679 * @source_buffer: memory location from which the image is installed
1680 * @source_size: size of the memory area from which the image is installed
1681 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001682 *
1683 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001684 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001685 * See the Unified Extensible Firmware Interface (UEFI) specification
1686 * for details.
1687 *
Mario Six8fac2912018-07-10 08:40:17 +02001688 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001689 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001690efi_status_t EFIAPI efi_load_image(bool boot_policy,
1691 efi_handle_t parent_image,
1692 struct efi_device_path *file_path,
1693 void *source_buffer,
1694 efi_uintn_t source_size,
1695 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001696{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001697 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001698 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001699 struct efi_loaded_image_obj **image_obj =
1700 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001701 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001702 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001703
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001704 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001705 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001706
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001707 if (!image_handle || !parent_image) {
1708 ret = EFI_INVALID_PARAMETER;
1709 goto error;
1710 }
1711
1712 if (!source_buffer && !file_path) {
1713 ret = EFI_NOT_FOUND;
1714 goto error;
1715 }
Rob Clark857a1222017-09-13 18:05:35 -04001716
1717 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001718 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001719 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001720 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001721 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001722 /*
1723 * split file_path which contains both the device and
1724 * file parts:
1725 */
1726 efi_dp_split_file_path(file_path, &dp, &fp);
Rob Clark857a1222017-09-13 18:05:35 -04001727 } else {
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001728 /* In this case, file_path is the "device" path, i.e.
Rob Clark857a1222017-09-13 18:05:35 -04001729 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1730 */
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001731 dest_buffer = source_buffer;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001732 dp = file_path;
1733 fp = NULL;
Rob Clark857a1222017-09-13 18:05:35 -04001734 }
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001735 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001736 if (ret == EFI_SUCCESS)
1737 ret = efi_load_pe(*image_obj, dest_buffer, info);
1738 if (!source_buffer)
1739 /* Release buffer to which file was loaded */
1740 efi_free_pages((uintptr_t)dest_buffer,
1741 efi_size_in_pages(source_size));
1742 if (ret == EFI_SUCCESS) {
1743 info->system_table = &systab;
1744 info->parent_handle = parent_image;
1745 } else {
1746 /* The image is invalid. Release all associated resources. */
1747 efi_delete_handle(*image_handle);
1748 *image_handle = NULL;
1749 free(info);
1750 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001751error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001752 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001753}
1754
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001755/**
Mario Six8fac2912018-07-10 08:40:17 +02001756 * efi_unload_image() - unload an EFI image
1757 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001758 *
1759 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001760 *
1761 * See the Unified Extensible Firmware Interface (UEFI) specification for
1762 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001763 *
Mario Six8fac2912018-07-10 08:40:17 +02001764 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001765 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001766efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001767{
1768 struct efi_object *efiobj;
1769
1770 EFI_ENTRY("%p", image_handle);
1771 efiobj = efi_search_obj(image_handle);
1772 if (efiobj)
1773 list_del(&efiobj->link);
1774
1775 return EFI_EXIT(EFI_SUCCESS);
1776}
1777
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001778/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001779 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1780 */
1781static void efi_exit_caches(void)
1782{
1783#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1784 /*
1785 * Grub on 32bit ARM needs to have caches disabled before jumping into
1786 * a zImage, but does not know of all cache layers. Give it a hand.
1787 */
1788 if (efi_is_direct_boot)
1789 cleanup_before_linux();
1790#endif
1791}
1792
1793/**
Mario Six8fac2912018-07-10 08:40:17 +02001794 * efi_exit_boot_services() - stop all boot services
1795 * @image_handle: handle of the loaded image
1796 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001797 *
1798 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001799 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001800 * See the Unified Extensible Firmware Interface (UEFI) specification
1801 * for details.
1802 *
Mario Six8fac2912018-07-10 08:40:17 +02001803 * All timer events are disabled. For exit boot services events the
1804 * notification function is called. The boot services are disabled in the
1805 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001806 *
Mario Six8fac2912018-07-10 08:40:17 +02001807 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001808 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001809static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001810 unsigned long map_key)
1811{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001812 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001813
Alexander Grafc15d9212016-03-04 01:09:59 +01001814 EFI_ENTRY("%p, %ld", image_handle, map_key);
1815
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001816 /* Check that the caller has read the current memory map */
1817 if (map_key != efi_memory_map_key)
1818 return EFI_INVALID_PARAMETER;
1819
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001820 /* Make sure that notification functions are not called anymore */
1821 efi_tpl = TPL_HIGH_LEVEL;
1822
1823 /* Check if ExitBootServices has already been called */
1824 if (!systab.boottime)
1825 return EFI_EXIT(EFI_SUCCESS);
1826
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001827 /* Add related events to the event group */
1828 list_for_each_entry(evt, &efi_events, link) {
1829 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1830 evt->group = &efi_guid_event_group_exit_boot_services;
1831 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001832 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001833 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001834 if (evt->group &&
1835 !guidcmp(evt->group,
1836 &efi_guid_event_group_exit_boot_services)) {
1837 efi_signal_event(evt, false);
1838 break;
1839 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001840 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001841
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001842 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001843
Alexander Graf2ebeb442016-11-17 01:02:57 +01001844 board_quiesce_devices();
1845
Alexander Graffa5246b2018-11-15 21:23:47 +01001846 /* Fix up caches for EFI payloads if necessary */
1847 efi_exit_caches();
1848
Alexander Grafc15d9212016-03-04 01:09:59 +01001849 /* This stops all lingering devices */
1850 bootm_disable_interrupts();
1851
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001852 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001853 systab.con_in_handle = NULL;
1854 systab.con_in = NULL;
1855 systab.con_out_handle = NULL;
1856 systab.con_out = NULL;
1857 systab.stderr_handle = NULL;
1858 systab.std_err = NULL;
1859 systab.boottime = NULL;
1860
1861 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001862 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001863
Alexander Grafc15d9212016-03-04 01:09:59 +01001864 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001865 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001866 WATCHDOG_RESET();
1867
1868 return EFI_EXIT(EFI_SUCCESS);
1869}
1870
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001871/**
Mario Six8fac2912018-07-10 08:40:17 +02001872 * efi_get_next_monotonic_count() - get next value of the counter
1873 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001874 *
1875 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001876 *
1877 * See the Unified Extensible Firmware Interface (UEFI) specification for
1878 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001879 *
Mario Six8fac2912018-07-10 08:40:17 +02001880 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001881 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001882static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1883{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001884 static uint64_t mono;
1885
Alexander Grafc15d9212016-03-04 01:09:59 +01001886 EFI_ENTRY("%p", count);
1887 *count = mono++;
1888 return EFI_EXIT(EFI_SUCCESS);
1889}
1890
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001891/**
Mario Six8fac2912018-07-10 08:40:17 +02001892 * efi_stall() - sleep
1893 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001894 *
Mario Six8fac2912018-07-10 08:40:17 +02001895 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001896 *
Mario Six8fac2912018-07-10 08:40:17 +02001897 * See the Unified Extensible Firmware Interface (UEFI) specification for
1898 * details.
1899 *
1900 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001901 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001902static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1903{
1904 EFI_ENTRY("%ld", microseconds);
1905 udelay(microseconds);
1906 return EFI_EXIT(EFI_SUCCESS);
1907}
1908
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001909/**
Mario Six8fac2912018-07-10 08:40:17 +02001910 * efi_set_watchdog_timer() - reset the watchdog timer
1911 * @timeout: seconds before reset by watchdog
1912 * @watchdog_code: code to be logged when resetting
1913 * @data_size: size of buffer in bytes
1914 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001915 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001916 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001917 *
Mario Six8fac2912018-07-10 08:40:17 +02001918 * See the Unified Extensible Firmware Interface (UEFI) specification for
1919 * details.
1920 *
1921 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001922 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001923static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1924 uint64_t watchdog_code,
1925 unsigned long data_size,
1926 uint16_t *watchdog_data)
1927{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001928 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001929 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001930 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001931}
1932
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001933/**
Mario Six8fac2912018-07-10 08:40:17 +02001934 * efi_close_protocol() - close a protocol
1935 * @handle: handle on which the protocol shall be closed
1936 * @protocol: GUID of the protocol to close
1937 * @agent_handle: handle of the driver
1938 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001939 *
1940 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001941 *
Mario Six8fac2912018-07-10 08:40:17 +02001942 * See the Unified Extensible Firmware Interface (UEFI) specification for
1943 * details.
1944 *
1945 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001946 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001947static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001948 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001949 efi_handle_t agent_handle,
1950 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001951{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001952 struct efi_handler *handler;
1953 struct efi_open_protocol_info_item *item;
1954 struct efi_open_protocol_info_item *pos;
1955 efi_status_t r;
1956
Rob Clark238f88c2017-09-13 18:05:41 -04001957 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001958 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001959
1960 if (!agent_handle) {
1961 r = EFI_INVALID_PARAMETER;
1962 goto out;
1963 }
1964 r = efi_search_protocol(handle, protocol, &handler);
1965 if (r != EFI_SUCCESS)
1966 goto out;
1967
1968 r = EFI_NOT_FOUND;
1969 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1970 if (item->info.agent_handle == agent_handle &&
1971 item->info.controller_handle == controller_handle) {
1972 efi_delete_open_info(item);
1973 r = EFI_SUCCESS;
1974 break;
1975 }
1976 }
1977out:
1978 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001979}
1980
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001981/**
Mario Six8fac2912018-07-10 08:40:17 +02001982 * efi_open_protocol_information() - provide information about then open status
1983 * of a protocol on a handle
1984 * @handle: handle for which the information shall be retrieved
1985 * @protocol: GUID of the protocol
1986 * @entry_buffer: buffer to receive the open protocol information
1987 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001988 *
1989 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02001990 *
1991 * See the Unified Extensible Firmware Interface (UEFI) specification for
1992 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001993 *
Mario Six8fac2912018-07-10 08:40:17 +02001994 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001995 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001996static efi_status_t EFIAPI efi_open_protocol_information(
1997 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01001998 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001999 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002000{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002001 unsigned long buffer_size;
2002 unsigned long count;
2003 struct efi_handler *handler;
2004 struct efi_open_protocol_info_item *item;
2005 efi_status_t r;
2006
Rob Clark238f88c2017-09-13 18:05:41 -04002007 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002008 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002009
2010 /* Check parameters */
2011 if (!entry_buffer) {
2012 r = EFI_INVALID_PARAMETER;
2013 goto out;
2014 }
2015 r = efi_search_protocol(handle, protocol, &handler);
2016 if (r != EFI_SUCCESS)
2017 goto out;
2018
2019 /* Count entries */
2020 count = 0;
2021 list_for_each_entry(item, &handler->open_infos, link) {
2022 if (item->info.open_count)
2023 ++count;
2024 }
2025 *entry_count = count;
2026 *entry_buffer = NULL;
2027 if (!count) {
2028 r = EFI_SUCCESS;
2029 goto out;
2030 }
2031
2032 /* Copy entries */
2033 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002034 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002035 (void **)entry_buffer);
2036 if (r != EFI_SUCCESS)
2037 goto out;
2038 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2039 if (item->info.open_count)
2040 (*entry_buffer)[--count] = item->info;
2041 }
2042out:
2043 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002044}
2045
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002046/**
Mario Six8fac2912018-07-10 08:40:17 +02002047 * efi_protocols_per_handle() - get protocols installed on a handle
2048 * @handle: handle for which the information is retrieved
2049 * @protocol_buffer: buffer with protocol GUIDs
2050 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002051 *
2052 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002053 *
Mario Six8fac2912018-07-10 08:40:17 +02002054 * See the Unified Extensible Firmware Interface (UEFI) specification for
2055 * details.
2056 *
2057 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002058 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002059static efi_status_t EFIAPI efi_protocols_per_handle(
2060 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002061 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002062{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002063 unsigned long buffer_size;
2064 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002065 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002066 efi_status_t r;
2067
Alexander Grafc15d9212016-03-04 01:09:59 +01002068 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2069 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002070
2071 if (!handle || !protocol_buffer || !protocol_buffer_count)
2072 return EFI_EXIT(EFI_INVALID_PARAMETER);
2073
2074 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002075 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002076
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002077 efiobj = efi_search_obj(handle);
2078 if (!efiobj)
2079 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002080
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002081 /* Count protocols */
2082 list_for_each(protocol_handle, &efiobj->protocols) {
2083 ++*protocol_buffer_count;
2084 }
2085
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002086 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002087 if (*protocol_buffer_count) {
2088 size_t j = 0;
2089
2090 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002091 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002092 (void **)protocol_buffer);
2093 if (r != EFI_SUCCESS)
2094 return EFI_EXIT(r);
2095 list_for_each(protocol_handle, &efiobj->protocols) {
2096 struct efi_handler *protocol;
2097
2098 protocol = list_entry(protocol_handle,
2099 struct efi_handler, link);
2100 (*protocol_buffer)[j] = (void *)protocol->guid;
2101 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002102 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002103 }
2104
2105 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002106}
2107
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002108/**
Mario Six8fac2912018-07-10 08:40:17 +02002109 * efi_locate_handle_buffer() - locate handles implementing a protocol
2110 * @search_type: selection criterion
2111 * @protocol: GUID of the protocol
2112 * @search_key: registration key
2113 * @no_handles: number of returned handles
2114 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002115 *
2116 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002117 *
Mario Six8fac2912018-07-10 08:40:17 +02002118 * See the Unified Extensible Firmware Interface (UEFI) specification for
2119 * details.
2120 *
2121 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002122 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002123static efi_status_t EFIAPI efi_locate_handle_buffer(
2124 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002125 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002126 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002127{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002128 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002129 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002130
Rob Clark238f88c2017-09-13 18:05:41 -04002131 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002132 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002133
2134 if (!no_handles || !buffer) {
2135 r = EFI_INVALID_PARAMETER;
2136 goto out;
2137 }
2138 *no_handles = 0;
2139 *buffer = NULL;
2140 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2141 *buffer);
2142 if (r != EFI_BUFFER_TOO_SMALL)
2143 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002144 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002145 (void **)buffer);
2146 if (r != EFI_SUCCESS)
2147 goto out;
2148 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2149 *buffer);
2150 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002151 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002152out:
2153 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002154}
2155
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002156/**
Mario Six8fac2912018-07-10 08:40:17 +02002157 * efi_locate_protocol() - find an interface implementing a protocol
2158 * @protocol: GUID of the protocol
2159 * @registration: registration key passed to the notification function
2160 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002161 *
2162 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002163 *
2164 * See the Unified Extensible Firmware Interface (UEFI) specification for
2165 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002166 *
Mario Six8fac2912018-07-10 08:40:17 +02002167 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002168 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002169static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002170 void *registration,
2171 void **protocol_interface)
2172{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002173 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002174 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002175
Rob Clark238f88c2017-09-13 18:05:41 -04002176 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002177
2178 if (!protocol || !protocol_interface)
2179 return EFI_EXIT(EFI_INVALID_PARAMETER);
2180
2181 list_for_each(lhandle, &efi_obj_list) {
2182 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002183 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002184
2185 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002186
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002187 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002188 if (ret == EFI_SUCCESS) {
2189 *protocol_interface = handler->protocol_interface;
2190 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002191 }
2192 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002193 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002194
2195 return EFI_EXIT(EFI_NOT_FOUND);
2196}
2197
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002198/**
Mario Six8fac2912018-07-10 08:40:17 +02002199 * efi_locate_device_path() - Get the device path and handle of an device
2200 * implementing a protocol
2201 * @protocol: GUID of the protocol
2202 * @device_path: device path
2203 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002204 *
2205 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002206 *
Mario Six8fac2912018-07-10 08:40:17 +02002207 * See the Unified Extensible Firmware Interface (UEFI) specification for
2208 * details.
2209 *
2210 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002211 */
2212static efi_status_t EFIAPI efi_locate_device_path(
2213 const efi_guid_t *protocol,
2214 struct efi_device_path **device_path,
2215 efi_handle_t *device)
2216{
2217 struct efi_device_path *dp;
2218 size_t i;
2219 struct efi_handler *handler;
2220 efi_handle_t *handles;
2221 size_t len, len_dp;
2222 size_t len_best = 0;
2223 efi_uintn_t no_handles;
2224 u8 *remainder;
2225 efi_status_t ret;
2226
2227 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2228
2229 if (!protocol || !device_path || !*device_path || !device) {
2230 ret = EFI_INVALID_PARAMETER;
2231 goto out;
2232 }
2233
2234 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002235 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002236
2237 /* Get all handles implementing the protocol */
2238 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2239 &no_handles, &handles));
2240 if (ret != EFI_SUCCESS)
2241 goto out;
2242
2243 for (i = 0; i < no_handles; ++i) {
2244 /* Find the device path protocol */
2245 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2246 &handler);
2247 if (ret != EFI_SUCCESS)
2248 continue;
2249 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002250 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002251 /*
2252 * This handle can only be a better fit
2253 * if its device path length is longer than the best fit and
2254 * if its device path length is shorter of equal the searched
2255 * device path.
2256 */
2257 if (len_dp <= len_best || len_dp > len)
2258 continue;
2259 /* Check if dp is a subpath of device_path */
2260 if (memcmp(*device_path, dp, len_dp))
2261 continue;
2262 *device = handles[i];
2263 len_best = len_dp;
2264 }
2265 if (len_best) {
2266 remainder = (u8 *)*device_path + len_best;
2267 *device_path = (struct efi_device_path *)remainder;
2268 ret = EFI_SUCCESS;
2269 } else {
2270 ret = EFI_NOT_FOUND;
2271 }
2272out:
2273 return EFI_EXIT(ret);
2274}
2275
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002276/**
Mario Six8fac2912018-07-10 08:40:17 +02002277 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2278 * interfaces
2279 * @handle: handle on which the protocol interfaces shall be installed
2280 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2281 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002282 *
2283 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002284 *
2285 * See the Unified Extensible Firmware Interface (UEFI) specification for
2286 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002287 *
Mario Six8fac2912018-07-10 08:40:17 +02002288 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002289 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002290static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2291 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002292{
2293 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002294
Alexander Graf404a7c82018-06-18 17:23:05 +02002295 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002296 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002297 void *protocol_interface;
2298 efi_status_t r = EFI_SUCCESS;
2299 int i = 0;
2300
2301 if (!handle)
2302 return EFI_EXIT(EFI_INVALID_PARAMETER);
2303
Alexander Graf404a7c82018-06-18 17:23:05 +02002304 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002305 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002306 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002307 if (!protocol)
2308 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002309 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002310 r = EFI_CALL(efi_install_protocol_interface(
2311 handle, protocol,
2312 EFI_NATIVE_INTERFACE,
2313 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002314 if (r != EFI_SUCCESS)
2315 break;
2316 i++;
2317 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002318 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002319 if (r == EFI_SUCCESS)
2320 return EFI_EXIT(r);
2321
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002322 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002323 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002324 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002325 protocol = efi_va_arg(argptr, efi_guid_t*);
2326 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002327 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002328 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002329 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002330 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002331
2332 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002333}
2334
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002335/**
Mario Six8fac2912018-07-10 08:40:17 +02002336 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2337 * interfaces
2338 * @handle: handle from which the protocol interfaces shall be removed
2339 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2340 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002341 *
2342 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002343 *
Mario Six8fac2912018-07-10 08:40:17 +02002344 * See the Unified Extensible Firmware Interface (UEFI) specification for
2345 * details.
2346 *
2347 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002348 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002349static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002350 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002351{
2352 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002353
Alexander Graf404a7c82018-06-18 17:23:05 +02002354 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002355 const efi_guid_t *protocol;
2356 void *protocol_interface;
2357 efi_status_t r = EFI_SUCCESS;
2358 size_t i = 0;
2359
2360 if (!handle)
2361 return EFI_EXIT(EFI_INVALID_PARAMETER);
2362
Alexander Graf404a7c82018-06-18 17:23:05 +02002363 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002364 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002365 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002366 if (!protocol)
2367 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002368 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002369 r = efi_uninstall_protocol(handle, protocol,
2370 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002371 if (r != EFI_SUCCESS)
2372 break;
2373 i++;
2374 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002375 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002376 if (r == EFI_SUCCESS) {
2377 /* If the last protocol has been removed, delete the handle. */
2378 if (list_empty(&handle->protocols)) {
2379 list_del(&handle->link);
2380 free(handle);
2381 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002382 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002383 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002384
2385 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002386 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002387 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002388 protocol = efi_va_arg(argptr, efi_guid_t*);
2389 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002390 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2391 EFI_NATIVE_INTERFACE,
2392 protocol_interface));
2393 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002394 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002395
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002396 /* In case of an error always return EFI_INVALID_PARAMETER */
2397 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002398}
2399
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002400/**
Mario Six8fac2912018-07-10 08:40:17 +02002401 * efi_calculate_crc32() - calculate cyclic redundancy code
2402 * @data: buffer with data
2403 * @data_size: size of buffer in bytes
2404 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002405 *
2406 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002407 *
2408 * See the Unified Extensible Firmware Interface (UEFI) specification for
2409 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002410 *
Mario Six8fac2912018-07-10 08:40:17 +02002411 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002412 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002413static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2414 efi_uintn_t data_size,
2415 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002416{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002417 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002418 *crc32_p = crc32(0, data, data_size);
2419 return EFI_EXIT(EFI_SUCCESS);
2420}
2421
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002422/**
Mario Six8fac2912018-07-10 08:40:17 +02002423 * efi_copy_mem() - copy memory
2424 * @destination: destination of the copy operation
2425 * @source: source of the copy operation
2426 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002427 *
2428 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002429 *
Mario Six8fac2912018-07-10 08:40:17 +02002430 * See the Unified Extensible Firmware Interface (UEFI) specification for
2431 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002432 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002433static void EFIAPI efi_copy_mem(void *destination, const void *source,
2434 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002435{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002436 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002437 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002438 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002439}
2440
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002441/**
Mario Six8fac2912018-07-10 08:40:17 +02002442 * efi_set_mem() - Fill memory with a byte value.
2443 * @buffer: buffer to fill
2444 * @size: size of buffer in bytes
2445 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002446 *
2447 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002448 *
Mario Six8fac2912018-07-10 08:40:17 +02002449 * See the Unified Extensible Firmware Interface (UEFI) specification for
2450 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002451 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002452static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002453{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002454 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002455 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002456 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002457}
2458
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002459/**
Mario Six8fac2912018-07-10 08:40:17 +02002460 * efi_protocol_open() - open protocol interface on a handle
2461 * @handler: handler of a protocol
2462 * @protocol_interface: interface implementing the protocol
2463 * @agent_handle: handle of the driver
2464 * @controller_handle: handle of the controller
2465 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002466 *
Mario Six8fac2912018-07-10 08:40:17 +02002467 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002468 */
2469static efi_status_t efi_protocol_open(
2470 struct efi_handler *handler,
2471 void **protocol_interface, void *agent_handle,
2472 void *controller_handle, uint32_t attributes)
2473{
2474 struct efi_open_protocol_info_item *item;
2475 struct efi_open_protocol_info_entry *match = NULL;
2476 bool opened_by_driver = false;
2477 bool opened_exclusive = false;
2478
2479 /* If there is no agent, only return the interface */
2480 if (!agent_handle)
2481 goto out;
2482
2483 /* For TEST_PROTOCOL ignore interface attribute */
2484 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2485 *protocol_interface = NULL;
2486
2487 /*
2488 * Check if the protocol is already opened by a driver with the same
2489 * attributes or opened exclusively
2490 */
2491 list_for_each_entry(item, &handler->open_infos, link) {
2492 if (item->info.agent_handle == agent_handle) {
2493 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2494 (item->info.attributes == attributes))
2495 return EFI_ALREADY_STARTED;
2496 }
2497 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2498 opened_exclusive = true;
2499 }
2500
2501 /* Only one controller can open the protocol exclusively */
2502 if (opened_exclusive && attributes &
2503 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2504 return EFI_ACCESS_DENIED;
2505
2506 /* Prepare exclusive opening */
2507 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2508 /* Try to disconnect controllers */
2509 list_for_each_entry(item, &handler->open_infos, link) {
2510 if (item->info.attributes ==
2511 EFI_OPEN_PROTOCOL_BY_DRIVER)
2512 EFI_CALL(efi_disconnect_controller(
2513 item->info.controller_handle,
2514 item->info.agent_handle,
2515 NULL));
2516 }
2517 opened_by_driver = false;
2518 /* Check if all controllers are disconnected */
2519 list_for_each_entry(item, &handler->open_infos, link) {
2520 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2521 opened_by_driver = true;
2522 }
Heinrich Schuchardt8d0b45d2018-09-30 13:40:43 +02002523 /* Only one controller can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002524 if (opened_by_driver)
2525 return EFI_ACCESS_DENIED;
2526 }
2527
2528 /* Find existing entry */
2529 list_for_each_entry(item, &handler->open_infos, link) {
2530 if (item->info.agent_handle == agent_handle &&
2531 item->info.controller_handle == controller_handle)
2532 match = &item->info;
2533 }
2534 /* None found, create one */
2535 if (!match) {
2536 match = efi_create_open_info(handler);
2537 if (!match)
2538 return EFI_OUT_OF_RESOURCES;
2539 }
2540
2541 match->agent_handle = agent_handle;
2542 match->controller_handle = controller_handle;
2543 match->attributes = attributes;
2544 match->open_count++;
2545
2546out:
2547 /* For TEST_PROTOCOL ignore interface attribute. */
2548 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2549 *protocol_interface = handler->protocol_interface;
2550
2551 return EFI_SUCCESS;
2552}
2553
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002554/**
Mario Six8fac2912018-07-10 08:40:17 +02002555 * efi_open_protocol() - open protocol interface on a handle
2556 * @handle: handle on which the protocol shall be opened
2557 * @protocol: GUID of the protocol
2558 * @protocol_interface: interface implementing the protocol
2559 * @agent_handle: handle of the driver
2560 * @controller_handle: handle of the controller
2561 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002562 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002563 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002564 *
Mario Six8fac2912018-07-10 08:40:17 +02002565 * See the Unified Extensible Firmware Interface (UEFI) specification for
2566 * details.
2567 *
2568 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002569 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002570static efi_status_t EFIAPI efi_open_protocol
2571 (efi_handle_t handle, const efi_guid_t *protocol,
2572 void **protocol_interface, efi_handle_t agent_handle,
2573 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002574{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002575 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002576 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002577
Rob Clark238f88c2017-09-13 18:05:41 -04002578 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002579 protocol_interface, agent_handle, controller_handle,
2580 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002581
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002582 if (!handle || !protocol ||
2583 (!protocol_interface && attributes !=
2584 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002585 goto out;
2586 }
2587
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002588 switch (attributes) {
2589 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2590 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2591 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2592 break;
2593 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2594 if (controller_handle == handle)
2595 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002596 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002597 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2598 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002599 /* Check that the controller handle is valid */
2600 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002601 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002602 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002603 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002604 /* Check that the agent handle is valid */
2605 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002606 goto out;
2607 break;
2608 default:
2609 goto out;
2610 }
2611
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002612 r = efi_search_protocol(handle, protocol, &handler);
2613 if (r != EFI_SUCCESS)
2614 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002615
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002616 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2617 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002618out:
2619 return EFI_EXIT(r);
2620}
2621
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002622/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002623 * efi_start_image() - call the entry point of an image
2624 * @image_handle: handle of the image
2625 * @exit_data_size: size of the buffer
2626 * @exit_data: buffer to receive the exit data of the called image
2627 *
2628 * This function implements the StartImage service.
2629 *
2630 * See the Unified Extensible Firmware Interface (UEFI) specification for
2631 * details.
2632 *
2633 * Return: status code
2634 */
2635efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2636 efi_uintn_t *exit_data_size,
2637 u16 **exit_data)
2638{
2639 struct efi_loaded_image_obj *image_obj =
2640 (struct efi_loaded_image_obj *)image_handle;
2641 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002642 void *info;
2643 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002644
2645 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2646
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002647 /* Check parameters */
2648 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2649 &info, NULL, NULL,
2650 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2651 if (ret != EFI_SUCCESS)
2652 return EFI_EXIT(EFI_INVALID_PARAMETER);
2653
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002654 efi_is_direct_boot = false;
2655
2656 /* call the image! */
2657 if (setjmp(&image_obj->exit_jmp)) {
2658 /*
2659 * We called the entry point of the child image with EFI_CALL
2660 * in the lines below. The child image called the Exit() boot
2661 * service efi_exit() which executed the long jump that brought
2662 * us to the current line. This implies that the second half
2663 * of the EFI_CALL macro has not been executed.
2664 */
2665#ifdef CONFIG_ARM
2666 /*
2667 * efi_exit() called efi_restore_gd(). We have to undo this
2668 * otherwise __efi_entry_check() will put the wrong value into
2669 * app_gd.
2670 */
2671 gd = app_gd;
2672#endif
2673 /*
2674 * To get ready to call EFI_EXIT below we have to execute the
2675 * missed out steps of EFI_CALL.
2676 */
2677 assert(__efi_entry_check());
2678 debug("%sEFI: %lu returned by started image\n",
2679 __efi_nesting_dec(),
2680 (unsigned long)((uintptr_t)image_obj->exit_status &
2681 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002682 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002683 return EFI_EXIT(image_obj->exit_status);
2684 }
2685
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002686 current_image = image_handle;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002687 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2688
2689 /*
2690 * Usually UEFI applications call Exit() instead of returning.
2691 * But because the world doesn't consist of ponies and unicorns,
2692 * we're happy to emulate that behavior on behalf of a payload
2693 * that forgot.
2694 */
2695 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2696}
2697
2698/**
2699 * efi_exit() - leave an EFI application or driver
2700 * @image_handle: handle of the application or driver that is exiting
2701 * @exit_status: status code
2702 * @exit_data_size: size of the buffer in bytes
2703 * @exit_data: buffer with data describing an error
2704 *
2705 * This function implements the Exit service.
2706 *
2707 * See the Unified Extensible Firmware Interface (UEFI) specification for
2708 * details.
2709 *
2710 * Return: status code
2711 */
2712static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
2713 efi_status_t exit_status,
2714 efi_uintn_t exit_data_size,
2715 u16 *exit_data)
2716{
2717 /*
2718 * TODO: We should call the unload procedure of the loaded
2719 * image protocol.
2720 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002721 efi_status_t ret;
2722 void *info;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002723 struct efi_loaded_image_obj *image_obj =
2724 (struct efi_loaded_image_obj *)image_handle;
2725
2726 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
2727 exit_data_size, exit_data);
2728
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002729 /* Check parameters */
2730 if (image_handle != current_image)
2731 goto out;
2732 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2733 &info, NULL, NULL,
2734 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2735 if (ret != EFI_SUCCESS)
2736 goto out;
2737
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002738 /* Make sure entry/exit counts for EFI world cross-overs match */
2739 EFI_EXIT(exit_status);
2740
2741 /*
2742 * But longjmp out with the U-Boot gd, not the application's, as
2743 * the other end is a setjmp call inside EFI context.
2744 */
2745 efi_restore_gd();
2746
2747 image_obj->exit_status = exit_status;
2748 longjmp(&image_obj->exit_jmp, 1);
2749
2750 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002751out:
2752 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002753}
2754
2755/**
Mario Six8fac2912018-07-10 08:40:17 +02002756 * efi_handle_protocol() - get interface of a protocol on a handle
2757 * @handle: handle on which the protocol shall be opened
2758 * @protocol: GUID of the protocol
2759 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002760 *
2761 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002762 *
2763 * See the Unified Extensible Firmware Interface (UEFI) specification for
2764 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002765 *
Mario Six8fac2912018-07-10 08:40:17 +02002766 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002767 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002768static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002769 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002770 void **protocol_interface)
2771{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002772 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2773 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002774}
2775
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002776/**
Mario Six8fac2912018-07-10 08:40:17 +02002777 * efi_bind_controller() - bind a single driver to a controller
2778 * @controller_handle: controller handle
2779 * @driver_image_handle: driver handle
2780 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002781 *
Mario Six8fac2912018-07-10 08:40:17 +02002782 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002783 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002784static efi_status_t efi_bind_controller(
2785 efi_handle_t controller_handle,
2786 efi_handle_t driver_image_handle,
2787 struct efi_device_path *remain_device_path)
2788{
2789 struct efi_driver_binding_protocol *binding_protocol;
2790 efi_status_t r;
2791
2792 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2793 &efi_guid_driver_binding_protocol,
2794 (void **)&binding_protocol,
2795 driver_image_handle, NULL,
2796 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2797 if (r != EFI_SUCCESS)
2798 return r;
2799 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2800 controller_handle,
2801 remain_device_path));
2802 if (r == EFI_SUCCESS)
2803 r = EFI_CALL(binding_protocol->start(binding_protocol,
2804 controller_handle,
2805 remain_device_path));
2806 EFI_CALL(efi_close_protocol(driver_image_handle,
2807 &efi_guid_driver_binding_protocol,
2808 driver_image_handle, NULL));
2809 return r;
2810}
2811
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002812/**
Mario Six8fac2912018-07-10 08:40:17 +02002813 * efi_connect_single_controller() - connect a single driver to a controller
2814 * @controller_handle: controller
2815 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002816 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002817 *
Mario Six8fac2912018-07-10 08:40:17 +02002818 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002819 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002820static efi_status_t efi_connect_single_controller(
2821 efi_handle_t controller_handle,
2822 efi_handle_t *driver_image_handle,
2823 struct efi_device_path *remain_device_path)
2824{
2825 efi_handle_t *buffer;
2826 size_t count;
2827 size_t i;
2828 efi_status_t r;
2829 size_t connected = 0;
2830
2831 /* Get buffer with all handles with driver binding protocol */
2832 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2833 &efi_guid_driver_binding_protocol,
2834 NULL, &count, &buffer));
2835 if (r != EFI_SUCCESS)
2836 return r;
2837
2838 /* Context Override */
2839 if (driver_image_handle) {
2840 for (; *driver_image_handle; ++driver_image_handle) {
2841 for (i = 0; i < count; ++i) {
2842 if (buffer[i] == *driver_image_handle) {
2843 buffer[i] = NULL;
2844 r = efi_bind_controller(
2845 controller_handle,
2846 *driver_image_handle,
2847 remain_device_path);
2848 /*
2849 * For drivers that do not support the
2850 * controller or are already connected
2851 * we receive an error code here.
2852 */
2853 if (r == EFI_SUCCESS)
2854 ++connected;
2855 }
2856 }
2857 }
2858 }
2859
2860 /*
2861 * TODO: Some overrides are not yet implemented:
2862 * - Platform Driver Override
2863 * - Driver Family Override Search
2864 * - Bus Specific Driver Override
2865 */
2866
2867 /* Driver Binding Search */
2868 for (i = 0; i < count; ++i) {
2869 if (buffer[i]) {
2870 r = efi_bind_controller(controller_handle,
2871 buffer[i],
2872 remain_device_path);
2873 if (r == EFI_SUCCESS)
2874 ++connected;
2875 }
2876 }
2877
2878 efi_free_pool(buffer);
2879 if (!connected)
2880 return EFI_NOT_FOUND;
2881 return EFI_SUCCESS;
2882}
2883
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002884/**
Mario Six8fac2912018-07-10 08:40:17 +02002885 * efi_connect_controller() - connect a controller to a driver
2886 * @controller_handle: handle of the controller
2887 * @driver_image_handle: handle of the driver
2888 * @remain_device_path: device path of a child controller
2889 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002890 *
2891 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002892 *
2893 * See the Unified Extensible Firmware Interface (UEFI) specification for
2894 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002895 *
2896 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002897 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002898 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2899 *
Mario Six8fac2912018-07-10 08:40:17 +02002900 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002901 */
2902static efi_status_t EFIAPI efi_connect_controller(
2903 efi_handle_t controller_handle,
2904 efi_handle_t *driver_image_handle,
2905 struct efi_device_path *remain_device_path,
2906 bool recursive)
2907{
2908 efi_status_t r;
2909 efi_status_t ret = EFI_NOT_FOUND;
2910 struct efi_object *efiobj;
2911
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01002912 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002913 remain_device_path, recursive);
2914
2915 efiobj = efi_search_obj(controller_handle);
2916 if (!efiobj) {
2917 ret = EFI_INVALID_PARAMETER;
2918 goto out;
2919 }
2920
2921 r = efi_connect_single_controller(controller_handle,
2922 driver_image_handle,
2923 remain_device_path);
2924 if (r == EFI_SUCCESS)
2925 ret = EFI_SUCCESS;
2926 if (recursive) {
2927 struct efi_handler *handler;
2928 struct efi_open_protocol_info_item *item;
2929
2930 list_for_each_entry(handler, &efiobj->protocols, link) {
2931 list_for_each_entry(item, &handler->open_infos, link) {
2932 if (item->info.attributes &
2933 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2934 r = EFI_CALL(efi_connect_controller(
2935 item->info.controller_handle,
2936 driver_image_handle,
2937 remain_device_path,
2938 recursive));
2939 if (r == EFI_SUCCESS)
2940 ret = EFI_SUCCESS;
2941 }
2942 }
2943 }
2944 }
2945 /* Check for child controller specified by end node */
2946 if (ret != EFI_SUCCESS && remain_device_path &&
2947 remain_device_path->type == DEVICE_PATH_TYPE_END)
2948 ret = EFI_SUCCESS;
2949out:
2950 return EFI_EXIT(ret);
2951}
2952
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002953/**
Mario Six8fac2912018-07-10 08:40:17 +02002954 * efi_reinstall_protocol_interface() - reinstall protocol interface
2955 * @handle: handle on which the protocol shall be reinstalled
2956 * @protocol: GUID of the protocol to be installed
2957 * @old_interface: interface to be removed
2958 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002959 *
2960 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002961 *
2962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2963 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002964 *
2965 * The old interface is uninstalled. The new interface is installed.
2966 * Drivers are connected.
2967 *
Mario Six8fac2912018-07-10 08:40:17 +02002968 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002969 */
2970static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2971 efi_handle_t handle, const efi_guid_t *protocol,
2972 void *old_interface, void *new_interface)
2973{
2974 efi_status_t ret;
2975
2976 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2977 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002978
2979 /* Uninstall protocol but do not delete handle */
2980 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002981 if (ret != EFI_SUCCESS)
2982 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002983
2984 /* Install the new protocol */
2985 ret = efi_add_protocol(handle, protocol, new_interface);
2986 /*
2987 * The UEFI spec does not specify what should happen to the handle
2988 * if in case of an error no protocol interface remains on the handle.
2989 * So let's do nothing here.
2990 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002991 if (ret != EFI_SUCCESS)
2992 goto out;
2993 /*
2994 * The returned status code has to be ignored.
2995 * Do not create an error if no suitable driver for the handle exists.
2996 */
2997 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2998out:
2999 return EFI_EXIT(ret);
3000}
3001
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003002/**
Mario Six8fac2912018-07-10 08:40:17 +02003003 * efi_get_child_controllers() - get all child controllers associated to a driver
3004 * @efiobj: handle of the controller
3005 * @driver_handle: handle of the driver
3006 * @number_of_children: number of child controllers
3007 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003008 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003009 * The allocated buffer has to be freed with free().
3010 *
Mario Six8fac2912018-07-10 08:40:17 +02003011 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003012 */
3013static efi_status_t efi_get_child_controllers(
3014 struct efi_object *efiobj,
3015 efi_handle_t driver_handle,
3016 efi_uintn_t *number_of_children,
3017 efi_handle_t **child_handle_buffer)
3018{
3019 struct efi_handler *handler;
3020 struct efi_open_protocol_info_item *item;
3021 efi_uintn_t count = 0, i;
3022 bool duplicate;
3023
3024 /* Count all child controller associations */
3025 list_for_each_entry(handler, &efiobj->protocols, link) {
3026 list_for_each_entry(item, &handler->open_infos, link) {
3027 if (item->info.agent_handle == driver_handle &&
3028 item->info.attributes &
3029 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3030 ++count;
3031 }
3032 }
3033 /*
3034 * Create buffer. In case of duplicate child controller assignments
3035 * the buffer will be too large. But that does not harm.
3036 */
3037 *number_of_children = 0;
3038 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3039 if (!*child_handle_buffer)
3040 return EFI_OUT_OF_RESOURCES;
3041 /* Copy unique child handles */
3042 list_for_each_entry(handler, &efiobj->protocols, link) {
3043 list_for_each_entry(item, &handler->open_infos, link) {
3044 if (item->info.agent_handle == driver_handle &&
3045 item->info.attributes &
3046 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3047 /* Check this is a new child controller */
3048 duplicate = false;
3049 for (i = 0; i < *number_of_children; ++i) {
3050 if ((*child_handle_buffer)[i] ==
3051 item->info.controller_handle)
3052 duplicate = true;
3053 }
3054 /* Copy handle to buffer */
3055 if (!duplicate) {
3056 i = (*number_of_children)++;
3057 (*child_handle_buffer)[i] =
3058 item->info.controller_handle;
3059 }
3060 }
3061 }
3062 }
3063 return EFI_SUCCESS;
3064}
3065
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003066/**
Mario Six8fac2912018-07-10 08:40:17 +02003067 * efi_disconnect_controller() - disconnect a controller from a driver
3068 * @controller_handle: handle of the controller
3069 * @driver_image_handle: handle of the driver
3070 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003071 *
3072 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003073 *
3074 * See the Unified Extensible Firmware Interface (UEFI) specification for
3075 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003076 *
Mario Six8fac2912018-07-10 08:40:17 +02003077 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003078 */
3079static efi_status_t EFIAPI efi_disconnect_controller(
3080 efi_handle_t controller_handle,
3081 efi_handle_t driver_image_handle,
3082 efi_handle_t child_handle)
3083{
3084 struct efi_driver_binding_protocol *binding_protocol;
3085 efi_handle_t *child_handle_buffer = NULL;
3086 size_t number_of_children = 0;
3087 efi_status_t r;
3088 size_t stop_count = 0;
3089 struct efi_object *efiobj;
3090
3091 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3092 child_handle);
3093
3094 efiobj = efi_search_obj(controller_handle);
3095 if (!efiobj) {
3096 r = EFI_INVALID_PARAMETER;
3097 goto out;
3098 }
3099
3100 if (child_handle && !efi_search_obj(child_handle)) {
3101 r = EFI_INVALID_PARAMETER;
3102 goto out;
3103 }
3104
3105 /* If no driver handle is supplied, disconnect all drivers */
3106 if (!driver_image_handle) {
3107 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3108 goto out;
3109 }
3110
3111 /* Create list of child handles */
3112 if (child_handle) {
3113 number_of_children = 1;
3114 child_handle_buffer = &child_handle;
3115 } else {
3116 efi_get_child_controllers(efiobj,
3117 driver_image_handle,
3118 &number_of_children,
3119 &child_handle_buffer);
3120 }
3121
3122 /* Get the driver binding protocol */
3123 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3124 &efi_guid_driver_binding_protocol,
3125 (void **)&binding_protocol,
3126 driver_image_handle, NULL,
3127 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3128 if (r != EFI_SUCCESS)
3129 goto out;
3130 /* Remove the children */
3131 if (number_of_children) {
3132 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3133 controller_handle,
3134 number_of_children,
3135 child_handle_buffer));
3136 if (r == EFI_SUCCESS)
3137 ++stop_count;
3138 }
3139 /* Remove the driver */
3140 if (!child_handle)
3141 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3142 controller_handle,
3143 0, NULL));
3144 if (r == EFI_SUCCESS)
3145 ++stop_count;
3146 EFI_CALL(efi_close_protocol(driver_image_handle,
3147 &efi_guid_driver_binding_protocol,
3148 driver_image_handle, NULL));
3149
3150 if (stop_count)
3151 r = EFI_SUCCESS;
3152 else
3153 r = EFI_NOT_FOUND;
3154out:
3155 if (!child_handle)
3156 free(child_handle_buffer);
3157 return EFI_EXIT(r);
3158}
3159
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003160static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003161 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003162 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3163 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003164 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003165 },
3166 .raise_tpl = efi_raise_tpl,
3167 .restore_tpl = efi_restore_tpl,
3168 .allocate_pages = efi_allocate_pages_ext,
3169 .free_pages = efi_free_pages_ext,
3170 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003171 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003172 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003173 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003174 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003175 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003176 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003177 .close_event = efi_close_event,
3178 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003179 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003180 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003181 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003182 .handle_protocol = efi_handle_protocol,
3183 .reserved = NULL,
3184 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003185 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003186 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003187 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003188 .load_image = efi_load_image,
3189 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003190 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003191 .unload_image = efi_unload_image,
3192 .exit_boot_services = efi_exit_boot_services,
3193 .get_next_monotonic_count = efi_get_next_monotonic_count,
3194 .stall = efi_stall,
3195 .set_watchdog_timer = efi_set_watchdog_timer,
3196 .connect_controller = efi_connect_controller,
3197 .disconnect_controller = efi_disconnect_controller,
3198 .open_protocol = efi_open_protocol,
3199 .close_protocol = efi_close_protocol,
3200 .open_protocol_information = efi_open_protocol_information,
3201 .protocols_per_handle = efi_protocols_per_handle,
3202 .locate_handle_buffer = efi_locate_handle_buffer,
3203 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003204 .install_multiple_protocol_interfaces =
3205 efi_install_multiple_protocol_interfaces,
3206 .uninstall_multiple_protocol_interfaces =
3207 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003208 .calculate_crc32 = efi_calculate_crc32,
3209 .copy_mem = efi_copy_mem,
3210 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003211 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003212};
3213
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003214static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003215
Alexander Graf393dd912016-10-14 13:45:30 +02003216struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003217 .hdr = {
3218 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003219 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003220 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003221 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003222 .fw_vendor = firmware_vendor,
3223 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003224 .con_in = (void *)&efi_con_in,
3225 .con_out = (void *)&efi_con_out,
3226 .std_err = (void *)&efi_con_out,
3227 .runtime = (void *)&efi_runtime_services,
3228 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003229 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003230 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003231};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003232
3233/**
3234 * efi_initialize_system_table() - Initialize system table
3235 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003236 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003237 */
3238efi_status_t efi_initialize_system_table(void)
3239{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003240 efi_status_t ret;
3241
3242 /* Allocate configuration table array */
3243 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3244 EFI_MAX_CONFIGURATION_TABLES *
3245 sizeof(struct efi_configuration_table),
3246 (void **)&systab.tables);
3247
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003248 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003249 efi_update_table_header_crc32(&systab.hdr);
3250 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3251 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003252
3253 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003254}