Add support for using floppy images in CBFS.
[seabios.git] / src / disk.h
1 // Definitions for X86 bios disks.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __DISK_H
7 #define __DISK_H
8
9 #include "types.h" // u8
10 #include "config.h" // CONFIG_*
11
12 #define DISK_RET_SUCCESS       0x00
13 #define DISK_RET_EPARAM        0x01
14 #define DISK_RET_EADDRNOTFOUND 0x02
15 #define DISK_RET_EWRITEPROTECT 0x03
16 #define DISK_RET_ECHANGED      0x06
17 #define DISK_RET_EBOUNDARY     0x09
18 #define DISK_RET_EBADTRACK     0x0c
19 #define DISK_RET_ECONTROLLER   0x20
20 #define DISK_RET_ETIMEOUT      0x80
21 #define DISK_RET_ENOTLOCKED    0xb0
22 #define DISK_RET_ELOCKED       0xb1
23 #define DISK_RET_ENOTREMOVABLE 0xb2
24 #define DISK_RET_ETOOMANYLOCKS 0xb4
25 #define DISK_RET_EMEDIA        0xC0
26 #define DISK_RET_ENOTREADY     0xAA
27
28
29 /****************************************************************
30  * Interface structs
31  ****************************************************************/
32
33 // Bios disk structures.
34 struct int13ext_s {
35     u8  size;
36     u8  reserved;
37     u16 count;
38     u16 offset;
39     u16 segment;
40     u64 lba;
41 } PACKED;
42
43 #define GET_INT13EXT(regs,var)                                          \
44     GET_FARVAR((regs)->ds, ((struct int13ext_s*)((regs)->si+0))->var)
45 #define SET_INT13EXT(regs,var,val)                                      \
46     SET_FARVAR((regs)->ds, ((struct int13ext_s*)((regs)->si+0))->var, (val))
47
48 // Disk Physical Table definition
49 struct int13dpt_s {
50     u16 size;
51     u16 infos;
52     u32 cylinders;
53     u32 heads;
54     u32 spt;
55     u64 sector_count;
56     u16 blksize;
57     u16 dpte_offset;
58     u16 dpte_segment;
59     u16 key;
60     u8  dpi_length;
61     u8  reserved1;
62     u16 reserved2;
63     u8  host_bus[4];
64     u8  iface_type[8];
65     u64 iface_path;
66     u64 device_path;
67     u8  reserved3;
68     u8  checksum;
69 } PACKED;
70
71 #define GET_INT13DPT(regs,var)                                          \
72     GET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var)
73 #define SET_INT13DPT(regs,var,val)                                      \
74     SET_FARVAR((regs)->ds, ((struct int13dpt_s*)((regs)->si+0))->var, (val))
75
76 // Floppy "Disk Base Table"
77 struct floppy_dbt_s {
78     u8 specify1;
79     u8 specify2;
80     u8 shutoff_ticks;
81     u8 bps_code;
82     u8 sectors;
83     u8 interblock_len;
84     u8 data_len;
85     u8 gap_len;
86     u8 fill_byte;
87     u8 settle_time;
88     u8 startup_time;
89 } PACKED;
90
91 struct floppy_ext_dbt_s {
92     struct floppy_dbt_s dbt;
93     // Extra fields
94     u8 max_track;
95     u8 data_rate;
96     u8 drive_type;
97 } PACKED;
98
99 // Helper function for setting up a return code.
100 struct bregs;
101 void __disk_ret(struct bregs *regs, u32 linecode, const char *fname);
102 #define disk_ret(regs, code) \
103     __disk_ret((regs), (code) | (__LINE__ << 8), __func__)
104
105
106 /****************************************************************
107  * Master boot record
108  ****************************************************************/
109
110 struct packed_chs_s {
111     u8 heads;
112     u8 sptcyl;
113     u8 cyllow;
114 };
115
116 struct partition_s {
117     u8 status;
118     struct packed_chs_s first;
119     u8 type;
120     struct packed_chs_s last;
121     u32 lba;
122     u32 count;
123 } PACKED;
124
125 struct mbr_s {
126     u8 code[440];
127     // 0x01b8
128     u32 diskseg;
129     // 0x01bc
130     u16 null;
131     // 0x01be
132     struct partition_s partitions[4];
133     // 0x01fe
134     u16 signature;
135 } PACKED;
136
137 #define MBR_SIGNATURE 0xaa55
138
139
140 /****************************************************************
141  * Disk command request
142  ****************************************************************/
143
144 struct disk_op_s {
145     u64 lba;
146     void *buf_fl;
147     u16 count;
148     u8 driveid;
149     u8 command;
150 };
151
152 #define CMD_RESET   0x00
153 #define CMD_READ    0x02
154 #define CMD_WRITE   0x03
155 #define CMD_VERIFY  0x04
156 #define CMD_FORMAT  0x05
157 #define CMD_SEEK    0x07
158 #define CMD_ISREADY 0x10
159
160
161 /****************************************************************
162  * Global storage
163  ****************************************************************/
164
165 struct chs_s {
166     u16 heads;      // # heads
167     u16 cylinders;  // # cylinders
168     u16 spt;        // # sectors / track
169 };
170
171 struct drive_s {
172     u8  type;         // Detected type of drive (ata/atapi/none)
173     u8  removable;    // Removable device flag
174     u16 blksize;      // block size
175     int cntl_id;
176     u8  floppy_type;  // Type of floppy (only for floppy drives).
177
178     char model[41];
179
180     u8  translation;  // type of translation
181     struct chs_s  lchs;         // Logical CHS
182     struct chs_s  pchs;         // Physical CHS
183
184     u64 sectors;      // Total sectors count
185 };
186
187 #define DTYPE_NONE     0x00
188 #define DTYPE_FLOPPY   0x01
189 #define DTYPE_ATA      0x02
190 #define DTYPE_ATAPI    0x03
191 #define DTYPE_RAMDISK  0x04
192
193 #define TRANSLATION_NONE  0
194 #define TRANSLATION_LBA   1
195 #define TRANSLATION_LARGE 2
196 #define TRANSLATION_RECHS 3
197
198 struct drives_s {
199     // info on each internally handled drive
200     struct drive_s drives[CONFIG_MAX_DRIVES];
201     u8 drivecount;
202     //
203     // map between bios floppy/hd/cd id and driveid index into drives[]
204     u8 floppycount;
205     u8 cdcount;
206     u8 idmap[3][CONFIG_MAX_EXTDRIVE];
207 };
208
209 #define EXTTYPE_FLOPPY 0
210 #define EXTTYPE_HD 1
211 #define EXTTYPE_CD 2
212
213
214 /****************************************************************
215  * Function defs
216  ****************************************************************/
217
218 // block.c
219 extern struct drives_s Drives;
220 void setup_translation(int driveid);
221 void map_floppy_drive(int driveid);
222 void map_hd_drive(int driveid);
223 void map_cd_drive(int driveid);
224 int send_disk_op(struct disk_op_s *op);
225 void drive_setup();
226
227 // floppy.c
228 extern struct floppy_ext_dbt_s diskette_param_table2;
229 void floppy_setup();
230 void addFloppy(int floppyid, int ftype, int driver);
231 int find_floppy_type(u32 size);
232 int process_floppy_op(struct disk_op_s *op);
233 void floppy_tick();
234
235 // disk.c
236 void disk_13(struct bregs *regs, u8 driveid);
237 void disk_13XX(struct bregs *regs, u8 driveid);
238 void cdemu_access(struct bregs *regs, u8 driveid, u16 command);
239
240 // cdrom.c
241 void cdrom_13(struct bregs *regs, u8 driveid);
242 void cdemu_13(struct bregs *regs);
243 void cdemu_134b(struct bregs *regs);
244 int cdrom_boot(int cdid);
245
246 // ramdisk.c
247 void ramdisk_setup();
248 int process_ramdisk_op(struct disk_op_s *op);
249
250 #endif // disk.h