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