VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/disk.c@ 47631

Last change on this file since 47631 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.1 KB
Line 
1/*
2 * Copyright (C) 2006-2012 Oracle Corporation
3 *
4 * This file is part of VirtualBox Open Source Edition (OSE), as
5 * available from http://www.virtualbox.org. This file is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU
7 * General Public License (GPL) as published by the Free Software
8 * Foundation, in version 2 as it comes in the "COPYING" file of the
9 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
11 * --------------------------------------------------------------------
12 *
13 * This code is based on:
14 *
15 * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
16 *
17 * Copyright (C) 2002 MandrakeSoft S.A.
18 *
19 * MandrakeSoft S.A.
20 * 43, rue d'Aboukir
21 * 75002 Paris - France
22 * http://www.linux-mandrake.com/
23 * http://www.mandrakesoft.com/
24 *
25 * This library is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU Lesser General Public
27 * License as published by the Free Software Foundation; either
28 * version 2 of the License, or (at your option) any later version.
29 *
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 * Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public
36 * License along with this library; if not, write to the Free Software
37 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 *
39 */
40
41
42#include <stdint.h>
43#include "biosint.h"
44#include "inlines.h"
45#include "ebda.h"
46#include "ata.h"
47
48
49#if DEBUG_INT13_HD
50# define BX_DEBUG_INT13_HD(...) BX_DEBUG(__VA_ARGS__)
51#else
52# define BX_DEBUG_INT13_HD(...)
53#endif
54
55/* Generic disk read/write routine signature. */
56typedef __fastcall (* dsk_rw_func)(bio_dsk_t __far *bios_dsk);
57
58/* Controller specific disk access routines. Declared as a union to reduce
59 * the need for conditionals when choosing between read/write functions.
60 * Note that we get away with using near pointers, which is nice.
61 */
62typedef union {
63 struct {
64 dsk_rw_func read;
65 dsk_rw_func write;
66 } s;
67 dsk_rw_func a[2];
68} dsk_acc_t;
69
70/* Pointers to HW specific disk access routines. */
71dsk_acc_t dskacc[DSKTYP_CNT] = {
72 [DSK_TYPE_ATA] = { ata_read_sectors, ata_write_sectors },
73#ifdef VBOX_WITH_AHCI
74 [DSK_TYPE_AHCI] = { ahci_read_sectors, ahci_write_sectors },
75#endif
76#ifdef VBOX_WITH_SCSI
77 [DSK_TYPE_SCSI] = { scsi_read_sectors, scsi_write_sectors },
78#endif
79};
80
81
82//@todo: put in a header
83#define AX r.gr.u.r16.ax
84#define BX r.gr.u.r16.bx
85#define CX r.gr.u.r16.cx
86#define DX r.gr.u.r16.dx
87#define SI r.gr.u.r16.si
88#define DI r.gr.u.r16.di
89#define BP r.gr.u.r16.bp
90#define ELDX r.gr.u.r16.sp
91#define DS r.ds
92#define ES r.es
93#define FLAGS r.ra.flags.u.r16.flags
94
95
96/*
97 * Build translated CHS geometry given a disk size in sectors. Based on
98 * Phoenix EDD 3.0. This is used as a fallback to generate sane logical
99 * geometry in case none was provided in CMOS.
100 */
101void set_geom_lba(chs_t __far *lgeo, uint32_t nsectors)
102{
103 uint32_t limit = 8257536; /* 1024 * 128 * 63 */
104 unsigned heads = 255;
105 int i;
106
107 /* Start with ~4GB limit, go down to 504MB. */
108 for (i = 0; i < 4; ++i) {
109 if (nsectors <= limit)
110 heads = (heads + 1) / 2;
111 limit /= 2;
112 }
113
114 lgeo->cylinders = nsectors / (heads * 63UL);
115 if (lgeo->cylinders > 1024)
116 lgeo->cylinders = 1024;
117 lgeo->heads = heads;
118 lgeo->spt = 63; /* Always 63 sectors per track, the maximum. */
119}
120
121
122void BIOSCALL int13_harddisk(disk_regs_t r)
123{
124 uint32_t lba;
125 uint16_t cylinder, head, sector;
126 uint16_t nlc, nlh, nlspt;
127 uint16_t count;
128 uint8_t device, status;
129 bio_dsk_t __far *bios_dsk;
130
131 BX_DEBUG_INT13_HD("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
132
133 bios_dsk = read_word(0x0040,0x000E) :> &EbdaData->bdisk;
134 write_byte(0x0040, 0x008e, 0); // clear completion flag
135
136 // basic check : device has to be defined
137 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_STORAGE_DEVICES) ) {
138 BX_DEBUG("%s: function %02x, ELDL out of range %02x\n", __func__, GET_AH(), GET_ELDL());
139 goto int13_fail;
140 }
141
142 // Get the ata channel
143 device = bios_dsk->hdidmap[GET_ELDL()-0x80];
144
145 // basic check : device has to be valid
146 if (device >= BX_MAX_STORAGE_DEVICES) {
147 BX_DEBUG("%s: function %02x, unmapped device for ELDL=%02x\n", __func__, GET_AH(), GET_ELDL());
148 goto int13_fail;
149 }
150
151 switch (GET_AH()) {
152
153 case 0x00: /* disk controller reset */
154#ifdef VBOX_WITH_SCSI
155 /* SCSI controller does not need a reset. */
156 if (!VBOX_IS_SCSI_DEVICE(device))
157#endif
158 ata_reset (device);
159 goto int13_success;
160 break;
161
162 case 0x01: /* read disk status */
163 status = read_byte(0x0040, 0x0074);
164 SET_AH(status);
165 SET_DISK_RET_STATUS(0);
166 /* set CF if error status read */
167 if (status) goto int13_fail_nostatus;
168 else goto int13_success_noah;
169 break;
170
171 case 0x02: // read disk sectors
172 case 0x03: // write disk sectors
173 case 0x04: // verify disk sectors
174
175 count = GET_AL();
176 cylinder = GET_CH();
177 cylinder |= ( ((uint16_t) GET_CL()) << 2) & 0x300;
178 sector = (GET_CL() & 0x3f);
179 head = GET_DH();
180
181 /* Segment and offset are in ES:BX. */
182 if ( (count > 128) || (count == 0) ) {
183 BX_INFO("%s: function %02x, count out of range!\n", __func__, GET_AH());
184 goto int13_fail;
185 }
186
187 /* Get the logical CHS geometry. */
188 nlc = bios_dsk->devices[device].lchs.cylinders;
189 nlh = bios_dsk->devices[device].lchs.heads;
190 nlspt = bios_dsk->devices[device].lchs.spt;
191
192 /* Sanity check the geometry. */
193 if( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
194 BX_INFO("%s: function %02x, disk %02x, parameters out of range %04x/%04x/%04x!\n", __func__, GET_AH(), GET_DL(), cylinder, head, sector);
195 goto int13_fail;
196 }
197
198 // FIXME verify
199 if ( GET_AH() == 0x04 )
200 goto int13_success;
201
202 /* If required, translate LCHS to LBA and execute command. */
203 //@todo: The IS_SCSI_DEVICE check should be redundant...
204 if (( (bios_dsk->devices[device].pchs.heads != nlh) || (bios_dsk->devices[device].pchs.spt != nlspt)) || VBOX_IS_SCSI_DEVICE(device)) {
205 lba = ((((uint32_t)cylinder * (uint32_t)nlh) + (uint32_t)head) * (uint32_t)nlspt) + (uint32_t)sector - 1;
206 sector = 0; // this forces the command to be lba
207 }
208
209 /* Clear the count of transferred sectors/bytes. */
210 bios_dsk->drqp.trsfsectors = 0;
211 bios_dsk->drqp.trsfbytes = 0;
212
213 /* Pass request information to low level disk code. */
214 bios_dsk->drqp.lba = lba;
215 bios_dsk->drqp.buffer = MK_FP(ES, BX);
216 bios_dsk->drqp.nsect = count;
217 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
218 bios_dsk->drqp.cylinder = cylinder;
219 bios_dsk->drqp.head = head;
220 bios_dsk->drqp.sector = sector;
221 bios_dsk->drqp.dev_id = device;
222
223 status = dskacc[bios_dsk->devices[device].type].a[GET_AH() - 0x02](bios_dsk);
224
225 // Set nb of sector transferred
226 SET_AL(bios_dsk->drqp.trsfsectors);
227
228 if (status != 0) {
229 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
230 SET_AH(0x0c);
231 goto int13_fail_noah;
232 }
233
234 goto int13_success;
235 break;
236
237 case 0x05: /* format disk track */
238 BX_INFO("format disk track called\n");
239 goto int13_success;
240 return;
241 break;
242
243 case 0x08: /* read disk drive parameters */
244
245 /* Get the logical geometry from internal table. */
246 nlc = bios_dsk->devices[device].lchs.cylinders;
247 nlh = bios_dsk->devices[device].lchs.heads;
248 nlspt = bios_dsk->devices[device].lchs.spt;
249
250 count = bios_dsk->hdcount;
251 /* Maximum cylinder number is just one less than the number of cylinders. */
252 nlc = nlc - 1; /* 0 based , last sector not used */
253 SET_AL(0);
254 SET_CH(nlc & 0xff);
255 SET_CL(((nlc >> 2) & 0xc0) | (nlspt & 0x3f));
256 SET_DH(nlh - 1);
257 SET_DL(count); /* FIXME returns 0, 1, or n hard drives */
258
259 // FIXME should set ES & DI
260 // @todo: Actually, the above comment is nonsense.
261
262 goto int13_success;
263 break;
264
265 case 0x10: /* check drive ready */
266 // should look at 40:8E also???
267
268 // Read the status from controller
269 status = inb(bios_dsk->channels[device/2].iobase1 + ATA_CB_STAT);
270 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY ) {
271 goto int13_success;
272 } else {
273 SET_AH(0xAA);
274 goto int13_fail_noah;
275 }
276 break;
277
278 case 0x15: /* read disk drive size */
279
280 /* Get the physical geometry from internal table. */
281 cylinder = bios_dsk->devices[device].pchs.cylinders;
282 head = bios_dsk->devices[device].pchs.heads;
283 sector = bios_dsk->devices[device].pchs.spt;
284
285 /* Calculate sector count seen by old style INT 13h. */
286 lba = (uint32_t)cylinder * head * sector;
287 CX = lba >> 16;
288 DX = lba & 0xffff;
289
290 SET_AH(3); // hard disk accessible
291 goto int13_success_noah;
292 break;
293
294 case 0x09: /* initialize drive parameters */
295 case 0x0c: /* seek to specified cylinder */
296 case 0x0d: /* alternate disk reset */
297 case 0x11: /* recalibrate */
298 case 0x14: /* controller internal diagnostic */
299 BX_INFO("%s: function %02xh unimplemented, returns success\n", __func__, GET_AH());
300 goto int13_success;
301 break;
302
303 case 0x0a: /* read disk sectors with ECC */
304 case 0x0b: /* write disk sectors with ECC */
305 case 0x18: // set media type for format
306 default:
307 BX_INFO("%s: function %02xh unsupported, returns fail\n", __func__, GET_AH());
308 goto int13_fail;
309 break;
310 }
311
312int13_fail:
313 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
314int13_fail_noah:
315 SET_DISK_RET_STATUS(GET_AH());
316int13_fail_nostatus:
317 SET_CF(); // error occurred
318 return;
319
320int13_success:
321 SET_AH(0x00); // no error
322int13_success_noah:
323 SET_DISK_RET_STATUS(0x00);
324 CLEAR_CF(); // no error
325 return;
326}
327
328void BIOSCALL int13_harddisk_ext(disk_regs_t r)
329{
330 uint32_t lba;
331 uint16_t ebda_seg = read_word(0x0040,0x000E);
332 uint16_t segment, offset;
333 uint16_t npc, nph, npspt;
334 uint16_t size, count;
335 uint8_t device, status;
336 uint8_t type;
337 bio_dsk_t __far *bios_dsk;
338 int13ext_t __far *i13_ext;
339 dpt_t __far *dpt;
340
341 bios_dsk = read_word(0x0040,0x000E) :> &EbdaData->bdisk;
342
343 BX_DEBUG_INT13_HD("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
344
345 write_byte(0x0040, 0x008e, 0); // clear completion flag
346
347 // basic check : device has to be defined
348 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_STORAGE_DEVICES) ) {
349 BX_DEBUG("%s: function %02x, ELDL out of range %02x\n", __func__, GET_AH(), GET_ELDL());
350 goto int13x_fail;
351 }
352
353 // Get the ata channel
354 device = bios_dsk->hdidmap[GET_ELDL()-0x80];
355
356 // basic check : device has to be valid
357 if (device >= BX_MAX_STORAGE_DEVICES) {
358 BX_DEBUG("%s: function %02x, unmapped device for ELDL=%02x\n", __func__, GET_AH(), GET_ELDL());
359 goto int13x_fail;
360 }
361
362 switch (GET_AH()) {
363 case 0x41: // IBM/MS installation check
364 BX=0xaa55; // install check
365 SET_AH(0x30); // EDD 3.0
366 CX=0x0007; // ext disk access and edd, removable supported
367 goto int13x_success_noah;
368 break;
369
370 case 0x42: // IBM/MS extended read
371 case 0x43: // IBM/MS extended write
372 case 0x44: // IBM/MS verify
373 case 0x47: // IBM/MS extended seek
374
375 /* Get a pointer to the extended structure. */
376 i13_ext = DS :> (int13ext_t *)SI;
377
378 count = i13_ext->count;
379 segment = i13_ext->segment;
380 offset = i13_ext->offset;
381
382 BX_DEBUG_INT13_HD("%s: %d sectors from lba %u @ %04x:%04x\n", __func__,
383 count, i13_ext->lba1, segment, offset);
384
385 // Can't use 64 bits lba
386 lba = i13_ext->lba2;
387 if (lba != 0L) {
388 BX_PANIC("%s: function %02x. Can't use 64bits lba\n", __func__, GET_AH());
389 goto int13x_fail;
390 }
391
392 // Get 32 bits lba and check
393 lba = i13_ext->lba1;
394
395 type = bios_dsk->devices[device].type;
396 if (lba >= bios_dsk->devices[device].sectors) {
397 BX_INFO("%s: function %02x. LBA out of range\n", __func__, GET_AH());
398 goto int13x_fail;
399 }
400
401 /* Don't bother with seek or verify. */
402 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
403 goto int13x_success;
404
405 /* Clear the count of transferred sectors/bytes. */
406 bios_dsk->drqp.trsfsectors = 0;
407 bios_dsk->drqp.trsfbytes = 0;
408
409 /* Pass request information to low level disk code. */
410 bios_dsk->drqp.lba = lba;
411 bios_dsk->drqp.buffer = MK_FP(segment, offset);
412 bios_dsk->drqp.nsect = count;
413 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
414 bios_dsk->drqp.sector = 0; /* Indicate LBA. */
415 bios_dsk->drqp.dev_id = device;
416
417 /* Execute the read or write command. */
418 status = dskacc[type].a[GET_AH() - 0x42](bios_dsk);
419 count = bios_dsk->drqp.trsfsectors;
420 i13_ext->count = count;
421
422 if (status != 0) {
423 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
424 SET_AH(0x0c);
425 goto int13x_fail_noah;
426 }
427
428 goto int13x_success;
429 break;
430
431 case 0x45: // IBM/MS lock/unlock drive
432 case 0x49: // IBM/MS extended media change
433 goto int13x_success; // Always success for HD
434 break;
435
436 case 0x46: // IBM/MS eject media
437 SET_AH(0xb2); // Volume Not Removable
438 goto int13x_fail_noah; // Always fail for HD
439 break;
440
441 case 0x48: // IBM/MS get drive parameters
442 dpt = DS :> (dpt_t *)SI;
443 size = dpt->size;
444
445 /* Check if buffer is large enough. */
446 if (size < 0x1a)
447 goto int13x_fail;
448
449 /* Fill in EDD 1.x table. */
450 if (size >= 0x1a) {
451 uint16_t blksize;
452
453 npc = bios_dsk->devices[device].pchs.cylinders;
454 nph = bios_dsk->devices[device].pchs.heads;
455 npspt = bios_dsk->devices[device].pchs.spt;
456 lba = bios_dsk->devices[device].sectors;
457 blksize = bios_dsk->devices[device].blksize;
458
459 dpt->size = 0x1a;
460 dpt->infos = 0x02; // geometry is valid
461 dpt->cylinders = npc;
462 dpt->heads = nph;
463 dpt->spt = npspt;
464 dpt->blksize = blksize;
465 dpt->sector_count1 = lba; // FIXME should be Bit64
466 dpt->sector_count2 = 0;
467 }
468
469 /* Fill in EDD 2.x table. */
470 if (size >= 0x1e) {
471 uint8_t channel, irq, mode, checksum, i, translation;
472 uint16_t iobase1, iobase2, options;
473
474 dpt->size = 0x1e;
475 dpt->dpte_segment = ebda_seg;
476 dpt->dpte_offset = (uint16_t)&EbdaData->bdisk.dpte;
477
478 // Fill in dpte
479 channel = device / 2;
480 iobase1 = bios_dsk->channels[channel].iobase1;
481 iobase2 = bios_dsk->channels[channel].iobase2;
482 irq = bios_dsk->channels[channel].irq;
483 mode = bios_dsk->devices[device].mode;
484 translation = bios_dsk->devices[device].translation;
485
486 options = (translation == GEO_TRANSLATION_NONE ? 0 : 1 << 3); // chs translation
487 options |= (1 << 4); // lba translation
488 options |= (mode == ATA_MODE_PIO32 ? 1 : 0 << 7);
489 options |= (translation == GEO_TRANSLATION_LBA ? 1 : 0 << 9);
490 options |= (translation == GEO_TRANSLATION_RECHS ? 3 : 0 << 9);
491
492 bios_dsk->dpte.iobase1 = iobase1;
493 bios_dsk->dpte.iobase2 = iobase2;
494 bios_dsk->dpte.prefix = (0xe | (device % 2)) << 4;
495 bios_dsk->dpte.unused = 0xcb;
496 bios_dsk->dpte.irq = irq;
497 bios_dsk->dpte.blkcount = 1;
498 bios_dsk->dpte.dma = 0;
499 bios_dsk->dpte.pio = 0;
500 bios_dsk->dpte.options = options;
501 bios_dsk->dpte.reserved = 0;
502 bios_dsk->dpte.revision = 0x11;
503
504 checksum = 0;
505 for (i = 0; i < 15; ++i)
506 checksum += read_byte(ebda_seg, (uint16_t)&EbdaData->bdisk.dpte + i);
507 checksum = -checksum;
508 bios_dsk->dpte.checksum = checksum;
509 }
510
511 /* Fill in EDD 3.x table. */
512 if(size >= 0x42) {
513 uint8_t channel, iface, checksum, i;
514 uint16_t iobase1;
515
516 channel = device / 2;
517 iface = bios_dsk->channels[channel].iface;
518 iobase1 = bios_dsk->channels[channel].iobase1;
519
520 dpt->size = 0x42;
521 dpt->key = 0xbedd;
522 dpt->dpi_length = 0x24;
523 dpt->reserved1 = 0;
524 dpt->reserved2 = 0;
525
526 if (iface == ATA_IFACE_ISA) {
527 dpt->host_bus[0] = 'I';
528 dpt->host_bus[1] = 'S';
529 dpt->host_bus[2] = 'A';
530 dpt->host_bus[3] = ' ';
531 }
532 else {
533 // FIXME PCI
534 }
535 dpt->iface_type[0] = 'A';
536 dpt->iface_type[1] = 'T';
537 dpt->iface_type[2] = 'A';
538 dpt->iface_type[3] = ' ';
539 dpt->iface_type[4] = ' ';
540 dpt->iface_type[5] = ' ';
541 dpt->iface_type[6] = ' ';
542 dpt->iface_type[7] = ' ';
543
544 if (iface == ATA_IFACE_ISA) {
545 ((uint16_t __far *)dpt->iface_path)[0] = iobase1;
546 ((uint16_t __far *)dpt->iface_path)[1] = 0;
547 ((uint32_t __far *)dpt->iface_path)[1] = 0;
548 }
549 else {
550 // FIXME PCI
551 }
552 ((uint16_t __far *)dpt->device_path)[0] = device & 1; // device % 2; @todo: correct?
553 ((uint16_t __far *)dpt->device_path)[1] = 0;
554 ((uint32_t __far *)dpt->device_path)[1] = 0;
555
556 checksum = 0;
557 for (i = 30; i < 64; i++)
558 checksum += read_byte(DS, SI + i);
559 checksum = -checksum;
560 dpt->checksum = checksum;
561 }
562
563 goto int13x_success;
564 break;
565
566 case 0x4e: // // IBM/MS set hardware configuration
567 // DMA, prefetch, PIO maximum not supported
568 switch (GET_AL()) {
569 case 0x01:
570 case 0x03:
571 case 0x04:
572 case 0x06:
573 goto int13x_success;
574 break;
575 default :
576 goto int13x_fail;
577 }
578 break;
579
580 case 0x50: // IBM/MS send packet command
581 default:
582 BX_INFO("%s: function %02xh unsupported, returns fail\n", __func__, GET_AH());
583 goto int13x_fail;
584 break;
585 }
586
587int13x_fail:
588 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
589int13x_fail_noah:
590 SET_DISK_RET_STATUS(GET_AH());
591 SET_CF(); // error occurred
592 return;
593
594int13x_success:
595 SET_AH(0x00); // no error
596int13x_success_noah:
597 SET_DISK_RET_STATUS(0x00);
598 CLEAR_CF(); // no error
599 return;
600}
601
602/* Avoid saving general registers already saved by caller (PUSHA). */
603#pragma aux int13_harddisk modify [di si cx dx bx];
604#pragma aux int13_harddisk_ext modify [di si cx dx bx];
605
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette