blob: 88a7604bbf33710da7ab7b255d181dc5ed97d957 [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>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070013#include <time.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010015#include <u-boot/crc.h>
16#include <bootm.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070018#include <u-boot/crc.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010019#include <watchdog.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020023/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010024static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020025
Alexander Grafc15d9212016-03-04 01:09:59 +010026/* This list contains all the EFI objects our payload has access to */
27LIST_HEAD(efi_obj_list);
28
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010029/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020030__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010031
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020032/* List of queued events */
33LIST_HEAD(efi_event_queue);
34
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020035/* Flag to disable timer activity in ExitBootServices() */
36static bool timers_enabled = true;
37
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020038/* List of all events registered by RegisterProtocolNotify() */
39LIST_HEAD(efi_register_notify_events);
40
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010041/* Handle of the currently executing image */
42static efi_handle_t current_image;
43
Simon Glasscdfe6962016-09-25 15:27:35 -060044#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010045/*
46 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
47 * fixed when compiling U-Boot. However, the payload does not know about that
48 * restriction so we need to manually swap its and our view of that register on
49 * EFI callback entry/exit.
50 */
51static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060052#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010053
Heinrich Schuchardt72506232019-02-09 14:10:39 +010054/* 1 if inside U-Boot code, 0 if inside EFI payload code */
55static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040056static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010057/* GUID of the device tree table */
58const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010059/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
60const efi_guid_t efi_guid_driver_binding_protocol =
61 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040062
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010063/* event group ExitBootServices() invoked */
64const efi_guid_t efi_guid_event_group_exit_boot_services =
65 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
66/* event group SetVirtualAddressMap() invoked */
67const efi_guid_t efi_guid_event_group_virtual_address_change =
68 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
69/* event group memory map changed */
70const efi_guid_t efi_guid_event_group_memory_map_change =
71 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
72/* event group boot manager about to boot */
73const efi_guid_t efi_guid_event_group_ready_to_boot =
74 EFI_EVENT_GROUP_READY_TO_BOOT;
75/* event group ResetSystem() invoked (before ExitBootServices) */
76const efi_guid_t efi_guid_event_group_reset_system =
77 EFI_EVENT_GROUP_RESET_SYSTEM;
78
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010079static efi_status_t EFIAPI efi_disconnect_controller(
80 efi_handle_t controller_handle,
81 efi_handle_t driver_image_handle,
82 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010083
Rob Clark86789d52017-07-27 08:04:18 -040084/* Called on every callback entry */
85int __efi_entry_check(void)
86{
87 int ret = entry_count++ == 0;
88#ifdef CONFIG_ARM
89 assert(efi_gd);
90 app_gd = gd;
91 gd = efi_gd;
92#endif
93 return ret;
94}
95
96/* Called on every callback exit */
97int __efi_exit_check(void)
98{
99 int ret = --entry_count == 0;
100#ifdef CONFIG_ARM
101 gd = app_gd;
102#endif
103 return ret;
104}
105
Alexander Grafc15d9212016-03-04 01:09:59 +0100106/* Called from do_bootefi_exec() */
107void efi_save_gd(void)
108{
Simon Glasscdfe6962016-09-25 15:27:35 -0600109#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100110 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600111#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100112}
113
Rob Clark86789d52017-07-27 08:04:18 -0400114/*
Mario Six8fac2912018-07-10 08:40:17 +0200115 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200116 * world so we can dump out an abort message, without any care about returning
117 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400118 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100119void efi_restore_gd(void)
120{
Simon Glasscdfe6962016-09-25 15:27:35 -0600121#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100122 /* Only restore if we're already in EFI context */
123 if (!efi_gd)
124 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100125 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600126#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100127}
128
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200129/**
Mario Six8fac2912018-07-10 08:40:17 +0200130 * indent_string() - returns a string for indenting with two spaces per level
131 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100132 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200133 * A maximum of ten indent levels is supported. Higher indent levels will be
134 * truncated.
135 *
Mario Six8fac2912018-07-10 08:40:17 +0200136 * Return: A string for indenting with two spaces per level is
137 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400138 */
139static const char *indent_string(int level)
140{
141 const char *indent = " ";
142 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100143
Rob Clarke7896c32017-07-27 08:04:19 -0400144 level = min(max, level * 2);
145 return &indent[max - level];
146}
147
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200148const char *__efi_nesting(void)
149{
150 return indent_string(nesting_level);
151}
152
Rob Clarke7896c32017-07-27 08:04:19 -0400153const char *__efi_nesting_inc(void)
154{
155 return indent_string(nesting_level++);
156}
157
158const char *__efi_nesting_dec(void)
159{
160 return indent_string(--nesting_level);
161}
162
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200163/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200164 * efi_event_is_queued() - check if an event is queued
165 *
166 * @event: event
167 * Return: true if event is queued
168 */
169static bool efi_event_is_queued(struct efi_event *event)
170{
171 return !!event->queue_link.next;
172}
173
174/**
175 * efi_process_event_queue() - process event queue
176 */
177static void efi_process_event_queue(void)
178{
179 while (!list_empty(&efi_event_queue)) {
180 struct efi_event *event;
181 efi_uintn_t old_tpl;
182
183 event = list_first_entry(&efi_event_queue, struct efi_event,
184 queue_link);
185 if (efi_tpl >= event->notify_tpl)
186 return;
187 list_del(&event->queue_link);
188 event->queue_link.next = NULL;
189 event->queue_link.prev = NULL;
190 /* Events must be executed at the event's TPL */
191 old_tpl = efi_tpl;
192 efi_tpl = event->notify_tpl;
193 EFI_CALL_VOID(event->notify_function(event,
194 event->notify_context));
195 efi_tpl = old_tpl;
196 if (event->type == EVT_NOTIFY_SIGNAL)
197 event->is_signaled = 0;
198 }
199}
200
201/**
Mario Six8fac2912018-07-10 08:40:17 +0200202 * efi_queue_event() - queue an EFI event
203 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200204 *
205 * This function queues the notification function of the event for future
206 * execution.
207 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200208 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200209static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200210{
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200211 struct efi_event *item = NULL;
212
213 if (!event->notify_function)
214 return;
215
216 if (!efi_event_is_queued(event)) {
217 /*
218 * Events must be notified in order of decreasing task priority
219 * level. Insert the new event accordingly.
220 */
221 list_for_each_entry(item, &efi_event_queue, queue_link) {
222 if (item->notify_tpl < event->notify_tpl) {
223 list_add_tail(&event->queue_link,
224 &item->queue_link);
225 event = NULL;
226 break;
227 }
228 }
229 if (event)
230 list_add_tail(&event->queue_link, &efi_event_queue);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200231 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200232 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200233}
234
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200235/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200236 * is_valid_tpl() - check if the task priority level is valid
237 *
238 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200239 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200240 */
241efi_status_t is_valid_tpl(efi_uintn_t tpl)
242{
243 switch (tpl) {
244 case TPL_APPLICATION:
245 case TPL_CALLBACK:
246 case TPL_NOTIFY:
247 case TPL_HIGH_LEVEL:
248 return EFI_SUCCESS;
249 default:
250 return EFI_INVALID_PARAMETER;
251 }
252}
253
254/**
Mario Six8fac2912018-07-10 08:40:17 +0200255 * efi_signal_event() - signal an EFI event
256 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100257 *
Mario Six8fac2912018-07-10 08:40:17 +0200258 * This function signals an event. If the event belongs to an event group all
259 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100260 * their notification function is queued.
261 *
262 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100263 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200264void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100265{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200266 if (event->is_signaled)
267 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100268 if (event->group) {
269 struct efi_event *evt;
270
271 /*
272 * The signaled state has to set before executing any
273 * notification function
274 */
275 list_for_each_entry(evt, &efi_events, link) {
276 if (!evt->group || guidcmp(evt->group, event->group))
277 continue;
278 if (evt->is_signaled)
279 continue;
280 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100281 }
282 list_for_each_entry(evt, &efi_events, link) {
283 if (!evt->group || guidcmp(evt->group, event->group))
284 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200285 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100286 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200287 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100288 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200289 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100290 }
291}
292
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200293/**
Mario Six8fac2912018-07-10 08:40:17 +0200294 * efi_raise_tpl() - raise the task priority level
295 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200296 *
297 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200298 *
299 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200301 *
Mario Six8fac2912018-07-10 08:40:17 +0200302 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200303 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100304static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100305{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100306 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200307
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200308 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200309
310 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200311 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200312 efi_tpl = new_tpl;
313 if (efi_tpl > TPL_HIGH_LEVEL)
314 efi_tpl = TPL_HIGH_LEVEL;
315
316 EFI_EXIT(EFI_SUCCESS);
317 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100318}
319
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200320/**
Mario Six8fac2912018-07-10 08:40:17 +0200321 * efi_restore_tpl() - lower the task priority level
322 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200323 *
324 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200325 *
Mario Six8fac2912018-07-10 08:40:17 +0200326 * See the Unified Extensible Firmware Interface (UEFI) specification for
327 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200328 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100329static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100330{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200331 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200332
333 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200334 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200335 efi_tpl = old_tpl;
336 if (efi_tpl > TPL_HIGH_LEVEL)
337 efi_tpl = TPL_HIGH_LEVEL;
338
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100339 /*
340 * Lowering the TPL may have made queued events eligible for execution.
341 */
342 efi_timer_check();
343
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200344 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100345}
346
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200347/**
Mario Six8fac2912018-07-10 08:40:17 +0200348 * efi_allocate_pages_ext() - allocate memory pages
349 * @type: type of allocation to be performed
350 * @memory_type: usage type of the allocated memory
351 * @pages: number of pages to be allocated
352 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200353 *
354 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200355 *
Mario Six8fac2912018-07-10 08:40:17 +0200356 * See the Unified Extensible Firmware Interface (UEFI) specification for
357 * details.
358 *
359 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200360 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900361static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100362 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900363 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100364{
365 efi_status_t r;
366
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100367 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100368 r = efi_allocate_pages(type, memory_type, pages, memory);
369 return EFI_EXIT(r);
370}
371
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200372/**
Mario Six8fac2912018-07-10 08:40:17 +0200373 * efi_free_pages_ext() - Free memory pages.
374 * @memory: start of the memory area to be freed
375 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200376 *
377 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200378 *
379 * See the Unified Extensible Firmware Interface (UEFI) specification for
380 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200381 *
Mario Six8fac2912018-07-10 08:40:17 +0200382 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200383 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900384static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100385 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100386{
387 efi_status_t r;
388
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900389 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100390 r = efi_free_pages(memory, pages);
391 return EFI_EXIT(r);
392}
393
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200394/**
Mario Six8fac2912018-07-10 08:40:17 +0200395 * efi_get_memory_map_ext() - get map describing memory usage
396 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
397 * on exit the size of the copied memory map
398 * @memory_map: buffer to which the memory map is written
399 * @map_key: key for the memory map
400 * @descriptor_size: size of an individual memory descriptor
401 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200402 *
403 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200404 *
Mario Six8fac2912018-07-10 08:40:17 +0200405 * See the Unified Extensible Firmware Interface (UEFI) specification for
406 * details.
407 *
408 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200409 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900410static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100411 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900412 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100413 efi_uintn_t *map_key,
414 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900415 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100416{
417 efi_status_t r;
418
419 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
420 map_key, descriptor_size, descriptor_version);
421 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
422 descriptor_size, descriptor_version);
423 return EFI_EXIT(r);
424}
425
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200426/**
Mario Six8fac2912018-07-10 08:40:17 +0200427 * efi_allocate_pool_ext() - allocate memory from pool
428 * @pool_type: type of the pool from which memory is to be allocated
429 * @size: number of bytes to be allocated
430 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200431 *
432 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200433 *
434 * See the Unified Extensible Firmware Interface (UEFI) specification for
435 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200436 *
Mario Six8fac2912018-07-10 08:40:17 +0200437 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200438 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200439static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100440 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200441 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100442{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100443 efi_status_t r;
444
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100445 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200446 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100447 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100448}
449
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200450/**
Mario Six8fac2912018-07-10 08:40:17 +0200451 * efi_free_pool_ext() - free memory from pool
452 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200453 *
454 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200455 *
Mario Six8fac2912018-07-10 08:40:17 +0200456 * See the Unified Extensible Firmware Interface (UEFI) specification for
457 * details.
458 *
459 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200460 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200461static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100462{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100463 efi_status_t r;
464
465 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200466 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100467 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100468}
469
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200470/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200471 * efi_add_handle() - add a new handle to the object list
472 *
473 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100474 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200475 * The protocols list is initialized. The handle is added to the list of known
476 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100477 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200478void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100479{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200480 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100481 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200482 INIT_LIST_HEAD(&handle->protocols);
483 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100484}
485
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200486/**
Mario Six8fac2912018-07-10 08:40:17 +0200487 * efi_create_handle() - create handle
488 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200489 *
Mario Six8fac2912018-07-10 08:40:17 +0200490 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200491 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100492efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200493{
494 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200495
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200496 obj = calloc(1, sizeof(struct efi_object));
497 if (!obj)
498 return EFI_OUT_OF_RESOURCES;
499
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100500 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200501 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200502
503 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200504}
505
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200506/**
Mario Six8fac2912018-07-10 08:40:17 +0200507 * efi_search_protocol() - find a protocol on a handle.
508 * @handle: handle
509 * @protocol_guid: GUID of the protocol
510 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100511 *
Mario Six8fac2912018-07-10 08:40:17 +0200512 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100513 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100514efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100515 const efi_guid_t *protocol_guid,
516 struct efi_handler **handler)
517{
518 struct efi_object *efiobj;
519 struct list_head *lhandle;
520
521 if (!handle || !protocol_guid)
522 return EFI_INVALID_PARAMETER;
523 efiobj = efi_search_obj(handle);
524 if (!efiobj)
525 return EFI_INVALID_PARAMETER;
526 list_for_each(lhandle, &efiobj->protocols) {
527 struct efi_handler *protocol;
528
529 protocol = list_entry(lhandle, struct efi_handler, link);
530 if (!guidcmp(protocol->guid, protocol_guid)) {
531 if (handler)
532 *handler = protocol;
533 return EFI_SUCCESS;
534 }
535 }
536 return EFI_NOT_FOUND;
537}
538
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200539/**
Mario Six8fac2912018-07-10 08:40:17 +0200540 * efi_remove_protocol() - delete protocol from a handle
541 * @handle: handle from which the protocol shall be deleted
542 * @protocol: GUID of the protocol to be deleted
543 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100544 *
Mario Six8fac2912018-07-10 08:40:17 +0200545 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100546 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100547efi_status_t efi_remove_protocol(const efi_handle_t handle,
548 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100549 void *protocol_interface)
550{
551 struct efi_handler *handler;
552 efi_status_t ret;
553
554 ret = efi_search_protocol(handle, protocol, &handler);
555 if (ret != EFI_SUCCESS)
556 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200557 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200558 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100559 list_del(&handler->link);
560 free(handler);
561 return EFI_SUCCESS;
562}
563
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200564/**
Mario Six8fac2912018-07-10 08:40:17 +0200565 * efi_remove_all_protocols() - delete all protocols from a handle
566 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100567 *
Mario Six8fac2912018-07-10 08:40:17 +0200568 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100569 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100570efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100571{
572 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100573 struct efi_handler *protocol;
574 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100575
576 efiobj = efi_search_obj(handle);
577 if (!efiobj)
578 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100579 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100580 efi_status_t ret;
581
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100582 ret = efi_remove_protocol(handle, protocol->guid,
583 protocol->protocol_interface);
584 if (ret != EFI_SUCCESS)
585 return ret;
586 }
587 return EFI_SUCCESS;
588}
589
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200590/**
Mario Six8fac2912018-07-10 08:40:17 +0200591 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100592 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200593 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200595void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100596{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200597 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100598 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200599 efi_remove_all_protocols(handle);
600 list_del(&handle->link);
601 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100602}
603
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200604/**
Mario Six8fac2912018-07-10 08:40:17 +0200605 * efi_is_event() - check if a pointer is a valid event
606 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100607 *
Mario Six8fac2912018-07-10 08:40:17 +0200608 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100609 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100610static efi_status_t efi_is_event(const struct efi_event *event)
611{
612 const struct efi_event *evt;
613
614 if (!event)
615 return EFI_INVALID_PARAMETER;
616 list_for_each_entry(evt, &efi_events, link) {
617 if (evt == event)
618 return EFI_SUCCESS;
619 }
620 return EFI_INVALID_PARAMETER;
621}
Alexander Grafc15d9212016-03-04 01:09:59 +0100622
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200623/**
Mario Six8fac2912018-07-10 08:40:17 +0200624 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200625 *
Mario Six8fac2912018-07-10 08:40:17 +0200626 * @type: type of the event to create
627 * @notify_tpl: task priority level of the event
628 * @notify_function: notification function of the event
629 * @notify_context: pointer passed to the notification function
630 * @group: event group
631 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200632 *
633 * This function is used inside U-Boot code to create an event.
634 *
635 * For the API function implementing the CreateEvent service see
636 * efi_create_event_ext.
637 *
Mario Six8fac2912018-07-10 08:40:17 +0200638 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200639 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100640efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200641 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200642 struct efi_event *event,
643 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100644 void *notify_context, efi_guid_t *group,
645 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100646{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100647 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200648 efi_status_t ret;
649 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200650
Jonathan Gray7758b212017-03-12 19:26:07 +1100651 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200652 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100653
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200654 switch (type) {
655 case 0:
656 case EVT_TIMER:
657 case EVT_NOTIFY_SIGNAL:
658 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
659 case EVT_NOTIFY_WAIT:
660 case EVT_TIMER | EVT_NOTIFY_WAIT:
661 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200662 pool_type = EFI_BOOT_SERVICES_DATA;
663 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200664 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200665 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200666 break;
667 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200668 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200669 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100670
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900671 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardtad7a2152019-04-25 07:30:41 +0200672 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200673 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100674
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200675 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
676 (void **)&evt);
677 if (ret != EFI_SUCCESS)
678 return ret;
679 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100680 evt->type = type;
681 evt->notify_tpl = notify_tpl;
682 evt->notify_function = notify_function;
683 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100684 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200685 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100686 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100687 list_add_tail(&evt->link, &efi_events);
688 *event = evt;
689 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100690}
691
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200692/*
Mario Six8fac2912018-07-10 08:40:17 +0200693 * efi_create_event_ex() - create an event in a group
694 * @type: type of the event to create
695 * @notify_tpl: task priority level of the event
696 * @notify_function: notification function of the event
697 * @notify_context: pointer passed to the notification function
698 * @event: created event
699 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100700 *
701 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100702 *
Mario Six8fac2912018-07-10 08:40:17 +0200703 * See the Unified Extensible Firmware Interface (UEFI) specification for
704 * details.
705 *
706 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100707 */
708efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
709 void (EFIAPI *notify_function) (
710 struct efi_event *event,
711 void *context),
712 void *notify_context,
713 efi_guid_t *event_group,
714 struct efi_event **event)
715{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200716 efi_status_t ret;
717
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100718 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
719 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200720
721 /*
722 * The allowable input parameters are the same as in CreateEvent()
723 * except for the following two disallowed event types.
724 */
725 switch (type) {
726 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
727 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
728 ret = EFI_INVALID_PARAMETER;
729 goto out;
730 }
731
732 ret = efi_create_event(type, notify_tpl, notify_function,
733 notify_context, event_group, event);
734out:
735 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100736}
737
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200738/**
Mario Six8fac2912018-07-10 08:40:17 +0200739 * efi_create_event_ext() - create an event
740 * @type: type of the event to create
741 * @notify_tpl: task priority level of the event
742 * @notify_function: notification function of the event
743 * @notify_context: pointer passed to the notification function
744 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200745 *
746 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200747 *
748 * See the Unified Extensible Firmware Interface (UEFI) specification for
749 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200750 *
Mario Six8fac2912018-07-10 08:40:17 +0200751 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200752 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200753static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100754 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200755 void (EFIAPI *notify_function) (
756 struct efi_event *event,
757 void *context),
758 void *notify_context, struct efi_event **event)
759{
760 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
761 notify_context);
762 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100763 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200764}
765
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200766/**
Mario Six8fac2912018-07-10 08:40:17 +0200767 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200768 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200769 * Check if a timer event has occurred or a queued notification function should
770 * be called.
771 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100772 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200773 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100774 */
775void efi_timer_check(void)
776{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100777 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100778 u64 now = timer_get_us();
779
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100780 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200781 if (!timers_enabled)
782 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100783 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200784 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100785 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200786 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100787 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200788 break;
789 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100790 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200791 break;
792 default:
793 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200794 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100795 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200796 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100797 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200798 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100799 WATCHDOG_RESET();
800}
801
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200802/**
Mario Six8fac2912018-07-10 08:40:17 +0200803 * efi_set_timer() - set the trigger time for a timer event or stop the event
804 * @event: event for which the timer is set
805 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200806 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200807 *
808 * This is the function for internal usage in U-Boot. For the API function
809 * implementing the SetTimer service see efi_set_timer_ext.
810 *
Mario Six8fac2912018-07-10 08:40:17 +0200811 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200812 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200813efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200814 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100815{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100816 /* Check that the event is valid */
817 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
818 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100819
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200820 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200821 * The parameter defines a multiple of 100 ns.
822 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200823 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200824 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100825
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100826 switch (type) {
827 case EFI_TIMER_STOP:
828 event->trigger_next = -1ULL;
829 break;
830 case EFI_TIMER_PERIODIC:
831 case EFI_TIMER_RELATIVE:
832 event->trigger_next = timer_get_us() + trigger_time;
833 break;
834 default:
835 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100836 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100837 event->trigger_type = type;
838 event->trigger_time = trigger_time;
839 event->is_signaled = false;
840 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100841}
842
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200843/**
Mario Six8fac2912018-07-10 08:40:17 +0200844 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
845 * event
846 * @event: event for which the timer is set
847 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200848 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200849 *
850 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200851 *
Mario Six8fac2912018-07-10 08:40:17 +0200852 * See the Unified Extensible Firmware Interface (UEFI) specification for
853 * details.
854 *
855 *
856 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200857 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200858static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
859 enum efi_timer_delay type,
860 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200861{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900862 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200863 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
864}
865
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200866/**
Mario Six8fac2912018-07-10 08:40:17 +0200867 * efi_wait_for_event() - wait for events to be signaled
868 * @num_events: number of events to be waited for
869 * @event: events to be waited for
870 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200871 *
872 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200873 *
Mario Six8fac2912018-07-10 08:40:17 +0200874 * See the Unified Extensible Firmware Interface (UEFI) specification for
875 * details.
876 *
877 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200878 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100879static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200880 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100881 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100882{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100883 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100884
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100885 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100886
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200887 /* Check parameters */
888 if (!num_events || !event)
889 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200890 /* Check TPL */
891 if (efi_tpl != TPL_APPLICATION)
892 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200893 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100894 if (efi_is_event(event[i]) != EFI_SUCCESS)
895 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200896 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
897 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200898 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200899 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200900 }
901
902 /* Wait for signal */
903 for (;;) {
904 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200905 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200906 goto out;
907 }
908 /* Allow events to occur. */
909 efi_timer_check();
910 }
911
912out:
913 /*
914 * Reset the signal which is passed to the caller to allow periodic
915 * events to occur.
916 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200917 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200918 if (index)
919 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100920
921 return EFI_EXIT(EFI_SUCCESS);
922}
923
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200924/**
Mario Six8fac2912018-07-10 08:40:17 +0200925 * efi_signal_event_ext() - signal an EFI event
926 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200927 *
928 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200929 *
930 * See the Unified Extensible Firmware Interface (UEFI) specification for
931 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200932 *
933 * This functions sets the signaled state of the event and queues the
934 * notification function for execution.
935 *
Mario Six8fac2912018-07-10 08:40:17 +0200936 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200937 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200938static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100939{
940 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100941 if (efi_is_event(event) != EFI_SUCCESS)
942 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200943 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100944 return EFI_EXIT(EFI_SUCCESS);
945}
946
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200947/**
Mario Six8fac2912018-07-10 08:40:17 +0200948 * efi_close_event() - close an EFI event
949 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200950 *
951 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200952 *
Mario Six8fac2912018-07-10 08:40:17 +0200953 * See the Unified Extensible Firmware Interface (UEFI) specification for
954 * details.
955 *
956 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200957 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200958static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100959{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200960 struct efi_register_notify_event *item, *next;
961
Alexander Grafc15d9212016-03-04 01:09:59 +0100962 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100963 if (efi_is_event(event) != EFI_SUCCESS)
964 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200965
966 /* Remove protocol notify registrations for the event */
967 list_for_each_entry_safe(item, next, &efi_register_notify_events,
968 link) {
969 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200970 struct efi_protocol_notification *hitem, *hnext;
971
972 /* Remove signaled handles */
973 list_for_each_entry_safe(hitem, hnext, &item->handles,
974 link) {
975 list_del(&hitem->link);
976 free(hitem);
977 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200978 list_del(&item->link);
979 free(item);
980 }
981 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200982 /* Remove event from queue */
983 if (efi_event_is_queued(event))
984 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200985
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100986 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200987 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100988 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100989}
990
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200991/**
Mario Six8fac2912018-07-10 08:40:17 +0200992 * efi_check_event() - check if an event is signaled
993 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200994 *
995 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200996 *
997 * See the Unified Extensible Firmware Interface (UEFI) specification for
998 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200999 *
Mario Six8fac2912018-07-10 08:40:17 +02001000 * If an event is not signaled yet, the notification function is queued. The
1001 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001002 *
Mario Six8fac2912018-07-10 08:40:17 +02001003 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001004 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001005static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001006{
1007 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001008 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001009 if (efi_is_event(event) != EFI_SUCCESS ||
1010 event->type & EVT_NOTIFY_SIGNAL)
1011 return EFI_EXIT(EFI_INVALID_PARAMETER);
1012 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001013 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001014 if (event->is_signaled) {
1015 event->is_signaled = false;
1016 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001017 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001018 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001019}
1020
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001021/**
Mario Six8fac2912018-07-10 08:40:17 +02001022 * efi_search_obj() - find the internal EFI object for a handle
1023 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001024 *
Mario Six8fac2912018-07-10 08:40:17 +02001025 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001026 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001027struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001028{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001029 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001030
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001031 if (!handle)
1032 return NULL;
1033
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001034 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001035 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001036 return efiobj;
1037 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001038 return NULL;
1039}
1040
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001041/**
Mario Six8fac2912018-07-10 08:40:17 +02001042 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1043 * to a protocol
1044 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001045 *
Mario Six8fac2912018-07-10 08:40:17 +02001046 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001047 */
1048static struct efi_open_protocol_info_entry *efi_create_open_info(
1049 struct efi_handler *handler)
1050{
1051 struct efi_open_protocol_info_item *item;
1052
1053 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1054 if (!item)
1055 return NULL;
1056 /* Append the item to the open protocol info list. */
1057 list_add_tail(&item->link, &handler->open_infos);
1058
1059 return &item->info;
1060}
1061
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001062/**
Mario Six8fac2912018-07-10 08:40:17 +02001063 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1064 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001065 *
Mario Six8fac2912018-07-10 08:40:17 +02001066 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001067 */
1068static efi_status_t efi_delete_open_info(
1069 struct efi_open_protocol_info_item *item)
1070{
1071 list_del(&item->link);
1072 free(item);
1073 return EFI_SUCCESS;
1074}
1075
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001076/**
Mario Six8fac2912018-07-10 08:40:17 +02001077 * efi_add_protocol() - install new protocol on a handle
1078 * @handle: handle on which the protocol shall be installed
1079 * @protocol: GUID of the protocol to be installed
1080 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001081 *
Mario Six8fac2912018-07-10 08:40:17 +02001082 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001083 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001084efi_status_t efi_add_protocol(const efi_handle_t handle,
1085 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001086 void *protocol_interface)
1087{
1088 struct efi_object *efiobj;
1089 struct efi_handler *handler;
1090 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001091 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001092
1093 efiobj = efi_search_obj(handle);
1094 if (!efiobj)
1095 return EFI_INVALID_PARAMETER;
1096 ret = efi_search_protocol(handle, protocol, NULL);
1097 if (ret != EFI_NOT_FOUND)
1098 return EFI_INVALID_PARAMETER;
1099 handler = calloc(1, sizeof(struct efi_handler));
1100 if (!handler)
1101 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001102 handler->guid = protocol;
1103 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001104 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001105 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001106
1107 /* Notify registered events */
1108 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001109 if (!guidcmp(protocol, &event->protocol)) {
1110 struct efi_protocol_notification *notif;
1111
1112 notif = calloc(1, sizeof(*notif));
1113 if (!notif) {
1114 list_del(&handler->link);
1115 free(handler);
1116 return EFI_OUT_OF_RESOURCES;
1117 }
1118 notif->handle = handle;
1119 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001120 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001121 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001122 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001123 }
1124
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001125 if (!guidcmp(&efi_guid_device_path, protocol))
1126 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001127 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001128}
1129
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001130/**
Mario Six8fac2912018-07-10 08:40:17 +02001131 * efi_install_protocol_interface() - install protocol interface
1132 * @handle: handle on which the protocol shall be installed
1133 * @protocol: GUID of the protocol to be installed
1134 * @protocol_interface_type: type of the interface to be installed,
1135 * always EFI_NATIVE_INTERFACE
1136 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001137 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001138 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001139 *
Mario Six8fac2912018-07-10 08:40:17 +02001140 * See the Unified Extensible Firmware Interface (UEFI) specification for
1141 * details.
1142 *
1143 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001144 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001145static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001146 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001147 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001148{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001149 efi_status_t r;
1150
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001151 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1152 protocol_interface);
1153
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001154 if (!handle || !protocol ||
1155 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1156 r = EFI_INVALID_PARAMETER;
1157 goto out;
1158 }
1159
1160 /* Create new handle if requested. */
1161 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001162 r = efi_create_handle(handle);
1163 if (r != EFI_SUCCESS)
1164 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001165 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001166 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001167 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001168 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001169 /* Add new protocol */
1170 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001171out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001172 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001173}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001174
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001175/**
Mario Six8fac2912018-07-10 08:40:17 +02001176 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001177 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001178 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001179 * @number_of_drivers: number of child controllers
1180 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001181 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001182 * The allocated buffer has to be freed with free().
1183 *
Mario Six8fac2912018-07-10 08:40:17 +02001184 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001185 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001186static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001187 const efi_guid_t *protocol,
1188 efi_uintn_t *number_of_drivers,
1189 efi_handle_t **driver_handle_buffer)
1190{
1191 struct efi_handler *handler;
1192 struct efi_open_protocol_info_item *item;
1193 efi_uintn_t count = 0, i;
1194 bool duplicate;
1195
1196 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001197 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001198 if (protocol && guidcmp(handler->guid, protocol))
1199 continue;
1200 list_for_each_entry(item, &handler->open_infos, link) {
1201 if (item->info.attributes &
1202 EFI_OPEN_PROTOCOL_BY_DRIVER)
1203 ++count;
1204 }
1205 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001206 *number_of_drivers = 0;
1207 if (!count) {
1208 *driver_handle_buffer = NULL;
1209 return EFI_SUCCESS;
1210 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001211 /*
1212 * Create buffer. In case of duplicate driver assignments the buffer
1213 * will be too large. But that does not harm.
1214 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001215 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1216 if (!*driver_handle_buffer)
1217 return EFI_OUT_OF_RESOURCES;
1218 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001219 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001220 if (protocol && guidcmp(handler->guid, protocol))
1221 continue;
1222 list_for_each_entry(item, &handler->open_infos, link) {
1223 if (item->info.attributes &
1224 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1225 /* Check this is a new driver */
1226 duplicate = false;
1227 for (i = 0; i < *number_of_drivers; ++i) {
1228 if ((*driver_handle_buffer)[i] ==
1229 item->info.agent_handle)
1230 duplicate = true;
1231 }
1232 /* Copy handle to buffer */
1233 if (!duplicate) {
1234 i = (*number_of_drivers)++;
1235 (*driver_handle_buffer)[i] =
1236 item->info.agent_handle;
1237 }
1238 }
1239 }
1240 }
1241 return EFI_SUCCESS;
1242}
1243
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001244/**
Mario Six8fac2912018-07-10 08:40:17 +02001245 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001246 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001247 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001248 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001249 *
1250 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001251 *
1252 * See the Unified Extensible Firmware Interface (UEFI) specification for
1253 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001254 *
Mario Six8fac2912018-07-10 08:40:17 +02001255 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001256 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001257static efi_status_t efi_disconnect_all_drivers
1258 (efi_handle_t handle,
1259 const efi_guid_t *protocol,
1260 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001261{
1262 efi_uintn_t number_of_drivers;
1263 efi_handle_t *driver_handle_buffer;
1264 efi_status_t r, ret;
1265
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001266 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001267 &driver_handle_buffer);
1268 if (ret != EFI_SUCCESS)
1269 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001270 if (!number_of_drivers)
1271 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001272 ret = EFI_NOT_FOUND;
1273 while (number_of_drivers) {
1274 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001275 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001276 driver_handle_buffer[--number_of_drivers],
1277 child_handle));
1278 if (r == EFI_SUCCESS)
1279 ret = r;
1280 }
1281 free(driver_handle_buffer);
1282 return ret;
1283}
1284
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001285/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001286 * efi_uninstall_protocol() - uninstall protocol interface
1287 *
Mario Six8fac2912018-07-10 08:40:17 +02001288 * @handle: handle from which the protocol shall be removed
1289 * @protocol: GUID of the protocol to be removed
1290 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001291 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001292 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001293 *
1294 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001295 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001296static efi_status_t efi_uninstall_protocol
1297 (efi_handle_t handle, const efi_guid_t *protocol,
1298 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001299{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001300 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001301 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001302 struct efi_open_protocol_info_item *item;
1303 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001304 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001305
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001306 /* Check handle */
1307 efiobj = efi_search_obj(handle);
1308 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001309 r = EFI_INVALID_PARAMETER;
1310 goto out;
1311 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001312 /* Find the protocol on the handle */
1313 r = efi_search_protocol(handle, protocol, &handler);
1314 if (r != EFI_SUCCESS)
1315 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001316 /* Disconnect controllers */
1317 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001318 /* Close protocol */
1319 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1320 if (item->info.attributes ==
1321 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1322 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1323 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1324 list_del(&item->link);
1325 }
1326 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001327 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001328 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001329 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001330 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001331out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001332 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001333}
1334
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001335/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001336 * efi_uninstall_protocol_interface() - uninstall protocol interface
1337 * @handle: handle from which the protocol shall be removed
1338 * @protocol: GUID of the protocol to be removed
1339 * @protocol_interface: interface to be removed
1340 *
1341 * This function implements the UninstallProtocolInterface service.
1342 *
1343 * See the Unified Extensible Firmware Interface (UEFI) specification for
1344 * details.
1345 *
1346 * Return: status code
1347 */
1348static efi_status_t EFIAPI efi_uninstall_protocol_interface
1349 (efi_handle_t handle, const efi_guid_t *protocol,
1350 void *protocol_interface)
1351{
1352 efi_status_t ret;
1353
1354 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1355
1356 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1357 if (ret != EFI_SUCCESS)
1358 goto out;
1359
1360 /* If the last protocol has been removed, delete the handle. */
1361 if (list_empty(&handle->protocols)) {
1362 list_del(&handle->link);
1363 free(handle);
1364 }
1365out:
1366 return EFI_EXIT(ret);
1367}
1368
1369/**
Mario Six8fac2912018-07-10 08:40:17 +02001370 * efi_register_protocol_notify() - register an event for notification when a
1371 * protocol is installed.
1372 * @protocol: GUID of the protocol whose installation shall be notified
1373 * @event: event to be signaled upon installation of the protocol
1374 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001375 *
1376 * This function implements the RegisterProtocolNotify service.
1377 * See the Unified Extensible Firmware Interface (UEFI) specification
1378 * for details.
1379 *
Mario Six8fac2912018-07-10 08:40:17 +02001380 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001381 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001382static efi_status_t EFIAPI efi_register_protocol_notify(
1383 const efi_guid_t *protocol,
1384 struct efi_event *event,
1385 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001386{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001387 struct efi_register_notify_event *item;
1388 efi_status_t ret = EFI_SUCCESS;
1389
Rob Clark238f88c2017-09-13 18:05:41 -04001390 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001391
1392 if (!protocol || !event || !registration) {
1393 ret = EFI_INVALID_PARAMETER;
1394 goto out;
1395 }
1396
1397 item = calloc(1, sizeof(struct efi_register_notify_event));
1398 if (!item) {
1399 ret = EFI_OUT_OF_RESOURCES;
1400 goto out;
1401 }
1402
1403 item->event = event;
1404 memcpy(&item->protocol, protocol, sizeof(efi_guid_t));
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001405 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001406
1407 list_add_tail(&item->link, &efi_register_notify_events);
1408
1409 *registration = item;
1410out:
1411 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001412}
1413
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001414/**
Mario Six8fac2912018-07-10 08:40:17 +02001415 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001416 *
Mario Six8fac2912018-07-10 08:40:17 +02001417 * @search_type: selection criterion
1418 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001419 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001420 *
1421 * See the documentation of the LocateHandle service in the UEFI specification.
1422 *
Mario Six8fac2912018-07-10 08:40:17 +02001423 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001424 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001425static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001426 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001427{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001428 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001429
1430 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001431 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001432 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001433 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001434 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001435 return (ret != EFI_SUCCESS);
1436 default:
1437 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001438 return -1;
1439 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001440}
1441
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001442/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001443 * efi_check_register_notify_event() - check if registration key is valid
1444 *
1445 * Check that a pointer is a valid registration key as returned by
1446 * RegisterProtocolNotify().
1447 *
1448 * @key: registration key
1449 * Return: valid registration key or NULL
1450 */
1451static struct efi_register_notify_event *efi_check_register_notify_event
1452 (void *key)
1453{
1454 struct efi_register_notify_event *event;
1455
1456 list_for_each_entry(event, &efi_register_notify_events, link) {
1457 if (event == (struct efi_register_notify_event *)key)
1458 return event;
1459 }
1460 return NULL;
1461}
1462
1463/**
Mario Six8fac2912018-07-10 08:40:17 +02001464 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001465 *
1466 * @search_type: selection criterion
1467 * @protocol: GUID of the protocol
1468 * @search_key: registration key
1469 * @buffer_size: size of the buffer to receive the handles in bytes
1470 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001471 *
1472 * This function is meant for U-Boot internal calls. For the API implementation
1473 * of the LocateHandle service see efi_locate_handle_ext.
1474 *
Mario Six8fac2912018-07-10 08:40:17 +02001475 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001476 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001477static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001478 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001479 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001480 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001481{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001482 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001483 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001484 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001485 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001486
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001487 /* Check parameters */
1488 switch (search_type) {
1489 case ALL_HANDLES:
1490 break;
1491 case BY_REGISTER_NOTIFY:
1492 if (!search_key)
1493 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001494 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001495 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001496 if (!event)
1497 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001498 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001499 case BY_PROTOCOL:
1500 if (!protocol)
1501 return EFI_INVALID_PARAMETER;
1502 break;
1503 default:
1504 return EFI_INVALID_PARAMETER;
1505 }
1506
Alexander Grafc15d9212016-03-04 01:09:59 +01001507 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001508 if (search_type == BY_REGISTER_NOTIFY) {
1509 if (list_empty(&event->handles))
1510 return EFI_NOT_FOUND;
1511 handle = list_first_entry(&event->handles,
1512 struct efi_protocol_notification,
1513 link);
1514 efiobj = handle->handle;
1515 size += sizeof(void *);
1516 } else {
1517 list_for_each_entry(efiobj, &efi_obj_list, link) {
1518 if (!efi_search(search_type, protocol, efiobj))
1519 size += sizeof(void *);
1520 }
1521 if (size == 0)
1522 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001523 }
1524
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001525 if (!buffer_size)
1526 return EFI_INVALID_PARAMETER;
1527
Alexander Grafc15d9212016-03-04 01:09:59 +01001528 if (*buffer_size < size) {
1529 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001530 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001531 }
1532
Rob Clarkcdee3372017-08-06 14:10:07 -04001533 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001534
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001535 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001536 if (!buffer)
1537 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001538
Alexander Grafc15d9212016-03-04 01:09:59 +01001539 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001540 if (search_type == BY_REGISTER_NOTIFY) {
1541 *buffer = efiobj;
1542 list_del(&handle->link);
1543 } else {
1544 list_for_each_entry(efiobj, &efi_obj_list, link) {
1545 if (!efi_search(search_type, protocol, efiobj))
1546 *buffer++ = efiobj;
1547 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001548 }
1549
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001550 return EFI_SUCCESS;
1551}
1552
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001553/**
Mario Six8fac2912018-07-10 08:40:17 +02001554 * efi_locate_handle_ext() - locate handles implementing a protocol.
1555 * @search_type: selection criterion
1556 * @protocol: GUID of the protocol
1557 * @search_key: registration key
1558 * @buffer_size: size of the buffer to receive the handles in bytes
1559 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001560 *
1561 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001562 *
Mario Six8fac2912018-07-10 08:40:17 +02001563 * See the Unified Extensible Firmware Interface (UEFI) specification for
1564 * details.
1565 *
1566 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001567 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001568static efi_status_t EFIAPI efi_locate_handle_ext(
1569 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001570 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001571 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001572{
Rob Clark238f88c2017-09-13 18:05:41 -04001573 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001574 buffer_size, buffer);
1575
1576 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1577 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001578}
1579
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001580/**
Mario Six8fac2912018-07-10 08:40:17 +02001581 * efi_remove_configuration_table() - collapses configuration table entries,
1582 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001583 *
Mario Six8fac2912018-07-10 08:40:17 +02001584 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001585 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001586static void efi_remove_configuration_table(int i)
1587{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001588 struct efi_configuration_table *this = &systab.tables[i];
1589 struct efi_configuration_table *next = &systab.tables[i + 1];
1590 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001591
1592 memmove(this, next, (ulong)end - (ulong)next);
1593 systab.nr_tables--;
1594}
1595
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001596/**
Mario Six8fac2912018-07-10 08:40:17 +02001597 * efi_install_configuration_table() - adds, updates, or removes a
1598 * configuration table
1599 * @guid: GUID of the installed table
1600 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001601 *
1602 * This function is used for internal calls. For the API implementation of the
1603 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1604 *
Mario Six8fac2912018-07-10 08:40:17 +02001605 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001606 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001607efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1608 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001609{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001610 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001611 int i;
1612
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001613 if (!guid)
1614 return EFI_INVALID_PARAMETER;
1615
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001616 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001617 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001618 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001619 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001620 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001621 else
1622 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001623 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001624 }
1625 }
1626
Alexander Graffe3366f2017-07-26 13:41:04 +02001627 if (!table)
1628 return EFI_NOT_FOUND;
1629
Alexander Grafc15d9212016-03-04 01:09:59 +01001630 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001631 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001632 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001633
1634 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001635 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1636 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001637 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001638
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001639out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001640 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001641 efi_update_table_header_crc32(&systab.hdr);
1642
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001643 /* Notify that the configuration table was changed */
1644 list_for_each_entry(evt, &efi_events, link) {
1645 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001646 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001647 break;
1648 }
1649 }
1650
Alexander Grafc5c11632016-08-19 01:23:24 +02001651 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001652}
1653
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001654/**
Mario Six8fac2912018-07-10 08:40:17 +02001655 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1656 * configuration table.
1657 * @guid: GUID of the installed table
1658 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001659 *
1660 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001661 *
Mario Six8fac2912018-07-10 08:40:17 +02001662 * See the Unified Extensible Firmware Interface (UEFI) specification for
1663 * details.
1664 *
1665 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001666 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001667static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1668 void *table)
1669{
Rob Clark238f88c2017-09-13 18:05:41 -04001670 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001671 return EFI_EXIT(efi_install_configuration_table(guid, table));
1672}
1673
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001674/**
Mario Six8fac2912018-07-10 08:40:17 +02001675 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001676 *
1677 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001678 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001679 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001680 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001681 * code is returned.
1682 *
1683 * @device_path: device path of the loaded image
1684 * @file_path: file path of the loaded image
1685 * @handle_ptr: handle of the loaded image
1686 * @info_ptr: loaded image protocol
1687 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001688 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001689efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1690 struct efi_device_path *file_path,
1691 struct efi_loaded_image_obj **handle_ptr,
1692 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001693{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001694 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001695 struct efi_loaded_image *info = NULL;
1696 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001697 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001698
1699 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1700 *handle_ptr = NULL;
1701 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001702
1703 info = calloc(1, sizeof(*info));
1704 if (!info)
1705 return EFI_OUT_OF_RESOURCES;
1706 obj = calloc(1, sizeof(*obj));
1707 if (!obj) {
1708 free(info);
1709 return EFI_OUT_OF_RESOURCES;
1710 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001711 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001712
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001713 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001714 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001715
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001716 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001717 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001718 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001719
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001720 if (device_path) {
1721 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001722
1723 dp = efi_dp_append(device_path, file_path);
1724 if (!dp) {
1725 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001726 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001727 }
1728 } else {
1729 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001730 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001731 ret = efi_add_protocol(&obj->header,
1732 &efi_guid_loaded_image_device_path, dp);
1733 if (ret != EFI_SUCCESS)
1734 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001735
1736 /*
1737 * When asking for the loaded_image interface, just
1738 * return handle which points to loaded_image_info
1739 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001740 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001741 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001742 if (ret != EFI_SUCCESS)
1743 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001744
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001745 *info_ptr = info;
1746 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001747
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001748 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001749failure:
1750 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001751 efi_delete_handle(&obj->header);
1752 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001753 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001754}
1755
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001756/**
Mario Six8fac2912018-07-10 08:40:17 +02001757 * efi_load_image_from_path() - load an image using a file path
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001758 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001759 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1760 * callers obligation to update the memory type as needed.
1761 *
1762 * @file_path: the path of the image to load
1763 * @buffer: buffer containing the loaded image
1764 * @size: size of the loaded image
1765 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001766 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001767static
Rob Clarkc84c1102017-09-13 18:05:38 -04001768efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001769 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001770{
1771 struct efi_file_info *info = NULL;
1772 struct efi_file_handle *f;
1773 static efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001774 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001775 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001776
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001777 /* In case of failure nothing is returned */
1778 *buffer = NULL;
1779 *size = 0;
1780
1781 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001782 f = efi_file_from_path(file_path);
1783 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001784 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001785
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001786 /* Get file size */
Rob Clark857a1222017-09-13 18:05:35 -04001787 bs = 0;
1788 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1789 &bs, info));
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001790 if (ret != EFI_BUFFER_TOO_SMALL) {
1791 ret = EFI_DEVICE_ERROR;
Rob Clark857a1222017-09-13 18:05:35 -04001792 goto error;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001793 }
Rob Clark857a1222017-09-13 18:05:35 -04001794
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001795 info = malloc(bs);
1796 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs,
1797 info));
1798 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001799 goto error;
1800
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001801 /*
1802 * When reading the file we do not yet know if it contains an
1803 * application, a boottime driver, or a runtime driver. So here we
1804 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1805 * update the reservation according to the image type.
1806 */
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001807 bs = info->file_size;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001808 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1809 EFI_BOOT_SERVICES_DATA,
1810 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001811 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001812 ret = EFI_OUT_OF_RESOURCES;
1813 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001814 }
1815
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001816 /* Read file */
1817 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1818 if (ret != EFI_SUCCESS)
1819 efi_free_pages(addr, efi_size_in_pages(bs));
1820 *buffer = (void *)(uintptr_t)addr;
1821 *size = bs;
1822error:
1823 EFI_CALL(f->close(f));
1824 free(info);
Rob Clark857a1222017-09-13 18:05:35 -04001825 return ret;
1826}
1827
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001828/**
Mario Six8fac2912018-07-10 08:40:17 +02001829 * efi_load_image() - load an EFI image into memory
1830 * @boot_policy: true for request originating from the boot manager
1831 * @parent_image: the caller's image handle
1832 * @file_path: the path of the image to load
1833 * @source_buffer: memory location from which the image is installed
1834 * @source_size: size of the memory area from which the image is installed
1835 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001836 *
1837 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001838 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001839 * See the Unified Extensible Firmware Interface (UEFI) specification
1840 * for details.
1841 *
Mario Six8fac2912018-07-10 08:40:17 +02001842 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001843 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09001844efi_status_t EFIAPI efi_load_image(bool boot_policy,
1845 efi_handle_t parent_image,
1846 struct efi_device_path *file_path,
1847 void *source_buffer,
1848 efi_uintn_t source_size,
1849 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001850{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001851 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04001852 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001853 struct efi_loaded_image_obj **image_obj =
1854 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001855 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001856 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01001857
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001858 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001859 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001860
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02001861 if (!image_handle || (!source_buffer && !file_path) ||
1862 !efi_search_obj(parent_image) ||
1863 /* The parent image handle must refer to a loaded image */
1864 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02001865 ret = EFI_INVALID_PARAMETER;
1866 goto error;
1867 }
Rob Clark857a1222017-09-13 18:05:35 -04001868
1869 if (!source_buffer) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001870 ret = efi_load_image_from_path(file_path, &dest_buffer,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001871 &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001872 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001873 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001874 } else {
Heinrich Schuchardt78a23b52019-05-05 16:55:06 +02001875 if (!source_size) {
1876 ret = EFI_LOAD_ERROR;
1877 goto error;
1878 }
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001879 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04001880 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00001881 /* split file_path which contains both the device and file parts */
1882 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001883 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01001884 if (ret == EFI_SUCCESS)
1885 ret = efi_load_pe(*image_obj, dest_buffer, info);
1886 if (!source_buffer)
1887 /* Release buffer to which file was loaded */
1888 efi_free_pages((uintptr_t)dest_buffer,
1889 efi_size_in_pages(source_size));
1890 if (ret == EFI_SUCCESS) {
1891 info->system_table = &systab;
1892 info->parent_handle = parent_image;
1893 } else {
1894 /* The image is invalid. Release all associated resources. */
1895 efi_delete_handle(*image_handle);
1896 *image_handle = NULL;
1897 free(info);
1898 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001899error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001900 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001901}
1902
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001903/**
Alexander Graffa5246b2018-11-15 21:23:47 +01001904 * efi_exit_caches() - fix up caches for EFI payloads if necessary
1905 */
1906static void efi_exit_caches(void)
1907{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001908#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01001909 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001910 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
1911 * caches are enabled.
1912 *
1913 * TODO:
1914 * According to the UEFI spec caches that can be managed via CP15
1915 * operations should be enabled. Caches requiring platform information
1916 * to manage should be disabled. This should not happen in
1917 * ExitBootServices() but before invoking any UEFI binary is invoked.
1918 *
1919 * We want to keep the current workaround while GRUB prior to version
1920 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01001921 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02001922 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01001923#endif
1924}
1925
1926/**
Mario Six8fac2912018-07-10 08:40:17 +02001927 * efi_exit_boot_services() - stop all boot services
1928 * @image_handle: handle of the loaded image
1929 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001930 *
1931 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001932 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001933 * See the Unified Extensible Firmware Interface (UEFI) specification
1934 * for details.
1935 *
Mario Six8fac2912018-07-10 08:40:17 +02001936 * All timer events are disabled. For exit boot services events the
1937 * notification function is called. The boot services are disabled in the
1938 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001939 *
Mario Six8fac2912018-07-10 08:40:17 +02001940 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001941 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001942static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001943 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01001944{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001945 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001946 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001947
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02001948 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01001949
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001950 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001951 if (map_key != efi_memory_map_key) {
1952 ret = EFI_INVALID_PARAMETER;
1953 goto out;
1954 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001955
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001956 /* Check if ExitBootServices has already been called */
1957 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02001958 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001959
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001960 /* Stop all timer related activities */
1961 timers_enabled = false;
1962
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001963 /* Add related events to the event group */
1964 list_for_each_entry(evt, &efi_events, link) {
1965 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1966 evt->group = &efi_guid_event_group_exit_boot_services;
1967 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001968 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001969 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001970 if (evt->group &&
1971 !guidcmp(evt->group,
1972 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001973 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001974 break;
1975 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001976 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001977
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001978 /* Make sure that notification functions are not called anymore */
1979 efi_tpl = TPL_HIGH_LEVEL;
1980
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02001981 /* Notify variable services */
1982 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04001983
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001984 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
1985 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
1986 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
1987 list_del(&evt->link);
1988 }
1989
Alexander Graf2ebeb442016-11-17 01:02:57 +01001990 board_quiesce_devices();
1991
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02001992 /* Patch out unsupported runtime function */
1993 efi_runtime_detach();
1994
Alexander Graffa5246b2018-11-15 21:23:47 +01001995 /* Fix up caches for EFI payloads if necessary */
1996 efi_exit_caches();
1997
Alexander Grafc15d9212016-03-04 01:09:59 +01001998 /* This stops all lingering devices */
1999 bootm_disable_interrupts();
2000
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002001 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002002 systab.con_in_handle = NULL;
2003 systab.con_in = NULL;
2004 systab.con_out_handle = NULL;
2005 systab.con_out = NULL;
2006 systab.stderr_handle = NULL;
2007 systab.std_err = NULL;
2008 systab.boottime = NULL;
2009
2010 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002011 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002012
Alexander Grafc15d9212016-03-04 01:09:59 +01002013 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002014 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002015 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002016out:
2017 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002018}
2019
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002020/**
Mario Six8fac2912018-07-10 08:40:17 +02002021 * efi_get_next_monotonic_count() - get next value of the counter
2022 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002023 *
2024 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002025 *
2026 * See the Unified Extensible Firmware Interface (UEFI) specification for
2027 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002028 *
Mario Six8fac2912018-07-10 08:40:17 +02002029 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002030 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002031static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2032{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002033 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002034 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002035
Alexander Grafc15d9212016-03-04 01:09:59 +01002036 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002037 if (!count) {
2038 ret = EFI_INVALID_PARAMETER;
2039 goto out;
2040 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002041 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002042 ret = EFI_SUCCESS;
2043out:
2044 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002045}
2046
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002047/**
Mario Six8fac2912018-07-10 08:40:17 +02002048 * efi_stall() - sleep
2049 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002050 *
Mario Six8fac2912018-07-10 08:40:17 +02002051 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002052 *
Mario Six8fac2912018-07-10 08:40:17 +02002053 * See the Unified Extensible Firmware Interface (UEFI) specification for
2054 * details.
2055 *
2056 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002057 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002058static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2059{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002060 u64 end_tick;
2061
Alexander Grafc15d9212016-03-04 01:09:59 +01002062 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002063
2064 end_tick = get_ticks() + usec_to_tick(microseconds);
2065 while (get_ticks() < end_tick)
2066 efi_timer_check();
2067
Alexander Grafc15d9212016-03-04 01:09:59 +01002068 return EFI_EXIT(EFI_SUCCESS);
2069}
2070
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002071/**
Mario Six8fac2912018-07-10 08:40:17 +02002072 * efi_set_watchdog_timer() - reset the watchdog timer
2073 * @timeout: seconds before reset by watchdog
2074 * @watchdog_code: code to be logged when resetting
2075 * @data_size: size of buffer in bytes
2076 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002077 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002078 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002079 *
Mario Six8fac2912018-07-10 08:40:17 +02002080 * See the Unified Extensible Firmware Interface (UEFI) specification for
2081 * details.
2082 *
2083 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002084 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002085static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2086 uint64_t watchdog_code,
2087 unsigned long data_size,
2088 uint16_t *watchdog_data)
2089{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002090 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002091 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002092 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002093}
2094
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002095/**
Mario Six8fac2912018-07-10 08:40:17 +02002096 * efi_close_protocol() - close a protocol
2097 * @handle: handle on which the protocol shall be closed
2098 * @protocol: GUID of the protocol to close
2099 * @agent_handle: handle of the driver
2100 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002101 *
2102 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002103 *
Mario Six8fac2912018-07-10 08:40:17 +02002104 * See the Unified Extensible Firmware Interface (UEFI) specification for
2105 * details.
2106 *
2107 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002108 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002109static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002110 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002111 efi_handle_t agent_handle,
2112 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002113{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002114 struct efi_handler *handler;
2115 struct efi_open_protocol_info_item *item;
2116 struct efi_open_protocol_info_item *pos;
2117 efi_status_t r;
2118
Rob Clark238f88c2017-09-13 18:05:41 -04002119 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002120 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002121
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002122 if (!efi_search_obj(agent_handle) ||
2123 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002124 r = EFI_INVALID_PARAMETER;
2125 goto out;
2126 }
2127 r = efi_search_protocol(handle, protocol, &handler);
2128 if (r != EFI_SUCCESS)
2129 goto out;
2130
2131 r = EFI_NOT_FOUND;
2132 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2133 if (item->info.agent_handle == agent_handle &&
2134 item->info.controller_handle == controller_handle) {
2135 efi_delete_open_info(item);
2136 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002137 }
2138 }
2139out:
2140 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002141}
2142
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002143/**
Mario Six8fac2912018-07-10 08:40:17 +02002144 * efi_open_protocol_information() - provide information about then open status
2145 * of a protocol on a handle
2146 * @handle: handle for which the information shall be retrieved
2147 * @protocol: GUID of the protocol
2148 * @entry_buffer: buffer to receive the open protocol information
2149 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002150 *
2151 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002152 *
2153 * See the Unified Extensible Firmware Interface (UEFI) specification for
2154 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002155 *
Mario Six8fac2912018-07-10 08:40:17 +02002156 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002157 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002158static efi_status_t EFIAPI efi_open_protocol_information(
2159 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002160 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002161 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002162{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002163 unsigned long buffer_size;
2164 unsigned long count;
2165 struct efi_handler *handler;
2166 struct efi_open_protocol_info_item *item;
2167 efi_status_t r;
2168
Rob Clark238f88c2017-09-13 18:05:41 -04002169 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002170 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002171
2172 /* Check parameters */
2173 if (!entry_buffer) {
2174 r = EFI_INVALID_PARAMETER;
2175 goto out;
2176 }
2177 r = efi_search_protocol(handle, protocol, &handler);
2178 if (r != EFI_SUCCESS)
2179 goto out;
2180
2181 /* Count entries */
2182 count = 0;
2183 list_for_each_entry(item, &handler->open_infos, link) {
2184 if (item->info.open_count)
2185 ++count;
2186 }
2187 *entry_count = count;
2188 *entry_buffer = NULL;
2189 if (!count) {
2190 r = EFI_SUCCESS;
2191 goto out;
2192 }
2193
2194 /* Copy entries */
2195 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002196 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002197 (void **)entry_buffer);
2198 if (r != EFI_SUCCESS)
2199 goto out;
2200 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2201 if (item->info.open_count)
2202 (*entry_buffer)[--count] = item->info;
2203 }
2204out:
2205 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002206}
2207
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002208/**
Mario Six8fac2912018-07-10 08:40:17 +02002209 * efi_protocols_per_handle() - get protocols installed on a handle
2210 * @handle: handle for which the information is retrieved
2211 * @protocol_buffer: buffer with protocol GUIDs
2212 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002213 *
2214 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002215 *
Mario Six8fac2912018-07-10 08:40:17 +02002216 * See the Unified Extensible Firmware Interface (UEFI) specification for
2217 * details.
2218 *
2219 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002220 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002221static efi_status_t EFIAPI efi_protocols_per_handle(
2222 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002223 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002224{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002225 unsigned long buffer_size;
2226 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002227 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002228 efi_status_t r;
2229
Alexander Grafc15d9212016-03-04 01:09:59 +01002230 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2231 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002232
2233 if (!handle || !protocol_buffer || !protocol_buffer_count)
2234 return EFI_EXIT(EFI_INVALID_PARAMETER);
2235
2236 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002237 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002238
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002239 efiobj = efi_search_obj(handle);
2240 if (!efiobj)
2241 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002242
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002243 /* Count protocols */
2244 list_for_each(protocol_handle, &efiobj->protocols) {
2245 ++*protocol_buffer_count;
2246 }
2247
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002248 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002249 if (*protocol_buffer_count) {
2250 size_t j = 0;
2251
2252 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002253 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002254 (void **)protocol_buffer);
2255 if (r != EFI_SUCCESS)
2256 return EFI_EXIT(r);
2257 list_for_each(protocol_handle, &efiobj->protocols) {
2258 struct efi_handler *protocol;
2259
2260 protocol = list_entry(protocol_handle,
2261 struct efi_handler, link);
2262 (*protocol_buffer)[j] = (void *)protocol->guid;
2263 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002264 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002265 }
2266
2267 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002268}
2269
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002270/**
Mario Six8fac2912018-07-10 08:40:17 +02002271 * efi_locate_handle_buffer() - locate handles implementing a protocol
2272 * @search_type: selection criterion
2273 * @protocol: GUID of the protocol
2274 * @search_key: registration key
2275 * @no_handles: number of returned handles
2276 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002277 *
2278 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002279 *
Mario Six8fac2912018-07-10 08:40:17 +02002280 * See the Unified Extensible Firmware Interface (UEFI) specification for
2281 * details.
2282 *
2283 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002284 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002285static efi_status_t EFIAPI efi_locate_handle_buffer(
2286 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002287 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002288 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002289{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002290 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002291 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002292
Rob Clark238f88c2017-09-13 18:05:41 -04002293 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002294 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002295
2296 if (!no_handles || !buffer) {
2297 r = EFI_INVALID_PARAMETER;
2298 goto out;
2299 }
2300 *no_handles = 0;
2301 *buffer = NULL;
2302 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2303 *buffer);
2304 if (r != EFI_BUFFER_TOO_SMALL)
2305 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002306 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002307 (void **)buffer);
2308 if (r != EFI_SUCCESS)
2309 goto out;
2310 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2311 *buffer);
2312 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002313 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002314out:
2315 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002316}
2317
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002318/**
Mario Six8fac2912018-07-10 08:40:17 +02002319 * efi_locate_protocol() - find an interface implementing a protocol
2320 * @protocol: GUID of the protocol
2321 * @registration: registration key passed to the notification function
2322 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002323 *
2324 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002325 *
2326 * See the Unified Extensible Firmware Interface (UEFI) specification for
2327 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002328 *
Mario Six8fac2912018-07-10 08:40:17 +02002329 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002330 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002331static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002332 void *registration,
2333 void **protocol_interface)
2334{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002335 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002336 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002337 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002338
Rob Clark238f88c2017-09-13 18:05:41 -04002339 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002340
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002341 /*
2342 * The UEFI spec explicitly requires a protocol even if a registration
2343 * key is provided. This differs from the logic in LocateHandle().
2344 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002345 if (!protocol || !protocol_interface)
2346 return EFI_EXIT(EFI_INVALID_PARAMETER);
2347
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002348 if (registration) {
2349 struct efi_register_notify_event *event;
2350 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002351
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002352 event = efi_check_register_notify_event(registration);
2353 if (!event)
2354 return EFI_EXIT(EFI_INVALID_PARAMETER);
2355 /*
2356 * The UEFI spec requires to return EFI_NOT_FOUND if no
2357 * protocol instance matches protocol and registration.
2358 * So let's do the same for a mismatch between protocol and
2359 * registration.
2360 */
2361 if (guidcmp(&event->protocol, protocol))
2362 goto not_found;
2363 if (list_empty(&event->handles))
2364 goto not_found;
2365 handle = list_first_entry(&event->handles,
2366 struct efi_protocol_notification,
2367 link);
2368 efiobj = handle->handle;
2369 list_del(&handle->link);
2370 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002371 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002372 if (ret == EFI_SUCCESS)
2373 goto found;
2374 } else {
2375 list_for_each_entry(efiobj, &efi_obj_list, link) {
2376 ret = efi_search_protocol(efiobj, protocol, &handler);
2377 if (ret == EFI_SUCCESS)
2378 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002379 }
2380 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002381not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002382 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002383 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002384found:
2385 *protocol_interface = handler->protocol_interface;
2386 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002387}
2388
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002389/**
Mario Six8fac2912018-07-10 08:40:17 +02002390 * efi_locate_device_path() - Get the device path and handle of an device
2391 * implementing a protocol
2392 * @protocol: GUID of the protocol
2393 * @device_path: device path
2394 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002395 *
2396 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002397 *
Mario Six8fac2912018-07-10 08:40:17 +02002398 * See the Unified Extensible Firmware Interface (UEFI) specification for
2399 * details.
2400 *
2401 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002402 */
2403static efi_status_t EFIAPI efi_locate_device_path(
2404 const efi_guid_t *protocol,
2405 struct efi_device_path **device_path,
2406 efi_handle_t *device)
2407{
2408 struct efi_device_path *dp;
2409 size_t i;
2410 struct efi_handler *handler;
2411 efi_handle_t *handles;
2412 size_t len, len_dp;
2413 size_t len_best = 0;
2414 efi_uintn_t no_handles;
2415 u8 *remainder;
2416 efi_status_t ret;
2417
2418 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2419
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002420 if (!protocol || !device_path || !*device_path) {
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002421 ret = EFI_INVALID_PARAMETER;
2422 goto out;
2423 }
2424
2425 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002426 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002427
2428 /* Get all handles implementing the protocol */
2429 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2430 &no_handles, &handles));
2431 if (ret != EFI_SUCCESS)
2432 goto out;
2433
2434 for (i = 0; i < no_handles; ++i) {
2435 /* Find the device path protocol */
2436 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2437 &handler);
2438 if (ret != EFI_SUCCESS)
2439 continue;
2440 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002441 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002442 /*
2443 * This handle can only be a better fit
2444 * if its device path length is longer than the best fit and
2445 * if its device path length is shorter of equal the searched
2446 * device path.
2447 */
2448 if (len_dp <= len_best || len_dp > len)
2449 continue;
2450 /* Check if dp is a subpath of device_path */
2451 if (memcmp(*device_path, dp, len_dp))
2452 continue;
Heinrich Schuchardt6db1e5c2019-05-10 19:21:41 +02002453 if (!device) {
2454 ret = EFI_INVALID_PARAMETER;
2455 goto out;
2456 }
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002457 *device = handles[i];
2458 len_best = len_dp;
2459 }
2460 if (len_best) {
2461 remainder = (u8 *)*device_path + len_best;
2462 *device_path = (struct efi_device_path *)remainder;
2463 ret = EFI_SUCCESS;
2464 } else {
2465 ret = EFI_NOT_FOUND;
2466 }
2467out:
2468 return EFI_EXIT(ret);
2469}
2470
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002471/**
Mario Six8fac2912018-07-10 08:40:17 +02002472 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2473 * interfaces
2474 * @handle: handle on which the protocol interfaces shall be installed
2475 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2476 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002477 *
2478 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002479 *
2480 * See the Unified Extensible Firmware Interface (UEFI) specification for
2481 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002482 *
Mario Six8fac2912018-07-10 08:40:17 +02002483 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002484 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002485efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002486 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002487{
2488 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002489
Alexander Graf404a7c82018-06-18 17:23:05 +02002490 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002491 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002492 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002493 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002494 efi_status_t r = EFI_SUCCESS;
2495 int i = 0;
2496
2497 if (!handle)
2498 return EFI_EXIT(EFI_INVALID_PARAMETER);
2499
Alexander Graf404a7c82018-06-18 17:23:05 +02002500 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002501 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002502 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002503 if (!protocol)
2504 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002505 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002506 /* Check that a device path has not been installed before */
2507 if (!guidcmp(protocol, &efi_guid_device_path)) {
2508 struct efi_device_path *dp = protocol_interface;
2509
2510 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2511 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002512 if (r == EFI_SUCCESS &&
2513 dp->type == DEVICE_PATH_TYPE_END) {
2514 EFI_PRINT("Path %pD already installed\n",
2515 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002516 r = EFI_ALREADY_STARTED;
2517 break;
2518 }
2519 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002520 r = EFI_CALL(efi_install_protocol_interface(
2521 handle, protocol,
2522 EFI_NATIVE_INTERFACE,
2523 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002524 if (r != EFI_SUCCESS)
2525 break;
2526 i++;
2527 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002528 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002529 if (r == EFI_SUCCESS)
2530 return EFI_EXIT(r);
2531
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002532 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002533 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002534 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002535 protocol = efi_va_arg(argptr, efi_guid_t*);
2536 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002537 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002538 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002539 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002540 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002541
2542 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002543}
2544
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002545/**
Mario Six8fac2912018-07-10 08:40:17 +02002546 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2547 * interfaces
2548 * @handle: handle from which the protocol interfaces shall be removed
2549 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2550 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002551 *
2552 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002553 *
Mario Six8fac2912018-07-10 08:40:17 +02002554 * See the Unified Extensible Firmware Interface (UEFI) specification for
2555 * details.
2556 *
2557 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002558 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002559static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002560 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002561{
2562 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002563
Alexander Graf404a7c82018-06-18 17:23:05 +02002564 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002565 const efi_guid_t *protocol;
2566 void *protocol_interface;
2567 efi_status_t r = EFI_SUCCESS;
2568 size_t i = 0;
2569
2570 if (!handle)
2571 return EFI_EXIT(EFI_INVALID_PARAMETER);
2572
Alexander Graf404a7c82018-06-18 17:23:05 +02002573 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002574 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002575 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002576 if (!protocol)
2577 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002578 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002579 r = efi_uninstall_protocol(handle, protocol,
2580 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002581 if (r != EFI_SUCCESS)
2582 break;
2583 i++;
2584 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002585 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002586 if (r == EFI_SUCCESS) {
2587 /* If the last protocol has been removed, delete the handle. */
2588 if (list_empty(&handle->protocols)) {
2589 list_del(&handle->link);
2590 free(handle);
2591 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002592 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002593 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002594
2595 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002596 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002597 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002598 protocol = efi_va_arg(argptr, efi_guid_t*);
2599 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002600 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2601 EFI_NATIVE_INTERFACE,
2602 protocol_interface));
2603 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002604 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002605
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002606 /* In case of an error always return EFI_INVALID_PARAMETER */
2607 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002608}
2609
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002610/**
Mario Six8fac2912018-07-10 08:40:17 +02002611 * efi_calculate_crc32() - calculate cyclic redundancy code
2612 * @data: buffer with data
2613 * @data_size: size of buffer in bytes
2614 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002615 *
2616 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002617 *
2618 * See the Unified Extensible Firmware Interface (UEFI) specification for
2619 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002620 *
Mario Six8fac2912018-07-10 08:40:17 +02002621 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002622 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002623static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2624 efi_uintn_t data_size,
2625 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002626{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002627 efi_status_t ret = EFI_SUCCESS;
2628
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002629 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002630 if (!data || !data_size || !crc32_p) {
2631 ret = EFI_INVALID_PARAMETER;
2632 goto out;
2633 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002634 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002635out:
2636 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002637}
2638
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002639/**
Mario Six8fac2912018-07-10 08:40:17 +02002640 * efi_copy_mem() - copy memory
2641 * @destination: destination of the copy operation
2642 * @source: source of the copy operation
2643 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002644 *
2645 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002646 *
Mario Six8fac2912018-07-10 08:40:17 +02002647 * See the Unified Extensible Firmware Interface (UEFI) specification for
2648 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002649 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002650static void EFIAPI efi_copy_mem(void *destination, const void *source,
2651 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002652{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002653 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002654 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002655 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002656}
2657
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002658/**
Mario Six8fac2912018-07-10 08:40:17 +02002659 * efi_set_mem() - Fill memory with a byte value.
2660 * @buffer: buffer to fill
2661 * @size: size of buffer in bytes
2662 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002663 *
2664 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002665 *
Mario Six8fac2912018-07-10 08:40:17 +02002666 * See the Unified Extensible Firmware Interface (UEFI) specification for
2667 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002668 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002669static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002670{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002671 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002672 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002673 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002674}
2675
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002676/**
Mario Six8fac2912018-07-10 08:40:17 +02002677 * efi_protocol_open() - open protocol interface on a handle
2678 * @handler: handler of a protocol
2679 * @protocol_interface: interface implementing the protocol
2680 * @agent_handle: handle of the driver
2681 * @controller_handle: handle of the controller
2682 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002683 *
Mario Six8fac2912018-07-10 08:40:17 +02002684 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002685 */
2686static efi_status_t efi_protocol_open(
2687 struct efi_handler *handler,
2688 void **protocol_interface, void *agent_handle,
2689 void *controller_handle, uint32_t attributes)
2690{
2691 struct efi_open_protocol_info_item *item;
2692 struct efi_open_protocol_info_entry *match = NULL;
2693 bool opened_by_driver = false;
2694 bool opened_exclusive = false;
2695
2696 /* If there is no agent, only return the interface */
2697 if (!agent_handle)
2698 goto out;
2699
2700 /* For TEST_PROTOCOL ignore interface attribute */
2701 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2702 *protocol_interface = NULL;
2703
2704 /*
2705 * Check if the protocol is already opened by a driver with the same
2706 * attributes or opened exclusively
2707 */
2708 list_for_each_entry(item, &handler->open_infos, link) {
2709 if (item->info.agent_handle == agent_handle) {
2710 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2711 (item->info.attributes == attributes))
2712 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002713 } else {
2714 if (item->info.attributes &
2715 EFI_OPEN_PROTOCOL_BY_DRIVER)
2716 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002717 }
2718 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2719 opened_exclusive = true;
2720 }
2721
2722 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002723 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2724 if (opened_exclusive)
2725 return EFI_ACCESS_DENIED;
2726 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2727 if (opened_exclusive || opened_by_driver)
2728 return EFI_ACCESS_DENIED;
2729 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002730
2731 /* Prepare exclusive opening */
2732 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2733 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002734disconnect_next:
2735 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002736 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002737 efi_status_t ret;
2738
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002739 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002740 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2741 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002742 item->info.controller_handle,
2743 item->info.agent_handle,
2744 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002745 if (ret == EFI_SUCCESS)
2746 /*
2747 * Child controllers may have been
2748 * removed from the open_infos list. So
2749 * let's restart the loop.
2750 */
2751 goto disconnect_next;
2752 else
2753 opened_by_driver = true;
2754 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002755 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002756 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002757 if (opened_by_driver)
2758 return EFI_ACCESS_DENIED;
2759 }
2760
2761 /* Find existing entry */
2762 list_for_each_entry(item, &handler->open_infos, link) {
2763 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002764 item->info.controller_handle == controller_handle &&
2765 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002766 match = &item->info;
2767 }
2768 /* None found, create one */
2769 if (!match) {
2770 match = efi_create_open_info(handler);
2771 if (!match)
2772 return EFI_OUT_OF_RESOURCES;
2773 }
2774
2775 match->agent_handle = agent_handle;
2776 match->controller_handle = controller_handle;
2777 match->attributes = attributes;
2778 match->open_count++;
2779
2780out:
2781 /* For TEST_PROTOCOL ignore interface attribute. */
2782 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2783 *protocol_interface = handler->protocol_interface;
2784
2785 return EFI_SUCCESS;
2786}
2787
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002788/**
Mario Six8fac2912018-07-10 08:40:17 +02002789 * efi_open_protocol() - open protocol interface on a handle
2790 * @handle: handle on which the protocol shall be opened
2791 * @protocol: GUID of the protocol
2792 * @protocol_interface: interface implementing the protocol
2793 * @agent_handle: handle of the driver
2794 * @controller_handle: handle of the controller
2795 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002796 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002797 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002798 *
Mario Six8fac2912018-07-10 08:40:17 +02002799 * See the Unified Extensible Firmware Interface (UEFI) specification for
2800 * details.
2801 *
2802 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002803 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002804static efi_status_t EFIAPI efi_open_protocol
2805 (efi_handle_t handle, const efi_guid_t *protocol,
2806 void **protocol_interface, efi_handle_t agent_handle,
2807 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002808{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002809 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002810 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002811
Rob Clark238f88c2017-09-13 18:05:41 -04002812 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002813 protocol_interface, agent_handle, controller_handle,
2814 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002815
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002816 if (!handle || !protocol ||
2817 (!protocol_interface && attributes !=
2818 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002819 goto out;
2820 }
2821
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002822 switch (attributes) {
2823 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2824 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2825 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2826 break;
2827 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2828 if (controller_handle == handle)
2829 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002830 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002831 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2832 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002833 /* Check that the controller handle is valid */
2834 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002835 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002836 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002837 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002838 /* Check that the agent handle is valid */
2839 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002840 goto out;
2841 break;
2842 default:
2843 goto out;
2844 }
2845
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002846 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002847 switch (r) {
2848 case EFI_SUCCESS:
2849 break;
2850 case EFI_NOT_FOUND:
2851 r = EFI_UNSUPPORTED;
2852 goto out;
2853 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002854 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002855 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002856
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002857 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2858 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002859out:
2860 return EFI_EXIT(r);
2861}
2862
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002863/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002864 * efi_start_image() - call the entry point of an image
2865 * @image_handle: handle of the image
2866 * @exit_data_size: size of the buffer
2867 * @exit_data: buffer to receive the exit data of the called image
2868 *
2869 * This function implements the StartImage service.
2870 *
2871 * See the Unified Extensible Firmware Interface (UEFI) specification for
2872 * details.
2873 *
2874 * Return: status code
2875 */
2876efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2877 efi_uintn_t *exit_data_size,
2878 u16 **exit_data)
2879{
2880 struct efi_loaded_image_obj *image_obj =
2881 (struct efi_loaded_image_obj *)image_handle;
2882 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002883 void *info;
2884 efi_handle_t parent_image = current_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002885
2886 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2887
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002888 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002889 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2890 return EFI_EXIT(EFI_INVALID_PARAMETER);
2891
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002892 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2893 &info, NULL, NULL,
2894 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2895 if (ret != EFI_SUCCESS)
2896 return EFI_EXIT(EFI_INVALID_PARAMETER);
2897
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002898 image_obj->exit_data_size = exit_data_size;
2899 image_obj->exit_data = exit_data;
2900
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002901 /* call the image! */
2902 if (setjmp(&image_obj->exit_jmp)) {
2903 /*
2904 * We called the entry point of the child image with EFI_CALL
2905 * in the lines below. The child image called the Exit() boot
2906 * service efi_exit() which executed the long jump that brought
2907 * us to the current line. This implies that the second half
2908 * of the EFI_CALL macro has not been executed.
2909 */
2910#ifdef CONFIG_ARM
2911 /*
2912 * efi_exit() called efi_restore_gd(). We have to undo this
2913 * otherwise __efi_entry_check() will put the wrong value into
2914 * app_gd.
2915 */
2916 gd = app_gd;
2917#endif
2918 /*
2919 * To get ready to call EFI_EXIT below we have to execute the
2920 * missed out steps of EFI_CALL.
2921 */
2922 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02002923 EFI_PRINT("%lu returned by started image\n",
2924 (unsigned long)((uintptr_t)image_obj->exit_status &
2925 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002926 current_image = parent_image;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002927 return EFI_EXIT(image_obj->exit_status);
2928 }
2929
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002930 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02002931 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09002932 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002933 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
2934
2935 /*
2936 * Usually UEFI applications call Exit() instead of returning.
2937 * But because the world doesn't consist of ponies and unicorns,
2938 * we're happy to emulate that behavior on behalf of a payload
2939 * that forgot.
2940 */
2941 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
2942}
2943
2944/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002945 * efi_delete_image() - delete loaded image from memory)
2946 *
2947 * @image_obj: handle of the loaded image
2948 * @loaded_image_protocol: loaded image protocol
2949 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002950static efi_status_t efi_delete_image
2951 (struct efi_loaded_image_obj *image_obj,
2952 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002953{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002954 struct efi_object *efiobj;
2955 efi_status_t r, ret = EFI_SUCCESS;
2956
2957close_next:
2958 list_for_each_entry(efiobj, &efi_obj_list, link) {
2959 struct efi_handler *protocol;
2960
2961 list_for_each_entry(protocol, &efiobj->protocols, link) {
2962 struct efi_open_protocol_info_item *info;
2963
2964 list_for_each_entry(info, &protocol->open_infos, link) {
2965 if (info->info.agent_handle !=
2966 (efi_handle_t)image_obj)
2967 continue;
2968 r = EFI_CALL(efi_close_protocol
2969 (efiobj, protocol->guid,
2970 info->info.agent_handle,
2971 info->info.controller_handle
2972 ));
2973 if (r != EFI_SUCCESS)
2974 ret = r;
2975 /*
2976 * Closing protocols may results in further
2977 * items being deleted. To play it safe loop
2978 * over all elements again.
2979 */
2980 goto close_next;
2981 }
2982 }
2983 }
2984
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002985 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
2986 efi_size_in_pages(loaded_image_protocol->image_size));
2987 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02002988
2989 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02002990}
2991
2992/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02002993 * efi_unload_image() - unload an EFI image
2994 * @image_handle: handle of the image to be unloaded
2995 *
2996 * This function implements the UnloadImage service.
2997 *
2998 * See the Unified Extensible Firmware Interface (UEFI) specification for
2999 * details.
3000 *
3001 * Return: status code
3002 */
3003efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3004{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003005 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003006 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003007 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003008
3009 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003010
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003011 efiobj = efi_search_obj(image_handle);
3012 if (!efiobj) {
3013 ret = EFI_INVALID_PARAMETER;
3014 goto out;
3015 }
3016 /* Find the loaded image protocol */
3017 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3018 (void **)&loaded_image_protocol,
3019 NULL, NULL,
3020 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3021 if (ret != EFI_SUCCESS) {
3022 ret = EFI_INVALID_PARAMETER;
3023 goto out;
3024 }
3025 switch (efiobj->type) {
3026 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3027 /* Call the unload function */
3028 if (!loaded_image_protocol->unload) {
3029 ret = EFI_UNSUPPORTED;
3030 goto out;
3031 }
3032 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3033 if (ret != EFI_SUCCESS)
3034 goto out;
3035 break;
3036 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3037 break;
3038 default:
3039 ret = EFI_INVALID_PARAMETER;
3040 goto out;
3041 }
3042 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3043 loaded_image_protocol);
3044out:
3045 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003046}
3047
3048/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003049 * efi_update_exit_data() - fill exit data parameters of StartImage()
3050 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003051 * @image_obj: image handle
3052 * @exit_data_size: size of the exit data buffer
3053 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003054 * Return: status code
3055 */
3056static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3057 efi_uintn_t exit_data_size,
3058 u16 *exit_data)
3059{
3060 efi_status_t ret;
3061
3062 /*
3063 * If exit_data is not provided to StartImage(), exit_data_size must be
3064 * ignored.
3065 */
3066 if (!image_obj->exit_data)
3067 return EFI_SUCCESS;
3068 if (image_obj->exit_data_size)
3069 *image_obj->exit_data_size = exit_data_size;
3070 if (exit_data_size && exit_data) {
3071 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3072 exit_data_size,
3073 (void **)image_obj->exit_data);
3074 if (ret != EFI_SUCCESS)
3075 return ret;
3076 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3077 } else {
3078 image_obj->exit_data = NULL;
3079 }
3080 return EFI_SUCCESS;
3081}
3082
3083/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003084 * efi_exit() - leave an EFI application or driver
3085 * @image_handle: handle of the application or driver that is exiting
3086 * @exit_status: status code
3087 * @exit_data_size: size of the buffer in bytes
3088 * @exit_data: buffer with data describing an error
3089 *
3090 * This function implements the Exit service.
3091 *
3092 * See the Unified Extensible Firmware Interface (UEFI) specification for
3093 * details.
3094 *
3095 * Return: status code
3096 */
3097static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3098 efi_status_t exit_status,
3099 efi_uintn_t exit_data_size,
3100 u16 *exit_data)
3101{
3102 /*
3103 * TODO: We should call the unload procedure of the loaded
3104 * image protocol.
3105 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003106 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003107 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003108 struct efi_loaded_image_obj *image_obj =
3109 (struct efi_loaded_image_obj *)image_handle;
3110
3111 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3112 exit_data_size, exit_data);
3113
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003114 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003115 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003116 (void **)&loaded_image_protocol,
3117 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003118 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003119 if (ret != EFI_SUCCESS) {
3120 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003121 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003122 }
3123
3124 /* Unloading of unstarted images */
3125 switch (image_obj->header.type) {
3126 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3127 break;
3128 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3129 efi_delete_image(image_obj, loaded_image_protocol);
3130 ret = EFI_SUCCESS;
3131 goto out;
3132 default:
3133 /* Handle does not refer to loaded image */
3134 ret = EFI_INVALID_PARAMETER;
3135 goto out;
3136 }
3137 /* A started image can only be unloaded it is the last one started. */
3138 if (image_handle != current_image) {
3139 ret = EFI_INVALID_PARAMETER;
3140 goto out;
3141 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003142
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003143 /* Exit data is only foreseen in case of failure. */
3144 if (exit_status != EFI_SUCCESS) {
3145 ret = efi_update_exit_data(image_obj, exit_data_size,
3146 exit_data);
3147 /* Exiting has priority. Don't return error to caller. */
3148 if (ret != EFI_SUCCESS)
3149 EFI_PRINT("%s: out of memory\n", __func__);
3150 }
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003151 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3152 exit_status != EFI_SUCCESS)
3153 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003154
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003155 /* Make sure entry/exit counts for EFI world cross-overs match */
3156 EFI_EXIT(exit_status);
3157
3158 /*
3159 * But longjmp out with the U-Boot gd, not the application's, as
3160 * the other end is a setjmp call inside EFI context.
3161 */
3162 efi_restore_gd();
3163
3164 image_obj->exit_status = exit_status;
3165 longjmp(&image_obj->exit_jmp, 1);
3166
3167 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003168out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003169 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003170}
3171
3172/**
Mario Six8fac2912018-07-10 08:40:17 +02003173 * efi_handle_protocol() - get interface of a protocol on a handle
3174 * @handle: handle on which the protocol shall be opened
3175 * @protocol: GUID of the protocol
3176 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003177 *
3178 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003179 *
3180 * See the Unified Extensible Firmware Interface (UEFI) specification for
3181 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003182 *
Mario Six8fac2912018-07-10 08:40:17 +02003183 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003184 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01003185static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02003186 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01003187 void **protocol_interface)
3188{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003189 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003190 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003191}
3192
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003193/**
Mario Six8fac2912018-07-10 08:40:17 +02003194 * efi_bind_controller() - bind a single driver to a controller
3195 * @controller_handle: controller handle
3196 * @driver_image_handle: driver handle
3197 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003198 *
Mario Six8fac2912018-07-10 08:40:17 +02003199 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003200 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003201static efi_status_t efi_bind_controller(
3202 efi_handle_t controller_handle,
3203 efi_handle_t driver_image_handle,
3204 struct efi_device_path *remain_device_path)
3205{
3206 struct efi_driver_binding_protocol *binding_protocol;
3207 efi_status_t r;
3208
3209 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3210 &efi_guid_driver_binding_protocol,
3211 (void **)&binding_protocol,
3212 driver_image_handle, NULL,
3213 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3214 if (r != EFI_SUCCESS)
3215 return r;
3216 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3217 controller_handle,
3218 remain_device_path));
3219 if (r == EFI_SUCCESS)
3220 r = EFI_CALL(binding_protocol->start(binding_protocol,
3221 controller_handle,
3222 remain_device_path));
3223 EFI_CALL(efi_close_protocol(driver_image_handle,
3224 &efi_guid_driver_binding_protocol,
3225 driver_image_handle, NULL));
3226 return r;
3227}
3228
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003229/**
Mario Six8fac2912018-07-10 08:40:17 +02003230 * efi_connect_single_controller() - connect a single driver to a controller
3231 * @controller_handle: controller
3232 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003233 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003234 *
Mario Six8fac2912018-07-10 08:40:17 +02003235 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003236 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003237static efi_status_t efi_connect_single_controller(
3238 efi_handle_t controller_handle,
3239 efi_handle_t *driver_image_handle,
3240 struct efi_device_path *remain_device_path)
3241{
3242 efi_handle_t *buffer;
3243 size_t count;
3244 size_t i;
3245 efi_status_t r;
3246 size_t connected = 0;
3247
3248 /* Get buffer with all handles with driver binding protocol */
3249 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3250 &efi_guid_driver_binding_protocol,
3251 NULL, &count, &buffer));
3252 if (r != EFI_SUCCESS)
3253 return r;
3254
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003255 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003256 if (driver_image_handle) {
3257 for (; *driver_image_handle; ++driver_image_handle) {
3258 for (i = 0; i < count; ++i) {
3259 if (buffer[i] == *driver_image_handle) {
3260 buffer[i] = NULL;
3261 r = efi_bind_controller(
3262 controller_handle,
3263 *driver_image_handle,
3264 remain_device_path);
3265 /*
3266 * For drivers that do not support the
3267 * controller or are already connected
3268 * we receive an error code here.
3269 */
3270 if (r == EFI_SUCCESS)
3271 ++connected;
3272 }
3273 }
3274 }
3275 }
3276
3277 /*
3278 * TODO: Some overrides are not yet implemented:
3279 * - Platform Driver Override
3280 * - Driver Family Override Search
3281 * - Bus Specific Driver Override
3282 */
3283
3284 /* Driver Binding Search */
3285 for (i = 0; i < count; ++i) {
3286 if (buffer[i]) {
3287 r = efi_bind_controller(controller_handle,
3288 buffer[i],
3289 remain_device_path);
3290 if (r == EFI_SUCCESS)
3291 ++connected;
3292 }
3293 }
3294
3295 efi_free_pool(buffer);
3296 if (!connected)
3297 return EFI_NOT_FOUND;
3298 return EFI_SUCCESS;
3299}
3300
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003301/**
Mario Six8fac2912018-07-10 08:40:17 +02003302 * efi_connect_controller() - connect a controller to a driver
3303 * @controller_handle: handle of the controller
3304 * @driver_image_handle: handle of the driver
3305 * @remain_device_path: device path of a child controller
3306 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003307 *
3308 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003309 *
3310 * See the Unified Extensible Firmware Interface (UEFI) specification for
3311 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003312 *
3313 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003314 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003315 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3316 *
Mario Six8fac2912018-07-10 08:40:17 +02003317 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003318 */
3319static efi_status_t EFIAPI efi_connect_controller(
3320 efi_handle_t controller_handle,
3321 efi_handle_t *driver_image_handle,
3322 struct efi_device_path *remain_device_path,
3323 bool recursive)
3324{
3325 efi_status_t r;
3326 efi_status_t ret = EFI_NOT_FOUND;
3327 struct efi_object *efiobj;
3328
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003329 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003330 remain_device_path, recursive);
3331
3332 efiobj = efi_search_obj(controller_handle);
3333 if (!efiobj) {
3334 ret = EFI_INVALID_PARAMETER;
3335 goto out;
3336 }
3337
3338 r = efi_connect_single_controller(controller_handle,
3339 driver_image_handle,
3340 remain_device_path);
3341 if (r == EFI_SUCCESS)
3342 ret = EFI_SUCCESS;
3343 if (recursive) {
3344 struct efi_handler *handler;
3345 struct efi_open_protocol_info_item *item;
3346
3347 list_for_each_entry(handler, &efiobj->protocols, link) {
3348 list_for_each_entry(item, &handler->open_infos, link) {
3349 if (item->info.attributes &
3350 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3351 r = EFI_CALL(efi_connect_controller(
3352 item->info.controller_handle,
3353 driver_image_handle,
3354 remain_device_path,
3355 recursive));
3356 if (r == EFI_SUCCESS)
3357 ret = EFI_SUCCESS;
3358 }
3359 }
3360 }
3361 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003362 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003363 if (ret != EFI_SUCCESS && remain_device_path &&
3364 remain_device_path->type == DEVICE_PATH_TYPE_END)
3365 ret = EFI_SUCCESS;
3366out:
3367 return EFI_EXIT(ret);
3368}
3369
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003370/**
Mario Six8fac2912018-07-10 08:40:17 +02003371 * efi_reinstall_protocol_interface() - reinstall protocol interface
3372 * @handle: handle on which the protocol shall be reinstalled
3373 * @protocol: GUID of the protocol to be installed
3374 * @old_interface: interface to be removed
3375 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003376 *
3377 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003378 *
3379 * See the Unified Extensible Firmware Interface (UEFI) specification for
3380 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003381 *
3382 * The old interface is uninstalled. The new interface is installed.
3383 * Drivers are connected.
3384 *
Mario Six8fac2912018-07-10 08:40:17 +02003385 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003386 */
3387static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3388 efi_handle_t handle, const efi_guid_t *protocol,
3389 void *old_interface, void *new_interface)
3390{
3391 efi_status_t ret;
3392
3393 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3394 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003395
3396 /* Uninstall protocol but do not delete handle */
3397 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003398 if (ret != EFI_SUCCESS)
3399 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003400
3401 /* Install the new protocol */
3402 ret = efi_add_protocol(handle, protocol, new_interface);
3403 /*
3404 * The UEFI spec does not specify what should happen to the handle
3405 * if in case of an error no protocol interface remains on the handle.
3406 * So let's do nothing here.
3407 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003408 if (ret != EFI_SUCCESS)
3409 goto out;
3410 /*
3411 * The returned status code has to be ignored.
3412 * Do not create an error if no suitable driver for the handle exists.
3413 */
3414 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3415out:
3416 return EFI_EXIT(ret);
3417}
3418
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003419/**
Mario Six8fac2912018-07-10 08:40:17 +02003420 * efi_get_child_controllers() - get all child controllers associated to a driver
3421 * @efiobj: handle of the controller
3422 * @driver_handle: handle of the driver
3423 * @number_of_children: number of child controllers
3424 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003425 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003426 * The allocated buffer has to be freed with free().
3427 *
Mario Six8fac2912018-07-10 08:40:17 +02003428 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003429 */
3430static efi_status_t efi_get_child_controllers(
3431 struct efi_object *efiobj,
3432 efi_handle_t driver_handle,
3433 efi_uintn_t *number_of_children,
3434 efi_handle_t **child_handle_buffer)
3435{
3436 struct efi_handler *handler;
3437 struct efi_open_protocol_info_item *item;
3438 efi_uintn_t count = 0, i;
3439 bool duplicate;
3440
3441 /* Count all child controller associations */
3442 list_for_each_entry(handler, &efiobj->protocols, link) {
3443 list_for_each_entry(item, &handler->open_infos, link) {
3444 if (item->info.agent_handle == driver_handle &&
3445 item->info.attributes &
3446 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3447 ++count;
3448 }
3449 }
3450 /*
3451 * Create buffer. In case of duplicate child controller assignments
3452 * the buffer will be too large. But that does not harm.
3453 */
3454 *number_of_children = 0;
3455 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3456 if (!*child_handle_buffer)
3457 return EFI_OUT_OF_RESOURCES;
3458 /* Copy unique child handles */
3459 list_for_each_entry(handler, &efiobj->protocols, link) {
3460 list_for_each_entry(item, &handler->open_infos, link) {
3461 if (item->info.agent_handle == driver_handle &&
3462 item->info.attributes &
3463 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3464 /* Check this is a new child controller */
3465 duplicate = false;
3466 for (i = 0; i < *number_of_children; ++i) {
3467 if ((*child_handle_buffer)[i] ==
3468 item->info.controller_handle)
3469 duplicate = true;
3470 }
3471 /* Copy handle to buffer */
3472 if (!duplicate) {
3473 i = (*number_of_children)++;
3474 (*child_handle_buffer)[i] =
3475 item->info.controller_handle;
3476 }
3477 }
3478 }
3479 }
3480 return EFI_SUCCESS;
3481}
3482
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003483/**
Mario Six8fac2912018-07-10 08:40:17 +02003484 * efi_disconnect_controller() - disconnect a controller from a driver
3485 * @controller_handle: handle of the controller
3486 * @driver_image_handle: handle of the driver
3487 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003488 *
3489 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003490 *
3491 * See the Unified Extensible Firmware Interface (UEFI) specification for
3492 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003493 *
Mario Six8fac2912018-07-10 08:40:17 +02003494 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003495 */
3496static efi_status_t EFIAPI efi_disconnect_controller(
3497 efi_handle_t controller_handle,
3498 efi_handle_t driver_image_handle,
3499 efi_handle_t child_handle)
3500{
3501 struct efi_driver_binding_protocol *binding_protocol;
3502 efi_handle_t *child_handle_buffer = NULL;
3503 size_t number_of_children = 0;
3504 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003505 struct efi_object *efiobj;
3506
3507 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3508 child_handle);
3509
3510 efiobj = efi_search_obj(controller_handle);
3511 if (!efiobj) {
3512 r = EFI_INVALID_PARAMETER;
3513 goto out;
3514 }
3515
3516 if (child_handle && !efi_search_obj(child_handle)) {
3517 r = EFI_INVALID_PARAMETER;
3518 goto out;
3519 }
3520
3521 /* If no driver handle is supplied, disconnect all drivers */
3522 if (!driver_image_handle) {
3523 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3524 goto out;
3525 }
3526
3527 /* Create list of child handles */
3528 if (child_handle) {
3529 number_of_children = 1;
3530 child_handle_buffer = &child_handle;
3531 } else {
3532 efi_get_child_controllers(efiobj,
3533 driver_image_handle,
3534 &number_of_children,
3535 &child_handle_buffer);
3536 }
3537
3538 /* Get the driver binding protocol */
3539 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3540 &efi_guid_driver_binding_protocol,
3541 (void **)&binding_protocol,
3542 driver_image_handle, NULL,
3543 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003544 if (r != EFI_SUCCESS) {
3545 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003546 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003547 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003548 /* Remove the children */
3549 if (number_of_children) {
3550 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3551 controller_handle,
3552 number_of_children,
3553 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003554 if (r != EFI_SUCCESS) {
3555 r = EFI_DEVICE_ERROR;
3556 goto out;
3557 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003558 }
3559 /* Remove the driver */
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003560 if (!child_handle) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003561 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3562 controller_handle,
3563 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003564 if (r != EFI_SUCCESS) {
3565 r = EFI_DEVICE_ERROR;
3566 goto out;
3567 }
3568 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003569 EFI_CALL(efi_close_protocol(driver_image_handle,
3570 &efi_guid_driver_binding_protocol,
3571 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003572 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003573out:
3574 if (!child_handle)
3575 free(child_handle_buffer);
3576 return EFI_EXIT(r);
3577}
3578
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003579static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003580 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003581 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3582 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003583 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003584 },
3585 .raise_tpl = efi_raise_tpl,
3586 .restore_tpl = efi_restore_tpl,
3587 .allocate_pages = efi_allocate_pages_ext,
3588 .free_pages = efi_free_pages_ext,
3589 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003590 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003591 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003592 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003593 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003594 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003595 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003596 .close_event = efi_close_event,
3597 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003598 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003599 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003600 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003601 .handle_protocol = efi_handle_protocol,
3602 .reserved = NULL,
3603 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003604 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003605 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003606 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003607 .load_image = efi_load_image,
3608 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003609 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003610 .unload_image = efi_unload_image,
3611 .exit_boot_services = efi_exit_boot_services,
3612 .get_next_monotonic_count = efi_get_next_monotonic_count,
3613 .stall = efi_stall,
3614 .set_watchdog_timer = efi_set_watchdog_timer,
3615 .connect_controller = efi_connect_controller,
3616 .disconnect_controller = efi_disconnect_controller,
3617 .open_protocol = efi_open_protocol,
3618 .close_protocol = efi_close_protocol,
3619 .open_protocol_information = efi_open_protocol_information,
3620 .protocols_per_handle = efi_protocols_per_handle,
3621 .locate_handle_buffer = efi_locate_handle_buffer,
3622 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003623 .install_multiple_protocol_interfaces =
3624 efi_install_multiple_protocol_interfaces,
3625 .uninstall_multiple_protocol_interfaces =
3626 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003627 .calculate_crc32 = efi_calculate_crc32,
3628 .copy_mem = efi_copy_mem,
3629 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003630 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003631};
3632
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003633static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003634
Alexander Graf393dd912016-10-14 13:45:30 +02003635struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003636 .hdr = {
3637 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003638 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003639 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003640 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003641 .fw_vendor = firmware_vendor,
3642 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003643 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003644 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003645 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003646};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003647
3648/**
3649 * efi_initialize_system_table() - Initialize system table
3650 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003651 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003652 */
3653efi_status_t efi_initialize_system_table(void)
3654{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003655 efi_status_t ret;
3656
3657 /* Allocate configuration table array */
3658 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3659 EFI_MAX_CONFIGURATION_TABLES *
3660 sizeof(struct efi_configuration_table),
3661 (void **)&systab.tables);
3662
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003663 /*
3664 * These entries will be set to NULL in ExitBootServices(). To avoid
3665 * relocation in SetVirtualAddressMap(), set them dynamically.
3666 */
3667 systab.con_in = &efi_con_in;
3668 systab.con_out = &efi_con_out;
3669 systab.std_err = &efi_con_out;
3670 systab.boottime = &efi_boot_services;
3671
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003672 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003673 efi_update_table_header_crc32(&systab.hdr);
3674 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3675 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003676
3677 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003678}