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