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 */
70 #define CMD_RESET 0x0000
71 #define CMD_READ_CHS 0x0200
72 #define CMD_WRITE_CHS 0x0300
73 #define CMD_READ_PARAM 0x0800
74 #define CMD_DRIVE_TYPE 0x1500
75 #define CMD_CHECK_EDD 0x4100
76 #define CMD_READ_LBA 0x4200
77 #define CMD_WRITE_LBA 0x4300
78 #define CMD_EXT_PARAM 0x4800
79 #define CMD_CD_GET_STATUS 0x4b01
81 #define DISK_BIOS 0x13
84 #define DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
86 #define DPRINTF(fmt, args...) ((void)0)
89 struct specification_packet {
93 uint8_t sp_controller;
95 uint16_t sp_devicespec;
96 uint16_t sp_buffersegment;
97 uint16_t sp_loadsegment;
98 uint16_t sp_sectorcount;
101 uint8_t sp_dummy[16]; /* Avoid memory corruption */
105 * List of BIOS devices, translation from disk unit number to
108 typedef struct bdinfo
110 STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */
111 int bd_unit; /* BIOS unit number */
112 int bd_cyl; /* BIOS geometry */
116 #define BD_MODEINT13 0x0000
117 #define BD_MODEEDD1 0x0001
118 #define BD_MODEEDD3 0x0002
119 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
120 #define BD_MODEMASK 0x0003
121 #define BD_FLOPPY 0x0004
122 #define BD_CDROM 0x0008
123 #define BD_NO_MEDIA 0x0010
124 int bd_type; /* BIOS 'drive type' (floppy only) */
125 uint16_t bd_sectorsize; /* Sector size */
126 uint64_t bd_sectors; /* Disk size */
127 int bd_open; /* reference counter */
128 void *bd_bcache; /* buffer cache data */
134 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t;
135 static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
136 static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
137 static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
139 static void bd_io_workaround(bdinfo_t *);
140 static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int);
141 static bool bd_int13probe(bdinfo_t *);
143 static int bd_init(void);
144 static int cd_init(void);
145 static int fd_init(void);
146 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
147 char *buf, size_t *rsize);
148 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
149 char *buf, size_t *rsize);
150 static int bd_open(struct open_file *f, ...);
151 static int bd_close(struct open_file *f);
152 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
153 static int bd_print(int verbose);
154 static int cd_print(int verbose);
155 static int fd_print(int verbose);
156 static void bd_reset_disk(int);
157 static int bd_get_diskinfo_std(struct bdinfo *);
159 struct devsw biosfd = {
163 .dv_strategy = bd_strategy,
165 .dv_close = bd_close,
166 .dv_ioctl = bd_ioctl,
167 .dv_print = fd_print,
171 struct devsw bioscd = {
175 .dv_strategy = bd_strategy,
177 .dv_close = bd_close,
178 .dv_ioctl = bd_ioctl,
179 .dv_print = cd_print,
183 struct devsw bioshd = {
185 .dv_type = DEVT_DISK,
187 .dv_strategy = bd_strategy,
189 .dv_close = bd_close,
190 .dv_ioctl = bd_ioctl,
191 .dv_print = bd_print,
195 static bdinfo_list_t *
196 bd_get_bdinfo_list(struct devsw *dev)
198 if (dev->dv_type == DEVT_DISK)
200 if (dev->dv_type == DEVT_CD)
202 if (dev->dv_type == DEVT_FD)
207 /* XXX this gets called way way too often, investigate */
209 bd_get_bdinfo(struct devdesc *dev)
215 bdi = bd_get_bdinfo_list(dev->d_dev);
220 STAILQ_FOREACH(bd, bdi, bd_link) {
221 if (unit == dev->d_unit)
229 * Translate between BIOS device numbers and our private unit numbers.
232 bd_bios2unit(int biosdev)
234 bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL };
238 DPRINTF("looking for bios device 0x%x", biosdev);
239 for (i = 0; bdi[i] != NULL; i++) {
241 STAILQ_FOREACH(bd, bdi[i], bd_link) {
242 if (bd->bd_unit == biosdev) {
243 DPRINTF("bd unit %d is BIOS device 0x%x", unit,
254 bd_unit2bios(struct i386_devdesc *dev)
260 bdi = bd_get_bdinfo_list(dev->dd.d_dev);
265 STAILQ_FOREACH(bd, bdi, bd_link) {
266 if (unit == dev->dd.d_unit)
267 return (bd->bd_unit);
274 * Use INT13 AH=15 - Read Drive Type.
281 for (drive = 0; drive < MAXBDDEV; drive++) {
282 bd_reset_disk(drive);
285 v86.addr = DISK_BIOS;
286 v86.eax = CMD_DRIVE_TYPE;
293 if ((v86.eax & 0x300) == 0)
301 * Quiz the BIOS for disk devices, save a little info about them.
310 for (unit = 0; unit < numfd; unit++) {
311 if ((bd = calloc(1, sizeof(*bd))) == NULL)
314 bd->bd_sectorsize = BIOSDISK_SECSIZE;
315 bd->bd_flags = BD_FLOPPY;
318 /* Use std diskinfo for floppy drive */
319 if (bd_get_diskinfo_std(bd) != 0) {
323 if (bd->bd_sectors == 0)
324 bd->bd_flags |= BD_NO_MEDIA;
326 printf("BIOS drive %c: is %s%d\n", ('A' + unit),
327 biosfd.dv_name, unit);
329 STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link);
332 bcache_add_dev(unit);
345 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
347 * Check the BIOS equipment list for number of fixed disks.
349 if ((bd = calloc(1, sizeof(*bd))) == NULL)
351 bd->bd_unit = base + unit;
352 if (!bd_int13probe(bd)) {
357 printf("BIOS drive %c: is %s%d\n", ('C' + unit),
358 bioshd.dv_name, unit);
360 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
362 bcache_add_dev(unit);
368 * We can't quiz, we have to be told what device to use, so this function
369 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
370 * device number to add.
380 * Information from bootable CD-ROM.
383 bd_get_diskinfo_cd(struct bdinfo *bd)
385 struct specification_packet bc_sp;
388 (void) memset(&bc_sp, 0, sizeof (bc_sp));
389 /* Set sp_size as per specification. */
390 bc_sp.sp_size = sizeof (bc_sp) - sizeof (bc_sp.sp_dummy);
393 v86.addr = DISK_BIOS;
394 v86.eax = CMD_CD_GET_STATUS;
395 v86.edx = bd->bd_unit;
396 v86.ds = VTOPSEG(&bc_sp);
397 v86.esi = VTOPOFF(&bc_sp);
400 if ((v86.eax & 0xff00) == 0 &&
401 bc_sp.sp_drive == bd->bd_unit) {
402 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
403 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
404 bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
405 bd->bd_hds = bc_sp.sp_head + 1;
406 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
408 if (bc_sp.sp_bootmedia & 0x0F) {
409 /* Floppy or hard-disk emulation */
410 bd->bd_sectorsize = BIOSDISK_SECSIZE;
413 bd->bd_sectorsize = 2048;
414 bd->bd_flags = BD_MODEEDD | BD_CDROM;
420 * If this is the boot_drive, default to non-emulation bootable CD-ROM.
422 if (ret != 0 && bd->bd_unit >= 0x88) {
426 bd->bd_sectorsize = 2048;
427 bd->bd_flags = BD_MODEEDD | BD_CDROM;
433 * Note we can not use bd_get_diskinfo_ext() nor bd_get_diskinfo_std()
434 * here - some systems do get hung with those.
437 * Still no size? use 7.961GB. The size does not really matter
438 * as long as it is reasonably large to make our reads to pass
439 * the sector count check.
441 if (bd->bd_sectors == 0)
442 bd->bd_sectors = 4173824;
453 if (!STAILQ_EMPTY(&cdinfo))
456 if ((bd = calloc(1, sizeof(*bd))) == NULL)
459 bd->bd_unit = biosdev;
460 if (bd_get_diskinfo_cd(bd) < 0) {
465 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
466 printf("BIOS CD is cd%d\n", nbcinfo);
468 bcache_add_dev(nbcinfo); /* register cd device in bcache */
473 * Return EDD version or 0 if EDD is not supported on this drive.
476 bd_check_extensions(int unit)
478 /* do not use ext calls for floppy devices */
482 /* Determine if we can use EDD with this device. */
484 v86.addr = DISK_BIOS;
485 v86.eax = CMD_CHECK_EDD;
487 v86.ebx = EDD_QUERY_MAGIC;
490 if (V86_CY(v86.efl) || /* carry set */
491 (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */
494 /* extended disk access functions (AH=42h-44h,47h,48h) supported */
495 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
498 return ((v86.eax >> 8) & 0xff);
502 bd_reset_disk(int unit)
506 v86.addr = DISK_BIOS;
513 * Read CHS info. Return 0 on success, error otherwise.
516 bd_get_diskinfo_std(struct bdinfo *bd)
518 bzero(&v86, sizeof(v86));
520 v86.addr = DISK_BIOS;
521 v86.eax = CMD_READ_PARAM;
522 v86.edx = bd->bd_unit;
525 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
526 return ((v86.eax & 0xff00) >> 8);
528 /* return custom error on absurd sector number */
529 if ((v86.ecx & 0x3f) == 0)
532 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
533 /* Convert max head # -> # of heads */
534 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
535 bd->bd_sec = v86.ecx & 0x3f;
536 bd->bd_type = v86.ebx;
537 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
543 * Read EDD info. Return 0 on success, error otherwise.
545 * Avoid stack corruption on some systems by adding extra bytes to
549 bd_get_diskinfo_ext(struct bdinfo *bd)
552 struct edd_params head;
553 struct edd_device_path_v3 device_path;
556 struct edd_params *params;
559 params = &dparams.head;
561 /* Get disk params */
562 bzero(&dparams, sizeof(dparams));
563 params->len = sizeof(struct edd_params_v3);
565 v86.addr = DISK_BIOS;
566 v86.eax = CMD_EXT_PARAM;
567 v86.edx = bd->bd_unit;
568 v86.ds = VTOPSEG(&dparams);
569 v86.esi = VTOPOFF(&dparams);
572 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
573 return ((v86.eax & 0xff00) >> 8);
576 * Sector size must be a multiple of 512 bytes.
577 * An alternate test would be to check power of 2,
578 * powerof2(params.sector_size).
579 * 16K is largest read buffer we can use at this time.
581 if (params->sector_size >= 512 &&
582 params->sector_size <= 16384 &&
583 (params->sector_size % BIOSDISK_SECSIZE) == 0)
584 bd->bd_sectorsize = params->sector_size;
586 bd->bd_cyl = params->cylinders;
587 bd->bd_hds = params->heads;
588 bd->bd_sec = params->sectors_per_track;
590 if (params->sectors != 0) {
591 total = params->sectors;
593 total = (uint64_t)params->cylinders *
594 params->heads * params->sectors_per_track;
596 bd->bd_sectors = total;
602 * Try to detect a device supported by the legacy int13 BIOS
605 bd_int13probe(bdinfo_t *bd)
609 bd->bd_flags &= ~BD_NO_MEDIA;
611 if ((bd->bd_flags & BD_CDROM) != 0) {
612 return (bd_get_diskinfo_cd(bd) == 0);
615 edd = bd_check_extensions(bd->bd_unit);
617 bd->bd_flags |= BD_MODEINT13;
619 bd->bd_flags |= BD_MODEEDD1;
621 bd->bd_flags |= BD_MODEEDD3;
623 /* Default sector size */
624 if (bd->bd_sectorsize == 0)
625 bd->bd_sectorsize = BIOSDISK_SECSIZE;
628 * Test if the floppy device is present, so we can avoid receiving
629 * bogus information from bd_get_diskinfo_std().
631 if (bd->bd_unit < 0x80) {
633 bd_reset_disk(bd->bd_unit);
637 v86.addr = DISK_BIOS;
638 v86.eax = CMD_DRIVE_TYPE;
639 v86.edx = bd->bd_unit;
641 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
647 ret = bd_get_diskinfo_ext(bd);
648 if (ret != 0 || bd->bd_sectors == 0)
649 ret = bd_get_diskinfo_std(bd);
651 if (ret != 0 && bd->bd_unit < 0x80) {
652 /* Set defaults for 1.44 floppy */
656 bd->bd_sectors = 2880;
657 /* Since we are there, there most likely is no media */
658 bd->bd_flags |= BD_NO_MEDIA;
663 if (bd->bd_sectors != 0 && edd != 0) {
667 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
668 bd->bd_sec * bd->bd_hds;
672 if ((bd->bd_flags & BD_FLOPPY) != 0)
673 dv_name = biosfd.dv_name;
675 dv_name = bioshd.dv_name;
677 printf("Can not get information about %s unit %#x\n",
678 dv_name, bd->bd_unit);
688 if (bd->bd_sectors == 0)
689 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
691 DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
692 bd->bd_hds, bd->bd_sec);
698 bd_count(bdinfo_list_t *bdi)
704 STAILQ_FOREACH(bd, bdi, bd_link)
710 * Print information about disks
713 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
716 struct disk_devdesc devd;
721 if (STAILQ_EMPTY(bdi))
724 printf("%s devices:", dev->dv_name);
725 if ((ret = pager_output("\n")) != 0)
729 STAILQ_FOREACH(bd, bdi, bd_link) {
732 switch (dev->dv_type) {
737 drive = 'C' + bd_count(&hdinfo);
744 snprintf(line, sizeof(line),
745 " %s%d: BIOS drive %c (%s%ju X %u):\n",
746 dev->dv_name, i, drive + i,
747 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
749 (uintmax_t)bd->bd_sectors,
751 if ((ret = pager_output(line)) != 0)
754 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
757 if (dev->dv_type != DEVT_DISK)
762 devd.d_slice = D_SLICENONE;
763 devd.d_partition = D_PARTNONE;
765 bd->bd_sectorsize * bd->bd_sectors,
766 bd->bd_sectorsize) == 0) {
767 snprintf(line, sizeof(line), " %s%d",
769 ret = disk_print(&devd, line, verbose);
779 fd_print(int verbose)
781 return (bd_print_common(&biosfd, &fdinfo, verbose));
785 bd_print(int verbose)
787 return (bd_print_common(&bioshd, &hdinfo, verbose));
791 cd_print(int verbose)
793 return (bd_print_common(&bioscd, &cdinfo, verbose));
797 * Read disk size from partition.
798 * This is needed to work around buggy BIOS systems returning
799 * wrong (truncated) disk media size.
800 * During bd_probe() we tested if the multiplication of bd_sectors
801 * would overflow so it should be safe to perform here.
804 bd_disk_get_sectors(struct disk_devdesc *dev)
807 struct disk_devdesc disk;
810 bd = bd_get_bdinfo(&dev->dd);
814 disk.dd.d_dev = dev->dd.d_dev;
815 disk.dd.d_unit = dev->dd.d_unit;
816 disk.d_slice = D_SLICENONE;
817 disk.d_partition = D_PARTNONE;
820 size = bd->bd_sectors * bd->bd_sectorsize;
821 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
822 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
825 return (size / bd->bd_sectorsize);
829 * Attempt to open the disk described by (dev) for use by (f).
831 * Note that the philosophy here is "give them exactly what
832 * they ask for". This is necessary because being too "smart"
833 * about what the user might want leads to complications.
834 * (eg. given no slice or partition value, with a disk that is
835 * sliced - are they after the first BSD slice, or the DOS
839 bd_open(struct open_file *f, ...)
842 struct disk_devdesc *dev;
849 dev = va_arg(ap, struct disk_devdesc *);
852 bd = bd_get_bdinfo(&dev->dd);
856 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
857 if (!bd_int13probe(bd))
859 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
862 if (bd->bd_bcache == NULL)
863 bd->bd_bcache = bcache_allocate();
865 if (bd->bd_open == 0)
866 bd->bd_sectors = bd_disk_get_sectors(dev);
870 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
871 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
875 if (bd->bd_open == 0) {
876 bcache_free(bd->bd_bcache);
877 bd->bd_bcache = NULL;
886 bd_close(struct open_file *f)
888 struct disk_devdesc *dev;
892 dev = (struct disk_devdesc *)f->f_devdata;
893 bd = bd_get_bdinfo(&dev->dd);
898 if (bd->bd_open == 0) {
899 bcache_free(bd->bd_bcache);
900 bd->bd_bcache = NULL;
902 if (dev->dd.d_dev->dv_type == DEVT_DISK)
903 rc = disk_close(dev);
908 bd_ioctl(struct open_file *f, u_long cmd, void *data)
911 struct disk_devdesc *dev;
914 dev = (struct disk_devdesc *)f->f_devdata;
915 bd = bd_get_bdinfo(&dev->dd);
919 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
920 rc = disk_ioctl(dev, cmd, data);
926 case DIOCGSECTORSIZE:
927 *(uint32_t *)data = bd->bd_sectorsize;
930 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
939 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
940 char *buf, size_t *rsize)
943 struct bcache_devdata bcd;
944 struct disk_devdesc *dev;
947 dev = (struct disk_devdesc *)devdata;
948 bd = bd_get_bdinfo(&dev->dd);
952 bcd.dv_strategy = bd_realstrategy;
953 bcd.dv_devdata = devdata;
954 bcd.dv_cache = bd->bd_bcache;
957 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
959 offset = dev->d_offset * bd->bd_sectorsize;
960 offset /= BIOSDISK_SECSIZE;
962 return (bcache_strategy(&bcd, rw, dblk + offset, size,
967 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
968 char *buf, size_t *rsize)
970 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
972 uint64_t disk_blocks, offset, d_offset;
973 size_t blks, blkoff, bsize, bio_size, rest;
977 bd = bd_get_bdinfo(&dev->dd);
978 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
982 * First make sure the IO size is a multiple of 512 bytes. While we do
983 * process partial reads below, the strategy mechanism is built
984 * assuming IO is a multiple of 512B blocks. If the request is not
985 * a multiple of 512B blocks, it has to be some sort of bug.
987 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
988 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
989 size, BIOSDISK_SECSIZE);
993 DPRINTF("open_disk %p", dev);
995 offset = dblk * BIOSDISK_SECSIZE;
996 dblk = offset / bd->bd_sectorsize;
997 blkoff = offset % bd->bd_sectorsize;
1000 * Check the value of the size argument. We do have quite small
1001 * heap (64MB), but we do not know good upper limit, so we check against
1002 * INT_MAX here. This will also protect us against possible overflows
1003 * while translating block count to bytes.
1005 if (size > INT_MAX) {
1006 DPRINTF("too large I/O: %zu bytes", size);
1010 blks = size / bd->bd_sectorsize;
1011 if (blks == 0 || (size % bd->bd_sectorsize) != 0)
1014 if (dblk > dblk + blks)
1021 * Get disk blocks, this value is either for whole disk or for
1026 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1027 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1028 /* DIOCGMEDIASIZE does return bytes. */
1029 disk_blocks /= bd->bd_sectorsize;
1031 d_offset = dev->d_offset;
1033 if (disk_blocks == 0)
1034 disk_blocks = bd->bd_sectors - d_offset;
1036 /* Validate source block address. */
1037 if (dblk < d_offset || dblk >= d_offset + disk_blocks)
1041 * Truncate if we are crossing disk or partition end.
1043 if (dblk + blks >= d_offset + disk_blocks) {
1044 blks = d_offset + disk_blocks - dblk;
1045 size = blks * bd->bd_sectorsize;
1046 DPRINTF("short I/O %d", blks);
1049 bio_size = min(BIO_BUFFER_SIZE, size);
1050 while (bio_size > bd->bd_sectorsize) {
1051 bbuf = bio_alloc(bio_size);
1054 bio_size -= bd->bd_sectorsize;
1057 bio_size = V86_IO_BUFFER_SIZE;
1058 if (bio_size / bd->bd_sectorsize == 0)
1059 panic("BUG: Real mode buffer is too small");
1061 /* Use alternate 4k buffer */
1062 bbuf = PTOV(V86_IO_BUFFER);
1067 int x = min(blks, bio_size / bd->bd_sectorsize);
1069 switch (rw & F_MASK) {
1071 DPRINTF("read %d from %lld to %p", x, dblk, buf);
1072 bsize = bd->bd_sectorsize * x - blkoff;
1076 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) {
1081 bcopy(bbuf + blkoff, buf, bsize);
1084 DPRINTF("write %d from %lld to %p", x, dblk, buf);
1087 * We got offset to sector, read 1 sector to
1091 bsize = bd->bd_sectorsize - blkoff;
1092 bsize = min(bsize, rest);
1093 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1094 } else if (rest < bd->bd_sectorsize) {
1096 * The remaining block is not full
1097 * sector. Read 1 sector to bbuf.
1101 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1103 /* We can write full sector(s). */
1104 bsize = bd->bd_sectorsize * x;
1107 * Put your Data In, Put your Data out,
1108 * Put your Data In, and shake it all about
1110 bcopy(buf, bbuf + blkoff, bsize);
1111 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) {
1133 if (bbuf != PTOV(V86_IO_BUFFER))
1134 bio_free(bbuf, bio_size);
1139 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1142 static struct edd_packet packet;
1146 packet.len = sizeof(struct edd_packet);
1147 packet.count = blks;
1148 packet.off = VTOPOFF(dest);
1149 packet.seg = VTOPSEG(dest);
1151 v86.ctl = V86_FLAGS;
1152 v86.addr = DISK_BIOS;
1153 if (dowrite == BD_WR)
1154 v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */
1156 v86.eax = CMD_READ_LBA;
1157 v86.edx = bd->bd_unit;
1158 v86.ds = VTOPSEG(&packet);
1159 v86.esi = VTOPOFF(&packet);
1161 if (V86_CY(v86.efl))
1162 return (v86.eax >> 8);
1169 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1172 uint32_t x, bpc, cyl, hd, sec;
1176 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */
1178 cyl = x / bpc; /* block # / blocks per cylinder */
1179 x %= bpc; /* block offset into cylinder */
1180 hd = x / bd->bd_sec; /* offset / blocks per track */
1181 sec = x % bd->bd_sec; /* offset into track */
1183 /* correct sector number for 1-based BIOS numbering */
1187 /* CHS doesn't support cylinders > 1023. */
1191 v86.ctl = V86_FLAGS;
1192 v86.addr = DISK_BIOS;
1193 if (dowrite == BD_WR)
1194 v86.eax = CMD_WRITE_CHS | blks;
1196 v86.eax = CMD_READ_CHS | blks;
1197 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1198 v86.edx = (hd << 8) | bd->bd_unit;
1199 v86.es = VTOPSEG(dest);
1200 v86.ebx = VTOPOFF(dest);
1202 if (V86_CY(v86.efl))
1203 return (v86.eax >> 8);
1209 bd_io_workaround(bdinfo_t *bd)
1211 uint8_t buf[8 * 1024];
1213 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1217 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1218 caddr_t dest, int dowrite)
1224 /* Just in case some idiot actually tries to read/write -1 blocks... */
1229 * Workaround for a problem with some HP ProLiant BIOS failing to work
1230 * out the boot disk after installation. hrs and kuriyama discovered
1231 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1232 * discovered that an int13h call seems to cause a buffer overrun in
1233 * the bios. The problem is alleviated by doing an extra read before
1234 * the buggy read. It is not immediately known whether other models
1235 * are similarly affected.
1236 * Loop retrying the operation a couple of times. The BIOS
1239 if (dowrite == BD_RD && dblk >= 0x100000000)
1240 bd_io_workaround(bd);
1241 for (retry = 0; retry < 3; retry++) {
1242 if (bd->bd_flags & BD_MODEEDD)
1243 result = bd_edd_io(bd, dblk, blks, dest, dowrite);
1245 result = bd_chs_io(bd, dblk, blks, dest, dowrite);
1248 if (bd->bd_flags & BD_NO_MEDIA)
1249 bd->bd_flags &= ~BD_NO_MEDIA;
1253 bd_reset_disk(bd->bd_unit);
1257 * 20h controller failure
1258 * 31h no media in drive (IBM/MS INT 13 extensions)
1259 * 80h no media in drive, VMWare (Fusion)
1260 * There is no reason to repeat the IO with errors above.
1262 if (result == 0x20 || result == 0x31 || result == 0x80) {
1263 bd->bd_flags |= BD_NO_MEDIA;
1268 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
1269 if (dowrite == BD_WR) {
1270 printf("%s%d: Write %d sector(s) from %p (0x%x) "
1271 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1272 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1275 printf("%s%d: Read %d sector(s) from %lld to %p "
1276 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
1277 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
1288 * Return the BIOS geometry of a given "fixed drive" in a format
1289 * suitable for the legacy bootinfo structure. Since the kernel is
1290 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1291 * prefer to get the information directly, rather than rely on being
1292 * able to put it together from information already maintained for
1293 * different purposes and for a probably different number of drives.
1295 * For valid drives, the geometry is expected in the format (31..0)
1296 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1297 * indicated by returning the geometry of a "1.2M" PC-format floppy
1298 * disk. And, incidentally, what is returned is not the geometry as
1299 * such but the highest valid cylinder, head, and sector numbers.
1302 bd_getbigeom(int bunit)
1305 v86.ctl = V86_FLAGS;
1306 v86.addr = DISK_BIOS;
1307 v86.eax = CMD_READ_PARAM;
1308 v86.edx = 0x80 + bunit;
1310 if (V86_CY(v86.efl))
1312 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1313 (v86.edx & 0xff00) | (v86.ecx & 0x3f));
1317 * Return a suitable dev_t value for (dev).
1319 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1320 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1323 bd_getdev(struct i386_devdesc *d)
1325 struct disk_devdesc *dev;
1331 int i, unit, slice, partition;
1333 /* XXX: Assume partition 'a'. */
1337 dev = (struct disk_devdesc *)d;
1338 bd = bd_get_bdinfo(&dev->dd);
1342 biosdev = bd_unit2bios(d);
1343 DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1344 if (biosdev == -1) /* not a BIOS device */
1347 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1348 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1349 bd->bd_sectorsize) != 0) /* oops, not a viable device */
1353 slice = dev->d_slice + 1;
1354 partition = dev->d_partition;
1357 if (biosdev < 0x80) {
1358 /* floppy (or emulated floppy) or ATAPI device */
1359 if (bd->bd_type == DT_ATAPI) {
1360 /* is an ATAPI disk */
1363 /* is a floppy disk */
1367 /* assume an IDE disk */
1370 /* default root disk unit number */
1371 unit = biosdev & 0x7f;
1373 if (dev->dd.d_dev->dv_type == DEVT_CD) {
1375 * XXX: Need to examine device spec here to figure out if
1376 * SCSI or ATAPI. No idea on how to figure out device number.
1377 * All we can really pass to the kernel is what bus and device
1378 * on which bus we were booted from, which dev_t isn't well
1379 * suited to since those number don't match to unit numbers
1380 * very well. We may just need to engage in a hack where
1381 * we pass -C to the boot args if we are the boot device.
1387 /* XXX a better kludge to set the root disk unit number */
1388 if ((nip = getenv("root_disk_unit")) != NULL) {
1389 i = strtol(nip, &cp, 0);
1390 /* check for parse error */
1391 if ((cp != nip) && (*cp == 0))
1395 rootdev = MAKEBOOTDEV(major, slice, unit, partition);
1396 DPRINTF("dev is 0x%x\n", rootdev);