Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | 359a699 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3 | * EFI application boot time services |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | 359a699 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 8 | #include <common.h> |
Heinrich Schuchardt | 7d96332 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 9 | #include <div64.h> |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Simon Glass | 36bf446 | 2019-11-14 12:57:42 -0700 | [diff] [blame] | 11 | #include <irq_func.h> |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 13 | #include <time.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt_env.h> |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 15 | #include <u-boot/crc.h> |
| 16 | #include <bootm.h> |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 17 | #include <pe.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 19 | #include <watchdog.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 23 | /* Task priority level */ |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 24 | static efi_uintn_t efi_tpl = TPL_APPLICATION; |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 25 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 26 | /* This list contains all the EFI objects our payload has access to */ |
| 27 | LIST_HEAD(efi_obj_list); |
| 28 | |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 29 | /* List of all events */ |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 30 | __efi_runtime_data LIST_HEAD(efi_events); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 31 | |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 32 | /* List of queued events */ |
| 33 | LIST_HEAD(efi_event_queue); |
| 34 | |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 35 | /* Flag to disable timer activity in ExitBootServices() */ |
| 36 | static bool timers_enabled = true; |
| 37 | |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 38 | /* List of all events registered by RegisterProtocolNotify() */ |
| 39 | LIST_HEAD(efi_register_notify_events); |
| 40 | |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 41 | /* Handle of the currently executing image */ |
| 42 | static efi_handle_t current_image; |
| 43 | |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 44 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 45 | /* |
| 46 | * The "gd" pointer lives in a register on ARM and AArch64 that we declare |
| 47 | * fixed when compiling U-Boot. However, the payload does not know about that |
| 48 | * restriction so we need to manually swap its and our view of that register on |
| 49 | * EFI callback entry/exit. |
| 50 | */ |
| 51 | static volatile void *efi_gd, *app_gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 52 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 53 | |
Heinrich Schuchardt | 914df75 | 2019-02-09 14:10:39 +0100 | [diff] [blame] | 54 | /* 1 if inside U-Boot code, 0 if inside EFI payload code */ |
| 55 | static int entry_count = 1; |
Rob Clark | af65db8 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 56 | static int nesting_level; |
Heinrich Schuchardt | bc4f913 | 2018-03-03 15:29:03 +0100 | [diff] [blame] | 57 | /* GUID of the device tree table */ |
| 58 | const efi_guid_t efi_guid_fdt = EFI_FDT_GUID; |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 59 | /* GUID of the EFI_DRIVER_BINDING_PROTOCOL */ |
| 60 | const efi_guid_t efi_guid_driver_binding_protocol = |
| 61 | EFI_DRIVER_BINDING_PROTOCOL_GUID; |
Rob Clark | c160d2f | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 62 | |
Heinrich Schuchardt | a3a28f5 | 2018-02-18 15:17:51 +0100 | [diff] [blame] | 63 | /* event group ExitBootServices() invoked */ |
| 64 | const efi_guid_t efi_guid_event_group_exit_boot_services = |
| 65 | EFI_EVENT_GROUP_EXIT_BOOT_SERVICES; |
| 66 | /* event group SetVirtualAddressMap() invoked */ |
| 67 | const efi_guid_t efi_guid_event_group_virtual_address_change = |
| 68 | EFI_EVENT_GROUP_VIRTUAL_ADDRESS_CHANGE; |
| 69 | /* event group memory map changed */ |
| 70 | const efi_guid_t efi_guid_event_group_memory_map_change = |
| 71 | EFI_EVENT_GROUP_MEMORY_MAP_CHANGE; |
| 72 | /* event group boot manager about to boot */ |
| 73 | const efi_guid_t efi_guid_event_group_ready_to_boot = |
| 74 | EFI_EVENT_GROUP_READY_TO_BOOT; |
| 75 | /* event group ResetSystem() invoked (before ExitBootServices) */ |
| 76 | const efi_guid_t efi_guid_event_group_reset_system = |
| 77 | EFI_EVENT_GROUP_RESET_SYSTEM; |
| 78 | |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 79 | static efi_status_t EFIAPI efi_disconnect_controller( |
| 80 | efi_handle_t controller_handle, |
| 81 | efi_handle_t driver_image_handle, |
| 82 | efi_handle_t child_handle); |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 83 | |
Rob Clark | c160d2f | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 84 | /* Called on every callback entry */ |
| 85 | int __efi_entry_check(void) |
| 86 | { |
| 87 | int ret = entry_count++ == 0; |
| 88 | #ifdef CONFIG_ARM |
| 89 | assert(efi_gd); |
| 90 | app_gd = gd; |
| 91 | gd = efi_gd; |
| 92 | #endif |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | /* Called on every callback exit */ |
| 97 | int __efi_exit_check(void) |
| 98 | { |
| 99 | int ret = --entry_count == 0; |
| 100 | #ifdef CONFIG_ARM |
| 101 | gd = app_gd; |
| 102 | #endif |
| 103 | return ret; |
| 104 | } |
| 105 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 106 | /* Called from do_bootefi_exec() */ |
| 107 | void efi_save_gd(void) |
| 108 | { |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 109 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 110 | efi_gd = gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 111 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Rob Clark | c160d2f | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 114 | /* |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 115 | * Special case handler for error/abort that just forces things back to u-boot |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 116 | * world so we can dump out an abort message, without any care about returning |
| 117 | * back to UEFI world. |
Rob Clark | c160d2f | 2017-07-27 08:04:18 -0400 | [diff] [blame] | 118 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 119 | void efi_restore_gd(void) |
| 120 | { |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 121 | #ifdef CONFIG_ARM |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 122 | /* Only restore if we're already in EFI context */ |
| 123 | if (!efi_gd) |
| 124 | return; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 125 | gd = efi_gd; |
Simon Glass | 65e4c0b | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 126 | #endif |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 129 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 130 | * indent_string() - returns a string for indenting with two spaces per level |
| 131 | * @level: indent level |
Heinrich Schuchardt | c8df80c | 2018-01-24 19:21:36 +0100 | [diff] [blame] | 132 | * |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 133 | * A maximum of ten indent levels is supported. Higher indent levels will be |
| 134 | * truncated. |
| 135 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 136 | * Return: A string for indenting with two spaces per level is |
| 137 | * returned. |
Rob Clark | af65db8 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 138 | */ |
| 139 | static const char *indent_string(int level) |
| 140 | { |
| 141 | const char *indent = " "; |
| 142 | const int max = strlen(indent); |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 143 | |
Rob Clark | af65db8 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 144 | level = min(max, level * 2); |
| 145 | return &indent[max - level]; |
| 146 | } |
| 147 | |
Heinrich Schuchardt | ae0bd3a | 2017-08-18 17:45:16 +0200 | [diff] [blame] | 148 | const char *__efi_nesting(void) |
| 149 | { |
| 150 | return indent_string(nesting_level); |
| 151 | } |
| 152 | |
Rob Clark | af65db8 | 2017-07-27 08:04:19 -0400 | [diff] [blame] | 153 | const char *__efi_nesting_inc(void) |
| 154 | { |
| 155 | return indent_string(nesting_level++); |
| 156 | } |
| 157 | |
| 158 | const char *__efi_nesting_dec(void) |
| 159 | { |
| 160 | return indent_string(--nesting_level); |
| 161 | } |
| 162 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 163 | /** |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 164 | * efi_event_is_queued() - check if an event is queued |
| 165 | * |
| 166 | * @event: event |
| 167 | * Return: true if event is queued |
| 168 | */ |
| 169 | static bool efi_event_is_queued(struct efi_event *event) |
| 170 | { |
| 171 | return !!event->queue_link.next; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * efi_process_event_queue() - process event queue |
| 176 | */ |
| 177 | static void efi_process_event_queue(void) |
| 178 | { |
| 179 | while (!list_empty(&efi_event_queue)) { |
| 180 | struct efi_event *event; |
| 181 | efi_uintn_t old_tpl; |
| 182 | |
| 183 | event = list_first_entry(&efi_event_queue, struct efi_event, |
| 184 | queue_link); |
| 185 | if (efi_tpl >= event->notify_tpl) |
| 186 | return; |
| 187 | list_del(&event->queue_link); |
| 188 | event->queue_link.next = NULL; |
| 189 | event->queue_link.prev = NULL; |
| 190 | /* Events must be executed at the event's TPL */ |
| 191 | old_tpl = efi_tpl; |
| 192 | efi_tpl = event->notify_tpl; |
| 193 | EFI_CALL_VOID(event->notify_function(event, |
| 194 | event->notify_context)); |
| 195 | efi_tpl = old_tpl; |
| 196 | if (event->type == EVT_NOTIFY_SIGNAL) |
| 197 | event->is_signaled = 0; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 202 | * efi_queue_event() - queue an EFI event |
| 203 | * @event: event to signal |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 204 | * |
| 205 | * This function queues the notification function of the event for future |
| 206 | * execution. |
| 207 | * |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 208 | */ |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 209 | static void efi_queue_event(struct efi_event *event) |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 210 | { |
Heinrich Schuchardt | 2b8568f | 2020-03-06 21:56:10 +0100 | [diff] [blame] | 211 | struct efi_event *item; |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 212 | |
| 213 | if (!event->notify_function) |
| 214 | return; |
| 215 | |
| 216 | if (!efi_event_is_queued(event)) { |
| 217 | /* |
| 218 | * Events must be notified in order of decreasing task priority |
| 219 | * level. Insert the new event accordingly. |
| 220 | */ |
| 221 | list_for_each_entry(item, &efi_event_queue, queue_link) { |
| 222 | if (item->notify_tpl < event->notify_tpl) { |
| 223 | list_add_tail(&event->queue_link, |
| 224 | &item->queue_link); |
| 225 | event = NULL; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | if (event) |
| 230 | list_add_tail(&event->queue_link, &efi_event_queue); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 231 | } |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 232 | efi_process_event_queue(); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 235 | /** |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 236 | * is_valid_tpl() - check if the task priority level is valid |
| 237 | * |
| 238 | * @tpl: TPL level to check |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 239 | * Return: status code |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 240 | */ |
| 241 | efi_status_t is_valid_tpl(efi_uintn_t tpl) |
| 242 | { |
| 243 | switch (tpl) { |
| 244 | case TPL_APPLICATION: |
| 245 | case TPL_CALLBACK: |
| 246 | case TPL_NOTIFY: |
| 247 | case TPL_HIGH_LEVEL: |
| 248 | return EFI_SUCCESS; |
| 249 | default: |
| 250 | return EFI_INVALID_PARAMETER; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 255 | * efi_signal_event() - signal an EFI event |
| 256 | * @event: event to signal |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 257 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 258 | * This function signals an event. If the event belongs to an event group all |
| 259 | * events of the group are signaled. If they are of type EVT_NOTIFY_SIGNAL |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 260 | * their notification function is queued. |
| 261 | * |
| 262 | * For the SignalEvent service see efi_signal_event_ext. |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 263 | */ |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 264 | void efi_signal_event(struct efi_event *event) |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 265 | { |
Heinrich Schuchardt | dfa306e | 2019-06-06 01:51:50 +0200 | [diff] [blame] | 266 | if (event->is_signaled) |
| 267 | return; |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 268 | if (event->group) { |
| 269 | struct efi_event *evt; |
| 270 | |
| 271 | /* |
| 272 | * The signaled state has to set before executing any |
| 273 | * notification function |
| 274 | */ |
| 275 | list_for_each_entry(evt, &efi_events, link) { |
| 276 | if (!evt->group || guidcmp(evt->group, event->group)) |
| 277 | continue; |
| 278 | if (evt->is_signaled) |
| 279 | continue; |
| 280 | evt->is_signaled = true; |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 281 | } |
| 282 | list_for_each_entry(evt, &efi_events, link) { |
| 283 | if (!evt->group || guidcmp(evt->group, event->group)) |
| 284 | continue; |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 285 | efi_queue_event(evt); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 286 | } |
Heinrich Schuchardt | 3626e53 | 2019-05-05 00:07:34 +0200 | [diff] [blame] | 287 | } else { |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 288 | event->is_signaled = true; |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 289 | efi_queue_event(event); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 293 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 294 | * efi_raise_tpl() - raise the task priority level |
| 295 | * @new_tpl: new value of the task priority level |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 296 | * |
| 297 | * This function implements the RaiseTpl service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 298 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 299 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 300 | * details. |
| 301 | * |
| 302 | * Return: old value of the task priority level |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 303 | */ |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 304 | static unsigned long EFIAPI efi_raise_tpl(efi_uintn_t new_tpl) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 305 | { |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 306 | efi_uintn_t old_tpl = efi_tpl; |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 307 | |
xypron.glpk@gmx.de | 503f269 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 308 | EFI_ENTRY("0x%zx", new_tpl); |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 309 | |
| 310 | if (new_tpl < efi_tpl) |
Heinrich Schuchardt | 529886a | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 311 | EFI_PRINT("WARNING: new_tpl < current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 312 | efi_tpl = new_tpl; |
| 313 | if (efi_tpl > TPL_HIGH_LEVEL) |
| 314 | efi_tpl = TPL_HIGH_LEVEL; |
| 315 | |
| 316 | EFI_EXIT(EFI_SUCCESS); |
| 317 | return old_tpl; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 320 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 321 | * efi_restore_tpl() - lower the task priority level |
| 322 | * @old_tpl: value of the task priority level to be restored |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 323 | * |
| 324 | * This function implements the RestoreTpl service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 325 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 326 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 327 | * details. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 328 | */ |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 329 | static void EFIAPI efi_restore_tpl(efi_uintn_t old_tpl) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 330 | { |
xypron.glpk@gmx.de | 503f269 | 2017-07-18 20:17:19 +0200 | [diff] [blame] | 331 | EFI_ENTRY("0x%zx", old_tpl); |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 332 | |
| 333 | if (old_tpl > efi_tpl) |
Heinrich Schuchardt | 529886a | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 334 | EFI_PRINT("WARNING: old_tpl > current_tpl in %s\n", __func__); |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 335 | efi_tpl = old_tpl; |
| 336 | if (efi_tpl > TPL_HIGH_LEVEL) |
| 337 | efi_tpl = TPL_HIGH_LEVEL; |
| 338 | |
Heinrich Schuchardt | 0f7fcc7 | 2018-03-24 18:40:21 +0100 | [diff] [blame] | 339 | /* |
| 340 | * Lowering the TPL may have made queued events eligible for execution. |
| 341 | */ |
| 342 | efi_timer_check(); |
| 343 | |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 344 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 347 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 348 | * efi_allocate_pages_ext() - allocate memory pages |
| 349 | * @type: type of allocation to be performed |
| 350 | * @memory_type: usage type of the allocated memory |
| 351 | * @pages: number of pages to be allocated |
| 352 | * @memory: allocated memory |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 353 | * |
| 354 | * This function implements the AllocatePages service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 355 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 356 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 357 | * details. |
| 358 | * |
| 359 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 360 | */ |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 361 | static efi_status_t EFIAPI efi_allocate_pages_ext(int type, int memory_type, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 362 | efi_uintn_t pages, |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 363 | uint64_t *memory) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 364 | { |
| 365 | efi_status_t r; |
| 366 | |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 367 | EFI_ENTRY("%d, %d, 0x%zx, %p", type, memory_type, pages, memory); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 368 | r = efi_allocate_pages(type, memory_type, pages, memory); |
| 369 | return EFI_EXIT(r); |
| 370 | } |
| 371 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 372 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 373 | * efi_free_pages_ext() - Free memory pages. |
| 374 | * @memory: start of the memory area to be freed |
| 375 | * @pages: number of pages to be freed |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 376 | * |
| 377 | * This function implements the FreePages service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 378 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 379 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 380 | * details. |
| 381 | * |
| 382 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 383 | */ |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 384 | static efi_status_t EFIAPI efi_free_pages_ext(uint64_t memory, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 385 | efi_uintn_t pages) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 386 | { |
| 387 | efi_status_t r; |
| 388 | |
Masahiro Yamada | dee37fc | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 389 | EFI_ENTRY("%llx, 0x%zx", memory, pages); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 390 | r = efi_free_pages(memory, pages); |
| 391 | return EFI_EXIT(r); |
| 392 | } |
| 393 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 394 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 395 | * efi_get_memory_map_ext() - get map describing memory usage |
| 396 | * @memory_map_size: on entry the size, in bytes, of the memory map buffer, |
| 397 | * on exit the size of the copied memory map |
| 398 | * @memory_map: buffer to which the memory map is written |
| 399 | * @map_key: key for the memory map |
| 400 | * @descriptor_size: size of an individual memory descriptor |
| 401 | * @descriptor_version: version number of the memory descriptor structure |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 402 | * |
| 403 | * This function implements the GetMemoryMap service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 404 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 405 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 406 | * details. |
| 407 | * |
| 408 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 409 | */ |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 410 | static efi_status_t EFIAPI efi_get_memory_map_ext( |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 411 | efi_uintn_t *memory_map_size, |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 412 | struct efi_mem_desc *memory_map, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 413 | efi_uintn_t *map_key, |
| 414 | efi_uintn_t *descriptor_size, |
Masahiro Yamada | 6e0bf8d | 2017-06-22 17:49:03 +0900 | [diff] [blame] | 415 | uint32_t *descriptor_version) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 416 | { |
| 417 | efi_status_t r; |
| 418 | |
| 419 | EFI_ENTRY("%p, %p, %p, %p, %p", memory_map_size, memory_map, |
| 420 | map_key, descriptor_size, descriptor_version); |
| 421 | r = efi_get_memory_map(memory_map_size, memory_map, map_key, |
| 422 | descriptor_size, descriptor_version); |
| 423 | return EFI_EXIT(r); |
| 424 | } |
| 425 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 426 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 427 | * efi_allocate_pool_ext() - allocate memory from pool |
| 428 | * @pool_type: type of the pool from which memory is to be allocated |
| 429 | * @size: number of bytes to be allocated |
| 430 | * @buffer: allocated memory |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 431 | * |
| 432 | * This function implements the AllocatePool service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 433 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 434 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 435 | * details. |
| 436 | * |
| 437 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 438 | */ |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 439 | static efi_status_t EFIAPI efi_allocate_pool_ext(int pool_type, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 440 | efi_uintn_t size, |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 441 | void **buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 442 | { |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 443 | efi_status_t r; |
| 444 | |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 445 | EFI_ENTRY("%d, %zd, %p", pool_type, size, buffer); |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 446 | r = efi_allocate_pool(pool_type, size, buffer); |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 447 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 450 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 451 | * efi_free_pool_ext() - free memory from pool |
| 452 | * @buffer: start of memory to be freed |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 453 | * |
| 454 | * This function implements the FreePool service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 455 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 456 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 457 | * details. |
| 458 | * |
| 459 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 460 | */ |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 461 | static efi_status_t EFIAPI efi_free_pool_ext(void *buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 462 | { |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 463 | efi_status_t r; |
| 464 | |
| 465 | EFI_ENTRY("%p", buffer); |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 466 | r = efi_free_pool(buffer); |
Alexander Graf | 1cd29f0 | 2016-03-24 01:37:37 +0100 | [diff] [blame] | 467 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 470 | /** |
Heinrich Schuchardt | e6023be | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 471 | * efi_add_handle() - add a new handle to the object list |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 472 | * |
Heinrich Schuchardt | e6023be | 2019-05-01 09:42:39 +0200 | [diff] [blame] | 473 | * @handle: handle to be added |
| 474 | * |
| 475 | * The protocols list is initialized. The handle is added to the list of known |
| 476 | * UEFI objects. |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 477 | */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 478 | void efi_add_handle(efi_handle_t handle) |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 479 | { |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 480 | if (!handle) |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 481 | return; |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 482 | INIT_LIST_HEAD(&handle->protocols); |
| 483 | list_add_tail(&handle->link, &efi_obj_list); |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 486 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 487 | * efi_create_handle() - create handle |
| 488 | * @handle: new handle |
Heinrich Schuchardt | 2edab5e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 489 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 490 | * Return: status code |
Heinrich Schuchardt | 2edab5e | 2017-10-26 19:25:49 +0200 | [diff] [blame] | 491 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 492 | efi_status_t efi_create_handle(efi_handle_t *handle) |
Heinrich Schuchardt | 3cc6e3f | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 493 | { |
| 494 | struct efi_object *obj; |
Heinrich Schuchardt | 3cc6e3f | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 495 | |
Heinrich Schuchardt | d29e782 | 2018-05-27 16:47:21 +0200 | [diff] [blame] | 496 | obj = calloc(1, sizeof(struct efi_object)); |
| 497 | if (!obj) |
| 498 | return EFI_OUT_OF_RESOURCES; |
| 499 | |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 500 | efi_add_handle(obj); |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 501 | *handle = obj; |
Heinrich Schuchardt | d29e782 | 2018-05-27 16:47:21 +0200 | [diff] [blame] | 502 | |
| 503 | return EFI_SUCCESS; |
Heinrich Schuchardt | 3cc6e3f | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 506 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 507 | * efi_search_protocol() - find a protocol on a handle. |
| 508 | * @handle: handle |
| 509 | * @protocol_guid: GUID of the protocol |
| 510 | * @handler: reference to the protocol |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 511 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 512 | * Return: status code |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 513 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 514 | efi_status_t efi_search_protocol(const efi_handle_t handle, |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 515 | const efi_guid_t *protocol_guid, |
| 516 | struct efi_handler **handler) |
| 517 | { |
| 518 | struct efi_object *efiobj; |
| 519 | struct list_head *lhandle; |
| 520 | |
| 521 | if (!handle || !protocol_guid) |
| 522 | return EFI_INVALID_PARAMETER; |
| 523 | efiobj = efi_search_obj(handle); |
| 524 | if (!efiobj) |
| 525 | return EFI_INVALID_PARAMETER; |
| 526 | list_for_each(lhandle, &efiobj->protocols) { |
| 527 | struct efi_handler *protocol; |
| 528 | |
| 529 | protocol = list_entry(lhandle, struct efi_handler, link); |
| 530 | if (!guidcmp(protocol->guid, protocol_guid)) { |
| 531 | if (handler) |
| 532 | *handler = protocol; |
| 533 | return EFI_SUCCESS; |
| 534 | } |
| 535 | } |
| 536 | return EFI_NOT_FOUND; |
| 537 | } |
| 538 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 539 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 540 | * efi_remove_protocol() - delete protocol from a handle |
| 541 | * @handle: handle from which the protocol shall be deleted |
| 542 | * @protocol: GUID of the protocol to be deleted |
| 543 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 544 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 545 | * Return: status code |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 546 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 547 | efi_status_t efi_remove_protocol(const efi_handle_t handle, |
| 548 | const efi_guid_t *protocol, |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 549 | void *protocol_interface) |
| 550 | { |
| 551 | struct efi_handler *handler; |
| 552 | efi_status_t ret; |
| 553 | |
| 554 | ret = efi_search_protocol(handle, protocol, &handler); |
| 555 | if (ret != EFI_SUCCESS) |
| 556 | return ret; |
Heinrich Schuchardt | 1f470e1 | 2018-05-11 12:09:21 +0200 | [diff] [blame] | 557 | if (handler->protocol_interface != protocol_interface) |
Heinrich Schuchardt | 96aa99c | 2019-05-10 20:06:48 +0200 | [diff] [blame] | 558 | return EFI_NOT_FOUND; |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 559 | list_del(&handler->link); |
| 560 | free(handler); |
| 561 | return EFI_SUCCESS; |
| 562 | } |
| 563 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 564 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 565 | * efi_remove_all_protocols() - delete all protocols from a handle |
| 566 | * @handle: handle from which the protocols shall be deleted |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 567 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 568 | * Return: status code |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 569 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 570 | efi_status_t efi_remove_all_protocols(const efi_handle_t handle) |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 571 | { |
| 572 | struct efi_object *efiobj; |
Heinrich Schuchardt | 32e6fed | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 573 | struct efi_handler *protocol; |
| 574 | struct efi_handler *pos; |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 575 | |
| 576 | efiobj = efi_search_obj(handle); |
| 577 | if (!efiobj) |
| 578 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 32e6fed | 2018-01-11 08:15:55 +0100 | [diff] [blame] | 579 | list_for_each_entry_safe(protocol, pos, &efiobj->protocols, link) { |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 580 | efi_status_t ret; |
| 581 | |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 582 | ret = efi_remove_protocol(handle, protocol->guid, |
| 583 | protocol->protocol_interface); |
| 584 | if (ret != EFI_SUCCESS) |
| 585 | return ret; |
| 586 | } |
| 587 | return EFI_SUCCESS; |
| 588 | } |
| 589 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 590 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 591 | * efi_delete_handle() - delete handle |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 592 | * |
Heinrich Schuchardt | 6631ca5 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 593 | * @handle: handle to delete |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 594 | */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 595 | void efi_delete_handle(efi_handle_t handle) |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 596 | { |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 597 | if (!handle) |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 598 | return; |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 599 | efi_remove_all_protocols(handle); |
| 600 | list_del(&handle->link); |
| 601 | free(handle); |
Heinrich Schuchardt | 678e03a | 2017-12-04 18:03:02 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 604 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 605 | * efi_is_event() - check if a pointer is a valid event |
| 606 | * @event: pointer to check |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 607 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 608 | * Return: status code |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 609 | */ |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 610 | static efi_status_t efi_is_event(const struct efi_event *event) |
| 611 | { |
| 612 | const struct efi_event *evt; |
| 613 | |
| 614 | if (!event) |
| 615 | return EFI_INVALID_PARAMETER; |
| 616 | list_for_each_entry(evt, &efi_events, link) { |
| 617 | if (evt == event) |
| 618 | return EFI_SUCCESS; |
| 619 | } |
| 620 | return EFI_INVALID_PARAMETER; |
| 621 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 622 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 623 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 624 | * efi_create_event() - create an event |
Heinrich Schuchardt | 6631ca5 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 625 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 626 | * @type: type of the event to create |
| 627 | * @notify_tpl: task priority level of the event |
| 628 | * @notify_function: notification function of the event |
| 629 | * @notify_context: pointer passed to the notification function |
| 630 | * @group: event group |
| 631 | * @event: created event |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 632 | * |
| 633 | * This function is used inside U-Boot code to create an event. |
| 634 | * |
| 635 | * For the API function implementing the CreateEvent service see |
| 636 | * efi_create_event_ext. |
| 637 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 638 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 639 | */ |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 640 | efi_status_t efi_create_event(uint32_t type, efi_uintn_t notify_tpl, |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 641 | void (EFIAPI *notify_function) ( |
xypron.glpk@gmx.de | 2fd945f | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 642 | struct efi_event *event, |
| 643 | void *context), |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 644 | void *notify_context, efi_guid_t *group, |
| 645 | struct efi_event **event) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 646 | { |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 647 | struct efi_event *evt; |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 648 | efi_status_t ret; |
| 649 | int pool_type; |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 650 | |
Jonathan Gray | a95343b | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 651 | if (event == NULL) |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 652 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | a95343b | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 653 | |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 654 | switch (type) { |
| 655 | case 0: |
| 656 | case EVT_TIMER: |
| 657 | case EVT_NOTIFY_SIGNAL: |
| 658 | case EVT_TIMER | EVT_NOTIFY_SIGNAL: |
| 659 | case EVT_NOTIFY_WAIT: |
| 660 | case EVT_TIMER | EVT_NOTIFY_WAIT: |
| 661 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 662 | pool_type = EFI_BOOT_SERVICES_DATA; |
| 663 | break; |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 664 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 665 | pool_type = EFI_RUNTIME_SERVICES_DATA; |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 666 | break; |
| 667 | default: |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 668 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 21b3edf | 2018-07-02 12:53:52 +0200 | [diff] [blame] | 669 | } |
Jonathan Gray | a95343b | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 670 | |
AKASHI Takahiro | 3748ed9 | 2018-08-10 15:36:32 +0900 | [diff] [blame] | 671 | if ((type & (EVT_NOTIFY_WAIT | EVT_NOTIFY_SIGNAL)) && |
Heinrich Schuchardt | 751e928 | 2019-04-25 07:30:41 +0200 | [diff] [blame] | 672 | (!notify_function || is_valid_tpl(notify_tpl) != EFI_SUCCESS)) |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 673 | return EFI_INVALID_PARAMETER; |
Jonathan Gray | a95343b | 2017-03-12 19:26:07 +1100 | [diff] [blame] | 674 | |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 675 | ret = efi_allocate_pool(pool_type, sizeof(struct efi_event), |
| 676 | (void **)&evt); |
| 677 | if (ret != EFI_SUCCESS) |
| 678 | return ret; |
| 679 | memset(evt, 0, sizeof(struct efi_event)); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 680 | evt->type = type; |
| 681 | evt->notify_tpl = notify_tpl; |
| 682 | evt->notify_function = notify_function; |
| 683 | evt->notify_context = notify_context; |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 684 | evt->group = group; |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 685 | /* Disable timers on boot up */ |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 686 | evt->trigger_next = -1ULL; |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 687 | list_add_tail(&evt->link, &efi_events); |
| 688 | *event = evt; |
| 689 | return EFI_SUCCESS; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 692 | /* |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 693 | * efi_create_event_ex() - create an event in a group |
| 694 | * @type: type of the event to create |
| 695 | * @notify_tpl: task priority level of the event |
| 696 | * @notify_function: notification function of the event |
| 697 | * @notify_context: pointer passed to the notification function |
| 698 | * @event: created event |
| 699 | * @event_group: event group |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 700 | * |
| 701 | * This function implements the CreateEventEx service. |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 702 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 703 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 704 | * details. |
| 705 | * |
| 706 | * Return: status code |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 707 | */ |
| 708 | efi_status_t EFIAPI efi_create_event_ex(uint32_t type, efi_uintn_t notify_tpl, |
| 709 | void (EFIAPI *notify_function) ( |
| 710 | struct efi_event *event, |
| 711 | void *context), |
| 712 | void *notify_context, |
| 713 | efi_guid_t *event_group, |
| 714 | struct efi_event **event) |
| 715 | { |
Heinrich Schuchardt | 1884512 | 2019-05-04 10:12:50 +0200 | [diff] [blame] | 716 | efi_status_t ret; |
| 717 | |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 718 | EFI_ENTRY("%d, 0x%zx, %p, %p, %pUl", type, notify_tpl, notify_function, |
| 719 | notify_context, event_group); |
Heinrich Schuchardt | 1884512 | 2019-05-04 10:12:50 +0200 | [diff] [blame] | 720 | |
| 721 | /* |
| 722 | * The allowable input parameters are the same as in CreateEvent() |
| 723 | * except for the following two disallowed event types. |
| 724 | */ |
| 725 | switch (type) { |
| 726 | case EVT_SIGNAL_EXIT_BOOT_SERVICES: |
| 727 | case EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE: |
| 728 | ret = EFI_INVALID_PARAMETER; |
| 729 | goto out; |
| 730 | } |
| 731 | |
| 732 | ret = efi_create_event(type, notify_tpl, notify_function, |
| 733 | notify_context, event_group, event); |
| 734 | out: |
| 735 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 738 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 739 | * efi_create_event_ext() - create an event |
| 740 | * @type: type of the event to create |
| 741 | * @notify_tpl: task priority level of the event |
| 742 | * @notify_function: notification function of the event |
| 743 | * @notify_context: pointer passed to the notification function |
| 744 | * @event: created event |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 745 | * |
| 746 | * This function implements the CreateEvent service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 747 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 748 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 749 | * details. |
| 750 | * |
| 751 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 752 | */ |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 753 | static efi_status_t EFIAPI efi_create_event_ext( |
Heinrich Schuchardt | 152cade | 2017-11-06 21:17:47 +0100 | [diff] [blame] | 754 | uint32_t type, efi_uintn_t notify_tpl, |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 755 | void (EFIAPI *notify_function) ( |
| 756 | struct efi_event *event, |
| 757 | void *context), |
| 758 | void *notify_context, struct efi_event **event) |
| 759 | { |
| 760 | EFI_ENTRY("%d, 0x%zx, %p, %p", type, notify_tpl, notify_function, |
| 761 | notify_context); |
| 762 | return EFI_EXIT(efi_create_event(type, notify_tpl, notify_function, |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 763 | notify_context, NULL, event)); |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 766 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 767 | * efi_timer_check() - check if a timer event has occurred |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 768 | * |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 769 | * Check if a timer event has occurred or a queued notification function should |
| 770 | * be called. |
| 771 | * |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 772 | * Our timers have to work without interrupts, so we check whenever keyboard |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 773 | * input or disk accesses happen if enough time elapsed for them to fire. |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 774 | */ |
| 775 | void efi_timer_check(void) |
| 776 | { |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 777 | struct efi_event *evt; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 778 | u64 now = timer_get_us(); |
| 779 | |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 780 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 781 | if (!timers_enabled) |
| 782 | continue; |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 783 | if (!(evt->type & EVT_TIMER) || now < evt->trigger_next) |
Heinrich Schuchardt | ca62a4f | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 784 | continue; |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 785 | switch (evt->trigger_type) { |
Heinrich Schuchardt | ca62a4f | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 786 | case EFI_TIMER_RELATIVE: |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 787 | evt->trigger_type = EFI_TIMER_STOP; |
Heinrich Schuchardt | ca62a4f | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 788 | break; |
| 789 | case EFI_TIMER_PERIODIC: |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 790 | evt->trigger_next += evt->trigger_time; |
Heinrich Schuchardt | ca62a4f | 2017-09-15 10:06:13 +0200 | [diff] [blame] | 791 | break; |
| 792 | default: |
| 793 | continue; |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 794 | } |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 795 | evt->is_signaled = false; |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 796 | efi_signal_event(evt); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 797 | } |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 798 | efi_process_event_queue(); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 799 | WATCHDOG_RESET(); |
| 800 | } |
| 801 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 802 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 803 | * efi_set_timer() - set the trigger time for a timer event or stop the event |
| 804 | * @event: event for which the timer is set |
| 805 | * @type: type of the timer |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 806 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 807 | * |
| 808 | * This is the function for internal usage in U-Boot. For the API function |
| 809 | * implementing the SetTimer service see efi_set_timer_ext. |
| 810 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 811 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 812 | */ |
xypron.glpk@gmx.de | b521d29 | 2017-07-19 19:22:34 +0200 | [diff] [blame] | 813 | efi_status_t efi_set_timer(struct efi_event *event, enum efi_timer_delay type, |
xypron.glpk@gmx.de | bfc7246 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 814 | uint64_t trigger_time) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 815 | { |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 816 | /* Check that the event is valid */ |
| 817 | if (efi_is_event(event) != EFI_SUCCESS || !(event->type & EVT_TIMER)) |
| 818 | return EFI_INVALID_PARAMETER; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 819 | |
xypron.glpk@gmx.de | 8787b02 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 820 | /* |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 821 | * The parameter defines a multiple of 100 ns. |
| 822 | * We use multiples of 1000 ns. So divide by 10. |
xypron.glpk@gmx.de | 8787b02 | 2017-07-18 20:17:23 +0200 | [diff] [blame] | 823 | */ |
Heinrich Schuchardt | 7d96332 | 2017-10-05 16:14:14 +0200 | [diff] [blame] | 824 | do_div(trigger_time, 10); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 825 | |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 826 | switch (type) { |
| 827 | case EFI_TIMER_STOP: |
| 828 | event->trigger_next = -1ULL; |
| 829 | break; |
| 830 | case EFI_TIMER_PERIODIC: |
| 831 | case EFI_TIMER_RELATIVE: |
| 832 | event->trigger_next = timer_get_us() + trigger_time; |
| 833 | break; |
| 834 | default: |
| 835 | return EFI_INVALID_PARAMETER; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 836 | } |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 837 | event->trigger_type = type; |
| 838 | event->trigger_time = trigger_time; |
| 839 | event->is_signaled = false; |
| 840 | return EFI_SUCCESS; |
xypron.glpk@gmx.de | bfc7246 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 843 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 844 | * efi_set_timer_ext() - Set the trigger time for a timer event or stop the |
| 845 | * event |
| 846 | * @event: event for which the timer is set |
| 847 | * @type: type of the timer |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 848 | * @trigger_time: trigger period in multiples of 100 ns |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 849 | * |
| 850 | * This function implements the SetTimer service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 851 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 852 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 853 | * details. |
| 854 | * |
| 855 | * |
| 856 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 857 | */ |
xypron.glpk@gmx.de | b521d29 | 2017-07-19 19:22:34 +0200 | [diff] [blame] | 858 | static efi_status_t EFIAPI efi_set_timer_ext(struct efi_event *event, |
| 859 | enum efi_timer_delay type, |
| 860 | uint64_t trigger_time) |
xypron.glpk@gmx.de | bfc7246 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 861 | { |
Masahiro Yamada | dee37fc | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 862 | EFI_ENTRY("%p, %d, %llx", event, type, trigger_time); |
xypron.glpk@gmx.de | bfc7246 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 863 | return EFI_EXIT(efi_set_timer(event, type, trigger_time)); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 866 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 867 | * efi_wait_for_event() - wait for events to be signaled |
| 868 | * @num_events: number of events to be waited for |
| 869 | * @event: events to be waited for |
| 870 | * @index: index of the event that was signaled |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 871 | * |
| 872 | * This function implements the WaitForEvent service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 873 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 874 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 875 | * details. |
| 876 | * |
| 877 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 878 | */ |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 879 | static efi_status_t EFIAPI efi_wait_for_event(efi_uintn_t num_events, |
xypron.glpk@gmx.de | 2fd945f | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 880 | struct efi_event **event, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 881 | efi_uintn_t *index) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 882 | { |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 883 | int i; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 884 | |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 885 | EFI_ENTRY("%zd, %p, %p", num_events, event, index); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 886 | |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 887 | /* Check parameters */ |
| 888 | if (!num_events || !event) |
| 889 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 32f4b2f | 2017-09-15 10:06:16 +0200 | [diff] [blame] | 890 | /* Check TPL */ |
| 891 | if (efi_tpl != TPL_APPLICATION) |
| 892 | return EFI_EXIT(EFI_UNSUPPORTED); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 893 | for (i = 0; i < num_events; ++i) { |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 894 | if (efi_is_event(event[i]) != EFI_SUCCESS) |
| 895 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 896 | if (!event[i]->type || event[i]->type & EVT_NOTIFY_SIGNAL) |
| 897 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | e190e89 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 898 | if (!event[i]->is_signaled) |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 899 | efi_queue_event(event[i]); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | /* Wait for signal */ |
| 903 | for (;;) { |
| 904 | for (i = 0; i < num_events; ++i) { |
Heinrich Schuchardt | e190e89 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 905 | if (event[i]->is_signaled) |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 906 | goto out; |
| 907 | } |
| 908 | /* Allow events to occur. */ |
| 909 | efi_timer_check(); |
| 910 | } |
| 911 | |
| 912 | out: |
| 913 | /* |
| 914 | * Reset the signal which is passed to the caller to allow periodic |
| 915 | * events to occur. |
| 916 | */ |
Heinrich Schuchardt | e190e89 | 2017-10-04 15:03:24 +0200 | [diff] [blame] | 917 | event[i]->is_signaled = false; |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 918 | if (index) |
| 919 | *index = i; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 920 | |
| 921 | return EFI_EXIT(EFI_SUCCESS); |
| 922 | } |
| 923 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 924 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 925 | * efi_signal_event_ext() - signal an EFI event |
| 926 | * @event: event to signal |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 927 | * |
| 928 | * This function implements the SignalEvent service. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 929 | * |
| 930 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 931 | * details. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 932 | * |
| 933 | * This functions sets the signaled state of the event and queues the |
| 934 | * notification function for execution. |
| 935 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 936 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 937 | */ |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 938 | static efi_status_t EFIAPI efi_signal_event_ext(struct efi_event *event) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 939 | { |
| 940 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 941 | if (efi_is_event(event) != EFI_SUCCESS) |
| 942 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 943 | efi_signal_event(event); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 944 | return EFI_EXIT(EFI_SUCCESS); |
| 945 | } |
| 946 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 947 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 948 | * efi_close_event() - close an EFI event |
| 949 | * @event: event to close |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 950 | * |
| 951 | * This function implements the CloseEvent service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 952 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 953 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 954 | * details. |
| 955 | * |
| 956 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 957 | */ |
xypron.glpk@gmx.de | 2fd945f | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 958 | static efi_status_t EFIAPI efi_close_event(struct efi_event *event) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 959 | { |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 960 | struct efi_register_notify_event *item, *next; |
| 961 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 962 | EFI_ENTRY("%p", event); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 963 | if (efi_is_event(event) != EFI_SUCCESS) |
| 964 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 965 | |
| 966 | /* Remove protocol notify registrations for the event */ |
| 967 | list_for_each_entry_safe(item, next, &efi_register_notify_events, |
| 968 | link) { |
| 969 | if (event == item->event) { |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 970 | struct efi_protocol_notification *hitem, *hnext; |
| 971 | |
| 972 | /* Remove signaled handles */ |
| 973 | list_for_each_entry_safe(hitem, hnext, &item->handles, |
| 974 | link) { |
| 975 | list_del(&hitem->link); |
| 976 | free(hitem); |
| 977 | } |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 978 | list_del(&item->link); |
| 979 | free(item); |
| 980 | } |
| 981 | } |
Heinrich Schuchardt | 7a69e97 | 2019-06-05 21:00:39 +0200 | [diff] [blame] | 982 | /* Remove event from queue */ |
| 983 | if (efi_event_is_queued(event)) |
| 984 | list_del(&event->queue_link); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 985 | |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 986 | list_del(&event->link); |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 987 | efi_free_pool(event); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 988 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 991 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 992 | * efi_check_event() - check if an event is signaled |
| 993 | * @event: event to check |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 994 | * |
| 995 | * This function implements the CheckEvent service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 996 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 997 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 998 | * details. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 999 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1000 | * If an event is not signaled yet, the notification function is queued. The |
| 1001 | * signaled state is cleared. |
| 1002 | * |
| 1003 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1004 | */ |
xypron.glpk@gmx.de | 2fd945f | 2017-07-18 20:17:17 +0200 | [diff] [blame] | 1005 | static efi_status_t EFIAPI efi_check_event(struct efi_event *event) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1006 | { |
| 1007 | EFI_ENTRY("%p", event); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1008 | efi_timer_check(); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1009 | if (efi_is_event(event) != EFI_SUCCESS || |
| 1010 | event->type & EVT_NOTIFY_SIGNAL) |
| 1011 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 1012 | if (!event->is_signaled) |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1013 | efi_queue_event(event); |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1014 | if (event->is_signaled) { |
| 1015 | event->is_signaled = false; |
| 1016 | return EFI_EXIT(EFI_SUCCESS); |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 1017 | } |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1018 | return EFI_EXIT(EFI_NOT_READY); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1019 | } |
| 1020 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1021 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1022 | * efi_search_obj() - find the internal EFI object for a handle |
| 1023 | * @handle: handle to find |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1024 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1025 | * Return: EFI object |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1026 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1027 | struct efi_object *efi_search_obj(const efi_handle_t handle) |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1028 | { |
Heinrich Schuchardt | 1b68153 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1029 | struct efi_object *efiobj; |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1030 | |
Heinrich Schuchardt | ec163fa | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 1031 | if (!handle) |
| 1032 | return NULL; |
| 1033 | |
Heinrich Schuchardt | 1b68153 | 2017-11-06 21:17:50 +0100 | [diff] [blame] | 1034 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1035 | if (efiobj == handle) |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1036 | return efiobj; |
| 1037 | } |
Heinrich Schuchardt | 7b9f8ad | 2017-10-18 18:13:03 +0200 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
| 1040 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1041 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1042 | * efi_open_protocol_info_entry() - create open protocol info entry and add it |
| 1043 | * to a protocol |
| 1044 | * @handler: handler of a protocol |
Heinrich Schuchardt | fe1599d | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1045 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1046 | * Return: open protocol info entry |
Heinrich Schuchardt | fe1599d | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1047 | */ |
| 1048 | static struct efi_open_protocol_info_entry *efi_create_open_info( |
| 1049 | struct efi_handler *handler) |
| 1050 | { |
| 1051 | struct efi_open_protocol_info_item *item; |
| 1052 | |
| 1053 | item = calloc(1, sizeof(struct efi_open_protocol_info_item)); |
| 1054 | if (!item) |
| 1055 | return NULL; |
| 1056 | /* Append the item to the open protocol info list. */ |
| 1057 | list_add_tail(&item->link, &handler->open_infos); |
| 1058 | |
| 1059 | return &item->info; |
| 1060 | } |
| 1061 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1062 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1063 | * efi_delete_open_info() - remove an open protocol info entry from a protocol |
| 1064 | * @item: open protocol info entry to delete |
Heinrich Schuchardt | fe1599d | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1065 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1066 | * Return: status code |
Heinrich Schuchardt | fe1599d | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1067 | */ |
| 1068 | static efi_status_t efi_delete_open_info( |
| 1069 | struct efi_open_protocol_info_item *item) |
| 1070 | { |
| 1071 | list_del(&item->link); |
| 1072 | free(item); |
| 1073 | return EFI_SUCCESS; |
| 1074 | } |
| 1075 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1076 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1077 | * efi_add_protocol() - install new protocol on a handle |
| 1078 | * @handle: handle on which the protocol shall be installed |
| 1079 | * @protocol: GUID of the protocol to be installed |
| 1080 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | 3f79a2b | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1081 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1082 | * Return: status code |
Heinrich Schuchardt | 3f79a2b | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1083 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1084 | efi_status_t efi_add_protocol(const efi_handle_t handle, |
| 1085 | const efi_guid_t *protocol, |
Heinrich Schuchardt | 3f79a2b | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1086 | void *protocol_interface) |
| 1087 | { |
| 1088 | struct efi_object *efiobj; |
| 1089 | struct efi_handler *handler; |
| 1090 | efi_status_t ret; |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1091 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | 3f79a2b | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1092 | |
| 1093 | efiobj = efi_search_obj(handle); |
| 1094 | if (!efiobj) |
| 1095 | return EFI_INVALID_PARAMETER; |
| 1096 | ret = efi_search_protocol(handle, protocol, NULL); |
| 1097 | if (ret != EFI_NOT_FOUND) |
| 1098 | return EFI_INVALID_PARAMETER; |
| 1099 | handler = calloc(1, sizeof(struct efi_handler)); |
| 1100 | if (!handler) |
| 1101 | return EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1102 | handler->guid = protocol; |
| 1103 | handler->protocol_interface = protocol_interface; |
Heinrich Schuchardt | fe1599d | 2018-01-11 08:15:57 +0100 | [diff] [blame] | 1104 | INIT_LIST_HEAD(&handler->open_infos); |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1105 | list_add_tail(&handler->link, &efiobj->protocols); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* Notify registered events */ |
| 1108 | list_for_each_entry(event, &efi_register_notify_events, link) { |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1109 | if (!guidcmp(protocol, &event->protocol)) { |
| 1110 | struct efi_protocol_notification *notif; |
| 1111 | |
| 1112 | notif = calloc(1, sizeof(*notif)); |
| 1113 | if (!notif) { |
| 1114 | list_del(&handler->link); |
| 1115 | free(handler); |
| 1116 | return EFI_OUT_OF_RESOURCES; |
| 1117 | } |
| 1118 | notif->handle = handle; |
| 1119 | list_add_tail(¬if->link, &event->handles); |
Heinrich Schuchardt | daa3f84 | 2019-06-07 07:43:24 +0200 | [diff] [blame] | 1120 | event->event->is_signaled = false; |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1121 | efi_signal_event(event->event); |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1122 | } |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1123 | } |
| 1124 | |
Heinrich Schuchardt | d550414 | 2018-01-11 08:16:01 +0100 | [diff] [blame] | 1125 | if (!guidcmp(&efi_guid_device_path, protocol)) |
| 1126 | EFI_PRINT("installed device path '%pD'\n", protocol_interface); |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 1127 | return EFI_SUCCESS; |
Heinrich Schuchardt | 3f79a2b | 2017-10-26 19:25:53 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1130 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1131 | * efi_install_protocol_interface() - install protocol interface |
| 1132 | * @handle: handle on which the protocol shall be installed |
| 1133 | * @protocol: GUID of the protocol to be installed |
| 1134 | * @protocol_interface_type: type of the interface to be installed, |
| 1135 | * always EFI_NATIVE_INTERFACE |
| 1136 | * @protocol_interface: interface of the protocol implementation |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1137 | * |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1138 | * This function implements the InstallProtocolInterface service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1139 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1140 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1141 | * details. |
| 1142 | * |
| 1143 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1144 | */ |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1145 | static efi_status_t EFIAPI efi_install_protocol_interface( |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 1146 | efi_handle_t *handle, const efi_guid_t *protocol, |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1147 | int protocol_interface_type, void *protocol_interface) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1148 | { |
xypron.glpk@gmx.de | e0549f8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1149 | efi_status_t r; |
| 1150 | |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1151 | EFI_ENTRY("%p, %pUl, %d, %p", handle, protocol, protocol_interface_type, |
| 1152 | protocol_interface); |
| 1153 | |
xypron.glpk@gmx.de | e0549f8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1154 | if (!handle || !protocol || |
| 1155 | protocol_interface_type != EFI_NATIVE_INTERFACE) { |
| 1156 | r = EFI_INVALID_PARAMETER; |
| 1157 | goto out; |
| 1158 | } |
| 1159 | |
| 1160 | /* Create new handle if requested. */ |
| 1161 | if (!*handle) { |
Heinrich Schuchardt | 3cc6e3f | 2017-08-27 00:51:09 +0200 | [diff] [blame] | 1162 | r = efi_create_handle(handle); |
| 1163 | if (r != EFI_SUCCESS) |
| 1164 | goto out; |
Heinrich Schuchardt | 529886a | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1165 | EFI_PRINT("new handle %p\n", *handle); |
Heinrich Schuchardt | af1408e | 2017-10-26 19:25:43 +0200 | [diff] [blame] | 1166 | } else { |
Heinrich Schuchardt | 529886a | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 1167 | EFI_PRINT("handle %p\n", *handle); |
xypron.glpk@gmx.de | e0549f8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1168 | } |
Heinrich Schuchardt | 1202530 | 2017-10-26 19:25:54 +0200 | [diff] [blame] | 1169 | /* Add new protocol */ |
| 1170 | r = efi_add_protocol(*handle, protocol, protocol_interface); |
xypron.glpk@gmx.de | e0549f8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1171 | out: |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 1172 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1173 | } |
xypron.glpk@gmx.de | e0549f8 | 2017-07-11 22:06:16 +0200 | [diff] [blame] | 1174 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1175 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1176 | * efi_get_drivers() - get all drivers associated to a controller |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1177 | * @handle: handle of the controller |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1178 | * @protocol: protocol GUID (optional) |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1179 | * @number_of_drivers: number of child controllers |
| 1180 | * @driver_handle_buffer: handles of the the drivers |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1181 | * |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1182 | * The allocated buffer has to be freed with free(). |
| 1183 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1184 | * Return: status code |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1185 | */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1186 | static efi_status_t efi_get_drivers(efi_handle_t handle, |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1187 | const efi_guid_t *protocol, |
| 1188 | efi_uintn_t *number_of_drivers, |
| 1189 | efi_handle_t **driver_handle_buffer) |
| 1190 | { |
| 1191 | struct efi_handler *handler; |
| 1192 | struct efi_open_protocol_info_item *item; |
| 1193 | efi_uintn_t count = 0, i; |
| 1194 | bool duplicate; |
| 1195 | |
| 1196 | /* Count all driver associations */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1197 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1198 | if (protocol && guidcmp(handler->guid, protocol)) |
| 1199 | continue; |
| 1200 | list_for_each_entry(item, &handler->open_infos, link) { |
| 1201 | if (item->info.attributes & |
| 1202 | EFI_OPEN_PROTOCOL_BY_DRIVER) |
| 1203 | ++count; |
| 1204 | } |
| 1205 | } |
Heinrich Schuchardt | 66ca24a | 2019-06-02 01:43:33 +0200 | [diff] [blame] | 1206 | *number_of_drivers = 0; |
| 1207 | if (!count) { |
| 1208 | *driver_handle_buffer = NULL; |
| 1209 | return EFI_SUCCESS; |
| 1210 | } |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1211 | /* |
| 1212 | * Create buffer. In case of duplicate driver assignments the buffer |
| 1213 | * will be too large. But that does not harm. |
| 1214 | */ |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1215 | *driver_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
| 1216 | if (!*driver_handle_buffer) |
| 1217 | return EFI_OUT_OF_RESOURCES; |
| 1218 | /* Collect unique driver handles */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1219 | list_for_each_entry(handler, &handle->protocols, link) { |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1220 | if (protocol && guidcmp(handler->guid, protocol)) |
| 1221 | continue; |
| 1222 | list_for_each_entry(item, &handler->open_infos, link) { |
| 1223 | if (item->info.attributes & |
| 1224 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 1225 | /* Check this is a new driver */ |
| 1226 | duplicate = false; |
| 1227 | for (i = 0; i < *number_of_drivers; ++i) { |
| 1228 | if ((*driver_handle_buffer)[i] == |
| 1229 | item->info.agent_handle) |
| 1230 | duplicate = true; |
| 1231 | } |
| 1232 | /* Copy handle to buffer */ |
| 1233 | if (!duplicate) { |
| 1234 | i = (*number_of_drivers)++; |
| 1235 | (*driver_handle_buffer)[i] = |
| 1236 | item->info.agent_handle; |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | return EFI_SUCCESS; |
| 1242 | } |
| 1243 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1244 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1245 | * efi_disconnect_all_drivers() - disconnect all drivers from a controller |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1246 | * @handle: handle of the controller |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1247 | * @protocol: protocol GUID (optional) |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1248 | * @child_handle: handle of the child to destroy |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1249 | * |
| 1250 | * This function implements the DisconnectController service. |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1251 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1252 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1253 | * details. |
| 1254 | * |
| 1255 | * Return: status code |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1256 | */ |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1257 | static efi_status_t efi_disconnect_all_drivers |
| 1258 | (efi_handle_t handle, |
| 1259 | const efi_guid_t *protocol, |
| 1260 | efi_handle_t child_handle) |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1261 | { |
| 1262 | efi_uintn_t number_of_drivers; |
| 1263 | efi_handle_t *driver_handle_buffer; |
| 1264 | efi_status_t r, ret; |
| 1265 | |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1266 | ret = efi_get_drivers(handle, protocol, &number_of_drivers, |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1267 | &driver_handle_buffer); |
| 1268 | if (ret != EFI_SUCCESS) |
| 1269 | return ret; |
Heinrich Schuchardt | 66ca24a | 2019-06-02 01:43:33 +0200 | [diff] [blame] | 1270 | if (!number_of_drivers) |
| 1271 | return EFI_SUCCESS; |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1272 | ret = EFI_NOT_FOUND; |
| 1273 | while (number_of_drivers) { |
| 1274 | r = EFI_CALL(efi_disconnect_controller( |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1275 | handle, |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 1276 | driver_handle_buffer[--number_of_drivers], |
| 1277 | child_handle)); |
| 1278 | if (r == EFI_SUCCESS) |
| 1279 | ret = r; |
| 1280 | } |
| 1281 | free(driver_handle_buffer); |
| 1282 | return ret; |
| 1283 | } |
| 1284 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1285 | /** |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1286 | * efi_uninstall_protocol() - uninstall protocol interface |
| 1287 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1288 | * @handle: handle from which the protocol shall be removed |
| 1289 | * @protocol: GUID of the protocol to be removed |
| 1290 | * @protocol_interface: interface to be removed |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1291 | * |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1292 | * This function DOES NOT delete a handle without installed protocol. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1293 | * |
| 1294 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1295 | */ |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1296 | static efi_status_t efi_uninstall_protocol |
| 1297 | (efi_handle_t handle, const efi_guid_t *protocol, |
| 1298 | void *protocol_interface) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1299 | { |
Heinrich Schuchardt | ad97373 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1300 | struct efi_object *efiobj; |
Heinrich Schuchardt | 5810511 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1301 | struct efi_handler *handler; |
Heinrich Schuchardt | ad97373 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1302 | struct efi_open_protocol_info_item *item; |
| 1303 | struct efi_open_protocol_info_item *pos; |
Heinrich Schuchardt | 5810511 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1304 | efi_status_t r; |
xypron.glpk@gmx.de | 4b6ed0d | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1305 | |
Heinrich Schuchardt | ad97373 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1306 | /* Check handle */ |
| 1307 | efiobj = efi_search_obj(handle); |
| 1308 | if (!efiobj) { |
xypron.glpk@gmx.de | 4b6ed0d | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1309 | r = EFI_INVALID_PARAMETER; |
| 1310 | goto out; |
| 1311 | } |
Heinrich Schuchardt | 5810511 | 2017-10-26 19:25:56 +0200 | [diff] [blame] | 1312 | /* Find the protocol on the handle */ |
| 1313 | r = efi_search_protocol(handle, protocol, &handler); |
| 1314 | if (r != EFI_SUCCESS) |
| 1315 | goto out; |
Heinrich Schuchardt | ad97373 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1316 | /* Disconnect controllers */ |
| 1317 | efi_disconnect_all_drivers(efiobj, protocol, NULL); |
Heinrich Schuchardt | ad97373 | 2018-01-11 08:16:05 +0100 | [diff] [blame] | 1318 | /* Close protocol */ |
| 1319 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { |
| 1320 | if (item->info.attributes == |
| 1321 | EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL || |
| 1322 | item->info.attributes == EFI_OPEN_PROTOCOL_GET_PROTOCOL || |
| 1323 | item->info.attributes == EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
| 1324 | list_del(&item->link); |
| 1325 | } |
| 1326 | if (!list_empty(&handler->open_infos)) { |
| 1327 | r = EFI_ACCESS_DENIED; |
| 1328 | goto out; |
| 1329 | } |
| 1330 | r = efi_remove_protocol(handle, protocol, protocol_interface); |
xypron.glpk@gmx.de | 4b6ed0d | 2017-07-11 22:06:17 +0200 | [diff] [blame] | 1331 | out: |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 1332 | return r; |
| 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * efi_uninstall_protocol_interface() - uninstall protocol interface |
| 1337 | * @handle: handle from which the protocol shall be removed |
| 1338 | * @protocol: GUID of the protocol to be removed |
| 1339 | * @protocol_interface: interface to be removed |
| 1340 | * |
| 1341 | * This function implements the UninstallProtocolInterface service. |
| 1342 | * |
| 1343 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1344 | * details. |
| 1345 | * |
| 1346 | * Return: status code |
| 1347 | */ |
| 1348 | static efi_status_t EFIAPI efi_uninstall_protocol_interface |
| 1349 | (efi_handle_t handle, const efi_guid_t *protocol, |
| 1350 | void *protocol_interface) |
| 1351 | { |
| 1352 | efi_status_t ret; |
| 1353 | |
| 1354 | EFI_ENTRY("%p, %pUl, %p", handle, protocol, protocol_interface); |
| 1355 | |
| 1356 | ret = efi_uninstall_protocol(handle, protocol, protocol_interface); |
| 1357 | if (ret != EFI_SUCCESS) |
| 1358 | goto out; |
| 1359 | |
| 1360 | /* If the last protocol has been removed, delete the handle. */ |
| 1361 | if (list_empty(&handle->protocols)) { |
| 1362 | list_del(&handle->link); |
| 1363 | free(handle); |
| 1364 | } |
| 1365 | out: |
| 1366 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1367 | } |
| 1368 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1369 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1370 | * efi_register_protocol_notify() - register an event for notification when a |
| 1371 | * protocol is installed. |
| 1372 | * @protocol: GUID of the protocol whose installation shall be notified |
| 1373 | * @event: event to be signaled upon installation of the protocol |
| 1374 | * @registration: key for retrieving the registration information |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1375 | * |
| 1376 | * This function implements the RegisterProtocolNotify service. |
| 1377 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 1378 | * for details. |
| 1379 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1380 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1381 | */ |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1382 | static efi_status_t EFIAPI efi_register_protocol_notify( |
| 1383 | const efi_guid_t *protocol, |
| 1384 | struct efi_event *event, |
| 1385 | void **registration) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1386 | { |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1387 | struct efi_register_notify_event *item; |
| 1388 | efi_status_t ret = EFI_SUCCESS; |
| 1389 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 1390 | EFI_ENTRY("%pUl, %p, %p", protocol, event, registration); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1391 | |
| 1392 | if (!protocol || !event || !registration) { |
| 1393 | ret = EFI_INVALID_PARAMETER; |
| 1394 | goto out; |
| 1395 | } |
| 1396 | |
| 1397 | item = calloc(1, sizeof(struct efi_register_notify_event)); |
| 1398 | if (!item) { |
| 1399 | ret = EFI_OUT_OF_RESOURCES; |
| 1400 | goto out; |
| 1401 | } |
| 1402 | |
| 1403 | item->event = event; |
Sughosh Ganu | 61e42d9 | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1404 | guidcpy(&item->protocol, protocol); |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1405 | INIT_LIST_HEAD(&item->handles); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1406 | |
| 1407 | list_add_tail(&item->link, &efi_register_notify_events); |
| 1408 | |
| 1409 | *registration = item; |
| 1410 | out: |
| 1411 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1412 | } |
| 1413 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1414 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1415 | * efi_search() - determine if an EFI handle implements a protocol |
Heinrich Schuchardt | 6631ca5 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 1416 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1417 | * @search_type: selection criterion |
| 1418 | * @protocol: GUID of the protocol |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1419 | * @handle: handle |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1420 | * |
| 1421 | * See the documentation of the LocateHandle service in the UEFI specification. |
| 1422 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1423 | * Return: 0 if the handle implements the protocol |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1424 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1425 | static int efi_search(enum efi_locate_search_type search_type, |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1426 | const efi_guid_t *protocol, efi_handle_t handle) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1427 | { |
Heinrich Schuchardt | 42cf124 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1428 | efi_status_t ret; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1429 | |
| 1430 | switch (search_type) { |
Heinrich Schuchardt | 9f0770f | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1431 | case ALL_HANDLES: |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1432 | return 0; |
Heinrich Schuchardt | 9f0770f | 2017-11-06 21:17:42 +0100 | [diff] [blame] | 1433 | case BY_PROTOCOL: |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 1434 | ret = efi_search_protocol(handle, protocol, NULL); |
Heinrich Schuchardt | 42cf124 | 2017-10-26 19:25:55 +0200 | [diff] [blame] | 1435 | return (ret != EFI_SUCCESS); |
| 1436 | default: |
| 1437 | /* Invalid search type */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1438 | return -1; |
| 1439 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1442 | /** |
Heinrich Schuchardt | b8abd74 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1443 | * efi_check_register_notify_event() - check if registration key is valid |
| 1444 | * |
| 1445 | * Check that a pointer is a valid registration key as returned by |
| 1446 | * RegisterProtocolNotify(). |
| 1447 | * |
| 1448 | * @key: registration key |
| 1449 | * Return: valid registration key or NULL |
| 1450 | */ |
| 1451 | static struct efi_register_notify_event *efi_check_register_notify_event |
| 1452 | (void *key) |
| 1453 | { |
| 1454 | struct efi_register_notify_event *event; |
| 1455 | |
| 1456 | list_for_each_entry(event, &efi_register_notify_events, link) { |
| 1457 | if (event == (struct efi_register_notify_event *)key) |
| 1458 | return event; |
| 1459 | } |
| 1460 | return NULL; |
| 1461 | } |
| 1462 | |
| 1463 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1464 | * efi_locate_handle() - locate handles implementing a protocol |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1465 | * |
| 1466 | * @search_type: selection criterion |
| 1467 | * @protocol: GUID of the protocol |
| 1468 | * @search_key: registration key |
| 1469 | * @buffer_size: size of the buffer to receive the handles in bytes |
| 1470 | * @buffer: buffer to receive the relevant handles |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1471 | * |
| 1472 | * This function is meant for U-Boot internal calls. For the API implementation |
| 1473 | * of the LocateHandle service see efi_locate_handle_ext. |
| 1474 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1475 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1476 | */ |
xypron.glpk@gmx.de | ebf199b | 2017-08-09 20:55:00 +0200 | [diff] [blame] | 1477 | static efi_status_t efi_locate_handle( |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1478 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1479 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1480 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1481 | { |
Heinrich Schuchardt | caf864e | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1482 | struct efi_object *efiobj; |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1483 | efi_uintn_t size = 0; |
Heinrich Schuchardt | b8abd74 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1484 | struct efi_register_notify_event *event; |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1485 | struct efi_protocol_notification *handle = NULL; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1486 | |
Heinrich Schuchardt | caf864e | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1487 | /* Check parameters */ |
| 1488 | switch (search_type) { |
| 1489 | case ALL_HANDLES: |
| 1490 | break; |
| 1491 | case BY_REGISTER_NOTIFY: |
| 1492 | if (!search_key) |
| 1493 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1494 | /* Check that the registration key is valid */ |
Heinrich Schuchardt | b8abd74 | 2019-05-29 07:46:33 +0200 | [diff] [blame] | 1495 | event = efi_check_register_notify_event(search_key); |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1496 | if (!event) |
| 1497 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | ab15d41 | 2019-05-04 17:27:54 +0200 | [diff] [blame] | 1498 | break; |
Heinrich Schuchardt | caf864e | 2017-11-06 21:17:49 +0100 | [diff] [blame] | 1499 | case BY_PROTOCOL: |
| 1500 | if (!protocol) |
| 1501 | return EFI_INVALID_PARAMETER; |
| 1502 | break; |
| 1503 | default: |
| 1504 | return EFI_INVALID_PARAMETER; |
| 1505 | } |
| 1506 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1507 | /* Count how much space we need */ |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1508 | if (search_type == BY_REGISTER_NOTIFY) { |
| 1509 | if (list_empty(&event->handles)) |
| 1510 | return EFI_NOT_FOUND; |
| 1511 | handle = list_first_entry(&event->handles, |
| 1512 | struct efi_protocol_notification, |
| 1513 | link); |
| 1514 | efiobj = handle->handle; |
| 1515 | size += sizeof(void *); |
| 1516 | } else { |
| 1517 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 1518 | if (!efi_search(search_type, protocol, efiobj)) |
| 1519 | size += sizeof(void *); |
| 1520 | } |
| 1521 | if (size == 0) |
| 1522 | return EFI_NOT_FOUND; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1523 | } |
| 1524 | |
Heinrich Schuchardt | 8dfb5e6 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1525 | if (!buffer_size) |
| 1526 | return EFI_INVALID_PARAMETER; |
| 1527 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1528 | if (*buffer_size < size) { |
| 1529 | *buffer_size = size; |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1530 | return EFI_BUFFER_TOO_SMALL; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1531 | } |
| 1532 | |
Rob Clark | 796a78c | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1533 | *buffer_size = size; |
Heinrich Schuchardt | 8dfb5e6 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1534 | |
Heinrich Schuchardt | 0a84319 | 2019-05-10 19:03:49 +0200 | [diff] [blame] | 1535 | /* The buffer size is sufficient but there is no buffer */ |
Heinrich Schuchardt | 8dfb5e6 | 2019-05-04 17:37:32 +0200 | [diff] [blame] | 1536 | if (!buffer) |
| 1537 | return EFI_INVALID_PARAMETER; |
Rob Clark | 796a78c | 2017-08-06 14:10:07 -0400 | [diff] [blame] | 1538 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1539 | /* Then fill the array */ |
Heinrich Schuchardt | f09cea3 | 2019-05-21 18:19:01 +0200 | [diff] [blame] | 1540 | if (search_type == BY_REGISTER_NOTIFY) { |
| 1541 | *buffer = efiobj; |
| 1542 | list_del(&handle->link); |
| 1543 | } else { |
| 1544 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 1545 | if (!efi_search(search_type, protocol, efiobj)) |
| 1546 | *buffer++ = efiobj; |
| 1547 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1550 | return EFI_SUCCESS; |
| 1551 | } |
| 1552 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1553 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1554 | * efi_locate_handle_ext() - locate handles implementing a protocol. |
| 1555 | * @search_type: selection criterion |
| 1556 | * @protocol: GUID of the protocol |
| 1557 | * @search_key: registration key |
| 1558 | * @buffer_size: size of the buffer to receive the handles in bytes |
| 1559 | * @buffer: buffer to receive the relevant handles |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1560 | * |
| 1561 | * This function implements the LocateHandle service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1562 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1563 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1564 | * details. |
| 1565 | * |
| 1566 | * Return: 0 if the handle implements the protocol |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1567 | */ |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1568 | static efi_status_t EFIAPI efi_locate_handle_ext( |
| 1569 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 1570 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 1571 | efi_uintn_t *buffer_size, efi_handle_t *buffer) |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1572 | { |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 1573 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 1574 | buffer_size, buffer); |
| 1575 | |
| 1576 | return EFI_EXIT(efi_locate_handle(search_type, protocol, search_key, |
| 1577 | buffer_size, buffer)); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1580 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1581 | * efi_remove_configuration_table() - collapses configuration table entries, |
| 1582 | * removing index i |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1583 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1584 | * @i: index of the table entry to be removed |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1585 | */ |
Alexander Graf | d98cdf6 | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1586 | static void efi_remove_configuration_table(int i) |
| 1587 | { |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1588 | struct efi_configuration_table *this = &systab.tables[i]; |
| 1589 | struct efi_configuration_table *next = &systab.tables[i + 1]; |
| 1590 | struct efi_configuration_table *end = &systab.tables[systab.nr_tables]; |
Alexander Graf | d98cdf6 | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1591 | |
| 1592 | memmove(this, next, (ulong)end - (ulong)next); |
| 1593 | systab.nr_tables--; |
| 1594 | } |
| 1595 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1596 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1597 | * efi_install_configuration_table() - adds, updates, or removes a |
| 1598 | * configuration table |
| 1599 | * @guid: GUID of the installed table |
| 1600 | * @table: table to be installed |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1601 | * |
| 1602 | * This function is used for internal calls. For the API implementation of the |
| 1603 | * InstallConfigurationTable service see efi_install_configuration_table_ext. |
| 1604 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1605 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1606 | */ |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 1607 | efi_status_t efi_install_configuration_table(const efi_guid_t *guid, |
| 1608 | void *table) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1609 | { |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1610 | struct efi_event *evt; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1611 | int i; |
| 1612 | |
Heinrich Schuchardt | eb68b4e | 2018-02-18 00:08:00 +0100 | [diff] [blame] | 1613 | if (!guid) |
| 1614 | return EFI_INVALID_PARAMETER; |
| 1615 | |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1616 | /* Check for GUID override */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1617 | for (i = 0; i < systab.nr_tables; i++) { |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1618 | if (!guidcmp(guid, &systab.tables[i].guid)) { |
Alexander Graf | d98cdf6 | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1619 | if (table) |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1620 | systab.tables[i].table = table; |
Alexander Graf | d98cdf6 | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1621 | else |
| 1622 | efi_remove_configuration_table(i); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1623 | goto out; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | |
Alexander Graf | d98cdf6 | 2017-07-26 13:41:04 +0200 | [diff] [blame] | 1627 | if (!table) |
| 1628 | return EFI_NOT_FOUND; |
| 1629 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1630 | /* No override, check for overflow */ |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1631 | if (i >= EFI_MAX_CONFIGURATION_TABLES) |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1632 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1633 | |
| 1634 | /* Add a new entry */ |
Sughosh Ganu | 61e42d9 | 2019-12-29 00:01:04 +0530 | [diff] [blame] | 1635 | guidcpy(&systab.tables[i].guid, guid); |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 1636 | systab.tables[i].table = table; |
Alexander Graf | aba5e91 | 2016-08-19 01:23:30 +0200 | [diff] [blame] | 1637 | systab.nr_tables = i + 1; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1638 | |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1639 | out: |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 1640 | /* systab.nr_tables may have changed. So we need to update the CRC32 */ |
Heinrich Schuchardt | 55d8ee3 | 2018-07-07 15:36:05 +0200 | [diff] [blame] | 1641 | efi_update_table_header_crc32(&systab.hdr); |
| 1642 | |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1643 | /* Notify that the configuration table was changed */ |
| 1644 | list_for_each_entry(evt, &efi_events, link) { |
| 1645 | if (evt->group && !guidcmp(evt->group, guid)) { |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1646 | efi_signal_event(evt); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1647 | break; |
| 1648 | } |
| 1649 | } |
| 1650 | |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1651 | return EFI_SUCCESS; |
| 1652 | } |
| 1653 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1654 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1655 | * efi_install_configuration_table_ex() - Adds, updates, or removes a |
| 1656 | * configuration table. |
| 1657 | * @guid: GUID of the installed table |
| 1658 | * @table: table to be installed |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1659 | * |
| 1660 | * This function implements the InstallConfigurationTable service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1661 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1662 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1663 | * details. |
| 1664 | * |
| 1665 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1666 | */ |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1667 | static efi_status_t EFIAPI efi_install_configuration_table_ext(efi_guid_t *guid, |
| 1668 | void *table) |
| 1669 | { |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 1670 | EFI_ENTRY("%pUl, %p", guid, table); |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 1671 | return EFI_EXIT(efi_install_configuration_table(guid, table)); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1672 | } |
| 1673 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1674 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1675 | * efi_setup_loaded_image() - initialize a loaded image |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1676 | * |
| 1677 | * Initialize a loaded_image_info and loaded_image_info object with correct |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1678 | * protocols, boot-device, etc. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1679 | * |
Heinrich Schuchardt | 6631ca5 | 2019-07-14 11:05:34 +0200 | [diff] [blame] | 1680 | * In case of an error \*handle_ptr and \*info_ptr are set to NULL and an error |
Heinrich Schuchardt | 16112f9 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1681 | * code is returned. |
| 1682 | * |
| 1683 | * @device_path: device path of the loaded image |
| 1684 | * @file_path: file path of the loaded image |
| 1685 | * @handle_ptr: handle of the loaded image |
| 1686 | * @info_ptr: loaded image protocol |
| 1687 | * Return: status code |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1688 | */ |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1689 | efi_status_t efi_setup_loaded_image(struct efi_device_path *device_path, |
| 1690 | struct efi_device_path *file_path, |
| 1691 | struct efi_loaded_image_obj **handle_ptr, |
| 1692 | struct efi_loaded_image **info_ptr) |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1693 | { |
Heinrich Schuchardt | 48b6623 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1694 | efi_status_t ret; |
Heinrich Schuchardt | 16112f9 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1695 | struct efi_loaded_image *info = NULL; |
| 1696 | struct efi_loaded_image_obj *obj = NULL; |
AKASHI Takahiro | bc8fc32 | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1697 | struct efi_device_path *dp; |
Heinrich Schuchardt | 16112f9 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1698 | |
| 1699 | /* In case of EFI_OUT_OF_RESOURCES avoid illegal free by caller. */ |
| 1700 | *handle_ptr = NULL; |
| 1701 | *info_ptr = NULL; |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1702 | |
| 1703 | info = calloc(1, sizeof(*info)); |
| 1704 | if (!info) |
| 1705 | return EFI_OUT_OF_RESOURCES; |
| 1706 | obj = calloc(1, sizeof(*obj)); |
| 1707 | if (!obj) { |
| 1708 | free(info); |
| 1709 | return EFI_OUT_OF_RESOURCES; |
| 1710 | } |
Heinrich Schuchardt | cd73aba | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 1711 | obj->header.type = EFI_OBJECT_TYPE_LOADED_IMAGE; |
Heinrich Schuchardt | 48b6623 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1712 | |
Heinrich Schuchardt | 44549d6 | 2017-11-26 14:05:23 +0100 | [diff] [blame] | 1713 | /* Add internal object to object list */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1714 | efi_add_handle(&obj->header); |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1715 | |
Heinrich Schuchardt | 9514731 | 2018-07-05 08:17:58 +0200 | [diff] [blame] | 1716 | info->revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1717 | info->file_path = file_path; |
Heinrich Schuchardt | 7e1effc | 2018-08-26 15:31:52 +0200 | [diff] [blame] | 1718 | info->system_table = &systab; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1719 | |
Heinrich Schuchardt | 7df5af6 | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1720 | if (device_path) { |
| 1721 | info->device_handle = efi_dp_find_obj(device_path, NULL); |
AKASHI Takahiro | bc8fc32 | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1722 | |
| 1723 | dp = efi_dp_append(device_path, file_path); |
| 1724 | if (!dp) { |
| 1725 | ret = EFI_OUT_OF_RESOURCES; |
Heinrich Schuchardt | 7df5af6 | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1726 | goto failure; |
AKASHI Takahiro | bc8fc32 | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1727 | } |
| 1728 | } else { |
| 1729 | dp = NULL; |
Heinrich Schuchardt | 7df5af6 | 2018-01-26 06:50:54 +0100 | [diff] [blame] | 1730 | } |
AKASHI Takahiro | bc8fc32 | 2019-03-27 13:40:32 +0900 | [diff] [blame] | 1731 | ret = efi_add_protocol(&obj->header, |
| 1732 | &efi_guid_loaded_image_device_path, dp); |
| 1733 | if (ret != EFI_SUCCESS) |
| 1734 | goto failure; |
Heinrich Schuchardt | 48b6623 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1735 | |
| 1736 | /* |
| 1737 | * When asking for the loaded_image interface, just |
| 1738 | * return handle which points to loaded_image_info |
| 1739 | */ |
Heinrich Schuchardt | d39646a | 2018-09-26 05:27:56 +0200 | [diff] [blame] | 1740 | ret = efi_add_protocol(&obj->header, |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1741 | &efi_guid_loaded_image, info); |
Heinrich Schuchardt | 48b6623 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1742 | if (ret != EFI_SUCCESS) |
| 1743 | goto failure; |
| 1744 | |
Heinrich Schuchardt | d5974af | 2019-03-19 18:58:58 +0100 | [diff] [blame] | 1745 | *info_ptr = info; |
| 1746 | *handle_ptr = obj; |
Heinrich Schuchardt | 16112f9 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1747 | |
Heinrich Schuchardt | 56d9288 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1748 | return ret; |
Heinrich Schuchardt | 48b6623 | 2017-10-26 19:25:58 +0200 | [diff] [blame] | 1749 | failure: |
| 1750 | printf("ERROR: Failure to install protocols for loaded image\n"); |
Heinrich Schuchardt | 16112f9 | 2019-02-06 19:41:29 +0100 | [diff] [blame] | 1751 | efi_delete_handle(&obj->header); |
| 1752 | free(info); |
Heinrich Schuchardt | 56d9288 | 2017-12-04 18:03:01 +0100 | [diff] [blame] | 1753 | return ret; |
Rob Clark | 95c5553 | 2017-09-13 18:05:33 -0400 | [diff] [blame] | 1754 | } |
| 1755 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1756 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1757 | * efi_load_image_from_path() - load an image using a file path |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1758 | * |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1759 | * Read a file into a buffer allocated as EFI_BOOT_SERVICES_DATA. It is the |
| 1760 | * callers obligation to update the memory type as needed. |
| 1761 | * |
| 1762 | * @file_path: the path of the image to load |
| 1763 | * @buffer: buffer containing the loaded image |
| 1764 | * @size: size of the loaded image |
| 1765 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1766 | */ |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 1767 | static |
Rob Clark | 9975fe9 | 2017-09-13 18:05:38 -0400 | [diff] [blame] | 1768 | efi_status_t efi_load_image_from_path(struct efi_device_path *file_path, |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1769 | void **buffer, efi_uintn_t *size) |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1770 | { |
| 1771 | struct efi_file_info *info = NULL; |
| 1772 | struct efi_file_handle *f; |
| 1773 | static efi_status_t ret; |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1774 | u64 addr; |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 1775 | efi_uintn_t bs; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1776 | |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1777 | /* In case of failure nothing is returned */ |
| 1778 | *buffer = NULL; |
| 1779 | *size = 0; |
| 1780 | |
| 1781 | /* Open file */ |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1782 | f = efi_file_from_path(file_path); |
| 1783 | if (!f) |
Heinrich Schuchardt | 2000003 | 2019-06-11 19:00:56 +0200 | [diff] [blame] | 1784 | return EFI_NOT_FOUND; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1785 | |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1786 | /* Get file size */ |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1787 | bs = 0; |
| 1788 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, |
| 1789 | &bs, info)); |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1790 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 1791 | ret = EFI_DEVICE_ERROR; |
| 1792 | goto error; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1793 | } |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1794 | |
| 1795 | info = malloc(bs); |
| 1796 | EFI_CALL(ret = f->getinfo(f, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 1797 | info)); |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1798 | if (ret != EFI_SUCCESS) |
| 1799 | goto error; |
| 1800 | |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1801 | /* |
| 1802 | * When reading the file we do not yet know if it contains an |
| 1803 | * application, a boottime driver, or a runtime driver. So here we |
| 1804 | * allocate a buffer as EFI_BOOT_SERVICES_DATA. The caller has to |
| 1805 | * update the reservation according to the image type. |
| 1806 | */ |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 1807 | bs = info->file_size; |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1808 | ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES, |
| 1809 | EFI_BOOT_SERVICES_DATA, |
| 1810 | efi_size_in_pages(bs), &addr); |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1811 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1812 | ret = EFI_OUT_OF_RESOURCES; |
| 1813 | goto error; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1814 | } |
| 1815 | |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1816 | /* Read file */ |
| 1817 | EFI_CALL(ret = f->read(f, &bs, (void *)(uintptr_t)addr)); |
| 1818 | if (ret != EFI_SUCCESS) |
| 1819 | efi_free_pages(addr, efi_size_in_pages(bs)); |
| 1820 | *buffer = (void *)(uintptr_t)addr; |
| 1821 | *size = bs; |
| 1822 | error: |
| 1823 | EFI_CALL(f->close(f)); |
| 1824 | free(info); |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1825 | return ret; |
| 1826 | } |
| 1827 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1828 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1829 | * efi_load_image() - load an EFI image into memory |
| 1830 | * @boot_policy: true for request originating from the boot manager |
| 1831 | * @parent_image: the caller's image handle |
| 1832 | * @file_path: the path of the image to load |
| 1833 | * @source_buffer: memory location from which the image is installed |
| 1834 | * @source_size: size of the memory area from which the image is installed |
| 1835 | * @image_handle: handle for the newly installed image |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1836 | * |
| 1837 | * This function implements the LoadImage service. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1838 | * |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1839 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 1840 | * for details. |
| 1841 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1842 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1843 | */ |
AKASHI Takahiro | d7e0b01 | 2019-03-05 14:53:31 +0900 | [diff] [blame] | 1844 | efi_status_t EFIAPI efi_load_image(bool boot_policy, |
| 1845 | efi_handle_t parent_image, |
| 1846 | struct efi_device_path *file_path, |
| 1847 | void *source_buffer, |
| 1848 | efi_uintn_t source_size, |
| 1849 | efi_handle_t *image_handle) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1850 | { |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1851 | struct efi_device_path *dp, *fp; |
Tom Rini | 1c3b2f4 | 2018-09-30 10:38:15 -0400 | [diff] [blame] | 1852 | struct efi_loaded_image *info = NULL; |
Heinrich Schuchardt | c982874 | 2018-09-23 17:21:51 +0200 | [diff] [blame] | 1853 | struct efi_loaded_image_obj **image_obj = |
| 1854 | (struct efi_loaded_image_obj **)image_handle; |
Heinrich Schuchardt | b9b1759 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1855 | efi_status_t ret; |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1856 | void *dest_buffer; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1857 | |
Heinrich Schuchardt | 7fb96a1 | 2018-04-03 22:29:30 +0200 | [diff] [blame] | 1858 | EFI_ENTRY("%d, %p, %pD, %p, %zd, %p", boot_policy, parent_image, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1859 | file_path, source_buffer, source_size, image_handle); |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1860 | |
Heinrich Schuchardt | e4afcb2 | 2019-06-11 18:52:33 +0200 | [diff] [blame] | 1861 | if (!image_handle || (!source_buffer && !file_path) || |
| 1862 | !efi_search_obj(parent_image) || |
| 1863 | /* The parent image handle must refer to a loaded image */ |
| 1864 | !parent_image->type) { |
Heinrich Schuchardt | 84a918e | 2019-05-05 16:55:06 +0200 | [diff] [blame] | 1865 | ret = EFI_INVALID_PARAMETER; |
| 1866 | goto error; |
| 1867 | } |
Heinrich Schuchardt | 28a4fd4 | 2018-03-07 02:40:51 +0100 | [diff] [blame] | 1868 | |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1869 | if (!source_buffer) { |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1870 | ret = efi_load_image_from_path(file_path, &dest_buffer, |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1871 | &source_size); |
Heinrich Schuchardt | b9b1759 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1872 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1873 | goto error; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1874 | } else { |
Heinrich Schuchardt | 470dfa5 | 2019-05-05 16:55:06 +0200 | [diff] [blame] | 1875 | if (!source_size) { |
| 1876 | ret = EFI_LOAD_ERROR; |
| 1877 | goto error; |
| 1878 | } |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1879 | dest_buffer = source_buffer; |
Rob Clark | 838ee4b | 2017-09-13 18:05:35 -0400 | [diff] [blame] | 1880 | } |
Heinrich Schuchardt | 1e15a9c | 2019-04-20 19:24:43 +0000 | [diff] [blame] | 1881 | /* split file_path which contains both the device and file parts */ |
| 1882 | efi_dp_split_file_path(file_path, &dp, &fp); |
Heinrich Schuchardt | 0e18f58 | 2018-12-24 09:19:07 +0100 | [diff] [blame] | 1883 | ret = efi_setup_loaded_image(dp, fp, image_obj, &info); |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1884 | if (ret == EFI_SUCCESS) |
AKASHI Takahiro | 4540dab | 2020-04-14 11:51:44 +0900 | [diff] [blame^] | 1885 | ret = efi_load_pe(*image_obj, dest_buffer, source_size, info); |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1886 | if (!source_buffer) |
| 1887 | /* Release buffer to which file was loaded */ |
| 1888 | efi_free_pages((uintptr_t)dest_buffer, |
| 1889 | efi_size_in_pages(source_size)); |
AKASHI Takahiro | 4540dab | 2020-04-14 11:51:44 +0900 | [diff] [blame^] | 1890 | if (ret == EFI_SUCCESS || ret == EFI_SECURITY_VIOLATION) { |
Heinrich Schuchardt | b0c3c34 | 2019-03-04 17:50:05 +0100 | [diff] [blame] | 1891 | info->system_table = &systab; |
| 1892 | info->parent_handle = parent_image; |
| 1893 | } else { |
| 1894 | /* The image is invalid. Release all associated resources. */ |
| 1895 | efi_delete_handle(*image_handle); |
| 1896 | *image_handle = NULL; |
| 1897 | free(info); |
| 1898 | } |
Heinrich Schuchardt | 28a4fd4 | 2018-03-07 02:40:51 +0100 | [diff] [blame] | 1899 | error: |
Heinrich Schuchardt | b9b1759 | 2017-12-04 18:03:03 +0100 | [diff] [blame] | 1900 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 1903 | /** |
Alexander Graf | f31239a | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1904 | * efi_exit_caches() - fix up caches for EFI payloads if necessary |
| 1905 | */ |
| 1906 | static void efi_exit_caches(void) |
| 1907 | { |
Heinrich Schuchardt | 6f3badb | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 1908 | #if defined(CONFIG_EFI_GRUB_ARM32_WORKAROUND) |
Alexander Graf | f31239a | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1909 | /* |
Heinrich Schuchardt | 6f3badb | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 1910 | * Boooting Linux via GRUB prior to version 2.04 fails on 32bit ARM if |
| 1911 | * caches are enabled. |
| 1912 | * |
| 1913 | * TODO: |
| 1914 | * According to the UEFI spec caches that can be managed via CP15 |
| 1915 | * operations should be enabled. Caches requiring platform information |
| 1916 | * to manage should be disabled. This should not happen in |
| 1917 | * ExitBootServices() but before invoking any UEFI binary is invoked. |
| 1918 | * |
| 1919 | * We want to keep the current workaround while GRUB prior to version |
| 1920 | * 2.04 is still in use. |
Alexander Graf | f31239a | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1921 | */ |
Heinrich Schuchardt | 6f3badb | 2019-07-22 22:04:36 +0200 | [diff] [blame] | 1922 | cleanup_before_linux(); |
Alexander Graf | f31239a | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1923 | #endif |
| 1924 | } |
| 1925 | |
| 1926 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1927 | * efi_exit_boot_services() - stop all boot services |
| 1928 | * @image_handle: handle of the loaded image |
| 1929 | * @map_key: key of the memory map |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1930 | * |
| 1931 | * This function implements the ExitBootServices service. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1932 | * |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1933 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 1934 | * for details. |
| 1935 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1936 | * All timer events are disabled. For exit boot services events the |
| 1937 | * notification function is called. The boot services are disabled in the |
| 1938 | * system table. |
Heinrich Schuchardt | cc20ed0 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 1939 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 1940 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 1941 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 1942 | static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle, |
Heinrich Schuchardt | b015ab5 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 1943 | efi_uintn_t map_key) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1944 | { |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 1945 | struct efi_event *evt, *next_event; |
Heinrich Schuchardt | 9896737 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 1946 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 152a263 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 1947 | |
Heinrich Schuchardt | b015ab5 | 2019-05-05 21:58:35 +0200 | [diff] [blame] | 1948 | EFI_ENTRY("%p, %zx", image_handle, map_key); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1949 | |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 1950 | /* Check that the caller has read the current memory map */ |
Heinrich Schuchardt | 9896737 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 1951 | if (map_key != efi_memory_map_key) { |
| 1952 | ret = EFI_INVALID_PARAMETER; |
| 1953 | goto out; |
| 1954 | } |
Heinrich Schuchardt | 1fcb7ea | 2018-07-02 12:53:55 +0200 | [diff] [blame] | 1955 | |
Heinrich Schuchardt | cc20ed0 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 1956 | /* Check if ExitBootServices has already been called */ |
| 1957 | if (!systab.boottime) |
Heinrich Schuchardt | 9896737 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 1958 | goto out; |
Heinrich Schuchardt | cc20ed0 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 1959 | |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1960 | /* Stop all timer related activities */ |
| 1961 | timers_enabled = false; |
| 1962 | |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1963 | /* Add related events to the event group */ |
| 1964 | list_for_each_entry(evt, &efi_events, link) { |
| 1965 | if (evt->type == EVT_SIGNAL_EXIT_BOOT_SERVICES) |
| 1966 | evt->group = &efi_guid_event_group_exit_boot_services; |
| 1967 | } |
Heinrich Schuchardt | 152a263 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 1968 | /* Notify that ExitBootServices is invoked. */ |
Heinrich Schuchardt | 43bce44 | 2018-02-18 15:17:50 +0100 | [diff] [blame] | 1969 | list_for_each_entry(evt, &efi_events, link) { |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1970 | if (evt->group && |
| 1971 | !guidcmp(evt->group, |
| 1972 | &efi_guid_event_group_exit_boot_services)) { |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1973 | efi_signal_event(evt); |
Heinrich Schuchardt | b095f3c | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 1974 | break; |
| 1975 | } |
Heinrich Schuchardt | 152a263 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 1976 | } |
Heinrich Schuchardt | 152a263 | 2017-09-15 10:06:18 +0200 | [diff] [blame] | 1977 | |
Heinrich Schuchardt | 7eaa900 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 1978 | /* Make sure that notification functions are not called anymore */ |
| 1979 | efi_tpl = TPL_HIGH_LEVEL; |
| 1980 | |
Heinrich Schuchardt | 29018ab | 2019-06-20 15:25:48 +0200 | [diff] [blame] | 1981 | /* Notify variable services */ |
| 1982 | efi_variables_boot_exit_notify(); |
Rob Clark | ad644e7 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 1983 | |
Heinrich Schuchardt | 14b4048 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 1984 | /* Remove all events except EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 1985 | list_for_each_entry_safe(evt, next_event, &efi_events, link) { |
| 1986 | if (evt->type != EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) |
| 1987 | list_del(&evt->link); |
| 1988 | } |
| 1989 | |
Alexander Graf | b7b8410 | 2016-11-17 01:02:57 +0100 | [diff] [blame] | 1990 | board_quiesce_devices(); |
| 1991 | |
Heinrich Schuchardt | 7f95104 | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 1992 | /* Patch out unsupported runtime function */ |
| 1993 | efi_runtime_detach(); |
| 1994 | |
Alexander Graf | f31239a | 2018-11-15 21:23:47 +0100 | [diff] [blame] | 1995 | /* Fix up caches for EFI payloads if necessary */ |
| 1996 | efi_exit_caches(); |
| 1997 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 1998 | /* This stops all lingering devices */ |
| 1999 | bootm_disable_interrupts(); |
| 2000 | |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2001 | /* Disable boot time services */ |
Heinrich Schuchardt | cc20ed0 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2002 | systab.con_in_handle = NULL; |
| 2003 | systab.con_in = NULL; |
| 2004 | systab.con_out_handle = NULL; |
| 2005 | systab.con_out = NULL; |
| 2006 | systab.stderr_handle = NULL; |
| 2007 | systab.std_err = NULL; |
| 2008 | systab.boottime = NULL; |
| 2009 | |
| 2010 | /* Recalculate CRC32 */ |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 2011 | efi_update_table_header_crc32(&systab.hdr); |
Heinrich Schuchardt | cc20ed0 | 2018-01-19 20:24:52 +0100 | [diff] [blame] | 2012 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2013 | /* Give the payload some time to boot */ |
Heinrich Schuchardt | b3d6090 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2014 | efi_set_watchdog(0); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2015 | WATCHDOG_RESET(); |
Heinrich Schuchardt | 9896737 | 2019-06-11 20:05:40 +0200 | [diff] [blame] | 2016 | out: |
| 2017 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2018 | } |
| 2019 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2020 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2021 | * efi_get_next_monotonic_count() - get next value of the counter |
| 2022 | * @count: returned value of the counter |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2023 | * |
| 2024 | * This function implements the NextMonotonicCount service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2025 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2026 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2027 | * details. |
| 2028 | * |
| 2029 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2030 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2031 | static efi_status_t EFIAPI efi_get_next_monotonic_count(uint64_t *count) |
| 2032 | { |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2033 | static uint64_t mono; |
Heinrich Schuchardt | 1344f7d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2034 | efi_status_t ret; |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2035 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2036 | EFI_ENTRY("%p", count); |
Heinrich Schuchardt | 1344f7d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2037 | if (!count) { |
| 2038 | ret = EFI_INVALID_PARAMETER; |
| 2039 | goto out; |
| 2040 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2041 | *count = mono++; |
Heinrich Schuchardt | 1344f7d | 2019-05-17 12:47:17 +0200 | [diff] [blame] | 2042 | ret = EFI_SUCCESS; |
| 2043 | out: |
| 2044 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2045 | } |
| 2046 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2047 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2048 | * efi_stall() - sleep |
| 2049 | * @microseconds: period to sleep in microseconds |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2050 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2051 | * This function implements the Stall service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2052 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2053 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2054 | * details. |
| 2055 | * |
| 2056 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2057 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2058 | static efi_status_t EFIAPI efi_stall(unsigned long microseconds) |
| 2059 | { |
Heinrich Schuchardt | 22f23db | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2060 | u64 end_tick; |
| 2061 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2062 | EFI_ENTRY("%ld", microseconds); |
Heinrich Schuchardt | 22f23db | 2019-06-02 21:12:17 +0200 | [diff] [blame] | 2063 | |
| 2064 | end_tick = get_ticks() + usec_to_tick(microseconds); |
| 2065 | while (get_ticks() < end_tick) |
| 2066 | efi_timer_check(); |
| 2067 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2068 | return EFI_EXIT(EFI_SUCCESS); |
| 2069 | } |
| 2070 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2071 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2072 | * efi_set_watchdog_timer() - reset the watchdog timer |
| 2073 | * @timeout: seconds before reset by watchdog |
| 2074 | * @watchdog_code: code to be logged when resetting |
| 2075 | * @data_size: size of buffer in bytes |
| 2076 | * @watchdog_data: buffer with data describing the reset reason |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2077 | * |
Heinrich Schuchardt | b3d6090 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2078 | * This function implements the SetWatchdogTimer service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2079 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2080 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2081 | * details. |
| 2082 | * |
| 2083 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2084 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2085 | static efi_status_t EFIAPI efi_set_watchdog_timer(unsigned long timeout, |
| 2086 | uint64_t watchdog_code, |
| 2087 | unsigned long data_size, |
| 2088 | uint16_t *watchdog_data) |
| 2089 | { |
Masahiro Yamada | dee37fc | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 2090 | EFI_ENTRY("%ld, 0x%llx, %ld, %p", timeout, watchdog_code, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2091 | data_size, watchdog_data); |
Heinrich Schuchardt | b3d6090 | 2017-10-18 18:13:04 +0200 | [diff] [blame] | 2092 | return EFI_EXIT(efi_set_watchdog(timeout)); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2095 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2096 | * efi_close_protocol() - close a protocol |
| 2097 | * @handle: handle on which the protocol shall be closed |
| 2098 | * @protocol: GUID of the protocol to close |
| 2099 | * @agent_handle: handle of the driver |
| 2100 | * @controller_handle: handle of the controller |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2101 | * |
| 2102 | * This function implements the CloseProtocol service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2103 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2104 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2105 | * details. |
| 2106 | * |
| 2107 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2108 | */ |
AKASHI Takahiro | b51ec63 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 2109 | efi_status_t EFIAPI efi_close_protocol(efi_handle_t handle, |
| 2110 | const efi_guid_t *protocol, |
| 2111 | efi_handle_t agent_handle, |
| 2112 | efi_handle_t controller_handle) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2113 | { |
Heinrich Schuchardt | 3b8a489 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2114 | struct efi_handler *handler; |
| 2115 | struct efi_open_protocol_info_item *item; |
| 2116 | struct efi_open_protocol_info_item *pos; |
| 2117 | efi_status_t r; |
| 2118 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2119 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, agent_handle, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2120 | controller_handle); |
Heinrich Schuchardt | 3b8a489 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2121 | |
Heinrich Schuchardt | ec163fa | 2019-05-05 10:37:51 +0200 | [diff] [blame] | 2122 | if (!efi_search_obj(agent_handle) || |
| 2123 | (controller_handle && !efi_search_obj(controller_handle))) { |
Heinrich Schuchardt | 3b8a489 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2124 | r = EFI_INVALID_PARAMETER; |
| 2125 | goto out; |
| 2126 | } |
| 2127 | r = efi_search_protocol(handle, protocol, &handler); |
| 2128 | if (r != EFI_SUCCESS) |
| 2129 | goto out; |
| 2130 | |
| 2131 | r = EFI_NOT_FOUND; |
| 2132 | list_for_each_entry_safe(item, pos, &handler->open_infos, link) { |
| 2133 | if (item->info.agent_handle == agent_handle && |
| 2134 | item->info.controller_handle == controller_handle) { |
| 2135 | efi_delete_open_info(item); |
| 2136 | r = EFI_SUCCESS; |
Heinrich Schuchardt | 3b8a489 | 2018-01-11 08:15:59 +0100 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | out: |
| 2140 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2143 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2144 | * efi_open_protocol_information() - provide information about then open status |
| 2145 | * of a protocol on a handle |
| 2146 | * @handle: handle for which the information shall be retrieved |
| 2147 | * @protocol: GUID of the protocol |
| 2148 | * @entry_buffer: buffer to receive the open protocol information |
| 2149 | * @entry_count: number of entries available in the buffer |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2150 | * |
| 2151 | * This function implements the OpenProtocolInformation service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2152 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2153 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2154 | * details. |
| 2155 | * |
| 2156 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2157 | */ |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 2158 | static efi_status_t EFIAPI efi_open_protocol_information( |
| 2159 | efi_handle_t handle, const efi_guid_t *protocol, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2160 | struct efi_open_protocol_info_entry **entry_buffer, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2161 | efi_uintn_t *entry_count) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2162 | { |
Heinrich Schuchardt | e3fbbc3 | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2163 | unsigned long buffer_size; |
| 2164 | unsigned long count; |
| 2165 | struct efi_handler *handler; |
| 2166 | struct efi_open_protocol_info_item *item; |
| 2167 | efi_status_t r; |
| 2168 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2169 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, entry_buffer, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2170 | entry_count); |
Heinrich Schuchardt | e3fbbc3 | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2171 | |
| 2172 | /* Check parameters */ |
| 2173 | if (!entry_buffer) { |
| 2174 | r = EFI_INVALID_PARAMETER; |
| 2175 | goto out; |
| 2176 | } |
| 2177 | r = efi_search_protocol(handle, protocol, &handler); |
| 2178 | if (r != EFI_SUCCESS) |
| 2179 | goto out; |
| 2180 | |
| 2181 | /* Count entries */ |
| 2182 | count = 0; |
| 2183 | list_for_each_entry(item, &handler->open_infos, link) { |
| 2184 | if (item->info.open_count) |
| 2185 | ++count; |
| 2186 | } |
| 2187 | *entry_count = count; |
| 2188 | *entry_buffer = NULL; |
| 2189 | if (!count) { |
| 2190 | r = EFI_SUCCESS; |
| 2191 | goto out; |
| 2192 | } |
| 2193 | |
| 2194 | /* Copy entries */ |
| 2195 | buffer_size = count * sizeof(struct efi_open_protocol_info_entry); |
Heinrich Schuchardt | eee6530 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2196 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | e3fbbc3 | 2018-01-11 08:16:00 +0100 | [diff] [blame] | 2197 | (void **)entry_buffer); |
| 2198 | if (r != EFI_SUCCESS) |
| 2199 | goto out; |
| 2200 | list_for_each_entry_reverse(item, &handler->open_infos, link) { |
| 2201 | if (item->info.open_count) |
| 2202 | (*entry_buffer)[--count] = item->info; |
| 2203 | } |
| 2204 | out: |
| 2205 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2208 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2209 | * efi_protocols_per_handle() - get protocols installed on a handle |
| 2210 | * @handle: handle for which the information is retrieved |
| 2211 | * @protocol_buffer: buffer with protocol GUIDs |
| 2212 | * @protocol_buffer_count: number of entries in the buffer |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2213 | * |
| 2214 | * This function implements the ProtocolsPerHandleService. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2215 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2216 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2217 | * details. |
| 2218 | * |
| 2219 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2220 | */ |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2221 | static efi_status_t EFIAPI efi_protocols_per_handle( |
| 2222 | efi_handle_t handle, efi_guid_t ***protocol_buffer, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2223 | efi_uintn_t *protocol_buffer_count) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2224 | { |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2225 | unsigned long buffer_size; |
| 2226 | struct efi_object *efiobj; |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2227 | struct list_head *protocol_handle; |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2228 | efi_status_t r; |
| 2229 | |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2230 | EFI_ENTRY("%p, %p, %p", handle, protocol_buffer, |
| 2231 | protocol_buffer_count); |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2232 | |
| 2233 | if (!handle || !protocol_buffer || !protocol_buffer_count) |
| 2234 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2235 | |
| 2236 | *protocol_buffer = NULL; |
Rob Clark | 661c832 | 2017-07-20 07:59:39 -0400 | [diff] [blame] | 2237 | *protocol_buffer_count = 0; |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2238 | |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2239 | efiobj = efi_search_obj(handle); |
| 2240 | if (!efiobj) |
| 2241 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2242 | |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2243 | /* Count protocols */ |
| 2244 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2245 | ++*protocol_buffer_count; |
| 2246 | } |
| 2247 | |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 2248 | /* Copy GUIDs */ |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2249 | if (*protocol_buffer_count) { |
| 2250 | size_t j = 0; |
| 2251 | |
| 2252 | buffer_size = sizeof(efi_guid_t *) * *protocol_buffer_count; |
Heinrich Schuchardt | eee6530 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2253 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
Heinrich Schuchardt | 69fb6b1 | 2017-11-26 14:05:17 +0100 | [diff] [blame] | 2254 | (void **)protocol_buffer); |
| 2255 | if (r != EFI_SUCCESS) |
| 2256 | return EFI_EXIT(r); |
| 2257 | list_for_each(protocol_handle, &efiobj->protocols) { |
| 2258 | struct efi_handler *protocol; |
| 2259 | |
| 2260 | protocol = list_entry(protocol_handle, |
| 2261 | struct efi_handler, link); |
| 2262 | (*protocol_buffer)[j] = (void *)protocol->guid; |
| 2263 | ++j; |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2264 | } |
xypron.glpk@gmx.de | c0ebfc8 | 2017-07-13 23:24:32 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2270 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2271 | * efi_locate_handle_buffer() - locate handles implementing a protocol |
| 2272 | * @search_type: selection criterion |
| 2273 | * @protocol: GUID of the protocol |
| 2274 | * @search_key: registration key |
| 2275 | * @no_handles: number of returned handles |
| 2276 | * @buffer: buffer with the returned handles |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2277 | * |
| 2278 | * This function implements the LocateHandleBuffer service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2279 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2280 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2281 | * details. |
| 2282 | * |
| 2283 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2284 | */ |
AKASHI Takahiro | b51ec63 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 2285 | efi_status_t EFIAPI efi_locate_handle_buffer( |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2286 | enum efi_locate_search_type search_type, |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2287 | const efi_guid_t *protocol, void *search_key, |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2288 | efi_uintn_t *no_handles, efi_handle_t **buffer) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2289 | { |
xypron.glpk@gmx.de | c2e703f | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2290 | efi_status_t r; |
Heinrich Schuchardt | f5a2a93 | 2017-11-06 21:17:48 +0100 | [diff] [blame] | 2291 | efi_uintn_t buffer_size = 0; |
xypron.glpk@gmx.de | c2e703f | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2292 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2293 | EFI_ENTRY("%d, %pUl, %p, %p, %p", search_type, protocol, search_key, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2294 | no_handles, buffer); |
xypron.glpk@gmx.de | c2e703f | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2295 | |
| 2296 | if (!no_handles || !buffer) { |
| 2297 | r = EFI_INVALID_PARAMETER; |
| 2298 | goto out; |
| 2299 | } |
| 2300 | *no_handles = 0; |
| 2301 | *buffer = NULL; |
| 2302 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2303 | *buffer); |
| 2304 | if (r != EFI_BUFFER_TOO_SMALL) |
| 2305 | goto out; |
Heinrich Schuchardt | eee6530 | 2018-10-03 20:02:29 +0200 | [diff] [blame] | 2306 | r = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, buffer_size, |
xypron.glpk@gmx.de | c2e703f | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2307 | (void **)buffer); |
| 2308 | if (r != EFI_SUCCESS) |
| 2309 | goto out; |
| 2310 | r = efi_locate_handle(search_type, protocol, search_key, &buffer_size, |
| 2311 | *buffer); |
| 2312 | if (r == EFI_SUCCESS) |
Heinrich Schuchardt | 2074f70 | 2018-01-11 08:16:09 +0100 | [diff] [blame] | 2313 | *no_handles = buffer_size / sizeof(efi_handle_t); |
xypron.glpk@gmx.de | c2e703f | 2017-07-11 22:06:22 +0200 | [diff] [blame] | 2314 | out: |
| 2315 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2318 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2319 | * efi_locate_protocol() - find an interface implementing a protocol |
| 2320 | * @protocol: GUID of the protocol |
| 2321 | * @registration: registration key passed to the notification function |
| 2322 | * @protocol_interface: interface implementing the protocol |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2323 | * |
| 2324 | * This function implements the LocateProtocol service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2325 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2326 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2327 | * details. |
| 2328 | * |
| 2329 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2330 | */ |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2331 | static efi_status_t EFIAPI efi_locate_protocol(const efi_guid_t *protocol, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2332 | void *registration, |
| 2333 | void **protocol_interface) |
| 2334 | { |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2335 | struct efi_handler *handler; |
Heinrich Schuchardt | 9172cd9 | 2017-10-26 19:25:57 +0200 | [diff] [blame] | 2336 | efi_status_t ret; |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2337 | struct efi_object *efiobj; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2338 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2339 | EFI_ENTRY("%pUl, %p, %p", protocol, registration, protocol_interface); |
xypron.glpk@gmx.de | 88adae5 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2340 | |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2341 | /* |
| 2342 | * The UEFI spec explicitly requires a protocol even if a registration |
| 2343 | * key is provided. This differs from the logic in LocateHandle(). |
| 2344 | */ |
xypron.glpk@gmx.de | 88adae5 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2345 | if (!protocol || !protocol_interface) |
| 2346 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2347 | |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2348 | if (registration) { |
| 2349 | struct efi_register_notify_event *event; |
| 2350 | struct efi_protocol_notification *handle; |
xypron.glpk@gmx.de | 88adae5 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2351 | |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2352 | event = efi_check_register_notify_event(registration); |
| 2353 | if (!event) |
| 2354 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2355 | /* |
| 2356 | * The UEFI spec requires to return EFI_NOT_FOUND if no |
| 2357 | * protocol instance matches protocol and registration. |
| 2358 | * So let's do the same for a mismatch between protocol and |
| 2359 | * registration. |
| 2360 | */ |
| 2361 | if (guidcmp(&event->protocol, protocol)) |
| 2362 | goto not_found; |
| 2363 | if (list_empty(&event->handles)) |
| 2364 | goto not_found; |
| 2365 | handle = list_first_entry(&event->handles, |
| 2366 | struct efi_protocol_notification, |
| 2367 | link); |
| 2368 | efiobj = handle->handle; |
| 2369 | list_del(&handle->link); |
| 2370 | free(handle); |
Heinrich Schuchardt | fae0118 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 2371 | ret = efi_search_protocol(efiobj, protocol, &handler); |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2372 | if (ret == EFI_SUCCESS) |
| 2373 | goto found; |
| 2374 | } else { |
| 2375 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 2376 | ret = efi_search_protocol(efiobj, protocol, &handler); |
| 2377 | if (ret == EFI_SUCCESS) |
| 2378 | goto found; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2379 | } |
| 2380 | } |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2381 | not_found: |
xypron.glpk@gmx.de | 88adae5 | 2017-07-11 22:06:24 +0200 | [diff] [blame] | 2382 | *protocol_interface = NULL; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2383 | return EFI_EXIT(EFI_NOT_FOUND); |
Heinrich Schuchardt | e31b3b1 | 2019-05-29 18:06:46 +0200 | [diff] [blame] | 2384 | found: |
| 2385 | *protocol_interface = handler->protocol_interface; |
| 2386 | return EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2387 | } |
| 2388 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2389 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2390 | * efi_locate_device_path() - Get the device path and handle of an device |
| 2391 | * implementing a protocol |
| 2392 | * @protocol: GUID of the protocol |
| 2393 | * @device_path: device path |
| 2394 | * @device: handle of the device |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2395 | * |
| 2396 | * This function implements the LocateDevicePath service. |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2397 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2398 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2399 | * details. |
| 2400 | * |
| 2401 | * Return: status code |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2402 | */ |
| 2403 | static efi_status_t EFIAPI efi_locate_device_path( |
| 2404 | const efi_guid_t *protocol, |
| 2405 | struct efi_device_path **device_path, |
| 2406 | efi_handle_t *device) |
| 2407 | { |
| 2408 | struct efi_device_path *dp; |
| 2409 | size_t i; |
| 2410 | struct efi_handler *handler; |
| 2411 | efi_handle_t *handles; |
| 2412 | size_t len, len_dp; |
| 2413 | size_t len_best = 0; |
| 2414 | efi_uintn_t no_handles; |
| 2415 | u8 *remainder; |
| 2416 | efi_status_t ret; |
| 2417 | |
| 2418 | EFI_ENTRY("%pUl, %p, %p", protocol, device_path, device); |
| 2419 | |
Heinrich Schuchardt | ab55714 | 2019-05-10 19:21:41 +0200 | [diff] [blame] | 2420 | if (!protocol || !device_path || !*device_path) { |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2421 | ret = EFI_INVALID_PARAMETER; |
| 2422 | goto out; |
| 2423 | } |
| 2424 | |
| 2425 | /* Find end of device path */ |
Heinrich Schuchardt | f6dd3f3 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 2426 | len = efi_dp_instance_size(*device_path); |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* Get all handles implementing the protocol */ |
| 2429 | ret = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, protocol, NULL, |
| 2430 | &no_handles, &handles)); |
| 2431 | if (ret != EFI_SUCCESS) |
| 2432 | goto out; |
| 2433 | |
| 2434 | for (i = 0; i < no_handles; ++i) { |
| 2435 | /* Find the device path protocol */ |
| 2436 | ret = efi_search_protocol(handles[i], &efi_guid_device_path, |
| 2437 | &handler); |
| 2438 | if (ret != EFI_SUCCESS) |
| 2439 | continue; |
| 2440 | dp = (struct efi_device_path *)handler->protocol_interface; |
Heinrich Schuchardt | f6dd3f3 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 2441 | len_dp = efi_dp_instance_size(dp); |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2442 | /* |
| 2443 | * This handle can only be a better fit |
| 2444 | * if its device path length is longer than the best fit and |
| 2445 | * if its device path length is shorter of equal the searched |
| 2446 | * device path. |
| 2447 | */ |
| 2448 | if (len_dp <= len_best || len_dp > len) |
| 2449 | continue; |
| 2450 | /* Check if dp is a subpath of device_path */ |
| 2451 | if (memcmp(*device_path, dp, len_dp)) |
| 2452 | continue; |
Heinrich Schuchardt | ab55714 | 2019-05-10 19:21:41 +0200 | [diff] [blame] | 2453 | if (!device) { |
| 2454 | ret = EFI_INVALID_PARAMETER; |
| 2455 | goto out; |
| 2456 | } |
Heinrich Schuchardt | ae2c85c | 2017-11-26 14:05:10 +0100 | [diff] [blame] | 2457 | *device = handles[i]; |
| 2458 | len_best = len_dp; |
| 2459 | } |
| 2460 | if (len_best) { |
| 2461 | remainder = (u8 *)*device_path + len_best; |
| 2462 | *device_path = (struct efi_device_path *)remainder; |
| 2463 | ret = EFI_SUCCESS; |
| 2464 | } else { |
| 2465 | ret = EFI_NOT_FOUND; |
| 2466 | } |
| 2467 | out: |
| 2468 | return EFI_EXIT(ret); |
| 2469 | } |
| 2470 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2471 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2472 | * efi_install_multiple_protocol_interfaces() - Install multiple protocol |
| 2473 | * interfaces |
| 2474 | * @handle: handle on which the protocol interfaces shall be installed |
| 2475 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2476 | * interfaces |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2477 | * |
| 2478 | * This function implements the MultipleProtocolInterfaces service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2479 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2480 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2481 | * details. |
| 2482 | * |
| 2483 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2484 | */ |
Heinrich Schuchardt | 7657ae1 | 2019-04-12 06:59:49 +0200 | [diff] [blame] | 2485 | efi_status_t EFIAPI efi_install_multiple_protocol_interfaces |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2486 | (efi_handle_t *handle, ...) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2487 | { |
| 2488 | EFI_ENTRY("%p", handle); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2489 | |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2490 | efi_va_list argptr; |
Heinrich Schuchardt | 5a9682d | 2017-10-05 16:35:53 +0200 | [diff] [blame] | 2491 | const efi_guid_t *protocol; |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2492 | void *protocol_interface; |
Heinrich Schuchardt | 226cddb | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2493 | efi_handle_t old_handle; |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2494 | efi_status_t r = EFI_SUCCESS; |
| 2495 | int i = 0; |
| 2496 | |
| 2497 | if (!handle) |
| 2498 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2499 | |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2500 | efi_va_start(argptr, handle); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2501 | for (;;) { |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2502 | protocol = efi_va_arg(argptr, efi_guid_t*); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2503 | if (!protocol) |
| 2504 | break; |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2505 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 226cddb | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2506 | /* Check that a device path has not been installed before */ |
| 2507 | if (!guidcmp(protocol, &efi_guid_device_path)) { |
| 2508 | struct efi_device_path *dp = protocol_interface; |
| 2509 | |
| 2510 | r = EFI_CALL(efi_locate_device_path(protocol, &dp, |
| 2511 | &old_handle)); |
Heinrich Schuchardt | 2056289 | 2019-05-20 19:55:18 +0000 | [diff] [blame] | 2512 | if (r == EFI_SUCCESS && |
| 2513 | dp->type == DEVICE_PATH_TYPE_END) { |
| 2514 | EFI_PRINT("Path %pD already installed\n", |
| 2515 | protocol_interface); |
Heinrich Schuchardt | 226cddb | 2019-05-16 21:54:04 +0200 | [diff] [blame] | 2516 | r = EFI_ALREADY_STARTED; |
| 2517 | break; |
| 2518 | } |
| 2519 | } |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 2520 | r = EFI_CALL(efi_install_protocol_interface( |
| 2521 | handle, protocol, |
| 2522 | EFI_NATIVE_INTERFACE, |
| 2523 | protocol_interface)); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2524 | if (r != EFI_SUCCESS) |
| 2525 | break; |
| 2526 | i++; |
| 2527 | } |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2528 | efi_va_end(argptr); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2529 | if (r == EFI_SUCCESS) |
| 2530 | return EFI_EXIT(r); |
| 2531 | |
Heinrich Schuchardt | 62471e4 | 2017-10-26 19:25:42 +0200 | [diff] [blame] | 2532 | /* If an error occurred undo all changes. */ |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2533 | efi_va_start(argptr, handle); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2534 | for (; i; --i) { |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2535 | protocol = efi_va_arg(argptr, efi_guid_t*); |
| 2536 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2537 | EFI_CALL(efi_uninstall_protocol_interface(*handle, protocol, |
Heinrich Schuchardt | cd53408 | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 2538 | protocol_interface)); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2539 | } |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2540 | efi_va_end(argptr); |
xypron.glpk@gmx.de | 58b8358 | 2017-07-11 22:06:20 +0200 | [diff] [blame] | 2541 | |
| 2542 | return EFI_EXIT(r); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2543 | } |
| 2544 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2545 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2546 | * efi_uninstall_multiple_protocol_interfaces() - uninstall multiple protocol |
| 2547 | * interfaces |
| 2548 | * @handle: handle from which the protocol interfaces shall be removed |
| 2549 | * @...: NULL terminated argument list with pairs of protocol GUIDS and |
| 2550 | * interfaces |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2551 | * |
| 2552 | * This function implements the UninstallMultipleProtocolInterfaces service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2553 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2554 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2555 | * details. |
| 2556 | * |
| 2557 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2558 | */ |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2559 | static efi_status_t EFIAPI efi_uninstall_multiple_protocol_interfaces( |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2560 | efi_handle_t handle, ...) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2561 | { |
| 2562 | EFI_ENTRY("%p", handle); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2563 | |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2564 | efi_va_list argptr; |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2565 | const efi_guid_t *protocol; |
| 2566 | void *protocol_interface; |
| 2567 | efi_status_t r = EFI_SUCCESS; |
| 2568 | size_t i = 0; |
| 2569 | |
| 2570 | if (!handle) |
| 2571 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2572 | |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2573 | efi_va_start(argptr, handle); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2574 | for (;;) { |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2575 | protocol = efi_va_arg(argptr, efi_guid_t*); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2576 | if (!protocol) |
| 2577 | break; |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2578 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 2579 | r = efi_uninstall_protocol(handle, protocol, |
| 2580 | protocol_interface); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2581 | if (r != EFI_SUCCESS) |
| 2582 | break; |
| 2583 | i++; |
| 2584 | } |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2585 | efi_va_end(argptr); |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 2586 | if (r == EFI_SUCCESS) { |
| 2587 | /* If the last protocol has been removed, delete the handle. */ |
| 2588 | if (list_empty(&handle->protocols)) { |
| 2589 | list_del(&handle->link); |
| 2590 | free(handle); |
| 2591 | } |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2592 | return EFI_EXIT(r); |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 2593 | } |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2594 | |
| 2595 | /* If an error occurred undo all changes. */ |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2596 | efi_va_start(argptr, handle); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2597 | for (; i; --i) { |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2598 | protocol = efi_va_arg(argptr, efi_guid_t*); |
| 2599 | protocol_interface = efi_va_arg(argptr, void*); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2600 | EFI_CALL(efi_install_protocol_interface(&handle, protocol, |
| 2601 | EFI_NATIVE_INTERFACE, |
| 2602 | protocol_interface)); |
| 2603 | } |
Alexander Graf | beb077a | 2018-06-18 17:23:05 +0200 | [diff] [blame] | 2604 | efi_va_end(argptr); |
Heinrich Schuchardt | 843ce54 | 2017-10-26 19:25:44 +0200 | [diff] [blame] | 2605 | |
Heinrich Schuchardt | e237302 | 2018-09-24 19:57:27 +0200 | [diff] [blame] | 2606 | /* In case of an error always return EFI_INVALID_PARAMETER */ |
| 2607 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2610 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2611 | * efi_calculate_crc32() - calculate cyclic redundancy code |
| 2612 | * @data: buffer with data |
| 2613 | * @data_size: size of buffer in bytes |
| 2614 | * @crc32_p: cyclic redundancy code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2615 | * |
| 2616 | * This function implements the CalculateCrc32 service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2617 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2618 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2619 | * details. |
| 2620 | * |
| 2621 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2622 | */ |
Heinrich Schuchardt | 8aa8360 | 2018-07-07 15:36:04 +0200 | [diff] [blame] | 2623 | static efi_status_t EFIAPI efi_calculate_crc32(const void *data, |
| 2624 | efi_uintn_t data_size, |
| 2625 | u32 *crc32_p) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2626 | { |
Heinrich Schuchardt | db80fe3 | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2627 | efi_status_t ret = EFI_SUCCESS; |
| 2628 | |
Heinrich Schuchardt | 8aa8360 | 2018-07-07 15:36:04 +0200 | [diff] [blame] | 2629 | EFI_ENTRY("%p, %zu", data, data_size); |
Heinrich Schuchardt | db80fe3 | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2630 | if (!data || !data_size || !crc32_p) { |
| 2631 | ret = EFI_INVALID_PARAMETER; |
| 2632 | goto out; |
| 2633 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2634 | *crc32_p = crc32(0, data, data_size); |
Heinrich Schuchardt | db80fe3 | 2019-05-16 23:31:29 +0200 | [diff] [blame] | 2635 | out: |
| 2636 | return EFI_EXIT(ret); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2639 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2640 | * efi_copy_mem() - copy memory |
| 2641 | * @destination: destination of the copy operation |
| 2642 | * @source: source of the copy operation |
| 2643 | * @length: number of bytes to copy |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2644 | * |
| 2645 | * This function implements the CopyMem service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2646 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2647 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2648 | * details. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2649 | */ |
Heinrich Schuchardt | fc05a95 | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2650 | static void EFIAPI efi_copy_mem(void *destination, const void *source, |
| 2651 | size_t length) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2652 | { |
Heinrich Schuchardt | fc05a95 | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2653 | EFI_ENTRY("%p, %p, %ld", destination, source, (unsigned long)length); |
Heinrich Schuchardt | 0bc81a7 | 2019-01-09 21:41:13 +0100 | [diff] [blame] | 2654 | memmove(destination, source, length); |
Heinrich Schuchardt | f7c7817 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2655 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2658 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2659 | * efi_set_mem() - Fill memory with a byte value. |
| 2660 | * @buffer: buffer to fill |
| 2661 | * @size: size of buffer in bytes |
| 2662 | * @value: byte to copy to the buffer |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2663 | * |
| 2664 | * This function implements the SetMem service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2665 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2666 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2667 | * details. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2668 | */ |
Heinrich Schuchardt | fc05a95 | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2669 | static void EFIAPI efi_set_mem(void *buffer, size_t size, uint8_t value) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2670 | { |
Heinrich Schuchardt | fc05a95 | 2017-10-05 16:35:52 +0200 | [diff] [blame] | 2671 | EFI_ENTRY("%p, %ld, 0x%x", buffer, (unsigned long)size, value); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2672 | memset(buffer, value, size); |
Heinrich Schuchardt | f7c7817 | 2017-10-05 16:35:51 +0200 | [diff] [blame] | 2673 | EFI_EXIT(EFI_SUCCESS); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2676 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2677 | * efi_protocol_open() - open protocol interface on a handle |
| 2678 | * @handler: handler of a protocol |
| 2679 | * @protocol_interface: interface implementing the protocol |
| 2680 | * @agent_handle: handle of the driver |
| 2681 | * @controller_handle: handle of the controller |
| 2682 | * @attributes: attributes indicating how to open the protocol |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2683 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2684 | * Return: status code |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2685 | */ |
| 2686 | static efi_status_t efi_protocol_open( |
| 2687 | struct efi_handler *handler, |
| 2688 | void **protocol_interface, void *agent_handle, |
| 2689 | void *controller_handle, uint32_t attributes) |
| 2690 | { |
| 2691 | struct efi_open_protocol_info_item *item; |
| 2692 | struct efi_open_protocol_info_entry *match = NULL; |
| 2693 | bool opened_by_driver = false; |
| 2694 | bool opened_exclusive = false; |
| 2695 | |
| 2696 | /* If there is no agent, only return the interface */ |
| 2697 | if (!agent_handle) |
| 2698 | goto out; |
| 2699 | |
| 2700 | /* For TEST_PROTOCOL ignore interface attribute */ |
| 2701 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
| 2702 | *protocol_interface = NULL; |
| 2703 | |
| 2704 | /* |
| 2705 | * Check if the protocol is already opened by a driver with the same |
| 2706 | * attributes or opened exclusively |
| 2707 | */ |
| 2708 | list_for_each_entry(item, &handler->open_infos, link) { |
| 2709 | if (item->info.agent_handle == agent_handle) { |
| 2710 | if ((attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) && |
| 2711 | (item->info.attributes == attributes)) |
| 2712 | return EFI_ALREADY_STARTED; |
Heinrich Schuchardt | 399a39e | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2713 | } else { |
| 2714 | if (item->info.attributes & |
| 2715 | EFI_OPEN_PROTOCOL_BY_DRIVER) |
| 2716 | opened_by_driver = true; |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2717 | } |
| 2718 | if (item->info.attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) |
| 2719 | opened_exclusive = true; |
| 2720 | } |
| 2721 | |
| 2722 | /* Only one controller can open the protocol exclusively */ |
Heinrich Schuchardt | 399a39e | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2723 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
| 2724 | if (opened_exclusive) |
| 2725 | return EFI_ACCESS_DENIED; |
| 2726 | } else if (attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 2727 | if (opened_exclusive || opened_by_driver) |
| 2728 | return EFI_ACCESS_DENIED; |
| 2729 | } |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2730 | |
| 2731 | /* Prepare exclusive opening */ |
| 2732 | if (attributes & EFI_OPEN_PROTOCOL_EXCLUSIVE) { |
| 2733 | /* Try to disconnect controllers */ |
Heinrich Schuchardt | dae7ce4 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2734 | disconnect_next: |
| 2735 | opened_by_driver = false; |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2736 | list_for_each_entry(item, &handler->open_infos, link) { |
Heinrich Schuchardt | dae7ce4 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2737 | efi_status_t ret; |
| 2738 | |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2739 | if (item->info.attributes == |
Heinrich Schuchardt | dae7ce4 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2740 | EFI_OPEN_PROTOCOL_BY_DRIVER) { |
| 2741 | ret = EFI_CALL(efi_disconnect_controller( |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2742 | item->info.controller_handle, |
| 2743 | item->info.agent_handle, |
| 2744 | NULL)); |
Heinrich Schuchardt | dae7ce4 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2745 | if (ret == EFI_SUCCESS) |
| 2746 | /* |
| 2747 | * Child controllers may have been |
| 2748 | * removed from the open_infos list. So |
| 2749 | * let's restart the loop. |
| 2750 | */ |
| 2751 | goto disconnect_next; |
| 2752 | else |
| 2753 | opened_by_driver = true; |
| 2754 | } |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2755 | } |
Heinrich Schuchardt | dae7ce4 | 2019-05-30 14:16:31 +0200 | [diff] [blame] | 2756 | /* Only one driver can be connected */ |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2757 | if (opened_by_driver) |
| 2758 | return EFI_ACCESS_DENIED; |
| 2759 | } |
| 2760 | |
| 2761 | /* Find existing entry */ |
| 2762 | list_for_each_entry(item, &handler->open_infos, link) { |
| 2763 | if (item->info.agent_handle == agent_handle && |
Heinrich Schuchardt | b4863ba | 2019-06-01 20:15:10 +0200 | [diff] [blame] | 2764 | item->info.controller_handle == controller_handle && |
| 2765 | item->info.attributes == attributes) |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2766 | match = &item->info; |
| 2767 | } |
| 2768 | /* None found, create one */ |
| 2769 | if (!match) { |
| 2770 | match = efi_create_open_info(handler); |
| 2771 | if (!match) |
| 2772 | return EFI_OUT_OF_RESOURCES; |
| 2773 | } |
| 2774 | |
| 2775 | match->agent_handle = agent_handle; |
| 2776 | match->controller_handle = controller_handle; |
| 2777 | match->attributes = attributes; |
| 2778 | match->open_count++; |
| 2779 | |
| 2780 | out: |
| 2781 | /* For TEST_PROTOCOL ignore interface attribute. */ |
| 2782 | if (attributes != EFI_OPEN_PROTOCOL_TEST_PROTOCOL) |
| 2783 | *protocol_interface = handler->protocol_interface; |
| 2784 | |
| 2785 | return EFI_SUCCESS; |
| 2786 | } |
| 2787 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2788 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2789 | * efi_open_protocol() - open protocol interface on a handle |
| 2790 | * @handle: handle on which the protocol shall be opened |
| 2791 | * @protocol: GUID of the protocol |
| 2792 | * @protocol_interface: interface implementing the protocol |
| 2793 | * @agent_handle: handle of the driver |
| 2794 | * @controller_handle: handle of the controller |
| 2795 | * @attributes: attributes indicating how to open the protocol |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2796 | * |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2797 | * This function implements the OpenProtocol interface. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2798 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 2799 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2800 | * details. |
| 2801 | * |
| 2802 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 2803 | */ |
Heinrich Schuchardt | faea104 | 2018-09-26 05:27:54 +0200 | [diff] [blame] | 2804 | static efi_status_t EFIAPI efi_open_protocol |
| 2805 | (efi_handle_t handle, const efi_guid_t *protocol, |
| 2806 | void **protocol_interface, efi_handle_t agent_handle, |
| 2807 | efi_handle_t controller_handle, uint32_t attributes) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2808 | { |
Heinrich Schuchardt | 80286e8 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2809 | struct efi_handler *handler; |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2810 | efi_status_t r = EFI_INVALID_PARAMETER; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2811 | |
Rob Clark | 778e6af | 2017-09-13 18:05:41 -0400 | [diff] [blame] | 2812 | EFI_ENTRY("%p, %pUl, %p, %p, %p, 0x%x", handle, protocol, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2813 | protocol_interface, agent_handle, controller_handle, |
| 2814 | attributes); |
xypron.glpk@gmx.de | b5349f7 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 2815 | |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2816 | if (!handle || !protocol || |
| 2817 | (!protocol_interface && attributes != |
| 2818 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL)) { |
| 2819 | goto out; |
| 2820 | } |
| 2821 | |
| 2822 | switch (attributes) { |
| 2823 | case EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL: |
| 2824 | case EFI_OPEN_PROTOCOL_GET_PROTOCOL: |
| 2825 | case EFI_OPEN_PROTOCOL_TEST_PROTOCOL: |
| 2826 | break; |
| 2827 | case EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER: |
| 2828 | if (controller_handle == handle) |
| 2829 | goto out; |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2830 | /* fall-through */ |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2831 | case EFI_OPEN_PROTOCOL_BY_DRIVER: |
| 2832 | case EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE: |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2833 | /* Check that the controller handle is valid */ |
| 2834 | if (!efi_search_obj(controller_handle)) |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2835 | goto out; |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2836 | /* fall-through */ |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2837 | case EFI_OPEN_PROTOCOL_EXCLUSIVE: |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2838 | /* Check that the agent handle is valid */ |
| 2839 | if (!efi_search_obj(agent_handle)) |
xypron.glpk@gmx.de | 69baec6 | 2017-07-11 22:06:15 +0200 | [diff] [blame] | 2840 | goto out; |
| 2841 | break; |
| 2842 | default: |
xypron.glpk@gmx.de | b5349f7 | 2017-07-11 22:06:14 +0200 | [diff] [blame] | 2843 | goto out; |
| 2844 | } |
| 2845 | |
Heinrich Schuchardt | 80286e8 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2846 | r = efi_search_protocol(handle, protocol, &handler); |
Heinrich Schuchardt | e7c3cd6 | 2019-05-05 11:24:53 +0200 | [diff] [blame] | 2847 | switch (r) { |
| 2848 | case EFI_SUCCESS: |
| 2849 | break; |
| 2850 | case EFI_NOT_FOUND: |
| 2851 | r = EFI_UNSUPPORTED; |
Heinrich Schuchardt | 80286e8 | 2017-11-26 14:05:15 +0100 | [diff] [blame] | 2852 | goto out; |
Heinrich Schuchardt | e7c3cd6 | 2019-05-05 11:24:53 +0200 | [diff] [blame] | 2853 | default: |
| 2854 | goto out; |
| 2855 | } |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2856 | |
Heinrich Schuchardt | 191a41c | 2018-01-11 08:15:58 +0100 | [diff] [blame] | 2857 | r = efi_protocol_open(handler, protocol_interface, agent_handle, |
| 2858 | controller_handle, attributes); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 2859 | out: |
| 2860 | return EFI_EXIT(r); |
| 2861 | } |
| 2862 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 2863 | /** |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2864 | * efi_start_image() - call the entry point of an image |
| 2865 | * @image_handle: handle of the image |
| 2866 | * @exit_data_size: size of the buffer |
| 2867 | * @exit_data: buffer to receive the exit data of the called image |
| 2868 | * |
| 2869 | * This function implements the StartImage service. |
| 2870 | * |
| 2871 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 2872 | * details. |
| 2873 | * |
| 2874 | * Return: status code |
| 2875 | */ |
| 2876 | efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle, |
| 2877 | efi_uintn_t *exit_data_size, |
| 2878 | u16 **exit_data) |
| 2879 | { |
| 2880 | struct efi_loaded_image_obj *image_obj = |
| 2881 | (struct efi_loaded_image_obj *)image_handle; |
| 2882 | efi_status_t ret; |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2883 | void *info; |
| 2884 | efi_handle_t parent_image = current_image; |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2885 | |
| 2886 | EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data); |
| 2887 | |
AKASHI Takahiro | 4540dab | 2020-04-14 11:51:44 +0900 | [diff] [blame^] | 2888 | if (!efi_search_obj(image_handle)) |
| 2889 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2890 | |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2891 | /* Check parameters */ |
Heinrich Schuchardt | 1d3e8dc | 2019-06-11 19:35:20 +0200 | [diff] [blame] | 2892 | if (image_obj->header.type != EFI_OBJECT_TYPE_LOADED_IMAGE) |
| 2893 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2894 | |
AKASHI Takahiro | 4540dab | 2020-04-14 11:51:44 +0900 | [diff] [blame^] | 2895 | if (image_obj->auth_status != EFI_IMAGE_AUTH_PASSED) |
| 2896 | return EFI_EXIT(EFI_SECURITY_VIOLATION); |
| 2897 | |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2898 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
| 2899 | &info, NULL, NULL, |
| 2900 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 2901 | if (ret != EFI_SUCCESS) |
| 2902 | return EFI_EXIT(EFI_INVALID_PARAMETER); |
| 2903 | |
Heinrich Schuchardt | 556d8dc | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 2904 | image_obj->exit_data_size = exit_data_size; |
| 2905 | image_obj->exit_data = exit_data; |
| 2906 | |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2907 | /* call the image! */ |
| 2908 | if (setjmp(&image_obj->exit_jmp)) { |
| 2909 | /* |
| 2910 | * We called the entry point of the child image with EFI_CALL |
| 2911 | * in the lines below. The child image called the Exit() boot |
| 2912 | * service efi_exit() which executed the long jump that brought |
| 2913 | * us to the current line. This implies that the second half |
| 2914 | * of the EFI_CALL macro has not been executed. |
| 2915 | */ |
| 2916 | #ifdef CONFIG_ARM |
| 2917 | /* |
| 2918 | * efi_exit() called efi_restore_gd(). We have to undo this |
| 2919 | * otherwise __efi_entry_check() will put the wrong value into |
| 2920 | * app_gd. |
| 2921 | */ |
| 2922 | gd = app_gd; |
| 2923 | #endif |
| 2924 | /* |
| 2925 | * To get ready to call EFI_EXIT below we have to execute the |
| 2926 | * missed out steps of EFI_CALL. |
| 2927 | */ |
| 2928 | assert(__efi_entry_check()); |
Heinrich Schuchardt | 529886a | 2019-05-05 11:56:23 +0200 | [diff] [blame] | 2929 | EFI_PRINT("%lu returned by started image\n", |
| 2930 | (unsigned long)((uintptr_t)image_obj->exit_status & |
| 2931 | ~EFI_ERROR_MASK)); |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2932 | current_image = parent_image; |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2933 | return EFI_EXIT(image_obj->exit_status); |
| 2934 | } |
| 2935 | |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 2936 | current_image = image_handle; |
Heinrich Schuchardt | cd73aba | 2019-05-01 14:20:18 +0200 | [diff] [blame] | 2937 | image_obj->header.type = EFI_OBJECT_TYPE_STARTED_IMAGE; |
AKASHI Takahiro | 6b95b38 | 2019-04-19 12:22:35 +0900 | [diff] [blame] | 2938 | EFI_PRINT("Jumping into 0x%p\n", image_obj->entry); |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2939 | ret = EFI_CALL(image_obj->entry(image_handle, &systab)); |
| 2940 | |
| 2941 | /* |
Heinrich Schuchardt | 55111c5 | 2020-01-10 22:06:54 +0100 | [diff] [blame] | 2942 | * Control is returned from a started UEFI image either by calling |
| 2943 | * Exit() (where exit data can be provided) or by simply returning from |
| 2944 | * the entry point. In the latter case call Exit() on behalf of the |
| 2945 | * image. |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 2946 | */ |
| 2947 | return EFI_CALL(systab.boottime->exit(image_handle, ret, 0, NULL)); |
| 2948 | } |
| 2949 | |
| 2950 | /** |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 2951 | * efi_delete_image() - delete loaded image from memory) |
| 2952 | * |
| 2953 | * @image_obj: handle of the loaded image |
| 2954 | * @loaded_image_protocol: loaded image protocol |
| 2955 | */ |
Heinrich Schuchardt | 120ff7b | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 2956 | static efi_status_t efi_delete_image |
| 2957 | (struct efi_loaded_image_obj *image_obj, |
| 2958 | struct efi_loaded_image *loaded_image_protocol) |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 2959 | { |
Heinrich Schuchardt | 120ff7b | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 2960 | struct efi_object *efiobj; |
| 2961 | efi_status_t r, ret = EFI_SUCCESS; |
| 2962 | |
| 2963 | close_next: |
| 2964 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
| 2965 | struct efi_handler *protocol; |
| 2966 | |
| 2967 | list_for_each_entry(protocol, &efiobj->protocols, link) { |
| 2968 | struct efi_open_protocol_info_item *info; |
| 2969 | |
| 2970 | list_for_each_entry(info, &protocol->open_infos, link) { |
| 2971 | if (info->info.agent_handle != |
| 2972 | (efi_handle_t)image_obj) |
| 2973 | continue; |
| 2974 | r = EFI_CALL(efi_close_protocol |
| 2975 | (efiobj, protocol->guid, |
| 2976 | info->info.agent_handle, |
| 2977 | info->info.controller_handle |
| 2978 | )); |
| 2979 | if (r != EFI_SUCCESS) |
| 2980 | ret = r; |
| 2981 | /* |
| 2982 | * Closing protocols may results in further |
| 2983 | * items being deleted. To play it safe loop |
| 2984 | * over all elements again. |
| 2985 | */ |
| 2986 | goto close_next; |
| 2987 | } |
| 2988 | } |
| 2989 | } |
| 2990 | |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 2991 | efi_free_pages((uintptr_t)loaded_image_protocol->image_base, |
| 2992 | efi_size_in_pages(loaded_image_protocol->image_size)); |
| 2993 | efi_delete_handle(&image_obj->header); |
Heinrich Schuchardt | 120ff7b | 2019-06-02 20:02:32 +0200 | [diff] [blame] | 2994 | |
| 2995 | return ret; |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 2996 | } |
| 2997 | |
| 2998 | /** |
Heinrich Schuchardt | 46e99a9 | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 2999 | * efi_unload_image() - unload an EFI image |
| 3000 | * @image_handle: handle of the image to be unloaded |
| 3001 | * |
| 3002 | * This function implements the UnloadImage service. |
| 3003 | * |
| 3004 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3005 | * details. |
| 3006 | * |
| 3007 | * Return: status code |
| 3008 | */ |
| 3009 | efi_status_t EFIAPI efi_unload_image(efi_handle_t image_handle) |
| 3010 | { |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3011 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 46e99a9 | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3012 | struct efi_object *efiobj; |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3013 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | 46e99a9 | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3014 | |
| 3015 | EFI_ENTRY("%p", image_handle); |
Heinrich Schuchardt | 46e99a9 | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3016 | |
Heinrich Schuchardt | df116e8 | 2019-05-01 18:25:45 +0200 | [diff] [blame] | 3017 | efiobj = efi_search_obj(image_handle); |
| 3018 | if (!efiobj) { |
| 3019 | ret = EFI_INVALID_PARAMETER; |
| 3020 | goto out; |
| 3021 | } |
| 3022 | /* Find the loaded image protocol */ |
| 3023 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
| 3024 | (void **)&loaded_image_protocol, |
| 3025 | NULL, NULL, |
| 3026 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 3027 | if (ret != EFI_SUCCESS) { |
| 3028 | ret = EFI_INVALID_PARAMETER; |
| 3029 | goto out; |
| 3030 | } |
| 3031 | switch (efiobj->type) { |
| 3032 | case EFI_OBJECT_TYPE_STARTED_IMAGE: |
| 3033 | /* Call the unload function */ |
| 3034 | if (!loaded_image_protocol->unload) { |
| 3035 | ret = EFI_UNSUPPORTED; |
| 3036 | goto out; |
| 3037 | } |
| 3038 | ret = EFI_CALL(loaded_image_protocol->unload(image_handle)); |
| 3039 | if (ret != EFI_SUCCESS) |
| 3040 | goto out; |
| 3041 | break; |
| 3042 | case EFI_OBJECT_TYPE_LOADED_IMAGE: |
| 3043 | break; |
| 3044 | default: |
| 3045 | ret = EFI_INVALID_PARAMETER; |
| 3046 | goto out; |
| 3047 | } |
| 3048 | efi_delete_image((struct efi_loaded_image_obj *)efiobj, |
| 3049 | loaded_image_protocol); |
| 3050 | out: |
| 3051 | return EFI_EXIT(ret); |
Heinrich Schuchardt | 46e99a9 | 2019-05-01 19:04:32 +0200 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | /** |
Heinrich Schuchardt | 556d8dc | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3055 | * efi_update_exit_data() - fill exit data parameters of StartImage() |
| 3056 | * |
Heinrich Schuchardt | 9ce9127 | 2019-07-14 11:25:06 +0200 | [diff] [blame] | 3057 | * @image_obj: image handle |
| 3058 | * @exit_data_size: size of the exit data buffer |
| 3059 | * @exit_data: buffer with data returned by UEFI payload |
Heinrich Schuchardt | 556d8dc | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3060 | * Return: status code |
| 3061 | */ |
| 3062 | static efi_status_t efi_update_exit_data(struct efi_loaded_image_obj *image_obj, |
| 3063 | efi_uintn_t exit_data_size, |
| 3064 | u16 *exit_data) |
| 3065 | { |
| 3066 | efi_status_t ret; |
| 3067 | |
| 3068 | /* |
| 3069 | * If exit_data is not provided to StartImage(), exit_data_size must be |
| 3070 | * ignored. |
| 3071 | */ |
| 3072 | if (!image_obj->exit_data) |
| 3073 | return EFI_SUCCESS; |
| 3074 | if (image_obj->exit_data_size) |
| 3075 | *image_obj->exit_data_size = exit_data_size; |
| 3076 | if (exit_data_size && exit_data) { |
| 3077 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, |
| 3078 | exit_data_size, |
| 3079 | (void **)image_obj->exit_data); |
| 3080 | if (ret != EFI_SUCCESS) |
| 3081 | return ret; |
| 3082 | memcpy(*image_obj->exit_data, exit_data, exit_data_size); |
| 3083 | } else { |
| 3084 | image_obj->exit_data = NULL; |
| 3085 | } |
| 3086 | return EFI_SUCCESS; |
| 3087 | } |
| 3088 | |
| 3089 | /** |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3090 | * efi_exit() - leave an EFI application or driver |
| 3091 | * @image_handle: handle of the application or driver that is exiting |
| 3092 | * @exit_status: status code |
| 3093 | * @exit_data_size: size of the buffer in bytes |
| 3094 | * @exit_data: buffer with data describing an error |
| 3095 | * |
| 3096 | * This function implements the Exit service. |
| 3097 | * |
| 3098 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3099 | * details. |
| 3100 | * |
| 3101 | * Return: status code |
| 3102 | */ |
| 3103 | static efi_status_t EFIAPI efi_exit(efi_handle_t image_handle, |
| 3104 | efi_status_t exit_status, |
| 3105 | efi_uintn_t exit_data_size, |
| 3106 | u16 *exit_data) |
| 3107 | { |
| 3108 | /* |
| 3109 | * TODO: We should call the unload procedure of the loaded |
| 3110 | * image protocol. |
| 3111 | */ |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3112 | efi_status_t ret; |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3113 | struct efi_loaded_image *loaded_image_protocol; |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3114 | struct efi_loaded_image_obj *image_obj = |
| 3115 | (struct efi_loaded_image_obj *)image_handle; |
| 3116 | |
| 3117 | EFI_ENTRY("%p, %ld, %zu, %p", image_handle, exit_status, |
| 3118 | exit_data_size, exit_data); |
| 3119 | |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3120 | /* Check parameters */ |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3121 | ret = EFI_CALL(efi_open_protocol(image_handle, &efi_guid_loaded_image, |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3122 | (void **)&loaded_image_protocol, |
| 3123 | NULL, NULL, |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3124 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3125 | if (ret != EFI_SUCCESS) { |
| 3126 | ret = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3127 | goto out; |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /* Unloading of unstarted images */ |
| 3131 | switch (image_obj->header.type) { |
| 3132 | case EFI_OBJECT_TYPE_STARTED_IMAGE: |
| 3133 | break; |
| 3134 | case EFI_OBJECT_TYPE_LOADED_IMAGE: |
| 3135 | efi_delete_image(image_obj, loaded_image_protocol); |
| 3136 | ret = EFI_SUCCESS; |
| 3137 | goto out; |
| 3138 | default: |
| 3139 | /* Handle does not refer to loaded image */ |
| 3140 | ret = EFI_INVALID_PARAMETER; |
| 3141 | goto out; |
| 3142 | } |
| 3143 | /* A started image can only be unloaded it is the last one started. */ |
| 3144 | if (image_handle != current_image) { |
| 3145 | ret = EFI_INVALID_PARAMETER; |
| 3146 | goto out; |
| 3147 | } |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3148 | |
Heinrich Schuchardt | 556d8dc | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3149 | /* Exit data is only foreseen in case of failure. */ |
| 3150 | if (exit_status != EFI_SUCCESS) { |
| 3151 | ret = efi_update_exit_data(image_obj, exit_data_size, |
| 3152 | exit_data); |
| 3153 | /* Exiting has priority. Don't return error to caller. */ |
| 3154 | if (ret != EFI_SUCCESS) |
| 3155 | EFI_PRINT("%s: out of memory\n", __func__); |
| 3156 | } |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3157 | if (image_obj->image_type == IMAGE_SUBSYSTEM_EFI_APPLICATION || |
| 3158 | exit_status != EFI_SUCCESS) |
| 3159 | efi_delete_image(image_obj, loaded_image_protocol); |
Heinrich Schuchardt | 556d8dc | 2019-04-30 17:57:30 +0200 | [diff] [blame] | 3160 | |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3161 | /* Make sure entry/exit counts for EFI world cross-overs match */ |
| 3162 | EFI_EXIT(exit_status); |
| 3163 | |
| 3164 | /* |
| 3165 | * But longjmp out with the U-Boot gd, not the application's, as |
| 3166 | * the other end is a setjmp call inside EFI context. |
| 3167 | */ |
| 3168 | efi_restore_gd(); |
| 3169 | |
| 3170 | image_obj->exit_status = exit_status; |
| 3171 | longjmp(&image_obj->exit_jmp, 1); |
| 3172 | |
| 3173 | panic("EFI application exited"); |
Heinrich Schuchardt | bb31c3f | 2019-03-26 19:03:17 +0100 | [diff] [blame] | 3174 | out: |
Heinrich Schuchardt | 126a43f | 2019-05-01 20:07:04 +0200 | [diff] [blame] | 3175 | return EFI_EXIT(ret); |
Heinrich Schuchardt | a115d56 | 2019-03-26 19:02:05 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3179 | * efi_handle_protocol() - get interface of a protocol on a handle |
| 3180 | * @handle: handle on which the protocol shall be opened |
| 3181 | * @protocol: GUID of the protocol |
| 3182 | * @protocol_interface: interface implementing the protocol |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3183 | * |
| 3184 | * This function implements the HandleProtocol service. |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3185 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3186 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3187 | * details. |
| 3188 | * |
| 3189 | * Return: status code |
Heinrich Schuchardt | 332468f | 2017-09-21 18:30:11 +0200 | [diff] [blame] | 3190 | */ |
AKASHI Takahiro | b51ec63 | 2020-03-17 11:12:36 +0900 | [diff] [blame] | 3191 | efi_status_t EFIAPI efi_handle_protocol(efi_handle_t handle, |
| 3192 | const efi_guid_t *protocol, |
| 3193 | void **protocol_interface) |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3194 | { |
Heinrich Schuchardt | 755d42d | 2019-06-01 19:29:39 +0200 | [diff] [blame] | 3195 | return efi_open_protocol(handle, protocol, protocol_interface, efi_root, |
xypron.glpk@gmx.de | 8e1d329 | 2017-06-29 21:16:19 +0200 | [diff] [blame] | 3196 | NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL); |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3197 | } |
| 3198 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3199 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3200 | * efi_bind_controller() - bind a single driver to a controller |
| 3201 | * @controller_handle: controller handle |
| 3202 | * @driver_image_handle: driver handle |
| 3203 | * @remain_device_path: remaining path |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3204 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3205 | * Return: status code |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3206 | */ |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3207 | static efi_status_t efi_bind_controller( |
| 3208 | efi_handle_t controller_handle, |
| 3209 | efi_handle_t driver_image_handle, |
| 3210 | struct efi_device_path *remain_device_path) |
| 3211 | { |
| 3212 | struct efi_driver_binding_protocol *binding_protocol; |
| 3213 | efi_status_t r; |
| 3214 | |
| 3215 | r = EFI_CALL(efi_open_protocol(driver_image_handle, |
| 3216 | &efi_guid_driver_binding_protocol, |
| 3217 | (void **)&binding_protocol, |
| 3218 | driver_image_handle, NULL, |
| 3219 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
| 3220 | if (r != EFI_SUCCESS) |
| 3221 | return r; |
| 3222 | r = EFI_CALL(binding_protocol->supported(binding_protocol, |
| 3223 | controller_handle, |
| 3224 | remain_device_path)); |
| 3225 | if (r == EFI_SUCCESS) |
| 3226 | r = EFI_CALL(binding_protocol->start(binding_protocol, |
| 3227 | controller_handle, |
| 3228 | remain_device_path)); |
| 3229 | EFI_CALL(efi_close_protocol(driver_image_handle, |
| 3230 | &efi_guid_driver_binding_protocol, |
| 3231 | driver_image_handle, NULL)); |
| 3232 | return r; |
| 3233 | } |
| 3234 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3235 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3236 | * efi_connect_single_controller() - connect a single driver to a controller |
| 3237 | * @controller_handle: controller |
| 3238 | * @driver_image_handle: driver |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3239 | * @remain_device_path: remaining path |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3240 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3241 | * Return: status code |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3242 | */ |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3243 | static efi_status_t efi_connect_single_controller( |
| 3244 | efi_handle_t controller_handle, |
| 3245 | efi_handle_t *driver_image_handle, |
| 3246 | struct efi_device_path *remain_device_path) |
| 3247 | { |
| 3248 | efi_handle_t *buffer; |
| 3249 | size_t count; |
| 3250 | size_t i; |
| 3251 | efi_status_t r; |
| 3252 | size_t connected = 0; |
| 3253 | |
| 3254 | /* Get buffer with all handles with driver binding protocol */ |
| 3255 | r = EFI_CALL(efi_locate_handle_buffer(BY_PROTOCOL, |
| 3256 | &efi_guid_driver_binding_protocol, |
| 3257 | NULL, &count, &buffer)); |
| 3258 | if (r != EFI_SUCCESS) |
| 3259 | return r; |
| 3260 | |
Heinrich Schuchardt | 359a699 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3261 | /* Context Override */ |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3262 | if (driver_image_handle) { |
| 3263 | for (; *driver_image_handle; ++driver_image_handle) { |
| 3264 | for (i = 0; i < count; ++i) { |
| 3265 | if (buffer[i] == *driver_image_handle) { |
| 3266 | buffer[i] = NULL; |
| 3267 | r = efi_bind_controller( |
| 3268 | controller_handle, |
| 3269 | *driver_image_handle, |
| 3270 | remain_device_path); |
| 3271 | /* |
| 3272 | * For drivers that do not support the |
| 3273 | * controller or are already connected |
| 3274 | * we receive an error code here. |
| 3275 | */ |
| 3276 | if (r == EFI_SUCCESS) |
| 3277 | ++connected; |
| 3278 | } |
| 3279 | } |
| 3280 | } |
| 3281 | } |
| 3282 | |
| 3283 | /* |
| 3284 | * TODO: Some overrides are not yet implemented: |
| 3285 | * - Platform Driver Override |
| 3286 | * - Driver Family Override Search |
| 3287 | * - Bus Specific Driver Override |
| 3288 | */ |
| 3289 | |
| 3290 | /* Driver Binding Search */ |
| 3291 | for (i = 0; i < count; ++i) { |
| 3292 | if (buffer[i]) { |
| 3293 | r = efi_bind_controller(controller_handle, |
| 3294 | buffer[i], |
| 3295 | remain_device_path); |
| 3296 | if (r == EFI_SUCCESS) |
| 3297 | ++connected; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | efi_free_pool(buffer); |
| 3302 | if (!connected) |
| 3303 | return EFI_NOT_FOUND; |
| 3304 | return EFI_SUCCESS; |
| 3305 | } |
| 3306 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3307 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3308 | * efi_connect_controller() - connect a controller to a driver |
| 3309 | * @controller_handle: handle of the controller |
| 3310 | * @driver_image_handle: handle of the driver |
| 3311 | * @remain_device_path: device path of a child controller |
| 3312 | * @recursive: true to connect all child controllers |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3313 | * |
| 3314 | * This function implements the ConnectController service. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3315 | * |
| 3316 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3317 | * details. |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3318 | * |
| 3319 | * First all driver binding protocol handles are tried for binding drivers. |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3320 | * Afterwards all handles that have opened a protocol of the controller |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3321 | * with EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER are connected to drivers. |
| 3322 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3323 | * Return: status code |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3324 | */ |
| 3325 | static efi_status_t EFIAPI efi_connect_controller( |
| 3326 | efi_handle_t controller_handle, |
| 3327 | efi_handle_t *driver_image_handle, |
| 3328 | struct efi_device_path *remain_device_path, |
| 3329 | bool recursive) |
| 3330 | { |
| 3331 | efi_status_t r; |
| 3332 | efi_status_t ret = EFI_NOT_FOUND; |
| 3333 | struct efi_object *efiobj; |
| 3334 | |
Heinrich Schuchardt | d178836 | 2018-12-09 16:39:20 +0100 | [diff] [blame] | 3335 | EFI_ENTRY("%p, %p, %pD, %d", controller_handle, driver_image_handle, |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3336 | remain_device_path, recursive); |
| 3337 | |
| 3338 | efiobj = efi_search_obj(controller_handle); |
| 3339 | if (!efiobj) { |
| 3340 | ret = EFI_INVALID_PARAMETER; |
| 3341 | goto out; |
| 3342 | } |
| 3343 | |
| 3344 | r = efi_connect_single_controller(controller_handle, |
| 3345 | driver_image_handle, |
| 3346 | remain_device_path); |
| 3347 | if (r == EFI_SUCCESS) |
| 3348 | ret = EFI_SUCCESS; |
| 3349 | if (recursive) { |
| 3350 | struct efi_handler *handler; |
| 3351 | struct efi_open_protocol_info_item *item; |
| 3352 | |
| 3353 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3354 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3355 | if (item->info.attributes & |
| 3356 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { |
| 3357 | r = EFI_CALL(efi_connect_controller( |
| 3358 | item->info.controller_handle, |
| 3359 | driver_image_handle, |
| 3360 | remain_device_path, |
| 3361 | recursive)); |
| 3362 | if (r == EFI_SUCCESS) |
| 3363 | ret = EFI_SUCCESS; |
| 3364 | } |
| 3365 | } |
| 3366 | } |
| 3367 | } |
Heinrich Schuchardt | 359a699 | 2019-07-03 20:27:24 +0200 | [diff] [blame] | 3368 | /* Check for child controller specified by end node */ |
Heinrich Schuchardt | f0959db | 2018-01-11 08:16:02 +0100 | [diff] [blame] | 3369 | if (ret != EFI_SUCCESS && remain_device_path && |
| 3370 | remain_device_path->type == DEVICE_PATH_TYPE_END) |
| 3371 | ret = EFI_SUCCESS; |
| 3372 | out: |
| 3373 | return EFI_EXIT(ret); |
| 3374 | } |
| 3375 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3376 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3377 | * efi_reinstall_protocol_interface() - reinstall protocol interface |
| 3378 | * @handle: handle on which the protocol shall be reinstalled |
| 3379 | * @protocol: GUID of the protocol to be installed |
| 3380 | * @old_interface: interface to be removed |
| 3381 | * @new_interface: interface to be installed |
Heinrich Schuchardt | e861a12 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3382 | * |
| 3383 | * This function implements the ReinstallProtocolInterface service. |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3384 | * |
| 3385 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3386 | * details. |
Heinrich Schuchardt | e861a12 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3387 | * |
| 3388 | * The old interface is uninstalled. The new interface is installed. |
| 3389 | * Drivers are connected. |
| 3390 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3391 | * Return: status code |
Heinrich Schuchardt | e861a12 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3392 | */ |
| 3393 | static efi_status_t EFIAPI efi_reinstall_protocol_interface( |
| 3394 | efi_handle_t handle, const efi_guid_t *protocol, |
| 3395 | void *old_interface, void *new_interface) |
| 3396 | { |
| 3397 | efi_status_t ret; |
| 3398 | |
| 3399 | EFI_ENTRY("%p, %pUl, %p, %p", handle, protocol, old_interface, |
| 3400 | new_interface); |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 3401 | |
| 3402 | /* Uninstall protocol but do not delete handle */ |
| 3403 | ret = efi_uninstall_protocol(handle, protocol, old_interface); |
Heinrich Schuchardt | e861a12 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3404 | if (ret != EFI_SUCCESS) |
| 3405 | goto out; |
Heinrich Schuchardt | 9b47f13 | 2018-09-28 22:14:17 +0200 | [diff] [blame] | 3406 | |
| 3407 | /* Install the new protocol */ |
| 3408 | ret = efi_add_protocol(handle, protocol, new_interface); |
| 3409 | /* |
| 3410 | * The UEFI spec does not specify what should happen to the handle |
| 3411 | * if in case of an error no protocol interface remains on the handle. |
| 3412 | * So let's do nothing here. |
| 3413 | */ |
Heinrich Schuchardt | e861a12 | 2018-05-11 12:09:22 +0200 | [diff] [blame] | 3414 | if (ret != EFI_SUCCESS) |
| 3415 | goto out; |
| 3416 | /* |
| 3417 | * The returned status code has to be ignored. |
| 3418 | * Do not create an error if no suitable driver for the handle exists. |
| 3419 | */ |
| 3420 | EFI_CALL(efi_connect_controller(handle, NULL, NULL, true)); |
| 3421 | out: |
| 3422 | return EFI_EXIT(ret); |
| 3423 | } |
| 3424 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3425 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3426 | * efi_get_child_controllers() - get all child controllers associated to a driver |
| 3427 | * @efiobj: handle of the controller |
| 3428 | * @driver_handle: handle of the driver |
| 3429 | * @number_of_children: number of child controllers |
| 3430 | * @child_handle_buffer: handles of the the child controllers |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3431 | * |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3432 | * The allocated buffer has to be freed with free(). |
| 3433 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3434 | * Return: status code |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3435 | */ |
| 3436 | static efi_status_t efi_get_child_controllers( |
| 3437 | struct efi_object *efiobj, |
| 3438 | efi_handle_t driver_handle, |
| 3439 | efi_uintn_t *number_of_children, |
| 3440 | efi_handle_t **child_handle_buffer) |
| 3441 | { |
| 3442 | struct efi_handler *handler; |
| 3443 | struct efi_open_protocol_info_item *item; |
| 3444 | efi_uintn_t count = 0, i; |
| 3445 | bool duplicate; |
| 3446 | |
| 3447 | /* Count all child controller associations */ |
| 3448 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3449 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3450 | if (item->info.agent_handle == driver_handle && |
| 3451 | item->info.attributes & |
| 3452 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) |
| 3453 | ++count; |
| 3454 | } |
| 3455 | } |
| 3456 | /* |
| 3457 | * Create buffer. In case of duplicate child controller assignments |
| 3458 | * the buffer will be too large. But that does not harm. |
| 3459 | */ |
| 3460 | *number_of_children = 0; |
| 3461 | *child_handle_buffer = calloc(count, sizeof(efi_handle_t)); |
| 3462 | if (!*child_handle_buffer) |
| 3463 | return EFI_OUT_OF_RESOURCES; |
| 3464 | /* Copy unique child handles */ |
| 3465 | list_for_each_entry(handler, &efiobj->protocols, link) { |
| 3466 | list_for_each_entry(item, &handler->open_infos, link) { |
| 3467 | if (item->info.agent_handle == driver_handle && |
| 3468 | item->info.attributes & |
| 3469 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) { |
| 3470 | /* Check this is a new child controller */ |
| 3471 | duplicate = false; |
| 3472 | for (i = 0; i < *number_of_children; ++i) { |
| 3473 | if ((*child_handle_buffer)[i] == |
| 3474 | item->info.controller_handle) |
| 3475 | duplicate = true; |
| 3476 | } |
| 3477 | /* Copy handle to buffer */ |
| 3478 | if (!duplicate) { |
| 3479 | i = (*number_of_children)++; |
| 3480 | (*child_handle_buffer)[i] = |
| 3481 | item->info.controller_handle; |
| 3482 | } |
| 3483 | } |
| 3484 | } |
| 3485 | } |
| 3486 | return EFI_SUCCESS; |
| 3487 | } |
| 3488 | |
Heinrich Schuchardt | 6b03cd1 | 2018-05-11 18:15:41 +0200 | [diff] [blame] | 3489 | /** |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3490 | * efi_disconnect_controller() - disconnect a controller from a driver |
| 3491 | * @controller_handle: handle of the controller |
| 3492 | * @driver_image_handle: handle of the driver |
| 3493 | * @child_handle: handle of the child to destroy |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3494 | * |
| 3495 | * This function implements the DisconnectController service. |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3496 | * |
Mario Six | 78a88f7 | 2018-07-10 08:40:17 +0200 | [diff] [blame] | 3497 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 3498 | * details. |
| 3499 | * |
| 3500 | * Return: status code |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3501 | */ |
| 3502 | static efi_status_t EFIAPI efi_disconnect_controller( |
| 3503 | efi_handle_t controller_handle, |
| 3504 | efi_handle_t driver_image_handle, |
| 3505 | efi_handle_t child_handle) |
| 3506 | { |
| 3507 | struct efi_driver_binding_protocol *binding_protocol; |
| 3508 | efi_handle_t *child_handle_buffer = NULL; |
| 3509 | size_t number_of_children = 0; |
| 3510 | efi_status_t r; |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3511 | struct efi_object *efiobj; |
| 3512 | |
| 3513 | EFI_ENTRY("%p, %p, %p", controller_handle, driver_image_handle, |
| 3514 | child_handle); |
| 3515 | |
| 3516 | efiobj = efi_search_obj(controller_handle); |
| 3517 | if (!efiobj) { |
| 3518 | r = EFI_INVALID_PARAMETER; |
| 3519 | goto out; |
| 3520 | } |
| 3521 | |
| 3522 | if (child_handle && !efi_search_obj(child_handle)) { |
| 3523 | r = EFI_INVALID_PARAMETER; |
| 3524 | goto out; |
| 3525 | } |
| 3526 | |
| 3527 | /* If no driver handle is supplied, disconnect all drivers */ |
| 3528 | if (!driver_image_handle) { |
| 3529 | r = efi_disconnect_all_drivers(efiobj, NULL, child_handle); |
| 3530 | goto out; |
| 3531 | } |
| 3532 | |
| 3533 | /* Create list of child handles */ |
| 3534 | if (child_handle) { |
| 3535 | number_of_children = 1; |
| 3536 | child_handle_buffer = &child_handle; |
| 3537 | } else { |
| 3538 | efi_get_child_controllers(efiobj, |
| 3539 | driver_image_handle, |
| 3540 | &number_of_children, |
| 3541 | &child_handle_buffer); |
| 3542 | } |
| 3543 | |
| 3544 | /* Get the driver binding protocol */ |
| 3545 | r = EFI_CALL(efi_open_protocol(driver_image_handle, |
| 3546 | &efi_guid_driver_binding_protocol, |
| 3547 | (void **)&binding_protocol, |
| 3548 | driver_image_handle, NULL, |
| 3549 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)); |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3550 | if (r != EFI_SUCCESS) { |
| 3551 | r = EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3552 | goto out; |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3553 | } |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3554 | /* Remove the children */ |
| 3555 | if (number_of_children) { |
| 3556 | r = EFI_CALL(binding_protocol->stop(binding_protocol, |
| 3557 | controller_handle, |
| 3558 | number_of_children, |
| 3559 | child_handle_buffer)); |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3560 | if (r != EFI_SUCCESS) { |
| 3561 | r = EFI_DEVICE_ERROR; |
| 3562 | goto out; |
| 3563 | } |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3564 | } |
| 3565 | /* Remove the driver */ |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3566 | if (!child_handle) { |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3567 | r = EFI_CALL(binding_protocol->stop(binding_protocol, |
| 3568 | controller_handle, |
| 3569 | 0, NULL)); |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3570 | if (r != EFI_SUCCESS) { |
| 3571 | r = EFI_DEVICE_ERROR; |
| 3572 | goto out; |
| 3573 | } |
| 3574 | } |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3575 | EFI_CALL(efi_close_protocol(driver_image_handle, |
| 3576 | &efi_guid_driver_binding_protocol, |
| 3577 | driver_image_handle, NULL)); |
Heinrich Schuchardt | 7dc10c9 | 2019-09-13 18:20:40 +0200 | [diff] [blame] | 3578 | r = EFI_SUCCESS; |
Heinrich Schuchardt | 3f9b004 | 2018-01-11 08:16:04 +0100 | [diff] [blame] | 3579 | out: |
| 3580 | if (!child_handle) |
| 3581 | free(child_handle_buffer); |
| 3582 | return EFI_EXIT(r); |
| 3583 | } |
| 3584 | |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3585 | static struct efi_boot_services efi_boot_services = { |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3586 | .hdr = { |
Heinrich Schuchardt | 112f243 | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 3587 | .signature = EFI_BOOT_SERVICES_SIGNATURE, |
| 3588 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 71c846a | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 3589 | .headersize = sizeof(struct efi_boot_services), |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3590 | }, |
| 3591 | .raise_tpl = efi_raise_tpl, |
| 3592 | .restore_tpl = efi_restore_tpl, |
| 3593 | .allocate_pages = efi_allocate_pages_ext, |
| 3594 | .free_pages = efi_free_pages_ext, |
| 3595 | .get_memory_map = efi_get_memory_map_ext, |
Stefan Brüns | ead1274 | 2016-10-09 22:17:18 +0200 | [diff] [blame] | 3596 | .allocate_pool = efi_allocate_pool_ext, |
Stefan Brüns | 42417bc | 2016-10-09 22:17:26 +0200 | [diff] [blame] | 3597 | .free_pool = efi_free_pool_ext, |
xypron.glpk@gmx.de | 49deb45 | 2017-07-18 20:17:20 +0200 | [diff] [blame] | 3598 | .create_event = efi_create_event_ext, |
xypron.glpk@gmx.de | bfc7246 | 2017-07-18 20:17:21 +0200 | [diff] [blame] | 3599 | .set_timer = efi_set_timer_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3600 | .wait_for_event = efi_wait_for_event, |
xypron.glpk@gmx.de | c684159 | 2017-07-18 20:17:18 +0200 | [diff] [blame] | 3601 | .signal_event = efi_signal_event_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3602 | .close_event = efi_close_event, |
| 3603 | .check_event = efi_check_event, |
Heinrich Schuchardt | 1760ef5 | 2017-11-06 21:17:44 +0100 | [diff] [blame] | 3604 | .install_protocol_interface = efi_install_protocol_interface, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3605 | .reinstall_protocol_interface = efi_reinstall_protocol_interface, |
Heinrich Schuchardt | cd53408 | 2017-11-06 21:17:45 +0100 | [diff] [blame] | 3606 | .uninstall_protocol_interface = efi_uninstall_protocol_interface, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3607 | .handle_protocol = efi_handle_protocol, |
| 3608 | .reserved = NULL, |
| 3609 | .register_protocol_notify = efi_register_protocol_notify, |
xypron.glpk@gmx.de | 2632958 | 2017-07-11 22:06:21 +0200 | [diff] [blame] | 3610 | .locate_handle = efi_locate_handle_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3611 | .locate_device_path = efi_locate_device_path, |
Alexander Graf | 488bf12 | 2016-08-19 01:23:24 +0200 | [diff] [blame] | 3612 | .install_configuration_table = efi_install_configuration_table_ext, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3613 | .load_image = efi_load_image, |
| 3614 | .start_image = efi_start_image, |
Alexander Graf | a86aeaf | 2016-05-20 23:28:23 +0200 | [diff] [blame] | 3615 | .exit = efi_exit, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3616 | .unload_image = efi_unload_image, |
| 3617 | .exit_boot_services = efi_exit_boot_services, |
| 3618 | .get_next_monotonic_count = efi_get_next_monotonic_count, |
| 3619 | .stall = efi_stall, |
| 3620 | .set_watchdog_timer = efi_set_watchdog_timer, |
| 3621 | .connect_controller = efi_connect_controller, |
| 3622 | .disconnect_controller = efi_disconnect_controller, |
| 3623 | .open_protocol = efi_open_protocol, |
| 3624 | .close_protocol = efi_close_protocol, |
| 3625 | .open_protocol_information = efi_open_protocol_information, |
| 3626 | .protocols_per_handle = efi_protocols_per_handle, |
| 3627 | .locate_handle_buffer = efi_locate_handle_buffer, |
| 3628 | .locate_protocol = efi_locate_protocol, |
Heinrich Schuchardt | ab9efa9 | 2018-02-18 15:17:49 +0100 | [diff] [blame] | 3629 | .install_multiple_protocol_interfaces = |
| 3630 | efi_install_multiple_protocol_interfaces, |
| 3631 | .uninstall_multiple_protocol_interfaces = |
| 3632 | efi_uninstall_multiple_protocol_interfaces, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3633 | .calculate_crc32 = efi_calculate_crc32, |
| 3634 | .copy_mem = efi_copy_mem, |
| 3635 | .set_mem = efi_set_mem, |
Heinrich Schuchardt | 9f0930e | 2018-02-04 23:05:13 +0100 | [diff] [blame] | 3636 | .create_event_ex = efi_create_event_ex, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3637 | }; |
| 3638 | |
Heinrich Schuchardt | 0b38653 | 2018-06-28 12:45:30 +0200 | [diff] [blame] | 3639 | static u16 __efi_runtime_data firmware_vendor[] = L"Das U-Boot"; |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3640 | |
Alexander Graf | 3c63db9 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 3641 | struct efi_system_table __efi_runtime_data systab = { |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3642 | .hdr = { |
| 3643 | .signature = EFI_SYSTEM_TABLE_SIGNATURE, |
Heinrich Schuchardt | 112f243 | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 3644 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 71c846a | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 3645 | .headersize = sizeof(struct efi_system_table), |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3646 | }, |
Heinrich Schuchardt | 0b38653 | 2018-06-28 12:45:30 +0200 | [diff] [blame] | 3647 | .fw_vendor = firmware_vendor, |
| 3648 | .fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8, |
Heinrich Schuchardt | 3c783bf | 2019-06-15 14:51:06 +0200 | [diff] [blame] | 3649 | .runtime = &efi_runtime_services, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3650 | .nr_tables = 0, |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 3651 | .tables = NULL, |
Alexander Graf | bee9116 | 2016-03-04 01:09:59 +0100 | [diff] [blame] | 3652 | }; |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3653 | |
| 3654 | /** |
| 3655 | * efi_initialize_system_table() - Initialize system table |
| 3656 | * |
Heinrich Schuchardt | 0414359 | 2018-09-03 05:00:43 +0200 | [diff] [blame] | 3657 | * Return: status code |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3658 | */ |
| 3659 | efi_status_t efi_initialize_system_table(void) |
| 3660 | { |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 3661 | efi_status_t ret; |
| 3662 | |
| 3663 | /* Allocate configuration table array */ |
| 3664 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, |
| 3665 | EFI_MAX_CONFIGURATION_TABLES * |
| 3666 | sizeof(struct efi_configuration_table), |
| 3667 | (void **)&systab.tables); |
| 3668 | |
Heinrich Schuchardt | 93148eb | 2019-06-29 02:00:16 +0200 | [diff] [blame] | 3669 | /* |
| 3670 | * These entries will be set to NULL in ExitBootServices(). To avoid |
| 3671 | * relocation in SetVirtualAddressMap(), set them dynamically. |
| 3672 | */ |
| 3673 | systab.con_in = &efi_con_in; |
| 3674 | systab.con_out = &efi_con_out; |
| 3675 | systab.std_err = &efi_con_out; |
| 3676 | systab.boottime = &efi_boot_services; |
| 3677 | |
Heinrich Schuchardt | b72aaa8 | 2018-09-03 19:12:24 +0200 | [diff] [blame] | 3678 | /* Set CRC32 field in table headers */ |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3679 | efi_update_table_header_crc32(&systab.hdr); |
| 3680 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
| 3681 | efi_update_table_header_crc32(&efi_boot_services.hdr); |
Heinrich Schuchardt | 4182a12 | 2018-06-28 12:45:32 +0200 | [diff] [blame] | 3682 | |
| 3683 | return ret; |
Heinrich Schuchardt | 640adad | 2018-06-28 12:45:31 +0200 | [diff] [blame] | 3684 | } |