efi_loader: update runtime services table crc32
[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 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 structs should come from the elf
34  * header for each arch (or a generic header) rather than being repeated here.
35  */
36 #if defined(__aarch64__)
37 #define R_RELATIVE      R_AARCH64_RELATIVE
38 #define R_MASK          0xffffffffULL
39 #define IS_RELA         1
40 #elif defined(__arm__)
41 #define R_RELATIVE      R_ARM_RELATIVE
42 #define R_MASK          0xffULL
43 #elif defined(__x86_64__) || defined(__i386__)
44 #define R_RELATIVE      R_386_RELATIVE
45 #define R_MASK          0xffULL
46 #elif defined(__riscv)
47 #define R_RELATIVE      R_RISCV_RELATIVE
48 #define R_MASK          0xffULL
49 #define IS_RELA         1
50
51 struct dyn_sym {
52         ulong foo1;
53         ulong addr;
54         u32 foo2;
55         u32 foo3;
56 };
57 #if (__riscv_xlen == 32)
58 #define R_ABSOLUTE      R_RISCV_32
59 #define SYM_INDEX       8
60 #elif (__riscv_xlen == 64)
61 #define R_ABSOLUTE      R_RISCV_64
62 #define SYM_INDEX       32
63 #else
64 #error unknown riscv target
65 #endif
66 #else
67 #error Need to add relocation awareness
68 #endif
69
70 struct elf_rel {
71         ulong *offset;
72         ulong info;
73 };
74
75 struct elf_rela {
76         ulong *offset;
77         ulong info;
78         long addend;
79 };
80
81 /*
82  * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
83  * payload are running concurrently at the same time. In this mode, we can
84  * handle a good number of runtime callbacks
85  */
86
87 /**
88  * efi_update_table_header_crc32() - Update crc32 in table header
89  *
90  * @table:      EFI table
91  */
92 void __efi_runtime efi_update_table_header_crc32(struct efi_table_hdr *table)
93 {
94         table->crc32 = 0;
95         table->crc32 = crc32(0, (const unsigned char *)table,
96                              table->headersize);
97 }
98
99 static void EFIAPI efi_reset_system_boottime(
100                         enum efi_reset_type reset_type,
101                         efi_status_t reset_status,
102                         unsigned long data_size, void *reset_data)
103 {
104         struct efi_event *evt;
105
106         EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
107                   reset_data);
108
109         /* Notify reset */
110         list_for_each_entry(evt, &efi_events, link) {
111                 if (evt->group &&
112                     !guidcmp(evt->group,
113                              &efi_guid_event_group_reset_system)) {
114                         efi_signal_event(evt, false);
115                         break;
116                 }
117         }
118         switch (reset_type) {
119         case EFI_RESET_COLD:
120         case EFI_RESET_WARM:
121         case EFI_RESET_PLATFORM_SPECIFIC:
122                 do_reset(NULL, 0, 0, NULL);
123                 break;
124         case EFI_RESET_SHUTDOWN:
125                 /* We don't have anything to map this to */
126                 break;
127         }
128
129         while (1) { }
130 }
131
132 /**
133  * efi_get_time_boottime - get current time
134  *
135  * This function implements the GetTime runtime service.
136  * See the Unified Extensible Firmware Interface (UEFI) specification
137  * for details.
138  *
139  * @time:               pointer to structure to receive current time
140  * @capabilities:       pointer to structure to receive RTC properties
141  * Return Value:        status code
142  */
143 static efi_status_t EFIAPI efi_get_time_boottime(
144                         struct efi_time *time,
145                         struct efi_time_cap *capabilities)
146 {
147 #ifdef CONFIG_DM_RTC
148         efi_status_t ret = EFI_SUCCESS;
149         int r;
150         struct rtc_time tm;
151         struct udevice *dev;
152
153         EFI_ENTRY("%p %p", time, capabilities);
154
155         if (!time) {
156                 ret = EFI_INVALID_PARAMETER;
157                 goto out;
158         }
159
160         r = uclass_get_device(UCLASS_RTC, 0, &dev);
161         if (!r)
162                 r = dm_rtc_get(dev, &tm);
163         if (r) {
164                 ret = EFI_DEVICE_ERROR;
165                 goto out;
166         }
167
168         memset(time, 0, sizeof(*time));
169         time->year = tm.tm_year;
170         time->month = tm.tm_mon;
171         time->day = tm.tm_mday;
172         time->hour = tm.tm_hour;
173         time->minute = tm.tm_min;
174         time->second = tm.tm_sec;
175         time->daylight = EFI_TIME_ADJUST_DAYLIGHT;
176         if (tm.tm_isdst > 0)
177                 time->daylight |= EFI_TIME_IN_DAYLIGHT;
178         time->timezone = EFI_UNSPECIFIED_TIMEZONE;
179
180         if (capabilities) {
181                 /* Set reasonable dummy values */
182                 capabilities->resolution = 1;           /* 1 Hz */
183                 capabilities->accuracy = 100000000;     /* 100 ppm */
184                 capabilities->sets_to_zero = false;
185         }
186 out:
187         return EFI_EXIT(ret);
188 #else
189         EFI_ENTRY("%p %p", time, capabilities);
190         return EFI_EXIT(EFI_DEVICE_ERROR);
191 #endif
192 }
193
194 /* Boards may override the helpers below to implement RTS functionality */
195
196 void __weak __efi_runtime EFIAPI efi_reset_system(
197                         enum efi_reset_type reset_type,
198                         efi_status_t reset_status,
199                         unsigned long data_size, void *reset_data)
200 {
201         /* Nothing we can do */
202         while (1) { }
203 }
204
205 efi_status_t __weak efi_reset_system_init(void)
206 {
207         return EFI_SUCCESS;
208 }
209
210 efi_status_t __weak __efi_runtime EFIAPI efi_get_time(
211                         struct efi_time *time,
212                         struct efi_time_cap *capabilities)
213 {
214         /* Nothing we can do */
215         return EFI_DEVICE_ERROR;
216 }
217
218 struct efi_runtime_detach_list_struct {
219         void *ptr;
220         void *patchto;
221 };
222
223 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
224         {
225                 /* do_reset is gone */
226                 .ptr = &efi_runtime_services.reset_system,
227                 .patchto = efi_reset_system,
228         }, {
229                 /* invalidate_*cache_all are gone */
230                 .ptr = &efi_runtime_services.set_virtual_address_map,
231                 .patchto = &efi_invalid_parameter,
232         }, {
233                 /* RTC accessors are gone */
234                 .ptr = &efi_runtime_services.get_time,
235                 .patchto = &efi_get_time,
236         }, {
237                 /* Clean up system table */
238                 .ptr = &systab.con_in,
239                 .patchto = NULL,
240         }, {
241                 /* Clean up system table */
242                 .ptr = &systab.con_out,
243                 .patchto = NULL,
244         }, {
245                 /* Clean up system table */
246                 .ptr = &systab.std_err,
247                 .patchto = NULL,
248         }, {
249                 /* Clean up system table */
250                 .ptr = &systab.boottime,
251                 .patchto = NULL,
252         }, {
253                 .ptr = &efi_runtime_services.get_variable,
254                 .patchto = &efi_device_error,
255         }, {
256                 .ptr = &efi_runtime_services.get_next_variable_name,
257                 .patchto = &efi_device_error,
258         }, {
259                 .ptr = &efi_runtime_services.set_variable,
260                 .patchto = &efi_device_error,
261         }
262 };
263
264 static bool efi_runtime_tobedetached(void *p)
265 {
266         int i;
267
268         for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
269                 if (efi_runtime_detach_list[i].ptr == p)
270                         return true;
271
272         return false;
273 }
274
275 static void efi_runtime_detach(ulong offset)
276 {
277         int i;
278         ulong patchoff = offset - (ulong)gd->relocaddr;
279
280         for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
281                 ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
282                 ulong *p = efi_runtime_detach_list[i].ptr;
283                 ulong newaddr = patchto ? (patchto + patchoff) : 0;
284
285                 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
286                 *p = newaddr;
287         }
288
289         /* Update crc32 */
290         efi_update_table_header_crc32(&efi_runtime_services.hdr);
291 }
292
293 /* Relocate EFI runtime to uboot_reloc_base = offset */
294 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
295 {
296 #ifdef IS_RELA
297         struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
298 #else
299         struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
300         static ulong lastoff = CONFIG_SYS_TEXT_BASE;
301 #endif
302
303         debug("%s: Relocating to offset=%lx\n", __func__, offset);
304         for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
305                 ulong base = CONFIG_SYS_TEXT_BASE;
306                 ulong *p;
307                 ulong newaddr;
308
309                 p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
310
311                 debug("%s: rel->info=%#lx *p=%#lx rel->offset=%p\n", __func__, rel->info, *p, rel->offset);
312
313                 switch (rel->info & R_MASK) {
314                 case R_RELATIVE:
315 #ifdef IS_RELA
316                 newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
317 #else
318                 newaddr = *p - lastoff + offset;
319 #endif
320                         break;
321 #ifdef R_ABSOLUTE
322                 case R_ABSOLUTE: {
323                         ulong symidx = rel->info >> SYM_INDEX;
324                         extern struct dyn_sym __dyn_sym_start[];
325                         newaddr = __dyn_sym_start[symidx].addr + offset;
326                         break;
327                 }
328 #endif
329                 default:
330                         continue;
331                 }
332
333                 /* Check if the relocation is inside bounds */
334                 if (map && ((newaddr < map->virtual_start) ||
335                     newaddr > (map->virtual_start +
336                               (map->num_pages << EFI_PAGE_SHIFT)))) {
337                         if (!efi_runtime_tobedetached(p))
338                                 printf("U-Boot EFI: Relocation at %p is out of "
339                                        "range (%lx)\n", p, newaddr);
340                         continue;
341                 }
342
343                 debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
344                 *p = newaddr;
345                 flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
346                         ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
347         }
348
349 #ifndef IS_RELA
350         lastoff = offset;
351 #endif
352
353         invalidate_icache_all();
354 }
355
356 static efi_status_t EFIAPI efi_set_virtual_address_map(
357                         unsigned long memory_map_size,
358                         unsigned long descriptor_size,
359                         uint32_t descriptor_version,
360                         struct efi_mem_desc *virtmap)
361 {
362         ulong runtime_start = (ulong)&__efi_runtime_start &
363                               ~(ulong)EFI_PAGE_MASK;
364         int n = memory_map_size / descriptor_size;
365         int i;
366
367         EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
368                   descriptor_version, virtmap);
369
370         /* Rebind mmio pointers */
371         for (i = 0; i < n; i++) {
372                 struct efi_mem_desc *map = (void*)virtmap +
373                                            (descriptor_size * i);
374                 struct list_head *lhandle;
375                 efi_physical_addr_t map_start = map->physical_start;
376                 efi_physical_addr_t map_len = map->num_pages << EFI_PAGE_SHIFT;
377                 efi_physical_addr_t map_end = map_start + map_len;
378                 u64 off = map->virtual_start - map_start;
379
380                 /* Adjust all mmio pointers in this region */
381                 list_for_each(lhandle, &efi_runtime_mmio) {
382                         struct efi_runtime_mmio_list *lmmio;
383
384                         lmmio = list_entry(lhandle,
385                                            struct efi_runtime_mmio_list,
386                                            link);
387                         if ((map_start <= lmmio->paddr) &&
388                             (map_end >= lmmio->paddr)) {
389                                 uintptr_t new_addr = lmmio->paddr + off;
390                                 *lmmio->ptr = (void *)new_addr;
391                         }
392                 }
393                 if ((map_start <= (uintptr_t)systab.tables) &&
394                     (map_end >= (uintptr_t)systab.tables)) {
395                         char *ptr = (char *)systab.tables;
396
397                         ptr += off;
398                         systab.tables = (struct efi_configuration_table *)ptr;
399                 }
400         }
401
402         /* Move the actual runtime code over */
403         for (i = 0; i < n; i++) {
404                 struct efi_mem_desc *map;
405
406                 map = (void*)virtmap + (descriptor_size * i);
407                 if (map->type == EFI_RUNTIME_SERVICES_CODE) {
408                         ulong new_offset = map->virtual_start -
409                                            (runtime_start - gd->relocaddr);
410
411                         efi_runtime_relocate(new_offset, map);
412                         /* Once we're virtual, we can no longer handle
413                            complex callbacks */
414                         efi_runtime_detach(new_offset);
415                         return EFI_EXIT(EFI_SUCCESS);
416                 }
417         }
418
419         return EFI_EXIT(EFI_INVALID_PARAMETER);
420 }
421
422 efi_status_t efi_add_runtime_mmio(void *mmio_ptr, u64 len)
423 {
424         struct efi_runtime_mmio_list *newmmio;
425         u64 pages = (len + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
426         uint64_t addr = *(uintptr_t *)mmio_ptr;
427         uint64_t retaddr;
428
429         retaddr = efi_add_memory_map(addr, pages, EFI_MMAP_IO, false);
430         if (retaddr != addr)
431                 return EFI_OUT_OF_RESOURCES;
432
433         newmmio = calloc(1, sizeof(*newmmio));
434         if (!newmmio)
435                 return EFI_OUT_OF_RESOURCES;
436         newmmio->ptr = mmio_ptr;
437         newmmio->paddr = *(uintptr_t *)mmio_ptr;
438         newmmio->len = len;
439         list_add_tail(&newmmio->link, &efi_runtime_mmio);
440
441         return EFI_SUCCESS;
442 }
443
444 /*
445  * In the second stage, U-Boot has disappeared. To isolate our runtime code
446  * that at this point still exists from the rest, we put it into a special
447  * section.
448  *
449  *        !!WARNING!!
450  *
451  * This means that we can not rely on any code outside of this file in any
452  * function or variable below this line.
453  *
454  * Please keep everything fully self-contained and annotated with
455  * __efi_runtime and __efi_runtime_data markers.
456  */
457
458 /*
459  * Relocate the EFI runtime stub to a different place. We need to call this
460  * the first time we expose the runtime interface to a user and on set virtual
461  * address map calls.
462  */
463
464 static efi_status_t __efi_runtime EFIAPI efi_unimplemented(void)
465 {
466         return EFI_UNSUPPORTED;
467 }
468
469 static efi_status_t __efi_runtime EFIAPI efi_device_error(void)
470 {
471         return EFI_DEVICE_ERROR;
472 }
473
474 static efi_status_t __efi_runtime EFIAPI efi_invalid_parameter(void)
475 {
476         return EFI_INVALID_PARAMETER;
477 }
478
479 efi_status_t __efi_runtime EFIAPI efi_update_capsule(
480                         struct efi_capsule_header **capsule_header_array,
481                         efi_uintn_t capsule_count,
482                         u64 scatter_gather_list)
483 {
484         return EFI_UNSUPPORTED;
485 }
486
487 efi_status_t __efi_runtime EFIAPI efi_query_capsule_caps(
488                         struct efi_capsule_header **capsule_header_array,
489                         efi_uintn_t capsule_count,
490                         u64 maximum_capsule_size,
491                         u32 reset_type)
492 {
493         return EFI_UNSUPPORTED;
494 }
495
496 efi_status_t __efi_runtime EFIAPI efi_query_variable_info(
497                         u32 attributes,
498                         u64 *maximum_variable_storage_size,
499                         u64 *remaining_variable_storage_size,
500                         u64 *maximum_variable_size)
501 {
502         return EFI_UNSUPPORTED;
503 }
504
505 struct efi_runtime_services __efi_runtime_data efi_runtime_services = {
506         .hdr = {
507                 .signature = EFI_RUNTIME_SERVICES_SIGNATURE,
508                 .revision = EFI_SPECIFICATION_VERSION,
509                 .headersize = sizeof(struct efi_runtime_services),
510         },
511         .get_time = &efi_get_time_boottime,
512         .set_time = (void *)&efi_device_error,
513         .get_wakeup_time = (void *)&efi_unimplemented,
514         .set_wakeup_time = (void *)&efi_unimplemented,
515         .set_virtual_address_map = &efi_set_virtual_address_map,
516         .convert_pointer = (void *)&efi_invalid_parameter,
517         .get_variable = efi_get_variable,
518         .get_next_variable_name = efi_get_next_variable_name,
519         .set_variable = efi_set_variable,
520         .get_next_high_mono_count = (void *)&efi_device_error,
521         .reset_system = &efi_reset_system_boottime,
522         .update_capsule = efi_update_capsule,
523         .query_capsule_caps = efi_query_capsule_caps,
524         .query_variable_info = efi_query_variable_info,
525 };