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