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