VirtualBox

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

Last change on this file since 104067 was 104067, checked in by vboxsync, 8 weeks ago

Comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.6 KB
Line 
1/* $Id: ata.c 104067 2024-03-26 16:35:54Z vboxsync $ */
2/** @file
3 * PC BIOS - ATA disk support.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 * --------------------------------------------------------------------
27 *
28 * This code is based on:
29 *
30 * ROM BIOS for use with Bochs/Plex86/QEMU emulation environment
31 *
32 * Copyright (C) 2002 MandrakeSoft S.A.
33 *
34 * MandrakeSoft S.A.
35 * 43, rue d'Aboukir
36 * 75002 Paris - France
37 * http://www.linux-mandrake.com/
38 * http://www.mandrakesoft.com/
39 *
40 * This library is free software; you can redistribute it and/or
41 * modify it under the terms of the GNU Lesser General Public
42 * License as published by the Free Software Foundation; either
43 * version 2 of the License, or (at your option) any later version.
44 *
45 * This library is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48 * Lesser General Public License for more details.
49 *
50 * You should have received a copy of the GNU Lesser General Public
51 * License along with this library; if not, write to the Free Software
52 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
53 *
54 */
55
56/*
57 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
58 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
59 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
60 * a choice of LGPL license versions is made available with the language indicating
61 * that LGPLv2 or any later version may be used, or where a choice of which version
62 * of the LGPL is applied is otherwise unspecified.
63 */
64
65
66#include <stdint.h>
67#include <stdarg.h>
68#include "inlines.h"
69#include "biosint.h"
70#include "ebda.h"
71#include "ata.h"
72#include "pciutil.h"
73
74
75#if DEBUG_ATA
76# define BX_DEBUG_ATA(...) BX_DEBUG(__VA_ARGS__)
77#else
78# define BX_DEBUG_ATA(...)
79#endif
80
81
82// ---------------------------------------------------------------------------
83// Start of ATA/ATAPI Driver
84// ---------------------------------------------------------------------------
85
86// ---------------------------------------------------------------------------
87// ATA/ATAPI driver : initialization
88// ---------------------------------------------------------------------------
89void BIOSCALL ata_init(void)
90{
91 uint8_t channel, device;
92 bio_dsk_t __far *bios_dsk;
93
94 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
95
96 // Channels info init.
97 for (channel=0; channel<BX_MAX_ATA_INTERFACES; channel++) {
98 bios_dsk->channels[channel].iface = ATA_IFACE_NONE;
99 bios_dsk->channels[channel].iobase1 = 0x0;
100 bios_dsk->channels[channel].iobase2 = 0x0;
101 bios_dsk->channels[channel].irq = 0;
102 }
103
104 // Devices info init.
105 for (device=0; device<BX_MAX_ATA_DEVICES; device++) {
106 bios_dsk->devices[device].type = DSK_TYPE_NONE;
107 bios_dsk->devices[device].device = DSK_DEVICE_NONE;
108 bios_dsk->devices[device].removable = 0;
109 bios_dsk->devices[device].lock = 0;
110 bios_dsk->devices[device].mode = ATA_MODE_NONE;
111 bios_dsk->devices[device].blksize = 0x200;
112 bios_dsk->devices[device].translation = GEO_TRANSLATION_NONE;
113 bios_dsk->devices[device].lchs.heads = 0;
114 bios_dsk->devices[device].lchs.cylinders = 0;
115 bios_dsk->devices[device].lchs.spt = 0;
116 bios_dsk->devices[device].pchs.heads = 0;
117 bios_dsk->devices[device].pchs.cylinders = 0;
118 bios_dsk->devices[device].pchs.spt = 0;
119 bios_dsk->devices[device].sectors = 0;
120 }
121
122 // hdidmap and cdidmap init.
123 for (device=0; device<BX_MAX_STORAGE_DEVICES; device++) {
124 bios_dsk->hdidmap[device] = BX_MAX_STORAGE_DEVICES;
125 bios_dsk->cdidmap[device] = BX_MAX_STORAGE_DEVICES;
126 }
127
128 bios_dsk->hdcount = 0;
129 bios_dsk->cdcount = 0;
130}
131
132// ---------------------------------------------------------------------------
133// ATA/ATAPI driver : software reset
134// ---------------------------------------------------------------------------
135// ATA-3
136// 8.2.1 Software reset - Device 0
137
138void ata_reset(uint16_t device)
139{
140 uint16_t iobase1, iobase2;
141 uint8_t channel, slave, sn, sc;
142 uint16_t max;
143 uint16_t pdelay;
144 bio_dsk_t __far *bios_dsk;
145
146 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
147 channel = device / 2;
148 slave = device % 2;
149
150 iobase1 = bios_dsk->channels[channel].iobase1;
151 iobase2 = bios_dsk->channels[channel].iobase2;
152
153 // Reset
154
155 // 8.2.1 (a) -- set SRST in DC
156 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST);
157
158 // 8.2.1 (b) -- wait for BSY
159 max=0xff;
160 while(--max>0) {
161 uint8_t status = inb(iobase1+ATA_CB_STAT);
162 if ((status & ATA_CB_STAT_BSY) != 0)
163 break;
164 }
165
166 // 8.2.1 (f) -- clear SRST
167 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
168
169 // 8.2.1 (h) -- wait for not BSY
170 max=0xffff; /* The ATA specification says that the drive may be busy for up to 30 seconds. */
171 while(--max>0) {
172 uint8_t status = inb(iobase1+ATA_CB_STAT);
173 if ((status & ATA_CB_STAT_BSY) == 0)
174 break;
175 pdelay=0xffff;
176 while (--pdelay>0) {
177 /* nothing */
178 }
179 }
180
181 if (bios_dsk->devices[device].type != DSK_TYPE_NONE) {
182 // 8.2.1 (g) -- check for sc==sn==0x01
183 // select device
184 outb(iobase1+ATA_CB_DH, slave?ATA_CB_DH_DEV1:ATA_CB_DH_DEV0);
185 sc = inb(iobase1+ATA_CB_SC);
186 sn = inb(iobase1+ATA_CB_SN);
187
188 if ( (sc==0x01) && (sn==0x01) ) {
189 // 8.2.1 (i) -- wait for DRDY
190 max = 0x10; /* Speed up for virtual drives. Disks are immediately ready, CDs never */
191 while(--max>0) {
192 uint8_t status = inb(iobase1+ATA_CB_STAT);
193 if ((status & ATA_CB_STAT_RDY) != 0)
194 break;
195 }
196 }
197 }
198
199 // Enable interrupts
200 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
201}
202
203// ---------------------------------------------------------------------------
204// ATA/ATAPI driver : execute a data-in command
205// ---------------------------------------------------------------------------
206 // returns
207 // 0 : no error
208 // 1 : BUSY bit set
209 // 2 : read error
210 // 3 : expected DRQ=1
211 // 4 : no sectors left to read/verify
212 // 5 : more sectors to read/verify
213 // 6 : no sectors left to write
214 // 7 : more sectors to write
215uint16_t ata_cmd_data_in(bio_dsk_t __far *bios_dsk, uint16_t command, uint16_t count)
216{
217 uint16_t iobase1, iobase2, blksize, mult_blk_cnt;
218 uint16_t cylinder;
219 uint8_t head;
220 uint8_t sector;
221 uint8_t device;
222 uint8_t status, mode;
223 char __far *buffer;
224
225 device = bios_dsk->drqp.dev_id;
226
227 iobase1 = bios_dsk->channels[device / 2].iobase1;
228 iobase2 = bios_dsk->channels[device / 2].iobase2;
229 mode = bios_dsk->devices[device].mode;
230 blksize = bios_dsk->devices[device].blksize;
231 if (blksize == 0) { /* If transfer size is exactly 64K */
232#if VBOX_BIOS_CPU >= 80386
233 if (mode == ATA_MODE_PIO32)
234 blksize = 0x4000;
235 else
236#endif
237 blksize = 0x8000;
238 } else {
239#if VBOX_BIOS_CPU >= 80386
240 if (mode == ATA_MODE_PIO32)
241 blksize >>= 2;
242 else
243#endif
244 blksize >>= 1;
245 }
246
247 status = inb(iobase1 + ATA_CB_STAT);
248 if (status & ATA_CB_STAT_BSY)
249 {
250 BX_DEBUG_ATA("%s: disk busy\n", __func__);
251 // Enable interrupts
252 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
253 return 1;
254 }
255
256 buffer = bios_dsk->drqp.buffer;
257 sector = bios_dsk->drqp.sector;
258 cylinder = bios_dsk->drqp.cylinder;
259 head = bios_dsk->drqp.head;
260
261 // sector will be 0 only on lba access. Convert to lba-chs
262 if (sector == 0) {
263 if (bios_dsk->drqp.lba + count >= 268435456)
264 {
265 sector = (bios_dsk->drqp.lba >> 24) & 0x00ff;
266 cylinder = (bios_dsk->drqp.lba >> 32) & 0xffff;
267 outb(iobase1 + ATA_CB_SC, (count & 0xff00) >> 8);
268 outb(iobase1 + ATA_CB_SN, sector);
269 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
270 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
271 /* Leave the bottom 24 bits as is, they are treated correctly by the
272 * LBA28 code path. */
273 }
274 sector = bios_dsk->drqp.lba & 0x000000ffL;
275 cylinder = (bios_dsk->drqp.lba >> 8) & 0x0000ffffL;
276 head = ((bios_dsk->drqp.lba >> 24) & 0x0000000fL) | 0x40;
277 }
278
279 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
280 outb(iobase1 + ATA_CB_FR, 0x00);
281 outb(iobase1 + ATA_CB_SC, count);
282 outb(iobase1 + ATA_CB_SN, sector);
283 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
284 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
285 outb(iobase1 + ATA_CB_DH, ((device & 1) ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | head );
286 outb(iobase1 + ATA_CB_CMD, command);
287
288 if (command == ATA_CMD_READ_MULTIPLE || command == ATA_CMD_READ_MULTIPLE_EXT) {
289 mult_blk_cnt = count;
290 count = 1;
291 } else {
292 mult_blk_cnt = 1;
293 }
294
295 while (1) {
296 status = inb(iobase1 + ATA_CB_STAT);
297 if ( !(status & ATA_CB_STAT_BSY) )
298 break;
299 }
300
301 if (status & ATA_CB_STAT_ERR) {
302 BX_DEBUG_ATA("%s: read error\n", __func__);
303 // Enable interrupts
304 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
305 return 2;
306 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
307 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
308 // Enable interrupts
309 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
310 return 3;
311 }
312
313 // FIXME : move seg/off translation here
314
315 int_enable(); // enable higher priority interrupts
316
317 while (1) {
318
319 // adjust if there will be an overrun. 2K max sector size
320 if (FP_OFF(buffer) >= 0xF800)
321 buffer = MK_FP(FP_SEG(buffer) + 0x80, FP_OFF(buffer) - 0x800);
322
323#if VBOX_BIOS_CPU >= 80386
324 if (mode == ATA_MODE_PIO32)
325 buffer = rep_insd(buffer, blksize, iobase1);
326 else
327#endif
328 buffer = rep_insw(buffer, blksize, iobase1);
329 bios_dsk->drqp.trsfsectors += mult_blk_cnt;
330 count--;
331 while (1) {
332 status = inb(iobase1 + ATA_CB_STAT);
333 if ( !(status & ATA_CB_STAT_BSY) )
334 break;
335 }
336 if (count == 0) {
337 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
338 != ATA_CB_STAT_RDY ) {
339 BX_DEBUG_ATA("%s: no sectors left (status %02x)\n", __func__, (unsigned) status);
340 // Enable interrupts
341 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
342 return 4;
343 }
344 break;
345 }
346 else {
347 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
348 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
349 BX_DEBUG_ATA("%s: more sectors left (status %02x)\n", __func__, (unsigned) status);
350 // Enable interrupts
351 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
352 return 5;
353 }
354 continue;
355 }
356 }
357 // Enable interrupts
358 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
359 return 0;
360}
361
362// ---------------------------------------------------------------------------
363// ATA/ATAPI driver : device detection
364// ---------------------------------------------------------------------------
365
366int ata_signature(uint16_t iobase1, uint8_t channel, uint8_t slave)
367{
368 int dsk_type = DSK_TYPE_NONE;
369 uint8_t sc, sn, st, cl, ch;
370
371 /*
372 * Wait for BSY=0 so that the signature can be read. We already determined that
373 * an ATA interface is present, and rely on the fact that for non-existent
374 * devices, the BSY bit will always be clear.
375 */
376 outb(iobase1+ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
377 do {
378 st = inb(iobase1+ATA_CB_STAT);
379 } while (st & ATA_CB_STAT_BSY);
380
381 /*
382 * Look for ATA/ATAPI signature. Fun Fact #1: If there's a Device 1 but no
383 * Device 0, Device 1 can't tell and does not respond for it. Accessing
384 * non-existent Device 0 behaves the same regardless of whether Device 1
385 * is present or not.
386 */
387 sc = inb(iobase1+ATA_CB_SC);
388 sn = inb(iobase1+ATA_CB_SN);
389 if ((sc == 1) && (sn == 1)) {
390 cl = inb(iobase1+ATA_CB_CL);
391 ch = inb(iobase1+ATA_CB_CH);
392
393 /*
394 * Fun fact #2: If Device 0 responds for Device 1, an ATA device generally
395 * returns the values of its own registers, while an ATAPI device returns
396 * zeros. In both cases, the Status register is read as zero.
397 */
398 if ((cl == 0x14) && (ch == 0xEB)) {
399 dsk_type = DSK_TYPE_ATAPI;
400 BX_DEBUG_ATA("ata%d-%d: ATAPI device\n", channel, slave);
401 } else if ((cl == 0) && (ch == 0)) {
402 if (st != 0) {
403 dsk_type = DSK_TYPE_ATA;
404 BX_DEBUG_ATA("ata%d-%d: ATA device\n", channel, slave);
405 } else {
406 BX_DEBUG_ATA("ata%d-%d: ATA master responding for slave\n", channel, slave);
407 }
408 } else {
409 dsk_type = DSK_TYPE_UNKNOWN;
410 BX_DEBUG_ATA("ata%d-%d: something else (%02X/%02X/%02X)\n", channel, slave, cl, ch, st);
411 }
412 } else /* Possibly ATAPI Device 0 responding for Device 1. */
413 BX_DEBUG_ATA("ata%d-%d: bad sc/sn signature (%02X/%02X)\n", channel, slave, sc, sn);
414
415 return dsk_type;
416}
417
418void BIOSCALL ata_detect(void)
419{
420 uint16_t ebda_seg = read_word(0x0040,0x000E);
421 uint8_t hdcount, cdcount, device, type;
422 uint8_t buffer[0x0200];
423 bio_dsk_t __far *bios_dsk;
424
425 /* If we have PCI support, look for an IDE controller (it has to be a PCI device)
426 * so that we can skip silly probing. If there's no PCI, assume IDE is present.
427 *
428 * Needs an internal PCI function because the Programming Interface byte can be
429 * almost anything, and we conly care about the base-class and sub-class code.
430 */
431#if VBOX_BIOS_CPU >= 80386
432 uint16_t busdevfn;
433
434 busdevfn = pci_find_class_noif(0x0101);
435 if (busdevfn == 0xffff) {
436 BX_INFO("No PCI IDE controller, not probing IDE\n");
437 return;
438 }
439#endif
440
441 bios_dsk = ebda_seg :> &EbdaData->bdisk;
442
443#if BX_MAX_ATA_INTERFACES > 0
444 bios_dsk->channels[0].iface = ATA_IFACE_ISA;
445 bios_dsk->channels[0].iobase1 = 0x1f0;
446 bios_dsk->channels[0].iobase2 = 0x3f0;
447 bios_dsk->channels[0].irq = 14;
448#endif
449#if BX_MAX_ATA_INTERFACES > 1
450 bios_dsk->channels[1].iface = ATA_IFACE_ISA;
451 bios_dsk->channels[1].iobase1 = 0x170;
452 bios_dsk->channels[1].iobase2 = 0x370;
453 bios_dsk->channels[1].irq = 15;
454#endif
455#if BX_MAX_ATA_INTERFACES > 2
456 bios_dsk->channels[2].iface = ATA_IFACE_ISA;
457 bios_dsk->channels[2].iobase1 = 0x1e8;
458 bios_dsk->channels[2].iobase2 = 0x3e0;
459 bios_dsk->channels[2].irq = 12;
460#endif
461#if BX_MAX_ATA_INTERFACES > 3
462 bios_dsk->channels[3].iface = ATA_IFACE_ISA;
463 bios_dsk->channels[3].iobase1 = 0x168;
464 bios_dsk->channels[3].iobase2 = 0x360;
465 bios_dsk->channels[3].irq = 11;
466#endif
467#if BX_MAX_ATA_INTERFACES > 4
468#error Please fill the ATA interface information
469#endif
470
471 // Device detection
472 hdcount = cdcount = 0;
473
474 for (device = 0; device < BX_MAX_ATA_DEVICES; device++) {
475 uint16_t iobase1, iobase2;
476 uint16_t retries;
477 uint8_t channel, slave;
478 uint8_t st;
479
480 channel = device / 2;
481 slave = device % 2;
482
483 iobase1 = bios_dsk->channels[channel].iobase1;
484 iobase2 = bios_dsk->channels[channel].iobase2;
485
486 /*
487 * Here we are in a tricky situation. We do not know if an ATA
488 * interface is even present at a given address. If it is present,
489 * we don't know if a device is present. We also need to consider
490 * the case of only a slave device being present, which does not
491 * respond for the missing master device. If a device is present,
492 * it may be still powering up or processing reset, which means it
493 * may be busy.
494 *
495 * If a device is busy, we can't reliably write any registers, and
496 * reads will return the Status register. If the Status register
497 * value is 0FFh, there might be no ATA controller at all, or it
498 * might be a busy drive. Fortunately we know that our own devices
499 * never return such a value when busy, and we use that knowledge
500 * to detect non-existent interfaces.
501 *
502 * We also know that our ATA interface will not return 0FFh even when
503 * no device is present on a given channel. This knowledge is handy
504 * when only a slave device exists because we won't read 0FFh and
505 * think there is no ATA interface at all.
506 */
507
508 st = inb(iobase1+ATA_CB_STAT);
509 BX_DEBUG_ATA("ata%d-%d: Status=%02X\n", channel, slave, st);
510 if (st == 0xff)
511 continue;
512
513 /*
514 * Perform a software reset by setting and clearing the SRST bit. This
515 * can be done at any time, and forces device signature into the task file
516 * registers. If present, both devices are reset at once, so we only do
517 * this once per channel.
518 */
519 if (!slave) {
520 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN | ATA_CB_DC_SRST);
521
522 /*
523 * Ensure reasonable SRST pulse width, but do not wait long for
524 * non-existent devices.
525 */
526 retries = 32;
527 while (--retries > 0) {
528 st = inb(iobase1+ATA_CB_STAT);
529 if (st & ATA_CB_STAT_BSY)
530 break;
531 }
532
533 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
534
535 /* After reset, device signature will be placed in registers. But
536 * executing any commands will overwrite it for Device 1. So that
537 * we don't have to reset twice, look for both Device 0 and Device 1
538 * signatures here right after reset.
539 */
540 bios_dsk->devices[device + 0].type = ata_signature(iobase1, channel, 0);
541 bios_dsk->devices[device + 1].type = ata_signature(iobase1, channel, 1);
542 }
543
544 // Enable interrupts
545 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
546
547 type = bios_dsk->devices[device].type;
548
549 // Now we send a IDENTIFY command to ATA device
550 if (type == DSK_TYPE_ATA) {
551 uint64_t sectors;
552 uint16_t cylinders, heads, spt, blksize;
553 chs_t lgeo;
554 uint8_t chsgeo_base;
555 uint8_t removable, mode;
556
557 //Temporary values to do the transfer
558 bios_dsk->devices[device].device = DSK_DEVICE_HD;
559 bios_dsk->devices[device].mode = ATA_MODE_PIO16;
560 bios_dsk->drqp.buffer = buffer;
561 bios_dsk->drqp.dev_id = device;
562
563 if (ata_cmd_data_in(bios_dsk, ATA_CMD_IDENTIFY_DEVICE, 1) !=0 )
564 BX_PANIC("ata-detect: Failed to detect ATA device\n");
565
566 removable = (*(buffer+0) & 0x80) ? 1 : 0;
567#if VBOX_BIOS_CPU >= 80386
568 mode = *(buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
569#else
570 mode = ATA_MODE_PIO16;
571#endif
572 blksize = 512; /* There is no sector size field any more. */
573
574 cylinders = *(uint16_t *)(buffer+(1*2)); // word 1
575 heads = *(uint16_t *)(buffer+(3*2)); // word 3
576 spt = *(uint16_t *)(buffer+(6*2)); // word 6
577
578 sectors = *(uint32_t *)(buffer+(60*2)); // word 60 and word 61
579 if (sectors == 0x0FFFFFFF) /* For disks bigger than ~128GB */
580 sectors = *(uint64_t *)(buffer+(100*2)); // words 100 to 103
581 switch (device)
582 {
583 case 0:
584 chsgeo_base = 0x1e;
585 break;
586 case 1:
587 chsgeo_base = 0x26;
588 break;
589 case 2:
590 chsgeo_base = 0x67;
591 break;
592 case 3:
593 chsgeo_base = 0x70;
594 break;
595 default:
596 chsgeo_base = 0;
597 }
598 if (chsgeo_base)
599 {
600 lgeo.cylinders = get_cmos_word(chsgeo_base /*, chsgeo_base + 1*/);
601 lgeo.heads = inb_cmos(chsgeo_base + 2);
602 lgeo.spt = inb_cmos(chsgeo_base + 7);
603 }
604 else
605 set_geom_lba(&lgeo, sectors); /* Default EDD-style translated LBA geometry. */
606
607 BX_INFO("ata%d-%d: PCHS=%u/%u/%u LCHS=%u/%u/%u\n", channel, slave,
608 cylinders, heads, spt, lgeo.cylinders, lgeo.heads, lgeo.spt);
609
610 bios_dsk->devices[device].device = DSK_DEVICE_HD;
611 bios_dsk->devices[device].removable = removable;
612 bios_dsk->devices[device].mode = mode;
613 bios_dsk->devices[device].blksize = blksize;
614 bios_dsk->devices[device].pchs.heads = heads;
615 bios_dsk->devices[device].pchs.cylinders = cylinders;
616 bios_dsk->devices[device].pchs.spt = spt;
617 bios_dsk->devices[device].sectors = sectors;
618 bios_dsk->devices[device].lchs = lgeo;
619 if (device < 2)
620 {
621 uint8_t sum, i;
622 fdpt_t __far *fdpt;
623 void __far * __far *int_vec;
624
625 if (device == 0)
626 fdpt = ebda_seg :> &EbdaData->fdpt0;
627 else
628 fdpt = ebda_seg :> &EbdaData->fdpt1;
629
630 /* Set the INT 41h or 46h pointer. */
631 int_vec = MK_FP(0, (0x41 + device * 5) * sizeof(void __far *));
632 *int_vec = fdpt;
633
634 /* Update the DPT for drive 0/1 pointed to by Int41/46. This used
635 * to be done at POST time with lots of ugly assembler code, which
636 * isn't worth the effort of converting from AMI to Award CMOS
637 * format. Just do it here. */
638 fdpt->resvd1 = fdpt->resvd2 = 0;
639
640 fdpt->lcyl = lgeo.cylinders;
641 fdpt->lhead = lgeo.heads;
642 fdpt->sig = 0xa0;
643 fdpt->spt = spt;
644 fdpt->cyl = cylinders;
645 fdpt->head = heads;
646 fdpt->lspt = lgeo.spt;
647 sum = 0;
648 for (i = 0; i < 0xf; i++)
649 sum += *((uint8_t __far *)fdpt + i);
650 sum = -sum;
651 fdpt->csum = sum;
652
653 /* Read the drive type from the CMOS. If it is one of the old
654 * IBM compatible drive types (rather than Type 47 or so), point
655 * INT 41h/46h at the drive table in ROM.
656 * This is required for some old guests which look at INT 41h/46h,
657 * but either insist that it points well above 640K (NetWare 2.x)
658 * or wipe out all RAM (286 XENIX 2.1.3/2.2.1).
659 *
660 * NB: Writing into the F000 segment and storing the FDPT there
661 * would also solve some of these problems.
662 */
663 i = inb_cmos(0x12);
664 i >>= ((1 - device) * 4);
665 i &= 0x0f;
666 if (i == 0xf)
667 i = inb_cmos(0x19 + device);
668
669 if (i <= 23) { // Should be in sync with DevPcBios.cpp and orgs.asm
670 fdpt = MK_FP(0xF000, 0xE401);
671 fdpt += i - 1;
672 *int_vec = fdpt;
673 }
674 }
675
676 // fill hdidmap
677 bios_dsk->hdidmap[hdcount] = device;
678 hdcount++;
679 }
680
681 // Now we send an IDENTIFY command to ATAPI device
682 if (type == DSK_TYPE_ATAPI) {
683 uint8_t type, removable, mode;
684 uint16_t blksize;
685
686 // Temporary values to do the transfer
687 bios_dsk->devices[device].device = DSK_DEVICE_CDROM;
688 bios_dsk->devices[device].mode = ATA_MODE_PIO16;
689 bios_dsk->drqp.buffer = buffer;
690 bios_dsk->drqp.dev_id = device;
691
692 if (ata_cmd_data_in(bios_dsk, ATA_CMD_IDENTIFY_PACKET, 1) != 0)
693 BX_PANIC("ata-detect: Failed to detect ATAPI device\n");
694
695 type = *(buffer+1) & 0x1f;
696 removable = (*(buffer+0) & 0x80) ? 1 : 0;
697#if VBOX_BIOS_CPU >= 80386
698 mode = *(buffer+96) ? ATA_MODE_PIO32 : ATA_MODE_PIO16;
699#else
700 mode = ATA_MODE_PIO16;
701#endif
702 blksize = 2048;
703
704 bios_dsk->devices[device].device = type;
705 bios_dsk->devices[device].removable = removable;
706 bios_dsk->devices[device].mode = mode;
707 bios_dsk->devices[device].blksize = blksize;
708
709 // fill cdidmap
710 bios_dsk->cdidmap[cdcount] = device;
711 cdcount++;
712 }
713
714 {
715 uint32_t sizeinmb;
716 uint16_t ataversion;
717 uint8_t version, model[41];
718 int i;
719
720 switch (type) {
721 case DSK_TYPE_ATA:
722 sizeinmb = (bios_dsk->devices[device].sectors >> 11);
723 case DSK_TYPE_ATAPI:
724 // Read ATA/ATAPI version
725 ataversion = ((uint16_t)(*(buffer+161))<<8) | *(buffer+160);
726 for (version = 15; version > 0; version--) {
727 if ((ataversion & (1 << version)) !=0 )
728 break;
729 }
730
731 // Read model name
732 for (i = 0; i < 20; i++ ) {
733 *(model+(i*2)) = *(buffer+(i*2)+54+1);
734 *(model+(i*2)+1) = *(buffer+(i*2)+54);
735 }
736
737 // Reformat
738 *(model+40) = 0x00;
739 for ( i = 39; i > 0; i-- ){
740 if (*(model+i) == 0x20)
741 *(model+i) = 0x00;
742 else
743 break;
744 }
745 break;
746 }
747
748#ifdef VBOXz
749 // we don't want any noisy output for now
750#else /* !VBOX */
751 switch (type) {
752 int c;
753 case DSK_TYPE_ATA:
754 printf("ata%d %s: ", channel, slave ? " slave" : "master");
755 i=0;
756 while(c=*(model+i++))
757 printf("%c", c);
758 printf(" ATA-%d Hard-Disk (%lu MBytes)\n", version, sizeinmb);
759 break;
760 case DSK_TYPE_ATAPI:
761 printf("ata%d %s: ", channel, slave ? " slave" : "master");
762 i=0;
763 while(c=*(model+i++))
764 printf("%c", c);
765 if (bios_dsk->devices[device].device == DSK_DEVICE_CDROM)
766 printf(" ATAPI-%d CD-ROM/DVD-ROM\n", version);
767 else
768 printf(" ATAPI-%d Device\n", version);
769 break;
770 case DSK_TYPE_UNKNOWN:
771 printf("ata%d %s: Unknown device\n", channel , slave ? " slave" : "master");
772 break;
773 }
774#endif /* !VBOX */
775 }
776 }
777
778 // Store the devices counts
779 bios_dsk->hdcount = hdcount;
780 bios_dsk->cdcount = cdcount;
781 write_byte(0x40,0x75, hdcount);
782
783#ifdef VBOX
784 // we don't want any noisy output for now
785#else /* !VBOX */
786 printf("\n");
787#endif /* !VBOX */
788
789 // FIXME : should use bios=cmos|auto|disable bits
790 // FIXME : should know about translation bits
791 // FIXME : move hard_drive_post here
792
793}
794
795// ---------------------------------------------------------------------------
796// ATA/ATAPI driver : execute a data-out command
797// ---------------------------------------------------------------------------
798 // returns
799 // 0 : no error
800 // 1 : BUSY bit set
801 // 2 : read error
802 // 3 : expected DRQ=1
803 // 4 : no sectors left to read/verify
804 // 5 : more sectors to read/verify
805 // 6 : no sectors left to write
806 // 7 : more sectors to write
807uint16_t ata_cmd_data_out(bio_dsk_t __far *bios_dsk, uint16_t command, uint16_t count)
808{
809 uint64_t lba;
810 char __far *buffer;
811 uint16_t iobase1, iobase2, blksize;
812 uint16_t cylinder;
813 uint16_t head;
814 uint16_t sector;
815 uint16_t device;
816 uint8_t channel, slave;
817 uint8_t status, mode;
818
819 device = bios_dsk->drqp.dev_id;
820 channel = device / 2;
821 slave = device % 2;
822
823 iobase1 = bios_dsk->channels[channel].iobase1;
824 iobase2 = bios_dsk->channels[channel].iobase2;
825 mode = bios_dsk->devices[device].mode;
826 blksize = 0x200; // was = bios_dsk->devices[device].blksize;
827#if VBOX_BIOS_CPU >= 80386
828 if (mode == ATA_MODE_PIO32)
829 blksize >>= 2;
830 else
831#endif
832 blksize >>= 1;
833
834 status = inb(iobase1 + ATA_CB_STAT);
835 if (status & ATA_CB_STAT_BSY)
836 {
837 // Enable interrupts
838 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
839 return 1;
840 }
841
842 lba = bios_dsk->drqp.lba;
843 buffer = bios_dsk->drqp.buffer;
844 sector = bios_dsk->drqp.sector;
845 cylinder = bios_dsk->drqp.cylinder;
846 head = bios_dsk->drqp.head;
847
848 // sector will be 0 only on lba access. Convert to lba-chs
849 if (sector == 0) {
850 if (lba + count >= 268435456)
851 {
852 sector = (lba >> 24) & 0x00ff;
853 cylinder = (lba >> 32) & 0xffff;
854 outb(iobase1 + ATA_CB_SC, (count & 0xff00) >> 8);
855 outb(iobase1 + ATA_CB_SN, sector);
856 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
857 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
858 /* Leave the bottom 24 bits as is, they are treated correctly by the
859 * LBA28 code path. */
860 lba &= 0xffffff;
861 }
862 sector = (uint16_t) (lba & 0x000000ffL);
863 lba >>= 8;
864 cylinder = (uint16_t) (lba & 0x0000ffffL);
865 lba >>= 16;
866 head = ((uint16_t) (lba & 0x0000000fL)) | 0x40;
867 }
868
869 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
870 outb(iobase1 + ATA_CB_FR, 0x00);
871 outb(iobase1 + ATA_CB_SC, count);
872 outb(iobase1 + ATA_CB_SN, sector);
873 outb(iobase1 + ATA_CB_CL, cylinder & 0x00ff);
874 outb(iobase1 + ATA_CB_CH, cylinder >> 8);
875 outb(iobase1 + ATA_CB_DH, (slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0) | (uint8_t) head );
876 outb(iobase1 + ATA_CB_CMD, command);
877
878 while (1) {
879 status = inb(iobase1 + ATA_CB_STAT);
880 if ( !(status & ATA_CB_STAT_BSY) )
881 break;
882 }
883
884 if (status & ATA_CB_STAT_ERR) {
885 BX_DEBUG_ATA("%s: write error\n", __func__);
886 // Enable interrupts
887 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
888 return 2;
889 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
890 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
891 // Enable interrupts
892 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
893 return 3;
894 }
895
896 // FIXME : move seg/off translation here
897
898 int_enable(); // enable higher priority interrupts
899
900 while (1) {
901
902 // adjust if there will be an overrun. 2K max sector size
903 if (FP_OFF(buffer) >= 0xF800)
904 buffer = MK_FP(FP_SEG(buffer) + 0x80, FP_OFF(buffer) - 0x800);
905
906#if VBOX_BIOS_CPU >= 80386
907 if (mode == ATA_MODE_PIO32)
908 buffer = rep_outsd(buffer, blksize, iobase1);
909 else
910#endif
911 buffer = rep_outsw(buffer, blksize, iobase1);
912
913 bios_dsk->drqp.trsfsectors++;
914 count--;
915 while (1) {
916 status = inb(iobase1 + ATA_CB_STAT);
917 if ( !(status & ATA_CB_STAT_BSY) )
918 break;
919 }
920 if (count == 0) {
921 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
922 != ATA_CB_STAT_RDY ) {
923 BX_DEBUG_ATA("%s: no sectors left (status %02x)\n", __func__, (unsigned) status);
924 // Enable interrupts
925 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
926 return 6;
927 }
928 break;
929 }
930 else {
931 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_ERR) )
932 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
933 BX_DEBUG_ATA("%s: more sectors left (status %02x)\n", __func__, (unsigned) status);
934 // Enable interrupts
935 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
936 return 7;
937 }
938 continue;
939 }
940 }
941 // Enable interrupts
942 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
943 return 0;
944}
945
946
947/**
948 * Read sectors from an attached ATA device.
949 *
950 * @returns status code.
951 * @param bios_dsk Pointer to disk request packet (in the
952 * EBDA).
953 */
954int ata_read_sectors(bio_dsk_t __far *bios_dsk)
955{
956 uint16_t n_sect;
957 int status;
958 uint8_t device_id;
959
960 device_id = bios_dsk->drqp.dev_id;
961 n_sect = bios_dsk->drqp.nsect;
962
963 if (bios_dsk->drqp.sector) {
964 /* CHS addressing. */
965 bios_dsk->devices[device_id].blksize = n_sect * 0x200;
966 BX_DEBUG_ATA("%s: reading %u sectors (CHS)\n", __func__, n_sect);
967 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_MULTIPLE, n_sect);
968 bios_dsk->devices[device_id].blksize = 0x200;
969 } else {
970 /* LBA addressing. */
971 if (bios_dsk->drqp.lba + n_sect >= 268435456) {
972 BX_DEBUG_ATA("%s: reading %u sector (LBA,EXT)\n", __func__, n_sect);
973 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_SECTORS_EXT, n_sect);
974 } else {
975 bios_dsk->devices[device_id].blksize = n_sect * 0x200;
976 BX_DEBUG_ATA("%s: reading %u sector (LBA,MULT)\n", __func__, n_sect);
977 status = ata_cmd_data_in(bios_dsk, ATA_CMD_READ_MULTIPLE, n_sect);
978 bios_dsk->devices[device_id].blksize = 0x200;
979 }
980 }
981 return status;
982}
983
984/**
985 * Write sectors to an attached ATA device.
986 *
987 * @returns status code.
988 * @param bios_dsk Pointer to disk request packet (in the
989 * EBDA).
990 */
991int ata_write_sectors(bio_dsk_t __far *bios_dsk)
992{
993 uint16_t n_sect;
994
995 n_sect = bios_dsk->drqp.nsect;
996
997 if (bios_dsk->drqp.sector) {
998 /* CHS addressing. */
999 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS, n_sect);
1000 } else {
1001 /* LBA addressing. */
1002 if (bios_dsk->drqp.lba + n_sect >= 268435456)
1003 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS_EXT, n_sect);
1004 else
1005 return ata_cmd_data_out(bios_dsk, ATA_CMD_WRITE_SECTORS, n_sect);
1006 }
1007}
1008
1009
1010// ---------------------------------------------------------------------------
1011// ATA/ATAPI driver : execute a packet command
1012// ---------------------------------------------------------------------------
1013 // returns
1014 // 0 : no error
1015 // 1 : error in parameters
1016 // 2 : BUSY bit set
1017 // 3 : error
1018 // 4 : not ready
1019uint16_t ata_cmd_packet(uint16_t device, uint8_t cmdlen, char __far *cmdbuf,
1020 uint32_t length, uint8_t inout, char __far *buffer)
1021{
1022 uint16_t iobase1, iobase2;
1023 uint16_t lcount, count;
1024 uint8_t channel, slave;
1025 uint8_t status, mode, lmode;
1026 uint32_t transfer;
1027 bio_dsk_t __far *bios_dsk;
1028
1029 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
1030
1031 channel = device / 2;
1032 slave = device % 2;
1033
1034 // Data out is not supported yet
1035 if (inout == ATA_DATA_OUT) {
1036 BX_INFO("%s: DATA_OUT not supported yet\n", __func__);
1037 return 1;
1038 }
1039
1040 iobase1 = bios_dsk->channels[channel].iobase1;
1041 iobase2 = bios_dsk->channels[channel].iobase2;
1042 mode = bios_dsk->devices[device].mode;
1043 transfer = 0L;
1044
1045 if (cmdlen < 12)
1046 cmdlen = 12;
1047 if (cmdlen > 12)
1048 cmdlen = 16;
1049 cmdlen >>= 1;
1050
1051 // Reset count of transferred data
1052 /// @todo clear in calling code?
1053 bios_dsk->drqp.trsfsectors = 0;
1054 bios_dsk->drqp.trsfbytes = 0;
1055
1056 status = inb(iobase1 + ATA_CB_STAT);
1057 if (status & ATA_CB_STAT_BSY)
1058 return 2;
1059
1060 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
1061 // outb(iobase1 + ATA_CB_FR, 0x00);
1062 // outb(iobase1 + ATA_CB_SC, 0x00);
1063 // outb(iobase1 + ATA_CB_SN, 0x00);
1064 outb(iobase1 + ATA_CB_CL, 0xfff0 & 0x00ff);
1065 outb(iobase1 + ATA_CB_CH, 0xfff0 >> 8);
1066 outb(iobase1 + ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
1067 outb(iobase1 + ATA_CB_CMD, ATA_CMD_PACKET);
1068
1069 // Device should ok to receive command
1070 while (1) {
1071 status = inb(iobase1 + ATA_CB_STAT);
1072 if ( !(status & ATA_CB_STAT_BSY) ) break;
1073 }
1074
1075 if (status & ATA_CB_STAT_CHK) {
1076 BX_DEBUG_ATA("%s: error, status is %02x\n", __func__, status);
1077 // Enable interrupts
1078 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1079 return 3;
1080 } else if ( !(status & ATA_CB_STAT_DRQ) ) {
1081 BX_DEBUG_ATA("%s: DRQ not set (status %02x)\n", __func__, (unsigned) status);
1082 // Enable interrupts
1083 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1084 return 4;
1085 }
1086
1087 int_enable(); // enable higher priority interrupts
1088
1089 // Normalize address
1090 BX_DEBUG_ATA("acp1 buffer ptr: %04x:%04x wlen %04x\n", FP_SEG(cmdbuf), FP_OFF(cmdbuf), cmdlen);
1091 cmdbuf = MK_FP(FP_SEG(cmdbuf) + FP_OFF(cmdbuf) / 16 , FP_OFF(cmdbuf) % 16);
1092 // cmdseg += (cmdoff / 16);
1093 // cmdoff %= 16;
1094
1095 // Send command to device
1096 rep_outsw(cmdbuf, cmdlen, iobase1);
1097
1098 if (inout == ATA_DATA_NO) {
1099 status = inb(iobase1 + ATA_CB_STAT);
1100 }
1101 else {
1102 while (1) {
1103
1104 while (1) {
1105 status = inb(iobase1 + ATA_CB_STAT);
1106 if ( !(status & ATA_CB_STAT_BSY) )
1107 break;
1108 }
1109
1110 // Check if command completed
1111 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_DRQ) ) ==0 )
1112 break;
1113
1114 if (status & ATA_CB_STAT_CHK) {
1115 BX_DEBUG_ATA("%s: error (status %02x)\n", __func__, status);
1116 // Enable interrupts
1117 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1118 return 3;
1119 }
1120
1121 // Device must be ready to send data
1122 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1123 != (ATA_CB_STAT_RDY | ATA_CB_STAT_DRQ) ) {
1124 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, status);
1125 // Enable interrupts
1126 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1127 return 4;
1128 }
1129
1130 // Normalize address
1131 BX_DEBUG_ATA("acp2 buffer ptr: %04x:%04x\n", FP_SEG(buffer), FP_OFF(buffer));
1132 buffer = MK_FP(FP_SEG(buffer) + FP_OFF(buffer) / 16 , FP_OFF(buffer) % 16);
1133 // bufseg += (bufoff / 16);
1134 // bufoff %= 16;
1135
1136 // Get the byte count
1137 lcount = ((uint16_t)(inb(iobase1 + ATA_CB_CH))<<8)+inb(iobase1 + ATA_CB_CL);
1138
1139 // Save byte count
1140 count = lcount;
1141
1142 BX_DEBUG_ATA("Trying to read %04x bytes ",lcount);
1143 BX_DEBUG_ATA("to 0x%04x:0x%04x\n",FP_SEG(buffer),FP_OFF(buffer));
1144
1145 // If counts not dividable by 4, use 16bits mode
1146 lmode = mode;
1147 if (lcount & 0x03)
1148 lmode = ATA_MODE_PIO16;
1149
1150 // adds an extra byte if count are odd. before is always even
1151 if (lcount & 0x01) {
1152 lcount += 1;
1153 }
1154
1155#if VBOX_BIOS_CPU >= 80386
1156 if (lmode == ATA_MODE_PIO32) {
1157 lcount >>= 2;
1158 } else
1159#endif
1160 {
1161 lcount >>= 1;
1162 }
1163
1164#if VBOX_BIOS_CPU >= 80386
1165 if (lmode == ATA_MODE_PIO32) {
1166 rep_insd(buffer, lcount, iobase1);
1167 } else
1168#endif
1169 {
1170 rep_insw(buffer, lcount, iobase1);
1171 }
1172
1173
1174 // Compute new buffer address
1175 buffer += count;
1176
1177 // Save transferred bytes count
1178 transfer += count;
1179 bios_dsk->drqp.trsfbytes = transfer;
1180 }
1181 }
1182
1183 // Final check, device must be ready
1184 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1185 != ATA_CB_STAT_RDY ) {
1186 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, (unsigned) status);
1187 // Enable interrupts
1188 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1189 return 4;
1190 }
1191
1192 // Enable interrupts
1193 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1194 return 0;
1195}
1196
1197// ---------------------------------------------------------------------------
1198// ATA/ATAPI driver : reset device; intended for ATAPI devices
1199// ---------------------------------------------------------------------------
1200 // returns
1201 // 0 : no error
1202 // 1 : error
1203uint16_t ata_soft_reset(uint16_t device)
1204{
1205 uint16_t iobase1, iobase2;
1206 uint8_t channel, slave;
1207 uint8_t status;
1208 bio_dsk_t __far *bios_dsk;
1209
1210 bios_dsk = read_word(0x0040, 0x000E) :> &EbdaData->bdisk;
1211
1212 channel = device / 2;
1213 slave = device % 2;
1214
1215 iobase1 = bios_dsk->channels[channel].iobase1;
1216 iobase2 = bios_dsk->channels[channel].iobase2;
1217
1218 /* Send a reset command to the device. */
1219 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15 | ATA_CB_DC_NIEN);
1220 outb(iobase1 + ATA_CB_DH, slave ? ATA_CB_DH_DEV1 : ATA_CB_DH_DEV0);
1221 outb(iobase1 + ATA_CB_CMD, ATA_CMD_DEVICE_RESET);
1222
1223 /* Wait for the device to clear BSY. */
1224 while (1) {
1225 status = inb(iobase1 + ATA_CB_STAT);
1226 if ( !(status & ATA_CB_STAT_BSY) ) break;
1227 }
1228
1229 /* Final check, device must be ready */
1230 if ( (status & (ATA_CB_STAT_BSY | ATA_CB_STAT_RDY | ATA_CB_STAT_DF | ATA_CB_STAT_DRQ | ATA_CB_STAT_CHK) )
1231 != ATA_CB_STAT_RDY ) {
1232 BX_DEBUG_ATA("%s: not ready (status %02x)\n", __func__, (unsigned) status);
1233 /* Enable interrupts */
1234 outb(iobase2 + ATA_CB_DC, ATA_CB_DC_HD15);
1235 return 1;
1236 }
1237
1238 /* Enable interrupts */
1239 outb(iobase2+ATA_CB_DC, ATA_CB_DC_HD15);
1240 return 0;
1241}
1242
1243
1244// ---------------------------------------------------------------------------
1245// End of ATA/ATAPI Driver
1246// ---------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use