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