blob: f6d5ba05e34ad288a7dc98750320da8ca82ff84f [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:
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200267 return EFI_SUCCESS;
268 default:
269 return EFI_INVALID_PARAMETER;
270 }
271}
272
273/**
Mario Six8fac2912018-07-10 08:40:17 +0200274 * efi_signal_event() - signal an EFI event
275 * @event: event to signal
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100276 *
Heinrich Schuchardt9de0fb72020-12-28 00:59:09 +0100277 * This function signals an event. If the event belongs to an event group, all
278 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100279 * their notification function is queued.
280 *
281 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100282 */
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200283void efi_signal_event(struct efi_event *event)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100284{
Heinrich Schuchardt0b4de562019-06-06 01:51:50 +0200285 if (event->is_signaled)
286 return;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100287 if (event->group) {
288 struct efi_event *evt;
289
290 /*
291 * The signaled state has to set before executing any
292 * notification function
293 */
294 list_for_each_entry(evt, &efi_events, link) {
295 if (!evt->group || guidcmp(evt->group, event->group))
296 continue;
297 if (evt->is_signaled)
298 continue;
299 evt->is_signaled = true;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100300 }
301 list_for_each_entry(evt, &efi_events, link) {
302 if (!evt->group || guidcmp(evt->group, event->group))
303 continue;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200304 efi_queue_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100305 }
Heinrich Schuchardt66ddc2e2019-05-05 00:07:34 +0200306 } else {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100307 event->is_signaled = true;
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200308 efi_queue_event(event);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100309 }
310}
311
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200312/**
Mario Six8fac2912018-07-10 08:40:17 +0200313 * efi_raise_tpl() - raise the task priority level
314 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200315 *
316 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200317 *
318 * See the Unified Extensible Firmware Interface (UEFI) specification for
319 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200320 *
Mario Six8fac2912018-07-10 08:40:17 +0200321 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200322 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100323static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100324{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100325 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200326
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200327 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200328
329 if (new_tpl < efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200330 EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200331 efi_tpl = new_tpl;
332 if (efi_tpl > TPL_HIGH_LEVEL)
333 efi_tpl = TPL_HIGH_LEVEL;
334
335 EFI_EXIT(EFI_SUCCESS);
336 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100337}
338
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200339/**
Mario Six8fac2912018-07-10 08:40:17 +0200340 * efi_restore_tpl() - lower the task priority level
341 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200342 *
343 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200344 *
Mario Six8fac2912018-07-10 08:40:17 +0200345 * See the Unified Extensible Firmware Interface (UEFI) specification for
346 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100348static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100349{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200350 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200351
352 if (old_tpl > efi_tpl)
Heinrich Schuchardt952e2062019-05-05 11:56:23 +0200353 EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200354 efi_tpl = old_tpl;
355 if (efi_tpl > TPL_HIGH_LEVEL)
356 efi_tpl = TPL_HIGH_LEVEL;
357
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100358 /*
359 * Lowering the TPL may have made queued events eligible for execution.
360 */
361 efi_timer_check();
362
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200363 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100364}
365
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200366/**
Mario Six8fac2912018-07-10 08:40:17 +0200367 * efi_allocate_pages_ext() - allocate memory pages
368 * @type: type of allocation to be performed
369 * @memory_type: usage type of the allocated memory
370 * @pages: number of pages to be allocated
371 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200372 *
373 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200374 *
Mario Six8fac2912018-07-10 08:40:17 +0200375 * See the Unified Extensible Firmware Interface (UEFI) specification for
376 * details.
377 *
378 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200379 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900380static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100381 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900382 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100383{
384 efi_status_t r;
385
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100386 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100387 r = efi_allocate_pages(type, memory_type, pages, memory);
388 return EFI_EXIT(r);
389}
390
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200391/**
Mario Six8fac2912018-07-10 08:40:17 +0200392 * efi_free_pages_ext() - Free memory pages.
393 * @memory: start of the memory area to be freed
394 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200395 *
396 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200397 *
398 * See the Unified Extensible Firmware Interface (UEFI) specification for
399 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200400 *
Mario Six8fac2912018-07-10 08:40:17 +0200401 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200402 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900403static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100404 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
406 efi_status_t r;
407
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900408 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100409 r = efi_free_pages(memory, pages);
410 return EFI_EXIT(r);
411}
412
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200413/**
Mario Six8fac2912018-07-10 08:40:17 +0200414 * efi_get_memory_map_ext() - get map describing memory usage
415 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
416 * on exit the size of the copied memory map
417 * @memory_map: buffer to which the memory map is written
418 * @map_key: key for the memory map
419 * @descriptor_size: size of an individual memory descriptor
420 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200421 *
422 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200423 *
Mario Six8fac2912018-07-10 08:40:17 +0200424 * See the Unified Extensible Firmware Interface (UEFI) specification for
425 * details.
426 *
427 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200428 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900429static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100430 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900431 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100432 efi_uintn_t *map_key,
433 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900434 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100435{
436 efi_status_t r;
437
438 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
439 map_key, descriptor_size, descriptor_version);
440 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
441 descriptor_size, descriptor_version);
442 return EFI_EXIT(r);
443}
444
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200445/**
Mario Six8fac2912018-07-10 08:40:17 +0200446 * efi_allocate_pool_ext() - allocate memory from pool
447 * @pool_type: type of the pool from which memory is to be allocated
448 * @size: number of bytes to be allocated
449 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200450 *
451 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200452 *
453 * See the Unified Extensible Firmware Interface (UEFI) specification for
454 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200455 *
Mario Six8fac2912018-07-10 08:40:17 +0200456 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200457 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200458static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100459 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200460 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100461{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100462 efi_status_t r;
463
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100464 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200465 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100466 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100467}
468
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200469/**
Mario Six8fac2912018-07-10 08:40:17 +0200470 * efi_free_pool_ext() - free memory from pool
471 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200472 *
473 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200474 *
Mario Six8fac2912018-07-10 08:40:17 +0200475 * See the Unified Extensible Firmware Interface (UEFI) specification for
476 * details.
477 *
478 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200479 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200480static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100481{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100482 efi_status_t r;
483
484 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200485 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100486 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100487}
488
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200489/**
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200490 * efi_add_handle() - add a new handle to the object list
491 *
492 * @handle: handle to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100493 *
Heinrich Schuchardt30cd0462019-05-01 09:42:39 +0200494 * The protocols list is initialized. The handle is added to the list of known
495 * UEFI objects.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100496 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200497void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100498{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200499 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100500 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200501 INIT_LIST_HEAD(&handle->protocols);
502 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100503}
504
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200505/**
Mario Six8fac2912018-07-10 08:40:17 +0200506 * efi_create_handle() - create handle
507 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200508 *
Mario Six8fac2912018-07-10 08:40:17 +0200509 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200510 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100511efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200512{
513 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200514
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200515 obj = calloc(1, sizeof(struct efi_object));
516 if (!obj)
517 return EFI_OUT_OF_RESOURCES;
518
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100519 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200520 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200521
522 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200523}
524
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200525/**
Mario Six8fac2912018-07-10 08:40:17 +0200526 * efi_search_protocol() - find a protocol on a handle.
527 * @handle: handle
528 * @protocol_guid: GUID of the protocol
529 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100530 *
Mario Six8fac2912018-07-10 08:40:17 +0200531 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100532 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100533efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100534 const efi_guid_t *protocol_guid,
535 struct efi_handler **handler)
536{
537 struct efi_object *efiobj;
538 struct list_head *lhandle;
539
540 if (!handle || !protocol_guid)
541 return EFI_INVALID_PARAMETER;
542 efiobj = efi_search_obj(handle);
543 if (!efiobj)
544 return EFI_INVALID_PARAMETER;
545 list_for_each(lhandle, &efiobj->protocols) {
546 struct efi_handler *protocol;
547
548 protocol = list_entry(lhandle, struct efi_handler, link);
549 if (!guidcmp(protocol->guid, protocol_guid)) {
550 if (handler)
551 *handler = protocol;
552 return EFI_SUCCESS;
553 }
554 }
555 return EFI_NOT_FOUND;
556}
557
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200558/**
Mario Six8fac2912018-07-10 08:40:17 +0200559 * efi_remove_protocol() - delete protocol from a handle
560 * @handle: handle from which the protocol shall be deleted
561 * @protocol: GUID of the protocol to be deleted
562 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100563 *
Mario Six8fac2912018-07-10 08:40:17 +0200564 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100565 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100566efi_status_t efi_remove_protocol(const efi_handle_t handle,
567 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100568 void *protocol_interface)
569{
570 struct efi_handler *handler;
571 efi_status_t ret;
572
573 ret = efi_search_protocol(handle, protocol, &handler);
574 if (ret != EFI_SUCCESS)
575 return ret;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200576 if (handler->protocol_interface != protocol_interface)
Heinrich Schuchardtfdeb1f02019-05-10 20:06:48 +0200577 return EFI_NOT_FOUND;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100578 list_del(&handler->link);
579 free(handler);
580 return EFI_SUCCESS;
581}
582
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200583/**
Mario Six8fac2912018-07-10 08:40:17 +0200584 * efi_remove_all_protocols() - delete all protocols from a handle
585 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100586 *
Mario Six8fac2912018-07-10 08:40:17 +0200587 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100588 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100589efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100590{
591 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100592 struct efi_handler *protocol;
593 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100594
595 efiobj = efi_search_obj(handle);
596 if (!efiobj)
597 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100598 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100599 efi_status_t ret;
600
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100601 ret = efi_remove_protocol(handle, protocol->guid,
602 protocol->protocol_interface);
603 if (ret != EFI_SUCCESS)
604 return ret;
605 }
606 return EFI_SUCCESS;
607}
608
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200609/**
Mario Six8fac2912018-07-10 08:40:17 +0200610 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100611 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200612 * @handle: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100613 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200614void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100615{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200616 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100617 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200618 efi_remove_all_protocols(handle);
619 list_del(&handle->link);
620 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100621}
622
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200623/**
Mario Six8fac2912018-07-10 08:40:17 +0200624 * efi_is_event() - check if a pointer is a valid event
625 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100626 *
Mario Six8fac2912018-07-10 08:40:17 +0200627 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100628 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100629static efi_status_t efi_is_event(const struct efi_event *event)
630{
631 const struct efi_event *evt;
632
633 if (!event)
634 return EFI_INVALID_PARAMETER;
635 list_for_each_entry(evt, &efi_events, link) {
636 if (evt == event)
637 return EFI_SUCCESS;
638 }
639 return EFI_INVALID_PARAMETER;
640}
Alexander Grafc15d9212016-03-04 01:09:59 +0100641
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200642/**
Mario Six8fac2912018-07-10 08:40:17 +0200643 * efi_create_event() - create an event
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +0200644 *
Mario Six8fac2912018-07-10 08:40:17 +0200645 * @type: type of the event to create
646 * @notify_tpl: task priority level of the event
647 * @notify_function: notification function of the event
648 * @notify_context: pointer passed to the notification function
649 * @group: event group
650 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200651 *
652 * This function is used inside U-Boot code to create an event.
653 *
654 * For the API function implementing the CreateEvent service see
655 * efi_create_event_ext.
656 *
Mario Six8fac2912018-07-10 08:40:17 +0200657 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200658 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100659efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200660 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200661 struct efi_event *event,
662 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100663 void *notify_context, efi_guid_t *group,
664 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100665{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100666 struct efi_event *evt;
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200667 efi_status_t ret;
668 int pool_type;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200669
Jonathan Gray7758b212017-03-12 19:26:07 +1100670 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200671 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100672
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200673 switch (type) {
674 case 0:
675 case EVT_TIMER:
676 case EVT_NOTIFY_SIGNAL:
677 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
678 case EVT_NOTIFY_WAIT:
679 case EVT_TIMER | EVT_NOTIFY_WAIT:
680 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200681 pool_type = EFI_BOOT_SERVICES_DATA;
682 break;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200683 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200684 pool_type = EFI_RUNTIME_SERVICES_DATA;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200685 break;
686 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200687 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200688 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100689
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100690 /*
691 * The UEFI specification requires event notification levels to be
692 * > TPL_APPLICATION and <= TPL_HIGH_LEVEL.
693 *
694 * Parameter NotifyTpl should not be checked if it is not used.
695 */
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900696 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
Heinrich Schuchardt3d83b732021-01-06 12:55:22 +0100697 (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS ||
698 notify_tpl == TPL_APPLICATION))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200699 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100700
Heinrich Schuchardt4429d872019-07-11 20:15:09 +0200701 ret = efi_allocate_pool(pool_type, sizeof(struct efi_event),
702 (void **)&evt);
703 if (ret != EFI_SUCCESS)
704 return ret;
705 memset(evt, 0, sizeof(struct efi_event));
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100706 evt->type = type;
707 evt->notify_tpl = notify_tpl;
708 evt->notify_function = notify_function;
709 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100710 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200711 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100712 evt->trigger_next = -1ULL;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100713 list_add_tail(&evt->link, &efi_events);
714 *event = evt;
715 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100716}
717
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200718/*
Mario Six8fac2912018-07-10 08:40:17 +0200719 * efi_create_event_ex() - create an event in a group
720 * @type: type of the event to create
721 * @notify_tpl: task priority level of the event
722 * @notify_function: notification function of the event
723 * @notify_context: pointer passed to the notification function
724 * @event: created event
725 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100726 *
727 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100728 *
Mario Six8fac2912018-07-10 08:40:17 +0200729 * See the Unified Extensible Firmware Interface (UEFI) specification for
730 * details.
731 *
732 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100733 */
734efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
735 void (EFIAPI *notify_function) (
736 struct efi_event *event,
737 void *context),
738 void *notify_context,
739 efi_guid_t *event_group,
740 struct efi_event **event)
741{
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200742 efi_status_t ret;
743
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100744 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
745 notify_context, event_group);
Heinrich Schuchardtce664992019-05-04 10:12:50 +0200746
747 /*
748 * The allowable input parameters are the same as in CreateEvent()
749 * except for the following two disallowed event types.
750 */
751 switch (type) {
752 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
753 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
754 ret = EFI_INVALID_PARAMETER;
755 goto out;
756 }
757
758 ret = efi_create_event(type, notify_tpl, notify_function,
759 notify_context, event_group, event);
760out:
761 return EFI_EXIT(ret);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100762}
763
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200764/**
Mario Six8fac2912018-07-10 08:40:17 +0200765 * efi_create_event_ext() - create an event
766 * @type: type of the event to create
767 * @notify_tpl: task priority level of the event
768 * @notify_function: notification function of the event
769 * @notify_context: pointer passed to the notification function
770 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200771 *
772 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200773 *
774 * See the Unified Extensible Firmware Interface (UEFI) specification for
775 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200776 *
Mario Six8fac2912018-07-10 08:40:17 +0200777 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200778 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200779static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100780 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200781 void (EFIAPI *notify_function) (
782 struct efi_event *event,
783 void *context),
784 void *notify_context, struct efi_event **event)
785{
786 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
787 notify_context);
788 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100789 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200790}
791
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200792/**
Mario Six8fac2912018-07-10 08:40:17 +0200793 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200794 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200795 * Check if a timer event has occurred or a queued notification function should
796 * be called.
797 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100798 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200799 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100800 */
801void efi_timer_check(void)
802{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100803 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100804 u64 now = timer_get_us();
805
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100806 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200807 if (!timers_enabled)
808 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100809 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200810 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100811 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200812 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100813 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200814 break;
815 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100816 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200817 break;
818 default:
819 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200820 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100821 evt->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200822 efi_signal_event(evt);
Alexander Grafc15d9212016-03-04 01:09:59 +0100823 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +0200824 efi_process_event_queue();
Alexander Grafc15d9212016-03-04 01:09:59 +0100825 WATCHDOG_RESET();
826}
827
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200828/**
Mario Six8fac2912018-07-10 08:40:17 +0200829 * efi_set_timer() - set the trigger time for a timer event or stop the event
830 * @event: event for which the timer is set
831 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200832 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200833 *
834 * This is the function for internal usage in U-Boot. For the API function
835 * implementing the SetTimer service see efi_set_timer_ext.
836 *
Mario Six8fac2912018-07-10 08:40:17 +0200837 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200838 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200839efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200840 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100841{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100842 /* Check that the event is valid */
843 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
844 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100845
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200846 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200847 * The parameter defines a multiple of 100 ns.
848 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200849 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200850 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100851
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100852 switch (type) {
853 case EFI_TIMER_STOP:
854 event->trigger_next = -1ULL;
855 break;
856 case EFI_TIMER_PERIODIC:
857 case EFI_TIMER_RELATIVE:
858 event->trigger_next = timer_get_us() + trigger_time;
859 break;
860 default:
861 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100862 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100863 event->trigger_type = type;
864 event->trigger_time = trigger_time;
865 event->is_signaled = false;
866 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100867}
868
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200869/**
Mario Six8fac2912018-07-10 08:40:17 +0200870 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
871 * event
872 * @event: event for which the timer is set
873 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200874 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200875 *
876 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200877 *
Mario Six8fac2912018-07-10 08:40:17 +0200878 * See the Unified Extensible Firmware Interface (UEFI) specification for
879 * details.
880 *
881 *
882 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200883 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200884static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
885 enum efi_timer_delay type,
886 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200887{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900888 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200889 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
890}
891
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200892/**
Mario Six8fac2912018-07-10 08:40:17 +0200893 * efi_wait_for_event() - wait for events to be signaled
894 * @num_events: number of events to be waited for
895 * @event: events to be waited for
896 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200897 *
898 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200899 *
Mario Six8fac2912018-07-10 08:40:17 +0200900 * See the Unified Extensible Firmware Interface (UEFI) specification for
901 * details.
902 *
903 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200904 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100905static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200906 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100907 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100908{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100909 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100910
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100911 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100912
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200913 /* Check parameters */
914 if (!num_events || !event)
915 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200916 /* Check TPL */
917 if (efi_tpl != TPL_APPLICATION)
918 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200919 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100920 if (efi_is_event(event[i]) != EFI_SUCCESS)
921 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200922 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
923 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200924 if (!event[i]->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200925 efi_queue_event(event[i]);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200926 }
927
928 /* Wait for signal */
929 for (;;) {
930 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200931 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200932 goto out;
933 }
934 /* Allow events to occur. */
935 efi_timer_check();
936 }
937
938out:
939 /*
940 * Reset the signal which is passed to the caller to allow periodic
941 * events to occur.
942 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200943 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200944 if (index)
945 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100946
947 return EFI_EXIT(EFI_SUCCESS);
948}
949
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200950/**
Mario Six8fac2912018-07-10 08:40:17 +0200951 * efi_signal_event_ext() - signal an EFI event
952 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200953 *
954 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200955 *
956 * See the Unified Extensible Firmware Interface (UEFI) specification for
957 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200958 *
959 * This functions sets the signaled state of the event and queues the
960 * notification function for execution.
961 *
Mario Six8fac2912018-07-10 08:40:17 +0200962 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200963 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200964static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100965{
966 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100967 if (efi_is_event(event) != EFI_SUCCESS)
968 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +0200969 efi_signal_event(event);
Alexander Grafc15d9212016-03-04 01:09:59 +0100970 return EFI_EXIT(EFI_SUCCESS);
971}
972
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200973/**
Mario Six8fac2912018-07-10 08:40:17 +0200974 * efi_close_event() - close an EFI event
975 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200976 *
977 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200978 *
Mario Six8fac2912018-07-10 08:40:17 +0200979 * See the Unified Extensible Firmware Interface (UEFI) specification for
980 * details.
981 *
982 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200983 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200984static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100985{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200986 struct efi_register_notify_event *item, *next;
987
Alexander Grafc15d9212016-03-04 01:09:59 +0100988 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100989 if (efi_is_event(event) != EFI_SUCCESS)
990 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +0200991
992 /* Remove protocol notify registrations for the event */
993 list_for_each_entry_safe(item, next, &efi_register_notify_events,
994 link) {
995 if (event == item->event) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +0200996 struct efi_protocol_notification *hitem, *hnext;
997
998 /* Remove signaled handles */
999 list_for_each_entry_safe(hitem, hnext, &item->handles,
1000 link) {
1001 list_del(&hitem->link);
1002 free(hitem);
1003 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001004 list_del(&item->link);
1005 free(item);
1006 }
1007 }
Heinrich Schuchardt35c0cf62019-06-05 21:00:39 +02001008 /* Remove event from queue */
1009 if (efi_event_is_queued(event))
1010 list_del(&event->queue_link);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001011
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001012 list_del(&event->link);
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02001013 efi_free_pool(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001014 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01001015}
1016
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001017/**
Mario Six8fac2912018-07-10 08:40:17 +02001018 * efi_check_event() - check if an event is signaled
1019 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001020 *
1021 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +02001022 *
1023 * See the Unified Extensible Firmware Interface (UEFI) specification for
1024 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001025 *
Mario Six8fac2912018-07-10 08:40:17 +02001026 * If an event is not signaled yet, the notification function is queued. The
1027 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001028 *
Mario Six8fac2912018-07-10 08:40:17 +02001029 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001030 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +02001031static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +01001032{
1033 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001034 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001035 if (efi_is_event(event) != EFI_SUCCESS ||
1036 event->type & EVT_NOTIFY_SIGNAL)
1037 return EFI_EXIT(EFI_INVALID_PARAMETER);
1038 if (!event->is_signaled)
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001039 efi_queue_event(event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001040 if (event->is_signaled) {
1041 event->is_signaled = false;
1042 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02001043 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001044 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +01001045}
1046
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001047/**
Mario Six8fac2912018-07-10 08:40:17 +02001048 * efi_search_obj() - find the internal EFI object for a handle
1049 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001050 *
Mario Six8fac2912018-07-10 08:40:17 +02001051 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001052 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001053struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001054{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001055 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001056
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02001057 if (!handle)
1058 return NULL;
1059
Heinrich Schuchardt274cc872017-11-06 21:17:50 +01001060 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001061 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001062 return efiobj;
1063 }
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +02001064 return NULL;
1065}
1066
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001067/**
Mario Six8fac2912018-07-10 08:40:17 +02001068 * efi_open_protocol_info_entry() - create open protocol info entry and add it
1069 * to a protocol
1070 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001071 *
Mario Six8fac2912018-07-10 08:40:17 +02001072 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001073 */
1074static struct efi_open_protocol_info_entry *efi_create_open_info(
1075 struct efi_handler *handler)
1076{
1077 struct efi_open_protocol_info_item *item;
1078
1079 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
1080 if (!item)
1081 return NULL;
1082 /* Append the item to the open protocol info list. */
1083 list_add_tail(&item->link, &handler->open_infos);
1084
1085 return &item->info;
1086}
1087
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001088/**
Mario Six8fac2912018-07-10 08:40:17 +02001089 * efi_delete_open_info() - remove an open protocol info entry from a protocol
1090 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001091 *
Mario Six8fac2912018-07-10 08:40:17 +02001092 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001093 */
1094static efi_status_t efi_delete_open_info(
1095 struct efi_open_protocol_info_item *item)
1096{
1097 list_del(&item->link);
1098 free(item);
1099 return EFI_SUCCESS;
1100}
1101
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001102/**
Mario Six8fac2912018-07-10 08:40:17 +02001103 * efi_add_protocol() - install new protocol on a handle
1104 * @handle: handle on which the protocol shall be installed
1105 * @protocol: GUID of the protocol to be installed
1106 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001107 *
Mario Six8fac2912018-07-10 08:40:17 +02001108 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001109 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001110efi_status_t efi_add_protocol(const efi_handle_t handle,
1111 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001112 void *protocol_interface)
1113{
1114 struct efi_object *efiobj;
1115 struct efi_handler *handler;
1116 efi_status_t ret;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001117 struct efi_register_notify_event *event;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001118
1119 efiobj = efi_search_obj(handle);
1120 if (!efiobj)
1121 return EFI_INVALID_PARAMETER;
1122 ret = efi_search_protocol(handle, protocol, NULL);
1123 if (ret != EFI_NOT_FOUND)
1124 return EFI_INVALID_PARAMETER;
1125 handler = calloc(1, sizeof(struct efi_handler));
1126 if (!handler)
1127 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001128 handler->guid = protocol;
1129 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +01001130 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001131 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001132
1133 /* Notify registered events */
1134 list_for_each_entry(event, &efi_register_notify_events, link) {
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001135 if (!guidcmp(protocol, &event->protocol)) {
1136 struct efi_protocol_notification *notif;
1137
1138 notif = calloc(1, sizeof(*notif));
1139 if (!notif) {
1140 list_del(&handler->link);
1141 free(handler);
1142 return EFI_OUT_OF_RESOURCES;
1143 }
1144 notif->handle = handle;
1145 list_add_tail(&notif->link, &event->handles);
Heinrich Schuchardtea0f6a82019-06-07 07:43:24 +02001146 event->event->is_signaled = false;
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001147 efi_signal_event(event->event);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001148 }
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001149 }
1150
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001151 if (!guidcmp(&efi_guid_device_path, protocol))
1152 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001153 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001154}
1155
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001156/**
Mario Six8fac2912018-07-10 08:40:17 +02001157 * efi_install_protocol_interface() - install protocol interface
1158 * @handle: handle on which the protocol shall be installed
1159 * @protocol: GUID of the protocol to be installed
1160 * @protocol_interface_type: type of the interface to be installed,
1161 * always EFI_NATIVE_INTERFACE
1162 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001163 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001164 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001165 *
Mario Six8fac2912018-07-10 08:40:17 +02001166 * See the Unified Extensible Firmware Interface (UEFI) specification for
1167 * details.
1168 *
1169 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001170 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001171static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001172 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001173 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001174{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001175 efi_status_t r;
1176
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001177 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1178 protocol_interface);
1179
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001180 if (!handle || !protocol ||
1181 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1182 r = EFI_INVALID_PARAMETER;
1183 goto out;
1184 }
1185
1186 /* Create new handle if requested. */
1187 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001188 r = efi_create_handle(handle);
1189 if (r != EFI_SUCCESS)
1190 goto out;
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001191 EFI_PRINT("new handle %p\n", *handle);
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001192 } else {
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02001193 EFI_PRINT("handle %p\n", *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001194 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001195 /* Add new protocol */
1196 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001197out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001198 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001199}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001200
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001201/**
Mario Six8fac2912018-07-10 08:40:17 +02001202 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001203 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001204 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001205 * @number_of_drivers: number of child controllers
1206 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001207 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001208 * The allocated buffer has to be freed with free().
1209 *
Mario Six8fac2912018-07-10 08:40:17 +02001210 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001211 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001212static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001213 const efi_guid_t *protocol,
1214 efi_uintn_t *number_of_drivers,
1215 efi_handle_t **driver_handle_buffer)
1216{
1217 struct efi_handler *handler;
1218 struct efi_open_protocol_info_item *item;
1219 efi_uintn_t count = 0, i;
1220 bool duplicate;
1221
1222 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001223 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001224 if (protocol && guidcmp(handler->guid, protocol))
1225 continue;
1226 list_for_each_entry(item, &handler->open_infos, link) {
1227 if (item->info.attributes &
1228 EFI_OPEN_PROTOCOL_BY_DRIVER)
1229 ++count;
1230 }
1231 }
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001232 *number_of_drivers = 0;
1233 if (!count) {
1234 *driver_handle_buffer = NULL;
1235 return EFI_SUCCESS;
1236 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001237 /*
1238 * Create buffer. In case of duplicate driver assignments the buffer
1239 * will be too large. But that does not harm.
1240 */
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001241 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1242 if (!*driver_handle_buffer)
1243 return EFI_OUT_OF_RESOURCES;
1244 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001245 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001246 if (protocol && guidcmp(handler->guid, protocol))
1247 continue;
1248 list_for_each_entry(item, &handler->open_infos, link) {
1249 if (item->info.attributes &
1250 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1251 /* Check this is a new driver */
1252 duplicate = false;
1253 for (i = 0; i < *number_of_drivers; ++i) {
1254 if ((*driver_handle_buffer)[i] ==
1255 item->info.agent_handle)
1256 duplicate = true;
1257 }
1258 /* Copy handle to buffer */
1259 if (!duplicate) {
1260 i = (*number_of_drivers)++;
1261 (*driver_handle_buffer)[i] =
1262 item->info.agent_handle;
1263 }
1264 }
1265 }
1266 }
1267 return EFI_SUCCESS;
1268}
1269
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001270/**
Mario Six8fac2912018-07-10 08:40:17 +02001271 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001272 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001273 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001274 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001275 *
1276 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001277 *
1278 * See the Unified Extensible Firmware Interface (UEFI) specification for
1279 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001280 *
Mario Six8fac2912018-07-10 08:40:17 +02001281 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001282 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001283static efi_status_t efi_disconnect_all_drivers
1284 (efi_handle_t handle,
1285 const efi_guid_t *protocol,
1286 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001287{
1288 efi_uintn_t number_of_drivers;
1289 efi_handle_t *driver_handle_buffer;
1290 efi_status_t r, ret;
1291
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001292 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001293 &driver_handle_buffer);
1294 if (ret != EFI_SUCCESS)
1295 return ret;
Heinrich Schuchardt7416aef2019-06-02 01:43:33 +02001296 if (!number_of_drivers)
1297 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001298 ret = EFI_NOT_FOUND;
1299 while (number_of_drivers) {
1300 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001301 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001302 driver_handle_buffer[--number_of_drivers],
1303 child_handle));
1304 if (r == EFI_SUCCESS)
1305 ret = r;
1306 }
1307 free(driver_handle_buffer);
1308 return ret;
1309}
1310
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001311/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001312 * efi_uninstall_protocol() - uninstall protocol interface
1313 *
Mario Six8fac2912018-07-10 08:40:17 +02001314 * @handle: handle from which the protocol shall be removed
1315 * @protocol: GUID of the protocol to be removed
1316 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001317 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001318 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001319 *
1320 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001321 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001322static efi_status_t efi_uninstall_protocol
1323 (efi_handle_t handle, const efi_guid_t *protocol,
1324 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001325{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001326 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001327 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001328 struct efi_open_protocol_info_item *item;
1329 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001330 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001331
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001332 /* Check handle */
1333 efiobj = efi_search_obj(handle);
1334 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001335 r = EFI_INVALID_PARAMETER;
1336 goto out;
1337 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001338 /* Find the protocol on the handle */
1339 r = efi_search_protocol(handle, protocol, &handler);
1340 if (r != EFI_SUCCESS)
1341 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001342 /* Disconnect controllers */
1343 efi_disconnect_all_drivers(efiobj, protocol, NULL);
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001344 /* Close protocol */
1345 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1346 if (item->info.attributes ==
1347 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1348 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1349 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1350 list_del(&item->link);
1351 }
1352 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001353 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001354 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001355 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001356 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001357out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001358 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001359}
1360
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001361/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001362 * efi_uninstall_protocol_interface() - uninstall protocol interface
1363 * @handle: handle from which the protocol shall be removed
1364 * @protocol: GUID of the protocol to be removed
1365 * @protocol_interface: interface to be removed
1366 *
1367 * This function implements the UninstallProtocolInterface service.
1368 *
1369 * See the Unified Extensible Firmware Interface (UEFI) specification for
1370 * details.
1371 *
1372 * Return: status code
1373 */
1374static efi_status_t EFIAPI efi_uninstall_protocol_interface
1375 (efi_handle_t handle, const efi_guid_t *protocol,
1376 void *protocol_interface)
1377{
1378 efi_status_t ret;
1379
1380 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1381
1382 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1383 if (ret != EFI_SUCCESS)
1384 goto out;
1385
1386 /* If the last protocol has been removed, delete the handle. */
1387 if (list_empty(&handle->protocols)) {
1388 list_del(&handle->link);
1389 free(handle);
1390 }
1391out:
1392 return EFI_EXIT(ret);
1393}
1394
1395/**
Mario Six8fac2912018-07-10 08:40:17 +02001396 * efi_register_protocol_notify() - register an event for notification when a
1397 * protocol is installed.
1398 * @protocol: GUID of the protocol whose installation shall be notified
1399 * @event: event to be signaled upon installation of the protocol
1400 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001401 *
1402 * This function implements the RegisterProtocolNotify service.
1403 * See the Unified Extensible Firmware Interface (UEFI) specification
1404 * for details.
1405 *
Mario Six8fac2912018-07-10 08:40:17 +02001406 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001407 */
Jose Marinhoebb61ee2021-03-02 17:26:38 +00001408efi_status_t EFIAPI efi_register_protocol_notify(const efi_guid_t *protocol,
1409 struct efi_event *event,
1410 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001411{
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001412 struct efi_register_notify_event *item;
1413 efi_status_t ret = EFI_SUCCESS;
1414
Rob Clark238f88c2017-09-13 18:05:41 -04001415 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001416
1417 if (!protocol || !event || !registration) {
1418 ret = EFI_INVALID_PARAMETER;
1419 goto out;
1420 }
1421
1422 item = calloc(1, sizeof(struct efi_register_notify_event));
1423 if (!item) {
1424 ret = EFI_OUT_OF_RESOURCES;
1425 goto out;
1426 }
1427
1428 item->event = event;
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301429 guidcpy(&item->protocol, protocol);
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001430 INIT_LIST_HEAD(&item->handles);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001431
1432 list_add_tail(&item->link, &efi_register_notify_events);
1433
1434 *registration = item;
1435out:
1436 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001437}
1438
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001439/**
Mario Six8fac2912018-07-10 08:40:17 +02001440 * efi_search() - determine if an EFI handle implements a protocol
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001441 *
Mario Six8fac2912018-07-10 08:40:17 +02001442 * @search_type: selection criterion
1443 * @protocol: GUID of the protocol
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001444 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001445 *
1446 * See the documentation of the LocateHandle service in the UEFI specification.
1447 *
Mario Six8fac2912018-07-10 08:40:17 +02001448 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001449 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001450static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001451 const efi_guid_t *protocol, efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001452{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001453 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001454
1455 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001456 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001457 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001458 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001459 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001460 return (ret != EFI_SUCCESS);
1461 default:
1462 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001463 return -1;
1464 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001465}
1466
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001467/**
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001468 * efi_check_register_notify_event() - check if registration key is valid
1469 *
1470 * Check that a pointer is a valid registration key as returned by
1471 * RegisterProtocolNotify().
1472 *
1473 * @key: registration key
1474 * Return: valid registration key or NULL
1475 */
1476static struct efi_register_notify_event *efi_check_register_notify_event
1477 (void *key)
1478{
1479 struct efi_register_notify_event *event;
1480
1481 list_for_each_entry(event, &efi_register_notify_events, link) {
1482 if (event == (struct efi_register_notify_event *)key)
1483 return event;
1484 }
1485 return NULL;
1486}
1487
1488/**
Mario Six8fac2912018-07-10 08:40:17 +02001489 * efi_locate_handle() - locate handles implementing a protocol
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001490 *
1491 * @search_type: selection criterion
1492 * @protocol: GUID of the protocol
1493 * @search_key: registration key
1494 * @buffer_size: size of the buffer to receive the handles in bytes
1495 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001496 *
1497 * This function is meant for U-Boot internal calls. For the API implementation
1498 * of the LocateHandle service see efi_locate_handle_ext.
1499 *
Mario Six8fac2912018-07-10 08:40:17 +02001500 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001501 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001502static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001503 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001504 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001505 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001506{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001507 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001508 efi_uintn_t size = 0;
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001509 struct efi_register_notify_event *event;
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001510 struct efi_protocol_notification *handle = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001511
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001512 /* Check parameters */
1513 switch (search_type) {
1514 case ALL_HANDLES:
1515 break;
1516 case BY_REGISTER_NOTIFY:
1517 if (!search_key)
1518 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001519 /* Check that the registration key is valid */
Heinrich Schuchardteb616522019-05-29 07:46:33 +02001520 event = efi_check_register_notify_event(search_key);
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001521 if (!event)
1522 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt6eec42d2019-05-04 17:27:54 +02001523 break;
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001524 case BY_PROTOCOL:
1525 if (!protocol)
1526 return EFI_INVALID_PARAMETER;
1527 break;
1528 default:
1529 return EFI_INVALID_PARAMETER;
1530 }
1531
Alexander Grafc15d9212016-03-04 01:09:59 +01001532 /* Count how much space we need */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001533 if (search_type == BY_REGISTER_NOTIFY) {
1534 if (list_empty(&event->handles))
1535 return EFI_NOT_FOUND;
1536 handle = list_first_entry(&event->handles,
1537 struct efi_protocol_notification,
1538 link);
1539 efiobj = handle->handle;
1540 size += sizeof(void *);
1541 } else {
1542 list_for_each_entry(efiobj, &efi_obj_list, link) {
1543 if (!efi_search(search_type, protocol, efiobj))
1544 size += sizeof(void *);
1545 }
1546 if (size == 0)
1547 return EFI_NOT_FOUND;
Alexander Grafc15d9212016-03-04 01:09:59 +01001548 }
1549
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001550 if (!buffer_size)
1551 return EFI_INVALID_PARAMETER;
1552
Alexander Grafc15d9212016-03-04 01:09:59 +01001553 if (*buffer_size < size) {
1554 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001555 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001556 }
1557
Rob Clarkcdee3372017-08-06 14:10:07 -04001558 *buffer_size = size;
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001559
Heinrich Schuchardtc97f9472019-05-10 19:03:49 +02001560 /* The buffer size is sufficient but there is no buffer */
Heinrich Schuchardtc0ee5f32019-05-04 17:37:32 +02001561 if (!buffer)
1562 return EFI_INVALID_PARAMETER;
Rob Clarkcdee3372017-08-06 14:10:07 -04001563
Alexander Grafc15d9212016-03-04 01:09:59 +01001564 /* Then fill the array */
Heinrich Schuchardtd375e752019-05-21 18:19:01 +02001565 if (search_type == BY_REGISTER_NOTIFY) {
1566 *buffer = efiobj;
1567 list_del(&handle->link);
1568 } else {
1569 list_for_each_entry(efiobj, &efi_obj_list, link) {
1570 if (!efi_search(search_type, protocol, efiobj))
1571 *buffer++ = efiobj;
1572 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001573 }
1574
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001575 return EFI_SUCCESS;
1576}
1577
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001578/**
Mario Six8fac2912018-07-10 08:40:17 +02001579 * efi_locate_handle_ext() - locate handles implementing a protocol.
1580 * @search_type: selection criterion
1581 * @protocol: GUID of the protocol
1582 * @search_key: registration key
1583 * @buffer_size: size of the buffer to receive the handles in bytes
1584 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001585 *
1586 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001587 *
Mario Six8fac2912018-07-10 08:40:17 +02001588 * See the Unified Extensible Firmware Interface (UEFI) specification for
1589 * details.
1590 *
1591 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001592 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001593static efi_status_t EFIAPI efi_locate_handle_ext(
1594 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001595 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001596 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001597{
Rob Clark238f88c2017-09-13 18:05:41 -04001598 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001599 buffer_size, buffer);
1600
1601 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1602 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001603}
1604
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001605/**
Mario Six8fac2912018-07-10 08:40:17 +02001606 * efi_remove_configuration_table() - collapses configuration table entries,
1607 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001608 *
Mario Six8fac2912018-07-10 08:40:17 +02001609 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001610 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001611static void efi_remove_configuration_table(int i)
1612{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001613 struct efi_configuration_table *this = &systab.tables[i];
1614 struct efi_configuration_table *next = &systab.tables[i + 1];
1615 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001616
1617 memmove(this, next, (ulong)end - (ulong)next);
1618 systab.nr_tables--;
1619}
1620
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001621/**
Mario Six8fac2912018-07-10 08:40:17 +02001622 * efi_install_configuration_table() - adds, updates, or removes a
1623 * configuration table
1624 * @guid: GUID of the installed table
1625 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001626 *
1627 * This function is used for internal calls. For the API implementation of the
1628 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1629 *
Mario Six8fac2912018-07-10 08:40:17 +02001630 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001631 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001632efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1633 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001634{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001635 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001636 int i;
1637
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001638 if (!guid)
1639 return EFI_INVALID_PARAMETER;
1640
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001641 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001642 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001643 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001644 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001645 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001646 else
1647 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001648 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001649 }
1650 }
1651
Alexander Graffe3366f2017-07-26 13:41:04 +02001652 if (!table)
1653 return EFI_NOT_FOUND;
1654
Alexander Grafc15d9212016-03-04 01:09:59 +01001655 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001656 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001657 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001658
1659 /* Add a new entry */
Sughosh Ganu196f66d2019-12-29 00:01:04 +05301660 guidcpy(&systab.tables[i].guid, guid);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001661 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001662 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001663
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001664out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001665 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001666 efi_update_table_header_crc32(&systab.hdr);
1667
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001668 /* Notify that the configuration table was changed */
1669 list_for_each_entry(evt, &efi_events, link) {
1670 if (evt->group && !guidcmp(evt->group, guid)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02001671 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001672 break;
1673 }
1674 }
1675
Alexander Grafc5c11632016-08-19 01:23:24 +02001676 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001677}
1678
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001679/**
Mario Six8fac2912018-07-10 08:40:17 +02001680 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1681 * configuration table.
1682 * @guid: GUID of the installed table
1683 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001684 *
1685 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001686 *
Mario Six8fac2912018-07-10 08:40:17 +02001687 * See the Unified Extensible Firmware Interface (UEFI) specification for
1688 * details.
1689 *
1690 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001691 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001692static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1693 void *table)
1694{
Rob Clark238f88c2017-09-13 18:05:41 -04001695 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001696 return EFI_EXIT(efi_install_configuration_table(guid, table));
1697}
1698
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001699/**
Mario Six8fac2912018-07-10 08:40:17 +02001700 * efi_setup_loaded_image() - initialize a loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001701 *
1702 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001703 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001704 *
Heinrich Schuchardt41cc3c12019-07-14 11:05:34 +02001705 * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001706 * code is returned.
1707 *
1708 * @device_path: device path of the loaded image
1709 * @file_path: file path of the loaded image
1710 * @handle_ptr: handle of the loaded image
1711 * @info_ptr: loaded image protocol
1712 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001713 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001714efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1715 struct efi_device_path *file_path,
1716 struct efi_loaded_image_obj **handle_ptr,
1717 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001718{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001719 efi_status_t ret;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001720 struct efi_loaded_image *info = NULL;
1721 struct efi_loaded_image_obj *obj = NULL;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001722 struct efi_device_path *dp;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001723
1724 /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */
1725 *handle_ptr = NULL;
1726 *info_ptr = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001727
1728 info = calloc(1, sizeof(*info));
1729 if (!info)
1730 return EFI_OUT_OF_RESOURCES;
1731 obj = calloc(1, sizeof(*obj));
1732 if (!obj) {
1733 free(info);
1734 return EFI_OUT_OF_RESOURCES;
1735 }
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02001736 obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001737
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001738 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001739 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001740
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001741 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001742 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001743 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001744
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001745 if (device_path) {
1746 info->device_handle = efi_dp_find_obj(device_path, NULL);
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001747
1748 dp = efi_dp_append(device_path, file_path);
1749 if (!dp) {
1750 ret = EFI_OUT_OF_RESOURCES;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001751 goto failure;
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001752 }
1753 } else {
1754 dp = NULL;
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001755 }
AKASHI Takahirobbe13da2019-03-27 13:40:32 +09001756 ret = efi_add_protocol(&obj->header,
1757 &efi_guid_loaded_image_device_path, dp);
1758 if (ret != EFI_SUCCESS)
1759 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001760
1761 /*
1762 * When asking for the loaded_image interface, just
1763 * return handle which points to loaded_image_info
1764 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001765 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001766 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001767 if (ret != EFI_SUCCESS)
1768 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001769
Heinrich Schuchardtd20f5122019-03-19 18:58:58 +01001770 *info_ptr = info;
1771 *handle_ptr = obj;
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001772
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001773 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001774failure:
1775 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt6346a902019-02-06 19:41:29 +01001776 efi_delete_handle(&obj->header);
1777 free(info);
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001778 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001779}
1780
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001781/**
Heinrich Schuchardt1d59a572020-12-04 03:02:03 +01001782 * efi_locate_device_path() - Get the device path and handle of an device
1783 * implementing a protocol
1784 * @protocol: GUID of the protocol
1785 * @device_path: device path
1786 * @device: handle of the device
1787 *
1788 * This function implements the LocateDevicePath service.
1789 *
1790 * See the Unified Extensible Firmware Interface (UEFI) specification for
1791 * details.
1792 *
1793 * Return: status code
1794 */
1795static efi_status_t EFIAPI efi_locate_device_path(
1796 const efi_guid_t *protocol,
1797 struct efi_device_path **device_path,
1798 efi_handle_t *device)
1799{
1800 struct efi_device_path *dp;
1801 size_t i;
1802 struct efi_handler *handler;
1803 efi_handle_t *handles;
1804 size_t len, len_dp;
1805 size_t len_best = 0;
1806 efi_uintn_t no_handles;
1807 u8 *remainder;
1808 efi_status_t ret;
1809
1810 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
1811
1812 if (!protocol || !device_path || !*device_path) {
1813 ret = EFI_INVALID_PARAMETER;
1814 goto out;
1815 }
1816
1817 /* Find end of device path */
1818 len = efi_dp_instance_size(*device_path);
1819
1820 /* Get all handles implementing the protocol */
1821 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
1822 &no_handles, &handles));
1823 if (ret != EFI_SUCCESS)
1824 goto out;
1825
1826 for (i = 0; i < no_handles; ++i) {
1827 /* Find the device path protocol */
1828 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
1829 &handler);
1830 if (ret != EFI_SUCCESS)
1831 continue;
1832 dp = (struct efi_device_path *)handler->protocol_interface;
1833 len_dp = efi_dp_instance_size(dp);
1834 /*
1835 * This handle can only be a better fit
1836 * if its device path length is longer than the best fit and
1837 * if its device path length is shorter of equal the searched
1838 * device path.
1839 */
1840 if (len_dp <= len_best || len_dp > len)
1841 continue;
1842 /* Check if dp is a subpath of device_path */
1843 if (memcmp(*device_path, dp, len_dp))
1844 continue;
1845 if (!device) {
1846 ret = EFI_INVALID_PARAMETER;
1847 goto out;
1848 }
1849 *device = handles[i];
1850 len_best = len_dp;
1851 }
1852 if (len_best) {
1853 remainder = (u8 *)*device_path + len_best;
1854 *device_path = (struct efi_device_path *)remainder;
1855 ret = EFI_SUCCESS;
1856 } else {
1857 ret = EFI_NOT_FOUND;
1858 }
1859out:
1860 return EFI_EXIT(ret);
1861}
1862
1863/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001864 * efi_load_image_from_file() - load an image from file system
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001865 *
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001866 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1867 * callers obligation to update the memory type as needed.
1868 *
Heinrich Schuchardt471db442020-12-04 09:27:41 +01001869 * @file_path: the path of the image to load
1870 * @buffer: buffer containing the loaded image
1871 * @size: size of the loaded image
1872 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001873 */
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09001874static
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001875efi_status_t efi_load_image_from_file(struct efi_device_path *file_path,
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001876 void **buffer, efi_uintn_t *size)
Rob Clark857a1222017-09-13 18:05:35 -04001877{
Rob Clark857a1222017-09-13 18:05:35 -04001878 struct efi_file_handle *f;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001879 efi_status_t ret;
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001880 u64 addr;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001881 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001882
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001883 /* Open file */
Rob Clark857a1222017-09-13 18:05:35 -04001884 f = efi_file_from_path(file_path);
1885 if (!f)
Heinrich Schuchardt6a41cf22019-06-11 19:00:56 +02001886 return EFI_NOT_FOUND;
Rob Clark857a1222017-09-13 18:05:35 -04001887
Ilias Apalodimas03d0d762021-03-25 21:55:16 +02001888 ret = efi_file_size(f, &bs);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001889 if (ret != EFI_SUCCESS)
Rob Clark857a1222017-09-13 18:05:35 -04001890 goto error;
1891
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001892 /*
1893 * When reading the file we do not yet know if it contains an
1894 * application, a boottime driver, or a runtime driver. So here we
1895 * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to
1896 * update the reservation according to the image type.
1897 */
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001898 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
1899 EFI_BOOT_SERVICES_DATA,
1900 efi_size_in_pages(bs), &addr);
Rob Clark857a1222017-09-13 18:05:35 -04001901 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001902 ret = EFI_OUT_OF_RESOURCES;
1903 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001904 }
1905
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01001906 /* Read file */
1907 EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr));
1908 if (ret != EFI_SUCCESS)
1909 efi_free_pages(addr, efi_size_in_pages(bs));
1910 *buffer = (void *)(uintptr_t)addr;
1911 *size = bs;
1912error:
1913 EFI_CALL(f->close(f));
Rob Clark857a1222017-09-13 18:05:35 -04001914 return ret;
1915}
1916
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001917/**
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001918 * efi_load_image_from_path() - load an image using a file path
1919 *
1920 * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the
1921 * callers obligation to update the memory type as needed.
1922 *
1923 * @boot_policy: true for request originating from the boot manager
1924 * @file_path: the path of the image to load
1925 * @buffer: buffer containing the loaded image
1926 * @size: size of the loaded image
1927 * Return: status code
1928 */
1929static
1930efi_status_t efi_load_image_from_path(bool boot_policy,
1931 struct efi_device_path *file_path,
1932 void **buffer, efi_uintn_t *size)
1933{
1934 efi_handle_t device;
1935 efi_status_t ret;
1936 struct efi_device_path *dp;
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001937 struct efi_load_file_protocol *load_file_protocol = NULL;
1938 efi_uintn_t buffer_size;
1939 uint64_t addr, pages;
1940 const efi_guid_t *guid;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001941
1942 /* In case of failure nothing is returned */
1943 *buffer = NULL;
1944 *size = 0;
1945
1946 dp = file_path;
1947 ret = EFI_CALL(efi_locate_device_path(
1948 &efi_simple_file_system_protocol_guid, &dp, &device));
1949 if (ret == EFI_SUCCESS)
1950 return efi_load_image_from_file(file_path, buffer, size);
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001951
1952 ret = EFI_CALL(efi_locate_device_path(
1953 &efi_guid_load_file_protocol, &dp, &device));
1954 if (ret == EFI_SUCCESS) {
1955 guid = &efi_guid_load_file_protocol;
1956 } else if (!boot_policy) {
1957 guid = &efi_guid_load_file2_protocol;
1958 ret = EFI_CALL(efi_locate_device_path(guid, &dp, &device));
1959 }
1960 if (ret != EFI_SUCCESS)
1961 return EFI_NOT_FOUND;
1962 ret = EFI_CALL(efi_handle_protocol(device, guid,
1963 (void **)&load_file_protocol));
1964 if (ret != EFI_SUCCESS)
1965 return EFI_NOT_FOUND;
1966 buffer_size = 0;
1967 ret = load_file_protocol->load_file(load_file_protocol, dp,
1968 boot_policy, &buffer_size,
1969 NULL);
1970 if (ret != EFI_BUFFER_TOO_SMALL)
1971 goto out;
1972 pages = efi_size_in_pages(buffer_size);
1973 ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, EFI_BOOT_SERVICES_DATA,
1974 pages, &addr);
1975 if (ret != EFI_SUCCESS) {
1976 ret = EFI_OUT_OF_RESOURCES;
1977 goto out;
1978 }
1979 ret = EFI_CALL(load_file_protocol->load_file(
1980 load_file_protocol, dp, boot_policy,
1981 &buffer_size, (void *)(uintptr_t)addr));
1982 if (ret != EFI_SUCCESS)
1983 efi_free_pages(addr, pages);
1984out:
Heinrich Schuchardt20d88c72021-01-20 22:57:46 +01001985 EFI_CALL(efi_close_protocol(device, guid, efi_root, NULL));
Heinrich Schuchardt82608272020-12-06 13:00:15 +01001986 if (ret == EFI_SUCCESS) {
1987 *buffer = (void *)(uintptr_t)addr;
1988 *size = buffer_size;
1989 }
1990
1991 return ret;
Heinrich Schuchardt3dd733e2020-12-06 10:47:57 +01001992}
1993
1994/**
Mario Six8fac2912018-07-10 08:40:17 +02001995 * efi_load_image() - load an EFI image into memory
1996 * @boot_policy: true for request originating from the boot manager
1997 * @parent_image: the caller's image handle
1998 * @file_path: the path of the image to load
1999 * @source_buffer: memory location from which the image is installed
2000 * @source_size: size of the memory area from which the image is installed
2001 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002002 *
2003 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02002004 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002005 * See the Unified Extensible Firmware Interface (UEFI) specification
2006 * for details.
2007 *
Mario Six8fac2912018-07-10 08:40:17 +02002008 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002009 */
AKASHI Takahirob0ad9052019-03-05 14:53:31 +09002010efi_status_t EFIAPI efi_load_image(bool boot_policy,
2011 efi_handle_t parent_image,
2012 struct efi_device_path *file_path,
2013 void *source_buffer,
2014 efi_uintn_t source_size,
2015 efi_handle_t *image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002016{
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002017 struct efi_device_path *dp, *fp;
Tom Rini04c49062018-09-30 10:38:15 -04002018 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02002019 struct efi_loaded_image_obj **image_obj =
2020 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002021 efi_status_t ret;
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002022 void *dest_buffer;
Alexander Grafc15d9212016-03-04 01:09:59 +01002023
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02002024 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01002025 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04002026
Heinrich Schuchardtb7b10112019-06-11 18:52:33 +02002027 if (!image_handle || (!source_buffer && !file_path) ||
2028 !efi_search_obj(parent_image) ||
2029 /* The parent image handle must refer to a loaded image */
2030 !parent_image->type) {
Heinrich Schuchardt2e0a7902019-05-05 16:55:06 +02002031 ret = EFI_INVALID_PARAMETER;
2032 goto error;
2033 }
Rob Clark857a1222017-09-13 18:05:35 -04002034
2035 if (!source_buffer) {
Heinrich Schuchardt471db442020-12-04 09:27:41 +01002036 ret = efi_load_image_from_path(boot_policy, file_path,
2037 &dest_buffer, &source_size);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002038 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002039 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04002040 } else {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002041 dest_buffer = source_buffer;
Rob Clark857a1222017-09-13 18:05:35 -04002042 }
Heinrich Schuchardt28b39172019-04-20 19:24:43 +00002043 /* split file_path which contains both the device and file parts */
2044 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardte3d3a0c2018-12-24 09:19:07 +01002045 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002046 if (ret == EFI_SUCCESS)
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002047 ret = efi_load_pe(*image_obj, dest_buffer, source_size, info);
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002048 if (!source_buffer)
2049 /* Release buffer to which file was loaded */
2050 efi_free_pages((uintptr_t)dest_buffer,
2051 efi_size_in_pages(source_size));
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002052 if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) {
Heinrich Schuchardt47e35a92019-03-04 17:50:05 +01002053 info->system_table = &systab;
2054 info->parent_handle = parent_image;
2055 } else {
2056 /* The image is invalid. Release all associated resources. */
2057 efi_delete_handle(*image_handle);
2058 *image_handle = NULL;
2059 free(info);
2060 }
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01002061error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01002062 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002063}
2064
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002065/**
Alexander Graffa5246b2018-11-15 21:23:47 +01002066 * efi_exit_caches() - fix up caches for EFI payloads if necessary
2067 */
2068static void efi_exit_caches(void)
2069{
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002070#if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND)
Alexander Graffa5246b2018-11-15 21:23:47 +01002071 /*
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002072 * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if
2073 * caches are enabled.
2074 *
2075 * TODO:
2076 * According to the UEFI spec caches that can be managed via CP15
2077 * operations should be enabled. Caches requiring platform information
2078 * to manage should be disabled. This should not happen in
2079 * ExitBootServices() but before invoking any UEFI binary is invoked.
2080 *
2081 * We want to keep the current workaround while GRUB prior to version
2082 * 2.04 is still in use.
Alexander Graffa5246b2018-11-15 21:23:47 +01002083 */
Heinrich Schuchardt149d5d42019-07-22 22:04:36 +02002084 cleanup_before_linux();
Alexander Graffa5246b2018-11-15 21:23:47 +01002085#endif
2086}
2087
2088/**
Mario Six8fac2912018-07-10 08:40:17 +02002089 * efi_exit_boot_services() - stop all boot services
2090 * @image_handle: handle of the loaded image
2091 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002092 *
2093 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02002094 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002095 * See the Unified Extensible Firmware Interface (UEFI) specification
2096 * for details.
2097 *
Mario Six8fac2912018-07-10 08:40:17 +02002098 * All timer events are disabled. For exit boot services events the
2099 * notification function is called. The boot services are disabled in the
2100 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002101 *
Mario Six8fac2912018-07-10 08:40:17 +02002102 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002103 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002104static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002105 efi_uintn_t map_key)
Alexander Grafc15d9212016-03-04 01:09:59 +01002106{
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002107 struct efi_event *evt, *next_event;
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002108 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002109
Heinrich Schuchardt8bd4f412019-05-05 21:58:35 +02002110 EFI_ENTRY("%p, %zx", image_handle, map_key);
Alexander Grafc15d9212016-03-04 01:09:59 +01002111
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002112 /* Check that the caller has read the current memory map */
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002113 if (map_key != efi_memory_map_key) {
2114 ret = EFI_INVALID_PARAMETER;
2115 goto out;
2116 }
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02002117
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002118 /* Check if ExitBootServices has already been called */
2119 if (!systab.boottime)
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002120 goto out;
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002121
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002122 /* Stop all timer related activities */
2123 timers_enabled = false;
2124
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002125 /* Add related events to the event group */
2126 list_for_each_entry(evt, &efi_events, link) {
2127 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
2128 evt->group = &efi_guid_event_group_exit_boot_services;
2129 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002130 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01002131 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002132 if (evt->group &&
2133 !guidcmp(evt->group,
2134 &efi_guid_event_group_exit_boot_services)) {
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002135 efi_signal_event(evt);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01002136 break;
2137 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002138 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02002139
Heinrich Schuchardt7b4b8d862019-06-07 06:47:01 +02002140 /* Make sure that notification functions are not called anymore */
2141 efi_tpl = TPL_HIGH_LEVEL;
2142
Heinrich Schuchardt2ac62582019-06-20 15:25:48 +02002143 /* Notify variable services */
2144 efi_variables_boot_exit_notify();
Rob Clark15f3d742017-09-13 18:05:37 -04002145
Heinrich Schuchardt4429d872019-07-11 20:15:09 +02002146 /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */
2147 list_for_each_entry_safe(evt, next_event, &efi_events, link) {
2148 if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE)
2149 list_del(&evt->link);
2150 }
2151
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002152 if (!efi_st_keep_devices) {
Heinrich Schuchardt20dbedb2020-12-27 15:33:09 +01002153 if (IS_ENABLED(CONFIG_USB_DEVICE))
Heinrich Schuchardt628dbd72020-11-12 21:26:28 +01002154 udc_disconnect();
2155 board_quiesce_devices();
2156 dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL);
2157 }
Alexander Graf2ebeb442016-11-17 01:02:57 +01002158
Heinrich Schuchardt1943dbb2019-07-05 17:42:16 +02002159 /* Patch out unsupported runtime function */
2160 efi_runtime_detach();
2161
Alexander Graffa5246b2018-11-15 21:23:47 +01002162 /* Fix up caches for EFI payloads if necessary */
2163 efi_exit_caches();
2164
Alexander Grafc15d9212016-03-04 01:09:59 +01002165 /* This stops all lingering devices */
2166 bootm_disable_interrupts();
2167
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002168 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002169 systab.con_in_handle = NULL;
2170 systab.con_in = NULL;
2171 systab.con_out_handle = NULL;
2172 systab.con_out = NULL;
2173 systab.stderr_handle = NULL;
2174 systab.std_err = NULL;
2175 systab.boottime = NULL;
2176
2177 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02002178 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01002179
Alexander Grafc15d9212016-03-04 01:09:59 +01002180 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002181 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01002182 WATCHDOG_RESET();
Heinrich Schuchardtafc33672019-06-11 20:05:40 +02002183out:
2184 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002185}
2186
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002187/**
Mario Six8fac2912018-07-10 08:40:17 +02002188 * efi_get_next_monotonic_count() - get next value of the counter
2189 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002190 *
2191 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02002192 *
2193 * See the Unified Extensible Firmware Interface (UEFI) specification for
2194 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002195 *
Mario Six8fac2912018-07-10 08:40:17 +02002196 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002197 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002198static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
2199{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002200 static uint64_t mono;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002201 efi_status_t ret;
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002202
Alexander Grafc15d9212016-03-04 01:09:59 +01002203 EFI_ENTRY("%p", count);
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002204 if (!count) {
2205 ret = EFI_INVALID_PARAMETER;
2206 goto out;
2207 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002208 *count = mono++;
Heinrich Schuchardtddc787d2019-05-17 12:47:17 +02002209 ret = EFI_SUCCESS;
2210out:
2211 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002212}
2213
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002214/**
Mario Six8fac2912018-07-10 08:40:17 +02002215 * efi_stall() - sleep
2216 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002217 *
Mario Six8fac2912018-07-10 08:40:17 +02002218 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002219 *
Mario Six8fac2912018-07-10 08:40:17 +02002220 * See the Unified Extensible Firmware Interface (UEFI) specification for
2221 * details.
2222 *
2223 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002224 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002225static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
2226{
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002227 u64 end_tick;
2228
Alexander Grafc15d9212016-03-04 01:09:59 +01002229 EFI_ENTRY("%ld", microseconds);
Heinrich Schuchardt9912c032019-06-02 21:12:17 +02002230
2231 end_tick = get_ticks() + usec_to_tick(microseconds);
2232 while (get_ticks() < end_tick)
2233 efi_timer_check();
2234
Alexander Grafc15d9212016-03-04 01:09:59 +01002235 return EFI_EXIT(EFI_SUCCESS);
2236}
2237
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002238/**
Mario Six8fac2912018-07-10 08:40:17 +02002239 * efi_set_watchdog_timer() - reset the watchdog timer
2240 * @timeout: seconds before reset by watchdog
2241 * @watchdog_code: code to be logged when resetting
2242 * @data_size: size of buffer in bytes
2243 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002244 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002245 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002246 *
Mario Six8fac2912018-07-10 08:40:17 +02002247 * See the Unified Extensible Firmware Interface (UEFI) specification for
2248 * details.
2249 *
2250 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002251 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002252static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
2253 uint64_t watchdog_code,
2254 unsigned long data_size,
2255 uint16_t *watchdog_data)
2256{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09002257 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01002258 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02002259 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01002260}
2261
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002262/**
Mario Six8fac2912018-07-10 08:40:17 +02002263 * efi_close_protocol() - close a protocol
2264 * @handle: handle on which the protocol shall be closed
2265 * @protocol: GUID of the protocol to close
2266 * @agent_handle: handle of the driver
2267 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002268 *
2269 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002270 *
Mario Six8fac2912018-07-10 08:40:17 +02002271 * See the Unified Extensible Firmware Interface (UEFI) specification for
2272 * details.
2273 *
2274 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002275 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002276efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
2277 const efi_guid_t *protocol,
2278 efi_handle_t agent_handle,
2279 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01002280{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002281 struct efi_handler *handler;
2282 struct efi_open_protocol_info_item *item;
2283 struct efi_open_protocol_info_item *pos;
2284 efi_status_t r;
2285
Rob Clark238f88c2017-09-13 18:05:41 -04002286 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01002287 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002288
Heinrich Schuchardt5d8231b2019-05-05 10:37:51 +02002289 if (!efi_search_obj(agent_handle) ||
2290 (controller_handle && !efi_search_obj(controller_handle))) {
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002291 r = EFI_INVALID_PARAMETER;
2292 goto out;
2293 }
2294 r = efi_search_protocol(handle, protocol, &handler);
2295 if (r != EFI_SUCCESS)
2296 goto out;
2297
2298 r = EFI_NOT_FOUND;
2299 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2300 if (item->info.agent_handle == agent_handle &&
2301 item->info.controller_handle == controller_handle) {
2302 efi_delete_open_info(item);
2303 r = EFI_SUCCESS;
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01002304 }
2305 }
2306out:
2307 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002308}
2309
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002310/**
Mario Six8fac2912018-07-10 08:40:17 +02002311 * efi_open_protocol_information() - provide information about then open status
2312 * of a protocol on a handle
2313 * @handle: handle for which the information shall be retrieved
2314 * @protocol: GUID of the protocol
2315 * @entry_buffer: buffer to receive the open protocol information
2316 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002317 *
2318 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002319 *
2320 * See the Unified Extensible Firmware Interface (UEFI) specification for
2321 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002322 *
Mario Six8fac2912018-07-10 08:40:17 +02002323 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002324 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002325static efi_status_t EFIAPI efi_open_protocol_information(
2326 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002327 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002328 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002329{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002330 unsigned long buffer_size;
2331 unsigned long count;
2332 struct efi_handler *handler;
2333 struct efi_open_protocol_info_item *item;
2334 efi_status_t r;
2335
Rob Clark238f88c2017-09-13 18:05:41 -04002336 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002337 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002338
2339 /* Check parameters */
2340 if (!entry_buffer) {
2341 r = EFI_INVALID_PARAMETER;
2342 goto out;
2343 }
2344 r = efi_search_protocol(handle, protocol, &handler);
2345 if (r != EFI_SUCCESS)
2346 goto out;
2347
2348 /* Count entries */
2349 count = 0;
2350 list_for_each_entry(item, &handler->open_infos, link) {
2351 if (item->info.open_count)
2352 ++count;
2353 }
2354 *entry_count = count;
2355 *entry_buffer = NULL;
2356 if (!count) {
2357 r = EFI_SUCCESS;
2358 goto out;
2359 }
2360
2361 /* Copy entries */
2362 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002363 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002364 (void **)entry_buffer);
2365 if (r != EFI_SUCCESS)
2366 goto out;
2367 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2368 if (item->info.open_count)
2369 (*entry_buffer)[--count] = item->info;
2370 }
2371out:
2372 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002373}
2374
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002375/**
Mario Six8fac2912018-07-10 08:40:17 +02002376 * efi_protocols_per_handle() - get protocols installed on a handle
2377 * @handle: handle for which the information is retrieved
2378 * @protocol_buffer: buffer with protocol GUIDs
2379 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002380 *
2381 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002382 *
Mario Six8fac2912018-07-10 08:40:17 +02002383 * See the Unified Extensible Firmware Interface (UEFI) specification for
2384 * details.
2385 *
2386 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002387 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002388static efi_status_t EFIAPI efi_protocols_per_handle(
2389 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002390 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002391{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002392 unsigned long buffer_size;
2393 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002394 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002395 efi_status_t r;
2396
Alexander Grafc15d9212016-03-04 01:09:59 +01002397 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2398 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002399
2400 if (!handle || !protocol_buffer || !protocol_buffer_count)
2401 return EFI_EXIT(EFI_INVALID_PARAMETER);
2402
2403 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002404 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002405
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002406 efiobj = efi_search_obj(handle);
2407 if (!efiobj)
2408 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002409
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002410 /* Count protocols */
2411 list_for_each(protocol_handle, &efiobj->protocols) {
2412 ++*protocol_buffer_count;
2413 }
2414
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002415 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002416 if (*protocol_buffer_count) {
2417 size_t j = 0;
2418
2419 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002420 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002421 (void **)protocol_buffer);
2422 if (r != EFI_SUCCESS)
2423 return EFI_EXIT(r);
2424 list_for_each(protocol_handle, &efiobj->protocols) {
2425 struct efi_handler *protocol;
2426
2427 protocol = list_entry(protocol_handle,
2428 struct efi_handler, link);
2429 (*protocol_buffer)[j] = (void *)protocol->guid;
2430 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002431 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002432 }
2433
2434 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002435}
2436
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002437/**
Mario Six8fac2912018-07-10 08:40:17 +02002438 * efi_locate_handle_buffer() - locate handles implementing a protocol
2439 * @search_type: selection criterion
2440 * @protocol: GUID of the protocol
2441 * @search_key: registration key
2442 * @no_handles: number of returned handles
2443 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002444 *
2445 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002446 *
Mario Six8fac2912018-07-10 08:40:17 +02002447 * See the Unified Extensible Firmware Interface (UEFI) specification for
2448 * details.
2449 *
2450 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002451 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09002452efi_status_t EFIAPI efi_locate_handle_buffer(
Alexander Grafc15d9212016-03-04 01:09:59 +01002453 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002454 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002455 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002456{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002457 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002458 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002459
Rob Clark238f88c2017-09-13 18:05:41 -04002460 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002461 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002462
2463 if (!no_handles || !buffer) {
2464 r = EFI_INVALID_PARAMETER;
2465 goto out;
2466 }
2467 *no_handles = 0;
2468 *buffer = NULL;
2469 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2470 *buffer);
2471 if (r != EFI_BUFFER_TOO_SMALL)
2472 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002473 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002474 (void **)buffer);
2475 if (r != EFI_SUCCESS)
2476 goto out;
2477 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2478 *buffer);
2479 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002480 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002481out:
2482 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002483}
2484
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002485/**
Mario Six8fac2912018-07-10 08:40:17 +02002486 * efi_locate_protocol() - find an interface implementing a protocol
2487 * @protocol: GUID of the protocol
2488 * @registration: registration key passed to the notification function
2489 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002490 *
2491 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002492 *
2493 * See the Unified Extensible Firmware Interface (UEFI) specification for
2494 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002495 *
Mario Six8fac2912018-07-10 08:40:17 +02002496 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002497 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002498static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002499 void *registration,
2500 void **protocol_interface)
2501{
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002502 struct efi_handler *handler;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002503 efi_status_t ret;
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002504 struct efi_object *efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01002505
Rob Clark238f88c2017-09-13 18:05:41 -04002506 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002507
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002508 /*
2509 * The UEFI spec explicitly requires a protocol even if a registration
2510 * key is provided. This differs from the logic in LocateHandle().
2511 */
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002512 if (!protocol || !protocol_interface)
2513 return EFI_EXIT(EFI_INVALID_PARAMETER);
2514
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002515 if (registration) {
2516 struct efi_register_notify_event *event;
2517 struct efi_protocol_notification *handle;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002518
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002519 event = efi_check_register_notify_event(registration);
2520 if (!event)
2521 return EFI_EXIT(EFI_INVALID_PARAMETER);
2522 /*
2523 * The UEFI spec requires to return EFI_NOT_FOUND if no
2524 * protocol instance matches protocol and registration.
2525 * So let's do the same for a mismatch between protocol and
2526 * registration.
2527 */
2528 if (guidcmp(&event->protocol, protocol))
2529 goto not_found;
2530 if (list_empty(&event->handles))
2531 goto not_found;
2532 handle = list_first_entry(&event->handles,
2533 struct efi_protocol_notification,
2534 link);
2535 efiobj = handle->handle;
2536 list_del(&handle->link);
2537 free(handle);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002538 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002539 if (ret == EFI_SUCCESS)
2540 goto found;
2541 } else {
2542 list_for_each_entry(efiobj, &efi_obj_list, link) {
2543 ret = efi_search_protocol(efiobj, protocol, &handler);
2544 if (ret == EFI_SUCCESS)
2545 goto found;
Alexander Grafc15d9212016-03-04 01:09:59 +01002546 }
2547 }
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002548not_found:
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002549 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002550 return EFI_EXIT(EFI_NOT_FOUND);
Heinrich Schuchardt2600eff2019-05-29 18:06:46 +02002551found:
2552 *protocol_interface = handler->protocol_interface;
2553 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002554}
2555
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002556/**
Mario Six8fac2912018-07-10 08:40:17 +02002557 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2558 * interfaces
2559 * @handle: handle on which the protocol interfaces shall be installed
2560 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2561 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002562 *
2563 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002564 *
2565 * See the Unified Extensible Firmware Interface (UEFI) specification for
2566 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002567 *
Mario Six8fac2912018-07-10 08:40:17 +02002568 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002569 */
Heinrich Schuchardt744d83e2019-04-12 06:59:49 +02002570efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002571 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002572{
2573 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002574
Alexander Graf404a7c82018-06-18 17:23:05 +02002575 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002576 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002577 void *protocol_interface;
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002578 efi_handle_t old_handle;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002579 efi_status_t r = EFI_SUCCESS;
2580 int i = 0;
2581
2582 if (!handle)
2583 return EFI_EXIT(EFI_INVALID_PARAMETER);
2584
Alexander Graf404a7c82018-06-18 17:23:05 +02002585 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002586 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002587 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002588 if (!protocol)
2589 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002590 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002591 /* Check that a device path has not been installed before */
2592 if (!guidcmp(protocol, &efi_guid_device_path)) {
2593 struct efi_device_path *dp = protocol_interface;
2594
2595 r = EFI_CALL(efi_locate_device_path(protocol, &dp,
2596 &old_handle));
Heinrich Schuchardt29344972019-05-20 19:55:18 +00002597 if (r == EFI_SUCCESS &&
2598 dp->type == DEVICE_PATH_TYPE_END) {
2599 EFI_PRINT("Path %pD already installed\n",
2600 protocol_interface);
Heinrich Schuchardt61ba10d2019-05-16 21:54:04 +02002601 r = EFI_ALREADY_STARTED;
2602 break;
2603 }
2604 }
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002605 r = EFI_CALL(efi_install_protocol_interface(
2606 handle, protocol,
2607 EFI_NATIVE_INTERFACE,
2608 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002609 if (r != EFI_SUCCESS)
2610 break;
2611 i++;
2612 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002613 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002614 if (r == EFI_SUCCESS)
2615 return EFI_EXIT(r);
2616
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002617 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002618 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002619 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002620 protocol = efi_va_arg(argptr, efi_guid_t*);
2621 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002622 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002623 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002624 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002625 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002626
2627 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002628}
2629
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002630/**
Mario Six8fac2912018-07-10 08:40:17 +02002631 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2632 * interfaces
2633 * @handle: handle from which the protocol interfaces shall be removed
2634 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2635 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002636 *
2637 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002638 *
Mario Six8fac2912018-07-10 08:40:17 +02002639 * See the Unified Extensible Firmware Interface (UEFI) specification for
2640 * details.
2641 *
2642 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002643 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002644static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002645 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002646{
2647 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002648
Alexander Graf404a7c82018-06-18 17:23:05 +02002649 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002650 const efi_guid_t *protocol;
2651 void *protocol_interface;
2652 efi_status_t r = EFI_SUCCESS;
2653 size_t i = 0;
2654
2655 if (!handle)
2656 return EFI_EXIT(EFI_INVALID_PARAMETER);
2657
Alexander Graf404a7c82018-06-18 17:23:05 +02002658 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002659 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002660 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002661 if (!protocol)
2662 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002663 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002664 r = efi_uninstall_protocol(handle, protocol,
2665 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002666 if (r != EFI_SUCCESS)
2667 break;
2668 i++;
2669 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002670 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002671 if (r == EFI_SUCCESS) {
2672 /* If the last protocol has been removed, delete the handle. */
2673 if (list_empty(&handle->protocols)) {
2674 list_del(&handle->link);
2675 free(handle);
2676 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002677 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002678 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002679
2680 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002681 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002682 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002683 protocol = efi_va_arg(argptr, efi_guid_t*);
2684 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002685 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2686 EFI_NATIVE_INTERFACE,
2687 protocol_interface));
2688 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002689 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002690
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002691 /* In case of an error always return EFI_INVALID_PARAMETER */
2692 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002693}
2694
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002695/**
Mario Six8fac2912018-07-10 08:40:17 +02002696 * efi_calculate_crc32() - calculate cyclic redundancy code
2697 * @data: buffer with data
2698 * @data_size: size of buffer in bytes
2699 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002700 *
2701 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002702 *
2703 * See the Unified Extensible Firmware Interface (UEFI) specification for
2704 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002705 *
Mario Six8fac2912018-07-10 08:40:17 +02002706 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002707 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002708static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2709 efi_uintn_t data_size,
2710 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002711{
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002712 efi_status_t ret = EFI_SUCCESS;
2713
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002714 EFI_ENTRY("%p, %zu", data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002715 if (!data || !data_size || !crc32_p) {
2716 ret = EFI_INVALID_PARAMETER;
2717 goto out;
2718 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002719 *crc32_p = crc32(0, data, data_size);
Heinrich Schuchardt3f0282c2019-05-16 23:31:29 +02002720out:
2721 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01002722}
2723
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002724/**
Mario Six8fac2912018-07-10 08:40:17 +02002725 * efi_copy_mem() - copy memory
2726 * @destination: destination of the copy operation
2727 * @source: source of the copy operation
2728 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002729 *
2730 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002731 *
Mario Six8fac2912018-07-10 08:40:17 +02002732 * See the Unified Extensible Firmware Interface (UEFI) specification for
2733 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002734 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002735static void EFIAPI efi_copy_mem(void *destination, const void *source,
2736 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002737{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002738 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Heinrich Schuchardtefba6ba2019-01-09 21:41:13 +01002739 memmove(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002740 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002741}
2742
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002743/**
Mario Six8fac2912018-07-10 08:40:17 +02002744 * efi_set_mem() - Fill memory with a byte value.
2745 * @buffer: buffer to fill
2746 * @size: size of buffer in bytes
2747 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002748 *
2749 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002750 *
Mario Six8fac2912018-07-10 08:40:17 +02002751 * See the Unified Extensible Firmware Interface (UEFI) specification for
2752 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002753 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002754static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002755{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002756 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002757 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002758 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002759}
2760
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002761/**
Mario Six8fac2912018-07-10 08:40:17 +02002762 * efi_protocol_open() - open protocol interface on a handle
2763 * @handler: handler of a protocol
2764 * @protocol_interface: interface implementing the protocol
2765 * @agent_handle: handle of the driver
2766 * @controller_handle: handle of the controller
2767 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002768 *
Mario Six8fac2912018-07-10 08:40:17 +02002769 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002770 */
Heinrich Schuchardt9c89d142020-01-10 12:33:59 +01002771efi_status_t efi_protocol_open(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002772 struct efi_handler *handler,
2773 void **protocol_interface, void *agent_handle,
2774 void *controller_handle, uint32_t attributes)
2775{
2776 struct efi_open_protocol_info_item *item;
2777 struct efi_open_protocol_info_entry *match = NULL;
2778 bool opened_by_driver = false;
2779 bool opened_exclusive = false;
2780
2781 /* If there is no agent, only return the interface */
2782 if (!agent_handle)
2783 goto out;
2784
2785 /* For TEST_PROTOCOL ignore interface attribute */
2786 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2787 *protocol_interface = NULL;
2788
2789 /*
2790 * Check if the protocol is already opened by a driver with the same
2791 * attributes or opened exclusively
2792 */
2793 list_for_each_entry(item, &handler->open_infos, link) {
2794 if (item->info.agent_handle == agent_handle) {
2795 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2796 (item->info.attributes == attributes))
2797 return EFI_ALREADY_STARTED;
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002798 } else {
2799 if (item->info.attributes &
2800 EFI_OPEN_PROTOCOL_BY_DRIVER)
2801 opened_by_driver = true;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002802 }
2803 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2804 opened_exclusive = true;
2805 }
2806
2807 /* Only one controller can open the protocol exclusively */
Heinrich Schuchardtda433d52019-05-30 14:16:31 +02002808 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2809 if (opened_exclusive)
2810 return EFI_ACCESS_DENIED;
2811 } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) {
2812 if (opened_exclusive || opened_by_driver)
2813 return EFI_ACCESS_DENIED;
2814 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002815
2816 /* Prepare exclusive opening */
2817 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2818 /* Try to disconnect controllers */
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002819disconnect_next:
2820 opened_by_driver = false;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002821 list_for_each_entry(item, &handler->open_infos, link) {
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002822 efi_status_t ret;
2823
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002824 if (item->info.attributes ==
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002825 EFI_OPEN_PROTOCOL_BY_DRIVER) {
2826 ret = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002827 item->info.controller_handle,
2828 item->info.agent_handle,
2829 NULL));
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002830 if (ret == EFI_SUCCESS)
2831 /*
2832 * Child controllers may have been
2833 * removed from the open_infos list. So
2834 * let's restart the loop.
2835 */
2836 goto disconnect_next;
2837 else
2838 opened_by_driver = true;
2839 }
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002840 }
Heinrich Schuchardtfa8dddf2019-05-30 14:16:31 +02002841 /* Only one driver can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002842 if (opened_by_driver)
2843 return EFI_ACCESS_DENIED;
2844 }
2845
2846 /* Find existing entry */
2847 list_for_each_entry(item, &handler->open_infos, link) {
2848 if (item->info.agent_handle == agent_handle &&
Heinrich Schuchardt7d69fc32019-06-01 20:15:10 +02002849 item->info.controller_handle == controller_handle &&
2850 item->info.attributes == attributes)
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002851 match = &item->info;
2852 }
2853 /* None found, create one */
2854 if (!match) {
2855 match = efi_create_open_info(handler);
2856 if (!match)
2857 return EFI_OUT_OF_RESOURCES;
2858 }
2859
2860 match->agent_handle = agent_handle;
2861 match->controller_handle = controller_handle;
2862 match->attributes = attributes;
2863 match->open_count++;
2864
2865out:
2866 /* For TEST_PROTOCOL ignore interface attribute. */
2867 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2868 *protocol_interface = handler->protocol_interface;
2869
2870 return EFI_SUCCESS;
2871}
2872
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002873/**
Mario Six8fac2912018-07-10 08:40:17 +02002874 * efi_open_protocol() - open protocol interface on a handle
2875 * @handle: handle on which the protocol shall be opened
2876 * @protocol: GUID of the protocol
2877 * @protocol_interface: interface implementing the protocol
2878 * @agent_handle: handle of the driver
2879 * @controller_handle: handle of the controller
2880 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002881 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002882 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002883 *
Mario Six8fac2912018-07-10 08:40:17 +02002884 * See the Unified Extensible Firmware Interface (UEFI) specification for
2885 * details.
2886 *
2887 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002888 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002889static efi_status_t EFIAPI efi_open_protocol
2890 (efi_handle_t handle, const efi_guid_t *protocol,
2891 void **protocol_interface, efi_handle_t agent_handle,
2892 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002893{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002894 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002895 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002896
Rob Clark238f88c2017-09-13 18:05:41 -04002897 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002898 protocol_interface, agent_handle, controller_handle,
2899 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002900
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002901 if (!handle || !protocol ||
2902 (!protocol_interface && attributes !=
2903 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002904 goto out;
2905 }
2906
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002907 switch (attributes) {
2908 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2909 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2910 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2911 break;
2912 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2913 if (controller_handle == handle)
2914 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002915 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002916 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2917 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002918 /* Check that the controller handle is valid */
2919 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002920 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002921 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002922 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002923 /* Check that the agent handle is valid */
2924 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002925 goto out;
2926 break;
2927 default:
2928 goto out;
2929 }
2930
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002931 r = efi_search_protocol(handle, protocol, &handler);
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002932 switch (r) {
2933 case EFI_SUCCESS:
2934 break;
2935 case EFI_NOT_FOUND:
2936 r = EFI_UNSUPPORTED;
2937 goto out;
2938 default:
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002939 goto out;
Heinrich Schuchardtb673e4b2019-05-05 11:24:53 +02002940 }
Alexander Grafc15d9212016-03-04 01:09:59 +01002941
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002942 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2943 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002944out:
2945 return EFI_EXIT(r);
2946}
2947
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002948/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002949 * efi_start_image() - call the entry point of an image
2950 * @image_handle: handle of the image
2951 * @exit_data_size: size of the buffer
2952 * @exit_data: buffer to receive the exit data of the called image
2953 *
2954 * This function implements the StartImage service.
2955 *
2956 * See the Unified Extensible Firmware Interface (UEFI) specification for
2957 * details.
2958 *
2959 * Return: status code
2960 */
2961efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
2962 efi_uintn_t *exit_data_size,
2963 u16 **exit_data)
2964{
2965 struct efi_loaded_image_obj *image_obj =
2966 (struct efi_loaded_image_obj *)image_handle;
2967 efi_status_t ret;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002968 void *info;
2969 efi_handle_t parent_image = current_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002970 efi_status_t exit_status;
2971 struct jmp_buf_data exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002972
2973 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
2974
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002975 if (!efi_search_obj(image_handle))
2976 return EFI_EXIT(EFI_INVALID_PARAMETER);
2977
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002978 /* Check parameters */
Heinrich Schuchardtff94bca2019-06-11 19:35:20 +02002979 if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE)
2980 return EFI_EXIT(EFI_INVALID_PARAMETER);
2981
AKASHI Takahiro0e104e32020-04-14 11:51:44 +09002982 if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED)
2983 return EFI_EXIT(EFI_SECURITY_VIOLATION);
2984
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01002985 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
2986 &info, NULL, NULL,
2987 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2988 if (ret != EFI_SUCCESS)
2989 return EFI_EXIT(EFI_INVALID_PARAMETER);
2990
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002991 image_obj->exit_data_size = exit_data_size;
2992 image_obj->exit_data = exit_data;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002993 image_obj->exit_status = &exit_status;
2994 image_obj->exit_jmp = &exit_jmp;
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02002995
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002996 /* call the image! */
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01002997 if (setjmp(&exit_jmp)) {
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01002998 /*
2999 * We called the entry point of the child image with EFI_CALL
3000 * in the lines below. The child image called the Exit() boot
3001 * service efi_exit() which executed the long jump that brought
3002 * us to the current line. This implies that the second half
3003 * of the EFI_CALL macro has not been executed.
3004 */
Heinrich Schuchardtf277d942020-09-10 12:22:54 +02003005#if defined(CONFIG_ARM) || defined(CONFIG_RISCV)
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003006 /*
3007 * efi_exit() called efi_restore_gd(). We have to undo this
3008 * otherwise __efi_entry_check() will put the wrong value into
3009 * app_gd.
3010 */
Heinrich Schuchardt1a3732c2020-05-27 01:58:30 +02003011 set_gd(app_gd);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003012#endif
3013 /*
3014 * To get ready to call EFI_EXIT below we have to execute the
3015 * missed out steps of EFI_CALL.
3016 */
3017 assert(__efi_entry_check());
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003018 EFI_PRINT("%lu returned by started image\n",
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003019 (unsigned long)((uintptr_t)exit_status &
Heinrich Schuchardt952e2062019-05-05 11:56:23 +02003020 ~EFI_ERROR_MASK));
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003021 current_image = parent_image;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003022 return EFI_EXIT(exit_status);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003023 }
3024
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003025 current_image = image_handle;
Heinrich Schuchardtb27ced42019-05-01 14:20:18 +02003026 image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE;
AKASHI Takahiro14ff23b2019-04-19 12:22:35 +09003027 EFI_PRINT("Jumping into 0x%p\n", image_obj->entry);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003028 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
3029
3030 /*
Heinrich Schuchardtb7bc28d2020-01-10 22:06:54 +01003031 * Control is returned from a started UEFI image either by calling
3032 * Exit() (where exit data can be provided) or by simply returning from
3033 * the entry point. In the latter case call Exit() on behalf of the
3034 * image.
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003035 */
3036 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
3037}
3038
3039/**
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003040 * efi_delete_image() - delete loaded image from memory)
3041 *
3042 * @image_obj: handle of the loaded image
3043 * @loaded_image_protocol: loaded image protocol
3044 */
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003045static efi_status_t efi_delete_image
3046 (struct efi_loaded_image_obj *image_obj,
3047 struct efi_loaded_image *loaded_image_protocol)
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003048{
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003049 struct efi_object *efiobj;
3050 efi_status_t r, ret = EFI_SUCCESS;
3051
3052close_next:
3053 list_for_each_entry(efiobj, &efi_obj_list, link) {
3054 struct efi_handler *protocol;
3055
3056 list_for_each_entry(protocol, &efiobj->protocols, link) {
3057 struct efi_open_protocol_info_item *info;
3058
3059 list_for_each_entry(info, &protocol->open_infos, link) {
3060 if (info->info.agent_handle !=
3061 (efi_handle_t)image_obj)
3062 continue;
3063 r = EFI_CALL(efi_close_protocol
3064 (efiobj, protocol->guid,
3065 info->info.agent_handle,
3066 info->info.controller_handle
3067 ));
3068 if (r != EFI_SUCCESS)
3069 ret = r;
3070 /*
3071 * Closing protocols may results in further
3072 * items being deleted. To play it safe loop
3073 * over all elements again.
3074 */
3075 goto close_next;
3076 }
3077 }
3078 }
3079
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003080 efi_free_pages((uintptr_t)loaded_image_protocol->image_base,
3081 efi_size_in_pages(loaded_image_protocol->image_size));
3082 efi_delete_handle(&image_obj->header);
Heinrich Schuchardte3bca2a2019-06-02 20:02:32 +02003083
3084 return ret;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003085}
3086
3087/**
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003088 * efi_unload_image() - unload an EFI image
3089 * @image_handle: handle of the image to be unloaded
3090 *
3091 * This function implements the UnloadImage service.
3092 *
3093 * See the Unified Extensible Firmware Interface (UEFI) specification for
3094 * details.
3095 *
3096 * Return: status code
3097 */
3098efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
3099{
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003100 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003101 struct efi_object *efiobj;
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003102 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003103
3104 EFI_ENTRY("%p", image_handle);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003105
Heinrich Schuchardt4ed66e52019-05-01 18:25:45 +02003106 efiobj = efi_search_obj(image_handle);
3107 if (!efiobj) {
3108 ret = EFI_INVALID_PARAMETER;
3109 goto out;
3110 }
3111 /* Find the loaded image protocol */
3112 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
3113 (void **)&loaded_image_protocol,
3114 NULL, NULL,
3115 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3116 if (ret != EFI_SUCCESS) {
3117 ret = EFI_INVALID_PARAMETER;
3118 goto out;
3119 }
3120 switch (efiobj->type) {
3121 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3122 /* Call the unload function */
3123 if (!loaded_image_protocol->unload) {
3124 ret = EFI_UNSUPPORTED;
3125 goto out;
3126 }
3127 ret = EFI_CALL(loaded_image_protocol->unload(image_handle));
3128 if (ret != EFI_SUCCESS)
3129 goto out;
3130 break;
3131 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3132 break;
3133 default:
3134 ret = EFI_INVALID_PARAMETER;
3135 goto out;
3136 }
3137 efi_delete_image((struct efi_loaded_image_obj *)efiobj,
3138 loaded_image_protocol);
3139out:
3140 return EFI_EXIT(ret);
Heinrich Schuchardt6b3581a2019-05-01 19:04:32 +02003141}
3142
3143/**
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003144 * efi_update_exit_data() - fill exit data parameters of StartImage()
3145 *
Heinrich Schuchardt74c25292019-07-14 11:25:06 +02003146 * @image_obj: image handle
3147 * @exit_data_size: size of the exit data buffer
3148 * @exit_data: buffer with data returned by UEFI payload
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003149 * Return: status code
3150 */
3151static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj,
3152 efi_uintn_t exit_data_size,
3153 u16 *exit_data)
3154{
3155 efi_status_t ret;
3156
3157 /*
3158 * If exit_data is not provided to StartImage(), exit_data_size must be
3159 * ignored.
3160 */
3161 if (!image_obj->exit_data)
3162 return EFI_SUCCESS;
3163 if (image_obj->exit_data_size)
3164 *image_obj->exit_data_size = exit_data_size;
3165 if (exit_data_size && exit_data) {
3166 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
3167 exit_data_size,
3168 (void **)image_obj->exit_data);
3169 if (ret != EFI_SUCCESS)
3170 return ret;
3171 memcpy(*image_obj->exit_data, exit_data, exit_data_size);
3172 } else {
3173 image_obj->exit_data = NULL;
3174 }
3175 return EFI_SUCCESS;
3176}
3177
3178/**
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003179 * efi_exit() - leave an EFI application or driver
3180 * @image_handle: handle of the application or driver that is exiting
3181 * @exit_status: status code
3182 * @exit_data_size: size of the buffer in bytes
3183 * @exit_data: buffer with data describing an error
3184 *
3185 * This function implements the Exit service.
3186 *
3187 * See the Unified Extensible Firmware Interface (UEFI) specification for
3188 * details.
3189 *
3190 * Return: status code
3191 */
3192static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
3193 efi_status_t exit_status,
3194 efi_uintn_t exit_data_size,
3195 u16 *exit_data)
3196{
3197 /*
3198 * TODO: We should call the unload procedure of the loaded
3199 * image protocol.
3200 */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003201 efi_status_t ret;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003202 struct efi_loaded_image *loaded_image_protocol;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003203 struct efi_loaded_image_obj *image_obj =
3204 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003205 struct jmp_buf_data *exit_jmp;
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003206
3207 EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status,
3208 exit_data_size, exit_data);
3209
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003210 /* Check parameters */
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003211 ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image,
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003212 (void **)&loaded_image_protocol,
3213 NULL, NULL,
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003214 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003215 if (ret != EFI_SUCCESS) {
3216 ret = EFI_INVALID_PARAMETER;
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003217 goto out;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003218 }
3219
3220 /* Unloading of unstarted images */
3221 switch (image_obj->header.type) {
3222 case EFI_OBJECT_TYPE_STARTED_IMAGE:
3223 break;
3224 case EFI_OBJECT_TYPE_LOADED_IMAGE:
3225 efi_delete_image(image_obj, loaded_image_protocol);
3226 ret = EFI_SUCCESS;
3227 goto out;
3228 default:
3229 /* Handle does not refer to loaded image */
3230 ret = EFI_INVALID_PARAMETER;
3231 goto out;
3232 }
3233 /* A started image can only be unloaded it is the last one started. */
3234 if (image_handle != current_image) {
3235 ret = EFI_INVALID_PARAMETER;
3236 goto out;
3237 }
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003238
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003239 /* Exit data is only foreseen in case of failure. */
3240 if (exit_status != EFI_SUCCESS) {
3241 ret = efi_update_exit_data(image_obj, exit_data_size,
3242 exit_data);
3243 /* Exiting has priority. Don't return error to caller. */
3244 if (ret != EFI_SUCCESS)
3245 EFI_PRINT("%s: out of memory\n", __func__);
3246 }
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003247 /* efi_delete_image() frees image_obj. Copy before the call. */
3248 exit_jmp = image_obj->exit_jmp;
3249 *image_obj->exit_status = exit_status;
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003250 if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION ||
3251 exit_status != EFI_SUCCESS)
3252 efi_delete_image(image_obj, loaded_image_protocol);
Heinrich Schuchardt3d445122019-04-30 17:57:30 +02003253
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003254 /* Make sure entry/exit counts for EFI world cross-overs match */
3255 EFI_EXIT(exit_status);
3256
3257 /*
3258 * But longjmp out with the U-Boot gd, not the application's, as
3259 * the other end is a setjmp call inside EFI context.
3260 */
3261 efi_restore_gd();
3262
Heinrich Schuchardt026c7ec2020-12-28 23:24:40 +01003263 longjmp(exit_jmp, 1);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003264
3265 panic("EFI application exited");
Heinrich Schuchardt8efe0792019-03-26 19:03:17 +01003266out:
Heinrich Schuchardt37587522019-05-01 20:07:04 +02003267 return EFI_EXIT(ret);
Heinrich Schuchardt2650a4d2019-03-26 19:02:05 +01003268}
3269
3270/**
Mario Six8fac2912018-07-10 08:40:17 +02003271 * efi_handle_protocol() - get interface of a protocol on a handle
3272 * @handle: handle on which the protocol shall be opened
3273 * @protocol: GUID of the protocol
3274 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003275 *
3276 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02003277 *
3278 * See the Unified Extensible Firmware Interface (UEFI) specification for
3279 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003280 *
Mario Six8fac2912018-07-10 08:40:17 +02003281 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02003282 */
AKASHI Takahirod99b1b32020-03-17 11:12:36 +09003283efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
3284 const efi_guid_t *protocol,
3285 void **protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01003286{
Heinrich Schuchardtef096152019-06-01 19:29:39 +02003287 return efi_open_protocol(handle, protocol, protocol_interface, efi_root,
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02003288 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01003289}
3290
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003291/**
Mario Six8fac2912018-07-10 08:40:17 +02003292 * efi_bind_controller() - bind a single driver to a controller
3293 * @controller_handle: controller handle
3294 * @driver_image_handle: driver handle
3295 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003296 *
Mario Six8fac2912018-07-10 08:40:17 +02003297 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003298 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003299static efi_status_t efi_bind_controller(
3300 efi_handle_t controller_handle,
3301 efi_handle_t driver_image_handle,
3302 struct efi_device_path *remain_device_path)
3303{
3304 struct efi_driver_binding_protocol *binding_protocol;
3305 efi_status_t r;
3306
3307 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3308 &efi_guid_driver_binding_protocol,
3309 (void **)&binding_protocol,
3310 driver_image_handle, NULL,
3311 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3312 if (r != EFI_SUCCESS)
3313 return r;
3314 r = EFI_CALL(binding_protocol->supported(binding_protocol,
3315 controller_handle,
3316 remain_device_path));
3317 if (r == EFI_SUCCESS)
3318 r = EFI_CALL(binding_protocol->start(binding_protocol,
3319 controller_handle,
3320 remain_device_path));
3321 EFI_CALL(efi_close_protocol(driver_image_handle,
3322 &efi_guid_driver_binding_protocol,
3323 driver_image_handle, NULL));
3324 return r;
3325}
3326
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003327/**
Mario Six8fac2912018-07-10 08:40:17 +02003328 * efi_connect_single_controller() - connect a single driver to a controller
3329 * @controller_handle: controller
3330 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003331 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003332 *
Mario Six8fac2912018-07-10 08:40:17 +02003333 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003334 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003335static efi_status_t efi_connect_single_controller(
3336 efi_handle_t controller_handle,
3337 efi_handle_t *driver_image_handle,
3338 struct efi_device_path *remain_device_path)
3339{
3340 efi_handle_t *buffer;
3341 size_t count;
3342 size_t i;
3343 efi_status_t r;
3344 size_t connected = 0;
3345
3346 /* Get buffer with all handles with driver binding protocol */
3347 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
3348 &efi_guid_driver_binding_protocol,
3349 NULL, &count, &buffer));
3350 if (r != EFI_SUCCESS)
3351 return r;
3352
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003353 /* Context Override */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003354 if (driver_image_handle) {
3355 for (; *driver_image_handle; ++driver_image_handle) {
3356 for (i = 0; i < count; ++i) {
3357 if (buffer[i] == *driver_image_handle) {
3358 buffer[i] = NULL;
3359 r = efi_bind_controller(
3360 controller_handle,
3361 *driver_image_handle,
3362 remain_device_path);
3363 /*
3364 * For drivers that do not support the
3365 * controller or are already connected
3366 * we receive an error code here.
3367 */
3368 if (r == EFI_SUCCESS)
3369 ++connected;
3370 }
3371 }
3372 }
3373 }
3374
3375 /*
3376 * TODO: Some overrides are not yet implemented:
3377 * - Platform Driver Override
3378 * - Driver Family Override Search
3379 * - Bus Specific Driver Override
3380 */
3381
3382 /* Driver Binding Search */
3383 for (i = 0; i < count; ++i) {
3384 if (buffer[i]) {
3385 r = efi_bind_controller(controller_handle,
3386 buffer[i],
3387 remain_device_path);
3388 if (r == EFI_SUCCESS)
3389 ++connected;
3390 }
3391 }
3392
3393 efi_free_pool(buffer);
3394 if (!connected)
3395 return EFI_NOT_FOUND;
3396 return EFI_SUCCESS;
3397}
3398
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003399/**
Mario Six8fac2912018-07-10 08:40:17 +02003400 * efi_connect_controller() - connect a controller to a driver
3401 * @controller_handle: handle of the controller
3402 * @driver_image_handle: handle of the driver
3403 * @remain_device_path: device path of a child controller
3404 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003405 *
3406 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003407 *
3408 * See the Unified Extensible Firmware Interface (UEFI) specification for
3409 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003410 *
3411 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003412 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003413 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
3414 *
Mario Six8fac2912018-07-10 08:40:17 +02003415 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003416 */
3417static efi_status_t EFIAPI efi_connect_controller(
3418 efi_handle_t controller_handle,
3419 efi_handle_t *driver_image_handle,
3420 struct efi_device_path *remain_device_path,
3421 bool recursive)
3422{
3423 efi_status_t r;
3424 efi_status_t ret = EFI_NOT_FOUND;
3425 struct efi_object *efiobj;
3426
Heinrich Schuchardt7c89fb02018-12-09 16:39:20 +01003427 EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle,
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003428 remain_device_path, recursive);
3429
3430 efiobj = efi_search_obj(controller_handle);
3431 if (!efiobj) {
3432 ret = EFI_INVALID_PARAMETER;
3433 goto out;
3434 }
3435
3436 r = efi_connect_single_controller(controller_handle,
3437 driver_image_handle,
3438 remain_device_path);
3439 if (r == EFI_SUCCESS)
3440 ret = EFI_SUCCESS;
3441 if (recursive) {
3442 struct efi_handler *handler;
3443 struct efi_open_protocol_info_item *item;
3444
3445 list_for_each_entry(handler, &efiobj->protocols, link) {
3446 list_for_each_entry(item, &handler->open_infos, link) {
3447 if (item->info.attributes &
3448 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3449 r = EFI_CALL(efi_connect_controller(
3450 item->info.controller_handle,
3451 driver_image_handle,
3452 remain_device_path,
3453 recursive));
3454 if (r == EFI_SUCCESS)
3455 ret = EFI_SUCCESS;
3456 }
3457 }
3458 }
3459 }
Heinrich Schuchardtda8f9002019-07-03 20:27:24 +02003460 /* Check for child controller specified by end node */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01003461 if (ret != EFI_SUCCESS && remain_device_path &&
3462 remain_device_path->type == DEVICE_PATH_TYPE_END)
3463 ret = EFI_SUCCESS;
3464out:
3465 return EFI_EXIT(ret);
3466}
3467
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003468/**
Mario Six8fac2912018-07-10 08:40:17 +02003469 * efi_reinstall_protocol_interface() - reinstall protocol interface
3470 * @handle: handle on which the protocol shall be reinstalled
3471 * @protocol: GUID of the protocol to be installed
3472 * @old_interface: interface to be removed
3473 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003474 *
3475 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02003476 *
3477 * See the Unified Extensible Firmware Interface (UEFI) specification for
3478 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003479 *
3480 * The old interface is uninstalled. The new interface is installed.
3481 * Drivers are connected.
3482 *
Mario Six8fac2912018-07-10 08:40:17 +02003483 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003484 */
3485static efi_status_t EFIAPI efi_reinstall_protocol_interface(
3486 efi_handle_t handle, const efi_guid_t *protocol,
3487 void *old_interface, void *new_interface)
3488{
3489 efi_status_t ret;
3490
3491 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
3492 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003493
3494 /* Uninstall protocol but do not delete handle */
3495 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003496 if (ret != EFI_SUCCESS)
3497 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02003498
3499 /* Install the new protocol */
3500 ret = efi_add_protocol(handle, protocol, new_interface);
3501 /*
3502 * The UEFI spec does not specify what should happen to the handle
3503 * if in case of an error no protocol interface remains on the handle.
3504 * So let's do nothing here.
3505 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02003506 if (ret != EFI_SUCCESS)
3507 goto out;
3508 /*
3509 * The returned status code has to be ignored.
3510 * Do not create an error if no suitable driver for the handle exists.
3511 */
3512 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
3513out:
3514 return EFI_EXIT(ret);
3515}
3516
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003517/**
Mario Six8fac2912018-07-10 08:40:17 +02003518 * efi_get_child_controllers() - get all child controllers associated to a driver
3519 * @efiobj: handle of the controller
3520 * @driver_handle: handle of the driver
3521 * @number_of_children: number of child controllers
3522 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003523 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003524 * The allocated buffer has to be freed with free().
3525 *
Mario Six8fac2912018-07-10 08:40:17 +02003526 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003527 */
3528static efi_status_t efi_get_child_controllers(
3529 struct efi_object *efiobj,
3530 efi_handle_t driver_handle,
3531 efi_uintn_t *number_of_children,
3532 efi_handle_t **child_handle_buffer)
3533{
3534 struct efi_handler *handler;
3535 struct efi_open_protocol_info_item *item;
3536 efi_uintn_t count = 0, i;
3537 bool duplicate;
3538
3539 /* Count all child controller associations */
3540 list_for_each_entry(handler, &efiobj->protocols, link) {
3541 list_for_each_entry(item, &handler->open_infos, link) {
3542 if (item->info.agent_handle == driver_handle &&
3543 item->info.attributes &
3544 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
3545 ++count;
3546 }
3547 }
3548 /*
3549 * Create buffer. In case of duplicate child controller assignments
3550 * the buffer will be too large. But that does not harm.
3551 */
3552 *number_of_children = 0;
Heinrich Schuchardt0f321b62020-07-07 04:21:26 +02003553 if (!count)
3554 return EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003555 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
3556 if (!*child_handle_buffer)
3557 return EFI_OUT_OF_RESOURCES;
3558 /* Copy unique child handles */
3559 list_for_each_entry(handler, &efiobj->protocols, link) {
3560 list_for_each_entry(item, &handler->open_infos, link) {
3561 if (item->info.agent_handle == driver_handle &&
3562 item->info.attributes &
3563 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
3564 /* Check this is a new child controller */
3565 duplicate = false;
3566 for (i = 0; i < *number_of_children; ++i) {
3567 if ((*child_handle_buffer)[i] ==
3568 item->info.controller_handle)
3569 duplicate = true;
3570 }
3571 /* Copy handle to buffer */
3572 if (!duplicate) {
3573 i = (*number_of_children)++;
3574 (*child_handle_buffer)[i] =
3575 item->info.controller_handle;
3576 }
3577 }
3578 }
3579 }
3580 return EFI_SUCCESS;
3581}
3582
Heinrich Schuchardt59999172018-05-11 18:15:41 +02003583/**
Mario Six8fac2912018-07-10 08:40:17 +02003584 * efi_disconnect_controller() - disconnect a controller from a driver
3585 * @controller_handle: handle of the controller
3586 * @driver_image_handle: handle of the driver
3587 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003588 *
3589 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02003590 *
3591 * See the Unified Extensible Firmware Interface (UEFI) specification for
3592 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003593 *
Mario Six8fac2912018-07-10 08:40:17 +02003594 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003595 */
3596static efi_status_t EFIAPI efi_disconnect_controller(
3597 efi_handle_t controller_handle,
3598 efi_handle_t driver_image_handle,
3599 efi_handle_t child_handle)
3600{
3601 struct efi_driver_binding_protocol *binding_protocol;
3602 efi_handle_t *child_handle_buffer = NULL;
3603 size_t number_of_children = 0;
3604 efi_status_t r;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003605 struct efi_object *efiobj;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003606 bool sole_child;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003607
3608 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
3609 child_handle);
3610
3611 efiobj = efi_search_obj(controller_handle);
3612 if (!efiobj) {
3613 r = EFI_INVALID_PARAMETER;
3614 goto out;
3615 }
3616
3617 if (child_handle && !efi_search_obj(child_handle)) {
3618 r = EFI_INVALID_PARAMETER;
3619 goto out;
3620 }
3621
3622 /* If no driver handle is supplied, disconnect all drivers */
3623 if (!driver_image_handle) {
3624 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
3625 goto out;
3626 }
3627
3628 /* Create list of child handles */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003629 r = efi_get_child_controllers(efiobj,
3630 driver_image_handle,
3631 &number_of_children,
3632 &child_handle_buffer);
3633 if (r != EFI_SUCCESS)
3634 return r;
3635 sole_child = (number_of_children == 1);
3636
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003637 if (child_handle) {
3638 number_of_children = 1;
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003639 free(child_handle_buffer);
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003640 child_handle_buffer = &child_handle;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003641 }
3642
3643 /* Get the driver binding protocol */
3644 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3645 &efi_guid_driver_binding_protocol,
3646 (void **)&binding_protocol,
3647 driver_image_handle, NULL,
3648 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003649 if (r != EFI_SUCCESS) {
3650 r = EFI_INVALID_PARAMETER;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003651 goto out;
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003652 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003653 /* Remove the children */
3654 if (number_of_children) {
3655 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3656 controller_handle,
3657 number_of_children,
3658 child_handle_buffer));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003659 if (r != EFI_SUCCESS) {
3660 r = EFI_DEVICE_ERROR;
3661 goto out;
3662 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003663 }
3664 /* Remove the driver */
Heinrich Schuchardtc7ce4042020-10-28 18:45:47 +01003665 if (!child_handle || sole_child) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003666 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3667 controller_handle,
3668 0, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003669 if (r != EFI_SUCCESS) {
3670 r = EFI_DEVICE_ERROR;
3671 goto out;
3672 }
3673 }
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003674 EFI_CALL(efi_close_protocol(driver_image_handle,
3675 &efi_guid_driver_binding_protocol,
3676 driver_image_handle, NULL));
Heinrich Schuchardt79622842019-09-13 18:20:40 +02003677 r = EFI_SUCCESS;
Heinrich Schuchardte9943282018-01-11 08:16:04 +01003678out:
3679 if (!child_handle)
3680 free(child_handle_buffer);
3681 return EFI_EXIT(r);
3682}
3683
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003684static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003685 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003686 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3687 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003688 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003689 },
3690 .raise_tpl = efi_raise_tpl,
3691 .restore_tpl = efi_restore_tpl,
3692 .allocate_pages = efi_allocate_pages_ext,
3693 .free_pages = efi_free_pages_ext,
3694 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003695 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003696 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003697 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003698 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003699 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003700 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003701 .close_event = efi_close_event,
3702 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003703 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003704 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003705 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003706 .handle_protocol = efi_handle_protocol,
3707 .reserved = NULL,
3708 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003709 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003710 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003711 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003712 .load_image = efi_load_image,
3713 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003714 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003715 .unload_image = efi_unload_image,
3716 .exit_boot_services = efi_exit_boot_services,
3717 .get_next_monotonic_count = efi_get_next_monotonic_count,
3718 .stall = efi_stall,
3719 .set_watchdog_timer = efi_set_watchdog_timer,
3720 .connect_controller = efi_connect_controller,
3721 .disconnect_controller = efi_disconnect_controller,
3722 .open_protocol = efi_open_protocol,
3723 .close_protocol = efi_close_protocol,
3724 .open_protocol_information = efi_open_protocol_information,
3725 .protocols_per_handle = efi_protocols_per_handle,
3726 .locate_handle_buffer = efi_locate_handle_buffer,
3727 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003728 .install_multiple_protocol_interfaces =
3729 efi_install_multiple_protocol_interfaces,
3730 .uninstall_multiple_protocol_interfaces =
3731 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003732 .calculate_crc32 = efi_calculate_crc32,
3733 .copy_mem = efi_copy_mem,
3734 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003735 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003736};
3737
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003738static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003739
Alexander Graf393dd912016-10-14 13:45:30 +02003740struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003741 .hdr = {
3742 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003743 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003744 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003745 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003746 .fw_vendor = firmware_vendor,
3747 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt372b8932019-06-15 14:51:06 +02003748 .runtime = &efi_runtime_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003749 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003750 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003751};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003752
3753/**
3754 * efi_initialize_system_table() - Initialize system table
3755 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003756 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003757 */
3758efi_status_t efi_initialize_system_table(void)
3759{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003760 efi_status_t ret;
3761
3762 /* Allocate configuration table array */
3763 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3764 EFI_MAX_CONFIGURATION_TABLES *
3765 sizeof(struct efi_configuration_table),
3766 (void **)&systab.tables);
3767
Heinrich Schuchardt98f89362019-06-29 02:00:16 +02003768 /*
3769 * These entries will be set to NULL in ExitBootServices(). To avoid
3770 * relocation in SetVirtualAddressMap(), set them dynamically.
3771 */
3772 systab.con_in = &efi_con_in;
3773 systab.con_out = &efi_con_out;
3774 systab.std_err = &efi_con_out;
3775 systab.boottime = &efi_boot_services;
3776
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003777 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003778 efi_update_table_header_crc32(&systab.hdr);
3779 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3780 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003781
3782 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003783}