Implement cdrom disk emulation at high-level instead of in low-level ATA.
[seabios.git] / src / biosvar.h
1 // Variable layouts of bios.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __BIOSVAR_H
7 #define __BIOSVAR_H
8
9 #include "types.h" // u8
10 #include "farptr.h" // GET_FARVAR
11 #include "config.h" // CONFIG_*
12 #include "disk.h" // struct chs_s
13
14 struct segoff_s {
15     union {
16         struct {
17             u16 offset;
18             u16 seg;
19         };
20         u32 segoff;
21     };
22 };
23 #define SEGOFF(s,o) ({struct segoff_s __so; __so.offset=(o); __so.seg=(s); __so;})
24
25
26 /****************************************************************
27  * Interupt vector table
28  ****************************************************************/
29
30 struct rmode_IVT {
31     struct segoff_s ivec[256];
32 };
33
34 #define GET_IVT(vector)                                         \
35     GET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector])
36 #define SET_IVT(vector, seg, off)                                       \
37     SET_FARVAR(SEG_IVT, ((struct rmode_IVT *)0)->ivec[vector].segoff, ((seg) << 16) | (off))
38
39
40 /****************************************************************
41  * Bios Data Area (BDA)
42  ****************************************************************/
43
44 struct bios_data_area_s {
45     // 40:00
46     u16 port_com[4];
47     u16 port_lpt[3];
48     u16 ebda_seg;
49     // 40:10
50     u16 equipment_list_flags;
51     u8 pad1;
52     u16 mem_size_kb;
53     u8 pad2;
54     u8 ps2_ctrl_flag;
55     u8 kbd_flag0;
56     u8 kbd_flag1;
57     u8 alt_keypad;
58     u16 kbd_buf_head;
59     u16 kbd_buf_tail;
60     // 40:1e
61     u8 kbd_buf[32];
62     u8 floppy_recalibration_status;
63     u8 floppy_motor_status;
64     // 40:40
65     u8 floppy_motor_counter;
66     u8 floppy_last_status;
67     u8 floppy_return_status[7];
68     u8 video_mode;
69     u16 video_cols;
70     u16 video_pagesize;
71     u16 video_pagestart;
72     // 40:50
73     u16 cursor_pos[8];
74     // 40:60
75     u16 cursor_type;
76     u8 video_page;
77     u16 crtc_address;
78     u8 video_msr;
79     u8 video_pal;
80     u16 jump_ip;
81     u16 jump_cs;
82     u8 other_6b;
83     u32 timer_counter;
84     // 40:70
85     u8 timer_rollover;
86     u8 break_flag;
87     u16 soft_reset_flag;
88     u8 disk_last_status;
89     u8 hdcount;
90     u8 disk_control_byte;
91     u8 port_disk;
92     u8 lpt_timeout[4];
93     u8 com_timeout[4];
94     // 40:80
95     u16 kbd_buf_start_offset;
96     u16 kbd_buf_end_offset;
97     u8 video_rows;
98     u16 char_height;
99     u8 video_ctl;
100     u8 video_switches;
101     u8 modeset_ctl;
102     u8 dcc_index;
103     u8 floppy_last_data_rate;
104     u8 disk_status_controller;
105     u8 disk_error_controller;
106     u8 disk_interrupt_flag;
107     u8 floppy_harddisk_info;
108     // 40:90
109     u8 floppy_media_state[4];
110     u8 floppy_track[2];
111     u8 kbd_mode;
112     u8 kbd_led;
113     u32 ptr_user_wait_complete_flag;
114     u32 user_wait_timeout;
115     // 40:A0
116     u8 rtc_wait_flag;
117     u8 other_a1[7];
118     u16 video_savetable_ptr;
119     u16 video_savetable_seg;
120     u8 other_ac[4];
121     // 40:B0
122     u8 other_b0[10];
123     u16 vbe_mode;
124 } PACKED;
125
126 // BDA floppy_recalibration_status bitdefs
127 #define FRS_TIMEOUT (1<<7)
128
129 // BDA rtc_wait_flag bitdefs
130 #define RWS_WAIT_PENDING (1<<0)
131 #define RWS_WAIT_ELAPSED (1<<7)
132
133 // BDA floppy_media_state bitdefs
134 #define FMS_DRIVE_STATE_MASK        (0x07)
135 #define FMS_MEDIA_DRIVE_ESTABLISHED (1<<4)
136 #define FMS_DOUBLE_STEPPING         (1<<5)
137 #define FMS_DATA_RATE_MASK          (0xc0)
138
139 // Accessor functions
140 #define GET_BDA(var) \
141     GET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var)
142 #define SET_BDA(var, val) \
143     SET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var, (val))
144 #define CLEARBITS_BDA(var, val) do {                                    \
145         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
146         SET_BDA(var, (__val & ~(val)));                                 \
147     } while (0)
148 #define SETBITS_BDA(var, val) do {                                      \
149         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
150         SET_BDA(var, (__val | (val)));                                  \
151     } while (0)
152
153
154 /****************************************************************
155  * Extended Bios Data Area (EBDA)
156  ****************************************************************/
157
158 // DPTE definition
159 struct dpte_s {
160     u16 iobase1;
161     u16 iobase2;
162     u8  prefix;
163     u8  unused;
164     u8  irq;
165     u8  blkcount;
166     u8  dma;
167     u8  pio;
168     u16 options;
169     u16 reserved;
170     u8  revision;
171     u8  checksum;
172 };
173
174 // ElTorito Device Emulation data
175 struct cdemu_s {
176     u8  active;
177     u8  media;
178     u8  emulated_drive;
179     u8  controller_index;
180     u16 device_spec;
181     u32 ilba;
182     u16 buffer_segment;
183     u16 load_segment;
184     u16 sector_count;
185
186     // Virtual device
187     struct chs_s lchs;
188 };
189
190 struct fdpt_s {
191     u16 cylinders;
192     u8 heads;
193     u8 a0h_signature;
194     u8 phys_sectors;
195     u16 precompensation;
196     u8 reserved;
197     u8 drive_control_byte;
198     u16 phys_cylinders;
199     u8 phys_heads;
200     u16 landing_zone;
201     u8 sectors;
202     u8 checksum;
203 } PACKED;
204
205 struct extended_bios_data_area_s {
206     u8 size;
207     u8 reserved1[0x21];
208     u32 far_call_pointer;
209     u8 mouse_flag1;
210     u8 mouse_flag2;
211     u8 mouse_data[0x08];
212     // 0x30
213     u8 other1[0x0d];
214
215     // 0x3d
216     struct fdpt_s fdpt[2];
217
218     // 0x5d
219     u8 other2[0xC4];
220
221     // 0x121 - Begin custom storage.
222     u8 ps2ctr;
223
224     // El Torito Emulation data
225     struct cdemu_s cdemu;
226
227     // Count of transferred sectors and bytes to/from disk
228     u16 sector_count;
229
230     // Buffer for disk DPTE table
231     struct dpte_s dpte;
232
233     // Locks for removable devices
234     u8 cdrom_locks[CONFIG_MAX_ATA_DEVICES];
235
236     u16 boot_sequence;
237
238     // Stack space available for code that needs it.
239     u8 extra_stack[512] __aligned(8);
240
241     u8 cdemu_buf[2048 * !!CONFIG_CDROM_EMU];
242 } PACKED;
243
244 // The initial size and location of EBDA
245 #define EBDA_SIZE_START \
246     DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024)
247 #define EBDA_SEGMENT_START \
248     FLATPTR_TO_SEG((640 - EBDA_SIZE_START) * 1024)
249 #define EBDA_SEGMENT_MINIMUM \
250     FLATPTR_TO_SEG((640 - 256) * 1024)
251
252 // Accessor functions
253 static inline u16 get_ebda_seg() {
254     return GET_BDA(ebda_seg);
255 }
256 static inline struct extended_bios_data_area_s *
257 get_ebda_ptr()
258 {
259     ASSERT32();
260     return MAKE_FLATPTR(get_ebda_seg(), 0);
261 }
262 #define GET_EBDA2(eseg, var)                                            \
263     GET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var)
264 #define SET_EBDA2(eseg, var, val)                                       \
265     SET_FARVAR(eseg, ((struct extended_bios_data_area_s *)0)->var, (val))
266 #define GET_EBDA(var)                           \
267     GET_EBDA2(get_ebda_seg(), var)
268 #define SET_EBDA(var, val)                      \
269     SET_EBDA2(get_ebda_seg(), var, (val))
270
271 #define EBDA_OFFSET_TOP_STACK                                   \
272     offsetof(struct extended_bios_data_area_s, extra_stack[     \
273                  FIELD_SIZEOF(struct extended_bios_data_area_s  \
274                               , extra_stack)])
275
276
277 /****************************************************************
278  * Global variables
279  ****************************************************************/
280
281 #define GLOBAL_SEGREG CS
282 static inline u16 get_global_seg() {
283     return GET_SEG(GLOBAL_SEGREG);
284 }
285 #define GET_GLOBAL(var)                         \
286     GET_VAR(GLOBAL_SEGREG, (var))
287 #define SET_GLOBAL(var, val) do {                                       \
288         ASSERT32();                                                     \
289         (var) = (val);                                                  \
290     } while (0)
291
292
293 /****************************************************************
294  * Bios Config Table
295  ****************************************************************/
296
297 struct bios_config_table_s {
298     u16 size;
299     u8 model;
300     u8 submodel;
301     u8 biosrev;
302     u8 feature1, feature2, feature3, feature4, feature5;
303 } PACKED;
304
305 extern struct bios_config_table_s BIOS_CONFIG_TABLE __aligned(1);
306
307 #endif // __BIOSVAR_H