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