]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/biosdisk.c
MFC i386 stand cleanup: r337353-r337354, r337356, r337872, r337878, r337881,
[FreeBSD/FreeBSD.git] / stand / i386 / libi386 / biosdisk.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
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.
14  *
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.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * BIOS disk device handling.
33  *
34  * Ideas and algorithms from:
35  *
36  * - NetBSD libi386/biosdisk.c
37  * - FreeBSD biosboot/disk.c
38  *
39  */
40
41 #include <sys/disk.h>
42 #include <sys/limits.h>
43 #include <stand.h>
44 #include <machine/bootinfo.h>
45 #include <stdarg.h>
46
47 #include <bootstrap.h>
48 #include <btxv86.h>
49 #include <edd.h>
50 #include "disk.h"
51 #include "libi386.h"
52
53 #define BIOS_NUMDRIVES          0x475
54 #define BIOSDISK_SECSIZE        512
55 #define BUFSIZE                 (1 * BIOSDISK_SECSIZE)
56
57 #define DT_ATAPI        0x10    /* disk type for ATAPI floppies */
58 #define WDMAJOR         0       /* major numbers for devices we frontend for */
59 #define WFDMAJOR        1
60 #define FDMAJOR         2
61 #define DAMAJOR         4
62
63 #ifdef DISK_DEBUG
64 #define DEBUG(fmt, args...)     printf("%s: " fmt "\n", __func__, ## args)
65 #else
66 #define DEBUG(fmt, args...)
67 #endif
68
69 /*
70  * List of BIOS devices, translation from disk unit number to
71  * BIOS unit number.
72  */
73 static struct bdinfo
74 {
75         int             bd_unit;        /* BIOS unit number */
76         int             bd_cyl;         /* BIOS geometry */
77         int             bd_hds;
78         int             bd_sec;
79         int             bd_flags;
80 #define BD_MODEINT13    0x0000
81 #define BD_MODEEDD1     0x0001
82 #define BD_MODEEDD3     0x0002
83 #define BD_MODEMASK     0x0003
84 #define BD_FLOPPY       0x0004
85         int             bd_type;        /* BIOS 'drive type' (floppy only) */
86         uint16_t        bd_sectorsize;  /* Sector size */
87         uint64_t        bd_sectors;     /* Disk size */
88         int             bd_open;        /* reference counter */
89         void            *bd_bcache;     /* buffer cache data */
90 } bdinfo [MAXBDDEV];
91 static int nbdinfo = 0;
92
93 #define BD(dev)         (bdinfo[(dev)->dd.d_unit])
94 #define BD_RD           0
95 #define BD_WR           1
96
97 static void bd_io_workaround(struct disk_devdesc *dev);
98
99 static int bd_io(struct disk_devdesc *, daddr_t, int, caddr_t, int);
100 static int bd_int13probe(struct bdinfo *bd);
101
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);
111
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
122 };
123
124 /*
125  * Translate between BIOS device numbers and our private unit numbers.
126  */
127 int
128 bd_bios2unit(int biosdev)
129 {
130         int i;
131
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);
137         }
138         return (-1);
139 }
140
141 int
142 bd_unit2bios(int unit)
143 {
144
145         if ((unit >= 0) && (unit < nbdinfo))
146                 return (bdinfo[unit].bd_unit);
147         return (-1);
148 }
149
150 /*
151  * Quiz the BIOS for disk devices, save a little info about them.
152  */
153 static int
154 bd_init(void)
155 {
156         int base, unit, nfd = 0;
157
158         /* sequence 0, 0x80 */
159         for (base = 0; base <= 0x80; base += 0x80) {
160                 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
161 #ifndef VIRTUALBOX
162                         /*
163                          * Check the BIOS equipment list for number
164                          * of fixed disks.
165                          */
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;
176
177                         /* XXX we need "disk aliases" to make this simpler */
178                         printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
179                             ('A' + unit): ('C' + unit - 0x80), nbdinfo);
180                         nbdinfo++;
181                         if (base == 0x80)
182                                 nfd++;
183                 }
184         }
185         bcache_add_dev(nbdinfo);
186         return (0);
187 }
188
189 /*
190  * Try to detect a device supported by the legacy int13 BIOS
191  */
192 static int
193 bd_int13probe(struct bdinfo *bd)
194 {
195         struct edd_params params;
196         int ret = 1;    /* assume success */
197
198         v86.ctl = V86_FLAGS;
199         v86.addr = 0x13;
200         v86.eax = 0x800;
201         v86.edx = bd->bd_unit;
202         v86int();
203
204         /* Don't error out if we get bad sector number, try EDD as well */
205         if (V86_CY(v86.efl) ||  /* carry set */
206             (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
207                 return (0);     /* skip device */
208
209         if ((v86.ecx & 0x3f) == 0)      /* absurd sector number */
210                 ret = 0;        /* set error */
211
212         /* Convert max cyl # -> # of cylinders */
213         bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
214         /* Convert max head # -> # of heads */
215         bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
216         bd->bd_sec = v86.ecx & 0x3f;
217         bd->bd_type = v86.ebx & 0xff;
218         bd->bd_flags |= BD_MODEINT13;
219
220         /* Calculate sectors count from the geometry */
221         bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
222         bd->bd_sectorsize = BIOSDISK_SECSIZE;
223         DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
224             bd->bd_hds, bd->bd_sec);
225
226         /* Determine if we can use EDD with this device. */
227         v86.ctl = V86_FLAGS;
228         v86.addr = 0x13;
229         v86.eax = 0x4100;
230         v86.edx = bd->bd_unit;
231         v86.ebx = 0x55aa;
232         v86int();
233         if (V86_CY(v86.efl) ||  /* carry set */
234             (v86.ebx & 0xffff) != 0xaa55 || /* signature */
235             (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
236                 return (ret);   /* return code from int13 AH=08 */
237
238         /* EDD supported */
239         bd->bd_flags |= BD_MODEEDD1;
240         if ((v86.eax & 0xff00) >= 0x3000)
241                 bd->bd_flags |= BD_MODEEDD3;
242         /* Get disk params */
243         params.len = sizeof(struct edd_params);
244         v86.ctl = V86_FLAGS;
245         v86.addr = 0x13;
246         v86.eax = 0x4800;
247         v86.edx = bd->bd_unit;
248         v86.ds = VTOPSEG(&params);
249         v86.esi = VTOPOFF(&params);
250         v86int();
251         if (!V86_CY(v86.efl)) {
252                 uint64_t total;
253
254                 /*
255                  * Sector size must be a multiple of 512 bytes.
256                  * An alternate test would be to check power of 2,
257                  * powerof2(params.sector_size).
258                  */
259                 if (params.sector_size % BIOSDISK_SECSIZE)
260                         bd->bd_sectorsize = BIOSDISK_SECSIZE;
261                 else
262                         bd->bd_sectorsize = params.sector_size;
263
264                 total = bd->bd_sectorsize * params.sectors;
265                 if (params.sectors != 0) {
266                         /* Only update if we did not overflow. */
267                         if (total > params.sectors)
268                                 bd->bd_sectors = params.sectors;
269                 }
270
271                 total = (uint64_t)params.cylinders *
272                     params.heads * params.sectors_per_track;
273                 if (total > 0 && bd->bd_sectors > total)
274                         bd->bd_sectors = total;
275
276                 ret = 1;
277         }
278         DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
279             bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
280         return (ret);
281 }
282
283 /*
284  * Print information about disks
285  */
286 static int
287 bd_print(int verbose)
288 {
289         static char line[80];
290         struct disk_devdesc dev;
291         int i, ret = 0;
292
293         if (nbdinfo == 0)
294                 return (0);
295
296         printf("%s devices:", biosdisk.dv_name);
297         if ((ret = pager_output("\n")) != 0)
298                 return (ret);
299
300         for (i = 0; i < nbdinfo; i++) {
301                 snprintf(line, sizeof(line),
302                     "    disk%d:   BIOS drive %c (%ju X %u):\n", i,
303                     (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
304                     ('C' + bdinfo[i].bd_unit - 0x80),
305                     (uintmax_t)bdinfo[i].bd_sectors,
306                     bdinfo[i].bd_sectorsize);
307                 if ((ret = pager_output(line)) != 0)
308                         break;
309
310                 dev.dd.d_dev = &biosdisk;
311                 dev.dd.d_unit = i;
312                 dev.d_slice = -1;
313                 dev.d_partition = -1;
314                 if (disk_open(&dev,
315                     bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
316                     bdinfo[i].bd_sectorsize) == 0) {
317                         snprintf(line, sizeof(line), "    disk%d", i);
318                         ret = disk_print(&dev, line, verbose);
319                         disk_close(&dev);
320                         if (ret != 0)
321                                 break;
322                 }
323         }
324         return (ret);
325 }
326
327 /*
328  * Attempt to open the disk described by (dev) for use by (f).
329  *
330  * Note that the philosophy here is "give them exactly what
331  * they ask for".  This is necessary because being too "smart"
332  * about what the user might want leads to complications.
333  * (eg. given no slice or partition value, with a disk that is
334  *  sliced - are they after the first BSD slice, or the DOS
335  *  slice before it?)
336  */
337 static int
338 bd_open(struct open_file *f, ...)
339 {
340         struct disk_devdesc *dev;
341         struct disk_devdesc disk;
342         va_list ap;
343         uint64_t size;
344         int rc;
345
346         va_start(ap, f);
347         dev = va_arg(ap, struct disk_devdesc *);
348         va_end(ap);
349
350         if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo)
351                 return (EIO);
352         BD(dev).bd_open++;
353         if (BD(dev).bd_bcache == NULL)
354             BD(dev).bd_bcache = bcache_allocate();
355
356         /*
357          * Read disk size from partition.
358          * This is needed to work around buggy BIOS systems returning
359          * wrong (truncated) disk media size.
360          * During bd_probe() we tested if the mulitplication of bd_sectors
361          * would overflow so it should be safe to perform here.
362          */
363         disk.dd.d_dev = dev->dd.d_dev;
364         disk.dd.d_unit = dev->dd.d_unit;
365         disk.d_slice = -1;
366         disk.d_partition = -1;
367         disk.d_offset = 0;
368
369         if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
370             BD(dev).bd_sectorsize) == 0) {
371
372                 if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) {
373                         size /= BD(dev).bd_sectorsize;
374                         if (size > BD(dev).bd_sectors)
375                                 BD(dev).bd_sectors = size;
376                 }
377                 disk_close(&disk);
378         }
379
380         rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
381             BD(dev).bd_sectorsize);
382         if (rc != 0) {
383                 BD(dev).bd_open--;
384                 if (BD(dev).bd_open == 0) {
385                         bcache_free(BD(dev).bd_bcache);
386                         BD(dev).bd_bcache = NULL;
387                 }
388         }
389         return (rc);
390 }
391
392 static int
393 bd_close(struct open_file *f)
394 {
395         struct disk_devdesc *dev;
396
397         dev = (struct disk_devdesc *)f->f_devdata;
398         BD(dev).bd_open--;
399         if (BD(dev).bd_open == 0) {
400             bcache_free(BD(dev).bd_bcache);
401             BD(dev).bd_bcache = NULL;
402         }
403         return (disk_close(dev));
404 }
405
406 static int
407 bd_ioctl(struct open_file *f, u_long cmd, void *data)
408 {
409         struct disk_devdesc *dev;
410         int rc;
411
412         dev = (struct disk_devdesc *)f->f_devdata;
413
414         rc = disk_ioctl(dev, cmd, data);
415         if (rc != ENOTTY)
416                 return (rc);
417
418         switch (cmd) {
419         case DIOCGSECTORSIZE:
420                 *(uint32_t *)data = BD(dev).bd_sectorsize;
421                 break;
422         case DIOCGMEDIASIZE:
423                 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
424                 break;
425         default:
426                 return (ENOTTY);
427         }
428         return (0);
429 }
430
431 static int
432 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
433     char *buf, size_t *rsize)
434 {
435         struct bcache_devdata bcd;
436         struct disk_devdesc *dev;
437
438         dev = (struct disk_devdesc *)devdata;
439         bcd.dv_strategy = bd_realstrategy;
440         bcd.dv_devdata = devdata;
441         bcd.dv_cache = BD(dev).bd_bcache;
442         return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
443             buf, rsize));
444 }
445
446 static int
447 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
448     char *buf, size_t *rsize)
449 {
450         struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
451         uint64_t                disk_blocks;
452         int                     blks, rc;
453
454         if (size % BD(dev).bd_sectorsize) {
455                 panic("bd_strategy: %d bytes I/O not multiple of block size",
456                     size);
457         }
458
459         DEBUG("open_disk %p", dev);
460
461         /*
462          * Check the value of the size argument. We do have quite small
463          * heap (64MB), but we do not know good upper limit, so we check against
464          * INT_MAX here. This will also protect us against possible overflows
465          * while translating block count to bytes.
466          */
467         if (size > INT_MAX) {
468                 DEBUG("too large read: %zu bytes", size);
469                 return (EIO);
470         }
471
472         blks = size / BD(dev).bd_sectorsize;
473         if (dblk > dblk + blks)
474                 return (EIO);
475
476         if (rsize)
477                 *rsize = 0;
478
479         /*
480          * Get disk blocks, this value is either for whole disk or for
481          * partition.
482          */
483         if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
484                 /* DIOCGMEDIASIZE returns bytes. */
485                 disk_blocks /= BD(dev).bd_sectorsize;
486         } else {
487                 /* We should not get here. Just try to survive. */
488                 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
489         }
490
491         /* Validate source block address. */
492         if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
493                 return (EIO);
494
495         /*
496          * Truncate if we are crossing disk or partition end.
497          */
498         if (dblk + blks >= dev->d_offset + disk_blocks) {
499                 blks = dev->d_offset + disk_blocks - dblk;
500                 size = blks * BD(dev).bd_sectorsize;
501                 DEBUG("short read %d", blks);
502         }
503
504         switch (rw & F_MASK) {
505         case F_READ:
506                 DEBUG("read %d from %lld to %p", blks, dblk, buf);
507
508                 if (blks && (rc = bd_io(dev, dblk, blks, buf, BD_RD))) {
509                         /* Filter out floppy controller errors */
510                         if (BD(dev).bd_flags != BD_FLOPPY || rc != 0x20) {
511                                 printf("read %d from %lld to %p, error: 0x%x\n",
512                                     blks, dblk, buf, rc);
513                         }
514                         return (EIO);
515                 }
516                 break;
517         case F_WRITE :
518                 DEBUG("write %d from %lld to %p", blks, dblk, buf);
519
520                 if (blks && bd_io(dev, dblk, blks, buf, BD_WR)) {
521                         DEBUG("write error");
522                         return (EIO);
523                 }
524                 break;
525         default:
526                 /* DO NOTHING */
527                 return (EROFS);
528         }
529
530         if (rsize)
531                 *rsize = size;
532         return (0);
533 }
534
535 static int
536 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
537     int dowrite)
538 {
539         static struct edd_packet packet;
540
541         packet.len = sizeof(struct edd_packet);
542         packet.count = blks;
543         packet.off = VTOPOFF(dest);
544         packet.seg = VTOPSEG(dest);
545         packet.lba = dblk;
546         v86.ctl = V86_FLAGS;
547         v86.addr = 0x13;
548         /* Should we Write with verify ?? 0x4302 ? */
549         if (dowrite == BD_WR)
550                 v86.eax = 0x4300;
551         else
552                 v86.eax = 0x4200;
553         v86.edx = BD(dev).bd_unit;
554         v86.ds = VTOPSEG(&packet);
555         v86.esi = VTOPOFF(&packet);
556         v86int();
557         if (V86_CY(v86.efl))
558                 return (v86.eax >> 8);
559         return (0);
560 }
561
562 static int
563 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
564     int dowrite)
565 {
566         uint32_t x, bpc, cyl, hd, sec;
567
568         bpc = BD(dev).bd_sec * BD(dev).bd_hds;  /* blocks per cylinder */
569         x = dblk;
570         cyl = x / bpc;                  /* block # / blocks per cylinder */
571         x %= bpc;                               /* block offset into cylinder */
572         hd = x / BD(dev).bd_sec;                /* offset / blocks per track */
573         sec = x % BD(dev).bd_sec;               /* offset into track */
574
575         /* correct sector number for 1-based BIOS numbering */
576         sec++;
577
578         if (cyl > 1023) {
579                 /* CHS doesn't support cylinders > 1023. */
580                 return (1);
581         }
582
583         v86.ctl = V86_FLAGS;
584         v86.addr = 0x13;
585         if (dowrite == BD_WR)
586                 v86.eax = 0x300 | blks;
587         else
588                 v86.eax = 0x200 | blks;
589         v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
590         v86.edx = (hd << 8) | BD(dev).bd_unit;
591         v86.es = VTOPSEG(dest);
592         v86.ebx = VTOPOFF(dest);
593         v86int();
594         if (V86_CY(v86.efl))
595                 return (v86.eax >> 8);
596         return (0);
597 }
598
599 static void
600 bd_io_workaround(struct disk_devdesc *dev)
601 {
602         uint8_t buf[8 * 1024];
603
604         bd_edd_io(dev, 0xffffffff, 1, (caddr_t)buf, BD_RD);
605 }
606
607
608 static int
609 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
610     int dowrite)
611 {
612         u_int   x, sec, result, resid, retry, maxfer;
613         caddr_t p, xp, bbuf;
614     
615         /* Just in case some idiot actually tries to read/write -1 blocks... */
616         if (blks < 0)
617                 return (-1);
618
619         resid = blks;
620         p = dest;
621
622         /*
623          * Workaround for a problem with some HP ProLiant BIOS failing to work
624          * out the boot disk after installation. hrs and kuriyama discovered
625          * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
626          * discovered that an int13h call seems to cause a buffer overrun in
627          * the bios. The problem is alleviated by doing an extra read before
628          * the buggy read. It is not immediately known whether other models
629          * are similarly affected.
630          */
631         if (dowrite == BD_RD && dblk >= 0x100000000)
632                 bd_io_workaround(dev);
633
634         /* Decide whether we have to bounce */
635         if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
636             (VTOP(dest) >> 16) !=
637             (VTOP(dest + blks * BD(dev).bd_sectorsize) >> 16))) {
638
639                 /* 
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.
646                  */
647                 x = V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize;
648                 x = min(x, (unsigned)blks);
649                 bbuf = PTOV(V86_IO_BUFFER);
650                 maxfer = x;     /* limit transfers to bounce region size */
651         } else {
652                 bbuf = NULL;
653                 maxfer = 0;
654         }
655     
656         while (resid > 0) {
657                 /*
658                  * Play it safe and don't cross track boundaries.
659                  * (XXX this is probably unnecessary)
660                  */
661                 sec = dblk % BD(dev).bd_sec;    /* offset into track */
662                 x = min(BD(dev).bd_sec - sec, resid);
663                 if (maxfer > 0)
664                         x = min(x, maxfer);             /* fit bounce buffer */
665
666                 /* where do we transfer to? */
667                 xp = bbuf == NULL ? p : bbuf;
668
669                 /*
670                  * Put your Data In, Put your Data out,
671                  * Put your Data In, and shake it all about 
672                  */
673                 if (dowrite == BD_WR && bbuf != NULL)
674                         bcopy(p, bbuf, x * BD(dev).bd_sectorsize);
675
676                 /*
677                  * Loop retrying the operation a couple of times.  The BIOS
678                  * may also retry.
679                  */
680                 for (retry = 0; retry < 3; retry++) {
681                         /* if retrying, reset the drive */
682                         if (retry > 0) {
683                                 v86.ctl = V86_FLAGS;
684                                 v86.addr = 0x13;
685                                 v86.eax = 0;
686                                 v86.edx = BD(dev).bd_unit;
687                                 v86int();
688                         }
689
690                         if (BD(dev).bd_flags & BD_MODEEDD1)
691                                 result = bd_edd_io(dev, dblk, x, xp, dowrite);
692                         else
693                                 result = bd_chs_io(dev, dblk, x, xp, dowrite);
694                         if (result == 0)
695                                 break;
696                 }
697
698                 if (dowrite == BD_WR)
699                         DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x,
700                             p, VTOP(p), dblk, result ? "failed" : "ok");
701                 else
702                         DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x,
703                             dblk, p, VTOP(p), result ? "failed" : "ok");
704                 if (result) {
705                         return (result);
706                 }
707                 if (dowrite == BD_RD && bbuf != NULL)
708                         bcopy(bbuf, p, x * BD(dev).bd_sectorsize);
709                 p += (x * BD(dev).bd_sectorsize);
710                 dblk += x;
711                 resid -= x;
712         }
713
714         return (0);
715 }
716
717 /*
718  * Return the BIOS geometry of a given "fixed drive" in a format
719  * suitable for the legacy bootinfo structure.  Since the kernel is
720  * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
721  * prefer to get the information directly, rather than rely on being
722  * able to put it together from information already maintained for
723  * different purposes and for a probably different number of drives.
724  *
725  * For valid drives, the geometry is expected in the format (31..0)
726  * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
727  * indicated by returning the geometry of a "1.2M" PC-format floppy
728  * disk.  And, incidentally, what is returned is not the geometry as
729  * such but the highest valid cylinder, head, and sector numbers.
730  */
731 uint32_t
732 bd_getbigeom(int bunit)
733 {
734
735         v86.ctl = V86_FLAGS;
736         v86.addr = 0x13;
737         v86.eax = 0x800;
738         v86.edx = 0x80 + bunit;
739         v86int();
740         if (V86_CY(v86.efl))
741                 return (0x4f010f);
742         return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
743             (v86.edx & 0xff00) | (v86.ecx & 0x3f));
744 }
745
746 /*
747  * Return a suitable dev_t value for (dev).
748  *
749  * In the case where it looks like (dev) is a SCSI disk, we allow the number of
750  * IDE disks to be specified in $num_ide_disks.  There should be a Better Way.
751  */
752 int
753 bd_getdev(struct i386_devdesc *d)
754 {
755         struct disk_devdesc *dev;
756         int     biosdev;
757         int     major;
758         int     rootdev;
759         char    *nip, *cp;
760         int     i, unit;
761
762         dev = (struct disk_devdesc *)d;
763         biosdev = bd_unit2bios(dev->dd.d_unit);
764         DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
765         if (biosdev == -1)                      /* not a BIOS device */
766                 return (-1);
767         if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
768             BD(dev).bd_sectorsize) != 0)        /* oops, not a viable device */
769                 return (-1);
770         else
771                 disk_close(dev);
772
773         if (biosdev < 0x80) {
774                 /* floppy (or emulated floppy) or ATAPI device */
775                 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
776                         /* is an ATAPI disk */
777                         major = WFDMAJOR;
778                 } else {
779                         /* is a floppy disk */
780                         major = FDMAJOR;
781                 }
782         } else {
783                 /* assume an IDE disk */
784                 major = WDMAJOR;
785         }
786         /* default root disk unit number */
787         unit = biosdev & 0x7f;
788
789         /* XXX a better kludge to set the root disk unit number */
790         if ((nip = getenv("root_disk_unit")) != NULL) {
791                 i = strtol(nip, &cp, 0);
792                 /* check for parse error */
793                 if ((cp != nip) && (*cp == 0))
794                         unit = i;
795         }
796
797         rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
798         DEBUG("dev is 0x%x\n", rootdev);
799         return (rootdev);
800 }