]> Joshua Wise's Git repositories - netwatch.git/blame - grubload/loader.c
Merge nyus.joshuawise.com:/storage/git/netwatch
[netwatch.git] / grubload / loader.c
CommitLineData
7e16b8e6 1#include <elf.h>
efea5b4e
JW
2#include <output.h>
3#include <minilib.h>
d56898ee 4
5static const unsigned char elf_ident[4] = { 0x7F, 'E', 'L', 'F' };
6
7int 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 */
efea5b4e 13 if (memcmp((void *)elf_hdr->e_ident, (void *)elf_ident, sizeof(elf_ident))) return -1;
d56898ee 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)) {
efea5b4e 34 outputf("Skipping %s", section_name);
d56898ee 35 continue;
36 }
37
efea5b4e 38 outputf("Loading %s at %08x", section_name, elf_sec_hdrs[i].sh_addr);
d56898ee 39
efea5b4e 40 memcpy((void *)elf_sec_hdrs[i].sh_addr,
d56898ee 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.022738 seconds and 4 git commands to generate.