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