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