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