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