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