Statistics
| Branch: | Revision:

root / hw / ide.c @ 7f777bf3

History | View | Annotate | Download (43.9 kB)

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 <stdlib.h>
25
#include <stdio.h>
26
#include <stdarg.h>
27
#include <string.h>
28
#include <getopt.h>
29
#include <inttypes.h>
30
#include <unistd.h>
31
#include <sys/mman.h>
32
#include <fcntl.h>
33
#include <signal.h>
34
#include <time.h>
35
#include <sys/time.h>
36
#include <malloc.h>
37
#include <termios.h>
38
#include <sys/poll.h>
39
#include <errno.h>
40
#include <sys/wait.h>
41
#include <netinet/in.h>
42

    
43
#define NO_THUNK_TYPE_SIZE
44
#include "thunk.h"
45

    
46
#include "cpu.h"
47
#include "exec-all.h"
48

    
49
#include "vl.h"
50

    
51
/* debug IDE devices */
52
//#define DEBUG_IDE
53
//#define DEBUG_IDE_ATAPI
54

    
55
/* Bits of HD_STATUS */
56
#define ERR_STAT                0x01
57
#define INDEX_STAT                0x02
58
#define ECC_STAT                0x04        /* Corrected error */
59
#define DRQ_STAT                0x08
60
#define SEEK_STAT                0x10
61
#define SRV_STAT                0x10
62
#define WRERR_STAT                0x20
63
#define READY_STAT                0x40
64
#define BUSY_STAT                0x80
65

    
66
/* Bits for HD_ERROR */
67
#define MARK_ERR                0x01        /* Bad address mark */
68
#define TRK0_ERR                0x02        /* couldn't find track 0 */
69
#define ABRT_ERR                0x04        /* Command aborted */
70
#define MCR_ERR                        0x08        /* media change request */
71
#define ID_ERR                        0x10        /* ID field not found */
72
#define MC_ERR                        0x20        /* media changed */
73
#define ECC_ERR                        0x40        /* Uncorrectable ECC error */
74
#define BBD_ERR                        0x80        /* pre-EIDE meaning:  block marked bad */
75
#define ICRC_ERR                0x80        /* new meaning:  CRC error during transfer */
76

    
77
/* Bits of HD_NSECTOR */
78
#define CD                        0x01
79
#define IO                        0x02
80
#define REL                        0x04
81
#define TAG_MASK                0xf8
82

    
83
#define IDE_CMD_RESET           0x04
84
#define IDE_CMD_DISABLE_IRQ     0x02
85

    
86
/* ATA/ATAPI Commands pre T13 Spec */
87
#define WIN_NOP                                0x00
88
/*
89
 *        0x01->0x02 Reserved
90
 */
91
#define CFA_REQ_EXT_ERROR_CODE                0x03 /* CFA Request Extended Error Code */
92
/*
93
 *        0x04->0x07 Reserved
94
 */
95
#define WIN_SRST                        0x08 /* ATAPI soft reset command */
96
#define WIN_DEVICE_RESET                0x08
97
/*
98
 *        0x09->0x0F Reserved
99
 */
100
#define WIN_RECAL                        0x10
101
#define WIN_RESTORE                        WIN_RECAL
102
/*
103
 *        0x10->0x1F Reserved
104
 */
105
#define WIN_READ                        0x20 /* 28-Bit */
106
#define WIN_READ_ONCE                        0x21 /* 28-Bit without retries */
107
#define WIN_READ_LONG                        0x22 /* 28-Bit */
108
#define WIN_READ_LONG_ONCE                0x23 /* 28-Bit without retries */
109
#define WIN_READ_EXT                        0x24 /* 48-Bit */
110
#define WIN_READDMA_EXT                        0x25 /* 48-Bit */
111
#define WIN_READDMA_QUEUED_EXT                0x26 /* 48-Bit */
112
#define WIN_READ_NATIVE_MAX_EXT                0x27 /* 48-Bit */
113
/*
114
 *        0x28
115
 */
116
#define WIN_MULTREAD_EXT                0x29 /* 48-Bit */
117
/*
118
 *        0x2A->0x2F Reserved
119
 */
120
#define WIN_WRITE                        0x30 /* 28-Bit */
121
#define WIN_WRITE_ONCE                        0x31 /* 28-Bit without retries */
122
#define WIN_WRITE_LONG                        0x32 /* 28-Bit */
123
#define WIN_WRITE_LONG_ONCE                0x33 /* 28-Bit without retries */
124
#define WIN_WRITE_EXT                        0x34 /* 48-Bit */
125
#define WIN_WRITEDMA_EXT                0x35 /* 48-Bit */
126
#define WIN_WRITEDMA_QUEUED_EXT                0x36 /* 48-Bit */
127
#define WIN_SET_MAX_EXT                        0x37 /* 48-Bit */
128
#define CFA_WRITE_SECT_WO_ERASE                0x38 /* CFA Write Sectors without erase */
129
#define WIN_MULTWRITE_EXT                0x39 /* 48-Bit */
130
/*
131
 *        0x3A->0x3B Reserved
132
 */
133
#define WIN_WRITE_VERIFY                0x3C /* 28-Bit */
134
/*
135
 *        0x3D->0x3F Reserved
136
 */
137
#define WIN_VERIFY                        0x40 /* 28-Bit - Read Verify Sectors */
138
#define WIN_VERIFY_ONCE                        0x41 /* 28-Bit - without retries */
139
#define WIN_VERIFY_EXT                        0x42 /* 48-Bit */
140
/*
141
 *        0x43->0x4F Reserved
142
 */
143
#define WIN_FORMAT                        0x50
144
/*
145
 *        0x51->0x5F Reserved
146
 */
147
#define WIN_INIT                        0x60
148
/*
149
 *        0x61->0x5F Reserved
150
 */
151
#define WIN_SEEK                        0x70 /* 0x70-0x7F Reserved */
152
#define CFA_TRANSLATE_SECTOR                0x87 /* CFA Translate Sector */
153
#define WIN_DIAGNOSE                        0x90
154
#define WIN_SPECIFY                        0x91 /* set drive geometry translation */
155
#define WIN_DOWNLOAD_MICROCODE                0x92
156
#define WIN_STANDBYNOW2                        0x94
157
#define WIN_STANDBY2                        0x96
158
#define WIN_SETIDLE2                        0x97
159
#define WIN_CHECKPOWERMODE2                0x98
160
#define WIN_SLEEPNOW2                        0x99
161
/*
162
 *        0x9A VENDOR
163
 */
