VirtualBox

source: vbox/trunk/src/VBox/Devices/PC/BIOS-new/ata.c@ 39355

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

Reshuffled things some more.

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