Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.20
      1 /*	$NetBSD: ofdev.c,v 1.20 2008/05/22 19:49:43 hauke 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 extern char bootdev[];
     60 
     61 static char *
     62 filename(char *str, char *ppart)
     63 {
     64 	char *cp, *lp;
     65 	char savec;
     66 	int dhandle;
     67 	char devtype[16];
     68 
     69 	lp = str;
     70 	devtype[0] = 0;
     71 	*ppart = 0;
     72 	for (cp = str; *cp; lp = cp) {
     73 		/* For each component of the path name... */
     74 		while (*++cp && *cp != '/')
     75 			continue;
     76 		savec = *cp;
     77 		*cp = 0;
     78 		/* ...look whether there is a device with this name */
     79 		dhandle = OF_finddevice(str);
     80 		*cp = savec;
     81 		if (dhandle == -1) {
     82 			/*
     83 			 * if not, lp is the delimiter between device and path
     84 			 */
     85 			/* if the last component was a block device... */
     86 			if (!strcmp(devtype, "block")) {
     87 				/* search for arguments */
     88 				for (cp = lp;
     89 				    --cp >= str && *cp != '/' && *cp != ':';)
     90 					continue;
     91 				if (cp >= str && *cp == ':') {
     92 					/* found arguments */
     93 					for (cp = lp;
     94 					    *--cp != ':' && *cp != ',';)
     95 						continue;
     96 					if (*++cp >= 'a' &&
     97 					    *cp <= 'a' + MAXPARTITIONS)
     98 						*ppart = *cp;
     99 				}
    100 			}
    101 			return lp;
    102 		} else if (OF_getprop(dhandle, "device_type", devtype,
    103 		    sizeof devtype) < 0)
    104 			devtype[0] = 0;
    105 	}
    106 	return 0;
    107 }
    108 
    109 static int
    110 strategy(void *devdata, int rw, daddr_t blk, size_t size, void *buf,
    111 	 size_t *rsize)
    112 {
    113 	struct of_dev *dev = devdata;
    114 	u_quad_t pos;
    115 	int n;
    116 
    117 	if (rw != F_READ)
    118 		return EPERM;
    119 	if (dev->type != OFDEV_DISK)
    120 		panic("strategy");
    121 
    122 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
    123 
    124 	for (;;) {
    125 		if (OF_seek(dev->handle, pos) < 0)
    126 			break;
    127 		n = OF_read(dev->handle, buf, size);
    128 		if (n == -2)
    129 			continue;
    130 		if (n < 0)
    131 			break;
    132 		*rsize = n;
    133 		return 0;
    134 	}
    135 	return EIO;
    136 }
    137 
    138 static int
    139 devopen_dummy(struct open_file *of, ...) {
    140 	return -1;
    141 }
    142 
    143 static int
    144 devclose(struct open_file *of)
    145 {
    146 	struct of_dev *op = of->f_devdata;
    147 
    148 	if (op->type == OFDEV_NET)
    149 		net_close(op);
    150 	OF_call_method("dma-free", op->handle, 2, 0, op->dmabuf, MAXPHYS);
    151 	OF_close(op->handle);
    152 	op->handle = -1;
    153 	return 0;
    154 }
    155 
    156 static struct devsw of_devsw[1] = {
    157 	{ "OpenFirmware", strategy, devopen_dummy, devclose, noioctl }
    158 };
    159 int ndevs = sizeof of_devsw / sizeof of_devsw[0];
    160 
    161 static struct fs_ops file_system_ffsv1 = FS_OPS(ffsv1);
    162 static struct fs_ops file_system_ffsv2 = FS_OPS(ffsv2);
    163 static struct fs_ops file_system_lfsv1 = FS_OPS(lfsv1);
    164 static struct fs_ops file_system_lfsv2 = FS_OPS(lfsv2);
    165 static struct fs_ops file_system_hfs = FS_OPS(hfs);
    166 static struct fs_ops file_system_ustarfs = FS_OPS(ustarfs);
    167 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
    168 static struct fs_ops file_system_nfs = FS_OPS(nfs);
    169 
    170 struct fs_ops file_system[8];
    171 int nfsys;
    172 
    173 static struct of_dev ofdev = {
    174 	-1,
    175 };
    176 
    177 char opened_name[256];
    178 int floppyboot;
    179 
    180 static u_long
    181 get_long(const void *p)
    182 {
    183 	const unsigned char *cp = p;
    184 
    185 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    186 }
    187 
    188 /*
    189  * Find a valid disklabel.
    190  */
    191 static int
    192 search_label(struct of_dev *devp, u_long off, u_char *buf, struct disklabel *lp,
    193 	     u_long off0)
    194 {
    195 	size_t nread;
    196 	struct mbr_partition *p;
    197 	int i;
    198 	u_long poff;
    199 	static int recursion;
    200 
    201 	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    202 	    || nread != DEV_BSIZE)
    203 		return ERDLAB;
    204 
    205 	if (*(u_int16_t *)&buf[MBR_MAGIC_OFFSET] != sa_htole16(MBR_MAGIC))
    206 		return ERDLAB;
    207 
    208 	if (recursion++ <= 1)
    209 		off0 += off;
    210 	for (p = (struct mbr_partition *)(buf + MBR_PART_OFFSET), i = 4;
    211 	     --i >= 0; p++) {
    212 		if (p->mbrp_type == MBR_PTYPE_NETBSD
    213 #ifdef COMPAT_386BSD_MBRPART
    214 		    || (p->mbrp_type == MBR_PTYPE_386BSD &&
    215 			(printf("WARNING: old BSD partition ID!\n"), 1)
    216 			/* XXX XXX - libsa printf() is void */ )
    217 #endif
    218 		    ) {
    219 			poff = get_long(&p->mbrp_start) + off0;
    220 			if (strategy(devp, F_READ, poff + 1,
    221 				     DEV_BSIZE, buf, &nread) == 0
    222 			    && nread == DEV_BSIZE) {
    223 				if (!getdisklabel(buf, lp)) {
    224 					recursion--;
    225 					return 0;
    226 				}
    227 			}
    228 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    229 			    || nread != DEV_BSIZE) {
    230 				recursion--;
    231 				return ERDLAB;
    232 			}
    233 		} else if (p->mbrp_type == MBR_PTYPE_EXT) {
    234 			poff = get_long(&p->mbrp_start);
    235 			if (!search_label(devp, poff, buf, lp, off0)) {
    236 				recursion--;
    237 				return 0;
    238 			}
    239 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &nread)
    240 			    || nread != DEV_BSIZE) {
    241 				recursion--;
    242 				return ERDLAB;
    243 			}
    244 		}
    245 	}
    246 	recursion--;
    247 	return ERDLAB;
    248 }
    249 
    250 int
    251 devopen(struct open_file *of, const char *name, char **file)
    252 {
    253 	char *cp;
    254 	char partition;
    255 	char fname[256];
    256 	char buf[DEV_BSIZE];
    257 	struct disklabel label;
    258 	int handle, part;
    259 	size_t nread;
    260 	int error = 0;
    261 
    262 	if (ofdev.handle != -1)
    263 		panic("devopen");
    264 	if (of->f_flags != F_READ)
    265 		return EPERM;
    266 	strcpy(fname, name);
    267 	cp = filename(fname, &partition);
    268 	if (cp) {
    269 		strcpy(buf, cp);
    270 		*cp = 0;
    271 	}
    272 	if (!cp || !*buf)
    273 		return ENOENT;
    274 	if (!*fname)
    275 		strcpy(fname, bootdev);
    276 	strcpy(opened_name, fname);
    277 	if (partition) {
    278 		cp = opened_name + strlen(opened_name);
    279 		*cp++ = ':';
    280 		*cp++ = partition;
    281 		*cp = 0;
    282 	}
    283 	if (*buf != '/')
    284 		strcat(opened_name, "/");
    285 	strcat(opened_name, buf);
    286 	*file = opened_name + strlen(fname) + 1;
    287 	if ((handle = OF_finddevice(fname)) == -1)
    288 		return ENOENT;
    289 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    290 		return ENXIO;
    291 	floppyboot = !strcmp(buf, "floppy");
    292 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    293 		return ENXIO;
    294 #if 0
    295 	if (!strcmp(buf, "block"))
    296 		/*
    297 		 * For block devices, indicate raw partition
    298 		 * (:0 in OpenFirmware)
    299 		 */
    300 		strcat(fname, ":0");
    301 #endif
    302 	if ((handle = OF_open(fname)) == -1)
    303 		return ENXIO;
    304 	memset(&ofdev, 0, sizeof ofdev);
    305 	ofdev.handle = handle;
    306 	ofdev.dmabuf = NULL;
    307 	OF_call_method("dma-alloc", handle, 1, 1, MAXPHYS, &ofdev.dmabuf);
    308 	if (!strcmp(buf, "block")) {
    309 		ofdev.type = OFDEV_DISK;
    310 		ofdev.bsize = DEV_BSIZE;
    311 		/* First try to find a disklabel without MBR partitions */
    312 		if (strategy(&ofdev, F_READ,
    313 			     LABELSECTOR, DEV_BSIZE, buf, &nread) != 0
    314 		    || nread != DEV_BSIZE
    315 		    || getdisklabel(buf, &label)) {
    316 			/* Else try MBR partitions */
    317 			error = search_label(&ofdev, 0, buf, &label, 0);
    318 			if (error && error != ERDLAB)
    319 				goto bad;
    320 		}
    321 
    322 		if (error == ERDLAB) {
    323 			if (partition)
    324 				/*
    325 				 * User specified a parititon,
    326 				 * but there is none
    327 				 */
    328 				goto bad;
    329 			/* No, label, just use complete disk */
    330 			ofdev.partoff = 0;
    331 		} else {
    332 			part = partition ? partition - 'a' : 0;
    333 			ofdev.partoff = label.d_partitions[part].p_offset;
    334 		}
    335 
    336 		of->f_dev = of_devsw;
    337 		of->f_devdata = &ofdev;
    338 		file_system[0] = file_system_ffsv1;
    339 		file_system[1] = file_system_ffsv2;
    340 		file_system[2] = file_system_lfsv1;
    341 		file_system[3] = file_system_lfsv2;
    342 		file_system[4] = file_system_ustarfs;
    343 		file_system[5] = file_system_cd9660;
    344 		file_system[6] = file_system_hfs;
    345 		nfsys = 7;
    346 		return 0;
    347 	}
    348 	if (!strcmp(buf, "network")) {
    349 		ofdev.type = OFDEV_NET;
    350 		of->f_dev = of_devsw;
    351 		of->f_devdata = &ofdev;
    352 		file_system[0] = file_system_nfs;
    353 		nfsys = 1;
    354 		if ((error = net_open(&ofdev)))
    355 			goto bad;
    356 		return 0;
    357 	}
    358 	error = EFTYPE;
    359 bad:
    360 	OF_close(handle);
    361 	ofdev.handle = -1;
    362 	return error;
    363 }
    364