164
#define WIN_PACKETCMD                        0xA0 /* Send a packet command. */
165
#define WIN_PIDENTIFY                        0xA1 /* identify ATAPI device        */
166
#define WIN_QUEUED_SERVICE                0xA2
167
#define WIN_SMART                        0xB0 /* self-monitoring and reporting */
168
#define CFA_ERASE_SECTORS               0xC0
169
#define WIN_MULTREAD                        0xC4 /* read sectors using multiple mode*/
170
#define WIN_MULTWRITE                        0xC5 /* write sectors using multiple mode */
171
#define WIN_SETMULT                        0xC6 /* enable/disable multiple mode */
172
#define WIN_READDMA_QUEUED                0xC7 /* read sectors using Queued DMA transfers */
173
#define WIN_READDMA                        0xC8 /* read sectors using DMA transfers */
174
#define WIN_READDMA_ONCE                0xC9 /* 28-Bit - without retries */
175
#define WIN_WRITEDMA                        0xCA /* write sectors using DMA transfers */
176
#define WIN_WRITEDMA_ONCE                0xCB /* 28-Bit - without retries */
177
#define WIN_WRITEDMA_QUEUED                0xCC /* write sectors using Queued DMA transfers */
178
#define CFA_WRITE_MULTI_WO_ERASE        0xCD /* CFA Write multiple without erase */
179
#define WIN_GETMEDIASTATUS                0xDA        
180
#define WIN_ACKMEDIACHANGE                0xDB /* ATA-1, ATA-2 vendor */
181
#define WIN_POSTBOOT                        0xDC
182
#define WIN_PREBOOT                        0xDD
183
#define WIN_DOORLOCK                        0xDE /* lock door on removable drives */
184
#define WIN_DOORUNLOCK                        0xDF /* unlock door on removable drives */
185
#define WIN_STANDBYNOW1                        0xE0
186
#define WIN_IDLEIMMEDIATE                0xE1 /* force drive to become "ready" */
187
#define WIN_STANDBY                     0xE2 /* Set device in Standby Mode */
188
#define WIN_SETIDLE1                        0xE3
189
#define WIN_READ_BUFFER                        0xE4 /* force read only 1 sector */
190
#define WIN_CHECKPOWERMODE1                0xE5
191
#define WIN_SLEEPNOW1                        0xE6
192
#define WIN_FLUSH_CACHE                        0xE7
193
#define WIN_WRITE_BUFFER                0xE8 /* force write only 1 sector */
194
#define WIN_WRITE_SAME                        0xE9 /* read ata-2 to use */
195
        /* SET_FEATURES 0x22 or 0xDD */
196
#define WIN_FLUSH_CACHE_EXT                0xEA /* 48-Bit */
197
#define WIN_IDENTIFY                        0xEC /* ask drive to identify itself        */
198
#define WIN_MEDIAEJECT                        0xED
199
#define WIN_IDENTIFY_DMA                0xEE /* same as WIN_IDENTIFY, but DMA */
200
#define WIN_SETFEATURES                        0xEF /* set special drive features */
201
#define EXABYTE_ENABLE_NEST                0xF0
202
#define WIN_SECURITY_SET_PASS                0xF1
203
#define WIN_SECURITY_UNLOCK                0xF2
204
#define WIN_SECURITY_ERASE_PREPARE        0xF3
205
#define WIN_SECURITY_ERASE_UNIT                0xF4
206
#define WIN_SECURITY_FREEZE_LOCK        0xF5
207
#define WIN_SECURITY_DISABLE                0xF6
208
#define WIN_READ_NATIVE_MAX                0xF8 /* return the native maximum address */
209
#define WIN_SET_MAX                        0xF9
210
#define DISABLE_SEAGATE                        0xFB
211

    
212
/* set to 1 set disable mult support */
213
#define MAX_MULT_SECTORS 8
214

    
215
/* ATAPI defines */
216

    
217
#define ATAPI_PACKET_SIZE 12
218

    
219
/* The generic packet command opcodes for CD/DVD Logical Units,
220
 * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
221
#define GPCMD_BLANK                            0xa1
222
#define GPCMD_CLOSE_TRACK                    0x5b
223
#define GPCMD_FLUSH_CACHE                    0x35
224
#define GPCMD_FORMAT_UNIT                    0x04
225
#define GPCMD_GET_CONFIGURATION                    0x46
226
#define GPCMD_GET_EVENT_STATUS_NOTIFICATION 0x4a
227
#define GPCMD_GET_PERFORMANCE                    0xac
228
#define GPCMD_INQUIRY                            0x12
229
#define GPCMD_LOAD_UNLOAD                    0xa6
230
#define GPCMD_MECHANISM_STATUS                    0xbd
231
#define GPCMD_MODE_SELECT_10                    0x55
232
#define GPCMD_MODE_SENSE_10                    0x5a
233
#define GPCMD_PAUSE_RESUME                    0x4b
234
#define GPCMD_PLAY_AUDIO_10                    0x45
235
#define GPCMD_PLAY_AUDIO_MSF                    0x47
236
#define GPCMD_PLAY_AUDIO_TI                    0x48
237
#define GPCMD_PLAY_CD                            0xbc
238
#define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL  0x1e
239
#define GPCMD_READ_10                            0x28
240
#define GPCMD_READ_12                            0xa8
241
#define GPCMD_READ_CDVD_CAPACITY            0x25
242
#define GPCMD_READ_CD                            0xbe
243
#define GPCMD_READ_CD_MSF                    0xb9
244
#define GPCMD_READ_DISC_INFO                    0x51
245
#define GPCMD_READ_DVD_STRUCTURE            0xad
246
#define GPCMD_READ_FORMAT_CAPACITIES            0x23
247
#define GPCMD_READ_HEADER                    0x44
248
#define GPCMD_READ_TRACK_RZONE_INFO            0x52
249
#define GPCMD_READ_SUBCHANNEL                    0x42
250
#define GPCMD_READ_TOC_PMA_ATIP                    0x43
251
#define GPCMD_REPAIR_RZONE_TRACK            0x58
252
#define GPCMD_REPORT_KEY                    0xa4
253
#define GPCMD_REQUEST_SENSE                    0x03
254
#define GPCMD_RESERVE_RZONE_TRACK            0x53
255
#define GPCMD_SCAN                            0xba
256
#define GPCMD_SEEK                            0x2b
257
#define GPCMD_SEND_DVD_STRUCTURE            0xad
258
#define GPCMD_SEND_EVENT                    0xa2
259
#define GPCMD_SEND_KEY                            0xa3
260
#define GPCMD_SEND_OPC                            0x54
261
#define GPCMD_SET_READ_AHEAD                    0xa7
262
#define GPCMD_SET_STREAMING                    0xb6
263
#define GPCMD_START_STOP_UNIT                    0x1b
264
#define GPCMD_STOP_PLAY_SCAN                    0x4e
265
#define GPCMD_TEST_UNIT_READY                    0x00
266
#define GPCMD_VERIFY_10                            0x2f
267
#define GPCMD_WRITE_10                            0x2a
268
#define GPCMD_WRITE_AND_VERIFY_10            0x2e
269
/* This is listed as optional in ATAPI 2.6, but is (curiously) 
270
 * missing from Mt. Fuji, Table 57.  It _is_ mentioned in Mt. Fuji
271
 * Table 377 as an MMC command for SCSi devices though...  Most ATAPI
272
 * drives support it. */
273
#define GPCMD_SET_SPEED                            0xbb
274
/* This seems to be a SCSI specific CD-ROM opcode 
275
 * to play data at track/index */
276
#define GPCMD_PLAYAUDIO_TI                    0x48
277
/*
278
 * From MS Media Status Notification Support Specification. For
279
 * older drives only.
280
 */
281
#define GPCMD_GET_MEDIA_STATUS                    0xda
282

    
283
/* Mode page codes for mode sense/set */
284
#define GPMODE_R_W_ERROR_PAGE                0x01
285
#define GPMODE_WRITE_PARMS_PAGE                0x05
286
#define GPMODE_AUDIO_CTL_PAGE                0x0e
287
#define GPMODE_POWER_PAGE                0x1a
288
#define GPMODE_FAULT_FAIL_PAGE                0x1c
289
#define GPMODE_TO_PROTECT_PAGE                0x1d
290
#define GPMODE_CAPABILITIES_PAGE        0x2a
291
#define GPMODE_ALL_PAGES                0x3f
292
/* Not in Mt. Fuji, but in ATAPI 2.6 -- depricated now in favor
293
 * of MODE_SENSE_POWER_PAGE */
