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