blob: ca61e1ad411992520bc414beb380dcf1801980af [file] [log] [blame]
Tom Rinif739fcd2018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafbee91162016-03-04 01:09:59 +01002/*
3 * EFI application boot time services
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafbee91162016-03-04 01:09:59 +01006 */
7
Alexander Grafbee91162016-03-04 01:09:59 +01008#include <common.h>
Heinrich Schuchardt7d963322017-10-05 16:14:14 +02009#include <div64.h>
Alexander Grafbee91162016-03-04 01:09:59 +010010#include <efi_loader.h>
Rob Clarkad644e72017-09-13 18:05:37 -040011#include <environment.h>
Alexander Grafbee91162016-03-04 01:09:59 +010012#include <malloc.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090013#include <linux/libfdt_env.h>
Alexander Grafbee91162016-03-04 01:09:59 +010014#include <u-boot/crc.h>
15#include <bootm.h>
Alexander Grafbee91162016-03-04 01:09:59 +010016#include <watchdog.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020020/* Task priority level */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +010021static efi_uintn_t efi_tpl = TPL_APPLICATION;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +020022
Alexander Grafbee91162016-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 Schuchardt43bce442018-02-18 15:17:50 +010026/* List of all events */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +010027LIST_HEAD(efi_events);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +010028
Alexander Grafbee91162016-03-04 01:09:59 +010029/*
30 * If we're running on nasty systems (32bit ARM booting into non-EFI Linux)
31 * we need to do trickery with caches. Since we don't want to break the EFI
32 * aware boot path, only apply hacks when loading exiting directly (breaking
33 * direct Linux EFI booting along the way - oh well).
34 */
35static bool efi_is_direct_boot = true;
36
Simon Glass65e4c0b2016-09-25 15:27:35 -060037#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +010038/*
39 * The "gd" pointer lives in a register on ARM and AArch64 that we declare
40 * fixed when compiling U-Boot. However, the payload does not know about that
41 * restriction so we need to manually swap its and our view of that register on
42 * EFI callback entry/exit.
43 */
44static volatile void *efi_gd, *app_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -060045#endif
Alexander Grafbee91162016-03-04 01:09:59 +010046
Rob Clarkc160d2f2017-07-27 08:04:18 -040047static int entry_count;
Rob Clarkaf65db82017-07-27 08:04:19 -040048static int nesting_level;
Heinrich Schuchardtbc4f9132018-03-03 15:29:03 +010049/* GUID of the device tree table */
50const efi_guid_t efi_guid_fdt = EFI_FDT_GUID;
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +010051/* GUID of the EFI_DRIVER_BINDING_PROTOCOL */
52const efi_guid_t efi_guid_driver_binding_protocol =
53 EFI_DRIVER_BINDING_PROTOCOL_GUID;
Rob Clarkc160d2f2017-07-27 08:04:18 -040054
Heinrich Schuchardta3a28f52018-02-18 15:17:51 +010055/* event group ExitBootServices() invoked */
56const efi_guid_t efi_guid_event_group_exit_boot_services =
57 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES;
58/* event group SetVirtualAddressMap() invoked */
59const efi_guid_t efi_guid_event_group_virtual_address_change =
60 EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE;
61/* event group memory map changed */
62const efi_guid_t efi_guid_event_group_memory_map_change =
63 EFI_EVENT_GROUP_MEMORY_MAP_CHANGE;
64/* event group boot manager about to boot */
65const efi_guid_t efi_guid_event_group_ready_to_boot =
66 EFI_EVENT_GROUP_READY_TO_BOOT;
67/* event group ResetSystem() invoked (before ExitBootServices) */
68const efi_guid_t efi_guid_event_group_reset_system =
69 EFI_EVENT_GROUP_RESET_SYSTEM;
70
Heinrich Schuchardt2074f702018-01-11 08:16:09 +010071static efi_status_t EFIAPI efi_disconnect_controller(
72 efi_handle_t controller_handle,
73 efi_handle_t driver_image_handle,
74 efi_handle_t child_handle);
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +010075
Rob Clarkc160d2f2017-07-27 08:04:18 -040076/* Called on every callback entry */
77int __efi_entry_check(void)
78{
79 int ret = entry_count++ == 0;
80#ifdef CONFIG_ARM
81 assert(efi_gd);
82 app_gd = gd;
83 gd = efi_gd;
84#endif
85 return ret;
86}
87
88/* Called on every callback exit */
89int __efi_exit_check(void)
90{
91 int ret = --entry_count == 0;
92#ifdef CONFIG_ARM
93 gd = app_gd;
94#endif
95 return ret;
96}
97
Alexander Grafbee91162016-03-04 01:09:59 +010098/* Called from do_bootefi_exec() */
99void efi_save_gd(void)
100{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600101#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100102 efi_gd = gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600103#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100104}
105
Rob Clarkc160d2f2017-07-27 08:04:18 -0400106/*
Mario Six78a88f72018-07-10 08:40:17 +0200107 * Special case handler for error/abort that just forces things back to u-boot
108 * world so we can dump out an abort msg, without any care about returning back
109 * to UEFI world.
Rob Clarkc160d2f2017-07-27 08:04:18 -0400110 */
Alexander Grafbee91162016-03-04 01:09:59 +0100111void efi_restore_gd(void)
112{
Simon Glass65e4c0b2016-09-25 15:27:35 -0600113#ifdef CONFIG_ARM
Alexander Grafbee91162016-03-04 01:09:59 +0100114 /* Only restore if we're already in EFI context */
115 if (!efi_gd)
116 return;
Alexander Grafbee91162016-03-04 01:09:59 +0100117 gd = efi_gd;
Simon Glass65e4c0b2016-09-25 15:27:35 -0600118#endif
Alexander Grafbee91162016-03-04 01:09:59 +0100119}
120
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200121/**
Mario Six78a88f72018-07-10 08:40:17 +0200122 * indent_string() - returns a string for indenting with two spaces per level
123 * @level: indent level
Heinrich Schuchardtc8df80c2018-01-24 19:21:36 +0100124 *
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200125 * A maximum of ten indent levels is supported. Higher indent levels will be
126 * truncated.
127 *
Mario Six78a88f72018-07-10 08:40:17 +0200128 * Return: A string for indenting with two spaces per level is
129 * returned.
Rob Clarkaf65db82017-07-27 08:04:19 -0400130 */
131static const char *indent_string(int level)
132{
133 const char *indent = " ";
134 const int max = strlen(indent);
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +0100135
Rob Clarkaf65db82017-07-27 08:04:19 -0400136 level = min(max, level * 2);
137 return &indent[max - level];
138}
139
Heinrich Schuchardtae0bd3a2017-08-18 17:45:16 +0200140const char *__efi_nesting(void)
141{
142 return indent_string(nesting_level);
143}
144
Rob Clarkaf65db82017-07-27 08:04:19 -0400145const char *__efi_nesting_inc(void)
146{
147 return indent_string(nesting_level++);
148}
149
150const char *__efi_nesting_dec(void)
151{
152 return indent_string(--nesting_level);
153}
154
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200155/**
Mario Six78a88f72018-07-10 08:40:17 +0200156 * efi_queue_event() - queue an EFI event
157 * @event: event to signal
158 * @check_tpl: check the TPL level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200159 *
160 * This function queues the notification function of the event for future
161 * execution.
162 *
Mario Six78a88f72018-07-10 08:40:17 +0200163 * The notification function is called if the task priority level of the event
164 * is higher than the current task priority level.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200165 *
166 * For the SignalEvent service see efi_signal_event_ext.
167 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200168 */
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100169static void efi_queue_event(struct efi_event *event, bool check_tpl)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200170{
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200171 if (event->notify_function) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200172 event->is_queued = true;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200173 /* Check TPL */
Heinrich Schuchardt9bc96642018-01-19 20:24:51 +0100174 if (check_tpl && efi_tpl >= event->notify_tpl)
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200175 return;
Heinrich Schuchardtea630ce2017-09-15 10:06:10 +0200176 EFI_CALL_VOID(event->notify_function(event,
177 event->notify_context));
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200178 }
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200179 event->is_queued = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200180}
181
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200182/**
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200183 * is_valid_tpl() - check if the task priority level is valid
184 *
185 * @tpl: TPL level to check
186 * ReturnValue: status code
187 */
188efi_status_t is_valid_tpl(efi_uintn_t tpl)
189{
190 switch (tpl) {
191 case TPL_APPLICATION:
192 case TPL_CALLBACK:
193 case TPL_NOTIFY:
194 case TPL_HIGH_LEVEL:
195 return EFI_SUCCESS;
196 default:
197 return EFI_INVALID_PARAMETER;
198 }
199}
200
201/**
Mario Six78a88f72018-07-10 08:40:17 +0200202 * efi_signal_event() - signal an EFI event
203 * @event: event to signal
204 * @check_tpl: check the TPL level
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100205 *
Mario Six78a88f72018-07-10 08:40:17 +0200206 * This function signals an event. If the event belongs to an event group all
207 * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100208 * their notification function is queued.
209 *
210 * For the SignalEvent service see efi_signal_event_ext.
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100211 */
212void efi_signal_event(struct efi_event *event, bool check_tpl)
213{
214 if (event->group) {
215 struct efi_event *evt;
216
217 /*
218 * The signaled state has to set before executing any
219 * notification function
220 */
221 list_for_each_entry(evt, &efi_events, link) {
222 if (!evt->group || guidcmp(evt->group, event->group))
223 continue;
224 if (evt->is_signaled)
225 continue;
226 evt->is_signaled = true;
227 if (evt->type & EVT_NOTIFY_SIGNAL &&
228 evt->notify_function)
229 evt->is_queued = true;
230 }
231 list_for_each_entry(evt, &efi_events, link) {
232 if (!evt->group || guidcmp(evt->group, event->group))
233 continue;
234 if (evt->is_queued)
235 efi_queue_event(evt, check_tpl);
236 }
237 } else if (!event->is_signaled) {
238 event->is_signaled = true;
239 if (event->type & EVT_NOTIFY_SIGNAL)
240 efi_queue_event(event, check_tpl);
241 }
242}
243
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200244/**
Mario Six78a88f72018-07-10 08:40:17 +0200245 * efi_raise_tpl() - raise the task priority level
246 * @new_tpl: new value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200247 *
248 * This function implements the RaiseTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200249 *
Mario Six78a88f72018-07-10 08:40:17 +0200250 * See the Unified Extensible Firmware Interface (UEFI) specification for
251 * details.
252 *
253 * Return: old value of the task priority level
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200254 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100255static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100256{
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100257 efi_uintn_t old_tpl = efi_tpl;
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200258
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200259 EFI_ENTRY("0x%zx", new_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200260
261 if (new_tpl < efi_tpl)
262 debug("WARNING: new_tpl < current_tpl in %s\n", __func__);
263 efi_tpl = new_tpl;
264 if (efi_tpl > TPL_HIGH_LEVEL)
265 efi_tpl = TPL_HIGH_LEVEL;
266
267 EFI_EXIT(EFI_SUCCESS);
268 return old_tpl;
Alexander Grafbee91162016-03-04 01:09:59 +0100269}
270
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200271/**
Mario Six78a88f72018-07-10 08:40:17 +0200272 * efi_restore_tpl() - lower the task priority level
273 * @old_tpl: value of the task priority level to be restored
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200274 *
275 * This function implements the RestoreTpl service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200276 *
Mario Six78a88f72018-07-10 08:40:17 +0200277 * See the Unified Extensible Firmware Interface (UEFI) specification for
278 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200279 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100280static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl)
Alexander Grafbee91162016-03-04 01:09:59 +0100281{
xypron.glpk@gmx.de503f2692017-07-18 20:17:19 +0200282 EFI_ENTRY("0x%zx", old_tpl);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200283
284 if (old_tpl > efi_tpl)
285 debug("WARNING: old_tpl > current_tpl in %s\n", __func__);
286 efi_tpl = old_tpl;
287 if (efi_tpl > TPL_HIGH_LEVEL)
288 efi_tpl = TPL_HIGH_LEVEL;
289
Heinrich Schuchardt0f7fcc72018-03-24 18:40:21 +0100290 /*
291 * Lowering the TPL may have made queued events eligible for execution.
292 */
293 efi_timer_check();
294
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200295 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100296}
297
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200298/**
Mario Six78a88f72018-07-10 08:40:17 +0200299 * efi_allocate_pages_ext() - allocate memory pages
300 * @type: type of allocation to be performed
301 * @memory_type: usage type of the allocated memory
302 * @pages: number of pages to be allocated
303 * @memory: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200304 *
305 * This function implements the AllocatePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200306 *
Mario Six78a88f72018-07-10 08:40:17 +0200307 * See the Unified Extensible Firmware Interface (UEFI) specification for
308 * details.
309 *
310 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200311 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900312static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100313 efi_uintn_t pages,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900314 uint64_t *memory)
Alexander Grafbee91162016-03-04 01:09:59 +0100315{
316 efi_status_t r;
317
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100318 EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory);
Alexander Grafbee91162016-03-04 01:09:59 +0100319 r = efi_allocate_pages(type, memory_type, pages, memory);
320 return EFI_EXIT(r);
321}
322
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200323/**
Mario Six78a88f72018-07-10 08:40:17 +0200324 * efi_free_pages_ext() - Free memory pages.
325 * @memory: start of the memory area to be freed
326 * @pages: number of pages to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200327 *
328 * This function implements the FreePages service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200329 *
Mario Six78a88f72018-07-10 08:40:17 +0200330 * See the Unified Extensible Firmware Interface (UEFI) specification for
331 * details.
332 *
333 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200334 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900335static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100336 efi_uintn_t pages)
Alexander Grafbee91162016-03-04 01:09:59 +0100337{
338 efi_status_t r;
339
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900340 EFI_ENTRY("%llx, 0x%zx", memory, pages);
Alexander Grafbee91162016-03-04 01:09:59 +0100341 r = efi_free_pages(memory, pages);
342 return EFI_EXIT(r);
343}
344
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200345/**
Mario Six78a88f72018-07-10 08:40:17 +0200346 * efi_get_memory_map_ext() - get map describing memory usage
347 * @memory_map_size: on entry the size, in bytes, of the memory map buffer,
348 * on exit the size of the copied memory map
349 * @memory_map: buffer to which the memory map is written
350 * @map_key: key for the memory map
351 * @descriptor_size: size of an individual memory descriptor
352 * @descriptor_version: version number of the memory descriptor structure
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200353 *
354 * This function implements the GetMemoryMap service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200355 *
Mario Six78a88f72018-07-10 08:40:17 +0200356 * See the Unified Extensible Firmware Interface (UEFI) specification for
357 * details.
358 *
359 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200360 */
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900361static efi_status_t EFIAPI efi_get_memory_map_ext(
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100362 efi_uintn_t *memory_map_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900363 struct efi_mem_desc *memory_map,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100364 efi_uintn_t *map_key,
365 efi_uintn_t *descriptor_size,
Masahiro Yamada6e0bf8d2017-06-22 17:49:03 +0900366 uint32_t *descriptor_version)
Alexander Grafbee91162016-03-04 01:09:59 +0100367{
368 efi_status_t r;
369
370 EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map,
371 map_key, descriptor_size, descriptor_version);
372 r = efi_get_memory_map(memory_map_size, memory_map, map_key,
373 descriptor_size, descriptor_version);
374 return EFI_EXIT(r);
375}
376
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200377/**
Mario Six78a88f72018-07-10 08:40:17 +0200378 * efi_allocate_pool_ext() - allocate memory from pool
379 * @pool_type: type of the pool from which memory is to be allocated
380 * @size: number of bytes to be allocated
381 * @buffer: allocated memory
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200382 *
383 * This function implements the AllocatePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200384 *
Mario Six78a88f72018-07-10 08:40:17 +0200385 * See the Unified Extensible Firmware Interface (UEFI) specification for
386 * details.
387 *
388 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200389 */
Stefan Brünsead12742016-10-09 22:17:18 +0200390static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100391 efi_uintn_t size,
Stefan Brünsead12742016-10-09 22:17:18 +0200392 void **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100393{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100394 efi_status_t r;
395
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100396 EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer);
Stefan Brünsead12742016-10-09 22:17:18 +0200397 r = efi_allocate_pool(pool_type, size, buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100398 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100399}
400
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200401/**
Mario Six78a88f72018-07-10 08:40:17 +0200402 * efi_free_pool_ext() - free memory from pool
403 * @buffer: start of memory to be freed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200404 *
405 * This function implements the FreePool service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200406 *
Mario Six78a88f72018-07-10 08:40:17 +0200407 * See the Unified Extensible Firmware Interface (UEFI) specification for
408 * details.
409 *
410 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200411 */
Stefan Brüns42417bc2016-10-09 22:17:26 +0200412static efi_status_t EFIAPI efi_free_pool_ext(void *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +0100413{
Alexander Graf1cd29f02016-03-24 01:37:37 +0100414 efi_status_t r;
415
416 EFI_ENTRY("%p", buffer);
Stefan Brüns42417bc2016-10-09 22:17:26 +0200417 r = efi_free_pool(buffer);
Alexander Graf1cd29f02016-03-24 01:37:37 +0100418 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +0100419}
420
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200421/**
Mario Six78a88f72018-07-10 08:40:17 +0200422 * efi_add_handle() - add a new object to the object list
423 * @obj: object to be added
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100424 *
Mario Six78a88f72018-07-10 08:40:17 +0200425 * The protocols list is initialized. The object handle is set.
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100426 */
427void efi_add_handle(struct efi_object *obj)
428{
429 if (!obj)
430 return;
431 INIT_LIST_HEAD(&obj->protocols);
432 obj->handle = obj;
433 list_add_tail(&obj->link, &efi_obj_list);
434}
435
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200436/**
Mario Six78a88f72018-07-10 08:40:17 +0200437 * efi_create_handle() - create handle
438 * @handle: new handle
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200439 *
Mario Six78a88f72018-07-10 08:40:17 +0200440 * Return: status code
Heinrich Schuchardt2edab5e2017-10-26 19:25:49 +0200441 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100442efi_status_t efi_create_handle(efi_handle_t *handle)
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200443{
444 struct efi_object *obj;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200445
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200446 obj = calloc(1, sizeof(struct efi_object));
447 if (!obj)
448 return EFI_OUT_OF_RESOURCES;
449
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100450 efi_add_handle(obj);
451 *handle = obj->handle;
Heinrich Schuchardtd29e7822018-05-27 16:47:21 +0200452
453 return EFI_SUCCESS;
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +0200454}
455
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200456/**
Mario Six78a88f72018-07-10 08:40:17 +0200457 * efi_search_protocol() - find a protocol on a handle.
458 * @handle: handle
459 * @protocol_guid: GUID of the protocol
460 * @handler: reference to the protocol
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100461 *
Mario Six78a88f72018-07-10 08:40:17 +0200462 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100463 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100464efi_status_t efi_search_protocol(const efi_handle_t handle,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100465 const efi_guid_t *protocol_guid,
466 struct efi_handler **handler)
467{
468 struct efi_object *efiobj;
469 struct list_head *lhandle;
470
471 if (!handle || !protocol_guid)
472 return EFI_INVALID_PARAMETER;
473 efiobj = efi_search_obj(handle);
474 if (!efiobj)
475 return EFI_INVALID_PARAMETER;
476 list_for_each(lhandle, &efiobj->protocols) {
477 struct efi_handler *protocol;
478
479 protocol = list_entry(lhandle, struct efi_handler, link);
480 if (!guidcmp(protocol->guid, protocol_guid)) {
481 if (handler)
482 *handler = protocol;
483 return EFI_SUCCESS;
484 }
485 }
486 return EFI_NOT_FOUND;
487}
488
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200489/**
Mario Six78a88f72018-07-10 08:40:17 +0200490 * efi_remove_protocol() - delete protocol from a handle
491 * @handle: handle from which the protocol shall be deleted
492 * @protocol: GUID of the protocol to be deleted
493 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100494 *
Mario Six78a88f72018-07-10 08:40:17 +0200495 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100496 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100497efi_status_t efi_remove_protocol(const efi_handle_t handle,
498 const efi_guid_t *protocol,
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100499 void *protocol_interface)
500{
501 struct efi_handler *handler;
502 efi_status_t ret;
503
504 ret = efi_search_protocol(handle, protocol, &handler);
505 if (ret != EFI_SUCCESS)
506 return ret;
507 if (guidcmp(handler->guid, protocol))
508 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt1f470e12018-05-11 12:09:21 +0200509 if (handler->protocol_interface != protocol_interface)
510 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100511 list_del(&handler->link);
512 free(handler);
513 return EFI_SUCCESS;
514}
515
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200516/**
Mario Six78a88f72018-07-10 08:40:17 +0200517 * efi_remove_all_protocols() - delete all protocols from a handle
518 * @handle: handle from which the protocols shall be deleted
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100519 *
Mario Six78a88f72018-07-10 08:40:17 +0200520 * Return: status code
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100521 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100522efi_status_t efi_remove_all_protocols(const efi_handle_t handle)
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100523{
524 struct efi_object *efiobj;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100525 struct efi_handler *protocol;
526 struct efi_handler *pos;
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100527
528 efiobj = efi_search_obj(handle);
529 if (!efiobj)
530 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt32e6fed2018-01-11 08:15:55 +0100531 list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) {
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100532 efi_status_t ret;
533
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100534 ret = efi_remove_protocol(handle, protocol->guid,
535 protocol->protocol_interface);
536 if (ret != EFI_SUCCESS)
537 return ret;
538 }
539 return EFI_SUCCESS;
540}
541
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200542/**
Mario Six78a88f72018-07-10 08:40:17 +0200543 * efi_delete_handle() - delete handle
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100544 *
Mario Six78a88f72018-07-10 08:40:17 +0200545 * @obj: handle to delete
Heinrich Schuchardt678e03a2017-12-04 18:03:02 +0100546 */
547void efi_delete_handle(struct efi_object *obj)
548{
549 if (!obj)
550 return;
551 efi_remove_all_protocols(obj->handle);
552 list_del(&obj->link);
553 free(obj);
554}
555
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200556/**
Mario Six78a88f72018-07-10 08:40:17 +0200557 * efi_is_event() - check if a pointer is a valid event
558 * @event: pointer to check
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100559 *
Mario Six78a88f72018-07-10 08:40:17 +0200560 * Return: status code
Alexander Grafbee91162016-03-04 01:09:59 +0100561 */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100562static efi_status_t efi_is_event(const struct efi_event *event)
563{
564 const struct efi_event *evt;
565
566 if (!event)
567 return EFI_INVALID_PARAMETER;
568 list_for_each_entry(evt, &efi_events, link) {
569 if (evt == event)
570 return EFI_SUCCESS;
571 }
572 return EFI_INVALID_PARAMETER;
573}
Alexander Grafbee91162016-03-04 01:09:59 +0100574
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200575/**
Mario Six78a88f72018-07-10 08:40:17 +0200576 * efi_create_event() - create an event
577 * @type: type of the event to create
578 * @notify_tpl: task priority level of the event
579 * @notify_function: notification function of the event
580 * @notify_context: pointer passed to the notification function
581 * @group: event group
582 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200583 *
584 * This function is used inside U-Boot code to create an event.
585 *
586 * For the API function implementing the CreateEvent service see
587 * efi_create_event_ext.
588 *
Mario Six78a88f72018-07-10 08:40:17 +0200589 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200590 */
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100591efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200592 void (EFIAPI *notify_function) (
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200593 struct efi_event *event,
594 void *context),
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100595 void *notify_context, efi_guid_t *group,
596 struct efi_event **event)
Alexander Grafbee91162016-03-04 01:09:59 +0100597{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100598 struct efi_event *evt;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200599
Jonathan Graya95343b2017-03-12 19:26:07 +1100600 if (event == NULL)
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200601 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100602
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200603 switch (type) {
604 case 0:
605 case EVT_TIMER:
606 case EVT_NOTIFY_SIGNAL:
607 case EVT_TIMER | EVT_NOTIFY_SIGNAL:
608 case EVT_NOTIFY_WAIT:
609 case EVT_TIMER | EVT_NOTIFY_WAIT:
610 case EVT_SIGNAL_EXIT_BOOT_SERVICES:
611 case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE:
612 break;
613 default:
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200614 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt21b3edf2018-07-02 12:53:52 +0200615 }
Jonathan Graya95343b2017-03-12 19:26:07 +1100616
AKASHI Takahiro3748ed92018-08-10 15:36:32 +0900617 if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) &&
618 (is_valid_tpl(notify_tpl) != EFI_SUCCESS))
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200619 return EFI_INVALID_PARAMETER;
Jonathan Graya95343b2017-03-12 19:26:07 +1100620
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100621 evt = calloc(1, sizeof(struct efi_event));
622 if (!evt)
623 return EFI_OUT_OF_RESOURCES;
624 evt->type = type;
625 evt->notify_tpl = notify_tpl;
626 evt->notify_function = notify_function;
627 evt->notify_context = notify_context;
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100628 evt->group = group;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100629 /* Disable timers on bootup */
630 evt->trigger_next = -1ULL;
631 evt->is_queued = false;
632 evt->is_signaled = false;
633 list_add_tail(&evt->link, &efi_events);
634 *event = evt;
635 return EFI_SUCCESS;
Alexander Grafbee91162016-03-04 01:09:59 +0100636}
637
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200638/*
Mario Six78a88f72018-07-10 08:40:17 +0200639 * efi_create_event_ex() - create an event in a group
640 * @type: type of the event to create
641 * @notify_tpl: task priority level of the event
642 * @notify_function: notification function of the event
643 * @notify_context: pointer passed to the notification function
644 * @event: created event
645 * @event_group: event group
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100646 *
647 * This function implements the CreateEventEx service.
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100648 *
Mario Six78a88f72018-07-10 08:40:17 +0200649 * See the Unified Extensible Firmware Interface (UEFI) specification for
650 * details.
651 *
652 * Return: status code
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100653 */
654efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl,
655 void (EFIAPI *notify_function) (
656 struct efi_event *event,
657 void *context),
658 void *notify_context,
659 efi_guid_t *event_group,
660 struct efi_event **event)
661{
662 EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function,
663 notify_context, event_group);
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100664 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100665 notify_context, event_group, event));
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +0100666}
667
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200668/**
Mario Six78a88f72018-07-10 08:40:17 +0200669 * efi_create_event_ext() - create an event
670 * @type: type of the event to create
671 * @notify_tpl: task priority level of the event
672 * @notify_function: notification function of the event
673 * @notify_context: pointer passed to the notification function
674 * @event: created event
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200675 *
676 * This function implements the CreateEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200677 *
Mario Six78a88f72018-07-10 08:40:17 +0200678 * See the Unified Extensible Firmware Interface (UEFI) specification for
679 * details.
680 *
681 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200682 */
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200683static efi_status_t EFIAPI efi_create_event_ext(
Heinrich Schuchardt152cade2017-11-06 21:17:47 +0100684 uint32_t type, efi_uintn_t notify_tpl,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200685 void (EFIAPI *notify_function) (
686 struct efi_event *event,
687 void *context),
688 void *notify_context, struct efi_event **event)
689{
690 EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function,
691 notify_context);
692 return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function,
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100693 notify_context, NULL, event));
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +0200694}
695
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200696/**
Mario Six78a88f72018-07-10 08:40:17 +0200697 * efi_timer_check() - check if a timer event has occurred
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200698 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200699 * Check if a timer event has occurred or a queued notification function should
700 * be called.
701 *
Alexander Grafbee91162016-03-04 01:09:59 +0100702 * Our timers have to work without interrupts, so we check whenever keyboard
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200703 * input or disk accesses happen if enough time elapsed for them to fire.
Alexander Grafbee91162016-03-04 01:09:59 +0100704 */
705void efi_timer_check(void)
706{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100707 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +0100708 u64 now = timer_get_us();
709
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100710 list_for_each_entry(evt, &efi_events, link) {
711 if (evt->is_queued)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100712 efi_queue_event(evt, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100713 if (!(evt->type & EVT_TIMER) || now < evt->trigger_next)
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200714 continue;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100715 switch (evt->trigger_type) {
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200716 case EFI_TIMER_RELATIVE:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100717 evt->trigger_type = EFI_TIMER_STOP;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200718 break;
719 case EFI_TIMER_PERIODIC:
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100720 evt->trigger_next += evt->trigger_time;
Heinrich Schuchardtca62a4f2017-09-15 10:06:13 +0200721 break;
722 default:
723 continue;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200724 }
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100725 evt->is_signaled = false;
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100726 efi_signal_event(evt, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100727 }
Alexander Grafbee91162016-03-04 01:09:59 +0100728 WATCHDOG_RESET();
729}
730
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200731/**
Mario Six78a88f72018-07-10 08:40:17 +0200732 * efi_set_timer() - set the trigger time for a timer event or stop the event
733 * @event: event for which the timer is set
734 * @type: type of the timer
735 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200736 *
737 * This is the function for internal usage in U-Boot. For the API function
738 * implementing the SetTimer service see efi_set_timer_ext.
739 *
Mario Six78a88f72018-07-10 08:40:17 +0200740 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200741 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200742efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200743 uint64_t trigger_time)
Alexander Grafbee91162016-03-04 01:09:59 +0100744{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100745 /* Check that the event is valid */
746 if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER))
747 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100748
xypron.glpk@gmx.de8787b022017-07-18 20:17:23 +0200749 /*
750 * The parameter defines a multiple of 100ns.
751 * We use multiples of 1000ns. So divide by 10.
752 */
Heinrich Schuchardt7d963322017-10-05 16:14:14 +0200753 do_div(trigger_time, 10);
Alexander Grafbee91162016-03-04 01:09:59 +0100754
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100755 switch (type) {
756 case EFI_TIMER_STOP:
757 event->trigger_next = -1ULL;
758 break;
759 case EFI_TIMER_PERIODIC:
760 case EFI_TIMER_RELATIVE:
761 event->trigger_next = timer_get_us() + trigger_time;
762 break;
763 default:
764 return EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +0100765 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100766 event->trigger_type = type;
767 event->trigger_time = trigger_time;
768 event->is_signaled = false;
769 return EFI_SUCCESS;
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200770}
771
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200772/**
Mario Six78a88f72018-07-10 08:40:17 +0200773 * efi_set_timer_ext() - Set the trigger time for a timer event or stop the
774 * event
775 * @event: event for which the timer is set
776 * @type: type of the timer
777 * @trigger_time: trigger period in multiples of 100ns
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200778 *
779 * This function implements the SetTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200780 *
Mario Six78a88f72018-07-10 08:40:17 +0200781 * See the Unified Extensible Firmware Interface (UEFI) specification for
782 * details.
783 *
784 *
785 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200786 */
xypron.glpk@gmx.deb521d292017-07-19 19:22:34 +0200787static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event,
788 enum efi_timer_delay type,
789 uint64_t trigger_time)
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200790{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900791 EFI_ENTRY("%p, %d, %llx", event, type, trigger_time);
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +0200792 return EFI_EXIT(efi_set_timer(event, type, trigger_time));
Alexander Grafbee91162016-03-04 01:09:59 +0100793}
794
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200795/**
Mario Six78a88f72018-07-10 08:40:17 +0200796 * efi_wait_for_event() - wait for events to be signaled
797 * @num_events: number of events to be waited for
798 * @event: events to be waited for
799 * @index: index of the event that was signaled
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200800 *
801 * This function implements the WaitForEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200802 *
Mario Six78a88f72018-07-10 08:40:17 +0200803 * See the Unified Extensible Firmware Interface (UEFI) specification for
804 * details.
805 *
806 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200807 */
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100808static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events,
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200809 struct efi_event **event,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100810 efi_uintn_t *index)
Alexander Grafbee91162016-03-04 01:09:59 +0100811{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100812 int i;
Alexander Grafbee91162016-03-04 01:09:59 +0100813
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +0100814 EFI_ENTRY("%zd, %p, %p", num_events, event, index);
Alexander Grafbee91162016-03-04 01:09:59 +0100815
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200816 /* Check parameters */
817 if (!num_events || !event)
818 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardt32f4b2f2017-09-15 10:06:16 +0200819 /* Check TPL */
820 if (efi_tpl != TPL_APPLICATION)
821 return EFI_EXIT(EFI_UNSUPPORTED);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200822 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100823 if (efi_is_event(event[i]) != EFI_SUCCESS)
824 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200825 if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL)
826 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200827 if (!event[i]->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100828 efi_queue_event(event[i], true);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200829 }
830
831 /* Wait for signal */
832 for (;;) {
833 for (i = 0; i < num_events; ++i) {
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200834 if (event[i]->is_signaled)
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200835 goto out;
836 }
837 /* Allow events to occur. */
838 efi_timer_check();
839 }
840
841out:
842 /*
843 * Reset the signal which is passed to the caller to allow periodic
844 * events to occur.
845 */
Heinrich Schuchardte190e892017-10-04 15:03:24 +0200846 event[i]->is_signaled = false;
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200847 if (index)
848 *index = i;
Alexander Grafbee91162016-03-04 01:09:59 +0100849
850 return EFI_EXIT(EFI_SUCCESS);
851}
852
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200853/**
Mario Six78a88f72018-07-10 08:40:17 +0200854 * efi_signal_event_ext() - signal an EFI event
855 * @event: event to signal
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200856 *
857 * This function implements the SignalEvent service.
Mario Six78a88f72018-07-10 08:40:17 +0200858 *
859 * See the Unified Extensible Firmware Interface (UEFI) specification for
860 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200861 *
862 * This functions sets the signaled state of the event and queues the
863 * notification function for execution.
864 *
Mario Six78a88f72018-07-10 08:40:17 +0200865 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200866 */
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200867static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100868{
869 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100870 if (efi_is_event(event) != EFI_SUCCESS)
871 return EFI_EXIT(EFI_INVALID_PARAMETER);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100872 efi_signal_event(event, true);
Alexander Grafbee91162016-03-04 01:09:59 +0100873 return EFI_EXIT(EFI_SUCCESS);
874}
875
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200876/**
Mario Six78a88f72018-07-10 08:40:17 +0200877 * efi_close_event() - close an EFI event
878 * @event: event to close
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200879 *
880 * This function implements the CloseEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200881 *
Mario Six78a88f72018-07-10 08:40:17 +0200882 * See the Unified Extensible Firmware Interface (UEFI) specification for
883 * details.
884 *
885 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200886 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200887static efi_status_t EFIAPI efi_close_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100888{
889 EFI_ENTRY("%p", event);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100890 if (efi_is_event(event) != EFI_SUCCESS)
891 return EFI_EXIT(EFI_INVALID_PARAMETER);
892 list_del(&event->link);
893 free(event);
894 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +0100895}
896
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200897/**
Mario Six78a88f72018-07-10 08:40:17 +0200898 * efi_check_event() - check if an event is signaled
899 * @event: event to check
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200900 *
901 * This function implements the CheckEvent service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200902 *
Mario Six78a88f72018-07-10 08:40:17 +0200903 * See the Unified Extensible Firmware Interface (UEFI) specification for
904 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200905 *
Mario Six78a88f72018-07-10 08:40:17 +0200906 * If an event is not signaled yet, the notification function is queued. The
907 * signaled state is cleared.
908 *
909 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +0200910 */
xypron.glpk@gmx.de2fd945f2017-07-18 20:17:17 +0200911static efi_status_t EFIAPI efi_check_event(struct efi_event *event)
Alexander Grafbee91162016-03-04 01:09:59 +0100912{
913 EFI_ENTRY("%p", event);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200914 efi_timer_check();
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100915 if (efi_is_event(event) != EFI_SUCCESS ||
916 event->type & EVT_NOTIFY_SIGNAL)
917 return EFI_EXIT(EFI_INVALID_PARAMETER);
918 if (!event->is_signaled)
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +0100919 efi_queue_event(event, true);
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100920 if (event->is_signaled) {
921 event->is_signaled = false;
922 return EFI_EXIT(EFI_SUCCESS);
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +0200923 }
Heinrich Schuchardt43bce442018-02-18 15:17:50 +0100924 return EFI_EXIT(EFI_NOT_READY);
Alexander Grafbee91162016-03-04 01:09:59 +0100925}
926
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200927/**
Mario Six78a88f72018-07-10 08:40:17 +0200928 * efi_search_obj() - find the internal EFI object for a handle
929 * @handle: handle to find
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200930 *
Mario Six78a88f72018-07-10 08:40:17 +0200931 * Return: EFI object
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200932 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100933struct efi_object *efi_search_obj(const efi_handle_t handle)
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200934{
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100935 struct efi_object *efiobj;
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200936
Heinrich Schuchardt1b681532017-11-06 21:17:50 +0100937 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt7b9f8ad2017-10-18 18:13:03 +0200938 if (efiobj->handle == handle)
939 return efiobj;
940 }
941
942 return NULL;
943}
944
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200945/**
Mario Six78a88f72018-07-10 08:40:17 +0200946 * efi_open_protocol_info_entry() - create open protocol info entry and add it
947 * to a protocol
948 * @handler: handler of a protocol
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100949 *
Mario Six78a88f72018-07-10 08:40:17 +0200950 * Return: open protocol info entry
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100951 */
952static struct efi_open_protocol_info_entry *efi_create_open_info(
953 struct efi_handler *handler)
954{
955 struct efi_open_protocol_info_item *item;
956
957 item = calloc(1, sizeof(struct efi_open_protocol_info_item));
958 if (!item)
959 return NULL;
960 /* Append the item to the open protocol info list. */
961 list_add_tail(&item->link, &handler->open_infos);
962
963 return &item->info;
964}
965
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200966/**
Mario Six78a88f72018-07-10 08:40:17 +0200967 * efi_delete_open_info() - remove an open protocol info entry from a protocol
968 * @item: open protocol info entry to delete
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100969 *
Mario Six78a88f72018-07-10 08:40:17 +0200970 * Return: status code
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +0100971 */
972static efi_status_t efi_delete_open_info(
973 struct efi_open_protocol_info_item *item)
974{
975 list_del(&item->link);
976 free(item);
977 return EFI_SUCCESS;
978}
979
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +0200980/**
Mario Six78a88f72018-07-10 08:40:17 +0200981 * efi_add_protocol() - install new protocol on a handle
982 * @handle: handle on which the protocol shall be installed
983 * @protocol: GUID of the protocol to be installed
984 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200985 *
Mario Six78a88f72018-07-10 08:40:17 +0200986 * Return: status code
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200987 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +0100988efi_status_t efi_add_protocol(const efi_handle_t handle,
989 const efi_guid_t *protocol,
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200990 void *protocol_interface)
991{
992 struct efi_object *efiobj;
993 struct efi_handler *handler;
994 efi_status_t ret;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +0200995
996 efiobj = efi_search_obj(handle);
997 if (!efiobj)
998 return EFI_INVALID_PARAMETER;
999 ret = efi_search_protocol(handle, protocol, NULL);
1000 if (ret != EFI_NOT_FOUND)
1001 return EFI_INVALID_PARAMETER;
1002 handler = calloc(1, sizeof(struct efi_handler));
1003 if (!handler)
1004 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001005 handler->guid = protocol;
1006 handler->protocol_interface = protocol_interface;
Heinrich Schuchardtfe1599d2018-01-11 08:15:57 +01001007 INIT_LIST_HEAD(&handler->open_infos);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001008 list_add_tail(&handler->link, &efiobj->protocols);
Heinrich Schuchardtd5504142018-01-11 08:16:01 +01001009 if (!guidcmp(&efi_guid_device_path, protocol))
1010 EFI_PRINT("installed device path '%pD'\n", protocol_interface);
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01001011 return EFI_SUCCESS;
Heinrich Schuchardt3f79a2b2017-10-26 19:25:53 +02001012}
1013
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001014/**
Mario Six78a88f72018-07-10 08:40:17 +02001015 * efi_install_protocol_interface() - install protocol interface
1016 * @handle: handle on which the protocol shall be installed
1017 * @protocol: GUID of the protocol to be installed
1018 * @protocol_interface_type: type of the interface to be installed,
1019 * always EFI_NATIVE_INTERFACE
1020 * @protocol_interface: interface of the protocol implementation
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001021 *
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001022 * This function implements the InstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001023 *
Mario Six78a88f72018-07-10 08:40:17 +02001024 * See the Unified Extensible Firmware Interface (UEFI) specification for
1025 * details.
1026 *
1027 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001028 */
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001029static efi_status_t EFIAPI efi_install_protocol_interface(
1030 void **handle, const efi_guid_t *protocol,
1031 int protocol_interface_type, void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001032{
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001033 efi_status_t r;
1034
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001035 EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type,
1036 protocol_interface);
1037
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001038 if (!handle || !protocol ||
1039 protocol_interface_type != EFI_NATIVE_INTERFACE) {
1040 r = EFI_INVALID_PARAMETER;
1041 goto out;
1042 }
1043
1044 /* Create new handle if requested. */
1045 if (!*handle) {
Heinrich Schuchardt3cc6e3f2017-08-27 00:51:09 +02001046 r = efi_create_handle(handle);
1047 if (r != EFI_SUCCESS)
1048 goto out;
Heinrich Schuchardtaf1408e2017-10-26 19:25:43 +02001049 debug("%sEFI: new handle %p\n", indent_string(nesting_level),
1050 *handle);
1051 } else {
1052 debug("%sEFI: handle %p\n", indent_string(nesting_level),
1053 *handle);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001054 }
Heinrich Schuchardt12025302017-10-26 19:25:54 +02001055 /* Add new protocol */
1056 r = efi_add_protocol(*handle, protocol, protocol_interface);
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001057out:
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01001058 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001059}
xypron.glpk@gmx.dee0549f82017-07-11 22:06:16 +02001060
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001061/**
Mario Six78a88f72018-07-10 08:40:17 +02001062 * efi_get_drivers() - get all drivers associated to a controller
1063 * @efiobj: handle of the controller
1064 * @protocol: protocol guid (optional)
1065 * @number_of_drivers: number of child controllers
1066 * @driver_handle_buffer: handles of the the drivers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001067 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001068 * The allocated buffer has to be freed with free().
1069 *
Mario Six78a88f72018-07-10 08:40:17 +02001070 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001071 */
1072static efi_status_t efi_get_drivers(struct efi_object *efiobj,
1073 const efi_guid_t *protocol,
1074 efi_uintn_t *number_of_drivers,
1075 efi_handle_t **driver_handle_buffer)
1076{
1077 struct efi_handler *handler;
1078 struct efi_open_protocol_info_item *item;
1079 efi_uintn_t count = 0, i;
1080 bool duplicate;
1081
1082 /* Count all driver associations */
1083 list_for_each_entry(handler, &efiobj->protocols, link) {
1084 if (protocol && guidcmp(handler->guid, protocol))
1085 continue;
1086 list_for_each_entry(item, &handler->open_infos, link) {
1087 if (item->info.attributes &
1088 EFI_OPEN_PROTOCOL_BY_DRIVER)
1089 ++count;
1090 }
1091 }
1092 /*
1093 * Create buffer. In case of duplicate driver assignments the buffer
1094 * will be too large. But that does not harm.
1095 */
1096 *number_of_drivers = 0;
1097 *driver_handle_buffer = calloc(count, sizeof(efi_handle_t));
1098 if (!*driver_handle_buffer)
1099 return EFI_OUT_OF_RESOURCES;
1100 /* Collect unique driver handles */
1101 list_for_each_entry(handler, &efiobj->protocols, link) {
1102 if (protocol && guidcmp(handler->guid, protocol))
1103 continue;
1104 list_for_each_entry(item, &handler->open_infos, link) {
1105 if (item->info.attributes &
1106 EFI_OPEN_PROTOCOL_BY_DRIVER) {
1107 /* Check this is a new driver */
1108 duplicate = false;
1109 for (i = 0; i < *number_of_drivers; ++i) {
1110 if ((*driver_handle_buffer)[i] ==
1111 item->info.agent_handle)
1112 duplicate = true;
1113 }
1114 /* Copy handle to buffer */
1115 if (!duplicate) {
1116 i = (*number_of_drivers)++;
1117 (*driver_handle_buffer)[i] =
1118 item->info.agent_handle;
1119 }
1120 }
1121 }
1122 }
1123 return EFI_SUCCESS;
1124}
1125
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001126/**
Mario Six78a88f72018-07-10 08:40:17 +02001127 * efi_disconnect_all_drivers() - disconnect all drivers from a controller
1128 * @efiobj: handle of the controller
1129 * @protocol: protocol guid (optional)
1130 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001131 *
1132 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001133 *
Mario Six78a88f72018-07-10 08:40:17 +02001134 * See the Unified Extensible Firmware Interface (UEFI) specification for
1135 * details.
1136 *
1137 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01001138 */
1139static efi_status_t efi_disconnect_all_drivers(
1140 struct efi_object *efiobj,
1141 const efi_guid_t *protocol,
1142 efi_handle_t child_handle)
1143{
1144 efi_uintn_t number_of_drivers;
1145 efi_handle_t *driver_handle_buffer;
1146 efi_status_t r, ret;
1147
1148 ret = efi_get_drivers(efiobj, protocol, &number_of_drivers,
1149 &driver_handle_buffer);
1150 if (ret != EFI_SUCCESS)
1151 return ret;
1152
1153 ret = EFI_NOT_FOUND;
1154 while (number_of_drivers) {
1155 r = EFI_CALL(efi_disconnect_controller(
1156 efiobj->handle,
1157 driver_handle_buffer[--number_of_drivers],
1158 child_handle));
1159 if (r == EFI_SUCCESS)
1160 ret = r;
1161 }
1162 free(driver_handle_buffer);
1163 return ret;
1164}
1165
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001166/**
Mario Six78a88f72018-07-10 08:40:17 +02001167 * efi_uninstall_protocol_interface() - uninstall protocol interface
1168 * @handle: handle from which the protocol shall be removed
1169 * @protocol: GUID of the protocol to be removed
1170 * @protocol_interface: interface to be removed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001171 *
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001172 * This function implements the UninstallProtocolInterface service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001173 *
Mario Six78a88f72018-07-10 08:40:17 +02001174 * See the Unified Extensible Firmware Interface (UEFI) specification for
1175 * details.
1176 *
1177 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001178 */
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001179static efi_status_t EFIAPI efi_uninstall_protocol_interface(
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001180 efi_handle_t handle, const efi_guid_t *protocol,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001181 void *protocol_interface)
Alexander Grafbee91162016-03-04 01:09:59 +01001182{
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001183 struct efi_object *efiobj;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001184 struct efi_handler *handler;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001185 struct efi_open_protocol_info_item *item;
1186 struct efi_open_protocol_info_item *pos;
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001187 efi_status_t r;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001188
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001189 EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface);
1190
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001191 /* Check handle */
1192 efiobj = efi_search_obj(handle);
1193 if (!efiobj) {
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001194 r = EFI_INVALID_PARAMETER;
1195 goto out;
1196 }
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001197 /* Find the protocol on the handle */
1198 r = efi_search_protocol(handle, protocol, &handler);
1199 if (r != EFI_SUCCESS)
1200 goto out;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001201 /* Disconnect controllers */
1202 efi_disconnect_all_drivers(efiobj, protocol, NULL);
1203 if (!list_empty(&handler->open_infos)) {
Heinrich Schuchardt58105112017-10-26 19:25:56 +02001204 r = EFI_ACCESS_DENIED;
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001205 goto out;
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001206 }
Heinrich Schuchardtad973732018-01-11 08:16:05 +01001207 /* Close protocol */
1208 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
1209 if (item->info.attributes ==
1210 EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL ||
1211 item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL ||
1212 item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
1213 list_del(&item->link);
1214 }
1215 if (!list_empty(&handler->open_infos)) {
1216 r = EFI_ACCESS_DENIED;
1217 goto out;
1218 }
1219 r = efi_remove_protocol(handle, protocol, protocol_interface);
xypron.glpk@gmx.de4b6ed0d2017-07-11 22:06:17 +02001220out:
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01001221 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01001222}
1223
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001224/**
Mario Six78a88f72018-07-10 08:40:17 +02001225 * efi_register_protocol_notify() - register an event for notification when a
1226 * protocol is installed.
1227 * @protocol: GUID of the protocol whose installation shall be notified
1228 * @event: event to be signaled upon installation of the protocol
1229 * @registration: key for retrieving the registration information
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001230 *
1231 * This function implements the RegisterProtocolNotify service.
1232 * See the Unified Extensible Firmware Interface (UEFI) specification
1233 * for details.
1234 *
Mario Six78a88f72018-07-10 08:40:17 +02001235 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001236 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001237static efi_status_t EFIAPI efi_register_protocol_notify(
1238 const efi_guid_t *protocol,
1239 struct efi_event *event,
1240 void **registration)
Alexander Grafbee91162016-03-04 01:09:59 +01001241{
Rob Clark778e6af2017-09-13 18:05:41 -04001242 EFI_ENTRY("%pUl, %p, %p", protocol, event, registration);
Alexander Grafbee91162016-03-04 01:09:59 +01001243 return EFI_EXIT(EFI_OUT_OF_RESOURCES);
1244}
1245
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001246/**
Mario Six78a88f72018-07-10 08:40:17 +02001247 * efi_search() - determine if an EFI handle implements a protocol
1248 * @search_type: selection criterion
1249 * @protocol: GUID of the protocol
1250 * @search_key: registration key
1251 * @efiobj: handle
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001252 *
1253 * See the documentation of the LocateHandle service in the UEFI specification.
1254 *
Mario Six78a88f72018-07-10 08:40:17 +02001255 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001256 */
Alexander Grafbee91162016-03-04 01:09:59 +01001257static int efi_search(enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001258 const efi_guid_t *protocol, void *search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01001259 struct efi_object *efiobj)
1260{
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001261 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001262
1263 switch (search_type) {
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001264 case ALL_HANDLES:
Alexander Grafbee91162016-03-04 01:09:59 +01001265 return 0;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001266 case BY_REGISTER_NOTIFY:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001267 /* TODO: RegisterProtocolNotify is not implemented yet */
Alexander Grafbee91162016-03-04 01:09:59 +01001268 return -1;
Heinrich Schuchardt9f0770f2017-11-06 21:17:42 +01001269 case BY_PROTOCOL:
Heinrich Schuchardt42cf1242017-10-26 19:25:55 +02001270 ret = efi_search_protocol(efiobj->handle, protocol, NULL);
1271 return (ret != EFI_SUCCESS);
1272 default:
1273 /* Invalid search type */
Alexander Grafbee91162016-03-04 01:09:59 +01001274 return -1;
1275 }
Alexander Grafbee91162016-03-04 01:09:59 +01001276}
1277
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001278/**
Mario Six78a88f72018-07-10 08:40:17 +02001279 * efi_locate_handle() - locate handles implementing a protocol
1280 * @search_type: selection criterion
1281 * @protocol: GUID of the protocol
1282 * @search_key: registration key
1283 * @buffer_size: size of the buffer to receive the handles in bytes
1284 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001285 *
1286 * This function is meant for U-Boot internal calls. For the API implementation
1287 * of the LocateHandle service see efi_locate_handle_ext.
1288 *
Mario Six78a88f72018-07-10 08:40:17 +02001289 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001290 */
xypron.glpk@gmx.deebf199b2017-08-09 20:55:00 +02001291static efi_status_t efi_locate_handle(
Alexander Grafbee91162016-03-04 01:09:59 +01001292 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001293 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001294 efi_uintn_t *buffer_size, efi_handle_t *buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01001295{
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001296 struct efi_object *efiobj;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001297 efi_uintn_t size = 0;
Alexander Grafbee91162016-03-04 01:09:59 +01001298
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001299 /* Check parameters */
1300 switch (search_type) {
1301 case ALL_HANDLES:
1302 break;
1303 case BY_REGISTER_NOTIFY:
1304 if (!search_key)
1305 return EFI_INVALID_PARAMETER;
1306 /* RegisterProtocolNotify is not implemented yet */
1307 return EFI_UNSUPPORTED;
1308 case BY_PROTOCOL:
1309 if (!protocol)
1310 return EFI_INVALID_PARAMETER;
1311 break;
1312 default:
1313 return EFI_INVALID_PARAMETER;
1314 }
1315
1316 /*
1317 * efi_locate_handle_buffer uses this function for
1318 * the calculation of the necessary buffer size.
1319 * So do not require a buffer for buffersize == 0.
1320 */
1321 if (!buffer_size || (*buffer_size && !buffer))
1322 return EFI_INVALID_PARAMETER;
1323
Alexander Grafbee91162016-03-04 01:09:59 +01001324 /* Count how much space we need */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001325 list_for_each_entry(efiobj, &efi_obj_list, link) {
1326 if (!efi_search(search_type, protocol, search_key, efiobj))
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001327 size += sizeof(void *);
Alexander Grafbee91162016-03-04 01:09:59 +01001328 }
1329
1330 if (*buffer_size < size) {
1331 *buffer_size = size;
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001332 return EFI_BUFFER_TOO_SMALL;
Alexander Grafbee91162016-03-04 01:09:59 +01001333 }
1334
Rob Clark796a78c2017-08-06 14:10:07 -04001335 *buffer_size = size;
1336 if (size == 0)
1337 return EFI_NOT_FOUND;
1338
Alexander Grafbee91162016-03-04 01:09:59 +01001339 /* Then fill the array */
Heinrich Schuchardtcaf864e2017-11-06 21:17:49 +01001340 list_for_each_entry(efiobj, &efi_obj_list, link) {
1341 if (!efi_search(search_type, protocol, search_key, efiobj))
1342 *buffer++ = efiobj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001343 }
1344
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001345 return EFI_SUCCESS;
1346}
1347
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001348/**
Mario Six78a88f72018-07-10 08:40:17 +02001349 * efi_locate_handle_ext() - locate handles implementing a protocol.
1350 * @search_type: selection criterion
1351 * @protocol: GUID of the protocol
1352 * @search_key: registration key
1353 * @buffer_size: size of the buffer to receive the handles in bytes
1354 * @buffer: buffer to receive the relevant handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001355 *
1356 * This function implements the LocateHandle service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001357 *
Mario Six78a88f72018-07-10 08:40:17 +02001358 * See the Unified Extensible Firmware Interface (UEFI) specification for
1359 * details.
1360 *
1361 * Return: 0 if the handle implements the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001362 */
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001363static efi_status_t EFIAPI efi_locate_handle_ext(
1364 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001365 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01001366 efi_uintn_t *buffer_size, efi_handle_t *buffer)
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001367{
Rob Clark778e6af2017-09-13 18:05:41 -04001368 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02001369 buffer_size, buffer);
1370
1371 return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key,
1372 buffer_size, buffer));
Alexander Grafbee91162016-03-04 01:09:59 +01001373}
1374
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001375/**
Mario Six78a88f72018-07-10 08:40:17 +02001376 * efi_remove_configuration_table() - collapses configuration table entries,
1377 * removing index i
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001378 *
Mario Six78a88f72018-07-10 08:40:17 +02001379 * @i: index of the table entry to be removed
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001380 */
Alexander Grafd98cdf62017-07-26 13:41:04 +02001381static void efi_remove_configuration_table(int i)
1382{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001383 struct efi_configuration_table *this = &systab.tables[i];
1384 struct efi_configuration_table *next = &systab.tables[i + 1];
1385 struct efi_configuration_table *end = &systab.tables[systab.nr_tables];
Alexander Grafd98cdf62017-07-26 13:41:04 +02001386
1387 memmove(this, next, (ulong)end - (ulong)next);
1388 systab.nr_tables--;
1389}
1390
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001391/**
Mario Six78a88f72018-07-10 08:40:17 +02001392 * efi_install_configuration_table() - adds, updates, or removes a
1393 * configuration table
1394 * @guid: GUID of the installed table
1395 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001396 *
1397 * This function is used for internal calls. For the API implementation of the
1398 * InstallConfigurationTable service see efi_install_configuration_table_ext.
1399 *
Mario Six78a88f72018-07-10 08:40:17 +02001400 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001401 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001402efi_status_t efi_install_configuration_table(const efi_guid_t *guid,
1403 void *table)
Alexander Grafbee91162016-03-04 01:09:59 +01001404{
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001405 struct efi_event *evt;
Alexander Grafbee91162016-03-04 01:09:59 +01001406 int i;
1407
Heinrich Schuchardteb68b4e2018-02-18 00:08:00 +01001408 if (!guid)
1409 return EFI_INVALID_PARAMETER;
1410
Alexander Grafbee91162016-03-04 01:09:59 +01001411 /* Check for guid override */
1412 for (i = 0; i < systab.nr_tables; i++) {
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001413 if (!guidcmp(guid, &systab.tables[i].guid)) {
Alexander Grafd98cdf62017-07-26 13:41:04 +02001414 if (table)
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001415 systab.tables[i].table = table;
Alexander Grafd98cdf62017-07-26 13:41:04 +02001416 else
1417 efi_remove_configuration_table(i);
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001418 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01001419 }
1420 }
1421
Alexander Grafd98cdf62017-07-26 13:41:04 +02001422 if (!table)
1423 return EFI_NOT_FOUND;
1424
Alexander Grafbee91162016-03-04 01:09:59 +01001425 /* No override, check for overflow */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001426 if (i >= EFI_MAX_CONFIGURATION_TABLES)
Alexander Graf488bf122016-08-19 01:23:24 +02001427 return EFI_OUT_OF_RESOURCES;
Alexander Grafbee91162016-03-04 01:09:59 +01001428
1429 /* Add a new entry */
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02001430 memcpy(&systab.tables[i].guid, guid, sizeof(*guid));
1431 systab.tables[i].table = table;
Alexander Grafaba5e912016-08-19 01:23:30 +02001432 systab.nr_tables = i + 1;
Alexander Grafbee91162016-03-04 01:09:59 +01001433
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001434out:
Heinrich Schuchardt55d8ee32018-07-07 15:36:05 +02001435 /* systab.nr_tables may have changed. So we need to update the crc32 */
1436 efi_update_table_header_crc32(&systab.hdr);
1437
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001438 /* Notify that the configuration table was changed */
1439 list_for_each_entry(evt, &efi_events, link) {
1440 if (evt->group && !guidcmp(evt->group, guid)) {
1441 efi_signal_event(evt, false);
1442 break;
1443 }
1444 }
1445
Alexander Graf488bf122016-08-19 01:23:24 +02001446 return EFI_SUCCESS;
1447}
1448
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001449/**
Mario Six78a88f72018-07-10 08:40:17 +02001450 * efi_install_configuration_table_ex() - Adds, updates, or removes a
1451 * configuration table.
1452 * @guid: GUID of the installed table
1453 * @table: table to be installed
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001454 *
1455 * This function implements the InstallConfigurationTable service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001456 *
Mario Six78a88f72018-07-10 08:40:17 +02001457 * See the Unified Extensible Firmware Interface (UEFI) specification for
1458 * details.
1459 *
1460 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001461 */
Alexander Graf488bf122016-08-19 01:23:24 +02001462static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid,
1463 void *table)
1464{
Rob Clark778e6af2017-09-13 18:05:41 -04001465 EFI_ENTRY("%pUl, %p", guid, table);
Alexander Graf488bf122016-08-19 01:23:24 +02001466 return EFI_EXIT(efi_install_configuration_table(guid, table));
Alexander Grafbee91162016-03-04 01:09:59 +01001467}
1468
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001469/**
Mario Six78a88f72018-07-10 08:40:17 +02001470 * efi_setup_loaded_image() - initialize a loaded image
1471 * @info: loaded image info to be passed to the entry point of the image
1472 * @obj: internal object associated with the loaded image
1473 * @device_path: device path of the loaded image
1474 * @file_path: file path of the loaded image
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001475 *
1476 * Initialize a loaded_image_info and loaded_image_info object with correct
Rob Clark95c55532017-09-13 18:05:33 -04001477 * protocols, boot-device, etc.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001478 *
Mario Six78a88f72018-07-10 08:40:17 +02001479 * Return: status code
Rob Clark95c55532017-09-13 18:05:33 -04001480 */
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001481efi_status_t efi_setup_loaded_image(
1482 struct efi_loaded_image *info, struct efi_object *obj,
1483 struct efi_device_path *device_path,
1484 struct efi_device_path *file_path)
Rob Clark95c55532017-09-13 18:05:33 -04001485{
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001486 efi_status_t ret;
1487
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001488 /* Add internal object to object list */
1489 efi_add_handle(obj);
1490 /* efi_exit() assumes that the handle points to the info */
Rob Clark95c55532017-09-13 18:05:33 -04001491 obj->handle = info;
1492
Heinrich Schuchardt95147312018-07-05 08:17:58 +02001493 info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
Rob Clark95c55532017-09-13 18:05:33 -04001494 info->file_path = file_path;
Rob Clark95c55532017-09-13 18:05:33 -04001495
Heinrich Schuchardt7df5af62018-01-26 06:50:54 +01001496 if (device_path) {
1497 info->device_handle = efi_dp_find_obj(device_path, NULL);
1498 /*
1499 * When asking for the device path interface, return
1500 * bootefi_device_path
1501 */
1502 ret = efi_add_protocol(obj->handle, &efi_guid_device_path,
1503 device_path);
1504 if (ret != EFI_SUCCESS)
1505 goto failure;
1506 }
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001507
1508 /*
1509 * When asking for the loaded_image interface, just
1510 * return handle which points to loaded_image_info
1511 */
1512 ret = efi_add_protocol(obj->handle, &efi_guid_loaded_image, info);
1513 if (ret != EFI_SUCCESS)
1514 goto failure;
1515
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001516 ret = efi_add_protocol(obj->handle,
1517 &efi_guid_device_path_to_text_protocol,
1518 (void *)&efi_device_path_to_text);
1519 if (ret != EFI_SUCCESS)
1520 goto failure;
1521
Leif Lindholme70f8df2018-03-09 17:43:21 +01001522 ret = efi_add_protocol(obj->handle,
1523 &efi_guid_device_path_utilities_protocol,
1524 (void *)&efi_device_path_utilities);
1525 if (ret != EFI_SUCCESS)
1526 goto failure;
1527
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001528 return ret;
Heinrich Schuchardt48b66232017-10-26 19:25:58 +02001529failure:
1530 printf("ERROR: Failure to install protocols for loaded image\n");
Heinrich Schuchardt56d92882017-12-04 18:03:01 +01001531 return ret;
Rob Clark95c55532017-09-13 18:05:33 -04001532}
1533
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001534/**
Mario Six78a88f72018-07-10 08:40:17 +02001535 * efi_load_image_from_path() - load an image using a file path
1536 * @file_path: the path of the image to load
1537 * @buffer: buffer containing the loaded image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001538 *
Mario Six78a88f72018-07-10 08:40:17 +02001539 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001540 */
Rob Clark9975fe92017-09-13 18:05:38 -04001541efi_status_t efi_load_image_from_path(struct efi_device_path *file_path,
1542 void **buffer)
Rob Clark838ee4b2017-09-13 18:05:35 -04001543{
1544 struct efi_file_info *info = NULL;
1545 struct efi_file_handle *f;
1546 static efi_status_t ret;
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001547 efi_uintn_t bs;
Rob Clark838ee4b2017-09-13 18:05:35 -04001548
1549 f = efi_file_from_path(file_path);
1550 if (!f)
1551 return EFI_DEVICE_ERROR;
1552
1553 bs = 0;
1554 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1555 &bs, info));
1556 if (ret == EFI_BUFFER_TOO_SMALL) {
1557 info = malloc(bs);
1558 EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid,
1559 &bs, info));
1560 }
1561 if (ret != EFI_SUCCESS)
1562 goto error;
1563
1564 ret = efi_allocate_pool(EFI_LOADER_DATA, info->file_size, buffer);
1565 if (ret)
1566 goto error;
1567
Heinrich Schuchardtb6dd5772018-04-03 22:37:11 +02001568 bs = info->file_size;
1569 EFI_CALL(ret = f->read(f, &bs, *buffer));
Rob Clark838ee4b2017-09-13 18:05:35 -04001570
1571error:
1572 free(info);
1573 EFI_CALL(f->close(f));
1574
1575 if (ret != EFI_SUCCESS) {
1576 efi_free_pool(*buffer);
1577 *buffer = NULL;
1578 }
1579
1580 return ret;
1581}
1582
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001583/**
Mario Six78a88f72018-07-10 08:40:17 +02001584 * efi_load_image() - load an EFI image into memory
1585 * @boot_policy: true for request originating from the boot manager
1586 * @parent_image: the caller's image handle
1587 * @file_path: the path of the image to load
1588 * @source_buffer: memory location from which the image is installed
1589 * @source_size: size of the memory area from which the image is installed
1590 * @image_handle: handle for the newly installed image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001591 *
1592 * This function implements the LoadImage service.
Mario Six78a88f72018-07-10 08:40:17 +02001593 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001594 * See the Unified Extensible Firmware Interface (UEFI) specification
1595 * for details.
1596 *
Mario Six78a88f72018-07-10 08:40:17 +02001597 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001598 */
Alexander Grafbee91162016-03-04 01:09:59 +01001599static efi_status_t EFIAPI efi_load_image(bool boot_policy,
1600 efi_handle_t parent_image,
1601 struct efi_device_path *file_path,
1602 void *source_buffer,
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001603 efi_uintn_t source_size,
Alexander Grafbee91162016-03-04 01:09:59 +01001604 efi_handle_t *image_handle)
1605{
Alexander Grafbee91162016-03-04 01:09:59 +01001606 struct efi_loaded_image *info;
1607 struct efi_object *obj;
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001608 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001609
Heinrich Schuchardt7fb96a12018-04-03 22:29:30 +02001610 EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image,
Alexander Grafbee91162016-03-04 01:09:59 +01001611 file_path, source_buffer, source_size, image_handle);
Rob Clark838ee4b2017-09-13 18:05:35 -04001612
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001613 if (!image_handle || !parent_image) {
1614 ret = EFI_INVALID_PARAMETER;
1615 goto error;
1616 }
1617
1618 if (!source_buffer && !file_path) {
1619 ret = EFI_NOT_FOUND;
1620 goto error;
1621 }
1622
Rob Clark838ee4b2017-09-13 18:05:35 -04001623 info = calloc(1, sizeof(*info));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001624 if (!info) {
1625 ret = EFI_OUT_OF_RESOURCES;
1626 goto error;
1627 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001628 obj = calloc(1, sizeof(*obj));
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001629 if (!obj) {
1630 free(info);
1631 ret = EFI_OUT_OF_RESOURCES;
1632 goto error;
1633 }
Rob Clark838ee4b2017-09-13 18:05:35 -04001634
1635 if (!source_buffer) {
1636 struct efi_device_path *dp, *fp;
Rob Clark838ee4b2017-09-13 18:05:35 -04001637
Rob Clark9975fe92017-09-13 18:05:38 -04001638 ret = efi_load_image_from_path(file_path, &source_buffer);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001639 if (ret != EFI_SUCCESS)
1640 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001641 /*
1642 * split file_path which contains both the device and
1643 * file parts:
1644 */
1645 efi_dp_split_file_path(file_path, &dp, &fp);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001646 ret = efi_setup_loaded_image(info, obj, dp, fp);
1647 if (ret != EFI_SUCCESS)
1648 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001649 } else {
1650 /* In this case, file_path is the "device" path, ie.
1651 * something like a HARDWARE_DEVICE:MEMORY_MAPPED
1652 */
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001653 ret = efi_setup_loaded_image(info, obj, file_path, NULL);
1654 if (ret != EFI_SUCCESS)
1655 goto failure;
Rob Clark838ee4b2017-09-13 18:05:35 -04001656 }
Alexander Grafbee91162016-03-04 01:09:59 +01001657 info->reserved = efi_load_pe(source_buffer, info);
1658 if (!info->reserved) {
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001659 ret = EFI_UNSUPPORTED;
1660 goto failure;
Alexander Grafbee91162016-03-04 01:09:59 +01001661 }
Heinrich Schuchardt32fc2ac2017-10-18 18:13:20 +02001662 info->system_table = &systab;
1663 info->parent_handle = parent_image;
Heinrich Schuchardtea54ad52017-11-26 14:05:22 +01001664 *image_handle = obj->handle;
Alexander Grafbee91162016-03-04 01:09:59 +01001665 return EFI_EXIT(EFI_SUCCESS);
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001666failure:
1667 free(info);
1668 efi_delete_handle(obj);
Heinrich Schuchardt28a4fd42018-03-07 02:40:51 +01001669error:
Heinrich Schuchardtb9b17592017-12-04 18:03:03 +01001670 return EFI_EXIT(ret);
Alexander Grafbee91162016-03-04 01:09:59 +01001671}
1672
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001673/**
Mario Six78a88f72018-07-10 08:40:17 +02001674 * efi_start_image() - dall the entry point of an image
1675 * @image_handle: handle of the image
1676 * @exit_data_size: size of the buffer
1677 * @exit_data: buffer to receive the exit data of the called image
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001678 *
1679 * This function implements the StartImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001680 *
Mario Six78a88f72018-07-10 08:40:17 +02001681 * See the Unified Extensible Firmware Interface (UEFI) specification for
1682 * details.
1683 *
1684 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001685 */
Alexander Grafbee91162016-03-04 01:09:59 +01001686static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
1687 unsigned long *exit_data_size,
1688 s16 **exit_data)
1689{
Alexander Grafc6fa5df2018-01-24 00:18:08 +01001690 EFIAPI efi_status_t (*entry)(efi_handle_t image_handle,
1691 struct efi_system_table *st);
Alexander Grafbee91162016-03-04 01:09:59 +01001692 struct efi_loaded_image *info = image_handle;
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001693 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01001694
1695 EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
1696 entry = info->reserved;
1697
1698 efi_is_direct_boot = false;
1699
1700 /* call the image! */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001701 if (setjmp(&info->exit_jmp)) {
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001702 /*
1703 * We called the entry point of the child image with EFI_CALL
1704 * in the lines below. The child image called the Exit() boot
1705 * service efi_exit() which executed the long jump that brought
1706 * us to the current line. This implies that the second half
1707 * of the EFI_CALL macro has not been executed.
1708 */
1709#ifdef CONFIG_ARM
1710 /*
1711 * efi_exit() called efi_restore_gd(). We have to undo this
1712 * otherwise __efi_entry_check() will put the wrong value into
1713 * app_gd.
1714 */
1715 gd = app_gd;
1716#endif
1717 /*
1718 * To get ready to call EFI_EXIT below we have to execute the
1719 * missed out steps of EFI_CALL.
1720 */
1721 assert(__efi_entry_check());
1722 debug("%sEFI: %lu returned by started image\n",
1723 __efi_nesting_dec(),
1724 (unsigned long)((uintptr_t)info->exit_status &
1725 ~EFI_ERROR_MASK));
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001726 return EFI_EXIT(info->exit_status);
1727 }
1728
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001729 ret = EFI_CALL(entry(image_handle, &systab));
Alexander Grafbee91162016-03-04 01:09:59 +01001730
Alexander Graf56672bf2018-01-26 00:47:53 +01001731 /*
1732 * Usually UEFI applications call Exit() instead of returning.
1733 * But because the world doesn not consist of ponies and unicorns,
1734 * we're happy to emulate that behavior on behalf of a payload
1735 * that forgot.
1736 */
1737 return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL));
Alexander Grafbee91162016-03-04 01:09:59 +01001738}
1739
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001740/**
Mario Six78a88f72018-07-10 08:40:17 +02001741 * efi_exit() - leave an EFI application or driver
1742 * @image_handle: handle of the application or driver that is exiting
1743 * @exit_status: status code
1744 * @exit_data_size: size of the buffer in bytes
1745 * @exit_data: buffer with data describing an error
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001746 *
1747 * This function implements the Exit service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001748 *
Mario Six78a88f72018-07-10 08:40:17 +02001749 * See the Unified Extensible Firmware Interface (UEFI) specification for
1750 * details.
1751 *
1752 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001753 */
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001754static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001755 efi_status_t exit_status,
1756 unsigned long exit_data_size,
1757 int16_t *exit_data)
Alexander Grafbee91162016-03-04 01:09:59 +01001758{
Heinrich Schuchardt44549d62017-11-26 14:05:23 +01001759 /*
1760 * We require that the handle points to the original loaded
1761 * image protocol interface.
1762 *
1763 * For getting the longjmp address this is safer than locating
1764 * the protocol because the protocol may have been reinstalled
1765 * pointing to another memory location.
1766 *
1767 * TODO: We should call the unload procedure of the loaded
1768 * image protocol.
1769 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001770 struct efi_loaded_image *loaded_image_info = (void *)image_handle;
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001771
Alexander Grafbee91162016-03-04 01:09:59 +01001772 EFI_ENTRY("%p, %ld, %ld, %p", image_handle, exit_status,
1773 exit_data_size, exit_data);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001774
Alexander Grafa1489202017-09-03 14:14:17 +02001775 /* Make sure entry/exit counts for EFI world cross-overs match */
Heinrich Schuchardt727a1af2018-01-18 20:28:43 +01001776 EFI_EXIT(exit_status);
Heinrich Schuchardtda940732017-08-25 19:53:14 +02001777
Alexander Grafa1489202017-09-03 14:14:17 +02001778 /*
1779 * But longjmp out with the U-Boot gd, not the application's, as
1780 * the other end is a setjmp call inside EFI context.
1781 */
1782 efi_restore_gd();
1783
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001784 loaded_image_info->exit_status = exit_status;
Alexander Graf692fcdd2016-09-27 09:30:32 +02001785 longjmp(&loaded_image_info->exit_jmp, 1);
Alexander Grafa86aeaf2016-05-20 23:28:23 +02001786
1787 panic("EFI application exited");
Alexander Grafbee91162016-03-04 01:09:59 +01001788}
1789
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001790/**
Mario Six78a88f72018-07-10 08:40:17 +02001791 * efi_unload_image() - unload an EFI image
1792 * @image_handle: handle of the image to be unloaded
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001793 *
1794 * This function implements the UnloadImage service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001795 *
Mario Six78a88f72018-07-10 08:40:17 +02001796 * See the Unified Extensible Firmware Interface (UEFI) specification for
1797 * details.
1798 *
1799 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001800 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001801static efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001802{
1803 struct efi_object *efiobj;
1804
1805 EFI_ENTRY("%p", image_handle);
1806 efiobj = efi_search_obj(image_handle);
1807 if (efiobj)
1808 list_del(&efiobj->link);
1809
1810 return EFI_EXIT(EFI_SUCCESS);
1811}
1812
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001813/**
Mario Six78a88f72018-07-10 08:40:17 +02001814 * efi_exit_caches() - fix up caches for EFI payloads if necessary
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001815 */
Alexander Grafbee91162016-03-04 01:09:59 +01001816static void efi_exit_caches(void)
1817{
1818#if defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
1819 /*
1820 * Grub on 32bit ARM needs to have caches disabled before jumping into
1821 * a zImage, but does not know of all cache layers. Give it a hand.
1822 */
1823 if (efi_is_direct_boot)
1824 cleanup_before_linux();
1825#endif
1826}
1827
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001828/**
Mario Six78a88f72018-07-10 08:40:17 +02001829 * efi_exit_boot_services() - stop all boot services
1830 * @image_handle: handle of the loaded image
1831 * @map_key: key of the memory map
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001832 *
1833 * This function implements the ExitBootServices service.
Mario Six78a88f72018-07-10 08:40:17 +02001834 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001835 * See the Unified Extensible Firmware Interface (UEFI) specification
1836 * for details.
1837 *
Mario Six78a88f72018-07-10 08:40:17 +02001838 * All timer events are disabled. For exit boot services events the
1839 * notification function is called. The boot services are disabled in the
1840 * system table.
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001841 *
Mario Six78a88f72018-07-10 08:40:17 +02001842 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001843 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001844static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001845 unsigned long map_key)
1846{
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001847 struct efi_event *evt;
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001848
Alexander Grafbee91162016-03-04 01:09:59 +01001849 EFI_ENTRY("%p, %ld", image_handle, map_key);
1850
Heinrich Schuchardt1fcb7ea2018-07-02 12:53:55 +02001851 /* Check that the caller has read the current memory map */
1852 if (map_key != efi_memory_map_key)
1853 return EFI_INVALID_PARAMETER;
1854
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001855 /* Make sure that notification functions are not called anymore */
1856 efi_tpl = TPL_HIGH_LEVEL;
1857
1858 /* Check if ExitBootServices has already been called */
1859 if (!systab.boottime)
1860 return EFI_EXIT(EFI_SUCCESS);
1861
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001862 /* Add related events to the event group */
1863 list_for_each_entry(evt, &efi_events, link) {
1864 if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES)
1865 evt->group = &efi_guid_event_group_exit_boot_services;
1866 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001867 /* Notify that ExitBootServices is invoked. */
Heinrich Schuchardt43bce442018-02-18 15:17:50 +01001868 list_for_each_entry(evt, &efi_events, link) {
Heinrich Schuchardtb095f3c2018-02-18 15:17:52 +01001869 if (evt->group &&
1870 !guidcmp(evt->group,
1871 &efi_guid_event_group_exit_boot_services)) {
1872 efi_signal_event(evt, false);
1873 break;
1874 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001875 }
Heinrich Schuchardt152a2632017-09-15 10:06:18 +02001876
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001877 /* TODO Should persist EFI variables here */
Rob Clarkad644e72017-09-13 18:05:37 -04001878
Alexander Grafb7b84102016-11-17 01:02:57 +01001879 board_quiesce_devices();
1880
Alexander Grafbee91162016-03-04 01:09:59 +01001881 /* Fix up caches for EFI payloads if necessary */
1882 efi_exit_caches();
1883
1884 /* This stops all lingering devices */
1885 bootm_disable_interrupts();
1886
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001887 /* Disable boottime services */
1888 systab.con_in_handle = NULL;
1889 systab.con_in = NULL;
1890 systab.con_out_handle = NULL;
1891 systab.con_out = NULL;
1892 systab.stderr_handle = NULL;
1893 systab.std_err = NULL;
1894 systab.boottime = NULL;
1895
1896 /* Recalculate CRC32 */
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02001897 efi_update_table_header_crc32(&systab.hdr);
Heinrich Schuchardtcc20ed02018-01-19 20:24:52 +01001898
Alexander Grafbee91162016-03-04 01:09:59 +01001899 /* Give the payload some time to boot */
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001900 efi_set_watchdog(0);
Alexander Grafbee91162016-03-04 01:09:59 +01001901 WATCHDOG_RESET();
1902
1903 return EFI_EXIT(EFI_SUCCESS);
1904}
1905
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001906/**
Mario Six78a88f72018-07-10 08:40:17 +02001907 * efi_get_next_monotonic_count() - get next value of the counter
1908 * @count: returned value of the counter
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001909 *
1910 * This function implements the NextMonotonicCount service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001911 *
Mario Six78a88f72018-07-10 08:40:17 +02001912 * See the Unified Extensible Firmware Interface (UEFI) specification for
1913 * details.
1914 *
1915 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001916 */
Alexander Grafbee91162016-03-04 01:09:59 +01001917static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count)
1918{
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01001919 static uint64_t mono;
1920
Alexander Grafbee91162016-03-04 01:09:59 +01001921 EFI_ENTRY("%p", count);
1922 *count = mono++;
1923 return EFI_EXIT(EFI_SUCCESS);
1924}
1925
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001926/**
Mario Six78a88f72018-07-10 08:40:17 +02001927 * efi_stall() - sleep
1928 * @microseconds: period to sleep in microseconds
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001929 *
Mario Six78a88f72018-07-10 08:40:17 +02001930 * This function implements the Stall service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001931 *
Mario Six78a88f72018-07-10 08:40:17 +02001932 * See the Unified Extensible Firmware Interface (UEFI) specification for
1933 * details.
1934 *
1935 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001936 */
Alexander Grafbee91162016-03-04 01:09:59 +01001937static efi_status_t EFIAPI efi_stall(unsigned long microseconds)
1938{
1939 EFI_ENTRY("%ld", microseconds);
1940 udelay(microseconds);
1941 return EFI_EXIT(EFI_SUCCESS);
1942}
1943
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001944/**
Mario Six78a88f72018-07-10 08:40:17 +02001945 * efi_set_watchdog_timer() - reset the watchdog timer
1946 * @timeout: seconds before reset by watchdog
1947 * @watchdog_code: code to be logged when resetting
1948 * @data_size: size of buffer in bytes
1949 * @watchdog_data: buffer with data describing the reset reason
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001950 *
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001951 * This function implements the SetWatchdogTimer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001952 *
Mario Six78a88f72018-07-10 08:40:17 +02001953 * See the Unified Extensible Firmware Interface (UEFI) specification for
1954 * details.
1955 *
1956 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001957 */
Alexander Grafbee91162016-03-04 01:09:59 +01001958static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout,
1959 uint64_t watchdog_code,
1960 unsigned long data_size,
1961 uint16_t *watchdog_data)
1962{
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001963 EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code,
Alexander Grafbee91162016-03-04 01:09:59 +01001964 data_size, watchdog_data);
Heinrich Schuchardtb3d60902017-10-18 18:13:04 +02001965 return EFI_EXIT(efi_set_watchdog(timeout));
Alexander Grafbee91162016-03-04 01:09:59 +01001966}
1967
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02001968/**
Mario Six78a88f72018-07-10 08:40:17 +02001969 * efi_close_protocol() - close a protocol
1970 * @handle: handle on which the protocol shall be closed
1971 * @protocol: GUID of the protocol to close
1972 * @agent_handle: handle of the driver
1973 * @controller_handle: handle of the controller
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001974 *
1975 * This function implements the CloseProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001976 *
Mario Six78a88f72018-07-10 08:40:17 +02001977 * See the Unified Extensible Firmware Interface (UEFI) specification for
1978 * details.
1979 *
1980 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02001981 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001982static efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02001983 const efi_guid_t *protocol,
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01001984 efi_handle_t agent_handle,
1985 efi_handle_t controller_handle)
Alexander Grafbee91162016-03-04 01:09:59 +01001986{
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001987 struct efi_handler *handler;
1988 struct efi_open_protocol_info_item *item;
1989 struct efi_open_protocol_info_item *pos;
1990 efi_status_t r;
1991
Rob Clark778e6af2017-09-13 18:05:41 -04001992 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle,
Alexander Grafbee91162016-03-04 01:09:59 +01001993 controller_handle);
Heinrich Schuchardt3b8a4892018-01-11 08:15:59 +01001994
1995 if (!agent_handle) {
1996 r = EFI_INVALID_PARAMETER;
1997 goto out;
1998 }
1999 r = efi_search_protocol(handle, protocol, &handler);
2000 if (r != EFI_SUCCESS)
2001 goto out;
2002
2003 r = EFI_NOT_FOUND;
2004 list_for_each_entry_safe(item, pos, &handler->open_infos, link) {
2005 if (item->info.agent_handle == agent_handle &&
2006 item->info.controller_handle == controller_handle) {
2007 efi_delete_open_info(item);
2008 r = EFI_SUCCESS;
2009 break;
2010 }
2011 }
2012out:
2013 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002014}
2015
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002016/**
Mario Six78a88f72018-07-10 08:40:17 +02002017 * efi_open_protocol_information() - provide information about then open status
2018 * of a protocol on a handle
2019 * @handle: handle for which the information shall be retrieved
2020 * @protocol: GUID of the protocol
2021 * @entry_buffer: buffer to receive the open protocol information
2022 * @entry_count: number of entries available in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002023 *
2024 * This function implements the OpenProtocolInformation service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002025 *
Mario Six78a88f72018-07-10 08:40:17 +02002026 * See the Unified Extensible Firmware Interface (UEFI) specification for
2027 * details.
2028 *
2029 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002030 */
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01002031static efi_status_t EFIAPI efi_open_protocol_information(
2032 efi_handle_t handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002033 struct efi_open_protocol_info_entry **entry_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002034 efi_uintn_t *entry_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002035{
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002036 unsigned long buffer_size;
2037 unsigned long count;
2038 struct efi_handler *handler;
2039 struct efi_open_protocol_info_item *item;
2040 efi_status_t r;
2041
Rob Clark778e6af2017-09-13 18:05:41 -04002042 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer,
Alexander Grafbee91162016-03-04 01:09:59 +01002043 entry_count);
Heinrich Schuchardte3fbbc32018-01-11 08:16:00 +01002044
2045 /* Check parameters */
2046 if (!entry_buffer) {
2047 r = EFI_INVALID_PARAMETER;
2048 goto out;
2049 }
2050 r = efi_search_protocol(handle, protocol, &handler);
2051 if (r != EFI_SUCCESS)
2052 goto out;
2053
2054 /* Count entries */
2055 count = 0;
2056 list_for_each_entry(item, &handler->open_infos, link) {
2057 if (item->info.open_count)
2058 ++count;
2059 }
2060 *entry_count = count;
2061 *entry_buffer = NULL;
2062 if (!count) {
2063 r = EFI_SUCCESS;
2064 goto out;
2065 }
2066
2067 /* Copy entries */
2068 buffer_size = count * sizeof(struct efi_open_protocol_info_entry);
2069 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2070 (void **)entry_buffer);
2071 if (r != EFI_SUCCESS)
2072 goto out;
2073 list_for_each_entry_reverse(item, &handler->open_infos, link) {
2074 if (item->info.open_count)
2075 (*entry_buffer)[--count] = item->info;
2076 }
2077out:
2078 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002079}
2080
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002081/**
Mario Six78a88f72018-07-10 08:40:17 +02002082 * efi_protocols_per_handle() - get protocols installed on a handle
2083 * @handle: handle for which the information is retrieved
2084 * @protocol_buffer: buffer with protocol GUIDs
2085 * @protocol_buffer_count: number of entries in the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002086 *
2087 * This function implements the ProtocolsPerHandleService.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002088 *
Mario Six78a88f72018-07-10 08:40:17 +02002089 * See the Unified Extensible Firmware Interface (UEFI) specification for
2090 * details.
2091 *
2092 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002093 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002094static efi_status_t EFIAPI efi_protocols_per_handle(
2095 efi_handle_t handle, efi_guid_t ***protocol_buffer,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002096 efi_uintn_t *protocol_buffer_count)
Alexander Grafbee91162016-03-04 01:09:59 +01002097{
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002098 unsigned long buffer_size;
2099 struct efi_object *efiobj;
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002100 struct list_head *protocol_handle;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002101 efi_status_t r;
2102
Alexander Grafbee91162016-03-04 01:09:59 +01002103 EFI_ENTRY("%p, %p, %p", handle, protocol_buffer,
2104 protocol_buffer_count);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002105
2106 if (!handle || !protocol_buffer || !protocol_buffer_count)
2107 return EFI_EXIT(EFI_INVALID_PARAMETER);
2108
2109 *protocol_buffer = NULL;
Rob Clark661c8322017-07-20 07:59:39 -04002110 *protocol_buffer_count = 0;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002111
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002112 efiobj = efi_search_obj(handle);
2113 if (!efiobj)
2114 return EFI_EXIT(EFI_INVALID_PARAMETER);
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002115
Heinrich Schuchardt69fb6b12017-11-26 14:05:17 +01002116 /* Count protocols */
2117 list_for_each(protocol_handle, &efiobj->protocols) {
2118 ++*protocol_buffer_count;
2119 }
2120
2121 /* Copy guids */
2122 if (*protocol_buffer_count) {
2123 size_t j = 0;
2124
2125 buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count;
2126 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2127 (void **)protocol_buffer);
2128 if (r != EFI_SUCCESS)
2129 return EFI_EXIT(r);
2130 list_for_each(protocol_handle, &efiobj->protocols) {
2131 struct efi_handler *protocol;
2132
2133 protocol = list_entry(protocol_handle,
2134 struct efi_handler, link);
2135 (*protocol_buffer)[j] = (void *)protocol->guid;
2136 ++j;
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002137 }
xypron.glpk@gmx.dec0ebfc82017-07-13 23:24:32 +02002138 }
2139
2140 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002141}
2142
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002143/**
Mario Six78a88f72018-07-10 08:40:17 +02002144 * efi_locate_handle_buffer() - locate handles implementing a protocol
2145 * @search_type: selection criterion
2146 * @protocol: GUID of the protocol
2147 * @search_key: registration key
2148 * @no_handles: number of returned handles
2149 * @buffer: buffer with the returned handles
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002150 *
2151 * This function implements the LocateHandleBuffer service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002152 *
Mario Six78a88f72018-07-10 08:40:17 +02002153 * See the Unified Extensible Firmware Interface (UEFI) specification for
2154 * details.
2155 *
2156 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002157 */
Alexander Grafbee91162016-03-04 01:09:59 +01002158static efi_status_t EFIAPI efi_locate_handle_buffer(
2159 enum efi_locate_search_type search_type,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002160 const efi_guid_t *protocol, void *search_key,
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002161 efi_uintn_t *no_handles, efi_handle_t **buffer)
Alexander Grafbee91162016-03-04 01:09:59 +01002162{
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002163 efi_status_t r;
Heinrich Schuchardtf5a2a932017-11-06 21:17:48 +01002164 efi_uintn_t buffer_size = 0;
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002165
Rob Clark778e6af2017-09-13 18:05:41 -04002166 EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key,
Alexander Grafbee91162016-03-04 01:09:59 +01002167 no_handles, buffer);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002168
2169 if (!no_handles || !buffer) {
2170 r = EFI_INVALID_PARAMETER;
2171 goto out;
2172 }
2173 *no_handles = 0;
2174 *buffer = NULL;
2175 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2176 *buffer);
2177 if (r != EFI_BUFFER_TOO_SMALL)
2178 goto out;
2179 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, buffer_size,
2180 (void **)buffer);
2181 if (r != EFI_SUCCESS)
2182 goto out;
2183 r = efi_locate_handle(search_type, protocol, search_key, &buffer_size,
2184 *buffer);
2185 if (r == EFI_SUCCESS)
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002186 *no_handles = buffer_size / sizeof(efi_handle_t);
xypron.glpk@gmx.dec2e703f2017-07-11 22:06:22 +02002187out:
2188 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002189}
2190
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002191/**
Mario Six78a88f72018-07-10 08:40:17 +02002192 * efi_locate_protocol() - find an interface implementing a protocol
2193 * @protocol: GUID of the protocol
2194 * @registration: registration key passed to the notification function
2195 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002196 *
2197 * This function implements the LocateProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002198 *
Mario Six78a88f72018-07-10 08:40:17 +02002199 * See the Unified Extensible Firmware Interface (UEFI) specification for
2200 * details.
2201 *
2202 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002203 */
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002204static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002205 void *registration,
2206 void **protocol_interface)
2207{
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002208 struct list_head *lhandle;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002209 efi_status_t ret;
Alexander Grafbee91162016-03-04 01:09:59 +01002210
Rob Clark778e6af2017-09-13 18:05:41 -04002211 EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002212
2213 if (!protocol || !protocol_interface)
2214 return EFI_EXIT(EFI_INVALID_PARAMETER);
2215
2216 list_for_each(lhandle, &efi_obj_list) {
2217 struct efi_object *efiobj;
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002218 struct efi_handler *handler;
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002219
2220 efiobj = list_entry(lhandle, struct efi_object, link);
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002221
Heinrich Schuchardt9172cd92017-10-26 19:25:57 +02002222 ret = efi_search_protocol(efiobj->handle, protocol, &handler);
2223 if (ret == EFI_SUCCESS) {
2224 *protocol_interface = handler->protocol_interface;
2225 return EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002226 }
2227 }
xypron.glpk@gmx.de88adae52017-07-11 22:06:24 +02002228 *protocol_interface = NULL;
Alexander Grafbee91162016-03-04 01:09:59 +01002229
2230 return EFI_EXIT(EFI_NOT_FOUND);
2231}
2232
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002233/**
Mario Six78a88f72018-07-10 08:40:17 +02002234 * efi_locate_device_path() - Get the device path and handle of an device
2235 * implementing a protocol
2236 * @protocol: GUID of the protocol
2237 * @device_path: device path
2238 * @device: handle of the device
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002239 *
2240 * This function implements the LocateDevicePath service.
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002241 *
Mario Six78a88f72018-07-10 08:40:17 +02002242 * See the Unified Extensible Firmware Interface (UEFI) specification for
2243 * details.
2244 *
2245 * Return: status code
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002246 */
2247static efi_status_t EFIAPI efi_locate_device_path(
2248 const efi_guid_t *protocol,
2249 struct efi_device_path **device_path,
2250 efi_handle_t *device)
2251{
2252 struct efi_device_path *dp;
2253 size_t i;
2254 struct efi_handler *handler;
2255 efi_handle_t *handles;
2256 size_t len, len_dp;
2257 size_t len_best = 0;
2258 efi_uintn_t no_handles;
2259 u8 *remainder;
2260 efi_status_t ret;
2261
2262 EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device);
2263
2264 if (!protocol || !device_path || !*device_path || !device) {
2265 ret = EFI_INVALID_PARAMETER;
2266 goto out;
2267 }
2268
2269 /* Find end of device path */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002270 len = efi_dp_instance_size(*device_path);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002271
2272 /* Get all handles implementing the protocol */
2273 ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL,
2274 &no_handles, &handles));
2275 if (ret != EFI_SUCCESS)
2276 goto out;
2277
2278 for (i = 0; i < no_handles; ++i) {
2279 /* Find the device path protocol */
2280 ret = efi_search_protocol(handles[i], &efi_guid_device_path,
2281 &handler);
2282 if (ret != EFI_SUCCESS)
2283 continue;
2284 dp = (struct efi_device_path *)handler->protocol_interface;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +02002285 len_dp = efi_dp_instance_size(dp);
Heinrich Schuchardtae2c85c2017-11-26 14:05:10 +01002286 /*
2287 * This handle can only be a better fit
2288 * if its device path length is longer than the best fit and
2289 * if its device path length is shorter of equal the searched
2290 * device path.
2291 */
2292 if (len_dp <= len_best || len_dp > len)
2293 continue;
2294 /* Check if dp is a subpath of device_path */
2295 if (memcmp(*device_path, dp, len_dp))
2296 continue;
2297 *device = handles[i];
2298 len_best = len_dp;
2299 }
2300 if (len_best) {
2301 remainder = (u8 *)*device_path + len_best;
2302 *device_path = (struct efi_device_path *)remainder;
2303 ret = EFI_SUCCESS;
2304 } else {
2305 ret = EFI_NOT_FOUND;
2306 }
2307out:
2308 return EFI_EXIT(ret);
2309}
2310
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002311/**
Mario Six78a88f72018-07-10 08:40:17 +02002312 * efi_install_multiple_protocol_interfaces() - Install multiple protocol
2313 * interfaces
2314 * @handle: handle on which the protocol interfaces shall be installed
2315 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2316 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002317 *
2318 * This function implements the MultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002319 *
Mario Six78a88f72018-07-10 08:40:17 +02002320 * See the Unified Extensible Firmware Interface (UEFI) specification for
2321 * details.
2322 *
2323 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002324 */
Alexander Grafbee91162016-03-04 01:09:59 +01002325static efi_status_t EFIAPI efi_install_multiple_protocol_interfaces(
2326 void **handle, ...)
2327{
2328 EFI_ENTRY("%p", handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002329
Alexander Grafbeb077a2018-06-18 17:23:05 +02002330 efi_va_list argptr;
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002331 const efi_guid_t *protocol;
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002332 void *protocol_interface;
2333 efi_status_t r = EFI_SUCCESS;
2334 int i = 0;
2335
2336 if (!handle)
2337 return EFI_EXIT(EFI_INVALID_PARAMETER);
2338
Alexander Grafbeb077a2018-06-18 17:23:05 +02002339 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002340 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002341 protocol = efi_va_arg(argptr, efi_guid_t*);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002342 if (!protocol)
2343 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002344 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01002345 r = EFI_CALL(efi_install_protocol_interface(
2346 handle, protocol,
2347 EFI_NATIVE_INTERFACE,
2348 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002349 if (r != EFI_SUCCESS)
2350 break;
2351 i++;
2352 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002353 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002354 if (r == EFI_SUCCESS)
2355 return EFI_EXIT(r);
2356
Heinrich Schuchardt62471e42017-10-26 19:25:42 +02002357 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002358 efi_va_start(argptr, handle);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002359 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002360 protocol = efi_va_arg(argptr, efi_guid_t*);
2361 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01002362 EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2363 protocol_interface));
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002364 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002365 efi_va_end(argptr);
xypron.glpk@gmx.de58b83582017-07-11 22:06:20 +02002366
2367 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002368}
2369
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002370/**
Mario Six78a88f72018-07-10 08:40:17 +02002371 * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol
2372 * interfaces
2373 * @handle: handle from which the protocol interfaces shall be removed
2374 * @...: NULL terminated argument list with pairs of protocol GUIDS and
2375 * interfaces
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002376 *
2377 * This function implements the UninstallMultipleProtocolInterfaces service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002378 *
Mario Six78a88f72018-07-10 08:40:17 +02002379 * See the Unified Extensible Firmware Interface (UEFI) specification for
2380 * details.
2381 *
2382 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002383 */
Alexander Grafbee91162016-03-04 01:09:59 +01002384static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces(
2385 void *handle, ...)
2386{
2387 EFI_ENTRY("%p", handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002388
Alexander Grafbeb077a2018-06-18 17:23:05 +02002389 efi_va_list argptr;
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002390 const efi_guid_t *protocol;
2391 void *protocol_interface;
2392 efi_status_t r = EFI_SUCCESS;
2393 size_t i = 0;
2394
2395 if (!handle)
2396 return EFI_EXIT(EFI_INVALID_PARAMETER);
2397
Alexander Grafbeb077a2018-06-18 17:23:05 +02002398 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002399 for (;;) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002400 protocol = efi_va_arg(argptr, efi_guid_t*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002401 if (!protocol)
2402 break;
Alexander Grafbeb077a2018-06-18 17:23:05 +02002403 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002404 r = EFI_CALL(efi_uninstall_protocol_interface(
2405 handle, protocol,
2406 protocol_interface));
2407 if (r != EFI_SUCCESS)
2408 break;
2409 i++;
2410 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002411 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002412 if (r == EFI_SUCCESS)
2413 return EFI_EXIT(r);
2414
2415 /* If an error occurred undo all changes. */
Alexander Grafbeb077a2018-06-18 17:23:05 +02002416 efi_va_start(argptr, handle);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002417 for (; i; --i) {
Alexander Grafbeb077a2018-06-18 17:23:05 +02002418 protocol = efi_va_arg(argptr, efi_guid_t*);
2419 protocol_interface = efi_va_arg(argptr, void*);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002420 EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2421 EFI_NATIVE_INTERFACE,
2422 protocol_interface));
2423 }
Alexander Grafbeb077a2018-06-18 17:23:05 +02002424 efi_va_end(argptr);
Heinrich Schuchardt843ce542017-10-26 19:25:44 +02002425
2426 return EFI_EXIT(r);
Alexander Grafbee91162016-03-04 01:09:59 +01002427}
2428
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002429/**
Mario Six78a88f72018-07-10 08:40:17 +02002430 * efi_calculate_crc32() - calculate cyclic redundancy code
2431 * @data: buffer with data
2432 * @data_size: size of buffer in bytes
2433 * @crc32_p: cyclic redundancy code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002434 *
2435 * This function implements the CalculateCrc32 service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002436 *
Mario Six78a88f72018-07-10 08:40:17 +02002437 * See the Unified Extensible Firmware Interface (UEFI) specification for
2438 * details.
2439 *
2440 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002441 */
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002442static efi_status_t EFIAPI efi_calculate_crc32(const void *data,
2443 efi_uintn_t data_size,
2444 u32 *crc32_p)
Alexander Grafbee91162016-03-04 01:09:59 +01002445{
Heinrich Schuchardt8aa83602018-07-07 15:36:04 +02002446 EFI_ENTRY("%p, %zu", data, data_size);
Alexander Grafbee91162016-03-04 01:09:59 +01002447 *crc32_p = crc32(0, data, data_size);
2448 return EFI_EXIT(EFI_SUCCESS);
2449}
2450
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002451/**
Mario Six78a88f72018-07-10 08:40:17 +02002452 * efi_copy_mem() - copy memory
2453 * @destination: destination of the copy operation
2454 * @source: source of the copy operation
2455 * @length: number of bytes to copy
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002456 *
2457 * This function implements the CopyMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002458 *
Mario Six78a88f72018-07-10 08:40:17 +02002459 * See the Unified Extensible Firmware Interface (UEFI) specification for
2460 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002461 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002462static void EFIAPI efi_copy_mem(void *destination, const void *source,
2463 size_t length)
Alexander Grafbee91162016-03-04 01:09:59 +01002464{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002465 EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length);
Alexander Grafbee91162016-03-04 01:09:59 +01002466 memcpy(destination, source, length);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002467 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002468}
2469
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002470/**
Mario Six78a88f72018-07-10 08:40:17 +02002471 * efi_set_mem() - Fill memory with a byte value.
2472 * @buffer: buffer to fill
2473 * @size: size of buffer in bytes
2474 * @value: byte to copy to the buffer
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002475 *
2476 * This function implements the SetMem service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002477 *
Mario Six78a88f72018-07-10 08:40:17 +02002478 * See the Unified Extensible Firmware Interface (UEFI) specification for
2479 * details.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002480 */
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002481static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value)
Alexander Grafbee91162016-03-04 01:09:59 +01002482{
Heinrich Schuchardtfc05a952017-10-05 16:35:52 +02002483 EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value);
Alexander Grafbee91162016-03-04 01:09:59 +01002484 memset(buffer, value, size);
Heinrich Schuchardtf7c78172017-10-05 16:35:51 +02002485 EFI_EXIT(EFI_SUCCESS);
Alexander Grafbee91162016-03-04 01:09:59 +01002486}
2487
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002488/**
Mario Six78a88f72018-07-10 08:40:17 +02002489 * efi_protocol_open() - open protocol interface on a handle
2490 * @handler: handler of a protocol
2491 * @protocol_interface: interface implementing the protocol
2492 * @agent_handle: handle of the driver
2493 * @controller_handle: handle of the controller
2494 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002495 *
Mario Six78a88f72018-07-10 08:40:17 +02002496 * Return: status code
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002497 */
2498static efi_status_t efi_protocol_open(
2499 struct efi_handler *handler,
2500 void **protocol_interface, void *agent_handle,
2501 void *controller_handle, uint32_t attributes)
2502{
2503 struct efi_open_protocol_info_item *item;
2504 struct efi_open_protocol_info_entry *match = NULL;
2505 bool opened_by_driver = false;
2506 bool opened_exclusive = false;
2507
2508 /* If there is no agent, only return the interface */
2509 if (!agent_handle)
2510 goto out;
2511
2512 /* For TEST_PROTOCOL ignore interface attribute */
2513 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2514 *protocol_interface = NULL;
2515
2516 /*
2517 * Check if the protocol is already opened by a driver with the same
2518 * attributes or opened exclusively
2519 */
2520 list_for_each_entry(item, &handler->open_infos, link) {
2521 if (item->info.agent_handle == agent_handle) {
2522 if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) &&
2523 (item->info.attributes == attributes))
2524 return EFI_ALREADY_STARTED;
2525 }
2526 if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE)
2527 opened_exclusive = true;
2528 }
2529
2530 /* Only one controller can open the protocol exclusively */
2531 if (opened_exclusive && attributes &
2532 (EFI_OPEN_PROTOCOL_EXCLUSIVE | EFI_OPEN_PROTOCOL_BY_DRIVER))
2533 return EFI_ACCESS_DENIED;
2534
2535 /* Prepare exclusive opening */
2536 if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) {
2537 /* Try to disconnect controllers */
2538 list_for_each_entry(item, &handler->open_infos, link) {
2539 if (item->info.attributes ==
2540 EFI_OPEN_PROTOCOL_BY_DRIVER)
2541 EFI_CALL(efi_disconnect_controller(
2542 item->info.controller_handle,
2543 item->info.agent_handle,
2544 NULL));
2545 }
2546 opened_by_driver = false;
2547 /* Check if all controllers are disconnected */
2548 list_for_each_entry(item, &handler->open_infos, link) {
2549 if (item->info.attributes & EFI_OPEN_PROTOCOL_BY_DRIVER)
2550 opened_by_driver = true;
2551 }
2552 /* Only one controller can be conncected */
2553 if (opened_by_driver)
2554 return EFI_ACCESS_DENIED;
2555 }
2556
2557 /* Find existing entry */
2558 list_for_each_entry(item, &handler->open_infos, link) {
2559 if (item->info.agent_handle == agent_handle &&
2560 item->info.controller_handle == controller_handle)
2561 match = &item->info;
2562 }
2563 /* None found, create one */
2564 if (!match) {
2565 match = efi_create_open_info(handler);
2566 if (!match)
2567 return EFI_OUT_OF_RESOURCES;
2568 }
2569
2570 match->agent_handle = agent_handle;
2571 match->controller_handle = controller_handle;
2572 match->attributes = attributes;
2573 match->open_count++;
2574
2575out:
2576 /* For TEST_PROTOCOL ignore interface attribute. */
2577 if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL)
2578 *protocol_interface = handler->protocol_interface;
2579
2580 return EFI_SUCCESS;
2581}
2582
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002583/**
Mario Six78a88f72018-07-10 08:40:17 +02002584 * efi_open_protocol() - open protocol interface on a handle
2585 * @handle: handle on which the protocol shall be opened
2586 * @protocol: GUID of the protocol
2587 * @protocol_interface: interface implementing the protocol
2588 * @agent_handle: handle of the driver
2589 * @controller_handle: handle of the controller
2590 * @attributes: attributes indicating how to open the protocol
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002591 *
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002592 * This function implements the OpenProtocol interface.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002593 *
Mario Six78a88f72018-07-10 08:40:17 +02002594 * See the Unified Extensible Firmware Interface (UEFI) specification for
2595 * details.
2596 *
2597 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002598 */
Alexander Grafbee91162016-03-04 01:09:59 +01002599static efi_status_t EFIAPI efi_open_protocol(
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002600 void *handle, const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002601 void **protocol_interface, void *agent_handle,
2602 void *controller_handle, uint32_t attributes)
2603{
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002604 struct efi_handler *handler;
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002605 efi_status_t r = EFI_INVALID_PARAMETER;
Alexander Grafbee91162016-03-04 01:09:59 +01002606
Rob Clark778e6af2017-09-13 18:05:41 -04002607 EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002608 protocol_interface, agent_handle, controller_handle,
2609 attributes);
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002610
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002611 if (!handle || !protocol ||
2612 (!protocol_interface && attributes !=
2613 EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) {
2614 goto out;
2615 }
2616
2617 switch (attributes) {
2618 case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL:
2619 case EFI_OPEN_PROTOCOL_GET_PROTOCOL:
2620 case EFI_OPEN_PROTOCOL_TEST_PROTOCOL:
2621 break;
2622 case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER:
2623 if (controller_handle == handle)
2624 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002625 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002626 case EFI_OPEN_PROTOCOL_BY_DRIVER:
2627 case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002628 /* Check that the controller handle is valid */
2629 if (!efi_search_obj(controller_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002630 goto out;
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002631 /* fall-through */
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002632 case EFI_OPEN_PROTOCOL_EXCLUSIVE:
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002633 /* Check that the agent handle is valid */
2634 if (!efi_search_obj(agent_handle))
xypron.glpk@gmx.de69baec62017-07-11 22:06:15 +02002635 goto out;
2636 break;
2637 default:
xypron.glpk@gmx.deb5349f72017-07-11 22:06:14 +02002638 goto out;
2639 }
2640
Heinrich Schuchardt80286e82017-11-26 14:05:15 +01002641 r = efi_search_protocol(handle, protocol, &handler);
2642 if (r != EFI_SUCCESS)
2643 goto out;
Alexander Grafbee91162016-03-04 01:09:59 +01002644
Heinrich Schuchardt191a41c2018-01-11 08:15:58 +01002645 r = efi_protocol_open(handler, protocol_interface, agent_handle,
2646 controller_handle, attributes);
Alexander Grafbee91162016-03-04 01:09:59 +01002647out:
2648 return EFI_EXIT(r);
2649}
2650
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002651/**
Mario Six78a88f72018-07-10 08:40:17 +02002652 * efi_handle_protocol() - get interface of a protocol on a handle
2653 * @handle: handle on which the protocol shall be opened
2654 * @protocol: GUID of the protocol
2655 * @protocol_interface: interface implementing the protocol
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002656 *
2657 * This function implements the HandleProtocol service.
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002658 *
Mario Six78a88f72018-07-10 08:40:17 +02002659 * See the Unified Extensible Firmware Interface (UEFI) specification for
2660 * details.
2661 *
2662 * Return: status code
Heinrich Schuchardt332468f2017-09-21 18:30:11 +02002663 */
Heinrich Schuchardt2074f702018-01-11 08:16:09 +01002664static efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle,
Heinrich Schuchardt5a9682d2017-10-05 16:35:53 +02002665 const efi_guid_t *protocol,
Alexander Grafbee91162016-03-04 01:09:59 +01002666 void **protocol_interface)
2667{
xypron.glpk@gmx.de8e1d3292017-06-29 21:16:19 +02002668 return efi_open_protocol(handle, protocol, protocol_interface, NULL,
2669 NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Alexander Grafbee91162016-03-04 01:09:59 +01002670}
2671
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002672/**
Mario Six78a88f72018-07-10 08:40:17 +02002673 * efi_bind_controller() - bind a single driver to a controller
2674 * @controller_handle: controller handle
2675 * @driver_image_handle: driver handle
2676 * @remain_device_path: remaining path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002677 *
Mario Six78a88f72018-07-10 08:40:17 +02002678 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002679 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002680static efi_status_t efi_bind_controller(
2681 efi_handle_t controller_handle,
2682 efi_handle_t driver_image_handle,
2683 struct efi_device_path *remain_device_path)
2684{
2685 struct efi_driver_binding_protocol *binding_protocol;
2686 efi_status_t r;
2687
2688 r = EFI_CALL(efi_open_protocol(driver_image_handle,
2689 &efi_guid_driver_binding_protocol,
2690 (void **)&binding_protocol,
2691 driver_image_handle, NULL,
2692 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
2693 if (r != EFI_SUCCESS)
2694 return r;
2695 r = EFI_CALL(binding_protocol->supported(binding_protocol,
2696 controller_handle,
2697 remain_device_path));
2698 if (r == EFI_SUCCESS)
2699 r = EFI_CALL(binding_protocol->start(binding_protocol,
2700 controller_handle,
2701 remain_device_path));
2702 EFI_CALL(efi_close_protocol(driver_image_handle,
2703 &efi_guid_driver_binding_protocol,
2704 driver_image_handle, NULL));
2705 return r;
2706}
2707
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002708/**
Mario Six78a88f72018-07-10 08:40:17 +02002709 * efi_connect_single_controller() - connect a single driver to a controller
2710 * @controller_handle: controller
2711 * @driver_image_handle: driver
2712 * @remain_device_path: remainting path
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002713 *
Mario Six78a88f72018-07-10 08:40:17 +02002714 * Return: status code
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002715 */
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002716static efi_status_t efi_connect_single_controller(
2717 efi_handle_t controller_handle,
2718 efi_handle_t *driver_image_handle,
2719 struct efi_device_path *remain_device_path)
2720{
2721 efi_handle_t *buffer;
2722 size_t count;
2723 size_t i;
2724 efi_status_t r;
2725 size_t connected = 0;
2726
2727 /* Get buffer with all handles with driver binding protocol */
2728 r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL,
2729 &efi_guid_driver_binding_protocol,
2730 NULL, &count, &buffer));
2731 if (r != EFI_SUCCESS)
2732 return r;
2733
2734 /* Context Override */
2735 if (driver_image_handle) {
2736 for (; *driver_image_handle; ++driver_image_handle) {
2737 for (i = 0; i < count; ++i) {
2738 if (buffer[i] == *driver_image_handle) {
2739 buffer[i] = NULL;
2740 r = efi_bind_controller(
2741 controller_handle,
2742 *driver_image_handle,
2743 remain_device_path);
2744 /*
2745 * For drivers that do not support the
2746 * controller or are already connected
2747 * we receive an error code here.
2748 */
2749 if (r == EFI_SUCCESS)
2750 ++connected;
2751 }
2752 }
2753 }
2754 }
2755
2756 /*
2757 * TODO: Some overrides are not yet implemented:
2758 * - Platform Driver Override
2759 * - Driver Family Override Search
2760 * - Bus Specific Driver Override
2761 */
2762
2763 /* Driver Binding Search */
2764 for (i = 0; i < count; ++i) {
2765 if (buffer[i]) {
2766 r = efi_bind_controller(controller_handle,
2767 buffer[i],
2768 remain_device_path);
2769 if (r == EFI_SUCCESS)
2770 ++connected;
2771 }
2772 }
2773
2774 efi_free_pool(buffer);
2775 if (!connected)
2776 return EFI_NOT_FOUND;
2777 return EFI_SUCCESS;
2778}
2779
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002780/**
Mario Six78a88f72018-07-10 08:40:17 +02002781 * efi_connect_controller() - connect a controller to a driver
2782 * @controller_handle: handle of the controller
2783 * @driver_image_handle: handle of the driver
2784 * @remain_device_path: device path of a child controller
2785 * @recursive: true to connect all child controllers
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002786 *
2787 * This function implements the ConnectController service.
Mario Six78a88f72018-07-10 08:40:17 +02002788 *
2789 * See the Unified Extensible Firmware Interface (UEFI) specification for
2790 * details.
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002791 *
2792 * First all driver binding protocol handles are tried for binding drivers.
2793 * Afterwards all handles that have openened a protocol of the controller
2794 * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers.
2795 *
Mario Six78a88f72018-07-10 08:40:17 +02002796 * Return: status code
Heinrich Schuchardtf0959db2018-01-11 08:16:02 +01002797 */
2798static efi_status_t EFIAPI efi_connect_controller(
2799 efi_handle_t controller_handle,
2800 efi_handle_t *driver_image_handle,
2801 struct efi_device_path *remain_device_path,
2802 bool recursive)
2803{
2804 efi_status_t r;
2805 efi_status_t ret = EFI_NOT_FOUND;
2806 struct efi_object *efiobj;
2807
2808 EFI_ENTRY("%p, %p, %p, %d", controller_handle, driver_image_handle,
2809 remain_device_path, recursive);
2810
2811 efiobj = efi_search_obj(controller_handle);
2812 if (!efiobj) {
2813 ret = EFI_INVALID_PARAMETER;
2814 goto out;
2815 }
2816
2817 r = efi_connect_single_controller(controller_handle,
2818 driver_image_handle,
2819 remain_device_path);
2820 if (r == EFI_SUCCESS)
2821 ret = EFI_SUCCESS;
2822 if (recursive) {
2823 struct efi_handler *handler;
2824 struct efi_open_protocol_info_item *item;
2825
2826 list_for_each_entry(handler, &efiobj->protocols, link) {
2827 list_for_each_entry(item, &handler->open_infos, link) {
2828 if (item->info.attributes &
2829 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2830 r = EFI_CALL(efi_connect_controller(
2831 item->info.controller_handle,
2832 driver_image_handle,
2833 remain_device_path,
2834 recursive));
2835 if (r == EFI_SUCCESS)
2836 ret = EFI_SUCCESS;
2837 }
2838 }
2839 }
2840 }
2841 /* Check for child controller specified by end node */
2842 if (ret != EFI_SUCCESS && remain_device_path &&
2843 remain_device_path->type == DEVICE_PATH_TYPE_END)
2844 ret = EFI_SUCCESS;
2845out:
2846 return EFI_EXIT(ret);
2847}
2848
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002849/**
Mario Six78a88f72018-07-10 08:40:17 +02002850 * efi_reinstall_protocol_interface() - reinstall protocol interface
2851 * @handle: handle on which the protocol shall be reinstalled
2852 * @protocol: GUID of the protocol to be installed
2853 * @old_interface: interface to be removed
2854 * @new_interface: interface to be installed
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002855 *
2856 * This function implements the ReinstallProtocolInterface service.
Mario Six78a88f72018-07-10 08:40:17 +02002857 *
2858 * See the Unified Extensible Firmware Interface (UEFI) specification for
2859 * details.
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002860 *
2861 * The old interface is uninstalled. The new interface is installed.
2862 * Drivers are connected.
2863 *
Mario Six78a88f72018-07-10 08:40:17 +02002864 * Return: status code
Heinrich Schuchardte861a122018-05-11 12:09:22 +02002865 */
2866static efi_status_t EFIAPI efi_reinstall_protocol_interface(
2867 efi_handle_t handle, const efi_guid_t *protocol,
2868 void *old_interface, void *new_interface)
2869{
2870 efi_status_t ret;
2871
2872 EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface,
2873 new_interface);
2874 ret = EFI_CALL(efi_uninstall_protocol_interface(handle, protocol,
2875 old_interface));
2876 if (ret != EFI_SUCCESS)
2877 goto out;
2878 ret = EFI_CALL(efi_install_protocol_interface(&handle, protocol,
2879 EFI_NATIVE_INTERFACE,
2880 new_interface));
2881 if (ret != EFI_SUCCESS)
2882 goto out;
2883 /*
2884 * The returned status code has to be ignored.
2885 * Do not create an error if no suitable driver for the handle exists.
2886 */
2887 EFI_CALL(efi_connect_controller(handle, NULL, NULL, true));
2888out:
2889 return EFI_EXIT(ret);
2890}
2891
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002892/**
Mario Six78a88f72018-07-10 08:40:17 +02002893 * efi_get_child_controllers() - get all child controllers associated to a driver
2894 * @efiobj: handle of the controller
2895 * @driver_handle: handle of the driver
2896 * @number_of_children: number of child controllers
2897 * @child_handle_buffer: handles of the the child controllers
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002898 *
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002899 * The allocated buffer has to be freed with free().
2900 *
Mario Six78a88f72018-07-10 08:40:17 +02002901 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002902 */
2903static efi_status_t efi_get_child_controllers(
2904 struct efi_object *efiobj,
2905 efi_handle_t driver_handle,
2906 efi_uintn_t *number_of_children,
2907 efi_handle_t **child_handle_buffer)
2908{
2909 struct efi_handler *handler;
2910 struct efi_open_protocol_info_item *item;
2911 efi_uintn_t count = 0, i;
2912 bool duplicate;
2913
2914 /* Count all child controller associations */
2915 list_for_each_entry(handler, &efiobj->protocols, link) {
2916 list_for_each_entry(item, &handler->open_infos, link) {
2917 if (item->info.agent_handle == driver_handle &&
2918 item->info.attributes &
2919 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER)
2920 ++count;
2921 }
2922 }
2923 /*
2924 * Create buffer. In case of duplicate child controller assignments
2925 * the buffer will be too large. But that does not harm.
2926 */
2927 *number_of_children = 0;
2928 *child_handle_buffer = calloc(count, sizeof(efi_handle_t));
2929 if (!*child_handle_buffer)
2930 return EFI_OUT_OF_RESOURCES;
2931 /* Copy unique child handles */
2932 list_for_each_entry(handler, &efiobj->protocols, link) {
2933 list_for_each_entry(item, &handler->open_infos, link) {
2934 if (item->info.agent_handle == driver_handle &&
2935 item->info.attributes &
2936 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
2937 /* Check this is a new child controller */
2938 duplicate = false;
2939 for (i = 0; i < *number_of_children; ++i) {
2940 if ((*child_handle_buffer)[i] ==
2941 item->info.controller_handle)
2942 duplicate = true;
2943 }
2944 /* Copy handle to buffer */
2945 if (!duplicate) {
2946 i = (*number_of_children)++;
2947 (*child_handle_buffer)[i] =
2948 item->info.controller_handle;
2949 }
2950 }
2951 }
2952 }
2953 return EFI_SUCCESS;
2954}
2955
Heinrich Schuchardt6b03cd12018-05-11 18:15:41 +02002956/**
Mario Six78a88f72018-07-10 08:40:17 +02002957 * efi_disconnect_controller() - disconnect a controller from a driver
2958 * @controller_handle: handle of the controller
2959 * @driver_image_handle: handle of the driver
2960 * @child_handle: handle of the child to destroy
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002961 *
2962 * This function implements the DisconnectController service.
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002963 *
Mario Six78a88f72018-07-10 08:40:17 +02002964 * See the Unified Extensible Firmware Interface (UEFI) specification for
2965 * details.
2966 *
2967 * Return: status code
Heinrich Schuchardt3f9b0042018-01-11 08:16:04 +01002968 */
2969static efi_status_t EFIAPI efi_disconnect_controller(
2970 efi_handle_t controller_handle,
2971 efi_handle_t driver_image_handle,
2972 efi_handle_t child_handle)
2973{
2974 struct efi_driver_binding_protocol *binding_protocol;
2975 efi_handle_t *child_handle_buffer = NULL;
2976 size_t number_of_children = 0;
2977 efi_status_t r;
2978 size_t stop_count = 0;
2979 struct efi_object *efiobj;
2980
2981 EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle,
2982 child_handle);
2983
2984 efiobj = efi_search_obj(controller_handle);
2985 if (!efiobj) {
2986 r = EFI_INVALID_PARAMETER;
2987 goto out;
2988 }
2989
2990 if (child_handle && !efi_search_obj(child_handle)) {
2991 r = EFI_INVALID_PARAMETER;
2992 goto out;
2993 }
2994
2995 /* If no driver handle is supplied, disconnect all drivers */
2996 if (!driver_image_handle) {
2997 r = efi_disconnect_all_drivers(efiobj, NULL, child_handle);
2998 goto out;
2999 }
3000
3001 /* Create list of child handles */
3002 if (child_handle) {
3003 number_of_children = 1;
3004 child_handle_buffer = &child_handle;
3005 } else {
3006 efi_get_child_controllers(efiobj,
3007 driver_image_handle,
3008 &number_of_children,
3009 &child_handle_buffer);
3010 }
3011
3012 /* Get the driver binding protocol */
3013 r = EFI_CALL(efi_open_protocol(driver_image_handle,
3014 &efi_guid_driver_binding_protocol,
3015 (void **)&binding_protocol,
3016 driver_image_handle, NULL,
3017 EFI_OPEN_PROTOCOL_GET_PROTOCOL));
3018 if (r != EFI_SUCCESS)
3019 goto out;
3020 /* Remove the children */
3021 if (number_of_children) {
3022 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3023 controller_handle,
3024 number_of_children,
3025 child_handle_buffer));
3026 if (r == EFI_SUCCESS)
3027 ++stop_count;
3028 }
3029 /* Remove the driver */
3030 if (!child_handle)
3031 r = EFI_CALL(binding_protocol->stop(binding_protocol,
3032 controller_handle,
3033 0, NULL));
3034 if (r == EFI_SUCCESS)
3035 ++stop_count;
3036 EFI_CALL(efi_close_protocol(driver_image_handle,
3037 &efi_guid_driver_binding_protocol,
3038 driver_image_handle, NULL));
3039
3040 if (stop_count)
3041 r = EFI_SUCCESS;
3042 else
3043 r = EFI_NOT_FOUND;
3044out:
3045 if (!child_handle)
3046 free(child_handle_buffer);
3047 return EFI_EXIT(r);
3048}
3049
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003050static struct efi_boot_services efi_boot_services = {
Alexander Grafbee91162016-03-04 01:09:59 +01003051 .hdr = {
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003052 .signature = EFI_BOOT_SERVICES_SIGNATURE,
3053 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003054 .headersize = sizeof(struct efi_boot_services),
Alexander Grafbee91162016-03-04 01:09:59 +01003055 },
3056 .raise_tpl = efi_raise_tpl,
3057 .restore_tpl = efi_restore_tpl,
3058 .allocate_pages = efi_allocate_pages_ext,
3059 .free_pages = efi_free_pages_ext,
3060 .get_memory_map = efi_get_memory_map_ext,
Stefan Brünsead12742016-10-09 22:17:18 +02003061 .allocate_pool = efi_allocate_pool_ext,
Stefan Brüns42417bc2016-10-09 22:17:26 +02003062 .free_pool = efi_free_pool_ext,
xypron.glpk@gmx.de49deb452017-07-18 20:17:20 +02003063 .create_event = efi_create_event_ext,
xypron.glpk@gmx.debfc72462017-07-18 20:17:21 +02003064 .set_timer = efi_set_timer_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003065 .wait_for_event = efi_wait_for_event,
xypron.glpk@gmx.dec6841592017-07-18 20:17:18 +02003066 .signal_event = efi_signal_event_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003067 .close_event = efi_close_event,
3068 .check_event = efi_check_event,
Heinrich Schuchardt1760ef52017-11-06 21:17:44 +01003069 .install_protocol_interface = efi_install_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003070 .reinstall_protocol_interface = efi_reinstall_protocol_interface,
Heinrich Schuchardtcd534082017-11-06 21:17:45 +01003071 .uninstall_protocol_interface = efi_uninstall_protocol_interface,
Alexander Grafbee91162016-03-04 01:09:59 +01003072 .handle_protocol = efi_handle_protocol,
3073 .reserved = NULL,
3074 .register_protocol_notify = efi_register_protocol_notify,
xypron.glpk@gmx.de26329582017-07-11 22:06:21 +02003075 .locate_handle = efi_locate_handle_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003076 .locate_device_path = efi_locate_device_path,
Alexander Graf488bf122016-08-19 01:23:24 +02003077 .install_configuration_table = efi_install_configuration_table_ext,
Alexander Grafbee91162016-03-04 01:09:59 +01003078 .load_image = efi_load_image,
3079 .start_image = efi_start_image,
Alexander Grafa86aeaf2016-05-20 23:28:23 +02003080 .exit = efi_exit,
Alexander Grafbee91162016-03-04 01:09:59 +01003081 .unload_image = efi_unload_image,
3082 .exit_boot_services = efi_exit_boot_services,
3083 .get_next_monotonic_count = efi_get_next_monotonic_count,
3084 .stall = efi_stall,
3085 .set_watchdog_timer = efi_set_watchdog_timer,
3086 .connect_controller = efi_connect_controller,
3087 .disconnect_controller = efi_disconnect_controller,
3088 .open_protocol = efi_open_protocol,
3089 .close_protocol = efi_close_protocol,
3090 .open_protocol_information = efi_open_protocol_information,
3091 .protocols_per_handle = efi_protocols_per_handle,
3092 .locate_handle_buffer = efi_locate_handle_buffer,
3093 .locate_protocol = efi_locate_protocol,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003094 .install_multiple_protocol_interfaces =
3095 efi_install_multiple_protocol_interfaces,
3096 .uninstall_multiple_protocol_interfaces =
3097 efi_uninstall_multiple_protocol_interfaces,
Alexander Grafbee91162016-03-04 01:09:59 +01003098 .calculate_crc32 = efi_calculate_crc32,
3099 .copy_mem = efi_copy_mem,
3100 .set_mem = efi_set_mem,
Heinrich Schuchardt9f0930e2018-02-04 23:05:13 +01003101 .create_event_ex = efi_create_event_ex,
Alexander Grafbee91162016-03-04 01:09:59 +01003102};
3103
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003104static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot";
Alexander Grafbee91162016-03-04 01:09:59 +01003105
Alexander Graf3c63db92016-10-14 13:45:30 +02003106struct efi_system_table __efi_runtime_data systab = {
Alexander Grafbee91162016-03-04 01:09:59 +01003107 .hdr = {
3108 .signature = EFI_SYSTEM_TABLE_SIGNATURE,
Heinrich Schuchardt112f2432018-06-28 12:45:27 +02003109 .revision = EFI_SPECIFICATION_VERSION,
Heinrich Schuchardt71c846a2018-06-28 12:45:29 +02003110 .headersize = sizeof(struct efi_system_table),
Alexander Grafbee91162016-03-04 01:09:59 +01003111 },
Heinrich Schuchardt0b386532018-06-28 12:45:30 +02003112 .fw_vendor = firmware_vendor,
3113 .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8,
Heinrich Schuchardtab9efa92018-02-18 15:17:49 +01003114 .con_in = (void *)&efi_con_in,
3115 .con_out = (void *)&efi_con_out,
3116 .std_err = (void *)&efi_con_out,
3117 .runtime = (void *)&efi_runtime_services,
3118 .boottime = (void *)&efi_boot_services,
Alexander Grafbee91162016-03-04 01:09:59 +01003119 .nr_tables = 0,
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003120 .tables = NULL,
Alexander Grafbee91162016-03-04 01:09:59 +01003121};
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003122
3123/**
3124 * efi_initialize_system_table() - Initialize system table
3125 *
3126 * Return Value: status code
3127 */
3128efi_status_t efi_initialize_system_table(void)
3129{
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003130 efi_status_t ret;
3131
3132 /* Allocate configuration table array */
3133 ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA,
3134 EFI_MAX_CONFIGURATION_TABLES *
3135 sizeof(struct efi_configuration_table),
3136 (void **)&systab.tables);
3137
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003138 /* Set crc32 field in table headers */
3139 efi_update_table_header_crc32(&systab.hdr);
3140 efi_update_table_header_crc32(&efi_runtime_services.hdr);
3141 efi_update_table_header_crc32(&efi_boot_services.hdr);
Heinrich Schuchardt4182a122018-06-28 12:45:32 +02003142
3143 return ret;
Heinrich Schuchardt640adad2018-06-28 12:45:31 +02003144}