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