b106bb0cd675e39dbac4bea345ef04fb66206ea4
[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 GPLv3 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  * Bios Data Area (BDA)
16  ****************************************************************/
17
18 struct ivec {
19     u16 offset;
20     u16 seg;
21 };
22
23 struct bios_data_area_s {
24     // 00:00
25     struct ivec ivecs[256];
26     // 30:00
27 //    u8 stack[256];
28     // 40:00
29     u16 port_com[4];
30     u16 port_lpt[3];
31     u16 ebda_seg;
32     // 40:10
33     u16 equipment_list_flags;
34     u8 pad1;
35     u16 mem_size_kb;
36     u8 pad2;
37     u8 ps2_ctrl_flag;
38     u8 kbd_flag0;
39     u8 kbd_flag1;
40     u8 alt_keypad;
41     u16 kbd_buf_head;
42     u16 kbd_buf_tail;
43     // 40:1e
44     u8 kbd_buf[32];
45     u8 floppy_recalibration_status;
46     u8 floppy_motor_status;
47     // 40:40
48     u8 floppy_motor_counter;
49     u8 floppy_last_status;
50     u8 floppy_return_status[7];
51     u8 video_mode;
52     u16 video_cols;
53     u16 video_pagesize;
54     u16 video_pagestart;
55     // 40:50
56     u16 cursor_pos[8];
57     // 40:60
58     u16 cursor_type;
59     u8 video_page;
60     u16 crtc_address;
61     u8 video_msr;
62     u8 video_pal;
63     u16 jump_ip;
64     u16 jump_cs;
65     u8 other_6b;
66     u32 timer_counter;
67     // 40:70
68     u8 timer_rollover;
69     u8 break_flag;
70     u16 soft_reset_flag;
71     u8 disk_last_status;
72     u8 disk_count;
73     u8 disk_control_byte;
74     u8 port_disk;
75     u8 lpt_timeout[4];
76     u8 com_timeout[4];
77     // 40:80
78     u16 kbd_buf_start_offset;
79     u16 kbd_buf_end_offset;
80     u8 video_rows;
81     u16 char_height;
82     u8 video_ctl;
83     u8 video_switches;
84     u8 modeset_ctl;
85     u8 dcc_index;
86     u8 floppy_last_data_rate;
87     u8 disk_status_controller;
88     u8 disk_error_controller;
89     u8 disk_interrupt_flag;
90     u8 floppy_harddisk_info;
91     // 40:90
92     u8 floppy_media_state[4];
93     u8 floppy_track0;
94     u8 floppy_track1;
95     u8 kbd_mode;
96     u8 kbd_led;
97     u32 ptr_user_wait_complete_flag;
98     u32 user_wait_timeout;
99     // 40:A0
100     u8 rtc_wait_flag;
101 } PACKED;
102
103 // BDA floppy_recalibration_status bitdefs
104 #define FRS_TIMEOUT (1<<7)
105
106 // BDA rtc_wait_flag bitdefs
107 #define RWS_WAIT_PENDING (1<<0)
108 #define RWS_WAIT_ELAPSED (1<<7)
109
110 // BDA floppy_media_state bitdefs
111 #define FMS_DRIVE_STATE_MASK        (0x07)
112 #define FMS_MEDIA_DRIVE_ESTABLISHED (1<<4)
113 #define FMS_DOUBLE_STEPPING         (1<<5)
114 #define FMS_DATA_RATE_MASK          (0xc0)
115
116 // Accessor functions
117 #define GET_BDA(var) \
118     GET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var)
119 #define SET_BDA(var, val) \
120     SET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var, (val))
121 #define CLEARBITS_BDA(var, val) do {                                    \
122         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
123         SET_BDA(var, (__val & ~(val)));                                 \
124     } while (0)
125 #define SETBITS_BDA(var, val) do {                                      \
126         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
127         SET_BDA(var, (__val | (val)));                                  \
128     } while (0)
129
130
131 /****************************************************************
132  * Hard drive info
133  ****************************************************************/
134
135 struct chs_s {
136     u16 heads;      // # heads
137     u16 cylinders;  // # cylinders
138     u16 spt;        // # sectors / track
139 };
140
141 // DPTE definition
142 struct dpte_s {
143     u16 iobase1;
144     u16 iobase2;
145     u8  prefix;
146     u8  unused;
147     u8  irq;
148     u8  blkcount;
149     u8  dma;
150     u8  pio;
151     u16 options;
152     u16 reserved;
153     u8  revision;
154     u8  checksum;
155 };
156
157 struct ata_channel_s {
158     u16 iobase1;      // IO Base 1
159     u16 iobase2;      // IO Base 2
160     u16 pci_bdf;
161     u8  irq;          // IRQ
162 };
163
164 struct ata_device_s {
165     u8  type;         // Detected type of ata (ata/atapi/none/unknown)
166     u8  device;       // Detected type of attached devices (hd/cd/none)
167     u8  removable;    // Removable device flag
168     u8  lock;         // Locks for removable devices
169     u8  mode;         // transfer mode : PIO 16/32 bits - IRQ - ISADMA - PCIDMA
170     u16 blksize;      // block size
171
172     u8  translation;  // type of translation
173     struct chs_s  lchs;         // Logical CHS
174     struct chs_s  pchs;         // Physical CHS
175
176     u64 sectors;      // Total sectors count
177 };
178
179 struct ata_s {
180     // ATA channels info
181     struct ata_channel_s channels[CONFIG_MAX_ATA_INTERFACES];
182
183     // ATA devices info
184     struct ata_device_s  devices[CONFIG_MAX_ATA_DEVICES];
185     //
186     // map between bios hd/cd id and ata channels
187     u8 hdcount, cdcount;
188     u8 idmap[2][CONFIG_MAX_ATA_DEVICES];
189
190     // Buffer for DPTE table
191     struct dpte_s dpte;
192
193     // Count of transferred sectors and bytes
194     u16 trsfsectors;
195 };
196
197 // ElTorito Device Emulation data
198 struct cdemu_s {
199     u8  active;
200     u8  media;
201     u8  emulated_drive;
202     u8  controller_index;
203     u16 device_spec;
204     u32 ilba;
205     u16 buffer_segment;
206     u16 load_segment;
207     u16 sector_count;
208
209     // Virtual device
210     struct chs_s  vdevice;
211 };
212
213
214 /****************************************************************
215  * Initial Program Load (IPL)
216  ****************************************************************/
217
218 struct ipl_entry_s {
219     u16 type;
220     u16 flags;
221     u32 vector;
222     char *description;
223 };
224
225 struct ipl_s {
226     struct ipl_entry_s table[8];
227     u16 count;
228     u16 sequence;
229     u32 bootorder;
230     u8 checkfloppysig;
231 };
232
233 #define IPL_TYPE_FLOPPY      0x01
234 #define IPL_TYPE_HARDDISK    0x02
235 #define IPL_TYPE_CDROM       0x03
236 #define IPL_TYPE_BEV         0x80
237
238
239 /****************************************************************
240  * Extended Bios Data Area (EBDA)
241  ****************************************************************/
242
243 struct fdpt_s {
244     u16 cylinders;
245     u8 heads;
246     u8 a0h_signature;
247     u8 phys_sectors;
248     u16 precompensation;
249     u8 reserved;
250     u8 drive_control_byte;
251     u16 phys_cylinders;
252     u8 phys_heads;
253     u16 landing_zone;
254     u8 sectors;
255     u8 checksum;
256 } PACKED;
257
258 struct extended_bios_data_area_s {
259     u8 size;
260     u8 reserved1[0x21];
261     u32 far_call_pointer;
262     u8 mouse_flag1;
263     u8 mouse_flag2;
264     u8 mouse_data[0x08];
265     // 0x30
266     u8 other1[0x0d];
267
268     // 0x3d
269     struct fdpt_s fdpt[2];
270
271     // 0x5d
272     u8 other2[0xC4];
273
274     // 0x121 - Begin custom storage.
275     u8 ps2ctr;
276
277     // ATA Driver data
278     struct ata_s ata;
279
280     // El Torito Emulation data
281     struct cdemu_s cdemu;
282
283     // Initial program load
284     struct ipl_s ipl;
285
286     // Resume stack
287     u8 resume_stack[128] __aligned(8);
288 } PACKED;
289
290 // Accessor functions
291 #define GET_EBDA(var) \
292     GET_FARVAR(GET_BDA(ebda_seg), ((struct extended_bios_data_area_s *)0)->var)
293 #define SET_EBDA(var, val) \
294     SET_FARVAR(GET_BDA(ebda_seg), ((struct extended_bios_data_area_s *)0)->var, (val))
295 static inline struct extended_bios_data_area_s *
296 get_ebda_ptr()
297 {
298     extern void *__force_link_error__get_ebda_ptr_only_in_32bit();
299     if (MODE16)
300         return __force_link_error__get_ebda_ptr_only_in_32bit();
301     return (void*)MAKE_FARPTR(GET_BDA(ebda_seg), 0);
302 }
303
304
305 /****************************************************************
306  * Global variables
307  ****************************************************************/
308
309 #define GET_GLOBAL(var) \
310     GET_VAR(CS, (var))
311 #if MODE16
312 extern void __force_link_error__set_global_only_in_32bit();
313 #define SET_GLOBAL(var, val)                            \
314     __force_link_error__set_global_only_in_32bit()
315 #else
316 #define SET_GLOBAL(var, val)                    \
317     do { (var) = (val); } while (0)
318 #endif
319
320
321 /****************************************************************
322  * Bios Config Table
323  ****************************************************************/
324
325 struct bios_config_table_s {
326     u16 size;
327     u8 model;
328     u8 submodel;
329     u8 biosrev;
330     u8 feature1, feature2, feature3, feature4, feature5;
331 } PACKED;
332
333 extern struct bios_config_table_s BIOS_CONFIG_TABLE;
334
335 #endif // __BIOSVAR_H