2 * Copyright (c) 2016 Netflix, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer
9 * in this position and unchanged.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <machine/efi.h>
39 #include <sys/efiio.h>
41 static d_ioctl_t efidev_ioctl;
43 static struct cdevsw efi_cdevsw = {
45 .d_version = D_VERSION,
46 .d_ioctl = efidev_ioctl,
50 efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
51 int flags __unused, struct thread *td __unused)
56 case EFIIOC_GET_TABLE:
58 struct efi_get_table_ioc *egtioc =
59 (struct efi_get_table_ioc *)addr;
62 error = efi_copy_table(&egtioc->uuid, egtioc->buf ? &buf : NULL,
63 egtioc->buf_len, &egtioc->table_len);
65 if (error != 0 || egtioc->buf == NULL)
68 if (egtioc->buf_len < egtioc->table_len) {
74 error = copyout(buf, egtioc->buf, egtioc->buf_len);
81 struct efi_tm *tm = (struct efi_tm *)addr;
83 error = efi_get_time(tm);
88 struct efi_tm *tm = (struct efi_tm *)addr;
90 error = efi_set_time(tm);
95 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
99 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
100 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
101 error = copyin(ev->name, name, ev->namesize);
104 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
109 error = efi_var_get(name, &ev->vendor, &ev->attrib,
110 &ev->datasize, data);
113 error = copyout(data, ev->data, ev->datasize);
114 } else if (error == EOVERFLOW) {
116 * Pass back the size we really need, but
117 * convert the error to 0 so the copyout
118 * happens. datasize was updated in the
129 case EFIIOC_VAR_NEXT:
131 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
134 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
135 error = copyin(ev->name, name, ev->namesize);
138 /* Note: namesize is the buffer size, not the string lenght */
140 error = efi_var_nextname(&ev->namesize, name, &ev->vendor);
142 error = copyout(name, ev->name, ev->namesize);
143 } else if (error == EOVERFLOW) {
153 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
157 /* datasize == 0 -> delete (more or less) */
158 if (ev->datasize > 0)
159 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
160 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
162 error = copyin(ev->data, data, ev->datasize);
166 error = copyin(ev->name, name, ev->namesize);
169 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
174 error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize,
189 static struct cdev *efidev;
192 efidev_modevents(module_t m, int event, void *arg __unused)
194 struct make_dev_args mda;
200 * If we have no efi environment, then don't create the device.
202 if (efi_rt_ok() != 0)
204 make_dev_args_init(&mda);
205 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
206 mda.mda_devsw = &efi_cdevsw;
207 mda.mda_uid = UID_ROOT;
208 mda.mda_gid = GID_WHEEL;
210 error = make_dev_s(&mda, &efidev, "efi");
227 static moduledata_t efidev_moddata = {
229 .evhand = efidev_modevents,
233 DECLARE_MODULE(efidev, efidev_moddata, SI_SUB_DRIVERS, SI_ORDER_ANY);
234 MODULE_VERSION(efidev, 1);
235 MODULE_DEPEND(efidev, efirt, 1, 1, 1);