boot - Hack workarounds for chromebook 'SeaBIOS' issues
[dragonfly.git] / sys / boot / pc32 / libi386 / biosdisk.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
c7e270b1 26 * $FreeBSD: src/sys/boot/i386/libi386/biosdisk.c,v 1.45 2004/09/21 06:46:44 wes Exp $
984263bc
MD
27 */
28
29/*
30 * BIOS disk device handling.
31 *
32 * Ideas and algorithms from:
33 *
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
36 *
37 */
38
39#include <stand.h>
40
2b961883 41#include <sys/disklabel32.h>
6080181b 42#include <sys/disklabel64.h>
5ee58eed 43#include <sys/diskmbr.h>
ba0cc1ab 44#include <sys/dtype.h>
5ee58eed 45#include <machine/bootinfo.h>
984263bc
MD
46
47#include <stdarg.h>
48
49#include <bootstrap.h>
50#include <btxv86.h>
51#include "libi386.h"
52
53#define BIOS_NUMDRIVES 0x475
54#define BIOSDISK_SECSIZE 512
6080181b 55#define BUFSIZE (4 * BIOSDISK_SECSIZE)
984263bc
MD
56#define MAXBDDEV MAXDEV
57
5ee58eed
MD
58#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
59#define WDMAJOR 0 /* major numbers for devices we frontend for */
60#define WFDMAJOR 1
61#define FDMAJOR 2
62#define DAMAJOR 4
984263bc
MD
63
64#ifdef DISK_DEBUG
5ee58eed 65# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
984263bc
MD
66#else
67# define DEBUG(fmt, args...)
68#endif
69
70struct open_disk {
71 int od_dkunit; /* disk unit number */
72 int od_unit; /* BIOS unit number */
73 int od_cyl; /* BIOS geometry */
74 int od_hds;
75 int od_sec;
76 int od_boff; /* block offset from beginning of BIOS disk */
77 int od_flags;
78#define BD_MODEINT13 0x0000
79#define BD_MODEEDD1 0x0001
80#define BD_MODEEDD3 0x0002
81#define BD_MODEMASK 0x0003
82#define BD_FLOPPY 0x0004
83#define BD_LABELOK 0x0008
84#define BD_PARTTABOK 0x0010
6080181b
SS
85 union {
86 struct disklabel32 od_disklabel;
87 struct disklabel64 od_disklabel64;
88 };
984263bc 89 int od_nslices; /* slice count */
5ee58eed 90 struct dos_partition od_slicetab[NEXTDOSPART];
984263bc
MD
91};
92
93/*
94 * List of BIOS devices, translation from disk unit number to
95 * BIOS unit number.
96 */
97static struct bdinfo
98{
99 int bd_unit; /* BIOS unit number */
100 int bd_flags;
101 int bd_type; /* BIOS 'drive type' (floppy only) */
102} bdinfo [MAXBDDEV];
103static int nbdinfo = 0;
104
105static int bd_getgeom(struct open_disk *od);
106static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
107 caddr_t dest);
5ee58eed
MD
108static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
109 caddr_t dest);
984263bc
MD
110
111static int bd_int13probe(struct bdinfo *bd);
112
113static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
114 char *prefix, int verbose);
115static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
116 char *prefix, int verbose);
117
118static int bd_init(void);
119static int bd_strategy(void *devdata, int flag, daddr_t dblk,
120 size_t size, char *buf, size_t *rsize);
121static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
122 size_t size, char *buf, size_t *rsize);
123static int bd_open(struct open_file *f, ...);
124static int bd_close(struct open_file *f);
125static void bd_print(int verbose);
126
127struct devsw biosdisk = {
128 "disk",
129 DEVT_DISK,
130 bd_init,
131 bd_strategy,
132 bd_open,
133 bd_close,
134 noioctl,
135 bd_print,
136 NULL
137};
138
139static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
140static void bd_closedisk(struct open_disk *od);
141static int bd_bestslice(struct open_disk *od);
7755d172 142static void bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset);
984263bc
MD
143
144/*
145 * Translate between BIOS device numbers and our private unit numbers.
146 */
147int
148bd_bios2unit(int biosdev)
149{
150 int i;
151
152 DEBUG("looking for bios device 0x%x", biosdev);
153 for (i = 0; i < nbdinfo; i++) {
154 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
155 if (bdinfo[i].bd_unit == biosdev)
156 return(i);
157 }
158 return(-1);
159}
160
161int
162bd_unit2bios(int unit)
163{
164 if ((unit >= 0) && (unit < nbdinfo))
165 return(bdinfo[unit].bd_unit);
166 return(-1);
167}
168
169/*
170 * Quiz the BIOS for disk devices, save a little info about them.
171 */
172static int
173bd_init(void)
174{
175 int base, unit, nfd = 0;
176
177 /* sequence 0, 0x80 */
178 for (base = 0; base <= 0x80; base += 0x80) {
179 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
180 /* check the BIOS equipment list for number of fixed disks */
181 if((base == 0x80) &&
182 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
183 break;
184
185 bdinfo[nbdinfo].bd_unit = unit;
186 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
187
188 if (!bd_int13probe(&bdinfo[nbdinfo]))
189 break;
190
191 /* XXX we need "disk aliases" to make this simpler */
192 printf("BIOS drive %c: is disk%d\n",
193 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
194 nbdinfo++;
195 if (base == 0x80)
196 nfd++;
197 }
198 }
199 return(0);
200}
201
202/*
203 * Try to detect a device supported by the legacy int13 BIOS
204 */
205static int
206bd_int13probe(struct bdinfo *bd)
207{
208 v86.ctl = V86_FLAGS;
209 v86.addr = 0x13;
210 v86.eax = 0x800;
211 v86.edx = bd->bd_unit;
212 v86int();
213
214 if (!(v86.efl & 0x1) && /* carry clear */
215 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
c7e270b1
MD
216
217 /*
218 * Ignore devices with an absurd sector size.
219 */
220 if ((v86.ecx & 0x3f) == 0) {
221 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
222 return(0);
223 }
984263bc
MD
224 bd->bd_flags |= BD_MODEINT13;
225 bd->bd_type = v86.ebx & 0xff;
226
227 /* Determine if we can use EDD with this device. */
228 v86.eax = 0x4100;
229 v86.edx = bd->bd_unit;
230 v86.ebx = 0x55aa;
231 v86int();
232 if (!(v86.efl & 0x1) && /* carry clear */
233 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
234 (v86.ecx & 0x1)) { /* packets mode ok */
235 bd->bd_flags |= BD_MODEEDD1;
236 if((v86.eax & 0xff00) > 0x300)
237 bd->bd_flags |= BD_MODEEDD3;
238 }
239 return(1);
240 }
241 return(0);
242}
243
244/*
245 * Print information about disks
246 */
247static void
248bd_print(int verbose)
249{
250 int i, j;
251 char line[80];
252 struct i386_devdesc dev;
253 struct open_disk *od;
254 struct dos_partition *dptr;
255
256 for (i = 0; i < nbdinfo; i++) {
257 sprintf(line, " disk%d: BIOS drive %c:\n", i,
258 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
259 pager_output(line);
260
261 /* try to open the whole disk */
262 dev.d_kind.biosdisk.unit = i;
263 dev.d_kind.biosdisk.slice = -1;
264 dev.d_kind.biosdisk.partition = -1;
265
266 if (!bd_opendisk(&od, &dev)) {
267
268 /* Do we have a partition table? */
269 if (od->od_flags & BD_PARTTABOK) {
270 dptr = &od->od_slicetab[0];
271
272 /* Check for a "dedicated" disk */
0a7482e0
AH
273 if (((dptr[3].dp_typ == DOSPTYP_386BSD) ||
274 (dptr[3].dp_typ == DOSPTYP_NETBSD)) &&
984263bc
MD
275 (dptr[3].dp_start == 0) &&
276 (dptr[3].dp_size == 50000)) {
277 sprintf(line, " disk%d", i);
278 bd_printbsdslice(od, 0, line, verbose);
279 } else {
280 for (j = 0; j < od->od_nslices; j++) {
281 sprintf(line, " disk%ds%d", i, j + 1);
282 bd_printslice(od, &dptr[j], line, verbose);
283 }
284 }
285 }
286 bd_closedisk(od);
287 }
288 }
289}
290
291/*
292 * Print information about slices on a disk. For the size calculations we
293 * assume a 512 byte sector.
294 */
295static void
296bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
297 int verbose)
298{
299 char line[80];
300
301 switch (dp->dp_typ) {
302 case DOSPTYP_386BSD:
0a7482e0
AH
303 case DOSPTYP_NETBSD:
304 /* XXX: possibly add types 0 and 1, as in subr_disk, for gpt magic */
984263bc
MD
305 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
306 return;
307 case DOSPTYP_LINSWP:
308 if (verbose)
309 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
310 prefix, dp->dp_size / 2048,
311 dp->dp_start, dp->dp_start + dp->dp_size);
312 else
313 sprintf(line, "%s: Linux swap\n", prefix);
314 break;
315 case DOSPTYP_LINUX:
316 /*
317 * XXX
318 * read the superblock to confirm this is an ext2fs partition?
319 */
320 if (verbose)
321 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
322 dp->dp_size / 2048, dp->dp_start,
323 dp->dp_start + dp->dp_size);
324 else
325 sprintf(line, "%s: ext2fs\n", prefix);
326 break;
327 case 0x00: /* unused partition */
328 case DOSPTYP_EXT:
329 return;
330 case 0x01:
331 if (verbose)
332 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
333 dp->dp_size / 2048, dp->dp_start,
334 dp->dp_start + dp->dp_size);
335 else
336 sprintf(line, "%s: FAT-12\n", prefix);
337 break;
338 case 0x04:
339 case 0x06:
340 case 0x0e:
341 if (verbose)
342 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
343 dp->dp_size / 2048, dp->dp_start,
344 dp->dp_start + dp->dp_size);
345 else
346 sprintf(line, "%s: FAT-16\n", prefix);
347 break;
348 case 0x0b:
349 case 0x0c:
350 if (verbose)
351 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
352 dp->dp_size / 2048, dp->dp_start,
353 dp->dp_start + dp->dp_size);
354 else
355 sprintf(line, "%s: FAT-32\n", prefix);
356 break;
357 default:
358 if (verbose)
359 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
360 prefix, dp->dp_typ, dp->dp_size / 2048,
361 dp->dp_start, dp->dp_start + dp->dp_size);
362 else
363 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
364 dp->dp_typ);
365 }
366 pager_output(line);
367}
368
984263bc 369static void
0a7482e0
AH
370print_partition(u_int8_t fstype, unsigned long long offset,
371 unsigned long long size, int i, int od_flags,
372 char *prefix, int verbose, int type)
984263bc 373{
0a7482e0
AH
374 char line[80];
375
984263bc
MD
376 /*
377 * For each partition, make sure we know what type of fs it is. If
378 * not, then skip it. However, since floppies often have bogus
379 * fstypes, print the 'a' partition on a floppy even if it is marked
380 * unused.
381 */
0a7482e0
AH
382 if ((fstype == FS_SWAP) ||
383 (fstype == FS_VINUM) ||
384 (fstype == FS_HAMMER) ||
cdfd0a3e 385 (fstype == FS_HAMMER2) ||
0a7482e0
AH
386 (fstype == FS_BSDFFS) ||
387 (fstype == FS_ZFS) ||
388 (fstype == FS_JFS2) ||
389 ((fstype == FS_UNUSED) &&
390 (od_flags & BD_FLOPPY) && (i == 0))) {
391
392 /* Only print out statistics in verbose mode */
393 if (verbose) {
4f6dc91b
AH
394 sprintf(line, "%c %s%c: %s %.6lluMB (%llu - %llu)\n",
395 /* prefix disks that can be used to load modules with '*' */
396 ((fstype == FS_BSDFFS) || (fstype == FS_UNUSED) ||
397 (fstype == FS_VINUM)) ? '*' : ' ',
0a7482e0
AH
398 prefix, 'a' + i,
399 (fstype == FS_SWAP) ? "swap" :
400 (fstype == FS_VINUM) ? "vinum" :
401 (fstype == FS_HAMMER) ? "HAMMER" :
cdfd0a3e 402 (fstype == FS_HAMMER2) ? "HAMMER2" :
0a7482e0
AH
403 (fstype == FS_JFS2) ? "JFS2" :
404 (fstype == FS_ZFS) ? "ZFS" :
405 (fstype == FS_BSDFFS) ? "FFS" :
406 "(unknown)",
407 (type==32)?(size / 2048):(size/1024/1024),
408 offset,
409 offset + size);
410 } else {
4f6dc91b
AH
411 sprintf(line, "%c %s%c: %s\n",
412 /* prefix disks that can be used to load modules with '*' */
413 ((fstype == FS_BSDFFS) || (fstype == FS_UNUSED) ||
414 (fstype == FS_VINUM)) ? '*' : ' ',
415 prefix, 'a' + i,
0a7482e0
AH
416 (fstype == FS_SWAP) ? "swap" :
417 (fstype == FS_VINUM) ? "vinum" :
418 (fstype == FS_HAMMER) ? "HAMMER" :
cdfd0a3e 419 (fstype == FS_HAMMER2) ? "HAMMER2" :
0a7482e0
AH
420 (fstype == FS_JFS2) ? "JFS2" :
421 (fstype == FS_ZFS) ? "ZFS" :
422 (fstype == FS_BSDFFS) ? "FFS" :
423 "(unknown)");
424 }
425
426 pager_output(line);
427 }
428}
429
430/*
431 * Print out each valid partition in the disklabel of a FreeBSD slice.
432 * For size calculations, we assume a 512 byte sector size.
433 */
434static void
435bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
436 int verbose)
437{
438 char line[80];
439 char buf[BIOSDISK_SECSIZE*2];
440 struct disklabel32 *lp = NULL;
441 struct disklabel64 *lp64 = NULL;
442 int i;
443
444 /* read disklabel */
445 if (bd_read(od, offset + LABELSECTOR32, 1, buf))
446 return;
447 lp =(struct disklabel32 *)(&buf[0]);
448 if (lp->d_magic != DISKMAGIC32) {
449 if (bd_read(od, offset, 2, buf))
450 return;
451
452 lp64 =(struct disklabel64 *)(&buf[0]);
453 if (lp64->d_magic != DISKMAGIC64) {
454 sprintf(line, "%s: bad disklabel\n", prefix);
455 pager_output(line);
456 return;
457 }
458 lp = NULL; /* PARANOID */
459 }
460 if (lp64) {
461 /* We got ourselves a disklabel64 here */
462 for (i = 0; i < lp64->d_npartitions; i++) {
463 if (lp64->d_partitions[i].p_bsize == 0)
464 continue;
465
466 print_partition(lp64->d_partitions[i].p_fstype,
467 lp64->d_partitions[i].p_boffset,
468 lp64->d_partitions[i].p_bsize,
469 i, od->od_flags, prefix, verbose, 64);
470 }
471 } else if (lp) {
472 /* Print partitions */
473 for (i = 0; i < lp->d_npartitions; i++) {
474 print_partition(lp->d_partitions[i].p_fstype,
475 lp->d_partitions[i].p_offset,
476 lp->d_partitions[i].p_size,
477 i, od->od_flags, prefix, verbose, 32);
478 }
984263bc 479 }
984263bc
MD
480}
481
482
483/*
484 * Attempt to open the disk described by (dev) for use by (f).
485 *
486 * Note that the philosophy here is "give them exactly what
487 * they ask for". This is necessary because being too "smart"
488 * about what the user might want leads to complications.
489 * (eg. given no slice or partition value, with a disk that is
490 * sliced - are they after the first BSD slice, or the DOS
491 * slice before it?)
492 */
493static int
494bd_open(struct open_file *f, ...)
495{
5ee58eed 496 va_list ap;
984263bc
MD
497 struct i386_devdesc *dev;
498 struct open_disk *od;
499 int error;
500
5ee58eed
MD
501 va_start(ap, f);
502 dev = va_arg(ap, struct i386_devdesc *);
503 va_end(ap);
984263bc
MD
504 if ((error = bd_opendisk(&od, dev)))
505 return(error);
506
507 /*
508 * Save our context
509 */
510 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
511 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
512 return(0);
513}
514
515static int
516bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
517{
518 struct dos_partition *dptr;
2b961883 519 struct disklabel32 *lp;
6080181b 520 struct disklabel64 *lp64;
984263bc
MD
521 struct open_disk *od;
522 int sector, slice, i;
523 int error;
6080181b 524 static char buf[BUFSIZE];
984263bc
MD
525
526 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
527 DEBUG("attempt to open nonexistent disk");
528 return(ENXIO);
529 }
530
531 od = (struct open_disk *)malloc(sizeof(struct open_disk));
532 if (!od) {
533 DEBUG("no memory");
534 return (ENOMEM);
535 }
536
537 /* Look up BIOS unit number, intialise open_disk structure */
538 od->od_dkunit = dev->d_kind.biosdisk.unit;
539 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
540 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
541 od->od_boff = 0;
542 od->od_nslices = 0;
543 error = 0;
544 DEBUG("open '%s', unit 0x%x slice %d partition %c",
545 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
546 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
547
548 /* Get geometry for this open (removable device may have changed) */
549 if (bd_getgeom(od)) {
550 DEBUG("can't get geometry");
551 error = ENXIO;
552 goto out;
553 }
554
555 /*
556 * Following calculations attempt to determine the correct value
557 * for d->od_boff by looking for the slice and partition specified,
558 * or searching for reasonable defaults.
559 */
560
561 /*
562 * Find the slice in the DOS slice table.
563 */
564 if (bd_read(od, 0, 1, buf)) {
565 DEBUG("error reading MBR");
566 error = EIO;
567 goto out;
568 }
569
570 /*
571 * Check the slice table magic.
572 */
573 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
574 /* If a slice number was explicitly supplied, this is an error */
575 if (dev->d_kind.biosdisk.slice > 0) {
576 DEBUG("no slice table/MBR (no magic)");
577 error = ENOENT;
578 goto out;
579 }
580 sector = 0;
581 goto unsliced; /* may be a floppy */
582 }
583
584 /*
7755d172
MD
585 * copy the partition table, then pick up any extended partitions. The
586 * base partition table always has four entries, even if some of them
587 * represented extended partitions. However, any additional sub-extended
588 * partitions will be silently recursed and not included in the slice
589 * table.
984263bc
MD
590 */
591 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
592 sizeof(struct dos_partition) * NDOSPART);
7755d172
MD
593 od->od_nslices = NDOSPART;
594
595 dptr = &od->od_slicetab[0];
596 for (i = 0; i < NDOSPART; i++, dptr++) {
597 if ((dptr->dp_typ == DOSPTYP_EXT) || (dptr->dp_typ == DOSPTYP_EXTLBA))
598 bd_chainextended(od, dptr->dp_start, 0); /* 1st offset is zero */
599 }
984263bc
MD
600 od->od_flags |= BD_PARTTABOK;
601 dptr = &od->od_slicetab[0];
602
7755d172
MD
603 /*
604 * Overflow entries are not loaded into memory but we still keep
605 * track of the count. Fix it up now.
606 */
607 if (od->od_nslices > NEXTDOSPART)
608 od->od_nslices = NEXTDOSPART;
609
610 /*
611 * Is this a request for the whole disk?
612 */
984263bc
MD
613 if (dev->d_kind.biosdisk.slice == -1) {
614 sector = 0;
615 goto unsliced;
616 }
617
618 /*
619 * if a slice number was supplied but not found, this is an error.
620 */
621 if (dev->d_kind.biosdisk.slice > 0) {
622 slice = dev->d_kind.biosdisk.slice - 1;
623 if (slice >= od->od_nslices) {
624 DEBUG("slice %d not found", slice);
625 error = ENOENT;
626 goto out;
627 }
628 }
629
630 /*
631 * Check for the historically bogus MBR found on true dedicated disks
632 */
633 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
634 (dptr[3].dp_start == 0) &&
635 (dptr[3].dp_size == 50000)) {
636 sector = 0;
637 goto unsliced;
638 }
639
640 /* Try to auto-detect the best slice; this should always give a slice number */
641 if (dev->d_kind.biosdisk.slice == 0) {
642 slice = bd_bestslice(od);
643 if (slice == -1) {
644 error = ENOENT;
645 goto out;
646 }
647 dev->d_kind.biosdisk.slice = slice;
648 }
649
650 dptr = &od->od_slicetab[0];
651 /*
652 * Accept the supplied slice number unequivocally (we may be looking
653 * at a DOS partition).
654 */
655 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
656 sector = dptr->dp_start;
657 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
658
659 /*
660 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
661 */
662 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
663 dev->d_kind.biosdisk.partition = 0;
664
8f706258 665unsliced:
984263bc
MD
666 /*
667 * Now we have the slice offset, look for the partition in the disklabel if we have
668 * a partition to start with.
669 *
670 * XXX we might want to check the label checksum.
671 */
672 if (dev->d_kind.biosdisk.partition < 0) {
673 od->od_boff = sector; /* no partition, must be after the slice */
674 DEBUG("opening raw slice");
675 } else {
676
2b961883 677 if (bd_read(od, sector + LABELSECTOR32, 1, buf)) {
984263bc
MD
678 DEBUG("error reading disklabel");
679 error = EIO;
680 goto out;
681 }
2b961883
MD
682 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel32), buf + LABELOFFSET32, &od->od_disklabel);
683 bcopy(buf + LABELOFFSET32, &od->od_disklabel, sizeof(struct disklabel32));
984263bc 684 lp = &od->od_disklabel;
984263bc 685
6080181b
SS
686 if (lp->d_magic == DISKMAGIC32) {
687 od->od_flags |= BD_LABELOK;
688
689 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
690 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
691 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
692 error = EPART;
693 goto out;
694
695 }
696
697#ifdef DISK_DEBUG
698 /* Complain if the partition is unused unless this is a floppy. */
699 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
700 !(od->od_flags & BD_FLOPPY))
701 DEBUG("warning, partition marked as unused");
702#endif
703
704 od->od_boff =
705 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
706 lp->d_partitions[RAW_PART].p_offset +
707 sector;
708
984263bc
MD
709 goto out;
710 }
984263bc 711
6080181b
SS
712 /* else maybe DISKLABEL64? */
713
714 if (bd_read(od, sector, (sizeof(od->od_disklabel64) + 511) / 512, buf)) {
715 DEBUG("error reading disklabel");
716 error = EIO;
717 goto out;
984263bc 718 }
6080181b
SS
719 DEBUG("copy %d bytes of label from %p to %p", sizeof(od->od_disklabel64), buf, &od->od_disklabel64);
720 bcopy(buf, &od->od_disklabel64, sizeof(od->od_disklabel64));
721 lp64 = &od->od_disklabel64;
984263bc 722
6080181b
SS
723 if (lp64->d_magic == DISKMAGIC64) {
724 od->od_flags |= BD_LABELOK;
725
726 if (dev->d_kind.biosdisk.partition >= lp64->d_npartitions ||
727 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_bsize == 0) {
728 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
729 'a' + dev->d_kind.biosdisk.partition, 'a' + lp64->d_npartitions);
730 error = EPART;
731 goto out;
732
733 }
734
735 od->od_boff =
736 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_boffset / 512 +
737 sector;
738
739 DEBUG("disklabel64 slice at %d", od->od_boff);
740
741 } else {
742 DEBUG("no disklabel");
743 error = ENOENT;
744 }
984263bc 745 }
6080181b 746
984263bc
MD
747 out:
748 if (error) {
749 free(od);
750 } else {
751 *odp = od; /* return the open disk */
752 }
753 return(error);
754}
755
7755d172 756
984263bc 757static void
7755d172 758bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset)
984263bc 759{
7755d172
MD
760 char buf[BIOSDISK_SECSIZE];
761 struct dos_partition *dp1, *dp2;
762 int i;
763
764 if (bd_read(od, (daddr_t)(base + offset), 1, buf)) {
765 printf("\nerror reading extended partition table");
766 return;
984263bc 767 }
7755d172
MD
768
769 /*
770 * dp1 points to the first record in the on-disk XPT,
771 * dp2 points to the next entry in the in-memory parition table.
772 *
773 * NOTE: dp2 may be out of bounds if od_nslices >= NEXTDOSPART.
774 *
775 * NOTE: unlike the extended partitions in our primary dos partition
776 * table, we do not record recursed extended partitions themselves
777 * in our in-memory partition table.
778 *
779 * NOTE: recording within our in-memory table must be breadth first
780 * ot match what the kernel does. Thus, two passes are required.
781 *
782 * NOTE: partitioning programs which support extended partitions seem
783 * to always use the first entry for the user partition and the
784 * second entry to chain, and also appear to disallow more then one
785 * extended partition at each level. Nevertheless we make our code
786 * somewhat more generic (and the same as the kernel's own slice
787 * scan).
788 */
789 dp1 = (struct dos_partition *)(&buf[DOSPARTOFF]);
790 dp2 = &od->od_slicetab[od->od_nslices];
791
792 for (i = 0; i < NDOSPART; ++i, ++dp1) {
793 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
794 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
795 dp1->dp_size == 0) {
984263bc 796 continue;
7755d172
MD
797 }
798 if ((dp1->dp_typ == DOSPTYP_EXT) ||
799 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
800 /*
801 * breadth first traversal, must skip in the
802 * first pass
803 */
804 continue;
805 }
806
807 /*
808 * Only load up the in-memory data if we haven't overflowed
809 * our in-memory array.
810 */
811 if (od->od_nslices < NEXTDOSPART) {
812 dp2->dp_typ = dp1->dp_typ;
813 dp2->dp_start = base + offset + dp1->dp_start;
814 dp2->dp_size = dp1->dp_size;
815 }
816 ++od->od_nslices;
817 ++dp2;
984263bc 818 }
984263bc
MD
819
820 /*
7755d172
MD
821 * Pass 2 - handle extended partitions. Note that the extended
822 * slice itself is not stored in the slice array when we recurse,
823 * but any 'original' top-level extended slices are. This is to
824 * match what the kernel does.
984263bc 825 */
7755d172
MD
826 dp1 -= NDOSPART;
827 for (i = 0; i < NDOSPART; ++i, ++dp1) {
828 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
829 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
830 dp1->dp_size == 0) {
831 continue;
832 }
833 if ((dp1->dp_typ == DOSPTYP_EXT) ||
834 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
835 bd_chainextended(od, base, dp1->dp_start);
836 continue;
837 }
838 }
984263bc
MD
839}
840
841/*
842 * Search for a slice with the following preferences:
843 *
844 * 1: Active FreeBSD slice
845 * 2: Non-active FreeBSD slice
846 * 3: Active Linux slice
847 * 4: non-active Linux slice
848 * 5: Active FAT/FAT32 slice
849 * 6: non-active FAT/FAT32 slice
850 */
851#define PREF_RAWDISK 0
852#define PREF_FBSD_ACT 1
853#define PREF_FBSD 2
854#define PREF_LINUX_ACT 3
855#define PREF_LINUX 4
856#define PREF_DOS_ACT 5
857#define PREF_DOS 6
858#define PREF_NONE 7
859
860/*
861 * slicelimit is in the range 0 .. NDOSPART
862 */
863static int
864bd_bestslice(struct open_disk *od)
865{
866 struct dos_partition *dp;
867 int pref, preflevel;
868 int i, prefslice;
869
870 prefslice = 0;
871 preflevel = PREF_NONE;
872
873 dp = &od->od_slicetab[0];
874 for (i = 0; i < od->od_nslices; i++, dp++) {
984263bc
MD
875 switch (dp->dp_typ) {
876 case DOSPTYP_386BSD: /* FreeBSD */
877 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
878 break;
879
880 case DOSPTYP_LINUX:
881 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
882 break;
883
884 case 0x01: /* DOS/Windows */
885 case 0x04:
886 case 0x06:
887 case 0x0b:
888 case 0x0c:
889 case 0x0e:
890 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
891 break;
892
893 default:
894 pref = PREF_NONE;
895 }
896 if (pref < preflevel) {
897 preflevel = pref;
898 prefslice = i + 1;
899 }
900 }
901 return (prefslice);
902}
903
904static int
905bd_close(struct open_file *f)
906{
907 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
908
909 bd_closedisk(od);
910 return(0);
911}
912
913static void
914bd_closedisk(struct open_disk *od)
915{
916 DEBUG("open_disk %p", od);
917#if 0
918 /* XXX is this required? (especially if disk already open...) */
919 if (od->od_flags & BD_FLOPPY)
920 delay(3000000);
921#endif
922 free(od);
923}
924
925static int
926bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
927{
928 struct bcache_devdata bcd;
929 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
930
931 bcd.dv_strategy = bd_realstrategy;
932 bcd.dv_devdata = devdata;
933 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
934}
935
936static int
937bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
938{
939 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
940 int blks;
941#ifdef BD_SUPPORT_FRAGS
942 char fragbuf[BIOSDISK_SECSIZE];
943 size_t fragsize;
944
945 fragsize = size % BIOSDISK_SECSIZE;
946#else
947 if (size % BIOSDISK_SECSIZE)
948 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
949#endif
950
951 DEBUG("open_disk %p", od);
952
5644a38d
MD
953 switch(rw){
954 case F_READ:
955 blks = size / BIOSDISK_SECSIZE;
956 DEBUG("read %d from %d to %p", blks, dblk, buf);
984263bc 957
5644a38d
MD
958 if (rsize)
959 *rsize = 0;
960 if (blks && bd_read(od, dblk, blks, buf)) {
961 DEBUG("read error");
962 return (EIO);
963 }
984263bc 964#ifdef BD_SUPPORT_FRAGS
5644a38d 965 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
984263bc 966 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
5644a38d
MD
967 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
968 DEBUG("frag read error");
969 return(EIO);
970 }
971 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
984263bc 972#endif
5644a38d
MD
973 if (rsize)
974 *rsize = size;
975 return (0);
976 case F_WRITE :
977 blks = size / BIOSDISK_SECSIZE;
978 DEBUG("write %d from %d to %p", blks, dblk, buf);
979
980 if (rsize)
981 *rsize = 0;
982 if (blks && bd_write(od, dblk, blks, buf)) {
983 DEBUG("write error");
984 return (EIO);
985 }
5ee58eed
MD
986#ifdef BD_SUPPORT_FRAGS
987 if(fragsize) {
5644a38d
MD
988 DEBUG("Attempted to write a frag");
989 return (EIO);
5ee58eed
MD
990 }
991#endif
5644a38d
MD
992 if (rsize)
993 *rsize = size;
994 return (0);
995 default:
996 break; /* DO NOTHING */
997 }
998 return EROFS;
984263bc
MD
999}
1000
1001/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
1002#define FLOPPY_BOUNCEBUF 18
1003
1004static int
1005bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1006{
1007 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1008 caddr_t p, xp, bbuf, breg;
1009
1010 /* Just in case some idiot actually tries to read -1 blocks... */
1011 if (blks < 0)
1012 return (-1);
1013
1014 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1015 resid = blks;
1016 p = dest;
1017
5fc9dc6d
MD
1018 /*
1019 * Decide whether we have to bounce.
1020 *
1021 * We have to bounce if the destination buffer is not segment addressable
1022 * or if it crosses a 64KB boundary.
1023 */
984263bc 1024 if ((od->od_unit < 0x80) &&
5fc9dc6d
MD
1025 (VTOP(dest + blks * BIOSDISK_SECSIZE) >= 16384 * 1024 ||
1026 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16)))
1027 ) {
984263bc
MD
1028
1029 /*
5fc9dc6d
MD
1030 * There is a 64k physical boundary somewhere in the destination
1031 * buffer, so we have to arrange a suitable bounce buffer. Allocate
1032 * a buffer twice as large as we need to. Use the bottom half unless
1033 * there is a break there, in which case we use the top half.
984263bc
MD
1034 */
1035 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1036 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
5fc9dc6d
MD
1037 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
1038 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
984263bc
MD
1039 breg = bbuf;
1040 } else {
1041 breg = bbuf + x * BIOSDISK_SECSIZE;
1042 }
5fc9dc6d 1043 maxfer = x; /* limit transfers to bounce region size */
984263bc
MD
1044 } else {
1045 breg = bbuf = NULL;
1046 maxfer = 0;
1047 }
1048
1049 while (resid > 0) {
1050 x = dblk;
1051 cyl = x / bpc; /* block # / blocks per cylinder */
1052 x %= bpc; /* block offset into cylinder */
1053 hd = x / od->od_sec; /* offset / blocks per track */
1054 sec = x % od->od_sec; /* offset into track */
1055
1056 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
e54488bb 1057 x = szmin(od->od_sec - sec, resid);
984263bc
MD
1058 if (maxfer > 0)
1059 x = min(x, maxfer); /* fit bounce buffer */
1060
1061 /* where do we transfer to? */
1062 xp = bbuf == NULL ? p : breg;
1063
1064 /* correct sector number for 1-based BIOS numbering */
1065 sec++;
1066
ed987dc9
MD
1067 /*
1068 * Loop retrying the operation a couple of times. The BIOS may also
1069 * retry.
1070 */
984263bc 1071 for (retry = 0; retry < 3; retry++) {
ed987dc9
MD
1072 /*
1073 * If retrying, reset the drive.
1074 */
984263bc
MD
1075 if (retry > 0) {
1076 v86.ctl = V86_FLAGS;
1077 v86.addr = 0x13;
1078 v86.eax = 0;
1079 v86.edx = od->od_unit;
1080 v86int();
1081 }
1082
ed987dc9
MD
1083 /*
1084 * Always use EDD if the disk supports it, otherwise fall back
1085 * to CHS mode (returning an error if the cylinder number is
1086 * too large).
1087 */
1088 if (od->od_flags & BD_MODEEDD1) {
1089 static unsigned short packet[8];
1090
1091 packet[0] = 0x10;
1092 packet[1] = x;
1093 packet[2] = VTOPOFF(xp);
1094 packet[3] = VTOPSEG(xp);
1095 packet[4] = dblk & 0xffff;
1096 packet[5] = dblk >> 16;
1097 packet[6] = 0;
1098 packet[7] = 0;
1099 v86.ctl = V86_FLAGS;
1100 v86.addr = 0x13;
1101 v86.eax = 0x4200;
1102 v86.edx = od->od_unit;
1103 v86.ds = VTOPSEG(packet);
1104 v86.esi = VTOPOFF(packet);
1105 v86int();
1106 result = (v86.efl & 0x1);
1107 if (result == 0)
984263bc 1108 break;
ed987dc9 1109 } else if (cyl < 1024) {
984263bc
MD
1110 /* Use normal CHS addressing */
1111 v86.ctl = V86_FLAGS;
1112 v86.addr = 0x13;
1113 v86.eax = 0x200 | x;
1114 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1115 v86.edx = (hd << 8) | od->od_unit;
1116 v86.es = VTOPSEG(xp);
1117 v86.ebx = VTOPOFF(xp);
1118 v86int();
1119 result = (v86.efl & 0x1);
1120 if (result == 0)
ed987dc9
MD
1121 break;
1122 } else {
1123 result = 1;
1124 break;
984263bc
MD
1125 }
1126 }
1127
e451885f 1128 DEBUG("%u sectors from %u/%u/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
984263bc
MD
1129 /* BUG here, cannot use v86 in printf because putchar uses it too */
1130 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1131 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1132 if (result) {
1133 if (bbuf != NULL)
1134 free(bbuf);
1135 return(-1);
1136 }
1137 if (bbuf != NULL)
1138 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1139 p += (x * BIOSDISK_SECSIZE);
1140 dblk += x;
1141 resid -= x;
1142 }
1143
1144/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1145 if (bbuf != NULL)
1146 free(bbuf);
1147 return(0);
1148}
1149
5ee58eed
MD
1150
1151static int
1152bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1153{
1154 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1155 caddr_t p, xp, bbuf, breg;
1156
1157 /* Just in case some idiot actually tries to read -1 blocks... */
1158 if (blks < 0)
1159 return (-1);
1160
1161 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1162 resid = blks;
1163 p = dest;
1164
1165 /* Decide whether we have to bounce */
1166 if ((od->od_unit < 0x80) &&
1167 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1168
1169 /*
5fc9dc6d
MD
1170 * There is a 64k physical boundary somewhere in the destination
1171 * buffer, so we have to arrange a suitable bounce buffer. Allocate
1172 * a buffer twice as large as we need to. Use the bottom half
1173 * unless there is a break there, in which case we use the top half.
5ee58eed
MD
1174 */
1175
1176 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1177 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
c7e270b1 1178 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
5ee58eed
MD
1179 breg = bbuf;
1180 } else {
1181 breg = bbuf + x * BIOSDISK_SECSIZE;
1182 }
1183 maxfer = x; /* limit transfers to bounce region size */
1184 } else {
1185 breg = bbuf = NULL;
1186 maxfer = 0;
1187 }
1188
1189 while (resid > 0) {
1190 x = dblk;
1191 cyl = x / bpc; /* block # / blocks per cylinder */
1192 x %= bpc; /* block offset into cylinder */
1193 hd = x / od->od_sec; /* offset / blocks per track */
1194 sec = x % od->od_sec; /* offset into track */
1195
1196 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
e54488bb 1197 x = szmin(od->od_sec - sec, resid);
5ee58eed 1198 if (maxfer > 0)
e54488bb 1199 x = szmin(x, maxfer); /* fit bounce buffer */
5ee58eed
MD
1200
1201 /* where do we transfer to? */
1202 xp = bbuf == NULL ? p : breg;
1203
1204 /* correct sector number for 1-based BIOS numbering */
1205 sec++;
1206
1207
1208 /* Put your Data In, Put your Data out,
1209 Put your Data In, and shake it all about
1210 */
1211 if (bbuf != NULL)
1212 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1213 p += (x * BIOSDISK_SECSIZE);
1214 dblk += x;
1215 resid -= x;
1216
ed987dc9
MD
1217 /*
1218 * Loop retrying the operation a couple of times. The BIOS may also
1219 * retry.
1220 */
5ee58eed 1221 for (retry = 0; retry < 3; retry++) {
ed987dc9
MD
1222 /*
1223 * If retrying, reset the drive.
1224 */
5ee58eed
MD
1225 if (retry > 0) {
1226 v86.ctl = V86_FLAGS;
1227 v86.addr = 0x13;
1228 v86.eax = 0;
1229 v86.edx = od->od_unit;
1230 v86int();
1231 }
ed987dc9
MD
1232
1233 /*
1234 * Always use EDD if the disk supports it, otherwise fall back
1235 * to CHS mode (returning an error if the cylinder number is
1236 * too large).
1237 */
1238 if (od->od_flags & BD_MODEEDD1) {
1239 static unsigned short packet[8];
1240
1241 packet[0] = 0x10;
1242 packet[1] = x;
1243 packet[2] = VTOPOFF(xp);
1244 packet[3] = VTOPSEG(xp);
1245 packet[4] = dblk & 0xffff;
1246 packet[5] = dblk >> 16;
1247 packet[6] = 0;
1248 packet[7] = 0;
1249 v86.ctl = V86_FLAGS;
1250 v86.addr = 0x13;
1251 /* Should we Write with verify ?? 0x4302 ? */
1252 v86.eax = 0x4300;
1253 v86.edx = od->od_unit;
1254 v86.ds = VTOPSEG(packet);
1255 v86.esi = VTOPOFF(packet);
1256 v86int();
1257 result = (v86.efl & 0x1);
1258 if (result == 0)
5ee58eed 1259 break;
ed987dc9 1260 } else if (cyl < 1024) {
5ee58eed
MD
1261 /* Use normal CHS addressing */
1262 v86.ctl = V86_FLAGS;
1263 v86.addr = 0x13;
1264 v86.eax = 0x300 | x;
1265 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1266 v86.edx = (hd << 8) | od->od_unit;
1267 v86.es = VTOPSEG(xp);
1268 v86.ebx = VTOPOFF(xp);
1269 v86int();
1270 result = (v86.efl & 0x1);
1271 if (result == 0)
ed987dc9
MD
1272 break;
1273 } else {
1274 result = 1;
1275 break;
5ee58eed
MD
1276 }
1277 }
1278
e451885f 1279 DEBUG("%u sectors from %u/%u/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
5ee58eed
MD
1280 /* BUG here, cannot use v86 in printf because putchar uses it too */
1281 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1282 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1283 if (result) {
1284 if (bbuf != NULL)
1285 free(bbuf);
1286 return(-1);
1287 }
1288 }
1289
1290/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1291 if (bbuf != NULL)
1292 free(bbuf);
1293 return(0);
1294}
984263bc
MD
1295static int
1296bd_getgeom(struct open_disk *od)
1297{
1298
1299 v86.ctl = V86_FLAGS;
1300 v86.addr = 0x13;
1301 v86.eax = 0x800;
1302 v86.edx = od->od_unit;
1303 v86int();
1304
1305 if ((v86.efl & 0x1) || /* carry set */
1306 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1307 return(1);
1308
1309 /* convert max cyl # -> # of cylinders */
1310 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1311 /* convert max head # -> # of heads */
1312 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1313 od->od_sec = v86.ecx & 0x3f;
1314
1315 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1316 return(0);
1317}
1318
1319/*
1320 * Return the BIOS geometry of a given "fixed drive" in a format
1321 * suitable for the legacy bootinfo structure. Since the kernel is
1322 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1323 * prefer to get the information directly, rather than rely on being
1324 * able to put it together from information already maintained for
1325 * different purposes and for a probably different number of drives.
1326 *
1327 * For valid drives, the geometry is expected in the format (31..0)
1328 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1329 * indicated by returning the geometry of a "1.2M" PC-format floppy
1330 * disk. And, incidentally, what is returned is not the geometry as
1331 * such but the highest valid cylinder, head, and sector numbers.
1332 */
1333u_int32_t
1334bd_getbigeom(int bunit)
1335{
1336
1337 v86.ctl = V86_FLAGS;
1338 v86.addr = 0x13;
1339 v86.eax = 0x800;
1340 v86.edx = 0x80 + bunit;
1341 v86int();
1342 if (v86.efl & 0x1)
1343 return 0x4f010f;
1344 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1345 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1346}
1347
1348/*
b13267a5 1349 * Return a suitable cdev_t value for (dev).
984263bc
MD
1350 *
1351 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1352 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1353 */
1354int
1355bd_getdev(struct i386_devdesc *dev)
1356{
1357 struct open_disk *od;
1358 int biosdev;
1359 int major;
1360 int rootdev;
1361 char *nip, *cp;
1362 int unitofs = 0, i, unit;
1363
1364 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1365 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1366 if (biosdev == -1) /* not a BIOS device */
1367 return(-1);
1368 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1369 return(-1);
1370
1371 if (biosdev < 0x80) {
1372 /* floppy (or emulated floppy) or ATAPI device */
1373 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1374 /* is an ATAPI disk */
1375 major = WFDMAJOR;
1376 } else {
1377 /* is a floppy disk */
1378 major = FDMAJOR;
1379 }
1380 } else {
1381 /* harddisk */
1382 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1383 /* label OK, disk labelled as SCSI */
1384 major = DAMAJOR;
1385 /* check for unit number correction hint, now deprecated */
1386 if ((nip = getenv("num_ide_disks")) != NULL) {
1387 i = strtol(nip, &cp, 0);
1388 /* check for parse error */
1389 if ((cp != nip) && (*cp == 0))
1390 unitofs = i;
1391 }
1392 } else {
1393 /* assume an IDE disk */
1394 major = WDMAJOR;
1395 }
1396 }
1397 /* default root disk unit number */
1398 unit = (biosdev & 0x7f) - unitofs;
1399
1400 /* XXX a better kludge to set the root disk unit number */
1401 if ((nip = getenv("root_disk_unit")) != NULL) {
1402 i = strtol(nip, &cp, 0);
1403 /* check for parse error */
1404 if ((cp != nip) && (*cp == 0))
1405 unit = i;
1406 }
1407
1408 rootdev = MAKEBOOTDEV(major,
1409 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1410 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1411 unit,
1412 dev->d_kind.biosdisk.partition);
1413 DEBUG("dev is 0x%x\n", rootdev);
1414 return(rootdev);
1415}