Support a relocatable ebda segment.
[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 pir_header;
259
260 struct extended_bios_data_area_s {
261     u8 size;
262     u8 reserved1[0x21];
263     u32 far_call_pointer;
264     u8 mouse_flag1;
265     u8 mouse_flag2;
266     u8 mouse_data[0x08];
267     // 0x30
268     u8 other1[0x0d];
269
270     // 0x3d
271     struct fdpt_s fdpt[2];
272
273     // 0x5d
274     u8 other2[0xC4];
275
276     // 0x121 - Begin custom storage.
277     u8 ps2ctr;
278
279     // Physical memory available.
280     u32 ram_size;        // Amount of continuous ram under 4Gig
281     u64 ram_size_over4G; // Amount of continuous ram >4Gig
282     struct pir_header *pir_loc;
283
284     // ATA Driver data
285     struct ata_s ata;
286
287     // El Torito Emulation data
288     struct cdemu_s cdemu;
289
290     // Initial program load
291     struct ipl_s ipl;
292
293     // Resume stack
294     u8 resume_stack[128] __aligned(8);
295 } PACKED;
296
297 // Accessor functions
298 #define GET_EBDA(var) \
299     GET_FARVAR(GET_BDA(ebda_seg), ((struct extended_bios_data_area_s *)0)->var)
300 #define SET_EBDA(var, val) \
301     SET_FARVAR(GET_BDA(ebda_seg), ((struct extended_bios_data_area_s *)0)->var, (val))
302 static inline struct extended_bios_data_area_s *
303 get_ebda_ptr()
304 {
305     extern void *__force_link_error__get_ebda_ptr_only_in_32bit();
306     if (MODE16)
307         return __force_link_error__get_ebda_ptr_only_in_32bit();
308     return (void*)MAKE_FARPTR(GET_BDA(ebda_seg), 0);
309 }
310
311
312 /****************************************************************
313  * Global variables
314  ****************************************************************/
315
316 #define GET_GLOBAL(var) \
317     GET_VAR(CS, (var))
318 #if MODE16
319 extern void __force_link_error__set_global_only_in_32bit();
320 #define SET_GLOBAL(var, val)                            \
321     __force_link_error__set_global_only_in_32bit()
322 #else
323 #define SET_GLOBAL(var, val)                    \
324     do { (var) = (val); } while (0)
325 #endif
326
327
328 /****************************************************************
329  * Bios Config Table
330  ****************************************************************/
331
332 struct bios_config_table_s {
333     u16 size;
334     u8 model;
335     u8 submodel;
336     u8 biosrev;
337     u8 feature1, feature2, feature3, feature4, feature5;
338 } PACKED;
339
340 extern struct bios_config_table_s BIOS_CONFIG_TABLE;
341
342 #endif // __BIOSVAR_H