blob: 3d07372ae99f7d2abe06d5ad3b372d40cd26afba [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Definitions for X86 bios disks.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __DISK_H
7#define __DISK_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "types.h" // u8
Kevin O'Connor609da232008-12-28 23:18:57 -050010#include "config.h" // CONFIG_*
Kevin O'Connor9f985422009-09-09 11:34:39 -040011#include "farptr.h" // struct segoff_s
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050012
Kevin O'Connore43df9e2008-03-01 22:16:32 -050013#define DISK_RET_SUCCESS 0x00
14#define DISK_RET_EPARAM 0x01
Kevin O'Connorb74102d2008-03-03 21:57:30 -050015#define DISK_RET_EADDRNOTFOUND 0x02
16#define DISK_RET_EWRITEPROTECT 0x03
Kevin O'Connore43df9e2008-03-01 22:16:32 -050017#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'Connorb74102d2008-03-03 21:57:30 -050022#define DISK_RET_ENOTLOCKED 0xb0
23#define DISK_RET_ELOCKED 0xb1
Kevin O'Connore43df9e2008-03-01 22:16:32 -050024#define DISK_RET_ENOTREMOVABLE 0xb2
Kevin O'Connorb74102d2008-03-03 21:57:30 -050025#define DISK_RET_ETOOMANYLOCKS 0xb4
Kevin O'Connore43df9e2008-03-01 22:16:32 -050026#define DISK_RET_EMEDIA 0xC0
27#define DISK_RET_ENOTREADY 0xAA
28
Kevin O'Connor609da232008-12-28 23:18:57 -050029
30/****************************************************************
31 * Interface structs
32 ****************************************************************/
33
Kevin O'Connore43df9e2008-03-01 22:16:32 -050034// Bios disk structures.
35struct int13ext_s {
36 u8 size;
37 u8 reserved;
38 u16 count;
Kevin O'Connor9f985422009-09-09 11:34:39 -040039 struct segoff_s data;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040040 u64 lba;
Kevin O'Connored128492008-03-11 11:14:59 -040041} PACKED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050042
Kevin O'Connord3140832012-05-13 22:46:12 -040043// DPTE definition
44struct 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
59extern struct dpte_s DefaultDPTE;
60
Kevin O'Connore43df9e2008-03-01 22:16:32 -050061// Disk Physical Table definition
62struct int13dpt_s {
63 u16 size;
64 u16 infos;
65 u32 cylinders;
66 u32 heads;
67 u32 spt;
Kevin O'Connor1bb3b5c2008-05-14 00:43:13 -040068 u64 sector_count;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050069 u16 blksize;
Kevin O'Connord3140832012-05-13 22:46:12 -040070 struct segoff_s dpte;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050071 u16 key;
72 u8 dpi_length;
73 u8 reserved1;
74 u16 reserved2;
75 u8 host_bus[4];
76 u8 iface_type[8];
Kevin O'Connor53236cc2008-08-31 11:16:33 -040077 u64 iface_path;
Gleb Natapovf5154e22011-01-10 10:50:27 +020078 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'Connored128492008-03-11 11:14:59 -040090} PACKED;
Kevin O'Connore43df9e2008-03-01 22:16:32 -050091
Kevin O'Connor44c631d2008-03-02 11:24:36 -050092// Floppy "Disk Base Table"
93struct 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'Connored128492008-03-11 11:14:59 -0400105} PACKED;
Kevin O'Connor44c631d2008-03-02 11:24:36 -0500106
107struct 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'Connored128492008-03-11 11:14:59 -0400113} PACKED;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500114
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500115// Helper function for setting up a return code.
Kevin O'Connor9521e262008-07-04 13:04:29 -0400116struct bregs;
Kevin O'Connor05600342009-01-02 13:10:58 -0500117void __disk_ret(struct bregs *regs, u32 linecode, const char *fname);
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400118#define disk_ret(regs, code) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500119 __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
Kevin O'Connordfefeb52009-12-13 13:04:17 -0500120void __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'Connor31d8c8a2008-03-04 19:56:41 -0500124
Kevin O'Connor609da232008-12-28 23:18:57 -0500125
126/****************************************************************
Kevin O'Connor95827c42009-02-07 00:04:57 -0500127 * Master boot record
128 ****************************************************************/
129
130struct packed_chs_s {
131 u8 heads;
132 u8 sptcyl;
133 u8 cyllow;
134};
135
136struct 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
145struct 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'Connor4524bf72008-12-31 00:31:03 -0500161 * Disk command request
162 ****************************************************************/
163
164struct disk_op_s {
165 u64 lba;
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500166 void *buf_fl;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400167 struct drive_s *drive_g;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500168 u16 count;
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500169 u8 command;
170};
171
Kevin O'Connor42337662009-08-10 00:06:37 -0400172#define CMD_RESET 0x00
173#define CMD_READ 0x02
174#define CMD_WRITE 0x03
175#define CMD_VERIFY 0x04
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400176#define CMD_FORMAT 0x05
Kevin O'Connor42337662009-08-10 00:06:37 -0400177#define CMD_SEEK 0x07
178#define CMD_ISREADY 0x10
Kevin O'Connor4524bf72008-12-31 00:31:03 -0500179
180
181/****************************************************************
Kevin O'Connor609da232008-12-28 23:18:57 -0500182 * Global storage
183 ****************************************************************/
184
185struct chs_s {
186 u16 heads; // # heads
187 u16 cylinders; // # cylinders
188 u16 spt; // # sectors / track
Kevin O'Connor48365582012-05-27 10:45:32 -0400189 u16 pad;
Kevin O'Connor609da232008-12-28 23:18:57 -0500190};
191
Kevin O'Connord3140832012-05-13 22:46:12 -0400192// ElTorito Device Emulation data
193struct cdemu_s {
194 struct drive_s *emulated_drive_gf;
195 u32 ilba;
196 u16 buffer_segment;
197 u16 load_segment;
198 u16 sector_count;
199 u8 active;
200 u8 media;
201 u8 emulated_extdrive;
202
203 // Virtual device
204 struct chs_s lchs;
205};
206
Kevin O'Connorc892b132009-08-11 21:59:37 -0400207struct drive_s {
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500208 u8 type; // Driver type (DTYPE_*)
209 u8 floppy_type; // Type of floppy (only for floppy drives).
210 struct chs_s lchs; // Logical CHS
211 u64 sectors; // Total sectors count
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500212 u32 cntl_id; // Unique id for a given driver type.
213 u8 removable; // Is media removable (currently unused)
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500214
215 // Info for EDD calls
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500216 u8 translation; // type of translation
Kevin O'Connora4bd9192010-02-17 22:55:36 -0500217 u16 blksize; // block size
218 struct chs_s pchs; // Physical CHS
Kevin O'Connor609da232008-12-28 23:18:57 -0500219};
220
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400221#define DISK_SECTOR_SIZE 512
222#define CDROM_SECTOR_SIZE 2048
223
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100224#define DTYPE_NONE 0x00
225#define DTYPE_FLOPPY 0x01
226#define DTYPE_ATA 0x02
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400227#define DTYPE_ATA_ATAPI 0x03
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100228#define DTYPE_RAMDISK 0x04
229#define DTYPE_CDEMU 0x05
Kevin O'Connor4e752f52012-03-11 11:22:07 -0400230#define DTYPE_AHCI 0x06
Kevin O'Connorbd6afe52012-07-21 12:01:12 -0400231#define DTYPE_AHCI_ATAPI 0x07
232#define DTYPE_VIRTIO_SCSI 0x08
233#define DTYPE_VIRTIO_BLK 0x09
234#define DTYPE_USB 0x0a
235#define DTYPE_UAS 0x0b
236#define DTYPE_LSI_SCSI 0x0c
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200237#define DTYPE_ESP_SCSI 0x0d
Kevin O'Connor42337662009-08-10 00:06:37 -0400238
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500239#define MAXDESCSIZE 80
240
Kevin O'Connor42337662009-08-10 00:06:37 -0400241#define TRANSLATION_NONE 0
242#define TRANSLATION_LBA 1
243#define TRANSLATION_LARGE 2
244#define TRANSLATION_RECHS 3
245
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400246#define EXTTYPE_FLOPPY 0
247#define EXTTYPE_HD 1
248#define EXTTYPE_CD 2
249
Kevin O'Connor51cfbe72009-08-18 22:38:49 -0400250#define EXTSTART_HD 0x80
251#define EXTSTART_CD 0xE0
252
Kevin O'Connor609da232008-12-28 23:18:57 -0500253
254/****************************************************************
255 * Function defs
256 ****************************************************************/
257
Kevin O'Connorc892b132009-08-11 21:59:37 -0400258// block.c
Kevin O'Connora0842f82010-12-29 11:05:46 -0500259extern u8 FloppyCount, CDCount;
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200260extern u8 *bounce_buf_fl;
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400261struct drive_s *getDrive(u8 exttype, u8 extdriveoffset);
Gleb Natapov4c90a202010-12-07 13:50:54 +0200262int getDriveId(u8 exttype, struct drive_s *drive_g);
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400263void map_floppy_drive(struct drive_s *drive_g);
264void map_hd_drive(struct drive_s *drive_g);
265void map_cd_drive(struct drive_s *drive_g);
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400266int process_op(struct disk_op_s *op);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400267int send_disk_op(struct disk_op_s *op);
Gerd Hoffmannd7a7cf32011-08-04 19:36:27 +0200268int bounce_buf_init(void);
Kevin O'Connor609da232008-12-28 23:18:57 -0500269
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500270// floppy.c
Kevin O'Connor941d3e42008-03-04 19:45:04 -0500271extern struct floppy_ext_dbt_s diskette_param_table2;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500272void floppy_setup(void);
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500273struct drive_s *init_floppy(int floppyid, int ftype);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400274int find_floppy_type(u32 size);
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400275int process_floppy_op(struct disk_op_s *op);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500276void floppy_tick(void);
Kevin O'Connor786502d2008-02-27 10:41:41 -0500277
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500278// cdrom.c
Kevin O'Connord3140832012-05-13 22:46:12 -0400279extern u8 CDRom_locks[];
280extern struct cdemu_s CDEmu;
Kevin O'Connor8f469b92010-02-28 01:28:11 -0500281extern struct drive_s *cdemu_drive_gf;
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400282int process_cdemu_op(struct disk_op_s *op);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500283void cdemu_setup(void);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500284void cdemu_134b(struct bregs *regs);
Gleb Natapov4c90a202010-12-07 13:50:54 +0200285int cdrom_boot(struct drive_s *drive_g);
Kevin O'Connor31d8c8a2008-03-04 19:56:41 -0500286
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400287// ramdisk.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500288void ramdisk_setup(void);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400289int process_ramdisk_op(struct disk_op_s *op);
290
Kevin O'Connor786502d2008-02-27 10:41:41 -0500291#endif // disk.h