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 /*
30 * BIOS disk device handling.
31 *
32 * Ideas and algorithms from:
33 *
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
36 *
37 */
38
39 #include <sys/disk.h>
40 #include <sys/limits.h>
41 #include <sys/queue.h>
42 #include <stand.h>
43 #include <machine/bootinfo.h>
44 #include <stdarg.h>
45 #include <stdbool.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 #define ACDMAJOR 117
63 #define CDMAJOR 15
64
65 /*
66 * INT13 commands
67 */
68 #define CMD_RESET 0x0000
69 #define CMD_READ_CHS 0x0200
70 #define CMD_WRITE_CHS 0x0300
71 #define CMD_READ_PARAM 0x0800
72 #define CMD_DRIVE_TYPE 0x1500
73 #define CMD_CHECK_EDD 0x4100
74 #define CMD_READ_LBA 0x4200
75 #define CMD_WRITE_LBA 0x4300
76 #define CMD_EXT_PARAM 0x4800
77 #define CMD_CD_GET_STATUS 0x4b01
78
79 #define DISK_BIOS 0x13
80
81 #ifdef DISK_DEBUG
82 #define DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
83 #else
84 #define DPRINTF(fmt, args...) ((void)0)
85 #endif
86
87 struct specification_packet {
88 uint8_t sp_size;
89 uint8_t sp_bootmedia;
90 uint8_t sp_drive;
91 uint8_t sp_controller;
92 uint32_t sp_lba;
93 uint16_t sp_devicespec;
94 uint16_t sp_buffersegment;
95 uint16_t sp_loadsegment;
96 uint16_t sp_sectorcount;
97 uint16_t sp_cylsec;
98 uint8_t sp_head;
99 uint8_t sp_dummy[16]; /* Avoid memory corruption */
100 };
101
102 /*
103 * List of BIOS devices, translation from disk unit number to
104 * BIOS unit number.
105 */
106 typedef struct bdinfo
107 {
108 STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */
109 int bd_unit; /* BIOS unit number */
110 int bd_cyl; /* BIOS geometry */
111 int bd_hds;
112 int bd_sec;
113 int bd_flags;
114 #define BD_MODEINT13 0x0000
115 #define BD_MODEEDD1 0x0001
116 #define BD_MODEEDD3 0x0002
117 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
118 #define BD_MODEMASK 0x0003
119 #define BD_FLOPPY 0x0004
120 #define BD_CDROM 0x0008
121 #define BD_NO_MEDIA 0x0010
122 int bd_type; /* BIOS 'drive type' (floppy only) */
123 uint16_t bd_sectorsize; /* Sector size */
124 uint64_t bd_sectors; /* Disk size */
125 int bd_open; /* reference counter */
126 void *bd_bcache; /* buffer cache data */
127 } bdinfo_t;
128
129 #define BD_RD 0
130 #define BD_WR 1
131
132 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t;
133 static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
134 static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
135 static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
136
137 static void bd_io_workaround(bdinfo_t *);
138 static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int);
139 static bool bd_int13probe(bdinfo_t *);
140
141 static int bd_init(void);
142 static int cd_init(void);
143 static int fd_init(void);
144 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
145 char *buf, size_t *rsize);
146 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
147 char *buf, size_t *rsize);
148 static int bd_open(struct open_file *f, ...);
149 static int bd_close(struct open_file *f);
150 static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
151 static int bd_print(int verbose);
152 static int cd_print(int verbose);
153 static int fd_print(int verbose);
154 static void bd_reset_disk(int);
155 static int bd_get_diskinfo_std(struct bdinfo *);
156
157 struct devsw biosfd = {
158 .dv_name = "fd",
159 .dv_type = DEVT_FD,
160 .dv_init = fd_init,
161 .dv_strategy = bd_strategy,
162 .dv_open = bd_open,
163 .dv_close = bd_close,
164 .dv_ioctl = bd_ioctl,
165 .dv_print = fd_print,
166 .dv_cleanup = nullsys,
167 };
168
169 struct devsw bioscd = {
170 .dv_name = "cd",
171 .dv_type = DEVT_CD,
172 .dv_init = cd_init,
173 .dv_strategy = bd_strategy,
174 .dv_open = bd_open,
175 .dv_close = bd_close,
176 .dv_ioctl = bd_ioctl,
177 .dv_print = cd_print,
178 .dv_cleanup = nullsys,
179 };
180
181 struct devsw bioshd = {
182 .dv_name = "disk",
183 .dv_type = DEVT_DISK,
184 .dv_init = bd_init,
185 .dv_strategy = bd_strategy,
186 .dv_open = bd_open,
187 .dv_close = bd_close,
188 .dv_ioctl = bd_ioctl,
189 .dv_print = bd_print,
190 .dv_cleanup = nullsys,
191 .dv_fmtdev = disk_fmtdev,
192 .dv_parsedev = disk_parsedev,
193 };
194
195 static bdinfo_list_t *
bd_get_bdinfo_list(struct devsw * dev)196 bd_get_bdinfo_list(struct devsw *dev)
197 {
198 if (dev->dv_type == DEVT_DISK)
199 return (&hdinfo);
200 if (dev->dv_type == DEVT_CD)
201 return (&cdinfo);
202 if (dev->dv_type == DEVT_FD)
203 return (&fdinfo);
204 return (NULL);
205 }
206
207 /* XXX this gets called way way too often, investigate */
208 static bdinfo_t *
bd_get_bdinfo(struct devdesc * dev)209 bd_get_bdinfo(struct devdesc *dev)
210 {
211 bdinfo_list_t *bdi;
212 bdinfo_t *bd = NULL;
213 int unit;
214
215 bdi = bd_get_bdinfo_list(dev->d_dev);
216 if (bdi == NULL)
217 return (bd);
218
219 unit = 0;
220 STAILQ_FOREACH(bd, bdi, bd_link) {
221 if (unit == dev->d_unit)
222 return (bd);
223 unit++;
224 }
225 return (bd);
226 }
227
228 /*
229 * Translate between BIOS device numbers and our private unit numbers.
230 */
231 int
bd_bios2unit(int biosdev)232 bd_bios2unit(int biosdev)
233 {
234 bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL };
235 bdinfo_t *bd;
236 int i, unit;
237
238 DPRINTF("looking for bios device 0x%x", biosdev);
239 for (i = 0; bdi[i] != NULL; i++) {
240 unit = 0;
241 STAILQ_FOREACH(bd, bdi[i], bd_link) {
242 if (bd->bd_unit == biosdev) {
243 DPRINTF("bd unit %d is BIOS device 0x%x", unit,
244 bd->bd_unit);
245 return (unit);
246 }
247 unit++;
248 }
249 }
250 return (-1);
251 }
252
253 int
bd_unit2bios(struct i386_devdesc * dev)254 bd_unit2bios(struct i386_devdesc *dev)
255 {
256 bdinfo_list_t *bdi;
257 bdinfo_t *bd;
258 int unit;
259
260 bdi = bd_get_bdinfo_list(dev->dd.d_dev);
261 if (bdi == NULL)
262 return (-1);
263
264 unit = 0;
265 STAILQ_FOREACH(bd, bdi, bd_link) {
266 if (unit == dev->dd.d_unit)
267 return (bd->bd_unit);
268 unit++;
269 }
270 return (-1);
271 }
272
273 /*
274 * Use INT13 AH=15 - Read Drive Type.
275 */
276 static int
fd_count(void)277 fd_count(void)
278 {
279 int drive;
280
281 for (drive = 0; drive < MAXBDDEV; drive++) {
282 bd_reset_disk(drive);
283
284 v86.ctl = V86_FLAGS;
285 v86.addr = DISK_BIOS;
286 v86.eax = CMD_DRIVE_TYPE;
287 v86.edx = drive;
288 v86int();
289
290 if (V86_CY(v86.efl))
291 break;
292
293 if ((v86.eax & 0x300) == 0)
294 break;
295 }
296
297 return (drive);
298 }
299
300 /*
301 * Quiz the BIOS for disk devices, save a little info about them.
302 */
303 static int
fd_init(void)304 fd_init(void)
305 {
306 int unit, numfd;
307 bdinfo_t *bd;
308
309 numfd = fd_count();
310 for (unit = 0; unit < numfd; unit++) {
311 if ((bd = calloc(1, sizeof(*bd))) == NULL)
312 break;
313
314 bd->bd_sectorsize = BIOSDISK_SECSIZE;
315 bd->bd_flags = BD_FLOPPY;
316 bd->bd_unit = unit;
317
318 /* Use std diskinfo for floppy drive */
319 if (bd_get_diskinfo_std(bd) != 0) {
320 free(bd);
321 break;
322 }
323 if (bd->bd_sectors == 0)
324 bd->bd_flags |= BD_NO_MEDIA;
325
326 printf("BIOS drive %c: is %s%d\n", ('A' + unit),
327 biosfd.dv_name, unit);
328
329 STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link);
330 }
331
332 bcache_add_dev(unit);
333 return (0);
334 }
335
336 static int
bd_init(void)337 bd_init(void)
338 {
339 int base, unit;
340 bdinfo_t *bd;
341
342 TSENTER();
343
344 base = 0x80;
345 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
346 /*
347 * Check the BIOS equipment list for number of fixed disks.
348 */
349 if ((bd = calloc(1, sizeof(*bd))) == NULL)
350 break;
351 bd->bd_unit = base + unit;
352 if (!bd_int13probe(bd)) {
353 free(bd);
354 break;
355 }
356
357 printf("BIOS drive %c: is %s%d\n", ('C' + unit),
358 bioshd.dv_name, unit);
359
360 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
361 }
362 bcache_add_dev(unit);
363 TSEXIT();
364 return (0);
365 }
366
367 /*
368 * We can't quiz, we have to be told what device to use, so this function
369 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
370 * device number to add.
371 */
372 static int
cd_init(void)373 cd_init(void)
374 {
375
376 return (0);
377 }
378
379 /*
380 * Information from bootable CD-ROM.
381 */
382 static int
bd_get_diskinfo_cd(struct bdinfo * bd)383 bd_get_diskinfo_cd(struct bdinfo *bd)
384 {
385 struct specification_packet bc_sp;
386 int ret = -1;
387
388 (void) memset(&bc_sp, 0, sizeof (bc_sp));
389 /* Set sp_size as per specification. */
390 bc_sp.sp_size = sizeof (bc_sp) - sizeof (bc_sp.sp_dummy);
391
392 v86.ctl = V86_FLAGS;
393 v86.addr = DISK_BIOS;
394 v86.eax = CMD_CD_GET_STATUS;
395 v86.edx = bd->bd_unit;
396 v86.ds = VTOPSEG(&bc_sp);
397 v86.esi = VTOPOFF(&bc_sp);
398 v86int();
399
400 if ((v86.eax & 0xff00) == 0 &&
401 bc_sp.sp_drive == bd->bd_unit) {
402 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
403 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
404 bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
405 bd->bd_hds = bc_sp.sp_head + 1;
406 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
407
408 if (bc_sp.sp_bootmedia & 0x0F) {
409 /* Floppy or hard-disk emulation */
410 bd->bd_sectorsize = BIOSDISK_SECSIZE;
411 return (-1);
412 } else {
413 bd->bd_sectorsize = 2048;
414 bd->bd_flags = BD_MODEEDD | BD_CDROM;
415 ret = 0;
416 }
417 }
418
419 /*
420 * If this is the boot_drive, default to non-emulation bootable CD-ROM.
421 */
422 if (ret != 0 && bd->bd_unit >= 0x88) {
423 bd->bd_cyl = 0;
424 bd->bd_hds = 1;
425 bd->bd_sec = 15;
426 bd->bd_sectorsize = 2048;
427 bd->bd_flags = BD_MODEEDD | BD_CDROM;
428 bd->bd_sectors = 0;
429 ret = 0;
430 }
431
432 /*
433 * Note we can not use bd_get_diskinfo_ext() nor bd_get_diskinfo_std()
434 * here - some systems do get hung with those.
435 */
436 /*
437 * Still no size? use 7.961GB. The size does not really matter
438 * as long as it is reasonably large to make our reads to pass
439 * the sector count check.
440 */
441 if (bd->bd_sectors == 0)
442 bd->bd_sectors = 4173824;
443
444 return (ret);
445 }
446
447 int
bc_add(int biosdev)448 bc_add(int biosdev)
449 {
450 bdinfo_t *bd;
451 int nbcinfo = 0;
452
453 if (!STAILQ_EMPTY(&cdinfo))
454 return (-1);
455
456 if ((bd = calloc(1, sizeof(*bd))) == NULL)
457 return (-1);
458
459 bd->bd_unit = biosdev;
460 if (bd_get_diskinfo_cd(bd) < 0) {
461 free(bd);
462 return (-1);
463 }
464
465 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
466 printf("BIOS CD is cd%d\n", nbcinfo);
467 nbcinfo++;
468 bcache_add_dev(nbcinfo); /* register cd device in bcache */
469 return(0);
470 }
471
472 /*
473 * Return EDD version or 0 if EDD is not supported on this drive.
474 */
475 static int
bd_check_extensions(int unit)476 bd_check_extensions(int unit)
477 {
478 /* do not use ext calls for floppy devices */
479 if (unit < 0x80)
480 return (0);
481
482 /* Determine if we can use EDD with this device. */
483 v86.ctl = V86_FLAGS;
484 v86.addr = DISK_BIOS;
485 v86.eax = CMD_CHECK_EDD;
486 v86.edx = unit;
487 v86.ebx = EDD_QUERY_MAGIC;
488 v86int();
489
490 if (V86_CY(v86.efl) || /* carry set */
491 (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */
492 return (0);
493
494 /* extended disk access functions (AH=42h-44h,47h,48h) supported */
495 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
496 return (0);
497
498 return ((v86.eax >> 8) & 0xff);
499 }
500
501 static void
bd_reset_disk(int unit)502 bd_reset_disk(int unit)
503 {
504 /* reset disk */
505 v86.ctl = V86_FLAGS;
506 v86.addr = DISK_BIOS;
507 v86.eax = CMD_RESET;
508 v86.edx = unit;
509 v86int();
510 }
511
512 /*
513 * Read CHS info. Return 0 on success, error otherwise.
514 */
515 static int
bd_get_diskinfo_std(struct bdinfo * bd)516 bd_get_diskinfo_std(struct bdinfo *bd)
517 {
518 bzero(&v86, sizeof(v86));
519 v86.ctl = V86_FLAGS;
520 v86.addr = DISK_BIOS;
521 v86.eax = CMD_READ_PARAM;
522 v86.edx = bd->bd_unit;
523 v86int();
524
525 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
526 return ((v86.eax & 0xff00) >> 8);
527
528 /* return custom error on absurd sector number */
529 if ((v86.ecx & 0x3f) == 0)
530 return (0x60);
531
532 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
533 /* Convert max head # -> # of heads */
534 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
535 bd->bd_sec = v86.ecx & 0x3f;
536 bd->bd_type = v86.ebx;
537 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
538
539 return (0);
540 }
541
542 /*
543 * Read EDD info. Return 0 on success, error otherwise.
544 *
545 * Avoid stack corruption on some systems by adding extra bytes to
546 * params block.
547 */
548 static int
bd_get_diskinfo_ext(struct bdinfo * bd)549 bd_get_diskinfo_ext(struct bdinfo *bd)
550 {
551 struct disk_params {
552 struct edd_params head;
553 struct edd_device_path_v3 device_path;
554 uint8_t dummy[16];
555 } __packed dparams;
556 struct edd_params *params;
557 uint64_t total;
558
559 params = &dparams.head;
560
561 /* Get disk params */
562 bzero(&dparams, sizeof(dparams));
563 params->len = sizeof(struct edd_params_v3);
564 v86.ctl = V86_FLAGS;
565 v86.addr = DISK_BIOS;
566 v86.eax = CMD_EXT_PARAM;
567 v86.edx = bd->bd_unit;
568 v86.ds = VTOPSEG(&dparams);
569 v86.esi = VTOPOFF(&dparams);
570 v86int();
571
572 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0))
573 return ((v86.eax & 0xff00) >> 8);
574
575 /*
576 * Sector size must be a multiple of 512 bytes.
577 * An alternate test would be to check power of 2,
578 * powerof2(params.sector_size).
579 * 16K is largest read buffer we can use at this time.
580 */
581 if (params->sector_size >= 512 &&
582 params->sector_size <= 16384 &&
583 (params->sector_size % BIOSDISK_SECSIZE) == 0)
584 bd->bd_sectorsize = params->sector_size;
585
586 bd->bd_cyl = params->cylinders;
587 bd->bd_hds = params->heads;
588 bd->bd_sec = params->sectors_per_track;
589
590 if (params->sectors != 0) {
591 total = params->sectors;
592 } else {
593 total = (uint64_t)params->cylinders *
594 params->heads * params->sectors_per_track;
595 }
596 bd->bd_sectors = total;
597
598 return (0);
599 }
600
601 /*
602 * Try to detect a device supported by the legacy int13 BIOS
603 */
604 static bool
bd_int13probe(bdinfo_t * bd)605 bd_int13probe(bdinfo_t *bd)
606 {
607 int edd, ret;
608
609 bd->bd_flags &= ~BD_NO_MEDIA;
610
611 if ((bd->bd_flags & BD_CDROM) != 0) {
612 return (bd_get_diskinfo_cd(bd) == 0);
613 }
614
615 edd = bd_check_extensions(bd->bd_unit);
616 if (edd == 0)
617 bd->bd_flags |= BD_MODEINT13;
618 else if (edd < 0x30)
619 bd->bd_flags |= BD_MODEEDD1;
620 else
621 bd->bd_flags |= BD_MODEEDD3;
622
623 /* Default sector size */
624 if (bd->bd_sectorsize == 0)
625 bd->bd_sectorsize = BIOSDISK_SECSIZE;
626
627 /*
628 * Test if the floppy device is present, so we can avoid receiving
629 * bogus information from bd_get_diskinfo_std().
630 */
631 if (bd->bd_unit < 0x80) {
632 /* reset disk */
633 bd_reset_disk(bd->bd_unit);
634
635 /* Get disk type */
636 v86.ctl = V86_FLAGS;
637 v86.addr = DISK_BIOS;
638 v86.eax = CMD_DRIVE_TYPE;
639 v86.edx = bd->bd_unit;
640 v86int();
641 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
642 return (false);
643 }
644
645 ret = 1;
646 if (edd != 0)
647 ret = bd_get_diskinfo_ext(bd);
648 if (ret != 0 || bd->bd_sectors == 0)
649 ret = bd_get_diskinfo_std(bd);
650
651 if (ret != 0 && bd->bd_unit < 0x80) {
652 /* Set defaults for 1.44 floppy */
653 bd->bd_cyl = 80;
654 bd->bd_hds = 2;
655 bd->bd_sec = 18;
656 bd->bd_sectors = 2880;
657 /* Since we are there, there most likely is no media */
658 bd->bd_flags |= BD_NO_MEDIA;
659 ret = 0;
660 }
661
662 if (ret != 0) {
663 if (bd->bd_sectors != 0 && edd != 0) {
664 bd->bd_sec = 63;
665 bd->bd_hds = 255;
666 bd->bd_cyl =
667 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
668 bd->bd_sec * bd->bd_hds;
669 } else {
670 const char *dv_name;
671
672 if ((bd->bd_flags & BD_FLOPPY) != 0)
673 dv_name = biosfd.dv_name;
674 else
675 dv_name = bioshd.dv_name;
676
677 printf("Can not get information about %s unit %#x\n",
678 dv_name, bd->bd_unit);
679 return (false);
680 }
681 }
682
683 if (bd->bd_sec == 0)
684 bd->bd_sec = 63;
685 if (bd->bd_hds == 0)
686 bd->bd_hds = 255;
687
688 if (bd->bd_sectors == 0)
689 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
690
691 DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
692 bd->bd_hds, bd->bd_sec);
693
694 return (true);
695 }
696
697 static int
bd_count(bdinfo_list_t * bdi)698 bd_count(bdinfo_list_t *bdi)
699 {
700 bdinfo_t *bd;
701 int i;
702
703 i = 0;
704 STAILQ_FOREACH(bd, bdi, bd_link)
705 i++;
706 return (i);
707 }
708
709 /*
710 * Print information about disks
711 */
712 static int
bd_print_common(struct devsw * dev,bdinfo_list_t * bdi,int verbose)713 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
714 {
715 char line[80];
716 struct disk_devdesc devd;
717 bdinfo_t *bd;
718 int i, ret = 0;
719 char drive;
720
721 if (STAILQ_EMPTY(bdi))
722 return (0);
723
724 printf("%s devices:", dev->dv_name);
725 if ((ret = pager_output("\n")) != 0)
726 return (ret);
727
728 i = -1;
729 STAILQ_FOREACH(bd, bdi, bd_link) {
730 i++;
731
732 switch (dev->dv_type) {
733 case DEVT_FD:
734 drive = 'A';
735 break;
736 case DEVT_CD:
737 drive = 'C' + bd_count(&hdinfo);
738 break;
739 default:
740 drive = 'C';
741 break;
742 }
743
744 snprintf(line, sizeof(line),
745 " %s%d: BIOS drive %c (%s%ju X %u):\n",
746 dev->dv_name, i, drive + i,
747 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
748 "no media, " : "",
749 (uintmax_t)bd->bd_sectors,
750 bd->bd_sectorsize);
751 if ((ret = pager_output(line)) != 0)
752 break;
753
754 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
755 continue;
756
757 if (dev->dv_type != DEVT_DISK)
758 continue;
759
760 devd.dd.d_dev = dev;
761 devd.dd.d_unit = i;
762 devd.d_slice = D_SLICENONE;
763 devd.d_partition = D_PARTNONE;
764 if (disk_open(&devd,
765 bd->bd_sectorsize * bd->bd_sectors,
766 bd->bd_sectorsize) == 0) {
767 snprintf(line, sizeof(line), " %s%d",
768 dev->dv_name, i);
769 ret = disk_print(&devd, line, verbose);
770 disk_close(&devd);
771 if (ret != 0)
772 break;
773 }
774 }
775 return (ret);
776 }
777
778 static int
fd_print(int verbose)779 fd_print(int verbose)
780 {
781 return (bd_print_common(&biosfd, &fdinfo, verbose));
782 }
783
784 static int
bd_print(int verbose)785 bd_print(int verbose)
786 {
787 return (bd_print_common(&bioshd, &hdinfo, verbose));
788 }
789
790 static int
cd_print(int verbose)791 cd_print(int verbose)
792 {
793 return (bd_print_common(&bioscd, &cdinfo, verbose));
794 }
795
796 /*
797 * Read disk size from partition.
798 * This is needed to work around buggy BIOS systems returning
799 * wrong (truncated) disk media size.
800 * During bd_probe() we tested if the multiplication of bd_sectors
801 * would overflow so it should be safe to perform here.
802 */
803 static uint64_t
bd_disk_get_sectors(struct disk_devdesc * dev)804 bd_disk_get_sectors(struct disk_devdesc *dev)
805 {
806 bdinfo_t *bd;
807 struct disk_devdesc disk;
808 uint64_t size;
809
810 bd = bd_get_bdinfo(&dev->dd);
811 if (bd == NULL)
812 return (0);
813
814 disk.dd.d_dev = dev->dd.d_dev;
815 disk.dd.d_unit = dev->dd.d_unit;
816 disk.d_slice = D_SLICENONE;
817 disk.d_partition = D_PARTNONE;
818 disk.d_offset = 0;
819
820 size = bd->bd_sectors * bd->bd_sectorsize;
821 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
822 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
823 disk_close(&disk);
824 }
825 return (size / bd->bd_sectorsize);
826 }
827
828 /*
829 * Attempt to open the disk described by (dev) for use by (f).
830 *
831 * Note that the philosophy here is "give them exactly what
832 * they ask for". This is necessary because being too "smart"
833 * about what the user might want leads to complications.
834 * (eg. given no slice or partition value, with a disk that is
835 * sliced - are they after the first BSD slice, or the DOS
836 * slice before it?)
837 */
838 static int
bd_open(struct open_file * f,...)839 bd_open(struct open_file *f, ...)
840 {
841 bdinfo_t *bd;
842 struct disk_devdesc *dev;
843 va_list ap;
844 int rc;
845
846 TSENTER();
847
848 va_start(ap, f);
849 dev = va_arg(ap, struct disk_devdesc *);
850 va_end(ap);
851
852 bd = bd_get_bdinfo(&dev->dd);
853 if (bd == NULL)
854 return (EIO);
855
856 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
857 if (!bd_int13probe(bd))
858 return (EIO);
859 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
860 return (EIO);
861 }
862 if (bd->bd_bcache == NULL)
863 bd->bd_bcache = bcache_allocate();
864
865 if (bd->bd_open == 0)
866 bd->bd_sectors = bd_disk_get_sectors(dev);
867 bd->bd_open++;
868
869 rc = 0;
870 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
871 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
872 bd->bd_sectorsize);
873 if (rc != 0) {
874 bd->bd_open--;
875 if (bd->bd_open == 0) {
876 bcache_free(bd->bd_bcache);
877 bd->bd_bcache = NULL;
878 }
879 }
880 }
881 TSEXIT();
882 return (rc);
883 }
884
885 static int
bd_close(struct open_file * f)886 bd_close(struct open_file *f)
887 {
888 struct disk_devdesc *dev;
889 bdinfo_t *bd;
890 int rc = 0;
891
892 dev = (struct disk_devdesc *)f->f_devdata;
893 bd = bd_get_bdinfo(&dev->dd);
894 if (bd == NULL)
895 return (EIO);
896
897 bd->bd_open--;
898 if (bd->bd_open == 0) {
899 bcache_free(bd->bd_bcache);
900 bd->bd_bcache = NULL;
901 }
902 if (dev->dd.d_dev->dv_type == DEVT_DISK)
903 rc = disk_close(dev);
904 return (rc);
905 }
906
907 static int
bd_ioctl(struct open_file * f,u_long cmd,void * data)908 bd_ioctl(struct open_file *f, u_long cmd, void *data)
909 {
910 bdinfo_t *bd;
911 struct disk_devdesc *dev;
912 int rc;
913
914 dev = (struct disk_devdesc *)f->f_devdata;
915 bd = bd_get_bdinfo(&dev->dd);
916 if (bd == NULL)
917 return (EIO);
918
919 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
920 rc = disk_ioctl(dev, cmd, data);
921 if (rc != ENOTTY)
922 return (rc);
923 }
924
925 switch (cmd) {
926 case DIOCGSECTORSIZE:
927 *(uint32_t *)data = bd->bd_sectorsize;
928 break;
929 case DIOCGMEDIASIZE:
930 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
931 break;
932 default:
933 return (ENOTTY);
934 }
935 return (0);
936 }
937
938 static int
bd_strategy(void * devdata,int rw,daddr_t dblk,size_t size,char * buf,size_t * rsize)939 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
940 char *buf, size_t *rsize)
941 {
942 bdinfo_t *bd;
943 struct bcache_devdata bcd;
944 struct disk_devdesc *dev;
945 daddr_t offset;
946
947 dev = (struct disk_devdesc *)devdata;
948 bd = bd_get_bdinfo(&dev->dd);
949 if (bd == NULL)
950 return (EINVAL);
951
952 bcd.dv_strategy = bd_realstrategy;
953 bcd.dv_devdata = devdata;
954 bcd.dv_cache = bd->bd_bcache;
955
956 offset = 0;
957 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
958
959 offset = dev->d_offset * bd->bd_sectorsize;
960 offset /= BIOSDISK_SECSIZE;
961 }
962 return (bcache_strategy(&bcd, rw, dblk + offset, size,
963 buf, rsize));
964 }
965
966 static int
bd_realstrategy(void * devdata,int rw,daddr_t dblk,size_t size,char * buf,size_t * rsize)967 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
968 char *buf, size_t *rsize)
969 {
970 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
971 bdinfo_t *bd;
972 uint64_t disk_blocks, offset, d_offset;
973 size_t blks, blkoff, bsize, bio_size, rest;
974 caddr_t bbuf = NULL;
975 int rc;
976
977 bd = bd_get_bdinfo(&dev->dd);
978 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
979 return (EIO);
980
981 /*
982 * First make sure the IO size is a multiple of 512 bytes. While we do
983 * process partial reads below, the strategy mechanism is built
984 * assuming IO is a multiple of 512B blocks. If the request is not
985 * a multiple of 512B blocks, it has to be some sort of bug.
986 */
987 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
988 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
989 size, BIOSDISK_SECSIZE);
990 return (EIO);
991 }
992
993 DPRINTF("open_disk %p", dev);
994
995 offset = dblk * BIOSDISK_SECSIZE;
996 dblk = offset / bd->bd_sectorsize;
997 blkoff = offset % bd->bd_sectorsize;
998
999 /*
1000 * Check the value of the size argument. We do have quite small
1001 * heap (64MB), but we do not know good upper limit, so we check against
1002 * INT_MAX here. This will also protect us against possible overflows
1003 * while translating block count to bytes.
1004 */
1005 if (size > INT_MAX) {
1006 DPRINTF("too large I/O: %zu bytes", size);
1007 return (EIO);
1008 }
1009
1010 blks = size / bd->bd_sectorsize;
1011 if (blks == 0 || (size % bd->bd_sectorsize) != 0)
1012 blks++;
1013
1014 if (dblk > dblk + blks)
1015 return (EIO);
1016
1017 if (rsize)
1018 *rsize = 0;
1019
1020 /*
1021 * Get disk blocks, this value is either for whole disk or for
1022 * partition.
1023 */
1024 d_offset = 0;
1025 disk_blocks = 0;
1026 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1027 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
1028 /* DIOCGMEDIASIZE does return bytes. */
1029 disk_blocks /= bd->bd_sectorsize;
1030 }
1031 d_offset = dev->d_offset;
1032 }
1033 if (disk_blocks == 0)
1034 disk_blocks = bd->bd_sectors * (bd->bd_sectorsize /
1035 BIOSDISK_SECSIZE) - d_offset;
1036
1037 /* Validate source block address. */
1038 if (dblk < d_offset || dblk >= d_offset + disk_blocks)
1039 return (EIO);
1040
1041 /*
1042 * Truncate if we are crossing disk or partition end.
1043 */
1044 if (dblk + blks >= d_offset + disk_blocks) {
1045 blks = d_offset + disk_blocks - dblk;
1046 size = blks * bd->bd_sectorsize;
1047 DPRINTF("short I/O %d", blks);
1048 }
1049
1050 bio_size = min(BIO_BUFFER_SIZE, size);
1051 while (bio_size > bd->bd_sectorsize) {
1052 bbuf = bio_alloc(bio_size);
1053 if (bbuf != NULL)
1054 break;
1055 bio_size -= bd->bd_sectorsize;
1056 }
1057 if (bbuf == NULL) {
1058 bio_size = V86_IO_BUFFER_SIZE;
1059 if (bio_size / bd->bd_sectorsize == 0)
1060 panic("BUG: Real mode buffer is too small");
1061
1062 /* Use alternate 4k buffer */
1063 bbuf = PTOV(V86_IO_BUFFER);
1064 }
1065 rest = size;
1066 rc = 0;
1067 while (blks > 0) {
1068 int x = min(blks, bio_size / bd->bd_sectorsize);
1069
1070 switch (rw & F_MASK) {
1071 case F_READ:
1072 DPRINTF("read %d from %lld to %p", x, dblk, buf);
1073 bsize = bd->bd_sectorsize * x - blkoff;
1074 if (rest < bsize)
1075 bsize = rest;
1076
1077 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) {
1078 rc = EIO;
1079 goto error;
1080 }
1081
1082 bcopy(bbuf + blkoff, buf, bsize);
1083 break;
1084 case F_WRITE :
1085 DPRINTF("write %d from %lld to %p", x, dblk, buf);
1086 if (blkoff != 0) {
1087 /*
1088 * We got offset to sector, read 1 sector to
1089 * bbuf.
1090 */
1091 x = 1;
1092 bsize = bd->bd_sectorsize - blkoff;
1093 bsize = min(bsize, rest);
1094 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1095 } else if (rest < bd->bd_sectorsize) {
1096 /*
1097 * The remaining block is not full
1098 * sector. Read 1 sector to bbuf.
1099 */
1100 x = 1;
1101 bsize = rest;
1102 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
1103 } else {
1104 /* We can write full sector(s). */
1105 bsize = bd->bd_sectorsize * x;
1106 }
1107 /*
1108 * Put your Data In, Put your Data out,
1109 * Put your Data In, and shake it all about
1110 */
1111 bcopy(buf, bbuf + blkoff, bsize);
1112 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) {
1113 rc = EIO;
1114 goto error;
1115 }
1116
1117 break;
1118 default:
1119 /* DO NOTHING */
1120 rc = EROFS;
1121 goto error;
1122 }
1123
1124 blkoff = 0;
1125 buf += bsize;
1126 rest -= bsize;
1127 blks -= x;
1128 dblk += x;
1129 }
1130
1131 if (rsize != NULL)
1132 *rsize = size;
1133 error:
1134 if (bbuf != PTOV(V86_IO_BUFFER))
1135 bio_free(bbuf, bio_size);
1136 return (rc);
1137 }
1138
1139 static int
bd_edd_io(bdinfo_t * bd,daddr_t dblk,int blks,caddr_t dest,int dowrite)1140 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1141 int dowrite)
1142 {
1143 static struct edd_packet packet;
1144
1145 TSENTER();
1146
1147 packet.len = sizeof(struct edd_packet);
1148 packet.count = blks;
1149 packet.off = VTOPOFF(dest);
1150 packet.seg = VTOPSEG(dest);
1151 packet.lba = dblk;
1152 v86.ctl = V86_FLAGS;
1153 v86.addr = DISK_BIOS;
1154 if (dowrite == BD_WR)
1155 v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */
1156 else
1157 v86.eax = CMD_READ_LBA;
1158 v86.edx = bd->bd_unit;
1159 v86.ds = VTOPSEG(&packet);
1160 v86.esi = VTOPOFF(&packet);
1161 v86int();
1162 if (V86_CY(v86.efl))
1163 return (v86.eax >> 8);
1164
1165 TSEXIT();
1166 return (0);
1167 }
1168
1169 static int
bd_chs_io(bdinfo_t * bd,daddr_t dblk,int blks,caddr_t dest,int dowrite)1170 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
1171 int dowrite)
1172 {
1173 uint32_t x, bpc, cyl, hd, sec;
1174
1175 TSENTER();
1176
1177 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */
1178 x = dblk;
1179 cyl = x / bpc; /* block # / blocks per cylinder */
1180 x %= bpc; /* block offset into cylinder */
1181 hd = x / bd->bd_sec; /* offset / blocks per track */
1182 sec = x % bd->bd_sec; /* offset into track */
1183
1184 /* correct sector number for 1-based BIOS numbering */
1185 sec++;
1186
1187 if (cyl > 1023) {
1188 /* CHS doesn't support cylinders > 1023. */
1189 return (1);
1190 }
1191
1192 v86.ctl = V86_FLAGS;
1193 v86.addr = DISK_BIOS;
1194 if (dowrite == BD_WR)
1195 v86.eax = CMD_WRITE_CHS | blks;
1196 else
1197 v86.eax = CMD_READ_CHS | blks;
1198 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1199 v86.edx = (hd << 8) | bd->bd_unit;
1200 v86.es = VTOPSEG(dest);
1201 v86.ebx = VTOPOFF(dest);
1202 v86int();
1203 if (V86_CY(v86.efl))
1204 return (v86.eax >> 8);
1205 TSEXIT();
1206 return (0);
1207 }
1208
1209 static void
bd_io_workaround(bdinfo_t * bd)1210 bd_io_workaround(bdinfo_t *bd)
1211 {
1212 uint8_t buf[8 * 1024];
1213
1214 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1215 }
1216
1217 static int
bd_io(struct disk_devdesc * dev,bdinfo_t * bd,daddr_t dblk,int blks,caddr_t dest,int dowrite)1218 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1219 caddr_t dest, int dowrite)
1220 {
1221 int result, retry;
1222
1223 TSENTER();
1224
1225 /* Just in case some idiot actually tries to read/write -1 blocks... */
1226 if (blks < 0)
1227 return (-1);
1228
1229 /*
1230 * Workaround for a problem with some HP ProLiant BIOS failing to work
1231 * out the boot disk after installation. hrs and kuriyama discovered
1232 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1233 * discovered that an int13h call seems to cause a buffer overrun in
1234 * the bios. The problem is alleviated by doing an extra read before
1235 * the buggy read. It is not immediately known whether other models
1236 * are similarly affected.
1237 * Loop retrying the operation a couple of times. The BIOS
1238 * may also retry.
1239 */
1240 if (dowrite == BD_RD && dblk >= 0x100000000)
1241 bd_io_workaround(bd);
1242 for (retry = 0; retry < 3; retry++) {
1243 if (bd->bd_flags & BD_MODEEDD)
1244 result = bd_edd_io(bd, dblk, blks, dest, dowrite);
1245 else
1246 result = bd_chs_io(bd, dblk, blks, dest, dowrite);
1247
1248 if (result == 0) {
1249 if (bd->bd_flags & BD_NO_MEDIA)
1250 bd->bd_flags &= ~BD_NO_MEDIA;
1251 break;
1252 }
1253
1254 bd_reset_disk(bd->bd_unit);
1255
1256 /*
1257 * Error codes:
1258 * 20h controller failure
1259 * 31h no media in drive (IBM/MS INT 13 extensions)
1260 * 80h no media in drive, VMWare (Fusion)
1261 * There is no reason to repeat the IO with errors above.
1262 */
1263 if (result == 0x20 || result == 0x31 || result == 0x80) {
1264 bd->bd_flags |= BD_NO_MEDIA;
1265 break;
1266 }
1267 }
1268
1269 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
1270 if (dowrite == BD_WR) {
1271 printf("%s%d: Write %d sector(s) from %p (0x%x) "
1272 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1273 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1274 result);
1275 } else {
1276 printf("%s%d: Read %d sector(s) from %lld to %p "
1277 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,
1278 dev->dd.d_unit, blks, dblk, dest, VTOP(dest),
1279 result);
1280 }
1281 }
1282
1283 TSEXIT();
1284
1285 return (result);
1286 }
1287
1288 /*
1289 * Return a suitable dev_t value for (dev).
1290 *
1291 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1292 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1293 */
1294 int
bd_getdev(struct i386_devdesc * d)1295 bd_getdev(struct i386_devdesc *d)
1296 {
1297 struct disk_devdesc *dev;
1298 bdinfo_t *bd;
1299 int biosdev;
1300 int major;
1301 int rootdev;
1302 char *nip, *cp;
1303 int i, unit, slice, partition;
1304
1305 /* XXX: Assume partition 'a'. */
1306 slice = 0;
1307 partition = 0;
1308
1309 dev = (struct disk_devdesc *)d;
1310 bd = bd_get_bdinfo(&dev->dd);
1311 if (bd == NULL)
1312 return (-1);
1313
1314 biosdev = bd_unit2bios(d);
1315 DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1316 if (biosdev == -1) /* not a BIOS device */
1317 return (-1);
1318
1319 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1320 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1321 bd->bd_sectorsize) != 0) /* oops, not a viable device */
1322 return (-1);
1323 else
1324 disk_close(dev);
1325 slice = dev->d_slice + 1;
1326 partition = dev->d_partition;
1327 }
1328
1329 if (biosdev < 0x80) {
1330 /* floppy (or emulated floppy) or ATAPI device */
1331 if (bd->bd_type == DT_ATAPI) {
1332 /* is an ATAPI disk */
1333 major = WFDMAJOR;
1334 } else {
1335 /* is a floppy disk */
1336 major = FDMAJOR;
1337 }
1338 } else {
1339 /* assume an IDE disk */
1340 major = WDMAJOR;
1341 }
1342 /* default root disk unit number */
1343 unit = biosdev & 0x7f;
1344
1345 if (dev->dd.d_dev->dv_type == DEVT_CD) {
1346 /*
1347 * XXX: Need to examine device spec here to figure out if
1348 * SCSI or ATAPI. No idea on how to figure out device number.
1349 * All we can really pass to the kernel is what bus and device
1350 * on which bus we were booted from, which dev_t isn't well
1351 * suited to since those number don't match to unit numbers
1352 * very well. We may just need to engage in a hack where
1353 * we pass -C to the boot args if we are the boot device.
1354 */
1355 major = ACDMAJOR;
1356 unit = 0; /* XXX */
1357 }
1358
1359 /* XXX a better kludge to set the root disk unit number */
1360 if ((nip = getenv("root_disk_unit")) != NULL) {
1361 i = strtol(nip, &cp, 0);
1362 /* check for parse error */
1363 if ((cp != nip) && (*cp == 0))
1364 unit = i;
1365 }
1366
1367 rootdev = MAKEBOOTDEV(major, slice, unit, partition);
1368 DPRINTF("dev is 0x%x\n", rootdev);
1369 return (rootdev);
1370 }
1371