Home | History | Annotate | Line # | Download | only in ofwboot
ofdev.c revision 1.1
      1 /*	$NetBSD: ofdev.c,v 1.1 2000/08/20 14:58:41 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 /*
     56  * This is ugly.  A path on a sparc machine is something like this:
     57  *
     58  *	[device] [-<options] [path] [-options] [otherstuff] [-<more options]
     59  *
     60  */
     61 
     62 static char *
     63 filename(str, ppart)
     64 	char *str;
     65 	char *ppart;
     66 {
     67 	char *cp, *lp;
     68 	char savec;
     69 	int dhandle;
     70 	char devtype[16];
     71 
     72 	lp = str;
     73 	devtype[0] = 0;
     74 	*ppart = 0;
     75 	for (cp = str; *cp; lp = cp) {
     76 		/* For each component of the path name... */
     77 		while (*++cp && *cp != '/');
     78 		savec = *cp;
     79 		*cp = 0;
     80 		/* ...look whether there is a device with this name */
     81 		dhandle = OF_finddevice(str);
     82 #ifdef NOTDEF_DEBUG
     83 		printf("filename: OF_finddevice(%s) sez %x\n",
     84 		       str, dhandle);
     85 #endif
     86 		*cp = savec;
     87 		if (dhandle == -1) {
     88 			/* if not, lp is the delimiter between device and path */
     89 			/* if the last component was a block device... */
     90 			if (!strcmp(devtype, "block")) {
     91 				/* search for arguments */
     92 #ifdef NOTDEF_DEBUG
     93 				printf("filename: hunting for arguments in %s\n", str);
     94 #endif
     95 				for (cp = lp;
     96 				     --cp >= str && *cp != '/' && *cp != '-';);
     97 				if (cp >= str && *cp == '-') {
     98 					/* found arguments, make firmware ignore them */
     99 					*cp = 0;
    100 					for (cp = lp; *--cp && *cp != ',';);
    101 					if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS)
    102 						*ppart = *cp;
    103 				}
    104 			}
    105 #ifdef NOTDEF_DEBUG
    106 			printf("filename: found %s\n",lp);
    107 #endif
    108 			return lp;
    109 		} else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
    110 			devtype[0] = 0;
    111 	}
    112 #ifdef NOTDEF_DEBUG
    113 	printf("filename: not found\n",lp);
    114 #endif
    115 	return 0;
    116 }
    117 
    118 static int
    119 strategy(devdata, rw, blk, size, buf, rsize)
    120 	void *devdata;
    121 	int rw;
    122 	daddr_t blk;
    123 	size_t size;
    124 	void *buf;
    125 	size_t *rsize;
    126 {
    127 	struct of_dev *dev = devdata;
    128 	u_quad_t pos;
    129 	int n;
    130 
    131 	if (rw != F_READ)
    132 		return EPERM;
    133 	if (dev->type != OFDEV_DISK)
    134 		panic("strategy");
    135 
    136 #ifdef NON_DEBUG
    137 	printf("strategy: block %lx, partition offset %lx, blksz %lx\n",
    138 	       (long)blk, (long)dev->partoff, (long)dev->bsize);
    139 	printf("strategy: seek position should be: %lx\n",
    140 	       (long)((blk + dev->partoff) * dev->bsize));
    141 #endif
    142 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
    143 
    144 	for (;;) {
    145 #ifdef NON_DEBUG
    146 		printf("strategy: seeking to %lx\n", (long)pos);
    147 #endif
    148 		if (OF_seek(dev->handle, pos) < 0)
    149 			break;
    150 #ifdef NON_DEBUG
    151 		printf("strategy: reading %lx at %p\n", (long)size, buf);
    152 #endif
    153 		n = OF_read(dev->handle, buf, size);
    154 		if (n == -2)
    155 			continue;
    156 		if (n < 0)
    157 			break;
    158 		*rsize = n;
    159 		return 0;
    160 	}
    161 	return EIO;
    162 }
    163 
    164 static int
    165 devclose(of)
    166 	struct open_file *of;
    167 {
    168 	struct of_dev *op = of->f_devdata;
    169 
    170 #ifdef NETBOOT
    171 	if (op->type == OFDEV_NET)
    172 		net_close(op);
    173 #endif
    174 	OF_close(op->handle);
    175 	op->handle = -1;
    176 }
    177 
    178 static struct devsw devsw[1] = {
    179 	"OpenFirmware",
    180 	strategy,
    181 	(int (*)__P((struct open_file *, ...)))nodev,
    182 	devclose,
    183 	noioctl
    184 };
    185 int ndevs = sizeof devsw / sizeof devsw[0];
    186 
    187 #ifdef SPARC_BOOT_UFS
    188 static struct fs_ops file_system_ufs = {
    189 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
    190 };
    191 #endif
    192 #ifdef SPARC_BOOT_HSFS
    193 static struct fs_ops file_system_cd9660 = {
    194 	cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
    195 	    cd9660_stat
    196 };
    197 #endif
    198 #ifdef NETBOOT
    199 static struct fs_ops file_system_nfs = {
    200 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
    201 };
    202 #endif
    203 
    204 struct fs_ops file_system[3];
    205 int nfsys;
    206 
    207 static struct of_dev ofdev = {
    208 	-1,
    209 };
    210 
    211 char opened_name[256];
    212 int floppyboot;
    213 
    214 static u_long
    215 get_long(p)
    216 	const void *p;
    217 {
    218 	const unsigned char *cp = p;
    219 
    220 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
    221 }
    222 /************************************************************************
    223  *
    224  * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c
    225  * and then substantially rewritten by Gordon W. Ross
    226  *
    227  ************************************************************************/
    228 
    229 /* What partition types to assume for Sun disklabels: */
    230 static u_char
    231 sun_fstypes[8] = {
    232 	FS_BSDFFS,	/* a */
    233 	FS_SWAP,	/* b */
    234 	FS_OTHER,	/* c - whole disk */
    235 	FS_BSDFFS,	/* d */
    236 	FS_BSDFFS,	/* e */
    237 	FS_BSDFFS,	/* f */
    238 	FS_BSDFFS,	/* g */
    239 	FS_BSDFFS,	/* h */
    240 };
    241 
    242 /*
    243  * Given a SunOS disk label, set lp to a BSD disk label.
    244  * Returns NULL on success, else an error string.
    245  *
    246  * The BSD label is cleared out before this is called.
    247  */
    248 static char *
    249 disklabel_sun_to_bsd(cp, lp)
    250 	char *cp;
    251 	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(devp, off, buf, lp, off0)
    331 	struct of_dev *devp;
    332 	u_long off;
    333 	char *buf;
    334 	struct disklabel *lp;
    335 	u_long off0;
    336 {
    337 	size_t read;
    338 	struct mbr_partition *p;
    339 	int i;
    340 	u_long poff;
    341 	static int recursion;
    342 
    343 	struct disklabel *dlp;
    344 	struct sun_disklabel *slp;
    345 	int error;
    346 
    347 	/* minimal requirements for archtypal disk label */
    348 	if (lp->d_secperunit == 0)
    349 		lp->d_secperunit = 0x1fffffff;
    350 	lp->d_npartitions = 1;
    351 	if (lp->d_partitions[0].p_size == 0)
    352 		lp->d_partitions[0].p_size = 0x1fffffff;
    353 	lp->d_partitions[0].p_offset = 0;
    354 
    355 	if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read)
    356 	    || read != DEV_BSIZE)
    357 		return ("Cannot read label");
    358 	/* Check for a NetBSD disk label. */
    359 	dlp = (struct disklabel *) (buf + LABELOFFSET);
    360 	if (dlp->d_magic == DISKMAGIC) {
    361 		if (dkcksum(dlp))
    362 			return ("NetBSD disk label corrupted");
    363 		*lp = *dlp;
    364 #ifdef NOTDEF_DEBUG
    365 		printf("search_label: found NetBSD label\n");
    366 #endif
    367 		return (NULL);
    368 	}
    369 
    370 	/* Check for a Sun disk label (for PROM compatibility). */
    371 	slp = (struct sun_disklabel *) buf;
    372 	if (slp->sl_magic == SUN_DKMAGIC)
    373 		return (disklabel_sun_to_bsd(buf, lp));
    374 
    375 
    376 	bzero(buf, sizeof(buf));
    377 	return ("no disk label");
    378 }
    379 
    380 int
    381 devopen(of, name, file)
    382 	struct open_file *of;
    383 	const char *name;
    384 	char **file;
    385 {
    386 	char *cp;
    387 	char partition;
    388 	char fname[256];
    389 	char buf[DEV_BSIZE];
    390 	struct disklabel label;
    391 	int handle, part;
    392 	size_t read;
    393 	char *errmsg = NULL;
    394 	int error = 0;
    395 
    396 	if (ofdev.handle != -1)
    397 		panic("devopen");
    398 	if (of->f_flags != F_READ)
    399 		return EPERM;
    400 #ifdef NOTDEF_DEBUG
    401 	printf("devopen: you want %s\n", name);
    402 #endif
    403 	strcpy(fname, name);
    404 	cp = filename(fname, &partition);
    405 	if (cp) {
    406 		strcpy(buf, cp);
    407 		*cp = 0;
    408 	}
    409 	if (!cp || !*buf)
    410 		strcpy(buf, DEFAULT_KERNEL);
    411 	if (!*fname)
    412 		strcpy(fname, bootdev);
    413 	strcpy(opened_name, fname);
    414 	if (partition) {
    415 		cp = opened_name + strlen(opened_name);
    416 		*cp++ = ':';
    417 		*cp++ = partition;
    418 		*cp = 0;
    419 	}
    420 	if (*buf != '/')
    421 		strcat(opened_name, "/");
    422 	strcat(opened_name, buf);
    423 	*file = opened_name + strlen(fname) + 1;
    424 #ifdef NOTDEF_DEBUG
    425 	printf("devopen: trying %s\n", fname);
    426 #endif
    427 	if ((handle = OF_finddevice(fname)) == -1)
    428 		return ENOENT;
    429 #ifdef NOTDEF_DEBUG
    430 	printf("devopen: found %s\n", fname);
    431 #endif
    432 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
    433 		return ENXIO;
    434 #ifdef NOTDEF_DEBUG
    435 	printf("devopen: %s is called %s\n", fname, buf);
    436 #endif
    437 	floppyboot = !strcmp(buf, "floppy");
    438 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
    439 		return ENXIO;
    440 #ifdef NOTDEF_DEBUG
    441 	printf("devopen: %s is a %s device\n", fname, buf);
    442 #endif
    443 #ifdef NOTDEF_DEBUG
    444 	printf("devopen: opening %s\n", fname);
    445 #endif
    446 	if ((handle = OF_open(fname)) == -1) {
    447 #ifdef NOTDEF_DEBUG
    448 		printf("devopen: open of %s failed\n", fname);
    449 #endif
    450 		return ENXIO;
    451 	}
    452 #ifdef NOTDEF_DEBUG
    453 	printf("devopen: %s is now open\n", fname);
    454 #endif
    455 	bzero(&ofdev, sizeof ofdev);
    456 	ofdev.handle = handle;
    457 	if (!strcmp(buf, "block")) {
    458 		ofdev.type = OFDEV_DISK;
    459 		ofdev.bsize = DEV_BSIZE;
    460 		/* First try to find a disklabel without MBR partitions */
    461 #ifdef NOTDEF_DEBUG
    462 		printf("devopen: trying to read disklabel\n");
    463 #endif
    464 		if (strategy(&ofdev, F_READ,
    465 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
    466 		    || read != DEV_BSIZE
    467 		    || (errmsg = getdisklabel(buf, &label))) {
    468 			if (errmsg) printf("devopen: getdisklabel sez %s\n", errmsg);
    469 			/* Else try MBR partitions */
    470 			errmsg = search_label(&ofdev, 0, buf, &label, 0);
    471 			if (errmsg) {
    472 				printf("devopen: search_label sez %s\n", errmsg);
    473 				error = ERDLAB;
    474 			}
    475 			if (error && error != ERDLAB)
    476 				goto bad;
    477 		}
    478 
    479 		if (error == ERDLAB) {
    480 			if (partition)
    481 				/* User specified a parititon, but there is none */
    482 				goto bad;
    483 			/* No, label, just use complete disk */
    484 			ofdev.partoff = 0;
    485 		} else {
    486 			part = partition ? partition - 'a' : 0;
    487 			ofdev.partoff = label.d_partitions[part].p_offset;
    488 #ifdef NOTDEF_DEBUG
    489 			printf("devopen: setting partition %d offset %x\n",
    490 			       part, ofdev.partoff);
    491 #endif
    492 		}
    493 
    494 		of->f_dev = devsw;
    495 		of->f_devdata = &ofdev;
    496 #ifdef SPARC_BOOT_UFS
    497 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
    498 #endif
    499 #ifdef SPARC_BOOT_HSFS
    500 		bcopy(&file_system_cd9660, &file_system[nfsys++],
    501 		    sizeof file_system[0]);
    502 #endif
    503 #ifdef NOTDEF_DEBUG
    504 		printf("devopen: return 0\n");
    505 #endif
    506 		return 0;
    507 	}
    508 #ifdef NETBOOT
    509 	if (!strcmp(buf, "network")) {
    510 		ofdev.type = OFDEV_NET;
    511 		of->f_dev = devsw;
    512 		of->f_devdata = &ofdev;
    513 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
    514 		nfsys = 1;
    515 		if (error = net_open(&ofdev))
    516 			goto bad;
    517 		return 0;
    518 	}
    519 #endif
    520 	error = EFTYPE;
    521 bad:
    522 #ifdef NOTDEF_DEBUG
    523 	printf("devopen: error %d, cannot open device\n", error);
    524 #endif
    525 	OF_close(handle);
    526 	ofdev.handle = -1;
    527 	return error;
    528 }
    529