9954 loader: always set media size from partition.
[unleashed.git] / usr / src / boot / sys / boot / i386 / libi386 / biosdisk.c
blob30af54ea538bd955f94b64b19cf85466d78562c5
1 /*
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/cdefs.h>
31 * BIOS disk device handling.
33 * Ideas and algorithms from:
35 * - NetBSD libi386/biosdisk.c
36 * - FreeBSD biosboot/disk.c
40 #include <sys/disk.h>
41 #include <sys/limits.h>
42 #include <stand.h>
43 #include <machine/bootinfo.h>
44 #include <stdarg.h>
46 #include <bootstrap.h>
47 #include <btxv86.h>
48 #include <edd.h>
49 #include "disk.h"
50 #include "libi386.h"
52 #define BIOS_NUMDRIVES 0x475
53 #define BIOSDISK_SECSIZE 512
54 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
56 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
57 #define WDMAJOR 0 /* major numbers for devices we frontend for */
58 #define WFDMAJOR 1
59 #define FDMAJOR 2
60 #define DAMAJOR 4
62 #ifdef DISK_DEBUG
63 #define DEBUG(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
64 #else
65 #define DEBUG(fmt, args...)
66 #endif
69 * List of BIOS devices, translation from disk unit number to
70 * BIOS unit number.
72 static struct bdinfo
74 int bd_unit; /* BIOS unit number */
75 int bd_cyl; /* BIOS geometry */
76 int bd_hds;
77 int bd_sec;
78 int bd_flags;
79 #define BD_MODEINT13 0x0000
80 #define BD_MODEEDD1 0x0001
81 #define BD_MODEEDD3 0x0002
82 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
83 #define BD_MODEMASK 0x0003
84 #define BD_FLOPPY 0x0004
85 #define BD_NO_MEDIA 0x0008
86 int bd_type; /* BIOS 'drive type' (floppy only) */
87 uint16_t bd_sectorsize; /* Sector size */
88 uint64_t bd_sectors; /* Disk size */
89 int bd_open; /* reference counter */
90 void *bd_bcache; /* buffer cache data */
91 } bdinfo [MAXBDDEV];
92 static int nbdinfo = 0;
94 #define BD(dev) (bdinfo[(dev)->dd.d_unit])
95 #define BD_RD 0
96 #define BD_WR 1
98 static void bd_io_workaround(struct disk_devdesc *dev);
100 static int bd_io(struct disk_devdesc *, daddr_t, int, caddr_t, int);
101 static int bd_int13probe(struct bdinfo *bd);
103 static int bd_init(void);
104 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
105 char *buf, size_t *rsize);
106 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
107 char *buf, size_t *rsize);
108 static int bd_open(struct open_file *f, ...);
109 static int bd_close(struct open_file *f);
110 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
111 static int bd_print(int verbose);
113 struct devsw biosdisk = {
114 "disk",
115 DEVT_DISK,
116 bd_init,
117 bd_strategy,
118 bd_open,
119 bd_close,
120 bd_ioctl,
121 bd_print,
122 NULL
126 * Translate between BIOS device numbers and our private unit numbers.
129 bd_bios2unit(int biosdev)
131 int i;
133 DEBUG("looking for bios device 0x%x", biosdev);
134 for (i = 0; i < nbdinfo; i++) {
135 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
136 if (bdinfo[i].bd_unit == biosdev)
137 return (i);
139 return (-1);
143 bd_unit2bios(int unit)
146 if ((unit >= 0) && (unit < nbdinfo))
147 return (bdinfo[unit].bd_unit);
148 return (-1);
152 * Quiz the BIOS for disk devices, save a little info about them.
154 static int
155 bd_init(void)
157 int base, unit, nfd = 0;
159 /* sequence 0, 0x80 */
160 for (base = 0; base <= 0x80; base += 0x80) {
161 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
162 #ifndef VIRTUALBOX
164 * Check the BIOS equipment list for number
165 * of fixed disks.
167 if (base == 0x80 &&
168 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
169 break;
170 #endif
171 bdinfo[nbdinfo].bd_open = 0;
172 bdinfo[nbdinfo].bd_bcache = NULL;
173 bdinfo[nbdinfo].bd_unit = unit;
174 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
175 if (!bd_int13probe(&bdinfo[nbdinfo]))
176 break;
178 #ifndef BOOT2
179 /* XXX we need "disk aliases" to make this simpler */
180 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
181 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
182 #endif
183 nbdinfo++;
184 if (base == 0x80)
185 nfd++;
188 bcache_add_dev(nbdinfo);
189 return (0);
193 * Return EDD version or 0 if EDD is not supported on this drive.
195 static int
196 bd_check_extensions(int unit)
198 /* Determine if we can use EDD with this device. */
199 v86.ctl = V86_FLAGS;
200 v86.addr = 0x13;
201 v86.eax = 0x4100;
202 v86.edx = unit;
203 v86.ebx = 0x55aa;
204 v86int();
206 if (V86_CY(v86.efl) || /* carry set */
207 (v86.ebx & 0xffff) != 0xaa55) /* signature */
208 return (0);
210 /* extended disk access functions (AH=42h-44h,47h,48h) supported */
211 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
212 return (0);
214 return ((v86.eax >> 8) & 0xff);
217 static void
218 bd_reset_disk(int unit)
220 /* reset disk */
221 v86.ctl = V86_FLAGS;
222 v86.addr = 0x13;
223 v86.eax = 0;
224 v86.edx = unit;
225 v86int();
229 * Read CHS info. Return 0 on success, error otherwise.
231 static int
232 bd_get_diskinfo_std(struct bdinfo *bd)
234 bzero(&v86, sizeof (v86));
235 v86.ctl = V86_FLAGS;
236 v86.addr = 0x13;
237 v86.eax = 0x800;
238 v86.edx = bd->bd_unit;
239 v86int();
241 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
242 return ((v86.eax & 0xff00) >> 8);
244 /* return custom error on absurd sector number */
245 if ((v86.ecx & 0x3f) == 0)
246 return (0x60);
248 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
249 /* Convert max head # -> # of heads */
250 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
251 bd->bd_sec = v86.ecx & 0x3f;
252 bd->bd_type = v86.ebx;
253 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
255 return (0);
259 * Read EDD info. Return 0 on success, error otherwise.
261 static int
262 bd_get_diskinfo_ext(struct bdinfo *bd)
264 struct edd_params params;
265 uint64_t total;
267 /* Get disk params */
268 bzero(&params, sizeof (params));
269 params.len = sizeof (params);
270 v86.ctl = V86_FLAGS;
271 v86.addr = 0x13;
272 v86.eax = 0x4800;
273 v86.edx = bd->bd_unit;
274 v86.ds = VTOPSEG(&params);
275 v86.esi = VTOPOFF(&params);
276 v86int();
278 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
279 return ((v86.eax & 0xff00) >> 8);
282 * Sector size must be a multiple of 512 bytes.
283 * An alternate test would be to check power of 2,
284 * powerof2(params.sector_size).
285 * 4K is largest read buffer we can use at this time.
287 if (params.sector_size >= 512 &&
288 params.sector_size <= 4096 &&
289 (params.sector_size % BIOSDISK_SECSIZE) == 0)
290 bd->bd_sectorsize = params.sector_size;
292 bd->bd_cyl = params.cylinders;
293 bd->bd_hds = params.heads;
294 bd->bd_sec = params.sectors_per_track;
296 if (params.sectors != 0) {
297 total = params.sectors;
298 } else {
299 total = (uint64_t)params.cylinders *
300 params.heads * params.sectors_per_track;
302 bd->bd_sectors = total;
304 return (0);
308 * Try to detect a device supported by the legacy int13 BIOS
310 static int
311 bd_int13probe(struct bdinfo *bd)
313 int edd;
314 int ret;
316 bd->bd_flags &= ~BD_NO_MEDIA;
318 edd = bd_check_extensions(bd->bd_unit);
319 if (edd == 0)
320 bd->bd_flags |= BD_MODEINT13;
321 else if (edd < 0x30)
322 bd->bd_flags |= BD_MODEEDD1;
323 else
324 bd->bd_flags |= BD_MODEEDD3;
326 /* Default sector size */
327 bd->bd_sectorsize = BIOSDISK_SECSIZE;
330 * Test if the floppy device is present, so we can avoid receiving
331 * bogus information from bd_get_diskinfo_std().
333 if (bd->bd_unit < 0x80) {
334 /* reset disk */
335 bd_reset_disk(bd->bd_unit);
337 /* Get disk type */
338 v86.ctl = V86_FLAGS;
339 v86.addr = 0x13;
340 v86.eax = 0x1500;
341 v86.edx = bd->bd_unit;
342 v86int();
343 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
344 return (0);
347 ret = 1;
348 if (edd != 0)
349 ret = bd_get_diskinfo_ext(bd);
350 if (ret != 0 || bd->bd_sectors == 0)
351 ret = bd_get_diskinfo_std(bd);
353 if (ret != 0 && bd->bd_unit < 0x80) {
354 /* Set defaults for 1.44 floppy */
355 bd->bd_cyl = 80;
356 bd->bd_hds = 2;
357 bd->bd_sec = 18;
358 bd->bd_type = 4;
359 bd->bd_sectors = 2880;
360 /* Since we are there, there most likely is no media */
361 bd->bd_flags |= BD_NO_MEDIA;
362 ret = 0;
365 if (ret != 0) {
366 if (bd->bd_sectors != 0 && edd != 0) {
367 bd->bd_sec = 63;
368 bd->bd_hds = 255;
369 bd->bd_cyl =
370 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
371 bd->bd_sec * bd->bd_hds;
372 } else {
373 printf("Can not get information about %s unit %#x\n",
374 biosdisk.dv_name, bd->bd_unit);
375 return (0);
379 if (bd->bd_sec == 0)
380 bd->bd_sec = 63;
381 if (bd->bd_hds == 0)
382 bd->bd_hds = 255;
384 if (bd->bd_sectors == 0)
385 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
387 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
388 bd->bd_hds, bd->bd_sec);
390 return (1);
394 * Print information about disks
396 static int
397 bd_print(int verbose)
399 static char line[80];
400 struct disk_devdesc dev;
401 int i, ret = 0;
403 if (nbdinfo == 0)
404 return (0);
406 printf("%s devices:", biosdisk.dv_name);
407 if ((ret = pager_output("\n")) != 0)
408 return (ret);
410 for (i = 0; i < nbdinfo; i++) {
411 snprintf(line, sizeof (line),
412 " disk%d: BIOS drive %c (%s%ju X %u):\n", i,
413 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
414 ('C' + bdinfo[i].bd_unit - 0x80),
415 (bdinfo[i].bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
416 "no media, " : "",
417 (uintmax_t)bdinfo[i].bd_sectors,
418 bdinfo[i].bd_sectorsize);
419 if ((ret = pager_output(line)) != 0)
420 break;
422 if ((bdinfo[i].bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
423 continue;
425 dev.dd.d_dev = &biosdisk;
426 dev.dd.d_unit = i;
427 dev.d_slice = -1;
428 dev.d_partition = -1;
429 if (disk_open(&dev,
430 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
431 bdinfo[i].bd_sectorsize) == 0) {
432 snprintf(line, sizeof (line), " disk%d", i);
433 ret = disk_print(&dev, line, verbose);
434 disk_close(&dev);
435 if (ret != 0)
436 break;
439 return (ret);
443 * Read disk size from partition.
444 * This is needed to work around buggy BIOS systems returning
445 * wrong (truncated) disk media size.
446 * During bd_probe() we tested if the multiplication of bd_sectors
447 * would overflow so it should be safe to perform here.
449 static uint64_t
450 bd_disk_get_sectors(struct disk_devdesc *dev)
452 struct disk_devdesc disk;
453 uint64_t size;
455 disk.dd.d_dev = dev->dd.d_dev;
456 disk.dd.d_unit = dev->dd.d_unit;
457 disk.d_slice = -1;
458 disk.d_partition = -1;
459 disk.d_offset = 0;
461 size = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
462 if (disk_open(&disk, size, BD(dev).bd_sectorsize) == 0) {
463 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
464 disk_close(&disk);
466 return (size / BD(dev).bd_sectorsize);
470 * Attempt to open the disk described by (dev) for use by (f).
472 * Note that the philosophy here is "give them exactly what
473 * they ask for". This is necessary because being too "smart"
474 * about what the user might want leads to complications.
475 * (eg. given no slice or partition value, with a disk that is
476 * sliced - are they after the first BSD slice, or the DOS
477 * slice before it?)
479 static int
480 bd_open(struct open_file *f, ...)
482 struct disk_devdesc *dev;
483 va_list ap;
484 int rc;
486 va_start(ap, f);
487 dev = va_arg(ap, struct disk_devdesc *);
488 va_end(ap);
490 if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo)
491 return (EIO);
493 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
494 if (!bd_int13probe(&BD(dev)))
495 return (EIO);
496 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
497 return (EIO);
499 if (BD(dev).bd_bcache == NULL)
500 BD(dev).bd_bcache = bcache_allocate();
502 if (BD(dev).bd_open == 0)
503 BD(dev).bd_sectors = bd_disk_get_sectors(dev);
504 BD(dev).bd_open++;
506 rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
507 BD(dev).bd_sectorsize);
508 if (rc != 0) {
509 BD(dev).bd_open--;
510 if (BD(dev).bd_open == 0) {
511 bcache_free(BD(dev).bd_bcache);
512 BD(dev).bd_bcache = NULL;
515 return (rc);
518 static int
519 bd_close(struct open_file *f)
521 struct disk_devdesc *dev;
523 dev = (struct disk_devdesc *)f->f_devdata;
524 BD(dev).bd_open--;
525 if (BD(dev).bd_open == 0) {
526 bcache_free(BD(dev).bd_bcache);
527 BD(dev).bd_bcache = NULL;
529 return (disk_close(dev));
532 static int
533 bd_ioctl(struct open_file *f, u_long cmd, void *data)
535 struct disk_devdesc *dev;
536 int rc;
538 dev = (struct disk_devdesc *)f->f_devdata;
540 rc = disk_ioctl(dev, cmd, data);
541 if (rc != ENOTTY)
542 return (rc);
544 switch (cmd) {
545 case DIOCGSECTORSIZE:
546 *(uint32_t *)data = BD(dev).bd_sectorsize;
547 break;
548 case DIOCGMEDIASIZE:
549 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
550 break;
551 default:
552 return (ENOTTY);
554 return (0);
557 static int
558 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
559 char *buf, size_t *rsize)
561 struct bcache_devdata bcd;
562 struct disk_devdesc *dev;
564 dev = (struct disk_devdesc *)devdata;
565 bcd.dv_strategy = bd_realstrategy;
566 bcd.dv_devdata = devdata;
567 bcd.dv_cache = BD(dev).bd_bcache;
568 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
569 buf, rsize));
572 static int
573 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
574 char *buf, size_t *rsize)
576 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
577 uint64_t disk_blocks, offset;
578 size_t blks, blkoff, bsize, rest;
579 caddr_t bbuf;
580 int rc;
582 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
583 return (EIO);
586 * First make sure the IO size is a multiple of 512 bytes. While we do
587 * process partial reads below, the strategy mechanism is built
588 * assuming IO is a multiple of 512B blocks. If the request is not
589 * a multiple of 512B blocks, it has to be some sort of bug.
591 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
592 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
593 size, BIOSDISK_SECSIZE);
594 return (EIO);
597 DEBUG("open_disk %p", dev);
599 offset = dblk * BIOSDISK_SECSIZE;
600 dblk = offset / BD(dev).bd_sectorsize;
601 blkoff = offset % BD(dev).bd_sectorsize;
604 * Check the value of the size argument. We do have quite small
605 * heap (64MB), but we do not know good upper limit, so we check against
606 * INT_MAX here. This will also protect us against possible overflows
607 * while translating block count to bytes.
609 if (size > INT_MAX) {
610 DEBUG("too large I/O: %zu bytes", size);
611 return (EIO);
614 blks = size / BD(dev).bd_sectorsize;
615 if (blks == 0 || (size % BD(dev).bd_sectorsize) != 0)
616 blks++;
618 if (dblk > dblk + blks)
619 return (EIO);
621 if (rsize)
622 *rsize = 0;
625 * Get disk blocks, this value is either for whole disk or for
626 * partition.
628 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
629 /* DIOCGMEDIASIZE does return bytes. */
630 disk_blocks /= BD(dev).bd_sectorsize;
631 } else {
632 /* We should not get here. Just try to survive. */
633 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
636 /* Validate source block address. */
637 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
638 return (EIO);
641 * Truncate if we are crossing disk or partition end.
643 if (dblk + blks >= dev->d_offset + disk_blocks) {
644 blks = dev->d_offset + disk_blocks - dblk;
645 size = blks * BD(dev).bd_sectorsize;
646 DEBUG("short I/O %d", blks);
649 if (V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize == 0)
650 panic("BUG: Real mode buffer is too small\n");
652 bbuf = PTOV(V86_IO_BUFFER);
653 rest = size;
655 while (blks > 0) {
656 int x = min(blks, V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize);
658 switch (rw & F_MASK) {
659 case F_READ:
660 DEBUG("read %d from %lld to %p", x, dblk, buf);
661 bsize = BD(dev).bd_sectorsize * x - blkoff;
662 if (rest < bsize)
663 bsize = rest;
665 if ((rc = bd_io(dev, dblk, x, bbuf, BD_RD)) != 0)
666 return (EIO);
668 bcopy(bbuf + blkoff, buf, bsize);
669 break;
670 case F_WRITE :
671 DEBUG("write %d from %lld to %p", x, dblk, buf);
672 if (blkoff != 0) {
674 * We got offset to sector, read 1 sector to
675 * bbuf.
677 x = 1;
678 bsize = BD(dev).bd_sectorsize - blkoff;
679 bsize = min(bsize, rest);
680 rc = bd_io(dev, dblk, x, bbuf, BD_RD);
681 } else if (rest < BD(dev).bd_sectorsize) {
683 * The remaining block is not full
684 * sector. Read 1 sector to bbuf.
686 x = 1;
687 bsize = rest;
688 rc = bd_io(dev, dblk, x, bbuf, BD_RD);
689 } else {
690 /* We can write full sector(s). */
691 bsize = BD(dev).bd_sectorsize * x;
694 * Put your Data In, Put your Data out,
695 * Put your Data In, and shake it all about
697 bcopy(buf, bbuf + blkoff, bsize);
698 if ((rc = bd_io(dev, dblk, x, bbuf, BD_WR)) != 0)
699 return (EIO);
701 break;
702 default:
703 /* DO NOTHING */
704 return (EROFS);
707 blkoff = 0;
708 buf += bsize;
709 rest -= bsize;
710 blks -= x;
711 dblk += x;
714 if (rsize != NULL)
715 *rsize = size;
716 return (0);
719 static int
720 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
721 int dowrite)
723 static struct edd_packet packet;
725 packet.len = sizeof (struct edd_packet);
726 packet.count = blks;
727 packet.off = VTOPOFF(dest);
728 packet.seg = VTOPSEG(dest);
729 packet.lba = dblk;
730 v86.ctl = V86_FLAGS;
731 v86.addr = 0x13;
732 /* Should we Write with verify ?? 0x4302 ? */
733 if (dowrite == BD_WR)
734 v86.eax = 0x4300;
735 else
736 v86.eax = 0x4200;
737 v86.edx = BD(dev).bd_unit;
738 v86.ds = VTOPSEG(&packet);
739 v86.esi = VTOPOFF(&packet);
740 v86int();
741 if (V86_CY(v86.efl))
742 return (v86.eax >> 8);
743 return (0);
746 static int
747 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
748 int dowrite)
750 uint32_t x, bpc, cyl, hd, sec;
752 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
753 x = dblk;
754 cyl = x / bpc; /* block # / blocks per cylinder */
755 x %= bpc; /* block offset into cylinder */
756 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
757 sec = x % BD(dev).bd_sec; /* offset into track */
759 /* correct sector number for 1-based BIOS numbering */
760 sec++;
762 if (cyl > 1023) {
763 /* CHS doesn't support cylinders > 1023. */
764 return (1);
767 v86.ctl = V86_FLAGS;
768 v86.addr = 0x13;
769 if (dowrite == BD_WR)
770 v86.eax = 0x300 | blks;
771 else
772 v86.eax = 0x200 | blks;
773 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
774 v86.edx = (hd << 8) | BD(dev).bd_unit;
775 v86.es = VTOPSEG(dest);
776 v86.ebx = VTOPOFF(dest);
777 v86int();
778 if (V86_CY(v86.efl))
779 return (v86.eax >> 8);
780 return (0);
783 static void
784 bd_io_workaround(struct disk_devdesc *dev)
786 uint8_t buf[8 * 1024];
788 bd_edd_io(dev, 0xffffffff, 1, (caddr_t)buf, BD_RD);
791 static int
792 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
793 int dowrite)
795 int result, retry;
797 /* Just in case some idiot actually tries to read/write -1 blocks... */
798 if (blks < 0)
799 return (-1);
802 * Workaround for a problem with some HP ProLiant BIOS failing to work
803 * out the boot disk after installation. hrs and kuriyama discovered
804 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
805 * discovered that an int13h call seems to cause a buffer overrun in
806 * the bios. The problem is alleviated by doing an extra read before
807 * the buggy read. It is not immediately known whether other models
808 * are similarly affected.
809 * Loop retrying the operation a couple of times. The BIOS
810 * may also retry.
812 if (dowrite == BD_RD && dblk >= 0x100000000)
813 bd_io_workaround(dev);
814 for (retry = 0; retry < 3; retry++) {
815 if (BD(dev).bd_flags & BD_MODEEDD)
816 result = bd_edd_io(dev, dblk, blks, dest, dowrite);
817 else
818 result = bd_chs_io(dev, dblk, blks, dest, dowrite);
820 if (result == 0) {
821 if (BD(dev).bd_flags & BD_NO_MEDIA)
822 BD(dev).bd_flags &= ~BD_NO_MEDIA;
823 break;
826 bd_reset_disk(BD(dev).bd_unit);
829 * Error codes:
830 * 20h controller failure
831 * 31h no media in drive (IBM/MS INT 13 extensions)
832 * 80h no media in drive, VMWare (Fusion)
833 * There is no reason to repeat the IO with errors above.
835 if (result == 0x20 || result == 0x31 || result == 0x80) {
836 BD(dev).bd_flags |= BD_NO_MEDIA;
837 break;
841 if (result != 0 && (BD(dev).bd_flags & BD_NO_MEDIA) == 0) {
842 if (dowrite == BD_WR) {
843 printf("%s%d: Write %d sector(s) from %p (0x%x) "
844 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
845 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
846 result);
847 } else {
848 printf("%s%d: Read %d sector(s) from %lld to %p "
849 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
850 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
851 result);
855 return (result);
859 * Return the BIOS geometry of a given "fixed drive" in a format
860 * suitable for the legacy bootinfo structure. Since the kernel is
861 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
862 * prefer to get the information directly, rather than rely on being
863 * able to put it together from information already maintained for
864 * different purposes and for a probably different number of drives.
866 * For valid drives, the geometry is expected in the format (31..0)
867 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
868 * indicated by returning the geometry of a "1.2M" PC-format floppy
869 * disk. And, incidentally, what is returned is not the geometry as
870 * such but the highest valid cylinder, head, and sector numbers.
872 uint32_t
873 bd_getbigeom(int bunit)
876 v86.ctl = V86_FLAGS;
877 v86.addr = 0x13;
878 v86.eax = 0x800;
879 v86.edx = 0x80 + bunit;
880 v86int();
881 if (V86_CY(v86.efl))
882 return (0x4f010f);
883 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
884 (v86.edx & 0xff00) | (v86.ecx & 0x3f));
888 * Return a suitable dev_t value for (dev).
890 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
891 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
894 bd_getdev(struct i386_devdesc *d)
896 struct disk_devdesc *dev;
897 int biosdev;
898 int major;
899 int rootdev;
900 char *nip, *cp;
901 int i, unit;
903 dev = (struct disk_devdesc *)d;
904 biosdev = bd_unit2bios(dev->dd.d_unit);
905 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
906 if (biosdev == -1) /* not a BIOS device */
907 return (-1);
908 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
909 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
910 return (-1);
911 else
912 disk_close(dev);
914 if (biosdev < 0x80) {
915 /* floppy (or emulated floppy) or ATAPI device */
916 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
917 /* is an ATAPI disk */
918 major = WFDMAJOR;
919 } else {
920 /* is a floppy disk */
921 major = FDMAJOR;
923 } else {
924 /* assume an IDE disk */
925 major = WDMAJOR;
927 /* default root disk unit number */
928 unit = biosdev & 0x7f;
930 /* XXX a better kludge to set the root disk unit number */
931 if ((nip = getenv("root_disk_unit")) != NULL) {
932 i = strtol(nip, &cp, 0);
933 /* check for parse error */
934 if ((cp != nip) && (*cp == 0))
935 unit = i;
938 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
939 DEBUG("dev is 0x%x\n", rootdev);
940 return (rootdev);