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
43 #include <machine/bootinfo.h>
46 #include <bootstrap.h>
52 CTASSERT(sizeof(struct i386_devdesc) >= sizeof(struct disk_devdesc));
54 #define BIOS_NUMDRIVES 0x475
55 #define BIOSDISK_SECSIZE 512
56 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
58 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
59 #define WDMAJOR 0 /* major numbers for devices we frontend for */
65 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
67 # define DEBUG(fmt, args...)
71 * List of BIOS devices, translation from disk unit number to
76 int bd_unit; /* BIOS unit number */
77 int bd_cyl; /* BIOS geometry */
81 #define BD_MODEINT13 0x0000
82 #define BD_MODEEDD1 0x0001
83 #define BD_MODEEDD3 0x0002
84 #define BD_MODEMASK 0x0003
85 #define BD_FLOPPY 0x0004
86 int bd_type; /* BIOS 'drive type' (floppy only) */
87 uint16_t bd_sectorsize; /* Sector size */
88 uint64_t bd_sectors; /* Disk size */
90 static int nbdinfo = 0;
92 #define BD(dev) (bdinfo[(dev)->d_unit])
94 static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
96 static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
98 static int bd_int13probe(struct bdinfo *bd);
100 static int bd_init(void);
101 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
102 char *buf, size_t *rsize);
103 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
104 size_t size, char *buf, size_t *rsize);
105 static int bd_open(struct open_file *f, ...);
106 static int bd_close(struct open_file *f);
107 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
108 static void bd_print(int verbose);
109 static void bd_cleanup(void);
111 struct devsw biosdisk = {
124 * Translate between BIOS device numbers and our private unit numbers.
127 bd_bios2unit(int biosdev)
131 DEBUG("looking for bios device 0x%x", biosdev);
132 for (i = 0; i < nbdinfo; i++) {
133 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
134 if (bdinfo[i].bd_unit == biosdev)
141 bd_unit2bios(int unit)
144 if ((unit >= 0) && (unit < nbdinfo))
145 return (bdinfo[unit].bd_unit);
150 * Quiz the BIOS for disk devices, save a little info about them.
155 int base, unit, nfd = 0;
157 /* sequence 0, 0x80 */
158 for (base = 0; base <= 0x80; base += 0x80) {
159 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
162 * Check the BIOS equipment list for number
166 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
169 bdinfo[nbdinfo].bd_unit = unit;
170 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
171 if (!bd_int13probe(&bdinfo[nbdinfo]))
174 /* XXX we need "disk aliases" to make this simpler */
175 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
176 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
189 disk_cleanup(&biosdisk);
193 * Try to detect a device supported by the legacy int13 BIOS
196 bd_int13probe(struct bdinfo *bd)
198 struct edd_params params;
203 v86.edx = bd->bd_unit;
206 if (V86_CY(v86.efl) || /* carry set */
207 (v86.ecx & 0x3f) == 0 || /* absurd sector number */
208 (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
209 return (0); /* skip device */
211 /* Convert max cyl # -> # of cylinders */
212 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
213 /* Convert max head # -> # of heads */
214 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
215 bd->bd_sec = v86.ecx & 0x3f;
216 bd->bd_type = v86.ebx & 0xff;
217 bd->bd_flags |= BD_MODEINT13;
219 /* Calculate sectors count from the geometry */
220 bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
221 bd->bd_sectorsize = BIOSDISK_SECSIZE;
222 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
223 bd->bd_hds, bd->bd_sec);
225 /* Determine if we can use EDD with this device. */
229 v86.edx = bd->bd_unit;
232 if (V86_CY(v86.efl) || /* carry set */
233 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
234 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
237 bd->bd_flags |= BD_MODEEDD1;
238 if ((v86.eax & 0xff00) >= 0x3000)
239 bd->bd_flags |= BD_MODEEDD3;
240 /* Get disk params */
241 params.len = sizeof(struct edd_params);
245 v86.edx = bd->bd_unit;
246 v86.ds = VTOPSEG(¶ms);
247 v86.esi = VTOPOFF(¶ms);
249 if (!V86_CY(v86.efl)) {
250 bd->bd_sectors = params.sectors;
251 bd->bd_sectorsize = params.sector_size;
253 DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
254 bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
259 * Print information about disks
262 bd_print(int verbose)
264 static char line[80];
265 struct disk_devdesc dev;
268 for (i = 0; i < nbdinfo; i++) {
269 sprintf(line, " disk%d: BIOS drive %c:\n", i,
270 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
271 ('C' + bdinfo[i].bd_unit - 0x80));
273 dev.d_dev = &biosdisk;
276 dev.d_partition = -1;
278 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
279 bdinfo[i].bd_sectorsize,
280 (bdinfo[i].bd_flags & BD_FLOPPY) ?
281 DISK_F_NOCACHE: 0) == 0) {
282 sprintf(line, " disk%d", i);
283 disk_print(&dev, line, verbose);
290 * Attempt to open the disk described by (dev) for use by (f).
292 * Note that the philosophy here is "give them exactly what
293 * they ask for". This is necessary because being too "smart"
294 * about what the user might want leads to complications.
295 * (eg. given no slice or partition value, with a disk that is
296 * sliced - are they after the first BSD slice, or the DOS
300 bd_open(struct open_file *f, ...)
302 struct disk_devdesc *dev;
306 dev = va_arg(ap, struct disk_devdesc *);
309 if (dev->d_unit < 0 || dev->d_unit >= nbdinfo)
312 return (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
313 BD(dev).bd_sectorsize, (BD(dev).bd_flags & BD_FLOPPY) ?
318 bd_close(struct open_file *f)
320 struct disk_devdesc *dev;
322 dev = (struct disk_devdesc *)f->f_devdata;
323 return (disk_close(dev));
327 bd_ioctl(struct open_file *f, u_long cmd, void *data)
329 struct disk_devdesc *dev;
331 dev = (struct disk_devdesc *)f->f_devdata;
333 case DIOCGSECTORSIZE:
334 *(u_int *)data = BD(dev).bd_sectorsize;
337 *(off_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
346 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
349 struct bcache_devdata bcd;
350 struct disk_devdesc *dev;
352 dev = (struct disk_devdesc *)devdata;
353 bcd.dv_strategy = bd_realstrategy;
354 bcd.dv_devdata = devdata;
355 return (bcache_strategy(&bcd, BD(dev).bd_unit, rw, dblk + dev->d_offset,
360 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
363 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
365 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
366 char fragbuf[BIOSDISK_SECSIZE];
369 fragsize = size % BIOSDISK_SECSIZE;
371 if (size % BD(dev).bd_sectorsize)
372 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
375 DEBUG("open_disk %p", dev);
376 blks = size / BD(dev).bd_sectorsize;
382 DEBUG("read %d from %lld to %p", blks, dblk, buf);
384 if (blks && bd_read(dev, dblk, blks, buf)) {
388 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
389 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
390 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
391 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
392 DEBUG("frag read error");
395 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
399 DEBUG("write %d from %d to %p", blks, dblk, buf);
401 if (blks && bd_write(dev, dblk, blks, buf)) {
402 DEBUG("write error");
405 #ifdef BD_SUPPORT_FRAGS
407 DEBUG("Attempted to write a frag");
422 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
423 #define FLOPPY_BOUNCEBUF 18
426 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
429 static struct edd_packet packet;
431 packet.len = sizeof(struct edd_packet);
433 packet.off = VTOPOFF(dest);
434 packet.seg = VTOPSEG(dest);
439 /* Should we Write with verify ?? 0x4302 ? */
443 v86.edx = BD(dev).bd_unit;
444 v86.ds = VTOPSEG(&packet);
445 v86.esi = VTOPOFF(&packet);
447 return (V86_CY(v86.efl));
451 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
454 u_int x, bpc, cyl, hd, sec;
456 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
458 cyl = x / bpc; /* block # / blocks per cylinder */
459 x %= bpc; /* block offset into cylinder */
460 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
461 sec = x % BD(dev).bd_sec; /* offset into track */
463 /* correct sector number for 1-based BIOS numbering */
467 /* CHS doesn't support cylinders > 1023. */
473 v86.eax = 0x300 | blks;
475 v86.eax = 0x200 | blks;
476 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
477 v86.edx = (hd << 8) | BD(dev).bd_unit;
478 v86.es = VTOPSEG(dest);
479 v86.ebx = VTOPOFF(dest);
481 return (V86_CY(v86.efl));
485 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, int write)
487 u_int x, sec, result, resid, retry, maxfer;
488 caddr_t p, xp, bbuf, breg;
490 /* Just in case some idiot actually tries to read/write -1 blocks... */
497 /* Decide whether we have to bounce */
498 if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
499 (VTOP(dest) >> 16) != (VTOP(dest +
500 blks * BD(dev).bd_sectorsize) >> 16))) {
503 * There is a 64k physical boundary somewhere in the
504 * destination buffer, or the destination buffer is above
505 * first 1MB of physical memory so we have to arrange a
506 * suitable bounce buffer. Allocate a buffer twice as large
507 * as we need to. Use the bottom half unless there is a break
508 * there, in which case we use the top half.
510 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
511 bbuf = alloca(x * 2 * BD(dev).bd_sectorsize);
512 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
513 ((u_int32_t)VTOP(bbuf + x * BD(dev).bd_sectorsize) & 0xffff0000)) {
516 breg = bbuf + x * BD(dev).bd_sectorsize;
518 maxfer = x; /* limit transfers to bounce region size */
526 * Play it safe and don't cross track boundaries.
527 * (XXX this is probably unnecessary)
529 sec = dblk % BD(dev).bd_sec; /* offset into track */
530 x = min(BD(dev).bd_sec - sec, resid);
532 x = min(x, maxfer); /* fit bounce buffer */
534 /* where do we transfer to? */
535 xp = bbuf == NULL ? p : breg;
538 * Put your Data In, Put your Data out,
539 * Put your Data In, and shake it all about
541 if (write && bbuf != NULL)
542 bcopy(p, breg, x * BD(dev).bd_sectorsize);
545 * Loop retrying the operation a couple of times. The BIOS
548 for (retry = 0; retry < 3; retry++) {
549 /* if retrying, reset the drive */
554 v86.edx = BD(dev).bd_unit;
558 if (BD(dev).bd_flags & BD_MODEEDD1)
559 result = bd_edd_io(dev, dblk, x, xp, write);
561 result = bd_chs_io(dev, dblk, x, xp, write);
567 DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
568 p, VTOP(p), dblk, result ? "failed" : "ok");
570 DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
571 dblk, p, VTOP(p), result ? "failed" : "ok");
575 if (!write && bbuf != NULL)
576 bcopy(breg, p, x * BD(dev).bd_sectorsize);
577 p += (x * BD(dev).bd_sectorsize);
582 /* hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
587 bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
590 return (bd_io(dev, dblk, blks, dest, 0));
594 bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
597 return (bd_io(dev, dblk, blks, dest, 1));
601 * Return the BIOS geometry of a given "fixed drive" in a format
602 * suitable for the legacy bootinfo structure. Since the kernel is
603 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
604 * prefer to get the information directly, rather than rely on being
605 * able to put it together from information already maintained for
606 * different purposes and for a probably different number of drives.
608 * For valid drives, the geometry is expected in the format (31..0)
609 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
610 * indicated by returning the geometry of a "1.2M" PC-format floppy
611 * disk. And, incidentally, what is returned is not the geometry as
612 * such but the highest valid cylinder, head, and sector numbers.
615 bd_getbigeom(int bunit)
621 v86.edx = 0x80 + bunit;
625 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
626 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
630 * Return a suitable dev_t value for (dev).
632 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
633 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
636 bd_getdev(struct i386_devdesc *d)
638 struct disk_devdesc *dev;
645 dev = (struct disk_devdesc *)d;
646 biosdev = bd_unit2bios(dev->d_unit);
647 DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
648 if (biosdev == -1) /* not a BIOS device */
650 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
651 BD(dev).bd_sectorsize,(BD(dev).bd_flags & BD_FLOPPY) ?
652 DISK_F_NOCACHE: 0) != 0) /* oops, not a viable device */
657 if (biosdev < 0x80) {
658 /* floppy (or emulated floppy) or ATAPI device */
659 if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
660 /* is an ATAPI disk */
663 /* is a floppy disk */
667 /* assume an IDE disk */
670 /* default root disk unit number */
671 unit = biosdev & 0x7f;
673 /* XXX a better kludge to set the root disk unit number */
674 if ((nip = getenv("root_disk_unit")) != NULL) {
675 i = strtol(nip, &cp, 0);
676 /* check for parse error */
677 if ((cp != nip) && (*cp == 0))
681 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
682 DEBUG("dev is 0x%x\n", rootdev);