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