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