1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
|
/* $OpenBSD: biosdev.c,v 1.17 2012/01/11 14:47:02 jsing Exp $ */
/*
* Copyright (c) 1996 Michael Shalayeff
* Copyright (c) 2003 Tobias Weingartner
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/reboot.h>
#include <sys/disklabel.h>
#include <dev/biovar.h>
#include <dev/softraidvar.h>
#include <isofs/cd9660/iso.h>
#include <lib/libsa/saerrno.h>
#include <machine/tss.h>
#include <machine/biosvar.h>
#include "disk.h"
#include "libsa.h"
#include "biosdev.h"
static const char *biosdisk_err(u_int);
static int biosdisk_errno(u_int);
int CHS_rw (int, int, int, int, int, int, void *);
static int EDD_rw (int, int, u_int32_t, u_int32_t, void *);
static u_int findopenbsd(bios_diskinfo_t *, const char **);
static const char *sr_getdisklabel(struct sr_boot_volume *, struct disklabel *);
static int sr_strategy(struct sr_boot_volume *, int, daddr32_t, size_t,
void *, size_t *);
extern int debug;
int bios_bootdev;
int bios_cddev = -1; /* Set by srt0 if coming from CD */
struct EDD_CB {
u_int8_t edd_len; /* size of packet */
u_int8_t edd_res1; /* reserved */
u_int8_t edd_nblk; /* # of blocks to transfer */
u_int8_t edd_res2; /* reserved */
u_int16_t edd_off; /* address of buffer (offset) */
u_int16_t edd_seg; /* address of buffer (segment) */
u_int64_t edd_daddr; /* starting block */
};
/*
* reset disk system
*/
static int
biosdreset(int dev)
{
int rv;
__asm __volatile (DOINT(0x13) "; setc %b0" : "=a" (rv)
: "0" (0), "d" (dev) : "%ecx", "cc");
return ((rv & 0xff)? rv >> 8 : 0);
}
/*
* Fill out a bios_diskinfo_t for this device.
* Return 0 if all ok.
* Return 1 if not ok.
*/
int
bios_getdiskinfo(int dev, bios_diskinfo_t *pdi)
{
u_int rv;
/* Just reset, don't check return code */
rv = biosdreset(dev);
#ifdef BIOS_DEBUG
if (debug)
printf("getinfo: try #8, 0x%x, %p\n", dev, pdi);
#endif
__asm __volatile (DOINT(0x13) "\n\t"
"setc %b0; movzbl %h1, %1\n\t"
"movzbl %%cl, %3; andb $0x3f, %b3\n\t"
"xchgb %%cl, %%ch; rolb $2, %%ch"
: "=a" (rv), "=d" (pdi->bios_heads),
"=c" (pdi->bios_cylinders),
"=b" (pdi->bios_sectors)
: "0" (0x0800), "1" (dev) : "cc");
#ifdef BIOS_DEBUG
if (debug) {
printf("getinfo: got #8\n");
printf("disk 0x%x: %d,%d,%d\n", dev, pdi->bios_cylinders,
pdi->bios_heads, pdi->bios_sectors);
}
#endif
if (rv & 0xff)
return 1;
/* Fix up info */
pdi->bios_number = dev;
pdi->bios_heads++;
pdi->bios_cylinders &= 0x3ff;
pdi->bios_cylinders++;
/* NOTE:
* This currently hangs/reboots some machines
* The IBM ThinkPad 750ED for one.
*
* Funny that an IBM/MS extension would not be
* implemented by an IBM system...
*
* Future hangs (when reported) can be "fixed"
* with getSYSCONFaddr() and an exceptions list.
*/
if (dev & 0x80 && (dev == 0x80 || dev == 0x81 || dev == bios_bootdev)) {
int bm;
#ifdef BIOS_DEBUG
if (debug)
printf("getinfo: try #41, 0x%x\n", dev);
#endif
/* EDD support check */
__asm __volatile(DOINT(0x13) "; setc %b0"
: "=a" (rv), "=c" (bm)
: "0" (0x4100), "b" (0x55aa), "d" (dev) : "cc");
if (!(rv & 0xff) && (BIOS_regs.biosr_bx & 0xffff) == 0xaa55)
pdi->bios_edd = (bm & 0xffff) | ((rv & 0xff) << 16);
else
pdi->bios_edd = -1;
#ifdef BIOS_DEBUG
if (debug) {
printf("getinfo: got #41\n");
printf("disk 0x%x: 0x%x\n", dev, bm);
}
#endif
/*
* If extended disk access functions are not supported
* there is not much point on doing EDD.
*/
if (!(pdi->bios_edd & EXT_BM_EDA))
pdi->bios_edd = -1;
} else
pdi->bios_edd = -1;
/* Skip sanity check for CHS options in EDD mode. */
if (pdi->bios_edd != -1)
return 0;
/* Sanity check */
if (!pdi->bios_cylinders || !pdi->bios_heads || !pdi->bios_sectors)
return 1;
/* CD-ROMs sometimes return heads == 1 */
if (pdi->bios_heads < 2)
return 1;
return 0;
}
/*
* Read/Write a block from given place using the BIOS.
*/
int
CHS_rw(int rw, int dev, int cyl, int head, int sect, int nsect, void *buf)
{
int rv;
rw = rw == F_READ ? 2 : 3;
BIOS_regs.biosr_es = (u_int32_t)buf >> 4;
__asm __volatile ("movb %b7, %h1\n\t"
"movb %b6, %%dh\n\t"
"andl $0xf, %4\n\t"
/* cylinder; the highest 2 bits of cyl is in %cl */
"xchgb %%ch, %%cl\n\t"
"rorb $2, %%cl\n\t"
"orb %b5, %%cl\n\t"
"inc %%cx\n\t"
DOINT(0x13) "\n\t"
"setc %b0"
: "=a" (rv)
: "0" (nsect), "d" (dev), "c" (cyl),
"b" (buf), "m" (sect), "m" (head),
"m" (rw)
: "cc", "memory");
return ((rv & 0xff)? rv >> 8 : 0);
}
static __inline int
EDD_rw(int rw, int dev, u_int32_t daddr, u_int32_t nblk, void *buf)
{
int rv;
volatile static struct EDD_CB cb;
/* Zero out reserved stuff */
cb.edd_res1 = 0;
cb.edd_res2 = 0;
/* Fill in parameters */
cb.edd_len = sizeof(cb);
cb.edd_nblk = nblk;
cb.edd_seg = ((u_int32_t)buf >> 4) & 0xffff;
cb.edd_off = (u_int32_t)buf & 0xf;
cb.edd_daddr = daddr;
/* if offset/segment are zero, punt */
if (!cb.edd_seg && !cb.edd_off)
return 1;
/* Call extended read/write (with disk packet) */
BIOS_regs.biosr_ds = (u_int32_t)&cb >> 4;
__asm __volatile (DOINT(0x13) "; setc %b0" : "=a" (rv)
: "0" ((rw == F_READ)? 0x4200: 0x4300),
"d" (dev), "S" ((int) (&cb) & 0xf) : "%ecx", "cc");
return ((rv & 0xff)? rv >> 8 : 0);
}
/*
* Read given sector, handling retry/errors/etc.
*/
int
biosd_io(int rw, bios_diskinfo_t *bd, u_int off, int nsect, void *buf)
{
int dev = bd->bios_number;
int j, error;
void *bb;
int bbsize = nsect * DEV_BSIZE;
if (bd->flags & BDI_EL_TORITO) { /* It's a CD device */
dev &= 0xff; /* Mask out this flag bit */
/*
* sys/lib/libsa/cd9600.c converts 2,048-byte CD sectors
* to DEV_BSIZE blocks before calling the device strategy
* routine. However, the El Torito spec says that the
* BIOS will work in 2,048-byte sectors. So shift back.
*/
off >>= (ISO_DEFAULT_BLOCK_SHIFT - DEV_BSHIFT);
nsect >>= (ISO_DEFAULT_BLOCK_SHIFT - DEV_BSHIFT);
}
/*
* Use a bounce buffer to not cross 64k DMA boundary, and to
* not access 1 MB or above.
*/
if (((((u_int32_t)buf) & ~0xffff) !=
(((u_int32_t)buf + bbsize) & ~0xffff)) ||
(((u_int32_t)buf) >= 0x100000)) {
/*
* XXX we believe that all the io is buffered
* by fs routines, so no big reads anyway
*/
bb = alloca(bbsize);
if (rw != F_READ)
bcopy(buf, bb, bbsize);
} else
bb = buf;
/* Try to do operation up to 5 times */
for (error = 1, j = 5; j-- && error; ) {
/* CHS or LBA access? */
if (bd->bios_edd != -1) {
error = EDD_rw(rw, dev, off, nsect, bb);
} else {
int cyl, head, sect;
size_t i, n;
char *p = bb;
/* Handle track boundaries */
for (error = i = 0; error == 0 && i < nsect;
i += n, off += n, p += n * DEV_BSIZE) {
btochs(off, cyl, head, sect, bd->bios_heads,
bd->bios_sectors);
if ((sect + (nsect - i)) >= bd->bios_sectors)
n = bd->bios_sectors - sect;
else
n = nsect - i;
error = CHS_rw(rw, dev, cyl, head, sect, n, p);
/* ECC corrected */
if (error == 0x11)
error = 0;
}
}
switch (error) {
case 0x00: /* No errors */
case 0x11: /* ECC corrected */
error = 0;
break;
default: /* All other errors */
#ifdef BIOS_DEBUG
if (debug)
printf("\nBIOS error 0x%x (%s)\n",
error, biosdisk_err(error));
#endif
biosdreset(dev);
break;
}
}
if (bb != buf && rw == F_READ)
bcopy(bb, buf, bbsize);
#ifdef BIOS_DEBUG
if (debug) {
if (error != 0)
printf("=0x%x(%s)", error, biosdisk_err(error));
putchar('\n');
}
#endif
return error;
}
/*
* Try to read the bsd label on the given BIOS device.
*/
static u_int
findopenbsd(bios_diskinfo_t *bd, const char **err)
{
struct dos_mbr mbr;
struct dos_partition *dp;
u_int mbroff = DOSBBSECTOR;
u_int mbr_eoff = DOSBBSECTOR; /* Offset of MBR extended partition. */
int error, i, maxebr = DOS_MAXEBR, nextebr;
again:
if (!maxebr--) {
*err = "too many extended partitions";
return (-1);
}
/* Read MBR */
bzero(&mbr, sizeof(mbr));
error = biosd_io(F_READ, bd, mbroff, 1, &mbr);
if (error) {
*err = biosdisk_err(error);
return (-1);
}
/* check mbr signature */
if (mbr.dmbr_sign != DOSMBR_SIGNATURE) {
*err = "bad MBR signature\n";
return (-1);
}
/* Search for OpenBSD partition */
nextebr = 0;
for (i = 0; i < NDOSPART; i++) {
dp = &mbr.dmbr_parts[i];
if (!dp->dp_size)
continue;
#ifdef BIOS_DEBUG
if (debug)
printf("found partition %u: "
"type %u (0x%x) offset %u (0x%x)\n",
(int)(dp - mbr.dmbr_parts),
dp->dp_typ, dp->dp_typ,
dp->dp_start, dp->dp_start);
#endif
if (dp->dp_typ == DOSPTYP_OPENBSD) {
if (dp->dp_start > (dp->dp_start + mbroff))
continue;
return (dp->dp_start + mbroff);
}
/*
* Record location of next ebr if and only if this is the first
* extended partition in this boot record!
*/
if (!nextebr && (dp->dp_typ == DOSPTYP_EXTEND ||
dp->dp_typ == DOSPTYP_EXTENDL)) {
nextebr = dp->dp_start + mbr_eoff;
if (nextebr < dp->dp_start)
nextebr = (u_int)-1;
if (mbr_eoff == DOSBBSECTOR)
mbr_eoff = dp->dp_start;
}
}
if (nextebr && nextebr != (u_int)-1) {
mbroff = nextebr;
goto again;
}
return (-1);
}
const char *
bios_getdisklabel(bios_diskinfo_t *bd, struct disklabel *label)
{
u_int start = 0;
char *buf;
const char *err = NULL;
int error;
/* Sanity check */
if (bd->bios_edd == -1 &&
(bd->bios_heads == 0 || bd->bios_sectors == 0))
return "failed to read disklabel";
/* MBR is a harddisk thing */
if (bd->bios_number & 0x80) {
start = findopenbsd(bd, &err);
if (start == (u_int)-1) {
if (err != NULL)
return (err);
return "no OpenBSD partition\n";
}
}
start = LABELSECTOR + start;
/* Load BSD disklabel */
buf = alloca(DEV_BSIZE);
#ifdef BIOS_DEBUG
if (debug)
printf("loading disklabel @ %u\n", start);
#endif
/* read disklabel */
error = biosd_io(F_READ, bd, start, 1, buf);
if (error)
return "failed to read disklabel";
/* Fill in disklabel */
return (getdisklabel(buf, label));
}
int
biosopen(struct open_file *f, ...)
{
struct sr_boot_volume *bv;
register char *cp, **file;
dev_t maj, unit, part;
struct diskinfo *dip;
int biosdev, devlen;
const char *st;
va_list ap;
char *dev;
va_start(ap, f);
cp = *(file = va_arg(ap, char **));
va_end(ap);
#ifdef BIOS_DEBUG
if (debug)
printf("%s\n", cp);
#endif
f->f_devdata = NULL;
/* Search for device specification. */
dev = cp;
if (cp[4] == ':')
devlen = 2;
else if (cp[5] == ':')
devlen = 3;
else
return ENOENT;
cp += devlen;
/* Get unit. */
if ('0' <= *cp && *cp <= '9')
unit = *cp++ - '0';
else {
printf("Bad unit number\n");
return EUNIT;
}
/* Get partition. */
if ('a' <= *cp && *cp <= 'p')
part = *cp++ - 'a';
else {
printf("Bad partition\n");
return EPART;
}
/* Get filename. */
cp++; /* skip ':' */
if (*cp != 0)
*file = cp;
else
f->f_flags |= F_RAW;
/* Intercept softraid disks. */
if (strncmp("sr", dev, 2) == 0) {
/* Create a fake diskinfo for this softraid volume. */
SLIST_FOREACH(bv, &sr_volumes, sbv_link)
if (bv->sbv_unit == unit)
break;
if (bv == NULL) {
printf("Unknown device: sr%d\n", unit);
return EADAPT;
}
if (bv->sbv_diskinfo == NULL) {
dip = alloc(sizeof(struct diskinfo));
bzero(dip, sizeof(*dip));
bv->sbv_diskinfo = dip;
dip->sr_vol = bv;
dip->bios_info.flags |= BDI_BADLABEL;
}
dip = bv->sbv_diskinfo;
if (dip->bios_info.flags & BDI_BADLABEL) {
/* Attempt to read disklabel. */
bv->sbv_part = 'c';
if (sr_getdisklabel(bv, &dip->disklabel))
return ERDLAB;
dip->bios_info.flags &= ~BDI_BADLABEL;
}
bv->sbv_part = part + 'a';
bootdev_dip = dip;
f->f_devdata = dip;
return 0;
}
for (maj = 0; maj < nbdevs &&
strncmp(dev, bdevs[maj], devlen); maj++);
if (maj >= nbdevs) {
printf("Unknown device: ");
for (cp = *file; *cp != ':'; cp++)
putchar(*cp);
putchar('\n');
return EADAPT;
}
biosdev = unit;
switch (maj) {
case 0: /* wd */
case 4: /* sd */
case 17: /* hd */
biosdev |= 0x80;
break;
case 2: /* fd */
break;
case 6: /* cd */
biosdev = bios_bootdev & 0xff;
break;
default:
return ENXIO;
}
/* Find device */
bootdev_dip = dip = dklookup(biosdev);
/* Fix up bootdev */
{ dev_t bsd_dev;
bsd_dev = dip->bios_info.bsd_dev;
dip->bsddev = MAKEBOOTDEV(B_TYPE(bsd_dev), B_ADAPTOR(bsd_dev),
B_CONTROLLER(bsd_dev), unit, part);
dip->bootdev = MAKEBOOTDEV(B_TYPE(bsd_dev), B_ADAPTOR(bsd_dev),
B_CONTROLLER(bsd_dev), B_UNIT(bsd_dev), part);
}
#if 0
dip->bios_info.bsd_dev = dip->bootdev;
bootdev = dip->bootdev;
#endif
#ifdef BIOS_DEBUG
if (debug) {
printf("BIOS geometry: heads=%u, s/t=%u; EDD=%d\n",
dip->bios_info.bios_heads, dip->bios_info.bios_sectors,
dip->bios_info.bios_edd);
}
#endif
/* Try for disklabel again (might be removable media) */
if (dip->bios_info.flags & BDI_BADLABEL) {
st = bios_getdisklabel(&dip->bios_info, &dip->disklabel);
#ifdef BIOS_DEBUG
if (debug && st)
printf("%s\n", st);
#endif
if (!st) {
dip->bios_info.flags &= ~BDI_BADLABEL;
dip->bios_info.flags |= BDI_GOODLABEL;
} else
return ERDLAB;
}
f->f_devdata = dip;
return 0;
}
const u_char bidos_errs[] =
/* ignored "\x00" "successful completion\0" */
"\x01" "invalid function/parameter\0"
"\x02" "address mark not found\0"
"\x03" "write-protected\0"
"\x04" "sector not found\0"
"\x05" "reset failed\0"
"\x06" "disk changed\0"
"\x07" "drive parameter activity failed\0"
"\x08" "DMA overrun\0"
"\x09" "data boundary error\0"
"\x0A" "bad sector detected\0"
"\x0B" "bad track detected\0"
"\x0C" "invalid media\0"
"\x0E" "control data address mark detected\0"
"\x0F" "DMA arbitration level out of range\0"
"\x10" "uncorrectable CRC or ECC error on read\0"
/* ignored "\x11" "data ECC corrected\0" */
"\x20" "controller failure\0"
"\x31" "no media in drive\0"
"\x32" "incorrect drive type in CMOS\0"
"\x40" "seek failed\0"
"\x80" "operation timed out\0"
"\xAA" "drive not ready\0"
"\xB0" "volume not locked in drive\0"
"\xB1" "volume locked in drive\0"
"\xB2" "volume not removable\0"
"\xB3" "volume in use\0"
"\xB4" "lock count exceeded\0"
"\xB5" "valid eject request failed\0"
"\xBB" "undefined error\0"
"\xCC" "write fault\0"
"\xE0" "status register error\0"
"\xFF" "sense operation failed\0"
"\x00" "\0";
static const char *
biosdisk_err(u_int error)
{
register const u_char *p = bidos_errs;
while (*p && *p != error)
while (*p++);
return ++p;
}
const struct biosdisk_errors {
u_char error;
u_char errno;
} tab[] = {
{ 0x01, EINVAL },
{ 0x03, EROFS },
{ 0x08, EINVAL },
{ 0x09, EINVAL },
{ 0x0A, EBSE },
{ 0x0B, EBSE },
{ 0x0C, ENXIO },
{ 0x0D, EINVAL },
{ 0x10, EECC },
{ 0x20, EHER },
{ 0x31, ENXIO },
{ 0x32, ENXIO },
{ 0x00, EIO }
};
static int
biosdisk_errno(u_int error)
{
register const struct biosdisk_errors *p;
if (error == 0)
return 0;
for (p = tab; p->error && p->error != error; p++);
return p->errno;
}
int
biosstrategy(void *devdata, int rw, daddr32_t blk, size_t size, void *buf,
size_t *rsize)
{
struct diskinfo *dip = (struct diskinfo *)devdata;
bios_diskinfo_t *bd = &dip->bios_info;
u_int8_t error = 0;
size_t nsect;
/* Intercept strategy for softraid volumes. */
if (dip->sr_vol)
return sr_strategy(dip->sr_vol, rw, blk, size, buf, rsize);
nsect = (size + DEV_BSIZE-1) / DEV_BSIZE;
blk += dip->disklabel.d_partitions[B_PARTITION(dip->bsddev)].p_offset;
/* Read all, sub-functions handle track boundaries */
if (blk < 0)
error = EINVAL;
else
error = biosd_io(rw, bd, blk, nsect, buf);
#ifdef BIOS_DEBUG
if (debug) {
if (error != 0)
printf("=0x%x(%s)", error, biosdisk_err(error));
putchar('\n');
}
#endif
if (rsize != NULL)
*rsize = nsect * DEV_BSIZE;
return (biosdisk_errno(error));
}
int
biosclose(struct open_file *f)
{
f->f_devdata = NULL;
return 0;
}
int
biosioctl(struct open_file *f, u_long cmd, void *data)
{
return 0;
}
static int
sr_strategy(struct sr_boot_volume *bv, int rw, daddr32_t blk, size_t size,
void *buf, size_t *rsize)
{
struct diskinfo *sr_dip, *dip;
struct sr_boot_chunk *bc;
/* We only support read-only softraid. */
if (rw != F_READ)
return EPERM;
/* Partition offset within softraid volume. */
sr_dip = (struct diskinfo *)bv->sbv_diskinfo;
blk += sr_dip->disklabel.d_partitions[bv->sbv_part - 'a'].p_offset;
if (bv->sbv_level == 0) {
return ENOTSUP;
} else if (bv->sbv_level == 1) {
/* Select first online chunk. */
SLIST_FOREACH(bc, &bv->sbv_chunks, sbc_link)
if (bc->sbc_state == BIOC_SDONLINE)
break;
if (bc == NULL)
return EIO;
dip = (struct diskinfo *)bc->sbc_diskinfo;
dip->bsddev = bc->sbc_mm;
blk += bv->sbv_data_offset;
/* XXX - If I/O failed we should try another chunk... */
return biosstrategy(dip, rw, blk, size, buf, rsize);
} else if (bv->sbv_level == 'C') {
printf("mmmm... crypto!\n");
return ENOTSUP;
} else
return ENOTSUP;
}
static const char *
sr_getdisklabel(struct sr_boot_volume *bv, struct disklabel *label)
{
struct dos_partition *dp;
struct dos_mbr mbr;
u_int start = 0;
char *buf;
int i;
/* Check for MBR to determine partition offset. */
bzero(&mbr, sizeof(mbr));
sr_strategy(bv, F_READ, DOSBBSECTOR, sizeof(struct dos_mbr),
&mbr, NULL);
if (mbr.dmbr_sign == DOSMBR_SIGNATURE) {
/* Search for OpenBSD partition */
for (i = 0; i < NDOSPART; i++) {
dp = &mbr.dmbr_parts[i];
if (!dp->dp_size)
continue;
if (dp->dp_typ == DOSPTYP_OPENBSD) {
if (dp->dp_start > (dp->dp_start + DOSBBSECTOR))
continue;
start = dp->dp_start + DOSBBSECTOR;
}
}
}
start += LABELSECTOR;
/* Read the disklabel. */
buf = alloca(DEV_BSIZE);
sr_strategy(bv, F_READ, start, sizeof(struct disklabel), buf, NULL);
#if BIOS_DEBUG
printf("sr_getdisklabel: magic %lx\n",
((struct disklabel *)buf)->d_magic);
for (i = 0; i < MAXPARTITIONS; i++)
printf("part %c: type = %d, size = %d, offset = %d\n", 'a' + i,
(int)((struct disklabel *)buf)->d_partitions[i].p_fstype,
(int)((struct disklabel *)buf)->d_partitions[i].p_size,
(int)((struct disklabel *)buf)->d_partitions[i].p_offset);
#endif
/* Fill in disklabel */
return (getdisklabel(buf, label));
}
|