|
|
1.1 root 1: // 32bit code to Power On Self Test (POST) a machine.
2: //
1.1.1.4 root 3: // Copyright (C) 2008-2010 Kevin O'Connor <[email protected]>
1.1 root 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 "ata.h" // ata_setup
1.1.1.5 root 15: #include "ahci.h" // ahci_setup
1.1 root 16: #include "memmap.h" // add_e820
17: #include "pic.h" // pic_setup
18: #include "pci.h" // create_pirtable
19: #include "acpi.h" // acpi_bios_init
20: #include "bregs.h" // struct bregs
21: #include "mptable.h" // mptable_init
22: #include "boot.h" // IPL
23: #include "usb.h" // usb_setup
24: #include "smbios.h" // smbios_init
25: #include "paravirt.h" // qemu_cfg_port_probe
1.1.1.6 ! root 26: #include "xen.h" // xen_probe_hvm_info
1.1 root 27: #include "ps2port.h" // ps2port_setup
1.1.1.3 root 28: #include "virtio-blk.h" // virtio_blk_setup
1.1 root 29:
1.1.1.5 root 30:
31: /****************************************************************
32: * BIOS init
33: ****************************************************************/
34:
1.1 root 35: static void
1.1.1.2 root 36: init_ivt(void)
1.1 root 37: {
38: dprintf(3, "init ivt\n");
39:
40: // Initialize all vectors to the default handler.
41: int i;
42: for (i=0; i<256; i++)
1.1.1.4 root 43: SET_IVT(i, FUNC16(entry_iret_official));
1.1 root 44:
45: // Initialize all hw vectors to a default hw handler.
46: for (i=0x08; i<=0x0f; i++)
1.1.1.4 root 47: SET_IVT(i, FUNC16(entry_hwpic1));
1.1 root 48: for (i=0x70; i<=0x77; i++)
1.1.1.4 root 49: SET_IVT(i, FUNC16(entry_hwpic2));
1.1 root 50:
51: // Initialize software handlers.
1.1.1.4 root 52: SET_IVT(0x02, FUNC16(entry_02));
53: SET_IVT(0x10, FUNC16(entry_10));
54: SET_IVT(0x11, FUNC16(entry_11));
55: SET_IVT(0x12, FUNC16(entry_12));
56: SET_IVT(0x13, FUNC16(entry_13_official));
57: SET_IVT(0x14, FUNC16(entry_14));
58: SET_IVT(0x15, FUNC16(entry_15));
59: SET_IVT(0x16, FUNC16(entry_16));
60: SET_IVT(0x17, FUNC16(entry_17));
61: SET_IVT(0x18, FUNC16(entry_18));
62: SET_IVT(0x19, FUNC16(entry_19_official));
63: SET_IVT(0x1a, FUNC16(entry_1a));
64: SET_IVT(0x40, FUNC16(entry_40));
1.1 root 65:
1.1.1.2 root 66: // INT 60h-66h reserved for user interrupt
67: for (i=0x60; i<=0x66; i++)
68: SET_IVT(i, SEGOFF(0, 0));
69:
1.1 root 70: // set vector 0x79 to zero
71: // this is used by 'gardian angel' protection system
72: SET_IVT(0x79, SEGOFF(0, 0));
73:
1.1.1.4 root 74: SET_IVT(0x1E, SEGOFF(SEG_BIOS, (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
1.1 root 75: }
76:
77: static void
1.1.1.2 root 78: init_bda(void)
1.1 root 79: {
80: dprintf(3, "init bda\n");
81:
82: struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
83: memset(bda, 0, sizeof(*bda));
84:
85: int esize = EBDA_SIZE_START;
86: SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
1.1.1.5 root 87: u16 ebda_seg = EBDA_SEGMENT_START;
88: SET_BDA(ebda_seg, ebda_seg);
1.1 root 89:
90: // Init ebda
91: struct extended_bios_data_area_s *ebda = get_ebda_ptr();
92: memset(ebda, 0, sizeof(*ebda));
93: ebda->size = esize;
1.1.1.5 root 94:
95: add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
96: , E820_RESERVED);
1.1 root 97: }
98:
99: static void
100: ram_probe(void)
101: {
102: dprintf(3, "Find memory size\n");
103: if (CONFIG_COREBOOT) {
104: coreboot_setup();
1.1.1.6 ! root 105: } else if (usingXen()) {
! 106: xen_setup();
1.1 root 107: } else {
108: // On emulators, get memory size from nvram.
109: u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
110: | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
111: if (rs)
112: rs += 16 * 1024 * 1024;
113: else
114: rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
115: | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
116: + 1 * 1024 * 1024);
117: RamSize = rs;
118: add_e820(0, rs, E820_RAM);
119:
120: // Check for memory over 4Gig
121: u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
1.1.1.2 root 122: | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
1.1 root 123: | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
124: RamSizeOver4G = high;
125: add_e820(0x100000000ull, high, E820_RAM);
126:
127: /* reserve 256KB BIOS area at the end of 4 GB */
128: add_e820(0xfffc0000, 256*1024, E820_RESERVED);
129: }
130:
131: // Don't declare any memory between 0xa0000 and 0x100000
132: add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
133:
134: // Mark known areas as reserved.
135: add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
136:
1.1.1.3 root 137: u32 count = qemu_cfg_e820_entries();
138: if (count) {
139: struct e820_reservation entry;
140: int i;
141:
142: for (i = 0; i < count; i++) {
143: qemu_cfg_e820_load_next(&entry);
144: add_e820(entry.address, entry.length, entry.type);
145: }
146: } else if (kvm_para_available()) {
147: // Backwards compatibility - provide hard coded range.
1.1 root 148: // 4 pages before the bios, 3 pages for vmx tss pages, the
149: // other page for EPT real mode pagetable
150: add_e820(0xfffbc000, 4*4096, E820_RESERVED);
1.1.1.3 root 151: }
1.1 root 152:
1.1.1.2 root 153: dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
154: , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
1.1 root 155: }
156:
157: static void
158: init_bios_tables(void)
159: {
160: if (CONFIG_COREBOOT) {
161: coreboot_copy_biostable();
162: return;
163: }
1.1.1.6 ! root 164: if (usingXen()) {
! 165: xen_copy_biostables();
! 166: return;
! 167: }
1.1 root 168:
169: create_pirtable();
170:
171: mptable_init();
172:
173: smbios_init();
174:
175: acpi_bios_init();
176: }
177:
1.1.1.3 root 178: // Initialize hardware devices
179: static void
180: init_hw(void)
181: {
182: usb_setup();
183: ps2port_setup();
184: lpt_setup();
185: serial_setup();
186:
187: floppy_setup();
188: ata_setup();
1.1.1.5 root 189: ahci_setup();
190: cbfs_payload_setup();
1.1.1.3 root 191: ramdisk_setup();
192: virtio_blk_setup();
193: }
194:
1.1.1.5 root 195: // Begin the boot process by invoking an int0x19 in 16bit mode.
196: void VISIBLE32FLAT
197: startBoot(void)
198: {
199: // Clear low-memory allocations (required by PMM spec).
200: memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
201:
202: dprintf(3, "Jump to int19\n");
203: struct bregs br;
204: memset(&br, 0, sizeof(br));
205: br.flags = F_IF;
206: call16_int(0x19, &br);
207: }
208:
1.1 root 209: // Main setup code.
210: static void
1.1.1.5 root 211: maininit(void)
1.1 root 212: {
1.1.1.6 ! root 213: // Running at new code address - do code relocation fixups
! 214: malloc_fixupreloc();
! 215:
1.1.1.5 root 216: // Setup ivt/bda/ebda
1.1 root 217: init_ivt();
218: init_bda();
219:
220: // Init base pc hardware.
221: pic_setup();
222: timer_setup();
223: mathcp_setup();
224:
1.1.1.3 root 225: // Initialize mtrr
1.1 root 226: mtrr_setup();
227:
228: // Initialize pci
229: pci_setup();
230: smm_init();
231:
1.1.1.6 ! root 232: // Setup Xen hypercalls
! 233: xen_init_hypercalls();
! 234:
1.1.1.3 root 235: // Initialize internal tables
236: boot_setup();
237:
238: // Start hardware initialization (if optionrom threading)
239: if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
240: init_hw();
241:
242: // Find and initialize other cpus
243: smp_probe();
244:
1.1 root 245: // Setup interfaces that option roms may need
1.1.1.2 root 246: bios32_setup();
1.1 root 247: pmm_setup();
248: pnp_setup();
249: kbd_setup();
250: mouse_setup();
251: init_bios_tables();
252:
1.1.1.3 root 253: // Run vga option rom
254: vga_setup();
1.1 root 255:
1.1.1.3 root 256: // Do hardware initialization (if running synchronously)
257: if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
258: init_hw();
1.1 root 259: wait_threads();
260: }
261:
1.1.1.3 root 262: // Run option roms
263: optionrom_setup();
264:
1.1 root 265: // Run BCVs and show optional boot menu
266: boot_prep();
267:
268: // Finalize data structures before boot
1.1.1.3 root 269: cdemu_setup();
1.1 root 270: pmm_finalize();
271: malloc_finalize();
272: memmap_finalize();
1.1.1.5 root 273:
274: // Setup bios checksum.
275: BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
276:
277: // Write protect bios memory.
278: make_bios_readonly();
279:
280: // Invoke int 19 to start boot process.
281: startBoot();
282: }
283:
284:
285: /****************************************************************
1.1.1.6 ! root 286: * POST entry and code relocation
1.1.1.5 root 287: ****************************************************************/
288:
289: // Update given relocs for the code at 'dest' with a given 'delta'
290: static void
291: updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
292: {
293: u32 *reloc;
294: for (reloc = rstart; reloc < rend; reloc++)
295: *((u32*)(dest + *reloc)) += delta;
296: }
297:
1.1.1.6 ! root 298: // Relocate init code and then call maininit() at new address.
1.1.1.5 root 299: static void
300: reloc_init(void)
301: {
302: if (!CONFIG_RELOCATE_INIT) {
303: maininit();
304: return;
305: }
306: // Symbols populated by the build.
307: extern u8 code32flat_start[];
308: extern u8 _reloc_min_align[];
309: extern u32 _reloc_abs_start[], _reloc_abs_end[];
310: extern u32 _reloc_rel_start[], _reloc_rel_end[];
311: extern u32 _reloc_init_start[], _reloc_init_end[];
312: extern u8 code32init_start[], code32init_end[];
313:
314: // Allocate space for init code.
315: u32 initsize = code32init_end - code32init_start;
316: u32 align = (u32)&_reloc_min_align;
317: void *dest = memalign_tmp(align, initsize);
318: if (!dest)
319: panic("No space for init relocation.\n");
320:
321: // Copy code and update relocs (init absolute, init relative, and runtime)
322: dprintf(1, "Relocating init from %p to %p (size %d)\n"
323: , code32init_start, dest, initsize);
324: s32 delta = dest - (void*)code32init_start;
325: memcpy(dest, code32init_start, initsize);
326: updateRelocs(dest, _reloc_abs_start, _reloc_abs_end, delta);
327: updateRelocs(dest, _reloc_rel_start, _reloc_rel_end, -delta);
328: updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
329:
330: // Call maininit() in relocated code.
331: void (*func)(void) = (void*)maininit + delta;
332: barrier();
333: func();
334: }
335:
336: // Start of Power On Self Test (POST) - the BIOS initilization phase.
1.1.1.6 ! root 337: // This function does the setup needed for code relocation, and then
! 338: // invokes the relocation and main setup code.
1.1.1.5 root 339: void VISIBLE32INIT
1.1.1.6 ! root 340: handle_post(void)
1.1.1.5 root 341: {
1.1.1.6 ! root 342: debug_serial_setup();
! 343: dprintf(1, "Start bios (version %s)\n", VERSION);
1.1.1.5 root 344:
1.1.1.6 ! root 345: // Enable CPU caching
! 346: setcr0(getcr0() & ~(CR0_CD|CR0_NW));
1.1.1.5 root 347:
1.1.1.6 ! root 348: // Clear CMOS reboot flag.
! 349: outb_cmos(0, CMOS_RESET_CODE);
1.1 root 350:
1.1.1.6 ! root 351: // Make sure legacy DMA isn't running.
1.1 root 352: init_dma();
353:
1.1.1.6 ! root 354: // Check if we are running under Xen.
! 355: xen_probe();
1.1.1.5 root 356:
1.1 root 357: // Allow writes to modify bios area (0xf0000)
358: make_bios_writable();
1.1.1.5 root 359: HaveRunPost = 1;
1.1 root 360:
1.1.1.6 ! root 361: // Detect ram and setup internal malloc.
! 362: qemu_cfg_port_probe();
! 363: ram_probe();
! 364: malloc_setup();
! 365:
! 366: // Relocate initialization code and call maininit().
! 367: reloc_init();
1.1 root 368: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.