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