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