blob: 4777b35fd4face59fe3439434a7f0f14a33ec0f5 [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>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +03009#include <bootm.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +020010#include <div64.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030011#include <dm/device.h>
12#include <dm/root.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010013#include <efi_loader.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070014#include <irq_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <malloc.h>
Heinrich Schuchardt37587522019-05-01 20:07:04 +020017#include <pe.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030018#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070019#include <u-boot/crc.h>
Ilias Apalodimasc539fb82020-10-22 01:04:21 +030020#include <usb.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010021#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Ilias Apalodimas4cc06322020-10-22 01:04:20 +030023#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010024
25DECLARE_GLOBAL_DATA_PTR;
26
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020027/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010028static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020029
Alexander Grafc15d9212016-03-04 01:09:59 +010030/* This list contains all the EFI objects our payload has access to */
31LIST_HEAD(efi_obj_list);
32
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010033/* List of all events */
Heinrich Schuchardt4429d872019-07-11 20:15:09 +020034__efi_runtime_data LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010035
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +020036/* List of queued events */
37LIST_HEAD(efi_event_queue);
38
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +020039/* Flag to disable timer activity in ExitBootServices() */
40static bool timers_enabled = true;
41
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +010042/* Flag used by the selftest to avoid detaching devices in ExitBootServices() */
43bool efi_st_keep_devices;
44
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +020045/* List of all events registered by RegisterProtocolNotify() */
46LIST_HEAD(efi_register_notify_events);
47
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +010048/* Handle of the currently executing image */
49static efi_handle_t current_image;
50
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020051#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +010052/*
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020053 * The "gd" pointer lives in a register on ARM and RISC-V that we declare
Alexander Grafc15d9212016-03-04 01:09:59 +010054 * fixed when compiling U-Boot. However, the payload does not know about that
55 * restriction so we need to manually swap its and our view of that register on
56 * EFI callback entry/exit.
57 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +020058static volatile gd_t *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060059#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010060
Heinrich Schuchardt72506232019-02-09 14:10:39 +010061/* 1 if inside U-Boot code, 0 if inside EFI payload code */
62static int entry_count = 1;
Rob Clarke7896c32017-07-27 08:04:19 -040063static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010064/* GUID of the device tree table */
65const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010066/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
67const efi_guid_t efi_guid_driver_binding_protocol =
68 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040069
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010070/* event group ExitBootServices() invoked */
71const efi_guid_t efi_guid_event_group_exit_boot_services =
72 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
73/* event group SetVirtualAddressMap() invoked */
74const efi_guid_t efi_guid_event_group_virtual_address_change =
75 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
76/* event group memory map changed */
77const efi_guid_t efi_guid_event_group_memory_map_change =
78 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
79/* event group boot manager about to boot */
80const efi_guid_t efi_guid_event_group_ready_to_boot =
81 EFI_EVENT_GROUP_READY_TO_BOOT;
82/* event group ResetSystem() invoked (before ExitBootServices) */
83const efi_guid_t efi_guid_event_group_reset_system =
84 EFI_EVENT_GROUP_RESET_SYSTEM;
Heinrich Schuchardt485956d2020-12-04 03:33:41 +010085/* GUIDs of the Load File and Load File2 protocols */
86const efi_guid_t efi_guid_load_file_protocol = EFI_LOAD_FILE_PROTOCOL_GUID;
87const efi_guid_t efi_guid_load_file2_protocol = EFI_LOAD_FILE2_PROTOCOL_GUID;
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010088
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010089static efi_status_t EFIAPI efi_disconnect_controller(
90 efi_handle_t controller_handle,
91 efi_handle_t driver_image_handle,
92 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010093
Rob Clark86789d52017-07-27 08:04:18 -040094/* Called on every callback entry */
95int __efi_entry_check(void)
96{
97 int ret = entry_count++ == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +020098#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Rob Clark86789d52017-07-27 08:04:18 -040099 assert(efi_gd);
100 app_gd = gd;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200101 set_gd(efi_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400102#endif
103 return ret;
104}
105
106/* Called on every callback exit */
107int __efi_exit_check(void)
108{
109 int ret = --entry_count == 0;
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200110#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200111 set_gd(app_gd);
Rob Clark86789d52017-07-27 08:04:18 -0400112#endif
113 return ret;
114}
115
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200116/**
117 * efi_save_gd() - save global data register
118 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200119 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200120 * As this register may be overwritten by an EFI payload we save it here
121 * and restore it on every callback entered.
122 *
123 * This function is called after relocation from initr_reloc_global_data().
124 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100125void efi_save_gd(void)
126{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200127#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100128 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600129#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100130}
131
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200132/**
133 * efi_restore_gd() - restore global data register
134 *
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200135 * On the ARM and RISC-V architectures gd is mapped to a fixed register.
Heinrich Schuchardtc5949412020-07-18 09:53:01 +0200136 * Restore it after returning from the UEFI world to the value saved via
137 * efi_save_gd().
Rob Clark86789d52017-07-27 08:04:18 -0400138 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100139void efi_restore_gd(void)
140{
Heinrich Schuchardtf277d942020-09-10 12:22:54 +0200141#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Alexander Grafc15d9212016-03-04 01:09:59 +0100142 /* Only restore if we're already in EFI context */
143 if (!efi_gd)
144 return;
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +0200145 set_gd(efi_gd);
Simon Glasscdfe6962016-09-25 15:27:35 -0600146#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100147}
148
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200149/**
Mario Six8fac2912018-07-10 08:40:17 +0200150 * indent_string() - returns a string for indenting with two spaces per level
151 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100152 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200153 * A maximum of ten indent levels is supported. Higher indent levels will be
154 * truncated.
155 *
Mario Six8fac2912018-07-10 08:40:17 +0200156 * Return: A string for indenting with two spaces per level is
157 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400158 */
159static const char *indent_string(int level)
160{
161 const char *indent = " ";
162 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100163
Rob Clarke7896c32017-07-27 08:04:19 -0400164 level = min(max, level * 2);
165 return &indent[max - level];
166}
167
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200168const char *__efi_nesting(void)
169{
170 return indent_string(nesting_level);
171}
172
Rob Clarke7896c32017-07-27 08:04:19 -0400173const char *__efi_nesting_inc(void)
174{
175 return indent_string(nesting_level++);
176}
177
178const char *__efi_nesting_dec(void)
179{
180 return indent_string(--nesting_level);
181}
182
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200183/**
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200184 * efi_event_is_queued() - check if an event is queued
185 *
186 * @event: event
187 * Return: true if event is queued
188 */
189static bool efi_event_is_queued(struct efi_event *event)
190{
191 return !!event->queue_link.next;
192}
193
194/**
195 * efi_process_event_queue() - process event queue
196 */
197static void efi_process_event_queue(void)
198{
199 while (!list_empty(&efi_event_queue)) {
200 struct efi_event *event;
201 efi_uintn_t old_tpl;
202
203 event = list_first_entry(&efi_event_queue, struct efi_event,
204 queue_link);
205 if (efi_tpl >= event->notify_tpl)
206 return;
207 list_del(&event->queue_link);
208 event->queue_link.next = NULL;
209 event->queue_link.prev = NULL;
210 /* Events must be executed at the event's TPL */
211 old_tpl = efi_tpl;
212 efi_tpl = event->notify_tpl;
213 EFI_CALL_VOID(event->notify_function(event,
214 event->notify_context));
215 efi_tpl = old_tpl;
216 if (event->type == EVT_NOTIFY_SIGNAL)
217 event->is_signaled = 0;
218 }
219}
220
221/**
Mario Six8fac2912018-07-10 08:40:17 +0200222 * efi_queue_event() - queue an EFI event
223 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200224 *
225 * This function queues the notification function of the event for future
226 * execution.
227 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200228 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200229static void efi_queue_event(struct efi_event *event)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200230{
Heinrich Schuchardtec946902020-03-06 21:56:10 +0100231 struct efi_event *item;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200232
233 if (!event->notify_function)
234 return;
235
236 if (!efi_event_is_queued(event)) {
237 /*
238 * Events must be notified in order of decreasing task priority
239 * level. Insert the new event accordingly.
240 */
241 list_for_each_entry(item, &efi_event_queue, queue_link) {
242 if (item->notify_tpl < event->notify_tpl) {
243 list_add_tail(&event->queue_link,
244 &item->queue_link);
245 event = NULL;
246 break;
247 }
248 }
249 if (event)
250 list_add_tail(&event->queue_link, &efi_event_queue);
Heinrich Schuchardt3ef4c852020-12-28 00:25:34 +0100251 efi_process_event_queue();
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200252 }
253}
254
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200255/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200256 * is_valid_tpl() - check if the task priority level is valid
257 *
258 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200259 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200260 */
261efi_status_t is_valid_tpl(efi_uintn_t tpl)
262{
263 switch (tpl) {
264 case TPL_APPLICATION:
265 case TPL_CALLBACK:
266 case TPL_NOTIFY:
267 case TPL_HIGH_LEVEL:
268 return EFI_SUCCESS;
269 default:
270 return EFI_INVALID_PARAMETER;
271 }
272}
273
274/**
Mario Six8fac2912018-07-10 08:40:17 +0200275 * efi_signal_event() - signal an EFI event
276 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100277 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100278 * This function signals an event. If the event belongs to an event group, all
279 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100280 * their notification function is queued.
281 *
282 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100283 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200284void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100285{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200286 if (event->is_signaled)
287 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100288 if (event->group) {
289 struct efi_event *evt;
290
291 /*
292 * The signaled state has to set before executing any
293 * notification function
294 */
295 list_for_each_entry(evt, &efi_events, link) {
296 if (!evt->group || guidcmp(evt->group, event->group))
297 continue;
298 if (evt->is_signaled)
299 continue;
300 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100301 }
302 list_for_each_entry(evt, &efi_events, link) {
303 if (!evt->group || guidcmp(evt->group, event->group))
304 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200305 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100306 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200307 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100308 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200309 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100310 }
311}
312
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200313/**
Mario Six8fac2912018-07-10 08:40:17 +0200314 * efi_raise_tpl() - raise the task priority level
315 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200316 *
317 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200318 *
319 * See the Unified Extensible Firmware Interface (UEFI) specification for
320 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200321 *
Mario Six8fac2912018-07-10 08:40:17 +0200322 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200323 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100324static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100325{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100326 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200327
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200328 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200329
330 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200331 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200332 efi_tpl = new_tpl;
333 if (efi_tpl > TPL_HIGH_LEVEL)
334 efi_tpl = TPL_HIGH_LEVEL;
335
336 EFI_EXIT(EFI_SUCCESS);
337 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100338}
339
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200340/**
Mario Six8fac2912018-07-10 08:40:17 +0200341 * efi_restore_tpl() - lower the task priority level
342 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200343 *
344 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200345 *
Mario Six8fac2912018-07-10 08:40:17 +0200346 * See the Unified Extensible Firmware Interface (UEFI) specification for
347 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200348 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100349static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100350{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200351 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200352
353 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200354 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200355 efi_tpl = old_tpl;
356 if (efi_tpl > TPL_HIGH_LEVEL)
357 efi_tpl = TPL_HIGH_LEVEL;
358
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100359 /*
360 * Lowering the TPL may have made queued events eligible for execution.
361 */
362 efi_timer_check();
363
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200364 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100365}
366
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200367/**
Mario Six8fac2912018-07-10 08:40:17 +0200368 * efi_allocate_pages_ext() - allocate memory pages
369 * @type: type of allocation to be performed
370 * @memory_type: usage type of the allocated memory
371 * @pages: number of pages to be allocated
372 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200373 *
374 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200375 *
Mario Six8fac2912018-07-10 08:40:17 +0200376 * See the Unified Extensible Firmware Interface (UEFI) specification for
377 * details.
378 *
379 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200380 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900381static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100382 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900383 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100384{
385 efi_status_t r;
386
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100387 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100388 r = efi_allocate_pages(type, memory_type, pages, memory);
389 return EFI_EXIT(r);
390}
391
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200392/**
Mario Six8fac2912018-07-10 08:40:17 +0200393 * efi_free_pages_ext() - Free memory pages.
394 * @memory: start of the memory area to be freed
395 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 *
397 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200398 *
399 * See the Unified Extensible Firmware Interface (UEFI) specification for
400 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200401 *
Mario Six8fac2912018-07-10 08:40:17 +0200402 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200403 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900404static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100405 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100406{
407 efi_status_t r;
408
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900409 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100410 r = efi_free_pages(memory, pages);
411 return EFI_EXIT(r);
412}
413
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200414/**
Mario Six8fac2912018-07-10 08:40:17 +0200415 * efi_get_memory_map_ext() - get map describing memory usage
416 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
417 * on exit the size of the copied memory map
418 * @memory_map: buffer to which the memory map is written
419 * @map_key: key for the memory map
420 * @descriptor_size: size of an individual memory descriptor
421 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200422 *
423 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200424 *
Mario Six8fac2912018-07-10 08:40:17 +0200425 * See the Unified Extensible Firmware Interface (UEFI) specification for
426 * details.
427 *
428 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200429 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900430static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100431 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900432 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100433 efi_uintn_t *map_key,
434 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900435 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100436{
437 efi_status_t r;
438
439 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
440 map_key, descriptor_size, descriptor_version);
441 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
442 descriptor_size, descriptor_version);
443 return EFI_EXIT(r);
444}
445
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200446/**
Mario Six8fac2912018-07-10 08:40:17 +0200447 * efi_allocate_pool_ext() - allocate memory from pool
448 * @pool_type: type of the pool from which memory is to be allocated
449 * @size: number of bytes to be allocated
450 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200451 *
452 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200453 *
454 * See the Unified Extensible Firmware Interface (UEFI) specification for
455 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200456 *
Mario Six8fac2912018-07-10 08:40:17 +0200457 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200458 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200459static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100460 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200461 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100462{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100463 efi_status_t r;
464
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100465 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200466 r = efi_allocate_pool(pool_type, size, 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/**
Mario Six8fac2912018-07-10 08:40:17 +0200471 * efi_free_pool_ext() - free memory from pool
472 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200473 *
474 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200475 *
Mario Six8fac2912018-07-10 08:40:17 +0200476 * See the Unified Extensible Firmware Interface (UEFI) specification for
477 * details.
478 *
479 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200480 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200481static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100482{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100483 efi_status_t r;
484
485 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200486 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100487 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100488}
489
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200490/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200491 * efi_add_handle() - add a new handle to the object list
492 *
493 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100494 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200495 * The protocols list is initialized. The handle is added to the list of known
496 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100497 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200498void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100499{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200500 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100501 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200502 INIT_LIST_HEAD(&handle->protocols);
503 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100504}
505
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200506/**
Mario Six8fac2912018-07-10 08:40:17 +0200507 * efi_create_handle() - create handle
508 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200509 *
Mario Six8fac2912018-07-10 08:40:17 +0200510 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200511 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100512efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200513{
514 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200515
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200516 obj = calloc(1, sizeof(struct efi_object));
517 if (!obj)
518 return EFI_OUT_OF_RESOURCES;
519
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100520 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200521 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200522
523 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200524}
525
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200526/**
Mario Six8fac2912018-07-10 08:40:17 +0200527 * efi_search_protocol() - find a protocol on a handle.
528 * @handle: handle
529 * @protocol_guid: GUID of the protocol
530 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100531 *
Mario Six8fac2912018-07-10 08:40:17 +0200532 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100533 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100534efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100535 const efi_guid_t *protocol_guid,
536 struct efi_handler **handler)
537{
538 struct efi_object *efiobj;
539 struct list_head *lhandle;
540
541 if (!handle || !protocol_guid)
542 return EFI_INVALID_PARAMETER;
543 efiobj = efi_search_obj(handle);
544 if (!efiobj)
545 return EFI_INVALID_PARAMETER;
546 list_for_each(lhandle, &efiobj->protocols) {
547 struct efi_handler *protocol;
548
549 protocol = list_entry(lhandle, struct efi_handler, link);
550 if (!guidcmp(protocol->guid, protocol_guid)) {
551 if (handler)
552 *handler = protocol;
553 return EFI_SUCCESS;
554 }
555 }
556 return EFI_NOT_FOUND;
557}
558
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200559/**
Mario Six8fac2912018-07-10 08:40:17 +0200560 * efi_remove_protocol() - delete protocol from a handle
561 * @handle: handle from which the protocol shall be deleted
562 * @protocol: GUID of the protocol to be deleted
563 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100564 *
Mario Six8fac2912018-07-10 08:40:17 +0200565 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100566 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100567efi_status_t efi_remove_protocol(const efi_handle_t handle,
568 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100569 void *protocol_interface)
570{
571 struct efi_handler *handler;
572 efi_status_t ret;
573
574 ret = efi_search_protocol(handle, protocol, &handler);
575 if (ret != EFI_SUCCESS)
576 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200577 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200578 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100579 list_del(&handler->link);
580 free(handler);
581 return EFI_SUCCESS;
582}
583
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200584/**
Mario Six8fac2912018-07-10 08:40:17 +0200585 * efi_remove_all_protocols() - delete all protocols from a handle
586 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100587 *
Mario Six8fac2912018-07-10 08:40:17 +0200588 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100589 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100590efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100591{
592 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100593 struct efi_handler *protocol;
594 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100595
596 efiobj = efi_search_obj(handle);
597 if (!efiobj)
598 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100599 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100600 efi_status_t ret;
601
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100602 ret = efi_remove_protocol(handle, protocol->guid,
603 protocol->protocol_interface);
604 if (ret != EFI_SUCCESS)
605 return ret;
606 }
607 return EFI_SUCCESS;
608}
609
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200610/**
Mario Six8fac2912018-07-10 08:40:17 +0200611 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100612 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200613 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100614 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200615void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100616{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200617 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100618 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200619 efi_remove_all_protocols(handle);
620 list_del(&handle->link);
621 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100622}
623
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200624/**
Mario Six8fac2912018-07-10 08:40:17 +0200625 * efi_is_event() - check if a pointer is a valid event
626 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100627 *
Mario Six8fac2912018-07-10 08:40:17 +0200628 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100629 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100630static efi_status_t efi_is_event(const struct efi_event *event)
631{
632 const struct efi_event *evt;
633
634 if (!event)
635 return EFI_INVALID_PARAMETER;
636 list_for_each_entry(evt, &efi_events, link) {
637 if (evt == event)
638 return EFI_SUCCESS;
639 }
640 return EFI_INVALID_PARAMETER;
641}
Alexander Grafc15d9212016-03-04 01:09:59 +0100642
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200643/**
Mario Six8fac2912018-07-10 08:40:17 +0200644 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200645 *
Mario Six8fac2912018-07-10 08:40:17 +0200646 * @type: type of the event to create
647 * @notify_tpl: task priority level of the event
648 * @notify_function: notification function of the event
649 * @notify_context: pointer passed to the notification function
650 * @group: event group
651 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200652 *
653 * This function is used inside U-Boot code to create an event.
654 *
655 * For the API function implementing the CreateEvent service see
656 * efi_create_event_ext.
657 *
Mario Six8fac2912018-07-10 08:40:17 +0200658 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200659 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100660efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200661 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200662 struct efi_event *event,
663 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100664 void *notify_context, efi_guid_t *group,
665 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100666{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100667 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200668 efi_status_t ret;
669 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200670
Jonathan Gray7758b212017-03-12 19:26:07 +1100671 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200672 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100673
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200674 switch (type) {
675 case 0:
676 case EVT_TIMER:
677 case EVT_NOTIFY_SIGNAL:
678 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
679 case EVT_NOTIFY_WAIT:
680 case EVT_TIMER | EVT_NOTIFY_WAIT:
681 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200682 pool_type = EFI_BOOT_SERVICES_DATA;
683 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200684 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200685 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200686 break;
687 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200688 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200689 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100690
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100691 /*
692 * The UEFI specification requires event notification levels to be
693 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
694 *
695 * Parameter NotifyTpl should not be checked if it is not used.
696 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900697 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100698 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
699 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200700 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100701
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200702 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
703 (void **)&evt);
704 if (ret != EFI_SUCCESS)
705 return ret;
706 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100707 evt->type = type;
708 evt->notify_tpl = notify_tpl;
709 evt->notify_function = notify_function;
710 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100711 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200712 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100713 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100714 list_add_tail(&evt->link, &efi_events);
715 *event = evt;
716 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100717}
718
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200719/*
Mario Six8fac2912018-07-10 08:40:17 +0200720 * efi_create_event_ex() - create an event in a group
721 * @type: type of the event to create
722 * @notify_tpl: task priority level of the event
723 * @notify_function: notification function of the event
724 * @notify_context: pointer passed to the notification function
725 * @event: created event
726 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100727 *
728 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100729 *
Mario Six8fac2912018-07-10 08:40:17 +0200730 * See the Unified Extensible Firmware Interface (UEFI) specification for
731 * details.
732 *
733 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100734 */
735efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
736 void (EFIAPI *notify_function) (
737 struct efi_event *event,
738 void *context),
739 void *notify_context,
740 efi_guid_t *event_group,
741 struct efi_event **event)
742{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200743 efi_status_t ret;
744
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100745 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
746 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200747
748 /*
749 * The allowable input parameters are the same as in CreateEvent()
750 * except for the following two disallowed event types.
751 */
752 switch (type) {
753 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
754 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
755 ret = EFI_INVALID_PARAMETER;
756 goto out;
757 }
758
759 ret = efi_create_event(type, notify_tpl, notify_function,
760 notify_context, event_group, event);
761out:
762 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100763}
764
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200765/**
Mario Six8fac2912018-07-10 08:40:17 +0200766 * efi_create_event_ext() - create an event
767 * @type: type of the event to create
768 * @notify_tpl: task priority level of the event
769 * @notify_function: notification function of the event
770 * @notify_context: pointer passed to the notification function
771 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200772 *
773 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200774 *
775 * See the Unified Extensible Firmware Interface (UEFI) specification for
776 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200777 *
Mario Six8fac2912018-07-10 08:40:17 +0200778 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200779 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200780static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100781 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200782 void (EFIAPI *notify_function) (
783 struct efi_event *event,
784 void *context),
785 void *notify_context, struct efi_event **event)
786{
787 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
788 notify_context);
789 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100790 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200791}
792
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200793/**
Mario Six8fac2912018-07-10 08:40:17 +0200794 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200795 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200796 * Check if a timer event has occurred or a queued notification function should
797 * be called.
798 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100799 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200800 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100801 */
802void efi_timer_check(void)
803{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100804 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100805 u64 now = timer_get_us();
806
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100807 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200808 if (!timers_enabled)
809 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100810 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200811 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100812 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200813 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100814 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200815 break;
816 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100817 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200818 break;
819 default:
820 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200821 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100822 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200823 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100824 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200825 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100826 WATCHDOG_RESET();
827}
828
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200829/**
Mario Six8fac2912018-07-10 08:40:17 +0200830 * efi_set_timer() - set the trigger time for a timer event or stop the event
831 * @event: event for which the timer is set
832 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200833 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200834 *
835 * This is the function for internal usage in U-Boot. For the API function
836 * implementing the SetTimer service see efi_set_timer_ext.
837 *
Mario Six8fac2912018-07-10 08:40:17 +0200838 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200839 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200840efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200841 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100842{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100843 /* Check that the event is valid */
844 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
845 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100846
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200847 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200848 * The parameter defines a multiple of 100 ns.
849 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200850 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200851 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100852
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100853 switch (type) {
854 case EFI_TIMER_STOP:
855 event->trigger_next = -1ULL;
856 break;
857 case EFI_TIMER_PERIODIC:
858 case EFI_TIMER_RELATIVE:
859 event->trigger_next = timer_get_us() + trigger_time;
860 break;
861 default:
862 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100863 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100864 event->trigger_type = type;
865 event->trigger_time = trigger_time;
866 event->is_signaled = false;
867 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100868}
869
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200870/**
Mario Six8fac2912018-07-10 08:40:17 +0200871 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
872 * event
873 * @event: event for which the timer is set
874 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200875 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200876 *
877 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200878 *
Mario Six8fac2912018-07-10 08:40:17 +0200879 * See the Unified Extensible Firmware Interface (UEFI) specification for
880 * details.
881 *
882 *
883 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200884 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200885static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
886 enum efi_timer_delay type,
887 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200888{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900889 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200890 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
891}
892
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200893/**
Mario Six8fac2912018-07-10 08:40:17 +0200894 * efi_wait_for_event() - wait for events to be signaled
895 * @num_events: number of events to be waited for
896 * @event: events to be waited for
897 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200898 *
899 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200900 *
Mario Six8fac2912018-07-10 08:40:17 +0200901 * See the Unified Extensible Firmware Interface (UEFI) specification for
902 * details.
903 *
904 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200905 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100906static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200907 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100908 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100909{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100910 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100911
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100912 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100913
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200914 /* Check parameters */
915 if (!num_events || !event)
916 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200917 /* Check TPL */
918 if (efi_tpl != TPL_APPLICATION)
919 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200920 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100921 if (efi_is_event(event[i]) != EFI_SUCCESS)
922 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200923 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
924 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200925 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200926 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200927 }
928
929 /* Wait for signal */
930 for (;;) {
931 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200932 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200933 goto out;
934 }
935 /* Allow events to occur. */
936 efi_timer_check();
937 }
938
939out:
940 /*
941 * Reset the signal which is passed to the caller to allow periodic
942 * events to occur.
943 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200944 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200945 if (index)
946 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100947
948 return EFI_EXIT(EFI_SUCCESS);
949}
950
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200951/**
Mario Six8fac2912018-07-10 08:40:17 +0200952 * efi_signal_event_ext() - signal an EFI event
953 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200954 *
955 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200956 *
957 * See the Unified Extensible Firmware Interface (UEFI) specification for
958 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200959 *
960 * This functions sets the signaled state of the event and queues the
961 * notification function for execution.
962 *
Mario Six8fac2912018-07-10 08:40:17 +0200963 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200964 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200965static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100966{
967 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100968 if (efi_is_event(event) != EFI_SUCCESS)
969 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200970 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100971 return EFI_EXIT(EFI_SUCCESS);
972}
973
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200974/**
Mario Six8fac2912018-07-10 08:40:17 +0200975 * efi_close_event() - close an EFI event
976 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200977 *
978 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200979 *
Mario Six8fac2912018-07-10 08:40:17 +0200980 * See the Unified Extensible Firmware Interface (UEFI) specification for
981 * details.
982 *
983 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200984 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200985static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100986{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200987 struct efi_register_notify_event *item, *next;
988
Alexander Grafc15d9212016-03-04 01:09:59 +0100989 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100990 if (efi_is_event(event) != EFI_SUCCESS)
991 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200992
993 /* Remove protocol notify registrations for the event */
994 list_for_each_entry_safe(item, next, &efi_register_notify_events,
995 link) {
996 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200997 struct efi_protocol_notification *hitem, *hnext;
998
999 /* Remove signaled handles */
1000 list_for_each_entry_safe(hitem, hnext, &item->handles,
1001 link) {
1002 list_del(&hitem->link);
1003 free(hitem);
1004 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001005 list_del(&item->link);
1006 free(item);
1007 }
1008 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001009 /* Remove event from queue */
1010 if (efi_event_is_queued(event))
1011 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001012
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001013 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001014 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001015 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001016}
1017
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001018/**
Mario Six8fac2912018-07-10 08:40:17 +02001019 * efi_check_event() - check if an event is signaled
1020 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001021 *
1022 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001023 *
1024 * See the Unified Extensible Firmware Interface (UEFI) specification for
1025 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001026 *
Mario Six8fac2912018-07-10 08:40:17 +02001027 * If an event is not signaled yet, the notification function is queued. The
1028 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001029 *
Mario Six8fac2912018-07-10 08:40:17 +02001030 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001031 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001032static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001033{
1034 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001035 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001036 if (efi_is_event(event) != EFI_SUCCESS ||
1037 event->type & EVT_NOTIFY_SIGNAL)
1038 return EFI_EXIT(EFI_INVALID_PARAMETER);
1039 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001040 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001041 if (event->is_signaled) {
1042 event->is_signaled = false;
1043 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001044 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001045 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001046}
1047
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001048/**
Mario Six8fac2912018-07-10 08:40:17 +02001049 * efi_search_obj() - find the internal EFI object for a handle
1050 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001051 *
Mario Six8fac2912018-07-10 08:40:17 +02001052 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001053 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001054struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001055{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001056 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001057
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001058 if (!handle)
1059 return NULL;
1060
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001061 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001062 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001063 return efiobj;
1064 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001065 return NULL;
1066}
1067
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001068/**
Mario Six8fac2912018-07-10 08:40:17 +02001069 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1070 * to a protocol
1071 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001072 *
Mario Six8fac2912018-07-10 08:40:17 +02001073 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001074 */
1075static struct efi_open_protocol_info_entry *efi_create_open_info(
1076 struct efi_handler *handler)
1077{
1078 struct efi_open_protocol_info_item *item;
1079
1080 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1081 if (!item)
1082 return NULL;
1083 /* Append the item to the open protocol info list. */
1084 list_add_tail(&item->link, &handler->open_infos);
1085
1086 return &item->info;
1087}
1088
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001089/**
Mario Six8fac2912018-07-10 08:40:17 +02001090 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1091 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001092 *
Mario Six8fac2912018-07-10 08:40:17 +02001093 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001094 */
1095static efi_status_t efi_delete_open_info(
1096 struct efi_open_protocol_info_item *item)
1097{
1098 list_del(&item->link);
1099 free(item);
1100 return EFI_SUCCESS;
1101}
1102
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001103/**
Mario Six8fac2912018-07-10 08:40:17 +02001104 * efi_add_protocol() - install new protocol on a handle
1105 * @handle: handle on which the protocol shall be installed
1106 * @protocol: GUID of the protocol to be installed
1107 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001108 *
Mario Six8fac2912018-07-10 08:40:17 +02001109 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001110 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001111efi_status_t efi_add_protocol(const efi_handle_t handle,
1112 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001113 void *protocol_interface)
1114{
1115 struct efi_object *efiobj;
1116 struct efi_handler *handler;
1117 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001118 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001119
1120 efiobj = efi_search_obj(handle);
1121 if (!efiobj)
1122 return EFI_INVALID_PARAMETER;
1123 ret = efi_search_protocol(handle, protocol, NULL);
1124 if (ret != EFI_NOT_FOUND)
1125 return EFI_INVALID_PARAMETER;
1126 handler = calloc(1, sizeof(struct efi_handler));
1127 if (!handler)
1128 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001129 handler->guid = protocol;
1130 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001131 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001132 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001133
1134 /* Notify registered events */
1135 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001136 if (!guidcmp(protocol, &event->protocol)) {
1137 struct efi_protocol_notification *notif;
1138
1139 notif = calloc(1, sizeof(*notif));
1140 if (!notif) {
1141 list_del(&handler->link);
1142 free(handler);
1143 return EFI_OUT_OF_RESOURCES;
1144 }
1145 notif->handle = handle;
1146 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001147 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001148 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001149 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001150 }
1151
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001152 if (!guidcmp(&efi_guid_device_path, protocol))
1153 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001154 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001155}
1156
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001157/**
Mario Six8fac2912018-07-10 08:40:17 +02001158 * efi_install_protocol_interface() - install protocol interface
1159 * @handle: handle on which the protocol shall be installed
1160 * @protocol: GUID of the protocol to be installed
1161 * @protocol_interface_type: type of the interface to be installed,
1162 * always EFI_NATIVE_INTERFACE
1163 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001164 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001165 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001166 *
Mario Six8fac2912018-07-10 08:40:17 +02001167 * See the Unified Extensible Firmware Interface (UEFI) specification for
1168 * details.
1169 *
1170 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001171 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001172static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001173 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001174 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001175{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001176 efi_status_t r;
1177
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001178 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1179 protocol_interface);
1180
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001181 if (!handle || !protocol ||
1182 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1183 r = EFI_INVALID_PARAMETER;
1184 goto out;
1185 }
1186
1187 /* Create new handle if requested. */
1188 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001189 r = efi_create_handle(handle);
1190 if (r != EFI_SUCCESS)
1191 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001192 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001193 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001194 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001195 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001196 /* Add new protocol */
1197 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001198out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001199 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001200}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001201
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001202/**
Mario Six8fac2912018-07-10 08:40:17 +02001203 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001204 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001205 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001206 * @number_of_drivers: number of child controllers
1207 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001208 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001209 * The allocated buffer has to be freed with free().
1210 *
Mario Six8fac2912018-07-10 08:40:17 +02001211 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001212 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001213static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001214 const efi_guid_t *protocol,
1215 efi_uintn_t *number_of_drivers,
1216 efi_handle_t **driver_handle_buffer)
1217{
1218 struct efi_handler *handler;
1219 struct efi_open_protocol_info_item *item;
1220 efi_uintn_t count = 0, i;
1221 bool duplicate;
1222
1223 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001224 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001225 if (protocol && guidcmp(handler->guid, protocol))
1226 continue;
1227 list_for_each_entry(item, &handler->open_infos, link) {
1228 if (item->info.attributes &
1229 EFI_OPEN_PROTOCOL_BY_DRIVER)
1230 ++count;
1231 }
1232 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001233 *number_of_drivers = 0;
1234 if (!count) {
1235 *driver_handle_buffer = NULL;
1236 return EFI_SUCCESS;
1237 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001238 /*
1239 * Create buffer. In case of duplicate driver assignments the buffer
1240 * will be too large. But that does not harm.
1241 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001242 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1243 if (!*driver_handle_buffer)
1244 return EFI_OUT_OF_RESOURCES;
1245 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001246 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001247 if (protocol && guidcmp(handler->guid, protocol))
1248 continue;
1249 list_for_each_entry(item, &handler->open_infos, link) {
1250 if (item->info.attributes &
1251 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1252 /* Check this is a new driver */
1253 duplicate = false;
1254 for (i = 0; i < *number_of_drivers; ++i) {
1255 if ((*driver_handle_buffer)[i] ==
1256 item->info.agent_handle)
1257 duplicate = true;
1258 }
1259 /* Copy handle to buffer */
1260 if (!duplicate) {
1261 i = (*number_of_drivers)++;
1262 (*driver_handle_buffer)[i] =
1263 item->info.agent_handle;
1264 }
1265 }
1266 }
1267 }
1268 return EFI_SUCCESS;
1269}
1270
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001271/**
Mario Six8fac2912018-07-10 08:40:17 +02001272 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001273 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001274 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001275 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001276 *
1277 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001278 *
1279 * See the Unified Extensible Firmware Interface (UEFI) specification for
1280 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001281 *
Mario Six8fac2912018-07-10 08:40:17 +02001282 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001283 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001284static efi_status_t efi_disconnect_all_drivers
1285 (efi_handle_t handle,
1286 const efi_guid_t *protocol,
1287 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001288{
1289 efi_uintn_t number_of_drivers;
1290 efi_handle_t *driver_handle_buffer;
1291 efi_status_t r, ret;
1292
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001293 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001294 &driver_handle_buffer);
1295 if (ret != EFI_SUCCESS)
1296 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001297 if (!number_of_drivers)
1298 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001299 ret = EFI_NOT_FOUND;
1300 while (number_of_drivers) {
1301 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001302 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001303 driver_handle_buffer[--number_of_drivers],
1304 child_handle));
1305 if (r == EFI_SUCCESS)
1306 ret = r;
1307 }
1308 free(driver_handle_buffer);
1309 return ret;
1310}
1311
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001312/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001313 * efi_uninstall_protocol() - uninstall protocol interface
1314 *
Mario Six8fac2912018-07-10 08:40:17 +02001315 * @handle: handle from which the protocol shall be removed
1316 * @protocol: GUID of the protocol to be removed
1317 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001318 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001319 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001320 *
1321 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001322 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001323static efi_status_t efi_uninstall_protocol
1324 (efi_handle_t handle, const efi_guid_t *protocol,
1325 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001326{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001327 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001328 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001329 struct efi_open_protocol_info_item *item;
1330 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001331 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001332
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001333 /* Check handle */
1334 efiobj = efi_search_obj(handle);
1335 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001336 r = EFI_INVALID_PARAMETER;
1337 goto out;
1338 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001339 /* Find the protocol on the handle */
1340 r = efi_search_protocol(handle, protocol, &handler);
1341 if (r != EFI_SUCCESS)
1342 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001343 /* Disconnect controllers */
1344 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001345 /* Close protocol */
1346 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1347 if (item->info.attributes ==
1348 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1349 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1350 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1351 list_del(&item->link);
1352 }
1353 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001354 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001355 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001356 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001357 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001358out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001359 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001360}
1361
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001362/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001363 * efi_uninstall_protocol_interface() - uninstall protocol interface
1364 * @handle: handle from which the protocol shall be removed
1365 * @protocol: GUID of the protocol to be removed
1366 * @protocol_interface: interface to be removed
1367 *
1368 * This function implements the UninstallProtocolInterface service.
1369 *
1370 * See the Unified Extensible Firmware Interface (UEFI) specification for
1371 * details.
1372 *
1373 * Return: status code
1374 */
1375static efi_status_t EFIAPI efi_uninstall_protocol_interface
1376 (efi_handle_t handle, const efi_guid_t *protocol,
1377 void *protocol_interface)
1378{
1379 efi_status_t ret;
1380
1381 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1382
1383 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1384 if (ret != EFI_SUCCESS)
1385 goto out;
1386
1387 /* If the last protocol has been removed, delete the handle. */
1388 if (list_empty(&handle->protocols)) {
1389 list_del(&handle->link);
1390 free(handle);
1391 }
1392out:
1393 return EFI_EXIT(ret);
1394}
1395
1396/**
Mario Six8fac2912018-07-10 08:40:17 +02001397 * efi_register_protocol_notify() - register an event for notification when a
1398 * protocol is installed.
1399 * @protocol: GUID of the protocol whose installation shall be notified
1400 * @event: event to be signaled upon installation of the protocol
1401 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001402 *
1403 * This function implements the RegisterProtocolNotify service.
1404 * See the Unified Extensible Firmware Interface (UEFI) specification
1405 * for details.
1406 *
Mario Six8fac2912018-07-10 08:40:17 +02001407 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001408 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001409efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1410 struct efi_event *event,
1411 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001412{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001413 struct efi_register_notify_event *item;
1414 efi_status_t ret = EFI_SUCCESS;
1415
Rob Clark238f88c2017-09-13 18:05:41 -04001416 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001417
1418 if (!protocol || !event || !registration) {
1419 ret = EFI_INVALID_PARAMETER;
1420 goto out;
1421 }
1422
1423 item = calloc(1, sizeof(struct efi_register_notify_event));
1424 if (!item) {
1425 ret = EFI_OUT_OF_RESOURCES;
1426 goto out;
1427 }
1428
1429 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301430 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001431 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001432
1433 list_add_tail(&item->link, &efi_register_notify_events);
1434
1435 *registration = item;
1436out:
1437 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001438}
1439
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001440/**
Mario Six8fac2912018-07-10 08:40:17 +02001441 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001442 *
Mario Six8fac2912018-07-10 08:40:17 +02001443 * @search_type: selection criterion
1444 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001445 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001446 *
1447 * See the documentation of the LocateHandle service in the UEFI specification.
1448 *
Mario Six8fac2912018-07-10 08:40:17 +02001449 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001450 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001451static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001452 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001453{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001454 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001455
1456 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001457 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001458 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001459 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001460 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001461 return (ret != EFI_SUCCESS);
1462 default:
1463 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001464 return -1;
1465 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001466}
1467
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001468/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001469 * efi_check_register_notify_event() - check if registration key is valid
1470 *
1471 * Check that a pointer is a valid registration key as returned by
1472 * RegisterProtocolNotify().
1473 *
1474 * @key: registration key
1475 * Return: valid registration key or NULL
1476 */
1477static struct efi_register_notify_event *efi_check_register_notify_event
1478 (void *key)
1479{
1480 struct efi_register_notify_event *event;
1481
1482 list_for_each_entry(event, &efi_register_notify_events, link) {
1483 if (event == (struct efi_register_notify_event *)key)
1484 return event;
1485 }
1486 return NULL;
1487}
1488
1489/**
Mario Six8fac2912018-07-10 08:40:17 +02001490 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001491 *
1492 * @search_type: selection criterion
1493 * @protocol: GUID of the protocol
1494 * @search_key: registration key
1495 * @buffer_size: size of the buffer to receive the handles in bytes
1496 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001497 *
1498 * This function is meant for U-Boot internal calls. For the API implementation
1499 * of the LocateHandle service see efi_locate_handle_ext.
1500 *
Mario Six8fac2912018-07-10 08:40:17 +02001501 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001502 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001503static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001504 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001505 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001506 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001507{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001508 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001509 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001510 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001511 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001512
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001513 /* Check parameters */
1514 switch (search_type) {
1515 case ALL_HANDLES:
1516 break;
1517 case BY_REGISTER_NOTIFY:
1518 if (!search_key)
1519 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001520 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001521 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001522 if (!event)
1523 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001524 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001525 case BY_PROTOCOL:
1526 if (!protocol)
1527 return EFI_INVALID_PARAMETER;
1528 break;
1529 default:
1530 return EFI_INVALID_PARAMETER;
1531 }
1532
Alexander Grafc15d9212016-03-04 01:09:59 +01001533 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001534 if (search_type == BY_REGISTER_NOTIFY) {
1535 if (list_empty(&event->handles))
1536 return EFI_NOT_FOUND;
1537 handle = list_first_entry(&event->handles,
1538 struct efi_protocol_notification,
1539 link);
1540 efiobj = handle->handle;
1541 size += sizeof(void *);
1542 } else {
1543 list_for_each_entry(efiobj, &efi_obj_list, link) {
1544 if (!efi_search(search_type, protocol, efiobj))
1545 size += sizeof(void *);
1546 }
1547 if (size == 0)
1548 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001549 }
1550
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001551 if (!buffer_size)
1552 return EFI_INVALID_PARAMETER;
1553
Alexander Grafc15d9212016-03-04 01:09:59 +01001554 if (*buffer_size < size) {
1555 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001556 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001557 }
1558
Rob Clarkcdee3372017-08-06 14:10:07 -04001559 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001560
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001561 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001562 if (!buffer)
1563 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001564
Alexander Grafc15d9212016-03-04 01:09:59 +01001565 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001566 if (search_type == BY_REGISTER_NOTIFY) {
1567 *buffer = efiobj;
1568 list_del(&handle->link);
1569 } else {
1570 list_for_each_entry(efiobj, &efi_obj_list, link) {
1571 if (!efi_search(search_type, protocol, efiobj))
1572 *buffer++ = efiobj;
1573 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001574 }
1575
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001576 return EFI_SUCCESS;
1577}
1578
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001579/**
Mario Six8fac2912018-07-10 08:40:17 +02001580 * efi_locate_handle_ext() - locate handles implementing a protocol.
1581 * @search_type: selection criterion
1582 * @protocol: GUID of the protocol
1583 * @search_key: registration key
1584 * @buffer_size: size of the buffer to receive the handles in bytes
1585 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001586 *
1587 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001588 *
Mario Six8fac2912018-07-10 08:40:17 +02001589 * See the Unified Extensible Firmware Interface (UEFI) specification for
1590 * details.
1591 *
1592 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001593 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001594static efi_status_t EFIAPI efi_locate_handle_ext(
1595 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001596 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001597 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001598{
Rob Clark238f88c2017-09-13 18:05:41 -04001599 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001600 buffer_size, buffer);
1601
1602 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1603 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001604}
1605
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001606/**
Mario Six8fac2912018-07-10 08:40:17 +02001607 * efi_remove_configuration_table() - collapses configuration table entries,
1608 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001609 *
Mario Six8fac2912018-07-10 08:40:17 +02001610 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001611 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001612static void efi_remove_configuration_table(int i)
1613{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001614 struct efi_configuration_table *this = &systab.tables[i];
1615 struct efi_configuration_table *next = &systab.tables[i + 1];
1616 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001617
1618 memmove(this, next, (ulong)end - (ulong)next);
1619 systab.nr_tables--;
1620}
1621
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001622/**
Mario Six8fac2912018-07-10 08:40:17 +02001623 * efi_install_configuration_table() - adds, updates, or removes a
1624 * configuration table
1625 * @guid: GUID of the installed table
1626 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001627 *
1628 * This function is used for internal calls. For the API implementation of the
1629 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1630 *
Mario Six8fac2912018-07-10 08:40:17 +02001631 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001632 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001633efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1634 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001635{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001636 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001637 int i;
1638
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001639 if (!guid)
1640 return EFI_INVALID_PARAMETER;
1641
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001642 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001643 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001644 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001645 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001646 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001647 else
1648 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001649 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001650 }
1651 }
1652
Alexander Graffe3366f2017-07-26 13:41:04 +02001653 if (!table)
1654 return EFI_NOT_FOUND;
1655
Alexander Grafc15d9212016-03-04 01:09:59 +01001656 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001657 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001658 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001659
1660 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301661 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001662 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001663 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001664
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001665out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001666 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001667 efi_update_table_header_crc32(&systab.hdr);
1668
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001669 /* Notify that the configuration table was changed */
1670 list_for_each_entry(evt, &efi_events, link) {
1671 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001672 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001673 break;
1674 }
1675 }
1676
Alexander Grafc5c11632016-08-19 01:23:24 +02001677 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001678}
1679
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001680/**
Mario Six8fac2912018-07-10 08:40:17 +02001681 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1682 * configuration table.
1683 * @guid: GUID of the installed table
1684 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001685 *
1686 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001687 *
Mario Six8fac2912018-07-10 08:40:17 +02001688 * See the Unified Extensible Firmware Interface (UEFI) specification for
1689 * details.
1690 *
1691 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001692 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001693static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1694 void *table)
1695{
Rob Clark238f88c2017-09-13 18:05:41 -04001696 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001697 return EFI_EXIT(efi_install_configuration_table(guid, table));
1698}
1699
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001700/**
Mario Six8fac2912018-07-10 08:40:17 +02001701 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001702 *
1703 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001704 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001705 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001706 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001707 * code is returned.
1708 *
1709 * @device_path: device path of the loaded image
1710 * @file_path: file path of the loaded image
1711 * @handle_ptr: handle of the loaded image
1712 * @info_ptr: loaded image protocol
1713 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001714 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001715efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1716 struct efi_device_path *file_path,
1717 struct efi_loaded_image_obj **handle_ptr,
1718 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001719{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001720 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001721 struct efi_loaded_image *info = NULL;
1722 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001723 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001724
1725 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1726 *handle_ptr = NULL;
1727 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001728
1729 info = calloc(1, sizeof(*info));
1730 if (!info)
1731 return EFI_OUT_OF_RESOURCES;
1732 obj = calloc(1, sizeof(*obj));
1733 if (!obj) {
1734 free(info);
1735 return EFI_OUT_OF_RESOURCES;
1736 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001737 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001738
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001739 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001740 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001741
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001742 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001743 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001744 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001745
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001746 if (device_path) {
1747 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001748
1749 dp = efi_dp_append(device_path, file_path);
1750 if (!dp) {
1751 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001752 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001753 }
1754 } else {
1755 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001756 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001757 ret = efi_add_protocol(&obj->header,
1758 &efi_guid_loaded_image_device_path, dp);
1759 if (ret != EFI_SUCCESS)
1760 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001761
1762 /*
1763 * When asking for the loaded_image interface, just
1764 * return handle which points to loaded_image_info
1765 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001766 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001767 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001768 if (ret != EFI_SUCCESS)
1769 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001770
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001771 *info_ptr = info;
1772 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001773
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001774 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001775failure:
1776 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001777 efi_delete_handle(&obj->header);
1778 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001779 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001780}
1781
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001782/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001783 * efi_locate_device_path() - Get the device path and handle of an device
1784 * implementing a protocol
1785 * @protocol: GUID of the protocol
1786 * @device_path: device path
1787 * @device: handle of the device
1788 *
1789 * This function implements the LocateDevicePath service.
1790 *
1791 * See the Unified Extensible Firmware Interface (UEFI) specification for
1792 * details.
1793 *
1794 * Return: status code
1795 */
1796static efi_status_t EFIAPI efi_locate_device_path(
1797 const efi_guid_t *protocol,
1798 struct efi_device_path **device_path,
1799 efi_handle_t *device)
1800{
1801 struct efi_device_path *dp;
1802 size_t i;
1803 struct efi_handler *handler;
1804 efi_handle_t *handles;
1805 size_t len, len_dp;
1806 size_t len_best = 0;
1807 efi_uintn_t no_handles;
1808 u8 *remainder;
1809 efi_status_t ret;
1810
1811 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1812
1813 if (!protocol || !device_path || !*device_path) {
1814 ret = EFI_INVALID_PARAMETER;
1815 goto out;
1816 }
1817
1818 /* Find end of device path */
1819 len = efi_dp_instance_size(*device_path);
1820
1821 /* Get all handles implementing the protocol */
1822 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1823 &no_handles, &handles));
1824 if (ret != EFI_SUCCESS)
1825 goto out;
1826
1827 for (i = 0; i < no_handles; ++i) {
1828 /* Find the device path protocol */
1829 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1830 &handler);
1831 if (ret != EFI_SUCCESS)
1832 continue;
1833 dp = (struct efi_device_path *)handler->protocol_interface;
1834 len_dp = efi_dp_instance_size(dp);
1835 /*
1836 * This handle can only be a better fit
1837 * if its device path length is longer than the best fit and
1838 * if its device path length is shorter of equal the searched
1839 * device path.
1840 */
1841 if (len_dp <= len_best || len_dp > len)
1842 continue;
1843 /* Check if dp is a subpath of device_path */
1844 if (memcmp(*device_path, dp, len_dp))
1845 continue;
1846 if (!device) {
1847 ret = EFI_INVALID_PARAMETER;
1848 goto out;
1849 }
1850 *device = handles[i];
1851 len_best = len_dp;
1852 }
1853 if (len_best) {
1854 remainder = (u8 *)*device_path + len_best;
1855 *device_path = (struct efi_device_path *)remainder;
1856 ret = EFI_SUCCESS;
1857 } else {
1858 ret = EFI_NOT_FOUND;
1859 }
1860out:
1861 return EFI_EXIT(ret);
1862}
1863
1864/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001865 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001866 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001867 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1868 * callers obligation to update the memory type as needed.
1869 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001870 * @file_path: the path of the image to load
1871 * @buffer: buffer containing the loaded image
1872 * @size: size of the loaded image
1873 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001874 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001875static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001876efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001877 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001878{
Rob Clark857a1222017-09-13 18:05:35 -04001879 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001880 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001881 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001882 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001883
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001884 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001885 f = efi_file_from_path(file_path);
1886 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001887 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001888
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001889 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001890 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001891 goto error;
1892
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001893 /*
1894 * When reading the file we do not yet know if it contains an
1895 * application, a boottime driver, or a runtime driver. So here we
1896 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1897 * update the reservation according to the image type.
1898 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001899 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1900 EFI_BOOT_SERVICES_DATA,
1901 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001902 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001903 ret = EFI_OUT_OF_RESOURCES;
1904 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001905 }
1906
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001907 /* Read file */
1908 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1909 if (ret != EFI_SUCCESS)
1910 efi_free_pages(addr, efi_size_in_pages(bs));
1911 *buffer = (void *)(uintptr_t)addr;
1912 *size = bs;
1913error:
1914 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001915 return ret;
1916}
1917
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001918/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001919 * efi_load_image_from_path() - load an image using a file path
1920 *
1921 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1922 * callers obligation to update the memory type as needed.
1923 *
1924 * @boot_policy: true for request originating from the boot manager
1925 * @file_path: the path of the image to load
1926 * @buffer: buffer containing the loaded image
1927 * @size: size of the loaded image
1928 * Return: status code
1929 */
1930static
1931efi_status_t efi_load_image_from_path(bool boot_policy,
1932 struct efi_device_path *file_path,
1933 void **buffer, efi_uintn_t *size)
1934{
1935 efi_handle_t device;
1936 efi_status_t ret;
1937 struct efi_device_path *dp;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001938 struct efi_load_file_protocol *load_file_protocol = NULL;
1939 efi_uintn_t buffer_size;
1940 uint64_t addr, pages;
1941 const efi_guid_t *guid;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001942
1943 /* In case of failure nothing is returned */
1944 *buffer = NULL;
1945 *size = 0;
1946
1947 dp = file_path;
1948 ret = EFI_CALL(efi_locate_device_path(
1949 &efi_simple_file_system_protocol_guid, &dp, &device));
1950 if (ret == EFI_SUCCESS)
1951 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001952
1953 ret = EFI_CALL(efi_locate_device_path(
1954 &efi_guid_load_file_protocol, &dp, &device));
1955 if (ret == EFI_SUCCESS) {
1956 guid = &efi_guid_load_file_protocol;
1957 } else if (!boot_policy) {
1958 guid = &efi_guid_load_file2_protocol;
1959 ret = EFI_CALL(efi_locate_device_path(guid, &dp, &device));
1960 }
1961 if (ret != EFI_SUCCESS)
1962 return EFI_NOT_FOUND;
1963 ret = EFI_CALL(efi_handle_protocol(device, guid,
1964 (void **)&load_file_protocol));
1965 if (ret != EFI_SUCCESS)
1966 return EFI_NOT_FOUND;
1967 buffer_size = 0;
1968 ret = load_file_protocol->load_file(load_file_protocol, dp,
1969 boot_policy, &buffer_size,
1970 NULL);
1971 if (ret != EFI_BUFFER_TOO_SMALL)
1972 goto out;
1973 pages = efi_size_in_pages(buffer_size);
1974 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1975 pages, &addr);
1976 if (ret != EFI_SUCCESS) {
1977 ret = EFI_OUT_OF_RESOURCES;
1978 goto out;
1979 }
1980 ret = EFI_CALL(load_file_protocol->load_file(
1981 load_file_protocol, dp, boot_policy,
1982 &buffer_size, (void *)(uintptr_t)addr));
1983 if (ret != EFI_SUCCESS)
1984 efi_free_pages(addr, pages);
1985out:
Heinrich Schuchardt20d88c72021-01-20 22:57:46 +01001986 EFI_CALL(efi_close_protocol(device, guid, efi_root, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001987 if (ret == EFI_SUCCESS) {
1988 *buffer = (void *)(uintptr_t)addr;
1989 *size = buffer_size;
1990 }
1991
1992 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001993}
1994
1995/**
Mario Six8fac2912018-07-10 08:40:17 +02001996 * efi_load_image() - load an EFI image into memory
1997 * @boot_policy: true for request originating from the boot manager
1998 * @parent_image: the caller's image handle
1999 * @file_path: the path of the image to load
2000 * @source_buffer: memory location from which the image is installed
2001 * @source_size: size of the memory area from which the image is installed
2002 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002003 *
2004 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002005 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002006 * See the Unified Extensible Firmware Interface (UEFI) specification
2007 * for details.
2008 *
Mario Six8fac2912018-07-10 08:40:17 +02002009 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002010 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002011efi_status_t EFIAPI efi_load_image(bool boot_policy,
2012 efi_handle_t parent_image,
2013 struct efi_device_path *file_path,
2014 void *source_buffer,
2015 efi_uintn_t source_size,
2016 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002017{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002018 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002019 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002020 struct efi_loaded_image_obj **image_obj =
2021 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002022 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002023 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002024
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02002025 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002026 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002027
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002028 if (!image_handle || (!source_buffer && !file_path) ||
2029 !efi_search_obj(parent_image) ||
2030 /* The parent image handle must refer to a loaded image */
2031 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002032 ret = EFI_INVALID_PARAMETER;
2033 goto error;
2034 }
Rob Clark857a1222017-09-13 18:05:35 -04002035
2036 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002037 ret = efi_load_image_from_path(boot_policy, file_path,
2038 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002039 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002040 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002041 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002042 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002043 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002044 /* split file_path which contains both the device and file parts */
2045 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002046 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002047 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002048 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002049 if (!source_buffer)
2050 /* Release buffer to which file was loaded */
2051 efi_free_pages((uintptr_t)dest_buffer,
2052 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002053 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002054 info->system_table = &systab;
2055 info->parent_handle = parent_image;
2056 } else {
2057 /* The image is invalid. Release all associated resources. */
2058 efi_delete_handle(*image_handle);
2059 *image_handle = NULL;
2060 free(info);
2061 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002062error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002063 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002064}
2065
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002066/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002067 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2068 */
2069static void efi_exit_caches(void)
2070{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002071#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002072 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002073 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2074 * caches are enabled.
2075 *
2076 * TODO:
2077 * According to the UEFI spec caches that can be managed via CP15
2078 * operations should be enabled. Caches requiring platform information
2079 * to manage should be disabled. This should not happen in
2080 * ExitBootServices() but before invoking any UEFI binary is invoked.
2081 *
2082 * We want to keep the current workaround while GRUB prior to version
2083 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002084 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002085 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002086#endif
2087}
2088
2089/**
Mario Six8fac2912018-07-10 08:40:17 +02002090 * efi_exit_boot_services() - stop all boot services
2091 * @image_handle: handle of the loaded image
2092 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002093 *
2094 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002095 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002096 * See the Unified Extensible Firmware Interface (UEFI) specification
2097 * for details.
2098 *
Mario Six8fac2912018-07-10 08:40:17 +02002099 * All timer events are disabled. For exit boot services events the
2100 * notification function is called. The boot services are disabled in the
2101 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002102 *
Mario Six8fac2912018-07-10 08:40:17 +02002103 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002104 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002105static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002106 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002107{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002108 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002109 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002110
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002111 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002112
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002113 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002114 if (map_key != efi_memory_map_key) {
2115 ret = EFI_INVALID_PARAMETER;
2116 goto out;
2117 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002118
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002119 /* Check if ExitBootServices has already been called */
2120 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002121 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002122
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002123 /* Stop all timer related activities */
2124 timers_enabled = false;
2125
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002126 /* Add related events to the event group */
2127 list_for_each_entry(evt, &efi_events, link) {
2128 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2129 evt->group = &efi_guid_event_group_exit_boot_services;
2130 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002131 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002132 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002133 if (evt->group &&
2134 !guidcmp(evt->group,
2135 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002136 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002137 break;
2138 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002139 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002140
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002141 /* Make sure that notification functions are not called anymore */
2142 efi_tpl = TPL_HIGH_LEVEL;
2143
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002144 /* Notify variable services */
2145 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002146
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002147 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2148 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2149 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2150 list_del(&evt->link);
2151 }
2152
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002153 if (!efi_st_keep_devices) {
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002154 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002155 udc_disconnect();
2156 board_quiesce_devices();
2157 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2158 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002159
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002160 /* Patch out unsupported runtime function */
2161 efi_runtime_detach();
2162
Alexander Graffa5246b2018-11-15 21:23:47 +01002163 /* Fix up caches for EFI payloads if necessary */
2164 efi_exit_caches();
2165
Alexander Grafc15d9212016-03-04 01:09:59 +01002166 /* This stops all lingering devices */
2167 bootm_disable_interrupts();
2168
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002169 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002170 systab.con_in_handle = NULL;
2171 systab.con_in = NULL;
2172 systab.con_out_handle = NULL;
2173 systab.con_out = NULL;
2174 systab.stderr_handle = NULL;
2175 systab.std_err = NULL;
2176 systab.boottime = NULL;
2177
2178 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002179 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002180
Alexander Grafc15d9212016-03-04 01:09:59 +01002181 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002182 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002183 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002184out:
2185 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002186}
2187
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002188/**
Mario Six8fac2912018-07-10 08:40:17 +02002189 * efi_get_next_monotonic_count() - get next value of the counter
2190 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002191 *
2192 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002193 *
2194 * See the Unified Extensible Firmware Interface (UEFI) specification for
2195 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002196 *
Mario Six8fac2912018-07-10 08:40:17 +02002197 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002198 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002199static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2200{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002201 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002202 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002203
Alexander Grafc15d9212016-03-04 01:09:59 +01002204 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002205 if (!count) {
2206 ret = EFI_INVALID_PARAMETER;
2207 goto out;
2208 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002209 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002210 ret = EFI_SUCCESS;
2211out:
2212 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002213}
2214
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002215/**
Mario Six8fac2912018-07-10 08:40:17 +02002216 * efi_stall() - sleep
2217 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002218 *
Mario Six8fac2912018-07-10 08:40:17 +02002219 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002220 *
Mario Six8fac2912018-07-10 08:40:17 +02002221 * See the Unified Extensible Firmware Interface (UEFI) specification for
2222 * details.
2223 *
2224 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002225 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002226static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2227{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002228 u64 end_tick;
2229
Alexander Grafc15d9212016-03-04 01:09:59 +01002230 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002231
2232 end_tick = get_ticks() + usec_to_tick(microseconds);
2233 while (get_ticks() < end_tick)
2234 efi_timer_check();
2235
Alexander Grafc15d9212016-03-04 01:09:59 +01002236 return EFI_EXIT(EFI_SUCCESS);
2237}
2238
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002239/**
Mario Six8fac2912018-07-10 08:40:17 +02002240 * efi_set_watchdog_timer() - reset the watchdog timer
2241 * @timeout: seconds before reset by watchdog
2242 * @watchdog_code: code to be logged when resetting
2243 * @data_size: size of buffer in bytes
2244 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002245 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002246 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002247 *
Mario Six8fac2912018-07-10 08:40:17 +02002248 * See the Unified Extensible Firmware Interface (UEFI) specification for
2249 * details.
2250 *
2251 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002252 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002253static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2254 uint64_t watchdog_code,
2255 unsigned long data_size,
2256 uint16_t *watchdog_data)
2257{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002258 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002259 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002260 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002261}
2262
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002263/**
Mario Six8fac2912018-07-10 08:40:17 +02002264 * efi_close_protocol() - close a protocol
2265 * @handle: handle on which the protocol shall be closed
2266 * @protocol: GUID of the protocol to close
2267 * @agent_handle: handle of the driver
2268 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002269 *
2270 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002271 *
Mario Six8fac2912018-07-10 08:40:17 +02002272 * See the Unified Extensible Firmware Interface (UEFI) specification for
2273 * details.
2274 *
2275 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002276 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002277efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2278 const efi_guid_t *protocol,
2279 efi_handle_t agent_handle,
2280 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002281{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002282 struct efi_handler *handler;
2283 struct efi_open_protocol_info_item *item;
2284 struct efi_open_protocol_info_item *pos;
2285 efi_status_t r;
2286
Rob Clark238f88c2017-09-13 18:05:41 -04002287 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002288 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002289
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002290 if (!efi_search_obj(agent_handle) ||
2291 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002292 r = EFI_INVALID_PARAMETER;
2293 goto out;
2294 }
2295 r = efi_search_protocol(handle, protocol, &handler);
2296 if (r != EFI_SUCCESS)
2297 goto out;
2298
2299 r = EFI_NOT_FOUND;
2300 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2301 if (item->info.agent_handle == agent_handle &&
2302 item->info.controller_handle == controller_handle) {
2303 efi_delete_open_info(item);
2304 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002305 }
2306 }
2307out:
2308 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002309}
2310
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002311/**
Mario Six8fac2912018-07-10 08:40:17 +02002312 * efi_open_protocol_information() - provide information about then open status
2313 * of a protocol on a handle
2314 * @handle: handle for which the information shall be retrieved
2315 * @protocol: GUID of the protocol
2316 * @entry_buffer: buffer to receive the open protocol information
2317 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002318 *
2319 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002320 *
2321 * See the Unified Extensible Firmware Interface (UEFI) specification for
2322 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002323 *
Mario Six8fac2912018-07-10 08:40:17 +02002324 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002325 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002326static efi_status_t EFIAPI efi_open_protocol_information(
2327 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002328 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002329 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002330{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002331 unsigned long buffer_size;
2332 unsigned long count;
2333 struct efi_handler *handler;
2334 struct efi_open_protocol_info_item *item;
2335 efi_status_t r;
2336
Rob Clark238f88c2017-09-13 18:05:41 -04002337 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002338 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002339
2340 /* Check parameters */
2341 if (!entry_buffer) {
2342 r = EFI_INVALID_PARAMETER;
2343 goto out;
2344 }
2345 r = efi_search_protocol(handle, protocol, &handler);
2346 if (r != EFI_SUCCESS)
2347 goto out;
2348
2349 /* Count entries */
2350 count = 0;
2351 list_for_each_entry(item, &handler->open_infos, link) {
2352 if (item->info.open_count)
2353 ++count;
2354 }
2355 *entry_count = count;
2356 *entry_buffer = NULL;
2357 if (!count) {
2358 r = EFI_SUCCESS;
2359 goto out;
2360 }
2361
2362 /* Copy entries */
2363 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002364 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002365 (void **)entry_buffer);
2366 if (r != EFI_SUCCESS)
2367 goto out;
2368 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2369 if (item->info.open_count)
2370 (*entry_buffer)[--count] = item->info;
2371 }
2372out:
2373 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002374}
2375
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002376/**
Mario Six8fac2912018-07-10 08:40:17 +02002377 * efi_protocols_per_handle() - get protocols installed on a handle
2378 * @handle: handle for which the information is retrieved
2379 * @protocol_buffer: buffer with protocol GUIDs
2380 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002381 *
2382 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002383 *
Mario Six8fac2912018-07-10 08:40:17 +02002384 * See the Unified Extensible Firmware Interface (UEFI) specification for
2385 * details.
2386 *
2387 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002388 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002389static efi_status_t EFIAPI efi_protocols_per_handle(
2390 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002391 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002392{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002393 unsigned long buffer_size;
2394 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002395 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002396 efi_status_t r;
2397
Alexander Grafc15d9212016-03-04 01:09:59 +01002398 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2399 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002400
2401 if (!handle || !protocol_buffer || !protocol_buffer_count)
2402 return EFI_EXIT(EFI_INVALID_PARAMETER);
2403
2404 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002405 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002406
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002407 efiobj = efi_search_obj(handle);
2408 if (!efiobj)
2409 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002410
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002411 /* Count protocols */
2412 list_for_each(protocol_handle, &efiobj->protocols) {
2413 ++*protocol_buffer_count;
2414 }
2415
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002416 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002417 if (*protocol_buffer_count) {
2418 size_t j = 0;
2419
2420 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002421 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002422 (void **)protocol_buffer);
2423 if (r != EFI_SUCCESS)
2424 return EFI_EXIT(r);
2425 list_for_each(protocol_handle, &efiobj->protocols) {
2426 struct efi_handler *protocol;
2427
2428 protocol = list_entry(protocol_handle,
2429 struct efi_handler, link);
2430 (*protocol_buffer)[j] = (void *)protocol->guid;
2431 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002432 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002433 }
2434
2435 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002436}
2437
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002438/**
Mario Six8fac2912018-07-10 08:40:17 +02002439 * efi_locate_handle_buffer() - locate handles implementing a protocol
2440 * @search_type: selection criterion
2441 * @protocol: GUID of the protocol
2442 * @search_key: registration key
2443 * @no_handles: number of returned handles
2444 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002445 *
2446 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002447 *
Mario Six8fac2912018-07-10 08:40:17 +02002448 * See the Unified Extensible Firmware Interface (UEFI) specification for
2449 * details.
2450 *
2451 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002452 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002453efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002454 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002455 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002456 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002457{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002458 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002459 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002460
Rob Clark238f88c2017-09-13 18:05:41 -04002461 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002462 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002463
2464 if (!no_handles || !buffer) {
2465 r = EFI_INVALID_PARAMETER;
2466 goto out;
2467 }
2468 *no_handles = 0;
2469 *buffer = NULL;
2470 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2471 *buffer);
2472 if (r != EFI_BUFFER_TOO_SMALL)
2473 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002474 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002475 (void **)buffer);
2476 if (r != EFI_SUCCESS)
2477 goto out;
2478 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2479 *buffer);
2480 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002481 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002482out:
2483 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002484}
2485
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002486/**
Mario Six8fac2912018-07-10 08:40:17 +02002487 * efi_locate_protocol() - find an interface implementing a protocol
2488 * @protocol: GUID of the protocol
2489 * @registration: registration key passed to the notification function
2490 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002491 *
2492 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002493 *
2494 * See the Unified Extensible Firmware Interface (UEFI) specification for
2495 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002496 *
Mario Six8fac2912018-07-10 08:40:17 +02002497 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002498 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002499static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002500 void *registration,
2501 void **protocol_interface)
2502{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002503 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002504 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002505 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002506
Rob Clark238f88c2017-09-13 18:05:41 -04002507 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002508
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002509 /*
2510 * The UEFI spec explicitly requires a protocol even if a registration
2511 * key is provided. This differs from the logic in LocateHandle().
2512 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002513 if (!protocol || !protocol_interface)
2514 return EFI_EXIT(EFI_INVALID_PARAMETER);
2515
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002516 if (registration) {
2517 struct efi_register_notify_event *event;
2518 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002519
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002520 event = efi_check_register_notify_event(registration);
2521 if (!event)
2522 return EFI_EXIT(EFI_INVALID_PARAMETER);
2523 /*
2524 * The UEFI spec requires to return EFI_NOT_FOUND if no
2525 * protocol instance matches protocol and registration.
2526 * So let's do the same for a mismatch between protocol and
2527 * registration.
2528 */
2529 if (guidcmp(&event->protocol, protocol))
2530 goto not_found;
2531 if (list_empty(&event->handles))
2532 goto not_found;
2533 handle = list_first_entry(&event->handles,
2534 struct efi_protocol_notification,
2535 link);
2536 efiobj = handle->handle;
2537 list_del(&handle->link);
2538 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002539 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002540 if (ret == EFI_SUCCESS)
2541 goto found;
2542 } else {
2543 list_for_each_entry(efiobj, &efi_obj_list, link) {
2544 ret = efi_search_protocol(efiobj, protocol, &handler);
2545 if (ret == EFI_SUCCESS)
2546 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002547 }
2548 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002549not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002550 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002551 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002552found:
2553 *protocol_interface = handler->protocol_interface;
2554 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002555}
2556
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002557/**
Mario Six8fac2912018-07-10 08:40:17 +02002558 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2559 * interfaces
2560 * @handle: handle on which the protocol interfaces shall be installed
2561 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2562 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002563 *
2564 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002565 *
2566 * See the Unified Extensible Firmware Interface (UEFI) specification for
2567 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002568 *
Mario Six8fac2912018-07-10 08:40:17 +02002569 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002570 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002571efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002572 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002573{
2574 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002575
Alexander Graf404a7c82018-06-18 17:23:05 +02002576 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002577 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002578 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002579 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002580 efi_status_t r = EFI_SUCCESS;
2581 int i = 0;
2582
2583 if (!handle)
2584 return EFI_EXIT(EFI_INVALID_PARAMETER);
2585
Alexander Graf404a7c82018-06-18 17:23:05 +02002586 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002587 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002588 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002589 if (!protocol)
2590 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002591 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002592 /* Check that a device path has not been installed before */
2593 if (!guidcmp(protocol, &efi_guid_device_path)) {
2594 struct efi_device_path *dp = protocol_interface;
2595
2596 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2597 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002598 if (r == EFI_SUCCESS &&
2599 dp->type == DEVICE_PATH_TYPE_END) {
2600 EFI_PRINT("Path %pD already installed\n",
2601 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002602 r = EFI_ALREADY_STARTED;
2603 break;
2604 }
2605 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002606 r = EFI_CALL(efi_install_protocol_interface(
2607 handle, protocol,
2608 EFI_NATIVE_INTERFACE,
2609 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002610 if (r != EFI_SUCCESS)
2611 break;
2612 i++;
2613 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002614 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002615 if (r == EFI_SUCCESS)
2616 return EFI_EXIT(r);
2617
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002618 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002619 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002620 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002621 protocol = efi_va_arg(argptr, efi_guid_t*);
2622 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002623 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002624 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002625 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002626 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002627
2628 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002629}
2630
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002631/**
Mario Six8fac2912018-07-10 08:40:17 +02002632 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2633 * interfaces
2634 * @handle: handle from which the protocol interfaces shall be removed
2635 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2636 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002637 *
2638 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002639 *
Mario Six8fac2912018-07-10 08:40:17 +02002640 * See the Unified Extensible Firmware Interface (UEFI) specification for
2641 * details.
2642 *
2643 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002644 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002645static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002646 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002647{
2648 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002649
Alexander Graf404a7c82018-06-18 17:23:05 +02002650 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002651 const efi_guid_t *protocol;
2652 void *protocol_interface;
2653 efi_status_t r = EFI_SUCCESS;
2654 size_t i = 0;
2655
2656 if (!handle)
2657 return EFI_EXIT(EFI_INVALID_PARAMETER);
2658
Alexander Graf404a7c82018-06-18 17:23:05 +02002659 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002660 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002661 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002662 if (!protocol)
2663 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002664 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002665 r = efi_uninstall_protocol(handle, protocol,
2666 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002667 if (r != EFI_SUCCESS)
2668 break;
2669 i++;
2670 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002671 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002672 if (r == EFI_SUCCESS) {
2673 /* If the last protocol has been removed, delete the handle. */
2674 if (list_empty(&handle->protocols)) {
2675 list_del(&handle->link);
2676 free(handle);
2677 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002678 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002679 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002680
2681 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002682 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002683 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002684 protocol = efi_va_arg(argptr, efi_guid_t*);
2685 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002686 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2687 EFI_NATIVE_INTERFACE,
2688 protocol_interface));
2689 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002690 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002691
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002692 /* In case of an error always return EFI_INVALID_PARAMETER */
2693 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002694}
2695
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002696/**
Mario Six8fac2912018-07-10 08:40:17 +02002697 * efi_calculate_crc32() - calculate cyclic redundancy code
2698 * @data: buffer with data
2699 * @data_size: size of buffer in bytes
2700 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002701 *
2702 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002703 *
2704 * See the Unified Extensible Firmware Interface (UEFI) specification for
2705 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002706 *
Mario Six8fac2912018-07-10 08:40:17 +02002707 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002708 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002709static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2710 efi_uintn_t data_size,
2711 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002712{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002713 efi_status_t ret = EFI_SUCCESS;
2714
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002715 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002716 if (!data || !data_size || !crc32_p) {
2717 ret = EFI_INVALID_PARAMETER;
2718 goto out;
2719 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002720 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002721out:
2722 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002723}
2724
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002725/**
Mario Six8fac2912018-07-10 08:40:17 +02002726 * efi_copy_mem() - copy memory
2727 * @destination: destination of the copy operation
2728 * @source: source of the copy operation
2729 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002730 *
2731 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002732 *
Mario Six8fac2912018-07-10 08:40:17 +02002733 * See the Unified Extensible Firmware Interface (UEFI) specification for
2734 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002735 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002736static void EFIAPI efi_copy_mem(void *destination, const void *source,
2737 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002738{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002739 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002740 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002741 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002742}
2743
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002744/**
Mario Six8fac2912018-07-10 08:40:17 +02002745 * efi_set_mem() - Fill memory with a byte value.
2746 * @buffer: buffer to fill
2747 * @size: size of buffer in bytes
2748 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002749 *
2750 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002751 *
Mario Six8fac2912018-07-10 08:40:17 +02002752 * See the Unified Extensible Firmware Interface (UEFI) specification for
2753 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002754 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002755static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002756{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002757 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002758 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002759 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002760}
2761
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002762/**
Mario Six8fac2912018-07-10 08:40:17 +02002763 * efi_protocol_open() - open protocol interface on a handle
2764 * @handler: handler of a protocol
2765 * @protocol_interface: interface implementing the protocol
2766 * @agent_handle: handle of the driver
2767 * @controller_handle: handle of the controller
2768 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002769 *
Mario Six8fac2912018-07-10 08:40:17 +02002770 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002771 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002772efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002773 struct efi_handler *handler,
2774 void **protocol_interface, void *agent_handle,
2775 void *controller_handle, uint32_t attributes)
2776{
2777 struct efi_open_protocol_info_item *item;
2778 struct efi_open_protocol_info_entry *match = NULL;
2779 bool opened_by_driver = false;
2780 bool opened_exclusive = false;
2781
2782 /* If there is no agent, only return the interface */
2783 if (!agent_handle)
2784 goto out;
2785
2786 /* For TEST_PROTOCOL ignore interface attribute */
2787 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2788 *protocol_interface = NULL;
2789
2790 /*
2791 * Check if the protocol is already opened by a driver with the same
2792 * attributes or opened exclusively
2793 */
2794 list_for_each_entry(item, &handler->open_infos, link) {
2795 if (item->info.agent_handle == agent_handle) {
2796 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2797 (item->info.attributes == attributes))
2798 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002799 } else {
2800 if (item->info.attributes &
2801 EFI_OPEN_PROTOCOL_BY_DRIVER)
2802 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002803 }
2804 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2805 opened_exclusive = true;
2806 }
2807
2808 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002809 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2810 if (opened_exclusive)
2811 return EFI_ACCESS_DENIED;
2812 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2813 if (opened_exclusive || opened_by_driver)
2814 return EFI_ACCESS_DENIED;
2815 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002816
2817 /* Prepare exclusive opening */
2818 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2819 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002820disconnect_next:
2821 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002822 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002823 efi_status_t ret;
2824
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002825 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002826 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2827 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002828 item->info.controller_handle,
2829 item->info.agent_handle,
2830 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002831 if (ret == EFI_SUCCESS)
2832 /*
2833 * Child controllers may have been
2834 * removed from the open_infos list. So
2835 * let's restart the loop.
2836 */
2837 goto disconnect_next;
2838 else
2839 opened_by_driver = true;
2840 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002841 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002842 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002843 if (opened_by_driver)
2844 return EFI_ACCESS_DENIED;
2845 }
2846
2847 /* Find existing entry */
2848 list_for_each_entry(item, &handler->open_infos, link) {
2849 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002850 item->info.controller_handle == controller_handle &&
2851 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002852 match = &item->info;
2853 }
2854 /* None found, create one */
2855 if (!match) {
2856 match = efi_create_open_info(handler);
2857 if (!match)
2858 return EFI_OUT_OF_RESOURCES;
2859 }
2860
2861 match->agent_handle = agent_handle;
2862 match->controller_handle = controller_handle;
2863 match->attributes = attributes;
2864 match->open_count++;
2865
2866out:
2867 /* For TEST_PROTOCOL ignore interface attribute. */
2868 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2869 *protocol_interface = handler->protocol_interface;
2870
2871 return EFI_SUCCESS;
2872}
2873
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002874/**
Mario Six8fac2912018-07-10 08:40:17 +02002875 * efi_open_protocol() - open protocol interface on a handle
2876 * @handle: handle on which the protocol shall be opened
2877 * @protocol: GUID of the protocol
2878 * @protocol_interface: interface implementing the protocol
2879 * @agent_handle: handle of the driver
2880 * @controller_handle: handle of the controller
2881 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002882 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002883 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002884 *
Mario Six8fac2912018-07-10 08:40:17 +02002885 * See the Unified Extensible Firmware Interface (UEFI) specification for
2886 * details.
2887 *
2888 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002889 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002890static efi_status_t EFIAPI efi_open_protocol
2891 (efi_handle_t handle, const efi_guid_t *protocol,
2892 void **protocol_interface, efi_handle_t agent_handle,
2893 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002894{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002895 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002896 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002897
Rob Clark238f88c2017-09-13 18:05:41 -04002898 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002899 protocol_interface, agent_handle, controller_handle,
2900 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002901
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002902 if (!handle || !protocol ||
2903 (!protocol_interface && attributes !=
2904 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002905 goto out;
2906 }
2907
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002908 switch (attributes) {
2909 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2910 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2911 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2912 break;
2913 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2914 if (controller_handle == handle)
2915 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002916 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002917 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2918 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002919 /* Check that the controller handle is valid */
2920 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002921 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002922 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002923 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002924 /* Check that the agent handle is valid */
2925 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002926 goto out;
2927 break;
2928 default:
2929 goto out;
2930 }
2931
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002932 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002933 switch (r) {
2934 case EFI_SUCCESS:
2935 break;
2936 case EFI_NOT_FOUND:
2937 r = EFI_UNSUPPORTED;
2938 goto out;
2939 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002940 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002941 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002942
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002943 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2944 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002945out:
2946 return EFI_EXIT(r);
2947}
2948
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002949/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002950 * efi_start_image() - call the entry point of an image
2951 * @image_handle: handle of the image
2952 * @exit_data_size: size of the buffer
2953 * @exit_data: buffer to receive the exit data of the called image
2954 *
2955 * This function implements the StartImage service.
2956 *
2957 * See the Unified Extensible Firmware Interface (UEFI) specification for
2958 * details.
2959 *
2960 * Return: status code
2961 */
2962efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2963 efi_uintn_t *exit_data_size,
2964 u16 **exit_data)
2965{
2966 struct efi_loaded_image_obj *image_obj =
2967 (struct efi_loaded_image_obj *)image_handle;
2968 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002969 void *info;
2970 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002971 efi_status_t exit_status;
2972 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002973
2974 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2975
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002976 if (!efi_search_obj(image_handle))
2977 return EFI_EXIT(EFI_INVALID_PARAMETER);
2978
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002979 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002980 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2981 return EFI_EXIT(EFI_INVALID_PARAMETER);
2982
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002983 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2984 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2985
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002986 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2987 &info, NULL, NULL,
2988 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2989 if (ret != EFI_SUCCESS)
2990 return EFI_EXIT(EFI_INVALID_PARAMETER);
2991
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002992 image_obj->exit_data_size = exit_data_size;
2993 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002994 image_obj->exit_status = &exit_status;
2995 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002996
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002997 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002998 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002999 /*
3000 * We called the entry point of the child image with EFI_CALL
3001 * in the lines below. The child image called the Exit() boot
3002 * service efi_exit() which executed the long jump that brought
3003 * us to the current line. This implies that the second half
3004 * of the EFI_CALL macro has not been executed.
3005 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003006#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003007 /*
3008 * efi_exit() called efi_restore_gd(). We have to undo this
3009 * otherwise __efi_entry_check() will put the wrong value into
3010 * app_gd.
3011 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003012 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003013#endif
3014 /*
3015 * To get ready to call EFI_EXIT below we have to execute the
3016 * missed out steps of EFI_CALL.
3017 */
3018 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003019 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003020 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003021 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003022 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003023 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003024 }
3025
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003026 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003027 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003028 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003029 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3030
3031 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003032 * Control is returned from a started UEFI image either by calling
3033 * Exit() (where exit data can be provided) or by simply returning from
3034 * the entry point. In the latter case call Exit() on behalf of the
3035 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003036 */
3037 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3038}
3039
3040/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003041 * efi_delete_image() - delete loaded image from memory)
3042 *
3043 * @image_obj: handle of the loaded image
3044 * @loaded_image_protocol: loaded image protocol
3045 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003046static efi_status_t efi_delete_image
3047 (struct efi_loaded_image_obj *image_obj,
3048 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003049{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003050 struct efi_object *efiobj;
3051 efi_status_t r, ret = EFI_SUCCESS;
3052
3053close_next:
3054 list_for_each_entry(efiobj, &efi_obj_list, link) {
3055 struct efi_handler *protocol;
3056
3057 list_for_each_entry(protocol, &efiobj->protocols, link) {
3058 struct efi_open_protocol_info_item *info;
3059
3060 list_for_each_entry(info, &protocol->open_infos, link) {
3061 if (info->info.agent_handle !=
3062 (efi_handle_t)image_obj)
3063 continue;
3064 r = EFI_CALL(efi_close_protocol
3065 (efiobj, protocol->guid,
3066 info->info.agent_handle,
3067 info->info.controller_handle
3068 ));
3069 if (r != EFI_SUCCESS)
3070 ret = r;
3071 /*
3072 * Closing protocols may results in further
3073 * items being deleted. To play it safe loop
3074 * over all elements again.
3075 */
3076 goto close_next;
3077 }
3078 }
3079 }
3080
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003081 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3082 efi_size_in_pages(loaded_image_protocol->image_size));
3083 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003084
3085 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003086}
3087
3088/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003089 * efi_unload_image() - unload an EFI image
3090 * @image_handle: handle of the image to be unloaded
3091 *
3092 * This function implements the UnloadImage service.
3093 *
3094 * See the Unified Extensible Firmware Interface (UEFI) specification for
3095 * details.
3096 *
3097 * Return: status code
3098 */
3099efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3100{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003101 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003102 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003103 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003104
3105 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003106
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003107 efiobj = efi_search_obj(image_handle);
3108 if (!efiobj) {
3109 ret = EFI_INVALID_PARAMETER;
3110 goto out;
3111 }
3112 /* Find the loaded image protocol */
3113 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3114 (void **)&loaded_image_protocol,
3115 NULL, NULL,
3116 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3117 if (ret != EFI_SUCCESS) {
3118 ret = EFI_INVALID_PARAMETER;
3119 goto out;
3120 }
3121 switch (efiobj->type) {
3122 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3123 /* Call the unload function */
3124 if (!loaded_image_protocol->unload) {
3125 ret = EFI_UNSUPPORTED;
3126 goto out;
3127 }
3128 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3129 if (ret != EFI_SUCCESS)
3130 goto out;
3131 break;
3132 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3133 break;
3134 default:
3135 ret = EFI_INVALID_PARAMETER;
3136 goto out;
3137 }
3138 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3139 loaded_image_protocol);
3140out:
3141 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003142}
3143
3144/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003145 * efi_update_exit_data() - fill exit data parameters of StartImage()
3146 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003147 * @image_obj: image handle
3148 * @exit_data_size: size of the exit data buffer
3149 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003150 * Return: status code
3151 */
3152static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3153 efi_uintn_t exit_data_size,
3154 u16 *exit_data)
3155{
3156 efi_status_t ret;
3157
3158 /*
3159 * If exit_data is not provided to StartImage(), exit_data_size must be
3160 * ignored.
3161 */
3162 if (!image_obj->exit_data)
3163 return EFI_SUCCESS;
3164 if (image_obj->exit_data_size)
3165 *image_obj->exit_data_size = exit_data_size;
3166 if (exit_data_size && exit_data) {
3167 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3168 exit_data_size,
3169 (void **)image_obj->exit_data);
3170 if (ret != EFI_SUCCESS)
3171 return ret;
3172 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3173 } else {
3174 image_obj->exit_data = NULL;
3175 }
3176 return EFI_SUCCESS;
3177}
3178
3179/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003180 * efi_exit() - leave an EFI application or driver
3181 * @image_handle: handle of the application or driver that is exiting
3182 * @exit_status: status code
3183 * @exit_data_size: size of the buffer in bytes
3184 * @exit_data: buffer with data describing an error
3185 *
3186 * This function implements the Exit service.
3187 *
3188 * See the Unified Extensible Firmware Interface (UEFI) specification for
3189 * details.
3190 *
3191 * Return: status code
3192 */
3193static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3194 efi_status_t exit_status,
3195 efi_uintn_t exit_data_size,
3196 u16 *exit_data)
3197{
3198 /*
3199 * TODO: We should call the unload procedure of the loaded
3200 * image protocol.
3201 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003202 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003203 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003204 struct efi_loaded_image_obj *image_obj =
3205 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003206 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003207
3208 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3209 exit_data_size, exit_data);
3210
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003211 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003212 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003213 (void **)&loaded_image_protocol,
3214 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003215 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003216 if (ret != EFI_SUCCESS) {
3217 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003218 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003219 }
3220
3221 /* Unloading of unstarted images */
3222 switch (image_obj->header.type) {
3223 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3224 break;
3225 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3226 efi_delete_image(image_obj, loaded_image_protocol);
3227 ret = EFI_SUCCESS;
3228 goto out;
3229 default:
3230 /* Handle does not refer to loaded image */
3231 ret = EFI_INVALID_PARAMETER;
3232 goto out;
3233 }
3234 /* A started image can only be unloaded it is the last one started. */
3235 if (image_handle != current_image) {
3236 ret = EFI_INVALID_PARAMETER;
3237 goto out;
3238 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003239
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003240 /* Exit data is only foreseen in case of failure. */
3241 if (exit_status != EFI_SUCCESS) {
3242 ret = efi_update_exit_data(image_obj, exit_data_size,
3243 exit_data);
3244 /* Exiting has priority. Don't return error to caller. */
3245 if (ret != EFI_SUCCESS)
3246 EFI_PRINT("%s: out of memory\n", __func__);
3247 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003248 /* efi_delete_image() frees image_obj. Copy before the call. */
3249 exit_jmp = image_obj->exit_jmp;
3250 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003251 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3252 exit_status != EFI_SUCCESS)
3253 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003254
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003255 /* Make sure entry/exit counts for EFI world cross-overs match */
3256 EFI_EXIT(exit_status);
3257
3258 /*
3259 * But longjmp out with the U-Boot gd, not the application's, as
3260 * the other end is a setjmp call inside EFI context.
3261 */
3262 efi_restore_gd();
3263
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003264 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003265
3266 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003267out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003268 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003269}
3270
3271/**
Mario Six8fac2912018-07-10 08:40:17 +02003272 * efi_handle_protocol() - get interface of a protocol on a handle
3273 * @handle: handle on which the protocol shall be opened
3274 * @protocol: GUID of the protocol
3275 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003276 *
3277 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003278 *
3279 * See the Unified Extensible Firmware Interface (UEFI) specification for
3280 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003281 *
Mario Six8fac2912018-07-10 08:40:17 +02003282 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003283 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003284efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3285 const efi_guid_t *protocol,
3286 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003287{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003288 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003289 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003290}
3291
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003292/**
Mario Six8fac2912018-07-10 08:40:17 +02003293 * efi_bind_controller() - bind a single driver to a controller
3294 * @controller_handle: controller handle
3295 * @driver_image_handle: driver handle
3296 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003297 *
Mario Six8fac2912018-07-10 08:40:17 +02003298 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003299 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003300static efi_status_t efi_bind_controller(
3301 efi_handle_t controller_handle,
3302 efi_handle_t driver_image_handle,
3303 struct efi_device_path *remain_device_path)
3304{
3305 struct efi_driver_binding_protocol *binding_protocol;
3306 efi_status_t r;
3307
3308 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3309 &efi_guid_driver_binding_protocol,
3310 (void **)&binding_protocol,
3311 driver_image_handle, NULL,
3312 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3313 if (r != EFI_SUCCESS)
3314 return r;
3315 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3316 controller_handle,
3317 remain_device_path));
3318 if (r == EFI_SUCCESS)
3319 r = EFI_CALL(binding_protocol->start(binding_protocol,
3320 controller_handle,
3321 remain_device_path));
3322 EFI_CALL(efi_close_protocol(driver_image_handle,
3323 &efi_guid_driver_binding_protocol,
3324 driver_image_handle, NULL));
3325 return r;
3326}
3327
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003328/**
Mario Six8fac2912018-07-10 08:40:17 +02003329 * efi_connect_single_controller() - connect a single driver to a controller
3330 * @controller_handle: controller
3331 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003332 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003333 *
Mario Six8fac2912018-07-10 08:40:17 +02003334 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003335 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003336static efi_status_t efi_connect_single_controller(
3337 efi_handle_t controller_handle,
3338 efi_handle_t *driver_image_handle,
3339 struct efi_device_path *remain_device_path)
3340{
3341 efi_handle_t *buffer;
3342 size_t count;
3343 size_t i;
3344 efi_status_t r;
3345 size_t connected = 0;
3346
3347 /* Get buffer with all handles with driver binding protocol */
3348 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3349 &efi_guid_driver_binding_protocol,
3350 NULL, &count, &buffer));
3351 if (r != EFI_SUCCESS)
3352 return r;
3353
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003354 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003355 if (driver_image_handle) {
3356 for (; *driver_image_handle; ++driver_image_handle) {
3357 for (i = 0; i < count; ++i) {
3358 if (buffer[i] == *driver_image_handle) {
3359 buffer[i] = NULL;
3360 r = efi_bind_controller(
3361 controller_handle,
3362 *driver_image_handle,
3363 remain_device_path);
3364 /*
3365 * For drivers that do not support the
3366 * controller or are already connected
3367 * we receive an error code here.
3368 */
3369 if (r == EFI_SUCCESS)
3370 ++connected;
3371 }
3372 }
3373 }
3374 }
3375
3376 /*
3377 * TODO: Some overrides are not yet implemented:
3378 * - Platform Driver Override
3379 * - Driver Family Override Search
3380 * - Bus Specific Driver Override
3381 */
3382
3383 /* Driver Binding Search */
3384 for (i = 0; i < count; ++i) {
3385 if (buffer[i]) {
3386 r = efi_bind_controller(controller_handle,
3387 buffer[i],
3388 remain_device_path);
3389 if (r == EFI_SUCCESS)
3390 ++connected;
3391 }
3392 }
3393
3394 efi_free_pool(buffer);
3395 if (!connected)
3396 return EFI_NOT_FOUND;
3397 return EFI_SUCCESS;
3398}
3399
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003400/**
Mario Six8fac2912018-07-10 08:40:17 +02003401 * efi_connect_controller() - connect a controller to a driver
3402 * @controller_handle: handle of the controller
3403 * @driver_image_handle: handle of the driver
3404 * @remain_device_path: device path of a child controller
3405 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003406 *
3407 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003408 *
3409 * See the Unified Extensible Firmware Interface (UEFI) specification for
3410 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003411 *
3412 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003413 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003414 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3415 *
Mario Six8fac2912018-07-10 08:40:17 +02003416 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003417 */
3418static efi_status_t EFIAPI efi_connect_controller(
3419 efi_handle_t controller_handle,
3420 efi_handle_t *driver_image_handle,
3421 struct efi_device_path *remain_device_path,
3422 bool recursive)
3423{
3424 efi_status_t r;
3425 efi_status_t ret = EFI_NOT_FOUND;
3426 struct efi_object *efiobj;
3427
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003428 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003429 remain_device_path, recursive);
3430
3431 efiobj = efi_search_obj(controller_handle);
3432 if (!efiobj) {
3433 ret = EFI_INVALID_PARAMETER;
3434 goto out;
3435 }
3436
3437 r = efi_connect_single_controller(controller_handle,
3438 driver_image_handle,
3439 remain_device_path);
3440 if (r == EFI_SUCCESS)
3441 ret = EFI_SUCCESS;
3442 if (recursive) {
3443 struct efi_handler *handler;
3444 struct efi_open_protocol_info_item *item;
3445
3446 list_for_each_entry(handler, &efiobj->protocols, link) {
3447 list_for_each_entry(item, &handler->open_infos, link) {
3448 if (item->info.attributes &
3449 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3450 r = EFI_CALL(efi_connect_controller(
3451 item->info.controller_handle,
3452 driver_image_handle,
3453 remain_device_path,
3454 recursive));
3455 if (r == EFI_SUCCESS)
3456 ret = EFI_SUCCESS;
3457 }
3458 }
3459 }
3460 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003461 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003462 if (ret != EFI_SUCCESS && remain_device_path &&
3463 remain_device_path->type == DEVICE_PATH_TYPE_END)
3464 ret = EFI_SUCCESS;
3465out:
3466 return EFI_EXIT(ret);
3467}
3468
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003469/**
Mario Six8fac2912018-07-10 08:40:17 +02003470 * efi_reinstall_protocol_interface() - reinstall protocol interface
3471 * @handle: handle on which the protocol shall be reinstalled
3472 * @protocol: GUID of the protocol to be installed
3473 * @old_interface: interface to be removed
3474 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003475 *
3476 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003477 *
3478 * See the Unified Extensible Firmware Interface (UEFI) specification for
3479 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003480 *
3481 * The old interface is uninstalled. The new interface is installed.
3482 * Drivers are connected.
3483 *
Mario Six8fac2912018-07-10 08:40:17 +02003484 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003485 */
3486static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3487 efi_handle_t handle, const efi_guid_t *protocol,
3488 void *old_interface, void *new_interface)
3489{
3490 efi_status_t ret;
3491
3492 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3493 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003494
3495 /* Uninstall protocol but do not delete handle */
3496 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003497 if (ret != EFI_SUCCESS)
3498 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003499
3500 /* Install the new protocol */
3501 ret = efi_add_protocol(handle, protocol, new_interface);
3502 /*
3503 * The UEFI spec does not specify what should happen to the handle
3504 * if in case of an error no protocol interface remains on the handle.
3505 * So let's do nothing here.
3506 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003507 if (ret != EFI_SUCCESS)
3508 goto out;
3509 /*
3510 * The returned status code has to be ignored.
3511 * Do not create an error if no suitable driver for the handle exists.
3512 */
3513 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3514out:
3515 return EFI_EXIT(ret);
3516}
3517
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003518/**
Mario Six8fac2912018-07-10 08:40:17 +02003519 * efi_get_child_controllers() - get all child controllers associated to a driver
3520 * @efiobj: handle of the controller
3521 * @driver_handle: handle of the driver
3522 * @number_of_children: number of child controllers
3523 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003524 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003525 * The allocated buffer has to be freed with free().
3526 *
Mario Six8fac2912018-07-10 08:40:17 +02003527 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003528 */
3529static efi_status_t efi_get_child_controllers(
3530 struct efi_object *efiobj,
3531 efi_handle_t driver_handle,
3532 efi_uintn_t *number_of_children,
3533 efi_handle_t **child_handle_buffer)
3534{
3535 struct efi_handler *handler;
3536 struct efi_open_protocol_info_item *item;
3537 efi_uintn_t count = 0, i;
3538 bool duplicate;
3539
3540 /* Count all child controller associations */
3541 list_for_each_entry(handler, &efiobj->protocols, link) {
3542 list_for_each_entry(item, &handler->open_infos, link) {
3543 if (item->info.agent_handle == driver_handle &&
3544 item->info.attributes &
3545 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3546 ++count;
3547 }
3548 }
3549 /*
3550 * Create buffer. In case of duplicate child controller assignments
3551 * the buffer will be too large. But that does not harm.
3552 */
3553 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003554 if (!count)
3555 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003556 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3557 if (!*child_handle_buffer)
3558 return EFI_OUT_OF_RESOURCES;
3559 /* Copy unique child handles */
3560 list_for_each_entry(handler, &efiobj->protocols, link) {
3561 list_for_each_entry(item, &handler->open_infos, link) {
3562 if (item->info.agent_handle == driver_handle &&
3563 item->info.attributes &
3564 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3565 /* Check this is a new child controller */
3566 duplicate = false;
3567 for (i = 0; i < *number_of_children; ++i) {
3568 if ((*child_handle_buffer)[i] ==
3569 item->info.controller_handle)
3570 duplicate = true;
3571 }
3572 /* Copy handle to buffer */
3573 if (!duplicate) {
3574 i = (*number_of_children)++;
3575 (*child_handle_buffer)[i] =
3576 item->info.controller_handle;
3577 }
3578 }
3579 }
3580 }
3581 return EFI_SUCCESS;
3582}
3583
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003584/**
Mario Six8fac2912018-07-10 08:40:17 +02003585 * efi_disconnect_controller() - disconnect a controller from a driver
3586 * @controller_handle: handle of the controller
3587 * @driver_image_handle: handle of the driver
3588 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003589 *
3590 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003591 *
3592 * See the Unified Extensible Firmware Interface (UEFI) specification for
3593 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003594 *
Mario Six8fac2912018-07-10 08:40:17 +02003595 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003596 */
3597static efi_status_t EFIAPI efi_disconnect_controller(
3598 efi_handle_t controller_handle,
3599 efi_handle_t driver_image_handle,
3600 efi_handle_t child_handle)
3601{
3602 struct efi_driver_binding_protocol *binding_protocol;
3603 efi_handle_t *child_handle_buffer = NULL;
3604 size_t number_of_children = 0;
3605 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003606 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003607 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003608
3609 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3610 child_handle);
3611
3612 efiobj = efi_search_obj(controller_handle);
3613 if (!efiobj) {
3614 r = EFI_INVALID_PARAMETER;
3615 goto out;
3616 }
3617
3618 if (child_handle && !efi_search_obj(child_handle)) {
3619 r = EFI_INVALID_PARAMETER;
3620 goto out;
3621 }
3622
3623 /* If no driver handle is supplied, disconnect all drivers */
3624 if (!driver_image_handle) {
3625 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3626 goto out;
3627 }
3628
3629 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003630 r = efi_get_child_controllers(efiobj,
3631 driver_image_handle,
3632 &number_of_children,
3633 &child_handle_buffer);
3634 if (r != EFI_SUCCESS)
3635 return r;
3636 sole_child = (number_of_children == 1);
3637
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003638 if (child_handle) {
3639 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003640 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003641 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003642 }
3643
3644 /* Get the driver binding protocol */
3645 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3646 &efi_guid_driver_binding_protocol,
3647 (void **)&binding_protocol,
3648 driver_image_handle, NULL,
3649 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003650 if (r != EFI_SUCCESS) {
3651 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003652 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003653 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003654 /* Remove the children */
3655 if (number_of_children) {
3656 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3657 controller_handle,
3658 number_of_children,
3659 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003660 if (r != EFI_SUCCESS) {
3661 r = EFI_DEVICE_ERROR;
3662 goto out;
3663 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003664 }
3665 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003666 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003667 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3668 controller_handle,
3669 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003670 if (r != EFI_SUCCESS) {
3671 r = EFI_DEVICE_ERROR;
3672 goto out;
3673 }
3674 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003675 EFI_CALL(efi_close_protocol(driver_image_handle,
3676 &efi_guid_driver_binding_protocol,
3677 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003678 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003679out:
3680 if (!child_handle)
3681 free(child_handle_buffer);
3682 return EFI_EXIT(r);
3683}
3684
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003685static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003686 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003687 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3688 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003689 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003690 },
3691 .raise_tpl = efi_raise_tpl,
3692 .restore_tpl = efi_restore_tpl,
3693 .allocate_pages = efi_allocate_pages_ext,
3694 .free_pages = efi_free_pages_ext,
3695 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003696 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003697 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003698 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003699 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003700 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003701 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003702 .close_event = efi_close_event,
3703 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003704 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003705 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003706 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003707 .handle_protocol = efi_handle_protocol,
3708 .reserved = NULL,
3709 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003710 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003711 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003712 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003713 .load_image = efi_load_image,
3714 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003715 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003716 .unload_image = efi_unload_image,
3717 .exit_boot_services = efi_exit_boot_services,
3718 .get_next_monotonic_count = efi_get_next_monotonic_count,
3719 .stall = efi_stall,
3720 .set_watchdog_timer = efi_set_watchdog_timer,
3721 .connect_controller = efi_connect_controller,
3722 .disconnect_controller = efi_disconnect_controller,
3723 .open_protocol = efi_open_protocol,
3724 .close_protocol = efi_close_protocol,
3725 .open_protocol_information = efi_open_protocol_information,
3726 .protocols_per_handle = efi_protocols_per_handle,
3727 .locate_handle_buffer = efi_locate_handle_buffer,
3728 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003729 .install_multiple_protocol_interfaces =
3730 efi_install_multiple_protocol_interfaces,
3731 .uninstall_multiple_protocol_interfaces =
3732 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003733 .calculate_crc32 = efi_calculate_crc32,
3734 .copy_mem = efi_copy_mem,
3735 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003736 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003737};
3738
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003739static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003740
Alexander Graf393dd912016-10-14 13:45:30 +02003741struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003742 .hdr = {
3743 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003744 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003745 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003746 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003747 .fw_vendor = firmware_vendor,
3748 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003749 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003750 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003751 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003752};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003753
3754/**
3755 * efi_initialize_system_table() - Initialize system table
3756 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003757 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003758 */
3759efi_status_t efi_initialize_system_table(void)
3760{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003761 efi_status_t ret;
3762
3763 /* Allocate configuration table array */
3764 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3765 EFI_MAX_CONFIGURATION_TABLES *
3766 sizeof(struct efi_configuration_table),
3767 (void **)&systab.tables);
3768
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003769 /*
3770 * These entries will be set to NULL in ExitBootServices(). To avoid
3771 * relocation in SetVirtualAddressMap(), set them dynamically.
3772 */
3773 systab.con_in = &efi_con_in;
3774 systab.con_out = &efi_con_out;
3775 systab.std_err = &efi_con_out;
3776 systab.boottime = &efi_boot_services;
3777
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003778 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003779 efi_update_table_header_crc32(&systab.hdr);
3780 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3781 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003782
3783 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003784}