VirtualBox

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

Last change on this file since 55998 was 48122, checked in by vboxsync, 11 years ago

BIOS: Fixed disk logging, stripped trailing spaces.

  • 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-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 BX_DEBUG_INT13_HD("%s: %d sectors from lba %lu @ %04x:%04x\n", __func__,
210 count, lba, ES, BX);
211
212 /* Clear the count of transferred sectors/bytes. */
213 bios_dsk->drqp.trsfsectors = 0;
214 bios_dsk->drqp.trsfbytes = 0;
215
216 /* Pass request information to low level disk code. */
217 bios_dsk->drqp.lba = lba;
218 bios_dsk->drqp.buffer = MK_FP(ES, BX);
219 bios_dsk->drqp.nsect = count;
220 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
221 bios_dsk->drqp.cylinder = cylinder;
222 bios_dsk->drqp.head = head;
223 bios_dsk->drqp.sector = sector;
224 bios_dsk->drqp.dev_id = device;
225
226 status = dskacc[bios_dsk->devices[device].type].a[GET_AH() - 0x02](bios_dsk);
227
228 // Set nb of sector transferred
229 SET_AL(bios_dsk->drqp.trsfsectors);
230
231 if (status != 0) {
232 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
233 SET_AH(0x0c);
234 goto int13_fail_noah;
235 }
236
237 goto int13_success;
238 break;
239
240 case 0x05: /* format disk track */
241 BX_INFO("format disk track called\n");
242 goto int13_success;
243 return;
244 break;
245
246 case 0x08: /* read disk drive parameters */
247
248 /* Get the logical geometry from internal table. */
249 nlc = bios_dsk->devices[device].lchs.cylinders;
250 nlh = bios_dsk->devices[device].lchs.heads;
251 nlspt = bios_dsk->devices[device].lchs.spt;
252
253 count = bios_dsk->hdcount;
254 /* Maximum cylinder number is just one less than the number of cylinders. */
255 nlc = nlc - 1; /* 0 based , last sector not used */
256 SET_AL(0);
257 SET_CH(nlc & 0xff);
258 SET_CL(((nlc >> 2) & 0xc0) | (nlspt & 0x3f));
259 SET_DH(nlh - 1);
260 SET_DL(count); /* FIXME returns 0, 1, or n hard drives */
261
262 // FIXME should set ES & DI
263 // @todo: Actually, the above comment is nonsense.
264
265 goto int13_success;
266 break;
267
268 case 0x10: /* check drive ready */
269 // should look at 40:8E also???
270
271 // Read the status from controller
272 status = inb(bios_dsk->channels[device/2].iobase1 + ATA_CB_STAT);
273 if ( (status & ( ATA_CB_STAT_BSY | ATA_CB_STAT_RDY )) == ATA_CB_STAT_RDY ) {
274 goto int13_success;
275 } else {
276 SET_AH(0xAA);
277 goto int13_fail_noah;
278 }
279 break;
280
281 case 0x15: /* read disk drive size */
282
283 /* Get the physical geometry from internal table. */
284 cylinder = bios_dsk->devices[device].pchs.cylinders;
285 head = bios_dsk->devices[device].pchs.heads;
286 sector = bios_dsk->devices[device].pchs.spt;
287
288 /* Calculate sector count seen by old style INT 13h. */
289 lba = (uint32_t)cylinder * head * sector;
290 CX = lba >> 16;
291 DX = lba & 0xffff;
292
293 SET_AH(3); // hard disk accessible
294 goto int13_success_noah;
295 break;
296
297 case 0x09: /* initialize drive parameters */
298 case 0x0c: /* seek to specified cylinder */
299 case 0x0d: /* alternate disk reset */
300 case 0x11: /* recalibrate */
301 case 0x14: /* controller internal diagnostic */
302 BX_INFO("%s: function %02xh unimplemented, returns success\n", __func__, GET_AH());
303 goto int13_success;
304 break;
305
306 case 0x0a: /* read disk sectors with ECC */
307 case 0x0b: /* write disk sectors with ECC */
308 case 0x18: // set media type for format
309 default:
310 BX_INFO("%s: function %02xh unsupported, returns fail\n", __func__, GET_AH());
311 goto int13_fail;
312 break;
313 }
314
315int13_fail:
316 SET_AH(0x01); // defaults to invalid function in AH or invalid parameter
317int13_fail_noah:
318 SET_DISK_RET_STATUS(GET_AH());
319int13_fail_nostatus:
320 SET_CF(); // error occurred
321 return;
322
323int13_success:
324 SET_AH(0x00); // no error
325int13_success_noah:
326 SET_DISK_RET_STATUS(0x00);
327 CLEAR_CF(); // no error
328 return;
329}
330
331void BIOSCALL int13_harddisk_ext(disk_regs_t r)
332{
333 uint32_t lba;
334 uint16_t ebda_seg = read_word(0x0040,0x000E);
335 uint16_t segment, offset;
336 uint16_t npc, nph, npspt;
337 uint16_t size, count;
338 uint8_t device, status;
339 uint8_t type;
340 bio_dsk_t __far *bios_dsk;
341 int13ext_t __far *i13_ext;
342 dpt_t __far *dpt;
343
344 bios_dsk = read_word(0x0040,0x000E) :> &EbdaData->bdisk;
345
346 BX_DEBUG_INT13_HD("%s: AX=%04x BX=%04x CX=%04x DX=%04x ES=%04x DS=%04x SI=%04x\n",
347 __func__, AX, BX, CX, DX, ES, DS, SI);
348
349 write_byte(0x0040, 0x008e, 0); // clear completion flag
350
351 // basic check : device has to be defined
352 if ( (GET_ELDL() < 0x80) || (GET_ELDL() >= 0x80 + BX_MAX_STORAGE_DEVICES) ) {
353 BX_DEBUG("%s: function %02x, ELDL out of range %02x\n", __func__, GET_AH(), GET_ELDL());
354 goto int13x_fail;
355 }
356
357 // Get the ata channel
358 device = bios_dsk->hdidmap[GET_ELDL()-0x80];
359
360 // basic check : device has to be valid
361 if (device >= BX_MAX_STORAGE_DEVICES) {
362 BX_DEBUG("%s: function %02x, unmapped device for ELDL=%02x\n", __func__, GET_AH(), GET_ELDL());
363 goto int13x_fail;
364 }
365
366 switch (GET_AH()) {
367 case 0x41: // IBM/MS installation check
368 BX=0xaa55; // install check
369 SET_AH(0x30); // EDD 3.0
370 CX=0x0007; // ext disk access and edd, removable supported
371 goto int13x_success_noah;
372 break;
373
374 case 0x42: // IBM/MS extended read
375 case 0x43: // IBM/MS extended write
376 case 0x44: // IBM/MS verify
377 case 0x47: // IBM/MS extended seek
378
379 /* Get a pointer to the extended structure. */
380 i13_ext = DS :> (int13ext_t *)SI;
381
382 count = i13_ext->count;
383 segment = i13_ext->segment;
384 offset = i13_ext->offset;
385
386 BX_DEBUG_INT13_HD("%s: %d sectors from lba %lu @ %04x:%04x\n", __func__,
387 count, i13_ext->lba1, segment, offset);
388
389 // Can't use 64 bits lba
390 lba = i13_ext->lba2;
391 if (lba != 0L) {
392 BX_PANIC("%s: function %02x. Can't use 64bits lba\n", __func__, GET_AH());
393 goto int13x_fail;
394 }
395
396 // Get 32 bits lba and check
397 lba = i13_ext->lba1;
398
399 type = bios_dsk->devices[device].type;
400 if (lba >= bios_dsk->devices[device].sectors) {
401 BX_INFO("%s: function %02x. LBA out of range\n", __func__, GET_AH());
402 goto int13x_fail;
403 }
404
405 /* Don't bother with seek or verify. */
406 if (( GET_AH() == 0x44 ) || ( GET_AH() == 0x47 ))
407 goto int13x_success;
408
409 /* Clear the count of transferred sectors/bytes. */
410 bios_dsk->drqp.trsfsectors = 0;
411 bios_dsk->drqp.trsfbytes = 0;
412
413 /* Pass request information to low level disk code. */
414 bios_dsk->drqp.lba = lba;
415 bios_dsk->drqp.buffer = MK_FP(segment, offset);
416 bios_dsk->drqp.nsect = count;
417 bios_dsk->drqp.sect_sz = 512; //@todo: device specific?
418 bios_dsk->drqp.sector = 0; /* Indicate LBA. */
419 bios_dsk->drqp.dev_id = device;
420
421 /* Execute the read or write command. */
422 status = dskacc[type].a[GET_AH() - 0x42](bios_dsk);
423 count = bios_dsk->drqp.trsfsectors;
424 i13_ext->count = count;
425
426 if (status != 0) {
427 BX_INFO("%s: function %02x, error %02x !\n", __func__, GET_AH(), status);
428 SET_AH(0x0c);
429 goto int13x_fail_noah;
430 }
431
432 goto int13x_success;
433 break;
434
435 case 0x45: // IBM/MS lock/unlock drive
436 case 0x49: // IBM/MS extended media change
437 goto int13x_success; // Always success for HD
438 break;
439
440 case 0x46: // IBM/MS eject media
441 SET_AH(0xb2); // Volume Not Removable
442 goto int13x_fail_noah; // Always fail for HD
443 break;
444
445 case 0x48: // IBM/MS get drive parameters
446 dpt = DS :> (dpt_t *)SI;
447 size = dpt->size;
448
449 /* Check if buffer is large enough. */
450 if (size < 0x1a)
451 goto int13x_fail;
452
453 /* Fill in EDD 1.x table. */
454 if (size >= 0x1a) {
455 uint16_t blksize;
456
457 npc = bios_dsk->devices[device].pchs.cylinders;
458 nph = bios_dsk->devices[device].pchs.heads;
459 npspt = bios_dsk->devices[device].pchs.spt;
460 lba = bios_dsk->devices[device].sectors;
461 blksize = bios_dsk->devices[device].blksize;
462
463 dpt->size = 0x1a;
464 dpt->infos = 0x02; // geometry is valid
465 dpt->cylinders = npc;
466 dpt->heads = nph;
467 dpt->spt = npspt;
468 dpt->blksize = blksize;
469 dpt->sector_count1 = lba; // FIXME should be Bit64
470 dpt->sector_count2 = 0;
471 }
472
473 /* Fill in EDD 2.x table. */
474 if (size >= 0x1e) {
475 uint8_t channel, irq, mode, checksum, i, translation;
476 uint16_t iobase1, iobase2, options;
477
478 dpt->size = 0x1e;
479 dpt->dpte_segment = ebda_seg;
480 dpt->dpte_offset = (uint16_t)&EbdaData->bdisk.dpte;
481
482 // Fill in dpte
483 channel = device / 2;
484 iobase1 = bios_dsk->channels[channel].iobase1;
485 iobase2 = bios_dsk->channels[channel].iobase2;
486 irq = bios_dsk->channels[channel].irq;
487 mode = bios_dsk->devices[device].mode;
488 translation = bios_dsk->devices[device].translation;
489
490 options = (translation == GEO_TRANSLATION_NONE ? 0 : 1 << 3); // chs translation
491 options |= (1 << 4); // lba translation
492 options |= (mode == ATA_MODE_PIO32 ? 1 : 0 << 7);
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