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