VirtualBox

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

Last change on this file since 67805 was 67694, checked in by vboxsync, 8 years ago

BIOS: INT 13h, INT 15h always returns with interrupts enabled.

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