blob: 1591ad830078ff04080ce244a8ebcede307c7dbe [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>
Simon Glass8f3f7612019-11-14 12:57:42 -070011#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070014#include <time.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090015#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <u-boot/crc.h>
17#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020018#include <pe.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010020#include <watchdog.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020024/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010025static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020026
Alexander Grafc15d9212016-03-04 01:09:59 +010027/* This list contains all the EFI objects our payload has access to */
28LIST_HEAD(efi_obj_list);
29
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010030/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020031__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010032
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020033/* List of queued events */
34LIST_HEAD(efi_event_queue);
35
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020036/* Flag to disable timer activity in ExitBootServices() */
37static bool timers_enabled = true;
38
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020039/* List of all events registered by RegisterProtocolNotify() */
40LIST_HEAD(efi_register_notify_events);
41
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010042/* Handle of the currently executing image */
43static efi_handle_t current_image;
44
Simon Glasscdfe6962016-09-25 15:27:35 -060045#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010046/*
47 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
48 * fixed when compiling U-Boot. However, the payload does not know about that
49 * restriction so we need to manually swap its and our view of that register on
50 * EFI callback entry/exit.
51 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020052static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060053#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010054
Heinrich Schuchardt72506232019-02-09 14:10:39 +010055/* 1 if inside U-Boot code, 0 if inside EFI payload code */
56static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040057static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010058/* GUID of the device tree table */
59const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010060/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
61const efi_guid_t efi_guid_driver_binding_protocol =
62 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040063
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010064/* event group ExitBootServices() invoked */
65const efi_guid_t efi_guid_event_group_exit_boot_services =
66 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
67/* event group SetVirtualAddressMap() invoked */
68const efi_guid_t efi_guid_event_group_virtual_address_change =
69 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
70/* event group memory map changed */
71const efi_guid_t efi_guid_event_group_memory_map_change =
72 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
73/* event group boot manager about to boot */
74const efi_guid_t efi_guid_event_group_ready_to_boot =
75 EFI_EVENT_GROUP_READY_TO_BOOT;
76/* event group ResetSystem() invoked (before ExitBootServices) */
77const efi_guid_t efi_guid_event_group_reset_system =
78 EFI_EVENT_GROUP_RESET_SYSTEM;
79
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010080static efi_status_t EFIAPI efi_disconnect_controller(
81 efi_handle_t controller_handle,
82 efi_handle_t driver_image_handle,
83 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010084
Rob Clark86789d52017-07-27 08:04:18 -040085/* Called on every callback entry */
86int __efi_entry_check(void)
87{
88 int ret = entry_count++ == 0;
89#ifdef CONFIG_ARM
90 assert(efi_gd);
91 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020092 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -040093#endif
94 return ret;
95}
96
97/* Called on every callback exit */
98int __efi_exit_check(void)
99{
100 int ret = --entry_count == 0;
101#ifdef CONFIG_ARM
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200102 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400103#endif
104 return ret;
105}
106
Alexander Grafc15d9212016-03-04 01:09:59 +0100107/* Called from do_bootefi_exec() */
108void efi_save_gd(void)
109{
Simon Glasscdfe6962016-09-25 15:27:35 -0600110#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100111 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600112#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100113}
114
Rob Clark86789d52017-07-27 08:04:18 -0400115/*
Mario Six8fac2912018-07-10 08:40:17 +0200116 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200117 * world so we can dump out an abort message, without any care about returning
118 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400119 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100120void efi_restore_gd(void)
121{
Simon Glasscdfe6962016-09-25 15:27:35 -0600122#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100123 /* Only restore if we're already in EFI context */
124 if (!efi_gd)
125 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200126 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600127#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100128}
129
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200130/**
Mario Six8fac2912018-07-10 08:40:17 +0200131 * indent_string() - returns a string for indenting with two spaces per level
132 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100133 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200134 * A maximum of ten indent levels is supported. Higher indent levels will be
135 * truncated.
136 *
Mario Six8fac2912018-07-10 08:40:17 +0200137 * Return: A string for indenting with two spaces per level is
138 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400139 */
140static const char *indent_string(int level)
141{
142 const char *indent = " ";
143 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100144
Rob Clarke7896c32017-07-27 08:04:19 -0400145 level = min(max, level * 2);
146 return &indent[max - level];
147}
148
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200149const char *__efi_nesting(void)
150{
151 return indent_string(nesting_level);
152}
153
Rob Clarke7896c32017-07-27 08:04:19 -0400154const char *__efi_nesting_inc(void)
155{
156 return indent_string(nesting_level++);
157}
158
159const char *__efi_nesting_dec(void)
160{
161 return indent_string(--nesting_level);
162}
163
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200164/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200165 * efi_event_is_queued() - check if an event is queued
166 *
167 * @event: event
168 * Return: true if event is queued
169 */
170static bool efi_event_is_queued(struct efi_event *event)
171{
172 return !!event->queue_link.next;
173}
174
175/**
176 * efi_process_event_queue() - process event queue
177 */
178static void efi_process_event_queue(void)
179{
180 while (!list_empty(&efi_event_queue)) {
181 struct efi_event *event;
182 efi_uintn_t old_tpl;
183
184 event = list_first_entry(&efi_event_queue, struct efi_event,
185 queue_link);
186 if (efi_tpl >= event->notify_tpl)
187 return;
188 list_del(&event->queue_link);
189 event->queue_link.next = NULL;
190 event->queue_link.prev = NULL;
191 /* Events must be executed at the event's TPL */
192 old_tpl = efi_tpl;
193 efi_tpl = event->notify_tpl;
194 EFI_CALL_VOID(event->notify_function(event,
195 event->notify_context));
196 efi_tpl = old_tpl;
197 if (event->type == EVT_NOTIFY_SIGNAL)
198 event->is_signaled = 0;
199 }
200}
201
202/**
Mario Six8fac2912018-07-10 08:40:17 +0200203 * efi_queue_event() - queue an EFI event
204 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200205 *
206 * This function queues the notification function of the event for future
207 * execution.
208 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200209 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200210static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200211{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100212 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200213
214 if (!event->notify_function)
215 return;
216
217 if (!efi_event_is_queued(event)) {
218 /*
219 * Events must be notified in order of decreasing task priority
220 * level. Insert the new event accordingly.
221 */
222 list_for_each_entry(item, &efi_event_queue, queue_link) {
223 if (item->notify_tpl < event->notify_tpl) {
224 list_add_tail(&event->queue_link,
225 &item->queue_link);
226 event = NULL;
227 break;
228 }
229 }
230 if (event)
231 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200232 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200233 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200234}
235
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200236/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200237 * is_valid_tpl() - check if the task priority level is valid
238 *
239 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200240 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200241 */
242efi_status_t is_valid_tpl(efi_uintn_t tpl)
243{
244 switch (tpl) {
245 case TPL_APPLICATION:
246 case TPL_CALLBACK:
247 case TPL_NOTIFY:
248 case TPL_HIGH_LEVEL:
249 return EFI_SUCCESS;
250 default:
251 return EFI_INVALID_PARAMETER;
252 }
253}
254
255/**
Mario Six8fac2912018-07-10 08:40:17 +0200256 * efi_signal_event() - signal an EFI event
257 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100258 *
Mario Six8fac2912018-07-10 08:40:17 +0200259 * This function signals an event. If the event belongs to an event group all
260 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100261 * their notification function is queued.
262 *
263 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100264 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200265void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100266{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200267 if (event->is_signaled)
268 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100269 if (event->group) {
270 struct efi_event *evt;
271
272 /*
273 * The signaled state has to set before executing any
274 * notification function
275 */
276 list_for_each_entry(evt, &efi_events, link) {
277 if (!evt->group || guidcmp(evt->group, event->group))
278 continue;
279 if (evt->is_signaled)
280 continue;
281 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100282 }
283 list_for_each_entry(evt, &efi_events, link) {
284 if (!evt->group || guidcmp(evt->group, event->group))
285 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200286 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100287 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200288 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100289 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200290 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100291 }
292}
293
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200294/**
Mario Six8fac2912018-07-10 08:40:17 +0200295 * efi_raise_tpl() - raise the task priority level
296 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200297 *
298 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200299 *
300 * See the Unified Extensible Firmware Interface (UEFI) specification for
301 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200302 *
Mario Six8fac2912018-07-10 08:40:17 +0200303 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200304 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100305static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100306{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100307 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200308
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200309 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200310
311 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200312 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200313 efi_tpl = new_tpl;
314 if (efi_tpl > TPL_HIGH_LEVEL)
315 efi_tpl = TPL_HIGH_LEVEL;
316
317 EFI_EXIT(EFI_SUCCESS);
318 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100319}
320
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200321/**
Mario Six8fac2912018-07-10 08:40:17 +0200322 * efi_restore_tpl() - lower the task priority level
323 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 *
325 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200326 *
Mario Six8fac2912018-07-10 08:40:17 +0200327 * See the Unified Extensible Firmware Interface (UEFI) specification for
328 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200329 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100330static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100331{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200332 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200333
334 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200335 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200336 efi_tpl = old_tpl;
337 if (efi_tpl > TPL_HIGH_LEVEL)
338 efi_tpl = TPL_HIGH_LEVEL;
339
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100340 /*
341 * Lowering the TPL may have made queued events eligible for execution.
342 */
343 efi_timer_check();
344
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200345 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100346}
347
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200348/**
Mario Six8fac2912018-07-10 08:40:17 +0200349 * efi_allocate_pages_ext() - allocate memory pages
350 * @type: type of allocation to be performed
351 * @memory_type: usage type of the allocated memory
352 * @pages: number of pages to be allocated
353 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200354 *
355 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200356 *
Mario Six8fac2912018-07-10 08:40:17 +0200357 * See the Unified Extensible Firmware Interface (UEFI) specification for
358 * details.
359 *
360 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200361 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900362static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100363 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900364 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100365{
366 efi_status_t r;
367
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100368 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100369 r = efi_allocate_pages(type, memory_type, pages, memory);
370 return EFI_EXIT(r);
371}
372
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200373/**
Mario Six8fac2912018-07-10 08:40:17 +0200374 * efi_free_pages_ext() - Free memory pages.
375 * @memory: start of the memory area to be freed
376 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200377 *
378 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200379 *
380 * See the Unified Extensible Firmware Interface (UEFI) specification for
381 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200382 *
Mario Six8fac2912018-07-10 08:40:17 +0200383 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200384 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900385static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100386 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100387{
388 efi_status_t r;
389
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900390 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100391 r = efi_free_pages(memory, pages);
392 return EFI_EXIT(r);
393}
394
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200395/**
Mario Six8fac2912018-07-10 08:40:17 +0200396 * efi_get_memory_map_ext() - get map describing memory usage
397 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
398 * on exit the size of the copied memory map
399 * @memory_map: buffer to which the memory map is written
400 * @map_key: key for the memory map
401 * @descriptor_size: size of an individual memory descriptor
402 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200403 *
404 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200405 *
Mario Six8fac2912018-07-10 08:40:17 +0200406 * See the Unified Extensible Firmware Interface (UEFI) specification for
407 * details.
408 *
409 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200410 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900411static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100412 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900413 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100414 efi_uintn_t *map_key,
415 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900416 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100417{
418 efi_status_t r;
419
420 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
421 map_key, descriptor_size, descriptor_version);
422 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
423 descriptor_size, descriptor_version);
424 return EFI_EXIT(r);
425}
426
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200427/**
Mario Six8fac2912018-07-10 08:40:17 +0200428 * efi_allocate_pool_ext() - allocate memory from pool
429 * @pool_type: type of the pool from which memory is to be allocated
430 * @size: number of bytes to be allocated
431 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200432 *
433 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200434 *
435 * See the Unified Extensible Firmware Interface (UEFI) specification for
436 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200437 *
Mario Six8fac2912018-07-10 08:40:17 +0200438 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200439 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200440static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100441 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200442 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100443{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100444 efi_status_t r;
445
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100446 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200447 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100448 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100449}
450
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200451/**
Mario Six8fac2912018-07-10 08:40:17 +0200452 * efi_free_pool_ext() - free memory from pool
453 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200454 *
455 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200456 *
Mario Six8fac2912018-07-10 08:40:17 +0200457 * See the Unified Extensible Firmware Interface (UEFI) specification for
458 * details.
459 *
460 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200461 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200462static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100463{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100464 efi_status_t r;
465
466 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200467 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100468 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100469}
470
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200471/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200472 * efi_add_handle() - add a new handle to the object list
473 *
474 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100475 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200476 * The protocols list is initialized. The handle is added to the list of known
477 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100478 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200479void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100480{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200481 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100482 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200483 INIT_LIST_HEAD(&handle->protocols);
484 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100485}
486
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200487/**
Mario Six8fac2912018-07-10 08:40:17 +0200488 * efi_create_handle() - create handle
489 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200490 *
Mario Six8fac2912018-07-10 08:40:17 +0200491 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200492 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100493efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200494{
495 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200496
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200497 obj = calloc(1, sizeof(struct efi_object));
498 if (!obj)
499 return EFI_OUT_OF_RESOURCES;
500
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100501 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200502 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200503
504 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200505}
506
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200507/**
Mario Six8fac2912018-07-10 08:40:17 +0200508 * efi_search_protocol() - find a protocol on a handle.
509 * @handle: handle
510 * @protocol_guid: GUID of the protocol
511 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100512 *
Mario Six8fac2912018-07-10 08:40:17 +0200513 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100514 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100515efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100516 const efi_guid_t *protocol_guid,
517 struct efi_handler **handler)
518{
519 struct efi_object *efiobj;
520 struct list_head *lhandle;
521
522 if (!handle || !protocol_guid)
523 return EFI_INVALID_PARAMETER;
524 efiobj = efi_search_obj(handle);
525 if (!efiobj)
526 return EFI_INVALID_PARAMETER;
527 list_for_each(lhandle, &efiobj->protocols) {
528 struct efi_handler *protocol;
529
530 protocol = list_entry(lhandle, struct efi_handler, link);
531 if (!guidcmp(protocol->guid, protocol_guid)) {
532 if (handler)
533 *handler = protocol;
534 return EFI_SUCCESS;
535 }
536 }
537 return EFI_NOT_FOUND;
538}
539
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200540/**
Mario Six8fac2912018-07-10 08:40:17 +0200541 * efi_remove_protocol() - delete protocol from a handle
542 * @handle: handle from which the protocol shall be deleted
543 * @protocol: GUID of the protocol to be deleted
544 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100545 *
Mario Six8fac2912018-07-10 08:40:17 +0200546 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100547 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100548efi_status_t efi_remove_protocol(const efi_handle_t handle,
549 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100550 void *protocol_interface)
551{
552 struct efi_handler *handler;
553 efi_status_t ret;
554
555 ret = efi_search_protocol(handle, protocol, &handler);
556 if (ret != EFI_SUCCESS)
557 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200558 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200559 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100560 list_del(&handler->link);
561 free(handler);
562 return EFI_SUCCESS;
563}
564
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200565/**
Mario Six8fac2912018-07-10 08:40:17 +0200566 * efi_remove_all_protocols() - delete all protocols from a handle
567 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100568 *
Mario Six8fac2912018-07-10 08:40:17 +0200569 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100570 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100571efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100572{
573 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100574 struct efi_handler *protocol;
575 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100576
577 efiobj = efi_search_obj(handle);
578 if (!efiobj)
579 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100580 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100581 efi_status_t ret;
582
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100583 ret = efi_remove_protocol(handle, protocol->guid,
584 protocol->protocol_interface);
585 if (ret != EFI_SUCCESS)
586 return ret;
587 }
588 return EFI_SUCCESS;
589}
590
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200591/**
Mario Six8fac2912018-07-10 08:40:17 +0200592 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100593 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200594 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100595 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200596void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100597{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200598 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200600 efi_remove_all_protocols(handle);
601 list_del(&handle->link);
602 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100603}
604
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200605/**
Mario Six8fac2912018-07-10 08:40:17 +0200606 * efi_is_event() - check if a pointer is a valid event
607 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100608 *
Mario Six8fac2912018-07-10 08:40:17 +0200609 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100610 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100611static efi_status_t efi_is_event(const struct efi_event *event)
612{
613 const struct efi_event *evt;
614
615 if (!event)
616 return EFI_INVALID_PARAMETER;
617 list_for_each_entry(evt, &efi_events, link) {
618 if (evt == event)
619 return EFI_SUCCESS;
620 }
621 return EFI_INVALID_PARAMETER;
622}
Alexander Grafc15d9212016-03-04 01:09:59 +0100623
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200624/**
Mario Six8fac2912018-07-10 08:40:17 +0200625 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200626 *
Mario Six8fac2912018-07-10 08:40:17 +0200627 * @type: type of the event to create
628 * @notify_tpl: task priority level of the event
629 * @notify_function: notification function of the event
630 * @notify_context: pointer passed to the notification function
631 * @group: event group
632 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200633 *
634 * This function is used inside U-Boot code to create an event.
635 *
636 * For the API function implementing the CreateEvent service see
637 * efi_create_event_ext.
638 *
Mario Six8fac2912018-07-10 08:40:17 +0200639 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200640 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100641efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200642 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200643 struct efi_event *event,
644 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100645 void *notify_context, efi_guid_t *group,
646 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100647{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100648 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200649 efi_status_t ret;
650 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200651
Jonathan Gray7758b212017-03-12 19:26:07 +1100652 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200653 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100654
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200655 switch (type) {
656 case 0:
657 case EVT_TIMER:
658 case EVT_NOTIFY_SIGNAL:
659 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
660 case EVT_NOTIFY_WAIT:
661 case EVT_TIMER | EVT_NOTIFY_WAIT:
662 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200663 pool_type = EFI_BOOT_SERVICES_DATA;
664 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200665 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200666 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200667 break;
668 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200669 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200670 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100671
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900672 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200673 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200674 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100675
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200676 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
677 (void **)&evt);
678 if (ret != EFI_SUCCESS)
679 return ret;
680 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100681 evt->type = type;
682 evt->notify_tpl = notify_tpl;
683 evt->notify_function = notify_function;
684 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100685 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200686 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100687 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100688 list_add_tail(&evt->link, &efi_events);
689 *event = evt;
690 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100691}
692
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200693/*
Mario Six8fac2912018-07-10 08:40:17 +0200694 * efi_create_event_ex() - create an event in a group
695 * @type: type of the event to create
696 * @notify_tpl: task priority level of the event
697 * @notify_function: notification function of the event
698 * @notify_context: pointer passed to the notification function
699 * @event: created event
700 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100701 *
702 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100703 *
Mario Six8fac2912018-07-10 08:40:17 +0200704 * See the Unified Extensible Firmware Interface (UEFI) specification for
705 * details.
706 *
707 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100708 */
709efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
710 void (EFIAPI *notify_function) (
711 struct efi_event *event,
712 void *context),
713 void *notify_context,
714 efi_guid_t *event_group,
715 struct efi_event **event)
716{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200717 efi_status_t ret;
718
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100719 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
720 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200721
722 /*
723 * The allowable input parameters are the same as in CreateEvent()
724 * except for the following two disallowed event types.
725 */
726 switch (type) {
727 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
728 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
729 ret = EFI_INVALID_PARAMETER;
730 goto out;
731 }
732
733 ret = efi_create_event(type, notify_tpl, notify_function,
734 notify_context, event_group, event);
735out:
736 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100737}
738
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200739/**
Mario Six8fac2912018-07-10 08:40:17 +0200740 * efi_create_event_ext() - create an event
741 * @type: type of the event to create
742 * @notify_tpl: task priority level of the event
743 * @notify_function: notification function of the event
744 * @notify_context: pointer passed to the notification function
745 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200746 *
747 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200748 *
749 * See the Unified Extensible Firmware Interface (UEFI) specification for
750 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200751 *
Mario Six8fac2912018-07-10 08:40:17 +0200752 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200753 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200754static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100755 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200756 void (EFIAPI *notify_function) (
757 struct efi_event *event,
758 void *context),
759 void *notify_context, struct efi_event **event)
760{
761 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
762 notify_context);
763 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100764 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200765}
766
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200767/**
Mario Six8fac2912018-07-10 08:40:17 +0200768 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200769 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200770 * Check if a timer event has occurred or a queued notification function should
771 * be called.
772 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100773 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200774 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100775 */
776void efi_timer_check(void)
777{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100778 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100779 u64 now = timer_get_us();
780
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100781 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200782 if (!timers_enabled)
783 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100784 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200785 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100786 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200787 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100788 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200789 break;
790 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100791 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200792 break;
793 default:
794 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200795 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100796 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200797 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100798 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200799 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100800 WATCHDOG_RESET();
801}
802
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200803/**
Mario Six8fac2912018-07-10 08:40:17 +0200804 * efi_set_timer() - set the trigger time for a timer event or stop the event
805 * @event: event for which the timer is set
806 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200807 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200808 *
809 * This is the function for internal usage in U-Boot. For the API function
810 * implementing the SetTimer service see efi_set_timer_ext.
811 *
Mario Six8fac2912018-07-10 08:40:17 +0200812 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200813 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200814efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200815 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100816{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100817 /* Check that the event is valid */
818 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
819 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100820
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200821 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200822 * The parameter defines a multiple of 100 ns.
823 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200824 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200825 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100826
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100827 switch (type) {
828 case EFI_TIMER_STOP:
829 event->trigger_next = -1ULL;
830 break;
831 case EFI_TIMER_PERIODIC:
832 case EFI_TIMER_RELATIVE:
833 event->trigger_next = timer_get_us() + trigger_time;
834 break;
835 default:
836 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100837 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100838 event->trigger_type = type;
839 event->trigger_time = trigger_time;
840 event->is_signaled = false;
841 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100842}
843
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200844/**
Mario Six8fac2912018-07-10 08:40:17 +0200845 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
846 * event
847 * @event: event for which the timer is set
848 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200849 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200850 *
851 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200852 *
Mario Six8fac2912018-07-10 08:40:17 +0200853 * See the Unified Extensible Firmware Interface (UEFI) specification for
854 * details.
855 *
856 *
857 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200858 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200859static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
860 enum efi_timer_delay type,
861 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200862{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900863 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200864 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
865}
866
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200867/**
Mario Six8fac2912018-07-10 08:40:17 +0200868 * efi_wait_for_event() - wait for events to be signaled
869 * @num_events: number of events to be waited for
870 * @event: events to be waited for
871 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200872 *
873 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200874 *
Mario Six8fac2912018-07-10 08:40:17 +0200875 * See the Unified Extensible Firmware Interface (UEFI) specification for
876 * details.
877 *
878 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200879 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100880static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200881 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100882 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100883{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100884 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100885
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100886 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100887
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200888 /* Check parameters */
889 if (!num_events || !event)
890 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200891 /* Check TPL */
892 if (efi_tpl != TPL_APPLICATION)
893 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200894 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100895 if (efi_is_event(event[i]) != EFI_SUCCESS)
896 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200897 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
898 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200899 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200900 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200901 }
902
903 /* Wait for signal */
904 for (;;) {
905 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200906 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200907 goto out;
908 }
909 /* Allow events to occur. */
910 efi_timer_check();
911 }
912
913out:
914 /*
915 * Reset the signal which is passed to the caller to allow periodic
916 * events to occur.
917 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200918 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 if (index)
920 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100921
922 return EFI_EXIT(EFI_SUCCESS);
923}
924
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200925/**
Mario Six8fac2912018-07-10 08:40:17 +0200926 * efi_signal_event_ext() - signal an EFI event
927 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200928 *
929 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200930 *
931 * See the Unified Extensible Firmware Interface (UEFI) specification for
932 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200933 *
934 * This functions sets the signaled state of the event and queues the
935 * notification function for execution.
936 *
Mario Six8fac2912018-07-10 08:40:17 +0200937 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200938 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200939static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100940{
941 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100942 if (efi_is_event(event) != EFI_SUCCESS)
943 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200944 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100945 return EFI_EXIT(EFI_SUCCESS);
946}
947
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200948/**
Mario Six8fac2912018-07-10 08:40:17 +0200949 * efi_close_event() - close an EFI event
950 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200951 *
952 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200953 *
Mario Six8fac2912018-07-10 08:40:17 +0200954 * See the Unified Extensible Firmware Interface (UEFI) specification for
955 * details.
956 *
957 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200958 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200959static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100960{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200961 struct efi_register_notify_event *item, *next;
962
Alexander Grafc15d9212016-03-04 01:09:59 +0100963 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100964 if (efi_is_event(event) != EFI_SUCCESS)
965 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200966
967 /* Remove protocol notify registrations for the event */
968 list_for_each_entry_safe(item, next, &efi_register_notify_events,
969 link) {
970 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200971 struct efi_protocol_notification *hitem, *hnext;
972
973 /* Remove signaled handles */
974 list_for_each_entry_safe(hitem, hnext, &item->handles,
975 link) {
976 list_del(&hitem->link);
977 free(hitem);
978 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200979 list_del(&item->link);
980 free(item);
981 }
982 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200983 /* Remove event from queue */
984 if (efi_event_is_queued(event))
985 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200986
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100987 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200988 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100989 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100990}
991
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200992/**
Mario Six8fac2912018-07-10 08:40:17 +0200993 * efi_check_event() - check if an event is signaled
994 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200995 *
996 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200997 *
998 * See the Unified Extensible Firmware Interface (UEFI) specification for
999 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001000 *
Mario Six8fac2912018-07-10 08:40:17 +02001001 * If an event is not signaled yet, the notification function is queued. The
1002 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001003 *
Mario Six8fac2912018-07-10 08:40:17 +02001004 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001005 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001006static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001007{
1008 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001009 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001010 if (efi_is_event(event) != EFI_SUCCESS ||
1011 event->type & EVT_NOTIFY_SIGNAL)
1012 return EFI_EXIT(EFI_INVALID_PARAMETER);
1013 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001014 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001015 if (event->is_signaled) {
1016 event->is_signaled = false;
1017 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001018 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001019 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001020}
1021
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001022/**
Mario Six8fac2912018-07-10 08:40:17 +02001023 * efi_search_obj() - find the internal EFI object for a handle
1024 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001025 *
Mario Six8fac2912018-07-10 08:40:17 +02001026 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001027 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001028struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001029{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001030 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001031
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001032 if (!handle)
1033 return NULL;
1034
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001035 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001036 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001037 return efiobj;
1038 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001039 return NULL;
1040}
1041
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001042/**
Mario Six8fac2912018-07-10 08:40:17 +02001043 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1044 * to a protocol
1045 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001046 *
Mario Six8fac2912018-07-10 08:40:17 +02001047 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001048 */
1049static struct efi_open_protocol_info_entry *efi_create_open_info(
1050 struct efi_handler *handler)
1051{
1052 struct efi_open_protocol_info_item *item;
1053
1054 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1055 if (!item)
1056 return NULL;
1057 /* Append the item to the open protocol info list. */
1058 list_add_tail(&item->link, &handler->open_infos);
1059
1060 return &item->info;
1061}
1062
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001063/**
Mario Six8fac2912018-07-10 08:40:17 +02001064 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1065 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001066 *
Mario Six8fac2912018-07-10 08:40:17 +02001067 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001068 */
1069static efi_status_t efi_delete_open_info(
1070 struct efi_open_protocol_info_item *item)
1071{
1072 list_del(&item->link);
1073 free(item);
1074 return EFI_SUCCESS;
1075}
1076
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001077/**
Mario Six8fac2912018-07-10 08:40:17 +02001078 * efi_add_protocol() - install new protocol on a handle
1079 * @handle: handle on which the protocol shall be installed
1080 * @protocol: GUID of the protocol to be installed
1081 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001082 *
Mario Six8fac2912018-07-10 08:40:17 +02001083 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001084 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001085efi_status_t efi_add_protocol(const efi_handle_t handle,
1086 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001087 void *protocol_interface)
1088{
1089 struct efi_object *efiobj;
1090 struct efi_handler *handler;
1091 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001092 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001093
1094 efiobj = efi_search_obj(handle);
1095 if (!efiobj)
1096 return EFI_INVALID_PARAMETER;
1097 ret = efi_search_protocol(handle, protocol, NULL);
1098 if (ret != EFI_NOT_FOUND)
1099 return EFI_INVALID_PARAMETER;
1100 handler = calloc(1, sizeof(struct efi_handler));
1101 if (!handler)
1102 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001103 handler->guid = protocol;
1104 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001105 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001106 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001107
1108 /* Notify registered events */
1109 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001110 if (!guidcmp(protocol, &event->protocol)) {
1111 struct efi_protocol_notification *notif;
1112
1113 notif = calloc(1, sizeof(*notif));
1114 if (!notif) {
1115 list_del(&handler->link);
1116 free(handler);
1117 return EFI_OUT_OF_RESOURCES;
1118 }
1119 notif->handle = handle;
1120 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001121 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001122 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001123 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001124 }
1125
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001126 if (!guidcmp(&efi_guid_device_path, protocol))
1127 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001128 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001129}
1130
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001131/**
Mario Six8fac2912018-07-10 08:40:17 +02001132 * efi_install_protocol_interface() - install protocol interface
1133 * @handle: handle on which the protocol shall be installed
1134 * @protocol: GUID of the protocol to be installed
1135 * @protocol_interface_type: type of the interface to be installed,
1136 * always EFI_NATIVE_INTERFACE
1137 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001138 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001139 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001140 *
Mario Six8fac2912018-07-10 08:40:17 +02001141 * See the Unified Extensible Firmware Interface (UEFI) specification for
1142 * details.
1143 *
1144 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001145 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001146static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001147 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001148 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001149{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001150 efi_status_t r;
1151
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001152 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1153 protocol_interface);
1154
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001155 if (!handle || !protocol ||
1156 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1157 r = EFI_INVALID_PARAMETER;
1158 goto out;
1159 }
1160
1161 /* Create new handle if requested. */
1162 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001163 r = efi_create_handle(handle);
1164 if (r != EFI_SUCCESS)
1165 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001166 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001167 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001168 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001169 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001170 /* Add new protocol */
1171 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001172out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001173 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001174}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001175
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001176/**
Mario Six8fac2912018-07-10 08:40:17 +02001177 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001178 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001179 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001180 * @number_of_drivers: number of child controllers
1181 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001182 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001183 * The allocated buffer has to be freed with free().
1184 *
Mario Six8fac2912018-07-10 08:40:17 +02001185 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001186 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001187static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001188 const efi_guid_t *protocol,
1189 efi_uintn_t *number_of_drivers,
1190 efi_handle_t **driver_handle_buffer)
1191{
1192 struct efi_handler *handler;
1193 struct efi_open_protocol_info_item *item;
1194 efi_uintn_t count = 0, i;
1195 bool duplicate;
1196
1197 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001198 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001199 if (protocol && guidcmp(handler->guid, protocol))
1200 continue;
1201 list_for_each_entry(item, &handler->open_infos, link) {
1202 if (item->info.attributes &
1203 EFI_OPEN_PROTOCOL_BY_DRIVER)
1204 ++count;
1205 }
1206 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001207 *number_of_drivers = 0;
1208 if (!count) {
1209 *driver_handle_buffer = NULL;
1210 return EFI_SUCCESS;
1211 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001212 /*
1213 * Create buffer. In case of duplicate driver assignments the buffer
1214 * will be too large. But that does not harm.
1215 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001216 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1217 if (!*driver_handle_buffer)
1218 return EFI_OUT_OF_RESOURCES;
1219 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001220 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001221 if (protocol && guidcmp(handler->guid, protocol))
1222 continue;
1223 list_for_each_entry(item, &handler->open_infos, link) {
1224 if (item->info.attributes &
1225 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1226 /* Check this is a new driver */
1227 duplicate = false;
1228 for (i = 0; i < *number_of_drivers; ++i) {
1229 if ((*driver_handle_buffer)[i] ==
1230 item->info.agent_handle)
1231 duplicate = true;
1232 }
1233 /* Copy handle to buffer */
1234 if (!duplicate) {
1235 i = (*number_of_drivers)++;
1236 (*driver_handle_buffer)[i] =
1237 item->info.agent_handle;
1238 }
1239 }
1240 }
1241 }
1242 return EFI_SUCCESS;
1243}
1244
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001245/**
Mario Six8fac2912018-07-10 08:40:17 +02001246 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001247 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001248 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001249 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001250 *
1251 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001252 *
1253 * See the Unified Extensible Firmware Interface (UEFI) specification for
1254 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001255 *
Mario Six8fac2912018-07-10 08:40:17 +02001256 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001257 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001258static efi_status_t efi_disconnect_all_drivers
1259 (efi_handle_t handle,
1260 const efi_guid_t *protocol,
1261 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001262{
1263 efi_uintn_t number_of_drivers;
1264 efi_handle_t *driver_handle_buffer;
1265 efi_status_t r, ret;
1266
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001267 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001268 &driver_handle_buffer);
1269 if (ret != EFI_SUCCESS)
1270 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001271 if (!number_of_drivers)
1272 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001273 ret = EFI_NOT_FOUND;
1274 while (number_of_drivers) {
1275 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001276 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001277 driver_handle_buffer[--number_of_drivers],
1278 child_handle));
1279 if (r == EFI_SUCCESS)
1280 ret = r;
1281 }
1282 free(driver_handle_buffer);
1283 return ret;
1284}
1285
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001286/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001287 * efi_uninstall_protocol() - uninstall protocol interface
1288 *
Mario Six8fac2912018-07-10 08:40:17 +02001289 * @handle: handle from which the protocol shall be removed
1290 * @protocol: GUID of the protocol to be removed
1291 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001292 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001293 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001294 *
1295 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001296 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001297static efi_status_t efi_uninstall_protocol
1298 (efi_handle_t handle, const efi_guid_t *protocol,
1299 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001300{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001301 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001302 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001303 struct efi_open_protocol_info_item *item;
1304 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001305 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001306
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001307 /* Check handle */
1308 efiobj = efi_search_obj(handle);
1309 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001310 r = EFI_INVALID_PARAMETER;
1311 goto out;
1312 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001313 /* Find the protocol on the handle */
1314 r = efi_search_protocol(handle, protocol, &handler);
1315 if (r != EFI_SUCCESS)
1316 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001317 /* Disconnect controllers */
1318 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001319 /* Close protocol */
1320 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1321 if (item->info.attributes ==
1322 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1323 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1324 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1325 list_del(&item->link);
1326 }
1327 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001328 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001329 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001330 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001331 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001332out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001333 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001334}
1335
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001336/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001337 * efi_uninstall_protocol_interface() - uninstall protocol interface
1338 * @handle: handle from which the protocol shall be removed
1339 * @protocol: GUID of the protocol to be removed
1340 * @protocol_interface: interface to be removed
1341 *
1342 * This function implements the UninstallProtocolInterface service.
1343 *
1344 * See the Unified Extensible Firmware Interface (UEFI) specification for
1345 * details.
1346 *
1347 * Return: status code
1348 */
1349static efi_status_t EFIAPI efi_uninstall_protocol_interface
1350 (efi_handle_t handle, const efi_guid_t *protocol,
1351 void *protocol_interface)
1352{
1353 efi_status_t ret;
1354
1355 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1356
1357 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1358 if (ret != EFI_SUCCESS)
1359 goto out;
1360
1361 /* If the last protocol has been removed, delete the handle. */
1362 if (list_empty(&handle->protocols)) {
1363 list_del(&handle->link);
1364 free(handle);
1365 }
1366out:
1367 return EFI_EXIT(ret);
1368}
1369
1370/**
Mario Six8fac2912018-07-10 08:40:17 +02001371 * efi_register_protocol_notify() - register an event for notification when a
1372 * protocol is installed.
1373 * @protocol: GUID of the protocol whose installation shall be notified
1374 * @event: event to be signaled upon installation of the protocol
1375 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001376 *
1377 * This function implements the RegisterProtocolNotify service.
1378 * See the Unified Extensible Firmware Interface (UEFI) specification
1379 * for details.
1380 *
Mario Six8fac2912018-07-10 08:40:17 +02001381 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001382 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001383static efi_status_t EFIAPI efi_register_protocol_notify(
1384 const efi_guid_t *protocol,
1385 struct efi_event *event,
1386 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001387{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001388 struct efi_register_notify_event *item;
1389 efi_status_t ret = EFI_SUCCESS;
1390
Rob Clark238f88c2017-09-13 18:05:41 -04001391 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001392
1393 if (!protocol || !event || !registration) {
1394 ret = EFI_INVALID_PARAMETER;
1395 goto out;
1396 }
1397
1398 item = calloc(1, sizeof(struct efi_register_notify_event));
1399 if (!item) {
1400 ret = EFI_OUT_OF_RESOURCES;
1401 goto out;
1402 }
1403
1404 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301405 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001406 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001407
1408 list_add_tail(&item->link, &efi_register_notify_events);
1409
1410 *registration = item;
1411out:
1412 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001413}
1414
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001415/**
Mario Six8fac2912018-07-10 08:40:17 +02001416 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001417 *
Mario Six8fac2912018-07-10 08:40:17 +02001418 * @search_type: selection criterion
1419 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001420 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001421 *
1422 * See the documentation of the LocateHandle service in the UEFI specification.
1423 *
Mario Six8fac2912018-07-10 08:40:17 +02001424 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001425 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001426static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001427 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001428{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001429 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001430
1431 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001432 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001433 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001434 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001435 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001436 return (ret != EFI_SUCCESS);
1437 default:
1438 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001439 return -1;
1440 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001441}
1442
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001443/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001444 * efi_check_register_notify_event() - check if registration key is valid
1445 *
1446 * Check that a pointer is a valid registration key as returned by
1447 * RegisterProtocolNotify().
1448 *
1449 * @key: registration key
1450 * Return: valid registration key or NULL
1451 */
1452static struct efi_register_notify_event *efi_check_register_notify_event
1453 (void *key)
1454{
1455 struct efi_register_notify_event *event;
1456
1457 list_for_each_entry(event, &efi_register_notify_events, link) {
1458 if (event == (struct efi_register_notify_event *)key)
1459 return event;
1460 }
1461 return NULL;
1462}
1463
1464/**
Mario Six8fac2912018-07-10 08:40:17 +02001465 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001466 *
1467 * @search_type: selection criterion
1468 * @protocol: GUID of the protocol
1469 * @search_key: registration key
1470 * @buffer_size: size of the buffer to receive the handles in bytes
1471 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001472 *
1473 * This function is meant for U-Boot internal calls. For the API implementation
1474 * of the LocateHandle service see efi_locate_handle_ext.
1475 *
Mario Six8fac2912018-07-10 08:40:17 +02001476 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001477 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001478static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001479 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001480 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001481 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001482{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001483 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001484 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001485 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001486 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001487
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001488 /* Check parameters */
1489 switch (search_type) {
1490 case ALL_HANDLES:
1491 break;
1492 case BY_REGISTER_NOTIFY:
1493 if (!search_key)
1494 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001495 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001496 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001497 if (!event)
1498 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001499 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001500 case BY_PROTOCOL:
1501 if (!protocol)
1502 return EFI_INVALID_PARAMETER;
1503 break;
1504 default:
1505 return EFI_INVALID_PARAMETER;
1506 }
1507
Alexander Grafc15d9212016-03-04 01:09:59 +01001508 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001509 if (search_type == BY_REGISTER_NOTIFY) {
1510 if (list_empty(&event->handles))
1511 return EFI_NOT_FOUND;
1512 handle = list_first_entry(&event->handles,
1513 struct efi_protocol_notification,
1514 link);
1515 efiobj = handle->handle;
1516 size += sizeof(void *);
1517 } else {
1518 list_for_each_entry(efiobj, &efi_obj_list, link) {
1519 if (!efi_search(search_type, protocol, efiobj))
1520 size += sizeof(void *);
1521 }
1522 if (size == 0)
1523 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001524 }
1525
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001526 if (!buffer_size)
1527 return EFI_INVALID_PARAMETER;
1528
Alexander Grafc15d9212016-03-04 01:09:59 +01001529 if (*buffer_size < size) {
1530 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001531 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001532 }
1533
Rob Clarkcdee3372017-08-06 14:10:07 -04001534 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001535
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001536 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001537 if (!buffer)
1538 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001539
Alexander Grafc15d9212016-03-04 01:09:59 +01001540 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001541 if (search_type == BY_REGISTER_NOTIFY) {
1542 *buffer = efiobj;
1543 list_del(&handle->link);
1544 } else {
1545 list_for_each_entry(efiobj, &efi_obj_list, link) {
1546 if (!efi_search(search_type, protocol, efiobj))
1547 *buffer++ = efiobj;
1548 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001549 }
1550
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001551 return EFI_SUCCESS;
1552}
1553
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001554/**
Mario Six8fac2912018-07-10 08:40:17 +02001555 * efi_locate_handle_ext() - locate handles implementing a protocol.
1556 * @search_type: selection criterion
1557 * @protocol: GUID of the protocol
1558 * @search_key: registration key
1559 * @buffer_size: size of the buffer to receive the handles in bytes
1560 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001561 *
1562 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001563 *
Mario Six8fac2912018-07-10 08:40:17 +02001564 * See the Unified Extensible Firmware Interface (UEFI) specification for
1565 * details.
1566 *
1567 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001568 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001569static efi_status_t EFIAPI efi_locate_handle_ext(
1570 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001571 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001572 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001573{
Rob Clark238f88c2017-09-13 18:05:41 -04001574 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001575 buffer_size, buffer);
1576
1577 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1578 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001579}
1580
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001581/**
Mario Six8fac2912018-07-10 08:40:17 +02001582 * efi_remove_configuration_table() - collapses configuration table entries,
1583 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001584 *
Mario Six8fac2912018-07-10 08:40:17 +02001585 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001586 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001587static void efi_remove_configuration_table(int i)
1588{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001589 struct efi_configuration_table *this = &systab.tables[i];
1590 struct efi_configuration_table *next = &systab.tables[i + 1];
1591 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001592
1593 memmove(this, next, (ulong)end - (ulong)next);
1594 systab.nr_tables--;
1595}
1596
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001597/**
Mario Six8fac2912018-07-10 08:40:17 +02001598 * efi_install_configuration_table() - adds, updates, or removes a
1599 * configuration table
1600 * @guid: GUID of the installed table
1601 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001602 *
1603 * This function is used for internal calls. For the API implementation of the
1604 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1605 *
Mario Six8fac2912018-07-10 08:40:17 +02001606 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001607 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001608efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1609 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001610{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001611 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001612 int i;
1613
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001614 if (!guid)
1615 return EFI_INVALID_PARAMETER;
1616
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001617 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001618 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001619 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001620 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001621 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001622 else
1623 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001624 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001625 }
1626 }
1627
Alexander Graffe3366f2017-07-26 13:41:04 +02001628 if (!table)
1629 return EFI_NOT_FOUND;
1630
Alexander Grafc15d9212016-03-04 01:09:59 +01001631 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001632 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001633 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001634
1635 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301636 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001637 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001638 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001639
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001640out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001641 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001642 efi_update_table_header_crc32(&systab.hdr);
1643
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001644 /* Notify that the configuration table was changed */
1645 list_for_each_entry(evt, &efi_events, link) {
1646 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001647 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001648 break;
1649 }
1650 }
1651
Alexander Grafc5c11632016-08-19 01:23:24 +02001652 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001653}
1654
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001655/**
Mario Six8fac2912018-07-10 08:40:17 +02001656 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1657 * configuration table.
1658 * @guid: GUID of the installed table
1659 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001660 *
1661 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001662 *
Mario Six8fac2912018-07-10 08:40:17 +02001663 * See the Unified Extensible Firmware Interface (UEFI) specification for
1664 * details.
1665 *
1666 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001667 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001668static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1669 void *table)
1670{
Rob Clark238f88c2017-09-13 18:05:41 -04001671 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001672 return EFI_EXIT(efi_install_configuration_table(guid, table));
1673}
1674
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001675/**
Mario Six8fac2912018-07-10 08:40:17 +02001676 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001677 *
1678 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001679 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001680 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001681 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001682 * code is returned.
1683 *
1684 * @device_path: device path of the loaded image
1685 * @file_path: file path of the loaded image
1686 * @handle_ptr: handle of the loaded image
1687 * @info_ptr: loaded image protocol
1688 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001689 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001690efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1691 struct efi_device_path *file_path,
1692 struct efi_loaded_image_obj **handle_ptr,
1693 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001694{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001695 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001696 struct efi_loaded_image *info = NULL;
1697 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001698 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001699
1700 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1701 *handle_ptr = NULL;
1702 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001703
1704 info = calloc(1, sizeof(*info));
1705 if (!info)
1706 return EFI_OUT_OF_RESOURCES;
1707 obj = calloc(1, sizeof(*obj));
1708 if (!obj) {
1709 free(info);
1710 return EFI_OUT_OF_RESOURCES;
1711 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001712 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001713
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001714 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001715 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001716
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001717 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001718 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001719 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001720
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001721 if (device_path) {
1722 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001723
1724 dp = efi_dp_append(device_path, file_path);
1725 if (!dp) {
1726 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001727 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001728 }
1729 } else {
1730 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001731 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001732 ret = efi_add_protocol(&obj->header,
1733 &efi_guid_loaded_image_device_path, dp);
1734 if (ret != EFI_SUCCESS)
1735 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001736
1737 /*
1738 * When asking for the loaded_image interface, just
1739 * return handle which points to loaded_image_info
1740 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001741 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001742 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001743 if (ret != EFI_SUCCESS)
1744 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001745
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001746 *info_ptr = info;
1747 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001748
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001749 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001750failure:
1751 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001752 efi_delete_handle(&obj->header);
1753 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001754 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001755}
1756
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001757/**
Mario Six8fac2912018-07-10 08:40:17 +02001758 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001759 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001760 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1761 * callers obligation to update the memory type as needed.
1762 *
1763 * @file_path: the path of the image to load
1764 * @buffer: buffer containing the loaded image
1765 * @size: size of the loaded image
1766 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001767 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001768static
Rob Clarkc84c1102017-09-13 18:05:38 -04001769efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001770 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001771{
1772 struct efi_file_info *info = NULL;
1773 struct efi_file_handle *f;
1774 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001775 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001776 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001777
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001778 /* In case of failure nothing is returned */
1779 *buffer = NULL;
1780 *size = 0;
1781
1782 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001783 f = efi_file_from_path(file_path);
1784 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001785 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001786
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001787 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001788 bs = 0;
1789 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1790 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001791 if (ret != EFI_BUFFER_TOO_SMALL) {
1792 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001793 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001794 }
Rob Clark857a1222017-09-13 18:05:35 -04001795
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001796 info = malloc(bs);
1797 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1798 info));
1799 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001800 goto error;
1801
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001802 /*
1803 * When reading the file we do not yet know if it contains an
1804 * application, a boottime driver, or a runtime driver. So here we
1805 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1806 * update the reservation according to the image type.
1807 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001808 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001809 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1810 EFI_BOOT_SERVICES_DATA,
1811 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001812 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001813 ret = EFI_OUT_OF_RESOURCES;
1814 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001815 }
1816
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001817 /* Read file */
1818 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1819 if (ret != EFI_SUCCESS)
1820 efi_free_pages(addr, efi_size_in_pages(bs));
1821 *buffer = (void *)(uintptr_t)addr;
1822 *size = bs;
1823error:
1824 EFI_CALL(f->close(f));
1825 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001826 return ret;
1827}
1828
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001829/**
Mario Six8fac2912018-07-10 08:40:17 +02001830 * efi_load_image() - load an EFI image into memory
1831 * @boot_policy: true for request originating from the boot manager
1832 * @parent_image: the caller's image handle
1833 * @file_path: the path of the image to load
1834 * @source_buffer: memory location from which the image is installed
1835 * @source_size: size of the memory area from which the image is installed
1836 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001837 *
1838 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001839 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001840 * See the Unified Extensible Firmware Interface (UEFI) specification
1841 * for details.
1842 *
Mario Six8fac2912018-07-10 08:40:17 +02001843 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001844 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001845efi_status_t EFIAPI efi_load_image(bool boot_policy,
1846 efi_handle_t parent_image,
1847 struct efi_device_path *file_path,
1848 void *source_buffer,
1849 efi_uintn_t source_size,
1850 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001851{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001852 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001853 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001854 struct efi_loaded_image_obj **image_obj =
1855 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001856 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001857 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001858
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001859 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001860 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001861
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001862 if (!image_handle || (!source_buffer && !file_path) ||
1863 !efi_search_obj(parent_image) ||
1864 /* The parent image handle must refer to a loaded image */
1865 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001866 ret = EFI_INVALID_PARAMETER;
1867 goto error;
1868 }
Rob Clark857a1222017-09-13 18:05:35 -04001869
1870 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001871 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001872 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001873 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001874 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001875 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001876 if (!source_size) {
1877 ret = EFI_LOAD_ERROR;
1878 goto error;
1879 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001880 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001881 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001882 /* split file_path which contains both the device and file parts */
1883 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001884 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001885 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001886 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001887 if (!source_buffer)
1888 /* Release buffer to which file was loaded */
1889 efi_free_pages((uintptr_t)dest_buffer,
1890 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09001891 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001892 info->system_table = &systab;
1893 info->parent_handle = parent_image;
1894 } else {
1895 /* The image is invalid. Release all associated resources. */
1896 efi_delete_handle(*image_handle);
1897 *image_handle = NULL;
1898 free(info);
1899 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001900error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001901 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001902}
1903
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001904/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001905 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1906 */
1907static void efi_exit_caches(void)
1908{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001909#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01001910 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001911 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1912 * caches are enabled.
1913 *
1914 * TODO:
1915 * According to the UEFI spec caches that can be managed via CP15
1916 * operations should be enabled. Caches requiring platform information
1917 * to manage should be disabled. This should not happen in
1918 * ExitBootServices() but before invoking any UEFI binary is invoked.
1919 *
1920 * We want to keep the current workaround while GRUB prior to version
1921 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01001922 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001923 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01001924#endif
1925}
1926
1927/**
Mario Six8fac2912018-07-10 08:40:17 +02001928 * efi_exit_boot_services() - stop all boot services
1929 * @image_handle: handle of the loaded image
1930 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001931 *
1932 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001933 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001934 * See the Unified Extensible Firmware Interface (UEFI) specification
1935 * for details.
1936 *
Mario Six8fac2912018-07-10 08:40:17 +02001937 * All timer events are disabled. For exit boot services events the
1938 * notification function is called. The boot services are disabled in the
1939 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001940 *
Mario Six8fac2912018-07-10 08:40:17 +02001941 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001942 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001943static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001944 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001945{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001946 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001947 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001948
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001949 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001950
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001951 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001952 if (map_key != efi_memory_map_key) {
1953 ret = EFI_INVALID_PARAMETER;
1954 goto out;
1955 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001956
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001957 /* Check if ExitBootServices has already been called */
1958 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001959 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001960
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001961 /* Stop all timer related activities */
1962 timers_enabled = false;
1963
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001964 /* Add related events to the event group */
1965 list_for_each_entry(evt, &efi_events, link) {
1966 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1967 evt->group = &efi_guid_event_group_exit_boot_services;
1968 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001969 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001970 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001971 if (evt->group &&
1972 !guidcmp(evt->group,
1973 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001974 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001975 break;
1976 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001977 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001978
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001979 /* Make sure that notification functions are not called anymore */
1980 efi_tpl = TPL_HIGH_LEVEL;
1981
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001982 /* Notify variable services */
1983 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001984
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001985 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1986 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1987 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1988 list_del(&evt->link);
1989 }
1990
Alexander Graf2ebeb442016-11-17 01:02:57 +01001991 board_quiesce_devices();
1992
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02001993 /* Patch out unsupported runtime function */
1994 efi_runtime_detach();
1995
Alexander Graffa5246b2018-11-15 21:23:47 +01001996 /* Fix up caches for EFI payloads if necessary */
1997 efi_exit_caches();
1998
Alexander Grafc15d9212016-03-04 01:09:59 +01001999 /* This stops all lingering devices */
2000 bootm_disable_interrupts();
2001
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002002 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002003 systab.con_in_handle = NULL;
2004 systab.con_in = NULL;
2005 systab.con_out_handle = NULL;
2006 systab.con_out = NULL;
2007 systab.stderr_handle = NULL;
2008 systab.std_err = NULL;
2009 systab.boottime = NULL;
2010
2011 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002012 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002013
Alexander Grafc15d9212016-03-04 01:09:59 +01002014 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002015 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002016 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002017out:
2018 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002019}
2020
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002021/**
Mario Six8fac2912018-07-10 08:40:17 +02002022 * efi_get_next_monotonic_count() - get next value of the counter
2023 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002024 *
2025 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002026 *
2027 * See the Unified Extensible Firmware Interface (UEFI) specification for
2028 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002029 *
Mario Six8fac2912018-07-10 08:40:17 +02002030 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002031 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002032static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2033{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002034 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002035 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002036
Alexander Grafc15d9212016-03-04 01:09:59 +01002037 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002038 if (!count) {
2039 ret = EFI_INVALID_PARAMETER;
2040 goto out;
2041 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002042 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002043 ret = EFI_SUCCESS;
2044out:
2045 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002046}
2047
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002048/**
Mario Six8fac2912018-07-10 08:40:17 +02002049 * efi_stall() - sleep
2050 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002051 *
Mario Six8fac2912018-07-10 08:40:17 +02002052 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002053 *
Mario Six8fac2912018-07-10 08:40:17 +02002054 * See the Unified Extensible Firmware Interface (UEFI) specification for
2055 * details.
2056 *
2057 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002058 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002059static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2060{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002061 u64 end_tick;
2062
Alexander Grafc15d9212016-03-04 01:09:59 +01002063 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002064
2065 end_tick = get_ticks() + usec_to_tick(microseconds);
2066 while (get_ticks() < end_tick)
2067 efi_timer_check();
2068
Alexander Grafc15d9212016-03-04 01:09:59 +01002069 return EFI_EXIT(EFI_SUCCESS);
2070}
2071
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002072/**
Mario Six8fac2912018-07-10 08:40:17 +02002073 * efi_set_watchdog_timer() - reset the watchdog timer
2074 * @timeout: seconds before reset by watchdog
2075 * @watchdog_code: code to be logged when resetting
2076 * @data_size: size of buffer in bytes
2077 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002078 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002079 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002080 *
Mario Six8fac2912018-07-10 08:40:17 +02002081 * See the Unified Extensible Firmware Interface (UEFI) specification for
2082 * details.
2083 *
2084 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002085 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002086static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2087 uint64_t watchdog_code,
2088 unsigned long data_size,
2089 uint16_t *watchdog_data)
2090{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002091 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002092 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002093 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002094}
2095
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002096/**
Mario Six8fac2912018-07-10 08:40:17 +02002097 * efi_close_protocol() - close a protocol
2098 * @handle: handle on which the protocol shall be closed
2099 * @protocol: GUID of the protocol to close
2100 * @agent_handle: handle of the driver
2101 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002102 *
2103 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002104 *
Mario Six8fac2912018-07-10 08:40:17 +02002105 * See the Unified Extensible Firmware Interface (UEFI) specification for
2106 * details.
2107 *
2108 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002109 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002110efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2111 const efi_guid_t *protocol,
2112 efi_handle_t agent_handle,
2113 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002114{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002115 struct efi_handler *handler;
2116 struct efi_open_protocol_info_item *item;
2117 struct efi_open_protocol_info_item *pos;
2118 efi_status_t r;
2119
Rob Clark238f88c2017-09-13 18:05:41 -04002120 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002121 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002122
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002123 if (!efi_search_obj(agent_handle) ||
2124 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002125 r = EFI_INVALID_PARAMETER;
2126 goto out;
2127 }
2128 r = efi_search_protocol(handle, protocol, &handler);
2129 if (r != EFI_SUCCESS)
2130 goto out;
2131
2132 r = EFI_NOT_FOUND;
2133 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2134 if (item->info.agent_handle == agent_handle &&
2135 item->info.controller_handle == controller_handle) {
2136 efi_delete_open_info(item);
2137 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002138 }
2139 }
2140out:
2141 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002142}
2143
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002144/**
Mario Six8fac2912018-07-10 08:40:17 +02002145 * efi_open_protocol_information() - provide information about then open status
2146 * of a protocol on a handle
2147 * @handle: handle for which the information shall be retrieved
2148 * @protocol: GUID of the protocol
2149 * @entry_buffer: buffer to receive the open protocol information
2150 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002151 *
2152 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002153 *
2154 * See the Unified Extensible Firmware Interface (UEFI) specification for
2155 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002156 *
Mario Six8fac2912018-07-10 08:40:17 +02002157 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002158 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002159static efi_status_t EFIAPI efi_open_protocol_information(
2160 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002161 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002162 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002163{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002164 unsigned long buffer_size;
2165 unsigned long count;
2166 struct efi_handler *handler;
2167 struct efi_open_protocol_info_item *item;
2168 efi_status_t r;
2169
Rob Clark238f88c2017-09-13 18:05:41 -04002170 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002171 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002172
2173 /* Check parameters */
2174 if (!entry_buffer) {
2175 r = EFI_INVALID_PARAMETER;
2176 goto out;
2177 }
2178 r = efi_search_protocol(handle, protocol, &handler);
2179 if (r != EFI_SUCCESS)
2180 goto out;
2181
2182 /* Count entries */
2183 count = 0;
2184 list_for_each_entry(item, &handler->open_infos, link) {
2185 if (item->info.open_count)
2186 ++count;
2187 }
2188 *entry_count = count;
2189 *entry_buffer = NULL;
2190 if (!count) {
2191 r = EFI_SUCCESS;
2192 goto out;
2193 }
2194
2195 /* Copy entries */
2196 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002197 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002198 (void **)entry_buffer);
2199 if (r != EFI_SUCCESS)
2200 goto out;
2201 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2202 if (item->info.open_count)
2203 (*entry_buffer)[--count] = item->info;
2204 }
2205out:
2206 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002207}
2208
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002209/**
Mario Six8fac2912018-07-10 08:40:17 +02002210 * efi_protocols_per_handle() - get protocols installed on a handle
2211 * @handle: handle for which the information is retrieved
2212 * @protocol_buffer: buffer with protocol GUIDs
2213 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002214 *
2215 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002216 *
Mario Six8fac2912018-07-10 08:40:17 +02002217 * See the Unified Extensible Firmware Interface (UEFI) specification for
2218 * details.
2219 *
2220 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002221 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002222static efi_status_t EFIAPI efi_protocols_per_handle(
2223 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002224 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002225{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002226 unsigned long buffer_size;
2227 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002228 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002229 efi_status_t r;
2230
Alexander Grafc15d9212016-03-04 01:09:59 +01002231 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2232 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002233
2234 if (!handle || !protocol_buffer || !protocol_buffer_count)
2235 return EFI_EXIT(EFI_INVALID_PARAMETER);
2236
2237 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002238 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002239
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002240 efiobj = efi_search_obj(handle);
2241 if (!efiobj)
2242 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002243
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002244 /* Count protocols */
2245 list_for_each(protocol_handle, &efiobj->protocols) {
2246 ++*protocol_buffer_count;
2247 }
2248
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002249 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002250 if (*protocol_buffer_count) {
2251 size_t j = 0;
2252
2253 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002254 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002255 (void **)protocol_buffer);
2256 if (r != EFI_SUCCESS)
2257 return EFI_EXIT(r);
2258 list_for_each(protocol_handle, &efiobj->protocols) {
2259 struct efi_handler *protocol;
2260
2261 protocol = list_entry(protocol_handle,
2262 struct efi_handler, link);
2263 (*protocol_buffer)[j] = (void *)protocol->guid;
2264 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002265 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002266 }
2267
2268 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002269}
2270
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002271/**
Mario Six8fac2912018-07-10 08:40:17 +02002272 * efi_locate_handle_buffer() - locate handles implementing a protocol
2273 * @search_type: selection criterion
2274 * @protocol: GUID of the protocol
2275 * @search_key: registration key
2276 * @no_handles: number of returned handles
2277 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002278 *
2279 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002280 *
Mario Six8fac2912018-07-10 08:40:17 +02002281 * See the Unified Extensible Firmware Interface (UEFI) specification for
2282 * details.
2283 *
2284 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002285 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002286efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002287 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002288 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002289 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002290{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002291 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002292 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002293
Rob Clark238f88c2017-09-13 18:05:41 -04002294 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002295 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002296
2297 if (!no_handles || !buffer) {
2298 r = EFI_INVALID_PARAMETER;
2299 goto out;
2300 }
2301 *no_handles = 0;
2302 *buffer = NULL;
2303 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2304 *buffer);
2305 if (r != EFI_BUFFER_TOO_SMALL)
2306 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002307 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002308 (void **)buffer);
2309 if (r != EFI_SUCCESS)
2310 goto out;
2311 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2312 *buffer);
2313 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002314 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002315out:
2316 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002317}
2318
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002319/**
Mario Six8fac2912018-07-10 08:40:17 +02002320 * efi_locate_protocol() - find an interface implementing a protocol
2321 * @protocol: GUID of the protocol
2322 * @registration: registration key passed to the notification function
2323 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002324 *
2325 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002326 *
2327 * See the Unified Extensible Firmware Interface (UEFI) specification for
2328 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002329 *
Mario Six8fac2912018-07-10 08:40:17 +02002330 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002331 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002332static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002333 void *registration,
2334 void **protocol_interface)
2335{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002336 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002337 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002338 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002339
Rob Clark238f88c2017-09-13 18:05:41 -04002340 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002341
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002342 /*
2343 * The UEFI spec explicitly requires a protocol even if a registration
2344 * key is provided. This differs from the logic in LocateHandle().
2345 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002346 if (!protocol || !protocol_interface)
2347 return EFI_EXIT(EFI_INVALID_PARAMETER);
2348
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002349 if (registration) {
2350 struct efi_register_notify_event *event;
2351 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002352
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002353 event = efi_check_register_notify_event(registration);
2354 if (!event)
2355 return EFI_EXIT(EFI_INVALID_PARAMETER);
2356 /*
2357 * The UEFI spec requires to return EFI_NOT_FOUND if no
2358 * protocol instance matches protocol and registration.
2359 * So let's do the same for a mismatch between protocol and
2360 * registration.
2361 */
2362 if (guidcmp(&event->protocol, protocol))
2363 goto not_found;
2364 if (list_empty(&event->handles))
2365 goto not_found;
2366 handle = list_first_entry(&event->handles,
2367 struct efi_protocol_notification,
2368 link);
2369 efiobj = handle->handle;
2370 list_del(&handle->link);
2371 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002372 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002373 if (ret == EFI_SUCCESS)
2374 goto found;
2375 } else {
2376 list_for_each_entry(efiobj, &efi_obj_list, link) {
2377 ret = efi_search_protocol(efiobj, protocol, &handler);
2378 if (ret == EFI_SUCCESS)
2379 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002380 }
2381 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002382not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002383 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002384 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002385found:
2386 *protocol_interface = handler->protocol_interface;
2387 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002388}
2389
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002390/**
Mario Six8fac2912018-07-10 08:40:17 +02002391 * efi_locate_device_path() - Get the device path and handle of an device
2392 * implementing a protocol
2393 * @protocol: GUID of the protocol
2394 * @device_path: device path
2395 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002396 *
2397 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002398 *
Mario Six8fac2912018-07-10 08:40:17 +02002399 * See the Unified Extensible Firmware Interface (UEFI) specification for
2400 * details.
2401 *
2402 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002403 */
2404static efi_status_t EFIAPI efi_locate_device_path(
2405 const efi_guid_t *protocol,
2406 struct efi_device_path **device_path,
2407 efi_handle_t *device)
2408{
2409 struct efi_device_path *dp;
2410 size_t i;
2411 struct efi_handler *handler;
2412 efi_handle_t *handles;
2413 size_t len, len_dp;
2414 size_t len_best = 0;
2415 efi_uintn_t no_handles;
2416 u8 *remainder;
2417 efi_status_t ret;
2418
2419 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2420
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002421 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002422 ret = EFI_INVALID_PARAMETER;
2423 goto out;
2424 }
2425
2426 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002427 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002428
2429 /* Get all handles implementing the protocol */
2430 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2431 &no_handles, &handles));
2432 if (ret != EFI_SUCCESS)
2433 goto out;
2434
2435 for (i = 0; i < no_handles; ++i) {
2436 /* Find the device path protocol */
2437 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2438 &handler);
2439 if (ret != EFI_SUCCESS)
2440 continue;
2441 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002442 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002443 /*
2444 * This handle can only be a better fit
2445 * if its device path length is longer than the best fit and
2446 * if its device path length is shorter of equal the searched
2447 * device path.
2448 */
2449 if (len_dp <= len_best || len_dp > len)
2450 continue;
2451 /* Check if dp is a subpath of device_path */
2452 if (memcmp(*device_path, dp, len_dp))
2453 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002454 if (!device) {
2455 ret = EFI_INVALID_PARAMETER;
2456 goto out;
2457 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002458 *device = handles[i];
2459 len_best = len_dp;
2460 }
2461 if (len_best) {
2462 remainder = (u8 *)*device_path + len_best;
2463 *device_path = (struct efi_device_path *)remainder;
2464 ret = EFI_SUCCESS;
2465 } else {
2466 ret = EFI_NOT_FOUND;
2467 }
2468out:
2469 return EFI_EXIT(ret);
2470}
2471
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002472/**
Mario Six8fac2912018-07-10 08:40:17 +02002473 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2474 * interfaces
2475 * @handle: handle on which the protocol interfaces shall be installed
2476 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2477 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002478 *
2479 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002480 *
2481 * See the Unified Extensible Firmware Interface (UEFI) specification for
2482 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002483 *
Mario Six8fac2912018-07-10 08:40:17 +02002484 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002485 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002486efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002487 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002488{
2489 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002490
Alexander Graf404a7c82018-06-18 17:23:05 +02002491 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002492 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002493 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002494 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002495 efi_status_t r = EFI_SUCCESS;
2496 int i = 0;
2497
2498 if (!handle)
2499 return EFI_EXIT(EFI_INVALID_PARAMETER);
2500
Alexander Graf404a7c82018-06-18 17:23:05 +02002501 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002502 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002503 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002504 if (!protocol)
2505 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002506 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002507 /* Check that a device path has not been installed before */
2508 if (!guidcmp(protocol, &efi_guid_device_path)) {
2509 struct efi_device_path *dp = protocol_interface;
2510
2511 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2512 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002513 if (r == EFI_SUCCESS &&
2514 dp->type == DEVICE_PATH_TYPE_END) {
2515 EFI_PRINT("Path %pD already installed\n",
2516 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002517 r = EFI_ALREADY_STARTED;
2518 break;
2519 }
2520 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002521 r = EFI_CALL(efi_install_protocol_interface(
2522 handle, protocol,
2523 EFI_NATIVE_INTERFACE,
2524 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002525 if (r != EFI_SUCCESS)
2526 break;
2527 i++;
2528 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002529 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002530 if (r == EFI_SUCCESS)
2531 return EFI_EXIT(r);
2532
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002533 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002534 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002535 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002536 protocol = efi_va_arg(argptr, efi_guid_t*);
2537 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002538 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002539 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002540 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002541 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002542
2543 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002544}
2545
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002546/**
Mario Six8fac2912018-07-10 08:40:17 +02002547 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2548 * interfaces
2549 * @handle: handle from which the protocol interfaces shall be removed
2550 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2551 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002552 *
2553 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002554 *
Mario Six8fac2912018-07-10 08:40:17 +02002555 * See the Unified Extensible Firmware Interface (UEFI) specification for
2556 * details.
2557 *
2558 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002559 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002560static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002561 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002562{
2563 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002564
Alexander Graf404a7c82018-06-18 17:23:05 +02002565 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002566 const efi_guid_t *protocol;
2567 void *protocol_interface;
2568 efi_status_t r = EFI_SUCCESS;
2569 size_t i = 0;
2570
2571 if (!handle)
2572 return EFI_EXIT(EFI_INVALID_PARAMETER);
2573
Alexander Graf404a7c82018-06-18 17:23:05 +02002574 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002575 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002576 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002577 if (!protocol)
2578 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002579 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002580 r = efi_uninstall_protocol(handle, protocol,
2581 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002582 if (r != EFI_SUCCESS)
2583 break;
2584 i++;
2585 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002586 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002587 if (r == EFI_SUCCESS) {
2588 /* If the last protocol has been removed, delete the handle. */
2589 if (list_empty(&handle->protocols)) {
2590 list_del(&handle->link);
2591 free(handle);
2592 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002593 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002594 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002595
2596 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002597 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002598 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002599 protocol = efi_va_arg(argptr, efi_guid_t*);
2600 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002601 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2602 EFI_NATIVE_INTERFACE,
2603 protocol_interface));
2604 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002605 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002606
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002607 /* In case of an error always return EFI_INVALID_PARAMETER */
2608 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002609}
2610
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002611/**
Mario Six8fac2912018-07-10 08:40:17 +02002612 * efi_calculate_crc32() - calculate cyclic redundancy code
2613 * @data: buffer with data
2614 * @data_size: size of buffer in bytes
2615 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002616 *
2617 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002618 *
2619 * See the Unified Extensible Firmware Interface (UEFI) specification for
2620 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002621 *
Mario Six8fac2912018-07-10 08:40:17 +02002622 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002623 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002624static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2625 efi_uintn_t data_size,
2626 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002627{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002628 efi_status_t ret = EFI_SUCCESS;
2629
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002630 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002631 if (!data || !data_size || !crc32_p) {
2632 ret = EFI_INVALID_PARAMETER;
2633 goto out;
2634 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002635 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002636out:
2637 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002638}
2639
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002640/**
Mario Six8fac2912018-07-10 08:40:17 +02002641 * efi_copy_mem() - copy memory
2642 * @destination: destination of the copy operation
2643 * @source: source of the copy operation
2644 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002645 *
2646 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002647 *
Mario Six8fac2912018-07-10 08:40:17 +02002648 * See the Unified Extensible Firmware Interface (UEFI) specification for
2649 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002650 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002651static void EFIAPI efi_copy_mem(void *destination, const void *source,
2652 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002653{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002654 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002655 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002656 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002657}
2658
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002659/**
Mario Six8fac2912018-07-10 08:40:17 +02002660 * efi_set_mem() - Fill memory with a byte value.
2661 * @buffer: buffer to fill
2662 * @size: size of buffer in bytes
2663 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002664 *
2665 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002666 *
Mario Six8fac2912018-07-10 08:40:17 +02002667 * See the Unified Extensible Firmware Interface (UEFI) specification for
2668 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002669 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002670static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002671{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002672 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002673 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002674 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002675}
2676
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002677/**
Mario Six8fac2912018-07-10 08:40:17 +02002678 * efi_protocol_open() - open protocol interface on a handle
2679 * @handler: handler of a protocol
2680 * @protocol_interface: interface implementing the protocol
2681 * @agent_handle: handle of the driver
2682 * @controller_handle: handle of the controller
2683 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002684 *
Mario Six8fac2912018-07-10 08:40:17 +02002685 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002686 */
2687static efi_status_t efi_protocol_open(
2688 struct efi_handler *handler,
2689 void **protocol_interface, void *agent_handle,
2690 void *controller_handle, uint32_t attributes)
2691{
2692 struct efi_open_protocol_info_item *item;
2693 struct efi_open_protocol_info_entry *match = NULL;
2694 bool opened_by_driver = false;
2695 bool opened_exclusive = false;
2696
2697 /* If there is no agent, only return the interface */
2698 if (!agent_handle)
2699 goto out;
2700
2701 /* For TEST_PROTOCOL ignore interface attribute */
2702 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2703 *protocol_interface = NULL;
2704
2705 /*
2706 * Check if the protocol is already opened by a driver with the same
2707 * attributes or opened exclusively
2708 */
2709 list_for_each_entry(item, &handler->open_infos, link) {
2710 if (item->info.agent_handle == agent_handle) {
2711 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2712 (item->info.attributes == attributes))
2713 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002714 } else {
2715 if (item->info.attributes &
2716 EFI_OPEN_PROTOCOL_BY_DRIVER)
2717 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002718 }
2719 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2720 opened_exclusive = true;
2721 }
2722
2723 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002724 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2725 if (opened_exclusive)
2726 return EFI_ACCESS_DENIED;
2727 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2728 if (opened_exclusive || opened_by_driver)
2729 return EFI_ACCESS_DENIED;
2730 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002731
2732 /* Prepare exclusive opening */
2733 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2734 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002735disconnect_next:
2736 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002737 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002738 efi_status_t ret;
2739
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002740 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002741 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2742 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002743 item->info.controller_handle,
2744 item->info.agent_handle,
2745 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002746 if (ret == EFI_SUCCESS)
2747 /*
2748 * Child controllers may have been
2749 * removed from the open_infos list. So
2750 * let's restart the loop.
2751 */
2752 goto disconnect_next;
2753 else
2754 opened_by_driver = true;
2755 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002756 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002757 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002758 if (opened_by_driver)
2759 return EFI_ACCESS_DENIED;
2760 }
2761
2762 /* Find existing entry */
2763 list_for_each_entry(item, &handler->open_infos, link) {
2764 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002765 item->info.controller_handle == controller_handle &&
2766 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002767 match = &item->info;
2768 }
2769 /* None found, create one */
2770 if (!match) {
2771 match = efi_create_open_info(handler);
2772 if (!match)
2773 return EFI_OUT_OF_RESOURCES;
2774 }
2775
2776 match->agent_handle = agent_handle;
2777 match->controller_handle = controller_handle;
2778 match->attributes = attributes;
2779 match->open_count++;
2780
2781out:
2782 /* For TEST_PROTOCOL ignore interface attribute. */
2783 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2784 *protocol_interface = handler->protocol_interface;
2785
2786 return EFI_SUCCESS;
2787}
2788
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002789/**
Mario Six8fac2912018-07-10 08:40:17 +02002790 * efi_open_protocol() - open protocol interface on a handle
2791 * @handle: handle on which the protocol shall be opened
2792 * @protocol: GUID of the protocol
2793 * @protocol_interface: interface implementing the protocol
2794 * @agent_handle: handle of the driver
2795 * @controller_handle: handle of the controller
2796 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002797 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002798 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002799 *
Mario Six8fac2912018-07-10 08:40:17 +02002800 * See the Unified Extensible Firmware Interface (UEFI) specification for
2801 * details.
2802 *
2803 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002804 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002805static efi_status_t EFIAPI efi_open_protocol
2806 (efi_handle_t handle, const efi_guid_t *protocol,
2807 void **protocol_interface, efi_handle_t agent_handle,
2808 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002809{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002810 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002811 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002812
Rob Clark238f88c2017-09-13 18:05:41 -04002813 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002814 protocol_interface, agent_handle, controller_handle,
2815 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002816
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002817 if (!handle || !protocol ||
2818 (!protocol_interface && attributes !=
2819 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002820 goto out;
2821 }
2822
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002823 switch (attributes) {
2824 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2825 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2826 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2827 break;
2828 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2829 if (controller_handle == handle)
2830 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002831 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002832 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2833 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002834 /* Check that the controller handle is valid */
2835 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002836 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002837 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002838 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002839 /* Check that the agent handle is valid */
2840 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002841 goto out;
2842 break;
2843 default:
2844 goto out;
2845 }
2846
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002847 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002848 switch (r) {
2849 case EFI_SUCCESS:
2850 break;
2851 case EFI_NOT_FOUND:
2852 r = EFI_UNSUPPORTED;
2853 goto out;
2854 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002855 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002856 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002857
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002858 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2859 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002860out:
2861 return EFI_EXIT(r);
2862}
2863
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002864/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002865 * efi_start_image() - call the entry point of an image
2866 * @image_handle: handle of the image
2867 * @exit_data_size: size of the buffer
2868 * @exit_data: buffer to receive the exit data of the called image
2869 *
2870 * This function implements the StartImage service.
2871 *
2872 * See the Unified Extensible Firmware Interface (UEFI) specification for
2873 * details.
2874 *
2875 * Return: status code
2876 */
2877efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2878 efi_uintn_t *exit_data_size,
2879 u16 **exit_data)
2880{
2881 struct efi_loaded_image_obj *image_obj =
2882 (struct efi_loaded_image_obj *)image_handle;
2883 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002884 void *info;
2885 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002886
2887 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2888
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002889 if (!efi_search_obj(image_handle))
2890 return EFI_EXIT(EFI_INVALID_PARAMETER);
2891
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002892 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002893 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2894 return EFI_EXIT(EFI_INVALID_PARAMETER);
2895
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002896 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2897 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2898
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002899 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2900 &info, NULL, NULL,
2901 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2902 if (ret != EFI_SUCCESS)
2903 return EFI_EXIT(EFI_INVALID_PARAMETER);
2904
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002905 image_obj->exit_data_size = exit_data_size;
2906 image_obj->exit_data = exit_data;
2907
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002908 /* call the image! */
2909 if (setjmp(&image_obj->exit_jmp)) {
2910 /*
2911 * We called the entry point of the child image with EFI_CALL
2912 * in the lines below. The child image called the Exit() boot
2913 * service efi_exit() which executed the long jump that brought
2914 * us to the current line. This implies that the second half
2915 * of the EFI_CALL macro has not been executed.
2916 */
2917#ifdef CONFIG_ARM
2918 /*
2919 * efi_exit() called efi_restore_gd(). We have to undo this
2920 * otherwise __efi_entry_check() will put the wrong value into
2921 * app_gd.
2922 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02002923 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002924#endif
2925 /*
2926 * To get ready to call EFI_EXIT below we have to execute the
2927 * missed out steps of EFI_CALL.
2928 */
2929 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002930 EFI_PRINT("%lu returned by started image\n",
2931 (unsigned long)((uintptr_t)image_obj->exit_status &
2932 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002933 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002934 return EFI_EXIT(image_obj->exit_status);
2935 }
2936
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002937 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002938 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002939 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002940 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2941
2942 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01002943 * Control is returned from a started UEFI image either by calling
2944 * Exit() (where exit data can be provided) or by simply returning from
2945 * the entry point. In the latter case call Exit() on behalf of the
2946 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002947 */
2948 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2949}
2950
2951/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002952 * efi_delete_image() - delete loaded image from memory)
2953 *
2954 * @image_obj: handle of the loaded image
2955 * @loaded_image_protocol: loaded image protocol
2956 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002957static efi_status_t efi_delete_image
2958 (struct efi_loaded_image_obj *image_obj,
2959 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002960{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002961 struct efi_object *efiobj;
2962 efi_status_t r, ret = EFI_SUCCESS;
2963
2964close_next:
2965 list_for_each_entry(efiobj, &efi_obj_list, link) {
2966 struct efi_handler *protocol;
2967
2968 list_for_each_entry(protocol, &efiobj->protocols, link) {
2969 struct efi_open_protocol_info_item *info;
2970
2971 list_for_each_entry(info, &protocol->open_infos, link) {
2972 if (info->info.agent_handle !=
2973 (efi_handle_t)image_obj)
2974 continue;
2975 r = EFI_CALL(efi_close_protocol
2976 (efiobj, protocol->guid,
2977 info->info.agent_handle,
2978 info->info.controller_handle
2979 ));
2980 if (r != EFI_SUCCESS)
2981 ret = r;
2982 /*
2983 * Closing protocols may results in further
2984 * items being deleted. To play it safe loop
2985 * over all elements again.
2986 */
2987 goto close_next;
2988 }
2989 }
2990 }
2991
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002992 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2993 efi_size_in_pages(loaded_image_protocol->image_size));
2994 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002995
2996 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002997}
2998
2999/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003000 * efi_unload_image() - unload an EFI image
3001 * @image_handle: handle of the image to be unloaded
3002 *
3003 * This function implements the UnloadImage service.
3004 *
3005 * See the Unified Extensible Firmware Interface (UEFI) specification for
3006 * details.
3007 *
3008 * Return: status code
3009 */
3010efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3011{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003012 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003013 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003014 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003015
3016 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003017
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003018 efiobj = efi_search_obj(image_handle);
3019 if (!efiobj) {
3020 ret = EFI_INVALID_PARAMETER;
3021 goto out;
3022 }
3023 /* Find the loaded image protocol */
3024 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3025 (void **)&loaded_image_protocol,
3026 NULL, NULL,
3027 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3028 if (ret != EFI_SUCCESS) {
3029 ret = EFI_INVALID_PARAMETER;
3030 goto out;
3031 }
3032 switch (efiobj->type) {
3033 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3034 /* Call the unload function */
3035 if (!loaded_image_protocol->unload) {
3036 ret = EFI_UNSUPPORTED;
3037 goto out;
3038 }
3039 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3040 if (ret != EFI_SUCCESS)
3041 goto out;
3042 break;
3043 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3044 break;
3045 default:
3046 ret = EFI_INVALID_PARAMETER;
3047 goto out;
3048 }
3049 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3050 loaded_image_protocol);
3051out:
3052 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003053}
3054
3055/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003056 * efi_update_exit_data() - fill exit data parameters of StartImage()
3057 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003058 * @image_obj: image handle
3059 * @exit_data_size: size of the exit data buffer
3060 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003061 * Return: status code
3062 */
3063static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3064 efi_uintn_t exit_data_size,
3065 u16 *exit_data)
3066{
3067 efi_status_t ret;
3068
3069 /*
3070 * If exit_data is not provided to StartImage(), exit_data_size must be
3071 * ignored.
3072 */
3073 if (!image_obj->exit_data)
3074 return EFI_SUCCESS;
3075 if (image_obj->exit_data_size)
3076 *image_obj->exit_data_size = exit_data_size;
3077 if (exit_data_size && exit_data) {
3078 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3079 exit_data_size,
3080 (void **)image_obj->exit_data);
3081 if (ret != EFI_SUCCESS)
3082 return ret;
3083 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3084 } else {
3085 image_obj->exit_data = NULL;
3086 }
3087 return EFI_SUCCESS;
3088}
3089
3090/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003091 * efi_exit() - leave an EFI application or driver
3092 * @image_handle: handle of the application or driver that is exiting
3093 * @exit_status: status code
3094 * @exit_data_size: size of the buffer in bytes
3095 * @exit_data: buffer with data describing an error
3096 *
3097 * This function implements the Exit service.
3098 *
3099 * See the Unified Extensible Firmware Interface (UEFI) specification for
3100 * details.
3101 *
3102 * Return: status code
3103 */
3104static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3105 efi_status_t exit_status,
3106 efi_uintn_t exit_data_size,
3107 u16 *exit_data)
3108{
3109 /*
3110 * TODO: We should call the unload procedure of the loaded
3111 * image protocol.
3112 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003113 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003114 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003115 struct efi_loaded_image_obj *image_obj =
3116 (struct efi_loaded_image_obj *)image_handle;
3117
3118 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3119 exit_data_size, exit_data);
3120
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003121 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003122 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003123 (void **)&loaded_image_protocol,
3124 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003125 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003126 if (ret != EFI_SUCCESS) {
3127 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003128 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003129 }
3130
3131 /* Unloading of unstarted images */
3132 switch (image_obj->header.type) {
3133 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3134 break;
3135 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3136 efi_delete_image(image_obj, loaded_image_protocol);
3137 ret = EFI_SUCCESS;
3138 goto out;
3139 default:
3140 /* Handle does not refer to loaded image */
3141 ret = EFI_INVALID_PARAMETER;
3142 goto out;
3143 }
3144 /* A started image can only be unloaded it is the last one started. */
3145 if (image_handle != current_image) {
3146 ret = EFI_INVALID_PARAMETER;
3147 goto out;
3148 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003149
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003150 /* Exit data is only foreseen in case of failure. */
3151 if (exit_status != EFI_SUCCESS) {
3152 ret = efi_update_exit_data(image_obj, exit_data_size,
3153 exit_data);
3154 /* Exiting has priority. Don't return error to caller. */
3155 if (ret != EFI_SUCCESS)
3156 EFI_PRINT("%s: out of memory\n", __func__);
3157 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003158 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3159 exit_status != EFI_SUCCESS)
3160 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003161
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003162 /* Make sure entry/exit counts for EFI world cross-overs match */
3163 EFI_EXIT(exit_status);
3164
3165 /*
3166 * But longjmp out with the U-Boot gd, not the application's, as
3167 * the other end is a setjmp call inside EFI context.
3168 */
3169 efi_restore_gd();
3170
3171 image_obj->exit_status = exit_status;
3172 longjmp(&image_obj->exit_jmp, 1);
3173
3174 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003175out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003176 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003177}
3178
3179/**
Mario Six8fac2912018-07-10 08:40:17 +02003180 * efi_handle_protocol() - get interface of a protocol on a handle
3181 * @handle: handle on which the protocol shall be opened
3182 * @protocol: GUID of the protocol
3183 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003184 *
3185 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003186 *
3187 * See the Unified Extensible Firmware Interface (UEFI) specification for
3188 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003189 *
Mario Six8fac2912018-07-10 08:40:17 +02003190 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003191 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003192efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3193 const efi_guid_t *protocol,
3194 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003195{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003196 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003197 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003198}
3199
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003200/**
Mario Six8fac2912018-07-10 08:40:17 +02003201 * efi_bind_controller() - bind a single driver to a controller
3202 * @controller_handle: controller handle
3203 * @driver_image_handle: driver handle
3204 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003205 *
Mario Six8fac2912018-07-10 08:40:17 +02003206 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003207 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003208static efi_status_t efi_bind_controller(
3209 efi_handle_t controller_handle,
3210 efi_handle_t driver_image_handle,
3211 struct efi_device_path *remain_device_path)
3212{
3213 struct efi_driver_binding_protocol *binding_protocol;
3214 efi_status_t r;
3215
3216 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3217 &efi_guid_driver_binding_protocol,
3218 (void **)&binding_protocol,
3219 driver_image_handle, NULL,
3220 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3221 if (r != EFI_SUCCESS)
3222 return r;
3223 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3224 controller_handle,
3225 remain_device_path));
3226 if (r == EFI_SUCCESS)
3227 r = EFI_CALL(binding_protocol->start(binding_protocol,
3228 controller_handle,
3229 remain_device_path));
3230 EFI_CALL(efi_close_protocol(driver_image_handle,
3231 &efi_guid_driver_binding_protocol,
3232 driver_image_handle, NULL));
3233 return r;
3234}
3235
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003236/**
Mario Six8fac2912018-07-10 08:40:17 +02003237 * efi_connect_single_controller() - connect a single driver to a controller
3238 * @controller_handle: controller
3239 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003240 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003241 *
Mario Six8fac2912018-07-10 08:40:17 +02003242 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003243 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003244static efi_status_t efi_connect_single_controller(
3245 efi_handle_t controller_handle,
3246 efi_handle_t *driver_image_handle,
3247 struct efi_device_path *remain_device_path)
3248{
3249 efi_handle_t *buffer;
3250 size_t count;
3251 size_t i;
3252 efi_status_t r;
3253 size_t connected = 0;
3254
3255 /* Get buffer with all handles with driver binding protocol */
3256 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3257 &efi_guid_driver_binding_protocol,
3258 NULL, &count, &buffer));
3259 if (r != EFI_SUCCESS)
3260 return r;
3261
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003262 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003263 if (driver_image_handle) {
3264 for (; *driver_image_handle; ++driver_image_handle) {
3265 for (i = 0; i < count; ++i) {
3266 if (buffer[i] == *driver_image_handle) {
3267 buffer[i] = NULL;
3268 r = efi_bind_controller(
3269 controller_handle,
3270 *driver_image_handle,
3271 remain_device_path);
3272 /*
3273 * For drivers that do not support the
3274 * controller or are already connected
3275 * we receive an error code here.
3276 */
3277 if (r == EFI_SUCCESS)
3278 ++connected;
3279 }
3280 }
3281 }
3282 }
3283
3284 /*
3285 * TODO: Some overrides are not yet implemented:
3286 * - Platform Driver Override
3287 * - Driver Family Override Search
3288 * - Bus Specific Driver Override
3289 */
3290
3291 /* Driver Binding Search */
3292 for (i = 0; i < count; ++i) {
3293 if (buffer[i]) {
3294 r = efi_bind_controller(controller_handle,
3295 buffer[i],
3296 remain_device_path);
3297 if (r == EFI_SUCCESS)
3298 ++connected;
3299 }
3300 }
3301
3302 efi_free_pool(buffer);
3303 if (!connected)
3304 return EFI_NOT_FOUND;
3305 return EFI_SUCCESS;
3306}
3307
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003308/**
Mario Six8fac2912018-07-10 08:40:17 +02003309 * efi_connect_controller() - connect a controller to a driver
3310 * @controller_handle: handle of the controller
3311 * @driver_image_handle: handle of the driver
3312 * @remain_device_path: device path of a child controller
3313 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003314 *
3315 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003316 *
3317 * See the Unified Extensible Firmware Interface (UEFI) specification for
3318 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003319 *
3320 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003321 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003322 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3323 *
Mario Six8fac2912018-07-10 08:40:17 +02003324 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003325 */
3326static efi_status_t EFIAPI efi_connect_controller(
3327 efi_handle_t controller_handle,
3328 efi_handle_t *driver_image_handle,
3329 struct efi_device_path *remain_device_path,
3330 bool recursive)
3331{
3332 efi_status_t r;
3333 efi_status_t ret = EFI_NOT_FOUND;
3334 struct efi_object *efiobj;
3335
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003336 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003337 remain_device_path, recursive);
3338
3339 efiobj = efi_search_obj(controller_handle);
3340 if (!efiobj) {
3341 ret = EFI_INVALID_PARAMETER;
3342 goto out;
3343 }
3344
3345 r = efi_connect_single_controller(controller_handle,
3346 driver_image_handle,
3347 remain_device_path);
3348 if (r == EFI_SUCCESS)
3349 ret = EFI_SUCCESS;
3350 if (recursive) {
3351 struct efi_handler *handler;
3352 struct efi_open_protocol_info_item *item;
3353
3354 list_for_each_entry(handler, &efiobj->protocols, link) {
3355 list_for_each_entry(item, &handler->open_infos, link) {
3356 if (item->info.attributes &
3357 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3358 r = EFI_CALL(efi_connect_controller(
3359 item->info.controller_handle,
3360 driver_image_handle,
3361 remain_device_path,
3362 recursive));
3363 if (r == EFI_SUCCESS)
3364 ret = EFI_SUCCESS;
3365 }
3366 }
3367 }
3368 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003369 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003370 if (ret != EFI_SUCCESS && remain_device_path &&
3371 remain_device_path->type == DEVICE_PATH_TYPE_END)
3372 ret = EFI_SUCCESS;
3373out:
3374 return EFI_EXIT(ret);
3375}
3376
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003377/**
Mario Six8fac2912018-07-10 08:40:17 +02003378 * efi_reinstall_protocol_interface() - reinstall protocol interface
3379 * @handle: handle on which the protocol shall be reinstalled
3380 * @protocol: GUID of the protocol to be installed
3381 * @old_interface: interface to be removed
3382 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003383 *
3384 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003385 *
3386 * See the Unified Extensible Firmware Interface (UEFI) specification for
3387 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003388 *
3389 * The old interface is uninstalled. The new interface is installed.
3390 * Drivers are connected.
3391 *
Mario Six8fac2912018-07-10 08:40:17 +02003392 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003393 */
3394static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3395 efi_handle_t handle, const efi_guid_t *protocol,
3396 void *old_interface, void *new_interface)
3397{
3398 efi_status_t ret;
3399
3400 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3401 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003402
3403 /* Uninstall protocol but do not delete handle */
3404 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003405 if (ret != EFI_SUCCESS)
3406 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003407
3408 /* Install the new protocol */
3409 ret = efi_add_protocol(handle, protocol, new_interface);
3410 /*
3411 * The UEFI spec does not specify what should happen to the handle
3412 * if in case of an error no protocol interface remains on the handle.
3413 * So let's do nothing here.
3414 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003415 if (ret != EFI_SUCCESS)
3416 goto out;
3417 /*
3418 * The returned status code has to be ignored.
3419 * Do not create an error if no suitable driver for the handle exists.
3420 */
3421 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3422out:
3423 return EFI_EXIT(ret);
3424}
3425
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003426/**
Mario Six8fac2912018-07-10 08:40:17 +02003427 * efi_get_child_controllers() - get all child controllers associated to a driver
3428 * @efiobj: handle of the controller
3429 * @driver_handle: handle of the driver
3430 * @number_of_children: number of child controllers
3431 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003432 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003433 * The allocated buffer has to be freed with free().
3434 *
Mario Six8fac2912018-07-10 08:40:17 +02003435 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003436 */
3437static efi_status_t efi_get_child_controllers(
3438 struct efi_object *efiobj,
3439 efi_handle_t driver_handle,
3440 efi_uintn_t *number_of_children,
3441 efi_handle_t **child_handle_buffer)
3442{
3443 struct efi_handler *handler;
3444 struct efi_open_protocol_info_item *item;
3445 efi_uintn_t count = 0, i;
3446 bool duplicate;
3447
3448 /* Count all child controller associations */
3449 list_for_each_entry(handler, &efiobj->protocols, link) {
3450 list_for_each_entry(item, &handler->open_infos, link) {
3451 if (item->info.agent_handle == driver_handle &&
3452 item->info.attributes &
3453 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3454 ++count;
3455 }
3456 }
3457 /*
3458 * Create buffer. In case of duplicate child controller assignments
3459 * the buffer will be too large. But that does not harm.
3460 */
3461 *number_of_children = 0;
3462 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3463 if (!*child_handle_buffer)
3464 return EFI_OUT_OF_RESOURCES;
3465 /* Copy unique child handles */
3466 list_for_each_entry(handler, &efiobj->protocols, link) {
3467 list_for_each_entry(item, &handler->open_infos, link) {
3468 if (item->info.agent_handle == driver_handle &&
3469 item->info.attributes &
3470 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3471 /* Check this is a new child controller */
3472 duplicate = false;
3473 for (i = 0; i < *number_of_children; ++i) {
3474 if ((*child_handle_buffer)[i] ==
3475 item->info.controller_handle)
3476 duplicate = true;
3477 }
3478 /* Copy handle to buffer */
3479 if (!duplicate) {
3480 i = (*number_of_children)++;
3481 (*child_handle_buffer)[i] =
3482 item->info.controller_handle;
3483 }
3484 }
3485 }
3486 }
3487 return EFI_SUCCESS;
3488}
3489
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003490/**
Mario Six8fac2912018-07-10 08:40:17 +02003491 * efi_disconnect_controller() - disconnect a controller from a driver
3492 * @controller_handle: handle of the controller
3493 * @driver_image_handle: handle of the driver
3494 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003495 *
3496 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003497 *
3498 * See the Unified Extensible Firmware Interface (UEFI) specification for
3499 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003500 *
Mario Six8fac2912018-07-10 08:40:17 +02003501 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003502 */
3503static efi_status_t EFIAPI efi_disconnect_controller(
3504 efi_handle_t controller_handle,
3505 efi_handle_t driver_image_handle,
3506 efi_handle_t child_handle)
3507{
3508 struct efi_driver_binding_protocol *binding_protocol;
3509 efi_handle_t *child_handle_buffer = NULL;
3510 size_t number_of_children = 0;
3511 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003512 struct efi_object *efiobj;
3513
3514 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3515 child_handle);
3516
3517 efiobj = efi_search_obj(controller_handle);
3518 if (!efiobj) {
3519 r = EFI_INVALID_PARAMETER;
3520 goto out;
3521 }
3522
3523 if (child_handle && !efi_search_obj(child_handle)) {
3524 r = EFI_INVALID_PARAMETER;
3525 goto out;
3526 }
3527
3528 /* If no driver handle is supplied, disconnect all drivers */
3529 if (!driver_image_handle) {
3530 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3531 goto out;
3532 }
3533
3534 /* Create list of child handles */
3535 if (child_handle) {
3536 number_of_children = 1;
3537 child_handle_buffer = &child_handle;
3538 } else {
3539 efi_get_child_controllers(efiobj,
3540 driver_image_handle,
3541 &number_of_children,
3542 &child_handle_buffer);
3543 }
3544
3545 /* Get the driver binding protocol */
3546 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3547 &efi_guid_driver_binding_protocol,
3548 (void **)&binding_protocol,
3549 driver_image_handle, NULL,
3550 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003551 if (r != EFI_SUCCESS) {
3552 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003553 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003554 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003555 /* Remove the children */
3556 if (number_of_children) {
3557 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3558 controller_handle,
3559 number_of_children,
3560 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003561 if (r != EFI_SUCCESS) {
3562 r = EFI_DEVICE_ERROR;
3563 goto out;
3564 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003565 }
3566 /* Remove the driver */
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003567 if (!child_handle) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003568 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3569 controller_handle,
3570 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003571 if (r != EFI_SUCCESS) {
3572 r = EFI_DEVICE_ERROR;
3573 goto out;
3574 }
3575 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003576 EFI_CALL(efi_close_protocol(driver_image_handle,
3577 &efi_guid_driver_binding_protocol,
3578 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003579 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003580out:
3581 if (!child_handle)
3582 free(child_handle_buffer);
3583 return EFI_EXIT(r);
3584}
3585
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003586static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003587 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003588 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3589 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003590 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003591 },
3592 .raise_tpl = efi_raise_tpl,
3593 .restore_tpl = efi_restore_tpl,
3594 .allocate_pages = efi_allocate_pages_ext,
3595 .free_pages = efi_free_pages_ext,
3596 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003597 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003598 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003599 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003600 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003601 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003602 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003603 .close_event = efi_close_event,
3604 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003605 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003606 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003607 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003608 .handle_protocol = efi_handle_protocol,
3609 .reserved = NULL,
3610 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003611 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003612 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003613 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003614 .load_image = efi_load_image,
3615 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003616 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003617 .unload_image = efi_unload_image,
3618 .exit_boot_services = efi_exit_boot_services,
3619 .get_next_monotonic_count = efi_get_next_monotonic_count,
3620 .stall = efi_stall,
3621 .set_watchdog_timer = efi_set_watchdog_timer,
3622 .connect_controller = efi_connect_controller,
3623 .disconnect_controller = efi_disconnect_controller,
3624 .open_protocol = efi_open_protocol,
3625 .close_protocol = efi_close_protocol,
3626 .open_protocol_information = efi_open_protocol_information,
3627 .protocols_per_handle = efi_protocols_per_handle,
3628 .locate_handle_buffer = efi_locate_handle_buffer,
3629 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003630 .install_multiple_protocol_interfaces =
3631 efi_install_multiple_protocol_interfaces,
3632 .uninstall_multiple_protocol_interfaces =
3633 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003634 .calculate_crc32 = efi_calculate_crc32,
3635 .copy_mem = efi_copy_mem,
3636 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003637 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003638};
3639
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003640static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003641
Alexander Graf393dd912016-10-14 13:45:30 +02003642struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003643 .hdr = {
3644 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003645 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003646 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003647 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003648 .fw_vendor = firmware_vendor,
3649 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003650 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003651 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003652 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003653};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003654
3655/**
3656 * efi_initialize_system_table() - Initialize system table
3657 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003658 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003659 */
3660efi_status_t efi_initialize_system_table(void)
3661{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003662 efi_status_t ret;
3663
3664 /* Allocate configuration table array */
3665 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3666 EFI_MAX_CONFIGURATION_TABLES *
3667 sizeof(struct efi_configuration_table),
3668 (void **)&systab.tables);
3669
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003670 /*
3671 * These entries will be set to NULL in ExitBootServices(). To avoid
3672 * relocation in SetVirtualAddressMap(), set them dynamically.
3673 */
3674 systab.con_in = &efi_con_in;
3675 systab.con_out = &efi_con_out;
3676 systab.std_err = &efi_con_out;
3677 systab.boottime = &efi_boot_services;
3678
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003679 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003680 efi_update_table_header_crc32(&systab.hdr);
3681 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3682 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003683
3684 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003685}