294
#define GPMODE_CDROM_PAGE                0x0d
295

    
296
#define ATAPI_INT_REASON_CD             0x01 /* 0 = data transfer */
297
#define ATAPI_INT_REASON_IO             0x02 /* 1 = transfer to the host */
298
#define ATAPI_INT_REASON_REL            0x04
299
#define ATAPI_INT_REASON_TAG            0xf8
300

    
301
/* same constants as bochs */
302
#define ASC_ILLEGAL_OPCODE                   0x20
303
#define ASC_LOGICAL_BLOCK_OOR                0x21
304
#define ASC_INV_FIELD_IN_CMD_PACKET          0x24
305
#define ASC_MEDIUM_NOT_PRESENT               0x3a
306
#define ASC_SAVING_PARAMETERS_NOT_SUPPORTED  0x39
307

    
308
#define SENSE_NONE            0
309
#define SENSE_NOT_READY       2
310
#define SENSE_ILLEGAL_REQUEST 5
311
#define SENSE_UNIT_ATTENTION  6
312

    
313
struct IDEState;
314

    
315
typedef void EndTransferFunc(struct IDEState *);
316

    
317
typedef struct IDEState {
318
    /* ide config */
319
    int is_cdrom;
320
    int cdrom_locked;
321
    int cylinders, heads, sectors;
322
    int64_t nb_sectors;
323
    int mult_sectors;
324
    int irq;
325
    /* ide regs */
326
    uint8_t feature;
327
    uint8_t error;
328
    uint16_t nsector; /* 0 is 256 to ease computations */
329
    uint8_t sector;
330
    uint8_t lcyl;
331
    uint8_t hcyl;
332
    uint8_t select;
333
    uint8_t status;
334
    /* 0x3f6 command, only meaningful for drive 0 */
335
    uint8_t cmd;
336
    /* depends on bit 4 in select, only meaningful for drive 0 */
337
    struct IDEState *cur_drive; 
338
    BlockDriverState *bs;
339
    /* ATAPI specific */
340
    uint8_t sense_key;
341
    uint8_t asc;
342
    int packet_transfer_size;
343
    int elementary_transfer_size;
344
    int io_buffer_index;
345
    int lba;
346
    /* transfer handling */
347
    int req_nb_sectors; /* number of sectors per interrupt */
348
    EndTransferFunc *end_transfer_func;
349
    uint8_t *data_ptr;
350
    uint8_t *data_end;
351
    uint8_t io_buffer[MAX_MULT_SECTORS*512 + 4];
352
} IDEState;
353

    
354
IDEState ide_state[MAX_DISKS];
355
IDEState *ide_table[0x400 >> 3];
356

    
357
static inline IDEState *get_ide_interface(uint32_t addr)
358
{
359
    return ide_table[addr >> 3];
360
}
361

    
362
static void padstr(char *str, const char *src, int len)
363
{
364
    int i, v;
365
    for(i = 0; i < len; i++) {
366
        if (*src)
367
            v = *src++;
368
        else
369
            v = ' ';
370
        *(char *)((long)str ^ 1) = v;
371
        str++;
372
    }
373
}
374

    
375
static void padstr8(uint8_t *buf, int buf_size, const char *src)
376
{
377
    int i;
378
    for(i = 0; i < buf_size; i++) {
379
        if (*src)
380
            buf[i] = *src++;
381
        else
382
            buf[i] = ' ';
383
    }
384
}
385

    
386
static void ide_identify(IDEState *s)
387
{
388
    uint16_t *p;
389
    unsigned int oldsize;
390

    
391
    memset(s->io_buffer, 0, 512);
392
    p = (uint16_t *)s->io_buffer;
393
    stw_raw(p + 0, 0x0040);
394
    stw_raw(p + 1, s->cylinders); 
395
    stw_raw(p + 3, s->heads);
396
    stw_raw(p + 4, 512 * s->sectors); /* sectors */
397
    stw_raw(p + 5, 512); /* sector size */
398
    stw_raw(p + 6, s->sectors); 
399
    padstr((uint8_t *)(p + 10), "QM00001", 20); /* serial number */
400
    stw_raw(p + 20, 3); /* buffer type */
401
    stw_raw(p + 21, 512); /* cache size in sectors */
402
    stw_raw(p + 22, 4); /* ecc bytes */
403
    padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */
404
    padstr((uint8_t *)(p + 27), "QEMU HARDDISK", 40); /* model */
405
#if MAX_MULT_SECTORS > 1    
406
    stw_raw(p + 47, MAX_MULT_SECTORS);
407
#endif
408
    stw_raw(p + 48, 1); /* dword I/O */
409
    stw_raw(p + 49, 1 << 9); /* LBA supported, no DMA */
410
    stw_raw(p + 51, 0x200); /* PIO transfer cycle */
411
    stw_raw(p + 52, 0x200); /* DMA transfer cycle */
412
    stw_raw(p + 54, s->cylinders);
413
    stw_raw(p + 55, s->heads);
414
    stw_raw(p + 56, s->sectors);
415
    oldsize = s->cylinders * s->heads * s->sectors;
416
    stw_raw(p + 57, oldsize);
417
    stw_raw(p + 58, oldsize >> 16);
418
    if (s->mult_sectors)
419
        stw_raw(p + 59, 0x100 | s->mult_sectors);
420
    stw_raw(p + 60, s->nb_sectors);
421
    stw_raw(p + 61, s->nb_sectors >> 16);
422
    stw_raw(p + 80, (1 << 1) | (1 << 2));
423
    stw_raw(p + 82, (1 << 14));
424
    stw_raw(p + 83, (1 << 14));
425
    stw_raw(p + 84, (1 << 14));
426
    stw_raw(p + 85, (1 << 14));
427
    stw_raw(p + 86, 0);
428
    stw_raw(p + 87, (1 << 14));
429
}
430

    
431
static void ide_atapi_identify(IDEState *s)
432
{
433
    uint16_t *p;
434

    
435
    memset(s->io_buffer, 0, 512);
436
    p = (uint16_t *)s->io_buffer;
437
    /* Removable CDROM, 50us response, 12 byte packets */
438
    stw_raw(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
439
    stw_raw(p + 1, s->cylinders); 
440
    stw_raw(p + 3, s->heads);
441
    stw_raw(p + 4, 512 * s->sectors); /* sectors */
442
    stw_raw(p + 5, 512); /* sector size */
443
    stw_raw(p + 6, s->sectors); 
444
    padstr((uint8_t *)(p + 10), "QM00001", 20); /* serial number */
445
    stw_raw(p + 20, 3); /* buffer type */
446
    stw_raw(p + 21, 512); /* cache size in sectors */
447
    stw_raw(p + 22, 4); /* ecc bytes */
448
    padstr((uint8_t *)(p + 23), QEMU_VERSION, 8); /* firmware version */
449
    padstr((uint8_t *)(p + 27), "QEMU CD-ROM", 40); /* model */
450
    stw_raw(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
451
    stw_raw(p + 49, 1 << 9); /* LBA supported, no DMA */
452
    stw_raw(p + 53, 3); /* words 64-70, 54-58 valid */
453
    stw_raw(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
454
    stw_raw(p + 64, 1); /* PIO modes */
455
    stw_raw(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
456
    stw_raw(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
457
    stw_raw(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
458
    stw_raw(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
459
    
460
    stw_raw(p + 71, 30); /* in ns */
461
    stw_raw(p + 72, 30); /* in ns */
462

    
463
    stw_raw(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
464
}
465

    
466
static void ide_set_signature(IDEState *s)
467
{
468
    s->select &= 0xf0; /* clear head */
469
    /* put signature */
470
    s->nsector = 1;
471
    s->sector = 1;
472
    if (s->is_cdrom) {
473
        s->lcyl = 0x14;
474
        s->hcyl = 0xeb;
475
    } else if (s->bs) {
476
        s->lcyl = 0;
477
        s->hcyl = 0;
478
    } else {
479
        s->lcyl = 0xff;
480
        s->hcyl = 0xff;
481
    }
482
}
483

    
484
static inline void ide_abort_command(IDEState *s)
485
{
486
    s->status = READY_STAT | ERR_STAT;
487
    s->error = ABRT_ERR;
488
}
489

    
490
static inline void ide_set_irq(IDEState *s)
491
{
492
    if (!(s->cmd & IDE_CMD_DISABLE_IRQ)) {
493
        pic_set_irq(s->irq, 1);
494
    }
495
}
496

    
497
/* prepare data transfer and tell what to do after */
498
static void ide_transfer_start(IDEState *s, uint8_t *buf, int size, 
499
                               EndTransferFunc *end_transfer_func)
500
{
501
    s->end_transfer_func = end_transfer_func;
502
    s->data_ptr = buf;
503
    s->data_end = buf + size;
504
    s->status |= DRQ_STAT;
505
}
506

    
507
static void ide_transfer_stop(IDEState *s)
508
{
509
    s->end_transfer_func = ide_transfer_stop;
510
    s->data_ptr = s->io_buffer;
511
    s->data_end = s->io_buffer;
512
    s->status &= ~DRQ_STAT;
513
}
514

    
515
static int64_t ide_get_sector(IDEState *s)
516
{
517
    int64_t sector_num;
518
    if (s->select & 0x40) {
519
        /* lba */
520
        sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) | 
521
            (s->lcyl << 8) | s->sector;
522
    } else {
523
        sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
524
            (s->select & 0x0f) * s->sectors + 
525
            (s->sector - 1);
526
    }
527
    return sector_num;
528
}
529

    
530
static void ide_set_sector(IDEState *s, int64_t sector_num)
531
{
532
    unsigned int cyl, r;
533
    if (s->select & 0x40) {
534
        s->select = (s->select & 0xf0) | (sector_num >> 24);
535
        s->hcyl = (sector_num >> 16);
536
        s->lcyl = (sector_num >> 8);
537
        s->sector = (sector_num);
538
    } else {
539
        cyl = sector_num / (s->heads * s->sectors);
540
        r = sector_num % (s->heads * s->sectors);
541
        s->hcyl = cyl >> 8;
542
        s->lcyl = cyl;
543
        s->select = (s->select & 0xf0) | (r / s->sectors);
544
        s->sector = (r % s->sectors) + 1;
545
    }
546
}
547

    
548
static void ide_sector_read(IDEState *s)
549
{
550
    int64_t sector_num;
551
    int ret, n;
552

    
553
    s->status = READY_STAT | SEEK_STAT;
554
    s->error = 0; /* not needed by IDE spec, but needed by Windows */
555
    sector_num = ide_get_sector(s);
556
    n = s->nsector;
557
    if (n == 0) {
558
        /* no more sector to read from disk */
559
        ide_transfer_stop(s);
560
    } else {
561
#if defined(DEBUG_IDE)
562
        printf("read sector=%Ld\n", sector_num);
563
#endif
564
        if (n > s->req_nb_sectors)
565
            n = s->req_nb_sectors;
566
        ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
567
        ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
568
        ide_set_irq(s);
569
        ide_set_sector(s, sector_num + n);
570
        s->nsector -= n;
571
    }
572
}
573

    
574
static void ide_sector_write(IDEState *s)
575
{
576
    int64_t sector_num;
577
    int ret, n, n1;
578

    
579
    s->status = READY_STAT | SEEK_STAT;
580
    sector_num = ide_get_sector(s);
581
#if defined(DEBUG_IDE)
582
    printf("write sector=%Ld\n", sector_num);
583
#endif
584
    n = s->nsector;
585
    if (n > s->req_nb_sectors)
586
        n = s->req_nb_sectors;
587
    ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
588
    s->nsector -= n;
589
    if (s->nsector == 0) {
590
        /* no more sector to write */
591
        ide_transfer_stop(s);
592
    } else {
593
        n1 = s->nsector;
594
        if (n1 > s->req_nb_sectors)
595
            n1 = s->req_nb_sectors;
596
        ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
597
    }
598
    ide_set_sector(s, sector_num + n);
599
    ide_set_irq(s);
600
}
601

    
602
static void ide_atapi_cmd_ok(IDEState *s)
603
{
604
    s->error = 0;
605
    s->status = READY_STAT;
606
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
607
    ide_set_irq(s);
608
}
609

    
610
static void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
611
{
612
#ifdef DEBUG_IDE_ATAPI
613
    printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
614
#endif
615
    s->error = sense_key << 4;
616
    s->status = READY_STAT | ERR_STAT;
617
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
618
    s->sense_key = sense_key;
619
    s->asc = asc;
620
    ide_set_irq(s);
621
}
622

    
623
static inline void cpu_to_ube16(uint8_t *buf, int val)
624
{
625
    buf[0] = val >> 8;
626
    buf[1] = val;
627
}
628

    
629
static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
630
{
631
    buf[0] = val >> 24;
632
    buf[1] = val >> 16;
633
    buf[2] = val >> 8;
634
    buf[3] = val;
635
}
636

    
637
static inline int ube16_to_cpu(const uint8_t *buf)
638
{
639
    return (buf[0] << 8) | buf[1];
640
}
641

    
642
static inline int ube32_to_cpu(const uint8_t *buf)
643
{
644
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
645
}
646

    
647
/* The whole ATAPI transfer logic is handled in this function */
648
static void ide_atapi_cmd_reply_end(IDEState *s)
649
{
650
    int byte_count_limit, size;
651
#ifdef DEBUG_IDE_ATAPI
652
    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n", 
653
           s->packet_transfer_size,
654
           s->elementary_transfer_size,
655
           s->io_buffer_index);
656
#endif
657
    if (s->packet_transfer_size <= 0) {
658
        /* end of transfer */
659
        ide_transfer_stop(s);
660
        s->status = READY_STAT;
661
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
662
        ide_set_irq(s);
663
#ifdef DEBUG_IDE_ATAPI
664
        printf("status=0x%x\n", s->status);
665
#endif
666
    } else {
667
        /* see if a new sector must be read */
668
        if (s->lba != -1 && s->io_buffer_index >= 2048) {
669
            bdrv_read(s->bs, (int64_t)s->lba << 2, s->io_buffer, 4);
670
            s->lba++;
671
            s->io_buffer_index = 0;
672
        }
673
        if (s->elementary_transfer_size > 0) {
674
            /* there are some data left to transmit in this elementary
675
               transfer */
676
            size = 2048 - s->io_buffer_index;
677
            if (size > s->elementary_transfer_size)
678
                size = s->elementary_transfer_size;
679
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index, 
680
                               size, ide_atapi_cmd_reply_end);
681
            s->packet_transfer_size -= size;
682
            s->elementary_transfer_size -= size;
683
            s->io_buffer_index += size;
684
        } else {
685
            /* a new transfer is needed */
686
            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
687
            byte_count_limit = s->lcyl | (s->hcyl << 8);
688
#ifdef DEBUG_IDE_ATAPI
689
            printf("byte_count_limit=%d\n", byte_count_limit);
690
#endif
691
            if (byte_count_limit == 0xffff)
692
                byte_count_limit--;
693
            size = s->packet_transfer_size;
694
            if (size > byte_count_limit) {
695
                /* byte count limit must be even if this case */
696
                if (byte_count_limit & 1)
697
                    byte_count_limit--;
698
                size = byte_count_limit;
699
            }
700
            s->lcyl = size;
701
            s->hcyl = size >> 8;
702
            s->elementary_transfer_size = size;
703
            /* we cannot transmit more than one sector at a time */
704
            if (s->lba != -1) {
705
                if (size > (2048 - s->io_buffer_index))
706
                    size = (2048 - s->io_buffer_index);
707
            }
708
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index, 
709
                               size, ide_atapi_cmd_reply_end);
710
            s->packet_transfer_size -= size;
711
            s->elementary_transfer_size -= size;
712
            s->io_buffer_index += size;
713
            ide_set_irq(s);
714
#ifdef DEBUG_IDE_ATAPI
715
            printf("status=0x%x\n", s->status);
716
#endif
717
        }
718
    }
719
}
720

    
721
/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
722
static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
723
{
724
    if (size > max_size)
725
        size = max_size;
726
    s->lba = -1; /* no sector read */
727
    s->packet_transfer_size = size;
728
    s->elementary_transfer_size = 0;
729
    s->io_buffer_index = 0;
730

    
731
    s->status = READY_STAT;
732
    ide_atapi_cmd_reply_end(s);
733
}
734

    
735
/* start a CD-CDROM read command */
736
static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors)
737
{
738
#ifdef DEBUG_IDE_ATAPI
739
    printf("read: LBA=%d nb_sectors=%d\n", lba, nb_sectors);
740
#endif
741
    s->lba = lba;
742
    s->packet_transfer_size = nb_sectors * 2048;
743
    s->elementary_transfer_size = 0;
744
    s->io_buffer_index = 2048;
745

    
746
    s->status = READY_STAT;
747
    ide_atapi_cmd_reply_end(s);
748
}
749

    
750
/* same toc as bochs. Return -1 if error or the toc length */
751
static int cdrom_read_toc(IDEState *s, uint8_t *buf, int msf, int start_track)
752
{
753
    uint8_t *q;
754
    int nb_sectors, len;
755
    
756
    if (start_track > 1 && start_track != 0xaa)
757
        return -1;
758
    q = buf + 2;
759
    *q++ = 1;
760
    *q++ = 1;
761
    if (start_track <= 1) {
762
        *q++ = 0; /* reserved */
763
        *q++ = 0x14; /* ADR, control */
764
        *q++ = 1;    /* track number */
765
        *q++ = 0; /* reserved */
766
        if (msf) {
767
            *q++ = 0; /* reserved */
768
            *q++ = 0; /* minute */
769
            *q++ = 2; /* second */
770
            *q++ = 0; /* frame */
771
        } else {
772
            /* sector 0 */
773
            cpu_to_ube32(q, 0);
774
            q += 4;
775
        }
776
    }
777
    /* lead out track */
778
    *q++ = 0; /* reserved */
779
    *q++ = 0x16; /* ADR, control */
780
    *q++ = 0xaa; /* track number */
781
    *q++ = 0; /* reserved */
782
    nb_sectors = s->nb_sectors >> 2;
783
    if (msf) {
784
        *q++ = 0; /* reserved */
785
        *q++ = ((nb_sectors + 150) / 75) / 60;
786
        *q++ = ((nb_sectors + 150) / 75) % 60;
787
        *q++ = (nb_sectors + 150) % 75;
788
    } else {
789
        cpu_to_ube32(q, nb_sectors);
790
        q += 4;
791
    }
792
    len = q - buf;
793
    cpu_to_ube16(buf, len - 2);
794
    return len;
795
}
796

    
797
static void ide_atapi_cmd(IDEState *s)
798
{
799
    const uint8_t *packet;
800
    uint8_t *buf;
801
    int max_len;
802

    
803
    packet = s->io_buffer;
804
    buf = s->io_buffer;
805
#ifdef DEBUG_IDE_ATAPI
806
    {
807
        int i;
808
        printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
809
        for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
810
            printf(" %02x", packet[i]);
811
        }
812
        printf("\n");
813
    }
814
#endif
815
    switch(s->io_buffer[0]) {
816
    case GPCMD_TEST_UNIT_READY:
817
        if (s->bs) {
818
            ide_atapi_cmd_ok(s);
819
        } else {
820
            ide_atapi_cmd_error(s, SENSE_NOT_READY, 
821
                                ASC_MEDIUM_NOT_PRESENT);
822
        }
823
        break;
824
    case GPCMD_MODE_SENSE_10:
825
        {
826
            int action, code;
827
            max_len = ube16_to_cpu(packet + 7);
828
            action = packet[2] >> 6;
829
            code = packet[2] & 0x3f;
830
            switch(action) {
831
            case 0: /* current values */
832
                switch(code) {
833
                case 0x01: /* error recovery */
834
                    cpu_to_ube16(&buf[0], 16 + 6);
835
                    buf[2] = 0x70;
836
                    buf[3] = 0;
837
                    buf[4] = 0;
838
                    buf[5] = 0;
839
                    buf[6] = 0;
840
                    buf[7] = 0;
841

    
842
                    buf[8] = 0x01;
843
                    buf[9] = 0x06;
844
                    buf[10] = 0x00;
845
                    buf[11] = 0x05;
846
                    buf[12] = 0x00;
847
                    buf[13] = 0x00;
848
                    buf[14] = 0x00;
849
                    buf[15] = 0x00;
850
                    ide_atapi_cmd_reply(s, 16, max_len);
851
                    break;
852
                case 0x2a:
853
                    cpu_to_ube16(&buf[0], 28 + 6);
854
                    buf[2] = 0x70;
855
                    buf[3] = 0;
856
                    buf[4] = 0;
857
                    buf[5] = 0;
858
                    buf[6] = 0;
859
                    buf[7] = 0;
860

    
861
                    buf[8] = 0x2a;
862
                    buf[9] = 0x12;
863
                    buf[10] = 0x00;
864
                    buf[11] = 0x00;
865
                    
866
                    buf[12] = 0x70;
867
                    buf[13] = 3 << 5;
868
                    buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
869
                    if (s->cdrom_locked)
870
                        buf[6] |= 1 << 1;
871
                    buf[15] = 0x00;
872
                    cpu_to_ube16(&buf[16], 706);
873
                    buf[18] = 0;
874
                    buf[19] = 2;
875
                    cpu_to_ube16(&buf[20], 512);
876
                    cpu_to_ube16(&buf[22], 706);
877
                    buf[24] = 0;
878
                    buf[25] = 0;
879
                    buf[26] = 0;
880
                    buf[27] = 0;
881
                    ide_atapi_cmd_reply(s, 28, max_len);
882
                    break;
883
                default:
884
                    goto error_cmd;
885
                }
886
                break;
887
            case 1: /* changeable values */
888
                goto error_cmd;
889
            case 2: /* default values */
890
                goto error_cmd;
891
            default:
892
            case 3: /* saved values */
893
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, 
894
                                    ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
895
                break;
896
            }
897
        }
898
        break;
899
    case GPCMD_REQUEST_SENSE:
900
        max_len = packet[4];
901
        memset(buf, 0, 18);
902
        buf[0] = 0x70 | (1 << 7);
903
        buf[2] = s->sense_key;
904
        buf[7] = 10;
905
        buf[12] = s->asc;
906
        ide_atapi_cmd_reply(s, 18, max_len);
907
        break;
908
    case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
909
        if (s->bs) {
910
            s->cdrom_locked = packet[4] & 1;
911
            ide_atapi_cmd_ok(s);
912
        } else {
913
            ide_atapi_cmd_error(s, SENSE_NOT_READY, 
914
                                ASC_MEDIUM_NOT_PRESENT);
915
        }
916
        break;
917
    case GPCMD_READ_10:
918
    case GPCMD_READ_12:
919
        {
920
            int nb_sectors, lba;
921

    
922
            if (!s->bs) {
923
                ide_atapi_cmd_error(s, SENSE_NOT_READY, 
924
                                    ASC_MEDIUM_NOT_PRESENT);
925
                break;
926
            }
927
            if (packet[0] == GPCMD_READ_10)
928
                nb_sectors = ube16_to_cpu(packet + 7);
929
            else
930
                nb_sectors = ube32_to_cpu(packet + 6);
931
            lba = ube32_to_cpu(packet + 2);
932
            if (nb_sectors == 0) {
933
                ide_atapi_cmd_ok(s);
934
                break;
935
            }
936
            if (((int64_t)(lba + nb_sectors) << 2) > s->nb_sectors) {
937
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, 
938
                                    ASC_LOGICAL_BLOCK_OOR);
939
                break;
940
            }
941
            ide_atapi_cmd_read(s, lba, nb_sectors);
942
        }
943
        break;
944
    case GPCMD_SEEK:
945
        {
946
            int lba;
947
            if (!s->bs) {
948
                ide_atapi_cmd_error(s, SENSE_NOT_READY, 
949
                                    ASC_MEDIUM_NOT_PRESENT);
950
                break;
951
            }
952
            lba = ube32_to_cpu(packet + 2);
953
            if (((int64_t)lba << 2) > s->nb_sectors) {
954
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, 
955
                                    ASC_LOGICAL_BLOCK_OOR);
956
                break;
957
            }
958
            ide_atapi_cmd_ok(s);
959
        }
960
        break;
961
    case GPCMD_START_STOP_UNIT:
962
        {
963
            int start, eject;
964
            start = packet[4] & 1;
965
            eject = (packet[4] >> 1) & 1;
966
            
967
            /* XXX: currently none implemented */
968
            ide_atapi_cmd_ok(s);
969
        }
970
        break;
971
    case GPCMD_MECHANISM_STATUS:
972
        {
973
            max_len = ube16_to_cpu(packet + 8);
974
            cpu_to_ube16(buf, 0);
975
            /* no current LBA */
976
            buf[2] = 0;
977
            buf[3] = 0;
978
            buf[4] = 0;
979
            buf[5] = 1;
980
            cpu_to_ube16(buf + 6, 0);
981
            ide_atapi_cmd_reply(s, 8, max_len);
982
        }
983
        break;
984
    case GPCMD_READ_TOC_PMA_ATIP:
985
        {
986
            int format, msf, start_track, len;
987

    
988
            if (!s->bs) {
989
                ide_atapi_cmd_error(s, SENSE_NOT_READY, 
990
                                    ASC_MEDIUM_NOT_PRESENT);
991
                break;
992
            }
993
            max_len = ube16_to_cpu(packet + 7);
994
            format = packet[9] >> 6;
995
            msf = (packet[1] >> 1) & 1;
996
            start_track = packet[6];
997
            switch(format) {
998
            case 0:
999
                len = cdrom_read_toc(s, buf, msf, start_track);
1000
                if (len < 0)
1001
                    goto error_cmd;
1002
                ide_atapi_cmd_reply(s, len, max_len);
1003
                break;
1004
            case 1:
1005
                /* multi session : only a single session defined */
1006
                memset(buf, 0, 12);
1007
                buf[1] = 0x0a;
1008
                buf[2] = 0x01;
1009
                buf[3] = 0x01;
1010
                ide_atapi_cmd_reply(s, 12, max_len);
1011
                break;
1012
            default:
1013
            error_cmd:
1014
                ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, 
1015
                                    ASC_INV_FIELD_IN_CMD_PACKET);
1016
                break;
1017
            }
1018
        }
1019
        break;
1020
    case GPCMD_READ_CDVD_CAPACITY:
1021
        if (!s->bs) {
1022
            ide_atapi_cmd_error(s, SENSE_NOT_READY, 
1023
                                ASC_MEDIUM_NOT_PRESENT);
1024
            break;
1025
        }
1026
        /* NOTE: it is really the number of sectors minus 1 */
1027
        cpu_to_ube32(buf, (s->nb_sectors >> 2) - 1);
1028
        cpu_to_ube32(buf + 4, 2048);
1029
        ide_atapi_cmd_reply(s, 8, 8);
1030
        break;
1031
    case GPCMD_INQUIRY:
1032
        max_len = packet[4];
1033
        buf[0] = 0x05; /* CD-ROM */
1034
        buf[1] = 0x80; /* removable */
1035
        buf[2] = 0x00; /* ISO */
1036
        buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1037
        buf[4] = 31; /* additionnal length */
1038
        buf[5] = 0; /* reserved */
1039
        buf[6] = 0; /* reserved */
1040
        buf[7] = 0; /* reserved */
1041
        padstr8(buf + 8, 8, "QEMU");
1042
        padstr8(buf + 16, 16, "QEMU CD-ROM");
1043
        padstr8(buf + 32, 4, QEMU_VERSION);
1044
        ide_atapi_cmd_reply(s, 36, max_len);
1045
        break;
1046
    default:
1047
        ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, 
1048
                            ASC_ILLEGAL_OPCODE);
