]> Joshua Wise's Git repositories - netwatch.git/blob - grubload/loader.c
Make the log longer, and make it faster to write to.
[netwatch.git] / grubload / loader.c
1 #include <elf.h>
2 #include <output.h>
3 #include <minilib.h>
4
5 static const unsigned char elf_ident[4] = { 0x7F, 'E', 'L', 'F' }; 
6
7 int load_elf (char * buf, int size) {
8
9         Elf32_Ehdr * elf_hdr = (Elf32_Ehdr *) buf;
10         Elf32_Shdr * elf_sec_hdrs = (Elf32_Shdr *) (buf + elf_hdr->e_shoff);
11
12         /* Sanity check on ELF file */
13         if (memcmp((void *)elf_hdr->e_ident, (void *)elf_ident, sizeof(elf_ident))) return -1;
14         if (elf_hdr->e_type != ET_EXEC) return -1;
15         if (elf_hdr->e_machine != EM_386) return -1;
16         if (elf_hdr->e_version != EV_CURRENT) return -1;
17         if (size < sizeof(Elf32_Ehdr)) return -1;
18         if (((char *)&elf_sec_hdrs[elf_hdr->e_shnum]) > (buf + size)) return -1;
19
20         char * string_table = buf + elf_sec_hdrs[elf_hdr->e_shstrndx].sh_offset;
21
22         if (string_table > (buf + size)) return -1;
23
24         int i;
25         for (i = 0; i < elf_hdr->e_shnum; i++) {
26
27                 if (elf_sec_hdrs[i].sh_name == SHN_UNDEF) {
28                         continue;
29                 }
30
31                 char * section_name = string_table + elf_sec_hdrs[i].sh_name;
32
33                 if ((elf_sec_hdrs[i].sh_type != SHT_PROGBITS) || !(elf_sec_hdrs[i].sh_flags & SHF_ALLOC)) {
34                         outputf("Skipping %s", section_name);
35                         continue;
36                 }
37
38                 outputf("Loading %s at %08x", section_name, elf_sec_hdrs[i].sh_addr);
39
40                 memcpy((void *)elf_sec_hdrs[i].sh_addr,
41                        buf + elf_sec_hdrs[i].sh_offset,
42                        elf_sec_hdrs[i].sh_size);
43         }
44
45         return 0;
46 }
This page took 0.030996 seconds and 4 git commands to generate.