8010 loader: want mechanism to avoid RA with bcache
[unleashed.git] / usr / src / boot / sys / boot / i386 / libi386 / biosdisk.c
blobc30008a734737a5e77fb8767889cfe0a803c4c16
1 /*-
2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/cdefs.h>
31 * BIOS disk device handling.
33 * Ideas and algorithms from:
35 * - NetBSD libi386/biosdisk.c
36 * - FreeBSD biosboot/disk.c
40 #include <sys/disk.h>
41 #include <sys/limits.h>
42 #include <stand.h>
43 #include <machine/bootinfo.h>
44 #include <stdarg.h>
46 #include <bootstrap.h>
47 #include <btxv86.h>
48 #include <edd.h>
49 #include "disk.h"
50 #include "libi386.h"
52 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 */
60 #define WFDMAJOR 1
61 #define FDMAJOR 2
62 #define DAMAJOR 4
64 #ifdef DISK_DEBUG
65 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
66 #else
67 # define DEBUG(fmt, args...)
68 #endif
71 * List of BIOS devices, translation from disk unit number to
72 * BIOS unit number.
74 static struct bdinfo
76 int bd_unit; /* BIOS unit number */
77 int bd_cyl; /* BIOS geometry */
78 int bd_hds;
79 int bd_sec;
80 int bd_flags;
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 */
89 int bd_open; /* reference counter */
90 void *bd_bcache; /* buffer cache data */
91 } bdinfo [MAXBDDEV];
92 static int nbdinfo = 0;
94 #define BD(dev) (bdinfo[(dev)->d_unit])
96 static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
97 caddr_t dest);
98 static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
99 caddr_t dest);
100 static int bd_int13probe(struct bdinfo *bd);
102 static int bd_init(void);
103 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
104 char *buf, size_t *rsize);
105 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
106 char *buf, size_t *rsize);
107 static int bd_open(struct open_file *f, ...);
108 static int bd_close(struct open_file *f);
109 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
110 static int bd_print(int verbose);
112 struct devsw biosdisk = {
113 "disk",
114 DEVT_DISK,
115 bd_init,
116 bd_strategy,
117 bd_open,
118 bd_close,
119 bd_ioctl,
120 bd_print,
121 NULL
125 * Translate between BIOS device numbers and our private unit numbers.
128 bd_bios2unit(int biosdev)
130 int i;
132 DEBUG("looking for bios device 0x%x", biosdev);
133 for (i = 0; i < nbdinfo; i++) {
134 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
135 if (bdinfo[i].bd_unit == biosdev)
136 return (i);
138 return (-1);
142 bd_unit2bios(int unit)
145 if ((unit >= 0) && (unit < nbdinfo))
146 return (bdinfo[unit].bd_unit);
147 return (-1);
151 * Quiz the BIOS for disk devices, save a little info about them.
153 static int
154 bd_init(void)
156 int base, unit, nfd = 0;
158 /* sequence 0, 0x80 */
159 for (base = 0; base <= 0x80; base += 0x80) {
160 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
161 #ifndef VIRTUALBOX
163 * Check the BIOS equipment list for number
164 * of fixed disks.
166 if(base == 0x80 &&
167 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
168 break;
169 #endif
170 bdinfo[nbdinfo].bd_open = 0;
171 bdinfo[nbdinfo].bd_bcache = NULL;
172 bdinfo[nbdinfo].bd_unit = unit;
173 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
174 if (!bd_int13probe(&bdinfo[nbdinfo]))
175 break;
177 #ifndef BOOT2
178 /* XXX we need "disk aliases" to make this simpler */
179 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
180 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
181 #endif
182 nbdinfo++;
183 if (base == 0x80)
184 nfd++;
187 bcache_add_dev(nbdinfo);
188 return(0);
192 * Try to detect a device supported by the legacy int13 BIOS
194 static int
195 bd_int13probe(struct bdinfo *bd)
197 struct edd_params params;
198 int ret = 1; /* assume success */
200 v86.ctl = V86_FLAGS;
201 v86.addr = 0x13;
202 v86.eax = 0x800;
203 v86.edx = bd->bd_unit;
204 v86int();
206 /* Don't error out if we get bad sector number, try EDD as well */
207 if (V86_CY(v86.efl) || /* carry set */
208 (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
209 return (0); /* skip device */
211 if ((v86.ecx & 0x3f) == 0) /* absurd sector number */
212 ret = 0; /* set error */
214 /* Convert max cyl # -> # of cylinders */
215 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
216 /* Convert max head # -> # of heads */
217 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
218 bd->bd_sec = v86.ecx & 0x3f;
219 bd->bd_type = v86.ebx & 0xff;
220 bd->bd_flags |= BD_MODEINT13;
222 /* Calculate sectors count from the geometry */
223 bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
224 bd->bd_sectorsize = BIOSDISK_SECSIZE;
225 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
226 bd->bd_hds, bd->bd_sec);
228 /* Determine if we can use EDD with this device. */
229 v86.ctl = V86_FLAGS;
230 v86.addr = 0x13;
231 v86.eax = 0x4100;
232 v86.edx = bd->bd_unit;
233 v86.ebx = 0x55aa;
234 v86int();
235 if (V86_CY(v86.efl) || /* carry set */
236 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
237 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
238 return (ret); /* return code from int13 AH=08 */
240 /* EDD supported */
241 bd->bd_flags |= BD_MODEEDD1;
242 if ((v86.eax & 0xff00) >= 0x3000)
243 bd->bd_flags |= BD_MODEEDD3;
244 /* Get disk params */
245 params.len = sizeof(struct edd_params);
246 v86.ctl = V86_FLAGS;
247 v86.addr = 0x13;
248 v86.eax = 0x4800;
249 v86.edx = bd->bd_unit;
250 v86.ds = VTOPSEG(&params);
251 v86.esi = VTOPOFF(&params);
252 v86int();
253 if (!V86_CY(v86.efl)) {
254 uint64_t total;
257 * Sector size must be a multiple of 512 bytes.
258 * An alternate test would be to check power of 2,
259 * powerof2(params.sector_size).
261 if (params.sector_size % BIOSDISK_SECSIZE)
262 bd->bd_sectorsize = BIOSDISK_SECSIZE;
263 else
264 bd->bd_sectorsize = params.sector_size;
266 total = bd->bd_sectorsize * params.sectors;
267 if (params.sectors != 0) {
268 /* Only update if we did not overflow. */
269 if (total > params.sectors)
270 bd->bd_sectors = params.sectors;
273 total = (uint64_t)params.cylinders *
274 params.heads * params.sectors_per_track;
275 if (bd->bd_sectors < total)
276 bd->bd_sectors = total;
278 ret = 1;
280 DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
281 bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
282 return (ret);
286 * Print information about disks
288 static int
289 bd_print(int verbose)
291 static char line[80];
292 struct disk_devdesc dev;
293 int i, ret = 0;
295 if (nbdinfo == 0)
296 return (0);
298 printf("%s devices:", biosdisk.dv_name);
299 if ((ret = pager_output("\n")) != 0)
300 return (ret);
302 for (i = 0; i < nbdinfo; i++) {
303 snprintf(line, sizeof (line),
304 " disk%d: BIOS drive %c (%ju X %u):\n", i,
305 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
306 ('C' + bdinfo[i].bd_unit - 0x80),
307 (uintmax_t)bdinfo[i].bd_sectors,
308 bdinfo[i].bd_sectorsize);
309 ret = pager_output(line);
310 if (ret != 0)
311 return (ret);
313 dev.d_dev = &biosdisk;
314 dev.d_unit = i;
315 dev.d_slice = -1;
316 dev.d_partition = -1;
317 if (disk_open(&dev,
318 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
319 bdinfo[i].bd_sectorsize) == 0) {
320 sprintf(line, " disk%d", i);
321 ret = disk_print(&dev, line, verbose);
322 disk_close(&dev);
323 if (ret != 0)
324 return (ret);
327 return (ret);
331 * Attempt to open the disk described by (dev) for use by (f).
333 * Note that the philosophy here is "give them exactly what
334 * they ask for". This is necessary because being too "smart"
335 * about what the user might want leads to complications.
336 * (eg. given no slice or partition value, with a disk that is
337 * sliced - are they after the first BSD slice, or the DOS
338 * slice before it?)
340 static int
341 bd_open(struct open_file *f, ...)
343 struct disk_devdesc *dev;
344 struct disk_devdesc disk;
345 va_list ap;
346 uint64_t size;
347 int rc;
349 va_start(ap, f);
350 dev = va_arg(ap, struct disk_devdesc *);
351 va_end(ap);
353 if (dev->d_unit < 0 || dev->d_unit >= nbdinfo)
354 return (EIO);
355 BD(dev).bd_open++;
356 if (BD(dev).bd_bcache == NULL)
357 BD(dev).bd_bcache = bcache_allocate();
360 * Read disk size from partition.
361 * This is needed to work around buggy BIOS systems returning
362 * wrong (truncated) disk media size.
363 * During bd_probe() we tested if the mulitplication of bd_sectors
364 * would overflow so it should be safe to perform here.
366 disk.d_dev = dev->d_dev;
367 disk.d_type = dev->d_type;
368 disk.d_unit = dev->d_unit;
369 disk.d_opendata = NULL;
370 disk.d_slice = -1;
371 disk.d_partition = -1;
372 disk.d_offset = 0;
374 if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
375 BD(dev).bd_sectorsize) == 0) {
377 if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
378 size /= BD(dev).bd_sectorsize;
379 if (size > BD(dev).bd_sectors)
380 BD(dev).bd_sectors = size;
382 disk_close(&disk);
385 rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
386 BD(dev).bd_sectorsize);
387 if (rc != 0) {
388 BD(dev).bd_open--;
389 if (BD(dev).bd_open == 0) {
390 bcache_free(BD(dev).bd_bcache);
391 BD(dev).bd_bcache = NULL;
394 return (rc);
397 static int
398 bd_close(struct open_file *f)
400 struct disk_devdesc *dev;
402 dev = (struct disk_devdesc *)f->f_devdata;
403 BD(dev).bd_open--;
404 if (BD(dev).bd_open == 0) {
405 bcache_free(BD(dev).bd_bcache);
406 BD(dev).bd_bcache = NULL;
408 return (disk_close(dev));
411 static int
412 bd_ioctl(struct open_file *f, u_long cmd, void *data)
414 struct disk_devdesc *dev;
415 int rc;
417 dev = (struct disk_devdesc *)f->f_devdata;
419 rc = disk_ioctl(dev, cmd, data);
420 if (rc != ENOTTY)
421 return (rc);
423 switch (cmd) {
424 case DIOCGSECTORSIZE:
425 *(u_int *)data = BD(dev).bd_sectorsize;
426 break;
427 case DIOCGMEDIASIZE:
428 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
429 break;
430 default:
431 return (ENOTTY);
433 return (0);
436 static int
437 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
438 char *buf, size_t *rsize)
440 struct bcache_devdata bcd;
441 struct disk_devdesc *dev;
443 dev = (struct disk_devdesc *)devdata;
444 bcd.dv_strategy = bd_realstrategy;
445 bcd.dv_devdata = devdata;
446 bcd.dv_cache = BD(dev).bd_bcache;
448 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
449 buf, rsize));
452 static int
453 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
454 char *buf, size_t *rsize)
456 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
457 uint64_t disk_blocks;
458 int blks, rc;
459 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
460 char fragbuf[BIOSDISK_SECSIZE];
461 size_t fragsize;
463 fragsize = size % BIOSDISK_SECSIZE;
464 #else
465 if (size % BD(dev).bd_sectorsize)
466 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
467 #endif
469 DEBUG("open_disk %p", dev);
472 * Check the value of the size argument. We do have quite small
473 * heap (64MB), but we do not know good upper limit, so we check against
474 * INT_MAX here. This will also protect us against possible overflows
475 * while translating block count to bytes.
477 if (size > INT_MAX) {
478 DEBUG("too large read: %zu bytes", size);
479 return (EIO);
482 blks = size / BD(dev).bd_sectorsize;
483 if (dblk > dblk + blks)
484 return (EIO);
486 if (rsize)
487 *rsize = 0;
489 /* Get disk blocks, this value is either for whole disk or for partition */
490 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
491 /* DIOCGMEDIASIZE does return bytes. */
492 disk_blocks /= BD(dev).bd_sectorsize;
493 } else {
494 /* We should not get here. Just try to survive. */
495 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
498 /* Validate source block address. */
499 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
500 return (EIO);
503 * Truncate if we are crossing disk or partition end.
505 if (dblk + blks >= dev->d_offset + disk_blocks) {
506 blks = dev->d_offset + disk_blocks - dblk;
507 size = blks * BD(dev).bd_sectorsize;
508 DEBUG("short read %d", blks);
511 switch (rw & F_MASK) {
512 case F_READ:
513 DEBUG("read %d from %lld to %p", blks, dblk, buf);
515 if (blks && (rc = bd_read(dev, dblk, blks, buf))) {
516 /* Filter out floppy controller errors */
517 if (BD(dev).bd_flags != BD_FLOPPY || rc != 0x20) {
518 printf("read %d from %lld to %p, error: 0x%x\n", blks, dblk,
519 buf, rc);
521 return (EIO);
523 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
524 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
525 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
526 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
527 DEBUG("frag read error");
528 return(EIO);
530 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
531 #endif
532 break;
533 case F_WRITE :
534 DEBUG("write %d from %d to %p", blks, dblk, buf);
536 if (blks && bd_write(dev, dblk, blks, buf)) {
537 DEBUG("write error");
538 return (EIO);
540 #ifdef BD_SUPPORT_FRAGS
541 if(fragsize) {
542 DEBUG("Attempted to write a frag");
543 return (EIO);
545 #endif
546 break;
547 default:
548 /* DO NOTHING */
549 return (EROFS);
552 if (rsize)
553 *rsize = size;
554 return (0);
557 static int
558 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
559 int dowrite)
561 static struct edd_packet packet;
563 packet.len = sizeof(struct edd_packet);
564 packet.count = blks;
565 packet.off = VTOPOFF(dest);
566 packet.seg = VTOPSEG(dest);
567 packet.lba = dblk;
568 v86.ctl = V86_FLAGS;
569 v86.addr = 0x13;
570 if (dowrite)
571 /* Should we Write with verify ?? 0x4302 ? */
572 v86.eax = 0x4300;
573 else
574 v86.eax = 0x4200;
575 v86.edx = BD(dev).bd_unit;
576 v86.ds = VTOPSEG(&packet);
577 v86.esi = VTOPOFF(&packet);
578 v86int();
579 if (V86_CY(v86.efl))
580 return (v86.eax >> 8);
581 return (0);
584 static int
585 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
586 int dowrite)
588 u_int x, bpc, cyl, hd, sec;
590 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
591 x = dblk;
592 cyl = x / bpc; /* block # / blocks per cylinder */
593 x %= bpc; /* block offset into cylinder */
594 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
595 sec = x % BD(dev).bd_sec; /* offset into track */
597 /* correct sector number for 1-based BIOS numbering */
598 sec++;
600 if (cyl > 1023)
601 /* CHS doesn't support cylinders > 1023. */
602 return (1);
604 v86.ctl = V86_FLAGS;
605 v86.addr = 0x13;
606 if (dowrite)
607 v86.eax = 0x300 | blks;
608 else
609 v86.eax = 0x200 | blks;
610 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
611 v86.edx = (hd << 8) | BD(dev).bd_unit;
612 v86.es = VTOPSEG(dest);
613 v86.ebx = VTOPOFF(dest);
614 v86int();
615 if (V86_CY(v86.efl))
616 return (v86.eax >> 8);
617 return (0);
620 static int
621 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
622 int dowrite)
624 u_int x, sec, result, resid, retry, maxfer;
625 caddr_t p, xp, bbuf;
627 /* Just in case some idiot actually tries to read/write -1 blocks... */
628 if (blks < 0)
629 return (-1);
631 resid = blks;
632 p = dest;
634 /* Decide whether we have to bounce */
635 if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
636 (VTOP(dest) >> 16) != (VTOP(dest +
637 blks * BD(dev).bd_sectorsize) >> 16))) {
640 * There is a 64k physical boundary somewhere in the
641 * destination buffer, or the destination buffer is above
642 * first 1MB of physical memory so we have to arrange a
643 * suitable bounce buffer. Allocate a buffer twice as large
644 * as we need to. Use the bottom half unless there is a break
645 * there, in which case we use the top half.
647 x = V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize;
648 if (x == 0)
649 panic("BUG: Real mode buffer is too small\n");
650 x = min(x, (unsigned)blks);
651 bbuf = PTOV(V86_IO_BUFFER);
652 maxfer = x; /* limit transfers to bounce region size */
653 } else {
654 bbuf = NULL;
655 maxfer = 0;
658 while (resid > 0) {
660 * Play it safe and don't cross track boundaries.
661 * (XXX this is probably unnecessary)
663 sec = dblk % BD(dev).bd_sec; /* offset into track */
664 x = min(BD(dev).bd_sec - sec, resid);
665 if (maxfer > 0)
666 x = min(x, maxfer); /* fit bounce buffer */
668 /* where do we transfer to? */
669 xp = bbuf == NULL ? p : bbuf;
672 * Put your Data In, Put your Data out,
673 * Put your Data In, and shake it all about
675 if (dowrite && bbuf != NULL)
676 bcopy(p, bbuf, x * BD(dev).bd_sectorsize);
679 * Loop retrying the operation a couple of times. The BIOS
680 * may also retry.
682 for (retry = 0; retry < 3; retry++) {
683 /* if retrying, reset the drive */
684 if (retry > 0) {
685 v86.ctl = V86_FLAGS;
686 v86.addr = 0x13;
687 v86.eax = 0;
688 v86.edx = BD(dev).bd_unit;
689 v86int();
692 if (BD(dev).bd_flags & BD_MODEEDD1)
693 result = bd_edd_io(dev, dblk, x, xp, dowrite);
694 else
695 result = bd_chs_io(dev, dblk, x, xp, dowrite);
696 if (result == 0)
697 break;
700 if (dowrite)
701 DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
702 p, VTOP(p), dblk, result ? "failed" : "ok");
703 else
704 DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
705 dblk, p, VTOP(p), result ? "failed" : "ok");
706 if (result) {
707 return(result);
709 if (!dowrite && bbuf != NULL)
710 bcopy(bbuf, p, x * BD(dev).bd_sectorsize);
711 p += (x * BD(dev).bd_sectorsize);
712 dblk += x;
713 resid -= x;
716 /* hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
717 return(0);
720 static int
721 bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
724 return (bd_io(dev, dblk, blks, dest, 0));
727 static int
728 bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
731 return (bd_io(dev, dblk, blks, dest, 1));
735 * Return the BIOS geometry of a given "fixed drive" in a format
736 * suitable for the legacy bootinfo structure. Since the kernel is
737 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
738 * prefer to get the information directly, rather than rely on being
739 * able to put it together from information already maintained for
740 * different purposes and for a probably different number of drives.
742 * For valid drives, the geometry is expected in the format (31..0)
743 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
744 * indicated by returning the geometry of a "1.2M" PC-format floppy
745 * disk. And, incidentally, what is returned is not the geometry as
746 * such but the highest valid cylinder, head, and sector numbers.
748 u_int32_t
749 bd_getbigeom(int bunit)
752 v86.ctl = V86_FLAGS;
753 v86.addr = 0x13;
754 v86.eax = 0x800;
755 v86.edx = 0x80 + bunit;
756 v86int();
757 if (V86_CY(v86.efl))
758 return 0x4f010f;
759 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
760 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
764 * Return a suitable dev_t value for (dev).
766 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
767 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
770 bd_getdev(struct i386_devdesc *d)
772 struct disk_devdesc *dev;
773 int biosdev;
774 int major;
775 int rootdev;
776 char *nip, *cp;
777 int i, unit;
779 dev = (struct disk_devdesc *)d;
780 biosdev = bd_unit2bios(dev->d_unit);
781 DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
782 if (biosdev == -1) /* not a BIOS device */
783 return(-1);
784 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
785 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
786 return (-1);
787 else
788 disk_close(dev);
790 if (biosdev < 0x80) {
791 /* floppy (or emulated floppy) or ATAPI device */
792 if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
793 /* is an ATAPI disk */
794 major = WFDMAJOR;
795 } else {
796 /* is a floppy disk */
797 major = FDMAJOR;
799 } else {
800 /* assume an IDE disk */
801 major = WDMAJOR;
803 /* default root disk unit number */
804 unit = biosdev & 0x7f;
806 /* XXX a better kludge to set the root disk unit number */
807 if ((nip = getenv("root_disk_unit")) != NULL) {
808 i = strtol(nip, &cp, 0);
809 /* check for parse error */
810 if ((cp != nip) && (*cp == 0))
811 unit = i;
814 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
815 DEBUG("dev is 0x%x\n", rootdev);
816 return(rootdev);