2 * Copyright (c) 2010 Marcel Moolenaar
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 * 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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/fcntl.h>
33 #include <sys/ioccom.h>
34 #include <sys/malloc.h>
37 #include <sys/systm.h>
39 #include <machine/bus.h>
40 #include <machine/efi.h>
41 #include <machine/iodev.h>
43 static int iodev_efivar_getvar(struct iodev_efivar_req *req);
44 static int iodev_efivar_nextname(struct iodev_efivar_req *req);
45 static int iodev_efivar_setvar(struct iodev_efivar_req *req);
49 iodev_open(struct thread *td __unused)
57 iodev_close(struct thread *td __unused)
64 iodev_ioctl(u_long cmd, caddr_t data)
66 struct iodev_efivar_req *efivar_req;
71 efivar_req = (struct iodev_efivar_req *)data;
72 efivar_req->result = 0; /* So it's well-defined */
73 switch (efivar_req->access) {
74 case IODEV_EFIVAR_GETVAR:
75 error = iodev_efivar_getvar(efivar_req);
77 case IODEV_EFIVAR_NEXTNAME:
78 error = iodev_efivar_nextname(efivar_req);
80 case IODEV_EFIVAR_SETVAR:
81 error = iodev_efivar_setvar(efivar_req);
96 iodev_efivar_getvar(struct iodev_efivar_req *req)
102 if ((req->namesize & 1) != 0 || req->namesize < 4)
104 if (req->datasize == 0)
108 * Pre-zero the allocated memory and don't copy the last 2 bytes
109 * of the name. That should be the closing nul character (ucs-2)
110 * and if not, then we ensured a nul-terminating string. This is
111 * to protect the firmware and thus ourselves.
113 name = malloc(req->namesize, M_TEMP, M_WAITOK | M_ZERO);
114 error = copyin(req->name, name, req->namesize - 2);
120 data = malloc(req->datasize, M_TEMP, M_WAITOK);
121 error = efi_var_get(name, &req->vendor, &req->attrib, &req->datasize,
123 if (error == EOVERFLOW || error == ENOENT) {
127 if (!error && !req->result)
128 error = copyout(data, req->data, req->datasize);
136 iodev_efivar_nextname(struct iodev_efivar_req *req)
141 /* Enforce a reasonable minimum size of the name buffer. */
142 if (req->namesize < 4)
145 name = malloc(req->namesize, M_TEMP, M_WAITOK);
146 error = copyin(req->name, name, req->namesize);
152 error = efi_var_nextname(&req->namesize, name, &req->vendor);
153 if (error == EOVERFLOW || error == ENOENT) {
157 if (!error && !req->result)
158 error = copyout(name, req->name, req->namesize);
165 iodev_efivar_setvar(struct iodev_efivar_req *req)
171 if ((req->namesize & 1) != 0 || req->namesize < 4)
175 * Pre-zero the allocated memory and don't copy the last 2 bytes
176 * of the name. That should be the closing nul character (ucs-2)
177 * and if not, then we ensured a nul-terminating string. This is
178 * to protect the firmware and thus ourselves.
180 name = malloc(req->namesize, M_TEMP, M_WAITOK | M_ZERO);
181 error = copyin(req->name, name, req->namesize - 2);
188 data = malloc(req->datasize, M_TEMP, M_WAITOK);
189 error = copyin(req->data, data, req->datasize);
198 error = efi_var_set(name, &req->vendor, req->attrib, req->datasize,
200 if (error == EAGAIN || error == ENOENT) {