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