blob: 4fc550d9f376fa657c0805d6c9977bebd583f512 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafc15d9212016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020022
Alexander Grafc15d9212016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010028
Alexander Graffa5246b2018-11-15 21:23:47 +010029/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
Simon Glasscdfe6962016-09-25 15:27:35 -060037#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010038/*
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
43 */
44static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060045#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010046
Heinrich Schuchardt72506232019-02-09 14:10:39 +010047/* 1 if inside U-Boot code, 0 if inside EFI payload code */
48static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040049static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010050/* GUID of the device tree table */
51const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010052/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
53const efi_guid_t efi_guid_driver_binding_protocol =
54 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040055
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010056/* event group ExitBootServices() invoked */
57const efi_guid_t efi_guid_event_group_exit_boot_services =
58 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
59/* event group SetVirtualAddressMap() invoked */
60const efi_guid_t efi_guid_event_group_virtual_address_change =
61 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
62/* event group memory map changed */
63const efi_guid_t efi_guid_event_group_memory_map_change =
64 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
65/* event group boot manager about to boot */
66const efi_guid_t efi_guid_event_group_ready_to_boot =
67 EFI_EVENT_GROUP_READY_TO_BOOT;
68/* event group ResetSystem() invoked (before ExitBootServices) */
69const efi_guid_t efi_guid_event_group_reset_system =
70 EFI_EVENT_GROUP_RESET_SYSTEM;
71
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010072static efi_status_t EFIAPI efi_disconnect_controller(
73 efi_handle_t controller_handle,
74 efi_handle_t driver_image_handle,
75 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010076
Rob Clark86789d52017-07-27 08:04:18 -040077/* Called on every callback entry */
78int __efi_entry_check(void)
79{
80 int ret = entry_count++ == 0;
81#ifdef CONFIG_ARM
82 assert(efi_gd);
83 app_gd = gd;
84 gd = efi_gd;
85#endif
86 return ret;
87}
88
89/* Called on every callback exit */
90int __efi_exit_check(void)
91{
92 int ret = --entry_count == 0;
93#ifdef CONFIG_ARM
94 gd = app_gd;
95#endif
96 return ret;
97}
98
Alexander Grafc15d9212016-03-04 01:09:59 +010099/* Called from do_bootefi_exec() */
100void efi_save_gd(void)
101{
Simon Glasscdfe6962016-09-25 15:27:35 -0600102#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100103 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600104#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100105}
106
Rob Clark86789d52017-07-27 08:04:18 -0400107/*
Mario Six8fac2912018-07-10 08:40:17 +0200108 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200109 * world so we can dump out an abort message, without any care about returning
110 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400111 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100112void efi_restore_gd(void)
113{
Simon Glasscdfe6962016-09-25 15:27:35 -0600114#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100115 /* Only restore if we're already in EFI context */
116 if (!efi_gd)
117 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100118 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600119#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100120}
121
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200122/**
Mario Six8fac2912018-07-10 08:40:17 +0200123 * indent_string() - returns a string for indenting with two spaces per level
124 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100125 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200126 * A maximum of ten indent levels is supported. Higher indent levels will be
127 * truncated.
128 *
Mario Six8fac2912018-07-10 08:40:17 +0200129 * Return: A string for indenting with two spaces per level is
130 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400131 */
132static const char *indent_string(int level)
133{
134 const char *indent = " ";
135 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100136
Rob Clarke7896c32017-07-27 08:04:19 -0400137 level = min(max, level * 2);
138 return &indent[max - level];
139}
140
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200141const char *__efi_nesting(void)
142{
143 return indent_string(nesting_level);
144}
145
Rob Clarke7896c32017-07-27 08:04:19 -0400146const char *__efi_nesting_inc(void)
147{
148 return indent_string(nesting_level++);
149}
150
151const char *__efi_nesting_dec(void)
152{
153 return indent_string(--nesting_level);
154}
155
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200156/**
Mario Six8fac2912018-07-10 08:40:17 +0200157 * efi_queue_event() - queue an EFI event
158 * @event: event to signal
159 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200160 *
161 * This function queues the notification function of the event for future
162 * execution.
163 *
Mario Six8fac2912018-07-10 08:40:17 +0200164 * The notification function is called if the task priority level of the event
165 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200166 *
167 * For the SignalEvent service see efi_signal_event_ext.
168 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200169 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100170static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200171{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200172 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200173 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200174 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100175 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200176 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200177 EFI_CALL_VOID(event->notify_function(event,
178 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200179 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200180 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200181}
182
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200183/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200184 * is_valid_tpl() - check if the task priority level is valid
185 *
186 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200187 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200188 */
189efi_status_t is_valid_tpl(efi_uintn_t tpl)
190{
191 switch (tpl) {
192 case TPL_APPLICATION:
193 case TPL_CALLBACK:
194 case TPL_NOTIFY:
195 case TPL_HIGH_LEVEL:
196 return EFI_SUCCESS;
197 default:
198 return EFI_INVALID_PARAMETER;
199 }
200}
201
202/**
Mario Six8fac2912018-07-10 08:40:17 +0200203 * efi_signal_event() - signal an EFI event
204 * @event: event to signal
205 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100206 *
Mario Six8fac2912018-07-10 08:40:17 +0200207 * This function signals an event. If the event belongs to an event group all
208 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100209 * their notification function is queued.
210 *
211 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100212 */
213void efi_signal_event(struct efi_event *event, bool check_tpl)
214{
215 if (event->group) {
216 struct efi_event *evt;
217
218 /*
219 * The signaled state has to set before executing any
220 * notification function
221 */
222 list_for_each_entry(evt, &efi_events, link) {
223 if (!evt->group || guidcmp(evt->group, event->group))
224 continue;
225 if (evt->is_signaled)
226 continue;
227 evt->is_signaled = true;
228 if (evt->type & EVT_NOTIFY_SIGNAL &&
229 evt->notify_function)
230 evt->is_queued = true;
231 }
232 list_for_each_entry(evt, &efi_events, link) {
233 if (!evt->group || guidcmp(evt->group, event->group))
234 continue;
235 if (evt->is_queued)
236 efi_queue_event(evt, check_tpl);
237 }
238 } else if (!event->is_signaled) {
239 event->is_signaled = true;
240 if (event->type & EVT_NOTIFY_SIGNAL)
241 efi_queue_event(event, check_tpl);
242 }
243}
244
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200245/**
Mario Six8fac2912018-07-10 08:40:17 +0200246 * efi_raise_tpl() - raise the task priority level
247 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200248 *
249 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200250 *
251 * See the Unified Extensible Firmware Interface (UEFI) specification for
252 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200253 *
Mario Six8fac2912018-07-10 08:40:17 +0200254 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200255 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100256static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100257{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100258 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200259
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200260 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200261
262 if (new_tpl < efi_tpl)
263 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
264 efi_tpl = new_tpl;
265 if (efi_tpl > TPL_HIGH_LEVEL)
266 efi_tpl = TPL_HIGH_LEVEL;
267
268 EFI_EXIT(EFI_SUCCESS);
269 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100270}
271
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200272/**
Mario Six8fac2912018-07-10 08:40:17 +0200273 * efi_restore_tpl() - lower the task priority level
274 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200275 *
276 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200277 *
Mario Six8fac2912018-07-10 08:40:17 +0200278 * See the Unified Extensible Firmware Interface (UEFI) specification for
279 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200280 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100281static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100282{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200283 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200284
285 if (old_tpl > efi_tpl)
286 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
287 efi_tpl = old_tpl;
288 if (efi_tpl > TPL_HIGH_LEVEL)
289 efi_tpl = TPL_HIGH_LEVEL;
290
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100291 /*
292 * Lowering the TPL may have made queued events eligible for execution.
293 */
294 efi_timer_check();
295
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200296 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100297}
298
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200299/**
Mario Six8fac2912018-07-10 08:40:17 +0200300 * efi_allocate_pages_ext() - allocate memory pages
301 * @type: type of allocation to be performed
302 * @memory_type: usage type of the allocated memory
303 * @pages: number of pages to be allocated
304 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200305 *
306 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200307 *
Mario Six8fac2912018-07-10 08:40:17 +0200308 * See the Unified Extensible Firmware Interface (UEFI) specification for
309 * details.
310 *
311 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200312 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900313static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100314 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900315 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100316{
317 efi_status_t r;
318
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100319 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100320 r = efi_allocate_pages(type, memory_type, pages, memory);
321 return EFI_EXIT(r);
322}
323
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200324/**
Mario Six8fac2912018-07-10 08:40:17 +0200325 * efi_free_pages_ext() - Free memory pages.
326 * @memory: start of the memory area to be freed
327 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200328 *
329 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200330 *
331 * See the Unified Extensible Firmware Interface (UEFI) specification for
332 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200333 *
Mario Six8fac2912018-07-10 08:40:17 +0200334 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200335 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900336static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100337 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100338{
339 efi_status_t r;
340
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900341 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100342 r = efi_free_pages(memory, pages);
343 return EFI_EXIT(r);
344}
345
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200346/**
Mario Six8fac2912018-07-10 08:40:17 +0200347 * efi_get_memory_map_ext() - get map describing memory usage
348 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
349 * on exit the size of the copied memory map
350 * @memory_map: buffer to which the memory map is written
351 * @map_key: key for the memory map
352 * @descriptor_size: size of an individual memory descriptor
353 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200354 *
355 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200356 *
Mario Six8fac2912018-07-10 08:40:17 +0200357 * See the Unified Extensible Firmware Interface (UEFI) specification for
358 * details.
359 *
360 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200361 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900362static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100363 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900364 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100365 efi_uintn_t *map_key,
366 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900367 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100368{
369 efi_status_t r;
370
371 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
372 map_key, descriptor_size, descriptor_version);
373 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
374 descriptor_size, descriptor_version);
375 return EFI_EXIT(r);
376}
377
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200378/**
Mario Six8fac2912018-07-10 08:40:17 +0200379 * efi_allocate_pool_ext() - allocate memory from pool
380 * @pool_type: type of the pool from which memory is to be allocated
381 * @size: number of bytes to be allocated
382 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200383 *
384 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200385 *
386 * See the Unified Extensible Firmware Interface (UEFI) specification for
387 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200388 *
Mario Six8fac2912018-07-10 08:40:17 +0200389 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200390 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200391static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100392 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200393 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100394{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100395 efi_status_t r;
396
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100397 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200398 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100399 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100400}
401
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200402/**
Mario Six8fac2912018-07-10 08:40:17 +0200403 * efi_free_pool_ext() - free memory from pool
404 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200405 *
406 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200407 *
Mario Six8fac2912018-07-10 08:40:17 +0200408 * See the Unified Extensible Firmware Interface (UEFI) specification for
409 * details.
410 *
411 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200412 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200413static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100414{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100415 efi_status_t r;
416
417 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200418 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100419 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100420}
421
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200422/**
Mario Six8fac2912018-07-10 08:40:17 +0200423 * efi_add_handle() - add a new object to the object list
424 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100425 *
Mario Six8fac2912018-07-10 08:40:17 +0200426 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100427 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200428void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100429{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200430 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100431 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200432 INIT_LIST_HEAD(&handle->protocols);
433 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100434}
435
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200436/**
Mario Six8fac2912018-07-10 08:40:17 +0200437 * efi_create_handle() - create handle
438 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200439 *
Mario Six8fac2912018-07-10 08:40:17 +0200440 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200441 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100442efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200443{
444 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200445
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200446 obj = calloc(1, sizeof(struct efi_object));
447 if (!obj)
448 return EFI_OUT_OF_RESOURCES;
449
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100450 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200451 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200452
453 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200454}
455
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200456/**
Mario Six8fac2912018-07-10 08:40:17 +0200457 * efi_search_protocol() - find a protocol on a handle.
458 * @handle: handle
459 * @protocol_guid: GUID of the protocol
460 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100461 *
Mario Six8fac2912018-07-10 08:40:17 +0200462 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100463 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100464efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100465 const efi_guid_t *protocol_guid,
466 struct efi_handler **handler)
467{
468 struct efi_object *efiobj;
469 struct list_head *lhandle;
470
471 if (!handle || !protocol_guid)
472 return EFI_INVALID_PARAMETER;
473 efiobj = efi_search_obj(handle);
474 if (!efiobj)
475 return EFI_INVALID_PARAMETER;
476 list_for_each(lhandle, &efiobj->protocols) {
477 struct efi_handler *protocol;
478
479 protocol = list_entry(lhandle, struct efi_handler, link);
480 if (!guidcmp(protocol->guid, protocol_guid)) {
481 if (handler)
482 *handler = protocol;
483 return EFI_SUCCESS;
484 }
485 }
486 return EFI_NOT_FOUND;
487}
488
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200489/**
Mario Six8fac2912018-07-10 08:40:17 +0200490 * efi_remove_protocol() - delete protocol from a handle
491 * @handle: handle from which the protocol shall be deleted
492 * @protocol: GUID of the protocol to be deleted
493 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100494 *
Mario Six8fac2912018-07-10 08:40:17 +0200495 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100496 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100497efi_status_t efi_remove_protocol(const efi_handle_t handle,
498 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100499 void *protocol_interface)
500{
501 struct efi_handler *handler;
502 efi_status_t ret;
503
504 ret = efi_search_protocol(handle, protocol, &handler);
505 if (ret != EFI_SUCCESS)
506 return ret;
507 if (guidcmp(handler->guid, protocol))
508 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200509 if (handler->protocol_interface != protocol_interface)
510 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100511 list_del(&handler->link);
512 free(handler);
513 return EFI_SUCCESS;
514}
515
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200516/**
Mario Six8fac2912018-07-10 08:40:17 +0200517 * efi_remove_all_protocols() - delete all protocols from a handle
518 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100519 *
Mario Six8fac2912018-07-10 08:40:17 +0200520 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100521 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100522efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100523{
524 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100525 struct efi_handler *protocol;
526 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100527
528 efiobj = efi_search_obj(handle);
529 if (!efiobj)
530 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100531 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100532 efi_status_t ret;
533
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534 ret = efi_remove_protocol(handle, protocol->guid,
535 protocol->protocol_interface);
536 if (ret != EFI_SUCCESS)
537 return ret;
538 }
539 return EFI_SUCCESS;
540}
541
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200542/**
Mario Six8fac2912018-07-10 08:40:17 +0200543 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100544 *
Mario Six8fac2912018-07-10 08:40:17 +0200545 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200547void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100548{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200549 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100550 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200551 efi_remove_all_protocols(handle);
552 list_del(&handle->link);
553 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100554}
555
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200556/**
Mario Six8fac2912018-07-10 08:40:17 +0200557 * efi_is_event() - check if a pointer is a valid event
558 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100559 *
Mario Six8fac2912018-07-10 08:40:17 +0200560 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100561 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100562static efi_status_t efi_is_event(const struct efi_event *event)
563{
564 const struct efi_event *evt;
565
566 if (!event)
567 return EFI_INVALID_PARAMETER;
568 list_for_each_entry(evt, &efi_events, link) {
569 if (evt == event)
570 return EFI_SUCCESS;
571 }
572 return EFI_INVALID_PARAMETER;
573}
Alexander Grafc15d9212016-03-04 01:09:59 +0100574
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200575/**
Mario Six8fac2912018-07-10 08:40:17 +0200576 * efi_create_event() - create an event
577 * @type: type of the event to create
578 * @notify_tpl: task priority level of the event
579 * @notify_function: notification function of the event
580 * @notify_context: pointer passed to the notification function
581 * @group: event group
582 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200583 *
584 * This function is used inside U-Boot code to create an event.
585 *
586 * For the API function implementing the CreateEvent service see
587 * efi_create_event_ext.
588 *
Mario Six8fac2912018-07-10 08:40:17 +0200589 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200590 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100591efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200592 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200593 struct efi_event *event,
594 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100595 void *notify_context, efi_guid_t *group,
596 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100597{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100598 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200599
Jonathan Gray7758b212017-03-12 19:26:07 +1100600 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200601 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100602
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200603 switch (type) {
604 case 0:
605 case EVT_TIMER:
606 case EVT_NOTIFY_SIGNAL:
607 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
608 case EVT_NOTIFY_WAIT:
609 case EVT_TIMER | EVT_NOTIFY_WAIT:
610 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
611 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
612 break;
613 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200614 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200615 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100616
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900617 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
618 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200619 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100620
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100621 evt = calloc(1, sizeof(struct efi_event));
622 if (!evt)
623 return EFI_OUT_OF_RESOURCES;
624 evt->type = type;
625 evt->notify_tpl = notify_tpl;
626 evt->notify_function = notify_function;
627 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100628 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200629 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100630 evt->trigger_next = -1ULL;
631 evt->is_queued = false;
632 evt->is_signaled = false;
633 list_add_tail(&evt->link, &efi_events);
634 *event = evt;
635 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100636}
637
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200638/*
Mario Six8fac2912018-07-10 08:40:17 +0200639 * efi_create_event_ex() - create an event in a group
640 * @type: type of the event to create
641 * @notify_tpl: task priority level of the event
642 * @notify_function: notification function of the event
643 * @notify_context: pointer passed to the notification function
644 * @event: created event
645 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100646 *
647 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100648 *
Mario Six8fac2912018-07-10 08:40:17 +0200649 * See the Unified Extensible Firmware Interface (UEFI) specification for
650 * details.
651 *
652 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100653 */
654efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
655 void (EFIAPI *notify_function) (
656 struct efi_event *event,
657 void *context),
658 void *notify_context,
659 efi_guid_t *event_group,
660 struct efi_event **event)
661{
662 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
663 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100665 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100666}
667
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200668/**
Mario Six8fac2912018-07-10 08:40:17 +0200669 * efi_create_event_ext() - create an event
670 * @type: type of the event to create
671 * @notify_tpl: task priority level of the event
672 * @notify_function: notification function of the event
673 * @notify_context: pointer passed to the notification function
674 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200675 *
676 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200677 *
678 * See the Unified Extensible Firmware Interface (UEFI) specification for
679 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200680 *
Mario Six8fac2912018-07-10 08:40:17 +0200681 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200682 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200683static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100684 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200685 void (EFIAPI *notify_function) (
686 struct efi_event *event,
687 void *context),
688 void *notify_context, struct efi_event **event)
689{
690 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
691 notify_context);
692 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100693 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200694}
695
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200696/**
Mario Six8fac2912018-07-10 08:40:17 +0200697 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200698 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200699 * Check if a timer event has occurred or a queued notification function should
700 * be called.
701 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100702 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200703 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100704 */
705void efi_timer_check(void)
706{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100707 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100708 u64 now = timer_get_us();
709
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100710 list_for_each_entry(evt, &efi_events, link) {
711 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100712 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100713 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200714 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100715 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200716 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100717 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200718 break;
719 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100720 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200721 break;
722 default:
723 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200724 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100725 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100726 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100727 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100728 WATCHDOG_RESET();
729}
730
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200731/**
Mario Six8fac2912018-07-10 08:40:17 +0200732 * efi_set_timer() - set the trigger time for a timer event or stop the event
733 * @event: event for which the timer is set
734 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200735 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200736 *
737 * This is the function for internal usage in U-Boot. For the API function
738 * implementing the SetTimer service see efi_set_timer_ext.
739 *
Mario Six8fac2912018-07-10 08:40:17 +0200740 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200741 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200742efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200743 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100744{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100745 /* Check that the event is valid */
746 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
747 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100748
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200749 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200750 * The parameter defines a multiple of 100 ns.
751 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200752 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200753 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100754
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100755 switch (type) {
756 case EFI_TIMER_STOP:
757 event->trigger_next = -1ULL;
758 break;
759 case EFI_TIMER_PERIODIC:
760 case EFI_TIMER_RELATIVE:
761 event->trigger_next = timer_get_us() + trigger_time;
762 break;
763 default:
764 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100765 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100766 event->trigger_type = type;
767 event->trigger_time = trigger_time;
768 event->is_signaled = false;
769 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100770}
771
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200772/**
Mario Six8fac2912018-07-10 08:40:17 +0200773 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
774 * event
775 * @event: event for which the timer is set
776 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200777 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200778 *
779 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200780 *
Mario Six8fac2912018-07-10 08:40:17 +0200781 * See the Unified Extensible Firmware Interface (UEFI) specification for
782 * details.
783 *
784 *
785 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200786 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200787static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
788 enum efi_timer_delay type,
789 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200790{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900791 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200792 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
793}
794
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200795/**
Mario Six8fac2912018-07-10 08:40:17 +0200796 * efi_wait_for_event() - wait for events to be signaled
797 * @num_events: number of events to be waited for
798 * @event: events to be waited for
799 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200800 *
801 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200802 *
Mario Six8fac2912018-07-10 08:40:17 +0200803 * See the Unified Extensible Firmware Interface (UEFI) specification for
804 * details.
805 *
806 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200807 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100808static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200809 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100810 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100811{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100812 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100813
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100814 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100815
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200816 /* Check parameters */
817 if (!num_events || !event)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200819 /* Check TPL */
820 if (efi_tpl != TPL_APPLICATION)
821 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200822 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100823 if (efi_is_event(event[i]) != EFI_SUCCESS)
824 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200825 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
826 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200827 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100828 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200829 }
830
831 /* Wait for signal */
832 for (;;) {
833 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200834 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200835 goto out;
836 }
837 /* Allow events to occur. */
838 efi_timer_check();
839 }
840
841out:
842 /*
843 * Reset the signal which is passed to the caller to allow periodic
844 * events to occur.
845 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200846 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200847 if (index)
848 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100849
850 return EFI_EXIT(EFI_SUCCESS);
851}
852
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200853/**
Mario Six8fac2912018-07-10 08:40:17 +0200854 * efi_signal_event_ext() - signal an EFI event
855 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200856 *
857 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200858 *
859 * See the Unified Extensible Firmware Interface (UEFI) specification for
860 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200861 *
862 * This functions sets the signaled state of the event and queues the
863 * notification function for execution.
864 *
Mario Six8fac2912018-07-10 08:40:17 +0200865 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200866 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200867static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100868{
869 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100870 if (efi_is_event(event) != EFI_SUCCESS)
871 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100872 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100873 return EFI_EXIT(EFI_SUCCESS);
874}
875
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200876/**
Mario Six8fac2912018-07-10 08:40:17 +0200877 * efi_close_event() - close an EFI event
878 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200879 *
880 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200881 *
Mario Six8fac2912018-07-10 08:40:17 +0200882 * See the Unified Extensible Firmware Interface (UEFI) specification for
883 * details.
884 *
885 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200886 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200887static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100888{
889 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100890 if (efi_is_event(event) != EFI_SUCCESS)
891 return EFI_EXIT(EFI_INVALID_PARAMETER);
892 list_del(&event->link);
893 free(event);
894 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100895}
896
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200897/**
Mario Six8fac2912018-07-10 08:40:17 +0200898 * efi_check_event() - check if an event is signaled
899 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200900 *
901 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200902 *
903 * See the Unified Extensible Firmware Interface (UEFI) specification for
904 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200905 *
Mario Six8fac2912018-07-10 08:40:17 +0200906 * If an event is not signaled yet, the notification function is queued. The
907 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200908 *
Mario Six8fac2912018-07-10 08:40:17 +0200909 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200910 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200911static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100912{
913 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200914 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100915 if (efi_is_event(event) != EFI_SUCCESS ||
916 event->type & EVT_NOTIFY_SIGNAL)
917 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100919 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 if (event->is_signaled) {
921 event->is_signaled = false;
922 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200923 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100924 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100925}
926
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200927/**
Mario Six8fac2912018-07-10 08:40:17 +0200928 * efi_search_obj() - find the internal EFI object for a handle
929 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200930 *
Mario Six8fac2912018-07-10 08:40:17 +0200931 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200932 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100933struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200934{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100935 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200936
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100937 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200938 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200939 return efiobj;
940 }
941
942 return NULL;
943}
944
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200945/**
Mario Six8fac2912018-07-10 08:40:17 +0200946 * efi_open_protocol_info_entry() - create open protocol info entry and add it
947 * to a protocol
948 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100949 *
Mario Six8fac2912018-07-10 08:40:17 +0200950 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100951 */
952static struct efi_open_protocol_info_entry *efi_create_open_info(
953 struct efi_handler *handler)
954{
955 struct efi_open_protocol_info_item *item;
956
957 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
958 if (!item)
959 return NULL;
960 /* Append the item to the open protocol info list. */
961 list_add_tail(&item->link, &handler->open_infos);
962
963 return &item->info;
964}
965
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200966/**
Mario Six8fac2912018-07-10 08:40:17 +0200967 * efi_delete_open_info() - remove an open protocol info entry from a protocol
968 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100969 *
Mario Six8fac2912018-07-10 08:40:17 +0200970 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100971 */
972static efi_status_t efi_delete_open_info(
973 struct efi_open_protocol_info_item *item)
974{
975 list_del(&item->link);
976 free(item);
977 return EFI_SUCCESS;
978}
979
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200980/**
Mario Six8fac2912018-07-10 08:40:17 +0200981 * efi_add_protocol() - install new protocol on a handle
982 * @handle: handle on which the protocol shall be installed
983 * @protocol: GUID of the protocol to be installed
984 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200985 *
Mario Six8fac2912018-07-10 08:40:17 +0200986 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200987 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100988efi_status_t efi_add_protocol(const efi_handle_t handle,
989 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200990 void *protocol_interface)
991{
992 struct efi_object *efiobj;
993 struct efi_handler *handler;
994 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200995
996 efiobj = efi_search_obj(handle);
997 if (!efiobj)
998 return EFI_INVALID_PARAMETER;
999 ret = efi_search_protocol(handle, protocol, NULL);
1000 if (ret != EFI_NOT_FOUND)
1001 return EFI_INVALID_PARAMETER;
1002 handler = calloc(1, sizeof(struct efi_handler));
1003 if (!handler)
1004 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001005 handler->guid = protocol;
1006 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001007 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001008 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001009 if (!guidcmp(&efi_guid_device_path, protocol))
1010 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001011 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001012}
1013
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001014/**
Mario Six8fac2912018-07-10 08:40:17 +02001015 * efi_install_protocol_interface() - install protocol interface
1016 * @handle: handle on which the protocol shall be installed
1017 * @protocol: GUID of the protocol to be installed
1018 * @protocol_interface_type: type of the interface to be installed,
1019 * always EFI_NATIVE_INTERFACE
1020 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001021 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001022 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001023 *
Mario Six8fac2912018-07-10 08:40:17 +02001024 * See the Unified Extensible Firmware Interface (UEFI) specification for
1025 * details.
1026 *
1027 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001028 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001029static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001030 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001031 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001032{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001033 efi_status_t r;
1034
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001035 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1036 protocol_interface);
1037
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001038 if (!handle || !protocol ||
1039 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1040 r = EFI_INVALID_PARAMETER;
1041 goto out;
1042 }
1043
1044 /* Create new handle if requested. */
1045 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001046 r = efi_create_handle(handle);
1047 if (r != EFI_SUCCESS)
1048 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001049 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1050 *handle);
1051 } else {
1052 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1053 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001054 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001055 /* Add new protocol */
1056 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001057out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001058 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001059}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001060
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001061/**
Mario Six8fac2912018-07-10 08:40:17 +02001062 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001063 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001064 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001065 * @number_of_drivers: number of child controllers
1066 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001067 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001068 * The allocated buffer has to be freed with free().
1069 *
Mario Six8fac2912018-07-10 08:40:17 +02001070 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001071 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001072static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001073 const efi_guid_t *protocol,
1074 efi_uintn_t *number_of_drivers,
1075 efi_handle_t **driver_handle_buffer)
1076{
1077 struct efi_handler *handler;
1078 struct efi_open_protocol_info_item *item;
1079 efi_uintn_t count = 0, i;
1080 bool duplicate;
1081
1082 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001083 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001084 if (protocol && guidcmp(handler->guid, protocol))
1085 continue;
1086 list_for_each_entry(item, &handler->open_infos, link) {
1087 if (item->info.attributes &
1088 EFI_OPEN_PROTOCOL_BY_DRIVER)
1089 ++count;
1090 }
1091 }
1092 /*
1093 * Create buffer. In case of duplicate driver assignments the buffer
1094 * will be too large. But that does not harm.
1095 */
1096 *number_of_drivers = 0;
1097 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1098 if (!*driver_handle_buffer)
1099 return EFI_OUT_OF_RESOURCES;
1100 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001101 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001102 if (protocol && guidcmp(handler->guid, protocol))
1103 continue;
1104 list_for_each_entry(item, &handler->open_infos, link) {
1105 if (item->info.attributes &
1106 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1107 /* Check this is a new driver */
1108 duplicate = false;
1109 for (i = 0; i < *number_of_drivers; ++i) {
1110 if ((*driver_handle_buffer)[i] ==
1111 item->info.agent_handle)
1112 duplicate = true;
1113 }
1114 /* Copy handle to buffer */
1115 if (!duplicate) {
1116 i = (*number_of_drivers)++;
1117 (*driver_handle_buffer)[i] =
1118 item->info.agent_handle;
1119 }
1120 }
1121 }
1122 }
1123 return EFI_SUCCESS;
1124}
1125
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001126/**
Mario Six8fac2912018-07-10 08:40:17 +02001127 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001128 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001129 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001130 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001131 *
1132 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001133 *
1134 * See the Unified Extensible Firmware Interface (UEFI) specification for
1135 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001136 *
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_disconnect_all_drivers
1140 (efi_handle_t handle,
1141 const efi_guid_t *protocol,
1142 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001143{
1144 efi_uintn_t number_of_drivers;
1145 efi_handle_t *driver_handle_buffer;
1146 efi_status_t r, ret;
1147
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001148 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001149 &driver_handle_buffer);
1150 if (ret != EFI_SUCCESS)
1151 return ret;
1152
1153 ret = EFI_NOT_FOUND;
1154 while (number_of_drivers) {
1155 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001156 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001157 driver_handle_buffer[--number_of_drivers],
1158 child_handle));
1159 if (r == EFI_SUCCESS)
1160 ret = r;
1161 }
1162 free(driver_handle_buffer);
1163 return ret;
1164}
1165
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001166/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001167 * efi_uninstall_protocol() - uninstall protocol interface
1168 *
Mario Six8fac2912018-07-10 08:40:17 +02001169 * @handle: handle from which the protocol shall be removed
1170 * @protocol: GUID of the protocol to be removed
1171 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001172 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001173 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001174 *
1175 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001176 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001177static efi_status_t efi_uninstall_protocol
1178 (efi_handle_t handle, const efi_guid_t *protocol,
1179 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001180{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001181 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001182 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001183 struct efi_open_protocol_info_item *item;
1184 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001185 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001186
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001187 /* Check handle */
1188 efiobj = efi_search_obj(handle);
1189 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001190 r = EFI_INVALID_PARAMETER;
1191 goto out;
1192 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001193 /* Find the protocol on the handle */
1194 r = efi_search_protocol(handle, protocol, &handler);
1195 if (r != EFI_SUCCESS)
1196 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001197 /* Disconnect controllers */
1198 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1199 if (!list_empty(&handler->open_infos)) {
1200 r = EFI_ACCESS_DENIED;
1201 goto out;
1202 }
1203 /* Close protocol */
1204 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1205 if (item->info.attributes ==
1206 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1207 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1208 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1209 list_del(&item->link);
1210 }
1211 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001212 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001213 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001214 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001215 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001216out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001217 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001218}
1219
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001220/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001221 * efi_uninstall_protocol_interface() - uninstall protocol interface
1222 * @handle: handle from which the protocol shall be removed
1223 * @protocol: GUID of the protocol to be removed
1224 * @protocol_interface: interface to be removed
1225 *
1226 * This function implements the UninstallProtocolInterface service.
1227 *
1228 * See the Unified Extensible Firmware Interface (UEFI) specification for
1229 * details.
1230 *
1231 * Return: status code
1232 */
1233static efi_status_t EFIAPI efi_uninstall_protocol_interface
1234 (efi_handle_t handle, const efi_guid_t *protocol,
1235 void *protocol_interface)
1236{
1237 efi_status_t ret;
1238
1239 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1240
1241 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1242 if (ret != EFI_SUCCESS)
1243 goto out;
1244
1245 /* If the last protocol has been removed, delete the handle. */
1246 if (list_empty(&handle->protocols)) {
1247 list_del(&handle->link);
1248 free(handle);
1249 }
1250out:
1251 return EFI_EXIT(ret);
1252}
1253
1254/**
Mario Six8fac2912018-07-10 08:40:17 +02001255 * efi_register_protocol_notify() - register an event for notification when a
1256 * protocol is installed.
1257 * @protocol: GUID of the protocol whose installation shall be notified
1258 * @event: event to be signaled upon installation of the protocol
1259 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001260 *
1261 * This function implements the RegisterProtocolNotify service.
1262 * See the Unified Extensible Firmware Interface (UEFI) specification
1263 * for details.
1264 *
Mario Six8fac2912018-07-10 08:40:17 +02001265 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001266 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001267static efi_status_t EFIAPI efi_register_protocol_notify(
1268 const efi_guid_t *protocol,
1269 struct efi_event *event,
1270 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001271{
Rob Clark238f88c2017-09-13 18:05:41 -04001272 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001273 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1274}
1275
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001276/**
Mario Six8fac2912018-07-10 08:40:17 +02001277 * efi_search() - determine if an EFI handle implements a protocol
1278 * @search_type: selection criterion
1279 * @protocol: GUID of the protocol
1280 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001281 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001282 *
1283 * See the documentation of the LocateHandle service in the UEFI specification.
1284 *
Mario Six8fac2912018-07-10 08:40:17 +02001285 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001286 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001287static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001288 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001289 efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001290{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001291 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001292
1293 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001294 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001295 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001296 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001297 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001298 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001299 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001300 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001301 return (ret != EFI_SUCCESS);
1302 default:
1303 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001304 return -1;
1305 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001306}
1307
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001308/**
Mario Six8fac2912018-07-10 08:40:17 +02001309 * efi_locate_handle() - locate handles implementing a protocol
1310 * @search_type: selection criterion
1311 * @protocol: GUID of the protocol
1312 * @search_key: registration key
1313 * @buffer_size: size of the buffer to receive the handles in bytes
1314 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001315 *
1316 * This function is meant for U-Boot internal calls. For the API implementation
1317 * of the LocateHandle service see efi_locate_handle_ext.
1318 *
Mario Six8fac2912018-07-10 08:40:17 +02001319 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001320 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001321static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001322 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001323 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001324 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001325{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001326 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001327 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001328
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001329 /* Check parameters */
1330 switch (search_type) {
1331 case ALL_HANDLES:
1332 break;
1333 case BY_REGISTER_NOTIFY:
1334 if (!search_key)
1335 return EFI_INVALID_PARAMETER;
1336 /* RegisterProtocolNotify is not implemented yet */
1337 return EFI_UNSUPPORTED;
1338 case BY_PROTOCOL:
1339 if (!protocol)
1340 return EFI_INVALID_PARAMETER;
1341 break;
1342 default:
1343 return EFI_INVALID_PARAMETER;
1344 }
1345
1346 /*
1347 * efi_locate_handle_buffer uses this function for
1348 * the calculation of the necessary buffer size.
1349 * So do not require a buffer for buffersize == 0.
1350 */
1351 if (!buffer_size || (*buffer_size && !buffer))
1352 return EFI_INVALID_PARAMETER;
1353
Alexander Grafc15d9212016-03-04 01:09:59 +01001354 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001355 list_for_each_entry(efiobj, &efi_obj_list, link) {
1356 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001357 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001358 }
1359
1360 if (*buffer_size < size) {
1361 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001362 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001363 }
1364
Rob Clarkcdee3372017-08-06 14:10:07 -04001365 *buffer_size = size;
1366 if (size == 0)
1367 return EFI_NOT_FOUND;
1368
Alexander Grafc15d9212016-03-04 01:09:59 +01001369 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001370 list_for_each_entry(efiobj, &efi_obj_list, link) {
1371 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001372 *buffer++ = efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01001373 }
1374
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001375 return EFI_SUCCESS;
1376}
1377
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001378/**
Mario Six8fac2912018-07-10 08:40:17 +02001379 * efi_locate_handle_ext() - locate handles implementing a protocol.
1380 * @search_type: selection criterion
1381 * @protocol: GUID of the protocol
1382 * @search_key: registration key
1383 * @buffer_size: size of the buffer to receive the handles in bytes
1384 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001385 *
1386 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001387 *
Mario Six8fac2912018-07-10 08:40:17 +02001388 * See the Unified Extensible Firmware Interface (UEFI) specification for
1389 * details.
1390 *
1391 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001392 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001393static efi_status_t EFIAPI efi_locate_handle_ext(
1394 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001395 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001396 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001397{
Rob Clark238f88c2017-09-13 18:05:41 -04001398 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001399 buffer_size, buffer);
1400
1401 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1402 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001403}
1404
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001405/**
Mario Six8fac2912018-07-10 08:40:17 +02001406 * efi_remove_configuration_table() - collapses configuration table entries,
1407 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001408 *
Mario Six8fac2912018-07-10 08:40:17 +02001409 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001410 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001411static void efi_remove_configuration_table(int i)
1412{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001413 struct efi_configuration_table *this = &systab.tables[i];
1414 struct efi_configuration_table *next = &systab.tables[i + 1];
1415 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001416
1417 memmove(this, next, (ulong)end - (ulong)next);
1418 systab.nr_tables--;
1419}
1420
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001421/**
Mario Six8fac2912018-07-10 08:40:17 +02001422 * efi_install_configuration_table() - adds, updates, or removes a
1423 * configuration table
1424 * @guid: GUID of the installed table
1425 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001426 *
1427 * This function is used for internal calls. For the API implementation of the
1428 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1429 *
Mario Six8fac2912018-07-10 08:40:17 +02001430 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001431 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001432efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1433 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001434{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001435 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001436 int i;
1437
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001438 if (!guid)
1439 return EFI_INVALID_PARAMETER;
1440
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001441 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001442 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001443 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001444 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001445 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001446 else
1447 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001448 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001449 }
1450 }
1451
Alexander Graffe3366f2017-07-26 13:41:04 +02001452 if (!table)
1453 return EFI_NOT_FOUND;
1454
Alexander Grafc15d9212016-03-04 01:09:59 +01001455 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001456 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001457 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001458
1459 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001460 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1461 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001462 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001463
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001464out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001465 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001466 efi_update_table_header_crc32(&systab.hdr);
1467
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001468 /* Notify that the configuration table was changed */
1469 list_for_each_entry(evt, &efi_events, link) {
1470 if (evt->group && !guidcmp(evt->group, guid)) {
1471 efi_signal_event(evt, false);
1472 break;
1473 }
1474 }
1475
Alexander Grafc5c11632016-08-19 01:23:24 +02001476 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001477}
1478
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001479/**
Mario Six8fac2912018-07-10 08:40:17 +02001480 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1481 * configuration table.
1482 * @guid: GUID of the installed table
1483 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001484 *
1485 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001486 *
Mario Six8fac2912018-07-10 08:40:17 +02001487 * See the Unified Extensible Firmware Interface (UEFI) specification for
1488 * details.
1489 *
1490 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001491 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001492static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1493 void *table)
1494{
Rob Clark238f88c2017-09-13 18:05:41 -04001495 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001496 return EFI_EXIT(efi_install_configuration_table(guid, table));
1497}
1498
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001499/**
Mario Six8fac2912018-07-10 08:40:17 +02001500 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001501 *
1502 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001503 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001504 *
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001505 * In case of an error *handle_ptr and *info_ptr are set to NULL and an error
1506 * code is returned.
1507 *
1508 * @device_path: device path of the loaded image
1509 * @file_path: file path of the loaded image
1510 * @handle_ptr: handle of the loaded image
1511 * @info_ptr: loaded image protocol
1512 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001513 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001514efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1515 struct efi_device_path *file_path,
1516 struct efi_loaded_image_obj **handle_ptr,
1517 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001518{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001519 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001520 struct efi_loaded_image *info = NULL;
1521 struct efi_loaded_image_obj *obj = NULL;
1522
1523 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1524 *handle_ptr = NULL;
1525 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001526
1527 info = calloc(1, sizeof(*info));
1528 if (!info)
1529 return EFI_OUT_OF_RESOURCES;
1530 obj = calloc(1, sizeof(*obj));
1531 if (!obj) {
1532 free(info);
1533 return EFI_OUT_OF_RESOURCES;
1534 }
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001535
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001536 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001537 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001538
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001539 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001540 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001541 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001542
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001543 if (device_path) {
1544 info->device_handle = efi_dp_find_obj(device_path, NULL);
1545 /*
1546 * When asking for the device path interface, return
1547 * bootefi_device_path
1548 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001549 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001550 &efi_guid_device_path, device_path);
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001551 if (ret != EFI_SUCCESS)
1552 goto failure;
1553 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001554
1555 /*
1556 * When asking for the loaded_image interface, just
1557 * return handle which points to loaded_image_info
1558 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001559 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001560 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001561 if (ret != EFI_SUCCESS)
1562 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001563
Alexander Graf44be5da2019-02-11 15:24:00 +01001564#if CONFIG_IS_ENABLED(EFI_LOADER_HII)
Leif Lindholm5aab1b82019-01-21 12:12:57 +09001565 ret = efi_add_protocol(&obj->header,
1566 &efi_guid_hii_string_protocol,
1567 (void *)&efi_hii_string);
1568 if (ret != EFI_SUCCESS)
1569 goto failure;
1570
1571 ret = efi_add_protocol(&obj->header,
1572 &efi_guid_hii_database_protocol,
1573 (void *)&efi_hii_database);
1574 if (ret != EFI_SUCCESS)
1575 goto failure;
1576
AKASHI Takahiroca8aa712019-01-21 12:13:00 +09001577 ret = efi_add_protocol(&obj->header,
1578 &efi_guid_hii_config_routing_protocol,
1579 (void *)&efi_hii_config_routing);
1580 if (ret != EFI_SUCCESS)
1581 goto failure;
Alexander Graf44be5da2019-02-11 15:24:00 +01001582#endif
AKASHI Takahiroca8aa712019-01-21 12:13:00 +09001583
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001584 *info_ptr = info;
1585 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001586
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001587 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001588failure:
1589 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001590 efi_delete_handle(&obj->header);
1591 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001592 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001593}
1594
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001595/**
Mario Six8fac2912018-07-10 08:40:17 +02001596 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001597 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001598 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1599 * callers obligation to update the memory type as needed.
1600 *
1601 * @file_path: the path of the image to load
1602 * @buffer: buffer containing the loaded image
1603 * @size: size of the loaded image
1604 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001605 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001606efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001607 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001608{
1609 struct efi_file_info *info = NULL;
1610 struct efi_file_handle *f;
1611 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001612 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001613 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001614
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001615 /* In case of failure nothing is returned */
1616 *buffer = NULL;
1617 *size = 0;
1618
1619 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001620 f = efi_file_from_path(file_path);
1621 if (!f)
1622 return EFI_DEVICE_ERROR;
1623
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001624 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001625 bs = 0;
1626 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1627 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001628 if (ret != EFI_BUFFER_TOO_SMALL) {
1629 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001630 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001631 }
Rob Clark857a1222017-09-13 18:05:35 -04001632
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001633 info = malloc(bs);
1634 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1635 info));
1636 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001637 goto error;
1638
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001639 /*
1640 * When reading the file we do not yet know if it contains an
1641 * application, a boottime driver, or a runtime driver. So here we
1642 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1643 * update the reservation according to the image type.
1644 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001645 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001646 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1647 EFI_BOOT_SERVICES_DATA,
1648 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001649 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001650 ret = EFI_OUT_OF_RESOURCES;
1651 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001652 }
1653
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001654 /* Read file */
1655 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1656 if (ret != EFI_SUCCESS)
1657 efi_free_pages(addr, efi_size_in_pages(bs));
1658 *buffer = (void *)(uintptr_t)addr;
1659 *size = bs;
1660error:
1661 EFI_CALL(f->close(f));
1662 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001663 return ret;
1664}
1665
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001666/**
Mario Six8fac2912018-07-10 08:40:17 +02001667 * efi_load_image() - load an EFI image into memory
1668 * @boot_policy: true for request originating from the boot manager
1669 * @parent_image: the caller's image handle
1670 * @file_path: the path of the image to load
1671 * @source_buffer: memory location from which the image is installed
1672 * @source_size: size of the memory area from which the image is installed
1673 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001674 *
1675 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001676 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001677 * See the Unified Extensible Firmware Interface (UEFI) specification
1678 * for details.
1679 *
Mario Six8fac2912018-07-10 08:40:17 +02001680 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001681 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001682static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1683 efi_handle_t parent_image,
1684 struct efi_device_path *file_path,
1685 void *source_buffer,
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001686 efi_uintn_t source_size,
Alexander Grafc15d9212016-03-04 01:09:59 +01001687 efi_handle_t *image_handle)
1688{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001689 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001690 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001691 struct efi_loaded_image_obj **image_obj =
1692 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001693 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001694
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001695 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001696 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001697
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001698 if (!image_handle || !parent_image) {
1699 ret = EFI_INVALID_PARAMETER;
1700 goto error;
1701 }
1702
1703 if (!source_buffer && !file_path) {
1704 ret = EFI_NOT_FOUND;
1705 goto error;
1706 }
Rob Clark857a1222017-09-13 18:05:35 -04001707
1708 if (!source_buffer) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001709 ret = efi_load_image_from_path(file_path, &source_buffer,
1710 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001711 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001712 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001713 /*
1714 * split file_path which contains both the device and
1715 * file parts:
1716 */
1717 efi_dp_split_file_path(file_path, &dp, &fp);
Rob Clark857a1222017-09-13 18:05:35 -04001718 } else {
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001719 /* In this case, file_path is the "device" path, i.e.
Rob Clark857a1222017-09-13 18:05:35 -04001720 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1721 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001722 u64 addr;
1723 void *dest_buffer;
1724
1725 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1726 EFI_RUNTIME_SERVICES_CODE,
1727 efi_size_in_pages(source_size), &addr);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001728 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001729 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001730 dest_buffer = (void *)(uintptr_t)addr;
1731 memcpy(dest_buffer, source_buffer, source_size);
1732 source_buffer = dest_buffer;
1733
1734 dp = file_path;
1735 fp = NULL;
Rob Clark857a1222017-09-13 18:05:35 -04001736 }
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001737 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
1738 if (ret != EFI_SUCCESS)
1739 goto error_invalid_image;
Heinrich Schuchardt408fbcc2018-12-26 12:49:09 +01001740 ret = efi_load_pe(*image_obj, source_buffer, info);
1741 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001742 goto error_invalid_image;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001743 /* Update the type of the allocated memory */
1744 efi_add_memory_map((uintptr_t)source_buffer,
1745 efi_size_in_pages(source_size),
1746 info->image_code_type, false);
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001747 info->system_table = &systab;
1748 info->parent_handle = parent_image;
Alexander Grafc15d9212016-03-04 01:09:59 +01001749 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001750error_invalid_image:
1751 /* The image is invalid. Release all associated resources. */
1752 efi_free_pages((uintptr_t)source_buffer,
1753 efi_size_in_pages(source_size));
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001754 efi_delete_handle(*image_handle);
1755 *image_handle = NULL;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001756 free(info);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001757error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001758 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001759}
1760
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001761/**
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001762 * efi_start_image() - call the entry point of an image
Mario Six8fac2912018-07-10 08:40:17 +02001763 * @image_handle: handle of the image
1764 * @exit_data_size: size of the buffer
1765 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001766 *
1767 * This function implements the StartImage service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001768 *
Mario Six8fac2912018-07-10 08:40:17 +02001769 * See the Unified Extensible Firmware Interface (UEFI) specification for
1770 * details.
1771 *
1772 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001773 */
Heinrich Schuchardt2f8eb082018-12-26 13:28:09 +01001774efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1775 efi_uintn_t *exit_data_size,
1776 u16 **exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001777{
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001778 struct efi_loaded_image_obj *image_obj =
1779 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001780 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001781
1782 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
Alexander Grafc15d9212016-03-04 01:09:59 +01001783
Alexander Graffa5246b2018-11-15 21:23:47 +01001784 efi_is_direct_boot = false;
1785
Alexander Grafc15d9212016-03-04 01:09:59 +01001786 /* call the image! */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001787 if (setjmp(&image_obj->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001788 /*
1789 * We called the entry point of the child image with EFI_CALL
1790 * in the lines below. The child image called the Exit() boot
1791 * service efi_exit() which executed the long jump that brought
1792 * us to the current line. This implies that the second half
1793 * of the EFI_CALL macro has not been executed.
1794 */
1795#ifdef CONFIG_ARM
1796 /*
1797 * efi_exit() called efi_restore_gd(). We have to undo this
1798 * otherwise __efi_entry_check() will put the wrong value into
1799 * app_gd.
1800 */
1801 gd = app_gd;
1802#endif
1803 /*
1804 * To get ready to call EFI_EXIT below we have to execute the
1805 * missed out steps of EFI_CALL.
1806 */
1807 assert(__efi_entry_check());
1808 debug("%sEFI: %lu returned by started image\n",
1809 __efi_nesting_dec(),
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001810 (unsigned long)((uintptr_t)image_obj->exit_status &
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001811 ~EFI_ERROR_MASK));
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001812 return EFI_EXIT(image_obj->exit_status);
Alexander Graf988c0662016-05-20 23:28:23 +02001813 }
1814
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001815 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001816
Alexander Grafeff317c2018-01-26 00:47:53 +01001817 /*
1818 * Usually UEFI applications call Exit() instead of returning.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001819 * But because the world doesn't consist of ponies and unicorns,
Alexander Grafeff317c2018-01-26 00:47:53 +01001820 * we're happy to emulate that behavior on behalf of a payload
1821 * that forgot.
1822 */
1823 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001824}
1825
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001826/**
Mario Six8fac2912018-07-10 08:40:17 +02001827 * efi_exit() - leave an EFI application or driver
1828 * @image_handle: handle of the application or driver that is exiting
1829 * @exit_status: status code
1830 * @exit_data_size: size of the buffer in bytes
1831 * @exit_data: buffer with data describing an error
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001832 *
1833 * This function implements the Exit service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001834 *
Mario Six8fac2912018-07-10 08:40:17 +02001835 * See the Unified Extensible Firmware Interface (UEFI) specification for
1836 * details.
1837 *
1838 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001839 */
Alexander Graf988c0662016-05-20 23:28:23 +02001840static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001841 efi_status_t exit_status,
Heinrich Schuchardt23ef39a2018-12-28 12:41:15 +01001842 efi_uintn_t exit_data_size,
1843 u16 *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001844{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001845 /*
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001846 * TODO: We should call the unload procedure of the loaded
1847 * image protocol.
1848 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001849 struct efi_loaded_image_obj *image_obj =
1850 (struct efi_loaded_image_obj *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001851
Heinrich Schuchardt23ef39a2018-12-28 12:41:15 +01001852 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
Alexander Grafc15d9212016-03-04 01:09:59 +01001853 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001854
Alexander Graf87e787a2017-09-03 14:14:17 +02001855 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001856 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001857
Alexander Graf87e787a2017-09-03 14:14:17 +02001858 /*
1859 * But longjmp out with the U-Boot gd, not the application's, as
1860 * the other end is a setjmp call inside EFI context.
1861 */
1862 efi_restore_gd();
1863
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001864 image_obj->exit_status = exit_status;
1865 longjmp(&image_obj->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001866
1867 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001868}
1869
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001870/**
Mario Six8fac2912018-07-10 08:40:17 +02001871 * efi_unload_image() - unload an EFI image
1872 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001873 *
1874 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001875 *
1876 * See the Unified Extensible Firmware Interface (UEFI) specification for
1877 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001878 *
Mario Six8fac2912018-07-10 08:40:17 +02001879 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001880 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001881static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001882{
1883 struct efi_object *efiobj;
1884
1885 EFI_ENTRY("%p", image_handle);
1886 efiobj = efi_search_obj(image_handle);
1887 if (efiobj)
1888 list_del(&efiobj->link);
1889
1890 return EFI_EXIT(EFI_SUCCESS);
1891}
1892
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001893/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001894 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1895 */
1896static void efi_exit_caches(void)
1897{
1898#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1899 /*
1900 * Grub on 32bit ARM needs to have caches disabled before jumping into
1901 * a zImage, but does not know of all cache layers. Give it a hand.
1902 */
1903 if (efi_is_direct_boot)
1904 cleanup_before_linux();
1905#endif
1906}
1907
1908/**
Mario Six8fac2912018-07-10 08:40:17 +02001909 * efi_exit_boot_services() - stop all boot services
1910 * @image_handle: handle of the loaded image
1911 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001912 *
1913 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001914 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001915 * See the Unified Extensible Firmware Interface (UEFI) specification
1916 * for details.
1917 *
Mario Six8fac2912018-07-10 08:40:17 +02001918 * All timer events are disabled. For exit boot services events the
1919 * notification function is called. The boot services are disabled in the
1920 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001921 *
Mario Six8fac2912018-07-10 08:40:17 +02001922 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001923 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001924static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001925 unsigned long map_key)
1926{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001927 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001928
Alexander Grafc15d9212016-03-04 01:09:59 +01001929 EFI_ENTRY("%p, %ld", image_handle, map_key);
1930
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001931 /* Check that the caller has read the current memory map */
1932 if (map_key != efi_memory_map_key)
1933 return EFI_INVALID_PARAMETER;
1934
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001935 /* Make sure that notification functions are not called anymore */
1936 efi_tpl = TPL_HIGH_LEVEL;
1937
1938 /* Check if ExitBootServices has already been called */
1939 if (!systab.boottime)
1940 return EFI_EXIT(EFI_SUCCESS);
1941
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001942 /* Add related events to the event group */
1943 list_for_each_entry(evt, &efi_events, link) {
1944 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1945 evt->group = &efi_guid_event_group_exit_boot_services;
1946 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001947 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001948 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001949 if (evt->group &&
1950 !guidcmp(evt->group,
1951 &efi_guid_event_group_exit_boot_services)) {
1952 efi_signal_event(evt, false);
1953 break;
1954 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001955 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001956
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001957 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001958
Alexander Graf2ebeb442016-11-17 01:02:57 +01001959 board_quiesce_devices();
1960
Alexander Graffa5246b2018-11-15 21:23:47 +01001961 /* Fix up caches for EFI payloads if necessary */
1962 efi_exit_caches();
1963
Alexander Grafc15d9212016-03-04 01:09:59 +01001964 /* This stops all lingering devices */
1965 bootm_disable_interrupts();
1966
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001967 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001968 systab.con_in_handle = NULL;
1969 systab.con_in = NULL;
1970 systab.con_out_handle = NULL;
1971 systab.con_out = NULL;
1972 systab.stderr_handle = NULL;
1973 systab.std_err = NULL;
1974 systab.boottime = NULL;
1975
1976 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001977 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001978
Alexander Grafc15d9212016-03-04 01:09:59 +01001979 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001980 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001981 WATCHDOG_RESET();
1982
1983 return EFI_EXIT(EFI_SUCCESS);
1984}
1985
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001986/**
Mario Six8fac2912018-07-10 08:40:17 +02001987 * efi_get_next_monotonic_count() - get next value of the counter
1988 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001989 *
1990 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001991 *
1992 * See the Unified Extensible Firmware Interface (UEFI) specification for
1993 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001994 *
Mario Six8fac2912018-07-10 08:40:17 +02001995 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001996 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001997static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1998{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001999 static uint64_t mono;
2000
Alexander Grafc15d9212016-03-04 01:09:59 +01002001 EFI_ENTRY("%p", count);
2002 *count = mono++;
2003 return EFI_EXIT(EFI_SUCCESS);
2004}
2005
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002006/**
Mario Six8fac2912018-07-10 08:40:17 +02002007 * efi_stall() - sleep
2008 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002009 *
Mario Six8fac2912018-07-10 08:40:17 +02002010 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002011 *
Mario Six8fac2912018-07-10 08:40:17 +02002012 * See the Unified Extensible Firmware Interface (UEFI) specification for
2013 * details.
2014 *
2015 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002016 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002017static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2018{
2019 EFI_ENTRY("%ld", microseconds);
2020 udelay(microseconds);
2021 return EFI_EXIT(EFI_SUCCESS);
2022}
2023
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002024/**
Mario Six8fac2912018-07-10 08:40:17 +02002025 * efi_set_watchdog_timer() - reset the watchdog timer
2026 * @timeout: seconds before reset by watchdog
2027 * @watchdog_code: code to be logged when resetting
2028 * @data_size: size of buffer in bytes
2029 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002030 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002031 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002032 *
Mario Six8fac2912018-07-10 08:40:17 +02002033 * See the Unified Extensible Firmware Interface (UEFI) specification for
2034 * details.
2035 *
2036 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002037 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002038static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2039 uint64_t watchdog_code,
2040 unsigned long data_size,
2041 uint16_t *watchdog_data)
2042{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002043 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002044 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002045 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002046}
2047
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002048/**
Mario Six8fac2912018-07-10 08:40:17 +02002049 * efi_close_protocol() - close a protocol
2050 * @handle: handle on which the protocol shall be closed
2051 * @protocol: GUID of the protocol to close
2052 * @agent_handle: handle of the driver
2053 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002054 *
2055 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002056 *
Mario Six8fac2912018-07-10 08:40:17 +02002057 * See the Unified Extensible Firmware Interface (UEFI) specification for
2058 * details.
2059 *
2060 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002061 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002062static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002063 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002064 efi_handle_t agent_handle,
2065 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002066{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002067 struct efi_handler *handler;
2068 struct efi_open_protocol_info_item *item;
2069 struct efi_open_protocol_info_item *pos;
2070 efi_status_t r;
2071
Rob Clark238f88c2017-09-13 18:05:41 -04002072 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002073 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002074
2075 if (!agent_handle) {
2076 r = EFI_INVALID_PARAMETER;
2077 goto out;
2078 }
2079 r = efi_search_protocol(handle, protocol, &handler);
2080 if (r != EFI_SUCCESS)
2081 goto out;
2082
2083 r = EFI_NOT_FOUND;
2084 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2085 if (item->info.agent_handle == agent_handle &&
2086 item->info.controller_handle == controller_handle) {
2087 efi_delete_open_info(item);
2088 r = EFI_SUCCESS;
2089 break;
2090 }
2091 }
2092out:
2093 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002094}
2095
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002096/**
Mario Six8fac2912018-07-10 08:40:17 +02002097 * efi_open_protocol_information() - provide information about then open status
2098 * of a protocol on a handle
2099 * @handle: handle for which the information shall be retrieved
2100 * @protocol: GUID of the protocol
2101 * @entry_buffer: buffer to receive the open protocol information
2102 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002103 *
2104 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002105 *
2106 * See the Unified Extensible Firmware Interface (UEFI) specification for
2107 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002108 *
Mario Six8fac2912018-07-10 08:40:17 +02002109 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002110 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002111static efi_status_t EFIAPI efi_open_protocol_information(
2112 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002113 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002114 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002115{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002116 unsigned long buffer_size;
2117 unsigned long count;
2118 struct efi_handler *handler;
2119 struct efi_open_protocol_info_item *item;
2120 efi_status_t r;
2121
Rob Clark238f88c2017-09-13 18:05:41 -04002122 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002123 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002124
2125 /* Check parameters */
2126 if (!entry_buffer) {
2127 r = EFI_INVALID_PARAMETER;
2128 goto out;
2129 }
2130 r = efi_search_protocol(handle, protocol, &handler);
2131 if (r != EFI_SUCCESS)
2132 goto out;
2133
2134 /* Count entries */
2135 count = 0;
2136 list_for_each_entry(item, &handler->open_infos, link) {
2137 if (item->info.open_count)
2138 ++count;
2139 }
2140 *entry_count = count;
2141 *entry_buffer = NULL;
2142 if (!count) {
2143 r = EFI_SUCCESS;
2144 goto out;
2145 }
2146
2147 /* Copy entries */
2148 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002149 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002150 (void **)entry_buffer);
2151 if (r != EFI_SUCCESS)
2152 goto out;
2153 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2154 if (item->info.open_count)
2155 (*entry_buffer)[--count] = item->info;
2156 }
2157out:
2158 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002159}
2160
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002161/**
Mario Six8fac2912018-07-10 08:40:17 +02002162 * efi_protocols_per_handle() - get protocols installed on a handle
2163 * @handle: handle for which the information is retrieved
2164 * @protocol_buffer: buffer with protocol GUIDs
2165 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002166 *
2167 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002168 *
Mario Six8fac2912018-07-10 08:40:17 +02002169 * See the Unified Extensible Firmware Interface (UEFI) specification for
2170 * details.
2171 *
2172 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002173 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002174static efi_status_t EFIAPI efi_protocols_per_handle(
2175 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002176 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002177{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002178 unsigned long buffer_size;
2179 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002180 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002181 efi_status_t r;
2182
Alexander Grafc15d9212016-03-04 01:09:59 +01002183 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2184 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002185
2186 if (!handle || !protocol_buffer || !protocol_buffer_count)
2187 return EFI_EXIT(EFI_INVALID_PARAMETER);
2188
2189 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002190 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002191
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002192 efiobj = efi_search_obj(handle);
2193 if (!efiobj)
2194 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002195
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002196 /* Count protocols */
2197 list_for_each(protocol_handle, &efiobj->protocols) {
2198 ++*protocol_buffer_count;
2199 }
2200
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002201 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002202 if (*protocol_buffer_count) {
2203 size_t j = 0;
2204
2205 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002206 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002207 (void **)protocol_buffer);
2208 if (r != EFI_SUCCESS)
2209 return EFI_EXIT(r);
2210 list_for_each(protocol_handle, &efiobj->protocols) {
2211 struct efi_handler *protocol;
2212
2213 protocol = list_entry(protocol_handle,
2214 struct efi_handler, link);
2215 (*protocol_buffer)[j] = (void *)protocol->guid;
2216 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002217 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002218 }
2219
2220 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002221}
2222
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002223/**
Mario Six8fac2912018-07-10 08:40:17 +02002224 * efi_locate_handle_buffer() - locate handles implementing a protocol
2225 * @search_type: selection criterion
2226 * @protocol: GUID of the protocol
2227 * @search_key: registration key
2228 * @no_handles: number of returned handles
2229 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002230 *
2231 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002232 *
Mario Six8fac2912018-07-10 08:40:17 +02002233 * See the Unified Extensible Firmware Interface (UEFI) specification for
2234 * details.
2235 *
2236 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002237 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002238static efi_status_t EFIAPI efi_locate_handle_buffer(
2239 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002240 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002241 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002242{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002243 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002244 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002245
Rob Clark238f88c2017-09-13 18:05:41 -04002246 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002247 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002248
2249 if (!no_handles || !buffer) {
2250 r = EFI_INVALID_PARAMETER;
2251 goto out;
2252 }
2253 *no_handles = 0;
2254 *buffer = NULL;
2255 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2256 *buffer);
2257 if (r != EFI_BUFFER_TOO_SMALL)
2258 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002259 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002260 (void **)buffer);
2261 if (r != EFI_SUCCESS)
2262 goto out;
2263 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2264 *buffer);
2265 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002266 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002267out:
2268 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002269}
2270
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002271/**
Mario Six8fac2912018-07-10 08:40:17 +02002272 * efi_locate_protocol() - find an interface implementing a protocol
2273 * @protocol: GUID of the protocol
2274 * @registration: registration key passed to the notification function
2275 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002276 *
2277 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002278 *
2279 * See the Unified Extensible Firmware Interface (UEFI) specification for
2280 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002281 *
Mario Six8fac2912018-07-10 08:40:17 +02002282 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002283 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002284static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002285 void *registration,
2286 void **protocol_interface)
2287{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002288 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002289 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002290
Rob Clark238f88c2017-09-13 18:05:41 -04002291 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002292
2293 if (!protocol || !protocol_interface)
2294 return EFI_EXIT(EFI_INVALID_PARAMETER);
2295
2296 list_for_each(lhandle, &efi_obj_list) {
2297 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002298 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002299
2300 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002301
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002302 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002303 if (ret == EFI_SUCCESS) {
2304 *protocol_interface = handler->protocol_interface;
2305 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002306 }
2307 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002308 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002309
2310 return EFI_EXIT(EFI_NOT_FOUND);
2311}
2312
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002313/**
Mario Six8fac2912018-07-10 08:40:17 +02002314 * efi_locate_device_path() - Get the device path and handle of an device
2315 * implementing a protocol
2316 * @protocol: GUID of the protocol
2317 * @device_path: device path
2318 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002319 *
2320 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002321 *
Mario Six8fac2912018-07-10 08:40:17 +02002322 * See the Unified Extensible Firmware Interface (UEFI) specification for
2323 * details.
2324 *
2325 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002326 */
2327static efi_status_t EFIAPI efi_locate_device_path(
2328 const efi_guid_t *protocol,
2329 struct efi_device_path **device_path,
2330 efi_handle_t *device)
2331{
2332 struct efi_device_path *dp;
2333 size_t i;
2334 struct efi_handler *handler;
2335 efi_handle_t *handles;
2336 size_t len, len_dp;
2337 size_t len_best = 0;
2338 efi_uintn_t no_handles;
2339 u8 *remainder;
2340 efi_status_t ret;
2341
2342 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2343
2344 if (!protocol || !device_path || !*device_path || !device) {
2345 ret = EFI_INVALID_PARAMETER;
2346 goto out;
2347 }
2348
2349 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002350 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002351
2352 /* Get all handles implementing the protocol */
2353 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2354 &no_handles, &handles));
2355 if (ret != EFI_SUCCESS)
2356 goto out;
2357
2358 for (i = 0; i < no_handles; ++i) {
2359 /* Find the device path protocol */
2360 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2361 &handler);
2362 if (ret != EFI_SUCCESS)
2363 continue;
2364 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002365 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002366 /*
2367 * This handle can only be a better fit
2368 * if its device path length is longer than the best fit and
2369 * if its device path length is shorter of equal the searched
2370 * device path.
2371 */
2372 if (len_dp <= len_best || len_dp > len)
2373 continue;
2374 /* Check if dp is a subpath of device_path */
2375 if (memcmp(*device_path, dp, len_dp))
2376 continue;
2377 *device = handles[i];
2378 len_best = len_dp;
2379 }
2380 if (len_best) {
2381 remainder = (u8 *)*device_path + len_best;
2382 *device_path = (struct efi_device_path *)remainder;
2383 ret = EFI_SUCCESS;
2384 } else {
2385 ret = EFI_NOT_FOUND;
2386 }
2387out:
2388 return EFI_EXIT(ret);
2389}
2390
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002391/**
Mario Six8fac2912018-07-10 08:40:17 +02002392 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2393 * interfaces
2394 * @handle: handle on which the protocol interfaces shall be installed
2395 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2396 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002397 *
2398 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002399 *
2400 * See the Unified Extensible Firmware Interface (UEFI) specification for
2401 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002402 *
Mario Six8fac2912018-07-10 08:40:17 +02002403 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002404 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002405static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2406 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002407{
2408 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002409
Alexander Graf404a7c82018-06-18 17:23:05 +02002410 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002411 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002412 void *protocol_interface;
2413 efi_status_t r = EFI_SUCCESS;
2414 int i = 0;
2415
2416 if (!handle)
2417 return EFI_EXIT(EFI_INVALID_PARAMETER);
2418
Alexander Graf404a7c82018-06-18 17:23:05 +02002419 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002420 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002421 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002422 if (!protocol)
2423 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002424 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002425 r = EFI_CALL(efi_install_protocol_interface(
2426 handle, protocol,
2427 EFI_NATIVE_INTERFACE,
2428 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002429 if (r != EFI_SUCCESS)
2430 break;
2431 i++;
2432 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002433 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002434 if (r == EFI_SUCCESS)
2435 return EFI_EXIT(r);
2436
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002437 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002438 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002439 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002440 protocol = efi_va_arg(argptr, efi_guid_t*);
2441 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002442 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002443 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002444 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002445 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002446
2447 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002448}
2449
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002450/**
Mario Six8fac2912018-07-10 08:40:17 +02002451 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2452 * interfaces
2453 * @handle: handle from which the protocol interfaces shall be removed
2454 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2455 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002456 *
2457 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002458 *
Mario Six8fac2912018-07-10 08:40:17 +02002459 * See the Unified Extensible Firmware Interface (UEFI) specification for
2460 * details.
2461 *
2462 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002463 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002464static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002465 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002466{
2467 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002468
Alexander Graf404a7c82018-06-18 17:23:05 +02002469 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002470 const efi_guid_t *protocol;
2471 void *protocol_interface;
2472 efi_status_t r = EFI_SUCCESS;
2473 size_t i = 0;
2474
2475 if (!handle)
2476 return EFI_EXIT(EFI_INVALID_PARAMETER);
2477
Alexander Graf404a7c82018-06-18 17:23:05 +02002478 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002479 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002480 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002481 if (!protocol)
2482 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002483 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002484 r = efi_uninstall_protocol(handle, protocol,
2485 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002486 if (r != EFI_SUCCESS)
2487 break;
2488 i++;
2489 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002490 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002491 if (r == EFI_SUCCESS) {
2492 /* If the last protocol has been removed, delete the handle. */
2493 if (list_empty(&handle->protocols)) {
2494 list_del(&handle->link);
2495 free(handle);
2496 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002497 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002498 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002499
2500 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002501 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002502 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002503 protocol = efi_va_arg(argptr, efi_guid_t*);
2504 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002505 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2506 EFI_NATIVE_INTERFACE,
2507 protocol_interface));
2508 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002509 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002510
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002511 /* In case of an error always return EFI_INVALID_PARAMETER */
2512 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002513}
2514
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002515/**
Mario Six8fac2912018-07-10 08:40:17 +02002516 * efi_calculate_crc32() - calculate cyclic redundancy code
2517 * @data: buffer with data
2518 * @data_size: size of buffer in bytes
2519 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002520 *
2521 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002522 *
2523 * See the Unified Extensible Firmware Interface (UEFI) specification for
2524 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002525 *
Mario Six8fac2912018-07-10 08:40:17 +02002526 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002527 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002528static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2529 efi_uintn_t data_size,
2530 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002531{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002532 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002533 *crc32_p = crc32(0, data, data_size);
2534 return EFI_EXIT(EFI_SUCCESS);
2535}
2536
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002537/**
Mario Six8fac2912018-07-10 08:40:17 +02002538 * efi_copy_mem() - copy memory
2539 * @destination: destination of the copy operation
2540 * @source: source of the copy operation
2541 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002542 *
2543 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002544 *
Mario Six8fac2912018-07-10 08:40:17 +02002545 * See the Unified Extensible Firmware Interface (UEFI) specification for
2546 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002547 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002548static void EFIAPI efi_copy_mem(void *destination, const void *source,
2549 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002550{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002551 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002552 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002553 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002554}
2555
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002556/**
Mario Six8fac2912018-07-10 08:40:17 +02002557 * efi_set_mem() - Fill memory with a byte value.
2558 * @buffer: buffer to fill
2559 * @size: size of buffer in bytes
2560 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002561 *
2562 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002563 *
Mario Six8fac2912018-07-10 08:40:17 +02002564 * See the Unified Extensible Firmware Interface (UEFI) specification for
2565 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002566 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002567static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002568{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002569 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002570 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002571 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002572}
2573
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002574/**
Mario Six8fac2912018-07-10 08:40:17 +02002575 * efi_protocol_open() - open protocol interface on a handle
2576 * @handler: handler of a protocol
2577 * @protocol_interface: interface implementing the protocol
2578 * @agent_handle: handle of the driver
2579 * @controller_handle: handle of the controller
2580 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002581 *
Mario Six8fac2912018-07-10 08:40:17 +02002582 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002583 */
2584static efi_status_t efi_protocol_open(
2585 struct efi_handler *handler,
2586 void **protocol_interface, void *agent_handle,
2587 void *controller_handle, uint32_t attributes)
2588{
2589 struct efi_open_protocol_info_item *item;
2590 struct efi_open_protocol_info_entry *match = NULL;
2591 bool opened_by_driver = false;
2592 bool opened_exclusive = false;
2593
2594 /* If there is no agent, only return the interface */
2595 if (!agent_handle)
2596 goto out;
2597
2598 /* For TEST_PROTOCOL ignore interface attribute */
2599 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2600 *protocol_interface = NULL;
2601
2602 /*
2603 * Check if the protocol is already opened by a driver with the same
2604 * attributes or opened exclusively
2605 */
2606 list_for_each_entry(item, &handler->open_infos, link) {
2607 if (item->info.agent_handle == agent_handle) {
2608 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2609 (item->info.attributes == attributes))
2610 return EFI_ALREADY_STARTED;
2611 }
2612 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2613 opened_exclusive = true;
2614 }
2615
2616 /* Only one controller can open the protocol exclusively */
2617 if (opened_exclusive && attributes &
2618 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2619 return EFI_ACCESS_DENIED;
2620
2621 /* Prepare exclusive opening */
2622 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2623 /* Try to disconnect controllers */
2624 list_for_each_entry(item, &handler->open_infos, link) {
2625 if (item->info.attributes ==
2626 EFI_OPEN_PROTOCOL_BY_DRIVER)
2627 EFI_CALL(efi_disconnect_controller(
2628 item->info.controller_handle,
2629 item->info.agent_handle,
2630 NULL));
2631 }
2632 opened_by_driver = false;
2633 /* Check if all controllers are disconnected */
2634 list_for_each_entry(item, &handler->open_infos, link) {
2635 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2636 opened_by_driver = true;
2637 }
Heinrich Schuchardt8d0b45d2018-09-30 13:40:43 +02002638 /* Only one controller can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002639 if (opened_by_driver)
2640 return EFI_ACCESS_DENIED;
2641 }
2642
2643 /* Find existing entry */
2644 list_for_each_entry(item, &handler->open_infos, link) {
2645 if (item->info.agent_handle == agent_handle &&
2646 item->info.controller_handle == controller_handle)
2647 match = &item->info;
2648 }
2649 /* None found, create one */
2650 if (!match) {
2651 match = efi_create_open_info(handler);
2652 if (!match)
2653 return EFI_OUT_OF_RESOURCES;
2654 }
2655
2656 match->agent_handle = agent_handle;
2657 match->controller_handle = controller_handle;
2658 match->attributes = attributes;
2659 match->open_count++;
2660
2661out:
2662 /* For TEST_PROTOCOL ignore interface attribute. */
2663 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2664 *protocol_interface = handler->protocol_interface;
2665
2666 return EFI_SUCCESS;
2667}
2668
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002669/**
Mario Six8fac2912018-07-10 08:40:17 +02002670 * efi_open_protocol() - open protocol interface on a handle
2671 * @handle: handle on which the protocol shall be opened
2672 * @protocol: GUID of the protocol
2673 * @protocol_interface: interface implementing the protocol
2674 * @agent_handle: handle of the driver
2675 * @controller_handle: handle of the controller
2676 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002677 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002678 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002679 *
Mario Six8fac2912018-07-10 08:40:17 +02002680 * See the Unified Extensible Firmware Interface (UEFI) specification for
2681 * details.
2682 *
2683 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002684 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002685static efi_status_t EFIAPI efi_open_protocol
2686 (efi_handle_t handle, const efi_guid_t *protocol,
2687 void **protocol_interface, efi_handle_t agent_handle,
2688 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002689{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002690 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002691 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002692
Rob Clark238f88c2017-09-13 18:05:41 -04002693 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002694 protocol_interface, agent_handle, controller_handle,
2695 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002696
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002697 if (!handle || !protocol ||
2698 (!protocol_interface && attributes !=
2699 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002700 goto out;
2701 }
2702
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002703 switch (attributes) {
2704 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2705 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2706 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2707 break;
2708 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2709 if (controller_handle == handle)
2710 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002711 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002712 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2713 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002714 /* Check that the controller handle is valid */
2715 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002716 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002717 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002718 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002719 /* Check that the agent handle is valid */
2720 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002721 goto out;
2722 break;
2723 default:
2724 goto out;
2725 }
2726
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002727 r = efi_search_protocol(handle, protocol, &handler);
2728 if (r != EFI_SUCCESS)
2729 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002730
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002731 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2732 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002733out:
2734 return EFI_EXIT(r);
2735}
2736
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002737/**
Mario Six8fac2912018-07-10 08:40:17 +02002738 * efi_handle_protocol() - get interface of a protocol on a handle
2739 * @handle: handle on which the protocol shall be opened
2740 * @protocol: GUID of the protocol
2741 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002742 *
2743 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002744 *
2745 * See the Unified Extensible Firmware Interface (UEFI) specification for
2746 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002747 *
Mario Six8fac2912018-07-10 08:40:17 +02002748 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002749 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002750static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002751 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002752 void **protocol_interface)
2753{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002754 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2755 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002756}
2757
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002758/**
Mario Six8fac2912018-07-10 08:40:17 +02002759 * efi_bind_controller() - bind a single driver to a controller
2760 * @controller_handle: controller handle
2761 * @driver_image_handle: driver handle
2762 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002763 *
Mario Six8fac2912018-07-10 08:40:17 +02002764 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002765 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002766static efi_status_t efi_bind_controller(
2767 efi_handle_t controller_handle,
2768 efi_handle_t driver_image_handle,
2769 struct efi_device_path *remain_device_path)
2770{
2771 struct efi_driver_binding_protocol *binding_protocol;
2772 efi_status_t r;
2773
2774 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2775 &efi_guid_driver_binding_protocol,
2776 (void **)&binding_protocol,
2777 driver_image_handle, NULL,
2778 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2779 if (r != EFI_SUCCESS)
2780 return r;
2781 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2782 controller_handle,
2783 remain_device_path));
2784 if (r == EFI_SUCCESS)
2785 r = EFI_CALL(binding_protocol->start(binding_protocol,
2786 controller_handle,
2787 remain_device_path));
2788 EFI_CALL(efi_close_protocol(driver_image_handle,
2789 &efi_guid_driver_binding_protocol,
2790 driver_image_handle, NULL));
2791 return r;
2792}
2793
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002794/**
Mario Six8fac2912018-07-10 08:40:17 +02002795 * efi_connect_single_controller() - connect a single driver to a controller
2796 * @controller_handle: controller
2797 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002798 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002799 *
Mario Six8fac2912018-07-10 08:40:17 +02002800 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002801 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002802static efi_status_t efi_connect_single_controller(
2803 efi_handle_t controller_handle,
2804 efi_handle_t *driver_image_handle,
2805 struct efi_device_path *remain_device_path)
2806{
2807 efi_handle_t *buffer;
2808 size_t count;
2809 size_t i;
2810 efi_status_t r;
2811 size_t connected = 0;
2812
2813 /* Get buffer with all handles with driver binding protocol */
2814 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2815 &efi_guid_driver_binding_protocol,
2816 NULL, &count, &buffer));
2817 if (r != EFI_SUCCESS)
2818 return r;
2819
2820 /* Context Override */
2821 if (driver_image_handle) {
2822 for (; *driver_image_handle; ++driver_image_handle) {
2823 for (i = 0; i < count; ++i) {
2824 if (buffer[i] == *driver_image_handle) {
2825 buffer[i] = NULL;
2826 r = efi_bind_controller(
2827 controller_handle,
2828 *driver_image_handle,
2829 remain_device_path);
2830 /*
2831 * For drivers that do not support the
2832 * controller or are already connected
2833 * we receive an error code here.
2834 */
2835 if (r == EFI_SUCCESS)
2836 ++connected;
2837 }
2838 }
2839 }
2840 }
2841
2842 /*
2843 * TODO: Some overrides are not yet implemented:
2844 * - Platform Driver Override
2845 * - Driver Family Override Search
2846 * - Bus Specific Driver Override
2847 */
2848
2849 /* Driver Binding Search */
2850 for (i = 0; i < count; ++i) {
2851 if (buffer[i]) {
2852 r = efi_bind_controller(controller_handle,
2853 buffer[i],
2854 remain_device_path);
2855 if (r == EFI_SUCCESS)
2856 ++connected;
2857 }
2858 }
2859
2860 efi_free_pool(buffer);
2861 if (!connected)
2862 return EFI_NOT_FOUND;
2863 return EFI_SUCCESS;
2864}
2865
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002866/**
Mario Six8fac2912018-07-10 08:40:17 +02002867 * efi_connect_controller() - connect a controller to a driver
2868 * @controller_handle: handle of the controller
2869 * @driver_image_handle: handle of the driver
2870 * @remain_device_path: device path of a child controller
2871 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002872 *
2873 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002874 *
2875 * See the Unified Extensible Firmware Interface (UEFI) specification for
2876 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002877 *
2878 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002879 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002880 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2881 *
Mario Six8fac2912018-07-10 08:40:17 +02002882 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002883 */
2884static efi_status_t EFIAPI efi_connect_controller(
2885 efi_handle_t controller_handle,
2886 efi_handle_t *driver_image_handle,
2887 struct efi_device_path *remain_device_path,
2888 bool recursive)
2889{
2890 efi_status_t r;
2891 efi_status_t ret = EFI_NOT_FOUND;
2892 struct efi_object *efiobj;
2893
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01002894 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002895 remain_device_path, recursive);
2896
2897 efiobj = efi_search_obj(controller_handle);
2898 if (!efiobj) {
2899 ret = EFI_INVALID_PARAMETER;
2900 goto out;
2901 }
2902
2903 r = efi_connect_single_controller(controller_handle,
2904 driver_image_handle,
2905 remain_device_path);
2906 if (r == EFI_SUCCESS)
2907 ret = EFI_SUCCESS;
2908 if (recursive) {
2909 struct efi_handler *handler;
2910 struct efi_open_protocol_info_item *item;
2911
2912 list_for_each_entry(handler, &efiobj->protocols, link) {
2913 list_for_each_entry(item, &handler->open_infos, link) {
2914 if (item->info.attributes &
2915 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2916 r = EFI_CALL(efi_connect_controller(
2917 item->info.controller_handle,
2918 driver_image_handle,
2919 remain_device_path,
2920 recursive));
2921 if (r == EFI_SUCCESS)
2922 ret = EFI_SUCCESS;
2923 }
2924 }
2925 }
2926 }
2927 /* Check for child controller specified by end node */
2928 if (ret != EFI_SUCCESS && remain_device_path &&
2929 remain_device_path->type == DEVICE_PATH_TYPE_END)
2930 ret = EFI_SUCCESS;
2931out:
2932 return EFI_EXIT(ret);
2933}
2934
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002935/**
Mario Six8fac2912018-07-10 08:40:17 +02002936 * efi_reinstall_protocol_interface() - reinstall protocol interface
2937 * @handle: handle on which the protocol shall be reinstalled
2938 * @protocol: GUID of the protocol to be installed
2939 * @old_interface: interface to be removed
2940 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002941 *
2942 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002943 *
2944 * See the Unified Extensible Firmware Interface (UEFI) specification for
2945 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002946 *
2947 * The old interface is uninstalled. The new interface is installed.
2948 * Drivers are connected.
2949 *
Mario Six8fac2912018-07-10 08:40:17 +02002950 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002951 */
2952static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2953 efi_handle_t handle, const efi_guid_t *protocol,
2954 void *old_interface, void *new_interface)
2955{
2956 efi_status_t ret;
2957
2958 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2959 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002960
2961 /* Uninstall protocol but do not delete handle */
2962 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002963 if (ret != EFI_SUCCESS)
2964 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002965
2966 /* Install the new protocol */
2967 ret = efi_add_protocol(handle, protocol, new_interface);
2968 /*
2969 * The UEFI spec does not specify what should happen to the handle
2970 * if in case of an error no protocol interface remains on the handle.
2971 * So let's do nothing here.
2972 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002973 if (ret != EFI_SUCCESS)
2974 goto out;
2975 /*
2976 * The returned status code has to be ignored.
2977 * Do not create an error if no suitable driver for the handle exists.
2978 */
2979 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2980out:
2981 return EFI_EXIT(ret);
2982}
2983
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002984/**
Mario Six8fac2912018-07-10 08:40:17 +02002985 * efi_get_child_controllers() - get all child controllers associated to a driver
2986 * @efiobj: handle of the controller
2987 * @driver_handle: handle of the driver
2988 * @number_of_children: number of child controllers
2989 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002990 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002991 * The allocated buffer has to be freed with free().
2992 *
Mario Six8fac2912018-07-10 08:40:17 +02002993 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002994 */
2995static efi_status_t efi_get_child_controllers(
2996 struct efi_object *efiobj,
2997 efi_handle_t driver_handle,
2998 efi_uintn_t *number_of_children,
2999 efi_handle_t **child_handle_buffer)
3000{
3001 struct efi_handler *handler;
3002 struct efi_open_protocol_info_item *item;
3003 efi_uintn_t count = 0, i;
3004 bool duplicate;
3005
3006 /* Count all child controller associations */
3007 list_for_each_entry(handler, &efiobj->protocols, link) {
3008 list_for_each_entry(item, &handler->open_infos, link) {
3009 if (item->info.agent_handle == driver_handle &&
3010 item->info.attributes &
3011 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3012 ++count;
3013 }
3014 }
3015 /*
3016 * Create buffer. In case of duplicate child controller assignments
3017 * the buffer will be too large. But that does not harm.
3018 */
3019 *number_of_children = 0;
3020 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3021 if (!*child_handle_buffer)
3022 return EFI_OUT_OF_RESOURCES;
3023 /* Copy unique child handles */
3024 list_for_each_entry(handler, &efiobj->protocols, link) {
3025 list_for_each_entry(item, &handler->open_infos, link) {
3026 if (item->info.agent_handle == driver_handle &&
3027 item->info.attributes &
3028 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3029 /* Check this is a new child controller */
3030 duplicate = false;
3031 for (i = 0; i < *number_of_children; ++i) {
3032 if ((*child_handle_buffer)[i] ==
3033 item->info.controller_handle)
3034 duplicate = true;
3035 }
3036 /* Copy handle to buffer */
3037 if (!duplicate) {
3038 i = (*number_of_children)++;
3039 (*child_handle_buffer)[i] =
3040 item->info.controller_handle;
3041 }
3042 }
3043 }
3044 }
3045 return EFI_SUCCESS;
3046}
3047
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003048/**
Mario Six8fac2912018-07-10 08:40:17 +02003049 * efi_disconnect_controller() - disconnect a controller from a driver
3050 * @controller_handle: handle of the controller
3051 * @driver_image_handle: handle of the driver
3052 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003053 *
3054 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003055 *
3056 * See the Unified Extensible Firmware Interface (UEFI) specification for
3057 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003058 *
Mario Six8fac2912018-07-10 08:40:17 +02003059 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003060 */
3061static efi_status_t EFIAPI efi_disconnect_controller(
3062 efi_handle_t controller_handle,
3063 efi_handle_t driver_image_handle,
3064 efi_handle_t child_handle)
3065{
3066 struct efi_driver_binding_protocol *binding_protocol;
3067 efi_handle_t *child_handle_buffer = NULL;
3068 size_t number_of_children = 0;
3069 efi_status_t r;
3070 size_t stop_count = 0;
3071 struct efi_object *efiobj;
3072
3073 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3074 child_handle);
3075
3076 efiobj = efi_search_obj(controller_handle);
3077 if (!efiobj) {
3078 r = EFI_INVALID_PARAMETER;
3079 goto out;
3080 }
3081
3082 if (child_handle && !efi_search_obj(child_handle)) {
3083 r = EFI_INVALID_PARAMETER;
3084 goto out;
3085 }
3086
3087 /* If no driver handle is supplied, disconnect all drivers */
3088 if (!driver_image_handle) {
3089 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3090 goto out;
3091 }
3092
3093 /* Create list of child handles */
3094 if (child_handle) {
3095 number_of_children = 1;
3096 child_handle_buffer = &child_handle;
3097 } else {
3098 efi_get_child_controllers(efiobj,
3099 driver_image_handle,
3100 &number_of_children,
3101 &child_handle_buffer);
3102 }
3103
3104 /* Get the driver binding protocol */
3105 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3106 &efi_guid_driver_binding_protocol,
3107 (void **)&binding_protocol,
3108 driver_image_handle, NULL,
3109 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3110 if (r != EFI_SUCCESS)
3111 goto out;
3112 /* Remove the children */
3113 if (number_of_children) {
3114 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3115 controller_handle,
3116 number_of_children,
3117 child_handle_buffer));
3118 if (r == EFI_SUCCESS)
3119 ++stop_count;
3120 }
3121 /* Remove the driver */
3122 if (!child_handle)
3123 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3124 controller_handle,
3125 0, NULL));
3126 if (r == EFI_SUCCESS)
3127 ++stop_count;
3128 EFI_CALL(efi_close_protocol(driver_image_handle,
3129 &efi_guid_driver_binding_protocol,
3130 driver_image_handle, NULL));
3131
3132 if (stop_count)
3133 r = EFI_SUCCESS;
3134 else
3135 r = EFI_NOT_FOUND;
3136out:
3137 if (!child_handle)
3138 free(child_handle_buffer);
3139 return EFI_EXIT(r);
3140}
3141
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003142static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003143 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003144 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3145 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003146 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003147 },
3148 .raise_tpl = efi_raise_tpl,
3149 .restore_tpl = efi_restore_tpl,
3150 .allocate_pages = efi_allocate_pages_ext,
3151 .free_pages = efi_free_pages_ext,
3152 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003153 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003154 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003155 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003156 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003157 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003158 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003159 .close_event = efi_close_event,
3160 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003161 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003162 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003163 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003164 .handle_protocol = efi_handle_protocol,
3165 .reserved = NULL,
3166 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003167 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003168 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003169 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003170 .load_image = efi_load_image,
3171 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003172 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003173 .unload_image = efi_unload_image,
3174 .exit_boot_services = efi_exit_boot_services,
3175 .get_next_monotonic_count = efi_get_next_monotonic_count,
3176 .stall = efi_stall,
3177 .set_watchdog_timer = efi_set_watchdog_timer,
3178 .connect_controller = efi_connect_controller,
3179 .disconnect_controller = efi_disconnect_controller,
3180 .open_protocol = efi_open_protocol,
3181 .close_protocol = efi_close_protocol,
3182 .open_protocol_information = efi_open_protocol_information,
3183 .protocols_per_handle = efi_protocols_per_handle,
3184 .locate_handle_buffer = efi_locate_handle_buffer,
3185 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003186 .install_multiple_protocol_interfaces =
3187 efi_install_multiple_protocol_interfaces,
3188 .uninstall_multiple_protocol_interfaces =
3189 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003190 .calculate_crc32 = efi_calculate_crc32,
3191 .copy_mem = efi_copy_mem,
3192 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003193 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003194};
3195
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003196static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003197
Alexander Graf393dd912016-10-14 13:45:30 +02003198struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003199 .hdr = {
3200 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003201 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003202 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003203 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003204 .fw_vendor = firmware_vendor,
3205 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003206 .con_in = (void *)&efi_con_in,
3207 .con_out = (void *)&efi_con_out,
3208 .std_err = (void *)&efi_con_out,
3209 .runtime = (void *)&efi_runtime_services,
3210 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003211 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003212 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003213};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003214
3215/**
3216 * efi_initialize_system_table() - Initialize system table
3217 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003218 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003219 */
3220efi_status_t efi_initialize_system_table(void)
3221{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003222 efi_status_t ret;
3223
3224 /* Allocate configuration table array */
3225 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3226 EFI_MAX_CONFIGURATION_TABLES *
3227 sizeof(struct efi_configuration_table),
3228 (void **)&systab.tables);
3229
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003230 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003231 efi_update_table_header_crc32(&systab.hdr);
3232 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3233 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003234
3235 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003236}