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