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