Minor cleanups.
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "ioport.h" // PORT_*
9 #include "../out/rom16.offset.auto.h" // OFFSET_*
10 #include "config.h" // CONFIG_*
11 #include "cmos.h" // CMOS_*
12 #include "util.h" // memset
13 #include "biosvar.h" // struct bios_data_area_s
14 #include "ata.h" // hard_drive_setup
15 #include "kbd.h" // kbd_setup
16 #include "disk.h" // floppy_drive_setup
17
18 #define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
19 #define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
20
21 static void
22 init_bda()
23 {
24     memset(bda, 0, sizeof(*bda));
25
26     int i;
27     for (i=0; i<256; i++) {
28         SET_BDA(ivecs[i].seg, SEG_BIOS);
29         SET_BDA(ivecs[i].offset, OFFSET_dummy_iret_handler);
30     }
31
32     SET_BDA(mem_size_kb, BASE_MEM_IN_K);
33
34     // mov CMOS Equipment Byte to BDA Equipment Word
35     SET_BDA(equipment_list_flags, inb_cmos(CMOS_EQUIPMENT_INFO));
36
37     // set vector 0x79 to zero
38     // this is used by 'gardian angel' protection system
39     SET_BDA(ivecs[0x79].seg, 0);
40     SET_BDA(ivecs[0x79].offset, 0);
41
42     SET_BDA(ivecs[0x40].offset, OFFSET_entry_40);
43     SET_BDA(ivecs[0x0e].offset, OFFSET_entry_0e);
44     SET_BDA(ivecs[0x13].offset, OFFSET_entry_13);
45     SET_BDA(ivecs[0x76].offset, OFFSET_entry_76);
46     SET_BDA(ivecs[0x17].offset, OFFSET_entry_17);
47     SET_BDA(ivecs[0x18].offset, OFFSET_entry_18);
48     SET_BDA(ivecs[0x19].offset, OFFSET_entry_19);
49     SET_BDA(ivecs[0x1c].offset, OFFSET_entry_1c);
50     SET_BDA(ivecs[0x12].offset, OFFSET_entry_12);
51     SET_BDA(ivecs[0x11].offset, OFFSET_entry_11);
52     SET_BDA(ivecs[0x15].offset, OFFSET_entry_15);
53     SET_BDA(ivecs[0x08].offset, OFFSET_entry_08);
54     SET_BDA(ivecs[0x09].offset, OFFSET_entry_09);
55     SET_BDA(ivecs[0x16].offset, OFFSET_entry_16);
56     SET_BDA(ivecs[0x14].offset, OFFSET_entry_14);
57     SET_BDA(ivecs[0x1a].offset, OFFSET_entry_1a);
58     SET_BDA(ivecs[0x70].offset, OFFSET_entry_70);
59     SET_BDA(ivecs[0x74].offset, OFFSET_entry_74);
60     SET_BDA(ivecs[0x75].offset, OFFSET_entry_75);
61     SET_BDA(ivecs[0x10].offset, OFFSET_entry_10);
62
63     SET_BDA(ivecs[0x1E].offset, OFFSET_diskette_param_table2);
64 }
65
66 static void
67 init_ebda()
68 {
69     memset(ebda, 0, sizeof(*ebda));
70     ebda->size = EBDA_SIZE;
71     SET_BDA(ebda_seg, SEG_EBDA);
72     SET_BDA(ivecs[0x41].seg, SEG_EBDA);
73     SET_BDA(ivecs[0x41].offset
74             , offsetof(struct extended_bios_data_area_s, fdpt[0]));
75     SET_BDA(ivecs[0x46].seg, SEG_EBDA);
76     SET_BDA(ivecs[0x41].offset
77             , offsetof(struct extended_bios_data_area_s, fdpt[1]));
78 }
79
80 static void
81 pic_setup()
82 {
83     outb(0x11, PORT_PIC1);
84     outb(0x11, PORT_PIC2);
85     outb(0x08, PORT_PIC1_DATA);
86     outb(0x70, PORT_PIC2_DATA);
87     outb(0x04, PORT_PIC1_DATA);
88     outb(0x02, PORT_PIC2_DATA);
89     outb(0x01, PORT_PIC1_DATA);
90     outb(0x01, PORT_PIC2_DATA);
91     outb(0xb8, PORT_PIC1_DATA);
92     if (CONFIG_PS2_MOUSE)
93         outb(0x8f, PORT_PIC2_DATA);
94     else
95         outb(0x9f, PORT_PIC2_DATA);
96 }
97
98 static void
99 init_boot_vectors()
100 {
101     // Floppy drive
102     struct ipl_entry_s *ip = &ebda->ipl.table[0];
103     ip->type = IPL_TYPE_FLOPPY;
104     ip++;
105
106     // First HDD
107     ip->type = IPL_TYPE_HARDDISK;
108     ip++;
109
110     // CDROM
111     if (CONFIG_CDROM_BOOT) {
112         ip->type = IPL_TYPE_CDROM;
113         ip++;
114     }
115
116     ebda->ipl.count = ip - ebda->ipl.table;
117     ebda->ipl.sequence = 0xffff;
118     ebda->ipl.bootfirst = 0xffff;
119 }
120
121 static void
122 callrom(u16 seg, u16 offset)
123 {
124     struct bregs br;
125     memset(&br, 0, sizeof(br));
126     br.es = SEG_BIOS;
127     br.di = OFFSET_pnp_string + 1; // starts 1 past for alignment
128     br.cs = seg;
129     br.ip = offset;
130     call16(&br);
131 }
132
133 static void
134 rom_scan(u32 start, u32 end)
135 {
136     u8 *p = (u8*)start;
137     for (; p <= (u8*)end; p += 2048) {
138         u8 *rom = p;
139         if (*(u16*)rom != 0xaa55)
140             continue;
141         u32 len = rom[2] * 512;
142         if (checksum(rom, len) != 0)
143             continue;
144         p = (u8*)(((u32)p + len) / 2048 * 2048);
145         callrom(FARPTR_TO_SEG(rom), FARPTR_TO_OFFSET(rom + 3));
146
147         // Look at the ROM's PnP Expansion header.  Properly, we're supposed
148         // to init all the ROMs and then go back and build an IPL table of
149         // all the bootable devices, but we can get away with one pass.
150         if (rom[0x1a] != '$' || rom[0x1b] != 'P'
151             || rom[0x1c] != 'n' || rom[0x1d] != 'P')
152             continue;
153         // 0x1A is also the offset into the expansion header of...
154         // the Bootstrap Entry Vector, or zero if there is none.
155         u16 entry = *(u16*)&rom[0x1a+0x1a];
156         if (!entry)
157             continue;
158         // Found a device that thinks it can boot the system.  Record
159         // its BEV and product name string.
160
161         if (ebda->ipl.count >= ARRAY_SIZE(ebda->ipl.table))
162             continue;
163
164         struct ipl_entry_s *ip = &ebda->ipl.table[ebda->ipl.count];
165         ip->type = IPL_TYPE_BEV;
166         ip->vector = (FARPTR_TO_SEG(rom) << 16) | entry;
167
168         u16 desc = *(u16*)&rom[0x1a+0x10];
169         if (desc)
170             ip->description = (u32)MAKE_FARPTR(FARPTR_TO_SEG(rom), desc);
171
172         ebda->ipl.count++;
173     }
174 }
175
176 static void
177 post()
178 {
179     BX_INFO("Start bios\n");
180
181     init_bda();
182     init_ebda();
183
184     timer_setup();
185     kbd_setup();
186     lpt_setup();
187     serial_setup();
188     pic_setup();
189
190     rom_scan(0xc0000, 0xc7800);
191
192     printf("BIOS - begin\n\n");
193
194     // clear bss section -- XXX - shouldn't use globals
195     extern char __bss_start[], __bss_end[];
196     memset(__bss_start, 0, __bss_end - __bss_start);
197
198     rombios32_init();
199
200     floppy_drive_setup();
201     hard_drive_setup();
202
203     init_boot_vectors();
204
205     rom_scan(0xc8000, 0xe0000);
206
207     interactive_bootmenu();
208
209     // reset the memory (some boot loaders such as syslinux suppose
210     // that the memory is set to zero)
211     memset((void*)0x40000, 0, 0x40000); // XXX - shouldn't use globals
212
213     // Invoke int 19 to start boot process.
214     struct bregs br;
215     memset(&br, 0, sizeof(br));
216     call16_int(0x19, &br);
217 }
218
219 static void
220 init_dma()
221 {
222     // first reset the DMA controllers
223     outb(0, PORT_DMA1_MASTER_CLEAR);
224     outb(0, PORT_DMA2_MASTER_CLEAR);
225
226     // then initialize the DMA controllers
227     outb(0xc0, PORT_DMA2_MODE_REG);
228     outb(0x00, PORT_DMA2_MASK_REG);
229 }
230
231 static void
232 check_restart_status()
233 {
234     // Get and then clear CMOS shutdown status.
235     u8 status = inb_cmos(CMOS_RESET_CODE);
236     outb_cmos(0, CMOS_RESET_CODE);
237
238     if (status == 0x00 || status == 0x09 || status >= 0x0d)
239         // Normal post
240         return;
241
242     if (status != 0x05) {
243         BX_PANIC("Unimplemented shutdown status: %02x\n", status);
244         return;
245     }
246
247     // XXX - this is supposed to jump without changing any memory -
248     // but the stack has been altered by the time the code gets here.
249     eoi_both_pics();
250     struct bregs br;
251     memset(&br, 0, sizeof(br));
252     br.cs = GET_BDA(jump_cs_ip) >> 16;
253     br.ip = GET_BDA(jump_cs_ip);
254     call16(&br);
255 }
256
257 void VISIBLE32
258 _start()
259 {
260     init_dma();
261     check_restart_status();
262
263     post();
264 }