blob: 74aa5f9b7ca24b334a86bfc0892adb4df533675b [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020016#include <pe.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010017#include <watchdog.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020021/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010022static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023
Alexander Grafc15d9212016-03-04 01:09:59 +010024/* This list contains all the EFI objects our payload has access to */
25LIST_HEAD(efi_obj_list);
26
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010027/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010028LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020030/* List of all events registered by RegisterProtocolNotify() */
31LIST_HEAD(efi_register_notify_events);
32
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010033/* Handle of the currently executing image */
34static efi_handle_t current_image;
35
Alexander Graffa5246b2018-11-15 21:23:47 +010036/*
37 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
38 * we need to do trickery with caches. Since we don't want to break the EFI
39 * aware boot path, only apply hacks when loading exiting directly (breaking
40 * direct Linux EFI booting along the way - oh well).
41 */
42static bool efi_is_direct_boot = true;
43
Simon Glasscdfe6962016-09-25 15:27:35 -060044#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010045/*
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
50 */
51static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060052#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010053
Heinrich Schuchardt72506232019-02-09 14:10:39 +010054/* 1 if inside U-Boot code, 0 if inside EFI payload code */
55static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040056static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010057/* GUID of the device tree table */
58const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010059/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040062
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010063/* event group ExitBootServices() invoked */
64const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66/* event group SetVirtualAddressMap() invoked */
67const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69/* event group memory map changed */
70const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72/* event group boot manager about to boot */
73const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75/* event group ResetSystem() invoked (before ExitBootServices) */
76const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
78
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010079static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010083
Rob Clark86789d52017-07-27 08:04:18 -040084/* Called on every callback entry */
85int __efi_entry_check(void)
86{
87 int ret = entry_count++ == 0;
88#ifdef CONFIG_ARM
89 assert(efi_gd);
90 app_gd = gd;
91 gd = efi_gd;
92#endif
93 return ret;
94}
95
96/* Called on every callback exit */
97int __efi_exit_check(void)
98{
99 int ret = --entry_count == 0;
100#ifdef CONFIG_ARM
101 gd = app_gd;
102#endif
103 return ret;
104}
105
Alexander Grafc15d9212016-03-04 01:09:59 +0100106/* Called from do_bootefi_exec() */
107void efi_save_gd(void)
108{
Simon Glasscdfe6962016-09-25 15:27:35 -0600109#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100110 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600111#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100112}
113
Rob Clark86789d52017-07-27 08:04:18 -0400114/*
Mario Six8fac2912018-07-10 08:40:17 +0200115 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400118 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100119void efi_restore_gd(void)
120{
Simon Glasscdfe6962016-09-25 15:27:35 -0600121#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100122 /* Only restore if we're already in EFI context */
123 if (!efi_gd)
124 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600126#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100127}
128
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200129/**
Mario Six8fac2912018-07-10 08:40:17 +0200130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100132 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200133 * A maximum of ten indent levels is supported. Higher indent levels will be
134 * truncated.
135 *
Mario Six8fac2912018-07-10 08:40:17 +0200136 * Return: A string for indenting with two spaces per level is
137 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400138 */
139static const char *indent_string(int level)
140{
141 const char *indent = " ";
142 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100143
Rob Clarke7896c32017-07-27 08:04:19 -0400144 level = min(max, level * 2);
145 return &indent[max - level];
146}
147
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200148const char *__efi_nesting(void)
149{
150 return indent_string(nesting_level);
151}
152
Rob Clarke7896c32017-07-27 08:04:19 -0400153const char *__efi_nesting_inc(void)
154{
155 return indent_string(nesting_level++);
156}
157
158const char *__efi_nesting_dec(void)
159{
160 return indent_string(--nesting_level);
161}
162
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200163/**
Mario Six8fac2912018-07-10 08:40:17 +0200164 * efi_queue_event() - queue an EFI event
165 * @event: event to signal
166 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200167 *
168 * This function queues the notification function of the event for future
169 * execution.
170 *
Mario Six8fac2912018-07-10 08:40:17 +0200171 * The notification function is called if the task priority level of the event
172 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200173 *
174 * For the SignalEvent service see efi_signal_event_ext.
175 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200176 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100177static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200178{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200179 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200180 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200181 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100182 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200183 return;
Heinrich Schuchardtad559972019-05-11 21:44:59 +0200184 event->is_queued = false;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200185 EFI_CALL_VOID(event->notify_function(event,
186 event->notify_context));
Heinrich Schuchardtad559972019-05-11 21:44:59 +0200187 } else {
188 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200189 }
190}
191
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200192/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200193 * is_valid_tpl() - check if the task priority level is valid
194 *
195 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200196 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200197 */
198efi_status_t is_valid_tpl(efi_uintn_t tpl)
199{
200 switch (tpl) {
201 case TPL_APPLICATION:
202 case TPL_CALLBACK:
203 case TPL_NOTIFY:
204 case TPL_HIGH_LEVEL:
205 return EFI_SUCCESS;
206 default:
207 return EFI_INVALID_PARAMETER;
208 }
209}
210
211/**
Mario Six8fac2912018-07-10 08:40:17 +0200212 * efi_signal_event() - signal an EFI event
213 * @event: event to signal
214 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100215 *
Mario Six8fac2912018-07-10 08:40:17 +0200216 * This function signals an event. If the event belongs to an event group all
217 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100218 * their notification function is queued.
219 *
220 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100221 */
222void efi_signal_event(struct efi_event *event, bool check_tpl)
223{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200224 if (event->is_signaled)
225 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100226 if (event->group) {
227 struct efi_event *evt;
228
229 /*
230 * The signaled state has to set before executing any
231 * notification function
232 */
233 list_for_each_entry(evt, &efi_events, link) {
234 if (!evt->group || guidcmp(evt->group, event->group))
235 continue;
236 if (evt->is_signaled)
237 continue;
238 evt->is_signaled = true;
239 if (evt->type & EVT_NOTIFY_SIGNAL &&
240 evt->notify_function)
241 evt->is_queued = true;
242 }
243 list_for_each_entry(evt, &efi_events, link) {
244 if (!evt->group || guidcmp(evt->group, event->group))
245 continue;
246 if (evt->is_queued)
247 efi_queue_event(evt, check_tpl);
248 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200249 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100250 event->is_signaled = true;
251 if (event->type & EVT_NOTIFY_SIGNAL)
252 efi_queue_event(event, check_tpl);
253 }
254}
255
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200256/**
Mario Six8fac2912018-07-10 08:40:17 +0200257 * efi_raise_tpl() - raise the task priority level
258 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200259 *
260 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200261 *
262 * See the Unified Extensible Firmware Interface (UEFI) specification for
263 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200264 *
Mario Six8fac2912018-07-10 08:40:17 +0200265 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200266 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100267static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100268{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100269 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200270
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200271 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200272
273 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200274 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200275 efi_tpl = new_tpl;
276 if (efi_tpl > TPL_HIGH_LEVEL)
277 efi_tpl = TPL_HIGH_LEVEL;
278
279 EFI_EXIT(EFI_SUCCESS);
280 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100281}
282
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200283/**
Mario Six8fac2912018-07-10 08:40:17 +0200284 * efi_restore_tpl() - lower the task priority level
285 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200286 *
287 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200288 *
Mario Six8fac2912018-07-10 08:40:17 +0200289 * See the Unified Extensible Firmware Interface (UEFI) specification for
290 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200291 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100292static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100293{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200294 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200295
296 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200297 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200298 efi_tpl = old_tpl;
299 if (efi_tpl > TPL_HIGH_LEVEL)
300 efi_tpl = TPL_HIGH_LEVEL;
301
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100302 /*
303 * Lowering the TPL may have made queued events eligible for execution.
304 */
305 efi_timer_check();
306
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200307 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100308}
309
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200310/**
Mario Six8fac2912018-07-10 08:40:17 +0200311 * efi_allocate_pages_ext() - allocate memory pages
312 * @type: type of allocation to be performed
313 * @memory_type: usage type of the allocated memory
314 * @pages: number of pages to be allocated
315 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200316 *
317 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200318 *
Mario Six8fac2912018-07-10 08:40:17 +0200319 * See the Unified Extensible Firmware Interface (UEFI) specification for
320 * details.
321 *
322 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200323 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900324static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100325 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900326 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100327{
328 efi_status_t r;
329
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100330 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100331 r = efi_allocate_pages(type, memory_type, pages, memory);
332 return EFI_EXIT(r);
333}
334
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200335/**
Mario Six8fac2912018-07-10 08:40:17 +0200336 * efi_free_pages_ext() - Free memory pages.
337 * @memory: start of the memory area to be freed
338 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200339 *
340 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200341 *
342 * See the Unified Extensible Firmware Interface (UEFI) specification for
343 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200344 *
Mario Six8fac2912018-07-10 08:40:17 +0200345 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200346 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900347static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100348 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100349{
350 efi_status_t r;
351
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900352 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100353 r = efi_free_pages(memory, pages);
354 return EFI_EXIT(r);
355}
356
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200357/**
Mario Six8fac2912018-07-10 08:40:17 +0200358 * efi_get_memory_map_ext() - get map describing memory usage
359 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
360 * on exit the size of the copied memory map
361 * @memory_map: buffer to which the memory map is written
362 * @map_key: key for the memory map
363 * @descriptor_size: size of an individual memory descriptor
364 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200365 *
366 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200367 *
Mario Six8fac2912018-07-10 08:40:17 +0200368 * See the Unified Extensible Firmware Interface (UEFI) specification for
369 * details.
370 *
371 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200372 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900373static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100374 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900375 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100376 efi_uintn_t *map_key,
377 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900378 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100379{
380 efi_status_t r;
381
382 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
383 map_key, descriptor_size, descriptor_version);
384 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
385 descriptor_size, descriptor_version);
386 return EFI_EXIT(r);
387}
388
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200389/**
Mario Six8fac2912018-07-10 08:40:17 +0200390 * efi_allocate_pool_ext() - allocate memory from pool
391 * @pool_type: type of the pool from which memory is to be allocated
392 * @size: number of bytes to be allocated
393 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200394 *
395 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200396 *
397 * See the Unified Extensible Firmware Interface (UEFI) specification for
398 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200399 *
Mario Six8fac2912018-07-10 08:40:17 +0200400 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200401 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200402static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100403 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200404 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100406 efi_status_t r;
407
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100408 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200409 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100410 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100411}
412
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200413/**
Mario Six8fac2912018-07-10 08:40:17 +0200414 * efi_free_pool_ext() - free memory from pool
415 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200416 *
417 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200418 *
Mario Six8fac2912018-07-10 08:40:17 +0200419 * See the Unified Extensible Firmware Interface (UEFI) specification for
420 * details.
421 *
422 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200424static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100425{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100426 efi_status_t r;
427
428 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200429 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100430 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100431}
432
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200433/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200434 * efi_add_handle() - add a new handle to the object list
435 *
436 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100437 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200438 * The protocols list is initialized. The handle is added to the list of known
439 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100440 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200441void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100442{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200443 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100444 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200445 INIT_LIST_HEAD(&handle->protocols);
446 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100447}
448
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200449/**
Mario Six8fac2912018-07-10 08:40:17 +0200450 * efi_create_handle() - create handle
451 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200452 *
Mario Six8fac2912018-07-10 08:40:17 +0200453 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200454 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100455efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200456{
457 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200458
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200459 obj = calloc(1, sizeof(struct efi_object));
460 if (!obj)
461 return EFI_OUT_OF_RESOURCES;
462
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100463 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200464 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200465
466 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200467}
468
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200469/**
Mario Six8fac2912018-07-10 08:40:17 +0200470 * efi_search_protocol() - find a protocol on a handle.
471 * @handle: handle
472 * @protocol_guid: GUID of the protocol
473 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100474 *
Mario Six8fac2912018-07-10 08:40:17 +0200475 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100476 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100477efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100478 const efi_guid_t *protocol_guid,
479 struct efi_handler **handler)
480{
481 struct efi_object *efiobj;
482 struct list_head *lhandle;
483
484 if (!handle || !protocol_guid)
485 return EFI_INVALID_PARAMETER;
486 efiobj = efi_search_obj(handle);
487 if (!efiobj)
488 return EFI_INVALID_PARAMETER;
489 list_for_each(lhandle, &efiobj->protocols) {
490 struct efi_handler *protocol;
491
492 protocol = list_entry(lhandle, struct efi_handler, link);
493 if (!guidcmp(protocol->guid, protocol_guid)) {
494 if (handler)
495 *handler = protocol;
496 return EFI_SUCCESS;
497 }
498 }
499 return EFI_NOT_FOUND;
500}
501
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200502/**
Mario Six8fac2912018-07-10 08:40:17 +0200503 * efi_remove_protocol() - delete protocol from a handle
504 * @handle: handle from which the protocol shall be deleted
505 * @protocol: GUID of the protocol to be deleted
506 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100507 *
Mario Six8fac2912018-07-10 08:40:17 +0200508 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100509 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100510efi_status_t efi_remove_protocol(const efi_handle_t handle,
511 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100512 void *protocol_interface)
513{
514 struct efi_handler *handler;
515 efi_status_t ret;
516
517 ret = efi_search_protocol(handle, protocol, &handler);
518 if (ret != EFI_SUCCESS)
519 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200520 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200521 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100522 list_del(&handler->link);
523 free(handler);
524 return EFI_SUCCESS;
525}
526
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200527/**
Mario Six8fac2912018-07-10 08:40:17 +0200528 * efi_remove_all_protocols() - delete all protocols from a handle
529 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100530 *
Mario Six8fac2912018-07-10 08:40:17 +0200531 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100532 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100533efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534{
535 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100536 struct efi_handler *protocol;
537 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100538
539 efiobj = efi_search_obj(handle);
540 if (!efiobj)
541 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100542 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100543 efi_status_t ret;
544
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100545 ret = efi_remove_protocol(handle, protocol->guid,
546 protocol->protocol_interface);
547 if (ret != EFI_SUCCESS)
548 return ret;
549 }
550 return EFI_SUCCESS;
551}
552
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200553/**
Mario Six8fac2912018-07-10 08:40:17 +0200554 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100555 *
Mario Six8fac2912018-07-10 08:40:17 +0200556 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100557 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200558void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100559{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200560 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100561 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200562 efi_remove_all_protocols(handle);
563 list_del(&handle->link);
564 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100565}
566
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200567/**
Mario Six8fac2912018-07-10 08:40:17 +0200568 * efi_is_event() - check if a pointer is a valid event
569 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100570 *
Mario Six8fac2912018-07-10 08:40:17 +0200571 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100572 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100573static efi_status_t efi_is_event(const struct efi_event *event)
574{
575 const struct efi_event *evt;
576
577 if (!event)
578 return EFI_INVALID_PARAMETER;
579 list_for_each_entry(evt, &efi_events, link) {
580 if (evt == event)
581 return EFI_SUCCESS;
582 }
583 return EFI_INVALID_PARAMETER;
584}
Alexander Grafc15d9212016-03-04 01:09:59 +0100585
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200586/**
Mario Six8fac2912018-07-10 08:40:17 +0200587 * efi_create_event() - create an event
588 * @type: type of the event to create
589 * @notify_tpl: task priority level of the event
590 * @notify_function: notification function of the event
591 * @notify_context: pointer passed to the notification function
592 * @group: event group
593 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200594 *
595 * This function is used inside U-Boot code to create an event.
596 *
597 * For the API function implementing the CreateEvent service see
598 * efi_create_event_ext.
599 *
Mario Six8fac2912018-07-10 08:40:17 +0200600 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200601 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100602efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200603 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200604 struct efi_event *event,
605 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100606 void *notify_context, efi_guid_t *group,
607 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100608{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100609 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200610
Jonathan Gray7758b212017-03-12 19:26:07 +1100611 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200612 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100613
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200614 switch (type) {
615 case 0:
616 case EVT_TIMER:
617 case EVT_NOTIFY_SIGNAL:
618 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
619 case EVT_NOTIFY_WAIT:
620 case EVT_TIMER | EVT_NOTIFY_WAIT:
621 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
622 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
623 break;
624 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200625 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200626 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100627
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900628 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200629 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200630 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100631
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100632 evt = calloc(1, sizeof(struct efi_event));
633 if (!evt)
634 return EFI_OUT_OF_RESOURCES;
635 evt->type = type;
636 evt->notify_tpl = notify_tpl;
637 evt->notify_function = notify_function;
638 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100639 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200640 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100641 evt->trigger_next = -1ULL;
642 evt->is_queued = false;
643 evt->is_signaled = false;
644 list_add_tail(&evt->link, &efi_events);
645 *event = evt;
646 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100647}
648
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200649/*
Mario Six8fac2912018-07-10 08:40:17 +0200650 * efi_create_event_ex() - create an event in a group
651 * @type: type of the event to create
652 * @notify_tpl: task priority level of the event
653 * @notify_function: notification function of the event
654 * @notify_context: pointer passed to the notification function
655 * @event: created event
656 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100657 *
658 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100659 *
Mario Six8fac2912018-07-10 08:40:17 +0200660 * See the Unified Extensible Firmware Interface (UEFI) specification for
661 * details.
662 *
663 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100664 */
665efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
666 void (EFIAPI *notify_function) (
667 struct efi_event *event,
668 void *context),
669 void *notify_context,
670 efi_guid_t *event_group,
671 struct efi_event **event)
672{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200673 efi_status_t ret;
674
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100675 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
676 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200677
678 /*
679 * The allowable input parameters are the same as in CreateEvent()
680 * except for the following two disallowed event types.
681 */
682 switch (type) {
683 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
684 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
685 ret = EFI_INVALID_PARAMETER;
686 goto out;
687 }
688
689 ret = efi_create_event(type, notify_tpl, notify_function,
690 notify_context, event_group, event);
691out:
692 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100693}
694
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200695/**
Mario Six8fac2912018-07-10 08:40:17 +0200696 * efi_create_event_ext() - create an event
697 * @type: type of the event to create
698 * @notify_tpl: task priority level of the event
699 * @notify_function: notification function of the event
700 * @notify_context: pointer passed to the notification function
701 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200702 *
703 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200704 *
705 * See the Unified Extensible Firmware Interface (UEFI) specification for
706 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200707 *
Mario Six8fac2912018-07-10 08:40:17 +0200708 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200709 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200710static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100711 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200712 void (EFIAPI *notify_function) (
713 struct efi_event *event,
714 void *context),
715 void *notify_context, struct efi_event **event)
716{
717 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
718 notify_context);
719 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100720 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200721}
722
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200723/**
Mario Six8fac2912018-07-10 08:40:17 +0200724 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200725 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200726 * Check if a timer event has occurred or a queued notification function should
727 * be called.
728 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100729 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200730 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100731 */
732void efi_timer_check(void)
733{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100734 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100735 u64 now = timer_get_us();
736
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100737 list_for_each_entry(evt, &efi_events, link) {
738 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100739 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100740 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200741 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100742 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200743 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100744 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200745 break;
746 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100747 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200748 break;
749 default:
750 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200751 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100752 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100753 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100754 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100755 WATCHDOG_RESET();
756}
757
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200758/**
Mario Six8fac2912018-07-10 08:40:17 +0200759 * efi_set_timer() - set the trigger time for a timer event or stop the event
760 * @event: event for which the timer is set
761 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200762 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200763 *
764 * This is the function for internal usage in U-Boot. For the API function
765 * implementing the SetTimer service see efi_set_timer_ext.
766 *
Mario Six8fac2912018-07-10 08:40:17 +0200767 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200768 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200769efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200770 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100771{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100772 /* Check that the event is valid */
773 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
774 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100775
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200776 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200777 * The parameter defines a multiple of 100 ns.
778 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200779 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200780 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100781
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100782 switch (type) {
783 case EFI_TIMER_STOP:
784 event->trigger_next = -1ULL;
785 break;
786 case EFI_TIMER_PERIODIC:
787 case EFI_TIMER_RELATIVE:
788 event->trigger_next = timer_get_us() + trigger_time;
789 break;
790 default:
791 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100792 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100793 event->trigger_type = type;
794 event->trigger_time = trigger_time;
795 event->is_signaled = false;
796 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100797}
798
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200799/**
Mario Six8fac2912018-07-10 08:40:17 +0200800 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
801 * event
802 * @event: event for which the timer is set
803 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200804 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200805 *
806 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200807 *
Mario Six8fac2912018-07-10 08:40:17 +0200808 * See the Unified Extensible Firmware Interface (UEFI) specification for
809 * details.
810 *
811 *
812 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200813 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200814static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
815 enum efi_timer_delay type,
816 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200817{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900818 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200819 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
820}
821
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200822/**
Mario Six8fac2912018-07-10 08:40:17 +0200823 * efi_wait_for_event() - wait for events to be signaled
824 * @num_events: number of events to be waited for
825 * @event: events to be waited for
826 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200827 *
828 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200829 *
Mario Six8fac2912018-07-10 08:40:17 +0200830 * See the Unified Extensible Firmware Interface (UEFI) specification for
831 * details.
832 *
833 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200834 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100835static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200836 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100837 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100838{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100839 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100840
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100841 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100842
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200843 /* Check parameters */
844 if (!num_events || !event)
845 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200846 /* Check TPL */
847 if (efi_tpl != TPL_APPLICATION)
848 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200849 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100850 if (efi_is_event(event[i]) != EFI_SUCCESS)
851 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200852 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
853 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200854 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100855 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200856 }
857
858 /* Wait for signal */
859 for (;;) {
860 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200861 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200862 goto out;
863 }
864 /* Allow events to occur. */
865 efi_timer_check();
866 }
867
868out:
869 /*
870 * Reset the signal which is passed to the caller to allow periodic
871 * events to occur.
872 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200873 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200874 if (index)
875 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100876
877 return EFI_EXIT(EFI_SUCCESS);
878}
879
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200880/**
Mario Six8fac2912018-07-10 08:40:17 +0200881 * efi_signal_event_ext() - signal an EFI event
882 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200883 *
884 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200885 *
886 * See the Unified Extensible Firmware Interface (UEFI) specification for
887 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200888 *
889 * This functions sets the signaled state of the event and queues the
890 * notification function for execution.
891 *
Mario Six8fac2912018-07-10 08:40:17 +0200892 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200893 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200894static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100895{
896 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100897 if (efi_is_event(event) != EFI_SUCCESS)
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100899 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100900 return EFI_EXIT(EFI_SUCCESS);
901}
902
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200903/**
Mario Six8fac2912018-07-10 08:40:17 +0200904 * efi_close_event() - close an EFI event
905 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200906 *
907 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200908 *
Mario Six8fac2912018-07-10 08:40:17 +0200909 * See the Unified Extensible Firmware Interface (UEFI) specification for
910 * details.
911 *
912 * 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_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100915{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200916 struct efi_register_notify_event *item, *next;
917
Alexander Grafc15d9212016-03-04 01:09:59 +0100918 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100919 if (efi_is_event(event) != EFI_SUCCESS)
920 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200921
922 /* Remove protocol notify registrations for the event */
923 list_for_each_entry_safe(item, next, &efi_register_notify_events,
924 link) {
925 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200926 struct efi_protocol_notification *hitem, *hnext;
927
928 /* Remove signaled handles */
929 list_for_each_entry_safe(hitem, hnext, &item->handles,
930 link) {
931 list_del(&hitem->link);
932 free(hitem);
933 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200934 list_del(&item->link);
935 free(item);
936 }
937 }
938
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100939 list_del(&event->link);
940 free(event);
941 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100942}
943
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200944/**
Mario Six8fac2912018-07-10 08:40:17 +0200945 * efi_check_event() - check if an event is signaled
946 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200947 *
948 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200949 *
950 * See the Unified Extensible Firmware Interface (UEFI) specification for
951 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200952 *
Mario Six8fac2912018-07-10 08:40:17 +0200953 * If an event is not signaled yet, the notification function is queued. The
954 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200955 *
Mario Six8fac2912018-07-10 08:40:17 +0200956 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200957 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200958static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100959{
960 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200961 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100962 if (efi_is_event(event) != EFI_SUCCESS ||
963 event->type & EVT_NOTIFY_SIGNAL)
964 return EFI_EXIT(EFI_INVALID_PARAMETER);
965 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100966 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100967 if (event->is_signaled) {
968 event->is_signaled = false;
969 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200970 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100971 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100972}
973
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200974/**
Mario Six8fac2912018-07-10 08:40:17 +0200975 * efi_search_obj() - find the internal EFI object for a handle
976 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200977 *
Mario Six8fac2912018-07-10 08:40:17 +0200978 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200979 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100980struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200981{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100982 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200983
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +0200984 if (!handle)
985 return NULL;
986
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100987 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200988 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200989 return efiobj;
990 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200991 return NULL;
992}
993
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200994/**
Mario Six8fac2912018-07-10 08:40:17 +0200995 * efi_open_protocol_info_entry() - create open protocol info entry and add it
996 * to a protocol
997 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100998 *
Mario Six8fac2912018-07-10 08:40:17 +0200999 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001000 */
1001static struct efi_open_protocol_info_entry *efi_create_open_info(
1002 struct efi_handler *handler)
1003{
1004 struct efi_open_protocol_info_item *item;
1005
1006 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1007 if (!item)
1008 return NULL;
1009 /* Append the item to the open protocol info list. */
1010 list_add_tail(&item->link, &handler->open_infos);
1011
1012 return &item->info;
1013}
1014
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001015/**
Mario Six8fac2912018-07-10 08:40:17 +02001016 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1017 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001018 *
Mario Six8fac2912018-07-10 08:40:17 +02001019 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001020 */
1021static efi_status_t efi_delete_open_info(
1022 struct efi_open_protocol_info_item *item)
1023{
1024 list_del(&item->link);
1025 free(item);
1026 return EFI_SUCCESS;
1027}
1028
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001029/**
Mario Six8fac2912018-07-10 08:40:17 +02001030 * efi_add_protocol() - install new protocol on a handle
1031 * @handle: handle on which the protocol shall be installed
1032 * @protocol: GUID of the protocol to be installed
1033 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001034 *
Mario Six8fac2912018-07-10 08:40:17 +02001035 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001036 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001037efi_status_t efi_add_protocol(const efi_handle_t handle,
1038 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001039 void *protocol_interface)
1040{
1041 struct efi_object *efiobj;
1042 struct efi_handler *handler;
1043 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001044 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001045
1046 efiobj = efi_search_obj(handle);
1047 if (!efiobj)
1048 return EFI_INVALID_PARAMETER;
1049 ret = efi_search_protocol(handle, protocol, NULL);
1050 if (ret != EFI_NOT_FOUND)
1051 return EFI_INVALID_PARAMETER;
1052 handler = calloc(1, sizeof(struct efi_handler));
1053 if (!handler)
1054 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001055 handler->guid = protocol;
1056 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001057 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001058 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001059
1060 /* Notify registered events */
1061 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001062 if (!guidcmp(protocol, &event->protocol)) {
1063 struct efi_protocol_notification *notif;
1064
1065 notif = calloc(1, sizeof(*notif));
1066 if (!notif) {
1067 list_del(&handler->link);
1068 free(handler);
1069 return EFI_OUT_OF_RESOURCES;
1070 }
1071 notif->handle = handle;
1072 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001073 event->event->is_signaled = false;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001074 efi_signal_event(event->event, true);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001075 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001076 }
1077
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001078 if (!guidcmp(&efi_guid_device_path, protocol))
1079 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001080 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001081}
1082
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001083/**
Mario Six8fac2912018-07-10 08:40:17 +02001084 * efi_install_protocol_interface() - install protocol interface
1085 * @handle: handle on which the protocol shall be installed
1086 * @protocol: GUID of the protocol to be installed
1087 * @protocol_interface_type: type of the interface to be installed,
1088 * always EFI_NATIVE_INTERFACE
1089 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001090 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001091 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001092 *
Mario Six8fac2912018-07-10 08:40:17 +02001093 * See the Unified Extensible Firmware Interface (UEFI) specification for
1094 * details.
1095 *
1096 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001097 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001098static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001099 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001100 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001101{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001102 efi_status_t r;
1103
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001104 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1105 protocol_interface);
1106
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001107 if (!handle || !protocol ||
1108 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1109 r = EFI_INVALID_PARAMETER;
1110 goto out;
1111 }
1112
1113 /* Create new handle if requested. */
1114 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001115 r = efi_create_handle(handle);
1116 if (r != EFI_SUCCESS)
1117 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001118 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001119 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001120 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001121 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001122 /* Add new protocol */
1123 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001124out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001125 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001126}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001127
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001128/**
Mario Six8fac2912018-07-10 08:40:17 +02001129 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001130 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001131 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001132 * @number_of_drivers: number of child controllers
1133 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001134 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001135 * The allocated buffer has to be freed with free().
1136 *
Mario Six8fac2912018-07-10 08:40:17 +02001137 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001138 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001139static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001140 const efi_guid_t *protocol,
1141 efi_uintn_t *number_of_drivers,
1142 efi_handle_t **driver_handle_buffer)
1143{
1144 struct efi_handler *handler;
1145 struct efi_open_protocol_info_item *item;
1146 efi_uintn_t count = 0, i;
1147 bool duplicate;
1148
1149 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001150 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001151 if (protocol && guidcmp(handler->guid, protocol))
1152 continue;
1153 list_for_each_entry(item, &handler->open_infos, link) {
1154 if (item->info.attributes &
1155 EFI_OPEN_PROTOCOL_BY_DRIVER)
1156 ++count;
1157 }
1158 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001159 *number_of_drivers = 0;
1160 if (!count) {
1161 *driver_handle_buffer = NULL;
1162 return EFI_SUCCESS;
1163 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001164 /*
1165 * Create buffer. In case of duplicate driver assignments the buffer
1166 * will be too large. But that does not harm.
1167 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001168 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1169 if (!*driver_handle_buffer)
1170 return EFI_OUT_OF_RESOURCES;
1171 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001172 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001173 if (protocol && guidcmp(handler->guid, protocol))
1174 continue;
1175 list_for_each_entry(item, &handler->open_infos, link) {
1176 if (item->info.attributes &
1177 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1178 /* Check this is a new driver */
1179 duplicate = false;
1180 for (i = 0; i < *number_of_drivers; ++i) {
1181 if ((*driver_handle_buffer)[i] ==
1182 item->info.agent_handle)
1183 duplicate = true;
1184 }
1185 /* Copy handle to buffer */
1186 if (!duplicate) {
1187 i = (*number_of_drivers)++;
1188 (*driver_handle_buffer)[i] =
1189 item->info.agent_handle;
1190 }
1191 }
1192 }
1193 }
1194 return EFI_SUCCESS;
1195}
1196
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001197/**
Mario Six8fac2912018-07-10 08:40:17 +02001198 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001199 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001200 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001201 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001202 *
1203 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001204 *
1205 * See the Unified Extensible Firmware Interface (UEFI) specification for
1206 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001207 *
Mario Six8fac2912018-07-10 08:40:17 +02001208 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001209 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001210static efi_status_t efi_disconnect_all_drivers
1211 (efi_handle_t handle,
1212 const efi_guid_t *protocol,
1213 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001214{
1215 efi_uintn_t number_of_drivers;
1216 efi_handle_t *driver_handle_buffer;
1217 efi_status_t r, ret;
1218
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001219 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001220 &driver_handle_buffer);
1221 if (ret != EFI_SUCCESS)
1222 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001223 if (!number_of_drivers)
1224 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001225 ret = EFI_NOT_FOUND;
1226 while (number_of_drivers) {
1227 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001228 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001229 driver_handle_buffer[--number_of_drivers],
1230 child_handle));
1231 if (r == EFI_SUCCESS)
1232 ret = r;
1233 }
1234 free(driver_handle_buffer);
1235 return ret;
1236}
1237
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001238/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001239 * efi_uninstall_protocol() - uninstall protocol interface
1240 *
Mario Six8fac2912018-07-10 08:40:17 +02001241 * @handle: handle from which the protocol shall be removed
1242 * @protocol: GUID of the protocol to be removed
1243 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001244 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001245 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001246 *
1247 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001248 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001249static efi_status_t efi_uninstall_protocol
1250 (efi_handle_t handle, const efi_guid_t *protocol,
1251 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001252{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001253 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001254 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001255 struct efi_open_protocol_info_item *item;
1256 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001257 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001258
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001259 /* Check handle */
1260 efiobj = efi_search_obj(handle);
1261 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001262 r = EFI_INVALID_PARAMETER;
1263 goto out;
1264 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001265 /* Find the protocol on the handle */
1266 r = efi_search_protocol(handle, protocol, &handler);
1267 if (r != EFI_SUCCESS)
1268 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001269 /* Disconnect controllers */
1270 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001271 /* Close protocol */
1272 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1273 if (item->info.attributes ==
1274 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1275 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1276 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1277 list_del(&item->link);
1278 }
1279 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001280 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001281 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001282 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001283 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001284out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001285 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001286}
1287
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001288/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001289 * efi_uninstall_protocol_interface() - uninstall protocol interface
1290 * @handle: handle from which the protocol shall be removed
1291 * @protocol: GUID of the protocol to be removed
1292 * @protocol_interface: interface to be removed
1293 *
1294 * This function implements the UninstallProtocolInterface service.
1295 *
1296 * See the Unified Extensible Firmware Interface (UEFI) specification for
1297 * details.
1298 *
1299 * Return: status code
1300 */
1301static efi_status_t EFIAPI efi_uninstall_protocol_interface
1302 (efi_handle_t handle, const efi_guid_t *protocol,
1303 void *protocol_interface)
1304{
1305 efi_status_t ret;
1306
1307 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1308
1309 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1310 if (ret != EFI_SUCCESS)
1311 goto out;
1312
1313 /* If the last protocol has been removed, delete the handle. */
1314 if (list_empty(&handle->protocols)) {
1315 list_del(&handle->link);
1316 free(handle);
1317 }
1318out:
1319 return EFI_EXIT(ret);
1320}
1321
1322/**
Mario Six8fac2912018-07-10 08:40:17 +02001323 * efi_register_protocol_notify() - register an event for notification when a
1324 * protocol is installed.
1325 * @protocol: GUID of the protocol whose installation shall be notified
1326 * @event: event to be signaled upon installation of the protocol
1327 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001328 *
1329 * This function implements the RegisterProtocolNotify service.
1330 * See the Unified Extensible Firmware Interface (UEFI) specification
1331 * for details.
1332 *
Mario Six8fac2912018-07-10 08:40:17 +02001333 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001334 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001335static efi_status_t EFIAPI efi_register_protocol_notify(
1336 const efi_guid_t *protocol,
1337 struct efi_event *event,
1338 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001339{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001340 struct efi_register_notify_event *item;
1341 efi_status_t ret = EFI_SUCCESS;
1342
Rob Clark238f88c2017-09-13 18:05:41 -04001343 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001344
1345 if (!protocol || !event || !registration) {
1346 ret = EFI_INVALID_PARAMETER;
1347 goto out;
1348 }
1349
1350 item = calloc(1, sizeof(struct efi_register_notify_event));
1351 if (!item) {
1352 ret = EFI_OUT_OF_RESOURCES;
1353 goto out;
1354 }
1355
1356 item->event = event;
1357 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001358 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001359
1360 list_add_tail(&item->link, &efi_register_notify_events);
1361
1362 *registration = item;
1363out:
1364 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001365}
1366
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001367/**
Mario Six8fac2912018-07-10 08:40:17 +02001368 * efi_search() - determine if an EFI handle implements a protocol
1369 * @search_type: selection criterion
1370 * @protocol: GUID of the protocol
1371 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001372 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001373 *
1374 * See the documentation of the LocateHandle service in the UEFI specification.
1375 *
Mario Six8fac2912018-07-10 08:40:17 +02001376 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001377 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001378static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001379 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001380{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001381 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001382
1383 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001384 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001385 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001386 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001387 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001388 return (ret != EFI_SUCCESS);
1389 default:
1390 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001391 return -1;
1392 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001393}
1394
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001395/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001396 * efi_check_register_notify_event() - check if registration key is valid
1397 *
1398 * Check that a pointer is a valid registration key as returned by
1399 * RegisterProtocolNotify().
1400 *
1401 * @key: registration key
1402 * Return: valid registration key or NULL
1403 */
1404static struct efi_register_notify_event *efi_check_register_notify_event
1405 (void *key)
1406{
1407 struct efi_register_notify_event *event;
1408
1409 list_for_each_entry(event, &efi_register_notify_events, link) {
1410 if (event == (struct efi_register_notify_event *)key)
1411 return event;
1412 }
1413 return NULL;
1414}
1415
1416/**
Mario Six8fac2912018-07-10 08:40:17 +02001417 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001418 *
1419 * @search_type: selection criterion
1420 * @protocol: GUID of the protocol
1421 * @search_key: registration key
1422 * @buffer_size: size of the buffer to receive the handles in bytes
1423 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001424 *
1425 * This function is meant for U-Boot internal calls. For the API implementation
1426 * of the LocateHandle service see efi_locate_handle_ext.
1427 *
Mario Six8fac2912018-07-10 08:40:17 +02001428 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001429 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001430static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001431 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001432 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001433 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001434{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001435 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001436 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001437 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001438 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001439
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001440 /* Check parameters */
1441 switch (search_type) {
1442 case ALL_HANDLES:
1443 break;
1444 case BY_REGISTER_NOTIFY:
1445 if (!search_key)
1446 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001447 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001448 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001449 if (!event)
1450 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001451 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001452 case BY_PROTOCOL:
1453 if (!protocol)
1454 return EFI_INVALID_PARAMETER;
1455 break;
1456 default:
1457 return EFI_INVALID_PARAMETER;
1458 }
1459
Alexander Grafc15d9212016-03-04 01:09:59 +01001460 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001461 if (search_type == BY_REGISTER_NOTIFY) {
1462 if (list_empty(&event->handles))
1463 return EFI_NOT_FOUND;
1464 handle = list_first_entry(&event->handles,
1465 struct efi_protocol_notification,
1466 link);
1467 efiobj = handle->handle;
1468 size += sizeof(void *);
1469 } else {
1470 list_for_each_entry(efiobj, &efi_obj_list, link) {
1471 if (!efi_search(search_type, protocol, efiobj))
1472 size += sizeof(void *);
1473 }
1474 if (size == 0)
1475 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001476 }
1477
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001478 if (!buffer_size)
1479 return EFI_INVALID_PARAMETER;
1480
Alexander Grafc15d9212016-03-04 01:09:59 +01001481 if (*buffer_size < size) {
1482 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001483 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001484 }
1485
Rob Clarkcdee3372017-08-06 14:10:07 -04001486 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001487
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001488 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001489 if (!buffer)
1490 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001491
Alexander Grafc15d9212016-03-04 01:09:59 +01001492 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001493 if (search_type == BY_REGISTER_NOTIFY) {
1494 *buffer = efiobj;
1495 list_del(&handle->link);
1496 } else {
1497 list_for_each_entry(efiobj, &efi_obj_list, link) {
1498 if (!efi_search(search_type, protocol, efiobj))
1499 *buffer++ = efiobj;
1500 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001501 }
1502
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001503 return EFI_SUCCESS;
1504}
1505
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001506/**
Mario Six8fac2912018-07-10 08:40:17 +02001507 * efi_locate_handle_ext() - locate handles implementing a protocol.
1508 * @search_type: selection criterion
1509 * @protocol: GUID of the protocol
1510 * @search_key: registration key
1511 * @buffer_size: size of the buffer to receive the handles in bytes
1512 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001513 *
1514 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001515 *
Mario Six8fac2912018-07-10 08:40:17 +02001516 * See the Unified Extensible Firmware Interface (UEFI) specification for
1517 * details.
1518 *
1519 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001520 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001521static efi_status_t EFIAPI efi_locate_handle_ext(
1522 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001523 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001524 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001525{
Rob Clark238f88c2017-09-13 18:05:41 -04001526 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001527 buffer_size, buffer);
1528
1529 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1530 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001531}
1532
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001533/**
Mario Six8fac2912018-07-10 08:40:17 +02001534 * efi_remove_configuration_table() - collapses configuration table entries,
1535 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001536 *
Mario Six8fac2912018-07-10 08:40:17 +02001537 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001538 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001539static void efi_remove_configuration_table(int i)
1540{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001541 struct efi_configuration_table *this = &systab.tables[i];
1542 struct efi_configuration_table *next = &systab.tables[i + 1];
1543 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001544
1545 memmove(this, next, (ulong)end - (ulong)next);
1546 systab.nr_tables--;
1547}
1548
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001549/**
Mario Six8fac2912018-07-10 08:40:17 +02001550 * efi_install_configuration_table() - adds, updates, or removes a
1551 * configuration table
1552 * @guid: GUID of the installed table
1553 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001554 *
1555 * This function is used for internal calls. For the API implementation of the
1556 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1557 *
Mario Six8fac2912018-07-10 08:40:17 +02001558 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001559 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001560efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1561 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001562{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001563 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001564 int i;
1565
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001566 if (!guid)
1567 return EFI_INVALID_PARAMETER;
1568
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001569 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001570 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001571 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001572 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001573 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001574 else
1575 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001576 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001577 }
1578 }
1579
Alexander Graffe3366f2017-07-26 13:41:04 +02001580 if (!table)
1581 return EFI_NOT_FOUND;
1582
Alexander Grafc15d9212016-03-04 01:09:59 +01001583 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001584 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001585 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001586
1587 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001588 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1589 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001590 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001591
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001592out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001593 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001594 efi_update_table_header_crc32(&systab.hdr);
1595
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001596 /* Notify that the configuration table was changed */
1597 list_for_each_entry(evt, &efi_events, link) {
1598 if (evt->group && !guidcmp(evt->group, guid)) {
1599 efi_signal_event(evt, false);
1600 break;
1601 }
1602 }
1603
Alexander Grafc5c11632016-08-19 01:23:24 +02001604 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001605}
1606
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001607/**
Mario Six8fac2912018-07-10 08:40:17 +02001608 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1609 * configuration table.
1610 * @guid: GUID of the installed table
1611 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001612 *
1613 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001614 *
Mario Six8fac2912018-07-10 08:40:17 +02001615 * See the Unified Extensible Firmware Interface (UEFI) specification for
1616 * details.
1617 *
1618 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001619 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001620static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1621 void *table)
1622{
Rob Clark238f88c2017-09-13 18:05:41 -04001623 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001624 return EFI_EXIT(efi_install_configuration_table(guid, table));
1625}
1626
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001627/**
Mario Six8fac2912018-07-10 08:40:17 +02001628 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001629 *
1630 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001631 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001632 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001633 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1634 * code is returned.
1635 *
1636 * @device_path: device path of the loaded image
1637 * @file_path: file path of the loaded image
1638 * @handle_ptr: handle of the loaded image
1639 * @info_ptr: loaded image protocol
1640 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001641 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001642efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1643 struct efi_device_path *file_path,
1644 struct efi_loaded_image_obj **handle_ptr,
1645 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001646{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001647 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001648 struct efi_loaded_image *info = NULL;
1649 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001650 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001651
1652 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1653 *handle_ptr = NULL;
1654 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001655
1656 info = calloc(1, sizeof(*info));
1657 if (!info)
1658 return EFI_OUT_OF_RESOURCES;
1659 obj = calloc(1, sizeof(*obj));
1660 if (!obj) {
1661 free(info);
1662 return EFI_OUT_OF_RESOURCES;
1663 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001664 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001665
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001666 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001667 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001668
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001669 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001670 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001671 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001672
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001673 if (device_path) {
1674 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001675
1676 dp = efi_dp_append(device_path, file_path);
1677 if (!dp) {
1678 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001679 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001680 }
1681 } else {
1682 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001683 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001684 ret = efi_add_protocol(&obj->header,
1685 &efi_guid_loaded_image_device_path, dp);
1686 if (ret != EFI_SUCCESS)
1687 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001688
1689 /*
1690 * When asking for the loaded_image interface, just
1691 * return handle which points to loaded_image_info
1692 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001693 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001694 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001695 if (ret != EFI_SUCCESS)
1696 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001697
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001698 *info_ptr = info;
1699 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001700
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001701 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001702failure:
1703 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001704 efi_delete_handle(&obj->header);
1705 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001706 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001707}
1708
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001709/**
Mario Six8fac2912018-07-10 08:40:17 +02001710 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001711 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001712 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1713 * callers obligation to update the memory type as needed.
1714 *
1715 * @file_path: the path of the image to load
1716 * @buffer: buffer containing the loaded image
1717 * @size: size of the loaded image
1718 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001719 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001720static
Rob Clarkc84c1102017-09-13 18:05:38 -04001721efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001722 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001723{
1724 struct efi_file_info *info = NULL;
1725 struct efi_file_handle *f;
1726 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001727 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001728 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001729
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001730 /* In case of failure nothing is returned */
1731 *buffer = NULL;
1732 *size = 0;
1733
1734 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001735 f = efi_file_from_path(file_path);
1736 if (!f)
1737 return EFI_DEVICE_ERROR;
1738
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001739 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001740 bs = 0;
1741 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1742 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001743 if (ret != EFI_BUFFER_TOO_SMALL) {
1744 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001745 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001746 }
Rob Clark857a1222017-09-13 18:05:35 -04001747
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001748 info = malloc(bs);
1749 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1750 info));
1751 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001752 goto error;
1753
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001754 /*
1755 * When reading the file we do not yet know if it contains an
1756 * application, a boottime driver, or a runtime driver. So here we
1757 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1758 * update the reservation according to the image type.
1759 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001760 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001761 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1762 EFI_BOOT_SERVICES_DATA,
1763 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001764 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001765 ret = EFI_OUT_OF_RESOURCES;
1766 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001767 }
1768
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001769 /* Read file */
1770 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1771 if (ret != EFI_SUCCESS)
1772 efi_free_pages(addr, efi_size_in_pages(bs));
1773 *buffer = (void *)(uintptr_t)addr;
1774 *size = bs;
1775error:
1776 EFI_CALL(f->close(f));
1777 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001778 return ret;
1779}
1780
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001781/**
Mario Six8fac2912018-07-10 08:40:17 +02001782 * efi_load_image() - load an EFI image into memory
1783 * @boot_policy: true for request originating from the boot manager
1784 * @parent_image: the caller's image handle
1785 * @file_path: the path of the image to load
1786 * @source_buffer: memory location from which the image is installed
1787 * @source_size: size of the memory area from which the image is installed
1788 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001789 *
1790 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001791 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001792 * See the Unified Extensible Firmware Interface (UEFI) specification
1793 * for details.
1794 *
Mario Six8fac2912018-07-10 08:40:17 +02001795 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001796 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001797efi_status_t EFIAPI efi_load_image(bool boot_policy,
1798 efi_handle_t parent_image,
1799 struct efi_device_path *file_path,
1800 void *source_buffer,
1801 efi_uintn_t source_size,
1802 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001803{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001804 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001805 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001806 struct efi_loaded_image_obj **image_obj =
1807 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001808 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001809 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001810
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001811 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001812 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001813
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001814 if (!image_handle || !efi_search_obj(parent_image)) {
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001815 ret = EFI_INVALID_PARAMETER;
1816 goto error;
1817 }
1818
1819 if (!source_buffer && !file_path) {
1820 ret = EFI_NOT_FOUND;
1821 goto error;
1822 }
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001823 /* The parent image handle must refer to a loaded image */
1824 if (!parent_image->type) {
1825 ret = EFI_INVALID_PARAMETER;
1826 goto error;
1827 }
Rob Clark857a1222017-09-13 18:05:35 -04001828
1829 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001830 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001831 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001832 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001833 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001834 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001835 if (!source_size) {
1836 ret = EFI_LOAD_ERROR;
1837 goto error;
1838 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001839 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001840 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001841 /* split file_path which contains both the device and file parts */
1842 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001843 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001844 if (ret == EFI_SUCCESS)
1845 ret = efi_load_pe(*image_obj, dest_buffer, info);
1846 if (!source_buffer)
1847 /* Release buffer to which file was loaded */
1848 efi_free_pages((uintptr_t)dest_buffer,
1849 efi_size_in_pages(source_size));
1850 if (ret == EFI_SUCCESS) {
1851 info->system_table = &systab;
1852 info->parent_handle = parent_image;
1853 } else {
1854 /* The image is invalid. Release all associated resources. */
1855 efi_delete_handle(*image_handle);
1856 *image_handle = NULL;
1857 free(info);
1858 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001859error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001860 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001861}
1862
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001863/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001864 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1865 */
1866static void efi_exit_caches(void)
1867{
1868#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1869 /*
1870 * Grub on 32bit ARM needs to have caches disabled before jumping into
1871 * a zImage, but does not know of all cache layers. Give it a hand.
1872 */
1873 if (efi_is_direct_boot)
1874 cleanup_before_linux();
1875#endif
1876}
1877
1878/**
Mario Six8fac2912018-07-10 08:40:17 +02001879 * efi_exit_boot_services() - stop all boot services
1880 * @image_handle: handle of the loaded image
1881 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001882 *
1883 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001884 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001885 * See the Unified Extensible Firmware Interface (UEFI) specification
1886 * for details.
1887 *
Mario Six8fac2912018-07-10 08:40:17 +02001888 * All timer events are disabled. For exit boot services events the
1889 * notification function is called. The boot services are disabled in the
1890 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001891 *
Mario Six8fac2912018-07-10 08:40:17 +02001892 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001893 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001894static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001895 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001896{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001897 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001898
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001899 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001900
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001901 /* Check that the caller has read the current memory map */
1902 if (map_key != efi_memory_map_key)
1903 return EFI_INVALID_PARAMETER;
1904
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001905 /* Make sure that notification functions are not called anymore */
1906 efi_tpl = TPL_HIGH_LEVEL;
1907
1908 /* Check if ExitBootServices has already been called */
1909 if (!systab.boottime)
1910 return EFI_EXIT(EFI_SUCCESS);
1911
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001912 /* Add related events to the event group */
1913 list_for_each_entry(evt, &efi_events, link) {
1914 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1915 evt->group = &efi_guid_event_group_exit_boot_services;
1916 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001917 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001918 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001919 if (evt->group &&
1920 !guidcmp(evt->group,
1921 &efi_guid_event_group_exit_boot_services)) {
1922 efi_signal_event(evt, false);
1923 break;
1924 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001925 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001926
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001927 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001928
Alexander Graf2ebeb442016-11-17 01:02:57 +01001929 board_quiesce_devices();
1930
Alexander Graffa5246b2018-11-15 21:23:47 +01001931 /* Fix up caches for EFI payloads if necessary */
1932 efi_exit_caches();
1933
Alexander Grafc15d9212016-03-04 01:09:59 +01001934 /* This stops all lingering devices */
1935 bootm_disable_interrupts();
1936
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001937 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001938 systab.con_in_handle = NULL;
1939 systab.con_in = NULL;
1940 systab.con_out_handle = NULL;
1941 systab.con_out = NULL;
1942 systab.stderr_handle = NULL;
1943 systab.std_err = NULL;
1944 systab.boottime = NULL;
1945
1946 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001947 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001948
Alexander Grafc15d9212016-03-04 01:09:59 +01001949 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001950 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001951 WATCHDOG_RESET();
1952
1953 return EFI_EXIT(EFI_SUCCESS);
1954}
1955
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001956/**
Mario Six8fac2912018-07-10 08:40:17 +02001957 * efi_get_next_monotonic_count() - get next value of the counter
1958 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001959 *
1960 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001961 *
1962 * See the Unified Extensible Firmware Interface (UEFI) specification for
1963 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001964 *
Mario Six8fac2912018-07-10 08:40:17 +02001965 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001966 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001967static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1968{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001969 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001970 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001971
Alexander Grafc15d9212016-03-04 01:09:59 +01001972 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001973 if (!count) {
1974 ret = EFI_INVALID_PARAMETER;
1975 goto out;
1976 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001977 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02001978 ret = EFI_SUCCESS;
1979out:
1980 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001981}
1982
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001983/**
Mario Six8fac2912018-07-10 08:40:17 +02001984 * efi_stall() - sleep
1985 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001986 *
Mario Six8fac2912018-07-10 08:40:17 +02001987 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001988 *
Mario Six8fac2912018-07-10 08:40:17 +02001989 * See the Unified Extensible Firmware Interface (UEFI) specification for
1990 * details.
1991 *
1992 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001993 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001994static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1995{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02001996 u64 end_tick;
1997
Alexander Grafc15d9212016-03-04 01:09:59 +01001998 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02001999
2000 end_tick = get_ticks() + usec_to_tick(microseconds);
2001 while (get_ticks() < end_tick)
2002 efi_timer_check();
2003
Alexander Grafc15d9212016-03-04 01:09:59 +01002004 return EFI_EXIT(EFI_SUCCESS);
2005}
2006
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002007/**
Mario Six8fac2912018-07-10 08:40:17 +02002008 * efi_set_watchdog_timer() - reset the watchdog timer
2009 * @timeout: seconds before reset by watchdog
2010 * @watchdog_code: code to be logged when resetting
2011 * @data_size: size of buffer in bytes
2012 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002013 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002014 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002015 *
Mario Six8fac2912018-07-10 08:40:17 +02002016 * See the Unified Extensible Firmware Interface (UEFI) specification for
2017 * details.
2018 *
2019 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002020 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002021static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2022 uint64_t watchdog_code,
2023 unsigned long data_size,
2024 uint16_t *watchdog_data)
2025{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002026 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002027 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002028 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002029}
2030
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002031/**
Mario Six8fac2912018-07-10 08:40:17 +02002032 * efi_close_protocol() - close a protocol
2033 * @handle: handle on which the protocol shall be closed
2034 * @protocol: GUID of the protocol to close
2035 * @agent_handle: handle of the driver
2036 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002037 *
2038 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002039 *
Mario Six8fac2912018-07-10 08:40:17 +02002040 * See the Unified Extensible Firmware Interface (UEFI) specification for
2041 * details.
2042 *
2043 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002044 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002045static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002046 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002047 efi_handle_t agent_handle,
2048 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002049{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002050 struct efi_handler *handler;
2051 struct efi_open_protocol_info_item *item;
2052 struct efi_open_protocol_info_item *pos;
2053 efi_status_t r;
2054
Rob Clark238f88c2017-09-13 18:05:41 -04002055 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002056 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002057
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002058 if (!efi_search_obj(agent_handle) ||
2059 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002060 r = EFI_INVALID_PARAMETER;
2061 goto out;
2062 }
2063 r = efi_search_protocol(handle, protocol, &handler);
2064 if (r != EFI_SUCCESS)
2065 goto out;
2066
2067 r = EFI_NOT_FOUND;
2068 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2069 if (item->info.agent_handle == agent_handle &&
2070 item->info.controller_handle == controller_handle) {
2071 efi_delete_open_info(item);
2072 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002073 }
2074 }
2075out:
2076 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002077}
2078
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002079/**
Mario Six8fac2912018-07-10 08:40:17 +02002080 * efi_open_protocol_information() - provide information about then open status
2081 * of a protocol on a handle
2082 * @handle: handle for which the information shall be retrieved
2083 * @protocol: GUID of the protocol
2084 * @entry_buffer: buffer to receive the open protocol information
2085 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002086 *
2087 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002088 *
2089 * See the Unified Extensible Firmware Interface (UEFI) specification for
2090 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002091 *
Mario Six8fac2912018-07-10 08:40:17 +02002092 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002093 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002094static efi_status_t EFIAPI efi_open_protocol_information(
2095 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002096 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002097 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002098{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002099 unsigned long buffer_size;
2100 unsigned long count;
2101 struct efi_handler *handler;
2102 struct efi_open_protocol_info_item *item;
2103 efi_status_t r;
2104
Rob Clark238f88c2017-09-13 18:05:41 -04002105 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002106 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002107
2108 /* Check parameters */
2109 if (!entry_buffer) {
2110 r = EFI_INVALID_PARAMETER;
2111 goto out;
2112 }
2113 r = efi_search_protocol(handle, protocol, &handler);
2114 if (r != EFI_SUCCESS)
2115 goto out;
2116
2117 /* Count entries */
2118 count = 0;
2119 list_for_each_entry(item, &handler->open_infos, link) {
2120 if (item->info.open_count)
2121 ++count;
2122 }
2123 *entry_count = count;
2124 *entry_buffer = NULL;
2125 if (!count) {
2126 r = EFI_SUCCESS;
2127 goto out;
2128 }
2129
2130 /* Copy entries */
2131 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002132 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002133 (void **)entry_buffer);
2134 if (r != EFI_SUCCESS)
2135 goto out;
2136 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2137 if (item->info.open_count)
2138 (*entry_buffer)[--count] = item->info;
2139 }
2140out:
2141 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002142}
2143
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002144/**
Mario Six8fac2912018-07-10 08:40:17 +02002145 * efi_protocols_per_handle() - get protocols installed on a handle
2146 * @handle: handle for which the information is retrieved
2147 * @protocol_buffer: buffer with protocol GUIDs
2148 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002149 *
2150 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002151 *
Mario Six8fac2912018-07-10 08:40:17 +02002152 * See the Unified Extensible Firmware Interface (UEFI) specification for
2153 * details.
2154 *
2155 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002156 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002157static efi_status_t EFIAPI efi_protocols_per_handle(
2158 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002159 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002160{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002161 unsigned long buffer_size;
2162 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002163 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002164 efi_status_t r;
2165
Alexander Grafc15d9212016-03-04 01:09:59 +01002166 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2167 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002168
2169 if (!handle || !protocol_buffer || !protocol_buffer_count)
2170 return EFI_EXIT(EFI_INVALID_PARAMETER);
2171
2172 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002173 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002174
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002175 efiobj = efi_search_obj(handle);
2176 if (!efiobj)
2177 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002178
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002179 /* Count protocols */
2180 list_for_each(protocol_handle, &efiobj->protocols) {
2181 ++*protocol_buffer_count;
2182 }
2183
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002184 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002185 if (*protocol_buffer_count) {
2186 size_t j = 0;
2187
2188 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002189 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002190 (void **)protocol_buffer);
2191 if (r != EFI_SUCCESS)
2192 return EFI_EXIT(r);
2193 list_for_each(protocol_handle, &efiobj->protocols) {
2194 struct efi_handler *protocol;
2195
2196 protocol = list_entry(protocol_handle,
2197 struct efi_handler, link);
2198 (*protocol_buffer)[j] = (void *)protocol->guid;
2199 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002200 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002201 }
2202
2203 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002204}
2205
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002206/**
Mario Six8fac2912018-07-10 08:40:17 +02002207 * efi_locate_handle_buffer() - locate handles implementing a protocol
2208 * @search_type: selection criterion
2209 * @protocol: GUID of the protocol
2210 * @search_key: registration key
2211 * @no_handles: number of returned handles
2212 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002213 *
2214 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002215 *
Mario Six8fac2912018-07-10 08:40:17 +02002216 * See the Unified Extensible Firmware Interface (UEFI) specification for
2217 * details.
2218 *
2219 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002220 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002221static efi_status_t EFIAPI efi_locate_handle_buffer(
2222 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002223 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002224 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002225{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002226 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002227 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002228
Rob Clark238f88c2017-09-13 18:05:41 -04002229 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002230 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002231
2232 if (!no_handles || !buffer) {
2233 r = EFI_INVALID_PARAMETER;
2234 goto out;
2235 }
2236 *no_handles = 0;
2237 *buffer = NULL;
2238 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2239 *buffer);
2240 if (r != EFI_BUFFER_TOO_SMALL)
2241 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002242 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002243 (void **)buffer);
2244 if (r != EFI_SUCCESS)
2245 goto out;
2246 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2247 *buffer);
2248 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002249 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002250out:
2251 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002252}
2253
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002254/**
Mario Six8fac2912018-07-10 08:40:17 +02002255 * efi_locate_protocol() - find an interface implementing a protocol
2256 * @protocol: GUID of the protocol
2257 * @registration: registration key passed to the notification function
2258 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002259 *
2260 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002261 *
2262 * See the Unified Extensible Firmware Interface (UEFI) specification for
2263 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002264 *
Mario Six8fac2912018-07-10 08:40:17 +02002265 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002266 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002267static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002268 void *registration,
2269 void **protocol_interface)
2270{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002271 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002272 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002273 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002274
Rob Clark238f88c2017-09-13 18:05:41 -04002275 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002276
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002277 /*
2278 * The UEFI spec explicitly requires a protocol even if a registration
2279 * key is provided. This differs from the logic in LocateHandle().
2280 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002281 if (!protocol || !protocol_interface)
2282 return EFI_EXIT(EFI_INVALID_PARAMETER);
2283
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002284 if (registration) {
2285 struct efi_register_notify_event *event;
2286 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002287
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002288 event = efi_check_register_notify_event(registration);
2289 if (!event)
2290 return EFI_EXIT(EFI_INVALID_PARAMETER);
2291 /*
2292 * The UEFI spec requires to return EFI_NOT_FOUND if no
2293 * protocol instance matches protocol and registration.
2294 * So let's do the same for a mismatch between protocol and
2295 * registration.
2296 */
2297 if (guidcmp(&event->protocol, protocol))
2298 goto not_found;
2299 if (list_empty(&event->handles))
2300 goto not_found;
2301 handle = list_first_entry(&event->handles,
2302 struct efi_protocol_notification,
2303 link);
2304 efiobj = handle->handle;
2305 list_del(&handle->link);
2306 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002307 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002308 if (ret == EFI_SUCCESS)
2309 goto found;
2310 } else {
2311 list_for_each_entry(efiobj, &efi_obj_list, link) {
2312 ret = efi_search_protocol(efiobj, protocol, &handler);
2313 if (ret == EFI_SUCCESS)
2314 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002315 }
2316 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002317not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002318 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002319 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002320found:
2321 *protocol_interface = handler->protocol_interface;
2322 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002323}
2324
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002325/**
Mario Six8fac2912018-07-10 08:40:17 +02002326 * efi_locate_device_path() - Get the device path and handle of an device
2327 * implementing a protocol
2328 * @protocol: GUID of the protocol
2329 * @device_path: device path
2330 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002331 *
2332 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002333 *
Mario Six8fac2912018-07-10 08:40:17 +02002334 * See the Unified Extensible Firmware Interface (UEFI) specification for
2335 * details.
2336 *
2337 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002338 */
2339static efi_status_t EFIAPI efi_locate_device_path(
2340 const efi_guid_t *protocol,
2341 struct efi_device_path **device_path,
2342 efi_handle_t *device)
2343{
2344 struct efi_device_path *dp;
2345 size_t i;
2346 struct efi_handler *handler;
2347 efi_handle_t *handles;
2348 size_t len, len_dp;
2349 size_t len_best = 0;
2350 efi_uintn_t no_handles;
2351 u8 *remainder;
2352 efi_status_t ret;
2353
2354 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2355
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002356 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002357 ret = EFI_INVALID_PARAMETER;
2358 goto out;
2359 }
2360
2361 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002362 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002363
2364 /* Get all handles implementing the protocol */
2365 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2366 &no_handles, &handles));
2367 if (ret != EFI_SUCCESS)
2368 goto out;
2369
2370 for (i = 0; i < no_handles; ++i) {
2371 /* Find the device path protocol */
2372 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2373 &handler);
2374 if (ret != EFI_SUCCESS)
2375 continue;
2376 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002377 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002378 /*
2379 * This handle can only be a better fit
2380 * if its device path length is longer than the best fit and
2381 * if its device path length is shorter of equal the searched
2382 * device path.
2383 */
2384 if (len_dp <= len_best || len_dp > len)
2385 continue;
2386 /* Check if dp is a subpath of device_path */
2387 if (memcmp(*device_path, dp, len_dp))
2388 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002389 if (!device) {
2390 ret = EFI_INVALID_PARAMETER;
2391 goto out;
2392 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002393 *device = handles[i];
2394 len_best = len_dp;
2395 }
2396 if (len_best) {
2397 remainder = (u8 *)*device_path + len_best;
2398 *device_path = (struct efi_device_path *)remainder;
2399 ret = EFI_SUCCESS;
2400 } else {
2401 ret = EFI_NOT_FOUND;
2402 }
2403out:
2404 return EFI_EXIT(ret);
2405}
2406
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002407/**
Mario Six8fac2912018-07-10 08:40:17 +02002408 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2409 * interfaces
2410 * @handle: handle on which the protocol interfaces shall be installed
2411 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2412 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002413 *
2414 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002415 *
2416 * See the Unified Extensible Firmware Interface (UEFI) specification for
2417 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002418 *
Mario Six8fac2912018-07-10 08:40:17 +02002419 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002420 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002421efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002422 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002423{
2424 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002425
Alexander Graf404a7c82018-06-18 17:23:05 +02002426 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002427 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002428 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002429 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002430 efi_status_t r = EFI_SUCCESS;
2431 int i = 0;
2432
2433 if (!handle)
2434 return EFI_EXIT(EFI_INVALID_PARAMETER);
2435
Alexander Graf404a7c82018-06-18 17:23:05 +02002436 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002437 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002438 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002439 if (!protocol)
2440 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002441 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002442 /* Check that a device path has not been installed before */
2443 if (!guidcmp(protocol, &efi_guid_device_path)) {
2444 struct efi_device_path *dp = protocol_interface;
2445
2446 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2447 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002448 if (r == EFI_SUCCESS &&
2449 dp->type == DEVICE_PATH_TYPE_END) {
2450 EFI_PRINT("Path %pD already installed\n",
2451 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002452 r = EFI_ALREADY_STARTED;
2453 break;
2454 }
2455 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002456 r = EFI_CALL(efi_install_protocol_interface(
2457 handle, protocol,
2458 EFI_NATIVE_INTERFACE,
2459 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002460 if (r != EFI_SUCCESS)
2461 break;
2462 i++;
2463 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002464 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002465 if (r == EFI_SUCCESS)
2466 return EFI_EXIT(r);
2467
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002468 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002469 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002470 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002471 protocol = efi_va_arg(argptr, efi_guid_t*);
2472 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002473 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002474 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002475 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002476 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002477
2478 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002479}
2480
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002481/**
Mario Six8fac2912018-07-10 08:40:17 +02002482 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2483 * interfaces
2484 * @handle: handle from which the protocol interfaces shall be removed
2485 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2486 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002487 *
2488 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002489 *
Mario Six8fac2912018-07-10 08:40:17 +02002490 * See the Unified Extensible Firmware Interface (UEFI) specification for
2491 * details.
2492 *
2493 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002494 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002495static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002496 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002497{
2498 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002499
Alexander Graf404a7c82018-06-18 17:23:05 +02002500 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002501 const efi_guid_t *protocol;
2502 void *protocol_interface;
2503 efi_status_t r = EFI_SUCCESS;
2504 size_t i = 0;
2505
2506 if (!handle)
2507 return EFI_EXIT(EFI_INVALID_PARAMETER);
2508
Alexander Graf404a7c82018-06-18 17:23:05 +02002509 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002510 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002511 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002512 if (!protocol)
2513 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002514 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002515 r = efi_uninstall_protocol(handle, protocol,
2516 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002517 if (r != EFI_SUCCESS)
2518 break;
2519 i++;
2520 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002521 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002522 if (r == EFI_SUCCESS) {
2523 /* If the last protocol has been removed, delete the handle. */
2524 if (list_empty(&handle->protocols)) {
2525 list_del(&handle->link);
2526 free(handle);
2527 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002528 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002529 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002530
2531 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002532 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002533 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002534 protocol = efi_va_arg(argptr, efi_guid_t*);
2535 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002536 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2537 EFI_NATIVE_INTERFACE,
2538 protocol_interface));
2539 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002540 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002541
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002542 /* In case of an error always return EFI_INVALID_PARAMETER */
2543 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002544}
2545
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002546/**
Mario Six8fac2912018-07-10 08:40:17 +02002547 * efi_calculate_crc32() - calculate cyclic redundancy code
2548 * @data: buffer with data
2549 * @data_size: size of buffer in bytes
2550 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002551 *
2552 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002553 *
2554 * See the Unified Extensible Firmware Interface (UEFI) specification for
2555 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002556 *
Mario Six8fac2912018-07-10 08:40:17 +02002557 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002558 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002559static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2560 efi_uintn_t data_size,
2561 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002562{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002563 efi_status_t ret = EFI_SUCCESS;
2564
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002565 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002566 if (!data || !data_size || !crc32_p) {
2567 ret = EFI_INVALID_PARAMETER;
2568 goto out;
2569 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002570 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002571out:
2572 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002573}
2574
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002575/**
Mario Six8fac2912018-07-10 08:40:17 +02002576 * efi_copy_mem() - copy memory
2577 * @destination: destination of the copy operation
2578 * @source: source of the copy operation
2579 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002580 *
2581 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002582 *
Mario Six8fac2912018-07-10 08:40:17 +02002583 * See the Unified Extensible Firmware Interface (UEFI) specification for
2584 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002585 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002586static void EFIAPI efi_copy_mem(void *destination, const void *source,
2587 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002588{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002589 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002590 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002591 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002592}
2593
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002594/**
Mario Six8fac2912018-07-10 08:40:17 +02002595 * efi_set_mem() - Fill memory with a byte value.
2596 * @buffer: buffer to fill
2597 * @size: size of buffer in bytes
2598 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002599 *
2600 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002601 *
Mario Six8fac2912018-07-10 08:40:17 +02002602 * See the Unified Extensible Firmware Interface (UEFI) specification for
2603 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002604 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002605static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002606{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002607 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002608 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002609 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002610}
2611
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002612/**
Mario Six8fac2912018-07-10 08:40:17 +02002613 * efi_protocol_open() - open protocol interface on a handle
2614 * @handler: handler of a protocol
2615 * @protocol_interface: interface implementing the protocol
2616 * @agent_handle: handle of the driver
2617 * @controller_handle: handle of the controller
2618 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002619 *
Mario Six8fac2912018-07-10 08:40:17 +02002620 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002621 */
2622static efi_status_t efi_protocol_open(
2623 struct efi_handler *handler,
2624 void **protocol_interface, void *agent_handle,
2625 void *controller_handle, uint32_t attributes)
2626{
2627 struct efi_open_protocol_info_item *item;
2628 struct efi_open_protocol_info_entry *match = NULL;
2629 bool opened_by_driver = false;
2630 bool opened_exclusive = false;
2631
2632 /* If there is no agent, only return the interface */
2633 if (!agent_handle)
2634 goto out;
2635
2636 /* For TEST_PROTOCOL ignore interface attribute */
2637 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2638 *protocol_interface = NULL;
2639
2640 /*
2641 * Check if the protocol is already opened by a driver with the same
2642 * attributes or opened exclusively
2643 */
2644 list_for_each_entry(item, &handler->open_infos, link) {
2645 if (item->info.agent_handle == agent_handle) {
2646 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2647 (item->info.attributes == attributes))
2648 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002649 } else {
2650 if (item->info.attributes &
2651 EFI_OPEN_PROTOCOL_BY_DRIVER)
2652 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002653 }
2654 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2655 opened_exclusive = true;
2656 }
2657
2658 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002659 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2660 if (opened_exclusive)
2661 return EFI_ACCESS_DENIED;
2662 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2663 if (opened_exclusive || opened_by_driver)
2664 return EFI_ACCESS_DENIED;
2665 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002666
2667 /* Prepare exclusive opening */
2668 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2669 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002670disconnect_next:
2671 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002672 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002673 efi_status_t ret;
2674
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002675 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002676 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2677 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002678 item->info.controller_handle,
2679 item->info.agent_handle,
2680 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002681 if (ret == EFI_SUCCESS)
2682 /*
2683 * Child controllers may have been
2684 * removed from the open_infos list. So
2685 * let's restart the loop.
2686 */
2687 goto disconnect_next;
2688 else
2689 opened_by_driver = true;
2690 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002691 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002692 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002693 if (opened_by_driver)
2694 return EFI_ACCESS_DENIED;
2695 }
2696
2697 /* Find existing entry */
2698 list_for_each_entry(item, &handler->open_infos, link) {
2699 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002700 item->info.controller_handle == controller_handle &&
2701 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002702 match = &item->info;
2703 }
2704 /* None found, create one */
2705 if (!match) {
2706 match = efi_create_open_info(handler);
2707 if (!match)
2708 return EFI_OUT_OF_RESOURCES;
2709 }
2710
2711 match->agent_handle = agent_handle;
2712 match->controller_handle = controller_handle;
2713 match->attributes = attributes;
2714 match->open_count++;
2715
2716out:
2717 /* For TEST_PROTOCOL ignore interface attribute. */
2718 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2719 *protocol_interface = handler->protocol_interface;
2720
2721 return EFI_SUCCESS;
2722}
2723
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002724/**
Mario Six8fac2912018-07-10 08:40:17 +02002725 * efi_open_protocol() - open protocol interface on a handle
2726 * @handle: handle on which the protocol shall be opened
2727 * @protocol: GUID of the protocol
2728 * @protocol_interface: interface implementing the protocol
2729 * @agent_handle: handle of the driver
2730 * @controller_handle: handle of the controller
2731 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002732 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002733 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002734 *
Mario Six8fac2912018-07-10 08:40:17 +02002735 * See the Unified Extensible Firmware Interface (UEFI) specification for
2736 * details.
2737 *
2738 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002739 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002740static efi_status_t EFIAPI efi_open_protocol
2741 (efi_handle_t handle, const efi_guid_t *protocol,
2742 void **protocol_interface, efi_handle_t agent_handle,
2743 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002744{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002745 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002746 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002747
Rob Clark238f88c2017-09-13 18:05:41 -04002748 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002749 protocol_interface, agent_handle, controller_handle,
2750 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002751
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002752 if (!handle || !protocol ||
2753 (!protocol_interface && attributes !=
2754 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002755 goto out;
2756 }
2757
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002758 switch (attributes) {
2759 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2760 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2761 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2762 break;
2763 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2764 if (controller_handle == handle)
2765 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002766 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002767 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2768 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002769 /* Check that the controller handle is valid */
2770 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002771 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002772 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002773 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002774 /* Check that the agent handle is valid */
2775 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002776 goto out;
2777 break;
2778 default:
2779 goto out;
2780 }
2781
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002782 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002783 switch (r) {
2784 case EFI_SUCCESS:
2785 break;
2786 case EFI_NOT_FOUND:
2787 r = EFI_UNSUPPORTED;
2788 goto out;
2789 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002790 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002791 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002792
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002793 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2794 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002795out:
2796 return EFI_EXIT(r);
2797}
2798
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002799/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002800 * efi_start_image() - call the entry point of an image
2801 * @image_handle: handle of the image
2802 * @exit_data_size: size of the buffer
2803 * @exit_data: buffer to receive the exit data of the called image
2804 *
2805 * This function implements the StartImage service.
2806 *
2807 * See the Unified Extensible Firmware Interface (UEFI) specification for
2808 * details.
2809 *
2810 * Return: status code
2811 */
2812efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2813 efi_uintn_t *exit_data_size,
2814 u16 **exit_data)
2815{
2816 struct efi_loaded_image_obj *image_obj =
2817 (struct efi_loaded_image_obj *)image_handle;
2818 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002819 void *info;
2820 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002821
2822 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2823
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002824 /* Check parameters */
2825 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2826 &info, NULL, NULL,
2827 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2828 if (ret != EFI_SUCCESS)
2829 return EFI_EXIT(EFI_INVALID_PARAMETER);
2830
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002831 efi_is_direct_boot = false;
2832
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002833 image_obj->exit_data_size = exit_data_size;
2834 image_obj->exit_data = exit_data;
2835
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002836 /* call the image! */
2837 if (setjmp(&image_obj->exit_jmp)) {
2838 /*
2839 * We called the entry point of the child image with EFI_CALL
2840 * in the lines below. The child image called the Exit() boot
2841 * service efi_exit() which executed the long jump that brought
2842 * us to the current line. This implies that the second half
2843 * of the EFI_CALL macro has not been executed.
2844 */
2845#ifdef CONFIG_ARM
2846 /*
2847 * efi_exit() called efi_restore_gd(). We have to undo this
2848 * otherwise __efi_entry_check() will put the wrong value into
2849 * app_gd.
2850 */
2851 gd = app_gd;
2852#endif
2853 /*
2854 * To get ready to call EFI_EXIT below we have to execute the
2855 * missed out steps of EFI_CALL.
2856 */
2857 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002858 EFI_PRINT("%lu returned by started image\n",
2859 (unsigned long)((uintptr_t)image_obj->exit_status &
2860 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002861 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002862 return EFI_EXIT(image_obj->exit_status);
2863 }
2864
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002865 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002866 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002867 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002868 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2869
2870 /*
2871 * Usually UEFI applications call Exit() instead of returning.
2872 * But because the world doesn't consist of ponies and unicorns,
2873 * we're happy to emulate that behavior on behalf of a payload
2874 * that forgot.
2875 */
2876 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2877}
2878
2879/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002880 * efi_delete_image() - delete loaded image from memory)
2881 *
2882 * @image_obj: handle of the loaded image
2883 * @loaded_image_protocol: loaded image protocol
2884 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002885static efi_status_t efi_delete_image
2886 (struct efi_loaded_image_obj *image_obj,
2887 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002888{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002889 struct efi_object *efiobj;
2890 efi_status_t r, ret = EFI_SUCCESS;
2891
2892close_next:
2893 list_for_each_entry(efiobj, &efi_obj_list, link) {
2894 struct efi_handler *protocol;
2895
2896 list_for_each_entry(protocol, &efiobj->protocols, link) {
2897 struct efi_open_protocol_info_item *info;
2898
2899 list_for_each_entry(info, &protocol->open_infos, link) {
2900 if (info->info.agent_handle !=
2901 (efi_handle_t)image_obj)
2902 continue;
2903 r = EFI_CALL(efi_close_protocol
2904 (efiobj, protocol->guid,
2905 info->info.agent_handle,
2906 info->info.controller_handle
2907 ));
2908 if (r != EFI_SUCCESS)
2909 ret = r;
2910 /*
2911 * Closing protocols may results in further
2912 * items being deleted. To play it safe loop
2913 * over all elements again.
2914 */
2915 goto close_next;
2916 }
2917 }
2918 }
2919
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002920 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2921 efi_size_in_pages(loaded_image_protocol->image_size));
2922 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002923
2924 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002925}
2926
2927/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002928 * efi_unload_image() - unload an EFI image
2929 * @image_handle: handle of the image to be unloaded
2930 *
2931 * This function implements the UnloadImage service.
2932 *
2933 * See the Unified Extensible Firmware Interface (UEFI) specification for
2934 * details.
2935 *
2936 * Return: status code
2937 */
2938efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
2939{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002940 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002941 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002942 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002943
2944 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002945
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002946 efiobj = efi_search_obj(image_handle);
2947 if (!efiobj) {
2948 ret = EFI_INVALID_PARAMETER;
2949 goto out;
2950 }
2951 /* Find the loaded image protocol */
2952 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2953 (void **)&loaded_image_protocol,
2954 NULL, NULL,
2955 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2956 if (ret != EFI_SUCCESS) {
2957 ret = EFI_INVALID_PARAMETER;
2958 goto out;
2959 }
2960 switch (efiobj->type) {
2961 case EFI_OBJECT_TYPE_STARTED_IMAGE:
2962 /* Call the unload function */
2963 if (!loaded_image_protocol->unload) {
2964 ret = EFI_UNSUPPORTED;
2965 goto out;
2966 }
2967 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
2968 if (ret != EFI_SUCCESS)
2969 goto out;
2970 break;
2971 case EFI_OBJECT_TYPE_LOADED_IMAGE:
2972 break;
2973 default:
2974 ret = EFI_INVALID_PARAMETER;
2975 goto out;
2976 }
2977 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
2978 loaded_image_protocol);
2979out:
2980 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002981}
2982
2983/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002984 * efi_update_exit_data() - fill exit data parameters of StartImage()
2985 *
2986 * @image_obj image handle
2987 * @exit_data_size size of the exit data buffer
2988 * @exit_data buffer with data returned by UEFI payload
2989 * Return: status code
2990 */
2991static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
2992 efi_uintn_t exit_data_size,
2993 u16 *exit_data)
2994{
2995 efi_status_t ret;
2996
2997 /*
2998 * If exit_data is not provided to StartImage(), exit_data_size must be
2999 * ignored.
3000 */
3001 if (!image_obj->exit_data)
3002 return EFI_SUCCESS;
3003 if (image_obj->exit_data_size)
3004 *image_obj->exit_data_size = exit_data_size;
3005 if (exit_data_size && exit_data) {
3006 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3007 exit_data_size,
3008 (void **)image_obj->exit_data);
3009 if (ret != EFI_SUCCESS)
3010 return ret;
3011 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3012 } else {
3013 image_obj->exit_data = NULL;
3014 }
3015 return EFI_SUCCESS;
3016}
3017
3018/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003019 * efi_exit() - leave an EFI application or driver
3020 * @image_handle: handle of the application or driver that is exiting
3021 * @exit_status: status code
3022 * @exit_data_size: size of the buffer in bytes
3023 * @exit_data: buffer with data describing an error
3024 *
3025 * This function implements the Exit service.
3026 *
3027 * See the Unified Extensible Firmware Interface (UEFI) specification for
3028 * details.
3029 *
3030 * Return: status code
3031 */
3032static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3033 efi_status_t exit_status,
3034 efi_uintn_t exit_data_size,
3035 u16 *exit_data)
3036{
3037 /*
3038 * TODO: We should call the unload procedure of the loaded
3039 * image protocol.
3040 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003041 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003042 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003043 struct efi_loaded_image_obj *image_obj =
3044 (struct efi_loaded_image_obj *)image_handle;
3045
3046 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3047 exit_data_size, exit_data);
3048
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003049 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003050 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003051 (void **)&loaded_image_protocol,
3052 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003053 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003054 if (ret != EFI_SUCCESS) {
3055 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003056 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003057 }
3058
3059 /* Unloading of unstarted images */
3060 switch (image_obj->header.type) {
3061 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3062 break;
3063 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3064 efi_delete_image(image_obj, loaded_image_protocol);
3065 ret = EFI_SUCCESS;
3066 goto out;
3067 default:
3068 /* Handle does not refer to loaded image */
3069 ret = EFI_INVALID_PARAMETER;
3070 goto out;
3071 }
3072 /* A started image can only be unloaded it is the last one started. */
3073 if (image_handle != current_image) {
3074 ret = EFI_INVALID_PARAMETER;
3075 goto out;
3076 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003077
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003078 /* Exit data is only foreseen in case of failure. */
3079 if (exit_status != EFI_SUCCESS) {
3080 ret = efi_update_exit_data(image_obj, exit_data_size,
3081 exit_data);
3082 /* Exiting has priority. Don't return error to caller. */
3083 if (ret != EFI_SUCCESS)
3084 EFI_PRINT("%s: out of memory\n", __func__);
3085 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003086 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3087 exit_status != EFI_SUCCESS)
3088 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003089
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003090 /* Make sure entry/exit counts for EFI world cross-overs match */
3091 EFI_EXIT(exit_status);
3092
3093 /*
3094 * But longjmp out with the U-Boot gd, not the application's, as
3095 * the other end is a setjmp call inside EFI context.
3096 */
3097 efi_restore_gd();
3098
3099 image_obj->exit_status = exit_status;
3100 longjmp(&image_obj->exit_jmp, 1);
3101
3102 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003103out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003104 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003105}
3106
3107/**
Mario Six8fac2912018-07-10 08:40:17 +02003108 * efi_handle_protocol() - get interface of a protocol on a handle
3109 * @handle: handle on which the protocol shall be opened
3110 * @protocol: GUID of the protocol
3111 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003112 *
3113 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003114 *
3115 * See the Unified Extensible Firmware Interface (UEFI) specification for
3116 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003117 *
Mario Six8fac2912018-07-10 08:40:17 +02003118 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003119 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01003120static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02003121 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003122 void **protocol_interface)
3123{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003124 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003125 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003126}
3127
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003128/**
Mario Six8fac2912018-07-10 08:40:17 +02003129 * efi_bind_controller() - bind a single driver to a controller
3130 * @controller_handle: controller handle
3131 * @driver_image_handle: driver handle
3132 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003133 *
Mario Six8fac2912018-07-10 08:40:17 +02003134 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003135 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003136static efi_status_t efi_bind_controller(
3137 efi_handle_t controller_handle,
3138 efi_handle_t driver_image_handle,
3139 struct efi_device_path *remain_device_path)
3140{
3141 struct efi_driver_binding_protocol *binding_protocol;
3142 efi_status_t r;
3143
3144 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3145 &efi_guid_driver_binding_protocol,
3146 (void **)&binding_protocol,
3147 driver_image_handle, NULL,
3148 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3149 if (r != EFI_SUCCESS)
3150 return r;
3151 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3152 controller_handle,
3153 remain_device_path));
3154 if (r == EFI_SUCCESS)
3155 r = EFI_CALL(binding_protocol->start(binding_protocol,
3156 controller_handle,
3157 remain_device_path));
3158 EFI_CALL(efi_close_protocol(driver_image_handle,
3159 &efi_guid_driver_binding_protocol,
3160 driver_image_handle, NULL));
3161 return r;
3162}
3163
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003164/**
Mario Six8fac2912018-07-10 08:40:17 +02003165 * efi_connect_single_controller() - connect a single driver to a controller
3166 * @controller_handle: controller
3167 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003168 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003169 *
Mario Six8fac2912018-07-10 08:40:17 +02003170 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003171 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003172static efi_status_t efi_connect_single_controller(
3173 efi_handle_t controller_handle,
3174 efi_handle_t *driver_image_handle,
3175 struct efi_device_path *remain_device_path)
3176{
3177 efi_handle_t *buffer;
3178 size_t count;
3179 size_t i;
3180 efi_status_t r;
3181 size_t connected = 0;
3182
3183 /* Get buffer with all handles with driver binding protocol */
3184 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3185 &efi_guid_driver_binding_protocol,
3186 NULL, &count, &buffer));
3187 if (r != EFI_SUCCESS)
3188 return r;
3189
3190 /* Context Override */
3191 if (driver_image_handle) {
3192 for (; *driver_image_handle; ++driver_image_handle) {
3193 for (i = 0; i < count; ++i) {
3194 if (buffer[i] == *driver_image_handle) {
3195 buffer[i] = NULL;
3196 r = efi_bind_controller(
3197 controller_handle,
3198 *driver_image_handle,
3199 remain_device_path);
3200 /*
3201 * For drivers that do not support the
3202 * controller or are already connected
3203 * we receive an error code here.
3204 */
3205 if (r == EFI_SUCCESS)
3206 ++connected;
3207 }
3208 }
3209 }
3210 }
3211
3212 /*
3213 * TODO: Some overrides are not yet implemented:
3214 * - Platform Driver Override
3215 * - Driver Family Override Search
3216 * - Bus Specific Driver Override
3217 */
3218
3219 /* Driver Binding Search */
3220 for (i = 0; i < count; ++i) {
3221 if (buffer[i]) {
3222 r = efi_bind_controller(controller_handle,
3223 buffer[i],
3224 remain_device_path);
3225 if (r == EFI_SUCCESS)
3226 ++connected;
3227 }
3228 }
3229
3230 efi_free_pool(buffer);
3231 if (!connected)
3232 return EFI_NOT_FOUND;
3233 return EFI_SUCCESS;
3234}
3235
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003236/**
Mario Six8fac2912018-07-10 08:40:17 +02003237 * efi_connect_controller() - connect a controller to a driver
3238 * @controller_handle: handle of the controller
3239 * @driver_image_handle: handle of the driver
3240 * @remain_device_path: device path of a child controller
3241 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003242 *
3243 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003244 *
3245 * See the Unified Extensible Firmware Interface (UEFI) specification for
3246 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003247 *
3248 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003249 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003250 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3251 *
Mario Six8fac2912018-07-10 08:40:17 +02003252 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003253 */
3254static efi_status_t EFIAPI efi_connect_controller(
3255 efi_handle_t controller_handle,
3256 efi_handle_t *driver_image_handle,
3257 struct efi_device_path *remain_device_path,
3258 bool recursive)
3259{
3260 efi_status_t r;
3261 efi_status_t ret = EFI_NOT_FOUND;
3262 struct efi_object *efiobj;
3263
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003264 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003265 remain_device_path, recursive);
3266
3267 efiobj = efi_search_obj(controller_handle);
3268 if (!efiobj) {
3269 ret = EFI_INVALID_PARAMETER;
3270 goto out;
3271 }
3272
3273 r = efi_connect_single_controller(controller_handle,
3274 driver_image_handle,
3275 remain_device_path);
3276 if (r == EFI_SUCCESS)
3277 ret = EFI_SUCCESS;
3278 if (recursive) {
3279 struct efi_handler *handler;
3280 struct efi_open_protocol_info_item *item;
3281
3282 list_for_each_entry(handler, &efiobj->protocols, link) {
3283 list_for_each_entry(item, &handler->open_infos, link) {
3284 if (item->info.attributes &
3285 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3286 r = EFI_CALL(efi_connect_controller(
3287 item->info.controller_handle,
3288 driver_image_handle,
3289 remain_device_path,
3290 recursive));
3291 if (r == EFI_SUCCESS)
3292 ret = EFI_SUCCESS;
3293 }
3294 }
3295 }
3296 }
3297 /* Check for child controller specified by end node */
3298 if (ret != EFI_SUCCESS && remain_device_path &&
3299 remain_device_path->type == DEVICE_PATH_TYPE_END)
3300 ret = EFI_SUCCESS;
3301out:
3302 return EFI_EXIT(ret);
3303}
3304
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003305/**
Mario Six8fac2912018-07-10 08:40:17 +02003306 * efi_reinstall_protocol_interface() - reinstall protocol interface
3307 * @handle: handle on which the protocol shall be reinstalled
3308 * @protocol: GUID of the protocol to be installed
3309 * @old_interface: interface to be removed
3310 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003311 *
3312 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003313 *
3314 * See the Unified Extensible Firmware Interface (UEFI) specification for
3315 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003316 *
3317 * The old interface is uninstalled. The new interface is installed.
3318 * Drivers are connected.
3319 *
Mario Six8fac2912018-07-10 08:40:17 +02003320 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003321 */
3322static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3323 efi_handle_t handle, const efi_guid_t *protocol,
3324 void *old_interface, void *new_interface)
3325{
3326 efi_status_t ret;
3327
3328 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3329 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003330
3331 /* Uninstall protocol but do not delete handle */
3332 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003333 if (ret != EFI_SUCCESS)
3334 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003335
3336 /* Install the new protocol */
3337 ret = efi_add_protocol(handle, protocol, new_interface);
3338 /*
3339 * The UEFI spec does not specify what should happen to the handle
3340 * if in case of an error no protocol interface remains on the handle.
3341 * So let's do nothing here.
3342 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003343 if (ret != EFI_SUCCESS)
3344 goto out;
3345 /*
3346 * The returned status code has to be ignored.
3347 * Do not create an error if no suitable driver for the handle exists.
3348 */
3349 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3350out:
3351 return EFI_EXIT(ret);
3352}
3353
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003354/**
Mario Six8fac2912018-07-10 08:40:17 +02003355 * efi_get_child_controllers() - get all child controllers associated to a driver
3356 * @efiobj: handle of the controller
3357 * @driver_handle: handle of the driver
3358 * @number_of_children: number of child controllers
3359 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003360 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003361 * The allocated buffer has to be freed with free().
3362 *
Mario Six8fac2912018-07-10 08:40:17 +02003363 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003364 */
3365static efi_status_t efi_get_child_controllers(
3366 struct efi_object *efiobj,
3367 efi_handle_t driver_handle,
3368 efi_uintn_t *number_of_children,
3369 efi_handle_t **child_handle_buffer)
3370{
3371 struct efi_handler *handler;
3372 struct efi_open_protocol_info_item *item;
3373 efi_uintn_t count = 0, i;
3374 bool duplicate;
3375
3376 /* Count all child controller associations */
3377 list_for_each_entry(handler, &efiobj->protocols, link) {
3378 list_for_each_entry(item, &handler->open_infos, link) {
3379 if (item->info.agent_handle == driver_handle &&
3380 item->info.attributes &
3381 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3382 ++count;
3383 }
3384 }
3385 /*
3386 * Create buffer. In case of duplicate child controller assignments
3387 * the buffer will be too large. But that does not harm.
3388 */
3389 *number_of_children = 0;
3390 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3391 if (!*child_handle_buffer)
3392 return EFI_OUT_OF_RESOURCES;
3393 /* Copy unique child handles */
3394 list_for_each_entry(handler, &efiobj->protocols, link) {
3395 list_for_each_entry(item, &handler->open_infos, link) {
3396 if (item->info.agent_handle == driver_handle &&
3397 item->info.attributes &
3398 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3399 /* Check this is a new child controller */
3400 duplicate = false;
3401 for (i = 0; i < *number_of_children; ++i) {
3402 if ((*child_handle_buffer)[i] ==
3403 item->info.controller_handle)
3404 duplicate = true;
3405 }
3406 /* Copy handle to buffer */
3407 if (!duplicate) {
3408 i = (*number_of_children)++;
3409 (*child_handle_buffer)[i] =
3410 item->info.controller_handle;
3411 }
3412 }
3413 }
3414 }
3415 return EFI_SUCCESS;
3416}
3417
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003418/**
Mario Six8fac2912018-07-10 08:40:17 +02003419 * efi_disconnect_controller() - disconnect a controller from a driver
3420 * @controller_handle: handle of the controller
3421 * @driver_image_handle: handle of the driver
3422 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003423 *
3424 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003425 *
3426 * See the Unified Extensible Firmware Interface (UEFI) specification for
3427 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003428 *
Mario Six8fac2912018-07-10 08:40:17 +02003429 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003430 */
3431static efi_status_t EFIAPI efi_disconnect_controller(
3432 efi_handle_t controller_handle,
3433 efi_handle_t driver_image_handle,
3434 efi_handle_t child_handle)
3435{
3436 struct efi_driver_binding_protocol *binding_protocol;
3437 efi_handle_t *child_handle_buffer = NULL;
3438 size_t number_of_children = 0;
3439 efi_status_t r;
3440 size_t stop_count = 0;
3441 struct efi_object *efiobj;
3442
3443 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3444 child_handle);
3445
3446 efiobj = efi_search_obj(controller_handle);
3447 if (!efiobj) {
3448 r = EFI_INVALID_PARAMETER;
3449 goto out;
3450 }
3451
3452 if (child_handle && !efi_search_obj(child_handle)) {
3453 r = EFI_INVALID_PARAMETER;
3454 goto out;
3455 }
3456
3457 /* If no driver handle is supplied, disconnect all drivers */
3458 if (!driver_image_handle) {
3459 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3460 goto out;
3461 }
3462
3463 /* Create list of child handles */
3464 if (child_handle) {
3465 number_of_children = 1;
3466 child_handle_buffer = &child_handle;
3467 } else {
3468 efi_get_child_controllers(efiobj,
3469 driver_image_handle,
3470 &number_of_children,
3471 &child_handle_buffer);
3472 }
3473
3474 /* Get the driver binding protocol */
3475 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3476 &efi_guid_driver_binding_protocol,
3477 (void **)&binding_protocol,
3478 driver_image_handle, NULL,
3479 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3480 if (r != EFI_SUCCESS)
3481 goto out;
3482 /* Remove the children */
3483 if (number_of_children) {
3484 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3485 controller_handle,
3486 number_of_children,
3487 child_handle_buffer));
3488 if (r == EFI_SUCCESS)
3489 ++stop_count;
3490 }
3491 /* Remove the driver */
3492 if (!child_handle)
3493 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3494 controller_handle,
3495 0, NULL));
3496 if (r == EFI_SUCCESS)
3497 ++stop_count;
3498 EFI_CALL(efi_close_protocol(driver_image_handle,
3499 &efi_guid_driver_binding_protocol,
3500 driver_image_handle, NULL));
3501
3502 if (stop_count)
3503 r = EFI_SUCCESS;
3504 else
3505 r = EFI_NOT_FOUND;
3506out:
3507 if (!child_handle)
3508 free(child_handle_buffer);
3509 return EFI_EXIT(r);
3510}
3511
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003512static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003513 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003514 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3515 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003516 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003517 },
3518 .raise_tpl = efi_raise_tpl,
3519 .restore_tpl = efi_restore_tpl,
3520 .allocate_pages = efi_allocate_pages_ext,
3521 .free_pages = efi_free_pages_ext,
3522 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003523 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003524 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003525 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003526 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003527 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003528 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003529 .close_event = efi_close_event,
3530 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003531 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003532 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003533 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003534 .handle_protocol = efi_handle_protocol,
3535 .reserved = NULL,
3536 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003537 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003538 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003539 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003540 .load_image = efi_load_image,
3541 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003542 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003543 .unload_image = efi_unload_image,
3544 .exit_boot_services = efi_exit_boot_services,
3545 .get_next_monotonic_count = efi_get_next_monotonic_count,
3546 .stall = efi_stall,
3547 .set_watchdog_timer = efi_set_watchdog_timer,
3548 .connect_controller = efi_connect_controller,
3549 .disconnect_controller = efi_disconnect_controller,
3550 .open_protocol = efi_open_protocol,
3551 .close_protocol = efi_close_protocol,
3552 .open_protocol_information = efi_open_protocol_information,
3553 .protocols_per_handle = efi_protocols_per_handle,
3554 .locate_handle_buffer = efi_locate_handle_buffer,
3555 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003556 .install_multiple_protocol_interfaces =
3557 efi_install_multiple_protocol_interfaces,
3558 .uninstall_multiple_protocol_interfaces =
3559 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003560 .calculate_crc32 = efi_calculate_crc32,
3561 .copy_mem = efi_copy_mem,
3562 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003563 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003564};
3565
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003566static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003567
Alexander Graf393dd912016-10-14 13:45:30 +02003568struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003569 .hdr = {
3570 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003571 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003572 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003573 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003574 .fw_vendor = firmware_vendor,
3575 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003576 .con_in = (void *)&efi_con_in,
3577 .con_out = (void *)&efi_con_out,
3578 .std_err = (void *)&efi_con_out,
3579 .runtime = (void *)&efi_runtime_services,
3580 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003581 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003582 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003583};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003584
3585/**
3586 * efi_initialize_system_table() - Initialize system table
3587 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003588 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003589 */
3590efi_status_t efi_initialize_system_table(void)
3591{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003592 efi_status_t ret;
3593
3594 /* Allocate configuration table array */
3595 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3596 EFI_MAX_CONFIGURATION_TABLES *
3597 sizeof(struct efi_configuration_table),
3598 (void **)&systab.tables);
3599
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003600 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003601 efi_update_table_header_crc32(&systab.hdr);
3602 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3603 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003604
3605 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003606}