1049
        break;
1050
    }
1051
}
1052

    
1053
static void ide_ioport_write(CPUState *env, uint32_t addr, uint32_t val)
1054
{
1055
    IDEState *ide_if = get_ide_interface(addr);
1056
    IDEState *s = ide_if->cur_drive;
1057
    int unit, n;
1058

    
1059
#ifdef DEBUG_IDE
1060
    printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1061
#endif
1062
    addr &= 7;
1063
    switch(addr) {
1064
    case 0:
1065
        break;
1066
    case 1:
1067
        s->feature = val;
1068
        break;
1069
    case 2:
1070
        if (val == 0)
1071
            val = 256;
1072
        s->nsector = val;
1073
        break;
1074
    case 3:
1075
        s->sector = val;
1076
        break;
1077
    case 4:
1078
        s->lcyl = val;
1079
        break;
1080
    case 5:
1081
        s->hcyl = val;
1082
        break;
1083
    case 6:
1084
        /* select drive */
1085
        unit = (val >> 4) & 1;
1086
        s = ide_if + unit;
1087
        ide_if->cur_drive = s;
1088
        s->select = val;
1089
        break;
1090
    default:
1091
    case 7:
1092
        /* command */
1093
#if defined(DEBUG_IDE)
1094
        printf("ide: CMD=%02x\n", val);
1095
#endif
1096
        switch(val) {
1097
        case WIN_IDENTIFY:
1098
            if (s->bs && !s->is_cdrom) {
1099
                ide_identify(s);
1100
                s->status = READY_STAT;
1101
                ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1102
            } else {
1103
                if (s->is_cdrom) {
1104
                    ide_set_signature(s);
1105
                }
1106
                ide_abort_command(s);
1107
            }
1108
            ide_set_irq(s);
1109
            break;
1110
        case WIN_SPECIFY:
1111
        case WIN_RECAL:
1112
            s->error = 0;
1113
            s->status = READY_STAT;
1114
            ide_set_irq(s);
1115
            break;
1116
        case WIN_SETMULT:
1117
            if (s->nsector > MAX_MULT_SECTORS || 
1118
                s->nsector == 0 ||
1119
                (s->nsector & (s->nsector - 1)) != 0) {
1120
                ide_abort_command(s);
1121
            } else {
1122
                s->mult_sectors = s->nsector;
1123
                s->status = READY_STAT;
1124
            }
1125
            ide_set_irq(s);
1126
            break;
1127
        case WIN_VERIFY:
1128
        case WIN_VERIFY_ONCE:
1129
            /* do sector number check ? */
1130
            s->status = READY_STAT;
1131
            ide_set_irq(s);
1132
            break;
1133
        case WIN_READ:
1134
        case WIN_READ_ONCE:
1135
            if (!s->bs) 
1136
                goto abort_cmd;
1137
            s->req_nb_sectors = 1;
1138
            ide_sector_read(s);
1139
            break;
1140
        case WIN_WRITE:
1141
        case WIN_WRITE_ONCE:
1142
            s->error = 0;
1143
            s->status = SEEK_STAT;
1144
            s->req_nb_sectors = 1;
1145
            ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
1146
            break;
1147
        case WIN_MULTREAD:
1148
            if (!s->mult_sectors)
1149
                goto abort_cmd;
1150
            s->req_nb_sectors = s->mult_sectors;
1151
            ide_sector_read(s);
1152
            break;
1153
        case WIN_MULTWRITE:
1154
            if (!s->mult_sectors)
1155
                goto abort_cmd;
1156
            s->error = 0;
1157
            s->status = SEEK_STAT;
1158
            s->req_nb_sectors = s->mult_sectors;
1159
            n = s->nsector;
1160
            if (n > s->req_nb_sectors)
1161
                n = s->req_nb_sectors;
1162
            ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
1163
            break;
1164
        case WIN_READ_NATIVE_MAX:
1165
            ide_set_sector(s, s->nb_sectors - 1);
1166
            s->status = READY_STAT;
1167
            ide_set_irq(s);
1168
            break;
1169
        case WIN_CHECKPOWERMODE1:
1170
            s->nsector = 0xff; /* device active or idle */
1171
            s->status = READY_STAT;
1172
            ide_set_irq(s);
1173
            break;
1174

    
1175
            /* ATAPI commands */
1176
        case WIN_PIDENTIFY:
1177
            if (s->is_cdrom) {
1178
                ide_atapi_identify(s);
1179
                s->status = READY_STAT;
1180
                ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
1181
            } else {
1182
                ide_abort_command(s);
1183
            }
1184
            ide_set_irq(s);
1185
            break;
1186
        case WIN_SRST:
1187
            if (!s->is_cdrom)
1188
                goto abort_cmd;
1189
            ide_set_signature(s);
1190
            s->status = 0x00; /* NOTE: READY is _not_ set */
1191
            s->error = 0x01;
1192
            break;
1193
        case WIN_PACKETCMD:
1194
            if (!s->is_cdrom)
1195
                goto abort_cmd;
1196
            /* DMA or overlapping commands not supported */
1197
            if ((s->feature & 0x03) != 0)
1198
                goto abort_cmd;
1199
            s->nsector = 1;
1200
            ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE, 
1201
                               ide_atapi_cmd);
1202
            break;
1203
        default:
1204
        abort_cmd:
1205
            ide_abort_command(s);
1206
            ide_set_irq(s);
1207
            break;
1208
        }
1209
    }
