Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.17
      1 /*	$NetBSD: ofdev.c,v 1.17 2007/09/20 09:23:54 martin 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 #include <sys/param.h>
     37 #include <sys/disklabel.h>
     38 #ifdef NETBOOT
     39 #include <netinet/in.h>
     40 #endif
     41 
     42 #include <lib/libsa/stand.h>
     43 #include <lib/libsa/ufs.h>
     44 #include <lib/libsa/cd9660.h>
     45 #ifdef NETBOOT
     46 #include <lib/libsa/nfs.h>
     47 #endif
     48 #include <lib/libkern/libkern.h>
     49 
     50 #include <dev/sun/disklabel.h>
     51 #include <dev/raidframe/raidframevar.h>
     52 
     53 #include <machine/promlib.h>
     54 
     55 #include "ofdev.h"
     56 #include "boot.h"
     57 
     58 extern char bootdev[];
     59 
     60 /*
     61  * This is ugly.  A path on a sparc machine is something like this:
     62  *
     63  *	[device] [-<options] [path] [-options] [otherstuff] [-<more options]
     64  *
     65  */
     66 
     67 char *
     68 filename(char *str, char *ppart)
     69 {
     70 	char *cp, *lp;
     71 	char savec;
     72 	int dhandle;
     73 	char devtype[16];
     74 
     75 	lp = str;
     76 	devtype[0] = 0;
     77 	*ppart = '\0';
     78 	for (cp = str; *cp; lp = cp) {
     79 		/* For each component of the path name... */
     80 		while (*++cp && *cp != '/');
     81 		savec = *cp;
     82 		*cp = 0;
     83 		/* ...look whether there is a device with this name */
     84 		dhandle = prom_finddevice(str);
     85 		DPRINTF(("filename: prom_finddevice(%s) returned %x\n",
     86 		       str, dhandle));
     87 		*cp = savec;
     88 		if (dhandle == -1) {
     89 			/*
     90 			 * if not, lp is the delimiter between device and
     91 			 * path.  if the last component was a block device.
     92 			 */
     93 			if (!strcmp(devtype, "block")) {
     94 				/* search for arguments */
     95 				DPRINTF(("filename: hunting for arguments "
     96 				       "in %s\n", lp));
     97 				for (cp = lp; ; ) {
     98 					cp--;
     99 					if (cp < str ||
    100 					    cp[0] == '/' ||
    101 					    (cp[0] == ' ' && (cp+1) != lp &&
    102 					     cp[1] == '-'))
    103 						break;
    104 				}
    105 				if (cp >= str && *cp == '-')
    106 					/* found arguments, make firmware
    107 					   ignore them */
    108 					*cp = 0;
    109 				for (cp = lp; *--cp && *cp != ','
    110 					&& *cp != ':';)
    111 						;
    112 				if (cp[0] == ':' && cp[1] >= 'a' &&
    113 				    cp[1] <= 'a' + MAXPARTITIONS) {
    114 					*ppart = cp[1];
    115 					cp[0] = '\0';
    116 				}
    117 			}
    118 			DPRINTF(("filename: found %s\n",lp));
    119 			return lp;
    120 		} else if (_prom_getprop(dhandle, "device_type", devtype,
    121 				sizeof devtype) < 0)
    122 			devtype[0] = 0;
    123 	}
    124 	DPRINTF(("filename: not found\n",lp));
    125 	return 0;
    126 }
    127 
    128 static int
    129 strategy(devdata, rw, blk, size, buf, rsize)
    130 	void *devdata;
    131 	int rw;
    132 	daddr_t blk;
    133 	size_t size;
    134 	void *buf;
    135 	size_t *rsize;
    136 {
    137 	struct of_dev *dev = devdata;
    138 	u_quad_t pos;
    139 	int n;
    140 
    141 	if (rw != F_READ)
    142 		return EPERM;
    143 	if (dev->type != OFDEV_DISK)
    144 		panic("strategy");
    145 
    146 #ifdef NON_DEBUG
    147 	printf("strategy: block %lx, partition offset %lx, blksz %lx\n",
    148 	       (long)blk, (long)dev->partoff, (long)dev->bsize);
    149 	printf("strategy: seek position should be: %lx\n",
    150 	       (long)((blk + dev->partoff) * dev->bsize));
    151 #endif
    152 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
    153 
    154 	for (;;) {
    155 #ifdef NON_DEBUG
    156 		printf("strategy: seeking to %lx\n", (long)pos);
    157 #endif
    158 		if (prom_seek(dev->handle, pos) < 0)
    159 			break;
    160 #ifdef NON_DEBUG
    161 		printf("strategy: reading %lx at %p\n", (long)size, buf);
    162 #endif
    163 		n = prom_read(dev->handle, buf, size);
    164 		if (n == -2)
    165 			continue;
    166 		if (n < 0)
    167 			break;
    168 		*rsize = n;
    169 		return 0;
    170 	}
    171 	return EIO;
    172 }
    173 
    174 static int
    175 devclose(struct open_file *of)
    176 {
    177 	struct of_dev *op = of->f_devdata;
    178 
    179 #ifdef NETBOOT
    180 	if (op->type == OFDEV_NET)
    181 		net_close(op);
    182 #endif
    183 	prom_close(op->handle);
    184 	op->handle = -1;
    185 }
    186 
    187 static struct devsw ofdevsw[1] = {
    188 	"OpenFirmware",
    189 	strategy,
    190 	(int (*)(struct open_file *, ...))nodev,
    191 	devclose,
    192 	noioctl
    193 };
    194 int ndevs = sizeof ofdevsw / sizeof ofdevsw[0];
    195 
    196 #ifdef SPARC_BOOT_UFS
    197 static struct fs_ops file_system_ufs = FS_OPS(ufs);
    198 #endif
    199 #ifdef SPARC_BOOT_CD9660
    200 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
    201 #endif
    202 #ifdef NETBOOT
    203 static struct fs_ops file_system_nfs = FS_OPS(nfs);
    204 #endif
    205 
    206 struct fs_ops file_system[3];
    207 int nfsys;
    208 
    209 static struct of_dev ofdev = {
    210 	-1,
    211 };
    212 
    213 char opened_name[256];
    214 int floppyboot;
    215 
    216 static u_long
    217 get_long(const void *p)
    218 {
    219 	const unsigned char *cp = p;
    220 
    221 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    222 }
    223 /************************************************************************
    224  *
    225  * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c
    226  * and then substantially rewritten by Gordon W. Ross
    227  *
    228  ************************************************************************/
    229 
    230 /* What partition types to assume for Sun disklabels: */
    231 static u_char
    232 sun_fstypes[8] = {
    233 	FS_BSDFFS,	/* a */
    234 	FS_SWAP,	/* b */
    235 	FS_OTHER,	/* c - whole disk */
    236 	FS_BSDFFS,	/* d */
    237 	FS_BSDFFS,	/* e */
    238 	FS_BSDFFS,	/* f */
    239 	FS_BSDFFS,	/* g */
    240 	FS_BSDFFS,	/* h */
    241 };
    242 
    243 /*
    244  * Given a SunOS disk label, set lp to a BSD disk label.
    245  * Returns NULL on success, else an error string.
    246  *
    247  * The BSD label is cleared out before this is called.
    248  */
    249 static char *
    250 disklabel_sun_to_bsd(char *cp, struct disklabel *lp)
    251 {
    252 	struct sun_disklabel *sl;
    253 	struct partition *npp;
    254 	struct sun_dkpart *spp;
    255 	int i, secpercyl;
    256 	u_short cksum, *sp1, *sp2;
    257 
    258 	sl = (struct sun_disklabel *)cp;
    259 
    260 	/* Verify the XOR check. */
    261 	sp1 = (u_short *)sl;
    262 	sp2 = (u_short *)(sl + 1);
    263 	cksum = 0;
    264 	while (sp1 < sp2)
    265 		cksum ^= *sp1++;
    266 	if (cksum != 0)
    267 		return("SunOS disk label, bad checksum");
    268 
    269 	/* Format conversion. */
    270 	lp->d_magic = DISKMAGIC;
    271 	lp->d_magic2 = DISKMAGIC;
    272 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
    273 
    274 	lp->d_secsize = 512;
    275 	lp->d_nsectors   = sl->sl_nsectors;
    276 	lp->d_ntracks    = sl->sl_ntracks;
    277 	lp->d_ncylinders = sl->sl_ncylinders;
    278 
    279 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    280 	lp->d_secpercyl  = secpercyl;
    281 	lp->d_secperunit = secpercyl * sl->sl_ncylinders;
    282 
    283 	lp->d_sparespercyl = sl->sl_sparespercyl;
    284 	lp->d_acylinders   = sl->sl_acylinders;
    285 	lp->d_rpm          = sl->sl_rpm;
    286 	lp->d_interleave   = sl->sl_interleave;
    287 
    288 	lp->d_npartitions = 8;
    289 	/* These are as defined in <ufs/ffs/fs.h> */
    290 	lp->d_bbsize = 8192;	/* XXX */
    291 	lp->d_sbsize = 8192;	/* XXX */
    292 
    293 	for (i = 0; i < 8; i++) {
    294 		spp = &sl->sl_part[i];
    295 		npp = &lp->d_partitions[i];
    296 		npp->p_offset = spp->sdkp_cyloffset * secpercyl;
    297 		npp->p_size = spp->sdkp_nsectors;
    298 		DPRINTF(("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size));
    299 		if (npp->p_size == 0) {
    300 			npp->p_fstype = FS_UNUSED;
    301 		} else {
    302 			npp->p_fstype = sun_fstypes[i];
    303 			if (npp->p_fstype == FS_BSDFFS) {
    304 				/*
    305 				 * The sun label does not store the FFS fields,
    306 				 * so just set them with default values here.
    307 				 */
    308 				npp->p_fsize = 1024;
    309 				npp->p_frag = 8;
    310 				npp->p_cpg = 16;
    311 			}
    312 		}
    313 	}
    314 
    315 	lp->d_checksum = 0;
    316 	lp->d_checksum = dkcksum(lp);
    317 	DPRINTF(("disklabel_sun_to_bsd: success!\n"));
    318 	return (NULL);
    319 }
    320 
    321 /*
    322  * Find a valid disklabel.
    323  */
    324 static char *
    325 search_label(struct of_dev *devp, u_long off, char *buf,
    326 	     struct disklabel *lp, u_long off0)
    327 {
    328 	size_t read;
    329 	struct mbr_partition *p;
    330 	int i;
    331 	u_long poff;
    332 	static int recursion;
    333 
    334 	struct disklabel *dlp;
    335 	struct sun_disklabel *slp;
    336 	int error;
    337 
    338 	/* minimal requirements for archtypal disk label */
    339 	if (lp->d_secperunit == 0)
    340 		lp->d_secperunit = 0x1fffffff;
    341 	lp->d_npartitions = 1;
    342 	if (lp->d_partitions[0].p_size == 0)
    343 		lp->d_partitions[0].p_size = 0x1fffffff;
    344 	lp->d_partitions[0].p_offset = 0;
    345 
    346 	if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read)
    347 	    || read != DEV_BSIZE)
    348 		return ("Cannot read label");
    349 	/* Check for a NetBSD disk label. */
    350 	dlp = (struct disklabel *) (buf + LABELOFFSET);
    351 	if (dlp->d_magic == DISKMAGIC) {
    352 		if (dkcksum(dlp))
    353 			return ("NetBSD disk label corrupted");
    354 		*lp = *dlp;
    355 		DPRINTF(("search_label: found NetBSD label\n"));
    356 		return (NULL);
    357 	}
    358 
    359 	/* Check for a Sun disk label (for PROM compatibility). */
    360 	slp = (struct sun_disklabel *) buf;
    361 	if (slp->sl_magic == SUN_DKMAGIC)
    362 		return (disklabel_sun_to_bsd(buf, lp));
    363 
    364 
    365 	bzero(buf, sizeof(buf));
    366 	return ("no disk label");
    367 }
    368 
    369 int
    370 devopen(struct open_file *of, const char *name, char **file)
    371 {
    372 	char *cp;
    373 	char partition;
    374 	char fname[256], devname[256];
    375 	union {
    376 		char buf[DEV_BSIZE];
    377 		struct disklabel label;
    378 	} b;
    379 	struct disklabel label;
    380 	int handle, part, try = 0;
    381 	size_t read;
    382 	char *errmsg = NULL, *pp, savedpart = 0;
    383 	int error = 0;
    384 
    385 	if (ofdev.handle != -1)
    386 		panic("devopen");
    387 	if (of->f_flags != F_READ)
    388 		return EPERM;
    389 	DPRINTF(("devopen: you want %s\n", name));
    390 	strcpy(fname, name);
    391 	cp = filename(fname, &partition);
    392 	if (cp) {
    393 		strcpy(b.buf, cp);
    394 		*cp = 0;
    395 	}
    396 	if (!cp || !b.buf[0])
    397 		strcpy(b.buf, DEFAULT_KERNEL);
    398 	if (!*fname)
    399 		strcpy(fname, bootdev);
    400 	strcpy(opened_name, fname);
    401 	if (partition) {
    402 		cp = opened_name + strlen(opened_name);
    403 		*cp++ = ':';
    404 		*cp++ = partition;
    405 		*cp = 0;
    406 	}
    407 	*file = opened_name + strlen(opened_name);
    408 	if (b.buf[0] != '/')
    409 		strcat(opened_name, "/");
    410 	strcat(opened_name, b.buf);
    411 	DPRINTF(("devopen: trying %s\n", fname));
    412 	if ((handle = prom_finddevice(fname)) == -1)
    413 		return ENOENT;
    414 	DPRINTF(("devopen: found %s\n", fname));
    415 	if (_prom_getprop(handle, "name", b.buf, sizeof b.buf) < 0)
    416 		return ENXIO;
    417 	DPRINTF(("devopen: %s is called %s\n", fname, b.buf));
    418 	floppyboot = !strcmp(b.buf, "floppy");
    419 	if (_prom_getprop(handle, "device_type", b.buf, sizeof b.buf) < 0)
    420 		return ENXIO;
    421 	DPRINTF(("devopen: %s is a %s device\n", fname, b.buf));
    422 	if (!strcmp(b.buf, "block")) {
    423 		pp = strrchr(fname, ':');
    424 		if (pp && pp[1] >= 'a' && pp[1] <= 'f' && pp[2] == 0) {
    425 			savedpart = pp[1];
    426 		} else {
    427 			savedpart = 'a';
    428 			handle = prom_open(fname);
    429 			if (handle != -1) {
    430 				OF_instance_to_path(handle, devname,
    431 				    sizeof(devname));
    432 				DPRINTF(("real path: %s\n", devname));
    433 				prom_close(handle);
    434 				pp = devname + strlen(devname);
    435 				if (pp > devname + 3) pp -= 2;
    436 				if (pp[0] == ':')
    437 					savedpart = pp[1];
    438 			}
    439 			pp = fname + strlen(fname);
    440 			pp[0] = ':';
    441 			pp[2] = '\0';
    442 		}
    443 		pp[1] = 'c';
    444 		DPRINTF(("devopen: replacing by whole disk device %s\n",
    445 		    fname));
    446 		if (savedpart)
    447 			partition = savedpart;
    448 	}
    449 
    450 open_again:
    451 	DPRINTF(("devopen: opening %s\n", fname));
    452 	if ((handle = prom_open(fname)) == -1) {
    453 		DPRINTF(("devopen: open of %s failed\n", fname));
    454 		if (pp && savedpart) {
    455 			if (try == 0) {
    456 				pp[0] = '\0';
    457 				try = 1;
    458 			} else {
    459 				pp[0] = ':';
    460 				pp[1] = savedpart;
    461 				pp = NULL;
    462 				savedpart = '\0';
    463 			}
    464 			goto open_again;
    465 		}
    466 		return ENXIO;
    467 	}
    468 	DPRINTF(("devopen: %s is now open\n", fname));
    469 	bzero(&ofdev, sizeof ofdev);
    470 	ofdev.handle = handle;
    471 	if (!strcmp(b.buf, "block")) {
    472 		ofdev.type = OFDEV_DISK;
    473 		ofdev.bsize = DEV_BSIZE;
    474 		/* First try to find a disklabel without MBR partitions */
    475 		DPRINTF(("devopen: trying to read disklabel\n"));
    476 		if (strategy(&ofdev, F_READ,
    477 			     LABELSECTOR, DEV_BSIZE, b.buf, &read) != 0
    478 		    || read != DEV_BSIZE
    479 		    || (errmsg = getdisklabel(b.buf, &label))) {
    480 			if (errmsg) printf("devopen: getdisklabel returned %s\n", errmsg);
    481 			/* Else try MBR partitions */
    482 			errmsg = search_label(&ofdev, 0, b.buf, &label, 0);
    483 			if (errmsg) {
    484 				printf("devopen: search_label returned %s\n", errmsg);
    485 				error = ERDLAB;
    486 			}
    487 			if (error && error != ERDLAB)
    488 				goto bad;
    489 		}
    490 
    491 		if (error == ERDLAB) {
    492 			/* No, label, just use complete disk */
    493 			ofdev.partoff = 0;
    494 			if (pp && savedpart) {
    495 				pp[1] = savedpart;
    496 				prom_close(handle);
    497 				if ((handle = prom_open(fname)) == -1) {
    498 					DPRINTF(("devopen: open of %s failed\n",
    499 						fname));
    500 					return ENXIO;
    501 				}
    502 				ofdev.handle = handle;
    503 				DPRINTF(("devopen: back to original device %s\n",
    504 					fname));
    505 			}
    506 		} else {
    507 			part = partition ? partition - 'a' : 0;
    508 			ofdev.partoff = label.d_partitions[part].p_offset;
    509 			DPRINTF(("devopen: setting partition %d offset %x\n",
    510 			       part, ofdev.partoff));
    511 			if (label.d_partitions[part].p_fstype == FS_RAID) {
    512 				ofdev.partoff += RF_PROTECTED_SECTORS;
    513 				DPRINTF(("devopen: found RAID partition, "
    514 				    "adjusting offset to %x\n", ofdev.partoff));
    515 			}
    516 		}
    517 
    518 		nfsys = 0;
    519 		of->f_dev = ofdevsw;
    520 		of->f_devdata = &ofdev;
    521 #ifdef SPARC_BOOT_UFS
    522 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
    523 #endif
    524 #ifdef SPARC_BOOT_CD9660
    525 		bcopy(&file_system_cd9660, &file_system[nfsys++],
    526 		    sizeof file_system[0]);
    527 #endif
    528 		DPRINTF(("devopen: return 0\n"));
    529 		return 0;
    530 	}
    531 #ifdef NETBOOT
    532 	if (!strcmp(b.buf, "network")) {
    533 		ofdev.type = OFDEV_NET;
    534 		of->f_dev = ofdevsw;
    535 		of->f_devdata = &ofdev;
    536 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
    537 		nfsys = 1;
    538 		if (error = net_open(&ofdev))
    539 			goto bad;
    540 		return 0;
    541 	}
    542 #endif
    543 	error = EFTYPE;
    544 bad:
    545 	DPRINTF(("devopen: error %d, cannot open device\n", error));
    546 	prom_close(handle);
    547 	ofdev.handle = -1;
    548 	return error;
    549 }
    550