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