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