]>
Commit | Line | Data |
---|---|---|
c7eda62d | 1 | /* main.c |
f1584bb0 JW |
2 | * Main program for starting SMM code |
3 | * NetWatch multiboot loader | |
4 | * | |
5 | * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. | |
6 | * This program is free software; you can redistribute and/or modify it under | |
7 | * the terms found in the file LICENSE in the root of this source tree. | |
8 | * | |
9 | */ | |
10 | ||
9b8c947b | 11 | #include "console.h" |
f2da2cd3 | 12 | #include "loader.h" |
efea5b4e | 13 | #include <output.h> |
85bc8ca6 | 14 | #include <minilib.h> |
60a917ef | 15 | #include <io.h> |
c34aba05 | 16 | #include <smram.h> |
94d78d15 | 17 | #include <multiboot.h> |
85bc8ca6 | 18 | #include <smi.h> |
f2da2cd3 | 19 | #include <pci.h> |
9b8c947b | 20 | |
e5d94488 JP |
21 | #define INFO_SIGNATURE 0x5754454E |
22 | ||
c4a16564 JW |
23 | extern char _binary_realmode_bin_start[]; |
24 | extern int _binary_realmode_bin_size; | |
25 | ||
e5d94488 JP |
26 | struct info_section |
27 | { | |
28 | unsigned int signature; | |
29 | void (*firstrun)(); | |
30 | }; | |
31 | ||
e997a89b | 32 | void panic(const char *msg) |
9b8c947b | 33 | { |
efea5b4e | 34 | outputf("PANIC: %s\nSystem halted\n", msg); |
f2da2cd3 | 35 | while(1) { __asm__("hlt"); } |
e997a89b JW |
36 | } |
37 | ||
38 | void c_start(unsigned int magic, struct mb_info *mbinfo) | |
39 | { | |
40 | struct mod_info *mods = mbinfo->mods; | |
f2da2cd3 JP |
41 | smram_state_t old_smramc; |
42 | struct info_section * info; | |
9b8c947b JW |
43 | int i; |
44 | ||
7e16b8e6 | 45 | void (*realmode)() = (void (*)()) 0x4000; |
c4a16564 | 46 | |
9b8c947b | 47 | show_cursor(); |
efea5b4e | 48 | outputf("NetWatch loader"); |
81148fa1 | 49 | |
94d78d15 | 50 | if (magic != MULTIBOOT_LOADER_MAGIC) |
e997a89b | 51 | panic("Bootloader was not multiboot compliant; cannot continue."); |
9b8c947b | 52 | |
e997a89b | 53 | for (i = 0; i < mbinfo->mod_cnt; i++) |
9b8c947b | 54 | { |
efea5b4e JW |
55 | outputf("Module found:"); |
56 | outputf(" Start: %08x", (unsigned long) mods[i].mod_start); | |
57 | outputf(" Size: %08x", (unsigned long)mods[i].mod_end - (unsigned long)mods[i].mod_start); | |
58 | outputf(" Name: %s", mods[i].mod_string); | |
9b8c947b | 59 | } |
c4a16564 | 60 | |
e997a89b JW |
61 | if (mbinfo->mod_cnt != 1) |
62 | panic("Expected exactly one module; cannot continue."); | |
2768393f JW |
63 | outputf("Current SMRAMC state is: %02x", (unsigned char)smram_save_state()); |
64 | outputf("Current SMI state is: %08x", inl(0x830)); // XXX ICH2 specific | |
f2b87dd6 | 65 | |
85bc8ca6 | 66 | smi_disable(); |
60a917ef | 67 | |
c34aba05 | 68 | /* Open the SMRAM aperture and load our ELF. */ |
f2da2cd3 | 69 | old_smramc = smram_save_state(); |
c34aba05 JP |
70 | |
71 | if (smram_aseg_set_state(SMRAM_ASEG_OPEN) != 0) | |
e997a89b JW |
72 | panic("Opening SMRAM failed; cannot load ELF."); |
73 | ||
f2da2cd3 | 74 | load_elf(mods[0].mod_start, (unsigned long)mods[0].mod_end - (unsigned long)mods[0].mod_start); |
e997a89b | 75 | |
f2da2cd3 | 76 | info = (struct info_section *)0x10000; |
e997a89b | 77 | if (info->signature != INFO_SIGNATURE) |
c34aba05 | 78 | { |
e997a89b JW |
79 | smram_restore_state(old_smramc); /* Restore so that video ram is touchable again. */ |
80 | panic("Info section signature mismatch."); | |
c34aba05 JP |
81 | } |
82 | ||
e997a89b JW |
83 | info->firstrun(); |
84 | smram_restore_state(old_smramc); | |
f2b87dd6 | 85 | |
2768393f | 86 | outputf("New SMRAMC state is: %02x", (unsigned char)smram_save_state()); |
e997a89b | 87 | |
60a917ef JW |
88 | puts("Waiting for a bit before returning to real mode..."); |
89 | for (i=0; i<0x500000; i++) | |
90 | { | |
91 | if ((i % 0x100000) == 0) | |
92 | puts("."); | |
93 | inb(0x80); | |
94 | } | |
95 | puts("\n"); | |
d56898ee | 96 | |
efea5b4e | 97 | outputf("Now returning to real mode."); |
f2da2cd3 | 98 | memcpy((void *)0x4000, _binary_realmode_bin_start, (int)&_binary_realmode_bin_size); |
86c89e89 | 99 | realmode(); // goodbye! |
56553b73 | 100 | } |