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