Fix faligndata (Vince Weaver)
[qemu] / loader.c
1 /*
2  * QEMU Executable loader
3  *
4  * Copyright (c) 2006 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu-common.h"
25 #include "disas.h"
26 #include "sysemu.h"
27 #include "uboot_image.h"
28
29 /* return the size or -1 if error */
30 int get_image_size(const char *filename)
31 {
32     int fd, size;
33     fd = open(filename, O_RDONLY | O_BINARY);
34     if (fd < 0)
35         return -1;
36     size = lseek(fd, 0, SEEK_END);
37     close(fd);
38     return size;
39 }
40
41 /* return the size or -1 if error */
42 /* deprecated, because caller does not specify buffer size! */
43 int load_image(const char *filename, uint8_t *addr)
44 {
45     int fd, size;
46     fd = open(filename, O_RDONLY | O_BINARY);
47     if (fd < 0)
48         return -1;
49     size = lseek(fd, 0, SEEK_END);
50     lseek(fd, 0, SEEK_SET);
51     if (read(fd, addr, size) != size) {
52         close(fd);
53         return -1;
54     }
55     close(fd);
56     return size;
57 }
58
59 /* return the amount read, just like fread.  0 may mean error or eof */
60 int fread_targphys(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
61 {
62     uint8_t buf[4096];
63     target_phys_addr_t dst_begin = dst_addr;
64     size_t want, did;
65
66     while (nbytes) {
67         want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
68         did = fread(buf, 1, want, f);
69         if (did != want) break;
70
71         cpu_physical_memory_write_rom(dst_addr, buf, did);
72         dst_addr += did;
73         nbytes -= did;
74     }
75     return dst_addr - dst_begin;
76 }
77
78 /* returns 0 on error, 1 if ok */
79 int fread_targphys_ok(target_phys_addr_t dst_addr, size_t nbytes, FILE *f)
80 {
81     return fread_targphys(dst_addr, nbytes, f) == nbytes;
82 }
83
84 /* read()-like version */
85 int read_targphys(int fd, target_phys_addr_t dst_addr, size_t nbytes)
86 {
87     uint8_t buf[4096];
88     target_phys_addr_t dst_begin = dst_addr;
89     size_t want, did;
90
91     while (nbytes) {
92         want = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
93         did = read(fd, buf, want);
94         if (did != want) break;
95
96         cpu_physical_memory_write_rom(dst_addr, buf, did);
97         dst_addr += did;
98         nbytes -= did;
99     }
100     return dst_addr - dst_begin;
101 }
102
103 /* return the size or -1 if error */
104 int load_image_targphys(const char *filename,
105                         target_phys_addr_t addr, int max_sz)
106 {
107     FILE *f;
108     size_t got;
109
110     f = fopen(filename, "rb");
111     if (!f) return -1;
112
113     got = fread_targphys(addr, max_sz, f);
114     if (ferror(f)) { fclose(f); return -1; }
115     fclose(f);
116
117     return got;
118 }
119
120 void pstrcpy_targphys(target_phys_addr_t dest, int buf_size,
121                       const char *source)
122 {
123     static const uint8_t nul_byte = 0;
124     const char *nulp;
125
126     if (buf_size <= 0) return;
127     nulp = memchr(source, 0, buf_size);
128     if (nulp) {
129         cpu_physical_memory_write_rom(dest, (uint8_t *)source,
130                                       (nulp - source) + 1);
131     } else {
132         cpu_physical_memory_write_rom(dest, (uint8_t *)source, buf_size - 1);
133         cpu_physical_memory_write_rom(dest, &nul_byte, 1);
134     }
135 }
136
137 /* A.OUT loader */
138
139 struct exec
140 {
141   uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
142   uint32_t a_text;   /* length of text, in bytes */
143   uint32_t a_data;   /* length of data, in bytes */
144   uint32_t a_bss;    /* length of uninitialized data area, in bytes */
145   uint32_t a_syms;   /* length of symbol table data in file, in bytes */
146   uint32_t a_entry;  /* start address */
147   uint32_t a_trsize; /* length of relocation info for text, in bytes */
148   uint32_t a_drsize; /* length of relocation info for data, in bytes */
149 };
150
151 #ifdef BSWAP_NEEDED
152 static void bswap_ahdr(struct exec *e)
153 {
154     bswap32s(&e->a_info);
155     bswap32s(&e->a_text);
156     bswap32s(&e->a_data);
157     bswap32s(&e->a_bss);
158     bswap32s(&e->a_syms);
159     bswap32s(&e->a_entry);
160     bswap32s(&e->a_trsize);
161     bswap32s(&e->a_drsize);
162 }
163 #else
164 #define bswap_ahdr(x) do { } while (0)
165 #endif
166
167 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
168 #define OMAGIC 0407
169 #define NMAGIC 0410
170 #define ZMAGIC 0413
171 #define QMAGIC 0314
172 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
173 #define N_TXTOFF(x)                                                     \
174     (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) :     \
175      (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
176 #define N_TXTADDR(x) (N_MAGIC(x) == QMAGIC ? TARGET_PAGE_SIZE : 0)
177 #define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
178 #define _N_SEGMENT_ROUND(x) (((x) + TARGET_PAGE_SIZE - 1) & ~(TARGET_PAGE_SIZE - 1))
179
180 #define _N_TXTENDADDR(x) (N_TXTADDR(x)+(x).a_text)
181
182 #define N_DATADDR(x) \
183     (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x)) \
184      : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x))))
185
186
187 int load_aout(const char *filename, target_phys_addr_t addr, int max_sz)
188 {
189     int fd, size, ret;
190     struct exec e;
191     uint32_t magic;
192
193     fd = open(filename, O_RDONLY | O_BINARY);
194     if (fd < 0)
195         return -1;
196
197     size = read(fd, &e, sizeof(e));
198     if (size < 0)
199         goto fail;
200
201     bswap_ahdr(&e);
202
203     magic = N_MAGIC(e);
204     switch (magic) {
205     case ZMAGIC:
206     case QMAGIC:
207     case OMAGIC:
208         if (e.a_text + e.a_data > max_sz)
209             goto fail;
210         lseek(fd, N_TXTOFF(e), SEEK_SET);
211         size = read_targphys(fd, addr, e.a_text + e.a_data);
212         if (size < 0)
213             goto fail;
214         break;
215     case NMAGIC:
216         if (N_DATADDR(e) + e.a_data > max_sz)
217             goto fail;
218         lseek(fd, N_TXTOFF(e), SEEK_SET);
219         size = read_targphys(fd, addr, e.a_text);
220         if (size < 0)
221             goto fail;
222         ret = read_targphys(fd, addr + N_DATADDR(e), e.a_data);
223         if (ret < 0)
224             goto fail;
225         size += ret;
226         break;
227     default:
228         goto fail;
229     }
230     close(fd);
231     return size;
232  fail:
233     close(fd);
234     return -1;
235 }
236
237 /* ELF loader */
238
239 static void *load_at(int fd, int offset, int size)
240 {
241     void *ptr;
242     if (lseek(fd, offset, SEEK_SET) < 0)
243         return NULL;
244     ptr = qemu_malloc(size);
245     if (!ptr)
246         return NULL;
247     if (read(fd, ptr, size) != size) {
248         qemu_free(ptr);
249         return NULL;
250     }
251     return ptr;
252 }
253
254
255 #define ELF_CLASS   ELFCLASS32
256 #include "elf.h"
257
258 #define SZ              32
259 #define elf_word        uint32_t
260 #define elf_sword        int32_t
261 #define bswapSZs        bswap32s
262 #include "elf_ops.h"
263
264 #undef elfhdr
265 #undef elf_phdr
266 #undef elf_shdr
267 #undef elf_sym
268 #undef elf_note
269 #undef elf_word
270 #undef elf_sword
271 #undef bswapSZs
272 #undef SZ
273 #define elfhdr          elf64_hdr
274 #define elf_phdr        elf64_phdr
275 #define elf_note        elf64_note
276 #define elf_shdr        elf64_shdr
277 #define elf_sym         elf64_sym
278 #define elf_word        uint64_t
279 #define elf_sword        int64_t
280 #define bswapSZs        bswap64s
281 #define SZ              64
282 #include "elf_ops.h"
283
284 /* return < 0 if error, otherwise the number of bytes loaded in memory */
285 int load_elf(const char *filename, int64_t virt_to_phys_addend,
286              uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr)
287 {
288     int fd, data_order, host_data_order, must_swab, ret;
289     uint8_t e_ident[EI_NIDENT];
290
291     fd = open(filename, O_RDONLY | O_BINARY);
292     if (fd < 0) {
293         perror(filename);
294         return -1;
295     }
296     if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
297         goto fail;
298     if (e_ident[0] != ELFMAG0 ||
299         e_ident[1] != ELFMAG1 ||
300         e_ident[2] != ELFMAG2 ||
301         e_ident[3] != ELFMAG3)
302         goto fail;
303 #ifdef WORDS_BIGENDIAN
304     data_order = ELFDATA2MSB;
305 #else
306     data_order = ELFDATA2LSB;
307 #endif
308     must_swab = data_order != e_ident[EI_DATA];
309
310 #ifdef TARGET_WORDS_BIGENDIAN
311     host_data_order = ELFDATA2MSB;
312 #else
313     host_data_order = ELFDATA2LSB;
314 #endif
315     if (host_data_order != e_ident[EI_DATA])
316         return -1;
317
318     lseek(fd, 0, SEEK_SET);
319     if (e_ident[EI_CLASS] == ELFCLASS64) {
320         ret = load_elf64(fd, virt_to_phys_addend, must_swab, pentry,
321                          lowaddr, highaddr);
322     } else {
323         ret = load_elf32(fd, virt_to_phys_addend, must_swab, pentry,
324                          lowaddr, highaddr);
325     }
326
327     close(fd);
328     return ret;
329
330  fail:
331     close(fd);
332     return -1;
333 }
334
335 static void bswap_uboot_header(uboot_image_header_t *hdr)
336 {
337 #ifndef WORDS_BIGENDIAN
338     bswap32s(&hdr->ih_magic);
339     bswap32s(&hdr->ih_hcrc);
340     bswap32s(&hdr->ih_time);
341     bswap32s(&hdr->ih_size);
342     bswap32s(&hdr->ih_load);
343     bswap32s(&hdr->ih_ep);
344     bswap32s(&hdr->ih_dcrc);
345 #endif
346 }
347
348 /* Load a U-Boot image.  */
349 int load_uboot(const char *filename, target_ulong *ep, int *is_linux)
350 {
351
352     int fd;
353     int size;
354     uboot_image_header_t h;
355     uboot_image_header_t *hdr = &h;
356     uint8_t *data = NULL;
357
358     fd = open(filename, O_RDONLY | O_BINARY);
359     if (fd < 0)
360         return -1;
361
362     size = read(fd, hdr, sizeof(uboot_image_header_t));
363     if (size < 0)
364         goto fail;
365
366     bswap_uboot_header(hdr);
367
368     if (hdr->ih_magic != IH_MAGIC)
369         goto fail;
370
371     /* TODO: Implement Multi-File images.  */
372     if (hdr->ih_type == IH_TYPE_MULTI) {
373         fprintf(stderr, "Unable to load multi-file u-boot images\n");
374         goto fail;
375     }
376
377     /* TODO: Implement compressed images.  */
378     if (hdr->ih_comp != IH_COMP_NONE) {
379         fprintf(stderr, "Unable to load compressed u-boot images\n");
380         goto fail;
381     }
382
383     /* TODO: Check CPU type.  */
384     if (is_linux) {
385         if (hdr->ih_type == IH_TYPE_KERNEL && hdr->ih_os == IH_OS_LINUX)
386             *is_linux = 1;
387         else
388             *is_linux = 0;
389     }
390
391     *ep = hdr->ih_ep;
392     data = qemu_malloc(hdr->ih_size);
393     if (!data)
394         goto fail;
395
396     if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
397         fprintf(stderr, "Error reading file\n");
398         goto fail;
399     }
400
401     cpu_physical_memory_write_rom(hdr->ih_load, data, hdr->ih_size);
402
403     return hdr->ih_size;
404
405 fail:
406     if (data)
407         qemu_free(data);
408     close(fd);
409     return -1;
410 }