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>
44 #include <machine/bootinfo.h>
47 #include <bootstrap.h>
53 #ifdef LOADER_GELI_SUPPORT
60 struct ptable_entry part;
68 STAILQ_ENTRY(pentry) entry;
71 enum ptable_type type;
75 STAILQ_HEAD(, pentry) entries;
79 #endif /* LOADER_GELI_SUPPORT */
81 CTASSERT(sizeof(struct i386_devdesc) >= sizeof(struct disk_devdesc));
83 #define BIOS_NUMDRIVES 0x475
84 #define BIOSDISK_SECSIZE 512
85 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
87 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
88 #define WDMAJOR 0 /* major numbers for devices we frontend for */
94 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
96 # define DEBUG(fmt, args...)
100 * List of BIOS devices, translation from disk unit number to
105 int bd_unit; /* BIOS unit number */
106 int bd_cyl; /* BIOS geometry */
110 #define BD_MODEINT13 0x0000
111 #define BD_MODEEDD1 0x0001
112 #define BD_MODEEDD3 0x0002
113 #define BD_MODEMASK 0x0003
114 #define BD_FLOPPY 0x0004
115 int bd_type; /* BIOS 'drive type' (floppy only) */
116 uint16_t bd_sectorsize; /* Sector size */
117 uint64_t bd_sectors; /* Disk size */
118 int bd_open; /* reference counter */
119 void *bd_bcache; /* buffer cache data */
121 static int nbdinfo = 0;
123 #define BD(dev) (bdinfo[(dev)->d_unit])
125 static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
127 static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
129 static int bd_int13probe(struct bdinfo *bd);
131 static int bd_init(void);
132 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
133 char *buf, size_t *rsize);
134 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
135 char *buf, size_t *rsize);
136 static int bd_open(struct open_file *f, ...);
137 static int bd_close(struct open_file *f);
138 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
139 static int bd_print(int verbose);
141 #ifdef LOADER_GELI_SUPPORT
147 static enum isgeli geli_status[MAXBDDEV][MAXTBLENTS];
149 int bios_read(void *vdev __unused, struct dsk *priv, off_t off, char *buf,
151 #endif /* LOADER_GELI_SUPPORT */
153 struct devsw biosdisk = {
166 * Translate between BIOS device numbers and our private unit numbers.
169 bd_bios2unit(int biosdev)
173 DEBUG("looking for bios device 0x%x", biosdev);
174 for (i = 0; i < nbdinfo; i++) {
175 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
176 if (bdinfo[i].bd_unit == biosdev)
183 bd_unit2bios(int unit)
186 if ((unit >= 0) && (unit < nbdinfo))
187 return (bdinfo[unit].bd_unit);
192 * Quiz the BIOS for disk devices, save a little info about them.
197 int base, unit, nfd = 0;
199 #ifdef LOADER_GELI_SUPPORT
202 /* sequence 0, 0x80 */
203 for (base = 0; base <= 0x80; base += 0x80) {
204 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
207 * Check the BIOS equipment list for number
211 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
214 bdinfo[nbdinfo].bd_open = 0;
215 bdinfo[nbdinfo].bd_bcache = NULL;
216 bdinfo[nbdinfo].bd_unit = unit;
217 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
218 if (!bd_int13probe(&bdinfo[nbdinfo]))
221 /* XXX we need "disk aliases" to make this simpler */
222 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
223 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
229 bcache_add_dev(nbdinfo);
234 * Try to detect a device supported by the legacy int13 BIOS
237 bd_int13probe(struct bdinfo *bd)
239 struct edd_params params;
240 int ret = 1; /* assume success */
245 v86.edx = bd->bd_unit;
248 /* Don't error out if we get bad sector number, try EDD as well */
249 if (V86_CY(v86.efl) || /* carry set */
250 (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
251 return (0); /* skip device */
253 if ((v86.ecx & 0x3f) == 0) /* absurd sector number */
254 ret = 0; /* set error */
256 /* Convert max cyl # -> # of cylinders */
257 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
258 /* Convert max head # -> # of heads */
259 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
260 bd->bd_sec = v86.ecx & 0x3f;
261 bd->bd_type = v86.ebx & 0xff;
262 bd->bd_flags |= BD_MODEINT13;
264 /* Calculate sectors count from the geometry */
265 bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
266 bd->bd_sectorsize = BIOSDISK_SECSIZE;
267 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
268 bd->bd_hds, bd->bd_sec);
270 /* Determine if we can use EDD with this device. */
274 v86.edx = bd->bd_unit;
277 if (V86_CY(v86.efl) || /* carry set */
278 (v86.ebx & 0xffff) != 0xaa55 || /* signature */
279 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
280 return (ret); /* return code from int13 AH=08 */
283 bd->bd_flags |= BD_MODEEDD1;
284 if ((v86.eax & 0xff00) >= 0x3000)
285 bd->bd_flags |= BD_MODEEDD3;
286 /* Get disk params */
287 params.len = sizeof(struct edd_params);
291 v86.edx = bd->bd_unit;
292 v86.ds = VTOPSEG(¶ms);
293 v86.esi = VTOPOFF(¶ms);
295 if (!V86_CY(v86.efl)) {
299 * Sector size must be a multiple of 512 bytes.
300 * An alternate test would be to check power of 2,
301 * powerof2(params.sector_size).
303 if (params.sector_size % BIOSDISK_SECSIZE)
304 bd->bd_sectorsize = BIOSDISK_SECSIZE;
306 bd->bd_sectorsize = params.sector_size;
308 total = bd->bd_sectorsize * params.sectors;
309 if (params.sectors != 0) {
310 /* Only update if we did not overflow. */
311 if (total > params.sectors)
312 bd->bd_sectors = params.sectors;
315 total = (uint64_t)params.cylinders *
316 params.heads * params.sectors_per_track;
317 if (bd->bd_sectors < total)
318 bd->bd_sectors = total;
322 DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
323 bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
328 * Print information about disks
331 bd_print(int verbose)
333 static char line[80];
334 struct disk_devdesc dev;
340 printf("%s devices:", biosdisk.dv_name);
341 if ((ret = pager_output("\n")) != 0)
344 for (i = 0; i < nbdinfo; i++) {
345 snprintf(line, sizeof(line),
346 " disk%d: BIOS drive %c (%ju X %u):\n", i,
347 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
348 ('C' + bdinfo[i].bd_unit - 0x80),
349 (uintmax_t)bdinfo[i].bd_sectors,
350 bdinfo[i].bd_sectorsize);
351 if ((ret = pager_output(line)) != 0)
353 dev.d_dev = &biosdisk;
356 dev.d_partition = -1;
358 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
359 bdinfo[i].bd_sectorsize) == 0) {
360 snprintf(line, sizeof(line), " disk%d", i);
361 ret = disk_print(&dev, line, verbose);
371 * Attempt to open the disk described by (dev) for use by (f).
373 * Note that the philosophy here is "give them exactly what
374 * they ask for". This is necessary because being too "smart"
375 * about what the user might want leads to complications.
376 * (eg. given no slice or partition value, with a disk that is
377 * sliced - are they after the first BSD slice, or the DOS
381 bd_open(struct open_file *f, ...)
383 struct disk_devdesc *dev, rdev;
384 struct disk_devdesc disk;
390 dev = va_arg(ap, struct disk_devdesc *);
393 if (dev->d_unit < 0 || dev->d_unit >= nbdinfo)
396 if (BD(dev).bd_bcache == NULL)
397 BD(dev).bd_bcache = bcache_allocate();
400 * Read disk size from partition.
401 * This is needed to work around buggy BIOS systems returning
402 * wrong (truncated) disk media size.
403 * During bd_probe() we tested if the mulitplication of bd_sectors
404 * would overflow so it should be safe to perform here.
406 disk.d_dev = dev->d_dev;
407 disk.d_type = dev->d_type;
408 disk.d_unit = dev->d_unit;
409 disk.d_opendata = NULL;
411 disk.d_partition = -1;
413 if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
414 BD(dev).bd_sectorsize) == 0) {
416 if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
417 size /= BD(dev).bd_sectorsize;
418 if (size > BD(dev).bd_sectors)
419 BD(dev).bd_sectors = size;
424 err = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
425 BD(dev).bd_sectorsize);
427 #ifdef LOADER_GELI_SUPPORT
428 static char gelipw[GELI_PW_MAXLEN];
434 /* if we already know there is no GELI, skip the rest */
435 if (geli_status[dev->d_unit][dev->d_slice] != ISGELI_UNKNOWN)
439 struct ptable *table = NULL;
440 struct ptable_entry part;
441 struct pentry *entry;
444 dskp.drive = bd_unit2bios(dev->d_unit);
445 dskp.type = dev->d_type;
446 dskp.unit = dev->d_unit;
447 dskp.slice = dev->d_slice;
448 dskp.part = dev->d_partition;
449 dskp.start = dev->d_offset;
451 memcpy(&rdev, dev, sizeof(rdev));
452 /* to read the GPT table, we need to read the first sector */
454 /* We need the LBA of the end of the partition */
455 table = ptable_open(&rdev, BD(dev).bd_sectors,
456 BD(dev).bd_sectorsize, ptblread);
458 DEBUG("Can't read partition table");
459 /* soft failure, return the exit status of disk_open */
463 if (table->type == PTABLE_GPT)
466 STAILQ_FOREACH(entry, &table->entries, entry) {
467 dskp.slice = entry->part.index;
468 dskp.start = entry->part.start;
469 if (is_geli(&dskp) == 0) {
470 geli_status[dev->d_unit][dskp.slice] = ISGELI_YES;
473 if (geli_taste(bios_read, &dskp,
474 entry->part.end - entry->part.start) == 0) {
475 if (geli_havekey(&dskp) == 0) {
476 geli_status[dev->d_unit][dskp.slice] = ISGELI_YES;
480 if ((passphrase = getenv("kern.geom.eli.passphrase"))
482 /* Use the cached passphrase */
483 bcopy(passphrase, &gelipw, GELI_PW_MAXLEN);
485 if (geli_passphrase(&gelipw, dskp.unit, 'p',
486 (dskp.slice > 0 ? dskp.slice : dskp.part),
488 setenv("kern.geom.eli.passphrase", &gelipw, 1);
489 bzero(gelipw, sizeof(gelipw));
490 geli_status[dev->d_unit][dskp.slice] = ISGELI_YES;
495 geli_status[dev->d_unit][dskp.slice] = ISGELI_NO;
498 /* none of the partitions on this disk have GELI */
499 if (geli_part == 0) {
501 geli_status[dev->d_unit][dev->d_slice] = ISGELI_NO;
503 #endif /* LOADER_GELI_SUPPORT */
509 bd_close(struct open_file *f)
511 struct disk_devdesc *dev;
513 dev = (struct disk_devdesc *)f->f_devdata;
515 if (BD(dev).bd_open == 0) {
516 bcache_free(BD(dev).bd_bcache);
517 BD(dev).bd_bcache = NULL;
519 return (disk_close(dev));
523 bd_ioctl(struct open_file *f, u_long cmd, void *data)
525 struct disk_devdesc *dev;
528 dev = (struct disk_devdesc *)f->f_devdata;
530 rc = disk_ioctl(dev, cmd, data);
535 case DIOCGSECTORSIZE:
536 *(u_int *)data = BD(dev).bd_sectorsize;
539 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
548 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
549 char *buf, size_t *rsize)
551 struct bcache_devdata bcd;
552 struct disk_devdesc *dev;
554 dev = (struct disk_devdesc *)devdata;
555 bcd.dv_strategy = bd_realstrategy;
556 bcd.dv_devdata = devdata;
557 bcd.dv_cache = BD(dev).bd_bcache;
558 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset,
563 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
564 char *buf, size_t *rsize)
566 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
567 uint64_t disk_blocks;
569 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
570 char fragbuf[BIOSDISK_SECSIZE];
573 fragsize = size % BIOSDISK_SECSIZE;
575 if (size % BD(dev).bd_sectorsize)
576 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
579 DEBUG("open_disk %p", dev);
582 * Check the value of the size argument. We do have quite small
583 * heap (64MB), but we do not know good upper limit, so we check against
584 * INT_MAX here. This will also protect us against possible overflows
585 * while translating block count to bytes.
587 if (size > INT_MAX) {
588 DEBUG("too large read: %zu bytes", size);
592 blks = size / BD(dev).bd_sectorsize;
593 if (dblk > dblk + blks)
599 /* Get disk blocks, this value is either for whole disk or for partition */
600 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks)) {
601 /* DIOCGMEDIASIZE does return bytes. */
602 disk_blocks /= BD(dev).bd_sectorsize;
604 /* We should not get here. Just try to survive. */
605 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
608 /* Validate source block address. */
609 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
613 * Truncate if we are crossing disk or partition end.
615 if (dblk + blks >= dev->d_offset + disk_blocks) {
616 blks = dev->d_offset + disk_blocks - dblk;
617 size = blks * BD(dev).bd_sectorsize;
618 DEBUG("short read %d", blks);
621 switch (rw & F_MASK) {
623 DEBUG("read %d from %lld to %p", blks, dblk, buf);
625 if (blks && (rc = bd_read(dev, dblk, blks, buf))) {
626 /* Filter out floppy controller errors */
627 if (BD(dev).bd_flags != BD_FLOPPY || rc != 0x20) {
628 printf("read %d from %lld to %p, error: 0x%x", blks, dblk,
633 #ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
634 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
635 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
636 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
637 DEBUG("frag read error");
640 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
644 DEBUG("write %d from %d to %p", blks, dblk, buf);
646 if (blks && bd_write(dev, dblk, blks, buf)) {
647 DEBUG("write error");
650 #ifdef BD_SUPPORT_FRAGS
652 DEBUG("Attempted to write a frag");
668 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
671 static struct edd_packet packet;
673 packet.len = sizeof(struct edd_packet);
675 packet.off = VTOPOFF(dest);
676 packet.seg = VTOPSEG(dest);
681 /* Should we Write with verify ?? 0x4302 ? */
685 v86.edx = BD(dev).bd_unit;
686 v86.ds = VTOPSEG(&packet);
687 v86.esi = VTOPOFF(&packet);
690 return (v86.eax >> 8);
695 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
698 u_int x, bpc, cyl, hd, sec;
700 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
702 cyl = x / bpc; /* block # / blocks per cylinder */
703 x %= bpc; /* block offset into cylinder */
704 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
705 sec = x % BD(dev).bd_sec; /* offset into track */
707 /* correct sector number for 1-based BIOS numbering */
711 /* CHS doesn't support cylinders > 1023. */
717 v86.eax = 0x300 | blks;
719 v86.eax = 0x200 | blks;
720 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
721 v86.edx = (hd << 8) | BD(dev).bd_unit;
722 v86.es = VTOPSEG(dest);
723 v86.ebx = VTOPOFF(dest);
726 return (v86.eax >> 8);
731 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, int write)
733 u_int x, sec, result, resid, retry, maxfer;
736 /* Just in case some idiot actually tries to read/write -1 blocks... */
743 /* Decide whether we have to bounce */
744 if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
745 (VTOP(dest) >> 16) != (VTOP(dest +
746 blks * BD(dev).bd_sectorsize) >> 16))) {
749 * There is a 64k physical boundary somewhere in the
750 * destination buffer, or the destination buffer is above
751 * first 1MB of physical memory so we have to arrange a
752 * suitable bounce buffer. Allocate a buffer twice as large
753 * as we need to. Use the bottom half unless there is a break
754 * there, in which case we use the top half.
756 x = V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize;
757 x = min(x, (unsigned)blks);
758 bbuf = PTOV(V86_IO_BUFFER);
759 maxfer = x; /* limit transfers to bounce region size */
767 * Play it safe and don't cross track boundaries.
768 * (XXX this is probably unnecessary)
770 sec = dblk % BD(dev).bd_sec; /* offset into track */
771 x = min(BD(dev).bd_sec - sec, resid);
773 x = min(x, maxfer); /* fit bounce buffer */
775 /* where do we transfer to? */
776 xp = bbuf == NULL ? p : bbuf;
779 * Put your Data In, Put your Data out,
780 * Put your Data In, and shake it all about
782 if (write && bbuf != NULL)
783 bcopy(p, bbuf, x * BD(dev).bd_sectorsize);
786 * Loop retrying the operation a couple of times. The BIOS
789 for (retry = 0; retry < 3; retry++) {
790 /* if retrying, reset the drive */
795 v86.edx = BD(dev).bd_unit;
799 if (BD(dev).bd_flags & BD_MODEEDD1)
800 result = bd_edd_io(dev, dblk, x, xp, write);
802 result = bd_chs_io(dev, dblk, x, xp, write);
808 DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
809 p, VTOP(p), dblk, result ? "failed" : "ok");
811 DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
812 dblk, p, VTOP(p), result ? "failed" : "ok");
816 if (!write && bbuf != NULL)
817 bcopy(bbuf, p, x * BD(dev).bd_sectorsize);
818 p += (x * BD(dev).bd_sectorsize);
823 /* hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
828 bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
830 #ifdef LOADER_GELI_SUPPORT
834 int err, n, alignblks;
837 /* if we already know there is no GELI, skip the rest */
838 if (geli_status[dev->d_unit][dev->d_slice] != ISGELI_YES)
839 return (bd_io(dev, dblk, blks, dest, 0));
841 if (geli_status[dev->d_unit][dev->d_slice] == ISGELI_YES) {
843 * Align reads to DEV_GELIBOOT_BSIZE bytes because partial
844 * sectors cannot be decrypted. Round the requested LBA down to
845 * nearest multiple of DEV_GELIBOOT_BSIZE bytes.
847 alignlba = rounddown2(dblk * BD(dev).bd_sectorsize,
848 DEV_GELIBOOT_BSIZE) / BD(dev).bd_sectorsize;
850 * Round number of blocks to read up to nearest multiple of
853 diff = (dblk - alignlba) * BD(dev).bd_sectorsize;
854 alignblks = roundup2(blks * BD(dev).bd_sectorsize + diff,
855 DEV_GELIBOOT_BSIZE) / BD(dev).bd_sectorsize;
858 * If the read is rounded up to a larger size, use a temporary
859 * buffer here because the buffer provided by the caller may be
865 tmpbuf = malloc(alignblks * BD(dev).bd_sectorsize);
866 if (tmpbuf == NULL) {
871 err = bd_io(dev, alignlba, alignblks, tmpbuf, 0);
875 dskp.drive = bd_unit2bios(dev->d_unit);
876 dskp.type = dev->d_type;
877 dskp.unit = dev->d_unit;
878 dskp.slice = dev->d_slice;
879 dskp.part = dev->d_partition;
880 dskp.start = dev->d_offset;
882 /* GELI needs the offset relative to the partition start */
883 p_off = alignlba - dskp.start;
885 err = geli_read(&dskp, p_off * BD(dev).bd_sectorsize, tmpbuf,
886 alignblks * BD(dev).bd_sectorsize);
890 if (tmpbuf != dest) {
891 bcopy(tmpbuf + diff, dest, blks * BD(dev).bd_sectorsize);
896 #endif /* LOADER_GELI_SUPPORT */
898 return (bd_io(dev, dblk, blks, dest, 0));
902 bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
905 return (bd_io(dev, dblk, blks, dest, 1));
909 * Return the BIOS geometry of a given "fixed drive" in a format
910 * suitable for the legacy bootinfo structure. Since the kernel is
911 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
912 * prefer to get the information directly, rather than rely on being
913 * able to put it together from information already maintained for
914 * different purposes and for a probably different number of drives.
916 * For valid drives, the geometry is expected in the format (31..0)
917 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
918 * indicated by returning the geometry of a "1.2M" PC-format floppy
919 * disk. And, incidentally, what is returned is not the geometry as
920 * such but the highest valid cylinder, head, and sector numbers.
923 bd_getbigeom(int bunit)
929 v86.edx = 0x80 + bunit;
933 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
934 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
938 * Return a suitable dev_t value for (dev).
940 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
941 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
944 bd_getdev(struct i386_devdesc *d)
946 struct disk_devdesc *dev;
953 dev = (struct disk_devdesc *)d;
954 biosdev = bd_unit2bios(dev->d_unit);
955 DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
956 if (biosdev == -1) /* not a BIOS device */
958 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
959 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
964 if (biosdev < 0x80) {
965 /* floppy (or emulated floppy) or ATAPI device */
966 if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
967 /* is an ATAPI disk */
970 /* is a floppy disk */
974 /* assume an IDE disk */
977 /* default root disk unit number */
978 unit = biosdev & 0x7f;
980 /* XXX a better kludge to set the root disk unit number */
981 if ((nip = getenv("root_disk_unit")) != NULL) {
982 i = strtol(nip, &cp, 0);
983 /* check for parse error */
984 if ((cp != nip) && (*cp == 0))
988 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
989 DEBUG("dev is 0x%x\n", rootdev);
993 #ifdef LOADER_GELI_SUPPORT
995 bios_read(void *vdev __unused, struct dsk *priv, off_t off, char *buf, size_t bytes)
997 struct disk_devdesc dev;
999 dev.d_dev = &biosdisk;
1000 dev.d_type = priv->type;
1001 dev.d_unit = priv->unit;
1002 dev.d_slice = priv->slice;
1003 dev.d_partition = priv->part;
1004 dev.d_offset = priv->start;
1006 off = off / BD(&dev).bd_sectorsize;
1007 /* GELI gives us the offset relative to the partition start */
1008 off += dev.d_offset;
1009 bytes = bytes / BD(&dev).bd_sectorsize;
1011 return (bd_io(&dev, off, bytes, buf, 0));
1013 #endif /* LOADER_GELI_SUPPORT */