Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.8
      1 /*	$NetBSD: ofdev.c,v 1.8 2002/09/18 01:45:25 chs 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 <sys/param.h>
     38 #include <sys/disklabel.h>
     39 #include <sys/disklabel_mbr.h>
     40 
     41 #include <netinet/in.h>
     42 
     43 #include <lib/libsa/stand.h>
     44 #include <lib/libsa/ufs.h>
     45 #include <lib/libsa/cd9660.h>
     46 #include <lib/libsa/dosfs.h>
     47 #include <lib/libsa/nfs.h>
     48 
     49 #include "ofdev.h"
     50 
     51 extern char bootdev[];
     52 
     53 #ifdef DEBUG
     54 # define DPRINTF printf
     55 #else
     56 # define DPRINTF while (0) printf
     57 #endif
     58 
     59 static char *
     60 filename(str, ppart)
     61 	char *str;
     62 	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 
     74 		/* For each component of the path name... */
     75 		while (*++cp && *cp != '/')
     76 			;
     77 		savec = *cp;
     78 		*cp = 0;
     79 
     80 		/* ...look whether there is a device with this name */
     81 		dhandle = OF_finddevice(str);
     82 		*cp = savec;
     83 		if (dhandle == -1) {
     84 
     85 			/*
     86 			 * if not, lp is the delimiter between device and path
     87 			 * if the last component was a block device.
     88 			 */
     89 
     90 			if (!strcmp(devtype, "block")) {
     91 
     92 				/* search for arguments */
     93 				for (cp = lp;
     94 				     --cp >= str && *cp != '/' && *cp != ':';)
     95 					;
     96 				if (cp >= str && *cp == ':') {
     97 
     98 					/*
     99 					 * found some arguments,
    100 					 * make OFW ignore them.
    101 					 */
    102 
    103 					*cp = 0;
    104 					for (cp = lp; *--cp && *cp != ',';)
    105 						;
    106 					if (*++cp >= 'a' &&
    107 					    *cp < 'a' + MAXPARTITIONS)
    108 						*ppart = *cp;
    109 				}
    110 			}
    111 			return lp;
    112 		}
    113 		if (OF_getprop(dhandle, "device_type", devtype,
    114 		    sizeof devtype) < 0)
    115 			devtype[0] = 0;
    116 	}
    117 	return 0;
    118 }
    119 
    120 static int
    121 strategy(devdata, rw, blk, size, buf, rsize)
    122 	void *devdata;
    123 	int rw;
    124 	daddr_t blk;
    125 	size_t size;
    126 	void *buf;
    127 	size_t *rsize;
    128 {
    129 	struct of_dev *dev = devdata;
    130 	uint64_t pos;
    131 	int n;
    132 
    133 	if (rw != F_READ)
    134 		return EPERM;
    135 	if (dev->type != OFDEV_DISK)
    136 		panic("strategy");
    137 
    138 	pos = (uint64_t)(blk + dev->partoff) * dev->bsize;
    139 
    140 	for (;;) {
    141 		if (OF_seek(dev->handle, pos) < 0)
    142 			break;
    143 		n = OF_read(dev->handle, buf, size);
    144 		if (n == -2)
    145 			continue;
    146 		if (n < 0)
    147 			break;
    148 		*rsize = n;
    149 		return 0;
    150 	}
    151 	return EIO;
    152 }
    153 
    154 static int
    155 devclose(of)
    156 	struct open_file *of;
    157 {
    158 	struct of_dev *op = of->f_devdata;
    159 
    160 	if (op->type == OFDEV_NET)
    161 		net_close(op);
    162 	OF_close(op->handle);
    163 	op->handle = -1;
    164 }
    165 
    166 static struct devsw devsw[1] = {
    167 	"OpenFirmware",
    168 	strategy,
    169 	(int (*)__P((struct open_file *, ...)))nodev,
    170 	devclose,
    171 	noioctl
    172 };
    173 int ndevs = sizeof devsw / sizeof devsw[0];
    174 
    175 static struct fs_ops file_system_ufs = {
    176 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
    177 };
    178 static struct fs_ops file_system_cd9660 = {
    179 	cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
    180 	    cd9660_stat
    181 };
    182 static struct fs_ops file_system_dosfs = {
    183 	dosfs_open, dosfs_close, dosfs_read, dosfs_write, dosfs_seek,
    184 	    dosfs_stat
    185 };
    186 static struct fs_ops file_system_nfs = {
    187 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
    188 };
    189 
    190 struct fs_ops file_system[3];
    191 int nfsys;
    192 
    193 static struct of_dev ofdev = {
    194 	-1,
    195 };
    196 
    197 char opened_name[256];
    198 int floppyboot;
    199 
    200 static u_long
    201 get_long(p)
    202 	const void *p;
    203 {
    204 	const unsigned char *cp = p;
    205 
    206 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    207 }
    208 
    209 /*
    210  * Find a valid disklabel.
    211  */
    212 static int
    213 search_label(devp, off, buf, lp, off0)
    214 	struct of_dev *devp;
    215 	u_long off;
    216 	char *buf;
    217 	struct disklabel *lp;
    218 	u_long off0;
    219 {
    220 	size_t read;
    221 	struct mbr_partition *p;
    222 	int i;
    223 	u_long poff;
    224 	static int recursion;
    225 
    226 	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    227 	    || read != DEV_BSIZE)
    228 		return ERDLAB;
    229 
    230 	if (*(u_int16_t *)&buf[MBR_MAGICOFF] != sa_htole16(MBR_MAGIC))
    231 		return ERDLAB;
    232 
    233 	if (recursion++ <= 1)
    234 		off0 += off;
    235 	for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 0;
    236 	     i < NMBRPART; i++, p++) {
    237 		if (p->mbrp_typ == MBR_PTYPE_NETBSD
    238 #ifdef COMPAT_386BSD_MBRPART
    239 		    || (p->mbrp_typ == MBR_PTYPE_386BSD &&
    240 			(printf("WARNING: old BSD partition ID!\n"), 1)
    241 			/* XXX XXX - libsa printf() is void */ )
    242 #endif
    243 		    ) {
    244 			poff = get_long(&p->mbrp_start) + off0;
    245 			if (strategy(devp, F_READ, poff + LABELSECTOR,
    246 				     DEV_BSIZE, buf, &read) == 0
    247 			    && read == DEV_BSIZE) {
    248 				if (!getdisklabel(buf, lp)) {
    249 					recursion--;
    250 					return 0;
    251 				}
    252 			}
    253 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    254 			    || read != DEV_BSIZE) {
    255 				recursion--;
    256 				return ERDLAB;
    257 			}
    258 		} else if (p->mbrp_typ == MBR_PTYPE_EXT) {
    259 			poff = get_long(&p->mbrp_start);
    260 			if (!search_label(devp, poff, buf, lp, off0)) {
    261 				recursion--;
    262 				return 0;
    263 			}
    264 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    265 			    || read != DEV_BSIZE) {
    266 				recursion--;
    267 				return ERDLAB;
    268 			}
    269 		}
    270 	}
    271 
    272 	recursion--;
    273 	return ERDLAB;
    274 }
    275 
    276 int
    277 devopen(of, name, file)
    278 	struct open_file *of;
    279 	const char *name;
    280 	char **file;
    281 {
    282 	char *cp;
    283 	char partition;
    284 	char fname[256];
    285 	char buf[DEV_BSIZE];
    286 	struct disklabel label;
    287 	int handle, part;
    288 	size_t read;
    289 	int error = 0;
    290 
    291 	if (ofdev.handle != -1)
    292 		panic("devopen");
    293 	if (of->f_flags != F_READ)
    294 		return EPERM;
    295 	strcpy(fname, name);
    296 	cp = filename(fname, &partition);
    297 	if (cp) {
    298 		DPRINTF("filename=%s\n", cp);
    299 		strcpy(buf, cp);
    300 		*cp = 0;
    301 	}
    302 	if (!cp || !*buf)
    303 		strcpy(buf, DEFAULT_KERNEL);
    304 	if (!*fname)
    305 		strcpy(fname, bootdev);
    306 	DPRINTF("fname=%s\n", fname);
    307 	strcpy(opened_name, fname);
    308 	if (partition) {
    309 		cp = opened_name + strlen(opened_name);
    310 		*cp++ = ':';
    311 		*cp++ = partition;
    312 		*cp = 0;
    313 	}
    314 	if (*buf != '/')
    315 		strcat(opened_name, "/");
    316 	strcat(opened_name, buf);
    317 	*file = opened_name + strlen(fname) + 1;
    318 	if (partition) {
    319 		*file += 2;
    320 	}
    321 	if ((handle = OF_finddevice(fname)) == -1) {
    322 		DPRINTF("OF_finddevice(\"%s\") failed\n", fname);
    323 		return ENOENT;
    324 	}
    325 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    326 		return ENXIO;
    327 	floppyboot = !strcmp(buf, "floppy");
    328 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    329 		return ENXIO;
    330 	if (!strcmp(buf, "block")) {
    331 
    332 		/*
    333 		 * For block devices, indicate raw partition
    334 		 * (:0 in OpenFirmware)
    335 		 */
    336 
    337 		strcat(fname, ":0");
    338 	}
    339 	if ((handle = OF_open(fname)) == -1)
    340 		return ENXIO;
    341 	memset(&ofdev, 0, sizeof ofdev);
    342 	ofdev.handle = handle;
    343 	if (!strcmp(buf, "block")) {
    344 		ofdev.type = OFDEV_DISK;
    345 		ofdev.bsize = DEV_BSIZE;
    346 
    347 		/* First try to find a disklabel without MBR partitions */
    348 		if (strategy(&ofdev, F_READ,
    349 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
    350 		    || read != DEV_BSIZE
    351 		    || getdisklabel(buf, &label)) {
    352 
    353 			/* Else try MBR partitions */
    354 			error = search_label(&ofdev, 0, buf, &label, 0);
    355 			if (error && error != ERDLAB)
    356 				goto bad;
    357 		}
    358 
    359 		if (error == ERDLAB) {
    360 			if (partition) {
    361 
    362 				/*
    363 				 * User specified a parititon,
    364 				 * but there is none.
    365 				 */
    366 
    367 				goto bad;
    368 			}
    369 
    370 			/* No label, just use complete disk */
    371 			ofdev.partoff = 0;
    372 		} else {
    373 			part = partition ? partition - 'a' : 0;
    374 			ofdev.partoff = label.d_partitions[part].p_offset;
    375 		}
    376 
    377 		of->f_dev = devsw;
    378 		of->f_devdata = &ofdev;
    379 		file_system[0] = file_system_ufs;
    380 		file_system[1] = file_system_cd9660;
    381 		file_system[2] = file_system_dosfs;
    382 		nfsys = 3;
    383 		return 0;
    384 	}
    385 	if (!strcmp(buf, "network")) {
    386 		ofdev.type = OFDEV_NET;
    387 		of->f_dev = devsw;
    388 		of->f_devdata = &ofdev;
    389 		file_system[0] = file_system_nfs;
    390 		nfsys = 1;
    391 		if (error = net_open(&ofdev))
    392 			goto bad;
    393 		return 0;
    394 	}
    395 	error = EFTYPE;
    396 bad:
    397 	OF_close(handle);
    398 	ofdev.handle = -1;
    399 	return error;
    400 }
    401