blob: 493d906c641d1972ec7700398ac59a56f6765473 [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/*
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003 * EFI application boot time services
Alexander Grafc15d9212016-03-04 01:09:59 +01004 *
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02005 * 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>
11#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <u-boot/crc.h>
14#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020015#include <pe.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 Schuchardt4429d872019-07-11 20:15:09 +020027__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010028
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020029/* List of queued events */
30LIST_HEAD(efi_event_queue);
31
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020032/* Flag to disable timer activity in ExitBootServices() */
33static bool timers_enabled = true;
34
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020035/* List of all events registered by RegisterProtocolNotify() */
36LIST_HEAD(efi_register_notify_events);
37
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010038/* Handle of the currently executing image */
39static efi_handle_t current_image;
40
Simon Glasscdfe6962016-09-25 15:27:35 -060041#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010042/*
43 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
44 * fixed when compiling U-Boot. However, the payload does not know about that
45 * restriction so we need to manually swap its and our view of that register on
46 * EFI callback entry/exit.
47 */
48static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060049#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010050
Heinrich Schuchardt72506232019-02-09 14:10:39 +010051/* 1 if inside U-Boot code, 0 if inside EFI payload code */
52static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040053static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010054/* GUID of the device tree table */
55const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010056/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
57const efi_guid_t efi_guid_driver_binding_protocol =
58 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040059
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010060/* event group ExitBootServices() invoked */
61const efi_guid_t efi_guid_event_group_exit_boot_services =
62 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
63/* event group SetVirtualAddressMap() invoked */
64const efi_guid_t efi_guid_event_group_virtual_address_change =
65 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
66/* event group memory map changed */
67const efi_guid_t efi_guid_event_group_memory_map_change =
68 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
69/* event group boot manager about to boot */
70const efi_guid_t efi_guid_event_group_ready_to_boot =
71 EFI_EVENT_GROUP_READY_TO_BOOT;
72/* event group ResetSystem() invoked (before ExitBootServices) */
73const efi_guid_t efi_guid_event_group_reset_system =
74 EFI_EVENT_GROUP_RESET_SYSTEM;
75
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010076static efi_status_t EFIAPI efi_disconnect_controller(
77 efi_handle_t controller_handle,
78 efi_handle_t driver_image_handle,
79 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010080
Rob Clark86789d52017-07-27 08:04:18 -040081/* Called on every callback entry */
82int __efi_entry_check(void)
83{
84 int ret = entry_count++ == 0;
85#ifdef CONFIG_ARM
86 assert(efi_gd);
87 app_gd = gd;
88 gd = efi_gd;
89#endif
90 return ret;
91}
92
93/* Called on every callback exit */
94int __efi_exit_check(void)
95{
96 int ret = --entry_count == 0;
97#ifdef CONFIG_ARM
98 gd = app_gd;
99#endif
100 return ret;
101}
102
Alexander Grafc15d9212016-03-04 01:09:59 +0100103/* Called from do_bootefi_exec() */
104void efi_save_gd(void)
105{
Simon Glasscdfe6962016-09-25 15:27:35 -0600106#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100107 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600108#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100109}
110
Rob Clark86789d52017-07-27 08:04:18 -0400111/*
Mario Six8fac2912018-07-10 08:40:17 +0200112 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200113 * world so we can dump out an abort message, without any care about returning
114 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400115 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100116void efi_restore_gd(void)
117{
Simon Glasscdfe6962016-09-25 15:27:35 -0600118#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100119 /* Only restore if we're already in EFI context */
120 if (!efi_gd)
121 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100122 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600123#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100124}
125
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200126/**
Mario Six8fac2912018-07-10 08:40:17 +0200127 * indent_string() - returns a string for indenting with two spaces per level
128 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100129 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200130 * A maximum of ten indent levels is supported. Higher indent levels will be
131 * truncated.
132 *
Mario Six8fac2912018-07-10 08:40:17 +0200133 * Return: A string for indenting with two spaces per level is
134 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400135 */
136static const char *indent_string(int level)
137{
138 const char *indent = " ";
139 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100140
Rob Clarke7896c32017-07-27 08:04:19 -0400141 level = min(max, level * 2);
142 return &indent[max - level];
143}
144
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200145const char *__efi_nesting(void)
146{
147 return indent_string(nesting_level);
148}
149
Rob Clarke7896c32017-07-27 08:04:19 -0400150const char *__efi_nesting_inc(void)
151{
152 return indent_string(nesting_level++);
153}
154
155const char *__efi_nesting_dec(void)
156{
157 return indent_string(--nesting_level);
158}
159
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200160/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200161 * efi_event_is_queued() - check if an event is queued
162 *
163 * @event: event
164 * Return: true if event is queued
165 */
166static bool efi_event_is_queued(struct efi_event *event)
167{
168 return !!event->queue_link.next;
169}
170
171/**
172 * efi_process_event_queue() - process event queue
173 */
174static void efi_process_event_queue(void)
175{
176 while (!list_empty(&efi_event_queue)) {
177 struct efi_event *event;
178 efi_uintn_t old_tpl;
179
180 event = list_first_entry(&efi_event_queue, struct efi_event,
181 queue_link);
182 if (efi_tpl >= event->notify_tpl)
183 return;
184 list_del(&event->queue_link);
185 event->queue_link.next = NULL;
186 event->queue_link.prev = NULL;
187 /* Events must be executed at the event's TPL */
188 old_tpl = efi_tpl;
189 efi_tpl = event->notify_tpl;
190 EFI_CALL_VOID(event->notify_function(event,
191 event->notify_context));
192 efi_tpl = old_tpl;
193 if (event->type == EVT_NOTIFY_SIGNAL)
194 event->is_signaled = 0;
195 }
196}
197
198/**
Mario Six8fac2912018-07-10 08:40:17 +0200199 * efi_queue_event() - queue an EFI event
200 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200201 *
202 * This function queues the notification function of the event for future
203 * execution.
204 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200205 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200206static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200207{
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200208 struct efi_event *item = NULL;
209
210 if (!event->notify_function)
211 return;
212
213 if (!efi_event_is_queued(event)) {
214 /*
215 * Events must be notified in order of decreasing task priority
216 * level. Insert the new event accordingly.
217 */
218 list_for_each_entry(item, &efi_event_queue, queue_link) {
219 if (item->notify_tpl < event->notify_tpl) {
220 list_add_tail(&event->queue_link,
221 &item->queue_link);
222 event = NULL;
223 break;
224 }
225 }
226 if (event)
227 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200228 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200229 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200230}
231
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200232/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200233 * is_valid_tpl() - check if the task priority level is valid
234 *
235 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200236 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200237 */
238efi_status_t is_valid_tpl(efi_uintn_t tpl)
239{
240 switch (tpl) {
241 case TPL_APPLICATION:
242 case TPL_CALLBACK:
243 case TPL_NOTIFY:
244 case TPL_HIGH_LEVEL:
245 return EFI_SUCCESS;
246 default:
247 return EFI_INVALID_PARAMETER;
248 }
249}
250
251/**
Mario Six8fac2912018-07-10 08:40:17 +0200252 * efi_signal_event() - signal an EFI event
253 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100254 *
Mario Six8fac2912018-07-10 08:40:17 +0200255 * This function signals an event. If the event belongs to an event group all
256 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100257 * their notification function is queued.
258 *
259 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100260 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200261void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100262{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200263 if (event->is_signaled)
264 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100265 if (event->group) {
266 struct efi_event *evt;
267
268 /*
269 * The signaled state has to set before executing any
270 * notification function
271 */
272 list_for_each_entry(evt, &efi_events, link) {
273 if (!evt->group || guidcmp(evt->group, event->group))
274 continue;
275 if (evt->is_signaled)
276 continue;
277 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100278 }
279 list_for_each_entry(evt, &efi_events, link) {
280 if (!evt->group || guidcmp(evt->group, event->group))
281 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200282 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100283 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200284 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100285 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200286 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100287 }
288}
289
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200290/**
Mario Six8fac2912018-07-10 08:40:17 +0200291 * efi_raise_tpl() - raise the task priority level
292 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200293 *
294 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200295 *
296 * See the Unified Extensible Firmware Interface (UEFI) specification for
297 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200298 *
Mario Six8fac2912018-07-10 08:40:17 +0200299 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200300 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100301static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100302{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100303 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200304
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200305 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200306
307 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200308 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200309 efi_tpl = new_tpl;
310 if (efi_tpl > TPL_HIGH_LEVEL)
311 efi_tpl = TPL_HIGH_LEVEL;
312
313 EFI_EXIT(EFI_SUCCESS);
314 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100315}
316
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200317/**
Mario Six8fac2912018-07-10 08:40:17 +0200318 * efi_restore_tpl() - lower the task priority level
319 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200320 *
321 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200322 *
Mario Six8fac2912018-07-10 08:40:17 +0200323 * See the Unified Extensible Firmware Interface (UEFI) specification for
324 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200325 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100326static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100327{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200328 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200329
330 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200331 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200332 efi_tpl = old_tpl;
333 if (efi_tpl > TPL_HIGH_LEVEL)
334 efi_tpl = TPL_HIGH_LEVEL;
335
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100336 /*
337 * Lowering the TPL may have made queued events eligible for execution.
338 */
339 efi_timer_check();
340
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200341 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100342}
343
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200344/**
Mario Six8fac2912018-07-10 08:40:17 +0200345 * efi_allocate_pages_ext() - allocate memory pages
346 * @type: type of allocation to be performed
347 * @memory_type: usage type of the allocated memory
348 * @pages: number of pages to be allocated
349 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200350 *
351 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200352 *
Mario Six8fac2912018-07-10 08:40:17 +0200353 * See the Unified Extensible Firmware Interface (UEFI) specification for
354 * details.
355 *
356 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200357 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900358static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100359 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900360 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100361{
362 efi_status_t r;
363
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100364 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100365 r = efi_allocate_pages(type, memory_type, pages, memory);
366 return EFI_EXIT(r);
367}
368
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200369/**
Mario Six8fac2912018-07-10 08:40:17 +0200370 * efi_free_pages_ext() - Free memory pages.
371 * @memory: start of the memory area to be freed
372 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 *
374 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200375 *
376 * See the Unified Extensible Firmware Interface (UEFI) specification for
377 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200378 *
Mario Six8fac2912018-07-10 08:40:17 +0200379 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200380 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900381static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100382 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100383{
384 efi_status_t r;
385
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900386 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100387 r = efi_free_pages(memory, pages);
388 return EFI_EXIT(r);
389}
390
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200391/**
Mario Six8fac2912018-07-10 08:40:17 +0200392 * efi_get_memory_map_ext() - get map describing memory usage
393 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
394 * on exit the size of the copied memory map
395 * @memory_map: buffer to which the memory map is written
396 * @map_key: key for the memory map
397 * @descriptor_size: size of an individual memory descriptor
398 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200399 *
400 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200401 *
Mario Six8fac2912018-07-10 08:40:17 +0200402 * See the Unified Extensible Firmware Interface (UEFI) specification for
403 * details.
404 *
405 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200406 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900407static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100408 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900409 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100410 efi_uintn_t *map_key,
411 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900412 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100413{
414 efi_status_t r;
415
416 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
417 map_key, descriptor_size, descriptor_version);
418 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
419 descriptor_size, descriptor_version);
420 return EFI_EXIT(r);
421}
422
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200423/**
Mario Six8fac2912018-07-10 08:40:17 +0200424 * efi_allocate_pool_ext() - allocate memory from pool
425 * @pool_type: type of the pool from which memory is to be allocated
426 * @size: number of bytes to be allocated
427 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200428 *
429 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200430 *
431 * See the Unified Extensible Firmware Interface (UEFI) specification for
432 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200433 *
Mario Six8fac2912018-07-10 08:40:17 +0200434 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200435 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200436static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100437 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200438 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100439{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100440 efi_status_t r;
441
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100442 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200443 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100444 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100445}
446
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200447/**
Mario Six8fac2912018-07-10 08:40:17 +0200448 * efi_free_pool_ext() - free memory from pool
449 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200450 *
451 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200452 *
Mario Six8fac2912018-07-10 08:40:17 +0200453 * See the Unified Extensible Firmware Interface (UEFI) specification for
454 * details.
455 *
456 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200457 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200458static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100459{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100460 efi_status_t r;
461
462 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200463 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100464 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100465}
466
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200467/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200468 * efi_add_handle() - add a new handle to the object list
469 *
470 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100471 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200472 * The protocols list is initialized. The handle is added to the list of known
473 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100474 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200475void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100476{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200477 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100478 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200479 INIT_LIST_HEAD(&handle->protocols);
480 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100481}
482
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200483/**
Mario Six8fac2912018-07-10 08:40:17 +0200484 * efi_create_handle() - create handle
485 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200486 *
Mario Six8fac2912018-07-10 08:40:17 +0200487 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200488 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100489efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200490{
491 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200492
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200493 obj = calloc(1, sizeof(struct efi_object));
494 if (!obj)
495 return EFI_OUT_OF_RESOURCES;
496
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100497 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200498 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200499
500 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200501}
502
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200503/**
Mario Six8fac2912018-07-10 08:40:17 +0200504 * efi_search_protocol() - find a protocol on a handle.
505 * @handle: handle
506 * @protocol_guid: GUID of the protocol
507 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100508 *
Mario Six8fac2912018-07-10 08:40:17 +0200509 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100510 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100511efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100512 const efi_guid_t *protocol_guid,
513 struct efi_handler **handler)
514{
515 struct efi_object *efiobj;
516 struct list_head *lhandle;
517
518 if (!handle || !protocol_guid)
519 return EFI_INVALID_PARAMETER;
520 efiobj = efi_search_obj(handle);
521 if (!efiobj)
522 return EFI_INVALID_PARAMETER;
523 list_for_each(lhandle, &efiobj->protocols) {
524 struct efi_handler *protocol;
525
526 protocol = list_entry(lhandle, struct efi_handler, link);
527 if (!guidcmp(protocol->guid, protocol_guid)) {
528 if (handler)
529 *handler = protocol;
530 return EFI_SUCCESS;
531 }
532 }
533 return EFI_NOT_FOUND;
534}
535
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200536/**
Mario Six8fac2912018-07-10 08:40:17 +0200537 * efi_remove_protocol() - delete protocol from a handle
538 * @handle: handle from which the protocol shall be deleted
539 * @protocol: GUID of the protocol to be deleted
540 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100541 *
Mario Six8fac2912018-07-10 08:40:17 +0200542 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100543 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100544efi_status_t efi_remove_protocol(const efi_handle_t handle,
545 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 void *protocol_interface)
547{
548 struct efi_handler *handler;
549 efi_status_t ret;
550
551 ret = efi_search_protocol(handle, protocol, &handler);
552 if (ret != EFI_SUCCESS)
553 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200554 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200555 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100556 list_del(&handler->link);
557 free(handler);
558 return EFI_SUCCESS;
559}
560
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200561/**
Mario Six8fac2912018-07-10 08:40:17 +0200562 * efi_remove_all_protocols() - delete all protocols from a handle
563 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100564 *
Mario Six8fac2912018-07-10 08:40:17 +0200565 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100566 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100567efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100568{
569 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100570 struct efi_handler *protocol;
571 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100572
573 efiobj = efi_search_obj(handle);
574 if (!efiobj)
575 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100576 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100577 efi_status_t ret;
578
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100579 ret = efi_remove_protocol(handle, protocol->guid,
580 protocol->protocol_interface);
581 if (ret != EFI_SUCCESS)
582 return ret;
583 }
584 return EFI_SUCCESS;
585}
586
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200587/**
Mario Six8fac2912018-07-10 08:40:17 +0200588 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100589 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200590 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100591 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200592void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100593{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200594 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100595 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200596 efi_remove_all_protocols(handle);
597 list_del(&handle->link);
598 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599}
600
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200601/**
Mario Six8fac2912018-07-10 08:40:17 +0200602 * efi_is_event() - check if a pointer is a valid event
603 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100604 *
Mario Six8fac2912018-07-10 08:40:17 +0200605 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100606 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100607static efi_status_t efi_is_event(const struct efi_event *event)
608{
609 const struct efi_event *evt;
610
611 if (!event)
612 return EFI_INVALID_PARAMETER;
613 list_for_each_entry(evt, &efi_events, link) {
614 if (evt == event)
615 return EFI_SUCCESS;
616 }
617 return EFI_INVALID_PARAMETER;
618}
Alexander Grafc15d9212016-03-04 01:09:59 +0100619
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200620/**
Mario Six8fac2912018-07-10 08:40:17 +0200621 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200622 *
Mario Six8fac2912018-07-10 08:40:17 +0200623 * @type: type of the event to create
624 * @notify_tpl: task priority level of the event
625 * @notify_function: notification function of the event
626 * @notify_context: pointer passed to the notification function
627 * @group: event group
628 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200629 *
630 * This function is used inside U-Boot code to create an event.
631 *
632 * For the API function implementing the CreateEvent service see
633 * efi_create_event_ext.
634 *
Mario Six8fac2912018-07-10 08:40:17 +0200635 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200636 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100637efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200638 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200639 struct efi_event *event,
640 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100641 void *notify_context, efi_guid_t *group,
642 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100643{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100644 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200645 efi_status_t ret;
646 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200647
Jonathan Gray7758b212017-03-12 19:26:07 +1100648 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200649 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100650
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200651 switch (type) {
652 case 0:
653 case EVT_TIMER:
654 case EVT_NOTIFY_SIGNAL:
655 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
656 case EVT_NOTIFY_WAIT:
657 case EVT_TIMER | EVT_NOTIFY_WAIT:
658 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200659 pool_type = EFI_BOOT_SERVICES_DATA;
660 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200661 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200662 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200663 break;
664 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200665 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200666 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100667
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900668 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200669 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200670 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100671
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200672 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
673 (void **)&evt);
674 if (ret != EFI_SUCCESS)
675 return ret;
676 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100677 evt->type = type;
678 evt->notify_tpl = notify_tpl;
679 evt->notify_function = notify_function;
680 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100681 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200682 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100683 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100684 list_add_tail(&evt->link, &efi_events);
685 *event = evt;
686 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100687}
688
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200689/*
Mario Six8fac2912018-07-10 08:40:17 +0200690 * efi_create_event_ex() - create an event in a group
691 * @type: type of the event to create
692 * @notify_tpl: task priority level of the event
693 * @notify_function: notification function of the event
694 * @notify_context: pointer passed to the notification function
695 * @event: created event
696 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100697 *
698 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100699 *
Mario Six8fac2912018-07-10 08:40:17 +0200700 * See the Unified Extensible Firmware Interface (UEFI) specification for
701 * details.
702 *
703 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100704 */
705efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
706 void (EFIAPI *notify_function) (
707 struct efi_event *event,
708 void *context),
709 void *notify_context,
710 efi_guid_t *event_group,
711 struct efi_event **event)
712{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200713 efi_status_t ret;
714
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100715 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
716 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200717
718 /*
719 * The allowable input parameters are the same as in CreateEvent()
720 * except for the following two disallowed event types.
721 */
722 switch (type) {
723 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
724 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
725 ret = EFI_INVALID_PARAMETER;
726 goto out;
727 }
728
729 ret = efi_create_event(type, notify_tpl, notify_function,
730 notify_context, event_group, event);
731out:
732 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100733}
734
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200735/**
Mario Six8fac2912018-07-10 08:40:17 +0200736 * efi_create_event_ext() - create an event
737 * @type: type of the event to create
738 * @notify_tpl: task priority level of the event
739 * @notify_function: notification function of the event
740 * @notify_context: pointer passed to the notification function
741 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200742 *
743 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200744 *
745 * See the Unified Extensible Firmware Interface (UEFI) specification for
746 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200747 *
Mario Six8fac2912018-07-10 08:40:17 +0200748 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200749 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200750static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100751 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200752 void (EFIAPI *notify_function) (
753 struct efi_event *event,
754 void *context),
755 void *notify_context, struct efi_event **event)
756{
757 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
758 notify_context);
759 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100760 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200761}
762
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200763/**
Mario Six8fac2912018-07-10 08:40:17 +0200764 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200765 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200766 * Check if a timer event has occurred or a queued notification function should
767 * be called.
768 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100769 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200770 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100771 */
772void efi_timer_check(void)
773{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100774 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100775 u64 now = timer_get_us();
776
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100777 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200778 if (!timers_enabled)
779 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100780 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200781 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100782 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200783 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100784 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200785 break;
786 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100787 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200788 break;
789 default:
790 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200791 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100792 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200793 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100794 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200795 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100796 WATCHDOG_RESET();
797}
798
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200799/**
Mario Six8fac2912018-07-10 08:40:17 +0200800 * efi_set_timer() - set the trigger time for a timer event or stop the event
801 * @event: event for which the timer is set
802 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200803 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200804 *
805 * This is the function for internal usage in U-Boot. For the API function
806 * implementing the SetTimer service see efi_set_timer_ext.
807 *
Mario Six8fac2912018-07-10 08:40:17 +0200808 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200809 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200810efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200811 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100812{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100813 /* Check that the event is valid */
814 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
815 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100816
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200817 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200818 * The parameter defines a multiple of 100 ns.
819 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200820 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200821 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100822
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100823 switch (type) {
824 case EFI_TIMER_STOP:
825 event->trigger_next = -1ULL;
826 break;
827 case EFI_TIMER_PERIODIC:
828 case EFI_TIMER_RELATIVE:
829 event->trigger_next = timer_get_us() + trigger_time;
830 break;
831 default:
832 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100833 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100834 event->trigger_type = type;
835 event->trigger_time = trigger_time;
836 event->is_signaled = false;
837 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100838}
839
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200840/**
Mario Six8fac2912018-07-10 08:40:17 +0200841 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
842 * event
843 * @event: event for which the timer is set
844 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200845 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200846 *
847 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200848 *
Mario Six8fac2912018-07-10 08:40:17 +0200849 * See the Unified Extensible Firmware Interface (UEFI) specification for
850 * details.
851 *
852 *
853 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200854 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200855static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
856 enum efi_timer_delay type,
857 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200858{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900859 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200860 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
861}
862
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200863/**
Mario Six8fac2912018-07-10 08:40:17 +0200864 * efi_wait_for_event() - wait for events to be signaled
865 * @num_events: number of events to be waited for
866 * @event: events to be waited for
867 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200868 *
869 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200870 *
Mario Six8fac2912018-07-10 08:40:17 +0200871 * See the Unified Extensible Firmware Interface (UEFI) specification for
872 * details.
873 *
874 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200875 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100876static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200877 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100878 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100879{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100880 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100881
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100882 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100883
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200884 /* Check parameters */
885 if (!num_events || !event)
886 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200887 /* Check TPL */
888 if (efi_tpl != TPL_APPLICATION)
889 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200890 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100891 if (efi_is_event(event[i]) != EFI_SUCCESS)
892 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200893 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
894 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200895 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200896 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200897 }
898
899 /* Wait for signal */
900 for (;;) {
901 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200902 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200903 goto out;
904 }
905 /* Allow events to occur. */
906 efi_timer_check();
907 }
908
909out:
910 /*
911 * Reset the signal which is passed to the caller to allow periodic
912 * events to occur.
913 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200914 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200915 if (index)
916 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100917
918 return EFI_EXIT(EFI_SUCCESS);
919}
920
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200921/**
Mario Six8fac2912018-07-10 08:40:17 +0200922 * efi_signal_event_ext() - signal an EFI event
923 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200924 *
925 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200926 *
927 * See the Unified Extensible Firmware Interface (UEFI) specification for
928 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200929 *
930 * This functions sets the signaled state of the event and queues the
931 * notification function for execution.
932 *
Mario Six8fac2912018-07-10 08:40:17 +0200933 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200934 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200935static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100936{
937 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100938 if (efi_is_event(event) != EFI_SUCCESS)
939 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200940 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100941 return EFI_EXIT(EFI_SUCCESS);
942}
943
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200944/**
Mario Six8fac2912018-07-10 08:40:17 +0200945 * efi_close_event() - close an EFI event
946 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200947 *
948 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200949 *
Mario Six8fac2912018-07-10 08:40:17 +0200950 * See the Unified Extensible Firmware Interface (UEFI) specification for
951 * details.
952 *
953 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200954 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200955static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100956{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200957 struct efi_register_notify_event *item, *next;
958
Alexander Grafc15d9212016-03-04 01:09:59 +0100959 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100960 if (efi_is_event(event) != EFI_SUCCESS)
961 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200962
963 /* Remove protocol notify registrations for the event */
964 list_for_each_entry_safe(item, next, &efi_register_notify_events,
965 link) {
966 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200967 struct efi_protocol_notification *hitem, *hnext;
968
969 /* Remove signaled handles */
970 list_for_each_entry_safe(hitem, hnext, &item->handles,
971 link) {
972 list_del(&hitem->link);
973 free(hitem);
974 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200975 list_del(&item->link);
976 free(item);
977 }
978 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200979 /* Remove event from queue */
980 if (efi_event_is_queued(event))
981 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200982
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100983 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200984 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100985 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100986}
987
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200988/**
Mario Six8fac2912018-07-10 08:40:17 +0200989 * efi_check_event() - check if an event is signaled
990 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200991 *
992 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200993 *
994 * See the Unified Extensible Firmware Interface (UEFI) specification for
995 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200996 *
Mario Six8fac2912018-07-10 08:40:17 +0200997 * If an event is not signaled yet, the notification function is queued. The
998 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200999 *
Mario Six8fac2912018-07-10 08:40:17 +02001000 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001001 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001002static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001003{
1004 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001005 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001006 if (efi_is_event(event) != EFI_SUCCESS ||
1007 event->type & EVT_NOTIFY_SIGNAL)
1008 return EFI_EXIT(EFI_INVALID_PARAMETER);
1009 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001010 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001011 if (event->is_signaled) {
1012 event->is_signaled = false;
1013 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001014 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001015 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001016}
1017
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001018/**
Mario Six8fac2912018-07-10 08:40:17 +02001019 * efi_search_obj() - find the internal EFI object for a handle
1020 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001021 *
Mario Six8fac2912018-07-10 08:40:17 +02001022 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001023 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001024struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001025{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001026 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001027
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001028 if (!handle)
1029 return NULL;
1030
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001031 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001032 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001033 return efiobj;
1034 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001035 return NULL;
1036}
1037
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001038/**
Mario Six8fac2912018-07-10 08:40:17 +02001039 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1040 * to a protocol
1041 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001042 *
Mario Six8fac2912018-07-10 08:40:17 +02001043 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001044 */
1045static struct efi_open_protocol_info_entry *efi_create_open_info(
1046 struct efi_handler *handler)
1047{
1048 struct efi_open_protocol_info_item *item;
1049
1050 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1051 if (!item)
1052 return NULL;
1053 /* Append the item to the open protocol info list. */
1054 list_add_tail(&item->link, &handler->open_infos);
1055
1056 return &item->info;
1057}
1058
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001059/**
Mario Six8fac2912018-07-10 08:40:17 +02001060 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1061 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001062 *
Mario Six8fac2912018-07-10 08:40:17 +02001063 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001064 */
1065static efi_status_t efi_delete_open_info(
1066 struct efi_open_protocol_info_item *item)
1067{
1068 list_del(&item->link);
1069 free(item);
1070 return EFI_SUCCESS;
1071}
1072
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001073/**
Mario Six8fac2912018-07-10 08:40:17 +02001074 * efi_add_protocol() - install new protocol on a handle
1075 * @handle: handle on which the protocol shall be installed
1076 * @protocol: GUID of the protocol to be installed
1077 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001078 *
Mario Six8fac2912018-07-10 08:40:17 +02001079 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001080 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001081efi_status_t efi_add_protocol(const efi_handle_t handle,
1082 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001083 void *protocol_interface)
1084{
1085 struct efi_object *efiobj;
1086 struct efi_handler *handler;
1087 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001088 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001089
1090 efiobj = efi_search_obj(handle);
1091 if (!efiobj)
1092 return EFI_INVALID_PARAMETER;
1093 ret = efi_search_protocol(handle, protocol, NULL);
1094 if (ret != EFI_NOT_FOUND)
1095 return EFI_INVALID_PARAMETER;
1096 handler = calloc(1, sizeof(struct efi_handler));
1097 if (!handler)
1098 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001099 handler->guid = protocol;
1100 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001101 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001102 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001103
1104 /* Notify registered events */
1105 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001106 if (!guidcmp(protocol, &event->protocol)) {
1107 struct efi_protocol_notification *notif;
1108
1109 notif = calloc(1, sizeof(*notif));
1110 if (!notif) {
1111 list_del(&handler->link);
1112 free(handler);
1113 return EFI_OUT_OF_RESOURCES;
1114 }
1115 notif->handle = handle;
1116 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001117 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001118 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001119 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001120 }
1121
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001122 if (!guidcmp(&efi_guid_device_path, protocol))
1123 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001124 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001125}
1126
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001127/**
Mario Six8fac2912018-07-10 08:40:17 +02001128 * efi_install_protocol_interface() - install protocol interface
1129 * @handle: handle on which the protocol shall be installed
1130 * @protocol: GUID of the protocol to be installed
1131 * @protocol_interface_type: type of the interface to be installed,
1132 * always EFI_NATIVE_INTERFACE
1133 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001134 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001135 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001136 *
Mario Six8fac2912018-07-10 08:40:17 +02001137 * See the Unified Extensible Firmware Interface (UEFI) specification for
1138 * details.
1139 *
1140 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001141 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001142static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001143 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001144 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001145{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001146 efi_status_t r;
1147
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001148 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1149 protocol_interface);
1150
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001151 if (!handle || !protocol ||
1152 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1153 r = EFI_INVALID_PARAMETER;
1154 goto out;
1155 }
1156
1157 /* Create new handle if requested. */
1158 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001159 r = efi_create_handle(handle);
1160 if (r != EFI_SUCCESS)
1161 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001162 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001163 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001164 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001165 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001166 /* Add new protocol */
1167 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001168out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001169 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001170}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001171
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001172/**
Mario Six8fac2912018-07-10 08:40:17 +02001173 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001174 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001175 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001176 * @number_of_drivers: number of child controllers
1177 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001178 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001179 * The allocated buffer has to be freed with free().
1180 *
Mario Six8fac2912018-07-10 08:40:17 +02001181 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001182 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001183static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001184 const efi_guid_t *protocol,
1185 efi_uintn_t *number_of_drivers,
1186 efi_handle_t **driver_handle_buffer)
1187{
1188 struct efi_handler *handler;
1189 struct efi_open_protocol_info_item *item;
1190 efi_uintn_t count = 0, i;
1191 bool duplicate;
1192
1193 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001194 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001195 if (protocol && guidcmp(handler->guid, protocol))
1196 continue;
1197 list_for_each_entry(item, &handler->open_infos, link) {
1198 if (item->info.attributes &
1199 EFI_OPEN_PROTOCOL_BY_DRIVER)
1200 ++count;
1201 }
1202 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001203 *number_of_drivers = 0;
1204 if (!count) {
1205 *driver_handle_buffer = NULL;
1206 return EFI_SUCCESS;
1207 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001208 /*
1209 * Create buffer. In case of duplicate driver assignments the buffer
1210 * will be too large. But that does not harm.
1211 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001212 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1213 if (!*driver_handle_buffer)
1214 return EFI_OUT_OF_RESOURCES;
1215 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001216 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001217 if (protocol && guidcmp(handler->guid, protocol))
1218 continue;
1219 list_for_each_entry(item, &handler->open_infos, link) {
1220 if (item->info.attributes &
1221 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1222 /* Check this is a new driver */
1223 duplicate = false;
1224 for (i = 0; i < *number_of_drivers; ++i) {
1225 if ((*driver_handle_buffer)[i] ==
1226 item->info.agent_handle)
1227 duplicate = true;
1228 }
1229 /* Copy handle to buffer */
1230 if (!duplicate) {
1231 i = (*number_of_drivers)++;
1232 (*driver_handle_buffer)[i] =
1233 item->info.agent_handle;
1234 }
1235 }
1236 }
1237 }
1238 return EFI_SUCCESS;
1239}
1240
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001241/**
Mario Six8fac2912018-07-10 08:40:17 +02001242 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001243 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001244 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001245 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001246 *
1247 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001248 *
1249 * See the Unified Extensible Firmware Interface (UEFI) specification for
1250 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001251 *
Mario Six8fac2912018-07-10 08:40:17 +02001252 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001253 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001254static efi_status_t efi_disconnect_all_drivers
1255 (efi_handle_t handle,
1256 const efi_guid_t *protocol,
1257 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001258{
1259 efi_uintn_t number_of_drivers;
1260 efi_handle_t *driver_handle_buffer;
1261 efi_status_t r, ret;
1262
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001263 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001264 &driver_handle_buffer);
1265 if (ret != EFI_SUCCESS)
1266 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001267 if (!number_of_drivers)
1268 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001269 ret = EFI_NOT_FOUND;
1270 while (number_of_drivers) {
1271 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001272 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001273 driver_handle_buffer[--number_of_drivers],
1274 child_handle));
1275 if (r == EFI_SUCCESS)
1276 ret = r;
1277 }
1278 free(driver_handle_buffer);
1279 return ret;
1280}
1281
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001282/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001283 * efi_uninstall_protocol() - uninstall protocol interface
1284 *
Mario Six8fac2912018-07-10 08:40:17 +02001285 * @handle: handle from which the protocol shall be removed
1286 * @protocol: GUID of the protocol to be removed
1287 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001288 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001289 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001290 *
1291 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001292 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001293static efi_status_t efi_uninstall_protocol
1294 (efi_handle_t handle, const efi_guid_t *protocol,
1295 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001296{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001297 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001298 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001299 struct efi_open_protocol_info_item *item;
1300 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001301 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001302
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001303 /* Check handle */
1304 efiobj = efi_search_obj(handle);
1305 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001306 r = EFI_INVALID_PARAMETER;
1307 goto out;
1308 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001309 /* Find the protocol on the handle */
1310 r = efi_search_protocol(handle, protocol, &handler);
1311 if (r != EFI_SUCCESS)
1312 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001313 /* Disconnect controllers */
1314 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001315 /* Close protocol */
1316 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1317 if (item->info.attributes ==
1318 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1319 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1320 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1321 list_del(&item->link);
1322 }
1323 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001324 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001325 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001326 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001327 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001328out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001329 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001330}
1331
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001332/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001333 * efi_uninstall_protocol_interface() - uninstall protocol interface
1334 * @handle: handle from which the protocol shall be removed
1335 * @protocol: GUID of the protocol to be removed
1336 * @protocol_interface: interface to be removed
1337 *
1338 * This function implements the UninstallProtocolInterface service.
1339 *
1340 * See the Unified Extensible Firmware Interface (UEFI) specification for
1341 * details.
1342 *
1343 * Return: status code
1344 */
1345static efi_status_t EFIAPI efi_uninstall_protocol_interface
1346 (efi_handle_t handle, const efi_guid_t *protocol,
1347 void *protocol_interface)
1348{
1349 efi_status_t ret;
1350
1351 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1352
1353 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1354 if (ret != EFI_SUCCESS)
1355 goto out;
1356
1357 /* If the last protocol has been removed, delete the handle. */
1358 if (list_empty(&handle->protocols)) {
1359 list_del(&handle->link);
1360 free(handle);
1361 }
1362out:
1363 return EFI_EXIT(ret);
1364}
1365
1366/**
Mario Six8fac2912018-07-10 08:40:17 +02001367 * efi_register_protocol_notify() - register an event for notification when a
1368 * protocol is installed.
1369 * @protocol: GUID of the protocol whose installation shall be notified
1370 * @event: event to be signaled upon installation of the protocol
1371 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001372 *
1373 * This function implements the RegisterProtocolNotify service.
1374 * See the Unified Extensible Firmware Interface (UEFI) specification
1375 * for details.
1376 *
Mario Six8fac2912018-07-10 08:40:17 +02001377 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001378 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001379static efi_status_t EFIAPI efi_register_protocol_notify(
1380 const efi_guid_t *protocol,
1381 struct efi_event *event,
1382 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001383{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001384 struct efi_register_notify_event *item;
1385 efi_status_t ret = EFI_SUCCESS;
1386
Rob Clark238f88c2017-09-13 18:05:41 -04001387 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001388
1389 if (!protocol || !event || !registration) {
1390 ret = EFI_INVALID_PARAMETER;
1391 goto out;
1392 }
1393
1394 item = calloc(1, sizeof(struct efi_register_notify_event));
1395 if (!item) {
1396 ret = EFI_OUT_OF_RESOURCES;
1397 goto out;
1398 }
1399
1400 item->event = event;
1401 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001402 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001403
1404 list_add_tail(&item->link, &efi_register_notify_events);
1405
1406 *registration = item;
1407out:
1408 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001409}
1410
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001411/**
Mario Six8fac2912018-07-10 08:40:17 +02001412 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001413 *
Mario Six8fac2912018-07-10 08:40:17 +02001414 * @search_type: selection criterion
1415 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001416 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001417 *
1418 * See the documentation of the LocateHandle service in the UEFI specification.
1419 *
Mario Six8fac2912018-07-10 08:40:17 +02001420 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001421 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001422static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001423 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001424{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001425 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001426
1427 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001428 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001429 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001430 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001431 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001432 return (ret != EFI_SUCCESS);
1433 default:
1434 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001435 return -1;
1436 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001437}
1438
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001439/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001440 * efi_check_register_notify_event() - check if registration key is valid
1441 *
1442 * Check that a pointer is a valid registration key as returned by
1443 * RegisterProtocolNotify().
1444 *
1445 * @key: registration key
1446 * Return: valid registration key or NULL
1447 */
1448static struct efi_register_notify_event *efi_check_register_notify_event
1449 (void *key)
1450{
1451 struct efi_register_notify_event *event;
1452
1453 list_for_each_entry(event, &efi_register_notify_events, link) {
1454 if (event == (struct efi_register_notify_event *)key)
1455 return event;
1456 }
1457 return NULL;
1458}
1459
1460/**
Mario Six8fac2912018-07-10 08:40:17 +02001461 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001462 *
1463 * @search_type: selection criterion
1464 * @protocol: GUID of the protocol
1465 * @search_key: registration key
1466 * @buffer_size: size of the buffer to receive the handles in bytes
1467 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001468 *
1469 * This function is meant for U-Boot internal calls. For the API implementation
1470 * of the LocateHandle service see efi_locate_handle_ext.
1471 *
Mario Six8fac2912018-07-10 08:40:17 +02001472 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001473 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001474static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001475 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001476 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001477 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001478{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001479 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001480 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001481 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001482 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001483
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001484 /* Check parameters */
1485 switch (search_type) {
1486 case ALL_HANDLES:
1487 break;
1488 case BY_REGISTER_NOTIFY:
1489 if (!search_key)
1490 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001491 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001492 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001493 if (!event)
1494 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001495 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001496 case BY_PROTOCOL:
1497 if (!protocol)
1498 return EFI_INVALID_PARAMETER;
1499 break;
1500 default:
1501 return EFI_INVALID_PARAMETER;
1502 }
1503
Alexander Grafc15d9212016-03-04 01:09:59 +01001504 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001505 if (search_type == BY_REGISTER_NOTIFY) {
1506 if (list_empty(&event->handles))
1507 return EFI_NOT_FOUND;
1508 handle = list_first_entry(&event->handles,
1509 struct efi_protocol_notification,
1510 link);
1511 efiobj = handle->handle;
1512 size += sizeof(void *);
1513 } else {
1514 list_for_each_entry(efiobj, &efi_obj_list, link) {
1515 if (!efi_search(search_type, protocol, efiobj))
1516 size += sizeof(void *);
1517 }
1518 if (size == 0)
1519 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001520 }
1521
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001522 if (!buffer_size)
1523 return EFI_INVALID_PARAMETER;
1524
Alexander Grafc15d9212016-03-04 01:09:59 +01001525 if (*buffer_size < size) {
1526 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001527 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001528 }
1529
Rob Clarkcdee3372017-08-06 14:10:07 -04001530 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001531
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001532 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001533 if (!buffer)
1534 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001535
Alexander Grafc15d9212016-03-04 01:09:59 +01001536 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001537 if (search_type == BY_REGISTER_NOTIFY) {
1538 *buffer = efiobj;
1539 list_del(&handle->link);
1540 } else {
1541 list_for_each_entry(efiobj, &efi_obj_list, link) {
1542 if (!efi_search(search_type, protocol, efiobj))
1543 *buffer++ = efiobj;
1544 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001545 }
1546
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001547 return EFI_SUCCESS;
1548}
1549
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001550/**
Mario Six8fac2912018-07-10 08:40:17 +02001551 * efi_locate_handle_ext() - locate handles implementing a protocol.
1552 * @search_type: selection criterion
1553 * @protocol: GUID of the protocol
1554 * @search_key: registration key
1555 * @buffer_size: size of the buffer to receive the handles in bytes
1556 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001557 *
1558 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001559 *
Mario Six8fac2912018-07-10 08:40:17 +02001560 * See the Unified Extensible Firmware Interface (UEFI) specification for
1561 * details.
1562 *
1563 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001564 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001565static efi_status_t EFIAPI efi_locate_handle_ext(
1566 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001567 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001568 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001569{
Rob Clark238f88c2017-09-13 18:05:41 -04001570 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001571 buffer_size, buffer);
1572
1573 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1574 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001575}
1576
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001577/**
Mario Six8fac2912018-07-10 08:40:17 +02001578 * efi_remove_configuration_table() - collapses configuration table entries,
1579 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001580 *
Mario Six8fac2912018-07-10 08:40:17 +02001581 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001582 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001583static void efi_remove_configuration_table(int i)
1584{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001585 struct efi_configuration_table *this = &systab.tables[i];
1586 struct efi_configuration_table *next = &systab.tables[i + 1];
1587 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001588
1589 memmove(this, next, (ulong)end - (ulong)next);
1590 systab.nr_tables--;
1591}
1592
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001593/**
Mario Six8fac2912018-07-10 08:40:17 +02001594 * efi_install_configuration_table() - adds, updates, or removes a
1595 * configuration table
1596 * @guid: GUID of the installed table
1597 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001598 *
1599 * This function is used for internal calls. For the API implementation of the
1600 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1601 *
Mario Six8fac2912018-07-10 08:40:17 +02001602 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001603 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001604efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1605 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001606{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001607 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001608 int i;
1609
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001610 if (!guid)
1611 return EFI_INVALID_PARAMETER;
1612
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001613 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001614 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001615 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001616 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001617 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001618 else
1619 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001620 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001621 }
1622 }
1623
Alexander Graffe3366f2017-07-26 13:41:04 +02001624 if (!table)
1625 return EFI_NOT_FOUND;
1626
Alexander Grafc15d9212016-03-04 01:09:59 +01001627 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001628 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001629 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001630
1631 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001632 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1633 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001634 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001635
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001636out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001637 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001638 efi_update_table_header_crc32(&systab.hdr);
1639
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001640 /* Notify that the configuration table was changed */
1641 list_for_each_entry(evt, &efi_events, link) {
1642 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001643 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001644 break;
1645 }
1646 }
1647
Alexander Grafc5c11632016-08-19 01:23:24 +02001648 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001649}
1650
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001651/**
Mario Six8fac2912018-07-10 08:40:17 +02001652 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1653 * configuration table.
1654 * @guid: GUID of the installed table
1655 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001656 *
1657 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001658 *
Mario Six8fac2912018-07-10 08:40:17 +02001659 * See the Unified Extensible Firmware Interface (UEFI) specification for
1660 * details.
1661 *
1662 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001663 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001664static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1665 void *table)
1666{
Rob Clark238f88c2017-09-13 18:05:41 -04001667 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001668 return EFI_EXIT(efi_install_configuration_table(guid, table));
1669}
1670
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001671/**
Mario Six8fac2912018-07-10 08:40:17 +02001672 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001673 *
1674 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001675 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001676 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001677 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001678 * code is returned.
1679 *
1680 * @device_path: device path of the loaded image
1681 * @file_path: file path of the loaded image
1682 * @handle_ptr: handle of the loaded image
1683 * @info_ptr: loaded image protocol
1684 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001685 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001686efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1687 struct efi_device_path *file_path,
1688 struct efi_loaded_image_obj **handle_ptr,
1689 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001690{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001691 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001692 struct efi_loaded_image *info = NULL;
1693 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001694 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001695
1696 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1697 *handle_ptr = NULL;
1698 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001699
1700 info = calloc(1, sizeof(*info));
1701 if (!info)
1702 return EFI_OUT_OF_RESOURCES;
1703 obj = calloc(1, sizeof(*obj));
1704 if (!obj) {
1705 free(info);
1706 return EFI_OUT_OF_RESOURCES;
1707 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001708 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001709
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001710 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001711 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001712
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001713 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001714 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001715 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001716
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001717 if (device_path) {
1718 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001719
1720 dp = efi_dp_append(device_path, file_path);
1721 if (!dp) {
1722 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001723 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001724 }
1725 } else {
1726 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001727 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001728 ret = efi_add_protocol(&obj->header,
1729 &efi_guid_loaded_image_device_path, dp);
1730 if (ret != EFI_SUCCESS)
1731 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001732
1733 /*
1734 * When asking for the loaded_image interface, just
1735 * return handle which points to loaded_image_info
1736 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001737 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001738 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001739 if (ret != EFI_SUCCESS)
1740 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001741
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001742 *info_ptr = info;
1743 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001744
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001745 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001746failure:
1747 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001748 efi_delete_handle(&obj->header);
1749 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001750 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001751}
1752
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001753/**
Mario Six8fac2912018-07-10 08:40:17 +02001754 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001755 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001756 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1757 * callers obligation to update the memory type as needed.
1758 *
1759 * @file_path: the path of the image to load
1760 * @buffer: buffer containing the loaded image
1761 * @size: size of the loaded image
1762 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001763 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001764static
Rob Clarkc84c1102017-09-13 18:05:38 -04001765efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001766 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001767{
1768 struct efi_file_info *info = NULL;
1769 struct efi_file_handle *f;
1770 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001771 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001772 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001773
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001774 /* In case of failure nothing is returned */
1775 *buffer = NULL;
1776 *size = 0;
1777
1778 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001779 f = efi_file_from_path(file_path);
1780 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001781 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001782
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001783 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001784 bs = 0;
1785 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1786 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001787 if (ret != EFI_BUFFER_TOO_SMALL) {
1788 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001789 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001790 }
Rob Clark857a1222017-09-13 18:05:35 -04001791
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001792 info = malloc(bs);
1793 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1794 info));
1795 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001796 goto error;
1797
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001798 /*
1799 * When reading the file we do not yet know if it contains an
1800 * application, a boottime driver, or a runtime driver. So here we
1801 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1802 * update the reservation according to the image type.
1803 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001804 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001805 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1806 EFI_BOOT_SERVICES_DATA,
1807 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001808 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001809 ret = EFI_OUT_OF_RESOURCES;
1810 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001811 }
1812
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001813 /* Read file */
1814 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1815 if (ret != EFI_SUCCESS)
1816 efi_free_pages(addr, efi_size_in_pages(bs));
1817 *buffer = (void *)(uintptr_t)addr;
1818 *size = bs;
1819error:
1820 EFI_CALL(f->close(f));
1821 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001822 return ret;
1823}
1824
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001825/**
Mario Six8fac2912018-07-10 08:40:17 +02001826 * efi_load_image() - load an EFI image into memory
1827 * @boot_policy: true for request originating from the boot manager
1828 * @parent_image: the caller's image handle
1829 * @file_path: the path of the image to load
1830 * @source_buffer: memory location from which the image is installed
1831 * @source_size: size of the memory area from which the image is installed
1832 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001833 *
1834 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001835 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001836 * See the Unified Extensible Firmware Interface (UEFI) specification
1837 * for details.
1838 *
Mario Six8fac2912018-07-10 08:40:17 +02001839 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001840 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001841efi_status_t EFIAPI efi_load_image(bool boot_policy,
1842 efi_handle_t parent_image,
1843 struct efi_device_path *file_path,
1844 void *source_buffer,
1845 efi_uintn_t source_size,
1846 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001847{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001848 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001849 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001850 struct efi_loaded_image_obj **image_obj =
1851 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001852 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001853 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001854
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001855 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001856 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001857
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001858 if (!image_handle || (!source_buffer && !file_path) ||
1859 !efi_search_obj(parent_image) ||
1860 /* The parent image handle must refer to a loaded image */
1861 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001862 ret = EFI_INVALID_PARAMETER;
1863 goto error;
1864 }
Rob Clark857a1222017-09-13 18:05:35 -04001865
1866 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001867 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001868 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001869 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001870 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001871 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001872 if (!source_size) {
1873 ret = EFI_LOAD_ERROR;
1874 goto error;
1875 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001876 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001877 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001878 /* split file_path which contains both the device and file parts */
1879 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001880 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001881 if (ret == EFI_SUCCESS)
1882 ret = efi_load_pe(*image_obj, dest_buffer, info);
1883 if (!source_buffer)
1884 /* Release buffer to which file was loaded */
1885 efi_free_pages((uintptr_t)dest_buffer,
1886 efi_size_in_pages(source_size));
1887 if (ret == EFI_SUCCESS) {
1888 info->system_table = &systab;
1889 info->parent_handle = parent_image;
1890 } else {
1891 /* The image is invalid. Release all associated resources. */
1892 efi_delete_handle(*image_handle);
1893 *image_handle = NULL;
1894 free(info);
1895 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001896error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001897 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001898}
1899
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001900/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001901 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1902 */
1903static void efi_exit_caches(void)
1904{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001905#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01001906 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001907 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1908 * caches are enabled.
1909 *
1910 * TODO:
1911 * According to the UEFI spec caches that can be managed via CP15
1912 * operations should be enabled. Caches requiring platform information
1913 * to manage should be disabled. This should not happen in
1914 * ExitBootServices() but before invoking any UEFI binary is invoked.
1915 *
1916 * We want to keep the current workaround while GRUB prior to version
1917 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01001918 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001919 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01001920#endif
1921}
1922
1923/**
Mario Six8fac2912018-07-10 08:40:17 +02001924 * efi_exit_boot_services() - stop all boot services
1925 * @image_handle: handle of the loaded image
1926 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001927 *
1928 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001929 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001930 * See the Unified Extensible Firmware Interface (UEFI) specification
1931 * for details.
1932 *
Mario Six8fac2912018-07-10 08:40:17 +02001933 * All timer events are disabled. For exit boot services events the
1934 * notification function is called. The boot services are disabled in the
1935 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001936 *
Mario Six8fac2912018-07-10 08:40:17 +02001937 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001938 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001939static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001940 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001941{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001942 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001943 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001944
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001945 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001946
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001947 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001948 if (map_key != efi_memory_map_key) {
1949 ret = EFI_INVALID_PARAMETER;
1950 goto out;
1951 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001952
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001953 /* Check if ExitBootServices has already been called */
1954 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001955 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001956
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001957 /* Stop all timer related activities */
1958 timers_enabled = false;
1959
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001960 /* Add related events to the event group */
1961 list_for_each_entry(evt, &efi_events, link) {
1962 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1963 evt->group = &efi_guid_event_group_exit_boot_services;
1964 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001965 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001966 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001967 if (evt->group &&
1968 !guidcmp(evt->group,
1969 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001970 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001971 break;
1972 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001973 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001974
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001975 /* Make sure that notification functions are not called anymore */
1976 efi_tpl = TPL_HIGH_LEVEL;
1977
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001978 /* Notify variable services */
1979 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001980
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001981 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1982 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1983 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1984 list_del(&evt->link);
1985 }
1986
Alexander Graf2ebeb442016-11-17 01:02:57 +01001987 board_quiesce_devices();
1988
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02001989 /* Patch out unsupported runtime function */
1990 efi_runtime_detach();
1991
Alexander Graffa5246b2018-11-15 21:23:47 +01001992 /* Fix up caches for EFI payloads if necessary */
1993 efi_exit_caches();
1994
Alexander Grafc15d9212016-03-04 01:09:59 +01001995 /* This stops all lingering devices */
1996 bootm_disable_interrupts();
1997
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001998 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001999 systab.con_in_handle = NULL;
2000 systab.con_in = NULL;
2001 systab.con_out_handle = NULL;
2002 systab.con_out = NULL;
2003 systab.stderr_handle = NULL;
2004 systab.std_err = NULL;
2005 systab.boottime = NULL;
2006
2007 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002008 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002009
Alexander Grafc15d9212016-03-04 01:09:59 +01002010 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002011 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002012 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002013out:
2014 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002015}
2016
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002017/**
Mario Six8fac2912018-07-10 08:40:17 +02002018 * efi_get_next_monotonic_count() - get next value of the counter
2019 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002020 *
2021 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002022 *
2023 * See the Unified Extensible Firmware Interface (UEFI) specification for
2024 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002025 *
Mario Six8fac2912018-07-10 08:40:17 +02002026 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002027 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002028static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2029{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002030 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002031 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002032
Alexander Grafc15d9212016-03-04 01:09:59 +01002033 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002034 if (!count) {
2035 ret = EFI_INVALID_PARAMETER;
2036 goto out;
2037 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002038 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002039 ret = EFI_SUCCESS;
2040out:
2041 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002042}
2043
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002044/**
Mario Six8fac2912018-07-10 08:40:17 +02002045 * efi_stall() - sleep
2046 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002047 *
Mario Six8fac2912018-07-10 08:40:17 +02002048 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002049 *
Mario Six8fac2912018-07-10 08:40:17 +02002050 * See the Unified Extensible Firmware Interface (UEFI) specification for
2051 * details.
2052 *
2053 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002054 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002055static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2056{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002057 u64 end_tick;
2058
Alexander Grafc15d9212016-03-04 01:09:59 +01002059 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002060
2061 end_tick = get_ticks() + usec_to_tick(microseconds);
2062 while (get_ticks() < end_tick)
2063 efi_timer_check();
2064
Alexander Grafc15d9212016-03-04 01:09:59 +01002065 return EFI_EXIT(EFI_SUCCESS);
2066}
2067
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002068/**
Mario Six8fac2912018-07-10 08:40:17 +02002069 * efi_set_watchdog_timer() - reset the watchdog timer
2070 * @timeout: seconds before reset by watchdog
2071 * @watchdog_code: code to be logged when resetting
2072 * @data_size: size of buffer in bytes
2073 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002074 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002075 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002076 *
Mario Six8fac2912018-07-10 08:40:17 +02002077 * See the Unified Extensible Firmware Interface (UEFI) specification for
2078 * details.
2079 *
2080 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002081 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002082static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2083 uint64_t watchdog_code,
2084 unsigned long data_size,
2085 uint16_t *watchdog_data)
2086{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002087 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002088 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002089 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002090}
2091
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002092/**
Mario Six8fac2912018-07-10 08:40:17 +02002093 * efi_close_protocol() - close a protocol
2094 * @handle: handle on which the protocol shall be closed
2095 * @protocol: GUID of the protocol to close
2096 * @agent_handle: handle of the driver
2097 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002098 *
2099 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002100 *
Mario Six8fac2912018-07-10 08:40:17 +02002101 * See the Unified Extensible Firmware Interface (UEFI) specification for
2102 * details.
2103 *
2104 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002105 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002106static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002107 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002108 efi_handle_t agent_handle,
2109 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002110{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002111 struct efi_handler *handler;
2112 struct efi_open_protocol_info_item *item;
2113 struct efi_open_protocol_info_item *pos;
2114 efi_status_t r;
2115
Rob Clark238f88c2017-09-13 18:05:41 -04002116 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002117 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002118
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002119 if (!efi_search_obj(agent_handle) ||
2120 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002121 r = EFI_INVALID_PARAMETER;
2122 goto out;
2123 }
2124 r = efi_search_protocol(handle, protocol, &handler);
2125 if (r != EFI_SUCCESS)
2126 goto out;
2127
2128 r = EFI_NOT_FOUND;
2129 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2130 if (item->info.agent_handle == agent_handle &&
2131 item->info.controller_handle == controller_handle) {
2132 efi_delete_open_info(item);
2133 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002134 }
2135 }
2136out:
2137 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002138}
2139
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002140/**
Mario Six8fac2912018-07-10 08:40:17 +02002141 * efi_open_protocol_information() - provide information about then open status
2142 * of a protocol on a handle
2143 * @handle: handle for which the information shall be retrieved
2144 * @protocol: GUID of the protocol
2145 * @entry_buffer: buffer to receive the open protocol information
2146 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002147 *
2148 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002149 *
2150 * See the Unified Extensible Firmware Interface (UEFI) specification for
2151 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002152 *
Mario Six8fac2912018-07-10 08:40:17 +02002153 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002154 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002155static efi_status_t EFIAPI efi_open_protocol_information(
2156 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002157 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002158 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002159{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002160 unsigned long buffer_size;
2161 unsigned long count;
2162 struct efi_handler *handler;
2163 struct efi_open_protocol_info_item *item;
2164 efi_status_t r;
2165
Rob Clark238f88c2017-09-13 18:05:41 -04002166 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002167 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002168
2169 /* Check parameters */
2170 if (!entry_buffer) {
2171 r = EFI_INVALID_PARAMETER;
2172 goto out;
2173 }
2174 r = efi_search_protocol(handle, protocol, &handler);
2175 if (r != EFI_SUCCESS)
2176 goto out;
2177
2178 /* Count entries */
2179 count = 0;
2180 list_for_each_entry(item, &handler->open_infos, link) {
2181 if (item->info.open_count)
2182 ++count;
2183 }
2184 *entry_count = count;
2185 *entry_buffer = NULL;
2186 if (!count) {
2187 r = EFI_SUCCESS;
2188 goto out;
2189 }
2190
2191 /* Copy entries */
2192 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002193 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002194 (void **)entry_buffer);
2195 if (r != EFI_SUCCESS)
2196 goto out;
2197 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2198 if (item->info.open_count)
2199 (*entry_buffer)[--count] = item->info;
2200 }
2201out:
2202 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002203}
2204
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002205/**
Mario Six8fac2912018-07-10 08:40:17 +02002206 * efi_protocols_per_handle() - get protocols installed on a handle
2207 * @handle: handle for which the information is retrieved
2208 * @protocol_buffer: buffer with protocol GUIDs
2209 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002210 *
2211 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002212 *
Mario Six8fac2912018-07-10 08:40:17 +02002213 * See the Unified Extensible Firmware Interface (UEFI) specification for
2214 * details.
2215 *
2216 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002217 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002218static efi_status_t EFIAPI efi_protocols_per_handle(
2219 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002220 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002221{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002222 unsigned long buffer_size;
2223 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002224 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002225 efi_status_t r;
2226
Alexander Grafc15d9212016-03-04 01:09:59 +01002227 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2228 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002229
2230 if (!handle || !protocol_buffer || !protocol_buffer_count)
2231 return EFI_EXIT(EFI_INVALID_PARAMETER);
2232
2233 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002234 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002235
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002236 efiobj = efi_search_obj(handle);
2237 if (!efiobj)
2238 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002239
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002240 /* Count protocols */
2241 list_for_each(protocol_handle, &efiobj->protocols) {
2242 ++*protocol_buffer_count;
2243 }
2244
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002245 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002246 if (*protocol_buffer_count) {
2247 size_t j = 0;
2248
2249 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002250 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002251 (void **)protocol_buffer);
2252 if (r != EFI_SUCCESS)
2253 return EFI_EXIT(r);
2254 list_for_each(protocol_handle, &efiobj->protocols) {
2255 struct efi_handler *protocol;
2256
2257 protocol = list_entry(protocol_handle,
2258 struct efi_handler, link);
2259 (*protocol_buffer)[j] = (void *)protocol->guid;
2260 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002261 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002262 }
2263
2264 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002265}
2266
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002267/**
Mario Six8fac2912018-07-10 08:40:17 +02002268 * efi_locate_handle_buffer() - locate handles implementing a protocol
2269 * @search_type: selection criterion
2270 * @protocol: GUID of the protocol
2271 * @search_key: registration key
2272 * @no_handles: number of returned handles
2273 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002274 *
2275 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002276 *
Mario Six8fac2912018-07-10 08:40:17 +02002277 * See the Unified Extensible Firmware Interface (UEFI) specification for
2278 * details.
2279 *
2280 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002281 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002282static efi_status_t EFIAPI efi_locate_handle_buffer(
2283 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002284 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002285 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002286{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002287 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002288 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002289
Rob Clark238f88c2017-09-13 18:05:41 -04002290 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002291 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002292
2293 if (!no_handles || !buffer) {
2294 r = EFI_INVALID_PARAMETER;
2295 goto out;
2296 }
2297 *no_handles = 0;
2298 *buffer = NULL;
2299 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2300 *buffer);
2301 if (r != EFI_BUFFER_TOO_SMALL)
2302 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002303 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002304 (void **)buffer);
2305 if (r != EFI_SUCCESS)
2306 goto out;
2307 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2308 *buffer);
2309 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002310 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002311out:
2312 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002313}
2314
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002315/**
Mario Six8fac2912018-07-10 08:40:17 +02002316 * efi_locate_protocol() - find an interface implementing a protocol
2317 * @protocol: GUID of the protocol
2318 * @registration: registration key passed to the notification function
2319 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002320 *
2321 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002322 *
2323 * See the Unified Extensible Firmware Interface (UEFI) specification for
2324 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002325 *
Mario Six8fac2912018-07-10 08:40:17 +02002326 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002327 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002328static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002329 void *registration,
2330 void **protocol_interface)
2331{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002332 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002333 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002334 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002335
Rob Clark238f88c2017-09-13 18:05:41 -04002336 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002337
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002338 /*
2339 * The UEFI spec explicitly requires a protocol even if a registration
2340 * key is provided. This differs from the logic in LocateHandle().
2341 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002342 if (!protocol || !protocol_interface)
2343 return EFI_EXIT(EFI_INVALID_PARAMETER);
2344
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002345 if (registration) {
2346 struct efi_register_notify_event *event;
2347 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002348
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002349 event = efi_check_register_notify_event(registration);
2350 if (!event)
2351 return EFI_EXIT(EFI_INVALID_PARAMETER);
2352 /*
2353 * The UEFI spec requires to return EFI_NOT_FOUND if no
2354 * protocol instance matches protocol and registration.
2355 * So let's do the same for a mismatch between protocol and
2356 * registration.
2357 */
2358 if (guidcmp(&event->protocol, protocol))
2359 goto not_found;
2360 if (list_empty(&event->handles))
2361 goto not_found;
2362 handle = list_first_entry(&event->handles,
2363 struct efi_protocol_notification,
2364 link);
2365 efiobj = handle->handle;
2366 list_del(&handle->link);
2367 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002368 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002369 if (ret == EFI_SUCCESS)
2370 goto found;
2371 } else {
2372 list_for_each_entry(efiobj, &efi_obj_list, link) {
2373 ret = efi_search_protocol(efiobj, protocol, &handler);
2374 if (ret == EFI_SUCCESS)
2375 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002376 }
2377 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002378not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002379 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002380 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002381found:
2382 *protocol_interface = handler->protocol_interface;
2383 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002384}
2385
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002386/**
Mario Six8fac2912018-07-10 08:40:17 +02002387 * efi_locate_device_path() - Get the device path and handle of an device
2388 * implementing a protocol
2389 * @protocol: GUID of the protocol
2390 * @device_path: device path
2391 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002392 *
2393 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002394 *
Mario Six8fac2912018-07-10 08:40:17 +02002395 * See the Unified Extensible Firmware Interface (UEFI) specification for
2396 * details.
2397 *
2398 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002399 */
2400static efi_status_t EFIAPI efi_locate_device_path(
2401 const efi_guid_t *protocol,
2402 struct efi_device_path **device_path,
2403 efi_handle_t *device)
2404{
2405 struct efi_device_path *dp;
2406 size_t i;
2407 struct efi_handler *handler;
2408 efi_handle_t *handles;
2409 size_t len, len_dp;
2410 size_t len_best = 0;
2411 efi_uintn_t no_handles;
2412 u8 *remainder;
2413 efi_status_t ret;
2414
2415 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2416
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002417 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002418 ret = EFI_INVALID_PARAMETER;
2419 goto out;
2420 }
2421
2422 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002423 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002424
2425 /* Get all handles implementing the protocol */
2426 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2427 &no_handles, &handles));
2428 if (ret != EFI_SUCCESS)
2429 goto out;
2430
2431 for (i = 0; i < no_handles; ++i) {
2432 /* Find the device path protocol */
2433 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2434 &handler);
2435 if (ret != EFI_SUCCESS)
2436 continue;
2437 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002438 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002439 /*
2440 * This handle can only be a better fit
2441 * if its device path length is longer than the best fit and
2442 * if its device path length is shorter of equal the searched
2443 * device path.
2444 */
2445 if (len_dp <= len_best || len_dp > len)
2446 continue;
2447 /* Check if dp is a subpath of device_path */
2448 if (memcmp(*device_path, dp, len_dp))
2449 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002450 if (!device) {
2451 ret = EFI_INVALID_PARAMETER;
2452 goto out;
2453 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002454 *device = handles[i];
2455 len_best = len_dp;
2456 }
2457 if (len_best) {
2458 remainder = (u8 *)*device_path + len_best;
2459 *device_path = (struct efi_device_path *)remainder;
2460 ret = EFI_SUCCESS;
2461 } else {
2462 ret = EFI_NOT_FOUND;
2463 }
2464out:
2465 return EFI_EXIT(ret);
2466}
2467
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002468/**
Mario Six8fac2912018-07-10 08:40:17 +02002469 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2470 * interfaces
2471 * @handle: handle on which the protocol interfaces shall be installed
2472 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2473 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002474 *
2475 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002476 *
2477 * See the Unified Extensible Firmware Interface (UEFI) specification for
2478 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002479 *
Mario Six8fac2912018-07-10 08:40:17 +02002480 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002481 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002482efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002483 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002484{
2485 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002486
Alexander Graf404a7c82018-06-18 17:23:05 +02002487 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002488 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002489 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002490 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002491 efi_status_t r = EFI_SUCCESS;
2492 int i = 0;
2493
2494 if (!handle)
2495 return EFI_EXIT(EFI_INVALID_PARAMETER);
2496
Alexander Graf404a7c82018-06-18 17:23:05 +02002497 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002498 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002499 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002500 if (!protocol)
2501 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002502 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002503 /* Check that a device path has not been installed before */
2504 if (!guidcmp(protocol, &efi_guid_device_path)) {
2505 struct efi_device_path *dp = protocol_interface;
2506
2507 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2508 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002509 if (r == EFI_SUCCESS &&
2510 dp->type == DEVICE_PATH_TYPE_END) {
2511 EFI_PRINT("Path %pD already installed\n",
2512 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002513 r = EFI_ALREADY_STARTED;
2514 break;
2515 }
2516 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002517 r = EFI_CALL(efi_install_protocol_interface(
2518 handle, protocol,
2519 EFI_NATIVE_INTERFACE,
2520 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002521 if (r != EFI_SUCCESS)
2522 break;
2523 i++;
2524 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002525 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002526 if (r == EFI_SUCCESS)
2527 return EFI_EXIT(r);
2528
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002529 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002530 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002531 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002532 protocol = efi_va_arg(argptr, efi_guid_t*);
2533 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002534 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002535 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002536 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002537 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002538
2539 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002540}
2541
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002542/**
Mario Six8fac2912018-07-10 08:40:17 +02002543 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2544 * interfaces
2545 * @handle: handle from which the protocol interfaces shall be removed
2546 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2547 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002548 *
2549 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002550 *
Mario Six8fac2912018-07-10 08:40:17 +02002551 * See the Unified Extensible Firmware Interface (UEFI) specification for
2552 * details.
2553 *
2554 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002555 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002556static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002557 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002558{
2559 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002560
Alexander Graf404a7c82018-06-18 17:23:05 +02002561 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002562 const efi_guid_t *protocol;
2563 void *protocol_interface;
2564 efi_status_t r = EFI_SUCCESS;
2565 size_t i = 0;
2566
2567 if (!handle)
2568 return EFI_EXIT(EFI_INVALID_PARAMETER);
2569
Alexander Graf404a7c82018-06-18 17:23:05 +02002570 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002571 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002572 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002573 if (!protocol)
2574 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002575 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002576 r = efi_uninstall_protocol(handle, protocol,
2577 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002578 if (r != EFI_SUCCESS)
2579 break;
2580 i++;
2581 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002582 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002583 if (r == EFI_SUCCESS) {
2584 /* If the last protocol has been removed, delete the handle. */
2585 if (list_empty(&handle->protocols)) {
2586 list_del(&handle->link);
2587 free(handle);
2588 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002589 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002590 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002591
2592 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002593 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002594 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002595 protocol = efi_va_arg(argptr, efi_guid_t*);
2596 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002597 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2598 EFI_NATIVE_INTERFACE,
2599 protocol_interface));
2600 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002601 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002602
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002603 /* In case of an error always return EFI_INVALID_PARAMETER */
2604 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002605}
2606
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002607/**
Mario Six8fac2912018-07-10 08:40:17 +02002608 * efi_calculate_crc32() - calculate cyclic redundancy code
2609 * @data: buffer with data
2610 * @data_size: size of buffer in bytes
2611 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002612 *
2613 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002614 *
2615 * See the Unified Extensible Firmware Interface (UEFI) specification for
2616 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002617 *
Mario Six8fac2912018-07-10 08:40:17 +02002618 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002619 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002620static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2621 efi_uintn_t data_size,
2622 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002623{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002624 efi_status_t ret = EFI_SUCCESS;
2625
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002626 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002627 if (!data || !data_size || !crc32_p) {
2628 ret = EFI_INVALID_PARAMETER;
2629 goto out;
2630 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002631 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002632out:
2633 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002634}
2635
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002636/**
Mario Six8fac2912018-07-10 08:40:17 +02002637 * efi_copy_mem() - copy memory
2638 * @destination: destination of the copy operation
2639 * @source: source of the copy operation
2640 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002641 *
2642 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002643 *
Mario Six8fac2912018-07-10 08:40:17 +02002644 * See the Unified Extensible Firmware Interface (UEFI) specification for
2645 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002646 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002647static void EFIAPI efi_copy_mem(void *destination, const void *source,
2648 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002649{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002650 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002651 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002652 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002653}
2654
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002655/**
Mario Six8fac2912018-07-10 08:40:17 +02002656 * efi_set_mem() - Fill memory with a byte value.
2657 * @buffer: buffer to fill
2658 * @size: size of buffer in bytes
2659 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002660 *
2661 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002662 *
Mario Six8fac2912018-07-10 08:40:17 +02002663 * See the Unified Extensible Firmware Interface (UEFI) specification for
2664 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002665 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002666static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002667{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002668 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002669 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002670 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002671}
2672
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002673/**
Mario Six8fac2912018-07-10 08:40:17 +02002674 * efi_protocol_open() - open protocol interface on a handle
2675 * @handler: handler of a protocol
2676 * @protocol_interface: interface implementing the protocol
2677 * @agent_handle: handle of the driver
2678 * @controller_handle: handle of the controller
2679 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002680 *
Mario Six8fac2912018-07-10 08:40:17 +02002681 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002682 */
2683static efi_status_t efi_protocol_open(
2684 struct efi_handler *handler,
2685 void **protocol_interface, void *agent_handle,
2686 void *controller_handle, uint32_t attributes)
2687{
2688 struct efi_open_protocol_info_item *item;
2689 struct efi_open_protocol_info_entry *match = NULL;
2690 bool opened_by_driver = false;
2691 bool opened_exclusive = false;
2692
2693 /* If there is no agent, only return the interface */
2694 if (!agent_handle)
2695 goto out;
2696
2697 /* For TEST_PROTOCOL ignore interface attribute */
2698 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2699 *protocol_interface = NULL;
2700
2701 /*
2702 * Check if the protocol is already opened by a driver with the same
2703 * attributes or opened exclusively
2704 */
2705 list_for_each_entry(item, &handler->open_infos, link) {
2706 if (item->info.agent_handle == agent_handle) {
2707 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2708 (item->info.attributes == attributes))
2709 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002710 } else {
2711 if (item->info.attributes &
2712 EFI_OPEN_PROTOCOL_BY_DRIVER)
2713 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002714 }
2715 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2716 opened_exclusive = true;
2717 }
2718
2719 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002720 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2721 if (opened_exclusive)
2722 return EFI_ACCESS_DENIED;
2723 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2724 if (opened_exclusive || opened_by_driver)
2725 return EFI_ACCESS_DENIED;
2726 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002727
2728 /* Prepare exclusive opening */
2729 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2730 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002731disconnect_next:
2732 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002733 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002734 efi_status_t ret;
2735
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002736 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002737 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2738 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002739 item->info.controller_handle,
2740 item->info.agent_handle,
2741 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002742 if (ret == EFI_SUCCESS)
2743 /*
2744 * Child controllers may have been
2745 * removed from the open_infos list. So
2746 * let's restart the loop.
2747 */
2748 goto disconnect_next;
2749 else
2750 opened_by_driver = true;
2751 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002752 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002753 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002754 if (opened_by_driver)
2755 return EFI_ACCESS_DENIED;
2756 }
2757
2758 /* Find existing entry */
2759 list_for_each_entry(item, &handler->open_infos, link) {
2760 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002761 item->info.controller_handle == controller_handle &&
2762 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002763 match = &item->info;
2764 }
2765 /* None found, create one */
2766 if (!match) {
2767 match = efi_create_open_info(handler);
2768 if (!match)
2769 return EFI_OUT_OF_RESOURCES;
2770 }
2771
2772 match->agent_handle = agent_handle;
2773 match->controller_handle = controller_handle;
2774 match->attributes = attributes;
2775 match->open_count++;
2776
2777out:
2778 /* For TEST_PROTOCOL ignore interface attribute. */
2779 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2780 *protocol_interface = handler->protocol_interface;
2781
2782 return EFI_SUCCESS;
2783}
2784
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002785/**
Mario Six8fac2912018-07-10 08:40:17 +02002786 * efi_open_protocol() - open protocol interface on a handle
2787 * @handle: handle on which the protocol shall be opened
2788 * @protocol: GUID of the protocol
2789 * @protocol_interface: interface implementing the protocol
2790 * @agent_handle: handle of the driver
2791 * @controller_handle: handle of the controller
2792 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002793 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002794 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002795 *
Mario Six8fac2912018-07-10 08:40:17 +02002796 * See the Unified Extensible Firmware Interface (UEFI) specification for
2797 * details.
2798 *
2799 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002800 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002801static efi_status_t EFIAPI efi_open_protocol
2802 (efi_handle_t handle, const efi_guid_t *protocol,
2803 void **protocol_interface, efi_handle_t agent_handle,
2804 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002805{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002806 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002807 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002808
Rob Clark238f88c2017-09-13 18:05:41 -04002809 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002810 protocol_interface, agent_handle, controller_handle,
2811 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002812
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002813 if (!handle || !protocol ||
2814 (!protocol_interface && attributes !=
2815 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002816 goto out;
2817 }
2818
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002819 switch (attributes) {
2820 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2821 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2822 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2823 break;
2824 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2825 if (controller_handle == handle)
2826 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002827 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002828 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2829 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002830 /* Check that the controller handle is valid */
2831 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002832 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002833 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002834 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002835 /* Check that the agent handle is valid */
2836 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002837 goto out;
2838 break;
2839 default:
2840 goto out;
2841 }
2842
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002843 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002844 switch (r) {
2845 case EFI_SUCCESS:
2846 break;
2847 case EFI_NOT_FOUND:
2848 r = EFI_UNSUPPORTED;
2849 goto out;
2850 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002851 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002852 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002853
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002854 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2855 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002856out:
2857 return EFI_EXIT(r);
2858}
2859
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002860/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002861 * efi_start_image() - call the entry point of an image
2862 * @image_handle: handle of the image
2863 * @exit_data_size: size of the buffer
2864 * @exit_data: buffer to receive the exit data of the called image
2865 *
2866 * This function implements the StartImage service.
2867 *
2868 * See the Unified Extensible Firmware Interface (UEFI) specification for
2869 * details.
2870 *
2871 * Return: status code
2872 */
2873efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2874 efi_uintn_t *exit_data_size,
2875 u16 **exit_data)
2876{
2877 struct efi_loaded_image_obj *image_obj =
2878 (struct efi_loaded_image_obj *)image_handle;
2879 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002880 void *info;
2881 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002882
2883 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2884
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002885 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002886 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2887 return EFI_EXIT(EFI_INVALID_PARAMETER);
2888
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002889 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2890 &info, NULL, NULL,
2891 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2892 if (ret != EFI_SUCCESS)
2893 return EFI_EXIT(EFI_INVALID_PARAMETER);
2894
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002895 image_obj->exit_data_size = exit_data_size;
2896 image_obj->exit_data = exit_data;
2897
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002898 /* call the image! */
2899 if (setjmp(&image_obj->exit_jmp)) {
2900 /*
2901 * We called the entry point of the child image with EFI_CALL
2902 * in the lines below. The child image called the Exit() boot
2903 * service efi_exit() which executed the long jump that brought
2904 * us to the current line. This implies that the second half
2905 * of the EFI_CALL macro has not been executed.
2906 */
2907#ifdef CONFIG_ARM
2908 /*
2909 * efi_exit() called efi_restore_gd(). We have to undo this
2910 * otherwise __efi_entry_check() will put the wrong value into
2911 * app_gd.
2912 */
2913 gd = app_gd;
2914#endif
2915 /*
2916 * To get ready to call EFI_EXIT below we have to execute the
2917 * missed out steps of EFI_CALL.
2918 */
2919 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002920 EFI_PRINT("%lu returned by started image\n",
2921 (unsigned long)((uintptr_t)image_obj->exit_status &
2922 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002923 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002924 return EFI_EXIT(image_obj->exit_status);
2925 }
2926
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002927 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002928 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002929 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002930 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2931
2932 /*
2933 * Usually UEFI applications call Exit() instead of returning.
2934 * But because the world doesn't consist of ponies and unicorns,
2935 * we're happy to emulate that behavior on behalf of a payload
2936 * that forgot.
2937 */
2938 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2939}
2940
2941/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002942 * efi_delete_image() - delete loaded image from memory)
2943 *
2944 * @image_obj: handle of the loaded image
2945 * @loaded_image_protocol: loaded image protocol
2946 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002947static efi_status_t efi_delete_image
2948 (struct efi_loaded_image_obj *image_obj,
2949 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002950{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002951 struct efi_object *efiobj;
2952 efi_status_t r, ret = EFI_SUCCESS;
2953
2954close_next:
2955 list_for_each_entry(efiobj, &efi_obj_list, link) {
2956 struct efi_handler *protocol;
2957
2958 list_for_each_entry(protocol, &efiobj->protocols, link) {
2959 struct efi_open_protocol_info_item *info;
2960
2961 list_for_each_entry(info, &protocol->open_infos, link) {
2962 if (info->info.agent_handle !=
2963 (efi_handle_t)image_obj)
2964 continue;
2965 r = EFI_CALL(efi_close_protocol
2966 (efiobj, protocol->guid,
2967 info->info.agent_handle,
2968 info->info.controller_handle
2969 ));
2970 if (r != EFI_SUCCESS)
2971 ret = r;
2972 /*
2973 * Closing protocols may results in further
2974 * items being deleted. To play it safe loop
2975 * over all elements again.
2976 */
2977 goto close_next;
2978 }
2979 }
2980 }
2981
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002982 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2983 efi_size_in_pages(loaded_image_protocol->image_size));
2984 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002985
2986 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002987}
2988
2989/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002990 * efi_unload_image() - unload an EFI image
2991 * @image_handle: handle of the image to be unloaded
2992 *
2993 * This function implements the UnloadImage service.
2994 *
2995 * See the Unified Extensible Firmware Interface (UEFI) specification for
2996 * details.
2997 *
2998 * Return: status code
2999 */
3000efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3001{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003002 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003003 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003004 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003005
3006 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003007
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003008 efiobj = efi_search_obj(image_handle);
3009 if (!efiobj) {
3010 ret = EFI_INVALID_PARAMETER;
3011 goto out;
3012 }
3013 /* Find the loaded image protocol */
3014 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3015 (void **)&loaded_image_protocol,
3016 NULL, NULL,
3017 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3018 if (ret != EFI_SUCCESS) {
3019 ret = EFI_INVALID_PARAMETER;
3020 goto out;
3021 }
3022 switch (efiobj->type) {
3023 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3024 /* Call the unload function */
3025 if (!loaded_image_protocol->unload) {
3026 ret = EFI_UNSUPPORTED;
3027 goto out;
3028 }
3029 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3030 if (ret != EFI_SUCCESS)
3031 goto out;
3032 break;
3033 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3034 break;
3035 default:
3036 ret = EFI_INVALID_PARAMETER;
3037 goto out;
3038 }
3039 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3040 loaded_image_protocol);
3041out:
3042 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003043}
3044
3045/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003046 * efi_update_exit_data() - fill exit data parameters of StartImage()
3047 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003048 * @image_obj: image handle
3049 * @exit_data_size: size of the exit data buffer
3050 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003051 * Return: status code
3052 */
3053static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3054 efi_uintn_t exit_data_size,
3055 u16 *exit_data)
3056{
3057 efi_status_t ret;
3058
3059 /*
3060 * If exit_data is not provided to StartImage(), exit_data_size must be
3061 * ignored.
3062 */
3063 if (!image_obj->exit_data)
3064 return EFI_SUCCESS;
3065 if (image_obj->exit_data_size)
3066 *image_obj->exit_data_size = exit_data_size;
3067 if (exit_data_size && exit_data) {
3068 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3069 exit_data_size,
3070 (void **)image_obj->exit_data);
3071 if (ret != EFI_SUCCESS)
3072 return ret;
3073 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3074 } else {
3075 image_obj->exit_data = NULL;
3076 }
3077 return EFI_SUCCESS;
3078}
3079
3080/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003081 * efi_exit() - leave an EFI application or driver
3082 * @image_handle: handle of the application or driver that is exiting
3083 * @exit_status: status code
3084 * @exit_data_size: size of the buffer in bytes
3085 * @exit_data: buffer with data describing an error
3086 *
3087 * This function implements the Exit service.
3088 *
3089 * See the Unified Extensible Firmware Interface (UEFI) specification for
3090 * details.
3091 *
3092 * Return: status code
3093 */
3094static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3095 efi_status_t exit_status,
3096 efi_uintn_t exit_data_size,
3097 u16 *exit_data)
3098{
3099 /*
3100 * TODO: We should call the unload procedure of the loaded
3101 * image protocol.
3102 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003103 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003104 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003105 struct efi_loaded_image_obj *image_obj =
3106 (struct efi_loaded_image_obj *)image_handle;
3107
3108 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3109 exit_data_size, exit_data);
3110
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003111 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003112 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003113 (void **)&loaded_image_protocol,
3114 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003115 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003116 if (ret != EFI_SUCCESS) {
3117 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003118 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003119 }
3120
3121 /* Unloading of unstarted images */
3122 switch (image_obj->header.type) {
3123 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3124 break;
3125 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3126 efi_delete_image(image_obj, loaded_image_protocol);
3127 ret = EFI_SUCCESS;
3128 goto out;
3129 default:
3130 /* Handle does not refer to loaded image */
3131 ret = EFI_INVALID_PARAMETER;
3132 goto out;
3133 }
3134 /* A started image can only be unloaded it is the last one started. */
3135 if (image_handle != current_image) {
3136 ret = EFI_INVALID_PARAMETER;
3137 goto out;
3138 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003139
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003140 /* Exit data is only foreseen in case of failure. */
3141 if (exit_status != EFI_SUCCESS) {
3142 ret = efi_update_exit_data(image_obj, exit_data_size,
3143 exit_data);
3144 /* Exiting has priority. Don't return error to caller. */
3145 if (ret != EFI_SUCCESS)
3146 EFI_PRINT("%s: out of memory\n", __func__);
3147 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003148 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3149 exit_status != EFI_SUCCESS)
3150 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003151
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003152 /* Make sure entry/exit counts for EFI world cross-overs match */
3153 EFI_EXIT(exit_status);
3154
3155 /*
3156 * But longjmp out with the U-Boot gd, not the application's, as
3157 * the other end is a setjmp call inside EFI context.
3158 */
3159 efi_restore_gd();
3160
3161 image_obj->exit_status = exit_status;
3162 longjmp(&image_obj->exit_jmp, 1);
3163
3164 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003165out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003166 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003167}
3168
3169/**
Mario Six8fac2912018-07-10 08:40:17 +02003170 * efi_handle_protocol() - get interface of a protocol on a handle
3171 * @handle: handle on which the protocol shall be opened
3172 * @protocol: GUID of the protocol
3173 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003174 *
3175 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003176 *
3177 * See the Unified Extensible Firmware Interface (UEFI) specification for
3178 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003179 *
Mario Six8fac2912018-07-10 08:40:17 +02003180 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003181 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01003182static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02003183 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003184 void **protocol_interface)
3185{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003186 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003187 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003188}
3189
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003190/**
Mario Six8fac2912018-07-10 08:40:17 +02003191 * efi_bind_controller() - bind a single driver to a controller
3192 * @controller_handle: controller handle
3193 * @driver_image_handle: driver handle
3194 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003195 *
Mario Six8fac2912018-07-10 08:40:17 +02003196 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003197 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003198static efi_status_t efi_bind_controller(
3199 efi_handle_t controller_handle,
3200 efi_handle_t driver_image_handle,
3201 struct efi_device_path *remain_device_path)
3202{
3203 struct efi_driver_binding_protocol *binding_protocol;
3204 efi_status_t r;
3205
3206 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3207 &efi_guid_driver_binding_protocol,
3208 (void **)&binding_protocol,
3209 driver_image_handle, NULL,
3210 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3211 if (r != EFI_SUCCESS)
3212 return r;
3213 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3214 controller_handle,
3215 remain_device_path));
3216 if (r == EFI_SUCCESS)
3217 r = EFI_CALL(binding_protocol->start(binding_protocol,
3218 controller_handle,
3219 remain_device_path));
3220 EFI_CALL(efi_close_protocol(driver_image_handle,
3221 &efi_guid_driver_binding_protocol,
3222 driver_image_handle, NULL));
3223 return r;
3224}
3225
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003226/**
Mario Six8fac2912018-07-10 08:40:17 +02003227 * efi_connect_single_controller() - connect a single driver to a controller
3228 * @controller_handle: controller
3229 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003230 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003231 *
Mario Six8fac2912018-07-10 08:40:17 +02003232 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003233 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003234static efi_status_t efi_connect_single_controller(
3235 efi_handle_t controller_handle,
3236 efi_handle_t *driver_image_handle,
3237 struct efi_device_path *remain_device_path)
3238{
3239 efi_handle_t *buffer;
3240 size_t count;
3241 size_t i;
3242 efi_status_t r;
3243 size_t connected = 0;
3244
3245 /* Get buffer with all handles with driver binding protocol */
3246 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3247 &efi_guid_driver_binding_protocol,
3248 NULL, &count, &buffer));
3249 if (r != EFI_SUCCESS)
3250 return r;
3251
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003252 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003253 if (driver_image_handle) {
3254 for (; *driver_image_handle; ++driver_image_handle) {
3255 for (i = 0; i < count; ++i) {
3256 if (buffer[i] == *driver_image_handle) {
3257 buffer[i] = NULL;
3258 r = efi_bind_controller(
3259 controller_handle,
3260 *driver_image_handle,
3261 remain_device_path);
3262 /*
3263 * For drivers that do not support the
3264 * controller or are already connected
3265 * we receive an error code here.
3266 */
3267 if (r == EFI_SUCCESS)
3268 ++connected;
3269 }
3270 }
3271 }
3272 }
3273
3274 /*
3275 * TODO: Some overrides are not yet implemented:
3276 * - Platform Driver Override
3277 * - Driver Family Override Search
3278 * - Bus Specific Driver Override
3279 */
3280
3281 /* Driver Binding Search */
3282 for (i = 0; i < count; ++i) {
3283 if (buffer[i]) {
3284 r = efi_bind_controller(controller_handle,
3285 buffer[i],
3286 remain_device_path);
3287 if (r == EFI_SUCCESS)
3288 ++connected;
3289 }
3290 }
3291
3292 efi_free_pool(buffer);
3293 if (!connected)
3294 return EFI_NOT_FOUND;
3295 return EFI_SUCCESS;
3296}
3297
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003298/**
Mario Six8fac2912018-07-10 08:40:17 +02003299 * efi_connect_controller() - connect a controller to a driver
3300 * @controller_handle: handle of the controller
3301 * @driver_image_handle: handle of the driver
3302 * @remain_device_path: device path of a child controller
3303 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003304 *
3305 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003306 *
3307 * See the Unified Extensible Firmware Interface (UEFI) specification for
3308 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003309 *
3310 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003311 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003312 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3313 *
Mario Six8fac2912018-07-10 08:40:17 +02003314 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003315 */
3316static efi_status_t EFIAPI efi_connect_controller(
3317 efi_handle_t controller_handle,
3318 efi_handle_t *driver_image_handle,
3319 struct efi_device_path *remain_device_path,
3320 bool recursive)
3321{
3322 efi_status_t r;
3323 efi_status_t ret = EFI_NOT_FOUND;
3324 struct efi_object *efiobj;
3325
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003326 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003327 remain_device_path, recursive);
3328
3329 efiobj = efi_search_obj(controller_handle);
3330 if (!efiobj) {
3331 ret = EFI_INVALID_PARAMETER;
3332 goto out;
3333 }
3334
3335 r = efi_connect_single_controller(controller_handle,
3336 driver_image_handle,
3337 remain_device_path);
3338 if (r == EFI_SUCCESS)
3339 ret = EFI_SUCCESS;
3340 if (recursive) {
3341 struct efi_handler *handler;
3342 struct efi_open_protocol_info_item *item;
3343
3344 list_for_each_entry(handler, &efiobj->protocols, link) {
3345 list_for_each_entry(item, &handler->open_infos, link) {
3346 if (item->info.attributes &
3347 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3348 r = EFI_CALL(efi_connect_controller(
3349 item->info.controller_handle,
3350 driver_image_handle,
3351 remain_device_path,
3352 recursive));
3353 if (r == EFI_SUCCESS)
3354 ret = EFI_SUCCESS;
3355 }
3356 }
3357 }
3358 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003359 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003360 if (ret != EFI_SUCCESS && remain_device_path &&
3361 remain_device_path->type == DEVICE_PATH_TYPE_END)
3362 ret = EFI_SUCCESS;
3363out:
3364 return EFI_EXIT(ret);
3365}
3366
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003367/**
Mario Six8fac2912018-07-10 08:40:17 +02003368 * efi_reinstall_protocol_interface() - reinstall protocol interface
3369 * @handle: handle on which the protocol shall be reinstalled
3370 * @protocol: GUID of the protocol to be installed
3371 * @old_interface: interface to be removed
3372 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003373 *
3374 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003375 *
3376 * See the Unified Extensible Firmware Interface (UEFI) specification for
3377 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003378 *
3379 * The old interface is uninstalled. The new interface is installed.
3380 * Drivers are connected.
3381 *
Mario Six8fac2912018-07-10 08:40:17 +02003382 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003383 */
3384static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3385 efi_handle_t handle, const efi_guid_t *protocol,
3386 void *old_interface, void *new_interface)
3387{
3388 efi_status_t ret;
3389
3390 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3391 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003392
3393 /* Uninstall protocol but do not delete handle */
3394 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003395 if (ret != EFI_SUCCESS)
3396 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003397
3398 /* Install the new protocol */
3399 ret = efi_add_protocol(handle, protocol, new_interface);
3400 /*
3401 * The UEFI spec does not specify what should happen to the handle
3402 * if in case of an error no protocol interface remains on the handle.
3403 * So let's do nothing here.
3404 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003405 if (ret != EFI_SUCCESS)
3406 goto out;
3407 /*
3408 * The returned status code has to be ignored.
3409 * Do not create an error if no suitable driver for the handle exists.
3410 */
3411 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3412out:
3413 return EFI_EXIT(ret);
3414}
3415
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003416/**
Mario Six8fac2912018-07-10 08:40:17 +02003417 * efi_get_child_controllers() - get all child controllers associated to a driver
3418 * @efiobj: handle of the controller
3419 * @driver_handle: handle of the driver
3420 * @number_of_children: number of child controllers
3421 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003422 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003423 * The allocated buffer has to be freed with free().
3424 *
Mario Six8fac2912018-07-10 08:40:17 +02003425 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003426 */
3427static efi_status_t efi_get_child_controllers(
3428 struct efi_object *efiobj,
3429 efi_handle_t driver_handle,
3430 efi_uintn_t *number_of_children,
3431 efi_handle_t **child_handle_buffer)
3432{
3433 struct efi_handler *handler;
3434 struct efi_open_protocol_info_item *item;
3435 efi_uintn_t count = 0, i;
3436 bool duplicate;
3437
3438 /* Count all child controller associations */
3439 list_for_each_entry(handler, &efiobj->protocols, link) {
3440 list_for_each_entry(item, &handler->open_infos, link) {
3441 if (item->info.agent_handle == driver_handle &&
3442 item->info.attributes &
3443 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3444 ++count;
3445 }
3446 }
3447 /*
3448 * Create buffer. In case of duplicate child controller assignments
3449 * the buffer will be too large. But that does not harm.
3450 */
3451 *number_of_children = 0;
3452 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3453 if (!*child_handle_buffer)
3454 return EFI_OUT_OF_RESOURCES;
3455 /* Copy unique child handles */
3456 list_for_each_entry(handler, &efiobj->protocols, link) {
3457 list_for_each_entry(item, &handler->open_infos, link) {
3458 if (item->info.agent_handle == driver_handle &&
3459 item->info.attributes &
3460 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3461 /* Check this is a new child controller */
3462 duplicate = false;
3463 for (i = 0; i < *number_of_children; ++i) {
3464 if ((*child_handle_buffer)[i] ==
3465 item->info.controller_handle)
3466 duplicate = true;
3467 }
3468 /* Copy handle to buffer */
3469 if (!duplicate) {
3470 i = (*number_of_children)++;
3471 (*child_handle_buffer)[i] =
3472 item->info.controller_handle;
3473 }
3474 }
3475 }
3476 }
3477 return EFI_SUCCESS;
3478}
3479
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003480/**
Mario Six8fac2912018-07-10 08:40:17 +02003481 * efi_disconnect_controller() - disconnect a controller from a driver
3482 * @controller_handle: handle of the controller
3483 * @driver_image_handle: handle of the driver
3484 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003485 *
3486 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003487 *
3488 * See the Unified Extensible Firmware Interface (UEFI) specification for
3489 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003490 *
Mario Six8fac2912018-07-10 08:40:17 +02003491 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003492 */
3493static efi_status_t EFIAPI efi_disconnect_controller(
3494 efi_handle_t controller_handle,
3495 efi_handle_t driver_image_handle,
3496 efi_handle_t child_handle)
3497{
3498 struct efi_driver_binding_protocol *binding_protocol;
3499 efi_handle_t *child_handle_buffer = NULL;
3500 size_t number_of_children = 0;
3501 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003502 struct efi_object *efiobj;
3503
3504 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3505 child_handle);
3506
3507 efiobj = efi_search_obj(controller_handle);
3508 if (!efiobj) {
3509 r = EFI_INVALID_PARAMETER;
3510 goto out;
3511 }
3512
3513 if (child_handle && !efi_search_obj(child_handle)) {
3514 r = EFI_INVALID_PARAMETER;
3515 goto out;
3516 }
3517
3518 /* If no driver handle is supplied, disconnect all drivers */
3519 if (!driver_image_handle) {
3520 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3521 goto out;
3522 }
3523
3524 /* Create list of child handles */
3525 if (child_handle) {
3526 number_of_children = 1;
3527 child_handle_buffer = &child_handle;
3528 } else {
3529 efi_get_child_controllers(efiobj,
3530 driver_image_handle,
3531 &number_of_children,
3532 &child_handle_buffer);
3533 }
3534
3535 /* Get the driver binding protocol */
3536 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3537 &efi_guid_driver_binding_protocol,
3538 (void **)&binding_protocol,
3539 driver_image_handle, NULL,
3540 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003541 if (r != EFI_SUCCESS) {
3542 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003543 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003544 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003545 /* Remove the children */
3546 if (number_of_children) {
3547 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3548 controller_handle,
3549 number_of_children,
3550 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003551 if (r != EFI_SUCCESS) {
3552 r = EFI_DEVICE_ERROR;
3553 goto out;
3554 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003555 }
3556 /* Remove the driver */
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003557 if (!child_handle) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003558 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3559 controller_handle,
3560 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003561 if (r != EFI_SUCCESS) {
3562 r = EFI_DEVICE_ERROR;
3563 goto out;
3564 }
3565 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003566 EFI_CALL(efi_close_protocol(driver_image_handle,
3567 &efi_guid_driver_binding_protocol,
3568 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003569 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003570out:
3571 if (!child_handle)
3572 free(child_handle_buffer);
3573 return EFI_EXIT(r);
3574}
3575
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003576static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003577 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003578 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3579 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003580 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003581 },
3582 .raise_tpl = efi_raise_tpl,
3583 .restore_tpl = efi_restore_tpl,
3584 .allocate_pages = efi_allocate_pages_ext,
3585 .free_pages = efi_free_pages_ext,
3586 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003587 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003588 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003589 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003590 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003591 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003592 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003593 .close_event = efi_close_event,
3594 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003595 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003596 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003597 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003598 .handle_protocol = efi_handle_protocol,
3599 .reserved = NULL,
3600 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003601 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003602 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003603 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003604 .load_image = efi_load_image,
3605 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003606 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003607 .unload_image = efi_unload_image,
3608 .exit_boot_services = efi_exit_boot_services,
3609 .get_next_monotonic_count = efi_get_next_monotonic_count,
3610 .stall = efi_stall,
3611 .set_watchdog_timer = efi_set_watchdog_timer,
3612 .connect_controller = efi_connect_controller,
3613 .disconnect_controller = efi_disconnect_controller,
3614 .open_protocol = efi_open_protocol,
3615 .close_protocol = efi_close_protocol,
3616 .open_protocol_information = efi_open_protocol_information,
3617 .protocols_per_handle = efi_protocols_per_handle,
3618 .locate_handle_buffer = efi_locate_handle_buffer,
3619 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003620 .install_multiple_protocol_interfaces =
3621 efi_install_multiple_protocol_interfaces,
3622 .uninstall_multiple_protocol_interfaces =
3623 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003624 .calculate_crc32 = efi_calculate_crc32,
3625 .copy_mem = efi_copy_mem,
3626 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003627 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003628};
3629
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003630static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003631
Alexander Graf393dd912016-10-14 13:45:30 +02003632struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003633 .hdr = {
3634 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003635 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003636 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003637 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003638 .fw_vendor = firmware_vendor,
3639 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003640 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003641 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003642 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003643};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003644
3645/**
3646 * efi_initialize_system_table() - Initialize system table
3647 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003648 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003649 */
3650efi_status_t efi_initialize_system_table(void)
3651{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003652 efi_status_t ret;
3653
3654 /* Allocate configuration table array */
3655 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3656 EFI_MAX_CONFIGURATION_TABLES *
3657 sizeof(struct efi_configuration_table),
3658 (void **)&systab.tables);
3659
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003660 /*
3661 * These entries will be set to NULL in ExitBootServices(). To avoid
3662 * relocation in SetVirtualAddressMap(), set them dynamically.
3663 */
3664 systab.con_in = &efi_con_in;
3665 systab.con_out = &efi_con_out;
3666 systab.std_err = &efi_con_out;
3667 systab.boottime = &efi_boot_services;
3668
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003669 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003670 efi_update_table_header_crc32(&systab.hdr);
3671 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3672 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003673
3674 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003675}