xref: /freebsd/sys/dev/efidev/efirt.c (revision 137a344c6341d1469432e9deb3a25593f96672ad)
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 	int error;
198 
199 	if (efi_runtime == NULL)
200 		return (ENXIO);
201 	td = curthread;
202 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
203 	PMAP_LOCK(curpmap);
204 	mtx_lock(&efi_lock);
205 	error = fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
206 	if (error != 0) {
207 		PMAP_UNLOCK(curpmap);
208 		return (error);
209 	}
210 
211 	return (efi_arch_enter());
212 }
213 
214 static void
215 efi_leave(void)
216 {
217 	struct thread *td;
218 	pmap_t curpmap;
219 
220 	efi_arch_leave();
221 
222 	curpmap = &curproc->p_vmspace->vm_pmap;
223 	td = curthread;
224 	fpu_kern_leave(td, NULL);
225 	mtx_unlock(&efi_lock);
226 	PMAP_UNLOCK(curpmap);
227 }
228 
229 int
230 efi_get_table(struct uuid *uuid, void **ptr)
231 {
232 	struct efi_cfgtbl *ct;
233 	u_long count;
234 
235 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
236 		return (ENXIO);
237 	count = efi_systbl->st_entries;
238 	ct = efi_cfgtbl;
239 	while (count--) {
240 		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
241 			*ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
242 			return (0);
243 		}
244 		ct++;
245 	}
246 	return (ENOENT);
247 }
248 
249 static int
250 efi_get_time_locked(struct efi_tm *tm)
251 {
252 	efi_status status;
253 	int error;
254 
255 	EFI_TIME_OWNED()
256 	error = efi_enter();
257 	if (error != 0)
258 		return (error);
259 	status = efi_runtime->rt_gettime(tm, NULL);
260 	efi_leave();
261 	error = efi_status_to_errno(status);
262 	return (error);
263 }
264 
265 int
266 efi_get_time(struct efi_tm *tm)
267 {
268 	int error;
269 
270 	if (efi_runtime == NULL)
271 		return (ENXIO);
272 	EFI_TIME_LOCK()
273 	error = efi_get_time_locked(tm);
274 	EFI_TIME_UNLOCK()
275 	return (error);
276 }
277 
278 int
279 efi_reset_system(void)
280 {
281 	int error;
282 
283 	error = efi_enter();
284 	if (error != 0)
285 		return (error);
286 	efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
287 	efi_leave();
288 	return (EIO);
289 }
290 
291 static int
292 efi_set_time_locked(struct efi_tm *tm)
293 {
294 	efi_status status;
295 	int error;
296 
297 	EFI_TIME_OWNED();
298 	error = efi_enter();
299 	if (error != 0)
300 		return (error);
301 	status = efi_runtime->rt_settime(tm);
302 	efi_leave();
303 	error = efi_status_to_errno(status);
304 	return (error);
305 }
306 
307 int
308 efi_set_time(struct efi_tm *tm)
309 {
310 	int error;
311 
312 	if (efi_runtime == NULL)
313 		return (ENXIO);
314 	EFI_TIME_LOCK()
315 	error = efi_set_time_locked(tm);
316 	EFI_TIME_UNLOCK()
317 	return (error);
318 }
319 
320 int
321 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
322     size_t *datasize, void *data)
323 {
324 	efi_status status;
325 	int error;
326 
327 	error = efi_enter();
328 	if (error != 0)
329 		return (error);
330 	status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
331 	efi_leave();
332 	error = efi_status_to_errno(status);
333 	return (error);
334 }
335 
336 int
337 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
338 {
339 	efi_status status;
340 	int error;
341 
342 	error = efi_enter();
343 	if (error != 0)
344 		return (error);
345 	status = efi_runtime->rt_scanvar(namesize, name, vendor);
346 	efi_leave();
347 	error = efi_status_to_errno(status);
348 	return (error);
349 }
350 
351 int
352 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
353     size_t datasize, void *data)
354 {
355 	efi_status status;
356 	int error;
357 
358 	error = efi_enter();
359 	if (error != 0)
360 		return (error);
361 	status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
362 	efi_leave();
363 	error = efi_status_to_errno(status);
364 	return (error);
365 }
366 
367 static int
368 efirt_modevents(module_t m, int event, void *arg __unused)
369 {
370 
371 	switch (event) {
372 	case MOD_LOAD:
373 		return (efi_init());
374 
375 	case MOD_UNLOAD:
376 		efi_uninit();
377 		return (0);
378 
379 	case MOD_SHUTDOWN:
380 		return (0);
381 
382 	default:
383 		return (EOPNOTSUPP);
384 	}
385 }
386 
387 static moduledata_t efirt_moddata = {
388 	.name = "efirt",
389 	.evhand = efirt_modevents,
390 	.priv = NULL,
391 };
392 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
393 MODULE_VERSION(efirt, 1);
394