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