2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * BIOS disk device handling.
34 * Ideas and algorithms from:
36 * - NetBSD libi386/biosdisk.c
37 * - FreeBSD biosboot/disk.c
42 #include <sys/limits.h>
43 #include <sys/queue.h>
45 #include <machine/bootinfo.h>
49 #include <bootstrap.h>
55 #define BIOS_NUMDRIVES 0x475
56 #define BIOSDISK_SECSIZE 512
57 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
59 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
60 #define WDMAJOR 0 /* major numbers for devices we frontend for */
68 #define DEBUG(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
70 #define DEBUG(fmt, args...)
73 struct specification_packet {
77 uint8_t sp_controller;
79 uint16_t sp_devicespec;
80 uint16_t sp_buffersegment;
81 uint16_t sp_loadsegment;
82 uint16_t sp_sectorcount;
88 * List of BIOS devices, translation from disk unit number to
93 STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */
94 int bd_unit; /* BIOS unit number */
95 int bd_cyl; /* BIOS geometry */
99 #define BD_MODEINT13 0x0000
100 #define BD_MODEEDD1 0x0001
101 #define BD_MODEEDD3 0x0002
102 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
103 #define BD_MODEMASK 0x0003
104 #define BD_FLOPPY 0x0004
105 #define BD_CDROM 0x0008
106 #define BD_NO_MEDIA 0x0010
107 int bd_type; /* BIOS 'drive type' (floppy only) */
108 uint16_t bd_sectorsize; /* Sector size */
109 uint64_t bd_sectors; /* Disk size */
110 int bd_open; /* reference counter */
111 void *bd_bcache; /* buffer cache data */
117 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t;
118 static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
119 static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
120 static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
122 static void bd_io_workaround(bdinfo_t *);
123 static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int);
124 static bool bd_int13probe(bdinfo_t *);
126 static int bd_init(void);
127 static int cd_init(void);
128 static int fd_init(void);
129 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
130 char *buf, size_t *rsize);
131 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
132 char *buf, size_t *rsize);
133 static int bd_open(struct open_file *f, ...);
134 static int bd_close(struct open_file *f);
135 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
136 static int bd_print(int verbose);
137 static int cd_print(int verbose);
138 static int fd_print(int verbose);
139 static void bd_reset_disk(int);
140 static int bd_get_diskinfo_std(struct bdinfo *);
142 struct devsw biosfd = {
146 .dv_strategy = bd_strategy,
148 .dv_close = bd_close,
149 .dv_ioctl = bd_ioctl,
150 .dv_print = fd_print,
154 struct devsw bioscd = {
158 .dv_strategy = bd_strategy,
160 .dv_close = bd_close,
161 .dv_ioctl = bd_ioctl,
162 .dv_print = cd_print,
166 struct devsw bioshd = {
168 .dv_type = DEVT_DISK,
170 .dv_strategy = bd_strategy,
172 .dv_close = bd_close,
173 .dv_ioctl = bd_ioctl,
174 .dv_print = bd_print,
178 static bdinfo_list_t *
179 bd_get_bdinfo_list(struct devsw *dev)
181 if (dev->dv_type == DEVT_DISK)
183 if (dev->dv_type == DEVT_CD)
185 if (dev->dv_type == DEVT_FD)
190 /* XXX this gets called way way too often, investigate */
192 bd_get_bdinfo(struct devdesc *dev)
198 bdi = bd_get_bdinfo_list(dev->d_dev);
203 STAILQ_FOREACH(bd, bdi, bd_link) {
204 if (unit == dev->d_unit)
212 * Translate between BIOS device numbers and our private unit numbers.
215 bd_bios2unit(int biosdev)
217 bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL };
221 DEBUG("looking for bios device 0x%x", biosdev);
222 for (i = 0; bdi[i] != NULL; i++) {
224 STAILQ_FOREACH(bd, bdi[i], bd_link) {
225 if (bd->bd_unit == biosdev) {
226 DEBUG("bd unit %d is BIOS device 0x%x", unit,
237 bd_unit2bios(struct i386_devdesc *dev)
243 bdi = bd_get_bdinfo_list(dev->dd.d_dev);
248 STAILQ_FOREACH(bd, bdi, bd_link) {
249 if (unit == dev->dd.d_unit)
250 return (bd->bd_unit);
257 * Use INT13 AH=15 - Read Drive Type.
264 for (drive = 0; drive < MAXBDDEV; drive++) {
265 bd_reset_disk(drive);
276 if ((v86.eax & 0x300) == 0)
284 * Quiz the BIOS for disk devices, save a little info about them.
293 for (unit = 0; unit < numfd; unit++) {
294 if ((bd = calloc(1, sizeof(*bd))) == NULL)
297 bd->bd_sectorsize = BIOSDISK_SECSIZE;
298 bd->bd_flags = BD_FLOPPY;
301 /* Use std diskinfo for floppy drive */
302 if (bd_get_diskinfo_std(bd) != 0) {
306 if (bd->bd_sectors == 0)
307 bd->bd_flags |= BD_NO_MEDIA;
309 printf("BIOS drive %c: is %s%d\n", ('A' + unit),
310 biosfd.dv_name, unit);
312 STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link);
315 bcache_add_dev(unit);
326 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
328 * Check the BIOS equipment list for number of fixed disks.
330 if ((bd = calloc(1, sizeof(*bd))) == NULL)
332 bd->bd_unit = base + unit;
333 if (!bd_int13probe(bd)) {
338 printf("BIOS drive %c: is %s%d\n", ('C' + unit),
339 bioshd.dv_name, unit);
341 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
343 bcache_add_dev(unit);
348 * We can't quiz, we have to be told what device to use, so this function
349 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
350 * device number to add.
363 struct specification_packet bc_sp;
366 if (!STAILQ_EMPTY(&cdinfo))
373 v86.ds = VTOPSEG(&bc_sp);
374 v86.esi = VTOPOFF(&bc_sp);
376 if ((v86.eax & 0xff00) != 0)
379 if ((bd = calloc(1, sizeof(*bd))) == NULL)
382 bd->bd_flags = BD_CDROM;
383 bd->bd_unit = biosdev;
386 * Ignore result from bd_int13probe(), we will use local
389 (void)bd_int13probe(bd);
391 if (bd->bd_cyl == 0) {
392 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
393 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
396 bd->bd_hds = bc_sp.sp_head + 1;
398 bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
399 if (bd->bd_sectors == 0)
400 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
402 /* Still no size? use 7.961GB */
403 if (bd->bd_sectors == 0)
404 bd->bd_sectors = 4173824;
406 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
407 printf("BIOS CD is cd%d\n", nbcinfo);
409 bcache_add_dev(nbcinfo); /* register cd device in bcache */
414 * Return EDD version or 0 if EDD is not supported on this drive.
417 bd_check_extensions(int unit)
419 /* do not use ext calls for floppy devices */
423 /* Determine if we can use EDD with this device. */
431 if (V86_CY(v86.efl) || /* carry set */
432 (v86.ebx & 0xffff) != 0xaa55) /* signature */
435 /* extended disk access functions (AH=42h-44h,47h,48h) supported */
436 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
439 return ((v86.eax >> 8) & 0xff);
443 bd_reset_disk(int unit)
454 * Read CHS info. Return 0 on success, error otherwise.
457 bd_get_diskinfo_std(struct bdinfo *bd)
459 bzero(&v86, sizeof(v86));
463 v86.edx = bd->bd_unit;
466 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
467 return ((v86.eax & 0xff00) >> 8);
469 /* return custom error on absurd sector number */
470 if ((v86.ecx & 0x3f) == 0)
473 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
474 /* Convert max head # -> # of heads */
475 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
476 bd->bd_sec = v86.ecx & 0x3f;
477 bd->bd_type = v86.ebx;
478 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
484 * Read EDD info. Return 0 on success, error otherwise.
487 bd_get_diskinfo_ext(struct bdinfo *bd)
489 struct edd_params params;
492 /* Get disk params */
493 bzero(¶ms, sizeof(params));
494 params.len = sizeof(params);
498 v86.edx = bd->bd_unit;
499 v86.ds = VTOPSEG(¶ms);
500 v86.esi = VTOPOFF(¶ms);
503 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
504 return ((v86.eax & 0xff00) >> 8);
507 * Sector size must be a multiple of 512 bytes.
508 * An alternate test would be to check power of 2,
509 * powerof2(params.sector_size).
510 * 16K is largest read buffer we can use at this time.
512 if (params.sector_size >= 512 &&
513 params.sector_size <= 16384 &&
514 (params.sector_size % BIOSDISK_SECSIZE) == 0)
515 bd->bd_sectorsize = params.sector_size;
517 bd->bd_cyl = params.cylinders;
518 bd->bd_hds = params.heads;
519 bd->bd_sec = params.sectors_per_track;
521 if (params.sectors != 0) {
522 total = params.sectors;
524 total = (uint64_t)params.cylinders *
525 params.heads * params.sectors_per_track;
527 bd->bd_sectors = total;
533 * Try to detect a device supported by the legacy int13 BIOS
536 bd_int13probe(bdinfo_t *bd)
540 bd->bd_flags &= ~BD_NO_MEDIA;
542 edd = bd_check_extensions(bd->bd_unit);
544 bd->bd_flags |= BD_MODEINT13;
546 bd->bd_flags |= BD_MODEEDD1;
548 bd->bd_flags |= BD_MODEEDD3;
550 /* Default sector size */
551 bd->bd_sectorsize = BIOSDISK_SECSIZE;
554 * Test if the floppy device is present, so we can avoid receiving
555 * bogus information from bd_get_diskinfo_std().
557 if (bd->bd_unit < 0x80) {
559 bd_reset_disk(bd->bd_unit);
565 v86.edx = bd->bd_unit;
567 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
573 ret = bd_get_diskinfo_ext(bd);
574 if (ret != 0 || bd->bd_sectors == 0)
575 ret = bd_get_diskinfo_std(bd);
577 if (ret != 0 && bd->bd_unit < 0x80) {
578 /* Set defaults for 1.44 floppy */
582 bd->bd_sectors = 2880;
583 /* Since we are there, there most likely is no media */
584 bd->bd_flags |= BD_NO_MEDIA;
589 /* CD is special case, bc_add() has its own fallback. */
590 if ((bd->bd_flags & BD_CDROM) != 0)
593 if (bd->bd_sectors != 0 && edd != 0) {
597 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
598 bd->bd_sec * bd->bd_hds;
602 if ((bd->bd_flags & BD_FLOPPY) != 0)
603 dv_name = biosfd.dv_name;
604 else if ((bd->bd_flags & BD_CDROM) != 0)
605 dv_name = bioscd.dv_name;
607 dv_name = bioshd.dv_name;
609 printf("Can not get information about %s unit %#x\n",
610 dv_name, bd->bd_unit);
620 if (bd->bd_sectors == 0)
621 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
623 DEBUG("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
624 bd->bd_hds, bd->bd_sec);
630 bd_count(bdinfo_list_t *bdi)
636 STAILQ_FOREACH(bd, bdi, bd_link)
642 * Print information about disks
645 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
648 struct disk_devdesc devd;
653 if (STAILQ_EMPTY(bdi))
656 printf("%s devices:", dev->dv_name);
657 if ((ret = pager_output("\n")) != 0)
661 STAILQ_FOREACH(bd, bdi, bd_link) {
664 switch (dev->dv_type) {
669 drive = 'C' + bd_count(&hdinfo);
676 snprintf(line, sizeof(line),
677 " %s%d: BIOS drive %c (%s%ju X %u):\n",
678 dev->dv_name, i, drive + i,
679 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
681 (uintmax_t)bd->bd_sectors,
683 if ((ret = pager_output(line)) != 0)
686 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
689 if (dev->dv_type != DEVT_DISK)
695 devd.d_partition = -1;
697 bd->bd_sectorsize * bd->bd_sectors,
698 bd->bd_sectorsize) == 0) {
699 snprintf(line, sizeof(line), " %s%d",
701 ret = disk_print(&devd, line, verbose);
711 fd_print(int verbose)
713 return (bd_print_common(&biosfd, &fdinfo, verbose));
717 bd_print(int verbose)
719 return (bd_print_common(&bioshd, &hdinfo, verbose));
723 cd_print(int verbose)
725 return (bd_print_common(&bioscd, &cdinfo, verbose));
729 * Read disk size from partition.
730 * This is needed to work around buggy BIOS systems returning
731 * wrong (truncated) disk media size.
732 * During bd_probe() we tested if the multiplication of bd_sectors
733 * would overflow so it should be safe to perform here.
736 bd_disk_get_sectors(struct disk_devdesc *dev)
739 struct disk_devdesc disk;
742 bd = bd_get_bdinfo(&dev->dd);
746 disk.dd.d_dev = dev->dd.d_dev;
747 disk.dd.d_unit = dev->dd.d_unit;
749 disk.d_partition = -1;
752 size = bd->bd_sectors * bd->bd_sectorsize;
753 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
754 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
757 return (size / bd->bd_sectorsize);
761 * Attempt to open the disk described by (dev) for use by (f).
763 * Note that the philosophy here is "give them exactly what
764 * they ask for". This is necessary because being too "smart"
765 * about what the user might want leads to complications.
766 * (eg. given no slice or partition value, with a disk that is
767 * sliced - are they after the first BSD slice, or the DOS
771 bd_open(struct open_file *f, ...)
774 struct disk_devdesc *dev;
779 dev = va_arg(ap, struct disk_devdesc *);
782 bd = bd_get_bdinfo(&dev->dd);
786 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
787 if (!bd_int13probe(bd))
789 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
792 if (bd->bd_bcache == NULL)
793 bd->bd_bcache = bcache_allocate();
795 if (bd->bd_open == 0)
796 bd->bd_sectors = bd_disk_get_sectors(dev);
800 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
801 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
805 if (bd->bd_open == 0) {
806 bcache_free(bd->bd_bcache);
807 bd->bd_bcache = NULL;
815 bd_close(struct open_file *f)
817 struct disk_devdesc *dev;
821 dev = (struct disk_devdesc *)f->f_devdata;
822 bd = bd_get_bdinfo(&dev->dd);
827 if (bd->bd_open == 0) {
828 bcache_free(bd->bd_bcache);
829 bd->bd_bcache = NULL;
831 if (dev->dd.d_dev->dv_type == DEVT_DISK)
832 rc = disk_close(dev);
837 bd_ioctl(struct open_file *f, u_long cmd, void *data)
840 struct disk_devdesc *dev;
843 dev = (struct disk_devdesc *)f->f_devdata;
844 bd = bd_get_bdinfo(&dev->dd);
848 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
849 rc = disk_ioctl(dev, cmd, data);
855 case DIOCGSECTORSIZE:
856 *(uint32_t *)data = bd->bd_sectorsize;
859 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
868 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
869 char *buf, size_t *rsize)
872 struct bcache_devdata bcd;
873 struct disk_devdesc *dev;
876 dev = (struct disk_devdesc *)devdata;
877 bd = bd_get_bdinfo(&dev->dd);
881 bcd.dv_strategy = bd_realstrategy;
882 bcd.dv_devdata = devdata;
883 bcd.dv_cache = bd->bd_bcache;
886 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
888 offset = dev->d_offset * bd->bd_sectorsize;
889 offset /= BIOSDISK_SECSIZE;
891 return (bcache_strategy(&bcd, rw, dblk + offset, size,
896 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
897 char *buf, size_t *rsize)
899 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
901 uint64_t disk_blocks, offset, d_offset;
902 size_t blks, blkoff, bsize, bio_size, rest;
906 bd = bd_get_bdinfo(&dev->dd);
907 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
911 * First make sure the IO size is a multiple of 512 bytes. While we do
912 * process partial reads below, the strategy mechanism is built
913 * assuming IO is a multiple of 512B blocks. If the request is not
914 * a multiple of 512B blocks, it has to be some sort of bug.
916 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
917 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
918 size, BIOSDISK_SECSIZE);
922 DEBUG("open_disk %p", dev);
924 offset = dblk * BIOSDISK_SECSIZE;
925 dblk = offset / bd->bd_sectorsize;
926 blkoff = offset % bd->bd_sectorsize;
929 * Check the value of the size argument. We do have quite small
930 * heap (64MB), but we do not know good upper limit, so we check against
931 * INT_MAX here. This will also protect us against possible overflows
932 * while translating block count to bytes.
934 if (size > INT_MAX) {
935 DEBUG("too large I/O: %zu bytes", size);
939 blks = size / bd->bd_sectorsize;
940 if (blks == 0 || (size % bd->bd_sectorsize) != 0)
943 if (dblk > dblk + blks)
950 * Get disk blocks, this value is either for whole disk or for
955 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
956 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
957 /* DIOCGMEDIASIZE does return bytes. */
958 disk_blocks /= bd->bd_sectorsize;
960 d_offset = dev->d_offset;
962 if (disk_blocks == 0)
963 disk_blocks = bd->bd_sectors - d_offset;
965 /* Validate source block address. */
966 if (dblk < d_offset || dblk >= d_offset + disk_blocks)
970 * Truncate if we are crossing disk or partition end.
972 if (dblk + blks >= d_offset + disk_blocks) {
973 blks = d_offset + disk_blocks - dblk;
974 size = blks * bd->bd_sectorsize;
975 DEBUG("short I/O %d", blks);
978 bio_size = min(BIO_BUFFER_SIZE, size);
979 while (bio_size > bd->bd_sectorsize) {
980 bbuf = bio_alloc(bio_size);
983 bio_size -= bd->bd_sectorsize;
986 bio_size = V86_IO_BUFFER_SIZE;
987 if (bio_size / bd->bd_sectorsize == 0)
988 panic("BUG: Real mode buffer is too small");
990 /* Use alternate 4k buffer */
991 bbuf = PTOV(V86_IO_BUFFER);
996 int x = min(blks, bio_size / bd->bd_sectorsize);
998 switch (rw & F_MASK) {
1000 DEBUG("read %d from %lld to %p", x, dblk, buf);
1001 bsize = bd->bd_sectorsize * x - blkoff;
1005 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) {
1010 bcopy(bbuf + blkoff, buf, bsize);
1013 DEBUG("write %d from %lld to %p", x, dblk, buf);
1016 * We got offset to sector, read 1 sector to
1020 bsize = bd->bd_sectorsize - blkoff;
1021 bsize = min(bsize, rest);
1022 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1023 } else if (rest < bd->bd_sectorsize) {
1025 * The remaining block is not full
1026 * sector. Read 1 sector to bbuf.
1030 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1032 /* We can write full sector(s). */
1033 bsize = bd->bd_sectorsize * x;
1036 * Put your Data In, Put your Data out,
1037 * Put your Data In, and shake it all about
1039 bcopy(buf, bbuf + blkoff, bsize);
1040 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) {
1062 if (bbuf != PTOV(V86_IO_BUFFER))
1063 bio_free(bbuf, bio_size);
1068 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1071 static struct edd_packet packet;
1073 packet.len = sizeof(struct edd_packet);
1074 packet.count = blks;
1075 packet.off = VTOPOFF(dest);
1076 packet.seg = VTOPSEG(dest);
1078 v86.ctl = V86_FLAGS;
1080 /* Should we Write with verify ?? 0x4302 ? */
1081 if (dowrite == BD_WR)
1085 v86.edx = bd->bd_unit;
1086 v86.ds = VTOPSEG(&packet);
1087 v86.esi = VTOPOFF(&packet);
1089 if (V86_CY(v86.efl))
1090 return (v86.eax >> 8);
1095 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1098 uint32_t x, bpc, cyl, hd, sec;
1100 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */
1102 cyl = x / bpc; /* block # / blocks per cylinder */
1103 x %= bpc; /* block offset into cylinder */
1104 hd = x / bd->bd_sec; /* offset / blocks per track */
1105 sec = x % bd->bd_sec; /* offset into track */
1107 /* correct sector number for 1-based BIOS numbering */
1111 /* CHS doesn't support cylinders > 1023. */
1115 v86.ctl = V86_FLAGS;
1117 if (dowrite == BD_WR)
1118 v86.eax = 0x300 | blks;
1120 v86.eax = 0x200 | blks;
1121 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1122 v86.edx = (hd << 8) | bd->bd_unit;
1123 v86.es = VTOPSEG(dest);
1124 v86.ebx = VTOPOFF(dest);
1126 if (V86_CY(v86.efl))
1127 return (v86.eax >> 8);
1132 bd_io_workaround(bdinfo_t *bd)
1134 uint8_t buf[8 * 1024];
1136 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1140 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1141 caddr_t dest, int dowrite)
1145 /* Just in case some idiot actually tries to read/write -1 blocks... */
1150 * Workaround for a problem with some HP ProLiant BIOS failing to work
1151 * out the boot disk after installation. hrs and kuriyama discovered
1152 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1153 * discovered that an int13h call seems to cause a buffer overrun in
1154 * the bios. The problem is alleviated by doing an extra read before
1155 * the buggy read. It is not immediately known whether other models
1156 * are similarly affected.
1157 * Loop retrying the operation a couple of times. The BIOS
1160 if (dowrite == BD_RD && dblk >= 0x100000000)
1161 bd_io_workaround(bd);
1162 for (retry = 0; retry < 3; retry++) {
1163 if (bd->bd_flags & BD_MODEEDD)
1164 result = bd_edd_io(bd, dblk, blks, dest, dowrite);
1166 result = bd_chs_io(bd, dblk, blks, dest, dowrite);
1169 if (bd->bd_flags & BD_NO_MEDIA)
1170 bd->bd_flags &= ~BD_NO_MEDIA;
1174 bd_reset_disk(bd->bd_unit);
1178 * 20h controller failure
1179 * 31h no media in drive (IBM/MS INT 13 extensions)
1180 * 80h no media in drive, VMWare (Fusion)
1181 * There is no reason to repeat the IO with errors above.
1183 if (result == 0x20 || result == 0x31 || result == 0x80) {
1184 bd->bd_flags |= BD_NO_MEDIA;
1189 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
1190 if (dowrite == BD_WR) {
1191 printf("%s%d: Write %d sector(s) from %p (0x%x) "
1192 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1193 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1196 printf("%s%d: Read %d sector(s) from %lld to %p "
1197 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
1198 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
1207 * Return the BIOS geometry of a given "fixed drive" in a format
1208 * suitable for the legacy bootinfo structure. Since the kernel is
1209 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1210 * prefer to get the information directly, rather than rely on being
1211 * able to put it together from information already maintained for
1212 * different purposes and for a probably different number of drives.
1214 * For valid drives, the geometry is expected in the format (31..0)
1215 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1216 * indicated by returning the geometry of a "1.2M" PC-format floppy
1217 * disk. And, incidentally, what is returned is not the geometry as
1218 * such but the highest valid cylinder, head, and sector numbers.
1221 bd_getbigeom(int bunit)
1224 v86.ctl = V86_FLAGS;
1227 v86.edx = 0x80 + bunit;
1229 if (V86_CY(v86.efl))
1231 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1232 (v86.edx & 0xff00) | (v86.ecx & 0x3f));
1236 * Return a suitable dev_t value for (dev).
1238 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1239 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1242 bd_getdev(struct i386_devdesc *d)
1244 struct disk_devdesc *dev;
1250 int i, unit, slice, partition;
1252 /* XXX: Assume partition 'a'. */
1256 dev = (struct disk_devdesc *)d;
1257 bd = bd_get_bdinfo(&dev->dd);
1261 biosdev = bd_unit2bios(d);
1262 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1263 if (biosdev == -1) /* not a BIOS device */
1266 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1267 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1268 bd->bd_sectorsize) != 0) /* oops, not a viable device */
1272 slice = dev->d_slice + 1;
1273 partition = dev->d_partition;
1276 if (biosdev < 0x80) {
1277 /* floppy (or emulated floppy) or ATAPI device */
1278 if (bd->bd_type == DT_ATAPI) {
1279 /* is an ATAPI disk */
1282 /* is a floppy disk */
1286 /* assume an IDE disk */
1289 /* default root disk unit number */
1290 unit = biosdev & 0x7f;
1292 if (dev->dd.d_dev->dv_type == DEVT_CD) {
1294 * XXX: Need to examine device spec here to figure out if
1295 * SCSI or ATAPI. No idea on how to figure out device number.
1296 * All we can really pass to the kernel is what bus and device
1297 * on which bus we were booted from, which dev_t isn't well
1298 * suited to since those number don't match to unit numbers
1299 * very well. We may just need to engage in a hack where
1300 * we pass -C to the boot args if we are the boot device.
1306 /* XXX a better kludge to set the root disk unit number */
1307 if ((nip = getenv("root_disk_unit")) != NULL) {
1308 i = strtol(nip, &cp, 0);
1309 /* check for parse error */
1310 if ((cp != nip) && (*cp == 0))
1314 rootdev = MAKEBOOTDEV(major, slice, unit, partition);
1315 DEBUG("dev is 0x%x\n", rootdev);