blob: 6f407d92c06c97e045c8fc063c16c873f12e2ad6 [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
Kevin O'Connord21c0892008-11-26 17:02:43 -050024void
25__set_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'Connord21c0892008-11-26 17:02:43 -050031#define set_irq(vector, func) do { \
32 extern void func (void); \
33 __set_irq(vector, func); \
34 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040035
Kevin O'Connore3677b12008-07-04 15:29:23 -040036static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037init_bda()
38{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040039 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040 memset(bda, 0, sizeof(*bda));
41
Kevin O'Connorf54c1502008-06-14 15:56:16 -040042 SET_BDA(mem_size_kb, BASE_MEM_IN_K);
43
Kevin O'Connord21c0892008-11-26 17:02:43 -050044 // Initialize all vectors to a dummy handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040046 for (i=0; i<256; i++)
Kevin O'Connord21c0892008-11-26 17:02:43 -050047 set_irq(i, dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048
Kevin O'Connord21c0892008-11-26 17:02:43 -050049 // Initialize all hw vectors to a default hw handler.
50 for (i=0x08; i<=0x0f; i++)
51 set_irq(i, entry_hwirq);
52 for (i=0x70; i<=0x77; i++)
53 set_irq(i, entry_hwirq);
54
55 // Initialize software handlers.
56 set_irq(0x10, entry_10);
57 set_irq(0x11, entry_11);
58 set_irq(0x12, entry_12);
59 set_irq(0x13, entry_13);
60 set_irq(0x14, entry_14);
61 set_irq(0x15, entry_15);
62 set_irq(0x16, entry_16);
63 set_irq(0x17, entry_17);
64 set_irq(0x18, entry_18);
65 set_irq(0x19, entry_19);
66 set_irq(0x1a, entry_1a);
67 set_irq(0x1c, entry_1c);
68 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050069
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050070 // set vector 0x79 to zero
71 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040072 SET_BDA(ivecs[0x79].seg, 0);
73 SET_BDA(ivecs[0x79].offset, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050074
Kevin O'Connord21c0892008-11-26 17:02:43 -050075 __set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050076}
77
78static void
79init_ebda()
80{
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050081 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050082 ebda->size = EBDA_SIZE;
Kevin O'Connor438f6352008-03-30 21:46:53 -040083 SET_BDA(ebda_seg, SEG_EBDA);
84 SET_BDA(ivecs[0x41].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040085 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040086 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor438f6352008-03-30 21:46:53 -040087 SET_BDA(ivecs[0x46].seg, SEG_EBDA);
Kevin O'Connore9061492008-03-11 21:54:18 -040088 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040089 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050090}
91
92static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040093ram_probe(void)
94{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040095 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040096 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -040097 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040098 } else {
99 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400100 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
101 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400102 if (rs)
103 rs += 16 * 1024 * 1024;
104 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400105 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
106 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400107 + 1 * 1024 * 1024);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400108 SET_EBDA(ram_size, rs);
109 add_e820(0, rs, E820_RAM);
110
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400111 // Check for memory over 4Gig
112 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
113 | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
114 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
115 SET_EBDA(ram_size_over4G, high);
116 add_e820(0x100000000ull, high, E820_RAM);
117
Kevin O'Connorc7812932008-06-08 23:08:12 -0400118 /* reserve 256KB BIOS area at the end of 4 GB */
119 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400120 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400121
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400122 // Don't declare any memory between 0xa0000 and 0x100000
123 add_e820(0xa0000, 0x50000, E820_HOLE);
124
Kevin O'Connorc7812932008-06-08 23:08:12 -0400125 // Mark known areas as reserved.
126 add_e820((u32)MAKE_FARPTR(SEG_EBDA, 0), EBDA_SIZE * 1024, E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400127 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400128
129 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400130}
131
132static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400133init_bios_tables(void)
134{
135 if (CONFIG_COREBOOT)
136 // XXX - not supported on coreboot yet.
137 return;
138
139 smm_init();
140
141 create_pirtable();
142
143 mptable_init();
144
145 smbios_init();
146
147 acpi_bios_init();
148}
149
150static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500151init_boot_vectors()
152{
Kevin O'Connor40967022008-07-21 22:23:05 -0400153 if (! CONFIG_BOOT)
154 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400155 dprintf(3, "init boot device ordering\n");
156
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500157 // Floppy drive
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400158 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500159 ip->type = IPL_TYPE_FLOPPY;
160 ip++;
161
162 // First HDD
163 ip->type = IPL_TYPE_HARDDISK;
164 ip++;
165
166 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500167 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500168 ip->type = IPL_TYPE_CDROM;
169 ip++;
170 }
171
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400172 ebda->ipl.count = ip - ebda->ipl.table;
173 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400174 if (CONFIG_COREBOOT) {
175 // XXX - hardcode defaults for coreboot.
176 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400177 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400178 } else {
179 // On emulators, get boot order from nvram.
180 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
181 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
182 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
183 ebda->ipl.checkfloppysig = 1;
184 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500185}
186
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400187// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500188static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500189post()
190{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192 init_ebda();
193
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400194 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400195 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400196 mathcp_setup();
197
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500198 memmap_setup();
199 ram_probe();
200
Kevin O'Connor714325c2008-11-01 20:32:27 -0400201 vga_setup();
202
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203 kbd_setup();
204 lpt_setup();
205 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400206 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500207
Kevin O'Connor0525d292008-07-04 06:18:30 -0400208 pci_bios_setup();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400209 init_bios_tables();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400210 memmap_finalize();
211
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400212 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400213 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500214
215 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500216
Kevin O'Connor714325c2008-11-01 20:32:27 -0400217 optionrom_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500218}
219
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400220// Clear .bss section for C code.
221static void
222clear_bss()
223{
224 dprintf(3, "clearing .bss section\n");
225 extern char __bss_start[], __bss_end[];
226 memset(__bss_start, 0, __bss_end - __bss_start);
227}
228
229// Reset DMA controller
Kevin O'Connor95576e72008-03-01 09:57:51 -0500230static void
231init_dma()
232{
233 // first reset the DMA controllers
234 outb(0, PORT_DMA1_MASTER_CLEAR);
235 outb(0, PORT_DMA2_MASTER_CLEAR);
236
237 // then initialize the DMA controllers
238 outb(0xc0, PORT_DMA2_MODE_REG);
239 outb(0x00, PORT_DMA2_MASK_REG);
240}
241
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400242// Check if the machine was setup with a special restart vector.
Kevin O'Connor95576e72008-03-01 09:57:51 -0500243static void
244check_restart_status()
245{
246 // Get and then clear CMOS shutdown status.
247 u8 status = inb_cmos(CMOS_RESET_CODE);
248 outb_cmos(0, CMOS_RESET_CODE);
249
250 if (status == 0x00 || status == 0x09 || status >= 0x0d)
251 // Normal post
252 return;
253
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500254 if (status != 0x05) {
255 BX_PANIC("Unimplemented shutdown status: %02x\n", status);
256 return;
257 }
Kevin O'Connor95576e72008-03-01 09:57:51 -0500258
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500259 // XXX - this is supposed to jump without changing any memory -
260 // but the stack has been altered by the time the code gets here.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400261 eoi_pic2();
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500262 struct bregs br;
263 memset(&br, 0, sizeof(br));
Kevin O'Connore9061492008-03-11 21:54:18 -0400264 br.cs = GET_BDA(jump_cs_ip) >> 16;
265 br.ip = GET_BDA(jump_cs_ip);
Kevin O'Connorad4ec342008-03-02 23:25:11 -0500266 call16(&br);
Kevin O'Connor95576e72008-03-01 09:57:51 -0500267}
268
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400269// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500270void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500271_start()
272{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500273 init_dma();
274 check_restart_status();
275
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400276 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400277 dprintf(1, "Start bios\n");
278
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500279 pci_bus_setup();
280
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400281 // Setup for .bss and .data sections
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400282 make_bios_writable();
Kevin O'Connoreadf1ee2008-11-07 22:15:31 -0500283 clear_bss();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400284
285 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500286 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400287
288 // Present the user with a bootup menu.
289 interactive_bootmenu();
290
Kevin O'Connore90f2642008-06-28 12:18:39 -0400291 // Setup bios checksum.
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400292 extern char bios_checksum;
293 bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400294
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400295 // Prep for boot process.
296 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400297
298 // Invoke int 19 to start boot process.
299 dprintf(3, "Jump to int19\n");
300 struct bregs br;
301 memset(&br, 0, sizeof(br));
302 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500303}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400304
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400305// Ughh - some older gcc compilers have a bug which causes VISIBLE32
306// functions to not be exported as a global variable - force _start
307// to be global here.
308asm(".global _start");