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