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