Generate PIR table at post time.
[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" // SET_SEG
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 other_49[0x7];
52     // 40:50
53     u8 other_50[0x10];
54     // 40:60
55     u8 other_60[0x7];
56     u32 jump_cs_ip;
57     u8 dummy;
58     u32 timer_counter;
59     // 40:70
60     u8 timer_rollover;
61     u8 break_flag;
62     u16 soft_reset_flag;
63     u8 disk_last_status;
64     u8 disk_count;
65     u8 disk_control_byte;
66     u8 port_disk;
67     u8 lpt_timeout[4];
68     u8 com_timeout[4];
69     // 40:80
70     u16 kbd_buf_start_offset;
71     u16 kbd_buf_end_offset;
72     u8 other_84[7];
73     u8 floppy_last_data_rate;
74     u8 disk_status_controller;
75     u8 disk_error_controller;
76     u8 disk_interrupt_flag;
77     u8 floppy_harddisk_info;
78     // 40:90
79     u8 floppy_media_state[4];
80     u8 floppy_track0;
81     u8 floppy_track1;
82     u8 kbd_mode;
83     u8 kbd_led;
84     u32 ptr_user_wait_complete_flag;
85     u32 user_wait_timeout;
86     // 40:A0
87     u8 rtc_wait_flag;
88 } PACKED;
89
90 // BDA floppy_recalibration_status bitdefs
91 #define FRS_TIMEOUT (1<<7)
92
93 // BDA rtc_wait_flag bitdefs
94 #define RWS_WAIT_PENDING (1<<0)
95 #define RWS_WAIT_ELAPSED (1<<7)
96
97 // BDA floppy_media_state bitdefs
98 #define FMS_DRIVE_STATE_MASK        (0x07)
99 #define FMS_MEDIA_DRIVE_ESTABLISHED (1<<4)
100 #define FMS_DOUBLE_STEPPING         (1<<5)
101 #define FMS_DATA_RATE_MASK          (0xc0)
102
103 // Accessor functions
104 #define GET_BDA(var) \
105     GET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var)
106 #define SET_BDA(var, val) \
107     SET_FARVAR(SEG_BDA, ((struct bios_data_area_s *)0)->var, (val))
108 #define CLEARBITS_BDA(var, val) do {                                    \
109         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
110         SET_BDA(var, (__val & ~(val)));                                 \
111     } while (0)
112 #define SETBITS_BDA(var, val) do {                                      \
113         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
114         SET_BDA(var, (__val | (val)));                                  \
115     } while (0)
116
117
118 /****************************************************************
119  * Hard drive info
120  ****************************************************************/
121
122 struct chs_s {
123     u16 heads;      // # heads
124     u16 cylinders;  // # cylinders
125     u16 spt;        // # sectors / track
126 };
127
128 // DPTE definition
129 struct dpte_s {
130     u16 iobase1;
131     u16 iobase2;
132     u8  prefix;
133     u8  unused;
134     u8  irq;
135     u8  blkcount;
136     u8  dma;
137     u8  pio;
138     u16 options;
139     u16 reserved;
140     u8  revision;
141     u8  checksum;
142 };
143
144 struct ata_channel_s {
145     u8  iface;        // ISA or PCI
146     u16 iobase1;      // IO Base 1
147     u16 iobase2;      // IO Base 2
148     u8  irq;          // IRQ
149 };
150
151 struct ata_device_s {
152     u8  type;         // Detected type of ata (ata/atapi/none/unknown)
153     u8  device;       // Detected type of attached devices (hd/cd/none)
154     u8  removable;    // Removable device flag
155     u8  lock;         // Locks for removable devices
156     u8  mode;         // transfer mode : PIO 16/32 bits - IRQ - ISADMA - PCIDMA
157     u16 blksize;      // block size
158
159     u8  translation;  // type of translation
160     struct chs_s  lchs;         // Logical CHS
161     struct chs_s  pchs;         // Physical CHS
162
163     u64 sectors;      // Total sectors count
164 };
165
166 struct ata_s {
167     // ATA channels info
168     struct ata_channel_s channels[CONFIG_MAX_ATA_INTERFACES];
169
170     // ATA devices info
171     struct ata_device_s  devices[CONFIG_MAX_ATA_DEVICES];
172     //
173     // map between bios hd/cd id and ata channels
174     u8 hdcount, cdcount;
175     u8 idmap[2][CONFIG_MAX_ATA_DEVICES];
176
177     // Buffer for DPTE table
178     struct dpte_s dpte;
179
180     // Count of transferred sectors and bytes
181     u16 trsfsectors;
182 };
183
184 // ElTorito Device Emulation data
185 struct cdemu_s {
186     u8  active;
187     u8  media;
188     u8  emulated_drive;
189     u8  controller_index;
190     u16 device_spec;
191     u32 ilba;
192     u16 buffer_segment;
193     u16 load_segment;
194     u16 sector_count;
195
196     // Virtual device
197     struct chs_s  vdevice;
198 };
199
200
201 /****************************************************************
202  * Initial Program Load (IPL)
203  ****************************************************************/
204
205 struct ipl_entry_s {
206     u16 type;
207     u16 flags;
208     u32 vector;
209     u32 description;
210 };
211
212 struct ipl_s {
213     struct ipl_entry_s table[8];
214     u16 count;
215     u16 sequence;
216     u32 bootorder;
217     u8 checkfloppysig;
218 };
219
220 #define IPL_TYPE_FLOPPY      0x01
221 #define IPL_TYPE_HARDDISK    0x02
222 #define IPL_TYPE_CDROM       0x03
223 #define IPL_TYPE_BEV         0x80
224
225
226 /****************************************************************
227  * Extended Bios Data Area (EBDA)
228  ****************************************************************/
229
230 struct fdpt_s {
231     u16 cylinders;
232     u8 heads;
233     u8 a0h_signature;
234     u8 phys_sectors;
235     u16 precompensation;
236     u8 reserved;
237     u8 drive_control_byte;
238     u16 phys_cylinders;
239     u8 phys_heads;
240     u16 landing_zone;
241     u8 sectors;
242     u8 checksum;
243 } PACKED;
244
245 struct extended_bios_data_area_s {
246     u8 size;
247     u8 reserved1[0x21];
248     u32 far_call_pointer;
249     u8 mouse_flag1;
250     u8 mouse_flag2;
251     u8 mouse_data[0x08];
252     // 0x30
253     u8 other1[0x0d];
254
255     // 0x3d
256     struct fdpt_s fdpt[2];
257
258     // 0x5d
259     u8 other2[0xC4];
260
261     // Physical memory available.
262     u32 ram_size;
263     u16 e820_count;
264     u32 e820_loc;
265     u32 pir_loc;
266
267     // ATA Driver data
268     struct ata_s   ata;
269
270     // El Torito Emulation data
271     struct cdemu_s cdemu;
272
273     // Initial program load
274     struct ipl_s ipl;
275 } PACKED;
276
277 // Accessor functions
278 #define GET_EBDA(var) \
279     GET_FARVAR(SEG_EBDA, ((struct extended_bios_data_area_s *)0)->var)
280 #define SET_EBDA(var, val) \
281     SET_FARVAR(SEG_EBDA, ((struct extended_bios_data_area_s *)0)->var, (val))
282
283
284 /****************************************************************
285  * Registers saved/restored in romlayout.S
286  ****************************************************************/
287
288 #define UREG(ER, R, RH, RL) union { u32 ER; struct { u16 R; u16 R ## _hi; }; struct { u8 RL; u8 RH; u8 R ## _hilo; u8 R ## _hihi; }; }
289
290 // Layout of registers passed in to irq handlers.  Note that this
291 // layout corresponds to code in romlayout.S - don't change it here
292 // without also updating the assembler code.
293 struct bregs {
294     u16 ds;
295     u16 es;
296     UREG(edi, di, di_hi, di_lo);
297     UREG(esi, si, si_hi, si_lo);
298     UREG(ebx, bx, bh, bl);
299     UREG(edx, dx, dh, dl);
300     UREG(ecx, cx, ch, cl);
301     UREG(eax, ax, ah, al);
302     u16 ip;
303     u16 cs;
304     u16 flags;
305 } PACKED;
306
307 // bregs flags bitdefs
308 #define F_ZF (1<<6)
309 #define F_CF (1<<0)
310
311 static inline void
312 set_cf(struct bregs *regs, int cond)
313 {
314     if (cond)
315         regs->flags |= F_CF;
316     else
317         regs->flags &= ~F_CF;
318 }
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
336 /****************************************************************
337  * Memory layout info
338  ****************************************************************/
339
340 #define SEG_BIOS     0xf000
341 #define SEG_EBDA     0x9fc0
342 #define SEG_BDA      0x0000
343
344 #define EBDA_SIZE DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024)
345 #define BASE_MEM_IN_K (640 - EBDA_SIZE)
346
347 #endif // __BIOSVAR_H