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