]> Joshua Wise's Git repositories - netwatch.git/blob - grubload/multiboot_c.c
5e30dde991d41efa29074137b1ceb837503d83fd
[netwatch.git] / grubload / multiboot_c.c
1 #include "console.h"
2 #include "loader.h"
3 #include <output.h>
4 #include <minilib.h>
5 #include <io.h>
6 #include <smram.h>
7 #include <multiboot.h>
8 #include <smi.h>
9 #include <pci.h>
10
11 #define INFO_SIGNATURE 0x5754454E
12
13 extern char _binary_realmode_bin_start[];
14 extern int _binary_realmode_bin_size;
15
16 struct info_section
17 {
18         unsigned int signature;
19         void (*firstrun)();
20 };
21
22 void panic(const char *msg)
23 {
24         outputf("PANIC: %s\nSystem halted\n", msg);
25         while(1) { __asm__("hlt"); }
26 }
27
28 void c_start(unsigned int magic, struct mb_info *mbinfo)
29 {
30         struct mod_info *mods = mbinfo->mods;
31         smram_state_t old_smramc;
32         struct info_section * info;
33         int i;
34         
35         void (*realmode)() = (void (*)()) 0x4000;
36         
37         show_cursor();
38         outputf("NetWatch loader");
39         
40         if (magic != MULTIBOOT_LOADER_MAGIC)
41                 panic("Bootloader was not multiboot compliant; cannot continue.");
42         
43         for (i = 0; i < mbinfo->mod_cnt; i++)
44         {
45                 outputf("Module found:");
46                 outputf("  Start: %08x", (unsigned long) mods[i].mod_start);
47                 outputf("  Size: %08x", (unsigned long)mods[i].mod_end - (unsigned long)mods[i].mod_start);
48                 outputf("  Name: %s", mods[i].mod_string);
49         }
50
51         if (mbinfo->mod_cnt != 1)
52                 panic("Expected exactly one module; cannot continue.");
53         outputf("Current SMRAMC state is: %02x", pci_read8(0, 0, 0, 0x70));
54         outputf("Current USB state is: %04x %04x", pci_read16(0, 31, 2, 0xC0), pci_read16(0, 31, 4, 0xC0));
55         outputf("Current SMI state is: %08x", inl(0x830));
56         
57         smi_disable();
58         
59         /* Try really hard to shut up USB_LEGKEY. */
60         pci_write16(0, 31, 2, 0xC0, pci_read16(0, 31, 2, 0xC0));
61         pci_write16(0, 31, 2, 0xC0, 0);
62         pci_write16(0, 31, 4, 0xC0, pci_read16(0, 31, 4, 0xC0));
63         pci_write16(0, 31, 4, 0xC0, 0);
64 /*      
65         pci_bus_enum();
66 */
67         /* Open the SMRAM aperture and load our ELF. */
68         old_smramc = smram_save_state();
69
70         if (smram_aseg_set_state(SMRAM_ASEG_OPEN) != 0)
71                 panic("Opening SMRAM failed; cannot load ELF.");
72
73         load_elf(mods[0].mod_start, (unsigned long)mods[0].mod_end - (unsigned long)mods[0].mod_start);
74
75         info = (struct info_section *)0x10000;
76         if (info->signature != INFO_SIGNATURE)
77         {
78                 smram_restore_state(old_smramc);                /* Restore so that video ram is touchable again. */
79                 panic("Info section signature mismatch.");
80         }
81
82         info->firstrun();
83         smram_restore_state(old_smramc);
84         
85         outputf("New SMRAMC state is: %02x", pci_read8(0, 0, 0, 0x70));
86
87         puts("Waiting for a bit before returning to real mode...");
88         for (i=0; i<0x500000; i++)
89         {
90                 if ((i % 0x100000) == 0)
91                         puts(".");
92                 inb(0x80);
93         }
94         puts("\n");
95
96         outputf("Now returning to real mode.");
97         memcpy((void *)0x4000, _binary_realmode_bin_start, (int)&_binary_realmode_bin_size);
98         realmode();     // goodbye!
99 }
This page took 0.020524 seconds and 2 git commands to generate.