Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // Definitions for X86 bios disks. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 6 | #ifndef __DISK_H |
| 7 | #define __DISK_H |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "types.h" // u8 |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 11 | #include "farptr.h" // struct segoff_s |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 12 | |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 13 | #define DISK_RET_SUCCESS 0x00 |
| 14 | #define DISK_RET_EPARAM 0x01 |
Kevin O'Connor | b74102d | 2008-03-03 21:57:30 -0500 | [diff] [blame] | 15 | #define DISK_RET_EADDRNOTFOUND 0x02 |
| 16 | #define DISK_RET_EWRITEPROTECT 0x03 |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 17 | #define DISK_RET_ECHANGED 0x06 |
| 18 | #define DISK_RET_EBOUNDARY 0x09 |
| 19 | #define DISK_RET_EBADTRACK 0x0c |
| 20 | #define DISK_RET_ECONTROLLER 0x20 |
| 21 | #define DISK_RET_ETIMEOUT 0x80 |
Kevin O'Connor | b74102d | 2008-03-03 21:57:30 -0500 | [diff] [blame] | 22 | #define DISK_RET_ENOTLOCKED 0xb0 |
| 23 | #define DISK_RET_ELOCKED 0xb1 |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 24 | #define DISK_RET_ENOTREMOVABLE 0xb2 |
Kevin O'Connor | b74102d | 2008-03-03 21:57:30 -0500 | [diff] [blame] | 25 | #define DISK_RET_ETOOMANYLOCKS 0xb4 |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 26 | #define DISK_RET_EMEDIA 0xC0 |
| 27 | #define DISK_RET_ENOTREADY 0xAA |
| 28 | |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 29 | |
| 30 | /**************************************************************** |
| 31 | * Interface structs |
| 32 | ****************************************************************/ |
| 33 | |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 34 | // Bios disk structures. |
| 35 | struct int13ext_s { |
| 36 | u8 size; |
| 37 | u8 reserved; |
| 38 | u16 count; |
Kevin O'Connor | 9f98542 | 2009-09-09 11:34:39 -0400 | [diff] [blame] | 39 | struct segoff_s data; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 40 | u64 lba; |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 41 | } PACKED; |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 42 | |
Kevin O'Connor | d314083 | 2012-05-13 22:46:12 -0400 | [diff] [blame] | 43 | // DPTE definition |
| 44 | struct dpte_s { |
| 45 | u16 iobase1; |
| 46 | u16 iobase2; |
| 47 | u8 prefix; |
| 48 | u8 unused; |
| 49 | u8 irq; |
| 50 | u8 blkcount; |
| 51 | u8 dma; |
| 52 | u8 pio; |
| 53 | u16 options; |
| 54 | u16 reserved; |
| 55 | u8 revision; |
| 56 | u8 checksum; |
| 57 | }; |
| 58 | |
| 59 | extern struct dpte_s DefaultDPTE; |
| 60 | |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 61 | // Disk Physical Table definition |
| 62 | struct int13dpt_s { |
| 63 | u16 size; |
| 64 | u16 infos; |
| 65 | u32 cylinders; |
| 66 | u32 heads; |
| 67 | u32 spt; |
Kevin O'Connor | 1bb3b5c | 2008-05-14 00:43:13 -0400 | [diff] [blame] | 68 | u64 sector_count; |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 69 | u16 blksize; |
Kevin O'Connor | d314083 | 2012-05-13 22:46:12 -0400 | [diff] [blame] | 70 | struct segoff_s dpte; |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 71 | u16 key; |
| 72 | u8 dpi_length; |
| 73 | u8 reserved1; |
| 74 | u16 reserved2; |
| 75 | u8 host_bus[4]; |
| 76 | u8 iface_type[8]; |
Kevin O'Connor | 53236cc | 2008-08-31 11:16:33 -0400 | [diff] [blame] | 77 | u64 iface_path; |
Gleb Natapov | f5154e2 | 2011-01-10 10:50:27 +0200 | [diff] [blame] | 78 | union { |
| 79 | struct { |
| 80 | u64 device_path; |
| 81 | u8 reserved3; |
| 82 | u8 checksum; |
| 83 | } phoenix; |
| 84 | struct { |
| 85 | u64 device_path[2]; |
| 86 | u8 reserved3; |
| 87 | u8 checksum; |
| 88 | } t13; |
| 89 | }; |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 90 | } PACKED; |
Kevin O'Connor | e43df9e | 2008-03-01 22:16:32 -0500 | [diff] [blame] | 91 | |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 92 | // Floppy "Disk Base Table" |
| 93 | struct floppy_dbt_s { |
| 94 | u8 specify1; |
| 95 | u8 specify2; |
| 96 | u8 shutoff_ticks; |
| 97 | u8 bps_code; |
| 98 | u8 sectors; |
| 99 | u8 interblock_len; |
| 100 | u8 data_len; |
| 101 | u8 gap_len; |
| 102 | u8 fill_byte; |
| 103 | u8 settle_time; |
| 104 | u8 startup_time; |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 105 | } PACKED; |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 106 | |
| 107 | struct floppy_ext_dbt_s { |
| 108 | struct floppy_dbt_s dbt; |
| 109 | // Extra fields |
| 110 | u8 max_track; |
| 111 | u8 data_rate; |
| 112 | u8 drive_type; |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 113 | } PACKED; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 114 | |
Kevin O'Connor | 31d8c8a | 2008-03-04 19:56:41 -0500 | [diff] [blame] | 115 | // Helper function for setting up a return code. |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 116 | struct bregs; |
Kevin O'Connor | 0560034 | 2009-01-02 13:10:58 -0500 | [diff] [blame] | 117 | void __disk_ret(struct bregs *regs, u32 linecode, const char *fname); |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 118 | #define disk_ret(regs, code) \ |
Kevin O'Connor | 0560034 | 2009-01-02 13:10:58 -0500 | [diff] [blame] | 119 | __disk_ret((regs), (code) | (__LINE__ << 8), __func__) |
Kevin O'Connor | dfefeb5 | 2009-12-13 13:04:17 -0500 | [diff] [blame] | 120 | void __disk_ret_unimplemented(struct bregs *regs, u32 linecode |
| 121 | , const char *fname); |
| 122 | #define disk_ret_unimplemented(regs, code) \ |
| 123 | __disk_ret_unimplemented((regs), (code) | (__LINE__ << 8), __func__) |
Kevin O'Connor | 31d8c8a | 2008-03-04 19:56:41 -0500 | [diff] [blame] | 124 | |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 125 | |
| 126 | /**************************************************************** |
Kevin O'Connor | 95827c4 | 2009-02-07 00:04:57 -0500 | [diff] [blame] | 127 | * Master boot record |
| 128 | ****************************************************************/ |
| 129 | |
| 130 | struct packed_chs_s { |
| 131 | u8 heads; |
| 132 | u8 sptcyl; |
| 133 | u8 cyllow; |
| 134 | }; |
| 135 | |
| 136 | struct partition_s { |
| 137 | u8 status; |
| 138 | struct packed_chs_s first; |
| 139 | u8 type; |
| 140 | struct packed_chs_s last; |
| 141 | u32 lba; |
| 142 | u32 count; |
| 143 | } PACKED; |
| 144 | |
| 145 | struct mbr_s { |
| 146 | u8 code[440]; |
| 147 | // 0x01b8 |
| 148 | u32 diskseg; |
| 149 | // 0x01bc |
| 150 | u16 null; |
| 151 | // 0x01be |
| 152 | struct partition_s partitions[4]; |
| 153 | // 0x01fe |
| 154 | u16 signature; |
| 155 | } PACKED; |
| 156 | |
| 157 | #define MBR_SIGNATURE 0xaa55 |
| 158 | |
| 159 | |
| 160 | /**************************************************************** |
Kevin O'Connor | 4524bf7 | 2008-12-31 00:31:03 -0500 | [diff] [blame] | 161 | * Disk command request |
| 162 | ****************************************************************/ |
| 163 | |
| 164 | struct disk_op_s { |
| 165 | u64 lba; |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 166 | void *buf_fl; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 167 | struct drive_s *drive_g; |
Kevin O'Connor | 4524bf7 | 2008-12-31 00:31:03 -0500 | [diff] [blame] | 168 | u16 count; |
Kevin O'Connor | 4524bf7 | 2008-12-31 00:31:03 -0500 | [diff] [blame] | 169 | u8 command; |
| 170 | }; |
| 171 | |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 172 | #define CMD_RESET 0x00 |
| 173 | #define CMD_READ 0x02 |
| 174 | #define CMD_WRITE 0x03 |
| 175 | #define CMD_VERIFY 0x04 |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 176 | #define CMD_FORMAT 0x05 |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 177 | #define CMD_SEEK 0x07 |
| 178 | #define CMD_ISREADY 0x10 |
Kevin O'Connor | 4524bf7 | 2008-12-31 00:31:03 -0500 | [diff] [blame] | 179 | |
| 180 | |
| 181 | /**************************************************************** |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 182 | * Global storage |
| 183 | ****************************************************************/ |
| 184 | |
| 185 | struct chs_s { |
| 186 | u16 heads; // # heads |
| 187 | u16 cylinders; // # cylinders |
| 188 | u16 spt; // # sectors / track |
| 189 | }; |
| 190 | |
Kevin O'Connor | d314083 | 2012-05-13 22:46:12 -0400 | [diff] [blame] | 191 | // ElTorito Device Emulation data |
| 192 | struct cdemu_s { |
| 193 | struct drive_s *emulated_drive_gf; |
| 194 | u32 ilba; |
| 195 | u16 buffer_segment; |
| 196 | u16 load_segment; |
| 197 | u16 sector_count; |
| 198 | u8 active; |
| 199 | u8 media; |
| 200 | u8 emulated_extdrive; |
| 201 | |
| 202 | // Virtual device |
| 203 | struct chs_s lchs; |
| 204 | }; |
| 205 | |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 206 | struct drive_s { |
Kevin O'Connor | a4bd919 | 2010-02-17 22:55:36 -0500 | [diff] [blame] | 207 | u8 type; // Driver type (DTYPE_*) |
| 208 | u8 floppy_type; // Type of floppy (only for floppy drives). |
| 209 | struct chs_s lchs; // Logical CHS |
| 210 | u64 sectors; // Total sectors count |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 211 | u32 cntl_id; // Unique id for a given driver type. |
| 212 | u8 removable; // Is media removable (currently unused) |
Kevin O'Connor | a4bd919 | 2010-02-17 22:55:36 -0500 | [diff] [blame] | 213 | |
| 214 | // Info for EDD calls |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 215 | u8 translation; // type of translation |
Kevin O'Connor | a4bd919 | 2010-02-17 22:55:36 -0500 | [diff] [blame] | 216 | u16 blksize; // block size |
| 217 | struct chs_s pchs; // Physical CHS |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 218 | }; |
| 219 | |
Kevin O'Connor | 36c93a5 | 2009-09-12 19:35:04 -0400 | [diff] [blame] | 220 | #define DISK_SECTOR_SIZE 512 |
| 221 | #define CDROM_SECTOR_SIZE 2048 |
| 222 | |
Paolo Bonzini | 0e7fb5f | 2011-11-16 13:02:55 +0100 | [diff] [blame] | 223 | #define DTYPE_NONE 0x00 |
| 224 | #define DTYPE_FLOPPY 0x01 |
| 225 | #define DTYPE_ATA 0x02 |
| 226 | #define DTYPE_ATAPI 0x03 |
| 227 | #define DTYPE_RAMDISK 0x04 |
| 228 | #define DTYPE_CDEMU 0x05 |
Kevin O'Connor | 4e752f5 | 2012-03-11 11:22:07 -0400 | [diff] [blame] | 229 | #define DTYPE_AHCI 0x06 |
| 230 | #define DTYPE_VIRTIO_SCSI 0x07 |
| 231 | #define DTYPE_VIRTIO_BLK 0x08 |
| 232 | #define DTYPE_USB 0x09 |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 233 | |
Kevin O'Connor | 575ffc8 | 2010-02-21 23:20:10 -0500 | [diff] [blame] | 234 | #define MAXDESCSIZE 80 |
| 235 | |
Kevin O'Connor | 4233766 | 2009-08-10 00:06:37 -0400 | [diff] [blame] | 236 | #define TRANSLATION_NONE 0 |
| 237 | #define TRANSLATION_LBA 1 |
| 238 | #define TRANSLATION_LARGE 2 |
| 239 | #define TRANSLATION_RECHS 3 |
| 240 | |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 241 | #define EXTTYPE_FLOPPY 0 |
| 242 | #define EXTTYPE_HD 1 |
| 243 | #define EXTTYPE_CD 2 |
| 244 | |
Kevin O'Connor | 51cfbe7 | 2009-08-18 22:38:49 -0400 | [diff] [blame] | 245 | #define EXTSTART_HD 0x80 |
| 246 | #define EXTSTART_CD 0xE0 |
| 247 | |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 248 | |
| 249 | /**************************************************************** |
| 250 | * Function defs |
| 251 | ****************************************************************/ |
| 252 | |
Kevin O'Connor | c892b13 | 2009-08-11 21:59:37 -0400 | [diff] [blame] | 253 | // block.c |
Kevin O'Connor | a0842f8 | 2010-12-29 11:05:46 -0500 | [diff] [blame] | 254 | extern u8 FloppyCount, CDCount; |
Gerd Hoffmann | d7a7cf3 | 2011-08-04 19:36:27 +0200 | [diff] [blame] | 255 | extern u8 *bounce_buf_fl; |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 256 | struct drive_s *getDrive(u8 exttype, u8 extdriveoffset); |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 257 | int getDriveId(u8 exttype, struct drive_s *drive_g); |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 258 | void map_floppy_drive(struct drive_s *drive_g); |
| 259 | void map_hd_drive(struct drive_s *drive_g); |
| 260 | void map_cd_drive(struct drive_s *drive_g); |
Kevin O'Connor | 36c93a5 | 2009-09-12 19:35:04 -0400 | [diff] [blame] | 261 | int process_op(struct disk_op_s *op); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 262 | int send_disk_op(struct disk_op_s *op); |
Gerd Hoffmann | d7a7cf3 | 2011-08-04 19:36:27 +0200 | [diff] [blame] | 263 | int bounce_buf_init(void); |
Kevin O'Connor | 609da23 | 2008-12-28 23:18:57 -0500 | [diff] [blame] | 264 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 265 | // floppy.c |
Kevin O'Connor | 941d3e4 | 2008-03-04 19:45:04 -0500 | [diff] [blame] | 266 | extern struct floppy_ext_dbt_s diskette_param_table2; |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 267 | void floppy_setup(void); |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 268 | struct drive_s *init_floppy(int floppyid, int ftype); |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 269 | int find_floppy_type(u32 size); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 270 | int process_floppy_op(struct disk_op_s *op); |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 271 | void floppy_tick(void); |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 272 | |
Kevin O'Connor | 31d8c8a | 2008-03-04 19:56:41 -0500 | [diff] [blame] | 273 | // cdrom.c |
Kevin O'Connor | d314083 | 2012-05-13 22:46:12 -0400 | [diff] [blame] | 274 | extern u8 CDRom_locks[]; |
| 275 | extern struct cdemu_s CDEmu; |
Kevin O'Connor | 8f469b9 | 2010-02-28 01:28:11 -0500 | [diff] [blame] | 276 | extern struct drive_s *cdemu_drive_gf; |
Kevin O'Connor | 36c93a5 | 2009-09-12 19:35:04 -0400 | [diff] [blame] | 277 | int process_cdemu_op(struct disk_op_s *op); |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 278 | void cdemu_setup(void); |
Kevin O'Connor | 31d8c8a | 2008-03-04 19:56:41 -0500 | [diff] [blame] | 279 | void cdemu_134b(struct bregs *regs); |
Gleb Natapov | 4c90a20 | 2010-12-07 13:50:54 +0200 | [diff] [blame] | 280 | int cdrom_boot(struct drive_s *drive_g); |
Kevin O'Connor | 31d8c8a | 2008-03-04 19:56:41 -0500 | [diff] [blame] | 281 | |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 282 | // ramdisk.c |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 283 | void ramdisk_setup(void); |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 284 | int process_ramdisk_op(struct disk_op_s *op); |
| 285 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 286 | #endif // disk.h |