VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS/ata.c@ 60422

Last change on this file since 60422 was 60422, checked in by vboxsync, 9 years ago

VGABIOS,PCBIOS: Build variants for 80286 and 8086 in addition to the default 80386 one. Added build time checking of the BIOSORG results (sed + map file). Fixed shutdown code 0ah jump.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 38.8 KB
Line 
1/*
2 * Copyright (C) 2006-2015 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 <stdarg.h>
44#include "inlines.h"
45#include "biosint.h"
46#include "ebda.h"
47#include "ata.h"
48
49#if DEBUG_ATA
50# define BX_DEBUG_ATA(...) BX_DEBUG(__VA_ARGS__)
51#else
52# define BX_DEBUG_ATA(...)
53#endif
54
55
56// ---------------------------------------------------------------------------
57// Start of ATA/ATAPI Driver
58// ---------------------------------------------------------------------------
59
60void insw_discard(unsigned nwords, unsigned port);
61#pragma aux insw_discard = \
62 ".286" \
63 "again:" \
64 "in ax,dx" \
65 "loop again" \
66 parm [cx] [dx] modify exact [cx ax] nomemory;
67
68void insd_discard(unsigned ndwords, unsigned port);
69#if VBOX_BIOS_CPU >= 80386
70# pragma aux insd_discard = \
71 ".386" \
72 "push eax" \
73 "again:" \
74 "in eax,dx" \
75 "loop again" \
76 "pop eax" \
77 parm [cx] [dx] modify exact [cx] nomemory;
78#endif
79
80// ---------------------------------------------------------------------------
81// ATA/ATAPI driver : initialization
82// ---------------------------------------------------------------------------
83void BIOSCALL ata_init(void)
84{
85 uint8_t channel, device;
86 bio_dsk_t __far *bios_dsk;
87
88 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
89
90 // Channels info init.
91 for (channel=0; channel<BX_MAX_ATA_INTERFACES; channel++) {
92 bios_dsk->channels[channel].iface = ATA_IFACE_NONE;
93 bios_dsk->channels[channel].iobase1 = 0x0;
94 bios_dsk->channels[channel].iobase2 = 0x0;
95 bios_dsk->channels[channel].irq = 0;
96 }
97
98 // Devices info init.
99 for (device=0; device<BX_MAX_ATA_DEVICES; device++) {
100 bios_dsk->devices[device].type = DSK_TYPE_NONE;
101 bios_dsk->devices[device].device = DSK_DEVICE_NONE;
102 bios_dsk->devices[device].removable = 0;
103 bios_dsk->devices[device].lock = 0;
104 bios_dsk->devices[device].mode = ATA_MODE_NONE;
105 bios_dsk->devices[device].blksize = 0x200;
106 bios_dsk->devices[device].translation = GEO_TRANSLATION_NONE;
107 bios_dsk->devices[device].lchs.heads = 0;
108 bios_dsk->devices[device].lchs.cylinders = 0;
109 bios_dsk->devices[device].lchs.spt = 0;
110 bios_dsk->devices[device].pchs.heads = 0;
111 bios_dsk->devices[device].pchs.cylinders = 0;
112 bios_dsk->devices[device].pchs.spt = 0;
113 bios_dsk->devices[device].sectors = 0;
114 }
115
116 // hdidmap and cdidmap init.
117 for (device=0; device<BX_MAX_STORAGE_DEVICES; device++) {
118 bios_dsk->hdidmap[device] = BX_MAX_STORAGE_DEVICES;
119 bios_dsk->cdidmap[device] = BX_MAX_STORAGE_DEVICES;
120 }
121
122 bios_dsk->hdcount = 0;
123 bios_dsk->cdcount = 0;
124}
125
126// ---------------------------------------------------------------------------
127// ATA/ATAPI driver : software reset
128// ---------------------------------------------------------------------------
129// ATA-3
130// 8.2.1 Software reset - Device 0
131
132void ata_reset(uint16_t device)
133{
134 uint16_t iobase1, iobase2;
135 uint8_t channel, slave, sn, sc;
136 uint16_t max;
137 uint16_t pdelay;
138 bio_dsk_t __far *bios_dsk;
139
140 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
141 channel = device / 2;
142 slave = device % 2;
143
144 iobase1 = bios_dsk->channels[channel].iobase1;
145 iobase2 = bios_dsk->channels[channel].iobase2;
146
147 // Reset
148
149 // 8.2.1 (a) -- set SRST in DC
150 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST);
151
152 // 8.2.1 (b) -- wait for BSY
153 max=0xff;
154 while(--max>0) {
155 uint8_t status = inb(iobase1+ATA_CB_STAT);
156 if ((status & ATA_CB_STAT_BSY) != 0)
157 break;
158 }
159
160 // 8.2.1 (f) -- clear SRST
161 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
162
163 if (bios_dsk->devices[device].type != DSK_TYPE_NONE) {
164 // 8.2.1 (g) -- check for sc==sn==0x01
165 // select device
166 outb(iobase1+ATA_CB_DH, slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0);
167 sc = inb(iobase1+ATA_CB_SC);
168 sn = inb(iobase1+ATA_CB_SN);
169
170 if ( (sc==0x01) && (sn==0x01) ) {
171 // 8.2.1 (h) -- wait for not BSY
172 max=0xffff; /* The ATA specification says that the drive may be busy for up to 30 seconds. */
173 while(--max>0) {
174 uint8_t status = inb(iobase1+ATA_CB_STAT);
175 if ((status & ATA_CB_STAT_BSY) == 0)
176 break;
177 pdelay=0xffff;
178 while (--pdelay>0) {
179 /* nothing */
180 }
181 }
182 }
183 }
184
185 // 8.2.1 (i) -- wait for DRDY
186 max = 0x10; /* Speed up for virtual drives. Disks are immediately ready, CDs never */
187 while(--max>0) {
188 uint8_t status = inb(iobase1+ATA_CB_STAT);
189 if ((status & ATA_CB_STAT_RDY) != 0)
190 break;
191 }
192
193 // Enable interrupts
194 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
195}
196
197// ---------------------------------------------------------------------------
198// ATA/ATAPI driver : execute a data-in command
199// ---------------------------------------------------------------------------
200 // returns
201 // 0 : no error
202 // 1 : BUSY bit set
203 // 2 : read error
204 // 3 : expected DRQ=1
205 // 4 : no sectors left to read/verify
206 // 5 : more sectors to read/verify
207 // 6 : no sectors left to write
208 // 7 : more sectors to write
209uint16_t ata_cmd_data_in(bio_dsk_t __far *bios_dsk, uint16_t command, uint16_t count)
210{
211 uint16_t iobase1, iobase2, blksize, mult_blk_cnt;
212 uint16_t cylinder;
213 uint8_t head;
214 uint8_t sector;
215 uint8_t device;
216 uint8_t status, mode;
217 char __far *buffer;
218
219 device = bios_dsk->drqp.dev_id;
220
221 iobase1 = bios_dsk->channels[device / 2].iobase1;
222 iobase2 = bios_dsk->channels[device / 2].iobase2;
223 mode = bios_dsk->devices[device].mode;
224 blksize = bios_dsk->devices[device].blksize;
225 if (blksize == 0) { /* If transfer size is exactly 64K */
226#if VBOX_BIOS_CPU >= 80386
227 if (mode == ATA_MODE_PIO32)
228 blksize = 0x4000;
229 else
230#endif
231 blksize = 0x8000;
232 } else {
233#if VBOX_BIOS_CPU >= 80386
234 if (mode == ATA_MODE_PIO32)
235 blksize >>= 2;
236 else
237#endif
238 blksize >>= 1;
239 }
240
241 status = inb(iobase1 + ATA_CB_STAT);
242 if (status & ATA_CB_STAT_BSY)
243 {
244 BX_DEBUG_ATA("%s: disk busy\n", __func__);
245 // Enable interrupts
246 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
247 return 1;
248 }
249
250 buffer = bios_dsk->drqp.buffer;
251 sector = bios_dsk->drqp.sector;
252 cylinder = bios_dsk->drqp.cylinder;
253 head = bios_dsk->drqp.head;
254
255 // sector will be 0 only on lba access. Convert to lba-chs
256 if (sector == 0) {
257 if (bios_dsk->drqp.lba + count >= 268435456)
258 {
259 sector = (bios_dsk->drqp.lba >> 24) & 0x00ff;
260 cylinder = (bios_dsk->drqp.lba >> 32) & 0xffff;
261 outb(iobase1 + ATA_CB_SC, (count & 0xff00) >> 8);
262 outb(iobase1 + ATA_CB_SN, sector);
263 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
264 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
265 /* Leave the bottom 24 bits as is, they are treated correctly by the
266 * LBA28 code path. */
267 }
268 sector = bios_dsk->drqp.lba & 0x000000ffL;
269 cylinder = (bios_dsk->drqp.lba >> 8) & 0x0000ffffL;
270 head = ((bios_dsk->drqp.lba >> 24) & 0x0000000fL) | 0x40;
271 }
272
273 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
274 outb(iobase1 + ATA_CB_FR, 0x00);
275 outb(iobase1 + ATA_CB_SC, count);
276 outb(iobase1 + ATA_CB_SN, sector);
277 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
278 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
279 outb(iobase1 + ATA_CB_DH, ((device & 1) ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | head );
280 outb(iobase1 + ATA_CB_CMD, command);
281
282 if (command == ATA_CMD_READ_MULTIPLE || command == ATA_CMD_READ_MULTIPLE_EXT) {
283 mult_blk_cnt = count;
284 count = 1;
285 } else {
286 mult_blk_cnt = 1;
287 }
288
289 while (1) {
290 status = inb(iobase1 + ATA_CB_STAT);
291 if ( !(status & ATA_CB_STAT_BSY) )
292 break;
293 }
294
295 if (status & ATA_CB_STAT_ERR) {
296 BX_DEBUG_ATA("%s: read error\n", __func__);
297 // Enable interrupts
298 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
299 return 2;
300 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
301 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
302 // Enable interrupts
303 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
304 return 3;
305 }
306
307 // FIXME : move seg/off translation here
308
309 int_enable(); // enable higher priority interrupts
310
311 while (1) {
312
313 // adjust if there will be an overrun. 2K max sector size
314 if (FP_OFF(buffer) >= 0xF800)
315 buffer = MK_FP(FP_SEG(buffer) + 0x80, FP_OFF(buffer) - 0x800);
316
317#if VBOX_BIOS_CPU >= 80386
318 if (mode == ATA_MODE_PIO32)
319 buffer = rep_insd(buffer, blksize, iobase1);
320 else
321#endif
322 buffer = rep_insw(buffer, blksize, iobase1);
323 bios_dsk->drqp.trsfsectors += mult_blk_cnt;
324 count--;
325 while (1) {
326 status = inb(iobase1 + ATA_CB_STAT);
327 if ( !(status & ATA_CB_STAT_BSY) )
328 break;
329 }
330 if (count == 0) {
331 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
332 != ATA_CB_STAT_RDY ) {
333 BX_DEBUG_ATA("%s: no sectors left (status %02x)\n", __func__, (unsigned) status);
334 // Enable interrupts
335 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
336 return 4;
337 }
338 break;
339 }
340 else {
341 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
342 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
343 BX_DEBUG_ATA("%s: more sectors left (status %02x)\n", __func__, (unsigned) status);
344 // Enable interrupts
345 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
346 return 5;
347 }
348 continue;
349 }
350 }
351 // Enable interrupts
352 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
353 return 0;
354}
355
356// ---------------------------------------------------------------------------
357// ATA/ATAPI driver : device detection
358// ---------------------------------------------------------------------------
359
360void BIOSCALL ata_detect(void)
361{
362 uint16_t ebda_seg = read_word(0x0040,0x000E);
363 uint8_t hdcount, cdcount, device, type;
364 uint8_t buffer[0x0200];
365 bio_dsk_t __far *bios_dsk;
366
367 bios_dsk = ebda_seg :> &EbdaData->bdisk;
368
369#if BX_MAX_ATA_INTERFACES > 0
370 bios_dsk->channels[0].iface = ATA_IFACE_ISA;
371 bios_dsk->channels[0].iobase1 = 0x1f0;
372 bios_dsk->channels[0].iobase2 = 0x3f0;
373 bios_dsk->channels[0].irq = 14;
374#endif
375#if BX_MAX_ATA_INTERFACES > 1
376 bios_dsk->channels[1].iface = ATA_IFACE_ISA;
377 bios_dsk->channels[1].iobase1 = 0x170;
378 bios_dsk->channels[1].iobase2 = 0x370;
379 bios_dsk->channels[1].irq = 15;
380#endif
381#if 0 //@todo - temporarily removed to avoid conflict with AHCI
382#if BX_MAX_ATA_INTERFACES > 2
383 bios_dsk->channels[2].iface = ATA_IFACE_ISA;
384 bios_dsk->channels[2].iobase1 = 0x1e8;
385 bios_dsk->channels[2].iobase2 = 0x3e0;
386 bios_dsk->channels[2].irq = 12;
387#endif
388#if BX_MAX_ATA_INTERFACES > 3
389 bios_dsk->channels[3].iface = ATA_IFACE_ISA;
390 bios_dsk->channels[3].iobase1 = 0x168;
391 bios_dsk->channels[3].iobase2 = 0x360;
392 bios_dsk->channels[3].irq = 11;
393#endif
394#endif
395#if BX_MAX_ATA_INTERFACES > 4
396#error Please fill the ATA interface informations
397#endif
398
399 // Device detection
400 hdcount = cdcount = 0;
401
402 for (device = 0; device < BX_MAX_ATA_DEVICES; device++) {
403 uint16_t iobase1, iobase2;
404 uint8_t channel, slave;
405 uint8_t sc, sn, cl, ch, st;
406
407 channel = device / 2;
408 slave = device % 2;
409
410 iobase1 = bios_dsk->channels[channel].iobase1;
411 iobase2 = bios_dsk->channels[channel].iobase2;
412
413 // Disable interrupts
414 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
415
416 // Look for device
417 outb(iobase1+ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
418 outb(iobase1+ATA_CB_SC, 0x55);
419 outb(iobase1+ATA_CB_SN, 0xaa);
420 outb(iobase1+ATA_CB_SC, 0xaa);
421 outb(iobase1+ATA_CB_SN, 0x55);
422 outb(iobase1+ATA_CB_SC, 0x55);
423 outb(iobase1+ATA_CB_SN, 0xaa);
424
425 // If we found something
426 sc = inb(iobase1+ATA_CB_SC);
427 sn = inb(iobase1+ATA_CB_SN);
428
429 if ( (sc == 0x55) && (sn == 0xaa) ) {
430 bios_dsk->devices[device].type = DSK_TYPE_UNKNOWN;
431
432 // reset the channel
433 ata_reset(device);
434
435 // check for ATA or ATAPI
436 outb(iobase1+ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
437 sc = inb(iobase1+ATA_CB_SC);
438 sn = inb(iobase1+ATA_CB_SN);
439 if ((sc==0x01) && (sn==0x01)) {
440 cl = inb(iobase1+ATA_CB_CL);
441 ch = inb(iobase1+ATA_CB_CH);
442 st = inb(iobase1+ATA_CB_STAT);
443
444 if ((cl==0x14) && (ch==0xeb)) {
445 bios_dsk->devices[device].type = DSK_TYPE_ATAPI;
446 } else if ((cl==0x00) && (ch==0x00) && (st!=0x00)) {
447 bios_dsk->devices[device].type = DSK_TYPE_ATA;
448 } else if ((cl==0xff) && (ch==0xff)) {
449 bios_dsk->devices[device].type = DSK_TYPE_NONE;
450 }
451 }
452 }
453
454 // Enable interrupts
455 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
456
457 type = bios_dsk->devices[device].type;
458
459 // Now we send a IDENTIFY command to ATA device
460 if (type == DSK_TYPE_ATA) {
461 uint64_t sectors;
462 uint16_t cylinders, heads, spt, blksize;
463 chs_t lgeo;
464 uint8_t chsgeo_base;
465 uint8_t removable, mode;
466
467 //Temporary values to do the transfer
468 bios_dsk->devices[device].device = DSK_DEVICE_HD;
469 bios_dsk->devices[device].mode = ATA_MODE_PIO16;
470 bios_dsk->drqp.buffer = buffer;
471 bios_dsk->drqp.dev_id = device;
472
473 if (ata_cmd_data_in(bios_dsk, ATA_CMD_IDENTIFY_DEVICE, 1) !=0 )
474 BX_PANIC("ata-detect: Failed to detect ATA device\n");
475
476 removable = (*(buffer+0) & 0x80) ? 1 : 0;
477 mode = *(buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
478 blksize = 512; /* There is no sector size field any more. */
479
480 cylinders = *(uint16_t *)(buffer+(1*2)); // word 1
481 heads = *(uint16_t *)(buffer+(3*2)); // word 3
482 spt = *(uint16_t *)(buffer+(6*2)); // word 6
483
484 sectors = *(uint32_t *)(buffer+(60*2)); // word 60 and word 61
485 if (sectors == 0x0FFFFFFF) /* For disks bigger than ~128GB */
486 sectors = *(uint64_t *)(buffer+(100*2)); // words 100 to 103
487 switch (device)
488 {
489 case 0:
490 chsgeo_base = 0x1e;
491 break;
492 case 1:
493 chsgeo_base = 0x26;
494 break;
495 case 2:
496 chsgeo_base = 0x67;
497 break;
498 case 3:
499 chsgeo_base = 0x70;
500 break;
501 default:
502 chsgeo_base = 0;
503 }
504 if (chsgeo_base)
505 {
506 lgeo.cylinders = inb_cmos(chsgeo_base) + (inb_cmos(chsgeo_base + 1) << 8);
507 lgeo.heads = inb_cmos(chsgeo_base + 2);
508 lgeo.spt = inb_cmos(chsgeo_base + 7);
509 }
510 else
511 set_geom_lba(&lgeo, sectors); /* Default EDD-style translated LBA geometry. */
512
513 BX_INFO("ata%d-%d: PCHS=%u/%u/%u LCHS=%u/%u/%u\n", channel, slave,
514 cylinders, heads, spt, lgeo.cylinders, lgeo.heads, lgeo.spt);
515
516 bios_dsk->devices[device].device = DSK_DEVICE_HD;
517 bios_dsk->devices[device].removable = removable;
518 bios_dsk->devices[device].mode = mode;
519 bios_dsk->devices[device].blksize = blksize;
520 bios_dsk->devices[device].pchs.heads = heads;
521 bios_dsk->devices[device].pchs.cylinders = cylinders;
522 bios_dsk->devices[device].pchs.spt = spt;
523 bios_dsk->devices[device].sectors = sectors;
524 bios_dsk->devices[device].lchs = lgeo;
525 if (device < 2)
526 {
527 uint8_t sum, i;
528 fdpt_t __far *fdpt;
529
530 if (device == 0)
531 fdpt = ebda_seg :> &EbdaData->fdpt0;
532 else
533 fdpt = ebda_seg :> &EbdaData->fdpt1;
534
535 /* Update the DPT for drive 0/1 pointed to by Int41/46. This used
536 * to be done at POST time with lots of ugly assembler code, which
537 * isn't worth the effort of converting from AMI to Award CMOS
538 * format. Just do it here. */
539 fdpt->lcyl = lgeo.cylinders;
540 fdpt->lhead = lgeo.heads;
541 fdpt->sig = 0xa0;
542 fdpt->spt = spt;
543 fdpt->cyl = cylinders;
544 fdpt->head = heads;
545 fdpt->lspt = spt;
546 sum = 0;
547 for (i = 0; i < 0xf; i++)
548 sum += *((uint8_t __far *)fdpt + i);
549 sum = -sum;
550 fdpt->csum = sum;
551 }
552
553 // fill hdidmap
554 bios_dsk->hdidmap[hdcount] = device;
555 hdcount++;
556 }
557
558 // Now we send an IDENTIFY command to ATAPI device
559 if (type == DSK_TYPE_ATAPI) {
560 uint8_t type, removable, mode;
561 uint16_t blksize;
562
563 // Temporary values to do the transfer
564 bios_dsk->devices[device].device = DSK_DEVICE_CDROM;
565 bios_dsk->devices[device].mode = ATA_MODE_PIO16;
566 bios_dsk->drqp.buffer = buffer;
567 bios_dsk->drqp.dev_id = device;
568
569 if (ata_cmd_data_in(bios_dsk, ATA_CMD_IDENTIFY_PACKET, 1) != 0)
570 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
571
572 type = *(buffer+1) & 0x1f;
573 removable = (*(buffer+0) & 0x80) ? 1 : 0;
574 mode = *(buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
575 blksize = 2048;
576
577 bios_dsk->devices[device].device = type;
578 bios_dsk->devices[device].removable = removable;
579 bios_dsk->devices[device].mode = mode;
580 bios_dsk->devices[device].blksize = blksize;
581
582 // fill cdidmap
583 bios_dsk->cdidmap[cdcount] = device;
584 cdcount++;
585 }
586
587 {
588 uint32_t sizeinmb;
589 uint16_t ataversion;
590 uint8_t version, model[41];
591 int i;
592
593 switch (type) {
594 case DSK_TYPE_ATA:
595 sizeinmb = (bios_dsk->devices[device].sectors >> 11);
596 case DSK_TYPE_ATAPI:
597 // Read ATA/ATAPI version
598 ataversion = ((uint16_t)(*(buffer+161))<<8) | *(buffer+160);
599 for (version = 15; version > 0; version--) {
600 if ((ataversion & (1 << version)) !=0 )
601 break;
602 }
603
604 // Read model name
605 for (i = 0; i < 20; i++ ) {
606 *(model+(i*2)) = *(buffer+(i*2)+54+1);
607 *(model+(i*2)+1) = *(buffer+(i*2)+54);
608 }
609
610 // Reformat
611 *(model+40) = 0x00;
612 for ( i = 39; i > 0; i-- ){
613 if (*(model+i) == 0x20)
614 *(model+i) = 0x00;
615 else
616 break;
617 }
618 break;
619 }
620
621#ifdef VBOXz
622 // we don't want any noisy output for now
623#else /* !VBOX */
624 switch (type) {
625 int c;
626 case DSK_TYPE_ATA:
627 printf("ata%d %s: ", channel, slave ? " slave" : "master");
628 i=0;
629 while(c=*(model+i++))
630 printf("%c", c);
631 printf(" ATA-%d Hard-Disk (%lu MBytes)\n", version, sizeinmb);
632 break;
633 case DSK_TYPE_ATAPI:
634 printf("ata%d %s: ", channel, slave ? " slave" : "master");
635 i=0;
636 while(c=*(model+i++))
637 printf("%c", c);
638 if (bios_dsk->devices[device].device == DSK_DEVICE_CDROM)
639 printf(" ATAPI-%d CD-ROM/DVD-ROM\n", version);
640 else
641 printf(" ATAPI-%d Device\n", version);
642 break;
643 case DSK_TYPE_UNKNOWN:
644 printf("ata%d %s: Unknown device\n", channel , slave ? " slave" : "master");
645 break;
646 }
647#endif /* !VBOX */
648 }
649 }
650
651 // Store the devices counts
652 bios_dsk->hdcount = hdcount;
653 bios_dsk->cdcount = cdcount;
654 write_byte(0x40,0x75, hdcount);
655
656#ifdef VBOX
657 // we don't want any noisy output for now
658#else /* !VBOX */
659 printf("\n");
660#endif /* !VBOX */
661
662 // FIXME : should use bios=cmos|auto|disable bits
663 // FIXME : should know about translation bits
664 // FIXME : move hard_drive_post here
665
666}
667
668// ---------------------------------------------------------------------------
669// ATA/ATAPI driver : execute a data-out command
670// ---------------------------------------------------------------------------
671 // returns
672 // 0 : no error
673 // 1 : BUSY bit set
674 // 2 : read error
675 // 3 : expected DRQ=1
676 // 4 : no sectors left to read/verify
677 // 5 : more sectors to read/verify
678 // 6 : no sectors left to write
679 // 7 : more sectors to write
680uint16_t ata_cmd_data_out(bio_dsk_t __far *bios_dsk, uint16_t command, uint16_t count)
681{
682 uint64_t lba;
683 char __far *buffer;
684 uint16_t iobase1, iobase2, blksize;
685 uint16_t cylinder;
686 uint16_t head;
687 uint16_t sector;
688 uint16_t device;
689 uint8_t channel, slave;
690 uint8_t status, mode;
691
692 device = bios_dsk->drqp.dev_id;
693 channel = device / 2;
694 slave = device % 2;
695
696 iobase1 = bios_dsk->channels[channel].iobase1;
697 iobase2 = bios_dsk->channels[channel].iobase2;
698 mode = bios_dsk->devices[device].mode;
699 blksize = 0x200; // was = bios_dsk->devices[device].blksize;
700#if VBOX_BIOS_CPU >= 80386
701 if (mode == ATA_MODE_PIO32)
702 blksize >>= 2;
703 else
704#endif
705 blksize >>= 1;
706
707 status = inb(iobase1 + ATA_CB_STAT);
708 if (status & ATA_CB_STAT_BSY)
709 {
710 // Enable interrupts
711 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
712 return 1;
713 }
714
715 lba = bios_dsk->drqp.lba;
716 buffer = bios_dsk->drqp.buffer;
717 sector = bios_dsk->drqp.sector;
718 cylinder = bios_dsk->drqp.cylinder;
719 head = bios_dsk->drqp.head;
720
721 // sector will be 0 only on lba access. Convert to lba-chs
722 if (sector == 0) {
723 if (lba + count >= 268435456)
724 {
725 sector = (lba >> 24) & 0x00ff;
726 cylinder = (lba >> 32) & 0xffff;
727 outb(iobase1 + ATA_CB_SC, (count & 0xff00) >> 8);
728 outb(iobase1 + ATA_CB_SN, sector);
729 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
730 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
731 /* Leave the bottom 24 bits as is, they are treated correctly by the
732 * LBA28 code path. */
733 lba &= 0xffffff;
734 }
735 sector = (uint16_t) (lba & 0x000000ffL);
736 lba >>= 8;
737 cylinder = (uint16_t) (lba & 0x0000ffffL);
738 lba >>= 16;
739 head = ((uint16_t) (lba & 0x0000000fL)) | 0x40;
740 }
741
742 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
743 outb(iobase1 + ATA_CB_FR, 0x00);
744 outb(iobase1 + ATA_CB_SC, count);
745 outb(iobase1 + ATA_CB_SN, sector);
746 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
747 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
748 outb(iobase1 + ATA_CB_DH, (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (uint8_t) head );
749 outb(iobase1 + ATA_CB_CMD, command);
750
751 while (1) {
752 status = inb(iobase1 + ATA_CB_STAT);
753 if ( !(status & ATA_CB_STAT_BSY) )
754 break;
755 }
756
757 if (status & ATA_CB_STAT_ERR) {
758 BX_DEBUG_ATA("%s: write error\n", __func__);
759 // Enable interrupts
760 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
761 return 2;
762 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
763 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
764 // Enable interrupts
765 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
766 return 3;
767 }
768
769 // FIXME : move seg/off translation here
770
771 int_enable(); // enable higher priority interrupts
772
773 while (1) {
774
775 // adjust if there will be an overrun. 2K max sector size
776 if (FP_OFF(buffer) >= 0xF800)
777 buffer = MK_FP(FP_SEG(buffer) + 0x80, FP_OFF(buffer) - 0x800);
778
779#if VBOX_BIOS_CPU >= 80386
780 if (mode == ATA_MODE_PIO32)
781 buffer = rep_outsd(buffer, blksize, iobase1);
782 else
783#endif
784 buffer = rep_outsw(buffer, blksize, iobase1);
785
786 bios_dsk->drqp.trsfsectors++;
787 count--;
788 while (1) {
789 status = inb(iobase1 + ATA_CB_STAT);
790 if ( !(status & ATA_CB_STAT_BSY) )
791 break;
792 }
793 if (count == 0) {
794 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
795 != ATA_CB_STAT_RDY ) {
796 BX_DEBUG_ATA("%s: no sectors left (status %02x)\n", __func__, (unsigned) status);
797 // Enable interrupts
798 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
799 return 6;
800 }
801 break;
802 }
803 else {
804 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
805 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
806 BX_DEBUG_ATA("%s: more sectors left (status %02x)\n", __func__, (unsigned) status);
807 // Enable interrupts
808 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
809 return 7;
810 }
811 continue;
812 }
813 }
814 // Enable interrupts
815 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
816 return 0;
817}
818
819
820/**
821 * Read sectors from an attached ATA device.
822 *
823 * @returns status code.
824 * @param bios_dsk Pointer to disk request packet (in the
825 * EBDA).
826 */
827int ata_read_sectors(bio_dsk_t __far *bios_dsk)
828{
829 uint16_t n_sect;
830 int status;
831 uint8_t device_id;
832
833 device_id = bios_dsk->drqp.dev_id;
834 n_sect = bios_dsk->drqp.nsect;
835
836 if (bios_dsk->drqp.sector) {
837 /* CHS addressing. */
838 bios_dsk->devices[device_id].blksize = n_sect * 0x200;
839 BX_DEBUG_ATA("%s: reading %u sectors (CHS)\n", __func__, n_sect);
840 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_MULTIPLE, n_sect);
841 bios_dsk->devices[device_id].blksize = 0x200;
842 } else {
843 /* LBA addressing. */
844 if (bios_dsk->drqp.lba + n_sect >= 268435456) {
845 BX_DEBUG_ATA("%s: reading %u sector (LBA,EXT)\n", __func__, n_sect);
846 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_SECTORS_EXT, n_sect);
847 } else {
848 bios_dsk->devices[device_id].blksize = n_sect * 0x200;
849 BX_DEBUG_ATA("%s: reading %u sector (LBA,MULT)\n", __func__, n_sect);
850 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_MULTIPLE, n_sect);
851 bios_dsk->devices[device_id].blksize = 0x200;
852 }
853 }
854 return status;
855}
856
857/**
858 * Write sectors to an attached ATA device.
859 *
860 * @returns status code.
861 * @param bios_dsk Pointer to disk request packet (in the
862 * EBDA).
863 */
864int ata_write_sectors(bio_dsk_t __far *bios_dsk)
865{
866 uint16_t n_sect;
867
868 n_sect = bios_dsk->drqp.nsect;
869
870 if (bios_dsk->drqp.sector) {
871 /* CHS addressing. */
872 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS, n_sect);
873 } else {
874 /* LBA addressing. */
875 if (bios_dsk->drqp.lba + n_sect >= 268435456)
876 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS_EXT, n_sect);
877 else
878 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS, n_sect);
879 }
880}
881
882
883// ---------------------------------------------------------------------------
884// ATA/ATAPI driver : execute a packet command
885// ---------------------------------------------------------------------------
886 // returns
887 // 0 : no error
888 // 1 : error in parameters
889 // 2 : BUSY bit set
890 // 3 : error
891 // 4 : not ready
892uint16_t ata_cmd_packet(uint16_t device, uint8_t cmdlen, char __far *cmdbuf,
893 uint16_t header, uint32_t length, uint8_t inout, char __far *buffer)
894{
895 uint16_t iobase1, iobase2;
896 uint16_t lcount, lbefore, lafter, count;
897 uint8_t channel, slave;
898 uint8_t status, mode, lmode;
899 uint32_t transfer;
900 bio_dsk_t __far *bios_dsk;
901
902 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
903
904 channel = device / 2;
905 slave = device % 2;
906
907 // Data out is not supported yet
908 if (inout == ATA_DATA_OUT) {
909 BX_INFO("%s: DATA_OUT not supported yet\n", __func__);
910 return 1;
911 }
912
913 // The header length must be even
914 if (header & 1) {
915 BX_DEBUG_ATA("%s: header must be even (%04x)\n", __func__, header);
916 return 1;
917 }
918
919 iobase1 = bios_dsk->channels[channel].iobase1;
920 iobase2 = bios_dsk->channels[channel].iobase2;
921 mode = bios_dsk->devices[device].mode;
922 transfer = 0L;
923
924 if (cmdlen < 12)
925 cmdlen = 12;
926 if (cmdlen > 12)
927 cmdlen = 16;
928 cmdlen >>= 1;
929
930 // Reset count of transferred data
931 // @todo: clear in calling code?
932 bios_dsk->drqp.trsfsectors = 0;
933 bios_dsk->drqp.trsfbytes = 0;
934
935 status = inb(iobase1 + ATA_CB_STAT);
936 if (status & ATA_CB_STAT_BSY)
937 return 2;
938
939 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
940 // outb(iobase1 + ATA_CB_FR, 0x00);
941 // outb(iobase1 + ATA_CB_SC, 0x00);
942 // outb(iobase1 + ATA_CB_SN, 0x00);
943 outb(iobase1 + ATA_CB_CL, 0xfff0 & 0x00ff);
944 outb(iobase1 + ATA_CB_CH, 0xfff0 >> 8);
945 outb(iobase1 + ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
946 outb(iobase1 + ATA_CB_CMD, ATA_CMD_PACKET);
947
948 // Device should ok to receive command
949 while (1) {
950 status = inb(iobase1 + ATA_CB_STAT);
951 if ( !(status & ATA_CB_STAT_BSY) ) break;
952 }
953
954 if (status & ATA_CB_STAT_CHK) {
955 BX_DEBUG_ATA("%s: error, status is %02x\n", __func__, status);
956 // Enable interrupts
957 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
958 return 3;
959 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
960 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
961 // Enable interrupts
962 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
963 return 4;
964 }
965
966 int_enable(); // enable higher priority interrupts
967
968 // Normalize address
969 BX_DEBUG_ATA("acp1 buffer ptr: %04x:%04x wlen %04x\n", FP_SEG(cmdbuf), FP_OFF(cmdbuf), cmdlen);
970 cmdbuf = MK_FP(FP_SEG(cmdbuf) + FP_OFF(cmdbuf) / 16 , FP_OFF(cmdbuf) % 16);
971 // cmdseg += (cmdoff / 16);
972 // cmdoff %= 16;
973
974 // Send command to device
975 rep_outsw(cmdbuf, cmdlen, iobase1);
976
977 if (inout == ATA_DATA_NO) {
978 status = inb(iobase1 + ATA_CB_STAT);
979 }
980 else {
981 while (1) {
982
983 while (1) {
984 status = inb(iobase1 + ATA_CB_STAT);
985 if ( !(status & ATA_CB_STAT_BSY) )
986 break;
987 }
988
989 // Check if command completed
990 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ) ) ==0 )
991 break;
992
993 if (status & ATA_CB_STAT_CHK) {
994 BX_DEBUG_ATA("%s: error (status %02x)\n", __func__, status);
995 // Enable interrupts
996 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
997 return 3;
998 }
999
1000 // Device must be ready to send data
1001 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1002 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
1003 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, status);
1004 // Enable interrupts
1005 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1006 return 4;
1007 }
1008
1009 // Normalize address
1010 BX_DEBUG_ATA("acp2 buffer ptr: %04x:%04x\n", FP_SEG(buffer), FP_OFF(buffer));
1011 buffer = MK_FP(FP_SEG(buffer) + FP_OFF(buffer) / 16 , FP_OFF(buffer) % 16);
1012 // bufseg += (bufoff / 16);
1013 // bufoff %= 16;
1014
1015 // Get the byte count
1016 lcount = ((uint16_t)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
1017
1018 // adjust to read what we want
1019 if (header>lcount) {
1020 lbefore = lcount;
1021 header -= lcount;
1022 lcount = 0;
1023 }
1024 else {
1025 lbefore = header;
1026 header = 0;
1027 lcount -= lbefore;
1028 }
1029
1030 if (lcount>length) {
1031 lafter = lcount - length;
1032 lcount = length;
1033 length = 0;
1034 }
1035 else {
1036 lafter = 0;
1037 length -= lcount;
1038 }
1039
1040 // Save byte count
1041 count = lcount;
1042
1043 BX_DEBUG_ATA("Trying to read %04x bytes (%04x %04x %04x) ",lbefore+lcount+lafter,lbefore,lcount,lafter);
1044 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",FP_SEG(buffer),FP_OFF(buffer));
1045
1046 // If counts not dividable by 4, use 16bits mode
1047 lmode = mode;
1048 if (lbefore & 0x03)
1049 lmode = ATA_MODE_PIO16;
1050 if (lcount & 0x03)
1051 lmode = ATA_MODE_PIO16;
1052 if (lafter & 0x03)
1053 lmode = ATA_MODE_PIO16;
1054
1055 // adds an extra byte if count are odd. before is always even
1056 if (lcount & 0x01) {
1057 lcount += 1;
1058 if ((lafter > 0) && (lafter & 0x01)) {
1059 lafter -= 1;
1060 }
1061 }
1062
1063#if VBOX_BIOS_CPU >= 80386
1064 if (lmode == ATA_MODE_PIO32) {
1065 lcount >>= 2;
1066 lbefore >>= 2;
1067 lafter >>= 2;
1068 } else
1069#endif
1070 {
1071 lcount >>= 1;
1072 lbefore >>= 1;
1073 lafter >>= 1;
1074 }
1075
1076#if VBOX_BIOS_CPU >= 80386
1077 if (lmode == ATA_MODE_PIO32) {
1078 if (lbefore)
1079 insd_discard(lbefore, iobase1);
1080 rep_insd(buffer, lcount, iobase1);
1081 if (lafter)
1082 insd_discard(lafter, iobase1);
1083 } else
1084#endif
1085 {
1086 if (lbefore)
1087 insw_discard(lbefore, iobase1);
1088 rep_insw(buffer, lcount, iobase1);
1089 if (lafter)
1090 insw_discard(lafter, iobase1);
1091 }
1092
1093
1094 // Compute new buffer address
1095 buffer += count;
1096
1097 // Save transferred bytes count
1098 transfer += count;
1099 bios_dsk->drqp.trsfbytes = transfer;
1100 }
1101 }
1102
1103 // Final check, device must be ready
1104 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1105 != ATA_CB_STAT_RDY ) {
1106 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, (unsigned) status);
1107 // Enable interrupts
1108 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1109 return 4;
1110 }
1111
1112 // Enable interrupts
1113 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1114 return 0;
1115}
1116
1117// ---------------------------------------------------------------------------
1118// ATA/ATAPI driver : reset device; intended for ATAPI devices
1119// ---------------------------------------------------------------------------
1120 // returns
1121 // 0 : no error
1122 // 1 : error
1123uint16_t ata_soft_reset(uint16_t device)
1124{
1125 uint16_t iobase1, iobase2;
1126 uint8_t channel, slave;
1127 uint8_t status;
1128 bio_dsk_t __far *bios_dsk;
1129
1130 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
1131
1132 channel = device / 2;
1133 slave = device % 2;
1134
1135 iobase1 = bios_dsk->channels[channel].iobase1;
1136 iobase2 = bios_dsk->channels[channel].iobase2;
1137
1138 /* Send a reset command to the device. */
1139 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
1140 outb(iobase1 + ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
1141 outb(iobase1 + ATA_CB_CMD, ATA_CMD_DEVICE_RESET);
1142
1143 /* Wait for the device to clear BSY. */
1144 while (1) {
1145 status = inb(iobase1 + ATA_CB_STAT);
1146 if ( !(status & ATA_CB_STAT_BSY) ) break;
1147 }
1148
1149 /* Final check, device must be ready */
1150 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1151 != ATA_CB_STAT_RDY ) {
1152 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, (unsigned) status);
1153 /* Enable interrupts */
1154 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15);
1155 return 1;
1156 }
1157
1158 /* Enable interrupts */
1159 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1160 return 0;
1161}
1162
1163
1164// ---------------------------------------------------------------------------
1165// End of ATA/ATAPI Driver
1166// ---------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.

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