Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI application runtime services |
| 4 | * |
| 5 | * Copyright (c) 2016 Alexander Graf |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 12 | #include <elf.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 13 | #include <efi_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 16 | #include <rtc.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 17 | #include <u-boot/crc.h> |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 18 | |
| 19 | /* For manual relocation support */ |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 22 | /* GUID of the runtime properties table */ |
| 23 | static const efi_guid_t efi_rt_properties_table_guid = |
| 24 | EFI_RT_PROPERTIES_TABLE_GUID; |
| 25 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 26 | struct efi_runtime_mmio_list { |
| 27 | struct list_head link; |
| 28 | void **ptr; |
| 29 | u64 paddr; |
| 30 | u64 len; |
| 31 | }; |
| 32 | |
| 33 | /* This list contains all runtime available mmio regions */ |
| 34 | LIST_HEAD(efi_runtime_mmio); |
| 35 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 36 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 37 | |
Simon Glass | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 38 | /* |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 39 | * TODO(sjg@chromium.org): These defines and structures should come from the ELF |
| 40 | * header for each architecture (or a generic header) rather than being repeated |
| 41 | * here. |
Simon Glass | fff89d4 | 2018-06-11 23:26:40 -0600 | [diff] [blame] | 42 | */ |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 43 | #if defined(__aarch64__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 44 | #define R_RELATIVE R_AARCH64_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 45 | #define R_MASK 0xffffffffULL |
| 46 | #define IS_RELA 1 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 47 | #elif defined(__arm__) |
Alexander Graf | 46c1514 | 2018-06-18 17:23:11 +0200 | [diff] [blame] | 48 | #define R_RELATIVE R_ARM_RELATIVE |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 49 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 50 | #elif defined(__i386__) |
Simon Glass | cdfe696 | 2016-09-25 15:27:35 -0600 | [diff] [blame] | 51 | #define R_RELATIVE R_386_RELATIVE |
| 52 | #define R_MASK 0xffULL |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 53 | #elif defined(__x86_64__) |
| 54 | #define R_RELATIVE R_X86_64_RELATIVE |
| 55 | #define R_MASK 0xffffffffULL |
| 56 | #define IS_RELA 1 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 57 | #elif defined(__riscv) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 58 | #define R_RELATIVE R_RISCV_RELATIVE |
| 59 | #define R_MASK 0xffULL |
| 60 | #define IS_RELA 1 |
| 61 | |
| 62 | struct dyn_sym { |
| 63 | ulong foo1; |
| 64 | ulong addr; |
| 65 | u32 foo2; |
| 66 | u32 foo3; |
| 67 | }; |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 68 | #if (__riscv_xlen == 32) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 69 | #define R_ABSOLUTE R_RISCV_32 |
| 70 | #define SYM_INDEX 8 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 71 | #elif (__riscv_xlen == 64) |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 72 | #define R_ABSOLUTE R_RISCV_64 |
| 73 | #define SYM_INDEX 32 |
Alexander Graf | 0fe5192 | 2018-06-18 17:23:08 +0200 | [diff] [blame] | 74 | #else |
| 75 | #error unknown riscv target |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 76 | #endif |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 77 | #else |
| 78 | #error Need to add relocation awareness |
| 79 | #endif |
| 80 | |
| 81 | struct elf_rel { |
| 82 | ulong *offset; |
| 83 | ulong info; |
| 84 | }; |
| 85 | |
| 86 | struct elf_rela { |
| 87 | ulong *offset; |
| 88 | ulong info; |
| 89 | long addend; |
| 90 | }; |
| 91 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 92 | static __efi_runtime_data struct efi_mem_desc *efi_virtmap; |
| 93 | static __efi_runtime_data efi_uintn_t efi_descriptor_count; |
| 94 | static __efi_runtime_data efi_uintn_t efi_descriptor_size; |
| 95 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 96 | /* |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 97 | * EFI runtime code lives in two stages. In the first stage, U-Boot and an EFI |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 98 | * payload are running concurrently at the same time. In this mode, we can |
| 99 | * handle a good number of runtime callbacks |
| 100 | */ |
| 101 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 102 | /** |
| 103 | * efi_init_runtime_supported() - create runtime properties table |
| 104 | * |
| 105 | * Create a configuration table specifying which services are available at |
| 106 | * runtime. |
| 107 | * |
| 108 | * Return: status code |
| 109 | */ |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 110 | efi_status_t efi_init_runtime_supported(void) |
| 111 | { |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 112 | efi_status_t ret; |
| 113 | struct efi_rt_properties_table *rt_table; |
| 114 | |
| 115 | ret = efi_allocate_pool(EFI_RUNTIME_SERVICES_DATA, |
| 116 | sizeof(struct efi_rt_properties_table), |
| 117 | (void **)&rt_table); |
| 118 | if (ret != EFI_SUCCESS) |
| 119 | return ret; |
| 120 | |
| 121 | rt_table->version = EFI_RT_PROPERTIES_TABLE_VERSION; |
| 122 | rt_table->length = sizeof(struct efi_rt_properties_table); |
| 123 | rt_table->runtime_services_supported = |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 124 | EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP | |
| 125 | EFI_RT_SUPPORTED_CONVERT_POINTER; |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * This value must be synced with efi_runtime_detach_list |
| 129 | * as well as efi_runtime_services. |
| 130 | */ |
Heinrich Schuchardt | 05874fb | 2019-07-05 18:12:16 +0200 | [diff] [blame] | 131 | #ifdef CONFIG_EFI_HAVE_RUNTIME_RESET |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 132 | rt_table->runtime_services_supported |= EFI_RT_SUPPORTED_RESET_SYSTEM; |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 133 | #endif |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 134 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 135 | ret = efi_install_configuration_table(&efi_rt_properties_table_guid, |
| 136 | rt_table); |
| 137 | return ret; |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 138 | } |
| 139 | |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 140 | /** |
| 141 | * efi_update_table_header_crc32() - Update crc32 in table header |
| 142 | * |
| 143 | * @table: EFI table |
| 144 | */ |
| 145 | void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table) |
| 146 | { |
| 147 | table->crc32 = 0; |
| 148 | table->crc32 = crc32(0, (const unsigned char *)table, |
| 149 | table->headersize); |
| 150 | } |
| 151 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 152 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 153 | * efi_reset_system_boottime() - reset system at boot time |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 154 | * |
| 155 | * This function implements the ResetSystem() runtime service before |
| 156 | * SetVirtualAddressMap() is called. |
| 157 | * |
| 158 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 159 | * details. |
| 160 | * |
| 161 | * @reset_type: type of reset to perform |
| 162 | * @reset_status: status code for the reset |
| 163 | * @data_size: size of reset_data |
| 164 | * @reset_data: information about the reset |
| 165 | */ |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 166 | static void EFIAPI efi_reset_system_boottime( |
| 167 | enum efi_reset_type reset_type, |
| 168 | efi_status_t reset_status, |
| 169 | unsigned long data_size, void *reset_data) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 170 | { |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 171 | struct efi_event *evt; |
| 172 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 173 | EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size, |
| 174 | reset_data); |
| 175 | |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 176 | /* Notify reset */ |
| 177 | list_for_each_entry(evt, &efi_events, link) { |
| 178 | if (evt->group && |
| 179 | !guidcmp(evt->group, |
| 180 | &efi_guid_event_group_reset_system)) { |
Heinrich Schuchardt | 7b4b8d86 | 2019-06-07 06:47:01 +0200 | [diff] [blame] | 181 | efi_signal_event(evt); |
Heinrich Schuchardt | bf7f169 | 2018-02-18 15:17:52 +0100 | [diff] [blame] | 182 | break; |
| 183 | } |
| 184 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 185 | switch (reset_type) { |
| 186 | case EFI_RESET_COLD: |
| 187 | case EFI_RESET_WARM: |
Heinrich Schuchardt | 450d4c8 | 2018-02-06 22:00:22 +0100 | [diff] [blame] | 188 | case EFI_RESET_PLATFORM_SPECIFIC: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 189 | do_reset(NULL, 0, 0, NULL); |
| 190 | break; |
| 191 | case EFI_RESET_SHUTDOWN: |
Heinrich Schuchardt | 4448945 | 2018-10-16 07:44:53 +0200 | [diff] [blame] | 192 | #ifdef CONFIG_CMD_POWEROFF |
| 193 | do_poweroff(NULL, 0, 0, NULL); |
| 194 | #endif |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 198 | while (1) { } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 201 | /** |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 202 | * efi_get_time_boottime() - get current time at boot time |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 203 | * |
| 204 | * This function implements the GetTime runtime service before |
| 205 | * SetVirtualAddressMap() is called. |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 206 | * |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 207 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 208 | * for details. |
| 209 | * |
| 210 | * @time: pointer to structure to receive current time |
| 211 | * @capabilities: pointer to structure to receive RTC properties |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 212 | * Returns: status code |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 213 | */ |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 214 | static efi_status_t EFIAPI efi_get_time_boottime( |
| 215 | struct efi_time *time, |
| 216 | struct efi_time_cap *capabilities) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 217 | { |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 218 | #ifdef CONFIG_EFI_GET_TIME |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 219 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 220 | struct rtc_time tm; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 221 | struct udevice *dev; |
| 222 | |
| 223 | EFI_ENTRY("%p %p", time, capabilities); |
| 224 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 225 | if (!time) { |
| 226 | ret = EFI_INVALID_PARAMETER; |
| 227 | goto out; |
| 228 | } |
Heinrich Schuchardt | b4bd366 | 2019-05-19 21:41:28 +0200 | [diff] [blame] | 229 | if (uclass_get_device(UCLASS_RTC, 0, &dev) || |
| 230 | dm_rtc_get(dev, &tm)) { |
| 231 | ret = EFI_UNSUPPORTED; |
| 232 | goto out; |
| 233 | } |
| 234 | if (dm_rtc_get(dev, &tm)) { |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 235 | ret = EFI_DEVICE_ERROR; |
| 236 | goto out; |
| 237 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 238 | |
| 239 | memset(time, 0, sizeof(*time)); |
| 240 | time->year = tm.tm_year; |
| 241 | time->month = tm.tm_mon; |
| 242 | time->day = tm.tm_mday; |
| 243 | time->hour = tm.tm_hour; |
| 244 | time->minute = tm.tm_min; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 245 | time->second = tm.tm_sec; |
Heinrich Schuchardt | ba3e828 | 2019-05-31 22:08:45 +0200 | [diff] [blame] | 246 | if (tm.tm_isdst) |
| 247 | time->daylight = |
| 248 | EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT; |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 249 | time->timezone = EFI_UNSPECIFIED_TIMEZONE; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 250 | |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 251 | if (capabilities) { |
| 252 | /* Set reasonable dummy values */ |
| 253 | capabilities->resolution = 1; /* 1 Hz */ |
| 254 | capabilities->accuracy = 100000000; /* 100 ppm */ |
| 255 | capabilities->sets_to_zero = false; |
| 256 | } |
| 257 | out: |
| 258 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 259 | #else |
Heinrich Schuchardt | 37035b1 | 2018-07-07 23:39:14 +0200 | [diff] [blame] | 260 | EFI_ENTRY("%p %p", time, capabilities); |
Heinrich Schuchardt | b4bd366 | 2019-05-19 21:41:28 +0200 | [diff] [blame] | 261 | return EFI_EXIT(EFI_UNSUPPORTED); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 262 | #endif |
| 263 | } |
| 264 | |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 265 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | 33e5a0e | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * efi_validate_time() - checks if timestamp is valid |
| 269 | * |
| 270 | * @time: timestamp to validate |
| 271 | * Returns: 0 if timestamp is valid, 1 otherwise |
| 272 | */ |
| 273 | static int efi_validate_time(struct efi_time *time) |
| 274 | { |
| 275 | return (!time || |
| 276 | time->year < 1900 || time->year > 9999 || |
| 277 | !time->month || time->month > 12 || !time->day || |
| 278 | time->day > rtc_month_days(time->month - 1, time->year) || |
| 279 | time->hour > 23 || time->minute > 59 || time->second > 59 || |
| 280 | time->nanosecond > 999999999 || |
| 281 | time->daylight & |
| 282 | ~(EFI_TIME_IN_DAYLIGHT | EFI_TIME_ADJUST_DAYLIGHT) || |
| 283 | ((time->timezone < -1440 || time->timezone > 1440) && |
| 284 | time->timezone != EFI_UNSPECIFIED_TIMEZONE)); |
| 285 | } |
| 286 | |
| 287 | #endif |
| 288 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 289 | /** |
| 290 | * efi_set_time_boottime() - set current time |
| 291 | * |
| 292 | * This function implements the SetTime() runtime service before |
| 293 | * SetVirtualAddressMap() is called. |
| 294 | * |
| 295 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 296 | * for details. |
| 297 | * |
| 298 | * @time: pointer to structure to with current time |
| 299 | * Returns: status code |
| 300 | */ |
| 301 | static efi_status_t EFIAPI efi_set_time_boottime(struct efi_time *time) |
| 302 | { |
Heinrich Schuchardt | f2856ad | 2019-05-31 22:56:02 +0200 | [diff] [blame] | 303 | #ifdef CONFIG_EFI_SET_TIME |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 304 | efi_status_t ret = EFI_SUCCESS; |
| 305 | struct rtc_time tm; |
| 306 | struct udevice *dev; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 307 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 308 | EFI_ENTRY("%p", time); |
| 309 | |
Heinrich Schuchardt | 33e5a0e | 2019-05-31 07:35:19 +0200 | [diff] [blame] | 310 | if (efi_validate_time(time)) { |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 311 | ret = EFI_INVALID_PARAMETER; |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | if (uclass_get_device(UCLASS_RTC, 0, &dev)) { |
| 316 | ret = EFI_UNSUPPORTED; |
| 317 | goto out; |
| 318 | } |
| 319 | |
| 320 | memset(&tm, 0, sizeof(tm)); |
| 321 | tm.tm_year = time->year; |
| 322 | tm.tm_mon = time->month; |
| 323 | tm.tm_mday = time->day; |
| 324 | tm.tm_hour = time->hour; |
| 325 | tm.tm_min = time->minute; |
| 326 | tm.tm_sec = time->second; |
Heinrich Schuchardt | ba3e828 | 2019-05-31 22:08:45 +0200 | [diff] [blame] | 327 | tm.tm_isdst = time->daylight == |
| 328 | (EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT); |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 329 | /* Calculate day of week */ |
| 330 | rtc_calc_weekday(&tm); |
| 331 | |
| 332 | if (dm_rtc_set(dev, &tm)) |
| 333 | ret = EFI_DEVICE_ERROR; |
| 334 | out: |
| 335 | return EFI_EXIT(ret); |
| 336 | #else |
| 337 | EFI_ENTRY("%p", time); |
| 338 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 339 | #endif |
| 340 | } |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 341 | /** |
| 342 | * efi_reset_system() - reset system |
| 343 | * |
| 344 | * This function implements the ResetSystem() runtime service after |
| 345 | * SetVirtualAddressMap() is called. It only executes an endless loop. |
| 346 | * Boards may override the helpers below to implement reset functionality. |
| 347 | * |
| 348 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 349 | * details. |
| 350 | * |
| 351 | * @reset_type: type of reset to perform |
| 352 | * @reset_status: status code for the reset |
| 353 | * @data_size: size of reset_data |
| 354 | * @reset_data: information about the reset |
| 355 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 356 | void __weak __efi_runtime EFIAPI efi_reset_system( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 357 | enum efi_reset_type reset_type, |
| 358 | efi_status_t reset_status, |
| 359 | unsigned long data_size, void *reset_data) |
| 360 | { |
| 361 | /* Nothing we can do */ |
| 362 | while (1) { } |
| 363 | } |
| 364 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 365 | /** |
| 366 | * efi_reset_system_init() - initialize the reset driver |
| 367 | * |
| 368 | * Boards may override this function to initialize the reset driver. |
| 369 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 370 | efi_status_t __weak efi_reset_system_init(void) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 371 | { |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 372 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 375 | /** |
| 376 | * efi_get_time() - get current time |
| 377 | * |
| 378 | * This function implements the GetTime runtime service after |
| 379 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 380 | * anymore only an error code is returned. |
| 381 | * |
| 382 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 383 | * for details. |
| 384 | * |
| 385 | * @time: pointer to structure to receive current time |
| 386 | * @capabilities: pointer to structure to receive RTC properties |
| 387 | * Returns: status code |
| 388 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 389 | efi_status_t __weak __efi_runtime EFIAPI efi_get_time( |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 390 | struct efi_time *time, |
| 391 | struct efi_time_cap *capabilities) |
| 392 | { |
Heinrich Schuchardt | b4ba5be | 2019-06-13 18:42:40 +0200 | [diff] [blame] | 393 | return EFI_UNSUPPORTED; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 394 | } |
| 395 | |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 396 | /** |
| 397 | * efi_set_time() - set current time |
| 398 | * |
| 399 | * This function implements the SetTime runtime service after |
| 400 | * SetVirtualAddressMap() is called. As the U-Boot driver are not available |
| 401 | * anymore only an error code is returned. |
| 402 | * |
| 403 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 404 | * for details. |
| 405 | * |
| 406 | * @time: pointer to structure to with current time |
| 407 | * Returns: status code |
| 408 | */ |
| 409 | efi_status_t __weak __efi_runtime EFIAPI efi_set_time(struct efi_time *time) |
| 410 | { |
| 411 | return EFI_UNSUPPORTED; |
| 412 | } |
| 413 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 414 | /** |
| 415 | * efi_is_runtime_service_pointer() - check if pointer points to runtime table |
| 416 | * |
| 417 | * @p: pointer to check |
| 418 | * Return: true if the pointer points to a service function pointer in the |
| 419 | * runtime table |
| 420 | */ |
| 421 | static bool efi_is_runtime_service_pointer(void *p) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 422 | { |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 423 | return (p >= (void *)&efi_runtime_services.get_time && |
| 424 | p <= (void *)&efi_runtime_services.query_variable_info) || |
| 425 | p == (void *)&efi_events.prev || |
| 426 | p == (void *)&efi_events.next; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 429 | /** |
| 430 | * efi_runtime_detach() - detach unimplemented runtime functions |
| 431 | */ |
Heinrich Schuchardt | 1943dbb | 2019-07-05 17:42:16 +0200 | [diff] [blame] | 432 | void efi_runtime_detach(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 433 | { |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 434 | efi_runtime_services.reset_system = efi_reset_system; |
| 435 | efi_runtime_services.get_time = efi_get_time; |
| 436 | efi_runtime_services.set_time = efi_set_time; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 437 | |
Heinrich Schuchardt | c3f2a9f | 2019-07-05 18:12:21 +0200 | [diff] [blame] | 438 | /* Update CRC32 */ |
| 439 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 442 | /** |
| 443 | * efi_set_virtual_address_map_runtime() - change from physical to virtual |
| 444 | * mapping |
| 445 | * |
| 446 | * This function implements the SetVirtualAddressMap() runtime service after |
| 447 | * it is first called. |
| 448 | * |
| 449 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 450 | * details. |
| 451 | * |
| 452 | * @memory_map_size: size of the virtual map |
| 453 | * @descriptor_size: size of an entry in the map |
| 454 | * @descriptor_version: version of the map entries |
| 455 | * @virtmap: virtual address mapping information |
| 456 | * Return: status code EFI_UNSUPPORTED |
| 457 | */ |
Heinrich Schuchardt | 2b6bd38 | 2019-07-12 22:41:43 +0200 | [diff] [blame] | 458 | static __efi_runtime efi_status_t EFIAPI efi_set_virtual_address_map_runtime( |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 459 | efi_uintn_t memory_map_size, |
| 460 | efi_uintn_t descriptor_size, |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 461 | uint32_t descriptor_version, |
| 462 | struct efi_mem_desc *virtmap) |
| 463 | { |
| 464 | return EFI_UNSUPPORTED; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * efi_convert_pointer_runtime() - convert from physical to virtual pointer |
| 469 | * |
| 470 | * This function implements the ConvertPointer() runtime service after |
| 471 | * the first call to SetVirtualAddressMap(). |
| 472 | * |
| 473 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 474 | * details. |
| 475 | * |
| 476 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 477 | * @address: pointer to be converted |
| 478 | * Return: status code EFI_UNSUPPORTED |
| 479 | */ |
| 480 | static __efi_runtime efi_status_t EFIAPI efi_convert_pointer_runtime( |
| 481 | efi_uintn_t debug_disposition, void **address) |
| 482 | { |
| 483 | return EFI_UNSUPPORTED; |
| 484 | } |
| 485 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 486 | /** |
Heinrich Schuchardt | 582f857 | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 487 | * efi_convert_pointer() - convert from physical to virtual pointer |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 488 | * |
| 489 | * This function implements the ConvertPointer() runtime service until |
| 490 | * the first call to SetVirtualAddressMap(). |
| 491 | * |
| 492 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 493 | * details. |
| 494 | * |
| 495 | * @debug_disposition: indicates if pointer may be converted to NULL |
| 496 | * @address: pointer to be converted |
Heinrich Schuchardt | 582f857 | 2020-03-22 08:28:15 +0100 | [diff] [blame] | 497 | * Return: status code |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 498 | */ |
| 499 | static __efi_runtime efi_status_t EFIAPI efi_convert_pointer( |
| 500 | efi_uintn_t debug_disposition, void **address) |
| 501 | { |
| 502 | efi_physical_addr_t addr = (uintptr_t)*address; |
| 503 | efi_uintn_t i; |
| 504 | efi_status_t ret = EFI_NOT_FOUND; |
| 505 | |
| 506 | EFI_ENTRY("%zu %p", debug_disposition, address); |
| 507 | |
| 508 | if (!efi_virtmap) { |
| 509 | ret = EFI_UNSUPPORTED; |
| 510 | goto out; |
| 511 | } |
| 512 | |
| 513 | if (!address) { |
| 514 | ret = EFI_INVALID_PARAMETER; |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | for (i = 0; i < efi_descriptor_count; i++) { |
| 519 | struct efi_mem_desc *map = (void *)efi_virtmap + |
| 520 | (efi_descriptor_size * i); |
| 521 | |
| 522 | if (addr >= map->physical_start && |
| 523 | (addr < map->physical_start |
| 524 | + (map->num_pages << EFI_PAGE_SHIFT))) { |
| 525 | *address = (void *)(uintptr_t) |
| 526 | (addr + map->virtual_start - |
| 527 | map->physical_start); |
| 528 | |
| 529 | ret = EFI_SUCCESS; |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | out: |
| 535 | return EFI_EXIT(ret); |
| 536 | } |
| 537 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 538 | static __efi_runtime void efi_relocate_runtime_table(ulong offset) |
| 539 | { |
| 540 | ulong patchoff; |
| 541 | void **pos; |
| 542 | |
| 543 | /* Relocate the runtime services pointers */ |
| 544 | patchoff = offset - gd->relocaddr; |
| 545 | for (pos = (void **)&efi_runtime_services.get_time; |
| 546 | pos <= (void **)&efi_runtime_services.query_variable_info; ++pos) { |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 547 | if (*pos) |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 548 | *pos += patchoff; |
| 549 | } |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 550 | |
Heinrich Schuchardt | 2572851 | 2019-06-29 03:32:52 +0200 | [diff] [blame] | 551 | /* |
| 552 | * The entry for SetVirtualAddress() must point to a physical address. |
| 553 | * After the first execution the service must return EFI_UNSUPPORTED. |
| 554 | */ |
| 555 | efi_runtime_services.set_virtual_address_map = |
| 556 | &efi_set_virtual_address_map_runtime; |
| 557 | |
| 558 | /* |
| 559 | * The entry for ConvertPointer() must point to a physical address. |
| 560 | * The service is not usable after SetVirtualAddress(). |
| 561 | */ |
| 562 | efi_runtime_services.convert_pointer = &efi_convert_pointer_runtime; |
| 563 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 564 | /* |
| 565 | * TODO: Update UEFI variable RuntimeServicesSupported removing flags |
| 566 | * EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP and |
| 567 | * EFI_RT_SUPPORTED_CONVERT_POINTER as required by the UEFI spec 2.8. |
| 568 | */ |
| 569 | |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 570 | /* Update CRC32 */ |
Heinrich Schuchardt | 69c1576 | 2018-07-29 09:49:04 +0200 | [diff] [blame] | 571 | efi_update_table_header_crc32(&efi_runtime_services.hdr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* Relocate EFI runtime to uboot_reloc_base = offset */ |
| 575 | void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map) |
| 576 | { |
| 577 | #ifdef IS_RELA |
| 578 | struct elf_rela *rel = (void*)&__efi_runtime_rel_start; |
| 579 | #else |
| 580 | struct elf_rel *rel = (void*)&__efi_runtime_rel_start; |
| 581 | static ulong lastoff = CONFIG_SYS_TEXT_BASE; |
| 582 | #endif |
| 583 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 584 | debug("%s: Relocating to offset=%lx\n", __func__, offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 585 | for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) { |
| 586 | ulong base = CONFIG_SYS_TEXT_BASE; |
| 587 | ulong *p; |
| 588 | ulong newaddr; |
| 589 | |
| 590 | p = (void*)((ulong)rel->offset - base) + gd->relocaddr; |
| 591 | |
Heinrich Schuchardt | 3369158 | 2019-08-14 06:49:09 +0200 | [diff] [blame] | 592 | /* |
| 593 | * The runtime services table is updated in |
| 594 | * efi_relocate_runtime_table() |
| 595 | */ |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 596 | if (map && efi_is_runtime_service_pointer(p)) |
| 597 | continue; |
| 598 | |
Heinrich Schuchardt | 56e82be | 2018-10-13 20:52:08 -0700 | [diff] [blame] | 599 | debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, |
| 600 | rel->info, *p, rel->offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 601 | |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 602 | switch (rel->info & R_MASK) { |
| 603 | case R_RELATIVE: |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 604 | #ifdef IS_RELA |
| 605 | newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE; |
| 606 | #else |
| 607 | newaddr = *p - lastoff + offset; |
| 608 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 609 | break; |
| 610 | #ifdef R_ABSOLUTE |
| 611 | case R_ABSOLUTE: { |
| 612 | ulong symidx = rel->info >> SYM_INDEX; |
| 613 | extern struct dyn_sym __dyn_sym_start[]; |
| 614 | newaddr = __dyn_sym_start[symidx].addr + offset; |
Alexander Graf | df0f6c7 | 2018-11-04 22:25:22 +0100 | [diff] [blame] | 615 | #ifdef IS_RELA |
| 616 | newaddr -= CONFIG_SYS_TEXT_BASE; |
| 617 | #endif |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 618 | break; |
| 619 | } |
| 620 | #endif |
| 621 | default: |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 622 | printf("%s: Unknown relocation type %llx\n", |
| 623 | __func__, rel->info & R_MASK); |
Rick Chen | 9677a37 | 2018-05-28 19:06:37 +0800 | [diff] [blame] | 624 | continue; |
| 625 | } |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 626 | |
| 627 | /* Check if the relocation is inside bounds */ |
| 628 | if (map && ((newaddr < map->virtual_start) || |
Heinrich Schuchardt | 9ebc47f | 2017-09-18 22:11:34 +0200 | [diff] [blame] | 629 | newaddr > (map->virtual_start + |
| 630 | (map->num_pages << EFI_PAGE_SHIFT)))) { |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 631 | printf("%s: Relocation at %p is out of range (%lx)\n", |
| 632 | __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 633 | continue; |
| 634 | } |
| 635 | |
Alexander Graf | 62a6748 | 2016-06-02 11:38:27 +0200 | [diff] [blame] | 636 | debug("%s: Setting %p to %lx\n", __func__, p, newaddr); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 637 | *p = newaddr; |
Alexander Graf | e570232 | 2016-04-11 23:20:39 +0200 | [diff] [blame] | 638 | flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1), |
| 639 | ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE)); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | #ifndef IS_RELA |
| 643 | lastoff = offset; |
| 644 | #endif |
| 645 | |
| 646 | invalidate_icache_all(); |
| 647 | } |
| 648 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 649 | /** |
| 650 | * efi_set_virtual_address_map() - change from physical to virtual mapping |
| 651 | * |
| 652 | * This function implements the SetVirtualAddressMap() runtime service. |
| 653 | * |
| 654 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 655 | * details. |
| 656 | * |
| 657 | * @memory_map_size: size of the virtual map |
| 658 | * @descriptor_size: size of an entry in the map |
| 659 | * @descriptor_version: version of the map entries |
| 660 | * @virtmap: virtual address mapping information |
| 661 | * Return: status code |
| 662 | */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 663 | static efi_status_t EFIAPI efi_set_virtual_address_map( |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 664 | efi_uintn_t memory_map_size, |
| 665 | efi_uintn_t descriptor_size, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 666 | uint32_t descriptor_version, |
| 667 | struct efi_mem_desc *virtmap) |
| 668 | { |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 669 | efi_uintn_t n = memory_map_size / descriptor_size; |
| 670 | efi_uintn_t i; |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 671 | efi_status_t ret = EFI_INVALID_PARAMETER; |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 672 | int rt_code_sections = 0; |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 673 | struct efi_event *event; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 674 | |
Heinrich Schuchardt | f8599dc | 2019-07-27 20:28:47 +0200 | [diff] [blame] | 675 | EFI_ENTRY("%zx %zx %x %p", memory_map_size, descriptor_size, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 676 | descriptor_version, virtmap); |
| 677 | |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 678 | if (descriptor_version != EFI_MEMORY_DESCRIPTOR_VERSION || |
| 679 | descriptor_size < sizeof(struct efi_mem_desc)) |
| 680 | goto out; |
| 681 | |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 682 | efi_virtmap = virtmap; |
| 683 | efi_descriptor_size = descriptor_size; |
| 684 | efi_descriptor_count = n; |
| 685 | |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 686 | /* |
| 687 | * TODO: |
| 688 | * Further down we are cheating. While really we should implement |
| 689 | * SetVirtualAddressMap() events and ConvertPointer() to allow |
| 690 | * dynamically loaded drivers to expose runtime services, we don't |
| 691 | * today. |
| 692 | * |
| 693 | * So let's ensure we see exactly one single runtime section, as |
| 694 | * that is the built-in one. If we see more (or less), someone must |
| 695 | * have tried adding or removing to that which we don't support yet. |
| 696 | * In that case, let's better fail rather than expose broken runtime |
| 697 | * services. |
| 698 | */ |
| 699 | for (i = 0; i < n; i++) { |
| 700 | struct efi_mem_desc *map = (void*)virtmap + |
| 701 | (descriptor_size * i); |
| 702 | |
| 703 | if (map->type == EFI_RUNTIME_SERVICES_CODE) |
| 704 | rt_code_sections++; |
| 705 | } |
| 706 | |
| 707 | if (rt_code_sections != 1) { |
| 708 | /* |
| 709 | * We expose exactly one single runtime code section, so |
| 710 | * something is definitely going wrong. |
| 711 | */ |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 712 | goto out; |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 713 | } |
| 714 | |
Heinrich Schuchardt | 4429d87 | 2019-07-11 20:15:09 +0200 | [diff] [blame] | 715 | /* Notify EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE */ |
| 716 | list_for_each_entry(event, &efi_events, link) { |
| 717 | if (event->notify_function) |
| 718 | EFI_CALL_VOID(event->notify_function( |
| 719 | event, event->notify_context)); |
| 720 | } |
| 721 | |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 722 | /* Rebind mmio pointers */ |
| 723 | for (i = 0; i < n; i++) { |
| 724 | struct efi_mem_desc *map = (void*)virtmap + |
| 725 | (descriptor_size * i); |
| 726 | struct list_head *lhandle; |
| 727 | efi_physical_addr_t map_start = map->physical_start; |
| 728 | efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT; |
| 729 | efi_physical_addr_t map_end = map_start + map_len; |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 730 | u64 off = map->virtual_start - map_start; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 731 | |
| 732 | /* Adjust all mmio pointers in this region */ |
| 733 | list_for_each(lhandle, &efi_runtime_mmio) { |
| 734 | struct efi_runtime_mmio_list *lmmio; |
| 735 | |
| 736 | lmmio = list_entry(lhandle, |
| 737 | struct efi_runtime_mmio_list, |
| 738 | link); |
| 739 | if ((map_start <= lmmio->paddr) && |
| 740 | (map_end >= lmmio->paddr)) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 741 | uintptr_t new_addr = lmmio->paddr + off; |
| 742 | *lmmio->ptr = (void *)new_addr; |
| 743 | } |
| 744 | } |
Heinrich Schuchardt | 3905456 | 2018-08-04 23:16:06 +0200 | [diff] [blame] | 745 | if ((map_start <= (uintptr_t)systab.tables) && |
| 746 | (map_end >= (uintptr_t)systab.tables)) { |
| 747 | char *ptr = (char *)systab.tables; |
| 748 | |
| 749 | ptr += off; |
| 750 | systab.tables = (struct efi_configuration_table *)ptr; |
| 751 | } |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 754 | /* Relocate the runtime. See TODO above */ |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 755 | for (i = 0; i < n; i++) { |
| 756 | struct efi_mem_desc *map; |
| 757 | |
| 758 | map = (void*)virtmap + (descriptor_size * i); |
| 759 | if (map->type == EFI_RUNTIME_SERVICES_CODE) { |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 760 | ulong new_offset = map->virtual_start - |
Alexander Graf | 86e0021 | 2018-12-11 10:00:42 +0100 | [diff] [blame] | 761 | map->physical_start + gd->relocaddr; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 762 | |
Heinrich Schuchardt | 19cd80b | 2019-06-20 22:00:02 +0000 | [diff] [blame] | 763 | efi_relocate_runtime_table(new_offset); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 764 | efi_runtime_relocate(new_offset, map); |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 765 | ret = EFI_SUCCESS; |
| 766 | goto out; |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Heinrich Schuchardt | c70a161 | 2019-08-14 05:19:37 +0200 | [diff] [blame] | 770 | out: |
| 771 | return EFI_EXIT(ret); |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 772 | } |
| 773 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 774 | /** |
| 775 | * efi_add_runtime_mmio() - add memory-mapped IO region |
| 776 | * |
| 777 | * This function adds a memory-mapped IO region to the memory map to make it |
| 778 | * available at runtime. |
| 779 | * |
Heinrich Schuchardt | 7e30764 | 2018-12-23 02:35:13 +0100 | [diff] [blame] | 780 | * @mmio_ptr: pointer to a pointer to the start of the memory-mapped |
| 781 | * IO region |
Heinrich Schuchardt | 1112154 | 2018-09-03 19:29:41 +0200 | [diff] [blame] | 782 | * @len: size of the memory-mapped IO region |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 783 | * Returns: status code |
| 784 | */ |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 785 | efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len) |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 786 | { |
| 787 | struct efi_runtime_mmio_list *newmmio; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 788 | uint64_t addr = *(uintptr_t *)mmio_ptr; |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 789 | efi_status_t ret; |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 790 | |
Michael Walle | 282d386 | 2020-05-17 12:29:19 +0200 | [diff] [blame] | 791 | ret = efi_add_memory_map(addr, len, EFI_MMAP_IO); |
Bryan O'Donoghue | 89a22b4 | 2019-07-15 12:00:39 +0100 | [diff] [blame] | 792 | if (ret != EFI_SUCCESS) |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 793 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 794 | |
| 795 | newmmio = calloc(1, sizeof(*newmmio)); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 796 | if (!newmmio) |
| 797 | return EFI_OUT_OF_RESOURCES; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 798 | newmmio->ptr = mmio_ptr; |
| 799 | newmmio->paddr = *(uintptr_t *)mmio_ptr; |
| 800 | newmmio->len = len; |
| 801 | list_add_tail(&newmmio->link, &efi_runtime_mmio); |
Heinrich Schuchardt | 099b3b7 | 2018-03-03 15:28:59 +0100 | [diff] [blame] | 802 | |
Alexander Graf | 33a83a3 | 2018-03-15 15:08:16 +0100 | [diff] [blame] | 803 | return EFI_SUCCESS; |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 806 | /* |
| 807 | * In the second stage, U-Boot has disappeared. To isolate our runtime code |
| 808 | * that at this point still exists from the rest, we put it into a special |
| 809 | * section. |
| 810 | * |
| 811 | * !!WARNING!! |
| 812 | * |
| 813 | * This means that we can not rely on any code outside of this file in any |
| 814 | * function or variable below this line. |
| 815 | * |
| 816 | * Please keep everything fully self-contained and annotated with |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 817 | * __efi_runtime and __efi_runtime_data markers. |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 818 | */ |
| 819 | |
| 820 | /* |
| 821 | * Relocate the EFI runtime stub to a different place. We need to call this |
| 822 | * the first time we expose the runtime interface to a user and on set virtual |
| 823 | * address map calls. |
| 824 | */ |
| 825 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 826 | /** |
| 827 | * efi_unimplemented() - replacement function, returns EFI_UNSUPPORTED |
| 828 | * |
| 829 | * This function is used after SetVirtualAddressMap() is called as replacement |
| 830 | * for services that are not available anymore due to constraints of the U-Boot |
| 831 | * implementation. |
| 832 | * |
| 833 | * Return: EFI_UNSUPPORTED |
| 834 | */ |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 835 | static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void) |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 836 | { |
| 837 | return EFI_UNSUPPORTED; |
| 838 | } |
| 839 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 840 | /** |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 841 | * efi_update_capsule() - process information from operating system |
| 842 | * |
| 843 | * This function implements the UpdateCapsule() runtime service. |
| 844 | * |
| 845 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 846 | * details. |
| 847 | * |
| 848 | * @capsule_header_array: pointer to array of virtual pointers |
| 849 | * @capsule_count: number of pointers in capsule_header_array |
| 850 | * @scatter_gather_list: pointer to arry of physical pointers |
| 851 | * Returns: status code |
| 852 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 853 | efi_status_t __efi_runtime EFIAPI efi_update_capsule( |
| 854 | struct efi_capsule_header **capsule_header_array, |
| 855 | efi_uintn_t capsule_count, |
| 856 | u64 scatter_gather_list) |
| 857 | { |
| 858 | return EFI_UNSUPPORTED; |
| 859 | } |
| 860 | |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 861 | /** |
| 862 | * efi_query_capsule_caps() - check if capsule is supported |
| 863 | * |
| 864 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 865 | * |
| 866 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 867 | * details. |
| 868 | * |
| 869 | * @capsule_header_array: pointer to array of virtual pointers |
| 870 | * @capsule_count: number of pointers in capsule_header_array |
Heinrich Schuchardt | 1d1be36 | 2018-09-03 20:59:25 +0200 | [diff] [blame] | 871 | * @maximum_capsule_size: maximum capsule size |
Heinrich Schuchardt | 711853b | 2018-07-29 15:10:11 +0200 | [diff] [blame] | 872 | * @reset_type: type of reset needed for capsule update |
| 873 | * Returns: status code |
| 874 | */ |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 875 | efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps( |
| 876 | struct efi_capsule_header **capsule_header_array, |
| 877 | efi_uintn_t capsule_count, |
AKASHI Takahiro | d840b67 | 2018-11-14 16:18:53 +0900 | [diff] [blame] | 878 | u64 *maximum_capsule_size, |
| 879 | u32 *reset_type) |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 880 | { |
| 881 | return EFI_UNSUPPORTED; |
| 882 | } |
| 883 | |
Alexander Graf | 393dd91 | 2016-10-14 13:45:30 +0200 | [diff] [blame] | 884 | struct efi_runtime_services __efi_runtime_data efi_runtime_services = { |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 885 | .hdr = { |
| 886 | .signature = EFI_RUNTIME_SERVICES_SIGNATURE, |
Heinrich Schuchardt | e75b3cb | 2018-06-28 12:45:27 +0200 | [diff] [blame] | 887 | .revision = EFI_SPECIFICATION_VERSION, |
Heinrich Schuchardt | 1020425 | 2018-06-28 12:45:29 +0200 | [diff] [blame] | 888 | .headersize = sizeof(struct efi_runtime_services), |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 889 | }, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 890 | .get_time = &efi_get_time_boottime, |
Heinrich Schuchardt | 411b2d5 | 2019-05-19 20:07:39 +0200 | [diff] [blame] | 891 | .set_time = &efi_set_time_boottime, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 892 | .get_wakeup_time = (void *)&efi_unimplemented, |
| 893 | .set_wakeup_time = (void *)&efi_unimplemented, |
| 894 | .set_virtual_address_map = &efi_set_virtual_address_map, |
Heinrich Schuchardt | c680dc4 | 2019-07-27 20:35:24 +0200 | [diff] [blame] | 895 | .convert_pointer = efi_convert_pointer, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 896 | .get_variable = efi_get_variable, |
Heinrich Schuchardt | d6a6baa | 2018-05-17 07:57:05 +0200 | [diff] [blame] | 897 | .get_next_variable_name = efi_get_next_variable_name, |
Rob Clark | 15f3d74 | 2017-09-13 18:05:37 -0400 | [diff] [blame] | 898 | .set_variable = efi_set_variable, |
Heinrich Schuchardt | 5337a6c | 2019-06-20 15:40:49 +0200 | [diff] [blame] | 899 | .get_next_high_mono_count = (void *)&efi_unimplemented, |
Alexander Graf | 7725609 | 2016-08-16 21:08:45 +0200 | [diff] [blame] | 900 | .reset_system = &efi_reset_system_boottime, |
Heinrich Schuchardt | 897c072 | 2018-02-09 20:41:21 +0100 | [diff] [blame] | 901 | .update_capsule = efi_update_capsule, |
| 902 | .query_capsule_caps = efi_query_capsule_caps, |
| 903 | .query_variable_info = efi_query_variable_info, |
Alexander Graf | 0bd425a | 2016-03-04 01:10:01 +0100 | [diff] [blame] | 904 | }; |