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