1210
}
1211

    
1212
static uint32_t ide_ioport_read(CPUState *env, uint32_t addr1)
1213
{
1214
    IDEState *s = get_ide_interface(addr1)->cur_drive;
1215
    uint32_t addr;
1216
    int ret;
1217

    
1218
    addr = addr1 & 7;
1219
    switch(addr) {
1220
    case 0:
1221
        ret = 0xff;
1222
        break;
1223
    case 1:
1224
        ret = s->error;
1225
        break;
1226
    case 2:
1227
        ret = s->nsector & 0xff;
1228
        break;
1229
    case 3:
1230
        ret = s->sector;
1231
        break;
1232
    case 4:
1233
        ret = s->lcyl;
1234
        break;
1235
    case 5:
1236
        ret = s->hcyl;
1237
        break;
1238
    case 6:
1239
        ret = s->select;
1240
        break;
1241
    default:
1242
    case 7:
1243
        ret = s->status;
1244
        pic_set_irq(s->irq, 0);
1245
        break;
1246
    }
1247
#ifdef DEBUG_IDE
1248
    printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
1249
#endif
1250
    return ret;
1251
}
1252

    
1253
static uint32_t ide_status_read(CPUState *env, uint32_t addr)
1254
{
1255
    IDEState *s = get_ide_interface(addr)->cur_drive;
1256
    int ret;
1257
    ret = s->status;
1258
#ifdef DEBUG_IDE
1259
    printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
1260
#endif
1261
    return ret;
1262
}
1263

    
1264
static void ide_cmd_write(CPUState *env, uint32_t addr, uint32_t val)
1265
{
1266
    IDEState *ide_if = get_ide_interface(addr);
1267
    IDEState *s;
1268
    int i;
1269

    
1270
#ifdef DEBUG_IDE
1271
    printf("ide: write control addr=0x%x val=%02x\n", addr, val);
1272
#endif
1273
    /* common for both drives */
1274
    if (!(ide_if[0].cmd & IDE_CMD_RESET) &&
1275
        (val & IDE_CMD_RESET)) {
1276
        /* reset low to high */
1277
        for(i = 0;i < 2; i++) {
1278
            s = &ide_if[i];
1279
            s->status = BUSY_STAT | SEEK_STAT;
1280
            s->error = 0x01;
1281
        }
1282
    } else if ((ide_if[0].cmd & IDE_CMD_RESET) &&
1283
               !(val & IDE_CMD_RESET)) {
1284
        /* high to low */
1285
        for(i = 0;i < 2; i++) {
1286
            s = &ide_if[i];
1287
            if (s->is_cdrom)
1288
                s->status = 0x00; /* NOTE: READY is _not_ set */
1289
            else
1290
                s->status = READY_STAT | SEEK_STAT;
1291
            ide_set_signature(s);
1292
        }
1293
    }
1294

    
1295
    ide_if[0].cmd = val;
1296
    ide_if[1].cmd = val;
1297
}
1298

    
1299
static void ide_data_writew(CPUState *env, uint32_t addr, uint32_t val)
1300
{
1301
    IDEState *s = get_ide_interface(addr)->cur_drive;
1302
    uint8_t *p;
1303

    
1304
    p = s->data_ptr;
1305
    *(uint16_t *)p = tswap16(val);
1306
    p += 2;
1307
    s->data_ptr = p;
1308
    if (p >= s->data_end)
1309
        s->end_transfer_func(s);
1310
}
1311

    
1312
static uint32_t ide_data_readw(CPUState *env, uint32_t addr)
1313
{
1314
    IDEState *s = get_ide_interface(addr)->cur_drive;
1315
    uint8_t *p;
1316
    int ret;
1317
    p = s->data_ptr;
1318
    ret = tswap16(*(uint16_t *)p);
1319
    p += 2;
1320
    s->data_ptr = p;
1321
    if (p >= s->data_end)
1322
        s->end_transfer_func(s);
1323
    return ret;
1324
}
1325

    
1326
static void ide_data_writel(CPUState *env, uint32_t addr, uint32_t val)
1327
{
1328
    IDEState *s = get_ide_interface(addr)->cur_drive;
1329
    uint8_t *p;
1330

    
1331
    p = s->data_ptr;
1332
    *(uint32_t *)p = tswap32(val);
1333
    p += 4;
1334
    s->data_ptr = p;
1335
    if (p >= s->data_end)
1336
        s->end_transfer_func(s);
1337
}
1338

    
1339
static uint32_t ide_data_readl(CPUState *env, uint32_t addr)
1340
{
1341
    IDEState *s = get_ide_interface(addr)->cur_drive;
1342
    uint8_t *p;
1343
    int ret;
1344
    
1345
    p = s->data_ptr;
1346
    ret = tswap32(*(uint32_t *)p);
1347
    p += 4;
1348
    s->data_ptr = p;
1349
    if (p >= s->data_end)
1350
        s->end_transfer_func(s);
1351
    return ret;
1352
}
1353

    
1354
static void ide_reset(IDEState *s)
1355
{
1356
    s->mult_sectors = MAX_MULT_SECTORS;
1357
    s->cur_drive = s;
1358
    s->select = 0xa0;
1359
    s->status = READY_STAT;
1360
    ide_set_signature(s);
1361
}
1362

    
1363
struct partition {
1364
        uint8_t boot_ind;                /* 0x80 - active */
1365
        uint8_t head;                /* starting head */
1366
        uint8_t sector;                /* starting sector */
1367
        uint8_t cyl;                /* starting cylinder */
1368
        uint8_t sys_ind;                /* What partition type */
1369
        uint8_t end_head;                /* end head */
1370
        uint8_t end_sector;        /* end sector */
1371
        uint8_t end_cyl;                /* end cylinder */
1372
        uint32_t start_sect;        /* starting sector counting from 0 */
1373
        uint32_t nr_sects;                /* nr of sectors in partition */
1374
} __attribute__((packed));
1375

    
1376
/* try to guess the IDE geometry from the MSDOS partition table */
1377
static void ide_guess_geometry(IDEState *s)
1378
{
1379
    uint8_t buf[512];
1380
    int ret, i;
1381
    struct partition *p;
1382
    uint32_t nr_sects;
1383

    
1384
    if (s->cylinders != 0)
1385
        return;
1386
    ret = bdrv_read(s->bs, 0, buf, 1);
1387
    if (ret < 0)
1388
        return;
1389
    /* test msdos magic */
1390
    if (buf[510] != 0x55 || buf[511] != 0xaa)
1391
        return;
1392
    for(i = 0; i < 4; i++) {
1393
        p = ((struct partition *)(buf + 0x1be)) + i;
1394
        nr_sects = tswap32(p->nr_sects);
1395
        if (nr_sects && p->end_head) {
1396
            /* We make the assumption that the partition terminates on
1397
               a cylinder boundary */
1398
            s->heads = p->end_head + 1;
1399
            s->sectors = p->end_sector & 63;
1400
            s->cylinders = s->nb_sectors / (s->heads * s->sectors);
1401
#if 0
1402
            printf("guessed partition: CHS=%d %d %d\n", 
1403
                   s->cylinders, s->heads, s->sectors);
1404
#endif
1405
        }
1406
    }
1407
}
1408

    
1409
void ide_init(void)
1410
{
1411
    IDEState *s;
1412
    int i, cylinders, iobase, iobase2;
1413
    int64_t nb_sectors;
1414
    static const int ide_iobase[2] = { 0x1f0, 0x170 };
1415
    static const int ide_iobase2[2] = { 0x3f6, 0x376 };
1416
    static const int ide_irq[2] = { 14, 15 };
1417

    
1418
    for(i = 0; i < MAX_DISKS; i++) {
1419
        s = &ide_state[i];
1420
        s->bs = bs_table[i];
1421
        if (s->bs) {
1422
            bdrv_get_geometry(s->bs, &nb_sectors);
1423
            s->nb_sectors = nb_sectors;
1424
            ide_guess_geometry(s);
1425
            if (s->cylinders == 0) {
1426
                /* if no geometry, use a LBA compatible one */
1427
                cylinders = nb_sectors / (16 * 63);
1428
                if (cylinders > 16383)
1429
                    cylinders = 16383;
1430
                else if (cylinders < 2)
1431
                    cylinders = 2;
1432
                s->cylinders = cylinders;
1433
                s->heads = 16;
1434
                s->sectors = 63;
1435
            }
1436
        }
1437
        s->irq = ide_irq[i >> 1];
1438
        ide_reset(s);
1439
    }
1440
    for(i = 0; i < (MAX_DISKS / 2); i++) {
1441
        iobase = ide_iobase[i];
1442
        iobase2 = ide_iobase2[i];
1443
        ide_table[iobase >> 3] = &ide_state[2 * i];
1444
        if (ide_iobase2[i]) 
1445
            ide_table[iobase2 >> 3] = &ide_state[2 * i];
1446
        register_ioport_write(iobase, 8, ide_ioport_write, 1);
1447
        register_ioport_read(iobase, 8, ide_ioport_read, 1);
1448
        register_ioport_read(iobase2, 1, ide_status_read, 1);
1449
        register_ioport_write(iobase2, 1, ide_cmd_write, 1);
1450
        
1451
        /* data ports */
1452
        register_ioport_write(iobase, 2, ide_data_writew, 2);
1453
        register_ioport_read(iobase, 2, ide_data_readw, 2);
1454
        register_ioport_write(iobase, 4, ide_data_writel, 4);
1455
        register_ioport_read(iobase, 4, ide_data_readl, 4);
1456
    }
1457
}
1458

    
1459
void ide_set_geometry(int n, int cyls, int heads, int secs)
1460
{
1461
    ide_state[n].cylinders = cyls;
1462
    ide_state[n].heads = heads;
1463
    ide_state[n].sectors = secs;
1464
}
1465

    
1466
void ide_set_cdrom(int n, int is_cdrom)
1467
{
1468
    ide_state[n].is_cdrom = is_cdrom;
1469
}