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