714664d667f8e6a6682b0e42ebbd1489fc2ca5c2
1 /*
2 * Copyright (c) 2014, Mentor Graphics Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of Mentor Graphics Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
30 #ifndef ELF_LOADER_H_
31 #define ELF_LOADER_H_
33 #include "openamp/remoteproc_loader.h"
35 /* ELF base types - 32-bit. */
36 typedef unsigned int Elf32_Addr;
37 typedef unsigned short Elf32_Half;
38 typedef unsigned int Elf32_Off;
39 typedef signed int Elf32_Sword;
40 typedef unsigned int Elf32_Word;
42 /* Size of ELF identifier field in the ELF file header. */
43 #define EI_NIDENT 16
45 /* ELF file header */
46 typedef struct {
47 unsigned char e_ident[EI_NIDENT];
48 Elf32_Half e_type;
49 Elf32_Half e_machine;
50 Elf32_Word e_version;
51 Elf32_Addr e_entry;
52 Elf32_Off e_phoff;
53 Elf32_Off e_shoff;
54 Elf32_Word e_flags;
55 Elf32_Half e_ehsize;
56 Elf32_Half e_phentsize;
57 Elf32_Half e_phnum;
58 Elf32_Half e_shentsize;
59 Elf32_Half e_shnum;
60 Elf32_Half e_shstrndx;
62 } Elf32_Ehdr;
64 /* e_ident */
65 #define ET_NONE 0
66 #define ET_REL 1 /* Re-locatable file */
67 #define ET_EXEC 2 /* Executable file */
68 #define ET_DYN 3 /* Shared object file */
69 #define ET_CORE 4 /* Core file */
70 #define ET_LOOS 0xfe00 /* Operating system-specific */
71 #define ET_HIOS 0xfeff /* Operating system-specific */
72 #define ET_LOPROC 0xff00 /* remote_proc-specific */
73 #define ET_HIPROC 0xffff /* remote_proc-specific */
75 /* e_machine */
76 #define EM_ARM 40 /* ARM/Thumb Architecture */
78 /* e_version */
79 #define EV_CURRENT 1 /* Current version */
81 /* e_ident[] Identification Indexes */
82 #define EI_MAG0 0 /* File identification */
83 #define EI_MAG1 1 /* File identification */
84 #define EI_MAG2 2 /* File identification */
85 #define EI_MAG3 3 /* File identification */
86 #define EI_CLASS 4 /* File class */
87 #define EI_DATA 5 /* Data encoding */
88 #define EI_VERSION 6 /* File version */
89 #define EI_OSABI 7 /* Operating system/ABI identification */
90 #define EI_ABIVERSION 8 /* ABI version */
91 #define EI_PAD 9 /* Start of padding bytes */
92 #define EI_NIDENT 16 /* Size of e_ident[] */
94 /* EI_MAG0 to EI_MAG3 - A file's first 4 bytes hold amagic number, identifying the file as an ELF object file */
95 #define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
96 #define ELFMAG1 'E' /* e_ident[EI_MAG1] */
97 #define ELFMAG2 'L' /* e_ident[EI_MAG2] */
98 #define ELFMAG3 'F' /* e_ident[EI_MAG3] */
100 /* EI_CLASS - The next byte, e_ident[EI_CLASS], identifies the file's class, or capacity. */
101 #define ELFCLASSNONE 0 /* Invalid class */
102 #define ELFCLASS32 1 /* 32-bit objects */
103 #define ELFCLASS64 2 /* 64-bit objects */
105 /* EI_DATA - Byte e_ident[EI_DATA] specifies the data encoding of the remote_proc-specific data in the object
106 file. The following encodings are currently defined. */
107 #define ELFDATANONE 0 /* Invalid data encoding */
108 #define ELFDATA2LSB 1 /* See Data encodings, below */
109 #define ELFDATA2MSB 2 /* See Data encodings, below */
111 /* EI_OSABI - We do not define an OS specific ABI */
112 #define ELFOSABI_NONE 0
114 /* ELF section header. */
115 typedef struct {
116 Elf32_Word sh_name;
117 Elf32_Word sh_type;
118 Elf32_Word sh_flags;
119 Elf32_Addr sh_addr;
120 Elf32_Off sh_offset;
121 Elf32_Word sh_size;
122 Elf32_Word sh_link;
123 Elf32_Word sh_info;
124 Elf32_Word sh_addralign;
125 Elf32_Word sh_entsize;
127 } Elf32_Shdr;
129 /* sh_type */
130 #define SHT_NULL 0
131 #define SHT_PROGBITS 1
132 #define SHT_SYMTAB 2
133 #define SHT_STRTAB 3
134 #define SHT_RELA 4
135 #define SHT_HASH 5
136 #define SHT_DYNAMIC 6
137 #define SHT_NOTE 7
138 #define SHT_NOBITS 8
139 #define SHT_REL 9
140 #define SHT_SHLIB 10
141 #define SHT_DYNSYM 11
142 #define SHT_INIT_ARRAY 14
143 #define SHT_FINI_ARRAY 15
144 #define SHT_PREINIT_ARRAY 16
145 #define SHT_GROUP 17
146 #define SHT_SYMTAB_SHNDX 18
147 #define SHT_LOOS 0x60000000
148 #define SHT_HIOS 0x6fffffff
149 #define SHT_LOPROC 0x70000000
150 #define SHT_HIPROC 0x7fffffff
151 #define SHT_LOUSER 0x80000000
152 #define SHT_HIUSER 0xffffffff
154 /* sh_flags */
155 #define SHF_WRITE 0x1
156 #define SHF_ALLOC 0x2
157 #define SHF_EXECINSTR 0x4
158 #define SHF_MASKPROC 0xf0000000
160 /* Relocation entry (without addend) */
161 typedef struct {
162 Elf32_Addr r_offset;
163 Elf32_Word r_info;
165 } Elf32_Rel;
167 /* Relocation entry with addend */
168 typedef struct {
169 Elf32_Addr r_offset;
170 Elf32_Word r_info;
171 Elf32_Sword r_addend;
173 } Elf32_Rela;
175 /* Macros to extract information from 'r_info' field of relocation entries */
176 #define ELF32_R_SYM(i) ((i)>>8)
177 #define ELF32_R_TYPE(i) ((unsigned char)(i))
179 /* Symbol table entry */
180 typedef struct {
181 Elf32_Word st_name;
182 Elf32_Addr st_value;
183 Elf32_Word st_size;
184 unsigned char st_info;
185 unsigned char st_other;
186 Elf32_Half st_shndx;
188 } Elf32_Sym;
190 /* ARM specific dynamic relocation codes */
191 #define R_ARM_GLOB_DAT 21 /* 0x15 */
192 #define R_ARM_JUMP_SLOT 22 /* 0x16 */
193 #define R_ARM_RELATIVE 23 /* 0x17 */
194 #define R_ARM_ABS32 2 /* 0x02 */
196 /* ELF decoding information */
197 struct elf_decode_info {
198 Elf32_Ehdr elf_header;
199 unsigned char *section_headers_start;
200 char *shstrtab;
202 Elf32_Shdr *dynsym;
203 Elf32_Shdr *dynstr;
204 Elf32_Shdr *rel_plt;
205 Elf32_Shdr *rel_dyn;
206 Elf32_Shdr *rsc;
208 unsigned char *dynsym_addr;
209 unsigned char *dynstr_addr;
211 char *firmware;
213 };
215 /* ELF Loader functions. */
216 int elf_loader_init(struct remoteproc_loader *loader);
217 void *elf_loader_retrieve_entry_point(struct remoteproc_loader *loader);
218 void *elf_loader_retrieve_resource_section(struct remoteproc_loader *loader,
219 unsigned int *size);
220 int elf_loader_load_remote_firmware(struct remoteproc_loader *loader);
221 int elf_loader_attach_firmware(struct remoteproc_loader *loader,
222 void *firmware);
223 int elf_loader_detach_firmware(struct remoteproc_loader *loader);
224 void *elf_get_load_address(struct remoteproc_loader *loader);
226 #endif /* ELF_LOADER_H_ */