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