xref: /freebsd/sys/dev/efidev/efirt.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016 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 
59 static struct efi_systbl *efi_systbl;
60 static struct efi_cfgtbl *efi_cfgtbl;
61 static struct efi_rt *efi_runtime;
62 
63 static int efi_status2err[25] = {
64 	0,		/* EFI_SUCCESS */
65 	ENOEXEC,	/* EFI_LOAD_ERROR */
66 	EINVAL,		/* EFI_INVALID_PARAMETER */
67 	ENOSYS,		/* EFI_UNSUPPORTED */
68 	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
69 	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
70 	EBUSY,		/* EFI_NOT_READY */
71 	EIO,		/* EFI_DEVICE_ERROR */
72 	EROFS,		/* EFI_WRITE_PROTECTED */
73 	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
74 	EIO,		/* EFI_VOLUME_CORRUPTED */
75 	ENOSPC,		/* EFI_VOLUME_FULL */
76 	ENXIO,		/* EFI_NO_MEDIA */
77 	ESTALE,		/* EFI_MEDIA_CHANGED */
78 	ENOENT,		/* EFI_NOT_FOUND */
79 	EACCES,		/* EFI_ACCESS_DENIED */
80 	ETIMEDOUT,	/* EFI_NO_RESPONSE */
81 	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
82 	ETIMEDOUT,	/* EFI_TIMEOUT */
83 	EDOOFUS,	/* EFI_NOT_STARTED */
84 	EALREADY,	/* EFI_ALREADY_STARTED */
85 	ECANCELED,	/* EFI_ABORTED */
86 	EPROTO,		/* EFI_ICMP_ERROR */
87 	EPROTO,		/* EFI_TFTP_ERROR */
88 	EPROTO		/* EFI_PROTOCOL_ERROR */
89 };
90 
91 static int
92 efi_status_to_errno(efi_status status)
93 {
94 	u_long code;
95 
96 	code = status & 0x3ffffffffffffffful;
97 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
98 }
99 
100 static struct mtx efi_lock;
101 
102 static int
103 efi_init(void)
104 {
105 	struct efi_map_header *efihdr;
106 	struct efi_md *map;
107 	caddr_t kmdp;
108 	size_t efisz;
109 
110 	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
111 
112 	if (efi_systbl_phys == 0) {
113 		if (bootverbose)
114 			printf("EFI systbl not available\n");
115 		return (0);
116 	}
117 	if (!PMAP_HAS_DMAP) {
118 		if (bootverbose)
119 			printf("EFI systbl requires direct map\n");
120 		return (0);
121 	}
122 	efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
123 	if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
124 		efi_systbl = NULL;
125 		if (bootverbose)
126 			printf("EFI systbl signature invalid\n");
127 		return (0);
128 	}
129 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
130 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
131 	if (efi_cfgtbl == NULL) {
132 		if (bootverbose)
133 			printf("EFI config table is not present\n");
134 	}
135 
136 	kmdp = preload_search_by_type("elf kernel");
137 	if (kmdp == NULL)
138 		kmdp = preload_search_by_type("elf64 kernel");
139 	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
140 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
141 	if (efihdr == NULL) {
142 		if (bootverbose)
143 			printf("EFI map is not present\n");
144 		return (0);
145 	}
146 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
147 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
148 	if (efihdr->descriptor_size == 0)
149 		return (ENOMEM);
150 
151 	if (!efi_create_1t1_map(map, efihdr->memory_size /
152 	    efihdr->descriptor_size, efihdr->descriptor_size)) {
153 		if (bootverbose)
154 			printf("EFI cannot create runtime map\n");
155 		return (ENOMEM);
156 	}
157 
158 	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
159 	    (struct efi_rt *)efi_systbl->st_rt;
160 	if (efi_runtime == NULL) {
161 		if (bootverbose)
162 			printf("EFI runtime services table is not present\n");
163 		efi_destroy_1t1_map();
164 		return (ENXIO);
165 	}
166 
167 	return (0);
168 }
169 
170 static void
171 efi_uninit(void)
172 {
173 
174 	efi_destroy_1t1_map();
175 
176 	efi_systbl = NULL;
177 	efi_cfgtbl = NULL;
178 	efi_runtime = NULL;
179 
180 	mtx_destroy(&efi_lock);
181 }
182 
183 int
184 efi_rt_ok(void)
185 {
186 
187 	if (efi_runtime == NULL)
188 		return (ENXIO);
189 	return (0);
190 }
191 
192 static int
193 efi_enter(void)
194 {
195 	struct thread *td;
196 	pmap_t curpmap;
197 
198 	if (efi_runtime == NULL)
199 		return (ENXIO);
200 	td = curthread;
201 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
202 	PMAP_LOCK(curpmap);
203 	mtx_lock(&efi_lock);
204 	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
205 	return (efi_arch_enter());
206 }
207 
208 static void
209 efi_leave(void)
210 {
211 	struct thread *td;
212 	pmap_t curpmap;
213 
214 	efi_arch_leave();
215 
216 	curpmap = &curproc->p_vmspace->vm_pmap;
217 	td = curthread;
218 	fpu_kern_leave(td, NULL);
219 	mtx_unlock(&efi_lock);
220 	PMAP_UNLOCK(curpmap);
221 }
222 
223 int
224 efi_get_table(struct uuid *uuid, void **ptr)
225 {
226 	struct efi_cfgtbl *ct;
227 	u_long count;
228 
229 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
230 		return (ENXIO);
231 	count = efi_systbl->st_entries;
232 	ct = efi_cfgtbl;
233 	while (count--) {
234 		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
235 			*ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
236 			return (0);
237 		}
238 		ct++;
239 	}
240 	return (ENOENT);
241 }
242 
243 static int
244 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
245 {
246 	efi_status status;
247 	int error;
248 
249 	EFI_TIME_OWNED()
250 	error = efi_enter();
251 	if (error != 0)
252 		return (error);
253 	status = efi_runtime->rt_gettime(tm, tmcap);
254 	efi_leave();
255 	error = efi_status_to_errno(status);
256 	return (error);
257 }
258 
259 int
260 efi_get_time(struct efi_tm *tm)
261 {
262 	int error;
263 
264 	if (efi_runtime == NULL)
265 		return (ENXIO);
266 	EFI_TIME_LOCK()
267 	error = efi_get_time_locked(tm, NULL);
268 	EFI_TIME_UNLOCK()
269 	return (error);
270 }
271 
272 int
273 efi_get_time_capabilities(struct efi_tmcap *tmcap)
274 {
275 	struct efi_tm dummy;
276 	int error;
277 
278 	if (efi_runtime == NULL)
279 		return (ENXIO);
280 	EFI_TIME_LOCK()
281 	error = efi_get_time_locked(&dummy, tmcap);
282 	EFI_TIME_UNLOCK()
283 	return (error);
284 }
285 
286 int
287 efi_reset_system(void)
288 {
289 	int error;
290 
291 	error = efi_enter();
292 	if (error != 0)
293 		return (error);
294 	efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
295 	efi_leave();
296 	return (EIO);
297 }
298 
299 static int
300 efi_set_time_locked(struct efi_tm *tm)
301 {
302 	efi_status status;
303 	int error;
304 
305 	EFI_TIME_OWNED();
306 	error = efi_enter();
307 	if (error != 0)
308 		return (error);
309 	status = efi_runtime->rt_settime(tm);
310 	efi_leave();
311 	error = efi_status_to_errno(status);
312 	return (error);
313 }
314 
315 int
316 efi_set_time(struct efi_tm *tm)
317 {
318 	int error;
319 
320 	if (efi_runtime == NULL)
321 		return (ENXIO);
322 	EFI_TIME_LOCK()
323 	error = efi_set_time_locked(tm);
324 	EFI_TIME_UNLOCK()
325 	return (error);
326 }
327 
328 int
329 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
330     size_t *datasize, void *data)
331 {
332 	efi_status status;
333 	int error;
334 
335 	error = efi_enter();
336 	if (error != 0)
337 		return (error);
338 	status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
339 	efi_leave();
340 	error = efi_status_to_errno(status);
341 	return (error);
342 }
343 
344 int
345 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
346 {
347 	efi_status status;
348 	int error;
349 
350 	error = efi_enter();
351 	if (error != 0)
352 		return (error);
353 	status = efi_runtime->rt_scanvar(namesize, name, vendor);
354 	efi_leave();
355 	error = efi_status_to_errno(status);
356 	return (error);
357 }
358 
359 int
360 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
361     size_t datasize, void *data)
362 {
363 	efi_status status;
364 	int error;
365 
366 	error = efi_enter();
367 	if (error != 0)
368 		return (error);
369 	status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
370 	efi_leave();
371 	error = efi_status_to_errno(status);
372 	return (error);
373 }
374 
375 static int
376 efirt_modevents(module_t m, int event, void *arg __unused)
377 {
378 
379 	switch (event) {
380 	case MOD_LOAD:
381 		return (efi_init());
382 
383 	case MOD_UNLOAD:
384 		efi_uninit();
385 		return (0);
386 
387 	case MOD_SHUTDOWN:
388 		return (0);
389 
390 	default:
391 		return (EOPNOTSUPP);
392 	}
393 }
394 
395 static moduledata_t efirt_moddata = {
396 	.name = "efirt",
397 	.evhand = efirt_modevents,
398 	.priv = NULL,
399 };
400 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
401 MODULE_VERSION(efirt, 1);
402