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