VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS-new/disk.c@ 40723

Last change on this file since 40723 was 39651, checked in by vboxsync, 13 years ago

Use indirect calls to avoid conditionals; renamed constants for clarity.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.4 KB
Line 
1/*
2 * Copyright (C) 2006-2011 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
95void BIOSCALL int13_harddisk(disk_regs_t r)
96{
97 uint32_t lba;
98 uint16_t cylinder, head, sector;
99 uint16_t nlc, nlh, nlspt;
100 uint16_t count;
101 uint8_t device, status;
102 bio_dsk_t __far *bios_dsk;
103
104 BX_DEBUG_INT13_HD("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
105
106 bios_dsk = read_word(0x0040,0x000E) :> &EbdaData->bdisk;
107 write_byte(0x0040, 0x008e, 0); // clear completion flag
108
109 // basic check : device has to be defined
110 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_STORAGE_DEVICES) ) {
111 BX_DEBUG("%s: function %02x, ELDL out of range %02x\n", __func__, GET_AH(), GET_ELDL());
112 goto int13_fail;
113 }
114
115 // Get the ata channel
116 device = bios_dsk->hdidmap[GET_ELDL()-0x80];
117
118 // basic check : device has to be valid
119 if (device >= BX_MAX_STORAGE_DEVICES) {
120 BX_DEBUG("%s: function %02x, unmapped device for ELDL=%02x\n", __func__, GET_AH(), GET_ELDL());
121 goto int13_fail;
122 }
123
124 switch (GET_AH()) {
125
126 case 0x00: /* disk controller reset */
127#ifdef VBOX_WITH_SCSI
128 /* SCSI controller does not need a reset. */
129 if (!VBOX_IS_SCSI_DEVICE(device))
130#endif
131 ata_reset (device);
132 goto int13_success;
133 break;
134
135 case 0x01: /* read disk status */
136 status = read_byte(0x0040, 0x0074);
137 SET_AH(status);
138 SET_DISK_RET_STATUS(0);
139 /* set CF if error status read */
140 if (status) goto int13_fail_nostatus;
141 else goto int13_success_noah;
142 break;
143
144 case 0x02: // read disk sectors
145 case 0x03: // write disk sectors
146 case 0x04: // verify disk sectors
147
148 count = GET_AL();
149 cylinder = GET_CH();
150 cylinder |= ( ((uint16_t) GET_CL()) << 2) & 0x300;
151 sector = (GET_CL() & 0x3f);
152 head = GET_DH();
153
154 /* Segment and offset are in ES:BX. */
155 if ( (count > 128) || (count == 0) ) {
156 BX_INFO("%s: function %02x, count out of range!\n", __func__, GET_AH());
157 goto int13_fail;
158 }
159
160 /* Get the logical CHS geometry. */
161 nlc = bios_dsk->devices[device].lchs.cylinders;
162 nlh = bios_dsk->devices[device].lchs.heads;
163 nlspt = bios_dsk->devices[device].lchs.spt;
164
165 /* Sanity check the geometry. */
166 if( (cylinder >= nlc) || (head >= nlh) || (sector > nlspt )) {
167 BX_INFO("%s: function %02x, disk %02x, parameters out of range %04x/%04x/%04x!\n", __func__, GET_AH(), GET_DL(), cylinder, head, sector);
168 goto int13_fail;
169 }
170
171 // FIXME verify
172 if ( GET_AH() == 0x04 )
173 goto int13_success;
174
175 /* If required, translate LCHS to LBA and execute command. */
176 //@todo: The IS_SCSI_DEVICE check should be redundant...
177 if (( (bios_dsk->devices[device].pchs.heads != nlh) || (bios_dsk->devices[device].pchs.spt != nlspt)) || VBOX_IS_SCSI_DEVICE(device)) {
178 lba = ((((uint32_t)cylinder * (uint32_t)nlh) + (uint32_t)head) * (uint32_t)nlspt) + (uint32_t)sector - 1;
179 sector = 0; // this forces the command to be lba
180 }
181
182 /* Clear the count of transferred sectors/bytes. */
183 bios_dsk->drqp.trsfsectors = 0;
184 bios_dsk->drqp.trsfbytes = 0;
185
186 /* Pass request information to low level disk code. */
187 bios_dsk->drqp.lba = lba;
188 bios_dsk->drqp.buffer = MK_FP(ES, BX);
189 bios_dsk->drqp.nsect = count;
190 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
191 bios_dsk->drqp.cylinder = cylinder;
192 bios_dsk->drqp.head = head;
193 bios_dsk->drqp.sector = sector;
194 bios_dsk->drqp.dev_id = device;
195
196 status = dskacc[bios_dsk->devices[device].type].a[GET_AH() - 0x02](bios_dsk);
197
198 // Set nb of sector transferred
199 SET_AL(bios_dsk->drqp.trsfsectors);
200
201 if (status != 0) {
202 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
203 SET_AH(0x0c);
204 goto int13_fail_noah;
205 }
206
207 goto int13_success;
208 break;
209
210 case 0x05: /* format disk track */
211 BX_INFO("format disk track called\n");
212 goto int13_success;
213 return;
214 break;
215
216 case 0x08: /* read disk drive parameters */
217
218 /* Get the logical geometry from internal table. */
219 nlc = bios_dsk->devices[device].lchs.cylinders;
220 nlh = bios_dsk->devices[device].lchs.heads;
221 nlspt = bios_dsk->devices[device].lchs.spt;
222
223 count = bios_dsk->hdcount;
224 /* Maximum cylinder number is just one less than the number of cylinders. */
225 nlc = nlc - 1; /* 0 based , last sector not used */
226 SET_AL(0);
227 SET_CH(nlc & 0xff);
228 SET_CL(((nlc >> 2) & 0xc0) | (nlspt & 0x3f));
229 SET_DH(nlh - 1);
230 SET_DL(count); /* FIXME returns 0, 1, or n hard drives */
231
232 // FIXME should set ES & DI
233 // @todo: Actually, the above comment is nonsense.
234
235 goto int13_success;
236 break;
237
238 case 0x10: /* check drive ready */
239 // should look at 40:8E also???
240
241 // Read the status from controller
242 status = inb(bios_dsk->channels[device/2].iobase1 + ATA_CB_STAT);
243 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY ) {
244 goto int13_success;
245 } else {
246 SET_AH(0xAA);
247 goto int13_fail_noah;
248 }
249 break;
250
251 case 0x15: /* read disk drive size */
252
253 /* Get the physical geometry from internal table. */
254 cylinder = bios_dsk->devices[device].pchs.cylinders;
255 head = bios_dsk->devices[device].pchs.heads;
256 sector = bios_dsk->devices[device].pchs.spt;
257
258 /* Calculate sector count seen by old style INT 13h. */
259 lba = (uint32_t)cylinder * head * sector;
260 CX = lba >> 16;
261 DX = lba & 0xffff;
262
263 SET_AH(3); // hard disk accessible
264 goto int13_success_noah;
265 break;
266
267 case 0x09: /* initialize drive parameters */
268 case 0x0c: /* seek to specified cylinder */
269 case 0x0d: /* alternate disk reset */
270 case 0x11: /* recalibrate */
271 case 0x14: /* controller internal diagnostic */
272 BX_INFO("%s: function %02xh unimplemented, returns success\n", __func__, GET_AH());
273 goto int13_success;
274 break;
275
276 case 0x0a: /* read disk sectors with ECC */
277 case 0x0b: /* write disk sectors with ECC */
278 case 0x18: // set media type for format
279 default:
280 BX_INFO("%s: function %02xh unsupported, returns fail\n", __func__, GET_AH());
281 goto int13_fail;
282 break;
283 }
284
285int13_fail:
286 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
287int13_fail_noah:
288 SET_DISK_RET_STATUS(GET_AH());
289int13_fail_nostatus:
290 SET_CF(); // error occurred
291 return;
292
293int13_success:
294 SET_AH(0x00); // no error
295int13_success_noah:
296 SET_DISK_RET_STATUS(0x00);
297 CLEAR_CF(); // no error
298 return;
299}
300
301void BIOSCALL int13_harddisk_ext(disk_regs_t r)
302{
303 uint32_t lba;
304 uint16_t ebda_seg = read_word(0x0040,0x000E);
305 uint16_t segment, offset;
306 uint16_t npc, nph, npspt;
307 uint16_t size, count;
308 uint8_t device, status;
309 uint8_t type;
310 bio_dsk_t __far *bios_dsk;
311 int13ext_t __far *i13_ext;
312 dpt_t __far *dpt;
313
314 bios_dsk = read_word(0x0040,0x000E) :> &EbdaData->bdisk;
315
316 BX_DEBUG_INT13_HD("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x\n", __func__, AX, BX, CX, DX, ES);
317
318 write_byte(0x0040, 0x008e, 0); // clear completion flag
319
320 // basic check : device has to be defined
321 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_STORAGE_DEVICES) ) {
322 BX_DEBUG("%s: function %02x, ELDL out of range %02x\n", __func__, GET_AH(), GET_ELDL());
323 goto int13x_fail;
324 }
325
326 // Get the ata channel
327 device = bios_dsk->hdidmap[GET_ELDL()-0x80];
328
329 // basic check : device has to be valid
330 if (device >= BX_MAX_STORAGE_DEVICES) {
331 BX_DEBUG("%s: function %02x, unmapped device for ELDL=%02x\n", __func__, GET_AH(), GET_ELDL());
332 goto int13x_fail;
333 }
334
335 switch (GET_AH()) {
336 case 0x41: // IBM/MS installation check
337 BX=0xaa55; // install check
338 SET_AH(0x30); // EDD 3.0
339 CX=0x0007; // ext disk access and edd, removable supported
340 goto int13x_success_noah;
341 break;
342
343 case 0x42: // IBM/MS extended read
344 case 0x43: // IBM/MS extended write
345 case 0x44: // IBM/MS verify
346 case 0x47: // IBM/MS extended seek
347
348 /* Get a pointer to the extended structure. */
349 i13_ext = DS :> (int13ext_t *)SI;
350
351 count = i13_ext->count;
352 segment = i13_ext->segment;
353 offset = i13_ext->offset;
354
355 BX_DEBUG_INT13_HD("%s: %d sectors from lba %u @ %04x:%04x\n", __func__,
356 count, i13_ext->lba1, segment, offset);
357
358 // Can't use 64 bits lba
359 lba = i13_ext->lba2;
360 if (lba != 0L) {
361 BX_PANIC("%s: function %02x. Can't use 64bits lba\n", __func__, GET_AH());
362 goto int13x_fail;
363 }
364
365 // Get 32 bits lba and check
366 lba = i13_ext->lba1;
367
368 type = bios_dsk->devices[device].type;
369 if (lba >= bios_dsk->devices[device].sectors) {
370 BX_INFO("%s: function %02x. LBA out of range\n", __func__, GET_AH());
371 goto int13x_fail;
372 }
373
374 /* Don't bother with seek or verify. */
375 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
376 goto int13x_success;
377
378 /* Clear the count of transferred sectors/bytes. */
379 bios_dsk->drqp.trsfsectors = 0;
380 bios_dsk->drqp.trsfbytes = 0;
381
382 /* Pass request information to low level disk code. */
383 bios_dsk->drqp.lba = lba;
384 bios_dsk->drqp.buffer = MK_FP(segment, offset);
385 bios_dsk->drqp.nsect = count;
386 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
387 bios_dsk->drqp.sector = 0; /* Indicate LBA. */
388 bios_dsk->drqp.dev_id = device;
389
390 /* Execute the read or write command. */
391 status = dskacc[type].a[GET_AH() - 0x42](bios_dsk);
392 count = bios_dsk->drqp.trsfsectors;
393 i13_ext->count = count;
394
395 if (status != 0) {
396 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
397 SET_AH(0x0c);
398 goto int13x_fail_noah;
399 }
400
401 goto int13x_success;
402 break;
403
404 case 0x45: // IBM/MS lock/unlock drive
405 case 0x49: // IBM/MS extended media change
406 goto int13x_success; // Always success for HD
407 break;
408
409 case 0x46: // IBM/MS eject media
410 SET_AH(0xb2); // Volume Not Removable
411 goto int13x_fail_noah; // Always fail for HD
412 break;
413
414 case 0x48: // IBM/MS get drive parameters
415 dpt = DS :> (dpt_t *)SI;
416 size = dpt->size;
417
418 /* Check if buffer is large enough. */
419 if (size < 0x1a)
420 goto int13x_fail;
421
422 /* Fill in EDD 1.x table. */
423 if (size >= 0x1a) {
424 uint16_t blksize;
425
426 npc = bios_dsk->devices[device].pchs.cylinders;
427 nph = bios_dsk->devices[device].pchs.heads;
428 npspt = bios_dsk->devices[device].pchs.spt;
429 lba = bios_dsk->devices[device].sectors;
430 blksize = bios_dsk->devices[device].blksize;
431
432 dpt->size = 0x1a;
433 dpt->infos = 0x02; // geometry is valid
434 dpt->cylinders = npc;
435 dpt->heads = nph;
436 dpt->spt = npspt;
437 dpt->blksize = blksize;
438 dpt->sector_count1 = lba; // FIXME should be Bit64
439 dpt->sector_count2 = 0;
440 }
441
442 /* Fill in EDD 2.x table. */
443 if (size >= 0x1e) {
444 uint8_t channel, irq, mode, checksum, i, translation;
445 uint16_t iobase1, iobase2, options;
446
447 dpt->size = 0x1e;
448 dpt->dpte_segment = ebda_seg;
449 dpt->dpte_offset = (uint16_t)&EbdaData->bdisk.dpte;
450
451 // Fill in dpte
452 channel = device / 2;
453 iobase1 = bios_dsk->channels[channel].iobase1;
454 iobase2 = bios_dsk->channels[channel].iobase2;
455 irq = bios_dsk->channels[channel].irq;
456 mode = bios_dsk->devices[device].mode;
457 translation = bios_dsk->devices[device].translation;
458
459 options = (translation == GEO_TRANSLATION_NONE ? 0 : 1 << 3); // chs translation
460 options |= (1 << 4); // lba translation
461 options |= (mode == ATA_MODE_PIO32 ? 1 : 0 << 7);
462 options |= (translation == GEO_TRANSLATION_LBA ? 1 : 0 << 9);
463 options |= (translation == GEO_TRANSLATION_RECHS ? 3 : 0 << 9);
464
465 bios_dsk->dpte.iobase1 = iobase1;
466 bios_dsk->dpte.iobase2 = iobase2;
467 bios_dsk->dpte.prefix = (0xe | (device % 2)) << 4;
468 bios_dsk->dpte.unused = 0xcb;
469 bios_dsk->dpte.irq = irq;
470 bios_dsk->dpte.blkcount = 1;
471 bios_dsk->dpte.dma = 0;
472 bios_dsk->dpte.pio = 0;
473 bios_dsk->dpte.options = options;
474 bios_dsk->dpte.reserved = 0;
475 bios_dsk->dpte.revision = 0x11;
476
477 checksum = 0;
478 for (i = 0; i < 15; ++i)
479 checksum += read_byte(ebda_seg, (uint16_t)&EbdaData->bdisk.dpte + i);
480 checksum = -checksum;
481 bios_dsk->dpte.checksum = checksum;
482 }
483
484 /* Fill in EDD 3.x table. */
485 if(size >= 0x42) {
486 uint8_t channel, iface, checksum, i;
487 uint16_t iobase1;
488
489 channel = device / 2;
490 iface = bios_dsk->channels[channel].iface;
491 iobase1 = bios_dsk->channels[channel].iobase1;
492
493 dpt->size = 0x42;
494 dpt->key = 0xbedd;
495 dpt->dpi_length = 0x24;
496 dpt->reserved1 = 0;
497 dpt->reserved2 = 0;
498
499 if (iface == ATA_IFACE_ISA) {
500 dpt->host_bus[0] = 'I';
501 dpt->host_bus[1] = 'S';
502 dpt->host_bus[2] = 'A';
503 dpt->host_bus[3] = ' ';
504 }
505 else {
506 // FIXME PCI
507 }
508 dpt->iface_type[0] = 'A';
509 dpt->iface_type[1] = 'T';
510 dpt->iface_type[2] = 'A';
511 dpt->iface_type[3] = ' ';
512 dpt->iface_type[4] = ' ';
513 dpt->iface_type[5] = ' ';
514 dpt->iface_type[6] = ' ';
515 dpt->iface_type[7] = ' ';
516
517 if (iface == ATA_IFACE_ISA) {
518 ((uint16_t __far *)dpt->iface_path)[0] = iobase1;
519 ((uint16_t __far *)dpt->iface_path)[1] = 0;
520 ((uint32_t __far *)dpt->iface_path)[1] = 0;
521 }
522 else {
523 // FIXME PCI
524 }
525 ((uint16_t __far *)dpt->device_path)[0] = device & 1; // device % 2; @todo: correct?
526 ((uint16_t __far *)dpt->device_path)[1] = 0;
527 ((uint32_t __far *)dpt->device_path)[1] = 0;
528
529 checksum = 0;
530 for (i = 30; i < 64; i++)
531 checksum += read_byte(DS, SI + i);
532 checksum = -checksum;
533 dpt->checksum = checksum;
534 }
535
536 goto int13x_success;
537 break;
538
539 case 0x4e: // // IBM/MS set hardware configuration
540 // DMA, prefetch, PIO maximum not supported
541 switch (GET_AL()) {
542 case 0x01:
543 case 0x03:
544 case 0x04:
545 case 0x06:
546 goto int13x_success;
547 break;
548 default :
549 goto int13x_fail;
550 }
551 break;
552
553 case 0x50: // IBM/MS send packet command
554 default:
555 BX_INFO("%s: function %02xh unsupported, returns fail\n", __func__, GET_AH());
556 goto int13x_fail;
557 break;
558 }
559
560int13x_fail:
561 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
562int13x_fail_noah:
563 SET_DISK_RET_STATUS(GET_AH());
564 SET_CF(); // error occurred
565 return;
566
567int13x_success:
568 SET_AH(0x00); // no error
569int13x_success_noah:
570 SET_DISK_RET_STATUS(0x00);
571 CLEAR_CF(); // no error
572 return;
573}
574
575/* Avoid saving general registers already saved by caller (PUSHA). */
576#pragma aux int13_harddisk modify [di si cx dx bx];
577#pragma aux int13_harddisk_ext modify [di si cx dx bx];
578
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