xref: /openbmc/u-boot/lib/efi_loader/efi_runtime.c (revision 2290fe06)
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 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void);
20 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void);
21 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void);
22 
23 #ifdef CONFIG_SYS_CACHELINE_SIZE
24 #define EFI_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
25 #else
26 /* Just use the greatest cache flush alignment requirement I'm aware of */
27 #define EFI_CACHELINE_SIZE 128
28 #endif
29 
30 #if defined(CONFIG_ARM64)
31 #define R_RELATIVE	1027
32 #define R_MASK		0xffffffffULL
33 #define IS_RELA		1
34 #elif defined(CONFIG_ARM)
35 #define R_RELATIVE	23
36 #define R_MASK		0xffULL
37 #else
38 #error Need to add relocation awareness
39 #endif
40 
41 struct elf_rel {
42 	ulong *offset;
43 	ulong info;
44 };
45 
46 struct elf_rela {
47 	ulong *offset;
48 	ulong info;
49 	long addend;
50 };
51 
52 /*
53  * EFI Runtime code lives in 2 stages. In the first stage, U-Boot and an EFI
54  * payload are running concurrently at the same time. In this mode, we can
55  * handle a good number of runtime callbacks
56  */
57 
58 static void EFIAPI efi_reset_system(enum efi_reset_type reset_type,
59 				    efi_status_t reset_status,
60 				    unsigned long data_size, void *reset_data)
61 {
62 	EFI_ENTRY("%d %lx %lx %p", reset_type, reset_status, data_size,
63 		  reset_data);
64 
65 	switch (reset_type) {
66 	case EFI_RESET_COLD:
67 	case EFI_RESET_WARM:
68 		do_reset(NULL, 0, 0, NULL);
69 		break;
70 	case EFI_RESET_SHUTDOWN:
71 		/* We don't have anything to map this to */
72 		break;
73 	}
74 
75 	EFI_EXIT(EFI_SUCCESS);
76 }
77 
78 static efi_status_t EFIAPI efi_get_time(struct efi_time *time,
79 					struct efi_time_cap *capabilities)
80 {
81 #if defined(CONFIG_CMD_DATE) && defined(CONFIG_DM_RTC)
82 	struct rtc_time tm;
83 	int r;
84 	struct udevice *dev;
85 
86 	EFI_ENTRY("%p %p", time, capabilities);
87 
88 	r = uclass_get_device(UCLASS_RTC, 0, &dev);
89 	if (r)
90 		return EFI_EXIT(EFI_DEVICE_ERROR);
91 
92 	r = dm_rtc_get(dev, &tm);
93 	if (r)
94 		return EFI_EXIT(EFI_DEVICE_ERROR);
95 
96 	memset(time, 0, sizeof(*time));
97 	time->year = tm.tm_year;
98 	time->month = tm.tm_mon;
99 	time->day = tm.tm_mday;
100 	time->hour = tm.tm_hour;
101 	time->minute = tm.tm_min;
102 	time->daylight = tm.tm_isdst;
103 
104 	return EFI_EXIT(EFI_SUCCESS);
105 #else
106 	return EFI_DEVICE_ERROR;
107 #endif
108 }
109 
110 struct efi_runtime_detach_list_struct {
111 	void *ptr;
112 	void *patchto;
113 };
114 
115 static const struct efi_runtime_detach_list_struct efi_runtime_detach_list[] = {
116 	{
117 		/* do_reset is gone */
118 		.ptr = &efi_runtime_services.reset_system,
119 		.patchto = NULL,
120 	}, {
121 		/* invalidate_*cache_all are gone */
122 		.ptr = &efi_runtime_services.set_virtual_address_map,
123 		.patchto = &efi_invalid_parameter,
124 	}, {
125 		/* RTC accessors are gone */
126 		.ptr = &efi_runtime_services.get_time,
127 		.patchto = &efi_device_error,
128 	}, {
129 		/* Clean up system table */
130 		.ptr = &systab.con_in,
131 		.patchto = NULL,
132 	}, {
133 		/* Clean up system table */
134 		.ptr = &systab.con_out,
135 		.patchto = NULL,
136 	}, {
137 		/* Clean up system table */
138 		.ptr = &systab.std_err,
139 		.patchto = NULL,
140 	}, {
141 		/* Clean up system table */
142 		.ptr = &systab.boottime,
143 		.patchto = NULL,
144 	},
145 };
146 
147 static bool efi_runtime_tobedetached(void *p)
148 {
149 	int i;
150 
151 	for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++)
152 		if (efi_runtime_detach_list[i].ptr == p)
153 			return true;
154 
155 	return false;
156 }
157 
158 static void efi_runtime_detach(ulong offset)
159 {
160 	int i;
161 	ulong patchoff = offset - (ulong)gd->relocaddr;
162 
163 	for (i = 0; i < ARRAY_SIZE(efi_runtime_detach_list); i++) {
164 		ulong patchto = (ulong)efi_runtime_detach_list[i].patchto;
165 		ulong *p = efi_runtime_detach_list[i].ptr;
166 		ulong newaddr = patchto ? (patchto + patchoff) : 0;
167 
168 		debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
169 		*p = newaddr;
170 	}
171 }
172 
173 /* Relocate EFI runtime to uboot_reloc_base = offset */
174 void efi_runtime_relocate(ulong offset, struct efi_mem_desc *map)
175 {
176 #ifdef IS_RELA
177 	struct elf_rela *rel = (void*)&__efi_runtime_rel_start;
178 #else
179 	struct elf_rel *rel = (void*)&__efi_runtime_rel_start;
180 	static ulong lastoff = CONFIG_SYS_TEXT_BASE;
181 #endif
182 
183 	debug("%s: Relocating to offset=%lx\n", __func__, offset);
184 	for (; (ulong)rel < (ulong)&__efi_runtime_rel_stop; rel++) {
185 		ulong base = CONFIG_SYS_TEXT_BASE;
186 		ulong *p;
187 		ulong newaddr;
188 
189 		p = (void*)((ulong)rel->offset - base) + gd->relocaddr;
190 
191 		if ((rel->info & R_MASK) != R_RELATIVE) {
192 			continue;
193 		}
194 
195 #ifdef IS_RELA
196 		newaddr = rel->addend + offset - CONFIG_SYS_TEXT_BASE;
197 #else
198 		newaddr = *p - lastoff + offset;
199 #endif
200 
201 		/* Check if the relocation is inside bounds */
202 		if (map && ((newaddr < map->virtual_start) ||
203 		    newaddr > (map->virtual_start + (map->num_pages << 12)))) {
204 			if (!efi_runtime_tobedetached(p))
205 				printf("U-Boot EFI: Relocation at %p is out of "
206 				       "range (%lx)\n", p, newaddr);
207 			continue;
208 		}
209 
210 		debug("%s: Setting %p to %lx\n", __func__, p, newaddr);
211 		*p = newaddr;
212 		flush_dcache_range((ulong)p & ~(EFI_CACHELINE_SIZE - 1),
213 			ALIGN((ulong)&p[1], EFI_CACHELINE_SIZE));
214 	}
215 
216 #ifndef IS_RELA
217 	lastoff = offset;
218 #endif
219 
220         invalidate_icache_all();
221 }
222 
223 static efi_status_t EFIAPI efi_set_virtual_address_map(
224 			unsigned long memory_map_size,
225 			unsigned long descriptor_size,
226 			uint32_t descriptor_version,
227 			struct efi_mem_desc *virtmap)
228 {
229 	ulong runtime_start = (ulong)&__efi_runtime_start & ~0xfffULL;
230 	int n = memory_map_size / descriptor_size;
231 	int i;
232 
233 	EFI_ENTRY("%lx %lx %x %p", memory_map_size, descriptor_size,
234 		  descriptor_version, virtmap);
235 
236 	for (i = 0; i < n; i++) {
237 		struct efi_mem_desc *map;
238 
239 		map = (void*)virtmap + (descriptor_size * i);
240 		if (map->type == EFI_RUNTIME_SERVICES_CODE) {
241 			ulong new_offset = map->virtual_start - (runtime_start - gd->relocaddr);
242 
243 			efi_runtime_relocate(new_offset, map);
244 			/* Once we're virtual, we can no longer handle
245 			   complex callbacks */
246 			efi_runtime_detach(new_offset);
247 			return EFI_EXIT(EFI_SUCCESS);
248 		}
249 	}
250 
251 	return EFI_EXIT(EFI_INVALID_PARAMETER);
252 }
253 
254 /*
255  * In the second stage, U-Boot has disappeared. To isolate our runtime code
256  * that at this point still exists from the rest, we put it into a special
257  * section.
258  *
259  *        !!WARNING!!
260  *
261  * This means that we can not rely on any code outside of this file in any
262  * function or variable below this line.
263  *
264  * Please keep everything fully self-contained and annotated with
265  * EFI_RUNTIME_TEXT and EFI_RUNTIME_DATA markers.
266  */
267 
268 /*
269  * Relocate the EFI runtime stub to a different place. We need to call this
270  * the first time we expose the runtime interface to a user and on set virtual
271  * address map calls.
272  */
273 
274 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_unimplemented(void)
275 {
276 	return EFI_UNSUPPORTED;
277 }
278 
279 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_device_error(void)
280 {
281 	return EFI_DEVICE_ERROR;
282 }
283 
284 static efi_status_t EFI_RUNTIME_TEXT EFIAPI efi_invalid_parameter(void)
285 {
286 	return EFI_INVALID_PARAMETER;
287 }
288 
289 struct efi_runtime_services EFI_RUNTIME_DATA efi_runtime_services = {
290 	.hdr = {
291 		.signature = EFI_RUNTIME_SERVICES_SIGNATURE,
292 		.revision = EFI_RUNTIME_SERVICES_REVISION,
293 		.headersize = sizeof(struct efi_table_hdr),
294 	},
295 	.get_time = &efi_get_time,
296 	.set_time = (void *)&efi_device_error,
297 	.get_wakeup_time = (void *)&efi_unimplemented,
298 	.set_wakeup_time = (void *)&efi_unimplemented,
299 	.set_virtual_address_map = &efi_set_virtual_address_map,
300 	.convert_pointer = (void *)&efi_invalid_parameter,
301 	.get_variable = (void *)&efi_device_error,
302 	.get_next_variable = (void *)&efi_device_error,
303 	.set_variable = (void *)&efi_device_error,
304 	.get_next_high_mono_count = (void *)&efi_device_error,
305 	.reset_system = &efi_reset_system,
306 };
307