xref: /freebsd/sys/dev/efidev/efidev.c (revision bdc7291ec92fc8e6fc2a0e1c5c8f22f670a9393e)
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 /* ARGSUSED */
51 static int
52 efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr,
53     int flags __unused, struct thread *td __unused)
54 {
55 	int error;
56 
57 	switch (cmd) {
58 	case EFIIOC_GET_TABLE:
59 	{
60 		struct efi_get_table_ioc *egtioc =
61 		    (struct efi_get_table_ioc *)addr;
62 
63 		error = efi_get_table(&egtioc->uuid, &egtioc->ptr);
64 		break;
65 	}
66 	case EFIIOC_GET_TIME:
67 	{
68 		struct efi_tm *tm = (struct efi_tm *)addr;
69 
70 		error = efi_get_time(tm);
71 		break;
72 	}
73 	case EFIIOC_SET_TIME:
74 	{
75 		struct efi_tm *tm = (struct efi_tm *)addr;
76 
77 		error = efi_set_time(tm);
78 		break;
79 	}
80 	case EFIIOC_VAR_GET:
81 	{
82 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
83 		void *data;
84 		efi_char *name;
85 
86 		data = malloc(ev->datasize, M_TEMP, M_WAITOK);
87 		name = malloc(ev->namesize, M_TEMP, M_WAITOK);
88 		error = copyin(ev->name, name, ev->namesize);
89 		if (error)
90 			goto vg_out;
91 		if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
92 			error = EINVAL;
93 			goto vg_out;
94 		}
95 
96 		error = efi_var_get(name, &ev->vendor, &ev->attrib,
97 		    &ev->datasize, data);
98 
99 		if (error == 0) {
100 			error = copyout(data, ev->data, ev->datasize);
101 		} else if (error == EOVERFLOW) {
102 			/*
103 			 * Pass back the size we really need, but
104 			 * convert the error to 0 so the copyout
105 			 * happens. datasize was updated in the
106 			 * efi_var_get call.
107 			 */
108 			ev->data = NULL;
109 			error = 0;
110 		}
111 vg_out:
112 		free(data, M_TEMP);
113 		free(name, M_TEMP);
114 		break;
115 	}
116 	case EFIIOC_VAR_NEXT:
117 	{
118 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
119 		efi_char *name;
120 
121 		name = malloc(ev->namesize, M_TEMP, M_WAITOK);
122 		if (name == NULL) {
123 			error = ENOMEM;
124 			goto vn_out;
125 		}
126 		error = copyin(ev->name, name, ev->namesize);
127 		if (error)
128 			goto vn_out;
129 		/* Note: namesize is the buffer size, not the string lenght */
130 
131 		error = efi_var_nextname(&ev->namesize, name, &ev->vendor);
132 		if (error == 0) {
133 			error = copyout(name, ev->name, ev->namesize);
134 		} else if (error == EOVERFLOW) {
135 			ev->name = NULL;
136 			error = 0;
137 		}
138 	vn_out:
139 		if (name != NULL)
140 			free(name, M_TEMP);
141 		break;
142 	}
143 	case EFIIOC_VAR_SET:
144 	{
145 		struct efi_var_ioc *ev = (struct efi_var_ioc *)addr;
146 		void *data = NULL;
147 		efi_char *name;
148 
149 		/* datasize == 0 -> delete (more or less) */
150 		if (ev->datasize > 0)
151 			data = malloc(ev->datasize, M_TEMP, M_WAITOK);
152 		name = malloc(ev->namesize, M_TEMP, M_WAITOK);
153 		if (ev->datasize) {
154 			error = copyin(ev->data, data, ev->datasize);
155 			if (error)
156 				goto vs_out;
157 		}
158 		error = copyin(ev->name, name, ev->namesize);
159 		if (error)
160 			goto vs_out;
161 		if (name[ev->namesize / sizeof(efi_char) - 1] != 0) {
162 			error = EINVAL;
163 			goto vs_out;
164 		}
165 
166 		error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize,
167 		    data);
168 vs_out:
169 		if (data != NULL)
170 			free(data, M_TEMP);
171 		free(name, M_TEMP);
172 		break;
173 	}
174 	default:
175 		error = ENOTTY;
176 		break;
177 	}
178 
179 	return (error);
180 }
181 
182 int
183 efidev_init(struct cdev **cdev)
184 {
185 
186 	*cdev = make_dev(&efi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0700,
187 	    "efidev");
188 
189 	return (0);
190 }
191 
192 int
193 efidev_uninit(struct cdev *cdev)
194 {
195 
196 	destroy_dev(cdev);
197 
198 	return (0);
199 }
200