Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.2
      1 /*	$NetBSD: ofdev.c,v 1.2 1998/02/22 07:42:31 mycroft 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 	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 			poff = get_long(&p->mbr_start) + off0;
    209 			if (strategy(devp, F_READ, poff + LABELSECTOR,
    210 				     DEV_BSIZE, buf, &read) == 0
    211 			    && read == DEV_BSIZE) {
    212 				if (!getdisklabel(buf, lp)) {
    213 					recursion--;
    214 					return 0;
    215 				}
    216 			}
    217 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    218 			    || read != DEV_BSIZE) {
    219 				recursion--;
    220 				return ERDLAB;
    221 			}
    222 		} else if (p->mbr_type == MBR_EXTENDED) {
    223 			poff = get_long(&p->mbr_start);
    224 			if (!search_label(devp, poff, buf, lp, off0)) {
    225 				recursion--;
    226 				return 0;
    227 			}
    228 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    229 			    || read != DEV_BSIZE) {
    230 				recursion--;
    231 				return ERDLAB;
    232 			}
    233 		}
    234 	}
    235 	recursion--;
    236 	return ERDLAB;
    237 }
    238 
    239 int
    240 devopen(of, name, file)
    241 	struct open_file *of;
    242 	const char *name;
    243 	char **file;
    244 {
    245 	char *cp;
    246 	char partition;
    247 	char fname[256];
    248 	char buf[DEV_BSIZE];
    249 	struct disklabel label;
    250 	int handle, part;
    251 	size_t read;
    252 	int error = 0;
    253 
    254 	if (ofdev.handle != -1)
    255 		panic("devopen");
    256 	if (of->f_flags != F_READ)
    257 		return EPERM;
    258 	strcpy(fname, name);
    259 	cp = filename(fname, &partition);
    260 	if (cp) {
    261 		strcpy(buf, cp);
    262 		*cp = 0;
    263 	}
    264 	if (!cp || !*buf)
    265 		strcpy(buf, DEFAULT_KERNEL);
    266 	if (!*fname)
    267 		strcpy(fname, bootdev);
    268 	strcpy(opened_name, fname);
    269 	if (partition) {
    270 		cp = opened_name + strlen(opened_name);
    271 		*cp++ = ':';
    272 		*cp++ = partition;
    273 		*cp = 0;
    274 	}
    275 	if (*buf != '/')
    276 		strcat(opened_name, "/");
    277 	strcat(opened_name, buf);
    278 	*file = opened_name + strlen(fname) + 1;
    279 	if ((handle = OF_finddevice(fname)) == -1)
    280 		return ENOENT;
    281 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    282 		return ENXIO;
    283 	floppyboot = !strcmp(buf, "floppy");
    284 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    285 		return ENXIO;
    286 	if (!strcmp(buf, "block"))
    287 		/* For block devices, indicate raw partition (:0 in OpenFirmware) */
    288 		strcat(fname, ":0");
    289 	if ((handle = OF_open(fname)) == -1)
    290 		return ENXIO;
    291 	bzero(&ofdev, sizeof ofdev);
    292 	ofdev.handle = handle;
    293 	if (!strcmp(buf, "block")) {
    294 		ofdev.type = OFDEV_DISK;
    295 		ofdev.bsize = DEV_BSIZE;
    296 		/* First try to find a disklabel without MBR partitions */
    297 		if (strategy(&ofdev, F_READ,
    298 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
    299 		    || read != DEV_BSIZE
    300 		    || getdisklabel(buf, &label)) {
    301 			/* Else try MBR partitions */
    302 			error = search_label(&ofdev, 0, buf, &label, 0);
    303 			if (error && error != ERDLAB)
    304 				goto bad;
    305 		}
    306 
    307 		if (error == ERDLAB) {
    308 			if (partition)
    309 				/* User specified a parititon, but there is none */
    310 				goto bad;
    311 			/* No, label, just use complete disk */
    312 			ofdev.partoff = 0;
    313 		} else {
    314 			part = partition ? partition - 'a' : 0;
    315 			ofdev.partoff = label.d_partitions[part].p_offset;
    316 		}
    317 
    318 		of->f_dev = devsw;
    319 		of->f_devdata = &ofdev;
    320 		bcopy(&file_system_ufs, file_system, sizeof file_system[0]);
    321 		bcopy(&file_system_cd9660, file_system + 1,
    322 		    sizeof file_system[0]);
    323 		nfsys = 2;
    324 		return 0;
    325 	}
    326 	if (!strcmp(buf, "network")) {
    327 		ofdev.type = OFDEV_NET;
    328 		of->f_dev = devsw;
    329 		of->f_devdata = &ofdev;
    330 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
    331 		nfsys = 1;
    332 		if (error = net_open(&ofdev))
    333 			goto bad;
    334 		return 0;
    335 	}
    336 	error = EFTYPE;
    337 bad:
    338 	OF_close(handle);
    339 	ofdev.handle = -1;
    340 	return error;
    341 }
    342