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