Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.13
      1 /*	$NetBSD: ofdev.c,v 1.13 2007/09/14 09:19:39 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 
     57 extern char bootdev[];
     58 
     59 /*
     60  * This is ugly.  A path on a sparc machine is something like this:
     61  *
     62  *	[device] [-<options] [path] [-options] [otherstuff] [-<more options]
     63  *
     64  */
     65 
     66 static char *
     67 filename(char *str, char *ppart)
     68 {
     69 	char *cp, *lp;
     70 	char savec;
     71 	int dhandle;
     72 	char devtype[16];
     73 
     74 	lp = str;
     75 	devtype[0] = 0;
     76 	*ppart = 0;
     77 	for (cp = str; *cp; lp = cp) {
     78 		/* For each component of the path name... */
     79 		while (*++cp && *cp != '/');
     80 		savec = *cp;
     81 		*cp = 0;
     82 		/* ...look whether there is a device with this name */
     83 		dhandle = prom_finddevice(str);
     84 #ifdef NOTDEF_DEBUG
     85 		printf("filename: prom_finddevice(%s) returned %x\n",
     86 		       str, dhandle);
     87 #endif
     88 		*cp = savec;
     89 		if (dhandle == -1) {
     90 			/*
     91 			 * if not, lp is the delimiter between device and
     92 			 * path.  if the last component was a block device.
     93 			 */
     94 			if (!strcmp(devtype, "block")) {
     95 				/* search for arguments */
     96 #ifdef NOTDEF_DEBUG
     97 				printf("filename: hunting for arguments "
     98 				       "in %s\n", str);
     99 #endif
    100 				for (cp = lp; ; ) {
    101 					cp--;
    102 					if (cp < str ||
    103 					    cp[0] == '/' ||
    104 					    (cp[0] == ' ' && (cp+1) != lp &&
    105 					     cp[1] == '-'))
    106 						break;
    107 				}
    108 				if (cp >= str && *cp == '-')
    109 					*cp = 0;	/* found arguments, make firmware ignore them */
    110 				for (cp = lp; *--cp && *cp != ',' && *cp != ':';);
    111 				if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS) {
    112 					*ppart = *cp;
    113 					*--cp = '\0';
    114 				}
    115 			}
    116 #ifdef NOTDEF_DEBUG
    117 			printf("filename: found %s\n",lp);
    118 #endif
    119 			return lp;
    120 		} else if (_prom_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
    121 			devtype[0] = 0;
    122 	}
    123 #ifdef NOTDEF_DEBUG
    124 	printf("filename: not found\n",lp);
    125 #endif
    126 	return 0;
    127 }
    128 
    129 static int
    130 strategy(devdata, rw, blk, size, buf, rsize)
    131 	void *devdata;
    132 	int rw;
    133 	daddr_t blk;
    134 	size_t size;
    135 	void *buf;
    136 	size_t *rsize;
    137 {
    138 	struct of_dev *dev = devdata;
    139 	u_quad_t pos;
    140 	int n;
    141 
    142 	if (rw != F_READ)
    143 		return EPERM;
    144 	if (dev->type != OFDEV_DISK)
    145 		panic("strategy");
    146 
    147 #ifdef NON_DEBUG
    148 	printf("strategy: block %lx, partition offset %lx, blksz %lx\n",
    149 	       (long)blk, (long)dev->partoff, (long)dev->bsize);
    150 	printf("strategy: seek position should be: %lx\n",
    151 	       (long)((blk + dev->partoff) * dev->bsize));
    152 #endif
    153 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
    154 
    155 	for (;;) {
    156 #ifdef NON_DEBUG
    157 		printf("strategy: seeking to %lx\n", (long)pos);
    158 #endif
    159 		if (prom_seek(dev->handle, pos) < 0)
    160 			break;
    161 #ifdef NON_DEBUG
    162 		printf("strategy: reading %lx at %p\n", (long)size, buf);
    163 #endif
    164 		n = prom_read(dev->handle, buf, size);
    165 		if (n == -2)
    166 			continue;
    167 		if (n < 0)
    168 			break;
    169 		*rsize = n;
    170 		return 0;
    171 	}
    172 	return EIO;
    173 }
    174 
    175 static int
    176 devclose(struct open_file *of)
    177 {
    178 	struct of_dev *op = of->f_devdata;
    179 
    180 #ifdef NETBOOT
    181 	if (op->type == OFDEV_NET)
    182 		net_close(op);
    183 #endif
    184 	prom_close(op->handle);
    185 	op->handle = -1;
    186 }
    187 
    188 static struct devsw ofdevsw[1] = {
    189 	"OpenFirmware",
    190 	strategy,
    191 	(int (*)(struct open_file *, ...))nodev,
    192 	devclose,
    193 	noioctl
    194 };
    195 int ndevs = sizeof ofdevsw / sizeof ofdevsw[0];
    196 
    197 #ifdef SPARC_BOOT_UFS
    198 static struct fs_ops file_system_ufs = FS_OPS(ufs);
    199 #endif
    200 #ifdef SPARC_BOOT_HSFS
    201 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
    202 #endif
    203 #ifdef NETBOOT
    204 static struct fs_ops file_system_nfs = FS_OPS(nfs);
    205 #endif
    206 
    207 struct fs_ops file_system[3];
    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 #ifdef NOTDEF_DEBUG
    300 		printf("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size);
    301 #endif
    302 		if (npp->p_size == 0) {
    303 			npp->p_fstype = FS_UNUSED;
    304 		} else {
    305 			npp->p_fstype = sun_fstypes[i];
    306 			if (npp->p_fstype == FS_BSDFFS) {
    307 				/*
    308 				 * The sun label does not store the FFS fields,
    309 				 * so just set them with default values here.
    310 				 */
    311 				npp->p_fsize = 1024;
    312 				npp->p_frag = 8;
    313 				npp->p_cpg = 16;
    314 			}
    315 		}
    316 	}
    317 
    318 	lp->d_checksum = 0;
    319 	lp->d_checksum = dkcksum(lp);
    320 #ifdef NOTDEF_DEBUG
    321 	printf("disklabel_sun_to_bsd: success!\n");
    322 #endif
    323 	return (NULL);
    324 }
    325 
    326 /*
    327  * Find a valid disklabel.
    328  */
    329 static char *
    330 search_label(struct of_dev *devp, u_long off, char *buf,
    331 	     struct disklabel *lp, u_long off0)
    332 {
    333 	size_t read;
    334 	struct mbr_partition *p;
    335 	int i;
    336 	u_long poff;
    337 	static int recursion;
    338 
    339 	struct disklabel *dlp;
    340 	struct sun_disklabel *slp;
    341 	int error;
    342 
    343 	/* minimal requirements for archtypal disk label */
    344 	if (lp->d_secperunit == 0)
    345 		lp->d_secperunit = 0x1fffffff;
    346 	lp->d_npartitions = 1;
    347 	if (lp->d_partitions[0].p_size == 0)
    348 		lp->d_partitions[0].p_size = 0x1fffffff;
    349 	lp->d_partitions[0].p_offset = 0;
    350 
    351 	if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read)
    352 	    || read != DEV_BSIZE)
    353 		return ("Cannot read label");
    354 	/* Check for a NetBSD disk label. */
    355 	dlp = (struct disklabel *) (buf + LABELOFFSET);
    356 	if (dlp->d_magic == DISKMAGIC) {
    357 		if (dkcksum(dlp))
    358 			return ("NetBSD disk label corrupted");
    359 		*lp = *dlp;
    360 #ifdef NOTDEF_DEBUG
    361 		printf("search_label: found NetBSD label\n");
    362 #endif
    363 		return (NULL);
    364 	}
    365 
    366 	/* Check for a Sun disk label (for PROM compatibility). */
    367 	slp = (struct sun_disklabel *) buf;
    368 	if (slp->sl_magic == SUN_DKMAGIC)
    369 		return (disklabel_sun_to_bsd(buf, lp));
    370 
    371 
    372 	bzero(buf, sizeof(buf));
    373 	return ("no disk label");
    374 }
    375 
    376 int
    377 devopen(struct open_file *of, const char *name, char **file)
    378 {
    379 	char *cp;
    380 	char partition;
    381 	char fname[256];
    382 	union {
    383 		char buf[DEV_BSIZE];
    384 		struct disklabel label;
    385 	} b;
    386 	struct disklabel label;
    387 	int handle, part;
    388 	size_t read;
    389 	char *errmsg = NULL;
    390 	int error = 0;
    391 
    392 	if (ofdev.handle != -1)
    393 		panic("devopen");
    394 	if (of->f_flags != F_READ)
    395 		return EPERM;
    396 #ifdef NOTDEF_DEBUG
    397 	printf("devopen: you want %s\n", name);
    398 #endif
    399 	strcpy(fname, name);
    400 	cp = filename(fname, &partition);
    401 	if (cp) {
    402 		strcpy(b.buf, cp);
    403 		*cp = 0;
    404 	}
    405 	if (!cp || !b.buf[0])
    406 		strcpy(b.buf, DEFAULT_KERNEL);
    407 	if (!*fname)
    408 		strcpy(fname, bootdev);
    409 	strcpy(opened_name, fname);
    410 	if (partition) {
    411 		cp = opened_name + strlen(opened_name);
    412 		*cp++ = ':';
    413 		*cp++ = partition;
    414 		*cp = 0;
    415 	}
    416 	*file = opened_name + strlen(opened_name);
    417 	if (b.buf[0] != '/')
    418 		strcat(opened_name, "/");
    419 	strcat(opened_name, b.buf);
    420 #ifdef NOTDEF_DEBUG
    421 	printf("devopen: trying %s\n", fname);
    422 #endif
    423 	if ((handle = prom_finddevice(fname)) == -1)
    424 		return ENOENT;
    425 #ifdef NOTDEF_DEBUG
    426 	printf("devopen: found %s\n", fname);
    427 #endif
    428 	if (_prom_getprop(handle, "name", b.buf, sizeof b.buf) < 0)
    429 		return ENXIO;
    430 #ifdef NOTDEF_DEBUG
    431 	printf("devopen: %s is called %s\n", fname, b.buf);
    432 #endif
    433 	floppyboot = !strcmp(b.buf, "floppy");
    434 	if (_prom_getprop(handle, "device_type", b.buf, sizeof b.buf) < 0)
    435 		return ENXIO;
    436 #ifdef NOTDEF_DEBUG
    437 	printf("devopen: %s is a %s device\n", fname, b.buf);
    438 #endif
    439 #ifdef NOTDEF_DEBUG
    440 	printf("devopen: opening %s\n", fname);
    441 #endif
    442 	if ((handle = prom_open(fname)) == -1) {
    443 #ifdef NOTDEF_DEBUG
    444 		printf("devopen: open of %s failed\n", fname);
    445 #endif
    446 		return ENXIO;
    447 	}
    448 #ifdef NOTDEF_DEBUG
    449 	printf("devopen: %s is now open\n", fname);
    450 #endif
    451 	bzero(&ofdev, sizeof ofdev);
    452 	ofdev.handle = handle;
    453 	if (!strcmp(b.buf, "block")) {
    454 		ofdev.type = OFDEV_DISK;
    455 		ofdev.bsize = DEV_BSIZE;
    456 		/* First try to find a disklabel without MBR partitions */
    457 #ifdef NOTDEF_DEBUG
    458 		printf("devopen: trying to read disklabel\n");
    459 #endif
    460 		if (strategy(&ofdev, F_READ,
    461 			     LABELSECTOR, DEV_BSIZE, b.buf, &read) != 0
    462 		    || read != DEV_BSIZE
    463 		    || (errmsg = getdisklabel(b.buf, &label))) {
    464 			if (errmsg) printf("devopen: getdisklabel returned %s\n", errmsg);
    465 			/* Else try MBR partitions */
    466 			errmsg = search_label(&ofdev, 0, b.buf, &label, 0);
    467 			if (errmsg) {
    468 				printf("devopen: search_label returned %s\n", errmsg);
    469 				error = ERDLAB;
    470 			}
    471 			if (error && error != ERDLAB)
    472 				goto bad;
    473 		}
    474 
    475 		if (error == ERDLAB) {
    476 			if (partition)
    477 				/* User specified a parititon, but there is none */
    478 				goto bad;
    479 			/* No, label, just use complete disk */
    480 			ofdev.partoff = 0;
    481 		} else {
    482 			part = partition ? partition - 'a' : 0;
    483 			ofdev.partoff = label.d_partitions[part].p_offset;
    484 #ifdef NOTDEF_DEBUG
    485 			printf("devopen: setting partition %d offset %x\n",
    486 			       part, ofdev.partoff);
    487 #endif
    488 			if (label.d_partitions[part].p_fstype == FS_RAID) {
    489 				ofdev.partoff += RF_PROTECTED_SECTORS;
    490 #ifdef NOTDEF_DEBUG
    491 				printf("devopen: found RAID partition, "
    492 				    "adjusting offset to %x\n", ofdev.partoff);
    493 #endif
    494 			}
    495 		}
    496 
    497 		of->f_dev = ofdevsw;
    498 		of->f_devdata = &ofdev;
    499 #ifdef SPARC_BOOT_UFS
    500 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
    501 #endif
    502 #ifdef SPARC_BOOT_HSFS
    503 		bcopy(&file_system_cd9660, &file_system[nfsys++],
    504 		    sizeof file_system[0]);
    505 #endif
    506 #ifdef NOTDEF_DEBUG
    507 		printf("devopen: return 0\n");
    508 #endif
    509 		return 0;
    510 	}
    511 #ifdef NETBOOT
    512 	if (!strcmp(b.buf, "network")) {
    513 		ofdev.type = OFDEV_NET;
    514 		of->f_dev = ofdevsw;
    515 		of->f_devdata = &ofdev;
    516 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
    517 		nfsys = 1;
    518 		if (error = net_open(&ofdev))
    519 			goto bad;
    520 		return 0;
    521 	}
    522 #endif
    523 	error = EFTYPE;
    524 bad:
    525 #ifdef NOTDEF_DEBUG
    526 	printf("devopen: error %d, cannot open device\n", error);
    527 #endif
    528 	prom_close(handle);
    529 	ofdev.handle = -1;
    530 	return error;
    531 }
    532