blob: f38b994a51f39b11ffc425143f99ca0d8e86ee57 [file] [log] [blame]
bellard5fe141f2006-04-23 17:12:42 +00001/*
2 * QEMU Executable loader
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard5fe141f2006-04-23 17:12:42 +00004 * Copyright (c) 2006 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard5fe141f2006-04-23 17:12:42 +00006 * 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.
aliguori5a123572008-11-20 22:04:01 +000023 *
24 * Gunzip functionality in this file is derived from u-boot:
25 *
26 * (C) Copyright 2008 Semihalf
27 *
28 * (C) Copyright 2000-2005
29 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License as
33 * published by the Free Software Foundation; either version 2 of
34 * the License, or (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
aurel32fad6cb12009-01-04 22:05:52 +000041 * You should have received a copy of the GNU General Public License along
Blue Swirl8167ee82009-07-16 20:47:01 +000042 * with this program; if not, see <http://www.gnu.org/licenses/>.
bellard5fe141f2006-04-23 17:12:42 +000043 */
aliguori5a123572008-11-20 22:04:01 +000044
Blue Swirlca20cf32009-09-20 14:58:02 +000045#include "hw.h"
bellard5fe141f2006-04-23 17:12:42 +000046#include "disas.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020047#include "monitor.h"
pbrook9596ebb2007-11-18 01:44:38 +000048#include "sysemu.h"
pbrook1c7b3752007-03-06 23:52:01 +000049#include "uboot_image.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000050#include "loader.h"
bellard5fe141f2006-04-23 17:12:42 +000051
aliguori5a123572008-11-20 22:04:01 +000052#include <zlib.h>
53
bellard5fe141f2006-04-23 17:12:42 +000054/* return the size or -1 if error */
55int get_image_size(const char *filename)
56{
57 int fd, size;
58 fd = open(filename, O_RDONLY | O_BINARY);
59 if (fd < 0)
60 return -1;
61 size = lseek(fd, 0, SEEK_END);
62 close(fd);
63 return size;
64}
65
66/* return the size or -1 if error */
blueswir1293f78b2008-05-12 17:22:13 +000067/* deprecated, because caller does not specify buffer size! */
bellard5fe141f2006-04-23 17:12:42 +000068int load_image(const char *filename, uint8_t *addr)
69{
70 int fd, size;
71 fd = open(filename, O_RDONLY | O_BINARY);
72 if (fd < 0)
73 return -1;
74 size = lseek(fd, 0, SEEK_END);
75 lseek(fd, 0, SEEK_SET);
76 if (read(fd, addr, size) != size) {
77 close(fd);
78 return -1;
79 }
80 close(fd);
81 return size;
82}
83
blueswir1293f78b2008-05-12 17:22:13 +000084/* read()-like version */
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020085int read_targphys(const char *name,
86 int fd, target_phys_addr_t dst_addr, size_t nbytes)
blueswir1293f78b2008-05-12 17:22:13 +000087{
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020088 uint8_t *buf;
89 size_t did;
blueswir1293f78b2008-05-12 17:22:13 +000090
Gerd Hoffmann45a50b12009-10-01 16:42:33 +020091 buf = qemu_malloc(nbytes);
92 did = read(fd, buf, nbytes);
93 if (did > 0)
94 rom_add_blob_fixed("read", buf, did, dst_addr);
95 qemu_free(buf);
96 return did;
blueswir1293f78b2008-05-12 17:22:13 +000097}
98
99/* return the size or -1 if error */
100int load_image_targphys(const char *filename,
Anthony Liguoric227f092009-10-01 16:12:16 -0500101 target_phys_addr_t addr, int max_sz)
blueswir1293f78b2008-05-12 17:22:13 +0000102{
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200103 int size;
blueswir1293f78b2008-05-12 17:22:13 +0000104
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200105 size = get_image_size(filename);
106 if (size > 0)
107 rom_add_file_fixed(filename, addr);
108 return size;
blueswir1293f78b2008-05-12 17:22:13 +0000109}
110
Gerd Hoffmann3c178e72009-10-07 13:37:06 +0200111void pstrcpy_targphys(const char *name, target_phys_addr_t dest, int buf_size,
blueswir1293f78b2008-05-12 17:22:13 +0000112 const char *source)
113{
blueswir1293f78b2008-05-12 17:22:13 +0000114 const char *nulp;
Gerd Hoffmann3c178e72009-10-07 13:37:06 +0200115 char *ptr;
blueswir1293f78b2008-05-12 17:22:13 +0000116
117 if (buf_size <= 0) return;
118 nulp = memchr(source, 0, buf_size);
119 if (nulp) {
Gerd Hoffmann3c178e72009-10-07 13:37:06 +0200120 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
blueswir1293f78b2008-05-12 17:22:13 +0000121 } else {
Gerd Hoffmann3c178e72009-10-07 13:37:06 +0200122 rom_add_blob_fixed(name, source, buf_size, dest);
123 ptr = rom_ptr(dest + buf_size - 1);
124 *ptr = 0;
blueswir1293f78b2008-05-12 17:22:13 +0000125 }
126}
127
bellard5fe141f2006-04-23 17:12:42 +0000128/* A.OUT loader */
129
130struct exec
131{
132 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
133 uint32_t a_text; /* length of text, in bytes */
134 uint32_t a_data; /* length of data, in bytes */
135 uint32_t a_bss; /* length of uninitialized data area, in bytes */
136 uint32_t a_syms; /* length of symbol table data in file, in bytes */
137 uint32_t a_entry; /* start address */
138 uint32_t a_trsize; /* length of relocation info for text, in bytes */
139 uint32_t a_drsize; /* length of relocation info for data, in bytes */
140};
141
bellard5fe141f2006-04-23 17:12:42 +0000142static void bswap_ahdr(struct exec *e)
143{
144 bswap32s(&e->a_info);
145 bswap32s(&e->a_text);
146 bswap32s(&e->a_data);
147 bswap32s(&e->a_bss);
148 bswap32s(&e->a_syms);
149 bswap32s(&e->a_entry);
150 bswap32s(&e->a_trsize);
151 bswap32s(&e->a_drsize);
152}
bellard5fe141f2006-04-23 17:12:42 +0000153
154#define N_MAGIC(exec) ((exec).a_info & 0xffff)
155#define OMAGIC 0407
156#define NMAGIC 0410
157#define ZMAGIC 0413
158#define QMAGIC 0314
159#define _N_HDROFF(x) (1024 - sizeof (struct exec))
160#define N_TXTOFF(x) \
161 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
162 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
Blue Swirlca20cf32009-09-20 14:58:02 +0000163#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
164#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
bellard5fe141f2006-04-23 17:12:42 +0000165
Blue Swirlca20cf32009-09-20 14:58:02 +0000166#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
bellard5fe141f2006-04-23 17:12:42 +0000167
Blue Swirlca20cf32009-09-20 14:58:02 +0000168#define N_DATADDR(x, target_page_size) \
169 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
170 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
bellard5fe141f2006-04-23 17:12:42 +0000171
172
Anthony Liguoric227f092009-10-01 16:12:16 -0500173int load_aout(const char *filename, target_phys_addr_t addr, int max_sz,
174 int bswap_needed, target_phys_addr_t target_page_size)
bellard5fe141f2006-04-23 17:12:42 +0000175{
176 int fd, size, ret;
177 struct exec e;
178 uint32_t magic;
179
180 fd = open(filename, O_RDONLY | O_BINARY);
181 if (fd < 0)
182 return -1;
183
184 size = read(fd, &e, sizeof(e));
185 if (size < 0)
186 goto fail;
187
Blue Swirlca20cf32009-09-20 14:58:02 +0000188 if (bswap_needed) {
189 bswap_ahdr(&e);
190 }
bellard5fe141f2006-04-23 17:12:42 +0000191
192 magic = N_MAGIC(e);
193 switch (magic) {
194 case ZMAGIC:
195 case QMAGIC:
196 case OMAGIC:
blueswir1293f78b2008-05-12 17:22:13 +0000197 if (e.a_text + e.a_data > max_sz)
198 goto fail;
bellard5fe141f2006-04-23 17:12:42 +0000199 lseek(fd, N_TXTOFF(e), SEEK_SET);
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200200 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
bellard5fe141f2006-04-23 17:12:42 +0000201 if (size < 0)
202 goto fail;
203 break;
204 case NMAGIC:
Blue Swirlca20cf32009-09-20 14:58:02 +0000205 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
blueswir1293f78b2008-05-12 17:22:13 +0000206 goto fail;
bellard5fe141f2006-04-23 17:12:42 +0000207 lseek(fd, N_TXTOFF(e), SEEK_SET);
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200208 size = read_targphys(filename, fd, addr, e.a_text);
bellard5fe141f2006-04-23 17:12:42 +0000209 if (size < 0)
210 goto fail;
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200211 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
Blue Swirlca20cf32009-09-20 14:58:02 +0000212 e.a_data);
bellard5fe141f2006-04-23 17:12:42 +0000213 if (ret < 0)
214 goto fail;
215 size += ret;
216 break;
217 default:
218 goto fail;
219 }
220 close(fd);
221 return size;
222 fail:
223 close(fd);
224 return -1;
225}
226
227/* ELF loader */
228
229static void *load_at(int fd, int offset, int size)
230{
231 void *ptr;
232 if (lseek(fd, offset, SEEK_SET) < 0)
233 return NULL;
234 ptr = qemu_malloc(size);
bellard5fe141f2006-04-23 17:12:42 +0000235 if (read(fd, ptr, size) != size) {
236 qemu_free(ptr);
237 return NULL;
238 }
239 return ptr;
240}
241
malc3efa9a62009-07-18 13:10:12 +0400242#ifdef ELF_CLASS
243#undef ELF_CLASS
244#endif
bellard5fe141f2006-04-23 17:12:42 +0000245
246#define ELF_CLASS ELFCLASS32
247#include "elf.h"
248
249#define SZ 32
250#define elf_word uint32_t
ths82790062007-10-17 23:07:31 +0000251#define elf_sword int32_t
bellard5fe141f2006-04-23 17:12:42 +0000252#define bswapSZs bswap32s
253#include "elf_ops.h"
254
255#undef elfhdr
256#undef elf_phdr
257#undef elf_shdr
258#undef elf_sym
259#undef elf_note
260#undef elf_word
ths82790062007-10-17 23:07:31 +0000261#undef elf_sword
bellard5fe141f2006-04-23 17:12:42 +0000262#undef bswapSZs
263#undef SZ
264#define elfhdr elf64_hdr
265#define elf_phdr elf64_phdr
266#define elf_note elf64_note
267#define elf_shdr elf64_shdr
268#define elf_sym elf64_sym
269#define elf_word uint64_t
ths82790062007-10-17 23:07:31 +0000270#define elf_sword int64_t
bellard5fe141f2006-04-23 17:12:42 +0000271#define bswapSZs bswap64s
272#define SZ 64
273#include "elf_ops.h"
274
275/* return < 0 if error, otherwise the number of bytes loaded in memory */
pbrook83c1f872008-10-22 18:20:20 +0000276int load_elf(const char *filename, int64_t address_offset,
Blue Swirlca20cf32009-09-20 14:58:02 +0000277 uint64_t *pentry, uint64_t *lowaddr, uint64_t *highaddr,
278 int big_endian, int elf_machine, int clear_lsb)
bellard5fe141f2006-04-23 17:12:42 +0000279{
Blue Swirlca20cf32009-09-20 14:58:02 +0000280 int fd, data_order, target_data_order, must_swab, ret;
bellard5fe141f2006-04-23 17:12:42 +0000281 uint8_t e_ident[EI_NIDENT];
282
bellard699e4642006-05-07 18:06:27 +0000283 fd = open(filename, O_RDONLY | O_BINARY);
bellard5fe141f2006-04-23 17:12:42 +0000284 if (fd < 0) {
285 perror(filename);
286 return -1;
287 }
288 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
289 goto fail;
290 if (e_ident[0] != ELFMAG0 ||
291 e_ident[1] != ELFMAG1 ||
292 e_ident[2] != ELFMAG2 ||
293 e_ident[3] != ELFMAG3)
294 goto fail;
Juan Quintelae2542fe2009-07-27 16:13:06 +0200295#ifdef HOST_WORDS_BIGENDIAN
bellard5fe141f2006-04-23 17:12:42 +0000296 data_order = ELFDATA2MSB;
297#else
298 data_order = ELFDATA2LSB;
299#endif
300 must_swab = data_order != e_ident[EI_DATA];
Blue Swirlca20cf32009-09-20 14:58:02 +0000301 if (big_endian) {
302 target_data_order = ELFDATA2MSB;
303 } else {
304 target_data_order = ELFDATA2LSB;
305 }
ths9042c0e2006-12-23 14:18:40 +0000306
Blue Swirlca20cf32009-09-20 14:58:02 +0000307 if (target_data_order != e_ident[EI_DATA])
ths9042c0e2006-12-23 14:18:40 +0000308 return -1;
309
bellard5fe141f2006-04-23 17:12:42 +0000310 lseek(fd, 0, SEEK_SET);
311 if (e_ident[EI_CLASS] == ELFCLASS64) {
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200312 ret = load_elf64(filename, fd, address_offset, must_swab, pentry,
Blue Swirlca20cf32009-09-20 14:58:02 +0000313 lowaddr, highaddr, elf_machine, clear_lsb);
bellard5fe141f2006-04-23 17:12:42 +0000314 } else {
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200315 ret = load_elf32(filename, fd, address_offset, must_swab, pentry,
Blue Swirlca20cf32009-09-20 14:58:02 +0000316 lowaddr, highaddr, elf_machine, clear_lsb);
bellard5fe141f2006-04-23 17:12:42 +0000317 }
318
319 close(fd);
320 return ret;
321
322 fail:
323 close(fd);
324 return -1;
325}
pbrook1c7b3752007-03-06 23:52:01 +0000326
Anthony Liguoric227f092009-10-01 16:12:16 -0500327static void bswap_uboot_header(uboot_image_header_t *hdr)
pbrook1c7b3752007-03-06 23:52:01 +0000328{
Juan Quintelae2542fe2009-07-27 16:13:06 +0200329#ifndef HOST_WORDS_BIGENDIAN
pbrook1c7b3752007-03-06 23:52:01 +0000330 bswap32s(&hdr->ih_magic);
331 bswap32s(&hdr->ih_hcrc);
332 bswap32s(&hdr->ih_time);
333 bswap32s(&hdr->ih_size);
334 bswap32s(&hdr->ih_load);
335 bswap32s(&hdr->ih_ep);
336 bswap32s(&hdr->ih_dcrc);
337#endif
338}
339
aliguori5a123572008-11-20 22:04:01 +0000340
341#define ZALLOC_ALIGNMENT 16
342
343static void *zalloc(void *x, unsigned items, unsigned size)
344{
345 void *p;
346
347 size *= items;
348 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
349
350 p = qemu_malloc(size);
351
352 return (p);
353}
354
Stefan Weild084eab2009-06-09 23:36:03 +0200355static void zfree(void *x, void *addr)
aliguori5a123572008-11-20 22:04:01 +0000356{
357 qemu_free(addr);
358}
359
360
361#define HEAD_CRC 2
362#define EXTRA_FIELD 4
363#define ORIG_NAME 8
364#define COMMENT 0x10
365#define RESERVED 0xe0
366
367#define DEFLATED 8
368
369/* This is the maximum in uboot, so if a uImage overflows this, it would
370 * overflow on real hardware too. */
371#define UBOOT_MAX_GUNZIP_BYTES 0x800000
372
373static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
374 size_t srclen)
375{
376 z_stream s;
377 ssize_t dstbytes;
378 int r, i, flags;
379
380 /* skip header */
381 i = 10;
382 flags = src[3];
383 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
384 puts ("Error: Bad gzipped data\n");
385 return -1;
386 }
387 if ((flags & EXTRA_FIELD) != 0)
388 i = 12 + src[10] + (src[11] << 8);
389 if ((flags & ORIG_NAME) != 0)
390 while (src[i++] != 0)
391 ;
392 if ((flags & COMMENT) != 0)
393 while (src[i++] != 0)
394 ;
395 if ((flags & HEAD_CRC) != 0)
396 i += 2;
397 if (i >= srclen) {
398 puts ("Error: gunzip out of data in header\n");
399 return -1;
400 }
401
402 s.zalloc = zalloc;
Stefan Weild084eab2009-06-09 23:36:03 +0200403 s.zfree = zfree;
aliguori5a123572008-11-20 22:04:01 +0000404
405 r = inflateInit2(&s, -MAX_WBITS);
406 if (r != Z_OK) {
407 printf ("Error: inflateInit2() returned %d\n", r);
408 return (-1);
409 }
410 s.next_in = src + i;
411 s.avail_in = srclen - i;
412 s.next_out = dst;
413 s.avail_out = dstlen;
414 r = inflate(&s, Z_FINISH);
415 if (r != Z_OK && r != Z_STREAM_END) {
416 printf ("Error: inflate() returned %d\n", r);
417 return -1;
418 }
419 dstbytes = s.next_out - (unsigned char *) dst;
420 inflateEnd(&s);
421
422 return dstbytes;
423}
424
pbrook1c7b3752007-03-06 23:52:01 +0000425/* Load a U-Boot image. */
Anthony Liguoric227f092009-10-01 16:12:16 -0500426int load_uimage(const char *filename, target_phys_addr_t *ep,
427 target_phys_addr_t *loadaddr, int *is_linux)
pbrook1c7b3752007-03-06 23:52:01 +0000428{
pbrook1c7b3752007-03-06 23:52:01 +0000429 int fd;
430 int size;
Anthony Liguoric227f092009-10-01 16:12:16 -0500431 uboot_image_header_t h;
432 uboot_image_header_t *hdr = &h;
pbrook1c7b3752007-03-06 23:52:01 +0000433 uint8_t *data = NULL;
aliguori265ca292008-11-20 22:02:56 +0000434 int ret = -1;
pbrook1c7b3752007-03-06 23:52:01 +0000435
436 fd = open(filename, O_RDONLY | O_BINARY);
437 if (fd < 0)
438 return -1;
439
Anthony Liguoric227f092009-10-01 16:12:16 -0500440 size = read(fd, hdr, sizeof(uboot_image_header_t));
pbrook1c7b3752007-03-06 23:52:01 +0000441 if (size < 0)
aliguori265ca292008-11-20 22:02:56 +0000442 goto out;
pbrook1c7b3752007-03-06 23:52:01 +0000443
444 bswap_uboot_header(hdr);
445
446 if (hdr->ih_magic != IH_MAGIC)
aliguori265ca292008-11-20 22:02:56 +0000447 goto out;
pbrook1c7b3752007-03-06 23:52:01 +0000448
aliguori7e5f90f2008-11-20 22:15:46 +0000449 /* TODO: Implement other image types. */
450 if (hdr->ih_type != IH_TYPE_KERNEL) {
451 fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
aliguori265ca292008-11-20 22:02:56 +0000452 goto out;
pbrook1c7b3752007-03-06 23:52:01 +0000453 }
454
aliguori5a123572008-11-20 22:04:01 +0000455 switch (hdr->ih_comp) {
456 case IH_COMP_NONE:
457 case IH_COMP_GZIP:
458 break;
459 default:
460 fprintf(stderr,
461 "Unable to load u-boot images with compression type %d\n",
462 hdr->ih_comp);
aliguori265ca292008-11-20 22:02:56 +0000463 goto out;
pbrook1c7b3752007-03-06 23:52:01 +0000464 }
465
466 /* TODO: Check CPU type. */
467 if (is_linux) {
aliguori7e5f90f2008-11-20 22:15:46 +0000468 if (hdr->ih_os == IH_OS_LINUX)
pbrook1c7b3752007-03-06 23:52:01 +0000469 *is_linux = 1;
470 else
471 *is_linux = 0;
472 }
473
474 *ep = hdr->ih_ep;
475 data = qemu_malloc(hdr->ih_size);
pbrook1c7b3752007-03-06 23:52:01 +0000476
477 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
478 fprintf(stderr, "Error reading file\n");
aliguori265ca292008-11-20 22:02:56 +0000479 goto out;
pbrook1c7b3752007-03-06 23:52:01 +0000480 }
481
aliguori5a123572008-11-20 22:04:01 +0000482 if (hdr->ih_comp == IH_COMP_GZIP) {
483 uint8_t *compressed_data;
484 size_t max_bytes;
485 ssize_t bytes;
486
487 compressed_data = data;
488 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
489 data = qemu_malloc(max_bytes);
490
491 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
492 qemu_free(compressed_data);
493 if (bytes < 0) {
494 fprintf(stderr, "Unable to decompress gzipped image!\n");
495 goto out;
496 }
497 hdr->ih_size = bytes;
498 }
499
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200500 rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load);
pbrook1c7b3752007-03-06 23:52:01 +0000501
aliguori21cafd02008-11-20 22:11:52 +0000502 if (loadaddr)
503 *loadaddr = hdr->ih_load;
504
aliguori265ca292008-11-20 22:02:56 +0000505 ret = hdr->ih_size;
pbrook1c7b3752007-03-06 23:52:01 +0000506
aliguori265ca292008-11-20 22:02:56 +0000507out:
pbrook1c7b3752007-03-06 23:52:01 +0000508 if (data)
509 qemu_free(data);
510 close(fd);
aliguori265ca292008-11-20 22:02:56 +0000511 return ret;
pbrook1c7b3752007-03-06 23:52:01 +0000512}
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200513
514/*
515 * Functions for reboot-persistent memory regions.
516 * - used for vga bios and option roms.
517 * - also linux kernel (-kernel / -initrd).
518 */
519
520typedef struct Rom Rom;
521
522struct Rom {
523 char *name;
524 char *path;
525 size_t romsize;
526 uint8_t *data;
527 int align;
528 int isrom;
529
530 target_phys_addr_t min;
531 target_phys_addr_t max;
532 target_phys_addr_t addr;
533 QTAILQ_ENTRY(Rom) next;
534};
535
536static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
537
538static void rom_insert(Rom *rom)
539{
540 Rom *item;
541
542 /* list is ordered by load address */
543 QTAILQ_FOREACH(item, &roms, next) {
544 if (rom->min >= item->min)
545 continue;
546 QTAILQ_INSERT_BEFORE(item, rom, next);
547 return;
548 }
549 QTAILQ_INSERT_TAIL(&roms, rom, next);
550}
551
552int rom_add_file(const char *file,
553 target_phys_addr_t min, target_phys_addr_t max, int align)
554{
555 Rom *rom;
556 int rc, fd = -1;
557
558 rom = qemu_mallocz(sizeof(*rom));
559 rom->name = qemu_strdup(file);
560 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
561 if (rom->path == NULL) {
562 fprintf(stderr, "Could not find option rom '%s'\n", rom->name);
563 goto err;
564 }
565
malccef290b2009-10-10 19:02:40 +0400566 fd = open(rom->path, O_RDONLY | O_BINARY);
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200567 if (fd == -1) {
568 fprintf(stderr, "Could not open option rom '%s': %s\n",
569 rom->path, strerror(errno));
570 goto err;
571 }
572
573 rom->align = align;
574 rom->min = min;
575 rom->max = max;
576 rom->romsize = lseek(fd, 0, SEEK_END);
577 rom->data = qemu_mallocz(rom->romsize);
578 lseek(fd, 0, SEEK_SET);
579 rc = read(fd, rom->data, rom->romsize);
580 if (rc != rom->romsize) {
581 fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
582 rom->name, rc, rom->romsize);
583 goto err;
584 }
585 close(fd);
586 rom_insert(rom);
587 return 0;
588
589err:
590 if (fd != -1)
591 close(fd);
592 qemu_free(rom->data);
593 qemu_free(rom->path);
594 qemu_free(rom->name);
595 qemu_free(rom);
596 return -1;
597}
598
599int rom_add_blob(const char *name, const void *blob, size_t len,
600 target_phys_addr_t min, target_phys_addr_t max, int align)
601{
602 Rom *rom;
603
604 rom = qemu_mallocz(sizeof(*rom));
605 rom->name = qemu_strdup(name);
606 rom->align = align;
607 rom->min = min;
608 rom->max = max;
609 rom->romsize = len;
610 rom->data = qemu_mallocz(rom->romsize);
611 memcpy(rom->data, blob, len);
612 rom_insert(rom);
613 return 0;
614}
615
616static void rom_reset(void *unused)
617{
618 Rom *rom;
619
620 QTAILQ_FOREACH(rom, &roms, next) {
621 if (rom->data == NULL)
622 continue;
623 cpu_physical_memory_write_rom(rom->addr, rom->data, rom->romsize);
624 if (rom->isrom) {
625 /* rom needs to be written only once */
626 qemu_free(rom->data);
627 rom->data = NULL;
628 }
629 }
630}
631
632int rom_load_all(void)
633{
634 target_phys_addr_t addr = 0;
635 int memtype;
636 Rom *rom;
637
638 QTAILQ_FOREACH(rom, &roms, next) {
639 if (addr < rom->min)
640 addr = rom->min;
641 if (rom->max) {
642 /* load address range */
643 if (rom->align) {
644 addr += (rom->align-1);
645 addr &= ~(rom->align-1);
646 }
647 if (addr + rom->romsize > rom->max) {
648 fprintf(stderr, "rom: out of memory (rom %s, "
649 "addr 0x" TARGET_FMT_plx
650 ", size 0x%zx, max 0x" TARGET_FMT_plx ")\n",
651 rom->name, addr, rom->romsize, rom->max);
652 return -1;
653 }
654 } else {
655 /* fixed address requested */
656 if (addr != rom->min) {
657 fprintf(stderr, "rom: requested regions overlap "
658 "(rom %s. free=0x" TARGET_FMT_plx
659 ", addr=0x" TARGET_FMT_plx ")\n",
660 rom->name, addr, rom->min);
661 return -1;
662 }
663 }
664 rom->addr = addr;
665 addr += rom->romsize;
666 memtype = cpu_get_physical_page_desc(rom->addr) & (3 << IO_MEM_SHIFT);
667 if (memtype == IO_MEM_ROM)
668 rom->isrom = 1;
669 }
670 qemu_register_reset(rom_reset, NULL);
671 rom_reset(NULL);
672 return 0;
673}
674
Gerd Hoffmann3c178e72009-10-07 13:37:06 +0200675static Rom *find_rom(target_phys_addr_t addr)
676{
677 Rom *rom;
678
679 QTAILQ_FOREACH(rom, &roms, next) {
680 if (rom->max)
681 continue;
682 if (rom->min > addr)
683 continue;
684 if (rom->min + rom->romsize < addr)
685 continue;
686 return rom;
687 }
688 return NULL;
689}
690
691void *rom_ptr(target_phys_addr_t addr)
692{
693 Rom *rom;
694
695 rom = find_rom(addr);
696 if (!rom || !rom->data)
697 return NULL;
698 return rom->data + (addr - rom->min);
699}
700
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200701void do_info_roms(Monitor *mon)
702{
703 Rom *rom;
704
705 QTAILQ_FOREACH(rom, &roms, next) {
706 monitor_printf(mon, "addr=" TARGET_FMT_plx
707 " size=0x%06zx mem=%s name=\"%s\" \n",
708 rom->addr, rom->romsize,
709 rom->isrom ? "rom" : "ram",
710 rom->name);
711 }
712}