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