root / loader.c @ 9c22a623
History | View | Annotate | Download (13.5 kB)
1 | 5fe141fd | bellard | /*
|
---|---|---|---|
2 | 5fe141fd | bellard | * QEMU Executable loader
|
3 | 5fafdf24 | ths | *
|
4 | 5fe141fd | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 5fe141fd | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 5fe141fd | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 5fe141fd | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 5fe141fd | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 5fe141fd | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 5fe141fd | bellard | * furnished to do so, subject to the following conditions:
|
12 | 5fe141fd | bellard | *
|
13 | 5fe141fd | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 5fe141fd | bellard | * all copies or substantial portions of the Software.
|
15 | 5fe141fd | bellard | *
|
16 | 5fe141fd | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 5fe141fd | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 5fe141fd | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 5fe141fd | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 5fe141fd | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 5fe141fd | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 5fe141fd | bellard | * THE SOFTWARE.
|
23 | 5a123577 | aliguori | *
|
24 | 5a123577 | aliguori | * Gunzip functionality in this file is derived from u-boot:
|
25 | 5a123577 | aliguori | *
|
26 | 5a123577 | aliguori | * (C) Copyright 2008 Semihalf
|
27 | 5a123577 | aliguori | *
|
28 | 5a123577 | aliguori | * (C) Copyright 2000-2005
|
29 | 5a123577 | aliguori | * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
30 | 5a123577 | aliguori | *
|
31 | 5a123577 | aliguori | * This program is free software; you can redistribute it and/or
|
32 | 5a123577 | aliguori | * modify it under the terms of the GNU General Public License as
|
33 | 5a123577 | aliguori | * published by the Free Software Foundation; either version 2 of
|
34 | 5a123577 | aliguori | * the License, or (at your option) any later version.
|
35 | 5a123577 | aliguori | *
|
36 | 5a123577 | aliguori | * This program is distributed in the hope that it will be useful,
|
37 | 5a123577 | aliguori | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
38 | 5a123577 | aliguori | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
39 | 5a123577 | aliguori | * GNU General Public License for more details.
|
40 | 5a123577 | aliguori | *
|
41 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
42 | fad6cb1a | aurel32 | * with this program; if not, write to the Free Software Foundation, Inc.,
|
43 | fad6cb1a | aurel32 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
44 | 5fe141fd | bellard | */
|
45 | 5a123577 | aliguori | |
46 | 87ecb68b | pbrook | #include "qemu-common.h" |
47 | 5fe141fd | bellard | #include "disas.h" |
48 | 9596ebb7 | pbrook | #include "sysemu.h" |
49 | 1c7b3754 | pbrook | #include "uboot_image.h" |
50 | 5fe141fd | bellard | |
51 | 5a123577 | aliguori | #include <zlib.h> |
52 | 5a123577 | aliguori | |
53 | 5fe141fd | bellard | /* return the size or -1 if error */
|
54 | 5fe141fd | bellard | int get_image_size(const char *filename) |
55 | 5fe141fd | bellard | { |
56 | 5fe141fd | bellard | int fd, size;
|
57 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
58 | 5fe141fd | bellard | if (fd < 0) |
59 | 5fe141fd | bellard | return -1; |
60 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
61 | 5fe141fd | bellard | close(fd); |
62 | 5fe141fd | bellard | return size;
|
63 | 5fe141fd | bellard | } |
64 | 5fe141fd | bellard | |
65 | 5fe141fd | bellard | /* return the size or -1 if error */
|
66 | 293f78bc | blueswir1 | /* deprecated, because caller does not specify buffer size! */
|
67 | 5fe141fd | bellard | int load_image(const char *filename, uint8_t *addr) |
68 | 5fe141fd | bellard | { |
69 | 5fe141fd | bellard | int fd, size;
|
70 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
71 | 5fe141fd | bellard | if (fd < 0) |
72 | 5fe141fd | bellard | return -1; |
73 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
74 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
75 | 5fe141fd | bellard | if (read(fd, addr, size) != size) {
|
76 | 5fe141fd | bellard | close(fd); |
77 | 5fe141fd | bellard | return -1; |
78 | 5fe141fd | bellard | } |
79 | 5fe141fd | bellard | close(fd); |
80 | 5fe141fd | bellard | return size;
|
81 | 5fe141fd | bellard | } |
82 | 5fe141fd | bellard | |
83 | 293f78bc | blueswir1 | /* return the amount read, just like fread. 0 may mean error or eof */
|
84 | 293f78bc | blueswir1 | int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
|
85 | 293f78bc | blueswir1 | { |
86 | 293f78bc | blueswir1 | uint8_t buf[4096];
|
87 | 293f78bc | blueswir1 | target_phys_addr_t dst_begin = dst_addr; |
88 | 293f78bc | blueswir1 | size_t want, did; |
89 | 293f78bc | blueswir1 | |
90 | 293f78bc | blueswir1 | while (nbytes) {
|
91 | 293f78bc | blueswir1 | want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes; |
92 | 293f78bc | blueswir1 | did = fread(buf, 1, want, f);
|
93 | 293f78bc | blueswir1 | |
94 | 293f78bc | blueswir1 | cpu_physical_memory_write_rom(dst_addr, buf, did); |
95 | 293f78bc | blueswir1 | dst_addr += did; |
96 | 293f78bc | blueswir1 | nbytes -= did; |
97 | 7d515c1d | blueswir1 | if (did != want)
|
98 | 7d515c1d | blueswir1 | break;
|
99 | 293f78bc | blueswir1 | } |
100 | 293f78bc | blueswir1 | return dst_addr - dst_begin;
|
101 | 293f78bc | blueswir1 | } |
102 | 293f78bc | blueswir1 | |
103 | 293f78bc | blueswir1 | /* returns 0 on error, 1 if ok */
|
104 | 293f78bc | blueswir1 | int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
|
105 | 293f78bc | blueswir1 | { |
106 | 293f78bc | blueswir1 | return fread_targphys(dst_addr, nbytes, f) == nbytes;
|
107 | 293f78bc | blueswir1 | } |
108 | 293f78bc | blueswir1 | |
109 | 293f78bc | blueswir1 | /* read()-like version */
|
110 | 293f78bc | blueswir1 | int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes) |
111 | 293f78bc | blueswir1 | { |
112 | 293f78bc | blueswir1 | uint8_t buf[4096];
|
113 | 293f78bc | blueswir1 | target_phys_addr_t dst_begin = dst_addr; |
114 | 293f78bc | blueswir1 | size_t want, did; |
115 | 293f78bc | blueswir1 | |
116 | 293f78bc | blueswir1 | while (nbytes) {
|
117 | 293f78bc | blueswir1 | want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes; |
118 | 293f78bc | blueswir1 | did = read(fd, buf, want); |
119 | 293f78bc | blueswir1 | if (did != want) break; |
120 | 293f78bc | blueswir1 | |
121 | 293f78bc | blueswir1 | cpu_physical_memory_write_rom(dst_addr, buf, did); |
122 | 293f78bc | blueswir1 | dst_addr += did; |
123 | 293f78bc | blueswir1 | nbytes -= did; |
124 | 293f78bc | blueswir1 | } |
125 | 293f78bc | blueswir1 | return dst_addr - dst_begin;
|
126 | 293f78bc | blueswir1 | } |
127 | 293f78bc | blueswir1 | |
128 | 293f78bc | blueswir1 | /* return the size or -1 if error */
|
129 | 293f78bc | blueswir1 | int load_image_targphys(const char *filename, |
130 | 293f78bc | blueswir1 | target_phys_addr_t addr, int max_sz)
|
131 | 293f78bc | blueswir1 | { |
132 | 293f78bc | blueswir1 | FILE *f; |
133 | 293f78bc | blueswir1 | size_t got; |
134 | 293f78bc | blueswir1 | |
135 | 293f78bc | blueswir1 | f = fopen(filename, "rb");
|
136 | 293f78bc | blueswir1 | if (!f) return -1; |
137 | 293f78bc | blueswir1 | |
138 | 293f78bc | blueswir1 | got = fread_targphys(addr, max_sz, f); |
139 | 293f78bc | blueswir1 | if (ferror(f)) { fclose(f); return -1; } |
140 | 293f78bc | blueswir1 | fclose(f); |
141 | 293f78bc | blueswir1 | |
142 | 293f78bc | blueswir1 | return got;
|
143 | 293f78bc | blueswir1 | } |
144 | 293f78bc | blueswir1 | |
145 | 293f78bc | blueswir1 | void pstrcpy_targphys(target_phys_addr_t dest, int buf_size, |
146 | 293f78bc | blueswir1 | const char *source) |
147 | 293f78bc | blueswir1 | { |
148 | 293f78bc | blueswir1 | static const uint8_t nul_byte = 0; |
149 | 293f78bc | blueswir1 | const char *nulp; |
150 | 293f78bc | blueswir1 | |
151 | 293f78bc | blueswir1 | if (buf_size <= 0) return; |
152 | 293f78bc | blueswir1 | nulp = memchr(source, 0, buf_size);
|
153 | 293f78bc | blueswir1 | if (nulp) {
|
154 | 293f78bc | blueswir1 | cpu_physical_memory_write_rom(dest, (uint8_t *)source, |
155 | 293f78bc | blueswir1 | (nulp - source) + 1);
|
156 | 293f78bc | blueswir1 | } else {
|
157 | 293f78bc | blueswir1 | cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
|
158 | 293f78bc | blueswir1 | cpu_physical_memory_write_rom(dest, &nul_byte, 1);
|
159 | 293f78bc | blueswir1 | } |
160 | 293f78bc | blueswir1 | } |
161 | 293f78bc | blueswir1 | |
162 | 5fe141fd | bellard | /* A.OUT loader */
|
163 | 5fe141fd | bellard | |
164 | 5fe141fd | bellard | struct exec
|
165 | 5fe141fd | bellard | { |
166 | 5fe141fd | bellard | uint32_t a_info; /* Use macros N_MAGIC, etc for access */
|
167 | 5fe141fd | bellard | uint32_t a_text; /* length of text, in bytes */
|
168 | 5fe141fd | bellard | uint32_t a_data; /* length of data, in bytes */
|
169 | 5fe141fd | bellard | uint32_t a_bss; /* length of uninitialized data area, in bytes */
|
170 | 5fe141fd | bellard | uint32_t a_syms; /* length of symbol table data in file, in bytes */
|
171 | 5fe141fd | bellard | uint32_t a_entry; /* start address */
|
172 | 5fe141fd | bellard | uint32_t a_trsize; /* length of relocation info for text, in bytes */
|
173 | 5fe141fd | bellard | uint32_t a_drsize; /* length of relocation info for data, in bytes */
|
174 | 5fe141fd | bellard | }; |
175 | 5fe141fd | bellard | |
176 | 5fe141fd | bellard | #ifdef BSWAP_NEEDED
|
177 | 5fe141fd | bellard | static void bswap_ahdr(struct exec *e) |
178 | 5fe141fd | bellard | { |
179 | 5fe141fd | bellard | bswap32s(&e->a_info); |
180 | 5fe141fd | bellard | bswap32s(&e->a_text); |
181 | 5fe141fd | bellard | bswap32s(&e->a_data); |
182 | 5fe141fd | bellard | bswap32s(&e->a_bss); |
183 | 5fe141fd | bellard | bswap32s(&e->a_syms); |
184 | 5fe141fd | bellard | bswap32s(&e->a_entry); |
185 | 5fe141fd | bellard | bswap32s(&e->a_trsize); |
186 | 5fe141fd | bellard | bswap32s(&e->a_drsize); |
187 | 5fe141fd | bellard | } |
188 | 5fe141fd | bellard | #else
|
189 | 5fe141fd | bellard | #define bswap_ahdr(x) do { } while (0) |
190 | 5fe141fd | bellard | #endif
|
191 | 5fe141fd | bellard | |
192 | 5fe141fd | bellard | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
193 | 5fe141fd | bellard | #define OMAGIC 0407 |
194 | 5fe141fd | bellard | #define NMAGIC 0410 |
195 | 5fe141fd | bellard | #define ZMAGIC 0413 |
196 | 5fe141fd | bellard | #define QMAGIC 0314 |
197 | 5fe141fd | bellard | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
198 | 5fe141fd | bellard | #define N_TXTOFF(x) \
|
199 | 5fe141fd | bellard | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
200 | 5fe141fd | bellard | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
201 | 5fe141fd | bellard | #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0) |
202 | 5fe141fd | bellard | #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1)) |
203 | 5fe141fd | bellard | |
204 | 5fe141fd | bellard | #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
|
205 | 5fe141fd | bellard | |
206 | 5fe141fd | bellard | #define N_DATADDR(x) \
|
207 | 5fe141fd | bellard | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \ |
208 | 5fe141fd | bellard | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x)))) |
209 | 5fe141fd | bellard | |
210 | 5fe141fd | bellard | |
211 | 293f78bc | blueswir1 | int load_aout(const char *filename, target_phys_addr_t addr, int max_sz) |
212 | 5fe141fd | bellard | { |
213 | 5fe141fd | bellard | int fd, size, ret;
|
214 | 5fe141fd | bellard | struct exec e;
|
215 | 5fe141fd | bellard | uint32_t magic; |
216 | 5fe141fd | bellard | |
217 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
218 | 5fe141fd | bellard | if (fd < 0) |
219 | 5fe141fd | bellard | return -1; |
220 | 5fe141fd | bellard | |
221 | 5fe141fd | bellard | size = read(fd, &e, sizeof(e));
|
222 | 5fe141fd | bellard | if (size < 0) |
223 | 5fe141fd | bellard | goto fail;
|
224 | 5fe141fd | bellard | |
225 | 5fe141fd | bellard | bswap_ahdr(&e); |
226 | 5fe141fd | bellard | |
227 | 5fe141fd | bellard | magic = N_MAGIC(e); |
228 | 5fe141fd | bellard | switch (magic) {
|
229 | 5fe141fd | bellard | case ZMAGIC:
|
230 | 5fe141fd | bellard | case QMAGIC:
|
231 | 5fe141fd | bellard | case OMAGIC:
|
232 | 293f78bc | blueswir1 | if (e.a_text + e.a_data > max_sz)
|
233 | 293f78bc | blueswir1 | goto fail;
|
234 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
235 | 293f78bc | blueswir1 | size = read_targphys(fd, addr, e.a_text + e.a_data); |
236 | 5fe141fd | bellard | if (size < 0) |
237 | 5fe141fd | bellard | goto fail;
|
238 | 5fe141fd | bellard | break;
|
239 | 5fe141fd | bellard | case NMAGIC:
|
240 | 293f78bc | blueswir1 | if (N_DATADDR(e) + e.a_data > max_sz)
|
241 | 293f78bc | blueswir1 | goto fail;
|
242 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
243 | 293f78bc | blueswir1 | size = read_targphys(fd, addr, e.a_text); |
244 | 5fe141fd | bellard | if (size < 0) |
245 | 5fe141fd | bellard | goto fail;
|
246 | 293f78bc | blueswir1 | ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data); |
247 | 5fe141fd | bellard | if (ret < 0) |
248 | 5fe141fd | bellard | goto fail;
|
249 | 5fe141fd | bellard | size += ret; |
250 | 5fe141fd | bellard | break;
|
251 | 5fe141fd | bellard | default:
|
252 | 5fe141fd | bellard | goto fail;
|
253 | 5fe141fd | bellard | } |
254 | 5fe141fd | bellard | close(fd); |
255 | 5fe141fd | bellard | return size;
|
256 | 5fe141fd | bellard | fail:
|
257 | 5fe141fd | bellard | close(fd); |
258 | 5fe141fd | bellard | return -1; |
259 | 5fe141fd | bellard | } |
260 | 5fe141fd | bellard | |
261 | 5fe141fd | bellard | /* ELF loader */
|
262 | 5fe141fd | bellard | |
263 | 5fe141fd | bellard | static void *load_at(int fd, int offset, int size) |
264 | 5fe141fd | bellard | { |
265 | 5fe141fd | bellard | void *ptr;
|
266 | 5fe141fd | bellard | if (lseek(fd, offset, SEEK_SET) < 0) |
267 | 5fe141fd | bellard | return NULL; |
268 | 5fe141fd | bellard | ptr = qemu_malloc(size); |
269 | 5fe141fd | bellard | if (read(fd, ptr, size) != size) {
|
270 | 5fe141fd | bellard | qemu_free(ptr); |
271 | 5fe141fd | bellard | return NULL; |
272 | 5fe141fd | bellard | } |
273 | 5fe141fd | bellard | return ptr;
|
274 | 5fe141fd | bellard | } |
275 | 5fe141fd | bellard | |
276 | 5fe141fd | bellard | |
277 | 5fe141fd | bellard | #define ELF_CLASS ELFCLASS32
|
278 | 5fe141fd | bellard | #include "elf.h" |
279 | 5fe141fd | bellard | |
280 | 5fe141fd | bellard | #define SZ 32 |
281 | 5fe141fd | bellard | #define elf_word uint32_t
|
282 | 82790064 | ths | #define elf_sword int32_t
|
283 | 5fe141fd | bellard | #define bswapSZs bswap32s
|
284 | 5fe141fd | bellard | #include "elf_ops.h" |
285 | 5fe141fd | bellard | |
286 | 5fe141fd | bellard | #undef elfhdr
|
287 | 5fe141fd | bellard | #undef elf_phdr
|
288 | 5fe141fd | bellard | #undef elf_shdr
|
289 | 5fe141fd | bellard | #undef elf_sym
|
290 | 5fe141fd | bellard | #undef elf_note
|
291 | 5fe141fd | bellard | #undef elf_word
|
292 | 82790064 | ths | #undef elf_sword
|
293 | 5fe141fd | bellard | #undef bswapSZs
|
294 | 5fe141fd | bellard | #undef SZ
|
295 | 5fe141fd | bellard | #define elfhdr elf64_hdr
|
296 | 5fe141fd | bellard | #define elf_phdr elf64_phdr
|
297 | 5fe141fd | bellard | #define elf_note elf64_note
|
298 | 5fe141fd | bellard | #define elf_shdr elf64_shdr
|
299 | 5fe141fd | bellard | #define elf_sym elf64_sym
|
300 | 5fe141fd | bellard | #define elf_word uint64_t
|
301 | 82790064 | ths | #define elf_sword int64_t
|
302 | 5fe141fd | bellard | #define bswapSZs bswap64s
|
303 | 5fe141fd | bellard | #define SZ 64 |
304 | 5fe141fd | bellard | #include "elf_ops.h" |
305 | 5fe141fd | bellard | |
306 | 5fe141fd | bellard | /* return < 0 if error, otherwise the number of bytes loaded in memory */
|
307 | 83c1f87c | pbrook | int load_elf(const char *filename, int64_t address_offset, |
308 | 74287114 | ths | uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr) |
309 | 5fe141fd | bellard | { |
310 | 9042c0e2 | ths | int fd, data_order, host_data_order, must_swab, ret;
|
311 | 5fe141fd | bellard | uint8_t e_ident[EI_NIDENT]; |
312 | 5fe141fd | bellard | |
313 | 699e4642 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
314 | 5fe141fd | bellard | if (fd < 0) { |
315 | 5fe141fd | bellard | perror(filename); |
316 | 5fe141fd | bellard | return -1; |
317 | 5fe141fd | bellard | } |
318 | 5fe141fd | bellard | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
319 | 5fe141fd | bellard | goto fail;
|
320 | 5fe141fd | bellard | if (e_ident[0] != ELFMAG0 || |
321 | 5fe141fd | bellard | e_ident[1] != ELFMAG1 ||
|
322 | 5fe141fd | bellard | e_ident[2] != ELFMAG2 ||
|
323 | 5fe141fd | bellard | e_ident[3] != ELFMAG3)
|
324 | 5fe141fd | bellard | goto fail;
|
325 | 5fe141fd | bellard | #ifdef WORDS_BIGENDIAN
|
326 | 5fe141fd | bellard | data_order = ELFDATA2MSB; |
327 | 5fe141fd | bellard | #else
|
328 | 5fe141fd | bellard | data_order = ELFDATA2LSB; |
329 | 5fe141fd | bellard | #endif
|
330 | 5fe141fd | bellard | must_swab = data_order != e_ident[EI_DATA]; |
331 | 9042c0e2 | ths | |
332 | 9042c0e2 | ths | #ifdef TARGET_WORDS_BIGENDIAN
|
333 | 9042c0e2 | ths | host_data_order = ELFDATA2MSB; |
334 | 9042c0e2 | ths | #else
|
335 | 9042c0e2 | ths | host_data_order = ELFDATA2LSB; |
336 | 9042c0e2 | ths | #endif
|
337 | 9042c0e2 | ths | if (host_data_order != e_ident[EI_DATA])
|
338 | 9042c0e2 | ths | return -1; |
339 | 9042c0e2 | ths | |
340 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
341 | 5fe141fd | bellard | if (e_ident[EI_CLASS] == ELFCLASS64) {
|
342 | 83c1f87c | pbrook | ret = load_elf64(fd, address_offset, must_swab, pentry, |
343 | 74287114 | ths | lowaddr, highaddr); |
344 | 5fe141fd | bellard | } else {
|
345 | 83c1f87c | pbrook | ret = load_elf32(fd, address_offset, must_swab, pentry, |
346 | 74287114 | ths | lowaddr, highaddr); |
347 | 5fe141fd | bellard | } |
348 | 5fe141fd | bellard | |
349 | 5fe141fd | bellard | close(fd); |
350 | 5fe141fd | bellard | return ret;
|
351 | 5fe141fd | bellard | |
352 | 5fe141fd | bellard | fail:
|
353 | 5fe141fd | bellard | close(fd); |
354 | 5fe141fd | bellard | return -1; |
355 | 5fe141fd | bellard | } |
356 | 1c7b3754 | pbrook | |
357 | 1c7b3754 | pbrook | static void bswap_uboot_header(uboot_image_header_t *hdr) |
358 | 1c7b3754 | pbrook | { |
359 | 1c7b3754 | pbrook | #ifndef WORDS_BIGENDIAN
|
360 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_magic); |
361 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_hcrc); |
362 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_time); |
363 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_size); |
364 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_load); |
365 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_ep); |
366 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_dcrc); |
367 | 1c7b3754 | pbrook | #endif
|
368 | 1c7b3754 | pbrook | } |
369 | 1c7b3754 | pbrook | |
370 | 5a123577 | aliguori | |
371 | 5a123577 | aliguori | #define ZALLOC_ALIGNMENT 16 |
372 | 5a123577 | aliguori | |
373 | 5a123577 | aliguori | static void *zalloc(void *x, unsigned items, unsigned size) |
374 | 5a123577 | aliguori | { |
375 | 5a123577 | aliguori | void *p;
|
376 | 5a123577 | aliguori | |
377 | 5a123577 | aliguori | size *= items; |
378 | 5a123577 | aliguori | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); |
379 | 5a123577 | aliguori | |
380 | 5a123577 | aliguori | p = qemu_malloc(size); |
381 | 5a123577 | aliguori | |
382 | 5a123577 | aliguori | return (p);
|
383 | 5a123577 | aliguori | } |
384 | 5a123577 | aliguori | |
385 | 5a123577 | aliguori | static void zfree(void *x, void *addr, unsigned nb) |
386 | 5a123577 | aliguori | { |
387 | 5a123577 | aliguori | qemu_free(addr); |
388 | 5a123577 | aliguori | } |
389 | 5a123577 | aliguori | |
390 | 5a123577 | aliguori | |
391 | 5a123577 | aliguori | #define HEAD_CRC 2 |
392 | 5a123577 | aliguori | #define EXTRA_FIELD 4 |
393 | 5a123577 | aliguori | #define ORIG_NAME 8 |
394 | 5a123577 | aliguori | #define COMMENT 0x10 |
395 | 5a123577 | aliguori | #define RESERVED 0xe0 |
396 | 5a123577 | aliguori | |
397 | 5a123577 | aliguori | #define DEFLATED 8 |
398 | 5a123577 | aliguori | |
399 | 5a123577 | aliguori | /* This is the maximum in uboot, so if a uImage overflows this, it would
|
400 | 5a123577 | aliguori | * overflow on real hardware too. */
|
401 | 5a123577 | aliguori | #define UBOOT_MAX_GUNZIP_BYTES 0x800000 |
402 | 5a123577 | aliguori | |
403 | 5a123577 | aliguori | static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, |
404 | 5a123577 | aliguori | size_t srclen) |
405 | 5a123577 | aliguori | { |
406 | 5a123577 | aliguori | z_stream s; |
407 | 5a123577 | aliguori | ssize_t dstbytes; |
408 | 5a123577 | aliguori | int r, i, flags;
|
409 | 5a123577 | aliguori | |
410 | 5a123577 | aliguori | /* skip header */
|
411 | 5a123577 | aliguori | i = 10;
|
412 | 5a123577 | aliguori | flags = src[3];
|
413 | 5a123577 | aliguori | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { |
414 | 5a123577 | aliguori | puts ("Error: Bad gzipped data\n");
|
415 | 5a123577 | aliguori | return -1; |
416 | 5a123577 | aliguori | } |
417 | 5a123577 | aliguori | if ((flags & EXTRA_FIELD) != 0) |
418 | 5a123577 | aliguori | i = 12 + src[10] + (src[11] << 8); |
419 | 5a123577 | aliguori | if ((flags & ORIG_NAME) != 0) |
420 | 5a123577 | aliguori | while (src[i++] != 0) |
421 | 5a123577 | aliguori | ; |
422 | 5a123577 | aliguori | if ((flags & COMMENT) != 0) |
423 | 5a123577 | aliguori | while (src[i++] != 0) |
424 | 5a123577 | aliguori | ; |
425 | 5a123577 | aliguori | if ((flags & HEAD_CRC) != 0) |
426 | 5a123577 | aliguori | i += 2;
|
427 | 5a123577 | aliguori | if (i >= srclen) {
|
428 | 5a123577 | aliguori | puts ("Error: gunzip out of data in header\n");
|
429 | 5a123577 | aliguori | return -1; |
430 | 5a123577 | aliguori | } |
431 | 5a123577 | aliguori | |
432 | 5a123577 | aliguori | s.zalloc = zalloc; |
433 | 5a123577 | aliguori | s.zfree = (free_func)zfree; |
434 | 5a123577 | aliguori | |
435 | 5a123577 | aliguori | r = inflateInit2(&s, -MAX_WBITS); |
436 | 5a123577 | aliguori | if (r != Z_OK) {
|
437 | 5a123577 | aliguori | printf ("Error: inflateInit2() returned %d\n", r);
|
438 | 5a123577 | aliguori | return (-1); |
439 | 5a123577 | aliguori | } |
440 | 5a123577 | aliguori | s.next_in = src + i; |
441 | 5a123577 | aliguori | s.avail_in = srclen - i; |
442 | 5a123577 | aliguori | s.next_out = dst; |
443 | 5a123577 | aliguori | s.avail_out = dstlen; |
444 | 5a123577 | aliguori | r = inflate(&s, Z_FINISH); |
445 | 5a123577 | aliguori | if (r != Z_OK && r != Z_STREAM_END) {
|
446 | 5a123577 | aliguori | printf ("Error: inflate() returned %d\n", r);
|
447 | 5a123577 | aliguori | return -1; |
448 | 5a123577 | aliguori | } |
449 | 5a123577 | aliguori | dstbytes = s.next_out - (unsigned char *) dst; |
450 | 5a123577 | aliguori | inflateEnd(&s); |
451 | 5a123577 | aliguori | |
452 | 5a123577 | aliguori | return dstbytes;
|
453 | 5a123577 | aliguori | } |
454 | 5a123577 | aliguori | |
455 | 1c7b3754 | pbrook | /* Load a U-Boot image. */
|
456 | 5a9154e0 | aliguori | int load_uimage(const char *filename, target_ulong *ep, target_ulong *loadaddr, |
457 | 5a9154e0 | aliguori | int *is_linux)
|
458 | 1c7b3754 | pbrook | { |
459 | 1c7b3754 | pbrook | int fd;
|
460 | 1c7b3754 | pbrook | int size;
|
461 | 1c7b3754 | pbrook | uboot_image_header_t h; |
462 | 1c7b3754 | pbrook | uboot_image_header_t *hdr = &h; |
463 | 1c7b3754 | pbrook | uint8_t *data = NULL;
|
464 | 265ca29a | aliguori | int ret = -1; |
465 | 1c7b3754 | pbrook | |
466 | 1c7b3754 | pbrook | fd = open(filename, O_RDONLY | O_BINARY); |
467 | 1c7b3754 | pbrook | if (fd < 0) |
468 | 1c7b3754 | pbrook | return -1; |
469 | 1c7b3754 | pbrook | |
470 | 1c7b3754 | pbrook | size = read(fd, hdr, sizeof(uboot_image_header_t));
|
471 | 1c7b3754 | pbrook | if (size < 0) |
472 | 265ca29a | aliguori | goto out;
|
473 | 1c7b3754 | pbrook | |
474 | 1c7b3754 | pbrook | bswap_uboot_header(hdr); |
475 | 1c7b3754 | pbrook | |
476 | 1c7b3754 | pbrook | if (hdr->ih_magic != IH_MAGIC)
|
477 | 265ca29a | aliguori | goto out;
|
478 | 1c7b3754 | pbrook | |
479 | 7e5f90fa | aliguori | /* TODO: Implement other image types. */
|
480 | 7e5f90fa | aliguori | if (hdr->ih_type != IH_TYPE_KERNEL) {
|
481 | 7e5f90fa | aliguori | fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
|
482 | 265ca29a | aliguori | goto out;
|
483 | 1c7b3754 | pbrook | } |
484 | 1c7b3754 | pbrook | |
485 | 5a123577 | aliguori | switch (hdr->ih_comp) {
|
486 | 5a123577 | aliguori | case IH_COMP_NONE:
|
487 | 5a123577 | aliguori | case IH_COMP_GZIP:
|
488 | 5a123577 | aliguori | break;
|
489 | 5a123577 | aliguori | default:
|
490 | 5a123577 | aliguori | fprintf(stderr, |
491 | 5a123577 | aliguori | "Unable to load u-boot images with compression type %d\n",
|
492 | 5a123577 | aliguori | hdr->ih_comp); |
493 | 265ca29a | aliguori | goto out;
|
494 | 1c7b3754 | pbrook | } |
495 | 1c7b3754 | pbrook | |
496 | 1c7b3754 | pbrook | /* TODO: Check CPU type. */
|
497 | 1c7b3754 | pbrook | if (is_linux) {
|
498 | 7e5f90fa | aliguori | if (hdr->ih_os == IH_OS_LINUX)
|
499 | 1c7b3754 | pbrook | *is_linux = 1;
|
500 | 1c7b3754 | pbrook | else
|
501 | 1c7b3754 | pbrook | *is_linux = 0;
|
502 | 1c7b3754 | pbrook | } |
503 | 1c7b3754 | pbrook | |
504 | 1c7b3754 | pbrook | *ep = hdr->ih_ep; |
505 | 1c7b3754 | pbrook | data = qemu_malloc(hdr->ih_size); |
506 | 1c7b3754 | pbrook | |
507 | 1c7b3754 | pbrook | if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
|
508 | 1c7b3754 | pbrook | fprintf(stderr, "Error reading file\n");
|
509 | 265ca29a | aliguori | goto out;
|
510 | 1c7b3754 | pbrook | } |
511 | 1c7b3754 | pbrook | |
512 | 5a123577 | aliguori | if (hdr->ih_comp == IH_COMP_GZIP) {
|
513 | 5a123577 | aliguori | uint8_t *compressed_data; |
514 | 5a123577 | aliguori | size_t max_bytes; |
515 | 5a123577 | aliguori | ssize_t bytes; |
516 | 5a123577 | aliguori | |
517 | 5a123577 | aliguori | compressed_data = data; |
518 | 5a123577 | aliguori | max_bytes = UBOOT_MAX_GUNZIP_BYTES; |
519 | 5a123577 | aliguori | data = qemu_malloc(max_bytes); |
520 | 5a123577 | aliguori | |
521 | 5a123577 | aliguori | bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size); |
522 | 5a123577 | aliguori | qemu_free(compressed_data); |
523 | 5a123577 | aliguori | if (bytes < 0) { |
524 | 5a123577 | aliguori | fprintf(stderr, "Unable to decompress gzipped image!\n");
|
525 | 5a123577 | aliguori | goto out;
|
526 | 5a123577 | aliguori | } |
527 | 5a123577 | aliguori | hdr->ih_size = bytes; |
528 | 5a123577 | aliguori | } |
529 | 5a123577 | aliguori | |
530 | 1c7b3754 | pbrook | cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size); |
531 | 1c7b3754 | pbrook | |
532 | 21cafd08 | aliguori | if (loadaddr)
|
533 | 21cafd08 | aliguori | *loadaddr = hdr->ih_load; |
534 | 21cafd08 | aliguori | |
535 | 265ca29a | aliguori | ret = hdr->ih_size; |
536 | 1c7b3754 | pbrook | |
537 | 265ca29a | aliguori | out:
|
538 | 1c7b3754 | pbrook | if (data)
|
539 | 1c7b3754 | pbrook | qemu_free(data); |
540 | 1c7b3754 | pbrook | close(fd); |
541 | 265ca29a | aliguori | return ret;
|
542 | 1c7b3754 | pbrook | } |