1 /*-
2 * Copyright (c) 2016 Netflix, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
13 *
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.
24 */
25
26 #include <sys/cdefs.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/bus.h>
31 #include <sys/conf.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35
36 #include <machine/efi.h>
37 #include <sys/efiio.h>
38
39 static d_ioctl_t efidev_ioctl;
40
41 static struct cdevsw efi_cdevsw = {
42 .d_name = "efi",
43 .d_version = D_VERSION,
44 .d_ioctl = efidev_ioctl,
45 };
46
47 static int
efidev_ioctl(struct cdev * dev __unused,u_long cmd,caddr_t addr,int flags __unused,struct thread * td __unused)48 efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
49 int flags __unused, struct thread *td __unused)
50 {
51 int error;
52
53 switch (cmd) {
54 case EFIIOC_GET_TIME:
55 {
56 struct efi_tm *tm = (struct efi_tm *)addr;
57
58 error = efi_get_time(tm);
59 break;
60 }
61 case EFIIOC_SET_TIME:
62 {
63 struct efi_tm *tm = (struct efi_tm *)addr;
64
65 error = efi_set_time(tm);
66 break;
67 }
68 case EFIIOC_VAR_GET:
69 {
70 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
71 void *data;
72 efi_char *name;
73
74 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
75 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
76 error = copyin(ev->name, name, ev->namesize);
77 if (error)
78 goto vg_out;
79 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
80 error = EINVAL;
81 goto vg_out;
82 }
83
84 error = efi_var_get(name, &ev->vendor, &ev->attrib,
85 &ev->datasize, data);
86
87 if (error == 0) {
88 error = copyout(data, ev->data, ev->datasize);
89 } else if (error == EOVERFLOW) {
90 /*
91 * Pass back the size we really need, but
92 * convert the error to 0 so the copyout
93 * happens. datasize was updated in the
94 * efi_var_get call.
95 */
96 ev->data = NULL;
97 error = 0;
98 }
99 vg_out:
100 free(data, M_TEMP);
101 free(name, M_TEMP);
102 break;
103 }
104 case EFIIOC_VAR_NEXT:
105 {
106 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
107 efi_char *name;
108
109 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
110 error = copyin(ev->name, name, ev->namesize);
111 if (error)
112 goto vn_out;
113 /* Note: namesize is the buffer size, not the string lenght */
114
115 error = efi_var_nextname(&ev->namesize, name, &ev->vendor);
116 if (error == 0) {
117 error = copyout(name, ev->name, ev->namesize);
118 } else if (error == EOVERFLOW) {
119 ev->name = NULL;
120 error = 0;
121 }
122 vn_out:
123 free(name, M_TEMP);
124 break;
125 }
126 case EFIIOC_VAR_SET:
127 {
128 struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
129 void *data = NULL;
130 efi_char *name;
131
132 /* datasize == 0 -> delete (more or less) */
133 if (ev->datasize > 0)
134 data = malloc(ev->datasize, M_TEMP, M_WAITOK);
135 name = malloc(ev->namesize, M_TEMP, M_WAITOK);
136 if (ev->datasize) {
137 error = copyin(ev->data, data, ev->datasize);
138 if (error)
139 goto vs_out;
140 }
141 error = copyin(ev->name, name, ev->namesize);
142 if (error)
143 goto vs_out;
144 if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
145 error = EINVAL;
146 goto vs_out;
147 }
148
149 error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize,
150 data);
151 vs_out:
152 free(data, M_TEMP);
153 free(name, M_TEMP);
154 break;
155 }
156 default:
157 error = ENOTTY;
158 break;
159 }
160
161 return (error);
162 }
163
164 static struct cdev *efidev;
165
166 static int
efidev_modevents(module_t m,int event,void * arg __unused)167 efidev_modevents(module_t m, int event, void *arg __unused)
168 {
169 struct make_dev_args mda;
170 int error;
171
172 switch (event) {
173 case MOD_LOAD:
174 /*
175 * If we have no efi environment, then don't create the device.
176 */
177 if (efi_rt_ok() != 0)
178 return (0);
179 make_dev_args_init(&mda);
180 mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
181 mda.mda_devsw = &efi_cdevsw;
182 mda.mda_uid = UID_ROOT;
183 mda.mda_gid = GID_WHEEL;
184 mda.mda_mode = 0700;
185 error = make_dev_s(&mda, &efidev, "efi");
186 return (error);
187
188 case MOD_UNLOAD:
189 if (efidev != NULL)
190 destroy_dev(efidev);
191 efidev = NULL;
192 return (0);
193
194 case MOD_SHUTDOWN:
195 return (0);
196
197 default:
198 return (EOPNOTSUPP);
199 }
200 }
201
202 static moduledata_t efidev_moddata = {
203 .name = "efidev",
204 .evhand = efidev_modevents,
205 .priv = NULL,
206 };
207
208 DECLARE_MODULE(efidev, efidev_moddata, SI_SUB_DRIVERS, SI_ORDER_ANY);
209 MODULE_VERSION(efidev, 1);
210 MODULE_DEPEND(efidev, efirt, 1, 1, 1);
211