biosdisk.c (55d5c949431ec940e16cd25f681c986bccce952b) biosdisk.c (cdff10360ef14b687ceb80fd0b6814b55d406fed)
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:

--- 26 unchanged lines hidden (view full) ---

35 *
36 * - NetBSD libi386/biosdisk.c
37 * - FreeBSD biosboot/disk.c
38 *
39 */
40
41#include <sys/disk.h>
42#include <sys/limits.h>
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:

--- 26 unchanged lines hidden (view full) ---

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 <sys/queue.h>
43#include <stand.h>
44#include <machine/bootinfo.h>
45#include <stdarg.h>
44#include <stand.h>
45#include <machine/bootinfo.h>
46#include <stdarg.h>
47#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
48
49#include <bootstrap.h>
50#include <btxv86.h>
51#include <edd.h>
52#include "disk.h"
53#include "libi386.h"
54
55#define BIOS_NUMDRIVES 0x475
56#define BIOSDISK_SECSIZE 512
57#define BUFSIZE (1 * BIOSDISK_SECSIZE)
58
59#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
60#define WDMAJOR 0 /* major numbers for devices we frontend for */
61#define WFDMAJOR 1
62#define FDMAJOR 2
63#define DAMAJOR 4
64#define ACDMAJOR 117
65#define CDMAJOR 15
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
66
67#ifdef DISK_DEBUG
68#define DEBUG(fmt, args...) printf("%s: " fmt "\n", __func__, ## args)
69#else
70#define DEBUG(fmt, args...)
71#endif
72
73struct specification_packet {
74 uint8_t sp_size;
75 uint8_t sp_bootmedia;
76 uint8_t sp_drive;
77 uint8_t sp_controller;
78 uint32_t sp_lba;
79 uint16_t sp_devicespec;
80 uint16_t sp_buffersegment;
81 uint16_t sp_loadsegment;
82 uint16_t sp_sectorcount;
83 uint16_t sp_cylsec;
84 uint8_t sp_head;
85};
86
69/*
70 * List of BIOS devices, translation from disk unit number to
71 * BIOS unit number.
72 */
87/*
88 * List of BIOS devices, translation from disk unit number to
89 * BIOS unit number.
90 */
73static struct bdinfo
91typedef struct bdinfo
74{
92{
93 STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */
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_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
84#define BD_MODEMASK 0x0003
85#define BD_FLOPPY 0x0004
94 int bd_unit; /* BIOS unit number */
95 int bd_cyl; /* BIOS geometry */
96 int bd_hds;
97 int bd_sec;
98 int bd_flags;
99#define BD_MODEINT13 0x0000
100#define BD_MODEEDD1 0x0001
101#define BD_MODEEDD3 0x0002
102#define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3)
103#define BD_MODEMASK 0x0003
104#define BD_FLOPPY 0x0004
86#define BD_NO_MEDIA 0x0008
105#define BD_CDROM 0x0008
106#define BD_NO_MEDIA 0x0010
87 int bd_type; /* BIOS 'drive type' (floppy only) */
88 uint16_t bd_sectorsize; /* Sector size */
89 uint64_t bd_sectors; /* Disk size */
90 int bd_open; /* reference counter */
91 void *bd_bcache; /* buffer cache data */
107 int bd_type; /* BIOS 'drive type' (floppy only) */
108 uint16_t bd_sectorsize; /* Sector size */
109 uint64_t bd_sectors; /* Disk size */
110 int bd_open; /* reference counter */
111 void *bd_bcache; /* buffer cache data */
92} bdinfo [MAXBDDEV];
93static int nbdinfo = 0;
112} bdinfo_t;
94
113
95#define BD(dev) (bdinfo[(dev)->dd.d_unit])
96#define BD_RD 0
97#define BD_WR 1
98
114#define BD_RD 0
115#define BD_WR 1
116
99static void bd_io_workaround(struct disk_devdesc *dev);
117typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t;
118static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo);
119static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo);
120static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
100
121
101static int bd_io(struct disk_devdesc *, daddr_t, int, caddr_t, int);
102static int bd_int13probe(struct bdinfo *bd);
122static void bd_io_workaround(bdinfo_t *);
123static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int);
124static bool bd_int13probe(bdinfo_t *);
103
104static int bd_init(void);
125
126static int bd_init(void);
127static int cd_init(void);
128static int fd_init(void);
105static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
106 char *buf, size_t *rsize);
107static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
108 char *buf, size_t *rsize);
109static int bd_open(struct open_file *f, ...);
110static int bd_close(struct open_file *f);
111static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
112static int bd_print(int verbose);
129static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
130 char *buf, size_t *rsize);
131static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size,
132 char *buf, size_t *rsize);
133static int bd_open(struct open_file *f, ...);
134static int bd_close(struct open_file *f);
135static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
136static int bd_print(int verbose);
137static int cd_print(int verbose);
138static int fd_print(int verbose);
113
139
114struct devsw biosdisk = {
115 "disk",
116 DEVT_DISK,
117 bd_init,
118 bd_strategy,
119 bd_open,
120 bd_close,
121 bd_ioctl,
122 bd_print,
123 NULL
140struct devsw biosfd = {
141 .dv_name = "fd",
142 .dv_type = DEVT_FD,
143 .dv_init = fd_init,
144 .dv_strategy = bd_strategy,
145 .dv_open = bd_open,
146 .dv_close = bd_close,
147 .dv_ioctl = bd_ioctl,
148 .dv_print = fd_print,
149 .dv_cleanup = NULL
124};
125
150};
151
152struct devsw bioscd = {
153 .dv_name = "cd",
154 .dv_type = DEVT_CD,
155 .dv_init = cd_init,
156 .dv_strategy = bd_strategy,
157 .dv_open = bd_open,
158 .dv_close = bd_close,
159 .dv_ioctl = bd_ioctl,
160 .dv_print = cd_print,
161 .dv_cleanup = NULL
162};
163
164struct devsw bioshd = {
165 .dv_name = "disk",
166 .dv_type = DEVT_DISK,
167 .dv_init = bd_init,
168 .dv_strategy = bd_strategy,
169 .dv_open = bd_open,
170 .dv_close = bd_close,
171 .dv_ioctl = bd_ioctl,
172 .dv_print = bd_print,
173 .dv_cleanup = NULL
174};
175
176static bdinfo_list_t *
177bd_get_bdinfo_list(struct devsw *dev)
178{
179 if (dev->dv_type == DEVT_DISK)
180 return (&hdinfo);
181 if (dev->dv_type == DEVT_CD)
182 return (&cdinfo);
183 if (dev->dv_type == DEVT_FD)
184 return (&fdinfo);
185 return (NULL);
186}
187
188/* XXX this gets called way way too often, investigate */
189static bdinfo_t *
190bd_get_bdinfo(struct devdesc *dev)
191{
192 bdinfo_list_t *bdi;
193 bdinfo_t *bd = NULL;
194 int unit;
195
196 bdi = bd_get_bdinfo_list(dev->d_dev);
197 if (bdi == NULL)
198 return (bd);
199
200 unit = 0;
201 STAILQ_FOREACH(bd, bdi, bd_link) {
202 if (unit == dev->d_unit)
203 return (bd);
204 unit++;
205 }
206 return (bd);
207}
208
126/*
127 * Translate between BIOS device numbers and our private unit numbers.
128 */
129int
130bd_bios2unit(int biosdev)
131{
209/*
210 * Translate between BIOS device numbers and our private unit numbers.
211 */
212int
213bd_bios2unit(int biosdev)
214{
132 int i;
215 bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL };
216 bdinfo_t *bd;
217 int i, unit;
133
134 DEBUG("looking for bios device 0x%x", biosdev);
218
219 DEBUG("looking for bios device 0x%x", biosdev);
135 for (i = 0; i < nbdinfo; i++) {
136 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
137 if (bdinfo[i].bd_unit == biosdev)
138 return (i);
220 for (i = 0; bdi[i] != NULL; i++) {
221 unit = 0;
222 STAILQ_FOREACH(bd, bdi[i], bd_link) {
223 if (bd->bd_unit == biosdev) {
224 DEBUG("bd unit %d is BIOS device 0x%x", unit,
225 bd->bd_unit);
226 return (unit);
227 }
228 unit++;
229 }
139 }
140 return (-1);
141}
142
143int
230 }
231 return (-1);
232}
233
234int
144bd_unit2bios(int unit)
235bd_unit2bios(struct i386_devdesc *dev)
145{
236{
237 bdinfo_list_t *bdi;
238 bdinfo_t *bd;
239 int unit;
146
240
147 if ((unit >= 0) && (unit < nbdinfo))
148 return (bdinfo[unit].bd_unit);
241 bdi = bd_get_bdinfo_list(dev->dd.d_dev);
242 if (bdi == NULL)
243 return (-1);
244
245 unit = 0;
246 STAILQ_FOREACH(bd, bdi, bd_link) {
247 if (unit == dev->dd.d_unit)
248 return (bd->bd_unit);
249 unit++;
250 }
149 return (-1);
150}
151
152/*
153 * Quiz the BIOS for disk devices, save a little info about them.
154 */
155static int
251 return (-1);
252}
253
254/*
255 * Quiz the BIOS for disk devices, save a little info about them.
256 */
257static int
156bd_init(void)
258fd_init(void)
157{
259{
158 int base, unit, nfd = 0;
260 int unit;
261 bdinfo_t *bd;
159
262
160 /* sequence 0, 0x80 */
161 for (base = 0; base <= 0x80; base += 0x80) {
162 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
163#ifndef VIRTUALBOX
164 /*
165 * Check the BIOS equipment list for number
166 * of fixed disks.
167 */
168 if (base == 0x80 &&
169 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
170 break;
171#endif
172 bdinfo[nbdinfo].bd_open = 0;
173 bdinfo[nbdinfo].bd_bcache = NULL;
174 bdinfo[nbdinfo].bd_unit = unit;
175 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
176 if (!bd_int13probe(&bdinfo[nbdinfo]))
177 break;
263 for (unit = 0; unit < MAXBDDEV; unit++) {
264 if ((bd = calloc(1, sizeof(*bd))) == NULL)
265 break;
266 bd->bd_flags = BD_FLOPPY;
267 bd->bd_unit = unit;
268 if (!bd_int13probe(bd)) {
269 free(bd);
270 break;
271 }
272 if (bd->bd_sectors == 0)
273 bd->bd_flags |= BD_NO_MEDIA;
178
274
179 /* XXX we need "disk aliases" to make this simpler */
180 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
181 ('A' + unit): ('C' + unit - 0x80), nbdinfo);
182 nbdinfo++;
183 if (base == 0x80)
184 nfd++;
275 printf("BIOS drive %c: is %s%d\n", ('A' + unit),
276 biosfd.dv_name, unit);
277
278 STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link);
279 }
280
281 bcache_add_dev(unit);
282 return (0);
283}
284
285static int
286bd_init(void)
287{
288 int base, unit;
289 bdinfo_t *bd;
290
291 base = 0x80;
292 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) {
293 /*
294 * Check the BIOS equipment list for number of fixed disks.
295 */
296 if ((bd = calloc(1, sizeof(*bd))) == NULL)
297 break;
298 bd->bd_unit = base + unit;
299 if (!bd_int13probe(bd)) {
300 free(bd);
301 break;
185 }
302 }
303
304 printf("BIOS drive %c: is %s%d\n", ('C' + unit),
305 bioshd.dv_name, unit);
306
307 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link);
186 }
308 }
187 bcache_add_dev(nbdinfo);
309 bcache_add_dev(unit);
188 return (0);
189}
190
191/*
310 return (0);
311}
312
313/*
314 * We can't quiz, we have to be told what device to use, so this function
315 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS
316 * device number to add.
317 */
318static int
319cd_init(void)
320{
321
322 return (0);
323}
324
325int
326bc_add(int biosdev)
327{
328 bdinfo_t *bd;
329 struct specification_packet bc_sp;
330 int nbcinfo = 0;
331
332 if (!STAILQ_EMPTY(&cdinfo))
333 return (-1);
334
335 v86.ctl = V86_FLAGS;
336 v86.addr = 0x13;
337 v86.eax = 0x4b01;
338 v86.edx = biosdev;
339 v86.ds = VTOPSEG(&bc_sp);
340 v86.esi = VTOPOFF(&bc_sp);
341 v86int();
342 if ((v86.eax & 0xff00) != 0)
343 return (-1);
344
345 if ((bd = calloc(1, sizeof(*bd))) == NULL)
346 return (-1);
347
348 bd->bd_flags = BD_CDROM;
349 bd->bd_unit = biosdev;
350
351 /*
352 * Ignore result from bd_int13probe(), we will use local
353 * workaround below.
354 */
355 (void)bd_int13probe(bd);
356
357 if (bd->bd_cyl == 0) {
358 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) +
359 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1;
360 }
361 if (bd->bd_hds == 0)
362 bd->bd_hds = bc_sp.sp_head + 1;
363 if (bd->bd_sec == 0)
364 bd->bd_sec = bc_sp.sp_cylsec & 0x3f;
365 if (bd->bd_sectors == 0)
366 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
367
368 /* Still no size? use 7.961GB */
369 if (bd->bd_sectors == 0)
370 bd->bd_sectors = 4173824;
371
372 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link);
373 printf("BIOS CD is cd%d\n", nbcinfo);
374 nbcinfo++;
375 bcache_add_dev(nbcinfo); /* register cd device in bcache */
376 return(0);
377}
378
379/*
192 * Return EDD version or 0 if EDD is not supported on this drive.
193 */
194static int
195bd_check_extensions(int unit)
196{
197 /* Determine if we can use EDD with this device. */
198 v86.ctl = V86_FLAGS;
199 v86.addr = 0x13;

--- 101 unchanged lines hidden (view full) ---

301 bd->bd_sectors = total;
302
303 return (0);
304}
305
306/*
307 * Try to detect a device supported by the legacy int13 BIOS
308 */
380 * Return EDD version or 0 if EDD is not supported on this drive.
381 */
382static int
383bd_check_extensions(int unit)
384{
385 /* Determine if we can use EDD with this device. */
386 v86.ctl = V86_FLAGS;
387 v86.addr = 0x13;

--- 101 unchanged lines hidden (view full) ---

489 bd->bd_sectors = total;
490
491 return (0);
492}
493
494/*
495 * Try to detect a device supported by the legacy int13 BIOS
496 */
309static int
310bd_int13probe(struct bdinfo *bd)
497static bool
498bd_int13probe(bdinfo_t *bd)
311{
499{
312 int edd;
313 int ret;
500 int edd, ret;
314
315 bd->bd_flags &= ~BD_NO_MEDIA;
316
317 edd = bd_check_extensions(bd->bd_unit);
318 if (edd == 0)
319 bd->bd_flags |= BD_MODEINT13;
320 else if (edd < 0x30)
321 bd->bd_flags |= BD_MODEEDD1;

--- 13 unchanged lines hidden (view full) ---

335
336 /* Get disk type */
337 v86.ctl = V86_FLAGS;
338 v86.addr = 0x13;
339 v86.eax = 0x1500;
340 v86.edx = bd->bd_unit;
341 v86int();
342 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
501
502 bd->bd_flags &= ~BD_NO_MEDIA;
503
504 edd = bd_check_extensions(bd->bd_unit);
505 if (edd == 0)
506 bd->bd_flags |= BD_MODEINT13;
507 else if (edd < 0x30)
508 bd->bd_flags |= BD_MODEEDD1;

--- 13 unchanged lines hidden (view full) ---

522
523 /* Get disk type */
524 v86.ctl = V86_FLAGS;
525 v86.addr = 0x13;
526 v86.eax = 0x1500;
527 v86.edx = bd->bd_unit;
528 v86int();
529 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0)
343 return (0);
530 return (false);
344 }
345
346 ret = 1;
347 if (edd != 0)
348 ret = bd_get_diskinfo_ext(bd);
349 if (ret != 0 || bd->bd_sectors == 0)
350 ret = bd_get_diskinfo_std(bd);
351
352 if (ret != 0 && bd->bd_unit < 0x80) {
353 /* Set defaults for 1.44 floppy */
354 bd->bd_cyl = 80;
355 bd->bd_hds = 2;
356 bd->bd_sec = 18;
531 }
532
533 ret = 1;
534 if (edd != 0)
535 ret = bd_get_diskinfo_ext(bd);
536 if (ret != 0 || bd->bd_sectors == 0)
537 ret = bd_get_diskinfo_std(bd);
538
539 if (ret != 0 && bd->bd_unit < 0x80) {
540 /* Set defaults for 1.44 floppy */
541 bd->bd_cyl = 80;
542 bd->bd_hds = 2;
543 bd->bd_sec = 18;
357 bd->bd_type = 4;
358 bd->bd_sectors = 2880;
359 /* Since we are there, there most likely is no media */
360 bd->bd_flags |= BD_NO_MEDIA;
361 ret = 0;
362 }
363
364 if (ret != 0) {
544 bd->bd_sectors = 2880;
545 /* Since we are there, there most likely is no media */
546 bd->bd_flags |= BD_NO_MEDIA;
547 ret = 0;
548 }
549
550 if (ret != 0) {
551 /* CD is special case, bc_add() has its own fallback. */
552 if ((bd->bd_flags & BD_CDROM) != 0)
553 return (true);
554
365 if (bd->bd_sectors != 0 && edd != 0) {
366 bd->bd_sec = 63;
367 bd->bd_hds = 255;
368 bd->bd_cyl =
369 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
370 bd->bd_sec * bd->bd_hds;
371 } else {
555 if (bd->bd_sectors != 0 && edd != 0) {
556 bd->bd_sec = 63;
557 bd->bd_hds = 255;
558 bd->bd_cyl =
559 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) /
560 bd->bd_sec * bd->bd_hds;
561 } else {
562 const char *dv_name;
563
564 if ((bd->bd_flags & BD_FLOPPY) != 0)
565 dv_name = biosfd.dv_name;
566 else if ((bd->bd_flags & BD_CDROM) != 0)
567 dv_name = bioscd.dv_name;
568 else
569 dv_name = bioshd.dv_name;
570
372 printf("Can not get information about %s unit %#x\n",
571 printf("Can not get information about %s unit %#x\n",
373 biosdisk.dv_name, bd->bd_unit);
374 return (0);
572 dv_name, bd->bd_unit);
573 return (false);
375 }
376 }
377
378 if (bd->bd_sec == 0)
379 bd->bd_sec = 63;
380 if (bd->bd_hds == 0)
381 bd->bd_hds = 255;
382
383 if (bd->bd_sectors == 0)
384 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
385
574 }
575 }
576
577 if (bd->bd_sec == 0)
578 bd->bd_sec = 63;
579 if (bd->bd_hds == 0)
580 bd->bd_hds = 255;
581
582 if (bd->bd_sectors == 0)
583 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec;
584
386 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
585 DEBUG("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl,
387 bd->bd_hds, bd->bd_sec);
388
586 bd->bd_hds, bd->bd_sec);
587
389 return (1);
588 return (true);
390}
391
589}
590
591static int
592bd_count(bdinfo_list_t *bdi)
593{
594 bdinfo_t *bd;
595 int i;
596
597 i = 0;
598 STAILQ_FOREACH(bd, bdi, bd_link)
599 i++;
600 return (i);
601}
602
392/*
393 * Print information about disks
394 */
395static int
603/*
604 * Print information about disks
605 */
606static int
396bd_print(int verbose)
607bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose)
397{
608{
398 static char line[80];
399 struct disk_devdesc dev;
609 char line[80];
610 struct disk_devdesc devd;
611 bdinfo_t *bd;
400 int i, ret = 0;
612 int i, ret = 0;
613 char drive;
401
614
402 if (nbdinfo == 0)
615 if (STAILQ_EMPTY(bdi))
403 return (0);
404
616 return (0);
617
405 printf("%s devices:", biosdisk.dv_name);
618 printf("%s devices:", dev->dv_name);
406 if ((ret = pager_output("\n")) != 0)
407 return (ret);
408
619 if ((ret = pager_output("\n")) != 0)
620 return (ret);
621
409 for (i = 0; i < nbdinfo; i++) {
622 i = -1;
623 STAILQ_FOREACH(bd, bdi, bd_link) {
624 i++;
625
626 switch (dev->dv_type) {
627 case DEVT_FD:
628 drive = 'A';
629 break;
630 case DEVT_CD:
631 drive = 'C' + bd_count(&hdinfo);
632 break;
633 default:
634 drive = 'C';
635 break;
636 }
637
410 snprintf(line, sizeof(line),
638 snprintf(line, sizeof(line),
411 " disk%d: BIOS drive %c (%s%ju X %u):\n", i,
412 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
413 ('C' + bdinfo[i].bd_unit - 0x80),
414 (bdinfo[i].bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
639 " %s%d: BIOS drive %c (%s%ju X %u):\n",
640 dev->dv_name, i, drive + i,
641 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ?
415 "no media, " : "",
642 "no media, " : "",
416 (uintmax_t)bdinfo[i].bd_sectors,
417 bdinfo[i].bd_sectorsize);
643 (uintmax_t)bd->bd_sectors,
644 bd->bd_sectorsize);
418 if ((ret = pager_output(line)) != 0)
419 break;
420
645 if ((ret = pager_output(line)) != 0)
646 break;
647
421 if ((bdinfo[i].bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
648 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
422 continue;
423
649 continue;
650
424 dev.dd.d_dev = &biosdisk;
425 dev.dd.d_unit = i;
426 dev.d_slice = -1;
427 dev.d_partition = -1;
428 if (disk_open(&dev,
429 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
430 bdinfo[i].bd_sectorsize) == 0) {
431 snprintf(line, sizeof(line), " disk%d", i);
432 ret = disk_print(&dev, line, verbose);
433 disk_close(&dev);
651 if (dev->dv_type != DEVT_DISK)
652 continue;
653
654 devd.dd.d_dev = dev;
655 devd.dd.d_unit = i;
656 devd.d_slice = -1;
657 devd.d_partition = -1;
658 if (disk_open(&devd,
659 bd->bd_sectorsize * bd->bd_sectors,
660 bd->bd_sectorsize) == 0) {
661 snprintf(line, sizeof(line), " %s%d",
662 dev->dv_name, i);
663 ret = disk_print(&devd, line, verbose);
664 disk_close(&devd);
434 if (ret != 0)
435 break;
436 }
437 }
438 return (ret);
439}
440
665 if (ret != 0)
666 break;
667 }
668 }
669 return (ret);
670}
671
672static int
673fd_print(int verbose)
674{
675 return (bd_print_common(&biosfd, &fdinfo, verbose));
676}
677
678static int
679bd_print(int verbose)
680{
681 return (bd_print_common(&bioshd, &hdinfo, verbose));
682}
683
684static int
685cd_print(int verbose)
686{
687 return (bd_print_common(&bioscd, &cdinfo, verbose));
688}
689
441/*
442 * Read disk size from partition.
443 * This is needed to work around buggy BIOS systems returning
444 * wrong (truncated) disk media size.
445 * During bd_probe() we tested if the multiplication of bd_sectors
446 * would overflow so it should be safe to perform here.
447 */
448static uint64_t
449bd_disk_get_sectors(struct disk_devdesc *dev)
450{
690/*
691 * Read disk size from partition.
692 * This is needed to work around buggy BIOS systems returning
693 * wrong (truncated) disk media size.
694 * During bd_probe() we tested if the multiplication of bd_sectors
695 * would overflow so it should be safe to perform here.
696 */
697static uint64_t
698bd_disk_get_sectors(struct disk_devdesc *dev)
699{
700 bdinfo_t *bd;
451 struct disk_devdesc disk;
452 uint64_t size;
453
701 struct disk_devdesc disk;
702 uint64_t size;
703
704 bd = bd_get_bdinfo(&dev->dd);
705 if (bd == NULL)
706 return (0);
707
454 disk.dd.d_dev = dev->dd.d_dev;
455 disk.dd.d_unit = dev->dd.d_unit;
456 disk.d_slice = -1;
457 disk.d_partition = -1;
458 disk.d_offset = 0;
459
708 disk.dd.d_dev = dev->dd.d_dev;
709 disk.dd.d_unit = dev->dd.d_unit;
710 disk.d_slice = -1;
711 disk.d_partition = -1;
712 disk.d_offset = 0;
713
460 size = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
461 if (disk_open(&disk, size, BD(dev).bd_sectorsize) == 0) {
714 size = bd->bd_sectors * bd->bd_sectorsize;
715 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) {
462 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
463 disk_close(&disk);
464 }
716 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size);
717 disk_close(&disk);
718 }
465 return (size / BD(dev).bd_sectorsize);
719 return (size / bd->bd_sectorsize);
466}
467
468/*
469 * Attempt to open the disk described by (dev) for use by (f).
470 *
471 * Note that the philosophy here is "give them exactly what
472 * they ask for". This is necessary because being too "smart"
473 * about what the user might want leads to complications.
474 * (eg. given no slice or partition value, with a disk that is
475 * sliced - are they after the first BSD slice, or the DOS
476 * slice before it?)
477 */
478static int
479bd_open(struct open_file *f, ...)
480{
720}
721
722/*
723 * Attempt to open the disk described by (dev) for use by (f).
724 *
725 * Note that the philosophy here is "give them exactly what
726 * they ask for". This is necessary because being too "smart"
727 * about what the user might want leads to complications.
728 * (eg. given no slice or partition value, with a disk that is
729 * sliced - are they after the first BSD slice, or the DOS
730 * slice before it?)
731 */
732static int
733bd_open(struct open_file *f, ...)
734{
735 bdinfo_t *bd;
481 struct disk_devdesc *dev;
482 va_list ap;
483 int rc;
484
485 va_start(ap, f);
486 dev = va_arg(ap, struct disk_devdesc *);
487 va_end(ap);
488
736 struct disk_devdesc *dev;
737 va_list ap;
738 int rc;
739
740 va_start(ap, f);
741 dev = va_arg(ap, struct disk_devdesc *);
742 va_end(ap);
743
489 if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo)
744 bd = bd_get_bdinfo(&dev->dd);
745 if (bd == NULL)
490 return (EIO);
491
746 return (EIO);
747
492 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
493 if (!bd_int13probe(&BD(dev)))
748 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) {
749 if (!bd_int13probe(bd))
494 return (EIO);
750 return (EIO);
495 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
751 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
496 return (EIO);
497 }
752 return (EIO);
753 }
498 if (BD(dev).bd_bcache == NULL)
499 BD(dev).bd_bcache = bcache_allocate();
754 if (bd->bd_bcache == NULL)
755 bd->bd_bcache = bcache_allocate();
500
756
501 if (BD(dev).bd_open == 0)
502 BD(dev).bd_sectors = bd_disk_get_sectors(dev);
503 BD(dev).bd_open++;
757 if (bd->bd_open == 0)
758 bd->bd_sectors = bd_disk_get_sectors(dev);
759 bd->bd_open++;
504
760
505 rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
506 BD(dev).bd_sectorsize);
507 if (rc != 0) {
508 BD(dev).bd_open--;
509 if (BD(dev).bd_open == 0) {
510 bcache_free(BD(dev).bd_bcache);
511 BD(dev).bd_bcache = NULL;
761 rc = 0;
762 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
763 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
764 bd->bd_sectorsize);
765 if (rc != 0) {
766 bd->bd_open--;
767 if (bd->bd_open == 0) {
768 bcache_free(bd->bd_bcache);
769 bd->bd_bcache = NULL;
770 }
512 }
513 }
514 return (rc);
515}
516
517static int
518bd_close(struct open_file *f)
519{
520 struct disk_devdesc *dev;
771 }
772 }
773 return (rc);
774}
775
776static int
777bd_close(struct open_file *f)
778{
779 struct disk_devdesc *dev;
780 bdinfo_t *bd;
781 int rc = 0;
521
522 dev = (struct disk_devdesc *)f->f_devdata;
782
783 dev = (struct disk_devdesc *)f->f_devdata;
523 BD(dev).bd_open--;
524 if (BD(dev).bd_open == 0) {
525 bcache_free(BD(dev).bd_bcache);
526 BD(dev).bd_bcache = NULL;
784 bd = bd_get_bdinfo(&dev->dd);
785 if (bd == NULL)
786 return (EIO);
787
788 bd->bd_open--;
789 if (bd->bd_open == 0) {
790 bcache_free(bd->bd_bcache);
791 bd->bd_bcache = NULL;
527 }
792 }
528 return (disk_close(dev));
793 if (dev->dd.d_dev->dv_type == DEVT_DISK)
794 rc = disk_close(dev);
795 return (rc);
529}
530
531static int
532bd_ioctl(struct open_file *f, u_long cmd, void *data)
533{
796}
797
798static int
799bd_ioctl(struct open_file *f, u_long cmd, void *data)
800{
801 bdinfo_t *bd;
534 struct disk_devdesc *dev;
535 int rc;
536
537 dev = (struct disk_devdesc *)f->f_devdata;
802 struct disk_devdesc *dev;
803 int rc;
804
805 dev = (struct disk_devdesc *)f->f_devdata;
806 bd = bd_get_bdinfo(&dev->dd);
807 if (bd == NULL)
808 return (EIO);
538
809
539 rc = disk_ioctl(dev, cmd, data);
540 if (rc != ENOTTY)
541 return (rc);
810 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
811 rc = disk_ioctl(dev, cmd, data);
812 if (rc != ENOTTY)
813 return (rc);
814 }
542
543 switch (cmd) {
544 case DIOCGSECTORSIZE:
815
816 switch (cmd) {
817 case DIOCGSECTORSIZE:
545 *(uint32_t *)data = BD(dev).bd_sectorsize;
818 *(uint32_t *)data = bd->bd_sectorsize;
546 break;
547 case DIOCGMEDIASIZE:
819 break;
820 case DIOCGMEDIASIZE:
548 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
821 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize;
549 break;
550 default:
551 return (ENOTTY);
552 }
553 return (0);
554}
555
556static int
557bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
558 char *buf, size_t *rsize)
559{
822 break;
823 default:
824 return (ENOTTY);
825 }
826 return (0);
827}
828
829static int
830bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
831 char *buf, size_t *rsize)
832{
833 bdinfo_t *bd;
560 struct bcache_devdata bcd;
561 struct disk_devdesc *dev;
834 struct bcache_devdata bcd;
835 struct disk_devdesc *dev;
836 daddr_t offset;
562
563 dev = (struct disk_devdesc *)devdata;
837
838 dev = (struct disk_devdesc *)devdata;
839 bd = bd_get_bdinfo(&dev->dd);
840 if (bd == NULL)
841 return (EINVAL);
842
564 bcd.dv_strategy = bd_realstrategy;
565 bcd.dv_devdata = devdata;
843 bcd.dv_strategy = bd_realstrategy;
844 bcd.dv_devdata = devdata;
566 bcd.dv_cache = BD(dev).bd_bcache;
567 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size,
845 bcd.dv_cache = bd->bd_bcache;
846
847 offset = 0;
848 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
849
850 offset = dev->d_offset * bd->bd_sectorsize;
851 offset /= BIOSDISK_SECSIZE;
852 }
853 return (bcache_strategy(&bcd, rw, dblk + offset, size,
568 buf, rsize));
569}
570
571static int
572bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
573 char *buf, size_t *rsize)
574{
575 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
854 buf, rsize));
855}
856
857static int
858bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
859 char *buf, size_t *rsize)
860{
861 struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
576 uint64_t disk_blocks, offset;
862 bdinfo_t *bd;
863 uint64_t disk_blocks, offset, d_offset;
577 size_t blks, blkoff, bsize, rest;
578 caddr_t bbuf;
579 int rc;
580
864 size_t blks, blkoff, bsize, rest;
865 caddr_t bbuf;
866 int rc;
867
581 if ((BD(dev).bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
868 bd = bd_get_bdinfo(&dev->dd);
869 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA)
582 return (EIO);
583
584 /*
585 * First make sure the IO size is a multiple of 512 bytes. While we do
586 * process partial reads below, the strategy mechanism is built
587 * assuming IO is a multiple of 512B blocks. If the request is not
588 * a multiple of 512B blocks, it has to be some sort of bug.
589 */
590 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
591 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
592 size, BIOSDISK_SECSIZE);
593 return (EIO);
594 }
595
596 DEBUG("open_disk %p", dev);
597
598 offset = dblk * BIOSDISK_SECSIZE;
870 return (EIO);
871
872 /*
873 * First make sure the IO size is a multiple of 512 bytes. While we do
874 * process partial reads below, the strategy mechanism is built
875 * assuming IO is a multiple of 512B blocks. If the request is not
876 * a multiple of 512B blocks, it has to be some sort of bug.
877 */
878 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) {
879 printf("bd_strategy: %d bytes I/O not multiple of %d\n",
880 size, BIOSDISK_SECSIZE);
881 return (EIO);
882 }
883
884 DEBUG("open_disk %p", dev);
885
886 offset = dblk * BIOSDISK_SECSIZE;
599 dblk = offset / BD(dev).bd_sectorsize;
600 blkoff = offset % BD(dev).bd_sectorsize;
887 dblk = offset / bd->bd_sectorsize;
888 blkoff = offset % bd->bd_sectorsize;
601
602 /*
603 * Check the value of the size argument. We do have quite small
604 * heap (64MB), but we do not know good upper limit, so we check against
605 * INT_MAX here. This will also protect us against possible overflows
606 * while translating block count to bytes.
607 */
608 if (size > INT_MAX) {
609 DEBUG("too large I/O: %zu bytes", size);
610 return (EIO);
611 }
612
889
890 /*
891 * Check the value of the size argument. We do have quite small
892 * heap (64MB), but we do not know good upper limit, so we check against
893 * INT_MAX here. This will also protect us against possible overflows
894 * while translating block count to bytes.
895 */
896 if (size > INT_MAX) {
897 DEBUG("too large I/O: %zu bytes", size);
898 return (EIO);
899 }
900
613 blks = size / BD(dev).bd_sectorsize;
614 if (blks == 0 || (size % BD(dev).bd_sectorsize) != 0)
901 blks = size / bd->bd_sectorsize;
902 if (blks == 0 || (size % bd->bd_sectorsize) != 0)
615 blks++;
616
617 if (dblk > dblk + blks)
618 return (EIO);
619
620 if (rsize)
621 *rsize = 0;
622
623 /*
624 * Get disk blocks, this value is either for whole disk or for
625 * partition.
626 */
903 blks++;
904
905 if (dblk > dblk + blks)
906 return (EIO);
907
908 if (rsize)
909 *rsize = 0;
910
911 /*
912 * Get disk blocks, this value is either for whole disk or for
913 * partition.
914 */
627 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
628 /* DIOCGMEDIASIZE does return bytes. */
629 disk_blocks /= BD(dev).bd_sectorsize;
630 } else {
631 /* We should not get here. Just try to survive. */
632 disk_blocks = BD(dev).bd_sectors - dev->d_offset;
915 d_offset = 0;
916 disk_blocks = 0;
917 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
918 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) {
919 /* DIOCGMEDIASIZE does return bytes. */
920 disk_blocks /= bd->bd_sectorsize;
921 }
922 d_offset = dev->d_offset;
633 }
923 }
924 if (disk_blocks == 0)
925 disk_blocks = bd->bd_sectors - d_offset;
634
635 /* Validate source block address. */
926
927 /* Validate source block address. */
636 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks)
928 if (dblk < d_offset || dblk >= d_offset + disk_blocks)
637 return (EIO);
638
639 /*
640 * Truncate if we are crossing disk or partition end.
641 */
929 return (EIO);
930
931 /*
932 * Truncate if we are crossing disk or partition end.
933 */
642 if (dblk + blks >= dev->d_offset + disk_blocks) {
643 blks = dev->d_offset + disk_blocks - dblk;
644 size = blks * BD(dev).bd_sectorsize;
934 if (dblk + blks >= d_offset + disk_blocks) {
935 blks = d_offset + disk_blocks - dblk;
936 size = blks * bd->bd_sectorsize;
645 DEBUG("short I/O %d", blks);
646 }
647
937 DEBUG("short I/O %d", blks);
938 }
939
648 if (V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize == 0)
940 if (V86_IO_BUFFER_SIZE / bd->bd_sectorsize == 0)
649 panic("BUG: Real mode buffer is too small");
650
651 bbuf = PTOV(V86_IO_BUFFER);
652 rest = size;
653
654 while (blks > 0) {
941 panic("BUG: Real mode buffer is too small");
942
943 bbuf = PTOV(V86_IO_BUFFER);
944 rest = size;
945
946 while (blks > 0) {
655 int x = min(blks, V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize);
947 int x = min(blks, V86_IO_BUFFER_SIZE / bd->bd_sectorsize);
656
657 switch (rw & F_MASK) {
658 case F_READ:
659 DEBUG("read %d from %lld to %p", x, dblk, buf);
948
949 switch (rw & F_MASK) {
950 case F_READ:
951 DEBUG("read %d from %lld to %p", x, dblk, buf);
660 bsize = BD(dev).bd_sectorsize * x - blkoff;
952 bsize = bd->bd_sectorsize * x - blkoff;
661 if (rest < bsize)
662 bsize = rest;
663
953 if (rest < bsize)
954 bsize = rest;
955
664 if ((rc = bd_io(dev, dblk, x, bbuf, BD_RD)) != 0)
956 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0)
665 return (EIO);
666
667 bcopy(bbuf + blkoff, buf, bsize);
668 break;
669 case F_WRITE :
670 DEBUG("write %d from %lld to %p", x, dblk, buf);
671 if (blkoff != 0) {
672 /*
673 * We got offset to sector, read 1 sector to
674 * bbuf.
675 */
676 x = 1;
957 return (EIO);
958
959 bcopy(bbuf + blkoff, buf, bsize);
960 break;
961 case F_WRITE :
962 DEBUG("write %d from %lld to %p", x, dblk, buf);
963 if (blkoff != 0) {
964 /*
965 * We got offset to sector, read 1 sector to
966 * bbuf.
967 */
968 x = 1;
677 bsize = BD(dev).bd_sectorsize - blkoff;
969 bsize = bd->bd_sectorsize - blkoff;
678 bsize = min(bsize, rest);
970 bsize = min(bsize, rest);
679 rc = bd_io(dev, dblk, x, bbuf, BD_RD);
680 } else if (rest < BD(dev).bd_sectorsize) {
971 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
972 } else if (rest < bd->bd_sectorsize) {
681 /*
682 * The remaining block is not full
683 * sector. Read 1 sector to bbuf.
684 */
685 x = 1;
686 bsize = rest;
973 /*
974 * The remaining block is not full
975 * sector. Read 1 sector to bbuf.
976 */
977 x = 1;
978 bsize = rest;
687 rc = bd_io(dev, dblk, x, bbuf, BD_RD);
979 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD);
688 } else {
689 /* We can write full sector(s). */
980 } else {
981 /* We can write full sector(s). */
690 bsize = BD(dev).bd_sectorsize * x;
982 bsize = bd->bd_sectorsize * x;
691 }
692 /*
693 * Put your Data In, Put your Data out,
694 * Put your Data In, and shake it all about
695 */
696 bcopy(buf, bbuf + blkoff, bsize);
983 }
984 /*
985 * Put your Data In, Put your Data out,
986 * Put your Data In, and shake it all about
987 */
988 bcopy(buf, bbuf + blkoff, bsize);
697 if ((rc = bd_io(dev, dblk, x, bbuf, BD_WR)) != 0)
989 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0)
698 return (EIO);
699
700 break;
701 default:
702 /* DO NOTHING */
703 return (EROFS);
704 }
705

