Misc updates.
[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 #define PACKED __attribute__((packed))
14
15
16 /****************************************************************
17  * Bios Data Area (BDA)
18  ****************************************************************/
19
20 struct ivec {
21     u16 offset;
22     u16 seg;
23 };
24
25 struct bios_data_area_s {
26     // 00:00
27     struct ivec ivecs[256];
28     // 30:00
29 //    u8 stack[256];
30     // 40:00
31     u16 port_com[4];
32     u16 port_lpt[3];
33     u16 ebda_seg;
34     // 40:10
35     u16 equipment_list_flags;
36     u8 pad1;
37     u16 mem_size_kb;
38     u8 pad2;
39     u8 ps2_ctrl_flag;
40     u8 kbd_flag0;
41     u8 kbd_flag1;
42     u8 alt_keypad;
43     u16 kbd_buf_head;
44     u16 kbd_buf_tail;
45     // 40:1e
46     u8 kbd_buf[32];
47     u8 floppy_recalibration_status;
48     u8 floppy_motor_status;
49     // 40:40
50     u8 floppy_motor_counter;
51     u8 floppy_last_status;
52     u8 floppy_return_status[7];
53     u8 other1[0x7];
54     // 40:50
55     u8 other2[0x1c];
56     // 40:6c
57     u32 timer_counter;
58     // 40:70
59     u8 timer_rollover;
60     u8 other4[0x04];
61     u8 disk_count;
62     u8 disk_control_byte;
63     u8 port_disk;
64     u8 lpt_timeout[4];
65     u8 com_timeout[4];
66     // 40:80
67     u16 kbd_buf_start_offset;
68     u16 kbd_buf_end_offset;
69     u8 other5[7];
70     u8 floppy_last_data_rate;
71     u8 other6[3];
72     u8 floppy_harddisk_info;
73     // 40:90
74     u8 floppy_media_state[4];
75     u8 floppy_track0;
76     u8 floppy_track1;
77     u8 kbd_mode;
78     u8 kbd_led;
79     u32 ptr_user_wait_complete_flag;
80     u32 user_wait_timeout;
81     // 40:A0
82     u8 rtc_wait_flag;
83 } PACKED;
84
85 // BDA floppy_recalibration_status bitdefs
86 #define FRS_TIMEOUT (1<<7)
87
88 // BDA rtc_wait_flag bitdefs
89 #define RWS_WAIT_PENDING (1<<0)
90 #define RWS_WAIT_ELAPSED (1<<7)
91
92 // BDA floppy_media_state bitdefs
93 #define FMS_DRIVE_STATE_MASK        (0x07)
94 #define FMS_MEDIA_DRIVE_ESTABLISHED (1<<4)
95 #define FMS_DOUBLE_STEPPING         (1<<5)
96 #define FMS_DATA_RATE_MASK          (0xc0)
97
98 // Accessor functions
99 #define GET_BDA(var) \
100     GET_FARVAR(0x0000, ((struct bios_data_area_s *)0)->var)
101 #define SET_BDA(var, val) \
102     SET_FARVAR(0x0000, ((struct bios_data_area_s *)0)->var, (val))
103 #define CLEARBITS_BDA(var, val) do {                                    \
104         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
105         SET_BDA(var, (__val & ~(val)));                                 \
106     } while (0)
107 #define SETBITS_BDA(var, val) do {                                      \
108         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
109         SET_BDA(var, (__val | (val)));                                  \
110     } while (0)
111
112
113 /****************************************************************
114  * Hard drive info
115  ****************************************************************/
116
117 struct fdpt_s {
118     u16 cylinders;
119     u8 heads;
120     u8 a0h_signature;
121     u8 phys_sectors;
122     u16 precompensation;
123     u8 reserved;
124     u8 drive_control_byte;
125     u16 phys_cylinders;
126     u8 phys_heads;
127     u16 landing_zone;
128     u8 sectors;
129     u8 checksum;
130 } PACKED;
131
132 struct chs_s {
133     u16 heads;      // # heads
134     u16 cylinders;  // # cylinders
135     u16 spt;        // # sectors / track
136 };
137
138 // DPTE definition
139 struct dpte_s {
140     u16 iobase1;
141     u16 iobase2;
142     u8  prefix;
143     u8  unused;
144     u8  irq;
145     u8  blkcount;
146     u8  dma;
147     u8  pio;
148     u16 options;
149     u16 reserved;
150     u8  revision;
151     u8  checksum;
152 };
153
154 struct ata_channel_s {
155     u8  iface;        // ISA or PCI
156     u16 iobase1;      // IO Base 1
157     u16 iobase2;      // IO Base 2
158     u8  irq;          // IRQ
159 } PACKED;
160
161 struct ata_device_s {
162     u8  type;         // Detected type of ata (ata/atapi/none/unknown)
163     u8  device;       // Detected type of attached devices (hd/cd/none)
164     u8  removable;    // Removable device flag
165     u8  lock;         // Locks for removable devices
166     u8  mode;         // transfer mode : PIO 16/32 bits - IRQ - ISADMA - PCIDMA
167     u16 blksize;      // block size
168
169     u8  translation;  // type of translation
170     struct chs_s  lchs;         // Logical CHS
171     struct chs_s  pchs;         // Physical CHS
172
173     u32 sectors;      // Total sectors count
174 } PACKED;
175
176 struct ata_s {
177     // ATA channels info
178     struct ata_channel_s channels[CONFIG_MAX_ATA_INTERFACES];
179
180     // ATA devices info
181     struct ata_device_s  devices[CONFIG_MAX_ATA_DEVICES];
182     //
183     // map between (bios hd id - 0x80) and ata channels
184     u8  hdcount, hdidmap[CONFIG_MAX_ATA_DEVICES];
185
186     // map between (bios cd id - 0xE0) and ata channels
187     u8  cdcount, cdidmap[CONFIG_MAX_ATA_DEVICES];
188
189     // Buffer for DPTE table
190     struct dpte_s dpte;
191
192     // Count of transferred sectors and bytes
193     u16 trsfsectors;
194     u32 trsfbytes;
195 } PACKED;
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 } PACKED;
212
213
214 /****************************************************************
215  * Extended Bios Data Area (EBDA)
216  ****************************************************************/
217
218 struct extended_bios_data_area_s {
219     u8 size;
220     u8 other1[0x3c];
221
222     struct fdpt_s fdpt0;
223     struct fdpt_s fdpt1;
224
225     u8 other2[0xC4];
226
227     // ATA Driver data
228     struct ata_s   ata;
229
230     // El Torito Emulation data
231     struct cdemu_s cdemu;
232 };
233
234 // Accessor functions
235 #define GET_EBDA(var) \
236     GET_FARVAR(EBDA_SEG, ((struct extended_bios_data_area_s *)0)->var)
237 #define SET_EBDA(var, val) \
238     SET_FARVAR(EBDA_SEG, ((struct extended_bios_data_area_s *)0)->var, (val))
239
240
241 /****************************************************************
242  * Initial Program Load (IPL)
243  ****************************************************************/
244
245 // XXX - is this a standard, or just a bochs bios thing?
246
247 struct ipl_entry_s {
248     u16 type;
249     u16 flags;
250     u32 vector;
251     u32 description;
252     u32 reserved;
253 };
254
255 struct ipl_s {
256     struct ipl_entry_s table[8];
257     u16 count;
258     u16 sequence;
259     u8 pad[124];
260 };
261
262 #define IPL_TYPE_FLOPPY      0x01
263 #define IPL_TYPE_HARDDISK    0x02
264 #define IPL_TYPE_CDROM       0x03
265 #define IPL_TYPE_BEV         0x80
266
267 // Accessor functions
268 #define GET_IPL(var) \
269     GET_FARVAR(IPL_SEG, ((struct ipl_s *)0)->var)
270 #define SET_IPL(var, val) \
271     SET_FARVAR(IPL_SEG, ((struct ipl_s *)0)->var, (val))
272
273
274 /****************************************************************
275  * Registers saved/restored in romlayout.S
276  ****************************************************************/
277
278 #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; }; }
279
280 struct bregs {
281     u16 ds;
282     u16 es;
283     UREG(edi, di, di_hi, di_lo);
284     UREG(esi, si, si_hi, si_lo);
285     UREG(ebp, bp, bp_hi, bp_lo);
286     UREG(esp, sp, sp_hi, sp_lo);
287     UREG(ebx, bx, bh, bl);
288     UREG(edx, dx, dh, dl);
289     UREG(ecx, cx, ch, cl);
290     UREG(eax, ax, ah, al);
291     u16 ip;
292     u16 cs;
293     u16 flags;
294 } __attribute__((packed));
295
296 // bregs flags bitdefs
297 #define F_ZF (1<<6)
298 #define F_CF (1<<0)
299
300 static inline void
301 set_cf(struct bregs *regs, int cond)
302 {
303     if (cond)
304         regs->flags |= F_CF;
305     else
306         regs->flags &= ~F_CF;
307 }
308
309
310 /****************************************************************
311  * Bios Config Table
312  ****************************************************************/
313
314 struct bios_config_table_s {
315     // XXX
316     u8 x;
317 };
318
319 extern struct bios_config_table_s BIOS_CONFIG_TABLE;
320
321
322 /****************************************************************
323  * Memory layout info
324  ****************************************************************/
325
326 #define SEG_BIOS     0xf000
327
328 #define EBDA_SEG           0x9FC0
329 #define IPL_SEG            0x9FF0
330 #define EBDA_SIZE          1              // In KiB
331 #define BASE_MEM_IN_K   (640 - EBDA_SIZE)
332
333 #endif // __BIOSVAR_H