1 /* $NetBSD: firmload.c,v 1.17 2012/04/29 20:27:31 dsl 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.17 2012/04/29 20:27:31 dsl 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, 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 error = 0; 246 for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix); 247 path != NULL; 248 path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) { 249 pb = pathbuf_create(path); 250 if (pb == NULL) { 251 error = ENOMEM; 252 break; 253 } 254 NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, pb); 255 error = vn_open(&nd, FREAD, 0); 256 pathbuf_destroy(pb); 257 if (error == ENOENT) { 258 continue; 259 } 260 break; 261 } 262 263 PNBUF_PUT(pnbuf); 264 if (error) { 265 firmware_handle_free(fh); 266 return (error); 267 } 268 269 vp = nd.ni_vp; 270 271 error = VOP_GETATTR(vp, &va, kauth_cred_get()); 272 if (error) { 273 VOP_UNLOCK(vp); 274 (void)vn_close(vp, FREAD, kauth_cred_get()); 275 firmware_handle_free(fh); 276 return (error); 277 } 278 279 if (va.va_type != VREG) { 280 VOP_UNLOCK(vp); 281 (void)vn_close(vp, FREAD, kauth_cred_get()); 282 firmware_handle_free(fh); 283 return (EINVAL); 284 } 285 286 /* XXX Mark as busy text file. */ 287 288 fh->fh_vp = vp; 289 fh->fh_size = va.va_size; 290 291 VOP_UNLOCK(vp); 292 293 *fhp = fh; 294 return (0); 295 } 296 297 /* 298 * firmware_close: 299 * 300 * Close a firmware image. 301 */ 302 int 303 firmware_close(firmware_handle_t fh) 304 { 305 int error; 306 307 error = vn_close(fh->fh_vp, FREAD, kauth_cred_get()); 308 firmware_handle_free(fh); 309 return (error); 310 } 311 312 /* 313 * firmware_get_size: 314 * 315 * Return the total size of a firmware image. 316 */ 317 off_t 318 firmware_get_size(firmware_handle_t fh) 319 { 320 321 return (fh->fh_size); 322 } 323 324 /* 325 * firmware_read: 326 * 327 * Read data from a firmware image at the specified offset into 328 * the provided buffer. 329 */ 330 int 331 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len) 332 { 333 334 return (vn_rdwr(UIO_READ, fh->fh_vp, buf, len, offset, 335 UIO_SYSSPACE, 0, kauth_cred_get(), NULL, curlwp)); 336 } 337 338 /* 339 * firmware_malloc: 340 * 341 * Allocate a firmware buffer of the specified size. 342 * 343 * NOTE: This routine may block. 344 */ 345 void * 346 firmware_malloc(size_t size) 347 { 348 349 return (malloc(size, M_DEVFIRM, M_WAITOK)); 350 } 351 352 /* 353 * firmware_free: 354 * 355 * Free a previously allocated firmware buffer. 356 */ 357 /*ARGSUSED*/ 358 void 359 firmware_free(void *v, size_t size) 360 { 361 362 free(v, M_DEVFIRM); 363 } 364