firmload.c revision 1.15 1 /* $NetBSD: firmload.c,v 1.15 2010/11/19 06:44:39 dholland 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.15 2010/11/19 06:44:39 dholland 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/malloc.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 static MALLOC_DEFINE(M_DEVFIRM, "devfirm", "device firmware buffers");
54
55 struct firmware_handle {
56 struct vnode *fh_vp;
57 off_t fh_size;
58 };
59
60 static firmware_handle_t
61 firmware_handle_alloc(void)
62 {
63
64 return (malloc(sizeof(struct firmware_handle), M_DEVFIRM, M_WAITOK));
65 }
66
67 static void
68 firmware_handle_free(firmware_handle_t fh)
69 {
70
71 free(fh, M_DEVFIRM);
72 }
73
74 #if !defined(FIRMWARE_PATHS)
75 #define FIRMWARE_PATHS \
76 "/libdata/firmware:/usr/libdata/firmware:/usr/pkg/libdata/firmware:/usr/pkg/libdata"
77 #endif
78
79 static char firmware_paths[PATH_MAX+1] = FIRMWARE_PATHS;
80
81 static int
82 sysctl_hw_firmware_path(SYSCTLFN_ARGS)
83 {
84 int error, i;
85 char newpath[PATH_MAX+1];
86 struct sysctlnode node;
87 char expected_char;
88
89 node = *rnode;
90 node.sysctl_data = &newpath[0];
91 memcpy(node.sysctl_data, rnode->sysctl_data, PATH_MAX+1);
92 error = sysctl_lookup(SYSCTLFN_CALL(&node));
93 if (error || newp == NULL)
94 return (error);
95
96 /*
97 * Make sure that all of the paths in the new path list are
98 * absolute.
99 *
100 * When sysctl_lookup() deals with a string, it's guaranteed
101 * to come back nul-terminated.
102 */
103 expected_char = '/';
104 for (i = 0; i < PATH_MAX+1; i++) {
105 if (expected_char != 0 && newpath[i] != expected_char)
106 return (EINVAL);
107 if (newpath[i] == '\0')
108 break;
109 else if (newpath[i] == ':')
110 expected_char = '/';
111 else
112 expected_char = 0;
113 }
114
115 memcpy(rnode->sysctl_data, node.sysctl_data, PATH_MAX+1);
116
117 return (0);
118 }
119
120 SYSCTL_SETUP_PROTO(sysctl_hw_firmware_setup);
121
122 SYSCTL_SETUP(sysctl_hw_firmware_setup, "sysctl hw.firmware subtree setup")
123 {
124 const struct sysctlnode *firmware_node;
125
126 if (sysctl_createv(clog, 0, NULL, NULL,
127 CTLFLAG_PERMANENT,
128 CTLTYPE_NODE, "hw", NULL,
129 NULL, 0, NULL, 0,
130 CTL_HW, CTL_EOL) != 0)
131 return;
132
133 if (sysctl_createv(clog, 0, NULL, &firmware_node,
134 CTLFLAG_PERMANENT,
135 CTLTYPE_NODE, "firmware", NULL,
136 NULL, 0, NULL, 0,
137 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
138 return;
139
140 sysctl_createv(clog, 0, NULL, NULL,
141 CTLFLAG_READWRITE,
142 CTLTYPE_STRING, "path",
143 SYSCTL_DESCR("Device firmware loading path list"),
144 sysctl_hw_firmware_path, 0, firmware_paths, PATH_MAX+1,
145 CTL_HW, firmware_node->sysctl_num, CTL_CREATE, CTL_EOL);
146 }
147
148 static char *
149 firmware_path_next(const char *drvname, const char *imgname, char *pnbuf,
150 char **prefixp)
151 {
152 char *prefix = *prefixp;
153 size_t maxprefix, i;
154
155 if (prefix == NULL /* terminated early */
156 || *prefix == '\0' /* no more left */
157 || *prefix != '/') { /* not absolute */
158 *prefixp = NULL;
159 return (NULL);
160 }
161
162 /*
163 * Compute the max path prefix based on the length of the provided
164 * names.
165 */
166 maxprefix = MAXPATHLEN -
167 (1 /* / */
168 + strlen(drvname)
169 + 1 /* / */
170 + strlen(imgname)
171 + 1 /* terminating NUL */);
172
173 /* Check for underflow (size_t is unsigned). */
174 if (maxprefix > MAXPATHLEN) {
175 *prefixp = NULL;
176 return (NULL);
177 }
178
179 for (i = 0; i < maxprefix; i++) {
180 if (*prefix == ':' || *prefix == '\0')
181 break;
182 pnbuf[i] = *prefix++;
183 }
184
185 if (*prefix != ':' && *prefix != '\0') {
186 /* Path prefix was too long. */
187 *prefixp = NULL;
188 return (NULL);
189 }
190
191 if (*prefix != '\0')
192 prefix++;
193 *prefixp = prefix;
194
195 /*
196 * This sprintf() is safe because of the maxprefix calculation
197 * performed above.
198 */
199 sprintf(&pnbuf[i], "/%s/%s", drvname, imgname);
200
201 return (pnbuf);
202 }
203
204 static char *
205 firmware_path_first(const char *drvname, const char *imgname, char *pnbuf,
206 char **prefixp)
207 {
208
209 *prefixp = firmware_paths;
210 return (firmware_path_next(drvname, imgname, pnbuf, prefixp));
211 }
212
213 /*
214 * firmware_open:
215 *
216 * Open a firmware image and return its handle.
217 */
218 int
219 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp)
220 {
221 struct pathbuf *pb;
222 struct nameidata nd;
223 struct vattr va;
224 char *pnbuf, *path, *prefix;
225 firmware_handle_t fh;
226 struct vnode *vp;
227 int error;
228 extern struct cwdinfo cwdi0;
229
230 if (drvname == NULL || imgname == NULL)
231 return (EINVAL);
232
233 if (cwdi0.cwdi_cdir == NULL) {
234 printf("firmware_open(%s/%s) called too early.\n",
235 drvname, imgname);
236 return ENOENT;
237 }
238
239 pnbuf = PNBUF_GET();
240 KASSERT(pnbuf != NULL);
241
242 fh = firmware_handle_alloc();
243 KASSERT(fh != NULL);
244
245 pb = NULL;
246 error = 0;
247 for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix);
248 path != NULL;
249 path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) {
250 pb = pathbuf_create(path);
251 if (pb == NULL) {
252 error = ENOMEM;
253 break;
254 }
255 NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb);
256 error = vn_open(&nd, FREAD, 0);
257 if (error == ENOENT) {
258 pathbuf_destroy(pb);
259 continue;
260 }
261 break;
262 }
263
264 pathbuf_destroy(pb);
265 PNBUF_PUT(pnbuf);
266 if (error) {
267 firmware_handle_free(fh);
268 return (error);
269 }
270
271 vp = nd.ni_vp;
272
273 error = VOP_GETATTR(vp, &va, kauth_cred_get());
274 if (error) {
275 VOP_UNLOCK(vp);
276 (void)vn_close(vp, FREAD, kauth_cred_get());
277 firmware_handle_free(fh);
278 return (error);
279 }
280
281 if (va.va_type != VREG) {
282 VOP_UNLOCK(vp);
283 (void)vn_close(vp, FREAD, kauth_cred_get());
284 firmware_handle_free(fh);
285 return (EINVAL);
286 }
287
288 /* XXX Mark as busy text file. */
289
290 fh->fh_vp = vp;
291 fh->fh_size = va.va_size;
292
293 VOP_UNLOCK(vp);
294
295 *fhp = fh;
296 return (0);
297 }
298
299 /*
300 * firmware_close:
301 *
302 * Close a firmware image.
303 */
304 int
305 firmware_close(firmware_handle_t fh)
306 {
307 int error;
308
309 error = vn_close(fh->fh_vp, FREAD, kauth_cred_get());
310 firmware_handle_free(fh);
311 return (error);
312 }
313
314 /*
315 * firmware_get_size:
316 *
317 * Return the total size of a firmware image.
318 */
319 off_t
320 firmware_get_size(firmware_handle_t fh)
321 {
322
323 return (fh->fh_size);
324 }
325
326 /*
327 * firmware_read:
328 *
329 * Read data from a firmware image at the specified offset into
330 * the provided buffer.
331 */
332 int
333 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
334 {
335
336 return (vn_rdwr(UIO_READ, fh->fh_vp, buf, len, offset,
337 UIO_SYSSPACE, 0, kauth_cred_get(), NULL, curlwp));
338 }
339
340 /*
341 * firmware_malloc:
342 *
343 * Allocate a firmware buffer of the specified size.
344 *
345 * NOTE: This routine may block.
346 */
347 void *
348 firmware_malloc(size_t size)
349 {
350
351 return (malloc(size, M_DEVFIRM, M_WAITOK));
352 }
353
354 /*
355 * firmware_free:
356 *
357 * Free a previously allocated firmware buffer.
358 */
359 /*ARGSUSED*/
360 void
361 firmware_free(void *v, size_t size)
362 {
363
364 free(v, M_DEVFIRM);
365 }
366