Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.22
      1 /*	$NetBSD: ofdev.c,v 1.22 2009/01/28 15:03:28 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 /*
     34  * Device I/O routines using Open Firmware
     35  */
     36 
     37 #include "ofdev.h"
     38 #include "openfirm.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/disklabel.h>
     42 #include <sys/bootblock.h>
     43 
     44 #include <netinet/in.h>
     45 
     46 #include <lib/libkern/libkern.h>
     47 
     48 #include <lib/libsa/stand.h>
     49 #include <lib/libsa/byteorder.h>
     50 #include <lib/libsa/cd9660.h>
     51 #include <lib/libsa/nfs.h>
     52 #include <lib/libsa/ufs.h>
     53 #include <lib/libsa/lfs.h>
     54 #include <lib/libsa/ustarfs.h>
     55 
     56 #include "hfs.h"
     57 #include "net.h"
     58 
     59 #ifdef DEBUG
     60 # define DPRINTF printf
     61 #else
     62 # define DPRINTF while (0) printf
     63 #endif
     64 
     65 static int
     66 strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
     67 	 size_t *rsize)
     68 {
     69 	struct of_dev *dev = devdata;
     70 	u_quad_t pos;
     71 	int n;
     72 
     73 	if (rw != F_READ)
     74 		return EPERM;
     75 	if (dev->type != OFDEV_DISK)
     76 		panic("strategy");
     77 
     78 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
     79 
     80 	for (;;) {
     81 		if (OF_seek(dev->handle, pos) < 0)
     82 			break;
     83 		n = OF_read(dev->handle, buf, size);
     84 		if (n == -2)
     85 			continue;
     86 		if (n < 0)
     87 			break;
     88 		*rsize = n;
     89 		return 0;
     90 	}
     91 	return EIO;
     92 }
     93 
     94 static int
     95 devopen_dummy(struct open_file *of, ...) {
     96 	return -1;
     97 }
     98 
     99 static int
    100 devclose(struct open_file *of)
    101 {
    102 	struct of_dev *op = of->f_devdata;
    103 
    104 	if (op->type == OFDEV_NET)
    105 		net_close(op);
    106 	OF_call_method("dma-free", op->handle, 2, 0, op->dmabuf, MAXPHYS);
    107 	OF_close(op->handle);
    108 	op->handle = -1;
    109 	return 0;
    110 }
    111 
    112 static struct devsw of_devsw[1] = {
    113 	{ "OpenFirmware", strategy, devopen_dummy, devclose, noioctl }
    114 };
    115 int ndevs = sizeof of_devsw / sizeof of_devsw[0];
    116 
    117 static struct fs_ops file_system_ffsv1 = FS_OPS(ffsv1);
    118 static struct fs_ops file_system_ffsv2 = FS_OPS(ffsv2);
    119 static struct fs_ops file_system_lfsv1 = FS_OPS(lfsv1);
    120 static struct fs_ops file_system_lfsv2 = FS_OPS(lfsv2);
    121 static struct fs_ops file_system_hfs = FS_OPS(hfs);
    122 static struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
    123 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
    124 static struct fs_ops file_system_nfs = FS_OPS(nfs);
    125 
    126 struct fs_ops file_system[8];
    127 int nfsys;
    128 
    129 static struct of_dev ofdev = {
    130 	-1,
    131 };
    132 
    133 char opened_name[MAXBOOTPATHLEN];
    134 
    135 static u_long
    136 get_long(const void *p)
    137 {
    138 	const unsigned char *cp = p;
    139 
    140 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    141 }
    142 
    143 /*
    144  * Find a valid disklabel.
    145  */
    146 static int
    147 search_label(struct of_dev *devp, u_long off, char *buf, struct disklabel *lp,
    148 	     u_long off0)
    149 {
    150 	size_t nread;
    151 	struct mbr_partition *p;
    152 	int i;
    153 	u_long poff;
    154 	static int recursion;
    155 
    156 	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    157 	    || nread != DEV_BSIZE)
    158 		return ERDLAB;
    159 
    160 	if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
    161 		return ERDLAB;
    162 
    163 	if (recursion++ <= 1)
    164 		off0 += off;
    165 	for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 4;
    166 	     --i >= 0; p++) {
    167 		if (p->mbrp_type == MBR_PTYPE_NETBSD
    168 #ifdef COMPAT_386BSD_MBRPART
    169 		    || (p->mbrp_type == MBR_PTYPE_386BSD &&
    170 			(printf("WARNING: old BSD partition ID!\n"), 1)
    171 			/* XXX XXX - libsa printf() is void */ )
    172 #endif
    173 		    ) {
    174 			poff = get_long(&p->mbrp_start) + off0;
    175 			if (strategy(devp, F_READ, poff + 1,
    176 				     DEV_BSIZE, buf, &nread) == 0
    177 			    && nread == DEV_BSIZE) {
    178 				if (!getdisklabel(buf, lp)) {
    179 					recursion--;
    180 					return 0;
    181 				}
    182 			}
    183 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    184 			    || nread != DEV_BSIZE) {
    185 				recursion--;
    186 				return ERDLAB;
    187 			}
    188 		} else if (p->mbrp_type == MBR_PTYPE_EXT) {
    189 			poff = get_long(&p->mbrp_start);
    190 			if (!search_label(devp, poff, buf, lp, off0)) {
    191 				recursion--;
    192 				return 0;
    193 			}
    194 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    195 			    || nread != DEV_BSIZE) {
    196 				recursion--;
    197 				return ERDLAB;
    198 			}
    199 		}
    200 	}
    201 	recursion--;
    202 	return ERDLAB;
    203 }
    204 
    205 
    206 bool
    207 parsefilepath(const char *path, char *devname, char *fname, char *ppart)
    208 {
    209 	char *cp, *lp;
    210 	char savec;
    211 	int dhandle;
    212 	char str[MAXBOOTPATHLEN];
    213 	char devtype[16];
    214 
    215 	DPRINTF("%s: path = %s\n", __func__, path);
    216 
    217 	devtype[0] = '\0';
    218 
    219 	if (devname != NULL)
    220 		devname[0] = '\0';
    221 	if (fname != NULL)
    222 		fname[0] = '\0';
    223 	if (ppart != NULL)
    224 		*ppart = 0;
    225 
    226 	strlcpy(str, path, sizeof(str));
    227 	lp = str;
    228 	for (cp = str; *cp != '\0'; lp = cp) {
    229 		/* For each component of the path name... */
    230 		while (*++cp != '\0' && *cp != '/')
    231 			continue;
    232 		savec = *cp;
    233 		*cp = '\0';
    234 		/* ...look whether there is a device with this name */
    235 		dhandle = OF_finddevice(str);
    236 		DPRINTF("%s: Checking %s: dhandle = %d\n",
    237 		    __func__, str, dhandle);
    238 		*cp = savec;
    239 		if (dhandle != -1) {
    240 			/*
    241 			 * If it's a vaild device, lp is a delimiter
    242 			 * in the OF device path.
    243 			 */
    244 			if (OF_getprop(dhandle, "device_type", devtype,
    245 			    sizeof devtype) < 0)
    246 				devtype[0] = '\0';
    247 			continue;
    248 		}
    249 
    250 		/*
    251 		 * If not, lp is the delimiter between OF device path
    252 		 * and the specified filename.
    253 		 */
    254 
    255 		/* Check if the last component was a block device... */
    256 		if (strcmp(devtype, "block") == 0) {
    257 			/* search for arguments */
    258 			for (cp = lp;
    259 			    --cp >= str && *cp != '/' && *cp != ':';)
    260 				continue;
    261 			if (cp >= str && *cp == ':') {
    262 				/* found arguments */
    263 				for (cp = lp;
    264 				    *--cp != ':' && *cp != ',';)
    265 					continue;
    266 				if (*++cp >= 'a' &&
    267 				    *cp <= 'a' + MAXPARTITIONS &&
    268 				    ppart != NULL)
    269 					*ppart = *cp;
    270 			}
    271 		}
    272 		if (*lp != '\0') {
    273 			/* set filename */
    274 			DPRINTF("%s: filename = %s\n", __func__, lp);
    275 			if (fname != NULL)
    276 				strcpy(fname, lp);
    277 			if (str != lp) {
    278 				/* set device path */
    279 				*lp = '\0';
    280 				DPRINTF("%s: device path = %s\n",
    281 				    __func__, str);
    282 				if (devname != NULL)
    283 					strcpy(devname, str);
    284 			}
    285 		}
    286 		return true;
    287 	}
    288 
    289 	DPRINTF("%s: invalid path?\n", __func__);
    290 	return false;
    291 }
    292 
    293 int
    294 devopen(struct open_file *of, const char *name, char **file)
    295 {
    296 	char *cp;
    297 	char partition;
    298 	char devname[MAXBOOTPATHLEN];
    299 	char filename[MAXBOOTPATHLEN];
    300 	char buf[DEV_BSIZE];
    301 	struct disklabel label;
    302 	int handle, part;
    303 	size_t nread;
    304 	int error = 0;
    305 
    306 	if (ofdev.handle != -1)
    307 		panic("devopen");
    308 	if (of->f_flags != F_READ)
    309 		return EPERM;
    310 
    311 	if (!parsefilepath(name, devname, filename, &partition))
    312 		return ENOENT;
    313 
    314 	if (filename[0] == '\0')
    315 		/* no filename */
    316 		return ENOENT;
    317 
    318 	if (devname[0] == '\0')
    319 		/* no device, use default bootdev */
    320 		strlcpy(devname, bootdev, sizeof(devname));
    321 
    322 	DPRINTF("%s: devname =  %s, filename = %s\n",
    323 	    __func__, devname, filename);
    324 
    325 	strlcpy(opened_name, devname, sizeof(opened_name));
    326 	if (partition) {
    327 		cp = opened_name + strlen(opened_name);
    328 		*cp++ = ':';
    329 		*cp++ = partition;
    330 		*cp = 0;
    331 	}
    332 	if (filename[0] != '/')
    333 		strlcat(opened_name, "/", sizeof(opened_name));
    334 	strlcat(opened_name, filename, sizeof(opened_name));
    335 
    336 	DPRINTF("%s: opened_name =  %s\n", __func__, opened_name);
    337 
    338 	*file = opened_name + strlen(devname) + 1;
    339 	if ((handle = OF_finddevice(devname)) == -1)
    340 		return ENOENT;
    341 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    342 		return ENXIO;
    343 #if 0
    344 	if (!strcmp(buf, "block"))
    345 		/*
    346 		 * For block devices, indicate raw partition
    347 		 * (:0 in OpenFirmware)
    348 		 */
    349 		strlcat(devname, ":0", sizeof(devname));
    350 #endif
    351 	if ((handle = OF_open(devname)) == -1)
    352 		return ENXIO;
    353 	memset(&ofdev, 0, sizeof ofdev);
    354 	ofdev.handle = handle;
    355 	ofdev.dmabuf = NULL;
    356 	OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
    357 	if (!strcmp(buf, "block")) {
    358 		ofdev.type = OFDEV_DISK;
    359 		ofdev.bsize = DEV_BSIZE;
    360 		/* First try to find a disklabel without MBR partitions */
    361 		if (strategy(&ofdev, F_READ,
    362 			     LABELSECTOR, DEV_BSIZE, buf, &nread) != 0
    363 		    || nread != DEV_BSIZE
    364 		    || getdisklabel(buf, &label)) {
    365 			/* Else try MBR partitions */
    366 			error = search_label(&ofdev, 0, buf, &label, 0);
    367 			if (error && error != ERDLAB)
    368 				goto bad;
    369 		}
    370 
    371 		if (error == ERDLAB) {
    372 			if (partition)
    373 				/*
    374 				 * User specified a parititon,
    375 				 * but there is none
    376 				 */
    377 				goto bad;
    378 			/* No, label, just use complete disk */
    379 			ofdev.partoff = 0;
    380 		} else {
    381 			part = partition ? partition - 'a' : 0;
    382 			ofdev.partoff = label.d_partitions[part].p_offset;
    383 		}
    384 
    385 		of->f_dev = of_devsw;
    386 		of->f_devdata = &ofdev;
    387 		file_system[0] = file_system_ffsv1;
    388 		file_system[1] = file_system_ffsv2;
    389 		file_system[2] = file_system_lfsv1;
    390 		file_system[3] = file_system_lfsv2;
    391 		file_system[4] = file_system_ustarfs;
    392 		file_system[5] = file_system_cd9660;
    393 		file_system[6] = file_system_hfs;
    394 		nfsys = 7;
    395 		return 0;
    396 	}
    397 	if (!strcmp(buf, "network")) {
    398 		ofdev.type = OFDEV_NET;
    399 		of->f_dev = of_devsw;
    400 		of->f_devdata = &ofdev;
    401 		file_system[0] = file_system_nfs;
    402 		nfsys = 1;
    403 		if ((error = net_open(&ofdev)))
    404 			goto bad;
    405 		return 0;
    406 	}
    407 	error = EFTYPE;
    408 bad:
    409 	OF_close(handle);
    410 	ofdev.handle = -1;
    411 	return error;
    412 }
    413