xref: /openbsd/sys/arch/sparc64/stand/ofwboot/ofdev.c (revision a6445c1d)
1 /*	$OpenBSD: ofdev.c,v 1.22 2014/06/08 16:01:00 jsg Exp $	*/
2 /*	$NetBSD: ofdev.c,v 1.1 2000/08/20 14:58:41 mrg Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
6  * Copyright (C) 1995, 1996 TooLs GmbH.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Device I/O routines using Open Firmware
36  */
37 #include <sys/param.h>
38 #include <sys/disklabel.h>
39 #ifdef NETBOOT
40 #include <netinet/in.h>
41 #endif
42 
43 #include <lib/libsa/stand.h>
44 #include <lib/libsa/ufs.h>
45 #include <lib/libsa/cd9660.h>
46 #ifdef NETBOOT
47 #include <lib/libsa/nfs.h>
48 #endif
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(char *str, char *ppart)
64 {
65 	char *cp, *lp;
66 	char savec;
67 	int dhandle;
68 	char devtype[16];
69 
70 	lp = str;
71 	devtype[0] = 0;
72 	*ppart = 0;
73 	for (cp = str; *cp; lp = cp) {
74 		/* For each component of the path name... */
75 		while (*++cp && *cp != '/');
76 		savec = *cp;
77 		*cp = 0;
78 		/* ...look whether there is a device with this name */
79 		dhandle = OF_finddevice(str);
80 		DNPRINTF(BOOT_D_OFDEV, "filename: OF_finddevice(%s) says %x\n",
81 		    str, dhandle);
82 		*cp = savec;
83 		if (dhandle == -1) {
84 			/* if not, lp is the delimiter between device and path */
85 			/* if the last component was a block device... */
86 			if (!strcmp(devtype, "block")) {
87 				/* search for arguments */
88 				DNPRINTF(BOOT_D_OFDEV, "filename: hunting for "
89 				    "arguments in %s\n", str);
90 				for (cp = lp;
91 				     --cp >= str && *cp != '/' && *cp != '-';);
92 				if (cp >= str && *cp == '-') {
93 					/* found arguments, make firmware ignore them */
94 					*cp = 0;
95 					for (cp = lp; *--cp && *cp != ',';);
96 					if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS)
97 						*ppart = *cp;
98 				}
99 			}
100 			DNPRINTF(BOOT_D_OFDEV, "filename: found %s\n", lp);
101 			return lp;
102 		} else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
103 			devtype[0] = 0;
104 	}
105 	DNPRINTF(BOOT_D_OFDEV, "filename: not found\n", lp);
106 	return 0;
107 }
108 
109 static int
110 strategy(void *devdata, int rw, daddr32_t blk, size_t size, void *buf,
111     size_t *rsize)
112 {
113 	struct of_dev *dev = devdata;
114 	u_quad_t pos;
115 	int n;
116 
117 	if (rw != F_READ)
118 		return EPERM;
119 	if (dev->type != OFDEV_DISK)
120 		panic("strategy");
121 
122 	DNPRINTF(BOOT_D_OFDEV, "strategy: block %lx, partition offset %lx, "
123 	    "blksz %lx\n", (long)blk, (long)dev->partoff, (long)dev->bsize);
124 	DNPRINTF(BOOT_D_OFDEV, "strategy: seek position should be: %lx\n",
125 	    (long)((blk + dev->partoff) * dev->bsize));
126 	pos = (u_quad_t)(blk + dev->partoff) * dev->bsize;
127 
128 	for (;;) {
129 		DNPRINTF(BOOT_D_OFDEV, "strategy: seeking to %lx\n", (long)pos);
130 		if (OF_seek(dev->handle, pos) < 0)
131 			break;
132 		DNPRINTF(BOOT_D_OFDEV, "strategy: reading %lx at %p\n",
133 		    (long)size, buf);
134 		n = OF_read(dev->handle, buf, size);
135 		if (n == -2)
136 			continue;
137 		if (n < 0)
138 			break;
139 		*rsize = n;
140 		return 0;
141 	}
142 	return EIO;
143 }
144 
145 static int
146 devclose(struct open_file *of)
147 {
148 	struct of_dev *op = of->f_devdata;
149 
150 #ifdef NETBOOT
151 	if (op->type == OFDEV_NET)
152 		net_close(op);
153 #endif
154 	OF_close(op->handle);
155 	op->handle = -1;
156 }
157 
158 struct devsw devsw[1] = {
159 	"OpenFirmware",
160 	strategy,
161 	(int (*)(struct open_file *, ...))nodev,
162 	devclose,
163 	noioctl
164 };
165 int ndevs = sizeof devsw / sizeof devsw[0];
166 
167 #ifdef SPARC_BOOT_UFS
168 static struct fs_ops file_system_ufs = {
169 	ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat
170 };
171 #endif
172 #ifdef SPARC_BOOT_HSFS
173 static struct fs_ops file_system_cd9660 = {
174 	cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
175 	    cd9660_stat
176 };
177 #endif
178 #ifdef NETBOOT
179 static struct fs_ops file_system_nfs = {
180 	nfs_open, nfs_close, nfs_read, nfs_write, nfs_seek, nfs_stat
181 };
182 #endif
183 
184 struct fs_ops file_system[3];
185 int nfsys;
186 
187 static struct of_dev ofdev = {
188 	-1,
189 };
190 
191 char opened_name[256];
192 
193 static u_long
194 get_long(const void *p)
195 {
196 	const unsigned char *cp = p;
197 
198 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
199 }
200 
201 /************************************************************************
202  *
203  * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c
204  * and then substantially rewritten by Gordon W. Ross
205  *
206  ************************************************************************/
207 
208 /* What partition types to assume for Sun disklabels: */
209 static u_char
210 sun_fstypes[8] = {
211 	FS_BSDFFS,	/* a */
212 	FS_SWAP,	/* b */
213 	FS_OTHER,	/* c - whole disk */
214 	FS_BSDFFS,	/* d */
215 	FS_BSDFFS,	/* e */
216 	FS_BSDFFS,	/* f */
217 	FS_BSDFFS,	/* g */
218 	FS_BSDFFS,	/* h */
219 };
220 
221 /*
222  * Given a struct sun_disklabel, assume it has an extended partition
223  * table and compute the correct value for sl_xpsum.
224  */
225 static __inline u_int
226 sun_extended_sum(struct sun_disklabel *sl, void *end)
227 {
228 	u_int sum, *xp, *ep;
229 
230 	xp = (u_int *)&sl->sl_xpmag;
231 	ep = (u_int *)end;
232 
233 	sum = 0;
234 	for (; xp < ep; xp++)
235 		sum += *xp;
236 	return (sum);
237 }
238 
239 /*
240  * Given a SunOS disk label, set lp to a BSD disk label.
241  * The BSD label is cleared out before this is called.
242  */
243 static int
244 disklabel_sun_to_bsd(struct sun_disklabel *sl, struct disklabel *lp)
245 {
246 	struct sun_preamble *preamble = (struct sun_preamble *)sl;
247 	struct sun_partinfo *ppp;
248 	struct sun_dkpart *spp;
249 	struct partition *npp;
250 	u_short cksum = 0, *sp1, *sp2;
251 	int i, secpercyl;
252 
253 	/* Verify the XOR check. */
254 	sp1 = (u_short *)sl;
255 	sp2 = (u_short *)(sl + 1);
256 	while (sp1 < sp2)
257 		cksum ^= *sp1++;
258 	if (cksum != 0)
259 		return (EINVAL);	/* SunOS disk label, bad checksum */
260 
261 	/* Format conversion. */
262 	lp->d_magic = DISKMAGIC;
263 	lp->d_magic2 = DISKMAGIC;
264 	lp->d_flags = D_VENDOR;
265 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
266 
267 	lp->d_secsize = DEV_BSIZE;
268 	lp->d_nsectors = sl->sl_nsectors;
269 	lp->d_ntracks = sl->sl_ntracks;
270 	lp->d_ncylinders = sl->sl_ncylinders;
271 
272 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
273 	lp->d_secpercyl = secpercyl;
274 	if (DL_GETDSIZE(lp) == 0)
275 		DL_SETDSIZE(lp, (u_int64_t)secpercyl * sl->sl_ncylinders);
276 	lp->d_version = 1;
277 
278 	memcpy(&lp->d_uid, &sl->sl_uid, sizeof(lp->d_uid));
279 
280 	lp->d_acylinders = sl->sl_acylinders;
281 
282 	lp->d_npartitions = MAXPARTITIONS;
283 	/* These are as defined in <ufs/ffs/fs.h> */
284 	lp->d_bbsize = 8192;	/* XXX */
285 	lp->d_sbsize = 8192;	/* XXX */
286 
287 	for (i = 0; i < 8; i++) {
288 		spp = &sl->sl_part[i];
289 		npp = &lp->d_partitions[i];
290 		DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
291 		DL_SETPSIZE(npp, spp->sdkp_nsectors);
292 		if (DL_GETPSIZE(npp) == 0) {
293 			npp->p_fstype = FS_UNUSED;
294 		} else {
295 			npp->p_fstype = sun_fstypes[i];
296 			if (npp->p_fstype == FS_BSDFFS) {
297 				/*
298 				 * The sun label does not store the FFS fields,
299 				 * so just set them with default values here.
300 				 */
301 				npp->p_fragblock =
302 				    DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
303 				npp->p_cpg = 16;
304 			}
305 		}
306 	}
307 
308 	/* Clear "extended" partition info, tentatively */
309 	for (i = 0; i < SUNXPART; i++) {
310 		npp = &lp->d_partitions[i+8];
311 		DL_SETPOFFSET(npp, 0);
312 		DL_SETPSIZE(npp, 0);
313 		npp->p_fstype = FS_UNUSED;
314 	}
315 
316 	/* Check to see if there's an "extended" partition table
317 	 * SL_XPMAG partitions had checksums up to just before the
318 	 * (new) sl_types variable, while SL_XPMAGTYP partitions have
319 	 * checksums up to the just before the (new) sl_xxx1 variable.
320 	 * Also, disklabels created prior to the addition of sl_uid will
321 	 * have a checksum to just before the sl_uid variable.
322 	 */
323 	if ((sl->sl_xpmag == SL_XPMAG &&
324 	    sun_extended_sum(sl, &sl->sl_types) == sl->sl_xpsum) ||
325 	    (sl->sl_xpmag == SL_XPMAGTYP &&
326 	    sun_extended_sum(sl, &sl->sl_uid) == sl->sl_xpsum) ||
327 	    (sl->sl_xpmag == SL_XPMAGTYP &&
328 	    sun_extended_sum(sl, &sl->sl_xxx1) == sl->sl_xpsum)) {
329 		/*
330 		 * There is.  Copy over the "extended" partitions.
331 		 * This code parallels the loop for partitions a-h.
332 		 */
333 		for (i = 0; i < SUNXPART; i++) {
334 			spp = &sl->sl_xpart[i];
335 			npp = &lp->d_partitions[i+8];
336 			DL_SETPOFFSET(npp, spp->sdkp_cyloffset * secpercyl);
337 			DL_SETPSIZE(npp, spp->sdkp_nsectors);
338 			if (DL_GETPSIZE(npp) == 0) {
339 				npp->p_fstype = FS_UNUSED;
340 				continue;
341 			}
342 			npp->p_fstype = FS_BSDFFS;
343 			npp->p_fragblock =
344 			    DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
345 			npp->p_cpg = 16;
346 		}
347 		if (sl->sl_xpmag == SL_XPMAGTYP) {
348 			for (i = 0; i < MAXPARTITIONS; i++) {
349 				npp = &lp->d_partitions[i];
350 				npp->p_fstype = sl->sl_types[i];
351 				npp->p_fragblock = sl->sl_fragblock[i];
352 				npp->p_cpg = sl->sl_cpg[i];
353 			}
354 		}
355 	} else if (preamble->sl_nparts <= 8) {
356 		/*
357 		 * A more traditional Sun label.  Recognise certain filesystem
358 		 * types from it, if they are available.
359 		 */
360 		i = preamble->sl_nparts;
361 		if (i == 0)
362 			i = 8;
363 
364 		npp = &lp->d_partitions[i-1];
365 		ppp = &preamble->sl_part[i-1];
366 		for (; i > 0; i--, npp--, ppp--) {
367 			if (npp->p_size == 0)
368 				continue;
369 			if ((ppp->spi_tag == 0) && (ppp->spi_flag == 0))
370 				continue;
371 
372 			switch (ppp->spi_tag) {
373 			case SPTAG_SUNOS_ROOT:
374 			case SPTAG_SUNOS_USR:
375 			case SPTAG_SUNOS_VAR:
376 			case SPTAG_SUNOS_HOME:
377 				npp->p_fstype = FS_BSDFFS;
378 				npp->p_fragblock =
379 				    DISKLABELV1_FFS_FRAGBLOCK(2048, 8);
380 				npp->p_cpg = 16;
381 				break;
382 			case SPTAG_LINUX_EXT2:
383 				npp->p_fstype = FS_EXT2FS;
384 				break;
385 			default:
386 				/* FS_SWAP for _SUNOS_SWAP and _LINUX_SWAP? */
387 				npp->p_fstype = FS_UNUSED;
388 				break;
389 			}
390 		}
391 	}
392 
393 	lp->d_checksum = 0;
394 	lp->d_checksum = dkcksum(lp);
395 	DNPRINTF(BOOT_D_OFDEV, "disklabel_sun_to_bsd: success!\n");
396 	return (0);
397 }
398 
399 /*
400  * Find a valid disklabel.
401  */
402 static char *
403 search_label(struct of_dev *devp, u_long off, char *buf, struct disklabel *lp,
404     u_long off0)
405 {
406 	size_t read;
407 	struct mbr_partition *p;
408 	int i;
409 	u_long poff;
410 	static int recursion;
411 
412 	struct disklabel *dlp;
413 	struct sun_disklabel *slp;
414 	int error;
415 
416 	/* minimal requirements for archetypal disk label */
417 	if (DL_GETDSIZE(lp) == 0)
418 		DL_SETDSIZE(lp, 0x1fffffff);
419 	lp->d_npartitions = MAXPARTITIONS;
420 	if (DL_GETPSIZE(&lp->d_partitions[0]) == 0)
421 		DL_SETPSIZE(&lp->d_partitions[0], 0x1fffffff);
422 	DL_SETPOFFSET(&lp->d_partitions[0], 0);
423 
424 	if (strategy(devp, F_READ, off, DEV_BSIZE, buf, &read)
425 	    || read != DEV_BSIZE)
426 		return ("Cannot read label");
427 
428 	/* Check for a disk label. */
429 	dlp = (struct disklabel *) (buf + LABELOFFSET);
430 	if (dlp->d_magic == DISKMAGIC) {
431 		if (dkcksum(dlp))
432 			return ("corrupt disk label");
433 		*lp = *dlp;
434 		DNPRINTF(BOOT_D_OFDEV, "search_label: found disk label\n");
435 		return (NULL);
436 	}
437 
438 	/* Check for a Sun disk label (for PROM compatibility). */
439 	slp = (struct sun_disklabel *)buf;
440 	if (slp->sl_magic == SUN_DKMAGIC) {
441 		if (disklabel_sun_to_bsd(slp, lp) != 0)
442 			return ("corrupt disk label");
443 		DNPRINTF(BOOT_D_OFDEV, "search_label: found disk label\n");
444 		return (NULL);
445 	}
446 
447 	return ("no disk label");
448 }
449 
450 int
451 devopen(struct open_file *of, const char *name, char **file)
452 {
453 	char *cp;
454 	char partition;
455 	char fname[256];
456 	char buf[DEV_BSIZE];
457 	struct disklabel label;
458 	int handle, part;
459 	size_t read;
460 	char *errmsg = NULL;
461 	int error = 0;
462 
463 	if (ofdev.handle != -1)
464 		panic("devopen");
465 	if (of->f_flags != F_READ)
466 		return EPERM;
467 	DNPRINTF(BOOT_D_OFDEV, "devopen: you want %s\n", name);
468 	strlcpy(fname, name, sizeof fname);
469 	cp = filename(fname, &partition);
470 	if (cp) {
471 		strlcpy(buf, cp, sizeof buf);
472 		*cp = 0;
473 	}
474 	if (!cp || !*buf)
475 		strlcpy(buf, DEFAULT_KERNEL, sizeof buf);
476 	if (!*fname)
477 		strlcpy(fname, bootdev, sizeof fname);
478 	strlcpy(opened_name, fname, sizeof opened_name);
479 	if (partition) {
480 		cp = opened_name + strlen(opened_name);
481 		*cp++ = ':';
482 		*cp++ = partition;
483 		*cp = 0;
484 	}
485 	if (*buf != '/')
486 		strlcat(opened_name, "/", sizeof opened_name);
487 	strlcat(opened_name, buf, sizeof opened_name);
488 	*file = opened_name + strlen(fname) + 1;
489 	DNPRINTF(BOOT_D_OFDEV, "devopen: trying %s\n", fname);
490 	if ((handle = OF_finddevice(fname)) == -1)
491 		return ENOENT;
492 	DNPRINTF(BOOT_D_OFDEV, "devopen: found %s\n", fname);
493 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
494 		return ENXIO;
495 	DNPRINTF(BOOT_D_OFDEV, "devopen: %s is called %s\n", fname, buf);
496 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
497 		return ENXIO;
498 	DNPRINTF(BOOT_D_OFDEV, "devopen: %s is a %s device\n", fname, buf);
499 	DNPRINTF(BOOT_D_OFDEV, "devopen: opening %s\n", fname);
500 	if ((handle = OF_open(fname)) == -1) {
501 		DNPRINTF(BOOT_D_OFDEV, "devopen: open of %s failed\n", fname);
502 		return ENXIO;
503 	}
504 	DNPRINTF(BOOT_D_OFDEV, "devopen: %s is now open\n", fname);
505 	bzero(&ofdev, sizeof ofdev);
506 	ofdev.handle = handle;
507 	if (!strcmp(buf, "block")) {
508 		ofdev.type = OFDEV_DISK;
509 		ofdev.bsize = DEV_BSIZE;
510 		/* First try to find a disklabel without MBR partitions */
511 		DNPRINTF(BOOT_D_OFDEV, "devopen: trying to read disklabel\n");
512 		if (strategy(&ofdev, F_READ,
513 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
514 		    || read != DEV_BSIZE
515 		    || (errmsg = getdisklabel(buf, &label))) {
516 #ifdef BOOT_DEBUG
517 			if (errmsg)
518 				DNPRINTF(BOOT_D_OFDEV,
519 				    "devopen: getdisklabel says %s\n", errmsg);
520 #endif
521 			/* Else try MBR partitions */
522 			errmsg = search_label(&ofdev, LABELSECTOR, buf,
523 			    &label, 0);
524 			if (errmsg) {
525 				printf("devopen: search_label says %s\n", errmsg);
526 				error = ERDLAB;
527 			}
528 			if (error && error != ERDLAB)
529 				goto bad;
530 		}
531 
532 		if (error == ERDLAB) {
533 			if (partition)
534 				/* User specified a parititon, but there is none */
535 				goto bad;
536 			/* No, label, just use complete disk */
537 			ofdev.partoff = 0;
538 		} else {
539 			part = partition ? partition - 'a' : 0;
540 			ofdev.partoff = label.d_partitions[part].p_offset;
541 			DNPRINTF(BOOT_D_OFDEV, "devopen: setting partition %d "
542 			    "offset %x\n", part, ofdev.partoff);
543 		}
544 
545 		of->f_dev = devsw;
546 		of->f_devdata = &ofdev;
547 #ifdef SPARC_BOOT_UFS
548 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
549 #endif
550 #ifdef SPARC_BOOT_HSFS
551 		bcopy(&file_system_cd9660, &file_system[nfsys++],
552 		    sizeof file_system[0]);
553 #endif
554 		DNPRINTF(BOOT_D_OFDEV, "devopen: return 0\n");
555 		return 0;
556 	}
557 #ifdef NETBOOT
558 	if (!strcmp(buf, "network")) {
559 		ofdev.type = OFDEV_NET;
560 		of->f_dev = devsw;
561 		of->f_devdata = &ofdev;
562 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
563 		nfsys = 1;
564 		if (error = net_open(&ofdev))
565 			goto bad;
566 		return 0;
567 	}
568 #endif
569 	error = EFTYPE;
570 bad:
571 	DNPRINTF(BOOT_D_OFDEV, "devopen: error %d, cannot open device\n",
572 	    error);
573 	OF_close(handle);
574 	ofdev.handle = -1;
575 	return error;
576 }
577