]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ide.c
ide slave fixes (aka Win98 CD-ROM detection fix)
[mirror_qemu.git] / hw / ide.c
1 /*
2 * QEMU IDE disk and CD-ROM Emulator
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include "vl.h"
25
26 /* debug IDE devices */
27 //#define DEBUG_IDE
28 //#define DEBUG_IDE_ATAPI
29
30 /* Bits of HD_STATUS */
31 #define ERR_STAT 0x01
32 #define INDEX_STAT 0x02
33 #define ECC_STAT 0x04 /* Corrected error */
34 #define DRQ_STAT 0x08
35 #define SEEK_STAT 0x10
36 #define SRV_STAT 0x10
37 #define WRERR_STAT 0x20
38 #define READY_STAT 0x40
39 #define BUSY_STAT 0x80
40
41 /* Bits for HD_ERROR */
42 #define MARK_ERR 0x01 /* Bad address mark */
43 #define TRK0_ERR 0x02 /* couldn't find track 0 */
44 #define ABRT_ERR 0x04 /* Command aborted */
45 #define MCR_ERR 0x08 /* media change request */
46 #define ID_ERR 0x10 /* ID field not found */
47 #define MC_ERR 0x20 /* media changed */
48 #define ECC_ERR 0x40 /* Uncorrectable ECC error */
49 #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
50 #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
51
52 /* Bits of HD_NSECTOR */
53 #define CD 0x01
54 #define IO 0x02
55 #define REL 0x04
56 #define TAG_MASK 0xf8
57
58 #define IDE_CMD_RESET 0x04
59 #define IDE_CMD_DISABLE_IRQ 0x02
60
61 /* ATA/ATAPI Commands pre T13 Spec */
62 #define WIN_NOP 0x00
63 /*
64 * 0x01->0x02 Reserved
65 */
66 #define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */
67 /*
68 * 0x04->0x07 Reserved
69 */
70 #define WIN_SRST 0x08 /* ATAPI soft reset command */
71 #define WIN_DEVICE_RESET 0x08
72 /*
73 * 0x09->0x0F Reserved
74 */
75 #define WIN_RECAL 0x10
76 #define WIN_RESTORE WIN_RECAL
77 /*
78 * 0x10->0x1F Reserved
79 */
80 #define WIN_READ 0x20 /* 28-Bit */
81 #define WIN_READ_ONCE 0x21 /* 28-Bit without retries */
82 #define WIN_READ_LONG 0x22 /* 28-Bit */
83 #define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */
84 #define WIN_READ_EXT 0x24 /* 48-Bit */
85 #define WIN_READDMA_EXT 0x25 /* 48-Bit */
86 #define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */
87 #define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */
88 /*
89 * 0x28
90 */
91 #define WIN_MULTREAD_EXT 0x29 /* 48-Bit */
92 /*
93 * 0x2A->0x2F Reserved
94 */
95 #define WIN_WRITE 0x30 /* 28-Bit */
96 #define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */
97 #define WIN_WRITE_LONG 0x32 /* 28-Bit */
98 #define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */
99 #define WIN_WRITE_EXT 0x34 /* 48-Bit */
100 #define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */
101 #define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */
102 #define WIN_SET_MAX_EXT 0x37 /* 48-Bit */
103 #define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */
104 #define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */
105 /*
106 * 0x3A->0x3B Reserved
107 */
108 #define WIN_WRITE_VERIFY 0x3C /* 28-Bit */
109 /*
110 * 0x3D->0x3F Reserved
111 */
112 #define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */
113 #define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */
114 #define WIN_VERIFY_EXT 0x42 /* 48-Bit */
115 /*
116 * 0x43->0x4F Reserved
117 */
118 #define WIN_FORMAT 0x50
119 /*
120 * 0x51->0x5F Reserved
121 */
122 #define WIN_INIT 0x60
123 /*
124 * 0x61->0x5F Reserved
125 */
126 #define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */
127 #define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */
128 #define WIN_DIAGNOSE 0x90
129 #define WIN_SPECIFY 0x91 /* set drive geometry translation */
130 #define WIN_DOWNLOAD_MICROCODE 0x92
131 #define WIN_STANDBYNOW2 0x94
132 #define WIN_STANDBY2 0x96
133 #define WIN_SETIDLE2 0x97
134 #define WIN_CHECKPOWERMODE2 0x98
135 #define WIN_SLEEPNOW2 0x99
136 /*
137 * 0x9A VENDOR
138 */
139 #define WIN_PACKETCMD 0xA0 /* Send a packet command. */
140 #define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */
141 #define WIN_QUEUED_SERVICE 0xA2
142 #define WIN_SMART 0xB0 /* self-monitoring and reporting */
143 #define CFA_ERASE_SECTORS 0xC0
144 #define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/
145 #define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */
146 #define WIN_SETMULT 0xC6 /* enable/disable multiple mode */
147 #define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */
148 #define WIN_READDMA 0xC8 /* read sectors using DMA transfers */
149 #define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */
150 #define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */
151 #define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */
152 #define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */
153 #define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */
154 #define WIN_GETMEDIASTATUS 0xDA
155 #define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */
156 #define WIN_POSTBOOT 0xDC
157 #define WIN_PREBOOT 0xDD
158 #define WIN_DOORLOCK 0xDE /* lock door on removable drives */
159 #define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */
160 #define WIN_STANDBYNOW1 0xE0
161 #define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */
162 #define WIN_STANDBY 0xE2 /* Set device in Standby Mode */
163 #define WIN_SETIDLE1 0xE3
164 #define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */
165 #define WIN_CHECKPOWERMODE1 0xE5
166 #define WIN_SLEEPNOW1 0xE6
167 #define WIN_FLUSH_CACHE 0xE7
168 #define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */
169 #define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */
170 /* SET_FEATURES 0x22 or 0xDD */
171 #define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */
172 #define WIN_IDENTIFY 0xEC /* ask drive to identify itself */
173 #define WIN_MEDIAEJECT 0xED
174 #define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */
175 #define WIN_SETFEATURES 0xEF /* set special drive features */
176 #define EXABYTE_ENABLE_NEST 0xF0
177 #define WIN_SECURITY_SET_PASS 0xF1
178 #define WIN_SECURITY_UNLOCK 0xF2
179 #define WIN_SECURITY_ERASE_PREPARE 0xF3
180 #define WIN_SECURITY_ERASE_UNIT 0xF4
181 #define WIN_SECURITY_FREEZE_LOCK 0xF5
182 #define WIN_SECURITY_DISABLE 0xF6
183 #define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */
184 #define WIN_SET_MAX 0xF9
185 #define DISABLE_SEAGATE 0xFB
186
187 /* set to 1 set disable mult support */
188 #define MAX_MULT_SECTORS 8
189
190 /* ATAPI defines */
191
192 #define ATAPI_PACKET_SIZE 12
193
194 /* The generic packet command opcodes for CD/DVD Logical Units,
195 * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
196 #define GPCMD_BLANK 0xa1
197 #define GPCMD_CLOSE_TRACK 0x5b
198 #define GPCMD_FLUSH_CACHE 0x35
199 #define GPCMD_FORMAT_UNIT 0x04
200 #define GPCMD_GET_CONFIGURATION 0x46
201 #define GPCMD_GET_EVENT_STATUS_NOTIFICATION 0x4a
202 #define GPCMD_GET_PERFORMANCE 0xac
203 #define GPCMD_INQUIRY 0x12
204 #define GPCMD_LOAD_UNLOAD 0xa6
205 #define GPCMD_MECHANISM_STATUS 0xbd
206 #define GPCMD_MODE_SELECT_10 0x55
207 #define GPCMD_MODE_SENSE_10 0x5a
208 #define GPCMD_PAUSE_RESUME 0x4b
209 #define GPCMD_PLAY_AUDIO_10 0x45
210 #define GPCMD_PLAY_AUDIO_MSF 0x47
211 #define GPCMD_PLAY_AUDIO_TI 0x48
212 #define GPCMD_PLAY_CD 0xbc
213 #define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
214 #define GPCMD_READ_10 0x28
215 #define GPCMD_READ_12 0xa8
216 #define GPCMD_READ_CDVD_CAPACITY 0x25
217 #define GPCMD_READ_CD 0xbe
218 #define GPCMD_READ_CD_MSF 0xb9
219 #define GPCMD_READ_DISC_INFO 0x51
220 #define GPCMD_READ_DVD_STRUCTURE 0xad
221 #define GPCMD_READ_FORMAT_CAPACITIES 0x23
222 #define GPCMD_READ_HEADER 0x44
223 #define GPCMD_READ_TRACK_RZONE_INFO 0x52
224 #define GPCMD_READ_SUBCHANNEL 0x42
225 #define GPCMD_READ_TOC_PMA_ATIP 0x43
226 #define GPCMD_REPAIR_RZONE_TRACK 0x58
227 #define GPCMD_REPORT_KEY 0xa4
228 #define GPCMD_REQUEST_SENSE 0x03
229 #define GPCMD_RESERVE_RZONE_TRACK 0x53
230 #define GPCMD_SCAN 0xba
231 #define GPCMD_SEEK 0x2b
232 #define GPCMD_SEND_DVD_STRUCTURE 0xad
233 #define GPCMD_SEND_EVENT 0xa2
234 #define GPCMD_SEND_KEY 0xa3
235 #define GPCMD_SEND_OPC 0x54
236 #define GPCMD_SET_READ_AHEAD 0xa7
237 #define GPCMD_SET_STREAMING 0xb6
238 #define GPCMD_START_STOP_UNIT 0x1b
239 #define GPCMD_STOP_PLAY_SCAN 0x4e
240 #define GPCMD_TEST_UNIT_READY 0x00
241 #define GPCMD_VERIFY_10 0x2f
242 #define GPCMD_WRITE_10 0x2a
243 #define GPCMD_WRITE_AND_VERIFY_10 0x2e
244 /* This is listed as optional in ATAPI 2.6, but is (curiously)
245 * missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji
246 * Table 377 as an MMC command for SCSi devices though... Most ATAPI
247 * drives support it. */
248 #define GPCMD_SET_SPEED 0xbb
249 /* This seems to be a SCSI specific CD-ROM opcode
250 * to play data at track/index */
251 #define GPCMD_PLAYAUDIO_TI 0x48
252 /*
253 * From MS Media Status Notification Support Specification. For
254 * older drives only.
255 */
256 #define GPCMD_GET_MEDIA_STATUS 0xda
257
258 /* Mode page codes for mode sense/set */
259 #define GPMODE_R_W_ERROR_PAGE 0x01
260 #define GPMODE_WRITE_PARMS_PAGE 0x05
261 #define GPMODE_AUDIO_CTL_PAGE 0x0e
262 #define GPMODE_POWER_PAGE 0x1a
263 #define GPMODE_FAULT_FAIL_PAGE 0x1c
264 #define GPMODE_TO_PROTECT_PAGE 0x1d
265 #define GPMODE_CAPABILITIES_PAGE 0x2a
266 #define GPMODE_ALL_PAGES 0x3f
267 /* Not in Mt. Fuji, but in ATAPI 2.6 -- depricated now in favor
268 * of MODE_SENSE_POWER_PAGE */
269 #define GPMODE_CDROM_PAGE 0x0d
270
271 #define ATAPI_INT_REASON_CD 0x01 /* 0 = data transfer */
272 #define ATAPI_INT_REASON_IO 0x02 /* 1 = transfer to the host */
273 #define ATAPI_INT_REASON_REL 0x04
274 #define ATAPI_INT_REASON_TAG 0xf8
275
276 /* same constants as bochs */
277 #define ASC_ILLEGAL_OPCODE 0x20
278 #define ASC_LOGICAL_BLOCK_OOR 0x21
279 #define ASC_INV_FIELD_IN_CMD_PACKET 0x24
280 #define ASC_MEDIUM_NOT_PRESENT 0x3a
281 #define ASC_SAVING_PARAMETERS_NOT_SUPPORTED 0x39
282
283 #define SENSE_NONE 0
284 #define SENSE_NOT_READY 2
285 #define SENSE_ILLEGAL_REQUEST 5
286 #define SENSE_UNIT_ATTENTION 6
287
288 struct IDEState;
289
290 typedef void EndTransferFunc(struct IDEState *);
291
292 /* NOTE: IDEState represents in fact one drive */
293 typedef struct IDEState {
294 /* ide config */
295 int is_cdrom;
296 int cylinders, heads, sectors;
297 int64_t nb_sectors;
298 int mult_sectors;
299 int irq;
300 /* ide regs */
301 uint8_t feature;
302 uint8_t error;
303 uint16_t nsector; /* 0 is 256 to ease computations */
304 uint8_t sector;
305 uint8_t lcyl;
306 uint8_t hcyl;
307 uint8_t select;
308 uint8_t status;
309 /* 0x3f6 command, only meaningful for drive 0 */
310 uint8_t cmd;
311 /* depends on bit 4 in select, only meaningful for drive 0 */
312 struct IDEState *cur_drive;
313 BlockDriverState *bs;
314 /* ATAPI specific */
315 uint8_t sense_key;
316 uint8_t asc;
317 int packet_transfer_size;
318 int elementary_transfer_size;
319 int io_buffer_index;
320 int lba;
321 /* transfer handling */
322 int req_nb_sectors; /* number of sectors per interrupt */
323 EndTransferFunc *end_transfer_func;
324 uint8_t *data_ptr;
325 uint8_t *data_end;
326 uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4];
327 } IDEState;
328
329 static void padstr(char *str, const char *src, int len)
330 {
331 int i, v;
332 for(i = 0; i < len; i++) {
333 if (*src)
334 v = *src++;
335 else
336 v = ' ';
337 *(char *)((long)str ^ 1) = v;
338 str++;
339 }
340 }
341
342 static void padstr8(uint8_t *buf, int buf_size, const char *src)
343 {
344 int i;
345 for(i = 0; i < buf_size; i++) {
346 if (*src)
347 buf[i] = *src++;
348 else
349 buf[i] = ' ';
350 }
351 }
352
353 static void put_le16(uint16_t *p, unsigned int v)
354 {
355 *p = cpu_to_le16(v);
356 }
357
358 static void ide_identify(IDEState *s)
359 {
360 uint16_t *p;
361 unsigned int oldsize;
362
363 memset(s->io_buffer, 0, 512);
364 p = (uint16_t *)s->io_buffer;
365 put_le16(p + 0, 0x0040);
366 put_le16(p + 1, s->cylinders);
367 put_le16(p + 3, s->heads);
368 put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
369 put_le16(p + 5, 512); /* XXX: retired, remove ? */
370 put_le16(p + 6, s->sectors);
371 padstr((uint8_t *)(p + 10), "QM00001", 20); /* serial number */
372 put_le16(p + 20, 3); /* XXX: retired, remove ? */
373 put_le16(p + 21, 512); /* cache size in sectors */
374 put_le16(p + 22, 4); /* ecc bytes */
375 padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */
376 padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40); /* model */
377 #if MAX_MULT_SECTORS > 1
378 put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
379 #endif
380 put_le16(p + 48, 1); /* dword I/O */
381 put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
382 put_le16(p + 51, 0x200); /* PIO transfer cycle */
383 put_le16(p + 52, 0x200); /* DMA transfer cycle */
384 put_le16(p + 53, 1); /* words 54-58 are valid */
385 put_le16(p + 54, s->cylinders);
386 put_le16(p + 55, s->heads);
387 put_le16(p + 56, s->sectors);
388 oldsize = s->cylinders * s->heads * s->sectors;
389 put_le16(p + 57, oldsize);
390 put_le16(p + 58, oldsize >> 16);
391 if (s->mult_sectors)
392 put_le16(p + 59, 0x100 | s->mult_sectors);
393 put_le16(p + 60, s->nb_sectors);
394 put_le16(p + 61, s->nb_sectors >> 16);
395 put_le16(p + 80, (1 << 1) | (1 << 2));
396 put_le16(p + 82, (1 << 14));
397 put_le16(p + 83, (1 << 14));
398 put_le16(p + 84, (1 << 14));
399 put_le16(p + 85, (1 << 14));
400 put_le16(p + 86, 0);
401 put_le16(p + 87, (1 << 14));
402 }
403
404 static void ide_atapi_identify(IDEState *s)
405 {
406 uint16_t *p;
407
408 memset(s->io_buffer, 0, 512);
409 p = (uint16_t *)s->io_buffer;
410 /* Removable CDROM, 50us response, 12 byte packets */
411 put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
412 padstr((uint8_t *)(p + 10), "QM00001", 20); /* serial number */
413 put_le16(p + 20, 3); /* buffer type */
414 put_le16(p + 21, 512); /* cache size in sectors */
415 put_le16(p + 22, 4); /* ecc bytes */
416 padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */
417 padstr((uint8_t *)(p + 27), "QEMU CD-ROM", 40); /* model */
418 put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
419 put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
420 put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
421 put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
422 put_le16(p + 64, 1); /* PIO modes */
423 put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
424 put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
425 put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
426 put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
427
428 put_le16(p + 71, 30); /* in ns */
429 put_le16(p + 72, 30); /* in ns */
430
431 put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
432 }
433
434 static void ide_set_signature(IDEState *s)
435 {
436 s->select &= 0xf0; /* clear head */
437 /* put signature */
438 s->nsector = 1;
439 s->sector = 1;
440 if (s->is_cdrom) {
441 s->lcyl = 0x14;
442 s->hcyl = 0xeb;
443 } else if (s->bs) {
444 s->lcyl = 0;
445 s->hcyl = 0;
446 } else {
447 s->lcyl = 0xff;
448 s->hcyl = 0xff;
449 }
450 }
451
452 static inline void ide_abort_command(IDEState *s)
453 {
454 s->status = READY_STAT | ERR_STAT;
455 s->error = ABRT_ERR;
456 }
457
458 static inline void ide_set_irq(IDEState *s)
459 {
460 if (!(s->cmd & IDE_CMD_DISABLE_IRQ)) {
461 pic_set_irq(s->irq, 1);
462 }
463 }
464
465 /* prepare data transfer and tell what to do after */
466 static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
467 EndTransferFunc *end_transfer_func)
468 {
469 s->end_transfer_func = end_transfer_func;
470 s->data_ptr = buf;
471 s->data_end = buf + size;
472 s->status |= DRQ_STAT;
473 }
474
475 static void ide_transfer_stop(IDEState *s)
476 {
477 s->end_transfer_func = ide_transfer_stop;
478 s->data_ptr = s->io_buffer;
479 s->data_end = s->io_buffer;
480 s->status &= ~DRQ_STAT;
481 }
482
483 static int64_t ide_get_sector(IDEState *s)
484 {
485 int64_t sector_num;
486 if (s->select & 0x40) {
487 /* lba */
488 sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
489 (s->lcyl << 8) | s->sector;
490 } else {
491 sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
492 (s->select & 0x0f) * s->sectors +
493 (s->sector - 1);
494 }
495 return sector_num;
496 }
497
498 static void ide_set_sector(IDEState *s, int64_t sector_num)
499 {
500 unsigned int cyl, r;
501 if (s->select & 0x40) {
502 s->select = (s->select & 0xf0) | (sector_num >> 24);
503 s->hcyl = (sector_num >> 16);
504 s->lcyl = (sector_num >> 8);
505 s->sector = (sector_num);
506 } else {
507 cyl = sector_num / (s->heads * s->sectors);
508 r = sector_num % (s->heads * s->sectors);
509 s->hcyl = cyl >> 8;
510 s->lcyl = cyl;
511 s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
512 s->sector = (r % s->sectors) + 1;
513 }
514 }
515
516 static void ide_sector_read(IDEState *s)
517 {
518 int64_t sector_num;
519 int ret, n;
520
521 s->status = READY_STAT | SEEK_STAT;
522 s->error = 0; /* not needed by IDE spec, but needed by Windows */
523 sector_num = ide_get_sector(s);
524 n = s->nsector;
525 if (n == 0) {
526 /* no more sector to read from disk */
527 ide_transfer_stop(s);
528 } else {
529 #if defined(DEBUG_IDE)
530 printf("read sector=%Ld\n", sector_num);
531 #endif
532 if (n > s->req_nb_sectors)
533 n = s->req_nb_sectors;
534 ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
535 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
536 ide_set_irq(s);
537 ide_set_sector(s, sector_num + n);
538 s->nsector -= n;
539 }
540 }
541
542 static void ide_sector_write(IDEState *s)
543 {
544 int64_t sector_num;
545 int ret, n, n1;
546
547 s->status = READY_STAT | SEEK_STAT;
548 sector_num = ide_get_sector(s);
549 #if defined(DEBUG_IDE)
550 printf("write sector=%Ld\n", sector_num);
551 #endif
552 n = s->nsector;
553 if (n > s->req_nb_sectors)
554 n = s->req_nb_sectors;
555 ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
556 s->nsector -= n;
557 if (s->nsector == 0) {
558 /* no more sector to write */
559 ide_transfer_stop(s);
560 } else {
561 n1 = s->nsector;
562 if (n1 > s->req_nb_sectors)
563 n1 = s->req_nb_sectors;
564 ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
565 }
566 ide_set_sector(s, sector_num + n);
567 ide_set_irq(s);
568 }
569
570 static void ide_atapi_cmd_ok(IDEState *s)
571 {
572 s->error = 0;
573 s->status = READY_STAT;
574 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
575 ide_set_irq(s);
576 }
577
578 static void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
579 {
580 #ifdef DEBUG_IDE_ATAPI
581 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
582 #endif
583 s->error = sense_key << 4;
584 s->status = READY_STAT | ERR_STAT;
585 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
586 s->sense_key = sense_key;
587 s->asc = asc;
588 ide_set_irq(s);
589 }
590
591 static inline void cpu_to_ube16(uint8_t *buf, int val)
592 {
593 buf[0] = val >> 8;
594 buf[1] = val;
595 }
596
597 static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
598 {
599 buf[0] = val >> 24;
600 buf[1] = val >> 16;
601 buf[2] = val >> 8;
602 buf[3] = val;
603 }
604
605 static inline int ube16_to_cpu(const uint8_t *buf)
606 {
607 return (buf[0] << 8) | buf[1];
608 }
609
610 static inline int ube32_to_cpu(const uint8_t *buf)
611 {
612 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
613 }
614
615 /* The whole ATAPI transfer logic is handled in this function */
616 static void ide_atapi_cmd_reply_end(IDEState *s)
617 {
618 int byte_count_limit, size;
619 #ifdef DEBUG_IDE_ATAPI
620 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
621 s->packet_transfer_size,
622 s->elementary_transfer_size,
623 s->io_buffer_index);
624 #endif
625 if (s->packet_transfer_size <= 0) {
626 /* end of transfer */
627 ide_transfer_stop(s);
628 s->status = READY_STAT;
629 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
630 ide_set_irq(s);
631 #ifdef DEBUG_IDE_ATAPI
632 printf("status=0x%x\n", s->status);
633 #endif
634 } else {
635 /* see if a new sector must be read */
636 if (s->lba != -1 && s->io_buffer_index >= 2048) {
637 bdrv_read(s->bs, (int64_t)s->lba << 2, s->io_buffer, 4);
638 s->lba++;
639 s->io_buffer_index = 0;
640 }
641 if (s->elementary_transfer_size > 0) {
642 /* there are some data left to transmit in this elementary
643 transfer */
644 size = 2048 - s->io_buffer_index;
645 if (size > s->elementary_transfer_size)
646 size = s->elementary_transfer_size;
647 ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
648 size, ide_atapi_cmd_reply_end);
649 s->packet_transfer_size -= size;
650 s->elementary_transfer_size -= size;
651 s->io_buffer_index += size;
652 } else {
653 /* a new transfer is needed */
654 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
655 byte_count_limit = s->lcyl | (s->hcyl << 8);
656 #ifdef DEBUG_IDE_ATAPI
657 printf("byte_count_limit=%d\n", byte_count_limit);
658 #endif
659 if (byte_count_limit == 0xffff)
660 byte_count_limit--;
661 size = s->packet_transfer_size;
662 if (size > byte_count_limit) {
663 /* byte count limit must be even if this case */
664 if (byte_count_limit & 1)
665 byte_count_limit--;
666 size = byte_count_limit;
667 }
668 s->lcyl = size;
669 s->hcyl = size >> 8;
670 s->elementary_transfer_size = size;
671 /* we cannot transmit more than one sector at a time */
672 if (s->lba != -1) {
673 if (size > (2048 - s->io_buffer_index))
674 size = (2048 - s->io_buffer_index);
675 }
676 ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
677 size, ide_atapi_cmd_reply_end);
678 s->packet_transfer_size -= size;
679 s->elementary_transfer_size -= size;
680 s->io_buffer_index += size;
681 ide_set_irq(s);
682 #ifdef DEBUG_IDE_ATAPI
683 printf("status=0x%x\n", s->status);
684 #endif
685 }
686 }
687 }
688
689 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
690 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
691 {
692 if (size > max_size)
693 size = max_size;
694 s->lba = -1; /* no sector read */
695 s->packet_transfer_size = size;
696 s->elementary_transfer_size = 0;
697 s->io_buffer_index = 0;
698
699 s->status = READY_STAT;
700 ide_atapi_cmd_reply_end(s);
701 }
702
703 /* start a CD-CDROM read command */
704 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors)
705 {
706 #ifdef DEBUG_IDE_ATAPI
707 printf("read: LBA=%d nb_sectors=%d\n", lba, nb_sectors);
708 #endif
709 s->lba = lba;
710 s->packet_transfer_size = nb_sectors * 2048;
711 s->elementary_transfer_size = 0;
712 s->io_buffer_index = 2048;
713
714 s->status = READY_STAT;
715 ide_atapi_cmd_reply_end(s);
716 }
717
718 /* same toc as bochs. Return -1 if error or the toc length */
719 static int cdrom_read_toc(IDEState *s, uint8_t *buf, int msf, int start_track)
720 {
721 uint8_t *q;
722 int nb_sectors, len;
723
724 if (start_track > 1 && start_track != 0xaa)
725 return -1;
726 q = buf + 2;
727 *q++ = 1;
728 *q++ = 1;
729 if (start_track <= 1) {
730 *q++ = 0; /* reserved */
731 *q++ = 0x14; /* ADR, control */
732 *q++ = 1; /* track number */
733 *q++ = 0; /* reserved */
734 if (msf) {
735 *q++ = 0; /* reserved */
736 *q++ = 0; /* minute */
737 *q++ = 2; /* second */
738 *q++ = 0; /* frame */
739 } else {
740 /* sector 0 */
741 cpu_to_ube32(q, 0);
742 q += 4;
743 }
744 }
745 /* lead out track */
746 *q++ = 0; /* reserved */
747 *q++ = 0x16; /* ADR, control */
748 *q++ = 0xaa; /* track number */
749 *q++ = 0; /* reserved */
750 nb_sectors = s->nb_sectors >> 2;
751 if (msf) {
752 *q++ = 0; /* reserved */
753 *q++ = ((nb_sectors + 150) / 75) / 60;
754 *q++ = ((nb_sectors + 150) / 75) % 60;
755 *q++ = (nb_sectors + 150) % 75;
756 } else {
757 cpu_to_ube32(q, nb_sectors);
758 q += 4;
759 }
760 len = q - buf;
761 cpu_to_ube16(buf, len - 2);
762 return len;
763 }
764
765 static void ide_atapi_cmd(IDEState *s)
766 {
767 const uint8_t *packet;
768 uint8_t *buf;
769 int max_len;
770
771 packet = s->io_buffer;
772 buf = s->io_buffer;
773 #ifdef DEBUG_IDE_ATAPI
774 {
775 int i;
776 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
777 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
778 printf(" %02x", packet[i]);
779 }
780 printf("\n");
781 }
782 #endif
783 switch(s->io_buffer[0]) {
784 case GPCMD_TEST_UNIT_READY:
785 if (bdrv_is_inserted(s->bs)) {
786 ide_atapi_cmd_ok(s);
787 } else {
788 ide_atapi_cmd_error(s, SENSE_NOT_READY,
789 ASC_MEDIUM_NOT_PRESENT);
790 }
791 break;
792 case GPCMD_MODE_SENSE_10:
793 {
794 int action, code;
795 max_len = ube16_to_cpu(packet + 7);
796 action = packet[2] >> 6;
797 code = packet[2] & 0x3f;
798 switch(action) {
799 case 0: /* current values */
800 switch(code) {
801 case 0x01: /* error recovery */
802 cpu_to_ube16(&buf[0], 16 + 6);
803 buf[2] = 0x70;
804 buf[3] = 0;
805 buf[4] = 0;
806 buf[5] = 0;
807 buf[6] = 0;
808 buf[7] = 0;
809
810 buf[8] = 0x01;
811 buf[9] = 0x06;
812 buf[10] = 0x00;
813 buf[11] = 0x05;
814 buf[12] = 0x00;
815 buf[13] = 0x00;
816 buf[14] = 0x00;
817 buf[15] = 0x00;
818 ide_atapi_cmd_reply(s, 16, max_len);
819 break;
820 case 0x2a:
821 cpu_to_ube16(&buf[0], 28 + 6);
822 buf[2] = 0x70;
823 buf[3] = 0;
824 buf[4] = 0;
825 buf[5] = 0;
826 buf[6] = 0;
827 buf[7] = 0;
828
829 buf[8] = 0x2a;
830 buf[9] = 0x12;
831 buf[10] = 0x00;
832 buf[11] = 0x00;
833
834 buf[12] = 0x70;
835 buf[13] = 3 << 5;
836 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
837 if (bdrv_is_locked(s->bs))
838 buf[6] |= 1 << 1;
839 buf[15] = 0x00;
840 cpu_to_ube16(&buf[16], 706);
841 buf[18] = 0;
842 buf[19] = 2;
843 cpu_to_ube16(&buf[20], 512);
844 cpu_to_ube16(&buf[22], 706);
845 buf[24] = 0;
846 buf[25] = 0;
847 buf[26] = 0;
848 buf[27] = 0;
849 ide_atapi_cmd_reply(s, 28, max_len);
850 break;
851 default:
852 goto error_cmd;
853 }
854 break;
855 case 1: /* changeable values */
856 goto error_cmd;
857 case 2: /* default values */
858 goto error_cmd;
859 default:
860 case 3: /* saved values */
861 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
862 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
863 break;
864 }
865 }
866 break;
867 case GPCMD_REQUEST_SENSE:
868 max_len = packet[4];
869 memset(buf, 0, 18);
870 buf[0] = 0x70 | (1 << 7);
871 buf[2] = s->sense_key;
872 buf[7] = 10;
873 buf[12] = s->asc;
874 ide_atapi_cmd_reply(s, 18, max_len);
875 break;
876 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
877 if (bdrv_is_inserted(s->bs)) {
878 bdrv_set_locked(s->bs, packet[4] & 1);
879 ide_atapi_cmd_ok(s);
880 } else {
881 ide_atapi_cmd_error(s, SENSE_NOT_READY,
882 ASC_MEDIUM_NOT_PRESENT);
883 }
884 break;
885 case GPCMD_READ_10:
886 case GPCMD_READ_12:
887 {
888 int nb_sectors, lba;
889
890 if (!bdrv_is_inserted(s->bs)) {
891 ide_atapi_cmd_error(s, SENSE_NOT_READY,
892 ASC_MEDIUM_NOT_PRESENT);
893 break;
894 }
895 if (packet[0] == GPCMD_READ_10)
896 nb_sectors = ube16_to_cpu(packet + 7);
897 else
898 nb_sectors = ube32_to_cpu(packet + 6);
899 lba = ube32_to_cpu(packet + 2);
900 if (nb_sectors == 0) {
901 ide_atapi_cmd_ok(s);
902 break;
903 }
904 if (((int64_t)(lba + nb_sectors) << 2) > s->nb_sectors) {
905 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
906 ASC_LOGICAL_BLOCK_OOR);
907 break;
908 }
909 ide_atapi_cmd_read(s, lba, nb_sectors);
910 }
911 break;
912 case GPCMD_SEEK:
913 {
914 int lba;
915 if (!bdrv_is_inserted(s->bs)) {
916 ide_atapi_cmd_error(s, SENSE_NOT_READY,
917 ASC_MEDIUM_NOT_PRESENT);
918 break;
919 }
920 lba = ube32_to_cpu(packet + 2);
921 if (((int64_t)lba << 2) > s->nb_sectors) {
922 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
923 ASC_LOGICAL_BLOCK_OOR);
924 break;
925 }
926 ide_atapi_cmd_ok(s);
927 }
928 break;
929 case GPCMD_START_STOP_UNIT:
930 {
931 int start, eject;
932 start = packet[4] & 1;
933 eject = (packet[4] >> 1) & 1;
934
935 if (eject && !start) {
936 /* eject the disk */
937 bdrv_close(s->bs);
938 }
939 ide_atapi_cmd_ok(s);
940 }
941 break;
942 case GPCMD_MECHANISM_STATUS:
943 {
944 max_len = ube16_to_cpu(packet + 8);
945 cpu_to_ube16(buf, 0);
946 /* no current LBA */
947 buf[2] = 0;
948 buf[3] = 0;
949 buf[4] = 0;
950 buf[5] = 1;
951 cpu_to_ube16(buf + 6, 0);
952 ide_atapi_cmd_reply(s, 8, max_len);
953 }
954 break;
955 case GPCMD_READ_TOC_PMA_ATIP:
956 {
957 int format, msf, start_track, len;
958
959 if (!bdrv_is_inserted(s->bs)) {
960 ide_atapi_cmd_error(s, SENSE_NOT_READY,
961 ASC_MEDIUM_NOT_PRESENT);
962 break;
963 }
964 max_len = ube16_to_cpu(packet + 7);
965 format = packet[9] >> 6;
966 msf = (packet[1] >> 1) & 1;
967 start_track = packet[6];
968 switch(format) {
969 case 0:
970 len = cdrom_read_toc(s, buf, msf, start_track);
971 if (len < 0)
972 goto error_cmd;
973 ide_atapi_cmd_reply(s, len, max_len);
974 break;
975 case 1:
976 /* multi session : only a single session defined */
977 memset(buf, 0, 12);
978 buf[1] = 0x0a;
979 buf[2] = 0x01;
980 buf[3] = 0x01;
981 ide_atapi_cmd_reply(s, 12, max_len);
982 break;
983 default:
984 error_cmd:
985 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
986 ASC_INV_FIELD_IN_CMD_PACKET);
987 break;
988 }
989 }
990 break;
991 case GPCMD_READ_CDVD_CAPACITY:
992 if (!bdrv_is_inserted(s->bs)) {
993 ide_atapi_cmd_error(s, SENSE_NOT_READY,
994 ASC_MEDIUM_NOT_PRESENT);
995 break;
996 }
997 /* NOTE: it is really the number of sectors minus 1 */
998 cpu_to_ube32(buf, (s->nb_sectors >> 2) - 1);
999 cpu_to_ube32(buf + 4, 2048);
1000 ide_atapi_cmd_reply(s, 8, 8);
1001 break;
1002 case GPCMD_INQUIRY:
1003 max_len = packet[4];
1004 buf[0] = 0x05; /* CD-ROM */
1005 buf[1] = 0x80; /* removable */
1006 buf[2] = 0x00; /* ISO */
1007 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1008 buf[4] = 31; /* additionnal length */
1009 buf[5] = 0; /* reserved */
1010 buf[6] = 0; /* reserved */
1011 buf[7] = 0; /* reserved */
1012 padstr8(buf + 8, 8, "QEMU");
1013 padstr8(buf + 16, 16, "QEMU CD-ROM");
1014 padstr8(buf + 32, 4, QEMU_VERSION);
1015 ide_atapi_cmd_reply(s, 36, max_len);
1016 break;
1017 default:
1018 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1019 ASC_ILLEGAL_OPCODE);
1020 break;
1021 }
1022 }
1023
1024 /* called when the inserted state of the media has changed */
1025 static void cdrom_change_cb(void *opaque)
1026 {
1027 IDEState *s = opaque;
1028 int64_t nb_sectors;
1029
1030 /* XXX: send interrupt too */
1031 bdrv_get_geometry(s->bs, &nb_sectors);
1032 s->nb_sectors = nb_sectors;
1033 }
1034
1035 static void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1036 {
1037 IDEState *ide_if = opaque;
1038 IDEState *s;
1039 int unit, n;
1040
1041 #ifdef DEBUG_IDE
1042 printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1043 #endif
1044 addr &= 7;
1045 switch(addr) {
1046 case 0:
1047 break;
1048 case 1:
1049 /* NOTE: data is written to the two drives */
1050 ide_if[0].feature = val;
1051 ide_if[1].feature = val;
1052 break;
1053 case 2:
1054 if (val == 0)
1055 val = 256;
1056 ide_if[0].nsector = val;
1057 ide_if[1].nsector = val;
1058 break;
1059 case 3:
1060 ide_if[0].sector = val;
1061 ide_if[1].sector = val;
1062 break;
1063 case 4:
1064 ide_if[0].lcyl = val;
1065 ide_if[1].lcyl = val;
1066 break;
1067 case 5:
1068 ide_if[0].hcyl = val;
1069 ide_if[1].hcyl = val;
1070 break;
1071 case 6:
1072 ide_if[0].select = (val & ~0x10) | 0xa0;
1073 ide_if[1].select = (val | 0x10) | 0xa0;
1074 /* select drive */
1075 unit = (val >> 4) & 1;
1076 s = ide_if + unit;
1077 ide_if->cur_drive = s;
1078 break;
1079 default:
1080 case 7:
1081 /* command */
1082 #if defined(DEBUG_IDE)
1083 printf("ide: CMD=%02x\n", val);
1084 #endif
1085 s = ide_if->cur_drive;
1086 /* ignore commands to non existant slave */
1087 if (s != ide_if && !s->bs)
1088 break;
1089 switch(val) {
1090 case WIN_IDENTIFY:
1091 if (s->bs && !s->is_cdrom) {
1092 ide_identify(s);
1093 s->status = READY_STAT;
1094 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1095 } else {
1096 if (s->is_cdrom) {
1097 ide_set_signature(s);
1098 }
1099 ide_abort_command(s);
1100 }
1101 ide_set_irq(s);
1102 break;
1103 case WIN_SPECIFY:
1104 case WIN_RECAL:
1105 s->error = 0;
1106 s->status = READY_STAT;
1107 ide_set_irq(s);
1108 break;
1109 case WIN_SETMULT:
1110 if (s->nsector > MAX_MULT_SECTORS ||
1111 s->nsector == 0 ||
1112 (s->nsector & (s->nsector - 1)) != 0) {
1113 ide_abort_command(s);
1114 } else {
1115 s->mult_sectors = s->nsector;
1116 s->status = READY_STAT;
1117 }
1118 ide_set_irq(s);
1119 break;
1120 case WIN_VERIFY:
1121 case WIN_VERIFY_ONCE:
1122 /* do sector number check ? */
1123 s->status = READY_STAT;
1124 ide_set_irq(s);
1125 break;
1126 case WIN_READ:
1127 case WIN_READ_ONCE:
1128 if (!s->bs)
1129 goto abort_cmd;
1130 s->req_nb_sectors = 1;
1131 ide_sector_read(s);
1132 break;
1133 case WIN_WRITE:
1134 case WIN_WRITE_ONCE:
1135 s->error = 0;
1136 s->status = SEEK_STAT;
1137 s->req_nb_sectors = 1;
1138 ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1139 break;
1140 case WIN_MULTREAD:
1141 if (!s->mult_sectors)
1142 goto abort_cmd;
1143 s->req_nb_sectors = s->mult_sectors;
1144 ide_sector_read(s);
1145 break;
1146 case WIN_MULTWRITE:
1147 if (!s->mult_sectors)
1148 goto abort_cmd;
1149 s->error = 0;
1150 s->status = SEEK_STAT;
1151 s->req_nb_sectors = s->mult_sectors;
1152 n = s->nsector;
1153 if (n > s->req_nb_sectors)
1154 n = s->req_nb_sectors;
1155 ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1156 break;
1157 case WIN_READ_NATIVE_MAX:
1158 ide_set_sector(s, s->nb_sectors - 1);
1159 s->status = READY_STAT;
1160 ide_set_irq(s);
1161 break;
1162 case WIN_CHECKPOWERMODE1:
1163 s->nsector = 0xff; /* device active or idle */
1164 s->status = READY_STAT;
1165 ide_set_irq(s);
1166 break;
1167
1168 /* ATAPI commands */
1169 case WIN_PIDENTIFY:
1170 if (s->is_cdrom) {
1171 ide_atapi_identify(s);
1172 s->status = READY_STAT;
1173 ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1174 } else {
1175 ide_abort_command(s);
1176 }
1177 ide_set_irq(s);
1178 break;
1179 case WIN_SRST:
1180 if (!s->is_cdrom)
1181 goto abort_cmd;
1182 ide_set_signature(s);
1183 s->status = 0x00; /* NOTE: READY is _not_ set */
1184 s->error = 0x01;
1185 break;
1186 case WIN_PACKETCMD:
1187 if (!s->is_cdrom)
1188 goto abort_cmd;
1189 /* DMA or overlapping commands not supported */
1190 if ((s->feature & 0x03) != 0)
1191 goto abort_cmd;
1192 s->nsector = 1;
1193 ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
1194 ide_atapi_cmd);
1195 break;
1196 default:
1197 abort_cmd:
1198 ide_abort_command(s);
1199 ide_set_irq(s);
1200 break;
1201 }
1202 }
1203 }
1204
1205 static uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
1206 {
1207 IDEState *ide_if = opaque;
1208 IDEState *s = ide_if->cur_drive;
1209 uint32_t addr;
1210 int ret;
1211
1212 addr = addr1 & 7;
1213 switch(addr) {
1214 case 0:
1215 ret = 0xff;
1216 break;
1217 case 1:
1218 if (!ide_if[0].bs && !ide_if[1].bs)
1219 ret = 0;
1220 else
1221 ret = s->error;
1222 break;
1223 case 2:
1224 if (!ide_if[0].bs && !ide_if[1].bs)
1225 ret = 0;
1226 else
1227 ret = s->nsector & 0xff;
1228 break;
1229 case 3:
1230 if (!ide_if[0].bs && !ide_if[1].bs)
1231 ret = 0;
1232 else
1233 ret = s->sector;
1234 break;
1235 case 4:
1236 if (!ide_if[0].bs && !ide_if[1].bs)
1237 ret = 0;
1238 else
1239 ret = s->lcyl;
1240 break;
1241 case 5:
1242 if (!ide_if[0].bs && !ide_if[1].bs)
1243 ret = 0;
1244 else
1245 ret = s->hcyl;
1246 break;
1247 case 6:
1248 if (!ide_if[0].bs && !ide_if[1].bs)
1249 ret = 0;
1250 else
1251 ret = s->select;
1252 break;
1253 default:
1254 case 7:
1255 if ((!ide_if[0].bs && !ide_if[1].bs) ||
1256 (s != ide_if && !s->bs))
1257 ret = 0;
1258 else
1259 ret = s->status;
1260 pic_set_irq(s->irq, 0);
1261 break;
1262 }
1263 #ifdef DEBUG_IDE
1264 printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
1265 #endif
1266 return ret;
1267 }
1268
1269 static uint32_t ide_status_read(void *opaque, uint32_t addr)
1270 {
1271 IDEState *ide_if = opaque;
1272 IDEState *s = ide_if->cur_drive;
1273 int ret;
1274
1275 if ((!ide_if[0].bs && !ide_if[1].bs) ||
1276 (s != ide_if && !s->bs))
1277 ret = 0;
1278 else
1279 ret = s->status;
1280 #ifdef DEBUG_IDE
1281 printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
1282 #endif
1283 return ret;
1284 }
1285
1286 static void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
1287 {
1288 IDEState *ide_if = opaque;
1289 IDEState *s;
1290 int i;
1291
1292 #ifdef DEBUG_IDE
1293 printf("ide: write control addr=0x%x val=%02x\n", addr, val);
1294 #endif
1295 /* common for both drives */
1296 if (!(ide_if[0].cmd & IDE_CMD_RESET) &&
1297 (val & IDE_CMD_RESET)) {
1298 /* reset low to high */
1299 for(i = 0;i < 2; i++) {
1300 s = &ide_if[i];
1301 s->status = BUSY_STAT | SEEK_STAT;
1302 s->error = 0x01;
1303 }
1304 } else if ((ide_if[0].cmd & IDE_CMD_RESET) &&
1305 !(val & IDE_CMD_RESET)) {
1306 /* high to low */
1307 for(i = 0;i < 2; i++) {
1308 s = &ide_if[i];
1309 if (s->is_cdrom)
1310 s->status = 0x00; /* NOTE: READY is _not_ set */
1311 else
1312 s->status = READY_STAT | SEEK_STAT;
1313 ide_set_signature(s);
1314 }
1315 }
1316
1317 ide_if[0].cmd = val;
1318 ide_if[1].cmd = val;
1319 }
1320
1321 static void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
1322 {
1323 IDEState *s = ((IDEState *)opaque)->cur_drive;
1324 uint8_t *p;
1325
1326 p = s->data_ptr;
1327 *(uint16_t *)p = le16_to_cpu(val);
1328 p += 2;
1329 s->data_ptr = p;
1330 if (p >= s->data_end)
1331 s->end_transfer_func(s);
1332 }
1333
1334 static uint32_t ide_data_readw(void *opaque, uint32_t addr)
1335 {
1336 IDEState *s = ((IDEState *)opaque)->cur_drive;
1337 uint8_t *p;
1338 int ret;
1339 p = s->data_ptr;
1340 ret = cpu_to_le16(*(uint16_t *)p);
1341 p += 2;
1342 s->data_ptr = p;
1343 if (p >= s->data_end)
1344 s->end_transfer_func(s);
1345 return ret;
1346 }
1347
1348 static void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
1349 {
1350 IDEState *s = ((IDEState *)opaque)->cur_drive;
1351 uint8_t *p;
1352
1353 p = s->data_ptr;
1354 *(uint32_t *)p = le32_to_cpu(val);
1355 p += 4;
1356 s->data_ptr = p;
1357 if (p >= s->data_end)
1358 s->end_transfer_func(s);
1359 }
1360
1361 static uint32_t ide_data_readl(void *opaque, uint32_t addr)
1362 {
1363 IDEState *s = ((IDEState *)opaque)->cur_drive;
1364 uint8_t *p;
1365 int ret;
1366
1367 p = s->data_ptr;
1368 ret = cpu_to_le32(*(uint32_t *)p);
1369 p += 4;
1370 s->data_ptr = p;
1371 if (p >= s->data_end)
1372 s->end_transfer_func(s);
1373 return ret;
1374 }
1375
1376 static void ide_reset(IDEState *s)
1377 {
1378 s->mult_sectors = MAX_MULT_SECTORS;
1379 s->cur_drive = s;
1380 s->select = 0xa0;
1381 s->status = READY_STAT;
1382 ide_set_signature(s);
1383 }
1384
1385 struct partition {
1386 uint8_t boot_ind; /* 0x80 - active */
1387 uint8_t head; /* starting head */
1388 uint8_t sector; /* starting sector */
1389 uint8_t cyl; /* starting cylinder */
1390 uint8_t sys_ind; /* What partition type */
1391 uint8_t end_head; /* end head */
1392 uint8_t end_sector; /* end sector */
1393 uint8_t end_cyl; /* end cylinder */
1394 uint32_t start_sect; /* starting sector counting from 0 */
1395 uint32_t nr_sects; /* nr of sectors in partition */
1396 } __attribute__((packed));
1397
1398 /* try to guess the IDE geometry from the MSDOS partition table */
1399 static void ide_guess_geometry(IDEState *s)
1400 {
1401 uint8_t buf[512];
1402 int ret, i;
1403 struct partition *p;
1404 uint32_t nr_sects;
1405
1406 if (s->cylinders != 0)
1407 return;
1408 ret = bdrv_read(s->bs, 0, buf, 1);
1409 if (ret < 0)
1410 return;
1411 /* test msdos magic */
1412 if (buf[510] != 0x55 || buf[511] != 0xaa)
1413 return;
1414 for(i = 0; i < 4; i++) {
1415 p = ((struct partition *)(buf + 0x1be)) + i;
1416 nr_sects = le32_to_cpu(p->nr_sects);
1417 if (nr_sects && p->end_head) {
1418 /* We make the assumption that the partition terminates on
1419 a cylinder boundary */
1420 s->heads = p->end_head + 1;
1421 s->sectors = p->end_sector & 63;
1422 s->cylinders = s->nb_sectors / (s->heads * s->sectors);
1423 #if 0
1424 printf("guessed partition: CHS=%d %d %d\n",
1425 s->cylinders, s->heads, s->sectors);
1426 #endif
1427 }
1428 }
1429 }
1430
1431 void ide_init(int iobase, int iobase2, int irq,
1432 BlockDriverState *hd0, BlockDriverState *hd1)
1433 {
1434 IDEState *s, *ide_state;
1435 int i, cylinders, heads, secs;
1436 int64_t nb_sectors;
1437
1438 ide_state = qemu_mallocz(sizeof(IDEState) * 2);
1439 if (!ide_state)
1440 return;
1441
1442 for(i = 0; i < 2; i++) {
1443 s = ide_state + i;
1444 if (i == 0)
1445 s->bs = hd0;
1446 else
1447 s->bs = hd1;
1448 if (s->bs) {
1449 bdrv_get_geometry(s->bs, &nb_sectors);
1450 s->nb_sectors = nb_sectors;
1451 /* if a geometry hint is available, use it */
1452 bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
1453 if (cylinders != 0) {
1454 s->cylinders = cylinders;
1455 s->heads = heads;
1456 s->sectors = secs;
1457 } else {
1458 ide_guess_geometry(s);
1459 if (s->cylinders == 0) {
1460 /* if no geometry, use a LBA compatible one */
1461 cylinders = nb_sectors / (16 * 63);
1462 if (cylinders > 16383)
1463 cylinders = 16383;
1464 else if (cylinders < 2)
1465 cylinders = 2;
1466 s->cylinders = cylinders;
1467 s->heads = 16;
1468 s->sectors = 63;
1469 }
1470 }
1471 if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM) {
1472 s->is_cdrom = 1;
1473 bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
1474 }
1475 }
1476 s->irq = irq;
1477 ide_reset(s);
1478 }
1479 register_ioport_write(iobase, 8, 1, ide_ioport_write, ide_state);
1480 register_ioport_read(iobase, 8, 1, ide_ioport_read, ide_state);
1481 if (iobase2) {
1482 register_ioport_read(iobase2, 1, 1, ide_status_read, ide_state);
1483 register_ioport_write(iobase2, 1, 1, ide_cmd_write, ide_state);
1484 }
1485
1486 /* data ports */
1487 register_ioport_write(iobase, 2, 2, ide_data_writew, ide_state);
1488 register_ioport_read(iobase, 2, 2, ide_data_readw, ide_state);
1489 register_ioport_write(iobase, 4, 4, ide_data_writel, ide_state);
1490 register_ioport_read(iobase, 4, 4, ide_data_readl, ide_state);
1491 }