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