2 * Main program for starting SMM code
3 * NetWatch multiboot loader
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.
17 #include <multiboot.h>
21 #define INFO_SIGNATURE 0x5754454E
23 extern char _binary_realmode_bin_start[];
24 extern int _binary_realmode_bin_size;
28 unsigned int signature;
32 void panic(const char *msg)
34 outputf("PANIC: %s\nSystem halted\n", msg);
35 while(1) { __asm__("hlt"); }
38 void c_start(unsigned int magic, struct mb_info *mbinfo)
40 struct mod_info *mods = mbinfo->mods;
41 smram_state_t old_smramc;
42 struct info_section * info;
45 void (*realmode)() = (void (*)()) 0x4000;
48 outputf("NetWatch loader");
50 if (magic != MULTIBOOT_LOADER_MAGIC)
51 panic("Bootloader was not multiboot compliant; cannot continue.");
53 for (i = 0; i < mbinfo->mod_cnt; i++)
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);
61 if (mbinfo->mod_cnt != 1)
62 panic("Expected exactly one module; cannot continue.");
63 outputf("Current SMRAMC state is: %02x", (unsigned char)smram_save_state());
64 outputf("Current SMI state is: %08x", inl(0x830)); // XXX ICH2 specific
68 /* Open the SMRAM aperture and load our ELF. */
69 old_smramc = smram_save_state();
71 if (smram_aseg_set_state(SMRAM_ASEG_OPEN) != 0)
72 panic("Opening SMRAM failed; cannot load ELF.");
74 load_elf(mods[0].mod_start, (unsigned long)mods[0].mod_end - (unsigned long)mods[0].mod_start);
76 info = (struct info_section *)0x10000;
77 if (info->signature != INFO_SIGNATURE)
79 smram_restore_state(old_smramc); /* Restore so that video ram is touchable again. */
80 panic("Info section signature mismatch.");
84 smram_restore_state(old_smramc);
86 outputf("New SMRAMC state is: %02x", (unsigned char)smram_save_state());
88 puts("Waiting for a bit before returning to real mode...");
89 for (i=0; i<0x500000; i++)
91 if ((i % 0x100000) == 0)
97 outputf("Now returning to real mode.");
98 memcpy((void *)0x4000, _binary_realmode_bin_start, (int)&_binary_realmode_bin_size);
99 realmode(); // goodbye!