xref: /netbsd-src/sys/arch/sparc/stand/ofwboot/ofdev.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: ofdev.c,v 1.9 2005/12/11 12:19:08 christos 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 <dev/raidframe/raidframevar.h>
52 
53 #include "ofdev.h"
54 
55 extern char bootdev[];
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) returned %x\n",
86 		       str, dhandle);
87 #endif
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 #ifdef NOTDEF_DEBUG
97 				printf("filename: hunting for arguments "
98 				       "in %s\n", str);
99 #endif
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 					*cp = 0;	/* found arguments, make firmware ignore them */
110 				for (cp = lp; *--cp && *cp != ',' && *cp != ':';);
111 				if (*++cp >= 'a' && *cp <= 'a' + MAXPARTITIONS) {
112 					*ppart = *cp;
113 					*--cp = '\0';
114 				}
115 			}
116 #ifdef NOTDEF_DEBUG
117 			printf("filename: found %s\n",lp);
118 #endif
119 			return lp;
120 		} else if (OF_getprop(dhandle, "device_type", devtype, sizeof devtype) < 0)
121 			devtype[0] = 0;
122 	}
123 #ifdef NOTDEF_DEBUG
124 	printf("filename: not found\n",lp);
125 #endif
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 (OF_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 = OF_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(of)
177 	struct open_file *of;
178 {
179 	struct of_dev *op = of->f_devdata;
180 
181 #ifdef NETBOOT
182 	if (op->type == OFDEV_NET)
183 		net_close(op);
184 #endif
185 	OF_close(op->handle);
186 	op->handle = -1;
187 }
188 
189 static struct devsw devsw[1] = {
190 	"OpenFirmware",
191 	strategy,
192 	(int (*)__P((struct open_file *, ...)))nodev,
193 	devclose,
194 	noioctl
195 };
196 int ndevs = sizeof devsw / sizeof devsw[0];
197 
198 #ifdef SPARC_BOOT_UFS
199 static struct fs_ops file_system_ufs = FS_OPS(ufs);
200 #endif
201 #ifdef SPARC_BOOT_HSFS
202 static struct fs_ops file_system_cd9660 = FS_OPS(cd9660);
203 #endif
204 #ifdef NETBOOT
205 static struct fs_ops file_system_nfs = FS_OPS(nfs);
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(p)
220 	const void *p;
221 {
222 	const unsigned char *cp = p;
223 
224 	return cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24);
225 }
226 /************************************************************************
227  *
228  * The rest of this was taken from arch/sparc64/scsi/sun_disklabel.c
229  * and then substantially rewritten by Gordon W. Ross
230  *
231  ************************************************************************/
232 
233 /* What partition types to assume for Sun disklabels: */
234 static u_char
235 sun_fstypes[8] = {
236 	FS_BSDFFS,	/* a */
237 	FS_SWAP,	/* b */
238 	FS_OTHER,	/* c - whole disk */
239 	FS_BSDFFS,	/* d */
240 	FS_BSDFFS,	/* e */
241 	FS_BSDFFS,	/* f */
242 	FS_BSDFFS,	/* g */
243 	FS_BSDFFS,	/* h */
244 };
245 
246 /*
247  * Given a SunOS disk label, set lp to a BSD disk label.
248  * Returns NULL on success, else an error string.
249  *
250  * The BSD label is cleared out before this is called.
251  */
252 static char *
253 disklabel_sun_to_bsd(cp, lp)
254 	char *cp;
255 	struct disklabel *lp;
256 {
257 	struct sun_disklabel *sl;
258 	struct partition *npp;
259 	struct sun_dkpart *spp;
260 	int i, secpercyl;
261 	u_short cksum, *sp1, *sp2;
262 
263 	sl = (struct sun_disklabel *)cp;
264 
265 	/* Verify the XOR check. */
266 	sp1 = (u_short *)sl;
267 	sp2 = (u_short *)(sl + 1);
268 	cksum = 0;
269 	while (sp1 < sp2)
270 		cksum ^= *sp1++;
271 	if (cksum != 0)
272 		return("SunOS disk label, bad checksum");
273 
274 	/* Format conversion. */
275 	lp->d_magic = DISKMAGIC;
276 	lp->d_magic2 = DISKMAGIC;
277 	memcpy(lp->d_packname, sl->sl_text, sizeof(lp->d_packname));
278 
279 	lp->d_secsize = 512;
280 	lp->d_nsectors   = sl->sl_nsectors;
281 	lp->d_ntracks    = sl->sl_ntracks;
282 	lp->d_ncylinders = sl->sl_ncylinders;
283 
284 	secpercyl = sl->sl_nsectors * sl->sl_ntracks;
285 	lp->d_secpercyl  = secpercyl;
286 	lp->d_secperunit = secpercyl * sl->sl_ncylinders;
287 
288 	lp->d_sparespercyl = sl->sl_sparespercyl;
289 	lp->d_acylinders   = sl->sl_acylinders;
290 	lp->d_rpm          = sl->sl_rpm;
291 	lp->d_interleave   = sl->sl_interleave;
292 
293 	lp->d_npartitions = 8;
294 	/* These are as defined in <ufs/ffs/fs.h> */
295 	lp->d_bbsize = 8192;	/* XXX */
296 	lp->d_sbsize = 8192;	/* XXX */
297 
298 	for (i = 0; i < 8; i++) {
299 		spp = &sl->sl_part[i];
300 		npp = &lp->d_partitions[i];
301 		npp->p_offset = spp->sdkp_cyloffset * secpercyl;
302 		npp->p_size = spp->sdkp_nsectors;
303 #ifdef NOTDEF_DEBUG
304 		printf("partition %d start %x size %x\n", i, (int)npp->p_offset, (int)npp->p_size);
305 #endif
306 		if (npp->p_size == 0) {
307 			npp->p_fstype = FS_UNUSED;
308 		} else {
309 			npp->p_fstype = sun_fstypes[i];
310 			if (npp->p_fstype == FS_BSDFFS) {
311 				/*
312 				 * The sun label does not store the FFS fields,
313 				 * so just set them with default values here.
314 				 */
315 				npp->p_fsize = 1024;
316 				npp->p_frag = 8;
317 				npp->p_cpg = 16;
318 			}
319 		}
320 	}
321 
322 	lp->d_checksum = 0;
323 	lp->d_checksum = dkcksum(lp);
324 #ifdef NOTDEF_DEBUG
325 	printf("disklabel_sun_to_bsd: success!\n");
326 #endif
327 	return (NULL);
328 }
329 
330 /*
331  * Find a valid disklabel.
332  */
333 static char *
334 search_label(devp, off, buf, lp, off0)
335 	struct of_dev *devp;
336 	u_long off;
337 	char *buf;
338 	struct disklabel *lp;
339 	u_long off0;
340 {
341 	size_t read;
342 	struct mbr_partition *p;
343 	int i;
344 	u_long poff;
345 	static int recursion;
346 
347 	struct disklabel *dlp;
348 	struct sun_disklabel *slp;
349 	int error;
350 
351 	/* minimal requirements for archtypal disk label */
352 	if (lp->d_secperunit == 0)
353 		lp->d_secperunit = 0x1fffffff;
354 	lp->d_npartitions = 1;
355 	if (lp->d_partitions[0].p_size == 0)
356 		lp->d_partitions[0].p_size = 0x1fffffff;
357 	lp->d_partitions[0].p_offset = 0;
358 
359 	if (strategy(devp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &read)
360 	    || read != DEV_BSIZE)
361 		return ("Cannot read label");
362 	/* Check for a NetBSD disk label. */
363 	dlp = (struct disklabel *) (buf + LABELOFFSET);
364 	if (dlp->d_magic == DISKMAGIC) {
365 		if (dkcksum(dlp))
366 			return ("NetBSD disk label corrupted");
367 		*lp = *dlp;
368 #ifdef NOTDEF_DEBUG
369 		printf("search_label: found NetBSD label\n");
370 #endif
371 		return (NULL);
372 	}
373 
374 	/* Check for a Sun disk label (for PROM compatibility). */
375 	slp = (struct sun_disklabel *) buf;
376 	if (slp->sl_magic == SUN_DKMAGIC)
377 		return (disklabel_sun_to_bsd(buf, lp));
378 
379 
380 	bzero(buf, sizeof(buf));
381 	return ("no disk label");
382 }
383 
384 int
385 devopen(of, name, file)
386 	struct open_file *of;
387 	const char *name;
388 	char **file;
389 {
390 	char *cp;
391 	char partition;
392 	char fname[256];
393 	char buf[DEV_BSIZE];
394 	struct disklabel label;
395 	int handle, part;
396 	size_t read;
397 	char *errmsg = NULL;
398 	int error = 0;
399 
400 	if (ofdev.handle != -1)
401 		panic("devopen");
402 	if (of->f_flags != F_READ)
403 		return EPERM;
404 #ifdef NOTDEF_DEBUG
405 	printf("devopen: you want %s\n", name);
406 #endif
407 	strcpy(fname, name);
408 	cp = filename(fname, &partition);
409 	if (cp) {
410 		strcpy(buf, cp);
411 		*cp = 0;
412 	}
413 	if (!cp || !*buf)
414 		strcpy(buf, DEFAULT_KERNEL);
415 	if (!*fname)
416 		strcpy(fname, bootdev);
417 	strcpy(opened_name, fname);
418 	if (partition) {
419 		cp = opened_name + strlen(opened_name);
420 		*cp++ = ':';
421 		*cp++ = partition;
422 		*cp = 0;
423 	}
424 	*file = opened_name + strlen(opened_name);
425 	if (*buf != '/')
426 		strcat(opened_name, "/");
427 	strcat(opened_name, buf);
428 #ifdef NOTDEF_DEBUG
429 	printf("devopen: trying %s\n", fname);
430 #endif
431 	if ((handle = OF_finddevice(fname)) == -1)
432 		return ENOENT;
433 #ifdef NOTDEF_DEBUG
434 	printf("devopen: found %s\n", fname);
435 #endif
436 	if (OF_getprop(handle, "name", buf, sizeof buf) < 0)
437 		return ENXIO;
438 #ifdef NOTDEF_DEBUG
439 	printf("devopen: %s is called %s\n", fname, buf);
440 #endif
441 	floppyboot = !strcmp(buf, "floppy");
442 	if (OF_getprop(handle, "device_type", buf, sizeof buf) < 0)
443 		return ENXIO;
444 #ifdef NOTDEF_DEBUG
445 	printf("devopen: %s is a %s device\n", fname, buf);
446 #endif
447 #ifdef NOTDEF_DEBUG
448 	printf("devopen: opening %s\n", fname);
449 #endif
450 	if ((handle = OF_open(fname)) == -1) {
451 #ifdef NOTDEF_DEBUG
452 		printf("devopen: open of %s failed\n", fname);
453 #endif
454 		return ENXIO;
455 	}
456 #ifdef NOTDEF_DEBUG
457 	printf("devopen: %s is now open\n", fname);
458 #endif
459 	bzero(&ofdev, sizeof ofdev);
460 	ofdev.handle = handle;
461 	if (!strcmp(buf, "block")) {
462 		ofdev.type = OFDEV_DISK;
463 		ofdev.bsize = DEV_BSIZE;
464 		/* First try to find a disklabel without MBR partitions */
465 #ifdef NOTDEF_DEBUG
466 		printf("devopen: trying to read disklabel\n");
467 #endif
468 		if (strategy(&ofdev, F_READ,
469 			     LABELSECTOR, DEV_BSIZE, buf, &read) != 0
470 		    || read != DEV_BSIZE
471 		    || (errmsg = getdisklabel(buf, &label))) {
472 			if (errmsg) printf("devopen: getdisklabel returned %s\n", errmsg);
473 			/* Else try MBR partitions */
474 			errmsg = search_label(&ofdev, 0, buf, &label, 0);
475 			if (errmsg) {
476 				printf("devopen: search_label returned %s\n", errmsg);
477 				error = ERDLAB;
478 			}
479 			if (error && error != ERDLAB)
480 				goto bad;
481 		}
482 
483 		if (error == ERDLAB) {
484 			if (partition)
485 				/* User specified a parititon, but there is none */
486 				goto bad;
487 			/* No, label, just use complete disk */
488 			ofdev.partoff = 0;
489 		} else {
490 			part = partition ? partition - 'a' : 0;
491 			ofdev.partoff = label.d_partitions[part].p_offset;
492 #ifdef NOTDEF_DEBUG
493 			printf("devopen: setting partition %d offset %x\n",
494 			       part, ofdev.partoff);
495 #endif
496 			if (label.d_partitions[part].p_fstype == FS_RAID) {
497 				ofdev.partoff += RF_PROTECTED_SECTORS;
498 #ifdef NOTDEF_DEBUG
499 				printf("devopen: found RAID partition, "
500 				    "adjusting offset to %x\n", ofdev.partoff);
501 #endif
502 			}
503 		}
504 
505 		of->f_dev = devsw;
506 		of->f_devdata = &ofdev;
507 #ifdef SPARC_BOOT_UFS
508 		bcopy(&file_system_ufs, &file_system[nfsys++], sizeof file_system[0]);
509 #endif
510 #ifdef SPARC_BOOT_HSFS
511 		bcopy(&file_system_cd9660, &file_system[nfsys++],
512 		    sizeof file_system[0]);
513 #endif
514 #ifdef NOTDEF_DEBUG
515 		printf("devopen: return 0\n");
516 #endif
517 		return 0;
518 	}
519 #ifdef NETBOOT
520 	if (!strcmp(buf, "network")) {
521 		ofdev.type = OFDEV_NET;
522 		of->f_dev = devsw;
523 		of->f_devdata = &ofdev;
524 		bcopy(&file_system_nfs, file_system, sizeof file_system[0]);
525 		nfsys = 1;
526 		if (error = net_open(&ofdev))
527 			goto bad;
528 		return 0;
529 	}
530 #endif
531 	error = EFTYPE;
532 bad:
533 #ifdef NOTDEF_DEBUG
534 	printf("devopen: error %d, cannot open device\n", error);
535 #endif
536 	OF_close(handle);
537 	ofdev.handle = -1;
538 	return error;
539 }
540