blob: 7e8f5235b43aa8df94b986cdbb473b9f61836849 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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_*
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#include "config.h" // CONFIG_*
10#include "cmos.h" // CMOS_*
11#include "util.h" // memset
12#include "biosvar.h" // struct bios_data_area_s
Kevin O'Connor2ad37442008-05-06 19:49:01 -040013#include "ata.h" // hard_drive_setup
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040014#include "disk.h" // floppy_drive_setup
Kevin O'Connorc7812932008-06-08 23:08:12 -040015#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040016#include "pic.h" // pic_setup
Kevin O'Connor0525d292008-07-04 06:18:30 -040017#include "pci.h" // create_pirtable
18#include "acpi.h" // acpi_bios_init
Kevin O'Connor9521e262008-07-04 13:04:29 -040019#include "bregs.h" // struct bregs
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020
Kevin O'Connor438f6352008-03-30 21:46:53 -040021#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
22#define ebda ((struct extended_bios_data_area_s *)MAKE_FARPTR(SEG_EBDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050023
24static void
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040025set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040026{
27 SET_BDA(ivecs[vector].seg, SEG_BIOS);
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040028 SET_BDA(ivecs[vector].offset, (u32)loc - BUILD_BIOS_ADDR);
Kevin O'Connore3677b12008-07-04 15:29:23 -040029}
30
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040031// Symbols defined in romlayout.S
32extern void dummy_iret_handler();
33extern void entry_08();
34extern void entry_09();
35extern void entry_hwirq();
36extern void entry_0e();
37extern void entry_10();
38extern void entry_11();
39extern void entry_12();
40extern void entry_13();
41extern void entry_14();
42extern void entry_15();
43extern void entry_16();
44extern void entry_17();
45extern void entry_18();
46extern void entry_19();
47extern void entry_1a();
48extern void entry_1c();
49extern void entry_40();
50extern void entry_70();
51extern void entry_74();
52extern void entry_75();
53extern void entry_76();
54
Kevin O'Connore3677b12008-07-04 15:29:23 -040055static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050056init_bda()
57{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040058 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050059 memset(bda, 0, sizeof(*bda));
60
Kevin O'Connorf54c1502008-06-14 15:56:16 -040061 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
62
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050063 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040064 for (i=0; i<256; i++)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040065 set_irq(i, &dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050066
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040067 set_irq(0x08, &entry_08);
68 set_irq(0x09, &entry_09);
69 //set_irq(0x0a, &entry_hwirq);
70 //set_irq(0x0b, &entry_hwirq);
71 //set_irq(0x0c, &entry_hwirq);
72 //set_irq(0x0d, &entry_hwirq);
73 set_irq(0x0e, &entry_0e);
74 //set_irq(0x0f, &entry_hwirq);
75 set_irq(0x10, &entry_10);
76 set_irq(0x11, &entry_11);
77 set_irq(0x12, &entry_12);
78 set_irq(0x13, &entry_13);
79 set_irq(0x14, &entry_14);
80 set_irq(0x15, &entry_15);
81 set_irq(0x16, &entry_16);
82 set_irq(0x17, &entry_17);
83 set_irq(0x18, &entry_18);
84 set_irq(0x19, &entry_19);
85 set_irq(0x1a, &entry_1a);
86 set_irq(0x1c, &entry_1c);
87 set_irq(0x40, &entry_40);
88 set_irq(0x70, &entry_70);
89 //set_irq(0x71, &entry_hwirq);
90 //set_irq(0x72, &entry_hwirq);
91 //set_irq(0x73, &entry_hwirq);
92 set_irq(0x74, &entry_74);
93 set_irq(0x75, &entry_75);
94 set_irq(0x76, &entry_76);
95 //set_irq(0x77, &entry_hwirq);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050096
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050097 // set vector 0x79 to zero
98 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040099 SET_BDA(ivecs[0x79].seg, 0);
100 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500101
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400102 set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500103}
104
105static void
106init_ebda()
107{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500108 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500109 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -0400110 SET_BDA(ebda_seg, SEG_EBDA);
111 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -0400112 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400113 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -0400114 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -0400115 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400116 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500117}
118
119static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400120ram_probe(void)
121{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400122 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400123 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -0400124 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400125 } else {
126 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400127 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
128 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400129 if (rs)
130 rs += 16 * 1024 * 1024;
131 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400132 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
133 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400134 + 1 * 1024 * 1024);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400135 SET_EBDA(ram_size, rs);
136 add_e820(0, rs, E820_RAM);
137
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400138 // Check for memory over 4Gig
139 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
140 | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
141 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
142 SET_EBDA(ram_size_over4G, high);
143 add_e820(0x100000000ull, high, E820_RAM);
144
Kevin O'Connorc7812932008-06-08 23:08:12 -0400145 /* reserve 256KB BIOS area at the end of 4 GB */
146 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400147 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400148
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400149 // Don't declare any memory between 0xa0000 and 0x100000
150 add_e820(0xa0000, 0x50000, E820_HOLE);
151
Kevin O'Connorc7812932008-06-08 23:08:12 -0400152 // Mark known areas as reserved.
153 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400154 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400155
156 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400157}
158
159static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400160init_bios_tables(void)
161{
162 if (CONFIG_COREBOOT)
163 // XXX - not supported on coreboot yet.
164 return;
165
166 smm_init();
167
168 create_pirtable();
169
170 mptable_init();
171
172 smbios_init();
173
174 acpi_bios_init();
175}
176
177static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500178init_boot_vectors()
179{
Kevin O'Connor40967022008-07-21 22:23:05 -0400180 if (! CONFIG_BOOT)
181 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400182 dprintf(3, "init boot device ordering\n");
183
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500184 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400185 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500186 ip->type = IPL_TYPE_FLOPPY;
187 ip++;
188
189 // First HDD
190 ip->type = IPL_TYPE_HARDDISK;
191 ip++;
192
193 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500194 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500195 ip->type = IPL_TYPE_CDROM;
196 ip++;
197 }
198
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400199 ebda->ipl.count = ip - ebda->ipl.table;
200 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400201 if (CONFIG_COREBOOT) {
202 // XXX - hardcode defaults for coreboot.
203 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400204 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400205 } else {
206 // On emulators, get boot order from nvram.
207 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
208 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
209 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
210 ebda->ipl.checkfloppysig = 1;
211 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500212}
213
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400214// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500215static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216post()
217{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500218 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500219 init_ebda();
220
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400221 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400222 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400223 mathcp_setup();
224
225 vga_setup();
226
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500227 kbd_setup();
228 lpt_setup();
229 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400230 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500231
Kevin O'Connorc7812932008-06-08 23:08:12 -0400232 memmap_setup();
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400233 ram_probe();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400234 pci_bios_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400235 init_bios_tables();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400236 memmap_finalize();
237
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400238 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400239 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500240
241 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500242
Kevin O'Connor714325c2008-11-01 20:32:27 -0400243 optionrom_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500244}
245
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400246// Clear .bss section for C code.
247static void
248clear_bss()
249{
250 dprintf(3, "clearing .bss section\n");
251 extern char __bss_start[], __bss_end[];
252 memset(__bss_start, 0, __bss_end - __bss_start);
253}
254
255// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500256static void
257init_dma()
258{
259 // first reset the DMA controllers
260 outb(0, PORT_DMA1_MASTER_CLEAR);
261 outb(0, PORT_DMA2_MASTER_CLEAR);
262
263 // then initialize the DMA controllers
264 outb(0xc0, PORT_DMA2_MODE_REG);
265 outb(0x00, PORT_DMA2_MASK_REG);
266}
267
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400268// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500269static void
270check_restart_status()
271{
272 // Get and then clear CMOS shutdown status.
273 u8 status = inb_cmos(CMOS_RESET_CODE);
274 outb_cmos(0, CMOS_RESET_CODE);
275
276 if (status == 0x00 || status == 0x09 || status >= 0x0d)
277 // Normal post
278 return;
279
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500280 if (status != 0x05) {
281 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
282 return;
283 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500284
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500285 // XXX - this is supposed to jump without changing any memory -
286 // but the stack has been altered by the time the code gets here.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400287 eoi_pic2();
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500288 struct bregs br;
289 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400290 br.cs = GET_BDA(jump_cs_ip) >> 16;
291 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500292 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500293}
294
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400295// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500296void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500297_start()
298{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500299 init_dma();
300 check_restart_status();
301
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400302 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400303 dprintf(1, "Start bios\n");
304
305 // Setup for .bss and .data sections
306 clear_bss();
307 make_bios_writable();
308
309 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500310 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400311
312 // Present the user with a bootup menu.
313 interactive_bootmenu();
314
Kevin O'Connore90f2642008-06-28 12:18:39 -0400315 // Setup bios checksum.
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400316 extern char bios_checksum;
317 bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400318
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400319 // Prep for boot process.
320 make_bios_readonly();
321 clear_bss();
322
323 // Invoke int 19 to start boot process.
324 dprintf(3, "Jump to int19\n");
325 struct bregs br;
326 memset(&br, 0, sizeof(br));
327 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500328}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400329
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400330// Ughh - some older gcc compilers have a bug which causes VISIBLE32
331// functions to not be exported as a global variable - force _start
332// to be global here.
333asm(".global _start");