1 /* $NetBSD: firmload.c,v 1.3 2006/05/14 21:42:26 elad 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.3 2006/05/14 21:42:26 elad 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 #include <sys/kauth.h> 55 56 #include <dev/firmload.h> 57 58 static MALLOC_DEFINE(M_DEVFIRM, "devfirm", "device firmware buffers"); 59 60 struct firmware_handle { 61 struct vnode *fh_vp; 62 off_t fh_size; 63 }; 64 65 static firmware_handle_t 66 firmware_handle_alloc(void) 67 { 68 69 return (malloc(sizeof(struct firmware_handle), M_DEVFIRM, M_WAITOK)); 70 } 71 72 static void 73 firmware_handle_free(firmware_handle_t fh) 74 { 75 76 free(fh, M_DEVFIRM); 77 } 78 79 #if !defined(FIRMWARE_PATHS) 80 #define FIRMWARE_PATHS \ 81 "/libdata/firmware:/usr/libdata/firmware:/usr/pkg/libdata/firmware:/usr/pkg/libdata" 82 #endif 83 84 static char firmware_paths[PATH_MAX+1] = FIRMWARE_PATHS; 85 86 static int 87 sysctl_hw_firmware_path(SYSCTLFN_ARGS) 88 { 89 int error, i; 90 char newpath[PATH_MAX+1]; 91 struct sysctlnode node; 92 char expected_char; 93 94 node = *rnode; 95 node.sysctl_data = &newpath[0]; 96 memcpy(node.sysctl_data, rnode->sysctl_data, PATH_MAX+1); 97 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 98 if (error || newp == NULL) 99 return (error); 100 101 /* 102 * Make sure that all of the paths in the new path list are 103 * absolute. 104 * 105 * When sysctl_lookup() deals with a string, it's guaranteed 106 * to come back nul-terminated. 107 */ 108 expected_char = '/'; 109 for (i = 0; i < PATH_MAX+1; i++) { 110 if (expected_char != 0 && newpath[i] != expected_char) 111 return (EINVAL); 112 if (newpath[i] == '\0') 113 break; 114 else if (newpath[i] == ':') 115 expected_char = '/'; 116 else 117 expected_char = 0; 118 } 119 120 memcpy(rnode->sysctl_data, node.sysctl_data, PATH_MAX+1); 121 122 return (0); 123 } 124 125 SYSCTL_SETUP_PROTO(sysctl_hw_firmware_setup); 126 127 SYSCTL_SETUP(sysctl_hw_firmware_setup, "sysctl hw.firmware subtree setup") 128 { 129 const struct sysctlnode *firmware_node; 130 131 if (sysctl_createv(clog, 0, NULL, NULL, 132 CTLFLAG_PERMANENT, 133 CTLTYPE_NODE, "hw", NULL, 134 NULL, 0, NULL, 0, 135 CTL_HW, CTL_EOL) != 0) 136 return; 137 138 if (sysctl_createv(clog, 0, NULL, &firmware_node, 139 CTLFLAG_PERMANENT, 140 CTLTYPE_NODE, "firmware", NULL, 141 NULL, 0, NULL, 0, 142 CTL_HW, CTL_CREATE, CTL_EOL) != 0) 143 return; 144 145 sysctl_createv(clog, 0, NULL, NULL, 146 CTLFLAG_READWRITE, 147 CTLTYPE_STRING, "path", 148 SYSCTL_DESCR("Device firmware loading path list"), 149 sysctl_hw_firmware_path, 0, firmware_paths, PATH_MAX+1, 150 CTL_HW, firmware_node->sysctl_num, CTL_CREATE, CTL_EOL); 151 } 152 153 static char * 154 firmware_path_next(const char *drvname, const char *imgname, char *pnbuf, 155 char **prefixp) 156 { 157 char *prefix = *prefixp; 158 size_t maxprefix, i; 159 160 if (prefix == NULL /* terminated early */ 161 || *prefix == '\0' /* no more left */ 162 || *prefix != '/') { /* not absolute */ 163 *prefixp = NULL; 164 return (NULL); 165 } 166 167 /* 168 * Compute the max path prefix based on the length of the provided 169 * names. 170 */ 171 maxprefix = MAXPATHLEN - 172 (1 /* / */ 173 + strlen(drvname) 174 + 1 /* / */ 175 + strlen(imgname) 176 + 1 /* terminating NUL */); 177 178 /* Check for underflow (size_t is unsigned). */ 179 if (maxprefix > MAXPATHLEN) { 180 *prefixp = NULL; 181 return (NULL); 182 } 183 184 for (i = 0; i < maxprefix; i++) { 185 if (*prefix == ':' || *prefix == '\0') 186 break; 187 pnbuf[i] = *prefix++; 188 } 189 190 if (*prefix != ':' && *prefix != '\0') { 191 /* Path prefix was too long. */ 192 *prefixp = NULL; 193 return (NULL); 194 } 195 196 if (*prefix != '\0') 197 prefix++; 198 *prefixp = prefix; 199 200 /* 201 * This sprintf() is safe because of the maxprefix calculation 202 * performed above. 203 */ 204 sprintf(&pnbuf[i], "/%s/%s", drvname, imgname); 205 206 return (pnbuf); 207 } 208 209 static char * 210 firmware_path_first(const char *drvname, const char *imgname, char *pnbuf, 211 char **prefixp) 212 { 213 214 *prefixp = firmware_paths; 215 return (firmware_path_next(drvname, imgname, pnbuf, prefixp)); 216 } 217 218 /* 219 * firmware_open: 220 * 221 * Open a firmware image and return its handle. 222 */ 223 int 224 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp) 225 { 226 struct nameidata nd; 227 struct vattr va; 228 char *pnbuf, *path, *prefix; 229 firmware_handle_t fh; 230 struct vnode *vp; 231 int error; 232 233 if (drvname == NULL || imgname == NULL) 234 return (EINVAL); 235 236 pnbuf = PNBUF_GET(); 237 KASSERT(pnbuf != NULL); 238 239 fh = firmware_handle_alloc(); 240 KASSERT(fh != NULL); 241 242 error = 0; 243 for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix); 244 path != NULL; 245 path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) { 246 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, curlwp); 247 error = vn_open(&nd, FREAD, 0); 248 if (error == ENOENT) 249 continue; 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, FSCRED, curlwp); 262 if (error) { 263 VOP_UNLOCK(vp, 0); 264 (void)vn_close(vp, FREAD, FSCRED, curlwp); 265 firmware_handle_free(fh); 266 return (error); 267 } 268 269 if (va.va_type != VREG) { 270 VOP_UNLOCK(vp, 0); 271 (void)vn_close(vp, FREAD, FSCRED, curlwp); 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, 0); 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, FSCRED, curlwp); 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, FSCRED, 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