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