firmload.c revision 1.19 1 /* $NetBSD: firmload.c,v 1.19 2014/03/25 16:19:13 christos 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.19 2014/03/25 16:19:13 christos 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 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, &firmware_node,
127 CTLFLAG_PERMANENT,
128 CTLTYPE_NODE, "firmware", NULL,
129 NULL, 0, NULL, 0,
130 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
131 return;
132
133 sysctl_createv(clog, 0, NULL, NULL,
134 CTLFLAG_READWRITE,
135 CTLTYPE_STRING, "path",
136 SYSCTL_DESCR("Device firmware loading path list"),
137 sysctl_hw_firmware_path, 0, firmware_paths, PATH_MAX+1,
138 CTL_HW, firmware_node->sysctl_num, CTL_CREATE, CTL_EOL);
139 }
140
141 static char *
142 firmware_path_next(const char *drvname, const char *imgname, char *pnbuf,
143 char **prefixp)
144 {
145 char *prefix = *prefixp;
146 size_t maxprefix, i;
147
148 if (prefix == NULL /* terminated early */
149 || *prefix == '\0' /* no more left */
150 || *prefix != '/') { /* not absolute */
151 *prefixp = NULL;
152 return (NULL);
153 }
154
155 /*
156 * Compute the max path prefix based on the length of the provided
157 * names.
158 */
159 maxprefix = MAXPATHLEN -
160 (1 /* / */
161 + strlen(drvname)
162 + 1 /* / */
163 + strlen(imgname)
164 + 1 /* terminating NUL */);
165
166 /* Check for underflow (size_t is unsigned). */
167 if (maxprefix > MAXPATHLEN) {
168 *prefixp = NULL;
169 return (NULL);
170 }
171
172 for (i = 0; i < maxprefix; i++) {
173 if (*prefix == ':' || *prefix == '\0')
174 break;
175 pnbuf[i] = *prefix++;
176 }
177
178 if (*prefix != ':' && *prefix != '\0') {
179 /* Path prefix was too long. */
180 *prefixp = NULL;
181 return (NULL);
182 }
183
184 if (*prefix != '\0')
185 prefix++;
186 *prefixp = prefix;
187
188 KASSERT(MAXPATHLEN >= i);
189 snprintf(pnbuf + i, MAXPATHLEN - i, "/%s/%s", drvname, imgname);
190
191 return (pnbuf);
192 }
193
194 static char *
195 firmware_path_first(const char *drvname, const char *imgname, char *pnbuf,
196 char **prefixp)
197 {
198
199 *prefixp = firmware_paths;
200 return (firmware_path_next(drvname, imgname, pnbuf, prefixp));
201 }
202
203 /*
204 * firmware_open:
205 *
206 * Open a firmware image and return its handle.
207 */
208 int
209 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp)
210 {
211 struct pathbuf *pb;
212 struct nameidata nd;
213 struct vattr va;
214 char *pnbuf, *path, *prefix;
215 firmware_handle_t fh;
216 struct vnode *vp;
217 int error;
218 extern struct cwdinfo cwdi0;
219
220 if (drvname == NULL || imgname == NULL)
221 return (EINVAL);
222
223 if (cwdi0.cwdi_cdir == NULL) {
224 printf("firmware_open(%s/%s) called too early.\n",
225 drvname, imgname);
226 return ENOENT;
227 }
228
229 pnbuf = PNBUF_GET();
230 KASSERT(pnbuf != NULL);
231
232 fh = firmware_handle_alloc();
233 KASSERT(fh != NULL);
234
235 error = 0;
236 for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix);
237 path != NULL;
238 path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) {
239 pb = pathbuf_create(path);
240 if (pb == NULL) {
241 error = ENOMEM;
242 break;
243 }
244 NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb);
245 error = vn_open(&nd, FREAD, 0);
246 pathbuf_destroy(pb);
247 if (error == ENOENT) {
248 continue;
249 }
250 break;
251 }
252
253 PNBUF_PUT(pnbuf);
254 if (error) {
255 firmware_handle_free(fh);
256 return (error);
257 }
258
259 vp = nd.ni_vp;
260
261 error = VOP_GETATTR(vp, &va, kauth_cred_get());
262 if (error) {
263 VOP_UNLOCK(vp);
264 (void)vn_close(vp, FREAD, kauth_cred_get());
265 firmware_handle_free(fh);
266 return (error);
267 }
268
269 if (va.va_type != VREG) {
270 VOP_UNLOCK(vp);
271 (void)vn_close(vp, FREAD, kauth_cred_get());
272 firmware_handle_free(fh);
273 return (EINVAL);
274 }
275
276 /* XXX Mark as busy text file. */
277
278 fh->fh_vp = vp;
279 fh->fh_size = va.va_size;
280
281 VOP_UNLOCK(vp);
282
283 *fhp = fh;
284 return (0);
285 }
286
287 /*
288 * firmware_close:
289 *
290 * Close a firmware image.
291 */
292 int
293 firmware_close(firmware_handle_t fh)
294 {
295 int error;
296
297 error = vn_close(fh->fh_vp, FREAD, kauth_cred_get());
298 firmware_handle_free(fh);
299 return (error);
300 }
301
302 /*
303 * firmware_get_size:
304 *
305 * Return the total size of a firmware image.
306 */
307 off_t
308 firmware_get_size(firmware_handle_t fh)
309 {
310
311 return (fh->fh_size);
312 }
313
314 /*
315 * firmware_read:
316 *
317 * Read data from a firmware image at the specified offset into
318 * the provided buffer.
319 */
320 int
321 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
322 {
323
324 return (vn_rdwr(UIO_READ, fh->fh_vp, buf, len, offset,
325 UIO_SYSSPACE, 0, kauth_cred_get(), NULL, curlwp));
326 }
327
328 /*
329 * firmware_malloc:
330 *
331 * Allocate a firmware buffer of the specified size.
332 *
333 * NOTE: This routine may block.
334 */
335 void *
336 firmware_malloc(size_t size)
337 {
338
339 return (malloc(size, M_DEVFIRM, M_WAITOK));
340 }
341
342 /*
343 * firmware_free:
344 *
345 * Free a previously allocated firmware buffer.
346 */
347 /*ARGSUSED*/
348 void
349 firmware_free(void *v, size_t size)
350 {
351
352 free(v, M_DEVFIRM);
353 }
354