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