]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/efidev/efirt.c
MFC r336919, r336924
[FreeBSD/FreeBSD.git] / sys / dev / efidev / efirt.c
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 /*
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  */
65 static struct efi_cfgtbl *efi_cfgtbl;
66 static struct efi_rt *efi_runtime;
67
68 static 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
96 static int efi_enter(void);
97 static void efi_leave(void);
98
99 static int
100 efi_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
108 static struct mtx efi_lock;
109
110 static bool
111 efi_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
129 static int
130 efi_init(void)
131 {
132         struct efi_map_header *efihdr;
133         struct efi_md *map;
134         caddr_t kmdp;
135         size_t efisz;
136         int rt_disabled;
137
138         rt_disabled = 0;
139         TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
140         if (rt_disabled == 1)
141                 return (0);
142         mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
143
144         if (efi_systbl_phys == 0) {
145                 if (bootverbose)
146                         printf("EFI systbl not available\n");
147                 return (0);
148         }
149         efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
150         if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
151                 efi_systbl = NULL;
152                 if (bootverbose)
153                         printf("EFI systbl signature invalid\n");
154                 return (0);
155         }
156         efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
157             (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
158         if (efi_cfgtbl == NULL) {
159                 if (bootverbose)
160                         printf("EFI config table is not present\n");
161         }
162
163         kmdp = preload_search_by_type("elf kernel");
164         if (kmdp == NULL)
165                 kmdp = preload_search_by_type("elf64 kernel");
166         efihdr = (struct efi_map_header *)preload_search_info(kmdp,
167             MODINFO_METADATA | MODINFOMD_EFI_MAP);
168         if (efihdr == NULL) {
169                 if (bootverbose)
170                         printf("EFI map is not present\n");
171                 return (0);
172         }
173         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
174         map = (struct efi_md *)((uint8_t *)efihdr + efisz);
175         if (efihdr->descriptor_size == 0)
176                 return (ENOMEM);
177
178         if (!efi_create_1t1_map(map, efihdr->memory_size /
179             efihdr->descriptor_size, efihdr->descriptor_size)) {
180                 if (bootverbose)
181                         printf("EFI cannot create runtime map\n");
182                 return (ENOMEM);
183         }
184
185         efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
186             (struct efi_rt *)efi_systbl->st_rt;
187         if (efi_runtime == NULL) {
188                 if (bootverbose)
189                         printf("EFI runtime services table is not present\n");
190                 efi_destroy_1t1_map();
191                 return (ENXIO);
192         }
193
194         /*
195          * Some UEFI implementations have multiple implementations of the
196          * RS->GetTime function. They switch from one we can only use early
197          * in the boot process to one valid as a RunTime service only when we
198          * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
199          * with an old loader.efi, check if the RS->GetTime function is within
200          * the EFI map, and fail to attach if not.
201          *
202          * We need to enter into the EFI environment as efi_runtime may point
203          * to an EFI address.
204          */
205         efi_enter();
206         if (!efi_is_in_map(map, efihdr->memory_size / efihdr->descriptor_size,
207             efihdr->descriptor_size, (vm_offset_t)efi_runtime->rt_gettime)) {
208                 efi_leave();
209                 if (bootverbose)
210                         printf(
211                          "EFI runtime services table has an invalid pointer\n");
212                 efi_runtime = NULL;
213                 efi_destroy_1t1_map();
214                 return (ENXIO);
215         }
216         efi_leave();
217
218         return (0);
219 }
220
221 static void
222 efi_uninit(void)
223 {
224
225         /* Most likely disabled by tunable */
226         if (efi_runtime == NULL)
227                 return;
228         efi_destroy_1t1_map();
229
230         efi_systbl = NULL;
231         efi_cfgtbl = NULL;
232         efi_runtime = NULL;
233
234         mtx_destroy(&efi_lock);
235 }
236
237 int
238 efi_rt_ok(void)
239 {
240
241         if (efi_runtime == NULL)
242                 return (ENXIO);
243         return (0);
244 }
245
246 static int
247 efi_enter(void)
248 {
249         struct thread *td;
250         pmap_t curpmap;
251         int error;
252
253         if (efi_runtime == NULL)
254                 return (ENXIO);
255         td = curthread;
256         curpmap = &td->td_proc->p_vmspace->vm_pmap;
257         PMAP_LOCK(curpmap);
258         mtx_lock(&efi_lock);
259         error = fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
260         if (error != 0) {
261                 PMAP_UNLOCK(curpmap);
262                 return (error);
263         }
264
265         return (efi_arch_enter());
266 }
267
268 static void
269 efi_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
283 int
284 efi_get_table(struct uuid *uuid, void **ptr)
285 {
286         struct efi_cfgtbl *ct;
287         u_long count;
288
289         if (efi_cfgtbl == NULL || efi_systbl == NULL)
290                 return (ENXIO);
291         count = efi_systbl->st_entries;
292         ct = efi_cfgtbl;
293         while (count--) {
294                 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
295                         *ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
296                         return (0);
297                 }
298                 ct++;
299         }
300         return (ENOENT);
301 }
302
303 static int
304 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
305 {
306         efi_status status;
307         int error;
308
309         EFI_TIME_OWNED()
310         error = efi_enter();
311         if (error != 0)
312                 return (error);
313         status = efi_runtime->rt_gettime(tm, tmcap);
314         efi_leave();
315         error = efi_status_to_errno(status);
316         return (error);
317 }
318
319 int
320 efi_get_time(struct efi_tm *tm)
321 {
322         struct efi_tmcap dummy;
323         int error;
324
325         if (efi_runtime == NULL)
326                 return (ENXIO);
327         EFI_TIME_LOCK()
328         /*
329          * UEFI spec states that the Capabilities argument to GetTime is
330          * optional, but some UEFI implementations choke when passed a NULL
331          * pointer. Pass a dummy efi_tmcap, even though we won't use it,
332          * to workaround such implementations.
333          */
334         error = efi_get_time_locked(tm, &dummy);
335         EFI_TIME_UNLOCK()
336         return (error);
337 }
338
339 int
340 efi_get_time_capabilities(struct efi_tmcap *tmcap)
341 {
342         struct efi_tm dummy;
343         int error;
344
345         if (efi_runtime == NULL)
346                 return (ENXIO);
347         EFI_TIME_LOCK()
348         error = efi_get_time_locked(&dummy, tmcap);
349         EFI_TIME_UNLOCK()
350         return (error);
351 }
352
353 int
354 efi_reset_system(void)
355 {
356         int error;
357
358         error = efi_enter();
359         if (error != 0)
360                 return (error);
361         efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
362         efi_leave();
363         return (EIO);
364 }
365
366 static int
367 efi_set_time_locked(struct efi_tm *tm)
368 {
369         efi_status status;
370         int error;
371
372         EFI_TIME_OWNED();
373         error = efi_enter();
374         if (error != 0)
375                 return (error);
376         status = efi_runtime->rt_settime(tm);
377         efi_leave();
378         error = efi_status_to_errno(status);
379         return (error);
380 }
381
382 int
383 efi_set_time(struct efi_tm *tm)
384 {
385         int error;
386
387         if (efi_runtime == NULL)
388                 return (ENXIO);
389         EFI_TIME_LOCK()
390         error = efi_set_time_locked(tm);
391         EFI_TIME_UNLOCK()
392         return (error);
393 }
394
395 int
396 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
397     size_t *datasize, void *data)
398 {
399         efi_status status;
400         int error;
401
402         error = efi_enter();
403         if (error != 0)
404                 return (error);
405         status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
406         efi_leave();
407         error = efi_status_to_errno(status);
408         return (error);
409 }
410
411 int
412 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
413 {
414         efi_status status;
415         int error;
416
417         error = efi_enter();
418         if (error != 0)
419                 return (error);
420         status = efi_runtime->rt_scanvar(namesize, name, vendor);
421         efi_leave();
422         error = efi_status_to_errno(status);
423         return (error);
424 }
425
426 int
427 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
428     size_t datasize, void *data)
429 {
430         efi_status status;
431         int error;
432
433         error = efi_enter();
434         if (error != 0)
435                 return (error);
436         status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
437         efi_leave();
438         error = efi_status_to_errno(status);
439         return (error);
440 }
441
442 static int
443 efirt_modevents(module_t m, int event, void *arg __unused)
444 {
445
446         switch (event) {
447         case MOD_LOAD:
448                 return (efi_init());
449
450         case MOD_UNLOAD:
451                 efi_uninit();
452                 return (0);
453
454         case MOD_SHUTDOWN:
455                 return (0);
456
457         default:
458                 return (EOPNOTSUPP);
459         }
460 }
461
462 static moduledata_t efirt_moddata = {
463         .name = "efirt",
464         .evhand = efirt_modevents,
465         .priv = NULL,
466 };
467 /* After fpuinitstate, before efidev */
468 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
469 MODULE_VERSION(efirt, 1);