Home | History | Annotate | Line # | Download | only in dev
firmload.c revision 1.14
      1 /*	$NetBSD: firmload.c,v 1.14 2010/11/15 05:52:41 uebayasi 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.14 2010/11/15 05:52:41 uebayasi 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 nameidata nd;
    222 	struct vattr va;
    223 	char *pnbuf, *path, *prefix;
    224 	firmware_handle_t fh;
    225 	struct vnode *vp;
    226 	int error;
    227 	extern struct cwdinfo cwdi0;
    228 
    229 	if (drvname == NULL || imgname == NULL)
    230 		return (EINVAL);
    231 
    232 	if (cwdi0.cwdi_cdir == NULL) {
    233 		printf("firmware_open(%s/%s) called too early.\n",
    234 			drvname, imgname);
    235 		return ENOENT;
    236 	}
    237 
    238 	pnbuf = PNBUF_GET();
    239 	KASSERT(pnbuf != NULL);
    240 
    241 	fh = firmware_handle_alloc();
    242 	KASSERT(fh != NULL);
    243 
    244 	error = 0;
    245 	for (path = firmware_path_first(drvname, imgname, pnbuf, &prefix);
    246 	     path != NULL;
    247 	     path = firmware_path_next(drvname, imgname, pnbuf, &prefix)) {
    248 		NDINIT(&nd, LOOKUP, FOLLOW | NOCHROOT, UIO_SYSSPACE, path);
    249 		error = vn_open(&nd, FREAD, 0);
    250 		if (error == ENOENT)
    251 			continue;
    252 		break;
    253 	}
    254 
    255 	PNBUF_PUT(pnbuf);
    256 	if (error) {
    257 		firmware_handle_free(fh);
    258 		return (error);
    259 	}
    260 
    261 	vp = nd.ni_vp;
    262 
    263 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
    264 	if (error) {
    265 		VOP_UNLOCK(vp);
    266 		(void)vn_close(vp, FREAD, kauth_cred_get());
    267 		firmware_handle_free(fh);
    268 		return (error);
    269 	}
    270 
    271 	if (va.va_type != VREG) {
    272 		VOP_UNLOCK(vp);
    273 		(void)vn_close(vp, FREAD, kauth_cred_get());
    274 		firmware_handle_free(fh);
    275 		return (EINVAL);
    276 	}
    277 
    278 	/* XXX Mark as busy text file. */
    279 
    280 	fh->fh_vp = vp;
    281 	fh->fh_size = va.va_size;
    282 
    283 	VOP_UNLOCK(vp);
    284 
    285 	*fhp = fh;
    286 	return (0);
    287 }
    288 
    289 /*
    290  * firmware_close:
    291  *
    292  *	Close a firmware image.
    293  */
    294 int
    295 firmware_close(firmware_handle_t fh)
    296 {
    297 	int error;
    298 
    299 	error = vn_close(fh->fh_vp, FREAD, kauth_cred_get());
    300 	firmware_handle_free(fh);
    301 	return (error);
    302 }
    303 
    304 /*
    305  * firmware_get_size:
    306  *
    307  *	Return the total size of a firmware image.
    308  */
    309 off_t
    310 firmware_get_size(firmware_handle_t fh)
    311 {
    312 
    313 	return (fh->fh_size);
    314 }
    315 
    316 /*
    317  * firmware_read:
    318  *
    319  *	Read data from a firmware image at the specified offset into
    320  *	the provided buffer.
    321  */
    322 int
    323 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
    324 {
    325 
    326 	return (vn_rdwr(UIO_READ, fh->fh_vp, buf, len, offset,
    327 			UIO_SYSSPACE, 0, kauth_cred_get(), NULL, curlwp));
    328 }
    329 
    330 /*
    331  * firmware_malloc:
    332  *
    333  *	Allocate a firmware buffer of the specified size.
    334  *
    335  *	NOTE: This routine may block.
    336  */
    337 void *
    338 firmware_malloc(size_t size)
    339 {
    340 
    341 	return (malloc(size, M_DEVFIRM, M_WAITOK));
    342 }
    343 
    344 /*
    345  * firmware_free:
    346  *
    347  *	Free a previously allocated firmware buffer.
    348  */
    349 /*ARGSUSED*/
    350 void
    351 firmware_free(void *v, size_t size)
    352 {
    353 
    354 	free(v, M_DEVFIRM);
    355 }
    356