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