blob: eb652e8348562ba6a2b1ab59149db386b3bb0bb4 [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/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafc15d9212016-03-04 01:09:59 +01006 */
7
Alexander Grafc15d9212016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt368ca642017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clark15f3d742017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Alexander Grafc15d9212016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +020022
Alexander Grafc15d9212016-03-04 01:09:59 +010023/* This list contains all the EFI objects our payload has access to */
24LIST_HEAD(efi_obj_list);
25
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +010028
Simon Glasscdfe6962016-09-25 15:27:35 -060029#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010030/*
31 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
32 * fixed when compiling U-Boot. However, the payload does not know about that
33 * restriction so we need to manually swap its and our view of that register on
34 * EFI callback entry/exit.
35 */
36static volatile void *efi_gd, *app_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060037#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010038
Rob Clark86789d52017-07-27 08:04:18 -040039static int entry_count;
Rob Clarke7896c32017-07-27 08:04:19 -040040static int nesting_level;
Heinrich Schuchardt44ab21b2018-03-03 15:29:03 +010041/* GUID of the device tree table */
42const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardt760255f2018-01-11 08:16:02 +010043/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
44const efi_guid_t efi_guid_driver_binding_protocol =
45 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clark86789d52017-07-27 08:04:18 -040046
Heinrich Schuchardt672d99e2018-02-18 15:17:51 +010047/* event group ExitBootServices() invoked */
48const efi_guid_t efi_guid_event_group_exit_boot_services =
49 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
50/* event group SetVirtualAddressMap() invoked */
51const efi_guid_t efi_guid_event_group_virtual_address_change =
52 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
53/* event group memory map changed */
54const efi_guid_t efi_guid_event_group_memory_map_change =
55 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
56/* event group boot manager about to boot */
57const efi_guid_t efi_guid_event_group_ready_to_boot =
58 EFI_EVENT_GROUP_READY_TO_BOOT;
59/* event group ResetSystem() invoked (before ExitBootServices) */
60const efi_guid_t efi_guid_event_group_reset_system =
61 EFI_EVENT_GROUP_RESET_SYSTEM;
62
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +010063static efi_status_t EFIAPI efi_disconnect_controller(
64 efi_handle_t controller_handle,
65 efi_handle_t driver_image_handle,
66 efi_handle_t child_handle);
Heinrich Schuchardte9943282018-01-11 08:16:04 +010067
Rob Clark86789d52017-07-27 08:04:18 -040068/* Called on every callback entry */
69int __efi_entry_check(void)
70{
71 int ret = entry_count++ == 0;
72#ifdef CONFIG_ARM
73 assert(efi_gd);
74 app_gd = gd;
75 gd = efi_gd;
76#endif
77 return ret;
78}
79
80/* Called on every callback exit */
81int __efi_exit_check(void)
82{
83 int ret = --entry_count == 0;
84#ifdef CONFIG_ARM
85 gd = app_gd;
86#endif
87 return ret;
88}
89
Alexander Grafc15d9212016-03-04 01:09:59 +010090/* Called from do_bootefi_exec() */
91void efi_save_gd(void)
92{
Simon Glasscdfe6962016-09-25 15:27:35 -060093#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +010094 efi_gd = gd;
Simon Glasscdfe6962016-09-25 15:27:35 -060095#endif
Alexander Grafc15d9212016-03-04 01:09:59 +010096}
97
Rob Clark86789d52017-07-27 08:04:18 -040098/*
Mario Six8fac2912018-07-10 08:40:17 +020099 * Special case handler for error/abort that just forces things back to u-boot
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200100 * world so we can dump out an abort message, without any care about returning
101 * back to UEFI world.
Rob Clark86789d52017-07-27 08:04:18 -0400102 */
Alexander Grafc15d9212016-03-04 01:09:59 +0100103void efi_restore_gd(void)
104{
Simon Glasscdfe6962016-09-25 15:27:35 -0600105#ifdef CONFIG_ARM
Alexander Grafc15d9212016-03-04 01:09:59 +0100106 /* Only restore if we're already in EFI context */
107 if (!efi_gd)
108 return;
Alexander Grafc15d9212016-03-04 01:09:59 +0100109 gd = efi_gd;
Simon Glasscdfe6962016-09-25 15:27:35 -0600110#endif
Alexander Grafc15d9212016-03-04 01:09:59 +0100111}
112
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200113/**
Mario Six8fac2912018-07-10 08:40:17 +0200114 * indent_string() - returns a string for indenting with two spaces per level
115 * @level: indent level
Heinrich Schuchardt091cf432018-01-24 19:21:36 +0100116 *
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200117 * A maximum of ten indent levels is supported. Higher indent levels will be
118 * truncated.
119 *
Mario Six8fac2912018-07-10 08:40:17 +0200120 * Return: A string for indenting with two spaces per level is
121 * returned.
Rob Clarke7896c32017-07-27 08:04:19 -0400122 */
123static const char *indent_string(int level)
124{
125 const char *indent = " ";
126 const int max = strlen(indent);
Heinrich Schuchardt91064592018-02-18 15:17:49 +0100127
Rob Clarke7896c32017-07-27 08:04:19 -0400128 level = min(max, level * 2);
129 return &indent[max - level];
130}
131
Heinrich Schuchardt4d664892017-08-18 17:45:16 +0200132const char *__efi_nesting(void)
133{
134 return indent_string(nesting_level);
135}
136
Rob Clarke7896c32017-07-27 08:04:19 -0400137const char *__efi_nesting_inc(void)
138{
139 return indent_string(nesting_level++);
140}
141
142const char *__efi_nesting_dec(void)
143{
144 return indent_string(--nesting_level);
145}
146
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200147/**
Mario Six8fac2912018-07-10 08:40:17 +0200148 * efi_queue_event() - queue an EFI event
149 * @event: event to signal
150 * @check_tpl: check the TPL level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200151 *
152 * This function queues the notification function of the event for future
153 * execution.
154 *
Mario Six8fac2912018-07-10 08:40:17 +0200155 * The notification function is called if the task priority level of the event
156 * is higher than the current task priority level.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200157 *
158 * For the SignalEvent service see efi_signal_event_ext.
159 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200160 */
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100161static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200162{
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200163 if (event->notify_function) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200164 event->is_queued = true;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200165 /* Check TPL */
Heinrich Schuchardtd8b878a2018-01-19 20:24:51 +0100166 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200167 return;
Heinrich Schuchardt91e5b8a2017-09-15 10:06:10 +0200168 EFI_CALL_VOID(event->notify_function(event,
169 event->notify_context));
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200170 }
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200171 event->is_queued = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200172}
173
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200174/**
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200175 * is_valid_tpl() - check if the task priority level is valid
176 *
177 * @tpl: TPL level to check
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200178 * Return: status code
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200179 */
180efi_status_t is_valid_tpl(efi_uintn_t tpl)
181{
182 switch (tpl) {
183 case TPL_APPLICATION:
184 case TPL_CALLBACK:
185 case TPL_NOTIFY:
186 case TPL_HIGH_LEVEL:
187 return EFI_SUCCESS;
188 default:
189 return EFI_INVALID_PARAMETER;
190 }
191}
192
193/**
Mario Six8fac2912018-07-10 08:40:17 +0200194 * efi_signal_event() - signal an EFI event
195 * @event: event to signal
196 * @check_tpl: check the TPL level
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100197 *
Mario Six8fac2912018-07-10 08:40:17 +0200198 * This function signals an event. If the event belongs to an event group all
199 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100200 * their notification function is queued.
201 *
202 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100203 */
204void efi_signal_event(struct efi_event *event, bool check_tpl)
205{
206 if (event->group) {
207 struct efi_event *evt;
208
209 /*
210 * The signaled state has to set before executing any
211 * notification function
212 */
213 list_for_each_entry(evt, &efi_events, link) {
214 if (!evt->group || guidcmp(evt->group, event->group))
215 continue;
216 if (evt->is_signaled)
217 continue;
218 evt->is_signaled = true;
219 if (evt->type & EVT_NOTIFY_SIGNAL &&
220 evt->notify_function)
221 evt->is_queued = true;
222 }
223 list_for_each_entry(evt, &efi_events, link) {
224 if (!evt->group || guidcmp(evt->group, event->group))
225 continue;
226 if (evt->is_queued)
227 efi_queue_event(evt, check_tpl);
228 }
229 } else if (!event->is_signaled) {
230 event->is_signaled = true;
231 if (event->type & EVT_NOTIFY_SIGNAL)
232 efi_queue_event(event, check_tpl);
233 }
234}
235
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200236/**
Mario Six8fac2912018-07-10 08:40:17 +0200237 * efi_raise_tpl() - raise the task priority level
238 * @new_tpl: new value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200239 *
240 * This function implements the RaiseTpl service.
Mario Six8fac2912018-07-10 08:40:17 +0200241 *
242 * See the Unified Extensible Firmware Interface (UEFI) specification for
243 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200244 *
Mario Six8fac2912018-07-10 08:40:17 +0200245 * Return: old value of the task priority level
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200246 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100247static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100248{
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100249 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200250
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200251 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200252
253 if (new_tpl < efi_tpl)
254 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
255 efi_tpl = new_tpl;
256 if (efi_tpl > TPL_HIGH_LEVEL)
257 efi_tpl = TPL_HIGH_LEVEL;
258
259 EFI_EXIT(EFI_SUCCESS);
260 return old_tpl;
Alexander Grafc15d9212016-03-04 01:09:59 +0100261}
262
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200263/**
Mario Six8fac2912018-07-10 08:40:17 +0200264 * efi_restore_tpl() - lower the task priority level
265 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200266 *
267 * This function implements the RestoreTpl service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200268 *
Mario Six8fac2912018-07-10 08:40:17 +0200269 * See the Unified Extensible Firmware Interface (UEFI) specification for
270 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200271 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100272static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafc15d9212016-03-04 01:09:59 +0100273{
xypron.glpk@gmx.de48df2092017-07-18 20:17:19 +0200274 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200275
276 if (old_tpl > efi_tpl)
277 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
278 efi_tpl = old_tpl;
279 if (efi_tpl > TPL_HIGH_LEVEL)
280 efi_tpl = TPL_HIGH_LEVEL;
281
Heinrich Schuchardt59b20952018-03-24 18:40:21 +0100282 /*
283 * Lowering the TPL may have made queued events eligible for execution.
284 */
285 efi_timer_check();
286
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200287 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100288}
289
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200290/**
Mario Six8fac2912018-07-10 08:40:17 +0200291 * efi_allocate_pages_ext() - allocate memory pages
292 * @type: type of allocation to be performed
293 * @memory_type: usage type of the allocated memory
294 * @pages: number of pages to be allocated
295 * @memory: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200296 *
297 * This function implements the AllocatePages service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200298 *
Mario Six8fac2912018-07-10 08:40:17 +0200299 * See the Unified Extensible Firmware Interface (UEFI) specification for
300 * details.
301 *
302 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200303 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900304static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100305 efi_uintn_t pages,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900306 uint64_t *memory)
Alexander Grafc15d9212016-03-04 01:09:59 +0100307{
308 efi_status_t r;
309
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100310 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafc15d9212016-03-04 01:09:59 +0100311 r = efi_allocate_pages(type, memory_type, pages, memory);
312 return EFI_EXIT(r);
313}
314
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200315/**
Mario Six8fac2912018-07-10 08:40:17 +0200316 * efi_free_pages_ext() - Free memory pages.
317 * @memory: start of the memory area to be freed
318 * @pages: number of pages to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200319 *
320 * This function implements the FreePages service.
Mario Six8fac2912018-07-10 08:40:17 +0200321 *
322 * See the Unified Extensible Firmware Interface (UEFI) specification for
323 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200324 *
Mario Six8fac2912018-07-10 08:40:17 +0200325 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200326 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900327static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100328 efi_uintn_t pages)
Alexander Grafc15d9212016-03-04 01:09:59 +0100329{
330 efi_status_t r;
331
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900332 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafc15d9212016-03-04 01:09:59 +0100333 r = efi_free_pages(memory, pages);
334 return EFI_EXIT(r);
335}
336
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200337/**
Mario Six8fac2912018-07-10 08:40:17 +0200338 * efi_get_memory_map_ext() - get map describing memory usage
339 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
340 * on exit the size of the copied memory map
341 * @memory_map: buffer to which the memory map is written
342 * @map_key: key for the memory map
343 * @descriptor_size: size of an individual memory descriptor
344 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200345 *
346 * This function implements the GetMemoryMap service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200347 *
Mario Six8fac2912018-07-10 08:40:17 +0200348 * See the Unified Extensible Firmware Interface (UEFI) specification for
349 * details.
350 *
351 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200352 */
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900353static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100354 efi_uintn_t *memory_map_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900355 struct efi_mem_desc *memory_map,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100356 efi_uintn_t *map_key,
357 efi_uintn_t *descriptor_size,
Masahiro Yamadab2a05c12017-06-22 17:49:03 +0900358 uint32_t *descriptor_version)
Alexander Grafc15d9212016-03-04 01:09:59 +0100359{
360 efi_status_t r;
361
362 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
363 map_key, descriptor_size, descriptor_version);
364 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
365 descriptor_size, descriptor_version);
366 return EFI_EXIT(r);
367}
368
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200369/**
Mario Six8fac2912018-07-10 08:40:17 +0200370 * efi_allocate_pool_ext() - allocate memory from pool
371 * @pool_type: type of the pool from which memory is to be allocated
372 * @size: number of bytes to be allocated
373 * @buffer: allocated memory
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200374 *
375 * This function implements the AllocatePool service.
Mario Six8fac2912018-07-10 08:40:17 +0200376 *
377 * See the Unified Extensible Firmware Interface (UEFI) specification for
378 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200379 *
Mario Six8fac2912018-07-10 08:40:17 +0200380 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200381 */
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200382static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100383 efi_uintn_t size,
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200384 void **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100385{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100386 efi_status_t r;
387
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100388 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brüns5a09aef2016-10-09 22:17:18 +0200389 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100390 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100391}
392
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200393/**
Mario Six8fac2912018-07-10 08:40:17 +0200394 * efi_free_pool_ext() - free memory from pool
395 * @buffer: start of memory to be freed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200396 *
397 * This function implements the FreePool service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200398 *
Mario Six8fac2912018-07-10 08:40:17 +0200399 * See the Unified Extensible Firmware Interface (UEFI) specification for
400 * details.
401 *
402 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200403 */
Stefan Brüns67b67d92016-10-09 22:17:26 +0200404static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +0100405{
Alexander Graf1c34fa82016-03-24 01:37:37 +0100406 efi_status_t r;
407
408 EFI_ENTRY("%p", buffer);
Stefan Brüns67b67d92016-10-09 22:17:26 +0200409 r = efi_free_pool(buffer);
Alexander Graf1c34fa82016-03-24 01:37:37 +0100410 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +0100411}
412
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200413/**
Mario Six8fac2912018-07-10 08:40:17 +0200414 * efi_add_handle() - add a new object to the object list
415 * @obj: object to be added
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100416 *
Mario Six8fac2912018-07-10 08:40:17 +0200417 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100418 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200419void efi_add_handle(efi_handle_t handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100420{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200421 if (!handle)
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100422 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200423 INIT_LIST_HEAD(&handle->protocols);
424 list_add_tail(&handle->link, &efi_obj_list);
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100425}
426
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200427/**
Mario Six8fac2912018-07-10 08:40:17 +0200428 * efi_create_handle() - create handle
429 * @handle: new handle
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200430 *
Mario Six8fac2912018-07-10 08:40:17 +0200431 * Return: status code
Heinrich Schuchardteb6106e2017-10-26 19:25:49 +0200432 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100433efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200434{
435 struct efi_object *obj;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200436
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200437 obj = calloc(1, sizeof(struct efi_object));
438 if (!obj)
439 return EFI_OUT_OF_RESOURCES;
440
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +0100441 efi_add_handle(obj);
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200442 *handle = obj;
Heinrich Schuchardtae1d2062018-05-27 16:47:21 +0200443
444 return EFI_SUCCESS;
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +0200445}
446
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200447/**
Mario Six8fac2912018-07-10 08:40:17 +0200448 * efi_search_protocol() - find a protocol on a handle.
449 * @handle: handle
450 * @protocol_guid: GUID of the protocol
451 * @handler: reference to the protocol
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100452 *
Mario Six8fac2912018-07-10 08:40:17 +0200453 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100454 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100455efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100456 const efi_guid_t *protocol_guid,
457 struct efi_handler **handler)
458{
459 struct efi_object *efiobj;
460 struct list_head *lhandle;
461
462 if (!handle || !protocol_guid)
463 return EFI_INVALID_PARAMETER;
464 efiobj = efi_search_obj(handle);
465 if (!efiobj)
466 return EFI_INVALID_PARAMETER;
467 list_for_each(lhandle, &efiobj->protocols) {
468 struct efi_handler *protocol;
469
470 protocol = list_entry(lhandle, struct efi_handler, link);
471 if (!guidcmp(protocol->guid, protocol_guid)) {
472 if (handler)
473 *handler = protocol;
474 return EFI_SUCCESS;
475 }
476 }
477 return EFI_NOT_FOUND;
478}
479
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200480/**
Mario Six8fac2912018-07-10 08:40:17 +0200481 * efi_remove_protocol() - delete protocol from a handle
482 * @handle: handle from which the protocol shall be deleted
483 * @protocol: GUID of the protocol to be deleted
484 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100485 *
Mario Six8fac2912018-07-10 08:40:17 +0200486 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100487 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100488efi_status_t efi_remove_protocol(const efi_handle_t handle,
489 const efi_guid_t *protocol,
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100490 void *protocol_interface)
491{
492 struct efi_handler *handler;
493 efi_status_t ret;
494
495 ret = efi_search_protocol(handle, protocol, &handler);
496 if (ret != EFI_SUCCESS)
497 return ret;
498 if (guidcmp(handler->guid, protocol))
499 return EFI_INVALID_PARAMETER;
Heinrich Schuchardtb27594d2018-05-11 12:09:21 +0200500 if (handler->protocol_interface != protocol_interface)
501 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100502 list_del(&handler->link);
503 free(handler);
504 return EFI_SUCCESS;
505}
506
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200507/**
Mario Six8fac2912018-07-10 08:40:17 +0200508 * efi_remove_all_protocols() - delete all protocols from a handle
509 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100510 *
Mario Six8fac2912018-07-10 08:40:17 +0200511 * Return: status code
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100512 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100513efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100514{
515 struct efi_object *efiobj;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100516 struct efi_handler *protocol;
517 struct efi_handler *pos;
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100518
519 efiobj = efi_search_obj(handle);
520 if (!efiobj)
521 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta84731d2018-01-11 08:15:55 +0100522 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100523 efi_status_t ret;
524
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100525 ret = efi_remove_protocol(handle, protocol->guid,
526 protocol->protocol_interface);
527 if (ret != EFI_SUCCESS)
528 return ret;
529 }
530 return EFI_SUCCESS;
531}
532
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200533/**
Mario Six8fac2912018-07-10 08:40:17 +0200534 * efi_delete_handle() - delete handle
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100535 *
Mario Six8fac2912018-07-10 08:40:17 +0200536 * @obj: handle to delete
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100537 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200538void efi_delete_handle(efi_handle_t handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100539{
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200540 if (!handle)
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100541 return;
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200542 efi_remove_all_protocols(handle);
543 list_del(&handle->link);
544 free(handle);
Heinrich Schuchardt7d135462017-12-04 18:03:02 +0100545}
546
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200547/**
Mario Six8fac2912018-07-10 08:40:17 +0200548 * efi_is_event() - check if a pointer is a valid event
549 * @event: pointer to check
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100550 *
Mario Six8fac2912018-07-10 08:40:17 +0200551 * Return: status code
Alexander Grafc15d9212016-03-04 01:09:59 +0100552 */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100553static efi_status_t efi_is_event(const struct efi_event *event)
554{
555 const struct efi_event *evt;
556
557 if (!event)
558 return EFI_INVALID_PARAMETER;
559 list_for_each_entry(evt, &efi_events, link) {
560 if (evt == event)
561 return EFI_SUCCESS;
562 }
563 return EFI_INVALID_PARAMETER;
564}
Alexander Grafc15d9212016-03-04 01:09:59 +0100565
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200566/**
Mario Six8fac2912018-07-10 08:40:17 +0200567 * efi_create_event() - create an event
568 * @type: type of the event to create
569 * @notify_tpl: task priority level of the event
570 * @notify_function: notification function of the event
571 * @notify_context: pointer passed to the notification function
572 * @group: event group
573 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200574 *
575 * This function is used inside U-Boot code to create an event.
576 *
577 * For the API function implementing the CreateEvent service see
578 * efi_create_event_ext.
579 *
Mario Six8fac2912018-07-10 08:40:17 +0200580 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200581 */
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100582efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200583 void (EFIAPI *notify_function) (
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200584 struct efi_event *event,
585 void *context),
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100586 void *notify_context, efi_guid_t *group,
587 struct efi_event **event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100588{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100589 struct efi_event *evt;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200590
Jonathan Gray7758b212017-03-12 19:26:07 +1100591 if (event == NULL)
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200592 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100593
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200594 switch (type) {
595 case 0:
596 case EVT_TIMER:
597 case EVT_NOTIFY_SIGNAL:
598 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
599 case EVT_NOTIFY_WAIT:
600 case EVT_TIMER | EVT_NOTIFY_WAIT:
601 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
602 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
603 break;
604 default:
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200605 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt7f0f0052018-07-02 12:53:52 +0200606 }
Jonathan Gray7758b212017-03-12 19:26:07 +1100607
AKASHI Takahiro3f3835d2018-08-10 15:36:32 +0900608 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
609 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200610 return EFI_INVALID_PARAMETER;
Jonathan Gray7758b212017-03-12 19:26:07 +1100611
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100612 evt = calloc(1, sizeof(struct efi_event));
613 if (!evt)
614 return EFI_OUT_OF_RESOURCES;
615 evt->type = type;
616 evt->notify_tpl = notify_tpl;
617 evt->notify_function = notify_function;
618 evt->notify_context = notify_context;
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100619 evt->group = group;
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200620 /* Disable timers on boot up */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100621 evt->trigger_next = -1ULL;
622 evt->is_queued = false;
623 evt->is_signaled = false;
624 list_add_tail(&evt->link, &efi_events);
625 *event = evt;
626 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100627}
628
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200629/*
Mario Six8fac2912018-07-10 08:40:17 +0200630 * efi_create_event_ex() - create an event in a group
631 * @type: type of the event to create
632 * @notify_tpl: task priority level of the event
633 * @notify_function: notification function of the event
634 * @notify_context: pointer passed to the notification function
635 * @event: created event
636 * @event_group: event group
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100637 *
638 * This function implements the CreateEventEx service.
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100639 *
Mario Six8fac2912018-07-10 08:40:17 +0200640 * See the Unified Extensible Firmware Interface (UEFI) specification for
641 * details.
642 *
643 * Return: status code
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100644 */
645efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
646 void (EFIAPI *notify_function) (
647 struct efi_event *event,
648 void *context),
649 void *notify_context,
650 efi_guid_t *event_group,
651 struct efi_event **event)
652{
653 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
654 notify_context, event_group);
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100655 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100656 notify_context, event_group, event));
Heinrich Schuchardt717c4582018-02-04 23:05:13 +0100657}
658
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200659/**
Mario Six8fac2912018-07-10 08:40:17 +0200660 * efi_create_event_ext() - create an event
661 * @type: type of the event to create
662 * @notify_tpl: task priority level of the event
663 * @notify_function: notification function of the event
664 * @notify_context: pointer passed to the notification function
665 * @event: created event
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200666 *
667 * This function implements the CreateEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200668 *
669 * See the Unified Extensible Firmware Interface (UEFI) specification for
670 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200671 *
Mario Six8fac2912018-07-10 08:40:17 +0200672 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200673 */
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200674static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardtf8d4ec32017-11-06 21:17:47 +0100675 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200676 void (EFIAPI *notify_function) (
677 struct efi_event *event,
678 void *context),
679 void *notify_context, struct efi_event **event)
680{
681 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
682 notify_context);
683 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100684 notify_context, NULL, event));
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +0200685}
686
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200687/**
Mario Six8fac2912018-07-10 08:40:17 +0200688 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200689 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200690 * Check if a timer event has occurred or a queued notification function should
691 * be called.
692 *
Alexander Grafc15d9212016-03-04 01:09:59 +0100693 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200694 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafc15d9212016-03-04 01:09:59 +0100695 */
696void efi_timer_check(void)
697{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100698 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +0100699 u64 now = timer_get_us();
700
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100701 list_for_each_entry(evt, &efi_events, link) {
702 if (evt->is_queued)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100703 efi_queue_event(evt, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100704 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200705 continue;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100706 switch (evt->trigger_type) {
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200707 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100708 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200709 break;
710 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100711 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200712 break;
713 default:
714 continue;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200715 }
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100716 evt->is_signaled = false;
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100717 efi_signal_event(evt, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100718 }
Alexander Grafc15d9212016-03-04 01:09:59 +0100719 WATCHDOG_RESET();
720}
721
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200722/**
Mario Six8fac2912018-07-10 08:40:17 +0200723 * efi_set_timer() - set the trigger time for a timer event or stop the event
724 * @event: event for which the timer is set
725 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200726 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200727 *
728 * This is the function for internal usage in U-Boot. For the API function
729 * implementing the SetTimer service see efi_set_timer_ext.
730 *
Mario Six8fac2912018-07-10 08:40:17 +0200731 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200732 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200733efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200734 uint64_t trigger_time)
Alexander Grafc15d9212016-03-04 01:09:59 +0100735{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100736 /* Check that the event is valid */
737 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
738 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100739
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200740 /*
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200741 * The parameter defines a multiple of 100 ns.
742 * We use multiples of 1000 ns. So divide by 10.
xypron.glpk@gmx.de44c4be02017-07-18 20:17:23 +0200743 */
Heinrich Schuchardt368ca642017-10-05 16:14:14 +0200744 do_div(trigger_time, 10);
Alexander Grafc15d9212016-03-04 01:09:59 +0100745
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100746 switch (type) {
747 case EFI_TIMER_STOP:
748 event->trigger_next = -1ULL;
749 break;
750 case EFI_TIMER_PERIODIC:
751 case EFI_TIMER_RELATIVE:
752 event->trigger_next = timer_get_us() + trigger_time;
753 break;
754 default:
755 return EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +0100756 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100757 event->trigger_type = type;
758 event->trigger_time = trigger_time;
759 event->is_signaled = false;
760 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +0100761}
762
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200763/**
Mario Six8fac2912018-07-10 08:40:17 +0200764 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
765 * event
766 * @event: event for which the timer is set
767 * @type: type of the timer
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +0200768 * @trigger_time: trigger period in multiples of 100 ns
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200769 *
770 * This function implements the SetTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200771 *
Mario Six8fac2912018-07-10 08:40:17 +0200772 * See the Unified Extensible Firmware Interface (UEFI) specification for
773 * details.
774 *
775 *
776 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200777 */
xypron.glpk@gmx.de3ecc6bd2017-07-19 19:22:34 +0200778static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
779 enum efi_timer_delay type,
780 uint64_t trigger_time)
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200781{
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900782 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +0200783 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
784}
785
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200786/**
Mario Six8fac2912018-07-10 08:40:17 +0200787 * efi_wait_for_event() - wait for events to be signaled
788 * @num_events: number of events to be waited for
789 * @event: events to be waited for
790 * @index: index of the event that was signaled
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200791 *
792 * This function implements the WaitForEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200793 *
Mario Six8fac2912018-07-10 08:40:17 +0200794 * See the Unified Extensible Firmware Interface (UEFI) specification for
795 * details.
796 *
797 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200798 */
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100799static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200800 struct efi_event **event,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100801 efi_uintn_t *index)
Alexander Grafc15d9212016-03-04 01:09:59 +0100802{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100803 int i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100804
Heinrich Schuchardt798a4412017-11-06 21:17:48 +0100805 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafc15d9212016-03-04 01:09:59 +0100806
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200807 /* Check parameters */
808 if (!num_events || !event)
809 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt0c150ca2017-09-15 10:06:16 +0200810 /* Check TPL */
811 if (efi_tpl != TPL_APPLICATION)
812 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200813 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100814 if (efi_is_event(event[i]) != EFI_SUCCESS)
815 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200816 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
817 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200818 if (!event[i]->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100819 efi_queue_event(event[i], true);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200820 }
821
822 /* Wait for signal */
823 for (;;) {
824 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200825 if (event[i]->is_signaled)
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200826 goto out;
827 }
828 /* Allow events to occur. */
829 efi_timer_check();
830 }
831
832out:
833 /*
834 * Reset the signal which is passed to the caller to allow periodic
835 * events to occur.
836 */
Heinrich Schuchardt1bbee392017-10-04 15:03:24 +0200837 event[i]->is_signaled = false;
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200838 if (index)
839 *index = i;
Alexander Grafc15d9212016-03-04 01:09:59 +0100840
841 return EFI_EXIT(EFI_SUCCESS);
842}
843
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200844/**
Mario Six8fac2912018-07-10 08:40:17 +0200845 * efi_signal_event_ext() - signal an EFI event
846 * @event: event to signal
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200847 *
848 * This function implements the SignalEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200849 *
850 * See the Unified Extensible Firmware Interface (UEFI) specification for
851 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200852 *
853 * This functions sets the signaled state of the event and queues the
854 * notification function for execution.
855 *
Mario Six8fac2912018-07-10 08:40:17 +0200856 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200857 */
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200858static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100859{
860 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100861 if (efi_is_event(event) != EFI_SUCCESS)
862 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100863 efi_signal_event(event, true);
Alexander Grafc15d9212016-03-04 01:09:59 +0100864 return EFI_EXIT(EFI_SUCCESS);
865}
866
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200867/**
Mario Six8fac2912018-07-10 08:40:17 +0200868 * efi_close_event() - close an EFI event
869 * @event: event to close
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200870 *
871 * This function implements the CloseEvent service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200872 *
Mario Six8fac2912018-07-10 08:40:17 +0200873 * See the Unified Extensible Firmware Interface (UEFI) specification for
874 * details.
875 *
876 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200877 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200878static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100879{
880 EFI_ENTRY("%p", event);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100881 if (efi_is_event(event) != EFI_SUCCESS)
882 return EFI_EXIT(EFI_INVALID_PARAMETER);
883 list_del(&event->link);
884 free(event);
885 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +0100886}
887
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200888/**
Mario Six8fac2912018-07-10 08:40:17 +0200889 * efi_check_event() - check if an event is signaled
890 * @event: event to check
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200891 *
892 * This function implements the CheckEvent service.
Mario Six8fac2912018-07-10 08:40:17 +0200893 *
894 * See the Unified Extensible Firmware Interface (UEFI) specification for
895 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200896 *
Mario Six8fac2912018-07-10 08:40:17 +0200897 * If an event is not signaled yet, the notification function is queued. The
898 * signaled state is cleared.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200899 *
Mario Six8fac2912018-07-10 08:40:17 +0200900 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +0200901 */
xypron.glpk@gmx.decdbf3ac2017-07-18 20:17:17 +0200902static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafc15d9212016-03-04 01:09:59 +0100903{
904 EFI_ENTRY("%p", event);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200905 efi_timer_check();
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100906 if (efi_is_event(event) != EFI_SUCCESS ||
907 event->type & EVT_NOTIFY_SIGNAL)
908 return EFI_EXIT(EFI_INVALID_PARAMETER);
909 if (!event->is_signaled)
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +0100910 efi_queue_event(event, true);
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100911 if (event->is_signaled) {
912 event->is_signaled = false;
913 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +0200914 }
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +0100915 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafc15d9212016-03-04 01:09:59 +0100916}
917
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200918/**
Mario Six8fac2912018-07-10 08:40:17 +0200919 * efi_search_obj() - find the internal EFI object for a handle
920 * @handle: handle to find
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200921 *
Mario Six8fac2912018-07-10 08:40:17 +0200922 * Return: EFI object
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200923 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100924struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200925{
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100926 struct efi_object *efiobj;
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200927
Heinrich Schuchardt274cc872017-11-06 21:17:50 +0100928 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200929 if (efiobj == handle)
Heinrich Schuchardt37ebcba2017-10-18 18:13:03 +0200930 return efiobj;
931 }
932
933 return NULL;
934}
935
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200936/**
Mario Six8fac2912018-07-10 08:40:17 +0200937 * efi_open_protocol_info_entry() - create open protocol info entry and add it
938 * to a protocol
939 * @handler: handler of a protocol
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100940 *
Mario Six8fac2912018-07-10 08:40:17 +0200941 * Return: open protocol info entry
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100942 */
943static struct efi_open_protocol_info_entry *efi_create_open_info(
944 struct efi_handler *handler)
945{
946 struct efi_open_protocol_info_item *item;
947
948 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
949 if (!item)
950 return NULL;
951 /* Append the item to the open protocol info list. */
952 list_add_tail(&item->link, &handler->open_infos);
953
954 return &item->info;
955}
956
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200957/**
Mario Six8fac2912018-07-10 08:40:17 +0200958 * efi_delete_open_info() - remove an open protocol info entry from a protocol
959 * @item: open protocol info entry to delete
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100960 *
Mario Six8fac2912018-07-10 08:40:17 +0200961 * Return: status code
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100962 */
963static efi_status_t efi_delete_open_info(
964 struct efi_open_protocol_info_item *item)
965{
966 list_del(&item->link);
967 free(item);
968 return EFI_SUCCESS;
969}
970
Heinrich Schuchardt59999172018-05-11 18:15:41 +0200971/**
Mario Six8fac2912018-07-10 08:40:17 +0200972 * efi_add_protocol() - install new protocol on a handle
973 * @handle: handle on which the protocol shall be installed
974 * @protocol: GUID of the protocol to be installed
975 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200976 *
Mario Six8fac2912018-07-10 08:40:17 +0200977 * Return: status code
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200978 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +0100979efi_status_t efi_add_protocol(const efi_handle_t handle,
980 const efi_guid_t *protocol,
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200981 void *protocol_interface)
982{
983 struct efi_object *efiobj;
984 struct efi_handler *handler;
985 efi_status_t ret;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +0200986
987 efiobj = efi_search_obj(handle);
988 if (!efiobj)
989 return EFI_INVALID_PARAMETER;
990 ret = efi_search_protocol(handle, protocol, NULL);
991 if (ret != EFI_NOT_FOUND)
992 return EFI_INVALID_PARAMETER;
993 handler = calloc(1, sizeof(struct efi_handler));
994 if (!handler)
995 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100996 handler->guid = protocol;
997 handler->protocol_interface = protocol_interface;
Heinrich Schuchardta277d3a2018-01-11 08:15:57 +0100998 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +0100999 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardt3d2abc32018-01-11 08:16:01 +01001000 if (!guidcmp(&efi_guid_device_path, protocol))
1001 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01001002 return EFI_SUCCESS;
Heinrich Schuchardt5aef61d2017-10-26 19:25:53 +02001003}
1004
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001005/**
Mario Six8fac2912018-07-10 08:40:17 +02001006 * efi_install_protocol_interface() - install protocol interface
1007 * @handle: handle on which the protocol shall be installed
1008 * @protocol: GUID of the protocol to be installed
1009 * @protocol_interface_type: type of the interface to be installed,
1010 * always EFI_NATIVE_INTERFACE
1011 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001012 *
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001013 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001014 *
Mario Six8fac2912018-07-10 08:40:17 +02001015 * See the Unified Extensible Firmware Interface (UEFI) specification for
1016 * details.
1017 *
1018 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001019 */
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001020static efi_status_t EFIAPI efi_install_protocol_interface(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02001021 efi_handle_t *handle, const efi_guid_t *protocol,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001022 int protocol_interface_type, void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001023{
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001024 efi_status_t r;
1025
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001026 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1027 protocol_interface);
1028
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001029 if (!handle || !protocol ||
1030 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1031 r = EFI_INVALID_PARAMETER;
1032 goto out;
1033 }
1034
1035 /* Create new handle if requested. */
1036 if (!*handle) {
Heinrich Schuchardtcd522cb2017-08-27 00:51:09 +02001037 r = efi_create_handle(handle);
1038 if (r != EFI_SUCCESS)
1039 goto out;
Heinrich Schuchardt50f02102017-10-26 19:25:43 +02001040 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1041 *handle);
1042 } else {
1043 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1044 *handle);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001045 }
Heinrich Schuchardt865d5f32017-10-26 19:25:54 +02001046 /* Add new protocol */
1047 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001048out:
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01001049 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01001050}
xypron.glpk@gmx.de0581fa82017-07-11 22:06:16 +02001051
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001052/**
Mario Six8fac2912018-07-10 08:40:17 +02001053 * efi_get_drivers() - get all drivers associated to a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001054 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001055 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001056 * @number_of_drivers: number of child controllers
1057 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001058 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001059 * The allocated buffer has to be freed with free().
1060 *
Mario Six8fac2912018-07-10 08:40:17 +02001061 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001062 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001063static efi_status_t efi_get_drivers(efi_handle_t handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001064 const efi_guid_t *protocol,
1065 efi_uintn_t *number_of_drivers,
1066 efi_handle_t **driver_handle_buffer)
1067{
1068 struct efi_handler *handler;
1069 struct efi_open_protocol_info_item *item;
1070 efi_uintn_t count = 0, i;
1071 bool duplicate;
1072
1073 /* Count all driver associations */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001074 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001075 if (protocol && guidcmp(handler->guid, protocol))
1076 continue;
1077 list_for_each_entry(item, &handler->open_infos, link) {
1078 if (item->info.attributes &
1079 EFI_OPEN_PROTOCOL_BY_DRIVER)
1080 ++count;
1081 }
1082 }
1083 /*
1084 * Create buffer. In case of duplicate driver assignments the buffer
1085 * will be too large. But that does not harm.
1086 */
1087 *number_of_drivers = 0;
1088 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1089 if (!*driver_handle_buffer)
1090 return EFI_OUT_OF_RESOURCES;
1091 /* Collect unique driver handles */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001092 list_for_each_entry(handler, &handle->protocols, link) {
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001093 if (protocol && guidcmp(handler->guid, protocol))
1094 continue;
1095 list_for_each_entry(item, &handler->open_infos, link) {
1096 if (item->info.attributes &
1097 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1098 /* Check this is a new driver */
1099 duplicate = false;
1100 for (i = 0; i < *number_of_drivers; ++i) {
1101 if ((*driver_handle_buffer)[i] ==
1102 item->info.agent_handle)
1103 duplicate = true;
1104 }
1105 /* Copy handle to buffer */
1106 if (!duplicate) {
1107 i = (*number_of_drivers)++;
1108 (*driver_handle_buffer)[i] =
1109 item->info.agent_handle;
1110 }
1111 }
1112 }
1113 }
1114 return EFI_SUCCESS;
1115}
1116
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001117/**
Mario Six8fac2912018-07-10 08:40:17 +02001118 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001119 * @handle: handle of the controller
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001120 * @protocol: protocol GUID (optional)
Mario Six8fac2912018-07-10 08:40:17 +02001121 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001122 *
1123 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02001124 *
1125 * See the Unified Extensible Firmware Interface (UEFI) specification for
1126 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001127 *
Mario Six8fac2912018-07-10 08:40:17 +02001128 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001129 */
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001130static efi_status_t efi_disconnect_all_drivers
1131 (efi_handle_t handle,
1132 const efi_guid_t *protocol,
1133 efi_handle_t child_handle)
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001134{
1135 efi_uintn_t number_of_drivers;
1136 efi_handle_t *driver_handle_buffer;
1137 efi_status_t r, ret;
1138
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001139 ret = efi_get_drivers(handle, protocol, &number_of_drivers,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001140 &driver_handle_buffer);
1141 if (ret != EFI_SUCCESS)
1142 return ret;
1143
1144 ret = EFI_NOT_FOUND;
1145 while (number_of_drivers) {
1146 r = EFI_CALL(efi_disconnect_controller(
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001147 handle,
Heinrich Schuchardte9943282018-01-11 08:16:04 +01001148 driver_handle_buffer[--number_of_drivers],
1149 child_handle));
1150 if (r == EFI_SUCCESS)
1151 ret = r;
1152 }
1153 free(driver_handle_buffer);
1154 return ret;
1155}
1156
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001157/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001158 * efi_uninstall_protocol() - uninstall protocol interface
1159 *
Mario Six8fac2912018-07-10 08:40:17 +02001160 * @handle: handle from which the protocol shall be removed
1161 * @protocol: GUID of the protocol to be removed
1162 * @protocol_interface: interface to be removed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001163 *
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001164 * This function DOES NOT delete a handle without installed protocol.
Mario Six8fac2912018-07-10 08:40:17 +02001165 *
1166 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001167 */
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001168static efi_status_t efi_uninstall_protocol
1169 (efi_handle_t handle, const efi_guid_t *protocol,
1170 void *protocol_interface)
Alexander Grafc15d9212016-03-04 01:09:59 +01001171{
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001172 struct efi_object *efiobj;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001173 struct efi_handler *handler;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001174 struct efi_open_protocol_info_item *item;
1175 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001176 efi_status_t r;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001177
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001178 /* Check handle */
1179 efiobj = efi_search_obj(handle);
1180 if (!efiobj) {
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001181 r = EFI_INVALID_PARAMETER;
1182 goto out;
1183 }
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001184 /* Find the protocol on the handle */
1185 r = efi_search_protocol(handle, protocol, &handler);
1186 if (r != EFI_SUCCESS)
1187 goto out;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001188 /* Disconnect controllers */
1189 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1190 if (!list_empty(&handler->open_infos)) {
1191 r = EFI_ACCESS_DENIED;
1192 goto out;
1193 }
1194 /* Close protocol */
1195 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1196 if (item->info.attributes ==
1197 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1198 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1199 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1200 list_del(&item->link);
1201 }
1202 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt7538c272017-10-26 19:25:56 +02001203 r = EFI_ACCESS_DENIED;
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001204 goto out;
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001205 }
Heinrich Schuchardt86637f32018-01-11 08:16:05 +01001206 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de2cfad482017-07-11 22:06:17 +02001207out:
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001208 return r;
Alexander Grafc15d9212016-03-04 01:09:59 +01001209}
1210
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001211/**
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02001212 * efi_uninstall_protocol_interface() - uninstall protocol interface
1213 * @handle: handle from which the protocol shall be removed
1214 * @protocol: GUID of the protocol to be removed
1215 * @protocol_interface: interface to be removed
1216 *
1217 * This function implements the UninstallProtocolInterface service.
1218 *
1219 * See the Unified Extensible Firmware Interface (UEFI) specification for
1220 * details.
1221 *
1222 * Return: status code
1223 */
1224static efi_status_t EFIAPI efi_uninstall_protocol_interface
1225 (efi_handle_t handle, const efi_guid_t *protocol,
1226 void *protocol_interface)
1227{
1228 efi_status_t ret;
1229
1230 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1231
1232 ret = efi_uninstall_protocol(handle, protocol, protocol_interface);
1233 if (ret != EFI_SUCCESS)
1234 goto out;
1235
1236 /* If the last protocol has been removed, delete the handle. */
1237 if (list_empty(&handle->protocols)) {
1238 list_del(&handle->link);
1239 free(handle);
1240 }
1241out:
1242 return EFI_EXIT(ret);
1243}
1244
1245/**
Mario Six8fac2912018-07-10 08:40:17 +02001246 * efi_register_protocol_notify() - register an event for notification when a
1247 * protocol is installed.
1248 * @protocol: GUID of the protocol whose installation shall be notified
1249 * @event: event to be signaled upon installation of the protocol
1250 * @registration: key for retrieving the registration information
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001251 *
1252 * This function implements the RegisterProtocolNotify service.
1253 * See the Unified Extensible Firmware Interface (UEFI) specification
1254 * for details.
1255 *
Mario Six8fac2912018-07-10 08:40:17 +02001256 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001257 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001258static efi_status_t EFIAPI efi_register_protocol_notify(
1259 const efi_guid_t *protocol,
1260 struct efi_event *event,
1261 void **registration)
Alexander Grafc15d9212016-03-04 01:09:59 +01001262{
Rob Clark238f88c2017-09-13 18:05:41 -04001263 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafc15d9212016-03-04 01:09:59 +01001264 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1265}
1266
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001267/**
Mario Six8fac2912018-07-10 08:40:17 +02001268 * efi_search() - determine if an EFI handle implements a protocol
1269 * @search_type: selection criterion
1270 * @protocol: GUID of the protocol
1271 * @search_key: registration key
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001272 * @handle: handle
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001273 *
1274 * See the documentation of the LocateHandle service in the UEFI specification.
1275 *
Mario Six8fac2912018-07-10 08:40:17 +02001276 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001277 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001278static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001279 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001280 efi_handle_t handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001281{
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001282 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001283
1284 switch (search_type) {
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001285 case ALL_HANDLES:
Alexander Grafc15d9212016-03-04 01:09:59 +01001286 return 0;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001287 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001288 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafc15d9212016-03-04 01:09:59 +01001289 return -1;
Heinrich Schuchardt68845f02017-11-06 21:17:42 +01001290 case BY_PROTOCOL:
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001291 ret = efi_search_protocol(handle, protocol, NULL);
Heinrich Schuchardt6a430752017-10-26 19:25:55 +02001292 return (ret != EFI_SUCCESS);
1293 default:
1294 /* Invalid search type */
Alexander Grafc15d9212016-03-04 01:09:59 +01001295 return -1;
1296 }
Alexander Grafc15d9212016-03-04 01:09:59 +01001297}
1298
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001299/**
Mario Six8fac2912018-07-10 08:40:17 +02001300 * efi_locate_handle() - locate handles implementing a protocol
1301 * @search_type: selection criterion
1302 * @protocol: GUID of the protocol
1303 * @search_key: registration key
1304 * @buffer_size: size of the buffer to receive the handles in bytes
1305 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001306 *
1307 * This function is meant for U-Boot internal calls. For the API implementation
1308 * of the LocateHandle service see efi_locate_handle_ext.
1309 *
Mario Six8fac2912018-07-10 08:40:17 +02001310 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001311 */
xypron.glpk@gmx.decab4dd52017-08-09 20:55:00 +02001312static efi_status_t efi_locate_handle(
Alexander Grafc15d9212016-03-04 01:09:59 +01001313 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001314 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001315 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01001316{
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001317 struct efi_object *efiobj;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001318 efi_uintn_t size = 0;
Alexander Grafc15d9212016-03-04 01:09:59 +01001319
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001320 /* Check parameters */
1321 switch (search_type) {
1322 case ALL_HANDLES:
1323 break;
1324 case BY_REGISTER_NOTIFY:
1325 if (!search_key)
1326 return EFI_INVALID_PARAMETER;
1327 /* RegisterProtocolNotify is not implemented yet */
1328 return EFI_UNSUPPORTED;
1329 case BY_PROTOCOL:
1330 if (!protocol)
1331 return EFI_INVALID_PARAMETER;
1332 break;
1333 default:
1334 return EFI_INVALID_PARAMETER;
1335 }
1336
1337 /*
1338 * efi_locate_handle_buffer uses this function for
1339 * the calculation of the necessary buffer size.
1340 * So do not require a buffer for buffersize == 0.
1341 */
1342 if (!buffer_size || (*buffer_size && !buffer))
1343 return EFI_INVALID_PARAMETER;
1344
Alexander Grafc15d9212016-03-04 01:09:59 +01001345 /* Count how much space we need */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001346 list_for_each_entry(efiobj, &efi_obj_list, link) {
1347 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001348 size += sizeof(void *);
Alexander Grafc15d9212016-03-04 01:09:59 +01001349 }
1350
1351 if (*buffer_size < size) {
1352 *buffer_size = size;
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001353 return EFI_BUFFER_TOO_SMALL;
Alexander Grafc15d9212016-03-04 01:09:59 +01001354 }
1355
Rob Clarkcdee3372017-08-06 14:10:07 -04001356 *buffer_size = size;
1357 if (size == 0)
1358 return EFI_NOT_FOUND;
1359
Alexander Grafc15d9212016-03-04 01:09:59 +01001360 /* Then fill the array */
Heinrich Schuchardtec66fc82017-11-06 21:17:49 +01001361 list_for_each_entry(efiobj, &efi_obj_list, link) {
1362 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02001363 *buffer++ = efiobj;
Alexander Grafc15d9212016-03-04 01:09:59 +01001364 }
1365
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001366 return EFI_SUCCESS;
1367}
1368
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001369/**
Mario Six8fac2912018-07-10 08:40:17 +02001370 * efi_locate_handle_ext() - locate handles implementing a protocol.
1371 * @search_type: selection criterion
1372 * @protocol: GUID of the protocol
1373 * @search_key: registration key
1374 * @buffer_size: size of the buffer to receive the handles in bytes
1375 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001376 *
1377 * This function implements the LocateHandle service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001378 *
Mario Six8fac2912018-07-10 08:40:17 +02001379 * See the Unified Extensible Firmware Interface (UEFI) specification for
1380 * details.
1381 *
1382 * Return: 0 if the handle implements the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001383 */
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001384static efi_status_t EFIAPI efi_locate_handle_ext(
1385 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001386 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01001387 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001388{
Rob Clark238f88c2017-09-13 18:05:41 -04001389 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02001390 buffer_size, buffer);
1391
1392 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1393 buffer_size, buffer));
Alexander Grafc15d9212016-03-04 01:09:59 +01001394}
1395
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001396/**
Mario Six8fac2912018-07-10 08:40:17 +02001397 * efi_remove_configuration_table() - collapses configuration table entries,
1398 * removing index i
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001399 *
Mario Six8fac2912018-07-10 08:40:17 +02001400 * @i: index of the table entry to be removed
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001401 */
Alexander Graffe3366f2017-07-26 13:41:04 +02001402static void efi_remove_configuration_table(int i)
1403{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001404 struct efi_configuration_table *this = &systab.tables[i];
1405 struct efi_configuration_table *next = &systab.tables[i + 1];
1406 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Graffe3366f2017-07-26 13:41:04 +02001407
1408 memmove(this, next, (ulong)end - (ulong)next);
1409 systab.nr_tables--;
1410}
1411
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001412/**
Mario Six8fac2912018-07-10 08:40:17 +02001413 * efi_install_configuration_table() - adds, updates, or removes a
1414 * configuration table
1415 * @guid: GUID of the installed table
1416 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001417 *
1418 * This function is used for internal calls. For the API implementation of the
1419 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1420 *
Mario Six8fac2912018-07-10 08:40:17 +02001421 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001422 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001423efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1424 void *table)
Alexander Grafc15d9212016-03-04 01:09:59 +01001425{
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001426 struct efi_event *evt;
Alexander Grafc15d9212016-03-04 01:09:59 +01001427 int i;
1428
Heinrich Schuchardt754ce102018-02-18 00:08:00 +01001429 if (!guid)
1430 return EFI_INVALID_PARAMETER;
1431
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001432 /* Check for GUID override */
Alexander Grafc15d9212016-03-04 01:09:59 +01001433 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001434 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Graffe3366f2017-07-26 13:41:04 +02001435 if (table)
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001436 systab.tables[i].table = table;
Alexander Graffe3366f2017-07-26 13:41:04 +02001437 else
1438 efi_remove_configuration_table(i);
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001439 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01001440 }
1441 }
1442
Alexander Graffe3366f2017-07-26 13:41:04 +02001443 if (!table)
1444 return EFI_NOT_FOUND;
1445
Alexander Grafc15d9212016-03-04 01:09:59 +01001446 /* No override, check for overflow */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001447 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Grafc5c11632016-08-19 01:23:24 +02001448 return EFI_OUT_OF_RESOURCES;
Alexander Grafc15d9212016-03-04 01:09:59 +01001449
1450 /* Add a new entry */
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02001451 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1452 systab.tables[i].table = table;
Alexander Graf9982e672016-08-19 01:23:30 +02001453 systab.nr_tables = i + 1;
Alexander Grafc15d9212016-03-04 01:09:59 +01001454
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001455out:
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001456 /* systab.nr_tables may have changed. So we need to update the CRC32 */
Heinrich Schuchardtac2d4da2018-07-07 15:36:05 +02001457 efi_update_table_header_crc32(&systab.hdr);
1458
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001459 /* Notify that the configuration table was changed */
1460 list_for_each_entry(evt, &efi_events, link) {
1461 if (evt->group && !guidcmp(evt->group, guid)) {
1462 efi_signal_event(evt, false);
1463 break;
1464 }
1465 }
1466
Alexander Grafc5c11632016-08-19 01:23:24 +02001467 return EFI_SUCCESS;
Alexander Grafc15d9212016-03-04 01:09:59 +01001468}
1469
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001470/**
Mario Six8fac2912018-07-10 08:40:17 +02001471 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1472 * configuration table.
1473 * @guid: GUID of the installed table
1474 * @table: table to be installed
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001475 *
1476 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001477 *
Mario Six8fac2912018-07-10 08:40:17 +02001478 * See the Unified Extensible Firmware Interface (UEFI) specification for
1479 * details.
1480 *
1481 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001482 */
Alexander Grafc5c11632016-08-19 01:23:24 +02001483static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1484 void *table)
1485{
Rob Clark238f88c2017-09-13 18:05:41 -04001486 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Grafc5c11632016-08-19 01:23:24 +02001487 return EFI_EXIT(efi_install_configuration_table(guid, table));
1488}
1489
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001490/**
Mario Six8fac2912018-07-10 08:40:17 +02001491 * efi_setup_loaded_image() - initialize a loaded image
1492 * @info: loaded image info to be passed to the entry point of the image
1493 * @obj: internal object associated with the loaded image
1494 * @device_path: device path of the loaded image
1495 * @file_path: file path of the loaded image
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001496 *
1497 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clarkf8db9222017-09-13 18:05:33 -04001498 * protocols, boot-device, etc.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001499 *
Mario Six8fac2912018-07-10 08:40:17 +02001500 * Return: status code
Rob Clarkf8db9222017-09-13 18:05:33 -04001501 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001502efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path,
1503 struct efi_device_path *file_path,
1504 struct efi_loaded_image_obj **handle_ptr,
1505 struct efi_loaded_image **info_ptr)
Rob Clarkf8db9222017-09-13 18:05:33 -04001506{
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001507 efi_status_t ret;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001508 struct efi_loaded_image *info;
1509 struct efi_loaded_image_obj *obj;
1510
1511 info = calloc(1, sizeof(*info));
1512 if (!info)
1513 return EFI_OUT_OF_RESOURCES;
1514 obj = calloc(1, sizeof(*obj));
1515 if (!obj) {
1516 free(info);
1517 return EFI_OUT_OF_RESOURCES;
1518 }
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001519
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001520 /* Add internal object to object list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001521 efi_add_handle(&obj->header);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001522
1523 if (info_ptr)
1524 *info_ptr = info;
1525 if (handle_ptr)
1526 *handle_ptr = obj;
Rob Clarkf8db9222017-09-13 18:05:33 -04001527
Heinrich Schuchardtc47f8702018-07-05 08:17:58 +02001528 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001529 info->file_path = file_path;
Heinrich Schuchardt8a7e09d2018-08-26 15:31:52 +02001530 info->system_table = &systab;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001531
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001532 if (device_path) {
1533 info->device_handle = efi_dp_find_obj(device_path, NULL);
1534 /*
1535 * When asking for the device path interface, return
1536 * bootefi_device_path
1537 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001538 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001539 &efi_guid_device_path, device_path);
Heinrich Schuchardtf3996be2018-01-26 06:50:54 +01001540 if (ret != EFI_SUCCESS)
1541 goto failure;
1542 }
Rob Clarkf8db9222017-09-13 18:05:33 -04001543
1544 /*
1545 * When asking for the loaded_image interface, just
1546 * return handle which points to loaded_image_info
1547 */
Heinrich Schuchardt72928722018-09-26 05:27:56 +02001548 ret = efi_add_protocol(&obj->header,
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001549 &efi_guid_loaded_image, info);
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001550 if (ret != EFI_SUCCESS)
1551 goto failure;
Rob Clarkf8db9222017-09-13 18:05:33 -04001552
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001553 return ret;
Heinrich Schuchardt24d3a662017-10-26 19:25:58 +02001554failure:
1555 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt7db9f892017-12-04 18:03:01 +01001556 return ret;
Rob Clarkf8db9222017-09-13 18:05:33 -04001557}
1558
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001559/**
Mario Six8fac2912018-07-10 08:40:17 +02001560 * efi_load_image_from_path() - load an image using a file path
1561 * @file_path: the path of the image to load
1562 * @buffer: buffer containing the loaded image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001563 *
Mario Six8fac2912018-07-10 08:40:17 +02001564 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001565 */
Rob Clarkc84c1102017-09-13 18:05:38 -04001566efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1567 void **buffer)
Rob Clark857a1222017-09-13 18:05:35 -04001568{
1569 struct efi_file_info *info = NULL;
1570 struct efi_file_handle *f;
1571 static efi_status_t ret;
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001572 efi_uintn_t bs;
Rob Clark857a1222017-09-13 18:05:35 -04001573
1574 f = efi_file_from_path(file_path);
1575 if (!f)
1576 return EFI_DEVICE_ERROR;
1577
1578 bs = 0;
1579 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1580 &bs, info));
1581 if (ret == EFI_BUFFER_TOO_SMALL) {
1582 info = malloc(bs);
1583 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1584 &bs, info));
1585 }
1586 if (ret != EFI_SUCCESS)
1587 goto error;
1588
1589 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1590 if (ret)
1591 goto error;
1592
Heinrich Schuchardt6b06e0d2018-04-03 22:37:11 +02001593 bs = info->file_size;
1594 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark857a1222017-09-13 18:05:35 -04001595
1596error:
1597 free(info);
1598 EFI_CALL(f->close(f));
1599
1600 if (ret != EFI_SUCCESS) {
1601 efi_free_pool(*buffer);
1602 *buffer = NULL;
1603 }
1604
1605 return ret;
1606}
1607
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001608/**
Mario Six8fac2912018-07-10 08:40:17 +02001609 * efi_load_image() - load an EFI image into memory
1610 * @boot_policy: true for request originating from the boot manager
1611 * @parent_image: the caller's image handle
1612 * @file_path: the path of the image to load
1613 * @source_buffer: memory location from which the image is installed
1614 * @source_size: size of the memory area from which the image is installed
1615 * @image_handle: handle for the newly installed image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001616 *
1617 * This function implements the LoadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001618 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001619 * See the Unified Extensible Firmware Interface (UEFI) specification
1620 * for details.
1621 *
Mario Six8fac2912018-07-10 08:40:17 +02001622 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001623 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001624static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1625 efi_handle_t parent_image,
1626 struct efi_device_path *file_path,
1627 void *source_buffer,
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001628 efi_uintn_t source_size,
Alexander Grafc15d9212016-03-04 01:09:59 +01001629 efi_handle_t *image_handle)
1630{
Tom Rini04c49062018-09-30 10:38:15 -04001631 struct efi_loaded_image *info = NULL;
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001632 struct efi_loaded_image_obj **image_obj =
1633 (struct efi_loaded_image_obj **)image_handle;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001634 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001635
Heinrich Schuchardt4cde1fa2018-04-03 22:29:30 +02001636 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafc15d9212016-03-04 01:09:59 +01001637 file_path, source_buffer, source_size, image_handle);
Rob Clark857a1222017-09-13 18:05:35 -04001638
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001639 if (!image_handle || !parent_image) {
1640 ret = EFI_INVALID_PARAMETER;
1641 goto error;
1642 }
1643
1644 if (!source_buffer && !file_path) {
1645 ret = EFI_NOT_FOUND;
1646 goto error;
1647 }
Rob Clark857a1222017-09-13 18:05:35 -04001648
1649 if (!source_buffer) {
1650 struct efi_device_path *dp, *fp;
Rob Clark857a1222017-09-13 18:05:35 -04001651
Rob Clarkc84c1102017-09-13 18:05:38 -04001652 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001653 if (ret != EFI_SUCCESS)
1654 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001655 /*
1656 * split file_path which contains both the device and
1657 * file parts:
1658 */
1659 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001660 ret = efi_setup_loaded_image(dp, fp, image_obj, &info);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001661 if (ret != EFI_SUCCESS)
1662 goto failure;
Rob Clark857a1222017-09-13 18:05:35 -04001663 } else {
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001664 /* In this case, file_path is the "device" path, i.e.
Rob Clark857a1222017-09-13 18:05:35 -04001665 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1666 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001667 ret = efi_setup_loaded_image(file_path, NULL, image_obj, &info);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001668 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001669 goto error;
Rob Clark857a1222017-09-13 18:05:35 -04001670 }
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001671 (*image_obj)->entry = efi_load_pe(*image_obj, source_buffer, info);
1672 if (!(*image_obj)->entry) {
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001673 ret = EFI_UNSUPPORTED;
1674 goto failure;
Alexander Grafc15d9212016-03-04 01:09:59 +01001675 }
Heinrich Schuchardt1c176932017-10-18 18:13:20 +02001676 info->system_table = &systab;
1677 info->parent_handle = parent_image;
Alexander Grafc15d9212016-03-04 01:09:59 +01001678 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001679failure:
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001680 efi_delete_handle(*image_handle);
1681 *image_handle = NULL;
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001682 free(info);
Heinrich Schuchardtc935c2f2018-03-07 02:40:51 +01001683error:
Heinrich Schuchardtd4d7ca92017-12-04 18:03:03 +01001684 return EFI_EXIT(ret);
Alexander Grafc15d9212016-03-04 01:09:59 +01001685}
1686
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001687/**
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001688 * efi_start_image() - call the entry point of an image
Mario Six8fac2912018-07-10 08:40:17 +02001689 * @image_handle: handle of the image
1690 * @exit_data_size: size of the buffer
1691 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001692 *
1693 * This function implements the StartImage service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001694 *
Mario Six8fac2912018-07-10 08:40:17 +02001695 * See the Unified Extensible Firmware Interface (UEFI) specification for
1696 * details.
1697 *
1698 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001699 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001700static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1701 unsigned long *exit_data_size,
1702 s16 **exit_data)
1703{
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001704 struct efi_loaded_image_obj *image_obj =
1705 (struct efi_loaded_image_obj *)image_handle;
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001706 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01001707
1708 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
Alexander Grafc15d9212016-03-04 01:09:59 +01001709
Alexander Grafc15d9212016-03-04 01:09:59 +01001710 /* call the image! */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001711 if (setjmp(&image_obj->exit_jmp)) {
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001712 /*
1713 * We called the entry point of the child image with EFI_CALL
1714 * in the lines below. The child image called the Exit() boot
1715 * service efi_exit() which executed the long jump that brought
1716 * us to the current line. This implies that the second half
1717 * of the EFI_CALL macro has not been executed.
1718 */
1719#ifdef CONFIG_ARM
1720 /*
1721 * efi_exit() called efi_restore_gd(). We have to undo this
1722 * otherwise __efi_entry_check() will put the wrong value into
1723 * app_gd.
1724 */
1725 gd = app_gd;
1726#endif
1727 /*
1728 * To get ready to call EFI_EXIT below we have to execute the
1729 * missed out steps of EFI_CALL.
1730 */
1731 assert(__efi_entry_check());
1732 debug("%sEFI: %lu returned by started image\n",
1733 __efi_nesting_dec(),
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001734 (unsigned long)((uintptr_t)image_obj->exit_status &
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001735 ~EFI_ERROR_MASK));
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001736 return EFI_EXIT(image_obj->exit_status);
Alexander Graf988c0662016-05-20 23:28:23 +02001737 }
1738
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001739 ret = EFI_CALL(image_obj->entry(image_handle, &systab));
Alexander Grafc15d9212016-03-04 01:09:59 +01001740
Alexander Grafeff317c2018-01-26 00:47:53 +01001741 /*
1742 * Usually UEFI applications call Exit() instead of returning.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001743 * But because the world doesn't consist of ponies and unicorns,
Alexander Grafeff317c2018-01-26 00:47:53 +01001744 * we're happy to emulate that behavior on behalf of a payload
1745 * that forgot.
1746 */
1747 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafc15d9212016-03-04 01:09:59 +01001748}
1749
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001750/**
Mario Six8fac2912018-07-10 08:40:17 +02001751 * efi_exit() - leave an EFI application or driver
1752 * @image_handle: handle of the application or driver that is exiting
1753 * @exit_status: status code
1754 * @exit_data_size: size of the buffer in bytes
1755 * @exit_data: buffer with data describing an error
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001756 *
1757 * This function implements the Exit service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001758 *
Mario Six8fac2912018-07-10 08:40:17 +02001759 * See the Unified Extensible Firmware Interface (UEFI) specification for
1760 * details.
1761 *
1762 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001763 */
Alexander Graf988c0662016-05-20 23:28:23 +02001764static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001765 efi_status_t exit_status,
1766 unsigned long exit_data_size,
1767 int16_t *exit_data)
Alexander Grafc15d9212016-03-04 01:09:59 +01001768{
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001769 /*
Heinrich Schuchardt967d7de2017-11-26 14:05:23 +01001770 * TODO: We should call the unload procedure of the loaded
1771 * image protocol.
1772 */
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001773 struct efi_loaded_image_obj *image_obj =
1774 (struct efi_loaded_image_obj *)image_handle;
Alexander Graf988c0662016-05-20 23:28:23 +02001775
Alexander Grafc15d9212016-03-04 01:09:59 +01001776 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1777 exit_data_size, exit_data);
Alexander Graf988c0662016-05-20 23:28:23 +02001778
Alexander Graf87e787a2017-09-03 14:14:17 +02001779 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt6dcd4842018-01-18 20:28:43 +01001780 EFI_EXIT(exit_status);
Heinrich Schuchardt4a4c6462017-08-25 19:53:14 +02001781
Alexander Graf87e787a2017-09-03 14:14:17 +02001782 /*
1783 * But longjmp out with the U-Boot gd, not the application's, as
1784 * the other end is a setjmp call inside EFI context.
1785 */
1786 efi_restore_gd();
1787
Heinrich Schuchardt3c3a7352018-09-23 17:21:51 +02001788 image_obj->exit_status = exit_status;
1789 longjmp(&image_obj->exit_jmp, 1);
Alexander Graf988c0662016-05-20 23:28:23 +02001790
1791 panic("EFI application exited");
Alexander Grafc15d9212016-03-04 01:09:59 +01001792}
1793
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001794/**
Mario Six8fac2912018-07-10 08:40:17 +02001795 * efi_unload_image() - unload an EFI image
1796 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001797 *
1798 * This function implements the UnloadImage service.
Mario Six8fac2912018-07-10 08:40:17 +02001799 *
1800 * See the Unified Extensible Firmware Interface (UEFI) specification for
1801 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001802 *
Mario Six8fac2912018-07-10 08:40:17 +02001803 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001804 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001805static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001806{
1807 struct efi_object *efiobj;
1808
1809 EFI_ENTRY("%p", image_handle);
1810 efiobj = efi_search_obj(image_handle);
1811 if (efiobj)
1812 list_del(&efiobj->link);
1813
1814 return EFI_EXIT(EFI_SUCCESS);
1815}
1816
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001817/**
Mario Six8fac2912018-07-10 08:40:17 +02001818 * efi_exit_boot_services() - stop all boot services
1819 * @image_handle: handle of the loaded image
1820 * @map_key: key of the memory map
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001821 *
1822 * This function implements the ExitBootServices service.
Mario Six8fac2912018-07-10 08:40:17 +02001823 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001824 * See the Unified Extensible Firmware Interface (UEFI) specification
1825 * for details.
1826 *
Mario Six8fac2912018-07-10 08:40:17 +02001827 * All timer events are disabled. For exit boot services events the
1828 * notification function is called. The boot services are disabled in the
1829 * system table.
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001830 *
Mario Six8fac2912018-07-10 08:40:17 +02001831 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001832 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001833static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001834 unsigned long map_key)
1835{
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001836 struct efi_event *evt;
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001837
Alexander Grafc15d9212016-03-04 01:09:59 +01001838 EFI_ENTRY("%p, %ld", image_handle, map_key);
1839
Heinrich Schuchardt0f233c42018-07-02 12:53:55 +02001840 /* Check that the caller has read the current memory map */
1841 if (map_key != efi_memory_map_key)
1842 return EFI_INVALID_PARAMETER;
1843
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001844 /* Make sure that notification functions are not called anymore */
1845 efi_tpl = TPL_HIGH_LEVEL;
1846
1847 /* Check if ExitBootServices has already been called */
1848 if (!systab.boottime)
1849 return EFI_EXIT(EFI_SUCCESS);
1850
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001851 /* Add related events to the event group */
1852 list_for_each_entry(evt, &efi_events, link) {
1853 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1854 evt->group = &efi_guid_event_group_exit_boot_services;
1855 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001856 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt370c7a82018-02-18 15:17:50 +01001857 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtbf7f1692018-02-18 15:17:52 +01001858 if (evt->group &&
1859 !guidcmp(evt->group,
1860 &efi_guid_event_group_exit_boot_services)) {
1861 efi_signal_event(evt, false);
1862 break;
1863 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001864 }
Heinrich Schuchardt2d4c7382017-09-15 10:06:18 +02001865
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001866 /* TODO: Should persist EFI variables here */
Rob Clark15f3d742017-09-13 18:05:37 -04001867
Alexander Graf2ebeb442016-11-17 01:02:57 +01001868 board_quiesce_devices();
1869
Alexander Grafc15d9212016-03-04 01:09:59 +01001870 /* This stops all lingering devices */
1871 bootm_disable_interrupts();
1872
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02001873 /* Disable boot time services */
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001874 systab.con_in_handle = NULL;
1875 systab.con_in = NULL;
1876 systab.con_out_handle = NULL;
1877 systab.con_out = NULL;
1878 systab.stderr_handle = NULL;
1879 systab.std_err = NULL;
1880 systab.boottime = NULL;
1881
1882 /* Recalculate CRC32 */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02001883 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtb1fbb212018-01-19 20:24:52 +01001884
Alexander Grafc15d9212016-03-04 01:09:59 +01001885 /* Give the payload some time to boot */
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001886 efi_set_watchdog(0);
Alexander Grafc15d9212016-03-04 01:09:59 +01001887 WATCHDOG_RESET();
1888
1889 return EFI_EXIT(EFI_SUCCESS);
1890}
1891
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001892/**
Mario Six8fac2912018-07-10 08:40:17 +02001893 * efi_get_next_monotonic_count() - get next value of the counter
1894 * @count: returned value of the counter
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001895 *
1896 * This function implements the NextMonotonicCount service.
Mario Six8fac2912018-07-10 08:40:17 +02001897 *
1898 * See the Unified Extensible Firmware Interface (UEFI) specification for
1899 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001900 *
Mario Six8fac2912018-07-10 08:40:17 +02001901 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001902 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001903static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1904{
Heinrich Schuchardt91064592018-02-18 15:17:49 +01001905 static uint64_t mono;
1906
Alexander Grafc15d9212016-03-04 01:09:59 +01001907 EFI_ENTRY("%p", count);
1908 *count = mono++;
1909 return EFI_EXIT(EFI_SUCCESS);
1910}
1911
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001912/**
Mario Six8fac2912018-07-10 08:40:17 +02001913 * efi_stall() - sleep
1914 * @microseconds: period to sleep in microseconds
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001915 *
Mario Six8fac2912018-07-10 08:40:17 +02001916 * This function implements the Stall service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001917 *
Mario Six8fac2912018-07-10 08:40:17 +02001918 * See the Unified Extensible Firmware Interface (UEFI) specification for
1919 * details.
1920 *
1921 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001922 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001923static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1924{
1925 EFI_ENTRY("%ld", microseconds);
1926 udelay(microseconds);
1927 return EFI_EXIT(EFI_SUCCESS);
1928}
1929
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001930/**
Mario Six8fac2912018-07-10 08:40:17 +02001931 * efi_set_watchdog_timer() - reset the watchdog timer
1932 * @timeout: seconds before reset by watchdog
1933 * @watchdog_code: code to be logged when resetting
1934 * @data_size: size of buffer in bytes
1935 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001936 *
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001937 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001938 *
Mario Six8fac2912018-07-10 08:40:17 +02001939 * See the Unified Extensible Firmware Interface (UEFI) specification for
1940 * details.
1941 *
1942 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001943 */
Alexander Grafc15d9212016-03-04 01:09:59 +01001944static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1945 uint64_t watchdog_code,
1946 unsigned long data_size,
1947 uint16_t *watchdog_data)
1948{
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001949 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafc15d9212016-03-04 01:09:59 +01001950 data_size, watchdog_data);
Heinrich Schuchardt18081d42017-10-18 18:13:04 +02001951 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafc15d9212016-03-04 01:09:59 +01001952}
1953
Heinrich Schuchardt59999172018-05-11 18:15:41 +02001954/**
Mario Six8fac2912018-07-10 08:40:17 +02001955 * efi_close_protocol() - close a protocol
1956 * @handle: handle on which the protocol shall be closed
1957 * @protocol: GUID of the protocol to close
1958 * @agent_handle: handle of the driver
1959 * @controller_handle: handle of the controller
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001960 *
1961 * This function implements the CloseProtocol service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001962 *
Mario Six8fac2912018-07-10 08:40:17 +02001963 * See the Unified Extensible Firmware Interface (UEFI) specification for
1964 * details.
1965 *
1966 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02001967 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001968static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02001969 const efi_guid_t *protocol,
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01001970 efi_handle_t agent_handle,
1971 efi_handle_t controller_handle)
Alexander Grafc15d9212016-03-04 01:09:59 +01001972{
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001973 struct efi_handler *handler;
1974 struct efi_open_protocol_info_item *item;
1975 struct efi_open_protocol_info_item *pos;
1976 efi_status_t r;
1977
Rob Clark238f88c2017-09-13 18:05:41 -04001978 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafc15d9212016-03-04 01:09:59 +01001979 controller_handle);
Heinrich Schuchardt4fd1ee22018-01-11 08:15:59 +01001980
1981 if (!agent_handle) {
1982 r = EFI_INVALID_PARAMETER;
1983 goto out;
1984 }
1985 r = efi_search_protocol(handle, protocol, &handler);
1986 if (r != EFI_SUCCESS)
1987 goto out;
1988
1989 r = EFI_NOT_FOUND;
1990 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1991 if (item->info.agent_handle == agent_handle &&
1992 item->info.controller_handle == controller_handle) {
1993 efi_delete_open_info(item);
1994 r = EFI_SUCCESS;
1995 break;
1996 }
1997 }
1998out:
1999 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002000}
2001
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002002/**
Mario Six8fac2912018-07-10 08:40:17 +02002003 * efi_open_protocol_information() - provide information about then open status
2004 * of a protocol on a handle
2005 * @handle: handle for which the information shall be retrieved
2006 * @protocol: GUID of the protocol
2007 * @entry_buffer: buffer to receive the open protocol information
2008 * @entry_count: number of entries available in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002009 *
2010 * This function implements the OpenProtocolInformation service.
Mario Six8fac2912018-07-10 08:40:17 +02002011 *
2012 * See the Unified Extensible Firmware Interface (UEFI) specification for
2013 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002014 *
Mario Six8fac2912018-07-10 08:40:17 +02002015 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002016 */
Heinrich Schuchardt91064592018-02-18 15:17:49 +01002017static efi_status_t EFIAPI efi_open_protocol_information(
2018 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002019 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002020 efi_uintn_t *entry_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002021{
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002022 unsigned long buffer_size;
2023 unsigned long count;
2024 struct efi_handler *handler;
2025 struct efi_open_protocol_info_item *item;
2026 efi_status_t r;
2027
Rob Clark238f88c2017-09-13 18:05:41 -04002028 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafc15d9212016-03-04 01:09:59 +01002029 entry_count);
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002030
2031 /* Check parameters */
2032 if (!entry_buffer) {
2033 r = EFI_INVALID_PARAMETER;
2034 goto out;
2035 }
2036 r = efi_search_protocol(handle, protocol, &handler);
2037 if (r != EFI_SUCCESS)
2038 goto out;
2039
2040 /* Count entries */
2041 count = 0;
2042 list_for_each_entry(item, &handler->open_infos, link) {
2043 if (item->info.open_count)
2044 ++count;
2045 }
2046 *entry_count = count;
2047 *entry_buffer = NULL;
2048 if (!count) {
2049 r = EFI_SUCCESS;
2050 goto out;
2051 }
2052
2053 /* Copy entries */
2054 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002055 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardtf2ef22e2018-01-11 08:16:00 +01002056 (void **)entry_buffer);
2057 if (r != EFI_SUCCESS)
2058 goto out;
2059 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2060 if (item->info.open_count)
2061 (*entry_buffer)[--count] = item->info;
2062 }
2063out:
2064 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002065}
2066
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002067/**
Mario Six8fac2912018-07-10 08:40:17 +02002068 * efi_protocols_per_handle() - get protocols installed on a handle
2069 * @handle: handle for which the information is retrieved
2070 * @protocol_buffer: buffer with protocol GUIDs
2071 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002072 *
2073 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002074 *
Mario Six8fac2912018-07-10 08:40:17 +02002075 * See the Unified Extensible Firmware Interface (UEFI) specification for
2076 * details.
2077 *
2078 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002079 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002080static efi_status_t EFIAPI efi_protocols_per_handle(
2081 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002082 efi_uintn_t *protocol_buffer_count)
Alexander Grafc15d9212016-03-04 01:09:59 +01002083{
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002084 unsigned long buffer_size;
2085 struct efi_object *efiobj;
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002086 struct list_head *protocol_handle;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002087 efi_status_t r;
2088
Alexander Grafc15d9212016-03-04 01:09:59 +01002089 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2090 protocol_buffer_count);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002091
2092 if (!handle || !protocol_buffer || !protocol_buffer_count)
2093 return EFI_EXIT(EFI_INVALID_PARAMETER);
2094
2095 *protocol_buffer = NULL;
Rob Clarkd51b8ca2017-07-20 07:59:39 -04002096 *protocol_buffer_count = 0;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002097
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002098 efiobj = efi_search_obj(handle);
2099 if (!efiobj)
2100 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002101
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002102 /* Count protocols */
2103 list_for_each(protocol_handle, &efiobj->protocols) {
2104 ++*protocol_buffer_count;
2105 }
2106
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002107 /* Copy GUIDs */
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002108 if (*protocol_buffer_count) {
2109 size_t j = 0;
2110
2111 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002112 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
Heinrich Schuchardt99ce2062017-11-26 14:05:17 +01002113 (void **)protocol_buffer);
2114 if (r != EFI_SUCCESS)
2115 return EFI_EXIT(r);
2116 list_for_each(protocol_handle, &efiobj->protocols) {
2117 struct efi_handler *protocol;
2118
2119 protocol = list_entry(protocol_handle,
2120 struct efi_handler, link);
2121 (*protocol_buffer)[j] = (void *)protocol->guid;
2122 ++j;
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002123 }
xypron.glpk@gmx.de8960c972017-07-13 23:24:32 +02002124 }
2125
2126 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002127}
2128
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002129/**
Mario Six8fac2912018-07-10 08:40:17 +02002130 * efi_locate_handle_buffer() - locate handles implementing a protocol
2131 * @search_type: selection criterion
2132 * @protocol: GUID of the protocol
2133 * @search_key: registration key
2134 * @no_handles: number of returned handles
2135 * @buffer: buffer with the returned handles
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002136 *
2137 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002138 *
Mario Six8fac2912018-07-10 08:40:17 +02002139 * See the Unified Extensible Firmware Interface (UEFI) specification for
2140 * details.
2141 *
2142 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002143 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002144static efi_status_t EFIAPI efi_locate_handle_buffer(
2145 enum efi_locate_search_type search_type,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002146 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002147 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafc15d9212016-03-04 01:09:59 +01002148{
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002149 efi_status_t r;
Heinrich Schuchardt798a4412017-11-06 21:17:48 +01002150 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002151
Rob Clark238f88c2017-09-13 18:05:41 -04002152 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafc15d9212016-03-04 01:09:59 +01002153 no_handles, buffer);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002154
2155 if (!no_handles || !buffer) {
2156 r = EFI_INVALID_PARAMETER;
2157 goto out;
2158 }
2159 *no_handles = 0;
2160 *buffer = NULL;
2161 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2162 *buffer);
2163 if (r != EFI_BUFFER_TOO_SMALL)
2164 goto out;
Heinrich Schuchardtabb43872018-10-03 20:02:29 +02002165 r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size,
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002166 (void **)buffer);
2167 if (r != EFI_SUCCESS)
2168 goto out;
2169 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2170 *buffer);
2171 if (r == EFI_SUCCESS)
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002172 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.de550a68a2017-07-11 22:06:22 +02002173out:
2174 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002175}
2176
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002177/**
Mario Six8fac2912018-07-10 08:40:17 +02002178 * efi_locate_protocol() - find an interface implementing a protocol
2179 * @protocol: GUID of the protocol
2180 * @registration: registration key passed to the notification function
2181 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002182 *
2183 * This function implements the LocateProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002184 *
2185 * See the Unified Extensible Firmware Interface (UEFI) specification for
2186 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002187 *
Mario Six8fac2912018-07-10 08:40:17 +02002188 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002189 */
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002190static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002191 void *registration,
2192 void **protocol_interface)
2193{
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002194 struct list_head *lhandle;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002195 efi_status_t ret;
Alexander Grafc15d9212016-03-04 01:09:59 +01002196
Rob Clark238f88c2017-09-13 18:05:41 -04002197 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002198
2199 if (!protocol || !protocol_interface)
2200 return EFI_EXIT(EFI_INVALID_PARAMETER);
2201
2202 list_for_each(lhandle, &efi_obj_list) {
2203 struct efi_object *efiobj;
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002204 struct efi_handler *handler;
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002205
2206 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002207
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +02002208 ret = efi_search_protocol(efiobj, protocol, &handler);
Heinrich Schuchardt57505e92017-10-26 19:25:57 +02002209 if (ret == EFI_SUCCESS) {
2210 *protocol_interface = handler->protocol_interface;
2211 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002212 }
2213 }
xypron.glpk@gmx.de4534ad62017-07-11 22:06:24 +02002214 *protocol_interface = NULL;
Alexander Grafc15d9212016-03-04 01:09:59 +01002215
2216 return EFI_EXIT(EFI_NOT_FOUND);
2217}
2218
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002219/**
Mario Six8fac2912018-07-10 08:40:17 +02002220 * efi_locate_device_path() - Get the device path and handle of an device
2221 * implementing a protocol
2222 * @protocol: GUID of the protocol
2223 * @device_path: device path
2224 * @device: handle of the device
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002225 *
2226 * This function implements the LocateDevicePath service.
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002227 *
Mario Six8fac2912018-07-10 08:40:17 +02002228 * See the Unified Extensible Firmware Interface (UEFI) specification for
2229 * details.
2230 *
2231 * Return: status code
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002232 */
2233static efi_status_t EFIAPI efi_locate_device_path(
2234 const efi_guid_t *protocol,
2235 struct efi_device_path **device_path,
2236 efi_handle_t *device)
2237{
2238 struct efi_device_path *dp;
2239 size_t i;
2240 struct efi_handler *handler;
2241 efi_handle_t *handles;
2242 size_t len, len_dp;
2243 size_t len_best = 0;
2244 efi_uintn_t no_handles;
2245 u8 *remainder;
2246 efi_status_t ret;
2247
2248 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2249
2250 if (!protocol || !device_path || !*device_path || !device) {
2251 ret = EFI_INVALID_PARAMETER;
2252 goto out;
2253 }
2254
2255 /* Find end of device path */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002256 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002257
2258 /* Get all handles implementing the protocol */
2259 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2260 &no_handles, &handles));
2261 if (ret != EFI_SUCCESS)
2262 goto out;
2263
2264 for (i = 0; i < no_handles; ++i) {
2265 /* Find the device path protocol */
2266 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2267 &handler);
2268 if (ret != EFI_SUCCESS)
2269 continue;
2270 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +02002271 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardt06ec6892017-11-26 14:05:10 +01002272 /*
2273 * This handle can only be a better fit
2274 * if its device path length is longer than the best fit and
2275 * if its device path length is shorter of equal the searched
2276 * device path.
2277 */
2278 if (len_dp <= len_best || len_dp > len)
2279 continue;
2280 /* Check if dp is a subpath of device_path */
2281 if (memcmp(*device_path, dp, len_dp))
2282 continue;
2283 *device = handles[i];
2284 len_best = len_dp;
2285 }
2286 if (len_best) {
2287 remainder = (u8 *)*device_path + len_best;
2288 *device_path = (struct efi_device_path *)remainder;
2289 ret = EFI_SUCCESS;
2290 } else {
2291 ret = EFI_NOT_FOUND;
2292 }
2293out:
2294 return EFI_EXIT(ret);
2295}
2296
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002297/**
Mario Six8fac2912018-07-10 08:40:17 +02002298 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2299 * interfaces
2300 * @handle: handle on which the protocol interfaces shall be installed
2301 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2302 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002303 *
2304 * This function implements the MultipleProtocolInterfaces service.
Mario Six8fac2912018-07-10 08:40:17 +02002305 *
2306 * See the Unified Extensible Firmware Interface (UEFI) specification for
2307 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002308 *
Mario Six8fac2912018-07-10 08:40:17 +02002309 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002310 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002311static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces
2312 (efi_handle_t *handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002313{
2314 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002315
Alexander Graf404a7c82018-06-18 17:23:05 +02002316 efi_va_list argptr;
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002317 const efi_guid_t *protocol;
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002318 void *protocol_interface;
2319 efi_status_t r = EFI_SUCCESS;
2320 int i = 0;
2321
2322 if (!handle)
2323 return EFI_EXIT(EFI_INVALID_PARAMETER);
2324
Alexander Graf404a7c82018-06-18 17:23:05 +02002325 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002326 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002327 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002328 if (!protocol)
2329 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002330 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01002331 r = EFI_CALL(efi_install_protocol_interface(
2332 handle, protocol,
2333 EFI_NATIVE_INTERFACE,
2334 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002335 if (r != EFI_SUCCESS)
2336 break;
2337 i++;
2338 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002339 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002340 if (r == EFI_SUCCESS)
2341 return EFI_EXIT(r);
2342
Heinrich Schuchardtec47f3e2017-10-26 19:25:42 +02002343 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002344 efi_va_start(argptr, handle);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002345 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002346 protocol = efi_va_arg(argptr, efi_guid_t*);
2347 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002348 EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01002349 protocol_interface));
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002350 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002351 efi_va_end(argptr);
xypron.glpk@gmx.de83eebc72017-07-11 22:06:20 +02002352
2353 return EFI_EXIT(r);
Alexander Grafc15d9212016-03-04 01:09:59 +01002354}
2355
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002356/**
Mario Six8fac2912018-07-10 08:40:17 +02002357 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2358 * interfaces
2359 * @handle: handle from which the protocol interfaces shall be removed
2360 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2361 * interfaces
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002362 *
2363 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002364 *
Mario Six8fac2912018-07-10 08:40:17 +02002365 * See the Unified Extensible Firmware Interface (UEFI) specification for
2366 * details.
2367 *
2368 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002369 */
Alexander Grafc15d9212016-03-04 01:09:59 +01002370static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002371 efi_handle_t handle, ...)
Alexander Grafc15d9212016-03-04 01:09:59 +01002372{
2373 EFI_ENTRY("%p", handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002374
Alexander Graf404a7c82018-06-18 17:23:05 +02002375 efi_va_list argptr;
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002376 const efi_guid_t *protocol;
2377 void *protocol_interface;
2378 efi_status_t r = EFI_SUCCESS;
2379 size_t i = 0;
2380
2381 if (!handle)
2382 return EFI_EXIT(EFI_INVALID_PARAMETER);
2383
Alexander Graf404a7c82018-06-18 17:23:05 +02002384 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002385 for (;;) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002386 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002387 if (!protocol)
2388 break;
Alexander Graf404a7c82018-06-18 17:23:05 +02002389 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002390 r = efi_uninstall_protocol(handle, protocol,
2391 protocol_interface);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002392 if (r != EFI_SUCCESS)
2393 break;
2394 i++;
2395 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002396 efi_va_end(argptr);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002397 if (r == EFI_SUCCESS) {
2398 /* If the last protocol has been removed, delete the handle. */
2399 if (list_empty(&handle->protocols)) {
2400 list_del(&handle->link);
2401 free(handle);
2402 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002403 return EFI_EXIT(r);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002404 }
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002405
2406 /* If an error occurred undo all changes. */
Alexander Graf404a7c82018-06-18 17:23:05 +02002407 efi_va_start(argptr, handle);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002408 for (; i; --i) {
Alexander Graf404a7c82018-06-18 17:23:05 +02002409 protocol = efi_va_arg(argptr, efi_guid_t*);
2410 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002411 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2412 EFI_NATIVE_INTERFACE,
2413 protocol_interface));
2414 }
Alexander Graf404a7c82018-06-18 17:23:05 +02002415 efi_va_end(argptr);
Heinrich Schuchardta616dcf2017-10-26 19:25:44 +02002416
Heinrich Schuchardt1c76eda2018-09-24 19:57:27 +02002417 /* In case of an error always return EFI_INVALID_PARAMETER */
2418 return EFI_EXIT(EFI_INVALID_PARAMETER);
Alexander Grafc15d9212016-03-04 01:09:59 +01002419}
2420
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002421/**
Mario Six8fac2912018-07-10 08:40:17 +02002422 * efi_calculate_crc32() - calculate cyclic redundancy code
2423 * @data: buffer with data
2424 * @data_size: size of buffer in bytes
2425 * @crc32_p: cyclic redundancy code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002426 *
2427 * This function implements the CalculateCrc32 service.
Mario Six8fac2912018-07-10 08:40:17 +02002428 *
2429 * See the Unified Extensible Firmware Interface (UEFI) specification for
2430 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002431 *
Mario Six8fac2912018-07-10 08:40:17 +02002432 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002433 */
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002434static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2435 efi_uintn_t data_size,
2436 u32 *crc32_p)
Alexander Grafc15d9212016-03-04 01:09:59 +01002437{
Heinrich Schuchardtf2725582018-07-07 15:36:04 +02002438 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafc15d9212016-03-04 01:09:59 +01002439 *crc32_p = crc32(0, data, data_size);
2440 return EFI_EXIT(EFI_SUCCESS);
2441}
2442
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002443/**
Mario Six8fac2912018-07-10 08:40:17 +02002444 * efi_copy_mem() - copy memory
2445 * @destination: destination of the copy operation
2446 * @source: source of the copy operation
2447 * @length: number of bytes to copy
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002448 *
2449 * This function implements the CopyMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002450 *
Mario Six8fac2912018-07-10 08:40:17 +02002451 * See the Unified Extensible Firmware Interface (UEFI) specification for
2452 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002453 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002454static void EFIAPI efi_copy_mem(void *destination, const void *source,
2455 size_t length)
Alexander Grafc15d9212016-03-04 01:09:59 +01002456{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002457 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafc15d9212016-03-04 01:09:59 +01002458 memcpy(destination, source, length);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002459 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002460}
2461
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002462/**
Mario Six8fac2912018-07-10 08:40:17 +02002463 * efi_set_mem() - Fill memory with a byte value.
2464 * @buffer: buffer to fill
2465 * @size: size of buffer in bytes
2466 * @value: byte to copy to the buffer
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002467 *
2468 * This function implements the SetMem service.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002469 *
Mario Six8fac2912018-07-10 08:40:17 +02002470 * See the Unified Extensible Firmware Interface (UEFI) specification for
2471 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002472 */
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002473static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafc15d9212016-03-04 01:09:59 +01002474{
Heinrich Schuchardtd0a349e2017-10-05 16:35:52 +02002475 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafc15d9212016-03-04 01:09:59 +01002476 memset(buffer, value, size);
Heinrich Schuchardta5270e02017-10-05 16:35:51 +02002477 EFI_EXIT(EFI_SUCCESS);
Alexander Grafc15d9212016-03-04 01:09:59 +01002478}
2479
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002480/**
Mario Six8fac2912018-07-10 08:40:17 +02002481 * efi_protocol_open() - open protocol interface on a handle
2482 * @handler: handler of a protocol
2483 * @protocol_interface: interface implementing the protocol
2484 * @agent_handle: handle of the driver
2485 * @controller_handle: handle of the controller
2486 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002487 *
Mario Six8fac2912018-07-10 08:40:17 +02002488 * Return: status code
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002489 */
2490static efi_status_t efi_protocol_open(
2491 struct efi_handler *handler,
2492 void **protocol_interface, void *agent_handle,
2493 void *controller_handle, uint32_t attributes)
2494{
2495 struct efi_open_protocol_info_item *item;
2496 struct efi_open_protocol_info_entry *match = NULL;
2497 bool opened_by_driver = false;
2498 bool opened_exclusive = false;
2499
2500 /* If there is no agent, only return the interface */
2501 if (!agent_handle)
2502 goto out;
2503
2504 /* For TEST_PROTOCOL ignore interface attribute */
2505 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2506 *protocol_interface = NULL;
2507
2508 /*
2509 * Check if the protocol is already opened by a driver with the same
2510 * attributes or opened exclusively
2511 */
2512 list_for_each_entry(item, &handler->open_infos, link) {
2513 if (item->info.agent_handle == agent_handle) {
2514 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2515 (item->info.attributes == attributes))
2516 return EFI_ALREADY_STARTED;
2517 }
2518 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2519 opened_exclusive = true;
2520 }
2521
2522 /* Only one controller can open the protocol exclusively */
2523 if (opened_exclusive && attributes &
2524 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2525 return EFI_ACCESS_DENIED;
2526
2527 /* Prepare exclusive opening */
2528 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2529 /* Try to disconnect controllers */
2530 list_for_each_entry(item, &handler->open_infos, link) {
2531 if (item->info.attributes ==
2532 EFI_OPEN_PROTOCOL_BY_DRIVER)
2533 EFI_CALL(efi_disconnect_controller(
2534 item->info.controller_handle,
2535 item->info.agent_handle,
2536 NULL));
2537 }
2538 opened_by_driver = false;
2539 /* Check if all controllers are disconnected */
2540 list_for_each_entry(item, &handler->open_infos, link) {
2541 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2542 opened_by_driver = true;
2543 }
Heinrich Schuchardt8d0b45d2018-09-30 13:40:43 +02002544 /* Only one controller can be connected */
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002545 if (opened_by_driver)
2546 return EFI_ACCESS_DENIED;
2547 }
2548
2549 /* Find existing entry */
2550 list_for_each_entry(item, &handler->open_infos, link) {
2551 if (item->info.agent_handle == agent_handle &&
2552 item->info.controller_handle == controller_handle)
2553 match = &item->info;
2554 }
2555 /* None found, create one */
2556 if (!match) {
2557 match = efi_create_open_info(handler);
2558 if (!match)
2559 return EFI_OUT_OF_RESOURCES;
2560 }
2561
2562 match->agent_handle = agent_handle;
2563 match->controller_handle = controller_handle;
2564 match->attributes = attributes;
2565 match->open_count++;
2566
2567out:
2568 /* For TEST_PROTOCOL ignore interface attribute. */
2569 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2570 *protocol_interface = handler->protocol_interface;
2571
2572 return EFI_SUCCESS;
2573}
2574
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002575/**
Mario Six8fac2912018-07-10 08:40:17 +02002576 * efi_open_protocol() - open protocol interface on a handle
2577 * @handle: handle on which the protocol shall be opened
2578 * @protocol: GUID of the protocol
2579 * @protocol_interface: interface implementing the protocol
2580 * @agent_handle: handle of the driver
2581 * @controller_handle: handle of the controller
2582 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002583 *
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002584 * This function implements the OpenProtocol interface.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002585 *
Mario Six8fac2912018-07-10 08:40:17 +02002586 * See the Unified Extensible Firmware Interface (UEFI) specification for
2587 * details.
2588 *
2589 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002590 */
Heinrich Schuchardt4f94ec52018-09-26 05:27:54 +02002591static efi_status_t EFIAPI efi_open_protocol
2592 (efi_handle_t handle, const efi_guid_t *protocol,
2593 void **protocol_interface, efi_handle_t agent_handle,
2594 efi_handle_t controller_handle, uint32_t attributes)
Alexander Grafc15d9212016-03-04 01:09:59 +01002595{
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002596 struct efi_handler *handler;
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002597 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafc15d9212016-03-04 01:09:59 +01002598
Rob Clark238f88c2017-09-13 18:05:41 -04002599 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002600 protocol_interface, agent_handle, controller_handle,
2601 attributes);
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002602
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002603 if (!handle || !protocol ||
2604 (!protocol_interface && attributes !=
2605 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
xypron.glpk@gmx.dec35c9242017-07-11 22:06:14 +02002606 goto out;
2607 }
2608
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002609 switch (attributes) {
2610 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2611 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2612 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2613 break;
2614 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2615 if (controller_handle == handle)
2616 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002617 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002618 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2619 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002620 /* Check that the controller handle is valid */
2621 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002622 goto out;
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002623 /* fall-through */
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002624 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002625 /* Check that the agent handle is valid */
2626 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.def097c842017-07-11 22:06:15 +02002627 goto out;
2628 break;
2629 default:
2630 goto out;
2631 }
2632
Heinrich Schuchardte5e78a32017-11-26 14:05:15 +01002633 r = efi_search_protocol(handle, protocol, &handler);
2634 if (r != EFI_SUCCESS)
2635 goto out;
Alexander Grafc15d9212016-03-04 01:09:59 +01002636
Heinrich Schuchardt8cb38c02018-01-11 08:15:58 +01002637 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2638 controller_handle, attributes);
Alexander Grafc15d9212016-03-04 01:09:59 +01002639out:
2640 return EFI_EXIT(r);
2641}
2642
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002643/**
Mario Six8fac2912018-07-10 08:40:17 +02002644 * efi_handle_protocol() - get interface of a protocol on a handle
2645 * @handle: handle on which the protocol shall be opened
2646 * @protocol: GUID of the protocol
2647 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002648 *
2649 * This function implements the HandleProtocol service.
Mario Six8fac2912018-07-10 08:40:17 +02002650 *
2651 * See the Unified Extensible Firmware Interface (UEFI) specification for
2652 * details.
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002653 *
Mario Six8fac2912018-07-10 08:40:17 +02002654 * Return: status code
Heinrich Schuchardta40db6e2017-09-21 18:30:11 +02002655 */
Heinrich Schuchardtb7cb8b42018-01-11 08:16:09 +01002656static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardte547c662017-10-05 16:35:53 +02002657 const efi_guid_t *protocol,
Alexander Grafc15d9212016-03-04 01:09:59 +01002658 void **protocol_interface)
2659{
xypron.glpk@gmx.de1bf5d872017-06-29 21:16:19 +02002660 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2661 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafc15d9212016-03-04 01:09:59 +01002662}
2663
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002664/**
Mario Six8fac2912018-07-10 08:40:17 +02002665 * efi_bind_controller() - bind a single driver to a controller
2666 * @controller_handle: controller handle
2667 * @driver_image_handle: driver handle
2668 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002669 *
Mario Six8fac2912018-07-10 08:40:17 +02002670 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002671 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002672static efi_status_t efi_bind_controller(
2673 efi_handle_t controller_handle,
2674 efi_handle_t driver_image_handle,
2675 struct efi_device_path *remain_device_path)
2676{
2677 struct efi_driver_binding_protocol *binding_protocol;
2678 efi_status_t r;
2679
2680 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2681 &efi_guid_driver_binding_protocol,
2682 (void **)&binding_protocol,
2683 driver_image_handle, NULL,
2684 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2685 if (r != EFI_SUCCESS)
2686 return r;
2687 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2688 controller_handle,
2689 remain_device_path));
2690 if (r == EFI_SUCCESS)
2691 r = EFI_CALL(binding_protocol->start(binding_protocol,
2692 controller_handle,
2693 remain_device_path));
2694 EFI_CALL(efi_close_protocol(driver_image_handle,
2695 &efi_guid_driver_binding_protocol,
2696 driver_image_handle, NULL));
2697 return r;
2698}
2699
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002700/**
Mario Six8fac2912018-07-10 08:40:17 +02002701 * efi_connect_single_controller() - connect a single driver to a controller
2702 * @controller_handle: controller
2703 * @driver_image_handle: driver
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002704 * @remain_device_path: remaining path
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002705 *
Mario Six8fac2912018-07-10 08:40:17 +02002706 * Return: status code
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002707 */
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002708static efi_status_t efi_connect_single_controller(
2709 efi_handle_t controller_handle,
2710 efi_handle_t *driver_image_handle,
2711 struct efi_device_path *remain_device_path)
2712{
2713 efi_handle_t *buffer;
2714 size_t count;
2715 size_t i;
2716 efi_status_t r;
2717 size_t connected = 0;
2718
2719 /* Get buffer with all handles with driver binding protocol */
2720 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2721 &efi_guid_driver_binding_protocol,
2722 NULL, &count, &buffer));
2723 if (r != EFI_SUCCESS)
2724 return r;
2725
2726 /* Context Override */
2727 if (driver_image_handle) {
2728 for (; *driver_image_handle; ++driver_image_handle) {
2729 for (i = 0; i < count; ++i) {
2730 if (buffer[i] == *driver_image_handle) {
2731 buffer[i] = NULL;
2732 r = efi_bind_controller(
2733 controller_handle,
2734 *driver_image_handle,
2735 remain_device_path);
2736 /*
2737 * For drivers that do not support the
2738 * controller or are already connected
2739 * we receive an error code here.
2740 */
2741 if (r == EFI_SUCCESS)
2742 ++connected;
2743 }
2744 }
2745 }
2746 }
2747
2748 /*
2749 * TODO: Some overrides are not yet implemented:
2750 * - Platform Driver Override
2751 * - Driver Family Override Search
2752 * - Bus Specific Driver Override
2753 */
2754
2755 /* Driver Binding Search */
2756 for (i = 0; i < count; ++i) {
2757 if (buffer[i]) {
2758 r = efi_bind_controller(controller_handle,
2759 buffer[i],
2760 remain_device_path);
2761 if (r == EFI_SUCCESS)
2762 ++connected;
2763 }
2764 }
2765
2766 efi_free_pool(buffer);
2767 if (!connected)
2768 return EFI_NOT_FOUND;
2769 return EFI_SUCCESS;
2770}
2771
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002772/**
Mario Six8fac2912018-07-10 08:40:17 +02002773 * efi_connect_controller() - connect a controller to a driver
2774 * @controller_handle: handle of the controller
2775 * @driver_image_handle: handle of the driver
2776 * @remain_device_path: device path of a child controller
2777 * @recursive: true to connect all child controllers
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002778 *
2779 * This function implements the ConnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002780 *
2781 * See the Unified Extensible Firmware Interface (UEFI) specification for
2782 * details.
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002783 *
2784 * First all driver binding protocol handles are tried for binding drivers.
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02002785 * Afterwards all handles that have opened a protocol of the controller
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002786 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2787 *
Mario Six8fac2912018-07-10 08:40:17 +02002788 * Return: status code
Heinrich Schuchardt760255f2018-01-11 08:16:02 +01002789 */
2790static efi_status_t EFIAPI efi_connect_controller(
2791 efi_handle_t controller_handle,
2792 efi_handle_t *driver_image_handle,
2793 struct efi_device_path *remain_device_path,
2794 bool recursive)
2795{
2796 efi_status_t r;
2797 efi_status_t ret = EFI_NOT_FOUND;
2798 struct efi_object *efiobj;
2799
2800 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2801 remain_device_path, recursive);
2802
2803 efiobj = efi_search_obj(controller_handle);
2804 if (!efiobj) {
2805 ret = EFI_INVALID_PARAMETER;
2806 goto out;
2807 }
2808
2809 r = efi_connect_single_controller(controller_handle,
2810 driver_image_handle,
2811 remain_device_path);
2812 if (r == EFI_SUCCESS)
2813 ret = EFI_SUCCESS;
2814 if (recursive) {
2815 struct efi_handler *handler;
2816 struct efi_open_protocol_info_item *item;
2817
2818 list_for_each_entry(handler, &efiobj->protocols, link) {
2819 list_for_each_entry(item, &handler->open_infos, link) {
2820 if (item->info.attributes &
2821 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2822 r = EFI_CALL(efi_connect_controller(
2823 item->info.controller_handle,
2824 driver_image_handle,
2825 remain_device_path,
2826 recursive));
2827 if (r == EFI_SUCCESS)
2828 ret = EFI_SUCCESS;
2829 }
2830 }
2831 }
2832 }
2833 /* Check for child controller specified by end node */
2834 if (ret != EFI_SUCCESS && remain_device_path &&
2835 remain_device_path->type == DEVICE_PATH_TYPE_END)
2836 ret = EFI_SUCCESS;
2837out:
2838 return EFI_EXIT(ret);
2839}
2840
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002841/**
Mario Six8fac2912018-07-10 08:40:17 +02002842 * efi_reinstall_protocol_interface() - reinstall protocol interface
2843 * @handle: handle on which the protocol shall be reinstalled
2844 * @protocol: GUID of the protocol to be installed
2845 * @old_interface: interface to be removed
2846 * @new_interface: interface to be installed
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002847 *
2848 * This function implements the ReinstallProtocolInterface service.
Mario Six8fac2912018-07-10 08:40:17 +02002849 *
2850 * See the Unified Extensible Firmware Interface (UEFI) specification for
2851 * details.
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002852 *
2853 * The old interface is uninstalled. The new interface is installed.
2854 * Drivers are connected.
2855 *
Mario Six8fac2912018-07-10 08:40:17 +02002856 * Return: status code
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002857 */
2858static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2859 efi_handle_t handle, const efi_guid_t *protocol,
2860 void *old_interface, void *new_interface)
2861{
2862 efi_status_t ret;
2863
2864 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2865 new_interface);
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002866
2867 /* Uninstall protocol but do not delete handle */
2868 ret = efi_uninstall_protocol(handle, protocol, old_interface);
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002869 if (ret != EFI_SUCCESS)
2870 goto out;
Heinrich Schuchardt1b451342018-09-28 22:14:17 +02002871
2872 /* Install the new protocol */
2873 ret = efi_add_protocol(handle, protocol, new_interface);
2874 /*
2875 * The UEFI spec does not specify what should happen to the handle
2876 * if in case of an error no protocol interface remains on the handle.
2877 * So let's do nothing here.
2878 */
Heinrich Schuchardt90761b82018-05-11 12:09:22 +02002879 if (ret != EFI_SUCCESS)
2880 goto out;
2881 /*
2882 * The returned status code has to be ignored.
2883 * Do not create an error if no suitable driver for the handle exists.
2884 */
2885 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2886out:
2887 return EFI_EXIT(ret);
2888}
2889
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002890/**
Mario Six8fac2912018-07-10 08:40:17 +02002891 * efi_get_child_controllers() - get all child controllers associated to a driver
2892 * @efiobj: handle of the controller
2893 * @driver_handle: handle of the driver
2894 * @number_of_children: number of child controllers
2895 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002896 *
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002897 * The allocated buffer has to be freed with free().
2898 *
Mario Six8fac2912018-07-10 08:40:17 +02002899 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002900 */
2901static efi_status_t efi_get_child_controllers(
2902 struct efi_object *efiobj,
2903 efi_handle_t driver_handle,
2904 efi_uintn_t *number_of_children,
2905 efi_handle_t **child_handle_buffer)
2906{
2907 struct efi_handler *handler;
2908 struct efi_open_protocol_info_item *item;
2909 efi_uintn_t count = 0, i;
2910 bool duplicate;
2911
2912 /* Count all child controller associations */
2913 list_for_each_entry(handler, &efiobj->protocols, link) {
2914 list_for_each_entry(item, &handler->open_infos, link) {
2915 if (item->info.agent_handle == driver_handle &&
2916 item->info.attributes &
2917 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2918 ++count;
2919 }
2920 }
2921 /*
2922 * Create buffer. In case of duplicate child controller assignments
2923 * the buffer will be too large. But that does not harm.
2924 */
2925 *number_of_children = 0;
2926 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2927 if (!*child_handle_buffer)
2928 return EFI_OUT_OF_RESOURCES;
2929 /* Copy unique child handles */
2930 list_for_each_entry(handler, &efiobj->protocols, link) {
2931 list_for_each_entry(item, &handler->open_infos, link) {
2932 if (item->info.agent_handle == driver_handle &&
2933 item->info.attributes &
2934 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2935 /* Check this is a new child controller */
2936 duplicate = false;
2937 for (i = 0; i < *number_of_children; ++i) {
2938 if ((*child_handle_buffer)[i] ==
2939 item->info.controller_handle)
2940 duplicate = true;
2941 }
2942 /* Copy handle to buffer */
2943 if (!duplicate) {
2944 i = (*number_of_children)++;
2945 (*child_handle_buffer)[i] =
2946 item->info.controller_handle;
2947 }
2948 }
2949 }
2950 }
2951 return EFI_SUCCESS;
2952}
2953
Heinrich Schuchardt59999172018-05-11 18:15:41 +02002954/**
Mario Six8fac2912018-07-10 08:40:17 +02002955 * efi_disconnect_controller() - disconnect a controller from a driver
2956 * @controller_handle: handle of the controller
2957 * @driver_image_handle: handle of the driver
2958 * @child_handle: handle of the child to destroy
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002959 *
2960 * This function implements the DisconnectController service.
Mario Six8fac2912018-07-10 08:40:17 +02002961 *
2962 * See the Unified Extensible Firmware Interface (UEFI) specification for
2963 * details.
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002964 *
Mario Six8fac2912018-07-10 08:40:17 +02002965 * Return: status code
Heinrich Schuchardte9943282018-01-11 08:16:04 +01002966 */
2967static efi_status_t EFIAPI efi_disconnect_controller(
2968 efi_handle_t controller_handle,
2969 efi_handle_t driver_image_handle,
2970 efi_handle_t child_handle)
2971{
2972 struct efi_driver_binding_protocol *binding_protocol;
2973 efi_handle_t *child_handle_buffer = NULL;
2974 size_t number_of_children = 0;
2975 efi_status_t r;
2976 size_t stop_count = 0;
2977 struct efi_object *efiobj;
2978
2979 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2980 child_handle);
2981
2982 efiobj = efi_search_obj(controller_handle);
2983 if (!efiobj) {
2984 r = EFI_INVALID_PARAMETER;
2985 goto out;
2986 }
2987
2988 if (child_handle && !efi_search_obj(child_handle)) {
2989 r = EFI_INVALID_PARAMETER;
2990 goto out;
2991 }
2992
2993 /* If no driver handle is supplied, disconnect all drivers */
2994 if (!driver_image_handle) {
2995 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2996 goto out;
2997 }
2998
2999 /* Create list of child handles */
3000 if (child_handle) {
3001 number_of_children = 1;
3002 child_handle_buffer = &child_handle;
3003 } else {
3004 efi_get_child_controllers(efiobj,
3005 driver_image_handle,
3006 &number_of_children,
3007 &child_handle_buffer);
3008 }
3009
3010 /* Get the driver binding protocol */
3011 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3012 &efi_guid_driver_binding_protocol,
3013 (void **)&binding_protocol,
3014 driver_image_handle, NULL,
3015 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3016 if (r != EFI_SUCCESS)
3017 goto out;
3018 /* Remove the children */
3019 if (number_of_children) {
3020 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3021 controller_handle,
3022 number_of_children,
3023 child_handle_buffer));
3024 if (r == EFI_SUCCESS)
3025 ++stop_count;
3026 }
3027 /* Remove the driver */
3028 if (!child_handle)
3029 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3030 controller_handle,
3031 0, NULL));
3032 if (r == EFI_SUCCESS)
3033 ++stop_count;
3034 EFI_CALL(efi_close_protocol(driver_image_handle,
3035 &efi_guid_driver_binding_protocol,
3036 driver_image_handle, NULL));
3037
3038 if (stop_count)
3039 r = EFI_SUCCESS;
3040 else
3041 r = EFI_NOT_FOUND;
3042out:
3043 if (!child_handle)
3044 free(child_handle_buffer);
3045 return EFI_EXIT(r);
3046}
3047
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003048static struct efi_boot_services efi_boot_services = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003049 .hdr = {
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003050 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3051 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003052 .headersize = sizeof(struct efi_boot_services),
Alexander Grafc15d9212016-03-04 01:09:59 +01003053 },
3054 .raise_tpl = efi_raise_tpl,
3055 .restore_tpl = efi_restore_tpl,
3056 .allocate_pages = efi_allocate_pages_ext,
3057 .free_pages = efi_free_pages_ext,
3058 .get_memory_map = efi_get_memory_map_ext,
Stefan Brüns5a09aef2016-10-09 22:17:18 +02003059 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns67b67d92016-10-09 22:17:26 +02003060 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de852a0e1772017-07-18 20:17:20 +02003061 .create_event = efi_create_event_ext,
xypron.glpk@gmx.dea587fd12017-07-18 20:17:21 +02003062 .set_timer = efi_set_timer_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003063 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.de30708232017-07-18 20:17:18 +02003064 .signal_event = efi_signal_event_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003065 .close_event = efi_close_event,
3066 .check_event = efi_check_event,
Heinrich Schuchardt0a27ac82017-11-06 21:17:44 +01003067 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003068 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardt7cdc17f2017-11-06 21:17:45 +01003069 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafc15d9212016-03-04 01:09:59 +01003070 .handle_protocol = efi_handle_protocol,
3071 .reserved = NULL,
3072 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de69f94032017-07-11 22:06:21 +02003073 .locate_handle = efi_locate_handle_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003074 .locate_device_path = efi_locate_device_path,
Alexander Grafc5c11632016-08-19 01:23:24 +02003075 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafc15d9212016-03-04 01:09:59 +01003076 .load_image = efi_load_image,
3077 .start_image = efi_start_image,
Alexander Graf988c0662016-05-20 23:28:23 +02003078 .exit = efi_exit,
Alexander Grafc15d9212016-03-04 01:09:59 +01003079 .unload_image = efi_unload_image,
3080 .exit_boot_services = efi_exit_boot_services,
3081 .get_next_monotonic_count = efi_get_next_monotonic_count,
3082 .stall = efi_stall,
3083 .set_watchdog_timer = efi_set_watchdog_timer,
3084 .connect_controller = efi_connect_controller,
3085 .disconnect_controller = efi_disconnect_controller,
3086 .open_protocol = efi_open_protocol,
3087 .close_protocol = efi_close_protocol,
3088 .open_protocol_information = efi_open_protocol_information,
3089 .protocols_per_handle = efi_protocols_per_handle,
3090 .locate_handle_buffer = efi_locate_handle_buffer,
3091 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003092 .install_multiple_protocol_interfaces =
3093 efi_install_multiple_protocol_interfaces,
3094 .uninstall_multiple_protocol_interfaces =
3095 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafc15d9212016-03-04 01:09:59 +01003096 .calculate_crc32 = efi_calculate_crc32,
3097 .copy_mem = efi_copy_mem,
3098 .set_mem = efi_set_mem,
Heinrich Schuchardt717c4582018-02-04 23:05:13 +01003099 .create_event_ex = efi_create_event_ex,
Alexander Grafc15d9212016-03-04 01:09:59 +01003100};
3101
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003102static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafc15d9212016-03-04 01:09:59 +01003103
Alexander Graf393dd912016-10-14 13:45:30 +02003104struct efi_system_table __efi_runtime_data systab = {
Alexander Grafc15d9212016-03-04 01:09:59 +01003105 .hdr = {
3106 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardte75b3cb2018-06-28 12:45:27 +02003107 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt10204252018-06-28 12:45:29 +02003108 .headersize = sizeof(struct efi_system_table),
Alexander Grafc15d9212016-03-04 01:09:59 +01003109 },
Heinrich Schuchardt27685f72018-06-28 12:45:30 +02003110 .fw_vendor = firmware_vendor,
3111 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardt91064592018-02-18 15:17:49 +01003112 .con_in = (void *)&efi_con_in,
3113 .con_out = (void *)&efi_con_out,
3114 .std_err = (void *)&efi_con_out,
3115 .runtime = (void *)&efi_runtime_services,
3116 .boottime = (void *)&efi_boot_services,
Alexander Grafc15d9212016-03-04 01:09:59 +01003117 .nr_tables = 0,
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003118 .tables = NULL,
Alexander Grafc15d9212016-03-04 01:09:59 +01003119};
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003120
3121/**
3122 * efi_initialize_system_table() - Initialize system table
3123 *
Heinrich Schuchardt7b4b2a22018-09-03 05:00:43 +02003124 * Return: status code
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003125 */
3126efi_status_t efi_initialize_system_table(void)
3127{
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003128 efi_status_t ret;
3129
3130 /* Allocate configuration table array */
3131 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3132 EFI_MAX_CONFIGURATION_TABLES *
3133 sizeof(struct efi_configuration_table),
3134 (void **)&systab.tables);
3135
Heinrich Schuchardtcf70a732018-09-03 19:12:24 +02003136 /* Set CRC32 field in table headers */
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003137 efi_update_table_header_crc32(&systab.hdr);
3138 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3139 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt2f528c22018-06-28 12:45:32 +02003140
3141 return ret;
Heinrich Schuchardt15070db2018-06-28 12:45:31 +02003142}