2 * Copyright (c) 2016 Netflix, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <machine/efi.h>
40 #include <sys/efiio.h>
42 static d_ioctl_t efidev_ioctl;
44 static struct cdevsw efi_cdevsw = {
46 .d_version = D_VERSION,
47 .d_ioctl = efidev_ioctl,
51 efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
52 int flags __unused, struct thread *td __unused)
57 case EFIIOC_GET_TABLE:
59 struct efi_get_table_ioc *egtioc =
60 (struct efi_get_table_ioc *)addr;
62 error = efi_get_table(&egtioc->uuid, &egtioc->ptr);
67 struct efi_tm *tm = (struct efi_tm *)addr;
69 error = efi_get_time(tm);
74 struct efi_tm *tm = (struct efi_tm *)addr;
76 error = efi_set_time(tm);
81 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
85 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
86 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
87 error = copyin(ev->name, name, ev->namesize);
90 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
95 error = efi_var_get(name, &ev->vendor, &ev->attrib,
99 error = copyout(data, ev->data, ev->datasize);
100 } else if (error == EOVERFLOW) {
102 * Pass back the size we really need, but
103 * convert the error to 0 so the copyout
104 * happens. datasize was updated in the
115 case EFIIOC_VAR_NEXT:
117 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
120 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
121 error = copyin(ev->name, name, ev->namesize);
124 /* Note: namesize is the buffer size, not the string lenght */
126 error = efi_var_nextname(&ev->namesize, name, &ev->vendor);
128 error = copyout(name, ev->name, ev->namesize);
129 } else if (error == EOVERFLOW) {
139 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
143 /* datasize == 0 -> delete (more or less) */
144 if (ev->datasize > 0)
145 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
146 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
148 error = copyin(ev->data, data, ev->datasize);
152 error = copyin(ev->name, name, ev->namesize);
155 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
160 error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize,
175 static struct cdev *efidev;
178 efidev_modevents(module_t m, int event, void *arg __unused)
180 struct make_dev_args mda;
186 * If we have no efi environment, then don't create the device.
188 if (efi_rt_ok() != 0)
190 make_dev_args_init(&mda);
191 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
192 mda.mda_devsw = &efi_cdevsw;
193 mda.mda_uid = UID_ROOT;
194 mda.mda_gid = GID_WHEEL;
196 error = make_dev_s(&mda, &efidev, "efi");
213 static moduledata_t efidev_moddata = {
215 .evhand = efidev_modevents,
219 DECLARE_MODULE(efidev, efidev_moddata, SI_SUB_DRIVERS, SI_ORDER_ANY);
220 MODULE_VERSION(efidev, 1);
221 MODULE_DEPEND(efidev, efirt, 1, 1, 1);