Reorganize boot code.
[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 LGPLv3 license.
7
8 #include "ioport.h" // PORT_*
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_*
11 #include "util.h" // memset
12 #include "biosvar.h" // struct bios_data_area_s
13 #include "disk.h" // floppy_drive_setup
14 #include "memmap.h" // add_e820
15 #include "pic.h" // pic_setup
16 #include "pci.h" // create_pirtable
17 #include "acpi.h" // acpi_bios_init
18 #include "bregs.h" // struct bregs
19 #include "boot.h" // IPL
20
21 void
22 __set_irq(int vector, void *loc)
23 {
24     SET_IVT(vector, SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR);
25 }
26
27 #define set_irq(vector, func) do {              \
28         extern void func (void);                \
29         __set_irq(vector, func);                \
30     } while (0)
31
32 static void
33 init_ivt()
34 {
35     dprintf(3, "init ivt\n");
36
37     // Initialize all vectors to the default handler.
38     int i;
39     for (i=0; i<256; i++)
40         set_irq(i, entry_iret_official);
41
42     // Initialize all hw vectors to a default hw handler.
43     for (i=0x08; i<=0x0f; i++)
44         set_irq(i, entry_hwpic1);
45     for (i=0x70; i<=0x77; i++)
46         set_irq(i, entry_hwpic2);
47
48     // Initialize software handlers.
49     set_irq(0x10, entry_10);
50     set_irq(0x11, entry_11_official);
51     set_irq(0x12, entry_12_official);
52     set_irq(0x13, entry_13_official);
53     set_irq(0x14, entry_14);
54     set_irq(0x15, entry_15);
55     set_irq(0x16, entry_16);
56     set_irq(0x17, entry_17);
57     set_irq(0x18, entry_18);
58     set_irq(0x19, entry_19_official);
59     set_irq(0x1a, entry_1a);
60     set_irq(0x40, entry_40);
61
62     // set vector 0x79 to zero
63     // this is used by 'gardian angel' protection system
64     SET_IVT(0x79, 0, 0);
65
66     __set_irq(0x1E, &diskette_param_table2);
67 }
68
69 static void
70 init_bda()
71 {
72     dprintf(3, "init bda\n");
73
74     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
75     memset(bda, 0, sizeof(*bda));
76
77     int esize = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024);
78     SET_BDA(mem_size_kb, 640 - esize);
79     u16 eseg = FLATPTR_TO_SEG((640 - esize) * 1024);
80     SET_BDA(ebda_seg, eseg);
81
82     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
83     memset(ebda, 0, sizeof(*ebda));
84     ebda->size = esize;
85     SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0]));
86     SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1]));
87 }
88
89 static void
90 ram_probe(void)
91 {
92     dprintf(3, "Find memory size\n");
93     if (CONFIG_COREBOOT) {
94         coreboot_fill_map();
95     } else {
96         // On emulators, get memory size from nvram.
97         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
98                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
99         if (rs)
100             rs += 16 * 1024 * 1024;
101         else
102             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
103                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
104                   + 1 * 1024 * 1024);
105         RamSize = rs;
106         add_e820(0, rs, E820_RAM);
107
108         // Check for memory over 4Gig
109         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
110                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
111                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
112         RamSizeOver4G = high;
113         add_e820(0x100000000ull, high, E820_RAM);
114
115         /* reserve 256KB BIOS area at the end of 4 GB */
116         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
117     }
118
119     // Don't declare any memory between 0xa0000 and 0x100000
120     add_e820(0xa0000, 0x50000, E820_HOLE);
121
122     // Mark known areas as reserved.
123     u16 ebda_seg = get_ebda_seg();
124     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
125              , E820_RESERVED);
126     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
127
128     if (CONFIG_KVM)
129         // 4 pages before the bios, 3 pages for vmx tss pages, the
130         // other page for EPT real mode pagetable
131         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
132
133     dprintf(1, "Ram Size=0x%08x\n", RamSize);
134 }
135
136 static void
137 init_bios_tables(void)
138 {
139     if (CONFIG_COREBOOT)
140         // XXX - not supported on coreboot yet.
141         return;
142
143     create_pirtable();
144
145     mptable_init();
146
147     smbios_init();
148
149     acpi_bios_init();
150 }
151
152 // Main setup code.
153 static void
154 post()
155 {
156     init_ivt();
157     init_bda();
158
159     pic_setup();
160     timer_setup();
161     mathcp_setup();
162
163     smp_probe_setup();
164
165     memmap_setup();
166     ram_probe();
167     mtrr_setup();
168
169     pnp_setup();
170     vga_setup();
171
172     kbd_setup();
173     lpt_setup();
174     serial_setup();
175     mouse_setup();
176
177     pci_bios_setup();
178     smm_init();
179
180     init_bios_tables();
181     memmap_finalize();
182
183     boot_setup();
184
185     floppy_drive_setup();
186     hard_drive_setup();
187
188     optionrom_setup();
189 }
190
191 // 32-bit entry point.
192 void VISIBLE32
193 _start()
194 {
195     init_dma();
196
197     debug_serial_setup();
198     dprintf(1, "Start bios\n");
199
200     // Allow writes to modify bios area (0xf0000)
201     make_bios_writable();
202
203     // Perform main setup code.
204     post();
205
206     // Present the user with a bootup menu.
207     interactive_bootmenu();
208
209     // Setup bios checksum.
210     BiosChecksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
211
212     // Prep for boot process.
213     make_bios_readonly();
214
215     // Invoke int 19 to start boot process.
216     dprintf(3, "Jump to int19\n");
217     struct bregs br;
218     memset(&br, 0, sizeof(br));
219     call16_int(0x19, &br);
220 }
221
222 // Ughh - some older gcc compilers have a bug which causes VISIBLE32
223 // functions to not be exported as a global variable - force _start
224 // to be global here.
225 asm(".global _start");