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