--- 5 unchanged lines hidden (view full) ---

711 }
712
713 if (rsize != NULL)
714 *rsize = size;
715 return (0);
716}
717
718static int
990 return (EIO);
991
992 break;
993 default:
994 /* DO NOTHING */
995 return (EROFS);
996 }
997

--- 5 unchanged lines hidden (view full) ---

1003 }
1004
1005 if (rsize != NULL)
1006 *rsize = size;
1007 return (0);
1008}
1009
1010static int
719bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
1011bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
720 int dowrite)
721{
722 static struct edd_packet packet;
723
724 packet.len = sizeof(struct edd_packet);
725 packet.count = blks;
726 packet.off = VTOPOFF(dest);
727 packet.seg = VTOPSEG(dest);
728 packet.lba = dblk;
729 v86.ctl = V86_FLAGS;
730 v86.addr = 0x13;
731 /* Should we Write with verify ?? 0x4302 ? */
732 if (dowrite == BD_WR)
733 v86.eax = 0x4300;
734 else
735 v86.eax = 0x4200;
1012 int dowrite)
1013{
1014 static struct edd_packet packet;
1015
1016 packet.len = sizeof(struct edd_packet);
1017 packet.count = blks;
1018 packet.off = VTOPOFF(dest);
1019 packet.seg = VTOPSEG(dest);
1020 packet.lba = dblk;
1021 v86.ctl = V86_FLAGS;
1022 v86.addr = 0x13;
1023 /* Should we Write with verify ?? 0x4302 ? */
1024 if (dowrite == BD_WR)
1025 v86.eax = 0x4300;
1026 else
1027 v86.eax = 0x4200;
736 v86.edx = BD(dev).bd_unit;
1028 v86.edx = bd->bd_unit;
737 v86.ds = VTOPSEG(&packet);
738 v86.esi = VTOPOFF(&packet);
739 v86int();
740 if (V86_CY(v86.efl))
741 return (v86.eax >> 8);
742 return (0);
743}
744
745static int
1029 v86.ds = VTOPSEG(&packet);
1030 v86.esi = VTOPOFF(&packet);
1031 v86int();
1032 if (V86_CY(v86.efl))
1033 return (v86.eax >> 8);
1034 return (0);
1035}
1036
1037static int
746bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
1038bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest,
747 int dowrite)
748{
749 uint32_t x, bpc, cyl, hd, sec;
750
1039 int dowrite)
1040{
1041 uint32_t x, bpc, cyl, hd, sec;
1042
751 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
1043 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */
752 x = dblk;
753 cyl = x / bpc; /* block # / blocks per cylinder */
754 x %= bpc; /* block offset into cylinder */
1044 x = dblk;
1045 cyl = x / bpc; /* block # / blocks per cylinder */
1046 x %= bpc; /* block offset into cylinder */
755 hd = x / BD(dev).bd_sec; /* offset / blocks per track */
756 sec = x % BD(dev).bd_sec; /* offset into track */
1047 hd = x / bd->bd_sec; /* offset / blocks per track */
1048 sec = x % bd->bd_sec; /* offset into track */
757
758 /* correct sector number for 1-based BIOS numbering */
759 sec++;
760
761 if (cyl > 1023) {
762 /* CHS doesn't support cylinders > 1023. */
763 return (1);
764 }
765
766 v86.ctl = V86_FLAGS;
767 v86.addr = 0x13;
768 if (dowrite == BD_WR)
769 v86.eax = 0x300 | blks;
770 else
771 v86.eax = 0x200 | blks;
772 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1049
1050 /* correct sector number for 1-based BIOS numbering */
1051 sec++;
1052
1053 if (cyl > 1023) {
1054 /* CHS doesn't support cylinders > 1023. */
1055 return (1);
1056 }
1057
1058 v86.ctl = V86_FLAGS;
1059 v86.addr = 0x13;
1060 if (dowrite == BD_WR)
1061 v86.eax = 0x300 | blks;
1062 else
1063 v86.eax = 0x200 | blks;
1064 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
773 v86.edx = (hd << 8) | BD(dev).bd_unit;
1065 v86.edx = (hd << 8) | bd->bd_unit;
774 v86.es = VTOPSEG(dest);
775 v86.ebx = VTOPOFF(dest);
776 v86int();
777 if (V86_CY(v86.efl))
778 return (v86.eax >> 8);
779 return (0);
780}
781
782static void
1066 v86.es = VTOPSEG(dest);
1067 v86.ebx = VTOPOFF(dest);
1068 v86int();
1069 if (V86_CY(v86.efl))
1070 return (v86.eax >> 8);
1071 return (0);
1072}
1073
1074static void
783bd_io_workaround(struct disk_devdesc *dev)
1075bd_io_workaround(bdinfo_t *bd)
784{
785 uint8_t buf[8 * 1024];
786
1076{
1077 uint8_t buf[8 * 1024];
1078
787 bd_edd_io(dev, 0xffffffff, 1, (caddr_t)buf, BD_RD);
1079 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD);
788}
789
790static int
1080}
1081
1082static int
791bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
792 int dowrite)
1083bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks,
1084 caddr_t dest, int dowrite)
793{
794 int result, retry;
795
796 /* Just in case some idiot actually tries to read/write -1 blocks... */
797 if (blks < 0)
798 return (-1);
799
800 /*
801 * Workaround for a problem with some HP ProLiant BIOS failing to work
802 * out the boot disk after installation. hrs and kuriyama discovered
803 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
804 * discovered that an int13h call seems to cause a buffer overrun in
805 * the bios. The problem is alleviated by doing an extra read before
806 * the buggy read. It is not immediately known whether other models
807 * are similarly affected.
808 * Loop retrying the operation a couple of times. The BIOS
809 * may also retry.
810 */
811 if (dowrite == BD_RD && dblk >= 0x100000000)
1085{
1086 int result, retry;
1087
1088 /* Just in case some idiot actually tries to read/write -1 blocks... */
1089 if (blks < 0)
1090 return (-1);
1091
1092 /*
1093 * Workaround for a problem with some HP ProLiant BIOS failing to work
1094 * out the boot disk after installation. hrs and kuriyama discovered
1095 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and
1096 * discovered that an int13h call seems to cause a buffer overrun in
1097 * the bios. The problem is alleviated by doing an extra read before
1098 * the buggy read. It is not immediately known whether other models
1099 * are similarly affected.
1100 * Loop retrying the operation a couple of times. The BIOS
1101 * may also retry.
1102 */
1103 if (dowrite == BD_RD && dblk >= 0x100000000)
812 bd_io_workaround(dev);
1104 bd_io_workaround(bd);
813 for (retry = 0; retry < 3; retry++) {
1105 for (retry = 0; retry < 3; retry++) {
814 if (BD(dev).bd_flags & BD_MODEEDD)
815 result = bd_edd_io(dev, dblk, blks, dest, dowrite);
1106 if (bd->bd_flags & BD_MODEEDD)
1107 result = bd_edd_io(bd, dblk, blks, dest, dowrite);
816 else
1108 else
817 result = bd_chs_io(dev, dblk, blks, dest, dowrite);
1109 result = bd_chs_io(bd, dblk, blks, dest, dowrite);
818
819 if (result == 0) {
1110
1111 if (result == 0) {
820 if (BD(dev).bd_flags & BD_NO_MEDIA)
821 BD(dev).bd_flags &= ~BD_NO_MEDIA;
1112 if (bd->bd_flags & BD_NO_MEDIA)
1113 bd->bd_flags &= ~BD_NO_MEDIA;
822 break;
823 }
824
1114 break;
1115 }
1116
825 bd_reset_disk(BD(dev).bd_unit);
1117 bd_reset_disk(bd->bd_unit);
826
827 /*
828 * Error codes:
829 * 20h controller failure
830 * 31h no media in drive (IBM/MS INT 13 extensions)
831 * 80h no media in drive, VMWare (Fusion)
832 * There is no reason to repeat the IO with errors above.
833 */
834 if (result == 0x20 || result == 0x31 || result == 0x80) {
1118
1119 /*
1120 * Error codes:
1121 * 20h controller failure
1122 * 31h no media in drive (IBM/MS INT 13 extensions)
1123 * 80h no media in drive, VMWare (Fusion)
1124 * There is no reason to repeat the IO with errors above.
1125 */
1126 if (result == 0x20 || result == 0x31 || result == 0x80) {
835 BD(dev).bd_flags |= BD_NO_MEDIA;
1127 bd->bd_flags |= BD_NO_MEDIA;
836 break;
837 }
838 }
839
1128 break;
1129 }
1130 }
1131
840 if (result != 0 && (BD(dev).bd_flags & BD_NO_MEDIA) == 0) {
1132 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) {
841 if (dowrite == BD_WR) {
842 printf("%s%d: Write %d sector(s) from %p (0x%x) "
843 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
844 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
845 result);
846 } else {
847 printf("%s%d: Read %d sector(s) from %lld to %p "
848 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,

--- 39 unchanged lines hidden (view full) ---

888 *
889 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
890 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
891 */
892int
893bd_getdev(struct i386_devdesc *d)
894{
895 struct disk_devdesc *dev;
1133 if (dowrite == BD_WR) {
1134 printf("%s%d: Write %d sector(s) from %p (0x%x) "
1135 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name,
1136 dev->dd.d_unit, blks, dest, VTOP(dest), dblk,
1137 result);
1138 } else {
1139 printf("%s%d: Read %d sector(s) from %lld to %p "
1140 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name,

--- 39 unchanged lines hidden (view full) ---

1180 *
1181 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1182 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1183 */
1184int
1185bd_getdev(struct i386_devdesc *d)
1186{
1187 struct disk_devdesc *dev;
1188 bdinfo_t *bd;
896 int biosdev;
897 int major;
898 int rootdev;
899 char *nip, *cp;
1189 int biosdev;
1190 int major;
1191 int rootdev;
1192 char *nip, *cp;
900 int i, unit;
1193 int i, unit, slice, partition;
901
1194
1195 /* XXX: Assume partition 'a'. */
1196 slice = 0;
1197 partition = 0;
1198
902 dev = (struct disk_devdesc *)d;
1199 dev = (struct disk_devdesc *)d;
903 biosdev = bd_unit2bios(dev->dd.d_unit);
1200 bd = bd_get_bdinfo(&dev->dd);
1201 if (bd == NULL)
1202 return (-1);
1203
1204 biosdev = bd_unit2bios(d);
904 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
905 if (biosdev == -1) /* not a BIOS device */
906 return (-1);
1205 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev);
1206 if (biosdev == -1) /* not a BIOS device */
1207 return (-1);
907 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
908 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */
909 return (-1);
910 else
911 disk_close(dev);
912
1208
1209 if (dev->dd.d_dev->dv_type == DEVT_DISK) {
1210 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize,
1211 bd->bd_sectorsize) != 0) /* oops, not a viable device */
1212 return (-1);
1213 else
1214 disk_close(dev);
1215 slice = dev->d_slice + 1;
1216 partition = dev->d_partition;
1217 }
1218
913 if (biosdev < 0x80) {
914 /* floppy (or emulated floppy) or ATAPI device */
1219 if (biosdev < 0x80) {
1220 /* floppy (or emulated floppy) or ATAPI device */
915 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) {
1221 if (bd->bd_type == DT_ATAPI) {
916 /* is an ATAPI disk */
917 major = WFDMAJOR;
918 } else {
919 /* is a floppy disk */
920 major = FDMAJOR;
921 }
922 } else {
923 /* assume an IDE disk */
924 major = WDMAJOR;
925 }
926 /* default root disk unit number */
927 unit = biosdev & 0x7f;
928
1222 /* is an ATAPI disk */
1223 major = WFDMAJOR;
1224 } else {
1225 /* is a floppy disk */
1226 major = FDMAJOR;
1227 }
1228 } else {
1229 /* assume an IDE disk */
1230 major = WDMAJOR;
1231 }
1232 /* default root disk unit number */
1233 unit = biosdev & 0x7f;
1234
1235 if (dev->dd.d_dev->dv_type == DEVT_CD) {
1236 /*
1237 * XXX: Need to examine device spec here to figure out if
1238 * SCSI or ATAPI. No idea on how to figure out device number.
1239 * All we can really pass to the kernel is what bus and device
1240 * on which bus we were booted from, which dev_t isn't well
1241 * suited to since those number don't match to unit numbers
1242 * very well. We may just need to engage in a hack where
1243 * we pass -C to the boot args if we are the boot device.
1244 */
1245 major = ACDMAJOR;
1246 unit = 0; /* XXX */
1247 }
1248
929 /* XXX a better kludge to set the root disk unit number */
930 if ((nip = getenv("root_disk_unit")) != NULL) {
931 i = strtol(nip, &cp, 0);
932 /* check for parse error */
933 if ((cp != nip) && (*cp == 0))
934 unit = i;
935 }
936
1249 /* XXX a better kludge to set the root disk unit number */
1250 if ((nip = getenv("root_disk_unit")) != NULL) {
1251 i = strtol(nip, &cp, 0);
1252 /* check for parse error */
1253 if ((cp != nip) && (*cp == 0))
1254 unit = i;
1255 }
1256
937 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
1257 rootdev = MAKEBOOTDEV(major, slice, unit, partition);
938 DEBUG("dev is 0x%x\n", rootdev);
939 return (rootdev);
940}
1258 DEBUG("dev is 0x%x\n", rootdev);
1259 return (rootdev);
1260}