Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.2
      1 /*	$NetBSD: ofdev.c,v 1.2 2002/07/29 14:34:12 mrg 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 "ofdev.h"
     52 
     53 extern char bootdev[];
     54 
     55 #define RF_PROTECTED_SECTORS 64		/* XXX */
     56 
     57 /*
     58  * This is ugly.  A path on a sparc machine is something like this:
     59  *
     60  *	[device] [-<options] [path] [-options] [otherstuff] [-<more options]
     61  *
     62  */
     63 
     64 static char *
     65 filename(str, ppart)
     66 	char *str;
     67 	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 = OF_finddevice(str);
     84 #ifdef NOTDEF_DEBUG
     85 		printf("filename: OF_finddevice(%s) sez %x\n",
     86 		       str, dhandle);
     87 #endif
     88 		*cp = savec;
     89 		if (dhandle == -1) {
     90 			/* if not, lp is the delimiter between device and path */
     91 			/* if the last component was a block device... */
     92 			if (!strcmp(devtype, "block")) {
     93 				/* search for arguments */
     94 #ifdef NOTDEF_DEBUG
     95 				printf("filename: hunting for arguments in %s\n", str);
     96 #endif
     97 				for (cp = lp;
     98 				     --cp >= str && *cp != '/' && *cp != '-';);
     99 				if (cp >= str && *cp == '-') {
    100 					/* found arguments, make firmware ignore them */
    101 					*cp = 0;
    102 					for (cp = lp; *--cp && *cp != ',';);
    103 					if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS)
    104 						*ppart = *cp;
    105 				}
    106 			}
    107 #ifdef NOTDEF_DEBUG
    108 			printf("filename: found %s\n",lp);
    109 #endif
    110 			return lp;
    111 		} else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
    112 			devtype[0] = 0;
    113 	}
    114 #ifdef NOTDEF_DEBUG
    115 	printf("filename: not found\n",lp);
    116 #endif
    117 	return 0;
    118 }
    119 
    120 static int
    121 strategy(devdata, rw, blk, size, buf, rsize)
    122 	void *devdata;
    123 	int rw;
    124 	daddr_t blk;
    125 	size_t size;
    126 	void *buf;
    127 	size_t *rsize;
    128 {
    129 	struct of_dev *dev = devdata;
    130 	u_quad_t pos;
    131 	int n;
    132 
    133 	if (rw != F_READ)
    134 		return EPERM;
    135 	if (dev->type != OFDEV_DISK)
    136 		panic("strategy");
    137 
    138 #ifdef NON_DEBUG
    139 	printf("strategy: block %lx, partition offset %lx, blksz %lx\n",
    140 	       (long)blk, (long)dev->partoff, (long)dev->bsize);
    141 	printf("strategy: seek position should be: %lx\n",
    142 	       (long)((blk + dev->partoff) * dev->bsize));
    143 #endif
    144 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
    145 
    146 	for (;;) {
    147 #ifdef NON_DEBUG
    148 		printf("strategy: seeking to %lx\n", (long)pos);
    149 #endif
    150 		if (OF_seek(dev->handle, pos) < 0)
    151 			break;
    152 #ifdef NON_DEBUG
    153 		printf("strategy: reading %lx at %p\n", (long)size, buf);
    154 #endif
    155 		n = OF_read(dev->handle, buf, size);
    156 		if (n == -2)
    157 			continue;
    158 		if (n < 0)
    159 			break;
    160 		*rsize = n;
    161 		return 0;
    162 	}
    163 	return EIO;
    164 }
    165 
    166 static int
    167 devclose(of)
    168 	struct open_file *of;
    169 {
    170 	struct of_dev *op = of->f_devdata;
    171 
    172 #ifdef NETBOOT
    173 	if (op->type == OFDEV_NET)
    174 		net_close(op);
    175 #endif
    176 	OF_close(op->handle);
    177 	op->handle = -1;
    178 }
    179 
    180 static struct devsw devsw[1] = {
    181 	"OpenFirmware",
    182 	strategy,
    183 	(int (*)__P((struct open_file *, ...)))nodev,
    184 	devclose,
    185 	noioctl
    186 };
    187 int ndevs = sizeof devsw / sizeof devsw[0];
    188 
    189 #ifdef SPARC_BOOT_UFS
    190 static struct fs_ops file_system_ufs = {
    191 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
    192 };
    193 #endif
    194 #ifdef SPARC_BOOT_HSFS
    195 static struct fs_ops file_system_cd9660 = {
    196 	cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
    197 	    cd9660_stat
    198 };
    199 #endif
    200 #ifdef NETBOOT
    201 static struct fs_ops file_system_nfs = {
    202 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
    203 };
    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(p)
    218 	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(cp, lp)
    252 	char *cp;
    253 	struct disklabel *lp;
    254 {
    255 	struct sun_disklabel *sl;
    256 	struct partition *npp;
    257 	struct sun_dkpart *spp;
    258 	int i, secpercyl;
    259 	u_short cksum, *sp1, *sp2;
    260 
    261 	sl = (struct sun_disklabel *)cp;
    262 
    263 	/* Verify the XOR check. */
    264 	sp1 = (u_short *)sl;
    265 	sp2 = (u_short *)(sl + 1);
    266 	cksum = 0;
    267 	while (sp1 < sp2)
    268 		cksum ^= *sp1++;
    269 	if (cksum != 0)
    270 		return("SunOS disk label, bad checksum");
    271 
    272 	/* Format conversion. */
    273 	lp->d_magic = DISKMAGIC;
    274 	lp->d_magic2 = DISKMAGIC;
    275 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
    276 
    277 	lp->d_secsize = 512;
    278 	lp->d_nsectors   = sl->sl_nsectors;
    279 	lp->d_ntracks    = sl->sl_ntracks;
    280 	lp->d_ncylinders = sl->sl_ncylinders;
    281 
    282 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
    283 	lp->d_secpercyl  = secpercyl;
    284 	lp->d_secperunit = secpercyl * sl->sl_ncylinders;
    285 
    286 	lp->d_sparespercyl = sl->sl_sparespercyl;
    287 	lp->d_acylinders   = sl->sl_acylinders;
    288 	lp->d_rpm          = sl->sl_rpm;
    289 	lp->d_interleave   = sl->sl_interleave;
    290 
    291 	lp->d_npartitions = 8;
    292 	/* These are as defined in <ufs/ffs/fs.h> */
    293 	lp->d_bbsize = 8192;	/* XXX */
    294 	lp->d_sbsize = 8192;	/* XXX */
    295 
    296 	for (i = 0; i < 8; i++) {
    297 		spp = &sl->sl_part[i];
    298 		npp = &lp->d_partitions[i];
    299 		npp->p_offset = spp->sdkp_cyloffset * secpercyl;
    300 		npp->p_size = spp->sdkp_nsectors;
    301 #ifdef NOTDEF_DEBUG
    302 		printf("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size);
    303 #endif
    304 		if (npp->p_size == 0) {
    305 			npp->p_fstype = FS_UNUSED;
    306 		} else {
    307 			npp->p_fstype = sun_fstypes[i];
    308 			if (npp->p_fstype == FS_BSDFFS) {
    309 				/*
    310 				 * The sun label does not store the FFS fields,
    311 				 * so just set them with default values here.
    312 				 */
    313 				npp->p_fsize = 1024;
    314 				npp->p_frag = 8;
    315 				npp->p_cpg = 16;
    316 			}
    317 		}
    318 	}
    319 
    320 	lp->d_checksum = 0;
    321 	lp->d_checksum = dkcksum(lp);
    322 #ifdef NOTDEF_DEBUG
    323 	printf("disklabel_sun_to_bsd: success!\n");
    324 #endif
    325 	return (NULL);
    326 }
    327 
    328 /*
    329  * Find a valid disklabel.
    330  */
    331 static char *
    332 search_label(devp, off, buf, lp, off0)
    333 	struct of_dev *devp;
    334 	u_long off;
    335 	char *buf;
    336 	struct disklabel *lp;
    337 	u_long off0;
    338 {
    339 	size_t read;
    340 	struct mbr_partition *p;
    341 	int i;
    342 	u_long poff;
    343 	static int recursion;
    344 
    345 	struct disklabel *dlp;
    346 	struct sun_disklabel *slp;
    347 	int error;
    348 
    349 	/* minimal requirements for archtypal disk label */
    350 	if (lp->d_secperunit == 0)
    351 		lp->d_secperunit = 0x1fffffff;
    352 	lp->d_npartitions = 1;
    353 	if (lp->d_partitions[0].p_size == 0)
    354 		lp->d_partitions[0].p_size = 0x1fffffff;
    355 	lp->d_partitions[0].p_offset = 0;
    356 
    357 	if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read)
    358 	    || read != DEV_BSIZE)
    359 		return ("Cannot read label");
    360 	/* Check for a NetBSD disk label. */
    361 	dlp = (struct disklabel *) (buf + LABELOFFSET);
    362 	if (dlp->d_magic == DISKMAGIC) {
    363 		if (dkcksum(dlp))
    364 			return ("NetBSD disk label corrupted");
    365 		*lp = *dlp;
    366 #ifdef NOTDEF_DEBUG
    367 		printf("search_label: found NetBSD label\n");
    368 #endif
    369 		return (NULL);
    370 	}
    371 
    372 	/* Check for a Sun disk label (for PROM compatibility). */
    373 	slp = (struct sun_disklabel *) buf;
    374 	if (slp->sl_magic == SUN_DKMAGIC)
    375 		return (disklabel_sun_to_bsd(buf, lp));
    376 
    377 
    378 	bzero(buf, sizeof(buf));
    379 	return ("no disk label");
    380 }
    381 
    382 int
    383 devopen(of, name, file)
    384 	struct open_file *of;
    385 	const char *name;
    386 	char **file;
    387 {
    388 	char *cp;
    389 	char partition;
    390 	char fname[256];
    391 	char buf[DEV_BSIZE];
    392 	struct disklabel label;
    393 	int handle, part;
    394 	size_t read;
    395 	char *errmsg = NULL;
    396 	int error = 0;
    397 
    398 	if (ofdev.handle != -1)
    399 		panic("devopen");
    400 	if (of->f_flags != F_READ)
    401 		return EPERM;
    402 #ifdef NOTDEF_DEBUG
    403 	printf("devopen: you want %s\n", name);
    404 #endif
    405 	strcpy(fname, name);
    406 	cp = filename(fname, &partition);
    407 	if (cp) {
    408 		strcpy(buf, cp);
    409 		*cp = 0;
    410 	}
    411 	if (!cp || !*buf)
    412 		strcpy(buf, DEFAULT_KERNEL);
    413 	if (!*fname)
    414 		strcpy(fname, bootdev);
    415 	strcpy(opened_name, fname);
    416 	if (partition) {
    417 		cp = opened_name + strlen(opened_name);
    418 		*cp++ = ':';
    419 		*cp++ = partition;
    420 		*cp = 0;
    421 	}
    422 	if (*buf != '/')
    423 		strcat(opened_name, "/");
    424 	strcat(opened_name, buf);
    425 	*file = opened_name + strlen(fname) + 1;
    426 #ifdef NOTDEF_DEBUG
    427 	printf("devopen: trying %s\n", fname);
    428 #endif
    429 	if ((handle = OF_finddevice(fname)) == -1)
    430 		return ENOENT;
    431 #ifdef NOTDEF_DEBUG
    432 	printf("devopen: found %s\n", fname);
    433 #endif
    434 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    435 		return ENXIO;
    436 #ifdef NOTDEF_DEBUG
    437 	printf("devopen: %s is called %s\n", fname, buf);
    438 #endif
    439 	floppyboot = !strcmp(buf, "floppy");
    440 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    441 		return ENXIO;
    442 #ifdef NOTDEF_DEBUG
    443 	printf("devopen: %s is a %s device\n", fname, buf);
    444 #endif
    445 #ifdef NOTDEF_DEBUG
    446 	printf("devopen: opening %s\n", fname);
    447 #endif
    448 	if ((handle = OF_open(fname)) == -1) {
    449 #ifdef NOTDEF_DEBUG
    450 		printf("devopen: open of %s failed\n", fname);
    451 #endif
    452 		return ENXIO;
    453 	}
    454 #ifdef NOTDEF_DEBUG
    455 	printf("devopen: %s is now open\n", fname);
    456 #endif
    457 	bzero(&ofdev, sizeof ofdev);
    458 	ofdev.handle = handle;
    459 	if (!strcmp(buf, "block")) {
    460 		ofdev.type = OFDEV_DISK;
    461 		ofdev.bsize = DEV_BSIZE;
    462 		/* First try to find a disklabel without MBR partitions */
    463 #ifdef NOTDEF_DEBUG
    464 		printf("devopen: trying to read disklabel\n");
    465 #endif
    466 		if (strategy(&ofdev, F_READ,
    467 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
    468 		    || read != DEV_BSIZE
    469 		    || (errmsg = getdisklabel(buf, &label))) {
    470 			if (errmsg) printf("devopen: getdisklabel sez %s\n", errmsg);
    471 			/* Else try MBR partitions */
    472 			errmsg = search_label(&ofdev, 0, buf, &label, 0);
    473 			if (errmsg) {
    474 				printf("devopen: search_label sez %s\n", errmsg);
    475 				error = ERDLAB;
    476 			}
    477 			if (error && error != ERDLAB)
    478 				goto bad;
    479 		}
    480 
    481 		if (error == ERDLAB) {
    482 			if (partition)
    483 				/* User specified a parititon, but there is none */
    484 				goto bad;
    485 			/* No, label, just use complete disk */
    486 			ofdev.partoff = 0;
    487 		} else {
    488 			part = partition ? partition - 'a' : 0;
    489 			ofdev.partoff = label.d_partitions[part].p_offset;
    490 #ifdef NOTDEF_DEBUG
    491 			printf("devopen: setting partition %d offset %x\n",
    492 			       part, ofdev.partoff);
    493 #endif
    494 			if (label.d_partitions[part].p_fstype == FS_RAID) {
    495 				ofdev.partoff += RF_PROTECTED_SECTORS;
    496 #ifdef NOTDEF_DEBUG
    497 				printf("devopen: found RAID partition, "
    498 				    "adjusting offset to %x", ofdev.partoff);
    499 #endif
    500 			}
    501 		}
    502 
    503 		of->f_dev = devsw;
    504 		of->f_devdata = &ofdev;
    505 #ifdef SPARC_BOOT_UFS
    506 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
    507 #endif
    508 #ifdef SPARC_BOOT_HSFS
    509 		bcopy(&file_system_cd9660, &file_system[nfsys++],
    510 		    sizeof file_system[0]);
    511 #endif
    512 #ifdef NOTDEF_DEBUG
    513 		printf("devopen: return 0\n");
    514 #endif
    515 		return 0;
    516 	}
    517 #ifdef NETBOOT
    518 	if (!strcmp(buf, "network")) {
    519 		ofdev.type = OFDEV_NET;
    520 		of->f_dev = devsw;
    521 		of->f_devdata = &ofdev;
    522 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
    523 		nfsys = 1;
    524 		if (error = net_open(&ofdev))
    525 			goto bad;
    526 		return 0;
    527 	}
    528 #endif
    529 	error = EFTYPE;
    530 bad:
    531 #ifdef NOTDEF_DEBUG
    532 	printf("devopen: error %d, cannot open device\n", error);
    533 #endif
    534 	OF_close(handle);
    535 	ofdev.handle = -1;
    536 	return error;
    537 }
    538