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