OSDN Git Service

Moved the junk codes to junk directory.
[kozos-expbrd/kozos_expbrd.git] / firm / junk / 01 / bootload / elf.c
1 #include "defines.h"
2 #include "elf.h"
3 #include "lib.h"
4
5 struct elf_header {
6   struct {
7     unsigned char magic[4];
8     unsigned char class;
9     unsigned char format;
10     unsigned char version;
11     unsigned char abi;
12     unsigned char abi_version;
13     unsigned char reserve[7];
14   } id;
15   short type;
16   short arch;
17   long version;
18   long entry_point;
19   long program_header_offset;
20   long section_header_offset;
21   long flags;
22   short header_size;
23   short program_header_size;
24   short program_header_num;
25   short section_header_size;
26   short section_header_num;
27   short section_name_index;
28 };
29
30 struct elf_program_header {
31   long type;
32   long offset;
33   long virtual_addr;
34   long physical_addr;
35   long file_size;
36   long memory_size;
37   long flags;
38   long align;
39 };
40
41 /* ELF¥Ø¥Ã¥À¤Î¥Á¥§¥Ã¥¯ */
42 static int elf_check(struct elf_header *header)
43 {
44   if (memcmp(header->id.magic, "\x7f" "ELF", 4))
45     return -1;
46
47   if (header->id.class   != 1) return -1; /* ELF32 */
48   if (header->id.format  != 2) return -1; /* Big endian */
49   if (header->id.version != 1) return -1; /* version 1 */
50   if (header->type       != 2) return -1; /* Executable file */
51   if (header->version    != 1) return -1; /* version 1 */
52
53   /* Hitachi H8/300 or H8/300H */
54   if ((header->arch != 46) && (header->arch != 47)) return -1;
55
56   return 0;
57 }
58
59 /* ¥»¥°¥á¥ó¥Èñ°Ì¤Ç¤Î¥í¡¼¥É */
60 static int elf_load_program(struct elf_header *header)
61 {
62   int i;
63   struct elf_program_header *phdr;
64
65   for (i = 0; i < header->program_header_num; i++) {
66     /* ¥×¥í¥°¥é¥à¡¦¥Ø¥Ã¥À¤ò¼èÆÀ */
67     phdr = (struct elf_program_header *)
68       ((char *)header + header->program_header_offset +
69        header->program_header_size * i);
70
71     if (phdr->type != 1) /* ¥í¡¼¥É²Äǽ¤Ê¥»¥°¥á¥ó¥È¤«¡© */
72       continue;
73
74     memcpy((char *)phdr->physical_addr, (char *)header + phdr->offset,
75            phdr->file_size);
76     memset((char *)phdr->physical_addr + phdr->file_size, 0,
77            phdr->memory_size - phdr->file_size);
78   }
79
80   return 0;
81 }
82
83 char *elf_load(char *buf)
84 {
85   struct elf_header *header = (struct elf_header *)buf;
86
87   if (elf_check(header) < 0) /* ELF¥Ø¥Ã¥À¤Î¥Á¥§¥Ã¥¯ */
88     return NULL;
89
90   if (elf_load_program(header) < 0) /* ¥»¥°¥á¥ó¥Èñ°Ì¤Ç¤Î¥í¡¼¥É */
91     return NULL;
92
93   return (char *)header->entry_point;
94 }