Version 0.1.1
[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
7 #include "types.h" // u8
8 #include "farptr.h" // SET_SEG
9
10
11 /****************************************************************
12  * Bios Data Area (BDA)
13  ****************************************************************/
14
15 struct ivec {
16     u16 offset;
17     u16 seg;
18 };
19
20 struct bios_data_area_s {
21     // 00:00
22     struct ivec ivecs[256];
23     // 30:00
24 //    u8 stack[256];
25     // 40:00
26     u16 port_com1, port_com2, port_com3, port_com4;
27     u16 port_lpt1, port_lpt2, port_lpt3;
28     u16 ebda_seg;
29     // 40:10
30     u16 equipment_list_flags;
31     u8 pad1;
32     u16 mem_size_kb;
33     u8 pad2;
34     u8 ps2_ctrl_flag;
35     u8 kbd_flag0;
36     u8 kbd_flag1;
37     u8 alt_keypad;
38     u16 kbd_buf_head;
39     u16 kbd_buf_tail;
40     // 40:1e
41     u8 kbd_buf[32];
42     u8 floppy_recalibration_status;
43     u8 floppy_motor_status;
44     // 40:40
45     u8 floppy_motor_counter;
46     u8 floppy_last_status;
47     u8 floppy_return_status[7];
48     u8 other1[0x7];
49     // 40:50
50     u8 other2[0x1c];
51     // 40:6c
52     u32 timer_counter;
53     // 40:70
54     u8 timer_rollover;
55     u8 other4[0x0f];
56     // 40:80
57     u16 kbd_buf_start_offset;
58     u16 kbd_buf_end_offset;
59     u8 other5[7];
60     u8 floppy_last_data_rate;
61     u8 other6[3];
62     u8 floppy_harddisk_info;
63     // 40:90
64     u8 floppy_media_state[4];
65     u8 floppy_track0;
66     u8 floppy_track1;
67     u8 kbd_mode;
68     u8 kbd_led;
69     u32 ptr_user_wait_complete_flag;
70     u32 user_wait_timeout;
71     // 40:A0
72     u8 rtc_wait_flag;
73 } __attribute__((packed));
74
75 // BDA floppy_recalibration_status bitdefs
76 #define FRS_TIMEOUT (1<<7)
77
78 // BDA rtc_wait_flag bitdefs
79 #define RWS_WAIT_PENDING (1<<0)
80 #define RWS_WAIT_ELAPSED (1<<7)
81
82 // BDA floppy_media_state bitdefs
83 #define FMS_DRIVE_STATE_MASK        (0x07)
84 #define FMS_MEDIA_DRIVE_ESTABLISHED (1<<4)
85 #define FMS_DOUBLE_STEPPING         (1<<5)
86 #define FMS_DATA_RATE_MASK          (0xc0)
87
88 // Accessor functions
89 #define GET_BDA(var) \
90     GET_FARVAR(0x0000, ((struct bios_data_area_s *)0)->var)
91 #define SET_BDA(var, val) \
92     SET_FARVAR(0x0000, ((struct bios_data_area_s *)0)->var, (val))
93 #define CLEARBITS_BDA(var, val) do {                                    \
94         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
95         SET_BDA(var, (__val & ~(val)));                                 \
96     } while (0)
97 #define SETBITS_BDA(var, val) do {                                      \
98         typeof(((struct bios_data_area_s *)0)->var) __val = GET_BDA(var); \
99         SET_BDA(var, (__val | (val)));                                  \
100     } while (0)
101
102
103 /****************************************************************
104  * Extended Bios Data Area (EBDA)
105  ****************************************************************/
106
107 struct extended_bios_data_area_s {
108     u8 size;
109     u8 other1[0x3c];
110
111     // FDPT - Can be splitted in data members if needed
112     u8 fdpt0[0x10];
113     u8 fdpt1[0x10];
114
115     u8 other2[0xC4];
116
117     // ATA Driver data
118     //ata_t   ata;
119
120 #if BX_ELTORITO_BOOT
121     // El Torito Emulation data
122     cdemu_t cdemu;
123 #endif // BX_ELTORITO_BOOT
124 };
125
126
127 /****************************************************************
128  * Extended Bios Data Area (EBDA)
129  ****************************************************************/
130
131 #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; }; }
132
133 struct bregs {
134     u16 ds;
135     u16 es;
136     UREG(edi, di, di_hi, di_lo);
137     UREG(esi, si, si_hi, si_lo);
138     UREG(ebp, bp, bp_hi, bp_lo);
139     UREG(esp, sp, sp_hi, sp_lo);
140     UREG(ebx, bx, bh, bl);
141     UREG(edx, dx, dh, dl);
142     UREG(ecx, cx, ch, cl);
143     UREG(eax, ax, ah, al);
144     u16 ip;
145     u16 cs;
146     u16 flags;
147 } __attribute__((packed));
148
149 // bregs flags bitdefs
150 #define F_ZF (1<<6)
151 #define F_CF (1<<0)
152
153 static inline void
154 set_cf(struct bregs *regs, int cond)
155 {
156     if (cond)
157         regs->flags |= F_CF;
158     else
159         regs->flags &= ~F_CF;
160 }
161
162
163 /****************************************************************
164  * Bios Config Table
165  ****************************************************************/
166
167 struct bios_config_table_s {
168     // XXX
169     u8 x;
170 };
171
172 extern struct bios_config_table_s BIOS_CONFIG_TABLE;
173
174
175 /****************************************************************
176  * Memory layout info
177  ****************************************************************/
178
179 #define SEG_BIOS     0xf000
180
181 #define EBDA_SEG           0x9FC0
182 #define EBDA_SIZE          1              // In KiB
183 #define BASE_MEM_IN_K   (640 - EBDA_SIZE)