Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.4.14.4
      1  1.4.14.4  jdolecek /*	$NetBSD: ofdev.c,v 1.4.14.4 2002/10/10 18:34:51 jdolecek Exp $	*/
      2       1.1   thorpej 
      3       1.1   thorpej /*
      4       1.1   thorpej  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5       1.1   thorpej  * Copyright (C) 1995, 1996 TooLs GmbH.
      6       1.1   thorpej  * All rights reserved.
      7       1.1   thorpej  *
      8       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      9       1.1   thorpej  * modification, are permitted provided that the following conditions
     10       1.1   thorpej  * are met:
     11       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     15       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     16       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     17       1.1   thorpej  *    must display the following acknowledgement:
     18       1.1   thorpej  *	This product includes software developed by TooLs GmbH.
     19       1.1   thorpej  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20       1.1   thorpej  *    derived from this software without specific prior written permission.
     21       1.1   thorpej  *
     22       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23       1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24       1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25       1.1   thorpej  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26       1.1   thorpej  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27       1.1   thorpej  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28       1.1   thorpej  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29       1.1   thorpej  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30       1.1   thorpej  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31       1.1   thorpej  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1   thorpej  */
     33       1.1   thorpej /*
     34       1.1   thorpej  * Device I/O routines using Open Firmware
     35       1.1   thorpej  */
     36       1.2   mycroft 
     37       1.1   thorpej #include <sys/param.h>
     38       1.1   thorpej #include <sys/disklabel.h>
     39       1.4  wrstuden #include <sys/disklabel_mbr.h>
     40       1.2   mycroft 
     41       1.1   thorpej #include <netinet/in.h>
     42       1.1   thorpej 
     43       1.1   thorpej #include <lib/libsa/stand.h>
     44       1.1   thorpej #include <lib/libsa/ufs.h>
     45       1.1   thorpej #include <lib/libsa/cd9660.h>
     46  1.4.14.2   thorpej #include <lib/libsa/dosfs.h>
     47       1.1   thorpej #include <lib/libsa/nfs.h>
     48       1.1   thorpej 
     49       1.2   mycroft #include "ofdev.h"
     50       1.1   thorpej 
     51       1.1   thorpej extern char bootdev[];
     52       1.1   thorpej 
     53  1.4.14.2   thorpej #ifdef DEBUG
     54  1.4.14.2   thorpej # define DPRINTF printf
     55  1.4.14.4  jdolecek #else
     56  1.4.14.2   thorpej # define DPRINTF while (0) printf
     57  1.4.14.4  jdolecek #endif
     58  1.4.14.2   thorpej 
     59       1.1   thorpej static char *
     60       1.1   thorpej filename(str, ppart)
     61       1.1   thorpej 	char *str;
     62       1.1   thorpej 	char *ppart;
     63       1.1   thorpej {
     64       1.1   thorpej 	char *cp, *lp;
     65       1.1   thorpej 	char savec;
     66       1.1   thorpej 	int dhandle;
     67       1.1   thorpej 	char devtype[16];
     68  1.4.14.4  jdolecek 
     69       1.1   thorpej 	lp = str;
     70       1.1   thorpej 	devtype[0] = 0;
     71       1.1   thorpej 	*ppart = 0;
     72       1.1   thorpej 	for (cp = str; *cp; lp = cp) {
     73  1.4.14.4  jdolecek 
     74       1.1   thorpej 		/* For each component of the path name... */
     75  1.4.14.4  jdolecek 		while (*++cp && *cp != '/')
     76  1.4.14.4  jdolecek 			;
     77       1.1   thorpej 		savec = *cp;
     78       1.1   thorpej 		*cp = 0;
     79  1.4.14.4  jdolecek 
     80       1.1   thorpej 		/* ...look whether there is a device with this name */
     81       1.1   thorpej 		dhandle = OF_finddevice(str);
     82       1.1   thorpej 		*cp = savec;
     83       1.1   thorpej 		if (dhandle == -1) {
     84  1.4.14.4  jdolecek 
     85  1.4.14.4  jdolecek 			/*
     86  1.4.14.4  jdolecek 			 * if not, lp is the delimiter between device and path
     87  1.4.14.4  jdolecek 			 * if the last component was a block device.
     88  1.4.14.4  jdolecek 			 */
     89  1.4.14.4  jdolecek 
     90       1.1   thorpej 			if (!strcmp(devtype, "block")) {
     91  1.4.14.4  jdolecek 
     92       1.1   thorpej 				/* search for arguments */
     93       1.1   thorpej 				for (cp = lp;
     94  1.4.14.4  jdolecek 				     --cp >= str && *cp != '/' && *cp != ':';)
     95  1.4.14.4  jdolecek 					;
     96       1.1   thorpej 				if (cp >= str && *cp == ':') {
     97  1.4.14.4  jdolecek 
     98  1.4.14.4  jdolecek 					/*
     99  1.4.14.4  jdolecek 					 * found some arguments,
    100  1.4.14.4  jdolecek 					 * make OFW ignore them.
    101  1.4.14.4  jdolecek 					 */
    102  1.4.14.4  jdolecek 
    103       1.1   thorpej 					*cp = 0;
    104  1.4.14.4  jdolecek 					for (cp = lp; *--cp && *cp != ',';)
    105  1.4.14.4  jdolecek 						;
    106  1.4.14.4  jdolecek 					if (*++cp >= 'a' &&
    107  1.4.14.4  jdolecek 					    *cp < 'a' + MAXPARTITIONS)
    108       1.1   thorpej 						*ppart = *cp;
    109       1.1   thorpej 				}
    110       1.1   thorpej 			}
    111       1.1   thorpej 			return lp;
    112  1.4.14.4  jdolecek 		}
    113  1.4.14.4  jdolecek 		if (OF_getprop(dhandle, "device_type", devtype,
    114  1.4.14.4  jdolecek 		    sizeof devtype) < 0)
    115       1.1   thorpej 			devtype[0] = 0;
    116       1.1   thorpej 	}
    117       1.1   thorpej 	return 0;
    118       1.1   thorpej }
    119       1.1   thorpej 
    120       1.1   thorpej static int
    121       1.1   thorpej strategy(devdata, rw, blk, size, buf, rsize)
    122       1.1   thorpej 	void *devdata;
    123       1.1   thorpej 	int rw;
    124       1.1   thorpej 	daddr_t blk;
    125       1.1   thorpej 	size_t size;
    126       1.1   thorpej 	void *buf;
    127       1.1   thorpej 	size_t *rsize;
    128       1.1   thorpej {
    129       1.1   thorpej 	struct of_dev *dev = devdata;
    130  1.4.14.4  jdolecek 	uint64_t pos;
    131       1.1   thorpej 	int n;
    132  1.4.14.4  jdolecek 
    133       1.1   thorpej 	if (rw != F_READ)
    134       1.1   thorpej 		return EPERM;
    135       1.1   thorpej 	if (dev->type != OFDEV_DISK)
    136       1.1   thorpej 		panic("strategy");
    137  1.4.14.4  jdolecek 
    138  1.4.14.4  jdolecek 	pos = (uint64_t)(blk + dev->partoff) * dev->bsize;
    139  1.4.14.4  jdolecek 
    140       1.1   thorpej 	for (;;) {
    141       1.1   thorpej 		if (OF_seek(dev->handle, pos) < 0)
    142       1.1   thorpej 			break;
    143       1.1   thorpej 		n = OF_read(dev->handle, buf, size);
    144       1.1   thorpej 		if (n == -2)
    145       1.1   thorpej 			continue;
    146       1.1   thorpej 		if (n < 0)
    147       1.1   thorpej 			break;
    148       1.1   thorpej 		*rsize = n;
    149       1.1   thorpej 		return 0;
    150       1.1   thorpej 	}
    151       1.1   thorpej 	return EIO;
    152       1.1   thorpej }
    153       1.1   thorpej 
    154       1.1   thorpej static int
    155       1.1   thorpej devclose(of)
    156       1.1   thorpej 	struct open_file *of;
    157       1.1   thorpej {
    158       1.1   thorpej 	struct of_dev *op = of->f_devdata;
    159  1.4.14.4  jdolecek 
    160       1.1   thorpej 	if (op->type == OFDEV_NET)
    161       1.1   thorpej 		net_close(op);
    162       1.1   thorpej 	OF_close(op->handle);
    163       1.1   thorpej 	op->handle = -1;
    164       1.1   thorpej }
    165       1.1   thorpej 
    166       1.1   thorpej static struct devsw devsw[1] = {
    167       1.1   thorpej 	"OpenFirmware",
    168       1.1   thorpej 	strategy,
    169       1.1   thorpej 	(int (*)__P((struct open_file *, ...)))nodev,
    170       1.1   thorpej 	devclose,
    171       1.1   thorpej 	noioctl
    172       1.1   thorpej };
    173       1.1   thorpej int ndevs = sizeof devsw / sizeof devsw[0];
    174       1.1   thorpej 
    175       1.1   thorpej static struct fs_ops file_system_ufs = {
    176       1.1   thorpej 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
    177       1.1   thorpej };
    178       1.1   thorpej static struct fs_ops file_system_cd9660 = {
    179       1.1   thorpej 	cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
    180       1.1   thorpej 	    cd9660_stat
    181       1.1   thorpej };
    182  1.4.14.2   thorpej static struct fs_ops file_system_dosfs = {
    183  1.4.14.2   thorpej 	dosfs_open, dosfs_close, dosfs_read, dosfs_write, dosfs_seek,
    184  1.4.14.2   thorpej 	    dosfs_stat
    185  1.4.14.2   thorpej };
    186       1.1   thorpej static struct fs_ops file_system_nfs = {
    187       1.1   thorpej 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
    188       1.1   thorpej };
    189       1.1   thorpej 
    190       1.1   thorpej struct fs_ops file_system[3];
    191       1.1   thorpej int nfsys;
    192       1.1   thorpej 
    193       1.1   thorpej static struct of_dev ofdev = {
    194       1.1   thorpej 	-1,
    195       1.1   thorpej };
    196       1.1   thorpej 
    197       1.1   thorpej char opened_name[256];
    198       1.1   thorpej int floppyboot;
    199       1.1   thorpej 
    200       1.1   thorpej static u_long
    201       1.1   thorpej get_long(p)
    202       1.1   thorpej 	const void *p;
    203       1.1   thorpej {
    204       1.1   thorpej 	const unsigned char *cp = p;
    205  1.4.14.4  jdolecek 
    206       1.1   thorpej 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    207       1.1   thorpej }
    208       1.1   thorpej 
    209       1.1   thorpej /*
    210       1.1   thorpej  * Find a valid disklabel.
    211       1.1   thorpej  */
    212       1.1   thorpej static int
    213       1.1   thorpej search_label(devp, off, buf, lp, off0)
    214       1.1   thorpej 	struct of_dev *devp;
    215       1.1   thorpej 	u_long off;
    216       1.1   thorpej 	char *buf;
    217       1.1   thorpej 	struct disklabel *lp;
    218       1.1   thorpej 	u_long off0;
    219       1.1   thorpej {
    220       1.1   thorpej 	size_t read;
    221       1.1   thorpej 	struct mbr_partition *p;
    222       1.1   thorpej 	int i;
    223       1.1   thorpej 	u_long poff;
    224       1.1   thorpej 	static int recursion;
    225  1.4.14.4  jdolecek 
    226       1.1   thorpej 	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    227       1.1   thorpej 	    || read != DEV_BSIZE)
    228       1.1   thorpej 		return ERDLAB;
    229  1.4.14.4  jdolecek 
    230  1.4.14.3  jdolecek 	if (*(u_int16_t *)&buf[MBR_MAGICOFF] != sa_htole16(MBR_MAGIC))
    231       1.1   thorpej 		return ERDLAB;
    232       1.1   thorpej 
    233       1.1   thorpej 	if (recursion++ <= 1)
    234       1.1   thorpej 		off0 += off;
    235  1.4.14.4  jdolecek 	for (p = (struct mbr_partition *)(buf + MBR_PARTOFF), i = 0;
    236  1.4.14.4  jdolecek 	     i < NMBRPART; i++, p++) {
    237       1.4  wrstuden 		if (p->mbrp_typ == MBR_PTYPE_NETBSD
    238       1.3  drochner #ifdef COMPAT_386BSD_MBRPART
    239       1.4  wrstuden 		    || (p->mbrp_typ == MBR_PTYPE_386BSD &&
    240       1.3  drochner 			(printf("WARNING: old BSD partition ID!\n"), 1)
    241       1.3  drochner 			/* XXX XXX - libsa printf() is void */ )
    242       1.3  drochner #endif
    243       1.3  drochner 		    ) {
    244       1.4  wrstuden 			poff = get_long(&p->mbrp_start) + off0;
    245       1.1   thorpej 			if (strategy(devp, F_READ, poff + LABELSECTOR,
    246       1.1   thorpej 				     DEV_BSIZE, buf, &read) == 0
    247       1.1   thorpej 			    && read == DEV_BSIZE) {
    248       1.1   thorpej 				if (!getdisklabel(buf, lp)) {
    249       1.1   thorpej 					recursion--;
    250       1.1   thorpej 					return 0;
    251       1.1   thorpej 				}
    252       1.1   thorpej 			}
    253       1.1   thorpej 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    254       1.1   thorpej 			    || read != DEV_BSIZE) {
    255       1.1   thorpej 				recursion--;
    256       1.1   thorpej 				return ERDLAB;
    257       1.1   thorpej 			}
    258       1.4  wrstuden 		} else if (p->mbrp_typ == MBR_PTYPE_EXT) {
    259       1.4  wrstuden 			poff = get_long(&p->mbrp_start);
    260       1.1   thorpej 			if (!search_label(devp, poff, buf, lp, off0)) {
    261       1.1   thorpej 				recursion--;
    262       1.1   thorpej 				return 0;
    263       1.1   thorpej 			}
    264       1.1   thorpej 			if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
    265       1.1   thorpej 			    || read != DEV_BSIZE) {
    266       1.1   thorpej 				recursion--;
    267       1.1   thorpej 				return ERDLAB;
    268       1.1   thorpej 			}
    269       1.1   thorpej 		}
    270       1.1   thorpej 	}
    271  1.4.14.4  jdolecek 
    272       1.1   thorpej 	recursion--;
    273       1.1   thorpej 	return ERDLAB;
    274       1.1   thorpej }
    275       1.1   thorpej 
    276       1.1   thorpej int
    277       1.1   thorpej devopen(of, name, file)
    278       1.1   thorpej 	struct open_file *of;
    279       1.1   thorpej 	const char *name;
    280       1.1   thorpej 	char **file;
    281       1.1   thorpej {
    282       1.1   thorpej 	char *cp;
    283       1.1   thorpej 	char partition;
    284       1.1   thorpej 	char fname[256];
    285       1.1   thorpej 	char buf[DEV_BSIZE];
    286       1.1   thorpej 	struct disklabel label;
    287       1.1   thorpej 	int handle, part;
    288       1.1   thorpej 	size_t read;
    289       1.1   thorpej 	int error = 0;
    290       1.1   thorpej 
    291       1.1   thorpej 	if (ofdev.handle != -1)
    292       1.1   thorpej 		panic("devopen");
    293       1.1   thorpej 	if (of->f_flags != F_READ)
    294       1.1   thorpej 		return EPERM;
    295       1.1   thorpej 	strcpy(fname, name);
    296       1.1   thorpej 	cp = filename(fname, &partition);
    297       1.1   thorpej 	if (cp) {
    298  1.4.14.2   thorpej 		DPRINTF("filename=%s\n", cp);
    299       1.1   thorpej 		strcpy(buf, cp);
    300       1.1   thorpej 		*cp = 0;
    301       1.1   thorpej 	}
    302       1.1   thorpej 	if (!cp || !*buf)
    303       1.1   thorpej 		strcpy(buf, DEFAULT_KERNEL);
    304       1.1   thorpej 	if (!*fname)
    305       1.1   thorpej 		strcpy(fname, bootdev);
    306  1.4.14.2   thorpej 	DPRINTF("fname=%s\n", fname);
    307       1.1   thorpej 	strcpy(opened_name, fname);
    308       1.1   thorpej 	if (partition) {
    309       1.1   thorpej 		cp = opened_name + strlen(opened_name);
    310       1.1   thorpej 		*cp++ = ':';
    311       1.1   thorpej 		*cp++ = partition;
    312       1.1   thorpej 		*cp = 0;
    313       1.1   thorpej 	}
    314       1.1   thorpej 	if (*buf != '/')
    315       1.1   thorpej 		strcat(opened_name, "/");
    316       1.1   thorpej 	strcat(opened_name, buf);
    317       1.1   thorpej 	*file = opened_name + strlen(fname) + 1;
    318  1.4.14.4  jdolecek 	if (partition) {
    319  1.4.14.4  jdolecek 		*file += 2;
    320  1.4.14.4  jdolecek 	}
    321  1.4.14.2   thorpej 	if ((handle = OF_finddevice(fname)) == -1) {
    322  1.4.14.2   thorpej 		DPRINTF("OF_finddevice(\"%s\") failed\n", fname);
    323       1.1   thorpej 		return ENOENT;
    324  1.4.14.2   thorpej 	}
    325       1.1   thorpej 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    326       1.1   thorpej 		return ENXIO;
    327       1.1   thorpej 	floppyboot = !strcmp(buf, "floppy");
    328       1.1   thorpej 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    329       1.1   thorpej 		return ENXIO;
    330  1.4.14.4  jdolecek 	if (!strcmp(buf, "block")) {
    331  1.4.14.4  jdolecek 
    332  1.4.14.4  jdolecek 		/*
    333  1.4.14.4  jdolecek 		 * For block devices, indicate raw partition
    334  1.4.14.4  jdolecek 		 * (:0 in OpenFirmware)
    335  1.4.14.4  jdolecek 		 */
    336  1.4.14.4  jdolecek 
    337       1.1   thorpej 		strcat(fname, ":0");
    338  1.4.14.4  jdolecek 	}
    339       1.1   thorpej 	if ((handle = OF_open(fname)) == -1)
    340       1.1   thorpej 		return ENXIO;
    341  1.4.14.1     lukem 	memset(&ofdev, 0, sizeof ofdev);
    342       1.1   thorpej 	ofdev.handle = handle;
    343       1.1   thorpej 	if (!strcmp(buf, "block")) {
    344       1.1   thorpej 		ofdev.type = OFDEV_DISK;
    345       1.1   thorpej 		ofdev.bsize = DEV_BSIZE;
    346  1.4.14.4  jdolecek 
    347       1.1   thorpej 		/* First try to find a disklabel without MBR partitions */
    348       1.1   thorpej 		if (strategy(&ofdev, F_READ,
    349       1.1   thorpej 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
    350       1.1   thorpej 		    || read != DEV_BSIZE
    351       1.1   thorpej 		    || getdisklabel(buf, &label)) {
    352  1.4.14.4  jdolecek 
    353       1.1   thorpej 			/* Else try MBR partitions */
    354       1.1   thorpej 			error = search_label(&ofdev, 0, buf, &label, 0);
    355       1.1   thorpej 			if (error && error != ERDLAB)
    356       1.1   thorpej 				goto bad;
    357       1.1   thorpej 		}
    358       1.1   thorpej 
    359       1.1   thorpej 		if (error == ERDLAB) {
    360  1.4.14.4  jdolecek 			if (partition) {
    361  1.4.14.4  jdolecek 
    362  1.4.14.4  jdolecek 				/*
    363  1.4.14.4  jdolecek 				 * User specified a parititon,
    364  1.4.14.4  jdolecek 				 * but there is none.
    365  1.4.14.4  jdolecek 				 */
    366  1.4.14.4  jdolecek 
    367       1.1   thorpej 				goto bad;
    368  1.4.14.4  jdolecek 			}
    369  1.4.14.4  jdolecek 
    370  1.4.14.4  jdolecek 			/* No label, just use complete disk */
    371       1.1   thorpej 			ofdev.partoff = 0;
    372       1.1   thorpej 		} else {
    373       1.1   thorpej 			part = partition ? partition - 'a' : 0;
    374       1.1   thorpej 			ofdev.partoff = label.d_partitions[part].p_offset;
    375       1.1   thorpej 		}
    376  1.4.14.4  jdolecek 
    377       1.1   thorpej 		of->f_dev = devsw;
    378       1.1   thorpej 		of->f_devdata = &ofdev;
    379  1.4.14.2   thorpej 		file_system[0] = file_system_ufs;
    380  1.4.14.2   thorpej 		file_system[1] = file_system_cd9660;
    381  1.4.14.4  jdolecek 		file_system[2] = file_system_dosfs;
    382  1.4.14.4  jdolecek 		nfsys = 3;
    383       1.1   thorpej 		return 0;
    384       1.1   thorpej 	}
    385       1.1   thorpej 	if (!strcmp(buf, "network")) {
    386       1.1   thorpej 		ofdev.type = OFDEV_NET;
    387       1.1   thorpej 		of->f_dev = devsw;
    388       1.1   thorpej 		of->f_devdata = &ofdev;
    389  1.4.14.2   thorpej 		file_system[0] = file_system_nfs;
    390       1.1   thorpej 		nfsys = 1;
    391       1.1   thorpej 		if (error = net_open(&ofdev))
    392       1.1   thorpej 			goto bad;
    393       1.1   thorpej 		return 0;
    394       1.1   thorpej 	}
    395       1.1   thorpej 	error = EFTYPE;
    396       1.1   thorpej bad:
    397       1.1   thorpej 	OF_close(handle);
    398       1.1   thorpej 	ofdev.handle = -1;
    399       1.1   thorpej 	return error;
    400       1.1   thorpej }
    401