|
|
1.1 root 1: /*
2: Hatari - ide.c
3:
4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
6:
7: This is where we intercept read/writes to/from the IDE controller hardware.
8: */
1.1.1.2 root 9:
10: #include <SDL_endian.h>
11: #include <errno.h>
12:
13: #include <sys/types.h>
14: #include <sys/stat.h>
15: #include <unistd.h>
1.1 root 16:
17: #include "main.h"
18: #include "configuration.h"
1.1.1.2 root 19: #include "file.h"
1.1 root 20: #include "ide.h"
21: #include "m68000.h"
1.1.1.2 root 22: #include "mfp.h"
1.1 root 23: #include "stMemory.h"
24: #include "sysdeps.h"
25:
1.1.1.2 root 26: #if HAVE_MALLOC_H
27: # include <malloc.h>
28: #endif
29:
30:
31: struct IDEState;
32:
1.1 root 33:
34: #define IDE_DEBUG 0
35:
36: #if IDE_DEBUG
37: #define Dprintf(a) printf a
38: #else
39: #define Dprintf(a)
40: #endif
41:
1.1.1.2 root 42: static struct IDEState *opaque_ide_if;
43:
44: static void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val);
45: static uint32_t ide_ioport_read(void *opaque, uint32_t addr1);
46: static uint32_t ide_status_read(void *opaque, uint32_t addr);
47: static void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val);
48: static void ide_data_writew(void *opaque, uint32_t addr, uint32_t val);
49: static uint32_t ide_data_readw(void *opaque, uint32_t addr);
50: static void ide_data_writel(void *opaque, uint32_t addr, uint32_t val);
51: static uint32_t ide_data_readl(void *opaque, uint32_t addr);
52:
53:
54: /**
55: * Convert Falcon IDE registers to "normal" IDE register numbers.
56: * (taken from Aranym - cheers!)
57: */
58: static uint32_t fcha2io(uint32_t address)
59: {
60: switch (address)
61: {
62: case 0xf00000:
63: return 0x00;
64: case 0xf00005:
65: return 0x01;
66: case 0xf00009:
67: return 0x02;
68: case 0xf0000d:
69: return 0x03;
70: case 0xf00011:
71: return 0x04;
72: case 0xf00015:
73: return 0x05;
74: case 0xf00019:
75: return 0x06;
76: case 0xf0001d:
77: return 0x07;
78: case 0xf00039:
79: return 0x16;
80: default:
81: return 0xffffffff;
82: }
83: }
84:
1.1 root 85:
86: /**
87: * Handle byte read access from IDE IO memory.
88: */
89: uae_u32 Ide_Mem_bget(uaecptr addr)
90: {
1.1.1.2 root 91: int ideport;
92: uint8_t retval;
93:
1.1 root 94: Dprintf(("IdeMem_bget($%x)\n", addr));
95:
96: addr &= 0x00ffffff; /* Use a 24 bit address */
97:
98: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
99: {
100: /* invalid memory addressing --> bus error */
101: M68000_BusError(addr, 1);
102: return -1;
103: }
104:
1.1.1.2 root 105: ideport = fcha2io(addr);
106:
1.1.1.3 ! root 107: if (ideport >= 1 && ideport <= 7)
1.1.1.2 root 108: {
109: retval = ide_ioport_read(opaque_ide_if, ideport);
1.1.1.3 ! root 110: }
! 111: else if (ideport == 8 || ideport == 22)
! 112: {
1.1.1.2 root 113: retval = ide_status_read(opaque_ide_if, 0);
1.1.1.3 ! root 114: }
! 115: else
! 116: {
1.1.1.2 root 117: retval = 0xFF;
118: }
119:
120: return retval;
1.1 root 121: }
122:
123:
124: /**
125: * Handle word read access from IDE IO memory.
126: */
127: uae_u32 Ide_Mem_wget(uaecptr addr)
128: {
1.1.1.2 root 129: uint16_t retval;
1.1 root 130:
131: addr &= 0x00ffffff; /* Use a 24 bit address */
132:
133: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
134: {
135: /* invalid memory addressing --> bus error */
136: M68000_BusError(addr, 1);
1.1.1.2 root 137: if (ConfigureParams.HardDisk.bUseIdeHardDiskImage)
138: fprintf(stderr, "Illegal IDE IO memory access: IdeMem_wget($%x)\n", addr);
1.1 root 139: return -1;
140: }
141:
1.1.1.2 root 142: if (addr == 0xf00000)
143: {
144: retval = ide_data_readw(opaque_ide_if, 0);
145: }
146: else
147: {
148: retval = 0xFFFF;
149: }
150:
151: Dprintf(("IdeMem_wget($%x) = $%04x\n", addr, retval));
152:
153:
154: return retval;
1.1 root 155: }
156:
157:
158: /**
159: * Handle long-word read access from IDE IO memory.
160: */
161: uae_u32 Ide_Mem_lget(uaecptr addr)
162: {
1.1.1.2 root 163: uint32_t retval;
1.1 root 164:
165: addr &= 0x00ffffff; /* Use a 24 bit address */
166:
167: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
168: {
169: /* invalid memory addressing --> bus error */
170: M68000_BusError(addr, 1);
1.1.1.2 root 171: if (ConfigureParams.HardDisk.bUseIdeHardDiskImage)
172: fprintf(stderr, "Illegal IDE IO memory access: IdeMem_lget($%x)\n", addr);
1.1 root 173: return -1;
174: }
175:
1.1.1.2 root 176: if (addr == 0xf00000)
177: {
178: retval = ide_data_readl(opaque_ide_if, 0);
179: }
180: else
181: {
182: retval = 0xFFFFFFFF;
183: }
184:
185: /* word swap for long access to data register */
186: retval = ((retval >> 16) & 0x0000ffff) | ((retval & 0x0000ffff) << 16);
187:
188: Dprintf(("IdeMem_lget($%x) = $%08x\n", addr, retval));
189:
190: return retval;
1.1 root 191: }
192:
193:
194: /**
195: * Handle byte write access to IDE IO memory.
196: */
197: void Ide_Mem_bput(uaecptr addr, uae_u32 val)
198: {
1.1.1.2 root 199: int ideport;
1.1 root 200:
201: addr &= 0x00ffffff; /* Use a 24 bit address */
1.1.1.2 root 202: val &= 0x0ff;
203:
204: Dprintf(("IdeMem_bput($%x, $%x)\n", addr, val));
1.1 root 205:
206: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
207: {
208: /* invalid memory addressing --> bus error */
209: M68000_BusError(addr, 0);
210: //fprintf(stderr, "Illegal IDE IO memory access: IdeMem_bput($%x)\n", addr);
211: return;
212: }
213:
1.1.1.2 root 214: ideport = fcha2io(addr);
215:
1.1.1.3 ! root 216: if (ideport >= 1 && ideport <= 7)
1.1.1.2 root 217: {
218: ide_ioport_write(opaque_ide_if, ideport, val);
1.1.1.3 ! root 219: }
! 220: else if (ideport == 8 || ideport == 22)
! 221: {
1.1.1.2 root 222: ide_cmd_write(opaque_ide_if, 0, val);
223: }
1.1 root 224: }
225:
226:
227: /**
228: * Handle word write access to IDE IO memory.
229: */
230: void Ide_Mem_wput(uaecptr addr, uae_u32 val)
231: {
232: addr &= 0x00ffffff; /* Use a 24 bit address */
1.1.1.2 root 233: val &= 0x0ffff;
234:
235: Dprintf(("IdeMem_wput($%x, $%x)\n", addr, val));
1.1 root 236:
237: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
238: {
239: /* invalid memory addressing --> bus error */
240: M68000_BusError(addr, 0);
241: //fprintf(stderr, "Illegal IDE IO memory access: IdeMem_wput($%x)\n", addr);
242: return;
243: }
244:
1.1.1.2 root 245: if (addr == 0xf00000)
246: {
247: ide_data_writew(opaque_ide_if, 0, val);
248: }
1.1 root 249: }
250:
251:
252: /**
253: * Handle long-word write access to IDE IO memory.
254: */
255: void Ide_Mem_lput(uaecptr addr, uae_u32 val)
256: {
257: addr &= 0x00ffffff; /* Use a 24 bit address */
258:
1.1.1.2 root 259: Dprintf(("IdeMem_lput($%x, $%x)\n", addr, val));
260:
1.1 root 261: if (addr >= 0xf00040 || !ConfigureParams.HardDisk.bUseIdeHardDiskImage)
262: {
263: /* invalid memory addressing --> bus error */
264: M68000_BusError(addr, 0);
265: //fprintf(stderr, "Illegal IDE IO memory access: IdeMem_lput($%x)\n", addr);
266: return;
267: }
268:
1.1.1.2 root 269: /* word swap for long access to data register */
270: val = ((val >> 16) & 0x0000ffff) | ((val & 0x0000ffff) << 16);
271:
272: if (addr == 0xf00000)
273: {
274: ide_data_writel(opaque_ide_if, 0, val);
275: }
276: }
277:
278:
279: /*----------------------------------------------------------------------------*/
280:
281:
282: /*
283: * QEMU IDE disk and CD-ROM Emulator
284: *
285: * Copyright (c) 2003 Fabrice Bellard
286: * Copyright (c) 2006 Openedhand Ltd.
287: *
288: * Permission is hereby granted, free of charge, to any person obtaining a copy
289: * of this software and associated documentation files (the "Software"), to deal
290: * in the Software without restriction, including without limitation the rights
291: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
292: * copies of the Software, and to permit persons to whom the Software is
293: * furnished to do so, subject to the following conditions:
294: *
295: * The above copyright notice and this permission notice shall be included in
296: * all copies or substantial portions of the Software.
297: *
298: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
299: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
300: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
301: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
302: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
303: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
304: * THE SOFTWARE.
305: */
306:
307: #define FW_VERSION "1.0"
308:
309:
310: #define BDRV_TYPE_HD 0
311: #define BDRV_TYPE_CDROM 1
312: #define BDRV_TYPE_FLOPPY 2
313: #define BIOS_ATA_TRANSLATION_AUTO 0
314: #define BIOS_ATA_TRANSLATION_NONE 1
315: #define BIOS_ATA_TRANSLATION_LBA 2
316: #define BIOS_ATA_TRANSLATION_LARGE 3
317: #define BIOS_ATA_TRANSLATION_RECHS 4
318:
319: #ifndef ENOMEDIUM // It's not defined on Mac OS X for example
320: #define ENOMEDIUM ENODEV
321: #endif
322:
323:
324: typedef struct BlockDriverState BlockDriverState;
325:
326: struct BlockDriverState {
327: int64_t total_sectors; /* if we are reading a disk image, give its
328: size in sectors */
329: int read_only; /* if true, the media is read only */
330: int removable; /* if true, the media can be removed */
331: int locked; /* if true, the media cannot temporarily be ejected */
332: int sg; /* if true, the device is a /dev/sg* */
333: /* event callback when inserting/removing */
334: void (*change_cb)(void *opaque);
335: void *change_opaque;
336:
337: FILE *fhndl;
338: void *opaque;
339:
340: char filename[1024];
341: char backing_file[1024]; /* if non zero, the image is a diff of
342: this file image */
343: int media_changed;
344:
345: /* I/O stats (display with "info blockstats"). */
346: uint64_t rd_bytes;
347: uint64_t wr_bytes;
348: uint64_t rd_ops;
349: uint64_t wr_ops;
350:
351: /* NOTE: the following infos are only hints for real hardware
352: drivers. They are not used by the block driver */
353: int cyls, heads, secs, translation;
354: int type;
355: };
356:
357:
358: static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
359: {
360: uint8_t *p1 = (uint8_t *)p;
361:
362: p1[0] = v >> 8;
363: p1[1] = v;
364: }
365:
366:
367: #if defined(WIN32)
368:
369: #include <windows.h>
370:
371: static void *qemu_memalign(size_t alignment, size_t size)
372: {
373: return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
374: }
375:
376: static void qemu_free(void *ptr)
377: {
378: VirtualFree(ptr, 0, MEM_RELEASE);
379: }
380:
381: #else
382:
383: static void *qemu_memalign(size_t alignment, size_t size)
384: {
385: #if HAVE_POSIX_MEMALIGN
386: int ret;
387: void *ptr;
388: ret = posix_memalign(&ptr, alignment, size);
389: if (ret != 0)
390: return NULL;
391: return ptr;
392: #elif HAVE_MEMALIGN
393: return memalign(alignment, size);
394: #else
395: return valloc(size);
396: #endif
397: }
398:
399: #define qemu_free free
400:
401: #endif
402:
403:
404: #define le32_to_cpu SDL_SwapLE32
405: #define le16_to_cpu SDL_SwapLE16
406: #define cpu_to_le32 SDL_SwapLE32
407: #define cpu_to_le16 SDL_SwapLE16
408:
409:
410: #define MIN(a, b) (((a) < (b)) ? (a) : (b))
411:
412: #define SECTOR_BITS 9
413: #define SECTOR_SIZE (1 << SECTOR_BITS)
414:
415:
416: /**
417: * return 0 as number of sectors if no device present or error
418: */
419: static void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
420: {
421: int64_t length;
422: length = File_Length(bs->filename);
423:
424: if (length < 0)
425: length = 0;
426: else
427: length = length >> SECTOR_BITS;
428: *nb_sectors_ptr = length;
429: }
430:
431: static void bdrv_get_geometry_hint(BlockDriverState *bs,
432: int *pcyls, int *pheads, int *psecs)
433: {
434: *pcyls = bs->cyls;
435: *pheads = bs->heads;
436: *psecs = bs->secs;
437: }
438:
439: static void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
440: {
441: bs->translation = translation;
442: }
443:
444: static void bdrv_set_geometry_hint(BlockDriverState *bs,
445: int cyls, int heads, int secs)
446: {
447: bs->cyls = cyls;
448: bs->heads = heads;
449: bs->secs = secs;
450: }
451:
452: static int bdrv_get_type_hint(BlockDriverState *bs)
453: {
454: return bs->type;
455: }
456:
457: static int bdrv_get_translation_hint(BlockDriverState *bs)
458: {
459: return bs->translation;
460: }
461:
462: /* XXX: no longer used */
463: static void bdrv_set_change_cb(BlockDriverState *bs,
464: void (*change_cb)(void *opaque), void *opaque)
465: {
466: bs->change_cb = change_cb;
467: bs->change_opaque = opaque;
468: }
469:
470:
471: /**
472: * Return TRUE if the media is present
473: */
474: static int bdrv_is_inserted(BlockDriverState *bs)
475: {
476: return (bs->fhndl != NULL);
477: }
478:
479:
480: static int bdrv_is_locked(BlockDriverState *bs)
481: {
482: return bs->locked;
483: }
484:
485: /**
486: * Lock or unlock the media (if it is locked, the user won't be able
487: * to eject it manually).
488: */
489: static void bdrv_set_locked(BlockDriverState *bs, int locked)
490: {
491: bs->locked = locked;
492: }
493:
494:
495: /* return < 0 if error. See bdrv_write() for the return codes */
496: static int bdrv_read(BlockDriverState *bs, int64_t sector_num,
497: uint8_t *buf, int nb_sectors)
498: {
1.1.1.3 ! root 499: int ret, len;
! 500:
1.1.1.2 root 501: if (!bs->fhndl)
502: return -ENOMEDIUM;
503:
504: len = nb_sectors * 512;
505:
506: fseek(bs->fhndl, sector_num*512, SEEK_SET);
507: ret = fread(buf, 1, len, bs->fhndl);
508: if (ret != len)
509: {
510: fprintf(stderr,"Error during read!\n");
511: return -EINVAL;
512: }
513: else
514: {
515: bs->rd_bytes += (unsigned) len;
516: bs->rd_ops ++;
517: return 0;
518: }
519: }
520:
521:
522: /* Return < 0 if error. Important errors are:
523: -EIO generic I/O error (may happen for all errors)
524: -ENOMEDIUM No media inserted.
525: -EINVAL Invalid sector number or nb_sectors
526: -EACCES Trying to write a read-only device
527: */
528: static int bdrv_write(BlockDriverState *bs, int64_t sector_num,
529: const uint8_t *buf, int nb_sectors)
530: {
1.1.1.3 ! root 531: int ret, len;
! 532:
1.1.1.2 root 533: if (!bs->fhndl)
534: return -ENOMEDIUM;
535: if (bs->read_only)
536: return -EACCES;
537:
538: len = nb_sectors * 512;
539:
540: fseek(bs->fhndl, sector_num*512, SEEK_SET);
541: ret = fwrite(buf, 1, len, bs->fhndl);
542: if (ret != len)
543: {
544: fprintf(stderr,"Error during bdrv_write\n");
545: return -EIO;
546: }
547: else
548: {
549: bs->wr_bytes += (unsigned) len;
550: bs->wr_ops ++;
551: return 0;
552: }
553: }
554:
555:
556: static int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
557: {
558: fprintf(stderr,"Opening %s\n", filename);
559:
560: strncpy(bs->filename, filename, sizeof(bs->filename));
561:
562: bs->read_only = 0;
563:
564: bs->fhndl = fopen(filename, "rb+");
565:
566: if (!bs->fhndl) {
567: /* Maybe the file is read-only? */
568: bs->fhndl = fopen(filename, "rb");
569: if (!bs->fhndl)
570: perror("bdrv_open");
571: bs->read_only = 1;
572: }
573:
574: /* call the change callback */
575: bs->media_changed = 1;
576: if (bs->change_cb)
577: bs->change_cb(bs->change_opaque);
578:
579: return 0;
580: }
581:
582: static void bdrv_flush(BlockDriverState *bs)
583: {
584: fflush(bs->fhndl);
585: }
586:
587: static void bdrv_close(BlockDriverState *bs)
588: {
589: fclose(bs->fhndl);
590: bs->fhndl = NULL;
591: }
592:
593: /**
594: * If eject_flag is TRUE, eject the media. Otherwise, close the tray
595: */
596: static void bdrv_eject(BlockDriverState *bs, int eject_flag)
597: {
598: if (eject_flag)
599: bdrv_close(bs);
600: }
601:
602:
603: /* debug IDE devices */
604: // #define DEBUG_IDE
605: // #define DEBUG_IDE_ATAPI
606:
607: // #define USE_DMA_CDROM
608:
609: /* Bits of HD_STATUS */
610: #define ERR_STAT 0x01
611: #define INDEX_STAT 0x02
612: #define ECC_STAT 0x04 /* Corrected error */
613: #define DRQ_STAT 0x08
614: #define SEEK_STAT 0x10
615: #define SRV_STAT 0x10
616: #define WRERR_STAT 0x20
617: #define READY_STAT 0x40
618: #define BUSY_STAT 0x80
619:
620: /* Bits for HD_ERROR */
621: #define MARK_ERR 0x01 /* Bad address mark */
622: #define TRK0_ERR 0x02 /* couldn't find track 0 */
623: #define ABRT_ERR 0x04 /* Command aborted */
624: #define MCR_ERR 0x08 /* media change request */
625: #define ID_ERR 0x10 /* ID field not found */
626: #define MC_ERR 0x20 /* media changed */
627: #define ECC_ERR 0x40 /* Uncorrectable ECC error */
628: #define BBD_ERR 0x80 /* pre-EIDE meaning: block marked bad */
629: #define ICRC_ERR 0x80 /* new meaning: CRC error during transfer */
630:
631: /* Bits of HD_NSECTOR */
632: #define CD 0x01
633: #define IO 0x02
634: #define REL 0x04
635: #define TAG_MASK 0xf8
636:
637: #define IDE_CMD_RESET 0x04
638: #define IDE_CMD_DISABLE_IRQ 0x02
639:
640: /* ATA/ATAPI Commands pre T13 Spec */
641: #define WIN_NOP 0x00
642: /*
643: * 0x01->0x02 Reserved
644: */
645: #define CFA_REQ_EXT_ERROR_CODE 0x03 /* CFA Request Extended Error Code */
646: /*
647: * 0x04->0x07 Reserved
648: */
649: #define WIN_SRST 0x08 /* ATAPI soft reset command */
650: #define WIN_DEVICE_RESET 0x08
651: /*
652: * 0x09->0x0F Reserved
653: */
654: #define WIN_RECAL 0x10
655: #define WIN_RESTORE WIN_RECAL
656: /*
657: * 0x10->0x1F Reserved
658: */
659: #define WIN_READ 0x20 /* 28-Bit */
660: #define WIN_READ_ONCE 0x21 /* 28-Bit without retries */
661: #define WIN_READ_LONG 0x22 /* 28-Bit */
662: #define WIN_READ_LONG_ONCE 0x23 /* 28-Bit without retries */
663: #define WIN_READ_EXT 0x24 /* 48-Bit */
664: #define WIN_READDMA_EXT 0x25 /* 48-Bit */
665: #define WIN_READDMA_QUEUED_EXT 0x26 /* 48-Bit */
666: #define WIN_READ_NATIVE_MAX_EXT 0x27 /* 48-Bit */
667: /*
668: * 0x28
669: */
670: #define WIN_MULTREAD_EXT 0x29 /* 48-Bit */
671: /*
672: * 0x2A->0x2F Reserved
673: */
674: #define WIN_WRITE 0x30 /* 28-Bit */
675: #define WIN_WRITE_ONCE 0x31 /* 28-Bit without retries */
676: #define WIN_WRITE_LONG 0x32 /* 28-Bit */
677: #define WIN_WRITE_LONG_ONCE 0x33 /* 28-Bit without retries */
678: #define WIN_WRITE_EXT 0x34 /* 48-Bit */
679: #define WIN_WRITEDMA_EXT 0x35 /* 48-Bit */
680: #define WIN_WRITEDMA_QUEUED_EXT 0x36 /* 48-Bit */
681: #define WIN_SET_MAX_EXT 0x37 /* 48-Bit */
682: #define CFA_WRITE_SECT_WO_ERASE 0x38 /* CFA Write Sectors without erase */
683: #define WIN_MULTWRITE_EXT 0x39 /* 48-Bit */
684: /*
685: * 0x3A->0x3B Reserved
686: */
687: #define WIN_WRITE_VERIFY 0x3C /* 28-Bit */
688: /*
689: * 0x3D->0x3F Reserved
690: */
691: #define WIN_VERIFY 0x40 /* 28-Bit - Read Verify Sectors */
692: #define WIN_VERIFY_ONCE 0x41 /* 28-Bit - without retries */
693: #define WIN_VERIFY_EXT 0x42 /* 48-Bit */
694: /*
695: * 0x43->0x4F Reserved
696: */
697: #define WIN_FORMAT 0x50
698: /*
699: * 0x51->0x5F Reserved
700: */
701: #define WIN_INIT 0x60
702: /*
703: * 0x61->0x5F Reserved
704: */
705: #define WIN_SEEK 0x70 /* 0x70-0x7F Reserved */
706: #define CFA_TRANSLATE_SECTOR 0x87 /* CFA Translate Sector */
707: #define WIN_DIAGNOSE 0x90
708: #define WIN_SPECIFY 0x91 /* set drive geometry translation */
709: #define WIN_DOWNLOAD_MICROCODE 0x92
710: #define WIN_STANDBYNOW2 0x94
711: #define CFA_IDLEIMMEDIATE 0x95 /* force drive to become "ready" */
712: #define WIN_STANDBY2 0x96
713: #define WIN_SETIDLE2 0x97
714: #define WIN_CHECKPOWERMODE2 0x98
715: #define WIN_SLEEPNOW2 0x99
716: /*
717: * 0x9A VENDOR
718: */
719: #define WIN_PACKETCMD 0xA0 /* Send a packet command. */
720: #define WIN_PIDENTIFY 0xA1 /* identify ATAPI device */
721: #define WIN_QUEUED_SERVICE 0xA2
722: #define WIN_SMART 0xB0 /* self-monitoring and reporting */
723: #define CFA_ACCESS_METADATA_STORAGE 0xB8
724: #define CFA_ERASE_SECTORS 0xC0 /* microdrives implement as NOP */
725: #define WIN_MULTREAD 0xC4 /* read sectors using multiple mode*/
726: #define WIN_MULTWRITE 0xC5 /* write sectors using multiple mode */
727: #define WIN_SETMULT 0xC6 /* enable/disable multiple mode */
728: #define WIN_READDMA_QUEUED 0xC7 /* read sectors using Queued DMA transfers */
729: #define WIN_READDMA 0xC8 /* read sectors using DMA transfers */
730: #define WIN_READDMA_ONCE 0xC9 /* 28-Bit - without retries */
731: #define WIN_WRITEDMA 0xCA /* write sectors using DMA transfers */
732: #define WIN_WRITEDMA_ONCE 0xCB /* 28-Bit - without retries */
733: #define WIN_WRITEDMA_QUEUED 0xCC /* write sectors using Queued DMA transfers */
734: #define CFA_WRITE_MULTI_WO_ERASE 0xCD /* CFA Write multiple without erase */
735: #define WIN_GETMEDIASTATUS 0xDA
736: #define WIN_ACKMEDIACHANGE 0xDB /* ATA-1, ATA-2 vendor */
737: #define WIN_POSTBOOT 0xDC
738: #define WIN_PREBOOT 0xDD
739: #define WIN_DOORLOCK 0xDE /* lock door on removable drives */
740: #define WIN_DOORUNLOCK 0xDF /* unlock door on removable drives */
741: #define WIN_STANDBYNOW1 0xE0
742: #define WIN_IDLEIMMEDIATE 0xE1 /* force drive to become "ready" */
743: #define WIN_STANDBY 0xE2 /* Set device in Standby Mode */
744: #define WIN_SETIDLE1 0xE3
745: #define WIN_READ_BUFFER 0xE4 /* force read only 1 sector */
746: #define WIN_CHECKPOWERMODE1 0xE5
747: #define WIN_SLEEPNOW1 0xE6
748: #define WIN_FLUSH_CACHE 0xE7
749: #define WIN_WRITE_BUFFER 0xE8 /* force write only 1 sector */
750: #define WIN_WRITE_SAME 0xE9 /* read ata-2 to use */
751: /* SET_FEATURES 0x22 or 0xDD */
752: #define WIN_FLUSH_CACHE_EXT 0xEA /* 48-Bit */
753: #define WIN_IDENTIFY 0xEC /* ask drive to identify itself */
754: #define WIN_MEDIAEJECT 0xED
755: #define WIN_IDENTIFY_DMA 0xEE /* same as WIN_IDENTIFY, but DMA */
756: #define WIN_SETFEATURES 0xEF /* set special drive features */
757: #define EXABYTE_ENABLE_NEST 0xF0
758: #define IBM_SENSE_CONDITION 0xF0 /* measure disk temperature */
759: #define WIN_SECURITY_SET_PASS 0xF1
760: #define WIN_SECURITY_UNLOCK 0xF2
761: #define WIN_SECURITY_ERASE_PREPARE 0xF3
762: #define WIN_SECURITY_ERASE_UNIT 0xF4
763: #define WIN_SECURITY_FREEZE_LOCK 0xF5
764: #define CFA_WEAR_LEVEL 0xF5 /* microdrives implement as NOP */
765: #define WIN_SECURITY_DISABLE 0xF6
766: #define WIN_READ_NATIVE_MAX 0xF8 /* return the native maximum address */
767: #define WIN_SET_MAX 0xF9
768: #define DISABLE_SEAGATE 0xFB
769:
770: /* set to 1 set disable mult support */
771: #define MAX_MULT_SECTORS 16
772:
773: /* ATAPI defines */
774:
775: #define ATAPI_PACKET_SIZE 12
776:
777: /* The generic packet command opcodes for CD/DVD Logical Units,
778: * From Table 57 of the SFF8090 Ver. 3 (Mt. Fuji) draft standard. */
779: #define GPCMD_BLANK 0xa1
780: #define GPCMD_CLOSE_TRACK 0x5b
781: #define GPCMD_FLUSH_CACHE 0x35
782: #define GPCMD_FORMAT_UNIT 0x04
783: #define GPCMD_GET_CONFIGURATION 0x46
784: #define GPCMD_GET_EVENT_STATUS_NOTIFICATION 0x4a
785: #define GPCMD_GET_PERFORMANCE 0xac
786: #define GPCMD_INQUIRY 0x12
787: #define GPCMD_LOAD_UNLOAD 0xa6
788: #define GPCMD_MECHANISM_STATUS 0xbd
789: #define GPCMD_MODE_SELECT_10 0x55
790: #define GPCMD_MODE_SENSE_10 0x5a
791: #define GPCMD_PAUSE_RESUME 0x4b
792: #define GPCMD_PLAY_AUDIO_10 0x45
793: #define GPCMD_PLAY_AUDIO_MSF 0x47
794: #define GPCMD_PLAY_AUDIO_TI 0x48
795: #define GPCMD_PLAY_CD 0xbc
796: #define GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
797: #define GPCMD_READ_10 0x28
798: #define GPCMD_READ_12 0xa8
799: #define GPCMD_READ_CDVD_CAPACITY 0x25
800: #define GPCMD_READ_CD 0xbe
801: #define GPCMD_READ_CD_MSF 0xb9
802: #define GPCMD_READ_DISC_INFO 0x51
803: #define GPCMD_READ_DVD_STRUCTURE 0xad
804: #define GPCMD_READ_FORMAT_CAPACITIES 0x23
805: #define GPCMD_READ_HEADER 0x44
806: #define GPCMD_READ_TRACK_RZONE_INFO 0x52
807: #define GPCMD_READ_SUBCHANNEL 0x42
808: #define GPCMD_READ_TOC_PMA_ATIP 0x43
809: #define GPCMD_REPAIR_RZONE_TRACK 0x58
810: #define GPCMD_REPORT_KEY 0xa4
811: #define GPCMD_REQUEST_SENSE 0x03
812: #define GPCMD_RESERVE_RZONE_TRACK 0x53
813: #define GPCMD_SCAN 0xba
814: #define GPCMD_SEEK 0x2b
815: #define GPCMD_SEND_DVD_STRUCTURE 0xad
816: #define GPCMD_SEND_EVENT 0xa2
817: #define GPCMD_SEND_KEY 0xa3
818: #define GPCMD_SEND_OPC 0x54
819: #define GPCMD_SET_READ_AHEAD 0xa7
820: #define GPCMD_SET_STREAMING 0xb6
821: #define GPCMD_START_STOP_UNIT 0x1b
822: #define GPCMD_STOP_PLAY_SCAN 0x4e
823: #define GPCMD_TEST_UNIT_READY 0x00
824: #define GPCMD_VERIFY_10 0x2f
825: #define GPCMD_WRITE_10 0x2a
826: #define GPCMD_WRITE_AND_VERIFY_10 0x2e
827: /* This is listed as optional in ATAPI 2.6, but is (curiously)
828: * missing from Mt. Fuji, Table 57. It _is_ mentioned in Mt. Fuji
829: * Table 377 as an MMC command for SCSi devices though... Most ATAPI
830: * drives support it. */
831: #define GPCMD_SET_SPEED 0xbb
832: /* This seems to be a SCSI specific CD-ROM opcode
833: * to play data at track/index */
834: #define GPCMD_PLAYAUDIO_TI 0x48
835: /*
836: * From MS Media Status Notification Support Specification. For
837: * older drives only.
838: */
839: #define GPCMD_GET_MEDIA_STATUS 0xda
840: #define GPCMD_MODE_SENSE_6 0x1a
841:
842: /* Mode page codes for mode sense/set */
843: #define GPMODE_R_W_ERROR_PAGE 0x01
844: #define GPMODE_WRITE_PARMS_PAGE 0x05
845: #define GPMODE_AUDIO_CTL_PAGE 0x0e
846: #define GPMODE_POWER_PAGE 0x1a
847: #define GPMODE_FAULT_FAIL_PAGE 0x1c
848: #define GPMODE_TO_PROTECT_PAGE 0x1d
849: #define GPMODE_CAPABILITIES_PAGE 0x2a
850: #define GPMODE_ALL_PAGES 0x3f
851: /* Not in Mt. Fuji, but in ATAPI 2.6 -- depricated now in favor
852: * of MODE_SENSE_POWER_PAGE */
853: #define GPMODE_CDROM_PAGE 0x0d
854:
855: #define ATAPI_INT_REASON_CD 0x01 /* 0 = data transfer */
856: #define ATAPI_INT_REASON_IO 0x02 /* 1 = transfer to the host */
857: #define ATAPI_INT_REASON_REL 0x04
858: #define ATAPI_INT_REASON_TAG 0xf8
859:
860: /* same constants as bochs */
861: #define ASC_ILLEGAL_OPCODE 0x20
862: #define ASC_LOGICAL_BLOCK_OOR 0x21
863: #define ASC_INV_FIELD_IN_CMD_PACKET 0x24
864: #define ASC_MEDIUM_NOT_PRESENT 0x3a
865: #define ASC_SAVING_PARAMETERS_NOT_SUPPORTED 0x39
866:
867: #define SENSE_NONE 0
868: #define SENSE_NOT_READY 2
869: #define SENSE_ILLEGAL_REQUEST 5
870: #define SENSE_UNIT_ATTENTION 6
871:
872: typedef void EndTransferFunc(struct IDEState *);
873:
874: /* NOTE: IDEState represents in fact one drive */
875: typedef struct IDEState
876: {
877: /* ide config */
878: int is_cdrom;
879: int cylinders, heads, sectors;
880: int64_t nb_sectors;
881: int mult_sectors;
882: int identify_set;
883: uint16_t identify_data[256];
884: int drive_serial;
885: /* ide regs */
886: uint8_t feature;
887: uint8_t error;
888: uint32_t nsector;
889: uint8_t sector;
890: uint8_t lcyl;
891: uint8_t hcyl;
892: /* other part of tf for lba48 support */
893: uint8_t hob_feature;
894: uint8_t hob_nsector;
895: uint8_t hob_sector;
896: uint8_t hob_lcyl;
897: uint8_t hob_hcyl;
898:
899: uint8_t select;
900: uint8_t status;
901:
902: /* 0x3f6 command, only meaningful for drive 0 */
903: uint8_t cmd;
904: /* set for lba48 access */
905: uint8_t lba48;
906: /* depends on bit 4 in select, only meaningful for drive 0 */
907: struct IDEState *cur_drive;
908: BlockDriverState *bs;
909: /* ATAPI specific */
910: uint8_t sense_key;
911: uint8_t asc;
912: int packet_transfer_size;
913: int elementary_transfer_size;
914: int io_buffer_index;
915: int lba;
916: int cd_sector_size;
917: /* ATA DMA state */
918: int io_buffer_size;
919: /* PIO transfer handling */
920: int req_nb_sectors; /* number of sectors per interrupt */
921: EndTransferFunc *end_transfer_func;
922: uint8_t *data_ptr;
923: uint8_t *data_end;
924: uint8_t *io_buffer;
925: int media_changed;
926: } IDEState;
927:
928:
929: static void padstr(char *str, const char *src, int len)
930: {
931: int i, v;
932: for (i = 0; i < len; i++)
933: {
934: if (*src)
935: v = *src++;
936: else
937: v = ' ';
938: str[i^1] = v;
939: }
940: }
941:
942: static void padstr8(uint8_t *buf, int buf_size, const char *src)
943: {
944: int i;
945: for (i = 0; i < buf_size; i++)
946: {
947: if (*src)
948: buf[i] = *src++;
949: else
950: buf[i] = ' ';
951: }
952: }
953:
954: static void put_le16(uint16_t *p, unsigned int v)
955: {
956: *p = SDL_SwapLE16(v);
957: }
958:
959: static void ide_identify(IDEState *s)
960: {
961: uint16_t *p;
962: unsigned int oldsize;
963: char buf[20];
964:
965: if (s->identify_set)
966: {
967: memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
968: return;
969: }
970:
971: memset(s->io_buffer, 0, 512);
972: p = (uint16_t *)s->io_buffer;
973: put_le16(p + 0, 0x0040);
974: put_le16(p + 1, s->cylinders);
975: put_le16(p + 3, s->heads);
976: put_le16(p + 4, 512 * s->sectors); /* XXX: retired, remove ? */
977: put_le16(p + 5, 512); /* XXX: retired, remove ? */
978: put_le16(p + 6, s->sectors);
979: snprintf(buf, sizeof(buf), "QM%05d", s->drive_serial);
980: padstr((char *)(p + 10), buf, 20); /* serial number */
981: put_le16(p + 20, 3); /* XXX: retired, remove ? */
982: put_le16(p + 21, 512); /* cache size in sectors */
983: put_le16(p + 22, 4); /* ecc bytes */
984: padstr((char *)(p + 23), FW_VERSION, 8); /* firmware version */
985: padstr((char *)(p + 27), "Hatari IDE disk", 40); /* model */
986: #if MAX_MULT_SECTORS > 1
987: put_le16(p + 47, 0x8000 | MAX_MULT_SECTORS);
988: #endif
989: put_le16(p + 48, 1); /* dword I/O */
990: put_le16(p + 49, (1 << 11) | (1 << 9) | (1 << 8)); /* DMA and LBA supported */
991: put_le16(p + 51, 0x200); /* PIO transfer cycle */
992: put_le16(p + 52, 0x200); /* DMA transfer cycle */
993: put_le16(p + 53, 1 | (1 << 1) | (1 << 2)); /* words 54-58,64-70,88 are valid */
994: put_le16(p + 54, s->cylinders);
995: put_le16(p + 55, s->heads);
996: put_le16(p + 56, s->sectors);
997: oldsize = s->cylinders * s->heads * s->sectors;
998: put_le16(p + 57, oldsize);
999: put_le16(p + 58, oldsize >> 16);
1000: if (s->mult_sectors)
1001: put_le16(p + 59, 0x100 | s->mult_sectors);
1002: put_le16(p + 60, s->nb_sectors);
1003: put_le16(p + 61, s->nb_sectors >> 16);
1004: put_le16(p + 63, 0x07); /* mdma0-2 supported */
1005: put_le16(p + 65, 120);
1006: put_le16(p + 66, 120);
1007: put_le16(p + 67, 120);
1008: put_le16(p + 68, 120);
1009: put_le16(p + 80, 0xf0); /* ata3 -> ata6 supported */
1010: put_le16(p + 81, 0x16); /* conforms to ata5 */
1011: put_le16(p + 82, (1 << 14));
1012: /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
1013: put_le16(p + 83, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
1014: put_le16(p + 84, (1 << 14));
1015: put_le16(p + 85, (1 << 14));
1016: /* 13=flush_cache_ext,12=flush_cache,10=lba48 */
1017: put_le16(p + 86, (1 << 14) | (1 << 13) | (1 <<12) | (1 << 10));
1018: put_le16(p + 87, (1 << 14));
1019: put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
1020: put_le16(p + 93, 1 | (1 << 14) | 0x2000);
1021: put_le16(p + 100, s->nb_sectors);
1022: put_le16(p + 101, s->nb_sectors >> 16);
1023: put_le16(p + 102, s->nb_sectors >> 32);
1024: put_le16(p + 103, s->nb_sectors >> 48);
1025:
1026: memcpy(s->identify_data, p, sizeof(s->identify_data));
1027: s->identify_set = 1;
1028: }
1029:
1030: static void ide_atapi_identify(IDEState *s)
1031: {
1032: uint16_t *p;
1033: char buf[20];
1034:
1035: if (s->identify_set)
1036: {
1037: memcpy(s->io_buffer, s->identify_data, sizeof(s->identify_data));
1038: return;
1039: }
1040:
1041: memset(s->io_buffer, 0, 512);
1042: p = (uint16_t *)s->io_buffer;
1043: /* Removable CDROM, 50us response, 12 byte packets */
1044: put_le16(p + 0, (2 << 14) | (5 << 8) | (1 << 7) | (2 << 5) | (0 << 0));
1045: snprintf(buf, sizeof(buf), "QM%05d", s->drive_serial);
1046: padstr((char *)(p + 10), buf, 20); /* serial number */
1047: put_le16(p + 20, 3); /* buffer type */
1048: put_le16(p + 21, 512); /* cache size in sectors */
1049: put_le16(p + 22, 4); /* ecc bytes */
1050: padstr((char *)(p + 23), FW_VERSION, 8); /* firmware version */
1051: padstr((char *)(p + 27), "Hatari CD-ROM", 40); /* model */
1052: put_le16(p + 48, 1); /* dword I/O (XXX: should not be set on CDROM) */
1053: #ifdef USE_DMA_CDROM
1054: put_le16(p + 49, 1 << 9 | 1 << 8); /* DMA and LBA supported */
1055: put_le16(p + 53, 7); /* words 64-70, 54-58, 88 valid */
1056: put_le16(p + 63, 7); /* mdma0-2 supported */
1057: put_le16(p + 64, 0x3f); /* PIO modes supported */
1058: #else
1059: put_le16(p + 49, 1 << 9); /* LBA supported, no DMA */
1060: put_le16(p + 53, 3); /* words 64-70, 54-58 valid */
1061: put_le16(p + 63, 0x103); /* DMA modes XXX: may be incorrect */
1062: put_le16(p + 64, 1); /* PIO modes */
1063: #endif
1064: put_le16(p + 65, 0xb4); /* minimum DMA multiword tx cycle time */
1065: put_le16(p + 66, 0xb4); /* recommended DMA multiword tx cycle time */
1066: put_le16(p + 67, 0x12c); /* minimum PIO cycle time without flow control */
1067: put_le16(p + 68, 0xb4); /* minimum PIO cycle time with IORDY flow control */
1068:
1069: put_le16(p + 71, 30); /* in ns */
1070: put_le16(p + 72, 30); /* in ns */
1071:
1072: put_le16(p + 80, 0x1e); /* support up to ATA/ATAPI-4 */
1073: #ifdef USE_DMA_CDROM
1074: put_le16(p + 88, 0x3f | (1 << 13)); /* udma5 set and supported */
1075: #endif
1076: memcpy(s->identify_data, p, sizeof(s->identify_data));
1077: s->identify_set = 1;
1078: }
1079:
1080:
1081: static void ide_set_signature(IDEState *s)
1082: {
1083: s->select &= 0xf0; /* clear head */
1084: /* put signature */
1085: s->nsector = 1;
1086: s->sector = 1;
1087: if (s->is_cdrom)
1088: {
1089: s->lcyl = 0x14;
1090: s->hcyl = 0xeb;
1091: }
1092: else if (s->bs)
1093: {
1094: s->lcyl = 0;
1095: s->hcyl = 0;
1096: }
1097: else
1098: {
1099: s->lcyl = 0xff;
1100: s->hcyl = 0xff;
1101: }
1102: }
1103:
1104: static inline void ide_abort_command(IDEState *s)
1105: {
1106: s->status = READY_STAT | ERR_STAT;
1107: s->error = ABRT_ERR;
1108: }
1109:
1110: static inline void ide_set_irq(IDEState *s)
1111: {
1112: if (!(s->cmd & IDE_CMD_DISABLE_IRQ))
1113: {
1114: /* raise IRQ */
1115: MFP_InputOnChannel(MFP_FDCHDC_BIT, MFP_IERB, &MFP_IPRB);
1116: MFP_GPIP &= ~0x20;
1117: }
1118: }
1119:
1120: /* prepare data transfer and tell what to do after */
1121: static void ide_transfer_start(IDEState *s, uint8_t *buf, int size,
1122: EndTransferFunc *end_transfer_func)
1123: {
1124: s->end_transfer_func = end_transfer_func;
1125: s->data_ptr = buf;
1126: s->data_end = buf + size;
1127: if (!(s->status & ERR_STAT))
1128: s->status |= DRQ_STAT;
1129: }
1130:
1131: static void ide_transfer_stop(IDEState *s)
1132: {
1133: s->end_transfer_func = ide_transfer_stop;
1134: s->data_ptr = s->io_buffer;
1135: s->data_end = s->io_buffer;
1136: s->status &= ~DRQ_STAT;
1137: }
1138:
1139: static int64_t ide_get_sector(IDEState *s)
1140: {
1141: int64_t sector_num;
1142: if (s->select & 0x40)
1143: {
1144: /* lba */
1145: if (!s->lba48)
1146: {
1147: sector_num = ((s->select & 0x0f) << 24) | (s->hcyl << 16) |
1148: (s->lcyl << 8) | s->sector;
1149: }
1150: else
1151: {
1152: sector_num = ((int64_t)s->hob_hcyl << 40) |
1153: ((int64_t) s->hob_lcyl << 32) |
1154: ((int64_t) s->hob_sector << 24) |
1155: ((int64_t) s->hcyl << 16) |
1156: ((int64_t) s->lcyl << 8) | s->sector;
1157: }
1158: }
1159: else
1160: {
1161: sector_num = ((s->hcyl << 8) | s->lcyl) * s->heads * s->sectors +
1162: (s->select & 0x0f) * s->sectors + (s->sector - 1);
1163: }
1164: return sector_num;
1165: }
1166:
1167: static void ide_set_sector(IDEState *s, int64_t sector_num)
1168: {
1169: unsigned int cyl, r;
1170: if (s->select & 0x40)
1171: {
1172: if (!s->lba48)
1173: {
1174: s->select = (s->select & 0xf0) | (sector_num >> 24);
1175: s->hcyl = (sector_num >> 16);
1176: s->lcyl = (sector_num >> 8);
1177: s->sector = (sector_num);
1178: }
1179: else
1180: {
1181: s->sector = sector_num;
1182: s->lcyl = sector_num >> 8;
1183: s->hcyl = sector_num >> 16;
1184: s->hob_sector = sector_num >> 24;
1185: s->hob_lcyl = sector_num >> 32;
1186: s->hob_hcyl = sector_num >> 40;
1187: }
1188: }
1189: else
1190: {
1191: cyl = sector_num / (s->heads * s->sectors);
1192: r = sector_num % (s->heads * s->sectors);
1193: s->hcyl = cyl >> 8;
1194: s->lcyl = cyl;
1195: s->select = (s->select & 0xf0) | ((r / s->sectors) & 0x0f);
1196: s->sector = (r % s->sectors) + 1;
1197: }
1198: }
1199:
1200: static void ide_sector_read(IDEState *s)
1201: {
1202: int64_t sector_num;
1203: int ret, n;
1204:
1205: s->status = READY_STAT | SEEK_STAT;
1206: s->error = 0; /* not needed by IDE spec, but needed by Windows */
1207: sector_num = ide_get_sector(s);
1208: n = s->nsector;
1209: if (n == 0)
1210: {
1211: /* no more sector to read from disk */
1212: ide_transfer_stop(s);
1213: }
1214: else
1215: {
1216: #if defined(DEBUG_IDE)
1217: printf("read sector=%Ld\n", sector_num);
1218: #endif
1219: if (n > s->req_nb_sectors)
1220: n = s->req_nb_sectors;
1221: ret = bdrv_read(s->bs, sector_num, s->io_buffer, n);
1222: ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_read);
1223: ide_set_irq(s);
1224: ide_set_sector(s, sector_num + n);
1225: s->nsector -= n;
1226: }
1227: }
1228:
1229:
1230: static void ide_sector_write(IDEState *s)
1231: {
1232: int64_t sector_num;
1233: int ret, n, n1;
1234:
1235: s->status = READY_STAT | SEEK_STAT;
1236: sector_num = ide_get_sector(s);
1237: #if defined(DEBUG_IDE)
1238: printf("write sector=%Ld\n", sector_num);
1239: #endif
1240: n = s->nsector;
1241: if (n > s->req_nb_sectors)
1242: n = s->req_nb_sectors;
1243: ret = bdrv_write(s->bs, sector_num, s->io_buffer, n);
1244: s->nsector -= n;
1245: if (s->nsector == 0)
1246: {
1247: /* no more sectors to write */
1248: ide_transfer_stop(s);
1249: }
1250: else
1251: {
1252: n1 = s->nsector;
1253: if (n1 > s->req_nb_sectors)
1254: n1 = s->req_nb_sectors;
1255: ide_transfer_start(s, s->io_buffer, 512 * n1, ide_sector_write);
1256: }
1257: ide_set_sector(s, sector_num + n);
1258:
1259: ide_set_irq(s);
1260: }
1261:
1262:
1263: static void ide_atapi_cmd_ok(IDEState *s)
1264: {
1265: s->error = 0;
1266: s->status = READY_STAT;
1267: s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
1268: ide_set_irq(s);
1269: }
1270:
1271: static void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
1272: {
1273: #ifdef DEBUG_IDE_ATAPI
1274: printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
1275: #endif
1276: s->error = sense_key << 4;
1277: s->status = READY_STAT | ERR_STAT;
1278: s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
1279: s->sense_key = sense_key;
1280: s->asc = asc;
1281: ide_set_irq(s);
1282: }
1283:
1284: static inline void cpu_to_ube16(uint8_t *buf, int val)
1285: {
1286: buf[0] = val >> 8;
1287: buf[1] = val;
1288: }
1289:
1290: static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
1291: {
1292: buf[0] = val >> 24;
1293: buf[1] = val >> 16;
1294: buf[2] = val >> 8;
1295: buf[3] = val;
1296: }
1297:
1298: static inline int ube16_to_cpu(const uint8_t *buf)
1299: {
1300: return (buf[0] << 8) | buf[1];
1301: }
1302:
1303: static inline int ube32_to_cpu(const uint8_t *buf)
1304: {
1305: return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1306: }
1307:
1308: static void lba_to_msf(uint8_t *buf, int lba)
1309: {
1310: lba += 150;
1311: buf[0] = (lba / 75) / 60;
1312: buf[1] = (lba / 75) % 60;
1313: buf[2] = lba % 75;
1314: }
1315:
1316: static void cd_data_to_raw(uint8_t *buf, int lba)
1317: {
1318: /* sync bytes */
1319: buf[0] = 0x00;
1320: memset(buf + 1, 0xff, 10);
1321: buf[11] = 0x00;
1322: buf += 12;
1323: /* MSF */
1324: lba_to_msf(buf, lba);
1325: buf[3] = 0x01; /* mode 1 data */
1326: buf += 4;
1327: /* data */
1328: buf += 2048;
1329: /* XXX: ECC not computed */
1330: memset(buf, 0, 288);
1331: }
1332:
1333: static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
1334: int sector_size)
1335: {
1336: int ret;
1337:
1338: switch (sector_size)
1339: {
1340: case 2048:
1341: ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
1342: break;
1343: case 2352:
1344: ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
1345: if (ret < 0)
1346: return ret;
1347: cd_data_to_raw(buf, lba);
1348: break;
1349: default:
1350: ret = -EIO;
1351: break;
1352: }
1353: return ret;
1354: }
1355:
1356: static void ide_atapi_io_error(IDEState *s, int ret)
1357: {
1358: /* XXX: handle more errors */
1359: if (ret == -ENOMEDIUM)
1360: {
1361: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1362: ASC_MEDIUM_NOT_PRESENT);
1363: }
1364: else
1365: {
1366: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1367: ASC_LOGICAL_BLOCK_OOR);
1368: }
1369: }
1370:
1371: /* The whole ATAPI transfer logic is handled in this function */
1372: static void ide_atapi_cmd_reply_end(IDEState *s)
1373: {
1374: int byte_count_limit, size, ret;
1375: #ifdef DEBUG_IDE_ATAPI
1376: printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
1377: s->packet_transfer_size,
1378: s->elementary_transfer_size,
1379: s->io_buffer_index);
1380: #endif
1381: if (s->packet_transfer_size <= 0)
1382: {
1383: /* end of transfer */
1384: ide_transfer_stop(s);
1385: s->status = READY_STAT;
1386: s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
1387: ide_set_irq(s);
1388: #ifdef DEBUG_IDE_ATAPI
1389: printf("status=0x%x\n", s->status);
1390: #endif
1391: }
1392: else
1393: {
1394: /* see if a new sector must be read */
1395: if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size)
1396: {
1397: ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
1398: if (ret < 0)
1399: {
1400: ide_transfer_stop(s);
1401: ide_atapi_io_error(s, ret);
1402: return;
1403: }
1404: s->lba++;
1405: s->io_buffer_index = 0;
1406: }
1407: if (s->elementary_transfer_size > 0)
1408: {
1409: /* there are some data left to transmit in this elementary
1410: transfer */
1411: size = s->cd_sector_size - s->io_buffer_index;
1412: if (size > s->elementary_transfer_size)
1413: size = s->elementary_transfer_size;
1414: ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
1415: size, ide_atapi_cmd_reply_end);
1416: s->packet_transfer_size -= size;
1417: s->elementary_transfer_size -= size;
1418: s->io_buffer_index += size;
1419: }
1420: else
1421: {
1422: /* a new transfer is needed */
1423: s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
1424: byte_count_limit = s->lcyl | (s->hcyl << 8);
1425: #ifdef DEBUG_IDE_ATAPI
1426: printf("byte_count_limit=%d\n", byte_count_limit);
1427: #endif
1428: if (byte_count_limit == 0xffff)
1429: byte_count_limit--;
1430: size = s->packet_transfer_size;
1431: if (size > byte_count_limit)
1432: {
1433: /* byte count limit must be even if this case */
1434: if (byte_count_limit & 1)
1435: byte_count_limit--;
1436: size = byte_count_limit;
1437: }
1438: s->lcyl = size;
1439: s->hcyl = size >> 8;
1440: s->elementary_transfer_size = size;
1441: /* we cannot transmit more than one sector at a time */
1442: if (s->lba != -1)
1443: {
1444: if (size > (s->cd_sector_size - s->io_buffer_index))
1445: size = (s->cd_sector_size - s->io_buffer_index);
1446: }
1447: ide_transfer_start(s, s->io_buffer + s->io_buffer_index,
1448: size, ide_atapi_cmd_reply_end);
1449: s->packet_transfer_size -= size;
1450: s->elementary_transfer_size -= size;
1451: s->io_buffer_index += size;
1452: ide_set_irq(s);
1453: #ifdef DEBUG_IDE_ATAPI
1454: printf("status=0x%x\n", s->status);
1455: #endif
1456: }
1457: }
1458: }
1459:
1460: /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
1461: static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
1462: {
1463: if (size > max_size)
1464: size = max_size;
1465: s->lba = -1; /* no sector read */
1466: s->packet_transfer_size = size;
1467: s->io_buffer_size = size; /* dma: send the reply data as one chunk */
1468: s->elementary_transfer_size = 0;
1469: s->io_buffer_index = 0;
1470:
1471: s->status = READY_STAT;
1472: ide_atapi_cmd_reply_end(s);
1473: }
1474:
1475: /* start a CD-CDROM read command */
1476: static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
1477: int sector_size)
1478: {
1479: #ifdef DEBUG_IDE_ATAPI
1480: printf("read pio: LBA=%d nb_sectors=%d\n", lba, nb_sectors);
1481: #endif
1482: s->lba = lba;
1483: s->packet_transfer_size = nb_sectors * sector_size;
1484: s->elementary_transfer_size = 0;
1485: s->io_buffer_index = sector_size;
1486: s->cd_sector_size = sector_size;
1487:
1488: s->status = READY_STAT;
1489: ide_atapi_cmd_reply_end(s);
1490: }
1491:
1492:
1493: static void ide_atapi_cmd(IDEState *s)
1494: {
1495: const uint8_t *packet;
1496: uint8_t *buf;
1497: int max_len;
1498:
1499: packet = s->io_buffer;
1500: buf = s->io_buffer;
1501: #ifdef DEBUG_IDE_ATAPI
1502: {
1503: int i;
1504: printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1505: for (i = 0; i < ATAPI_PACKET_SIZE; i++)
1506: {
1507: printf(" %02x", packet[i]);
1508: }
1509: printf("\n");
1510: }
1511: #endif
1512: switch (s->io_buffer[0])
1513: {
1514: case GPCMD_TEST_UNIT_READY:
1515: if (bdrv_is_inserted(s->bs))
1516: {
1517: ide_atapi_cmd_ok(s);
1518: }
1519: else
1520: {
1521: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1522: ASC_MEDIUM_NOT_PRESENT);
1523: }
1524: break;
1525: case GPCMD_MODE_SENSE_6:
1526: case GPCMD_MODE_SENSE_10:
1527: {
1528: int action, code;
1529: if (packet[0] == GPCMD_MODE_SENSE_10)
1530: max_len = ube16_to_cpu(packet + 7);
1531: else
1532: max_len = packet[4];
1533: action = packet[2] >> 6;
1534: code = packet[2] & 0x3f;
1535: switch (action)
1536: {
1537: case 0: /* current values */
1538: switch (code)
1539: {
1540: case 0x01: /* error recovery */
1541: cpu_to_ube16(&buf[0], 16 + 6);
1542: buf[2] = 0x70;
1543: buf[3] = 0;
1544: buf[4] = 0;
1545: buf[5] = 0;
1546: buf[6] = 0;
1547: buf[7] = 0;
1548:
1549: buf[8] = 0x01;
1550: buf[9] = 0x06;
1551: buf[10] = 0x00;
1552: buf[11] = 0x05;
1553: buf[12] = 0x00;
1554: buf[13] = 0x00;
1555: buf[14] = 0x00;
1556: buf[15] = 0x00;
1557: ide_atapi_cmd_reply(s, 16, max_len);
1558: break;
1559: case 0x2a:
1560: cpu_to_ube16(&buf[0], 28 + 6);
1561: buf[2] = 0x70;
1562: buf[3] = 0;
1563: buf[4] = 0;
1564: buf[5] = 0;
1565: buf[6] = 0;
1566: buf[7] = 0;
1567:
1568: buf[8] = 0x2a;
1569: buf[9] = 0x12;
1570: buf[10] = 0x00;
1571: buf[11] = 0x00;
1572:
1573: buf[12] = 0x70;
1574: buf[13] = 3 << 5;
1575: buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
1576: if (bdrv_is_locked(s->bs))
1577: buf[6] |= 1 << 1;
1578: buf[15] = 0x00;
1579: cpu_to_ube16(&buf[16], 706);
1580: buf[18] = 0;
1581: buf[19] = 2;
1582: cpu_to_ube16(&buf[20], 512);
1583: cpu_to_ube16(&buf[22], 706);
1584: buf[24] = 0;
1585: buf[25] = 0;
1586: buf[26] = 0;
1587: buf[27] = 0;
1588: ide_atapi_cmd_reply(s, 28, max_len);
1589: break;
1590: default:
1591: goto error_cmd;
1592: }
1593: break;
1594: case 1: /* changeable values */
1595: goto error_cmd;
1596: case 2: /* default values */
1597: goto error_cmd;
1598: default:
1599: case 3: /* saved values */
1600: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1601: ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
1602: break;
1603: }
1604: }
1605: break;
1606: case GPCMD_REQUEST_SENSE:
1607: max_len = packet[4];
1608: memset(buf, 0, 18);
1609: buf[0] = 0x70 | (1 << 7);
1610: buf[2] = s->sense_key;
1611: buf[7] = 10;
1612: buf[12] = s->asc;
1613: ide_atapi_cmd_reply(s, 18, max_len);
1614: break;
1615: case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
1616: if (bdrv_is_inserted(s->bs))
1617: {
1618: bdrv_set_locked(s->bs, packet[4] & 1);
1619: ide_atapi_cmd_ok(s);
1620: }
1621: else
1622: {
1623: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1624: ASC_MEDIUM_NOT_PRESENT);
1625: }
1626: break;
1627: case GPCMD_READ_10:
1628: case GPCMD_READ_12:
1629: {
1630: int nb_sectors, lba;
1631:
1632: if (packet[0] == GPCMD_READ_10)
1633: nb_sectors = ube16_to_cpu(packet + 7);
1634: else
1635: nb_sectors = ube32_to_cpu(packet + 6);
1636: lba = ube32_to_cpu(packet + 2);
1637: if (nb_sectors == 0)
1638: {
1639: ide_atapi_cmd_ok(s);
1640: break;
1641: }
1642: ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1643: }
1644: break;
1645: case GPCMD_READ_CD:
1646: {
1647: int nb_sectors, lba, transfer_request;
1648:
1649: nb_sectors = (packet[6] << 16) | (packet[7] << 8) | packet[8];
1650: lba = ube32_to_cpu(packet + 2);
1651: if (nb_sectors == 0)
1652: {
1653: ide_atapi_cmd_ok(s);
1654: break;
1655: }
1656: transfer_request = packet[9];
1657: switch (transfer_request & 0xf8)
1658: {
1659: case 0x00:
1660: /* nothing */
1661: ide_atapi_cmd_ok(s);
1662: break;
1663: case 0x10:
1664: /* normal read */
1665: ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1666: break;
1667: case 0xf8:
1668: /* read all data */
1669: ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1670: break;
1671: default:
1672: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1673: ASC_INV_FIELD_IN_CMD_PACKET);
1674: break;
1675: }
1676: }
1677: break;
1678: case GPCMD_SEEK:
1679: {
1680: unsigned int lba;
1681: uint64_t total_sectors;
1682:
1683: bdrv_get_geometry(s->bs, &total_sectors);
1684: total_sectors >>= 2;
1685: if (total_sectors == 0)
1686: {
1687: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1688: ASC_MEDIUM_NOT_PRESENT);
1689: break;
1690: }
1691: lba = ube32_to_cpu(packet + 2);
1692: if (lba >= total_sectors)
1693: {
1694: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1695: ASC_LOGICAL_BLOCK_OOR);
1696: break;
1697: }
1698: ide_atapi_cmd_ok(s);
1699: }
1700: break;
1701: case GPCMD_START_STOP_UNIT:
1702: {
1703: int start, eject;
1704: start = packet[4] & 1;
1705: eject = (packet[4] >> 1) & 1;
1706:
1707: if (eject && !start)
1708: {
1709: /* eject the disk */
1710: bdrv_eject(s->bs, 1);
1711: }
1712: else if (eject && start)
1713: {
1714: /* close the tray */
1715: bdrv_eject(s->bs, 0);
1716: }
1717: ide_atapi_cmd_ok(s);
1718: }
1719: break;
1720: case GPCMD_MECHANISM_STATUS:
1721: {
1722: max_len = ube16_to_cpu(packet + 8);
1723: cpu_to_ube16(buf, 0);
1724: /* no current LBA */
1725: buf[2] = 0;
1726: buf[3] = 0;
1727: buf[4] = 0;
1728: buf[5] = 1;
1729: cpu_to_ube16(buf + 6, 0);
1730: ide_atapi_cmd_reply(s, 8, max_len);
1731: }
1732: break;
1733: case GPCMD_READ_TOC_PMA_ATIP:
1734: {
1735: int format, msf, start_track, len;
1736: uint64_t total_sectors;
1737:
1738: bdrv_get_geometry(s->bs, &total_sectors);
1739: total_sectors >>= 2;
1740: if (total_sectors == 0)
1741: {
1742: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1743: ASC_MEDIUM_NOT_PRESENT);
1744: break;
1745: }
1746: max_len = ube16_to_cpu(packet + 7);
1747: format = packet[9] >> 6;
1748: msf = (packet[1] >> 1) & 1;
1749: start_track = packet[6];
1750: switch (format)
1751: {
1752: case 0:
1753: fprintf(stderr,"FIXME: cdrom_read_toc");
1754: len=-1;
1755: //len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1756: if (len < 0)
1757: goto error_cmd;
1758: ide_atapi_cmd_reply(s, len, max_len);
1759: break;
1760: case 1:
1761: /* multi session : only a single session defined */
1762: memset(buf, 0, 12);
1763: buf[1] = 0x0a;
1764: buf[2] = 0x01;
1765: buf[3] = 0x01;
1766: ide_atapi_cmd_reply(s, 12, max_len);
1767: break;
1768: case 2:
1769: fprintf(stderr,"FIXME: cdrom_read_toc_raw");
1770: len=-1;
1771: //len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1772: if (len < 0)
1773: goto error_cmd;
1774: ide_atapi_cmd_reply(s, len, max_len);
1775: break;
1776: default:
1777: error_cmd:
1778: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1779: ASC_INV_FIELD_IN_CMD_PACKET);
1780: break;
1781: }
1782: }
1783: break;
1784: case GPCMD_READ_CDVD_CAPACITY:
1785: {
1786: uint64_t total_sectors;
1787:
1788: bdrv_get_geometry(s->bs, &total_sectors);
1789: total_sectors >>= 2;
1790: if (total_sectors == 0)
1791: {
1792: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1793: ASC_MEDIUM_NOT_PRESENT);
1794: break;
1795: }
1796: /* NOTE: it is really the number of sectors minus 1 */
1797: cpu_to_ube32(buf, total_sectors - 1);
1798: cpu_to_ube32(buf + 4, 2048);
1799: ide_atapi_cmd_reply(s, 8, 8);
1800: }
1801: break;
1802: case GPCMD_READ_DVD_STRUCTURE:
1803: {
1804: int media = packet[1];
1805: int layer = packet[6];
1806: int format = packet[2];
1807: uint64_t total_sectors;
1808:
1809: if (media != 0 || layer != 0)
1810: {
1811: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1812: ASC_INV_FIELD_IN_CMD_PACKET);
1813: }
1814:
1815: switch (format)
1816: {
1817: case 0:
1818: bdrv_get_geometry(s->bs, &total_sectors);
1819: total_sectors >>= 2;
1820: if (total_sectors == 0)
1821: {
1822: ide_atapi_cmd_error(s, SENSE_NOT_READY,
1823: ASC_MEDIUM_NOT_PRESENT);
1824: break;
1825: }
1826:
1827: memset(buf, 0, 2052);
1828:
1829: buf[4] = 1; // DVD-ROM, part version 1
1830: buf[5] = 0xf; // 120mm disc, maximum rate unspecified
1831: buf[6] = 0; // one layer, embossed data
1832: buf[7] = 0;
1833:
1834: cpu_to_ube32(buf + 8, 0);
1835: cpu_to_ube32(buf + 12, total_sectors - 1);
1836: cpu_to_ube32(buf + 16, total_sectors - 1);
1837:
1838: cpu_to_be16wu((uint16_t *)buf, 2048 + 4);
1839:
1840: ide_atapi_cmd_reply(s, 2048 + 3, 2048 + 4);
1841: break;
1842:
1843: default:
1844: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1845: ASC_INV_FIELD_IN_CMD_PACKET);
1846: break;
1847: }
1848: }
1849: break;
1850: case GPCMD_SET_SPEED:
1851: ide_atapi_cmd_ok(s);
1852: break;
1853: case GPCMD_INQUIRY:
1854: max_len = packet[4];
1855: buf[0] = 0x05; /* CD-ROM */
1856: buf[1] = 0x80; /* removable */
1857: buf[2] = 0x00; /* ISO */
1858: buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
1859: buf[4] = 31; /* additional length */
1860: buf[5] = 0; /* reserved */
1861: buf[6] = 0; /* reserved */
1862: buf[7] = 0; /* reserved */
1863: padstr8(buf + 8, 8, "QEMU");
1864: padstr8(buf + 16, 16, "QEMU CD-ROM");
1865: padstr8(buf + 32, 4, FW_VERSION);
1866: ide_atapi_cmd_reply(s, 36, max_len);
1867: break;
1868: case GPCMD_GET_CONFIGURATION:
1869: {
1870: uint64_t total_sectors;
1871:
1872: /* only feature 0 is supported */
1873: if (packet[2] != 0 || packet[3] != 0)
1874: {
1875: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1876: ASC_INV_FIELD_IN_CMD_PACKET);
1877: break;
1878: }
1879: memset(buf, 0, 32);
1880: bdrv_get_geometry(s->bs, &total_sectors);
1881: buf[3] = 16;
1882: buf[7] = total_sectors <= 1433600 ? 0x08 : 0x10; /* current profile */
1883: buf[10] = 0x10 | 0x1;
1884: buf[11] = 0x08; /* size of profile list */
1885: buf[13] = 0x10; /* DVD-ROM profile */
1886: buf[14] = buf[7] == 0x10; /* (in)active */
1887: buf[17] = 0x08; /* CD-ROM profile */
1888: buf[18] = buf[7] == 0x08; /* (in)active */
1889: ide_atapi_cmd_reply(s, 32, 32);
1890: break;
1891: }
1892: default:
1893: ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1894: ASC_ILLEGAL_OPCODE);
1895: break;
1896: }
1897: }
1898:
1899:
1900: /* called when the inserted state of the media has changed */
1901: static void cdrom_change_cb(void *opaque)
1902: {
1903: IDEState *s = opaque;
1904: uint64_t nb_sectors;
1905:
1906: /* XXX: send interrupt too */
1907: bdrv_get_geometry(s->bs, &nb_sectors);
1908: s->nb_sectors = nb_sectors;
1909: }
1910:
1911: static void ide_cmd_lba48_transform(IDEState *s, int lba48)
1912: {
1913: s->lba48 = lba48;
1914:
1915: /* handle the 'magic' 0 nsector count conversion here. to avoid
1916: * fiddling with the rest of the read logic, we just store the
1917: * full sector count in ->nsector and ignore ->hob_nsector from now
1918: */
1919: if (!s->lba48)
1920: {
1921: if (!s->nsector)
1922: s->nsector = 256;
1923: }
1924: else
1925: {
1926: if (!s->nsector && !s->hob_nsector)
1927: s->nsector = 65536;
1928: else
1929: {
1930: int lo = s->nsector;
1931: int hi = s->hob_nsector;
1932:
1933: s->nsector = (hi << 8) | lo;
1934: }
1935: }
1936: }
1937:
1938: static void ide_clear_hob(IDEState *ide_if)
1939: {
1940: /* any write clears HOB high bit of device control register */
1941: ide_if[0].select &= ~(1 << 7);
1942: ide_if[1].select &= ~(1 << 7);
1943: }
1944:
1945: static void ide_ioport_write(void *opaque, uint32_t addr, uint32_t val)
1946: {
1947: IDEState *ide_if = opaque;
1948: IDEState *s;
1949: int unit, n;
1950: int lba48 = 0;
1951:
1952: #ifdef DEBUG_IDE
1953: printf("IDE: write addr=0x%x val=0x%02x\n", addr, val);
1954: #endif
1955:
1956: addr &= 7;
1957: switch (addr)
1958: {
1959: case 0:
1960: break;
1961: case 1:
1962: ide_clear_hob(ide_if);
1963: /* NOTE: data is written to the two drives */
1964: ide_if[0].hob_feature = ide_if[0].feature;
1965: ide_if[1].hob_feature = ide_if[1].feature;
1966: ide_if[0].feature = val;
1967: ide_if[1].feature = val;
1968: break;
1969: case 2:
1970: ide_clear_hob(ide_if);
1971: ide_if[0].hob_nsector = ide_if[0].nsector;
1972: ide_if[1].hob_nsector = ide_if[1].nsector;
1973: ide_if[0].nsector = val;
1974: ide_if[1].nsector = val;
1975: break;
1976: case 3:
1977: ide_clear_hob(ide_if);
1978: ide_if[0].hob_sector = ide_if[0].sector;
1979: ide_if[1].hob_sector = ide_if[1].sector;
1980: ide_if[0].sector = val;
1981: ide_if[1].sector = val;
1982: break;
1983: case 4:
1984: ide_clear_hob(ide_if);
1985: ide_if[0].hob_lcyl = ide_if[0].lcyl;
1986: ide_if[1].hob_lcyl = ide_if[1].lcyl;
1987: ide_if[0].lcyl = val;
1988: ide_if[1].lcyl = val;
1989: break;
1990: case 5:
1991: ide_clear_hob(ide_if);
1992: ide_if[0].hob_hcyl = ide_if[0].hcyl;
1993: ide_if[1].hob_hcyl = ide_if[1].hcyl;
1994: ide_if[0].hcyl = val;
1995: ide_if[1].hcyl = val;
1996: break;
1997: case 6:
1998: /* FIXME: HOB readback uses bit 7 */
1999: ide_if[0].select = (val & ~0x10) | 0xa0;
2000: ide_if[1].select = (val | 0x10) | 0xa0;
2001: /* select drive */
2002: unit = (val >> 4) & 1;
2003: s = ide_if + unit;
2004: ide_if->cur_drive = s;
2005: break;
2006: default:
2007: case 7:
2008: /* command */
2009: #if defined(DEBUG_IDE)
2010: printf("ide: CMD=%02x\n", val);
2011: #endif
2012: s = ide_if->cur_drive;
2013: /* ignore commands to non existant slave */
2014: if (s != ide_if && !s->bs)
2015: {
2016: fprintf(stderr,"CMD to non-existant slave!\n");
2017: break;
2018: }
2019:
2020: switch (val)
2021: {
2022: case WIN_IDENTIFY:
2023: if (s->bs && !s->is_cdrom)
2024: {
2025: ide_identify(s);
2026: s->status = READY_STAT | SEEK_STAT;
2027: ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
2028: }
2029: else
2030: {
2031: if (s->is_cdrom)
2032: {
2033: ide_set_signature(s);
2034: }
2035: ide_abort_command(s);
2036: }
2037: ide_set_irq(s);
2038: break;
2039: case WIN_SPECIFY:
2040: case WIN_RECAL:
2041: s->error = 0;
2042: s->status = READY_STAT | SEEK_STAT;
2043: ide_set_irq(s);
2044: break;
2045: case WIN_SETMULT:
2046: if ((s->nsector & 0xff) != 0 &&
2047: ((s->nsector & 0xff) > MAX_MULT_SECTORS ||
2048: (s->nsector & (s->nsector - 1)) != 0))
2049: {
2050: ide_abort_command(s);
2051: }
2052: else
2053: {
2054: s->mult_sectors = s->nsector & 0xff;
2055: s->status = READY_STAT;
2056: }
2057: ide_set_irq(s);
2058: break;
2059: case WIN_VERIFY_EXT:
2060: lba48 = 1;
2061: case WIN_VERIFY:
2062: case WIN_VERIFY_ONCE:
2063: /* do sector number check ? */
2064: ide_cmd_lba48_transform(s, lba48);
2065: s->status = READY_STAT;
2066: ide_set_irq(s);
2067: break;
2068: case WIN_READ_EXT:
2069: lba48 = 1;
2070: case WIN_READ:
2071: case WIN_READ_ONCE:
2072: if (!s->bs)
2073: goto abort_cmd;
2074: ide_cmd_lba48_transform(s, lba48);
2075: s->req_nb_sectors = 1;
2076: ide_sector_read(s);
2077: break;
2078: case WIN_WRITE_EXT:
2079: lba48 = 1;
2080: case WIN_WRITE:
2081: case WIN_WRITE_ONCE:
2082: case CFA_WRITE_SECT_WO_ERASE:
2083: case WIN_WRITE_VERIFY:
2084: ide_cmd_lba48_transform(s, lba48);
2085: s->error = 0;
2086: s->status = SEEK_STAT | READY_STAT;
2087: s->req_nb_sectors = 1;
2088: ide_transfer_start(s, s->io_buffer, 512, ide_sector_write);
2089: s->media_changed = 1;
2090: break;
2091: case WIN_MULTREAD_EXT:
2092: lba48 = 1;
2093: case WIN_MULTREAD:
2094: if (!s->mult_sectors)
2095: goto abort_cmd;
2096: ide_cmd_lba48_transform(s, lba48);
2097: s->req_nb_sectors = s->mult_sectors;
2098: ide_sector_read(s);
2099: break;
2100: case WIN_MULTWRITE_EXT:
2101: lba48 = 1;
2102: case WIN_MULTWRITE:
2103: case CFA_WRITE_MULTI_WO_ERASE:
2104: if (!s->mult_sectors)
2105: goto abort_cmd;
2106: ide_cmd_lba48_transform(s, lba48);
2107: s->error = 0;
2108: s->status = SEEK_STAT | READY_STAT;
2109: s->req_nb_sectors = s->mult_sectors;
2110: n = s->nsector;
2111: if (n > s->req_nb_sectors)
2112: n = s->req_nb_sectors;
2113: ide_transfer_start(s, s->io_buffer, 512 * n, ide_sector_write);
2114: s->media_changed = 1;
2115: break;
2116: case WIN_READDMA_EXT:
2117: lba48 = 1;
2118: case WIN_READDMA:
2119: case WIN_READDMA_ONCE:
2120: if (!s->bs)
2121: goto abort_cmd;
2122: ide_cmd_lba48_transform(s, lba48);
2123: // ide_sector_read_dma(s);
2124: fprintf(stderr, "IDE: DMA read not supported!\n");
2125: break;
2126: case WIN_WRITEDMA_EXT:
2127: lba48 = 1;
2128: case WIN_WRITEDMA:
2129: case WIN_WRITEDMA_ONCE:
2130: if (!s->bs)
2131: goto abort_cmd;
2132: ide_cmd_lba48_transform(s, lba48);
2133: // ide_sector_write_dma(s);
2134: fprintf(stderr, "IDE: DMA write not supported!\n");
2135: s->media_changed = 1;
2136: break;
2137: case WIN_READ_NATIVE_MAX_EXT:
2138: lba48 = 1;
2139: case WIN_READ_NATIVE_MAX:
2140: ide_cmd_lba48_transform(s, lba48);
2141: ide_set_sector(s, s->nb_sectors - 1);
2142: s->status = READY_STAT;
2143: ide_set_irq(s);
2144: break;
2145: case WIN_CHECKPOWERMODE1:
2146: case WIN_CHECKPOWERMODE2:
2147: s->nsector = 0xff; /* device active or idle */
2148: s->status = READY_STAT;
2149: ide_set_irq(s);
2150: break;
2151: case WIN_SETFEATURES:
2152: if (!s->bs)
2153: goto abort_cmd;
2154: /* XXX: valid for CDROM ? */
2155: switch (s->feature)
2156: {
2157: case 0xcc: /* reverting to power-on defaults enable */
2158: case 0x66: /* reverting to power-on defaults disable */
2159: case 0x02: /* write cache enable */
2160: case 0x82: /* write cache disable */
2161: case 0xaa: /* read look-ahead enable */
2162: case 0x55: /* read look-ahead disable */
2163: case 0x05: /* set advanced power management mode */
2164: case 0x85: /* disable advanced power management mode */
2165: case 0x69: /* NOP */
2166: case 0x67: /* NOP */
2167: case 0x96: /* NOP */
2168: case 0x9a: /* NOP */
2169: case 0x42: /* enable Automatic Acoustic Mode */
2170: case 0xc2: /* disable Automatic Acoustic Mode */
2171: s->status = READY_STAT | SEEK_STAT;
2172: ide_set_irq(s);
2173: break;
2174: case 0x03: /* set transfer mode */
2175: {
2176: uint8_t val = s->nsector & 0x07;
2177:
2178: switch (s->nsector >> 3)
2179: {
2180: case 0x00: /* pio default */
2181: case 0x01: /* pio mode */
2182: put_le16(s->identify_data + 63,0x07);
2183: put_le16(s->identify_data + 88,0x3f);
2184: break;
2185: case 0x04: /* mdma mode */
2186: put_le16(s->identify_data + 63,0x07 | (1 << (val + 8)));
2187: put_le16(s->identify_data + 88,0x3f);
2188: break;
2189: case 0x08: /* udma mode */
2190: put_le16(s->identify_data + 63,0x07);
2191: put_le16(s->identify_data + 88,0x3f | (1 << (val + 8)));
2192: break;
2193: default:
2194: goto abort_cmd;
2195: }
2196: s->status = READY_STAT | SEEK_STAT;
2197: ide_set_irq(s);
2198: break;
2199: }
2200: default:
2201: goto abort_cmd;
2202: }
2203: break;
2204: case WIN_FLUSH_CACHE:
2205: case WIN_FLUSH_CACHE_EXT:
2206: if (s->bs)
2207: bdrv_flush(s->bs);
2208: s->status = READY_STAT;
2209: ide_set_irq(s);
2210: break;
2211: case WIN_STANDBY:
2212: case WIN_STANDBY2:
2213: case WIN_STANDBYNOW1:
2214: case WIN_STANDBYNOW2:
2215: case WIN_IDLEIMMEDIATE:
2216: case CFA_IDLEIMMEDIATE:
2217: case WIN_SETIDLE1:
2218: case WIN_SETIDLE2:
2219: case WIN_SLEEPNOW1:
2220: case WIN_SLEEPNOW2:
2221: s->status = READY_STAT;
2222: ide_set_irq(s);
2223: break;
2224: /* ATAPI commands */
2225: case WIN_PIDENTIFY:
2226: if (s->is_cdrom)
2227: {
2228: ide_atapi_identify(s);
2229: s->status = READY_STAT | SEEK_STAT;
2230: ide_transfer_start(s, s->io_buffer, 512, ide_transfer_stop);
2231: }
2232: else
2233: {
2234: ide_abort_command(s);
2235: }
2236: ide_set_irq(s);
2237: break;
2238: case WIN_DIAGNOSE:
2239: ide_set_signature(s);
2240: s->status = 0x00; /* NOTE: READY is _not_ set */
2241: s->error = 0x01;
2242: ide_set_irq(s);
2243: break;
2244: case WIN_SRST:
2245: if (!s->is_cdrom)
2246: goto abort_cmd;
2247: ide_set_signature(s);
2248: s->status = 0x00; /* NOTE: READY is _not_ set */
2249: s->error = 0x01;
2250: break;
2251: case WIN_PACKETCMD:
2252: if (!s->is_cdrom)
2253: goto abort_cmd;
2254: /* overlapping commands not supported */
2255: if (s->feature & 0x02)
2256: goto abort_cmd;
2257: s->status = READY_STAT;
2258: // s->atapi_dma = s->feature & 1;
2259: s->nsector = 1;
2260: ide_transfer_start(s, s->io_buffer, ATAPI_PACKET_SIZE,
2261: ide_atapi_cmd);
2262: break;
2263: default:
2264: abort_cmd:
2265: ide_abort_command(s);
2266: ide_set_irq(s);
2267: break;
2268: }
2269: }
2270: }
2271:
2272: static uint32_t ide_ioport_read(void *opaque, uint32_t addr1)
2273: {
2274: IDEState *ide_if = opaque;
2275: IDEState *s = ide_if->cur_drive;
2276: uint32_t addr;
2277: int ret, hob;
2278:
2279: addr = addr1 & 7;
2280: /* FIXME: HOB readback uses bit 7, but it's always set right now */
2281: //hob = s->select & (1 << 7);
2282: hob = 0;
2283: switch (addr)
2284: {
2285: case 0:
2286: ret = 0xff;
2287: break;
2288: case 1:
2289: if (!ide_if[0].bs && !ide_if[1].bs)
2290: ret = 0;
2291: else if (!hob)
2292: ret = s->error;
2293: else
2294: ret = s->hob_feature;
2295: break;
2296: case 2:
2297: if (!ide_if[0].bs && !ide_if[1].bs)
2298: ret = 0;
2299: else if (!hob)
2300: ret = s->nsector & 0xff;
2301: else
2302: ret = s->hob_nsector;
2303: break;
2304: case 3:
2305: if (!ide_if[0].bs && !ide_if[1].bs)
2306: ret = 0;
2307: else if (!hob)
2308: ret = s->sector;
2309: else
2310: ret = s->hob_sector;
2311: break;
2312: case 4:
2313: if (!ide_if[0].bs && !ide_if[1].bs)
2314: ret = 0;
2315: else if (!hob)
2316: ret = s->lcyl;
2317: else
2318: ret = s->hob_lcyl;
2319: break;
2320: case 5:
2321: if (!ide_if[0].bs && !ide_if[1].bs)
2322: ret = 0;
2323: else if (!hob)
2324: ret = s->hcyl;
2325: else
2326: ret = s->hob_hcyl;
2327: break;
2328: case 6:
2329: if (!ide_if[0].bs && !ide_if[1].bs)
2330: ret = 0;
2331: else
2332: ret = s->select;
2333: break;
2334: default:
2335: case 7:
2336: if ((!ide_if[0].bs && !ide_if[1].bs) ||
2337: (s != ide_if && !s->bs))
2338: ret = 0;
2339: else
2340: ret = s->status;
2341: /* Lower IRQ */
2342: MFP_GPIP |= 0x20;
2343: break;
2344: }
2345: #ifdef DEBUG_IDE
2346: printf("ide: read addr=0x%x val=%02x\n", addr1, ret);
2347: #endif
2348: return ret;
2349: }
2350:
2351: static uint32_t ide_status_read(void *opaque, uint32_t addr)
2352: {
2353: IDEState *ide_if = opaque;
2354: IDEState *s = ide_if->cur_drive;
2355: int ret;
2356:
2357: if ((!ide_if[0].bs && !ide_if[1].bs) ||
2358: (s != ide_if && !s->bs))
2359: ret = 0;
2360: else
2361: ret = s->status;
2362: #ifdef DEBUG_IDE
2363: printf("ide: read status addr=0x%x val=%02x\n", addr, ret);
2364: #endif
2365: return ret;
2366: }
2367:
2368: static void ide_cmd_write(void *opaque, uint32_t addr, uint32_t val)
2369: {
2370: IDEState *ide_if = opaque;
2371: IDEState *s;
2372: int i;
2373:
2374: #ifdef DEBUG_IDE
2375: printf("ide: write control addr=0x%x val=%02x\n", addr, val);
2376: #endif
2377: /* common for both drives */
2378: if (!(ide_if[0].cmd & IDE_CMD_RESET) &&
2379: (val & IDE_CMD_RESET))
2380: {
2381: /* reset low to high */
2382: for (i = 0;i < 2; i++)
2383: {
2384: s = &ide_if[i];
2385: s->status = BUSY_STAT | SEEK_STAT;
2386: s->error = 0x01;
2387: }
2388: }
2389: else if ((ide_if[0].cmd & IDE_CMD_RESET) &&
2390: !(val & IDE_CMD_RESET))
2391: {
2392: /* high to low */
2393: for (i = 0;i < 2; i++)
2394: {
2395: s = &ide_if[i];
2396: if (s->is_cdrom)
2397: s->status = 0x00; /* NOTE: READY is _not_ set */
2398: else
2399: s->status = READY_STAT | SEEK_STAT;
2400: ide_set_signature(s);
2401: }
2402: }
2403:
2404: ide_if[0].cmd = val;
2405: ide_if[1].cmd = val;
2406: }
2407:
2408: static void ide_data_writew(void *opaque, uint32_t addr, uint32_t val)
2409: {
2410: IDEState *s = ((IDEState *)opaque)->cur_drive;
2411: uint8_t *p;
2412:
2413: p = s->data_ptr;
2414: *(uint16_t *)p = le16_to_cpu(val);
2415: p += 2;
2416: s->data_ptr = p;
2417: if (p >= s->data_end)
2418: s->end_transfer_func(s);
2419: }
2420:
2421: static uint32_t ide_data_readw(void *opaque, uint32_t addr)
2422: {
2423: IDEState *s = ((IDEState *)opaque)->cur_drive;
2424: uint8_t *p;
2425: int ret;
2426: p = s->data_ptr;
2427: ret = cpu_to_le16(*(uint16_t *)p);
2428: p += 2;
2429: s->data_ptr = p;
2430: if (p >= s->data_end)
2431: s->end_transfer_func(s);
2432: return ret;
2433: }
2434:
2435: static void ide_data_writel(void *opaque, uint32_t addr, uint32_t val)
2436: {
2437: IDEState *s = ((IDEState *)opaque)->cur_drive;
2438: uint8_t *p;
2439:
2440: p = s->data_ptr;
2441: *(uint32_t *)p = le32_to_cpu(val);
2442: p += 4;
2443: s->data_ptr = p;
2444: if (p >= s->data_end)
2445: s->end_transfer_func(s);
2446: }
2447:
2448: static uint32_t ide_data_readl(void *opaque, uint32_t addr)
2449: {
2450: IDEState *s = ((IDEState *)opaque)->cur_drive;
2451: uint8_t *p;
2452: int ret;
2453:
2454: p = s->data_ptr;
2455: ret = cpu_to_le32(*(uint32_t *)p);
2456: p += 4;
2457: s->data_ptr = p;
2458: if (p >= s->data_end)
2459: s->end_transfer_func(s);
2460: return ret;
2461: }
2462:
2463: static void ide_dummy_transfer_stop(IDEState *s)
2464: {
2465: s->data_ptr = s->io_buffer;
2466: s->data_end = s->io_buffer;
2467: s->io_buffer[0] = 0xff;
2468: s->io_buffer[1] = 0xff;
2469: s->io_buffer[2] = 0xff;
2470: s->io_buffer[3] = 0xff;
2471: }
2472:
2473: static void ide_reset(IDEState *s)
2474: {
2475: s->mult_sectors = MAX_MULT_SECTORS;
2476: s->cur_drive = s;
2477: s->select = 0xa0;
2478: s->status = READY_STAT | SEEK_STAT;
2479:
2480: ide_set_signature(s);
2481: /* init the transfer handler so that 0xffff is returned on data
2482: accesses */
2483: s->end_transfer_func = ide_dummy_transfer_stop;
2484: ide_dummy_transfer_stop(s);
2485: s->media_changed = 0;
2486: }
2487:
2488: struct partition
2489: {
2490: uint8_t boot_ind; /* 0x80 - active */
2491: uint8_t head; /* starting head */
2492: uint8_t sector; /* starting sector */
2493: uint8_t cyl; /* starting cylinder */
2494: uint8_t sys_ind; /* What partition type */
2495: uint8_t end_head; /* end head */
2496: uint8_t end_sector; /* end sector */
2497: uint8_t end_cyl; /* end cylinder */
2498: uint32_t start_sect; /* starting sector counting from 0 */
2499: uint32_t nr_sects; /* nr of sectors in partition */
2500: } __attribute__((packed));
2501:
2502: /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
2503: static int guess_disk_lchs(IDEState *s,
2504: int *pcylinders, int *pheads, int *psectors)
2505: {
2506: uint8_t *buf;
2507: int ret, i, heads, sectors, cylinders;
2508: struct partition *p;
2509: uint32_t nr_sects;
2510:
2511: buf = qemu_memalign(512, 512);
2512: if (buf == NULL)
2513: return -1;
2514: ret = bdrv_read(s->bs, 0, buf, 1);
2515: if (ret < 0)
2516: {
2517: qemu_free(buf);
2518: return -1;
2519: }
2520: /* test msdos magic */
2521: if (buf[510] != 0x55 || buf[511] != 0xaa)
2522: {
2523: qemu_free(buf);
2524: return -1;
2525: }
2526: for (i = 0; i < 4; i++)
2527: {
2528: p = ((struct partition *)(buf + 0x1be)) + i;
2529: nr_sects = le32_to_cpu(p->nr_sects);
2530: if (nr_sects && p->end_head)
2531: {
2532: /* We make the assumption that the partition terminates on
2533: a cylinder boundary */
2534: heads = p->end_head + 1;
2535: sectors = p->end_sector & 63;
2536: if (sectors == 0)
2537: continue;
2538: cylinders = s->nb_sectors / (heads * sectors);
2539: if (cylinders < 1 || cylinders > 16383)
2540: continue;
2541: *pheads = heads;
2542: *psectors = sectors;
2543: *pcylinders = cylinders;
2544: #if 0
2545: printf("guessed geometry: LCHS=%d %d %d\n",
2546: cylinders, heads, sectors);
2547: #endif
2548: qemu_free(buf);
2549: return 0;
2550: }
2551: }
2552: qemu_free(buf);
2553: return -1;
2554: }
2555:
2556: static void ide_init2(IDEState *ide_state, BlockDriverState *hd0,
2557: BlockDriverState *hd1)
2558: {
2559: IDEState *s;
2560: static int drive_serial = 1;
2561: int i, cylinders, heads, secs, translation, lba_detected = 0;
2562: uint64_t nb_sectors;
2563:
2564: for (i = 0; i < 2; i++)
2565: {
2566: s = ide_state + i;
2567: s->io_buffer = qemu_memalign(512, MAX_MULT_SECTORS*512 + 4);
2568: if (i == 0)
2569: s->bs = hd0;
2570: else
2571: s->bs = hd1;
2572: if (s->bs)
2573: {
2574: bdrv_get_geometry(s->bs, &nb_sectors);
2575: s->nb_sectors = nb_sectors;
2576: /* if a geometry hint is available, use it */
2577: bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs);
2578: translation = bdrv_get_translation_hint(s->bs);
2579: if (cylinders != 0)
2580: {
2581: s->cylinders = cylinders;
2582: s->heads = heads;
2583: s->sectors = secs;
2584: }
2585: else
2586: {
2587: if (guess_disk_lchs(s, &cylinders, &heads, &secs) == 0)
2588: {
2589: if (heads > 16)
2590: {
2591: /* if heads > 16, it means that a BIOS LBA
2592: translation was active, so the default
2593: hardware geometry is OK */
2594: lba_detected = 1;
2595: goto default_geometry;
2596: }
2597: else
2598: {
2599: s->cylinders = cylinders;
2600: s->heads = heads;
2601: s->sectors = secs;
2602: /* disable any translation to be in sync with
2603: the logical geometry */
2604: if (translation == BIOS_ATA_TRANSLATION_AUTO)
2605: {
2606: bdrv_set_translation_hint(s->bs,
2607: BIOS_ATA_TRANSLATION_NONE);
2608: }
2609: }
2610: }
2611: else
2612: {
2613: default_geometry:
2614: /* if no geometry, use a standard physical disk geometry */
2615: cylinders = nb_sectors / (16 * 63);
2616: if (cylinders > 16383)
2617: cylinders = 16383;
2618: else if (cylinders < 2)
2619: cylinders = 2;
2620: s->cylinders = cylinders;
2621: s->heads = 16;
2622: s->sectors = 63;
2623: if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO))
2624: {
2625: if ((s->cylinders * s->heads) <= 131072)
2626: {
2627: bdrv_set_translation_hint(s->bs,
2628: BIOS_ATA_TRANSLATION_LARGE);
2629: }
2630: else
2631: {
2632: bdrv_set_translation_hint(s->bs,
2633: BIOS_ATA_TRANSLATION_LBA);
2634: }
2635: }
2636: }
2637: bdrv_set_geometry_hint(s->bs, s->cylinders, s->heads, s->sectors);
2638: }
2639: if (bdrv_get_type_hint(s->bs) == BDRV_TYPE_CDROM)
2640: {
2641: s->is_cdrom = 1;
2642: bdrv_set_change_cb(s->bs, cdrom_change_cb, s);
2643: }
2644: }
2645: s->drive_serial = drive_serial++;
2646:
2647: ide_reset(s);
2648: }
2649: }
2650:
2651:
2652: /*----------------------------------------------------------------------------*/
2653:
2654:
2655: static BlockDriverState *hd_table[2];
2656:
2657:
2658: /**
2659: * Initialize the IDE subsystem
2660: */
2661: void Ide_Init(void)
2662: {
2663: if (!ConfigureParams.HardDisk.bUseIdeHardDiskImage)
2664: return;
2665:
2666: opaque_ide_if = malloc(sizeof(IDEState) * 2);
2667: hd_table[0] = malloc(sizeof(BlockDriverState));
2668: hd_table[1] = malloc(sizeof(BlockDriverState));
2669:
2670: if (!opaque_ide_if || !hd_table[0] || !hd_table[1])
2671: {
2672: perror("Ide_Init");
2673: return;
2674: }
2675:
2676: memset(opaque_ide_if, 0, sizeof(IDEState) * 2);
2677:
2678: memset(hd_table[0], 0, sizeof(BlockDriverState));
2679: memset(hd_table[1], 0, sizeof(BlockDriverState));
2680:
2681: bdrv_open(hd_table[0], ConfigureParams.HardDisk.szIdeHardDiskImage, 0);
2682:
2683: ide_init2(&opaque_ide_if[0], hd_table[0], NULL /*hd_table[1]*/);
2684: }
2685:
2686:
2687: /**
2688: * Free resources from the IDE subsystem
2689: */
2690: void Ide_UnInit(void)
2691: {
2692: int i;
2693:
2694: for (i = 0; i < 2; i++)
2695: {
2696: if (hd_table[i])
2697: {
2698: if (bdrv_is_inserted(hd_table[i]))
2699: {
2700: bdrv_close(hd_table[i]);
2701: }
2702: free(hd_table[i]);
2703: hd_table[i] = NULL;
2704: }
2705: }
2706:
2707: if (opaque_ide_if)
2708: free(opaque_ide_if);
2709: opaque_ide_if = NULL;
1.1 root 2710: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.