1/*-
2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016, 2018 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/efi.h>
37#include <sys/kernel.h>
38#include <sys/linker.h>
39#include <sys/lock.h>
40#include <sys/module.h>
41#include <sys/mutex.h>
42#include <sys/clock.h>
43#include <sys/proc.h>
44#include <sys/rwlock.h>
45#include <sys/sched.h>
46#include <sys/sysctl.h>
47#include <sys/systm.h>
48#include <sys/vmmeter.h>
49
50#include <machine/fpu.h>
51#include <machine/efi.h>
52#include <machine/metadata.h>
53#include <machine/vmparam.h>
54
55#include <vm/vm.h>
56#include <vm/pmap.h>
57#include <vm/vm_map.h>
58
59static struct efi_systbl *efi_systbl;
60/*
61 * The following pointers point to tables in the EFI runtime service data pages.
62 * Care should be taken to make sure that we've properly entered the EFI runtime
63 * environment (efi_enter()) before dereferencing them.
64 */
65static struct efi_cfgtbl *efi_cfgtbl;
66static struct efi_rt *efi_runtime;
67
68static int efi_status2err[25] = {
69	0,		/* EFI_SUCCESS */
70	ENOEXEC,	/* EFI_LOAD_ERROR */
71	EINVAL,		/* EFI_INVALID_PARAMETER */
72	ENOSYS,		/* EFI_UNSUPPORTED */
73	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
74	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
75	EBUSY,		/* EFI_NOT_READY */
76	EIO,		/* EFI_DEVICE_ERROR */
77	EROFS,		/* EFI_WRITE_PROTECTED */
78	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
79	EIO,		/* EFI_VOLUME_CORRUPTED */
80	ENOSPC,		/* EFI_VOLUME_FULL */
81	ENXIO,		/* EFI_NO_MEDIA */
82	ESTALE,		/* EFI_MEDIA_CHANGED */
83	ENOENT,		/* EFI_NOT_FOUND */
84	EACCES,		/* EFI_ACCESS_DENIED */
85	ETIMEDOUT,	/* EFI_NO_RESPONSE */
86	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
87	ETIMEDOUT,	/* EFI_TIMEOUT */
88	EDOOFUS,	/* EFI_NOT_STARTED */
89	EALREADY,	/* EFI_ALREADY_STARTED */
90	ECANCELED,	/* EFI_ABORTED */
91	EPROTO,		/* EFI_ICMP_ERROR */
92	EPROTO,		/* EFI_TFTP_ERROR */
93	EPROTO		/* EFI_PROTOCOL_ERROR */
94};
95
96static int efi_enter(void);
97static void efi_leave(void);
98
99static int
100efi_status_to_errno(efi_status status)
101{
102	u_long code;
103
104	code = status & 0x3ffffffffffffffful;
105	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
106}
107
108static struct mtx efi_lock;
109
110static bool
111efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
112{
113	struct efi_md *p;
114	int i;
115
116	for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
117	    descsz)) {
118		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
119			continue;
120
121		if (addr >= (uintptr_t)p->md_virt &&
122		    addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE)
123			return (true);
124	}
125
126	return (false);
127}
128
129static int
130efi_init(void)
131{
132	struct efi_map_header *efihdr;
133	struct efi_md *map;
134	struct efi_rt *rtdm;
135	caddr_t kmdp;
136	size_t efisz;
137	int ndesc, rt_disabled;
138
139	rt_disabled = 0;
140	TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
141	if (rt_disabled == 1)
142		return (0);
143	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
144
145	if (efi_systbl_phys == 0) {
146		if (bootverbose)
147			printf("EFI systbl not available\n");
148		return (0);
149	}
150
151	efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
152	if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
153		efi_systbl = NULL;
154		if (bootverbose)
155			printf("EFI systbl signature invalid\n");
156		return (0);
157	}
158	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
159	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
160	if (efi_cfgtbl == NULL) {
161		if (bootverbose)
162			printf("EFI config table is not present\n");
163	}
164
165	kmdp = preload_search_by_type("elf kernel");
166	if (kmdp == NULL)
167		kmdp = preload_search_by_type("elf64 kernel");
168	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
169	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
170	if (efihdr == NULL) {
171		if (bootverbose)
172			printf("EFI map is not present\n");
173		return (0);
174	}
175	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
176	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
177	if (efihdr->descriptor_size == 0)
178		return (ENOMEM);
179
180	ndesc = efihdr->memory_size / efihdr->descriptor_size;
181	if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
182		if (bootverbose)
183			printf("EFI cannot create runtime map\n");
184		return (ENOMEM);
185	}
186
187	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
188	    (struct efi_rt *)efi_systbl->st_rt;
189	if (efi_runtime == NULL) {
190		if (bootverbose)
191			printf("EFI runtime services table is not present\n");
192		efi_destroy_1t1_map();
193		return (ENXIO);
194	}
195
196#if defined(__aarch64__) || defined(__amd64__)
197	/*
198	 * Some UEFI implementations have multiple implementations of the
199	 * RS->GetTime function. They switch from one we can only use early
200	 * in the boot process to one valid as a RunTime service only when we
201	 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
202	 * with an old loader.efi, check if the RS->GetTime function is within
203	 * the EFI map, and fail to attach if not.
204	 */
205	rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
206	if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
207	    (vm_offset_t)rtdm->rt_gettime)) {
208		if (bootverbose)
209			printf(
210			 "EFI runtime services table has an invalid pointer\n");
211		efi_runtime = NULL;
212		efi_destroy_1t1_map();
213		return (ENXIO);
214	}
215#endif
216
217	return (0);
218}
219
220static void
221efi_uninit(void)
222{
223
224	/* Most likely disabled by tunable */
225	if (efi_runtime == NULL)
226		return;
227	efi_destroy_1t1_map();
228
229	efi_systbl = NULL;
230	efi_cfgtbl = NULL;
231	efi_runtime = NULL;
232
233	mtx_destroy(&efi_lock);
234}
235
236int
237efi_rt_ok(void)
238{
239
240	if (efi_runtime == NULL)
241		return (ENXIO);
242	return (0);
243}
244
245static int
246efi_enter(void)
247{
248	struct thread *td;
249	pmap_t curpmap;
250	int error;
251
252	if (efi_runtime == NULL)
253		return (ENXIO);
254	td = curthread;
255	curpmap = &td->td_proc->p_vmspace->vm_pmap;
256	PMAP_LOCK(curpmap);
257	mtx_lock(&efi_lock);
258	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
259	error = efi_arch_enter();
260	if (error != 0) {
261		fpu_kern_leave(td, NULL);
262		mtx_unlock(&efi_lock);
263		PMAP_UNLOCK(curpmap);
264	}
265	return (error);
266}
267
268static void
269efi_leave(void)
270{
271	struct thread *td;
272	pmap_t curpmap;
273
274	efi_arch_leave();
275
276	curpmap = &curproc->p_vmspace->vm_pmap;
277	td = curthread;
278	fpu_kern_leave(td, NULL);
279	mtx_unlock(&efi_lock);
280	PMAP_UNLOCK(curpmap);
281}
282
283int
284efi_get_table(struct uuid *uuid, void **ptr)
285{
286	struct efi_cfgtbl *ct;
287	u_long count;
288	int error;
289
290	if (efi_cfgtbl == NULL || efi_systbl == NULL)
291		return (ENXIO);
292	error = efi_enter();
293	if (error != 0)
294		return (error);
295	count = efi_systbl->st_entries;
296	ct = efi_cfgtbl;
297	while (count--) {
298		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
299			*ptr = ct->ct_data;
300			efi_leave();
301			return (0);
302		}
303		ct++;
304	}
305
306	efi_leave();
307	return (ENOENT);
308}
309
310static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
311SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
312    &efi_rt_handle_faults, 0,
313    "Call EFI RT methods with fault handler wrapper around");
314
315static int
316efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
317{
318
319	switch (ec->ec_argcnt) {
320	case 0:
321		ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
322		break;
323	case 1:
324		ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
325		    (ec->ec_arg1);
326		break;
327	case 2:
328		ec->ec_efi_status = ((register_t (*)(register_t, register_t))
329		    ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
330		break;
331	case 3:
332		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
333		    register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
334		    ec->ec_arg3);
335		break;
336	case 4:
337		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
338		    register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
339		    ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
340		break;
341	case 5:
342		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
343		    register_t, register_t, register_t))ec->ec_fptr)(
344		    ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
345		    ec->ec_arg5);
346		break;
347	default:
348		panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
349	}
350
351	return (0);
352}
353
354static int
355efi_call(struct efirt_callinfo *ecp)
356{
357	int error;
358
359	error = efi_enter();
360	if (error != 0)
361		return (error);
362	error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
363	    efi_rt_arch_call_nofault(ecp);
364	efi_leave();
365	if (error == 0)
366		error = efi_status_to_errno(ecp->ec_efi_status);
367	else if (bootverbose)
368		printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
369	return (error);
370}
371
372#define	EFI_RT_METHOD_PA(method)				\
373    ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)	\
374    efi_runtime))->method)
375
376static int
377efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
378{
379	struct efirt_callinfo ec;
380
381	EFI_TIME_OWNED();
382	if (efi_runtime == NULL)
383		return (ENXIO);
384	bzero(&ec, sizeof(ec));
385	ec.ec_name = "rt_gettime";
386	ec.ec_argcnt = 2;
387	ec.ec_arg1 = (uintptr_t)tm;
388	ec.ec_arg2 = (uintptr_t)tmcap;
389	ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
390	return (efi_call(&ec));
391}
392
393int
394efi_get_time(struct efi_tm *tm)
395{
396	struct efi_tmcap dummy;
397	int error;
398
399	if (efi_runtime == NULL)
400		return (ENXIO);
401	EFI_TIME_LOCK();
402	/*
403	 * UEFI spec states that the Capabilities argument to GetTime is
404	 * optional, but some UEFI implementations choke when passed a NULL
405	 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
406	 * to workaround such implementations.
407	 */
408	error = efi_get_time_locked(tm, &dummy);
409	EFI_TIME_UNLOCK();
410	return (error);
411}
412
413int
414efi_get_time_capabilities(struct efi_tmcap *tmcap)
415{
416	struct efi_tm dummy;
417	int error;
418
419	if (efi_runtime == NULL)
420		return (ENXIO);
421	EFI_TIME_LOCK();
422	error = efi_get_time_locked(&dummy, tmcap);
423	EFI_TIME_UNLOCK();
424	return (error);
425}
426
427int
428efi_reset_system(void)
429{
430	struct efirt_callinfo ec;
431
432	if (efi_runtime == NULL)
433		return (ENXIO);
434	bzero(&ec, sizeof(ec));
435	ec.ec_name = "rt_reset";
436	ec.ec_argcnt = 4;
437	ec.ec_arg1 = (uintptr_t)EFI_RESET_WARM;
438	ec.ec_arg2 = (uintptr_t)0;
439	ec.ec_arg3 = (uintptr_t)0;
440	ec.ec_arg4 = (uintptr_t)NULL;
441	ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
442	return (efi_call(&ec));
443}
444
445static int
446efi_set_time_locked(struct efi_tm *tm)
447{
448	struct efirt_callinfo ec;
449
450	EFI_TIME_OWNED();
451	if (efi_runtime == NULL)
452		return (ENXIO);
453	bzero(&ec, sizeof(ec));
454	ec.ec_name = "rt_settime";
455	ec.ec_argcnt = 1;
456	ec.ec_arg1 = (uintptr_t)tm;
457	ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
458	return (efi_call(&ec));
459}
460
461int
462efi_set_time(struct efi_tm *tm)
463{
464	int error;
465
466	if (efi_runtime == NULL)
467		return (ENXIO);
468	EFI_TIME_LOCK();
469	error = efi_set_time_locked(tm);
470	EFI_TIME_UNLOCK();
471	return (error);
472}
473
474int
475efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
476    size_t *datasize, void *data)
477{
478	struct efirt_callinfo ec;
479
480	if (efi_runtime == NULL)
481		return (ENXIO);
482	bzero(&ec, sizeof(ec));
483	ec.ec_argcnt = 5;
484	ec.ec_name = "rt_getvar";
485	ec.ec_arg1 = (uintptr_t)name;
486	ec.ec_arg2 = (uintptr_t)vendor;
487	ec.ec_arg3 = (uintptr_t)attrib;
488	ec.ec_arg4 = (uintptr_t)datasize;
489	ec.ec_arg5 = (uintptr_t)data;
490	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
491	return (efi_call(&ec));
492}
493
494int
495efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
496{
497	struct efirt_callinfo ec;
498
499	if (efi_runtime == NULL)
500		return (ENXIO);
501	bzero(&ec, sizeof(ec));
502	ec.ec_argcnt = 3;
503	ec.ec_name = "rt_scanvar";
504	ec.ec_arg1 = (uintptr_t)namesize;
505	ec.ec_arg2 = (uintptr_t)name;
506	ec.ec_arg3 = (uintptr_t)vendor;
507	ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
508	return (efi_call(&ec));
509}
510
511int
512efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
513    size_t datasize, void *data)
514{
515	struct efirt_callinfo ec;
516
517	if (efi_runtime == NULL)
518		return (ENXIO);
519	bzero(&ec, sizeof(ec));
520	ec.ec_argcnt = 5;
521	ec.ec_name = "rt_setvar";
522	ec.ec_arg1 = (uintptr_t)name;
523	ec.ec_arg2 = (uintptr_t)vendor;
524	ec.ec_arg3 = (uintptr_t)attrib;
525	ec.ec_arg4 = (uintptr_t)datasize;
526	ec.ec_arg5 = (uintptr_t)data;
527	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
528	return (efi_call(&ec));
529}
530
531static int
532efirt_modevents(module_t m, int event, void *arg __unused)
533{
534
535	switch (event) {
536	case MOD_LOAD:
537		return (efi_init());
538
539	case MOD_UNLOAD:
540		efi_uninit();
541		return (0);
542
543	case MOD_SHUTDOWN:
544		return (0);
545
546	default:
547		return (EOPNOTSUPP);
548	}
549}
550
551static moduledata_t efirt_moddata = {
552	.name = "efirt",
553	.evhand = efirt_modevents,
554	.priv = NULL,
555};
556/* After fpuinitstate, before efidev */
557DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
558MODULE_VERSION(efirt, 1);
559