firmload.c revision 1.20 1 /* $NetBSD: firmload.c,v 1.20 2015/01/04 19:24:11 pooka Exp $ */
2
3 /*-
4 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: firmload.c,v 1.20 2015/01/04 19:24:11 pooka Exp $");
34
35 /*
36 * The firmload API provides an interface for device drivers to access
37 * firmware images that must be loaded onto their devices.
38 */
39
40 #include <sys/param.h>
41 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/kmem.h>
44 #include <sys/namei.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
48 #include <sys/kauth.h>
49 #include <sys/lwp.h>
50
51 #include <dev/firmload.h>
52
53 struct firmware_handle {
54 struct vnode *fh_vp;
55 off_t fh_size;
56 };
57
58 static firmware_handle_t
59 firmware_handle_alloc(void)
60 {
61
62 return (kmem_alloc(sizeof(struct firmware_handle), KM_SLEEP));
63 }
64
65 static void
66 firmware_handle_free(firmware_handle_t fh)
67 {
68
69 kmem_free(fh, sizeof(*fh));
70 }
71
72 #if !defined(FIRMWARE_PATHS)
73 #define FIRMWARE_PATHS \
74 "/libdata/firmware:/usr/libdata/firmware:/usr/pkg/libdata/firmware:/usr/pkg/libdata"
75 #endif
76
77 static char firmware_paths[PATH_MAX+1] = FIRMWARE_PATHS;
78
79 static int
80 sysctl_hw_firmware_path(SYSCTLFN_ARGS)
81 {
82 int error, i;
83 char newpath[PATH_MAX+1];
84 struct sysctlnode node;
85 char expected_char;
86
87 node = *rnode;
88 node.sysctl_data = &newpath[0];
89 memcpy(node.sysctl_data, rnode->sysctl_data, PATH_MAX+1);
90 error = sysctl_lookup(SYSCTLFN_CALL(&node));
91 if (error || newp == NULL)
92 return (error);
93
94 /*
95 * Make sure that all of the paths in the new path list are
96 * absolute.
97 *
98 * When sysctl_lookup() deals with a string, it's guaranteed
99 * to come back nul-terminated.
100 */
101 expected_char = '/';
102 for (i = 0; i < PATH_MAX+1; i++) {
103 if (expected_char != 0 && newpath[i] != expected_char)
104 return (EINVAL);
105 if (newpath[i] == '\0')
106 break;
107 else if (newpath[i] == ':')
108 expected_char = '/';
109 else
110 expected_char = 0;
111 }
112
113 memcpy(rnode->sysctl_data, node.sysctl_data, PATH_MAX+1);
114
115 return (0);
116 }
117
118 SYSCTL_SETUP_PROTO(sysctl_hw_firmware_setup);
119
120 SYSCTL_SETUP(sysctl_hw_firmware_setup, "sysctl hw.firmware subtree setup")
121 {
122 const struct sysctlnode *firmware_node;
123
124 if (sysctl_createv(clog, 0, NULL, &firmware_node,
125 CTLFLAG_PERMANENT,
126 CTLTYPE_NODE, "firmware", NULL,
127 NULL, 0, NULL, 0,
128 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
129 return;
130
131 sysctl_createv(clog, 0, NULL, NULL,
132 CTLFLAG_READWRITE,
133 CTLTYPE_STRING, "path",
134 SYSCTL_DESCR("Device firmware loading path list"),
135 sysctl_hw_firmware_path, 0, firmware_paths, PATH_MAX+1,
136 CTL_HW, firmware_node->sysctl_num, CTL_CREATE, CTL_EOL);
137 }
138
139 static char *
140 firmware_path_next(const char *drvname, const char *imgname, char *pnbuf,
141 char **prefixp)
142 {
143 char *prefix = *prefixp;
144 size_t maxprefix, i;
145
146 if (prefix == NULL /* terminated early */
147 || *prefix == '\0' /* no more left */
148 || *prefix != '/') { /* not absolute */
149 *prefixp = NULL;
150 return (NULL);
151 }
152
153 /*
154 * Compute the max path prefix based on the length of the provided
155 * names.
156 */
157 maxprefix = MAXPATHLEN -
158 (1 /* / */
159 + strlen(drvname)
160 + 1 /* / */
161 + strlen(imgname)
162 + 1 /* terminating NUL */);
163
164 /* Check for underflow (size_t is unsigned). */
165 if (maxprefix > MAXPATHLEN) {
166 *prefixp = NULL;
167 return (NULL);
168 }
169
170 for (i = 0; i < maxprefix; i++) {
171 if (*prefix == ':' || *prefix == '\0')
172 break;
173 pnbuf[i] = *prefix++;
174 }
175
176 if (*prefix != ':' && *prefix != '\0') {
177 /* Path prefix was too long. */
178 *prefixp = NULL;
179 return (NULL);
180 }
181
182 if (*prefix != '\0')
183 prefix++;
184 *prefixp = prefix;
185
186 KASSERT(MAXPATHLEN >= i);
187 snprintf(pnbuf + i, MAXPATHLEN - i, "/%s/%s", drvname, imgname);
188
189 return (pnbuf);
190 }
191
192 static char *
193 firmware_path_first(const char *drvname, const char *imgname, char *pnbuf,
194 char **prefixp)
195 {
196
197 *prefixp = firmware_paths;
198 return (firmware_path_next(drvname, imgname, pnbuf, prefixp));
199 }
200
201 /*
202 * firmware_open:
203 *
204 * Open a firmware image and return its handle.
205 */
206 int
207 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp)
208 {
209 struct pathbuf *pb;
210 struct nameidata nd;
211 struct vattr va;
212 char *pnbuf, *path, *prefix;
213 firmware_handle_t fh;
214 struct vnode *vp;
215 int error;
216 extern struct cwdinfo cwdi0;
217
218 if (drvname == NULL || imgname == NULL)
219 return (EINVAL);
220
221 if (cwdi0.cwdi_cdir == NULL) {
222 printf("firmware_open(%s/%s) called too early.\n",
223 drvname, imgname);
224 return ENOENT;
225 }
226
227 pnbuf = PNBUF_GET();
228 KASSERT(pnbuf != NULL);
229
230 fh = firmware_handle_alloc();
231 KASSERT(fh != NULL);
232
233 error = 0;
234 for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix);
235 path != NULL;
236 path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) {
237 pb = pathbuf_create(path);
238 if (pb == NULL) {
239 error = ENOMEM;
240 break;
241 }
242 NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb);
243 error = vn_open(&nd, FREAD, 0);
244 pathbuf_destroy(pb);
245 if (error == ENOENT) {
246 continue;
247 }
248 break;
249 }
250
251 PNBUF_PUT(pnbuf);
252 if (error) {
253 firmware_handle_free(fh);
254 return (error);
255 }
256
257 vp = nd.ni_vp;
258
259 error = VOP_GETATTR(vp, &va, kauth_cred_get());
260 if (error) {
261 VOP_UNLOCK(vp);
262 (void)vn_close(vp, FREAD, kauth_cred_get());
263 firmware_handle_free(fh);
264 return (error);
265 }
266
267 if (va.va_type != VREG) {
268 VOP_UNLOCK(vp);
269 (void)vn_close(vp, FREAD, kauth_cred_get());
270 firmware_handle_free(fh);
271 return (EINVAL);
272 }
273
274 /* XXX Mark as busy text file. */
275
276 fh->fh_vp = vp;
277 fh->fh_size = va.va_size;
278
279 VOP_UNLOCK(vp);
280
281 *fhp = fh;
282 return (0);
283 }
284
285 /*
286 * firmware_close:
287 *
288 * Close a firmware image.
289 */
290 int
291 firmware_close(firmware_handle_t fh)
292 {
293 int error;
294
295 error = vn_close(fh->fh_vp, FREAD, kauth_cred_get());
296 firmware_handle_free(fh);
297 return (error);
298 }
299
300 /*
301 * firmware_get_size:
302 *
303 * Return the total size of a firmware image.
304 */
305 off_t
306 firmware_get_size(firmware_handle_t fh)
307 {
308
309 return (fh->fh_size);
310 }
311
312 /*
313 * firmware_read:
314 *
315 * Read data from a firmware image at the specified offset into
316 * the provided buffer.
317 */
318 int
319 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
320 {
321
322 return (vn_rdwr(UIO_READ, fh->fh_vp, buf, len, offset,
323 UIO_SYSSPACE, 0, kauth_cred_get(), NULL, curlwp));
324 }
325
326 /*
327 * firmware_malloc:
328 *
329 * Allocate a firmware buffer of the specified size.
330 *
331 * NOTE: This routine may block.
332 */
333 void *
334 firmware_malloc(size_t size)
335 {
336
337 return (kmem_alloc(size, KM_SLEEP));
338 }
339
340 /*
341 * firmware_free:
342 *
343 * Free a previously allocated firmware buffer.
344 */
345 /*ARGSUSED*/
346 void
347 firmware_free(void *v, size_t size)
348 {
349
350 kmem_free(v, size);
351 }
352