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