9740 libi386: use snprintf in biosdisk and style fixes
[unleashed.git] / usr / src / boot / sys / boot / i386 / libi386 / biosdisk.c
blobb60f989d0066acf0223fb74285015ca84449d059
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 #define BIOS_NUMDRIVES 0x475
53 #define BIOSDISK_SECSIZE 512
54 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
56 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
57 #define WDMAJOR 0 /* major numbers for devices we frontend for */
58 #define WFDMAJOR 1
59 #define FDMAJOR 2
60 #define DAMAJOR 4
62 #ifdef DISK_DEBUG
63 #define DEBUG(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
64 #else
65 #define DEBUG(fmt, args...)
66 #endif
69 * List of BIOS devices, translation from disk unit number to
70 * BIOS unit number.
72 static struct bdinfo
74 int bd_unit; /* BIOS unit number */
75 int bd_cyl; /* BIOS geometry */
76 int bd_hds;
77 int bd_sec;
78 int bd_flags;
79 #define BD_MODEINT13 0x0000
80 #define BD_MODEEDD1 0x0001
81 #define BD_MODEEDD3 0x0002
82 #define BD_MODEMASK 0x0003
83 #define BD_FLOPPY 0x0004
84 int bd_type; /* BIOS 'drive type' (floppy only) */
85 uint16_t bd_sectorsize; /* Sector size */
86 uint64_t bd_sectors; /* Disk size */
87 int bd_open; /* reference counter */
88 void *bd_bcache; /* buffer cache data */
89 } bdinfo [MAXBDDEV];
90 static int nbdinfo = 0;
92 #define BD(dev) (bdinfo[(dev)->dd.d_unit])
94 static int bd_io(struct disk_devdesc *, daddr_t, int, caddr_t, int);
95 static int bd_int13probe(struct bdinfo *bd);
97 static int bd_init(void);
98 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
99 char *buf, size_t *rsize);
100 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
101 char *buf, size_t *rsize);
102 static int bd_open(struct open_file *f, ...);
103 static int bd_close(struct open_file *f);
104 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
105 static int bd_print(int verbose);
107 struct devsw biosdisk = {
108 "disk",
109 DEVT_DISK,
110 bd_init,
111 bd_strategy,
112 bd_open,
113 bd_close,
114 bd_ioctl,
115 bd_print,
116 NULL
120 * Translate between BIOS device numbers and our private unit numbers.
123 bd_bios2unit(int biosdev)
125 int i;
127 DEBUG("looking for bios device 0x%x", biosdev);
128 for (i = 0; i < nbdinfo; i++) {
129 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
130 if (bdinfo[i].bd_unit == biosdev)
131 return (i);
133 return (-1);
137 bd_unit2bios(int unit)
140 if ((unit >= 0) && (unit < nbdinfo))
141 return (bdinfo[unit].bd_unit);
142 return (-1);
146 * Quiz the BIOS for disk devices, save a little info about them.
148 static int
149 bd_init(void)
151 int base, unit, nfd = 0;
153 /* sequence 0, 0x80 */
154 for (base = 0; base <= 0x80; base += 0x80) {
155 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
156 #ifndef VIRTUALBOX
158 * Check the BIOS equipment list for number
159 * of fixed disks.
161 if (base == 0x80 &&
162 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
163 break;
164 #endif
165 bdinfo[nbdinfo].bd_open = 0;
166 bdinfo[nbdinfo].bd_bcache = NULL;
167 bdinfo[nbdinfo].bd_unit = unit;
168 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
169 if (!bd_int13probe(&bdinfo[nbdinfo]))
170 break;
172 #ifndef BOOT2
173 /* XXX we need "disk aliases" to make this simpler */
174 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
175 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
176 #endif
177 nbdinfo++;
178 if (base == 0x80)
179 nfd++;
182 bcache_add_dev(nbdinfo);
183 return (0);
187 * Try to detect a device supported by the legacy int13 BIOS
189 static int
190 bd_int13probe(struct bdinfo *bd)
192 struct edd_params params;
193 int ret = 1; /* assume success */
195 v86.ctl = V86_FLAGS;
196 v86.addr = 0x13;
197 v86.eax = 0x800;
198 v86.edx = bd->bd_unit;
199 v86int();
201 /* Don't error out if we get bad sector number, try EDD as well */
202 if (V86_CY(v86.efl) || /* carry set */
203 (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
204 return (0); /* skip device */
206 if ((v86.ecx & 0x3f) == 0) /* absurd sector number */
207 ret = 0; /* set error */
209 /* Convert max cyl # -> # of cylinders */
210 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
211 /* Convert max head # -> # of heads */
212 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
213 bd->bd_sec = v86.ecx & 0x3f;
214 bd->bd_type = v86.ebx & 0xff;
215 bd->bd_flags |= BD_MODEINT13;
217 /* Calculate sectors count from the geometry */
218 bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
219 bd->bd_sectorsize = BIOSDISK_SECSIZE;
220 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
221 bd->bd_hds, bd->bd_sec);
223 /* Determine if we can use EDD with this device. */
224 v86.ctl = V86_FLAGS;
225 v86.addr = 0x13;
226 v86.eax = 0x4100;
227 v86.edx = bd->bd_unit;
228 v86.ebx = 0x55aa;
229 v86int();
230 if (V86_CY(v86.efl) || /* carry set */
231 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
232 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
233 return (ret); /* return code from int13 AH=08 */
235 /* EDD supported */
236 bd->bd_flags |= BD_MODEEDD1;
237 if ((v86.eax & 0xff00) >= 0x3000)
238 bd->bd_flags |= BD_MODEEDD3;
239 /* Get disk params */
240 params.len = sizeof (struct edd_params);
241 v86.ctl = V86_FLAGS;
242 v86.addr = 0x13;
243 v86.eax = 0x4800;
244 v86.edx = bd->bd_unit;
245 v86.ds = VTOPSEG(&params);
246 v86.esi = VTOPOFF(&params);
247 v86int();
248 if (!V86_CY(v86.efl)) {
249 uint64_t total;
252 * Sector size must be a multiple of 512 bytes.
253 * An alternate test would be to check power of 2,
254 * powerof2(params.sector_size).
256 if (params.sector_size % BIOSDISK_SECSIZE)
257 bd->bd_sectorsize = BIOSDISK_SECSIZE;
258 else
259 bd->bd_sectorsize = params.sector_size;
261 total = bd->bd_sectorsize * params.sectors;
262 if (params.sectors != 0) {
263 /* Only update if we did not overflow. */
264 if (total > params.sectors)
265 bd->bd_sectors = params.sectors;
268 total = (uint64_t)params.cylinders *
269 params.heads * params.sectors_per_track;
270 if (bd->bd_sectors < total)
271 bd->bd_sectors = total;
273 ret = 1;
275 DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
276 bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
277 return (ret);
281 * Print information about disks
283 static int
284 bd_print(int verbose)
286 static char line[80];
287 struct disk_devdesc dev;
288 int i, ret = 0;
290 if (nbdinfo == 0)
291 return (0);
293 printf("%s devices:", biosdisk.dv_name);
294 if ((ret = pager_output("\n")) != 0)
295 return (ret);
297 for (i = 0; i < nbdinfo; i++) {
298 snprintf(line, sizeof (line),
299 " disk%d: BIOS drive %c (%ju X %u):\n", i,
300 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
301 ('C' + bdinfo[i].bd_unit - 0x80),
302 (uintmax_t)bdinfo[i].bd_sectors,
303 bdinfo[i].bd_sectorsize);
304 if ((ret = pager_output(line)) != 0)
305 break;
307 dev.dd.d_dev = &biosdisk;
308 dev.dd.d_unit = i;
309 dev.d_slice = -1;
310 dev.d_partition = -1;
311 if (disk_open(&dev,
312 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
313 bdinfo[i].bd_sectorsize) == 0) {
314 snprintf(line, sizeof (line), " disk%d", i);
315 ret = disk_print(&dev, line, verbose);
316 disk_close(&dev);
317 if (ret != 0)
318 break;
321 return (ret);
325 * Attempt to open the disk described by (dev) for use by (f).
327 * Note that the philosophy here is "give them exactly what
328 * they ask for". This is necessary because being too "smart"
329 * about what the user might want leads to complications.
330 * (eg. given no slice or partition value, with a disk that is
331 * sliced - are they after the first BSD slice, or the DOS
332 * slice before it?)
334 static int
335 bd_open(struct open_file *f, ...)
337 struct disk_devdesc *dev;
338 struct disk_devdesc disk;
339 va_list ap;
340 uint64_t size;
341 int rc;
343 va_start(ap, f);
344 dev = va_arg(ap, struct disk_devdesc *);
345 va_end(ap);
347 if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo)
348 return (EIO);
349 BD(dev).bd_open++;
350 if (BD(dev).bd_bcache == NULL)
351 BD(dev).bd_bcache = bcache_allocate();
354 * Read disk size from partition.
355 * This is needed to work around buggy BIOS systems returning
356 * wrong (truncated) disk media size.
357 * During bd_probe() we tested if the mulitplication of bd_sectors
358 * would overflow so it should be safe to perform here.
360 disk.dd.d_dev = dev->dd.d_dev;
361 disk.dd.d_unit = dev->dd.d_unit;
362 disk.d_slice = -1;
363 disk.d_partition = -1;
364 disk.d_offset = 0;
366 if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
367 BD(dev).bd_sectorsize) == 0) {
369 if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
370 size /= BD(dev).bd_sectorsize;
371 if (size > BD(dev).bd_sectors)
372 BD(dev).bd_sectors = size;
374 disk_close(&disk);
377 rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
378 BD(dev).bd_sectorsize);
379 if (rc != 0) {
380 BD(dev).bd_open--;
381 if (BD(dev).bd_open == 0) {
382 bcache_free(BD(dev).bd_bcache);
383 BD(dev).bd_bcache = NULL;
386 return (rc);
389 static int
390 bd_close(struct open_file *f)
392 struct disk_devdesc *dev;
394 dev = (struct disk_devdesc *)f->f_devdata;
395 BD(dev).bd_open--;
396 if (BD(dev).bd_open == 0) {
397 bcache_free(BD(dev).bd_bcache);
398 BD(dev).bd_bcache = NULL;
400 return (disk_close(dev));
403 static int
404 bd_ioctl(struct open_file *f, u_long cmd, void *data)
406 struct disk_devdesc *dev;
407 int rc;
409 dev = (struct disk_devdesc *)f->f_devdata;
411 rc = disk_ioctl(dev, cmd, data);
412 if (rc != ENOTTY)
413 return (rc);
415 switch (cmd) {
416 case DIOCGSECTORSIZE:
417 *(uint32_t *)data = BD(dev).bd_sectorsize;
418 break;
419 case DIOCGMEDIASIZE:
420 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
421 break;
422 default:
423 return (ENOTTY);
425 return (0);
428 static int
429 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
430 char *buf, size_t *rsize)
432 struct bcache_devdata bcd;
433 struct disk_devdesc *dev;
435 dev = (struct disk_devdesc *)devdata;
436 bcd.dv_strategy = bd_realstrategy;
437 bcd.dv_devdata = devdata;
438 bcd.dv_cache = BD(dev).bd_bcache;
439 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
440 buf, rsize));
443 static int
444 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
445 char *buf, size_t *rsize)
447 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
448 uint64_t disk_blocks, offset;
449 size_t blks, blkoff, bsize, rest;
450 caddr_t bbuf;
451 int rc;
454 * First make sure the IO size is a multiple of 512 bytes. While we do
455 * process partial reads below, the strategy mechanism is built
456 * assuming IO is a multiple of 512B blocks. If the request is not
457 * a multiple of 512B blocks, it has to be some sort of bug.
459 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
460 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
461 size, BIOSDISK_SECSIZE);
462 return (EIO);
465 DEBUG("open_disk %p", dev);
467 offset = dblk * BIOSDISK_SECSIZE;
468 dblk = offset / BD(dev).bd_sectorsize;
469 blkoff = offset % BD(dev).bd_sectorsize;
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("requested read: %zu too large", size);
479 return (EIO);
482 blks = size / BD(dev).bd_sectorsize;
483 if (blks == 0 || (size % BD(dev).bd_sectorsize) != 0)
484 blks++;
486 if (dblk > dblk + blks)
487 return (EIO);
489 if (rsize)
490 *rsize = 0;
493 * Get disk blocks, this value is either for whole disk or for
494 * partition.
496 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
497 /* DIOCGMEDIASIZE does return bytes. */
498 disk_blocks /= BD(dev).bd_sectorsize;
499 } else {
500 /* We should not get here. Just try to survive. */
501 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
504 /* Validate source block address. */
505 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
506 return (EIO);
509 * Truncate if we are crossing disk or partition end.
511 if (dblk + blks >= dev->d_offset + disk_blocks) {
512 blks = dev->d_offset + disk_blocks - dblk;
513 size = blks * BD(dev).bd_sectorsize;
514 DEBUG("short read %d", blks);
517 if (V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize == 0)
518 panic("BUG: Real mode buffer is too small\n");
520 bbuf = PTOV(V86_IO_BUFFER);
521 rest = size;
523 while (blks > 0) {
524 int x = min(blks, V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize);
526 switch (rw & F_MASK) {
527 case F_READ:
528 DEBUG("read %d from %lld to %p", x, dblk, buf);
529 bsize = BD(dev).bd_sectorsize * x - blkoff;
530 if (rest < bsize)
531 bsize = rest;
533 if ((rc = bd_io(dev, dblk, x, bbuf, 0)) != 0)
534 return (EIO);
536 bcopy(bbuf + blkoff, buf, bsize);
537 break;
538 case F_WRITE :
539 DEBUG("write %d from %lld to %p", x, dblk, buf);
540 if (blkoff != 0) {
542 * We got offset to sector, read 1 sector to
543 * bbuf.
545 x = 1;
546 bsize = BD(dev).bd_sectorsize - blkoff;
547 bsize = min(bsize, rest);
548 rc = bd_io(dev, dblk, x, bbuf, 0);
549 } else if (rest < BD(dev).bd_sectorsize) {
551 * The remaining block is not full
552 * sector. Read 1 sector to bbuf.
554 x = 1;
555 bsize = rest;
556 rc = bd_io(dev, dblk, x, bbuf, 0);
557 } else {
558 /* We can write full sector(s). */
559 bsize = BD(dev).bd_sectorsize * x;
562 * Put your Data In, Put your Data out,
563 * Put your Data In, and shake it all about
565 bcopy(buf, bbuf + blkoff, bsize);
566 if ((rc = bd_io(dev, dblk, x, bbuf, 1)) != 0)
567 return (EIO);
569 break;
570 default:
571 /* DO NOTHING */
572 return (EROFS);
575 blkoff = 0;
576 buf += bsize;
577 rest -= bsize;
578 blks -= x;
579 dblk += x;
582 if (rsize != NULL)
583 *rsize = size;
584 return (0);
587 static int
588 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
589 int dowrite)
591 static struct edd_packet packet;
593 packet.len = sizeof (struct edd_packet);
594 packet.count = blks;
595 packet.off = VTOPOFF(dest);
596 packet.seg = VTOPSEG(dest);
597 packet.lba = dblk;
598 v86.ctl = V86_FLAGS;
599 v86.addr = 0x13;
600 /* Should we Write with verify ?? 0x4302 ? */
601 if (dowrite)
602 v86.eax = 0x4300;
603 else
604 v86.eax = 0x4200;
605 v86.edx = BD(dev).bd_unit;
606 v86.ds = VTOPSEG(&packet);
607 v86.esi = VTOPOFF(&packet);
608 v86int();
609 if (V86_CY(v86.efl))
610 return (v86.eax >> 8);
611 return (0);
614 static int
615 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
616 int dowrite)
618 uint32_t x, bpc, cyl, hd, sec;
620 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
621 x = dblk;
622 cyl = x / bpc; /* block # / blocks per cylinder */
623 x %= bpc; /* block offset into cylinder */
624 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
625 sec = x % BD(dev).bd_sec; /* offset into track */
627 /* correct sector number for 1-based BIOS numbering */
628 sec++;
630 if (cyl > 1023) {
631 /* CHS doesn't support cylinders > 1023. */
632 return (1);
635 v86.ctl = V86_FLAGS;
636 v86.addr = 0x13;
637 if (dowrite)
638 v86.eax = 0x300 | blks;
639 else
640 v86.eax = 0x200 | blks;
641 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
642 v86.edx = (hd << 8) | BD(dev).bd_unit;
643 v86.es = VTOPSEG(dest);
644 v86.ebx = VTOPOFF(dest);
645 v86int();
646 if (V86_CY(v86.efl))
647 return (v86.eax >> 8);
648 return (0);
651 static int
652 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
653 int dowrite)
655 int result, retry;
657 /* Just in case some idiot actually tries to read/write -1 blocks... */
658 if (blks < 0)
659 return (-1);
662 * Loop retrying the operation a couple of times. The BIOS
663 * may also retry.
665 for (retry = 0; retry < 3; retry++) {
666 /* if retrying, reset the drive */
667 if (retry > 0) {
668 v86.ctl = V86_FLAGS;
669 v86.addr = 0x13;
670 v86.eax = 0;
671 v86.edx = BD(dev).bd_unit;
672 v86int();
675 if (BD(dev).bd_flags & BD_MODEEDD1)
676 result = bd_edd_io(dev, dblk, blks, dest, dowrite);
677 else
678 result = bd_chs_io(dev, dblk, blks, dest, dowrite);
680 if (result == 0)
681 break;
685 * 0x20 - Controller failure. This is common error when the
686 * media is not present.
688 if (result != 0 && result != 0x20) {
689 if (dowrite != 0) {
690 printf("%s%d: Write %d sector(s) from %p (0x%x) "
691 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
692 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
693 result);
694 } else {
695 printf("%s%d: Read %d sector(s) from %lld to %p "
696 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
697 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
698 result);
702 return (result);
706 * Return the BIOS geometry of a given "fixed drive" in a format
707 * suitable for the legacy bootinfo structure. Since the kernel is
708 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
709 * prefer to get the information directly, rather than rely on being
710 * able to put it together from information already maintained for
711 * different purposes and for a probably different number of drives.
713 * For valid drives, the geometry is expected in the format (31..0)
714 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
715 * indicated by returning the geometry of a "1.2M" PC-format floppy
716 * disk. And, incidentally, what is returned is not the geometry as
717 * such but the highest valid cylinder, head, and sector numbers.
719 uint32_t
720 bd_getbigeom(int bunit)
723 v86.ctl = V86_FLAGS;
724 v86.addr = 0x13;
725 v86.eax = 0x800;
726 v86.edx = 0x80 + bunit;
727 v86int();
728 if (V86_CY(v86.efl))
729 return (0x4f010f);
730 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
731 (v86.edx & 0xff00) | (v86.ecx & 0x3f));
735 * Return a suitable dev_t value for (dev).
737 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
738 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
741 bd_getdev(struct i386_devdesc *d)
743 struct disk_devdesc *dev;
744 int biosdev;
745 int major;
746 int rootdev;
747 char *nip, *cp;
748 int i, unit;
750 dev = (struct disk_devdesc *)d;
751 biosdev = bd_unit2bios(dev->dd.d_unit);
752 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
753 if (biosdev == -1) /* not a BIOS device */
754 return (-1);
755 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
756 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
757 return (-1);
758 else
759 disk_close(dev);
761 if (biosdev < 0x80) {
762 /* floppy (or emulated floppy) or ATAPI device */
763 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
764 /* is an ATAPI disk */
765 major = WFDMAJOR;
766 } else {
767 /* is a floppy disk */
768 major = FDMAJOR;
770 } else {
771 /* assume an IDE disk */
772 major = WDMAJOR;
774 /* default root disk unit number */
775 unit = biosdev & 0x7f;
777 /* XXX a better kludge to set the root disk unit number */
778 if ((nip = getenv("root_disk_unit")) != NULL) {
779 i = strtol(nip, &cp, 0);
780 /* check for parse error */
781 if ((cp != nip) && (*cp == 0))
782 unit = i;
785 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
786 DEBUG("dev is 0x%x\n", rootdev);
787 return (rootdev);