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);
343 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
345 * Check the BIOS equipment list for number of fixed disks.
347 if ((bd = calloc(1, sizeof(*bd))) == NULL)
349 bd->bd_unit = base + unit;
350 if (!bd_int13probe(bd)) {
355 printf("BIOS drive %c: is %s%d\n", ('C' + unit),
356 bioshd.dv_name, unit);
358 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
360 bcache_add_dev(unit);
365 * We can't quiz, we have to be told what device to use, so this function
366 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
367 * device number to add.
377 * Information from bootable CD-ROM.
380 bd_get_diskinfo_cd(struct bdinfo *bd)
382 struct specification_packet bc_sp;
385 (void) memset(&bc_sp, 0, sizeof (bc_sp));
386 /* Set sp_size as per specification. */
387 bc_sp.sp_size = sizeof (bc_sp) - sizeof (bc_sp.sp_dummy);
390 v86.addr = DISK_BIOS;
391 v86.eax = CMD_CD_GET_STATUS;
392 v86.edx = bd->bd_unit;
393 v86.ds = VTOPSEG(&bc_sp);
394 v86.esi = VTOPOFF(&bc_sp);
397 if ((v86.eax & 0xff00) == 0 &&
398 bc_sp.sp_drive == bd->bd_unit) {
399 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
400 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
401 bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
402 bd->bd_hds = bc_sp.sp_head + 1;
403 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
405 if (bc_sp.sp_bootmedia & 0x0F) {
406 /* Floppy or hard-disk emulation */
407 bd->bd_sectorsize = BIOSDISK_SECSIZE;
410 bd->bd_sectorsize = 2048;
411 bd->bd_flags = BD_MODEEDD | BD_CDROM;
417 * If this is the boot_drive, default to non-emulation bootable CD-ROM.
419 if (ret != 0 && bd->bd_unit >= 0x88) {
423 bd->bd_sectorsize = 2048;
424 bd->bd_flags = BD_MODEEDD | BD_CDROM;
430 * Note we can not use bd_get_diskinfo_ext() nor bd_get_diskinfo_std()
431 * here - some systems do get hung with those.
434 * Still no size? use 7.961GB. The size does not really matter
435 * as long as it is reasonably large to make our reads to pass
436 * the sector count check.
438 if (bd->bd_sectors == 0)
439 bd->bd_sectors = 4173824;
450 if (!STAILQ_EMPTY(&cdinfo))
453 if ((bd = calloc(1, sizeof(*bd))) == NULL)
456 bd->bd_unit = biosdev;
457 if (bd_get_diskinfo_cd(bd) < 0) {
462 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
463 printf("BIOS CD is cd%d\n", nbcinfo);
465 bcache_add_dev(nbcinfo); /* register cd device in bcache */
470 * Return EDD version or 0 if EDD is not supported on this drive.
473 bd_check_extensions(int unit)
475 /* do not use ext calls for floppy devices */
479 /* Determine if we can use EDD with this device. */
481 v86.addr = DISK_BIOS;
482 v86.eax = CMD_CHECK_EDD;
484 v86.ebx = EDD_QUERY_MAGIC;
487 if (V86_CY(v86.efl) || /* carry set */
488 (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */
491 /* extended disk access functions (AH=42h-44h,47h,48h) supported */
492 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
495 return ((v86.eax >> 8) & 0xff);
499 bd_reset_disk(int unit)
503 v86.addr = DISK_BIOS;
510 * Read CHS info. Return 0 on success, error otherwise.
513 bd_get_diskinfo_std(struct bdinfo *bd)
515 bzero(&v86, sizeof(v86));
517 v86.addr = DISK_BIOS;
518 v86.eax = CMD_READ_PARAM;
519 v86.edx = bd->bd_unit;
522 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
523 return ((v86.eax & 0xff00) >> 8);
525 /* return custom error on absurd sector number */
526 if ((v86.ecx & 0x3f) == 0)
529 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
530 /* Convert max head # -> # of heads */
531 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
532 bd->bd_sec = v86.ecx & 0x3f;
533 bd->bd_type = v86.ebx;
534 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
540 * Read EDD info. Return 0 on success, error otherwise.
542 * Avoid stack corruption on some systems by adding extra bytes to
546 bd_get_diskinfo_ext(struct bdinfo *bd)
549 struct edd_params head;
550 struct edd_device_path_v3 device_path;
553 struct edd_params *params;
556 params = &dparams.head;
558 /* Get disk params */
559 bzero(&dparams, sizeof(dparams));
560 params->len = sizeof(struct edd_params_v3);
562 v86.addr = DISK_BIOS;
563 v86.eax = CMD_EXT_PARAM;
564 v86.edx = bd->bd_unit;
565 v86.ds = VTOPSEG(&dparams);
566 v86.esi = VTOPOFF(&dparams);
569 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
570 return ((v86.eax & 0xff00) >> 8);
573 * Sector size must be a multiple of 512 bytes.
574 * An alternate test would be to check power of 2,
575 * powerof2(params.sector_size).
576 * 16K is largest read buffer we can use at this time.
578 if (params->sector_size >= 512 &&
579 params->sector_size <= 16384 &&
580 (params->sector_size % BIOSDISK_SECSIZE) == 0)
581 bd->bd_sectorsize = params->sector_size;
583 bd->bd_cyl = params->cylinders;
584 bd->bd_hds = params->heads;
585 bd->bd_sec = params->sectors_per_track;
587 if (params->sectors != 0) {
588 total = params->sectors;
590 total = (uint64_t)params->cylinders *
591 params->heads * params->sectors_per_track;
593 bd->bd_sectors = total;
599 * Try to detect a device supported by the legacy int13 BIOS
602 bd_int13probe(bdinfo_t *bd)
606 bd->bd_flags &= ~BD_NO_MEDIA;
608 if ((bd->bd_flags & BD_CDROM) != 0) {
609 return (bd_get_diskinfo_cd(bd) == 0);
612 edd = bd_check_extensions(bd->bd_unit);
614 bd->bd_flags |= BD_MODEINT13;
616 bd->bd_flags |= BD_MODEEDD1;
618 bd->bd_flags |= BD_MODEEDD3;
620 /* Default sector size */
621 if (bd->bd_sectorsize == 0)
622 bd->bd_sectorsize = BIOSDISK_SECSIZE;
625 * Test if the floppy device is present, so we can avoid receiving
626 * bogus information from bd_get_diskinfo_std().
628 if (bd->bd_unit < 0x80) {
630 bd_reset_disk(bd->bd_unit);
634 v86.addr = DISK_BIOS;
635 v86.eax = CMD_DRIVE_TYPE;
636 v86.edx = bd->bd_unit;
638 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
644 ret = bd_get_diskinfo_ext(bd);
645 if (ret != 0 || bd->bd_sectors == 0)
646 ret = bd_get_diskinfo_std(bd);
648 if (ret != 0 && bd->bd_unit < 0x80) {
649 /* Set defaults for 1.44 floppy */
653 bd->bd_sectors = 2880;
654 /* Since we are there, there most likely is no media */
655 bd->bd_flags |= BD_NO_MEDIA;
660 if (bd->bd_sectors != 0 && edd != 0) {
664 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
665 bd->bd_sec * bd->bd_hds;
669 if ((bd->bd_flags & BD_FLOPPY) != 0)
670 dv_name = biosfd.dv_name;
672 dv_name = bioshd.dv_name;
674 printf("Can not get information about %s unit %#x\n",
675 dv_name, bd->bd_unit);
685 if (bd->bd_sectors == 0)
686 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
688 DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
689 bd->bd_hds, bd->bd_sec);
695 bd_count(bdinfo_list_t *bdi)
701 STAILQ_FOREACH(bd, bdi, bd_link)
707 * Print information about disks
710 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
713 struct disk_devdesc devd;
718 if (STAILQ_EMPTY(bdi))
721 printf("%s devices:", dev->dv_name);
722 if ((ret = pager_output("\n")) != 0)
726 STAILQ_FOREACH(bd, bdi, bd_link) {
729 switch (dev->dv_type) {
734 drive = 'C' + bd_count(&hdinfo);
741 snprintf(line, sizeof(line),
742 " %s%d: BIOS drive %c (%s%ju X %u):\n",
743 dev->dv_name, i, drive + i,
744 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
746 (uintmax_t)bd->bd_sectors,
748 if ((ret = pager_output(line)) != 0)
751 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
754 if (dev->dv_type != DEVT_DISK)
759 devd.d_slice = D_SLICENONE;
760 devd.d_partition = D_PARTNONE;
762 bd->bd_sectorsize * bd->bd_sectors,
763 bd->bd_sectorsize) == 0) {
764 snprintf(line, sizeof(line), " %s%d",
766 ret = disk_print(&devd, line, verbose);
776 fd_print(int verbose)
778 return (bd_print_common(&biosfd, &fdinfo, verbose));
782 bd_print(int verbose)
784 return (bd_print_common(&bioshd, &hdinfo, verbose));
788 cd_print(int verbose)
790 return (bd_print_common(&bioscd, &cdinfo, verbose));
794 * Read disk size from partition.
795 * This is needed to work around buggy BIOS systems returning
796 * wrong (truncated) disk media size.
797 * During bd_probe() we tested if the multiplication of bd_sectors
798 * would overflow so it should be safe to perform here.
801 bd_disk_get_sectors(struct disk_devdesc *dev)
804 struct disk_devdesc disk;
807 bd = bd_get_bdinfo(&dev->dd);
811 disk.dd.d_dev = dev->dd.d_dev;
812 disk.dd.d_unit = dev->dd.d_unit;
813 disk.d_slice = D_SLICENONE;
814 disk.d_partition = D_PARTNONE;
817 size = bd->bd_sectors * bd->bd_sectorsize;
818 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
819 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
822 return (size / bd->bd_sectorsize);
826 * Attempt to open the disk described by (dev) for use by (f).
828 * Note that the philosophy here is "give them exactly what
829 * they ask for". This is necessary because being too "smart"
830 * about what the user might want leads to complications.
831 * (eg. given no slice or partition value, with a disk that is
832 * sliced - are they after the first BSD slice, or the DOS
836 bd_open(struct open_file *f, ...)
839 struct disk_devdesc *dev;
844 dev = va_arg(ap, struct disk_devdesc *);
847 bd = bd_get_bdinfo(&dev->dd);
851 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
852 if (!bd_int13probe(bd))
854 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
857 if (bd->bd_bcache == NULL)
858 bd->bd_bcache = bcache_allocate();
860 if (bd->bd_open == 0)
861 bd->bd_sectors = bd_disk_get_sectors(dev);
865 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
866 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
870 if (bd->bd_open == 0) {
871 bcache_free(bd->bd_bcache);
872 bd->bd_bcache = NULL;
880 bd_close(struct open_file *f)
882 struct disk_devdesc *dev;
886 dev = (struct disk_devdesc *)f->f_devdata;
887 bd = bd_get_bdinfo(&dev->dd);
892 if (bd->bd_open == 0) {
893 bcache_free(bd->bd_bcache);
894 bd->bd_bcache = NULL;
896 if (dev->dd.d_dev->dv_type == DEVT_DISK)
897 rc = disk_close(dev);
902 bd_ioctl(struct open_file *f, u_long cmd, void *data)
905 struct disk_devdesc *dev;
908 dev = (struct disk_devdesc *)f->f_devdata;
909 bd = bd_get_bdinfo(&dev->dd);
913 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
914 rc = disk_ioctl(dev, cmd, data);
920 case DIOCGSECTORSIZE:
921 *(uint32_t *)data = bd->bd_sectorsize;
924 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
933 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
934 char *buf, size_t *rsize)
937 struct bcache_devdata bcd;
938 struct disk_devdesc *dev;
941 dev = (struct disk_devdesc *)devdata;
942 bd = bd_get_bdinfo(&dev->dd);
946 bcd.dv_strategy = bd_realstrategy;
947 bcd.dv_devdata = devdata;
948 bcd.dv_cache = bd->bd_bcache;
951 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
953 offset = dev->d_offset * bd->bd_sectorsize;
954 offset /= BIOSDISK_SECSIZE;
956 return (bcache_strategy(&bcd, rw, dblk + offset, size,
961 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
962 char *buf, size_t *rsize)
964 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
966 uint64_t disk_blocks, offset, d_offset;
967 size_t blks, blkoff, bsize, bio_size, rest;
971 bd = bd_get_bdinfo(&dev->dd);
972 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
976 * First make sure the IO size is a multiple of 512 bytes. While we do
977 * process partial reads below, the strategy mechanism is built
978 * assuming IO is a multiple of 512B blocks. If the request is not
979 * a multiple of 512B blocks, it has to be some sort of bug.
981 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
982 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
983 size, BIOSDISK_SECSIZE);
987 DPRINTF("open_disk %p", dev);
989 offset = dblk * BIOSDISK_SECSIZE;
990 dblk = offset / bd->bd_sectorsize;
991 blkoff = offset % bd->bd_sectorsize;
994 * Check the value of the size argument. We do have quite small
995 * heap (64MB), but we do not know good upper limit, so we check against
996 * INT_MAX here. This will also protect us against possible overflows
997 * while translating block count to bytes.
999 if (size > INT_MAX) {
1000 DPRINTF("too large I/O: %zu bytes", size);
1004 blks = size / bd->bd_sectorsize;
1005 if (blks == 0 || (size % bd->bd_sectorsize) != 0)
1008 if (dblk > dblk + blks)
1015 * Get disk blocks, this value is either for whole disk or for
1020 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1021 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1022 /* DIOCGMEDIASIZE does return bytes. */
1023 disk_blocks /= bd->bd_sectorsize;
1025 d_offset = dev->d_offset;
1027 if (disk_blocks == 0)
1028 disk_blocks = bd->bd_sectors - d_offset;
1030 /* Validate source block address. */
1031 if (dblk < d_offset || dblk >= d_offset + disk_blocks)
1035 * Truncate if we are crossing disk or partition end.
1037 if (dblk + blks >= d_offset + disk_blocks) {
1038 blks = d_offset + disk_blocks - dblk;
1039 size = blks * bd->bd_sectorsize;
1040 DPRINTF("short I/O %d", blks);
1043 bio_size = min(BIO_BUFFER_SIZE, size);
1044 while (bio_size > bd->bd_sectorsize) {
1045 bbuf = bio_alloc(bio_size);
1048 bio_size -= bd->bd_sectorsize;
1051 bio_size = V86_IO_BUFFER_SIZE;
1052 if (bio_size / bd->bd_sectorsize == 0)
1053 panic("BUG: Real mode buffer is too small");
1055 /* Use alternate 4k buffer */
1056 bbuf = PTOV(V86_IO_BUFFER);
1061 int x = min(blks, bio_size / bd->bd_sectorsize);
1063 switch (rw & F_MASK) {
1065 DPRINTF("read %d from %lld to %p", x, dblk, buf);
1066 bsize = bd->bd_sectorsize * x - blkoff;
1070 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) {
1075 bcopy(bbuf + blkoff, buf, bsize);
1078 DPRINTF("write %d from %lld to %p", x, dblk, buf);
1081 * We got offset to sector, read 1 sector to
1085 bsize = bd->bd_sectorsize - blkoff;
1086 bsize = min(bsize, rest);
1087 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1088 } else if (rest < bd->bd_sectorsize) {
1090 * The remaining block is not full
1091 * sector. Read 1 sector to bbuf.
1095 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1097 /* We can write full sector(s). */
1098 bsize = bd->bd_sectorsize * x;
1101 * Put your Data In, Put your Data out,
1102 * Put your Data In, and shake it all about
1104 bcopy(buf, bbuf + blkoff, bsize);
1105 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) {
1127 if (bbuf != PTOV(V86_IO_BUFFER))
1128 bio_free(bbuf, bio_size);
1133 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1136 static struct edd_packet packet;
1138 packet.len = sizeof(struct edd_packet);
1139 packet.count = blks;
1140 packet.off = VTOPOFF(dest);
1141 packet.seg = VTOPSEG(dest);
1143 v86.ctl = V86_FLAGS;
1144 v86.addr = DISK_BIOS;
1145 if (dowrite == BD_WR)
1146 v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */
1148 v86.eax = CMD_READ_LBA;
1149 v86.edx = bd->bd_unit;
1150 v86.ds = VTOPSEG(&packet);
1151 v86.esi = VTOPOFF(&packet);
1153 if (V86_CY(v86.efl))
1154 return (v86.eax >> 8);
1159 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1162 uint32_t x, bpc, cyl, hd, sec;
1164 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */
1166 cyl = x / bpc; /* block # / blocks per cylinder */
1167 x %= bpc; /* block offset into cylinder */
1168 hd = x / bd->bd_sec; /* offset / blocks per track */
1169 sec = x % bd->bd_sec; /* offset into track */
1171 /* correct sector number for 1-based BIOS numbering */
1175 /* CHS doesn't support cylinders > 1023. */
1179 v86.ctl = V86_FLAGS;
1180 v86.addr = DISK_BIOS;
1181 if (dowrite == BD_WR)
1182 v86.eax = CMD_WRITE_CHS | blks;
1184 v86.eax = CMD_READ_CHS | blks;
1185 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1186 v86.edx = (hd << 8) | bd->bd_unit;
1187 v86.es = VTOPSEG(dest);
1188 v86.ebx = VTOPOFF(dest);
1190 if (V86_CY(v86.efl))
1191 return (v86.eax >> 8);
1196 bd_io_workaround(bdinfo_t *bd)
1198 uint8_t buf[8 * 1024];
1200 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1204 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1205 caddr_t dest, int dowrite)
1209 /* Just in case some idiot actually tries to read/write -1 blocks... */
1214 * Workaround for a problem with some HP ProLiant BIOS failing to work
1215 * out the boot disk after installation. hrs and kuriyama discovered
1216 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1217 * discovered that an int13h call seems to cause a buffer overrun in
1218 * the bios. The problem is alleviated by doing an extra read before
1219 * the buggy read. It is not immediately known whether other models
1220 * are similarly affected.
1221 * Loop retrying the operation a couple of times. The BIOS
1224 if (dowrite == BD_RD && dblk >= 0x100000000)
1225 bd_io_workaround(bd);
1226 for (retry = 0; retry < 3; retry++) {
1227 if (bd->bd_flags & BD_MODEEDD)
1228 result = bd_edd_io(bd, dblk, blks, dest, dowrite);
1230 result = bd_chs_io(bd, dblk, blks, dest, dowrite);
1233 if (bd->bd_flags & BD_NO_MEDIA)
1234 bd->bd_flags &= ~BD_NO_MEDIA;
1238 bd_reset_disk(bd->bd_unit);
1242 * 20h controller failure
1243 * 31h no media in drive (IBM/MS INT 13 extensions)
1244 * 80h no media in drive, VMWare (Fusion)
1245 * There is no reason to repeat the IO with errors above.
1247 if (result == 0x20 || result == 0x31 || result == 0x80) {
1248 bd->bd_flags |= BD_NO_MEDIA;
1253 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
1254 if (dowrite == BD_WR) {
1255 printf("%s%d: Write %d sector(s) from %p (0x%x) "
1256 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1257 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1260 printf("%s%d: Read %d sector(s) from %lld to %p "
1261 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
1262 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
1271 * Return the BIOS geometry of a given "fixed drive" in a format
1272 * suitable for the legacy bootinfo structure. Since the kernel is
1273 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1274 * prefer to get the information directly, rather than rely on being
1275 * able to put it together from information already maintained for
1276 * different purposes and for a probably different number of drives.
1278 * For valid drives, the geometry is expected in the format (31..0)
1279 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1280 * indicated by returning the geometry of a "1.2M" PC-format floppy
1281 * disk. And, incidentally, what is returned is not the geometry as
1282 * such but the highest valid cylinder, head, and sector numbers.
1285 bd_getbigeom(int bunit)
1288 v86.ctl = V86_FLAGS;
1289 v86.addr = DISK_BIOS;
1290 v86.eax = CMD_READ_PARAM;
1291 v86.edx = 0x80 + bunit;
1293 if (V86_CY(v86.efl))
1295 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1296 (v86.edx & 0xff00) | (v86.ecx & 0x3f));
1300 * Return a suitable dev_t value for (dev).
1302 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1303 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1306 bd_getdev(struct i386_devdesc *d)
1308 struct disk_devdesc *dev;
1314 int i, unit, slice, partition;
1316 /* XXX: Assume partition 'a'. */
1320 dev = (struct disk_devdesc *)d;
1321 bd = bd_get_bdinfo(&dev->dd);
1325 biosdev = bd_unit2bios(d);
1326 DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1327 if (biosdev == -1) /* not a BIOS device */
1330 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1331 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1332 bd->bd_sectorsize) != 0) /* oops, not a viable device */
1336 slice = dev->d_slice + 1;
1337 partition = dev->d_partition;
1340 if (biosdev < 0x80) {
1341 /* floppy (or emulated floppy) or ATAPI device */
1342 if (bd->bd_type == DT_ATAPI) {
1343 /* is an ATAPI disk */
1346 /* is a floppy disk */
1350 /* assume an IDE disk */
1353 /* default root disk unit number */
1354 unit = biosdev & 0x7f;
1356 if (dev->dd.d_dev->dv_type == DEVT_CD) {
1358 * XXX: Need to examine device spec here to figure out if
1359 * SCSI or ATAPI. No idea on how to figure out device number.
1360 * All we can really pass to the kernel is what bus and device
1361 * on which bus we were booted from, which dev_t isn't well
1362 * suited to since those number don't match to unit numbers
1363 * very well. We may just need to engage in a hack where
1364 * we pass -C to the boot args if we are the boot device.
1370 /* XXX a better kludge to set the root disk unit number */
1371 if ((nip = getenv("root_disk_unit")) != NULL) {
1372 i = strtol(nip, &cp, 0);
1373 /* check for parse error */
1374 if ((cp != nip) && (*cp == 0))
1378 rootdev = MAKEBOOTDEV(major, slice, unit, partition);
1379 DPRINTF("dev is 0x%x\n", rootdev);