]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/loader.c
target-arm: implement BE32 mode in system emulation
[mirror_qemu.git] / hw / core / loader.c
CommitLineData
5fe141fd
FB
1/*
2 * QEMU Executable loader
5fafdf24 3 *
5fe141fd 4 * Copyright (c) 2006 Fabrice Bellard
5fafdf24 5 *
5fe141fd
FB
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.
5a123577
AL
23 *
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 *
fad6cb1a 41 * You should have received a copy of the GNU General Public License along
8167ee88 42 * with this program; if not, see <http://www.gnu.org/licenses/>.
5fe141fd 43 */
5a123577 44
18c86e2b 45#include "qemu/osdep.h"
83c9f4ca 46#include "hw/hw.h"
76cad711 47#include "disas/disas.h"
83c9089e 48#include "monitor/monitor.h"
9c17d615 49#include "sysemu/sysemu.h"
47b43a1f 50#include "uboot_image.h"
83c9f4ca 51#include "hw/loader.h"
0d09e41a 52#include "hw/nvram/fw_cfg.h"
022c62cb
PB
53#include "exec/memory.h"
54#include "exec/address-spaces.h"
71ae9e94 55#include "hw/boards.h"
5fe141fd 56
5a123577
AL
57#include <zlib.h>
58
97fe84f5
PB
59static int roms_loaded;
60
5fe141fd
FB
61/* return the size or -1 if error */
62int get_image_size(const char *filename)
63{
64 int fd, size;
65 fd = open(filename, O_RDONLY | O_BINARY);
66 if (fd < 0)
67 return -1;
68 size = lseek(fd, 0, SEEK_END);
69 close(fd);
70 return size;
71}
72
73/* return the size or -1 if error */
293f78bc 74/* deprecated, because caller does not specify buffer size! */
5fe141fd
FB
75int load_image(const char *filename, uint8_t *addr)
76{
77 int fd, size;
78 fd = open(filename, O_RDONLY | O_BINARY);
79 if (fd < 0)
80 return -1;
81 size = lseek(fd, 0, SEEK_END);
ddd2eab7
GA
82 if (size == -1) {
83 fprintf(stderr, "file %-20s: get size error: %s\n",
84 filename, strerror(errno));
85 close(fd);
86 return -1;
87 }
88
5fe141fd
FB
89 lseek(fd, 0, SEEK_SET);
90 if (read(fd, addr, size) != size) {
91 close(fd);
92 return -1;
93 }
94 close(fd);
95 return size;
96}
97
ea87616d
BH
98/* return the size or -1 if error */
99ssize_t load_image_size(const char *filename, void *addr, size_t size)
100{
101 int fd;
102 ssize_t actsize;
103
104 fd = open(filename, O_RDONLY | O_BINARY);
105 if (fd < 0) {
106 return -1;
107 }
108
109 actsize = read(fd, addr, size);
110 if (actsize < 0) {
111 close(fd);
112 return -1;
113 }
114 close(fd);
115
116 return actsize;
117}
118
293f78bc 119/* read()-like version */
725e14e9 120ssize_t read_targphys(const char *name,
a8170e5e 121 int fd, hwaddr dst_addr, size_t nbytes)
293f78bc 122{
45a50b16 123 uint8_t *buf;
725e14e9 124 ssize_t did;
45a50b16 125
7267c094 126 buf = g_malloc(nbytes);
45a50b16
GH
127 did = read(fd, buf, nbytes);
128 if (did > 0)
129 rom_add_blob_fixed("read", buf, did, dst_addr);
7267c094 130 g_free(buf);
45a50b16 131 return did;
293f78bc
BS
132}
133
134/* return the size or -1 if error */
135int load_image_targphys(const char *filename,
a8170e5e 136 hwaddr addr, uint64_t max_sz)
293f78bc 137{
45a50b16 138 int size;
293f78bc 139
45a50b16 140 size = get_image_size(filename);
17df768c
BH
141 if (size > max_sz) {
142 return -1;
143 }
144 if (size > 0) {
2e55e842 145 rom_add_file_fixed(filename, addr, -1);
17df768c 146 }
45a50b16 147 return size;
293f78bc
BS
148}
149
76151cac
PM
150int load_image_mr(const char *filename, MemoryRegion *mr)
151{
152 int size;
153
154 if (!memory_access_is_direct(mr, false)) {
155 /* Can only load an image into RAM or ROM */
156 return -1;
157 }
158
159 size = get_image_size(filename);
160
161 if (size > memory_region_size(mr)) {
162 return -1;
163 }
164 if (size > 0) {
165 if (rom_add_file_mr(filename, mr, -1) < 0) {
166 return -1;
167 }
168 }
169 return size;
170}
171
a8170e5e 172void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
293f78bc
BS
173 const char *source)
174{
293f78bc 175 const char *nulp;
3c178e72 176 char *ptr;
293f78bc
BS
177
178 if (buf_size <= 0) return;
179 nulp = memchr(source, 0, buf_size);
180 if (nulp) {
3c178e72 181 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
293f78bc 182 } else {
3c178e72
GH
183 rom_add_blob_fixed(name, source, buf_size, dest);
184 ptr = rom_ptr(dest + buf_size - 1);
185 *ptr = 0;
293f78bc
BS
186 }
187}
188
5fe141fd
FB
189/* A.OUT loader */
190
191struct exec
192{
193 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
194 uint32_t a_text; /* length of text, in bytes */
195 uint32_t a_data; /* length of data, in bytes */
196 uint32_t a_bss; /* length of uninitialized data area, in bytes */
197 uint32_t a_syms; /* length of symbol table data in file, in bytes */
198 uint32_t a_entry; /* start address */
199 uint32_t a_trsize; /* length of relocation info for text, in bytes */
200 uint32_t a_drsize; /* length of relocation info for data, in bytes */
201};
202
5fe141fd
FB
203static void bswap_ahdr(struct exec *e)
204{
205 bswap32s(&e->a_info);
206 bswap32s(&e->a_text);
207 bswap32s(&e->a_data);
208 bswap32s(&e->a_bss);
209 bswap32s(&e->a_syms);
210 bswap32s(&e->a_entry);
211 bswap32s(&e->a_trsize);
212 bswap32s(&e->a_drsize);
213}
5fe141fd
FB
214
215#define N_MAGIC(exec) ((exec).a_info & 0xffff)
216#define OMAGIC 0407
217#define NMAGIC 0410
218#define ZMAGIC 0413
219#define QMAGIC 0314
220#define _N_HDROFF(x) (1024 - sizeof (struct exec))
221#define N_TXTOFF(x) \
222 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
223 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
ca20cf32
BS
224#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
225#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
5fe141fd 226
ca20cf32 227#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
5fe141fd 228
ca20cf32
BS
229#define N_DATADDR(x, target_page_size) \
230 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
231 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
5fe141fd
FB
232
233
a8170e5e
AK
234int load_aout(const char *filename, hwaddr addr, int max_sz,
235 int bswap_needed, hwaddr target_page_size)
5fe141fd 236{
725e14e9
MA
237 int fd;
238 ssize_t size, ret;
5fe141fd
FB
239 struct exec e;
240 uint32_t magic;
241
242 fd = open(filename, O_RDONLY | O_BINARY);
243 if (fd < 0)
244 return -1;
245
246 size = read(fd, &e, sizeof(e));
247 if (size < 0)
248 goto fail;
249
ca20cf32
BS
250 if (bswap_needed) {
251 bswap_ahdr(&e);
252 }
5fe141fd
FB
253
254 magic = N_MAGIC(e);
255 switch (magic) {
256 case ZMAGIC:
257 case QMAGIC:
258 case OMAGIC:
293f78bc
BS
259 if (e.a_text + e.a_data > max_sz)
260 goto fail;
5fe141fd 261 lseek(fd, N_TXTOFF(e), SEEK_SET);
45a50b16 262 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
5fe141fd
FB
263 if (size < 0)
264 goto fail;
265 break;
266 case NMAGIC:
ca20cf32 267 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
293f78bc 268 goto fail;
5fe141fd 269 lseek(fd, N_TXTOFF(e), SEEK_SET);
45a50b16 270 size = read_targphys(filename, fd, addr, e.a_text);
5fe141fd
FB
271 if (size < 0)
272 goto fail;
45a50b16 273 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
ca20cf32 274 e.a_data);
5fe141fd
FB
275 if (ret < 0)
276 goto fail;
277 size += ret;
278 break;
279 default:
280 goto fail;
281 }
282 close(fd);
283 return size;
284 fail:
285 close(fd);
286 return -1;
287}
288
289/* ELF loader */
290
6cbfb86f 291static void *load_at(int fd, off_t offset, size_t size)
5fe141fd
FB
292{
293 void *ptr;
294 if (lseek(fd, offset, SEEK_SET) < 0)
295 return NULL;
7267c094 296 ptr = g_malloc(size);
5fe141fd 297 if (read(fd, ptr, size) != size) {
7267c094 298 g_free(ptr);
5fe141fd
FB
299 return NULL;
300 }
301 return ptr;
302}
303
3efa9a67 304#ifdef ELF_CLASS
305#undef ELF_CLASS
306#endif
5fe141fd
FB
307
308#define ELF_CLASS ELFCLASS32
309#include "elf.h"
310
311#define SZ 32
312#define elf_word uint32_t
82790064 313#define elf_sword int32_t
5fe141fd 314#define bswapSZs bswap32s
83c9f4ca 315#include "hw/elf_ops.h"
5fe141fd
FB
316
317#undef elfhdr
318#undef elf_phdr
319#undef elf_shdr
320#undef elf_sym
5dce07e1 321#undef elf_rela
5fe141fd
FB
322#undef elf_note
323#undef elf_word
82790064 324#undef elf_sword
5fe141fd
FB
325#undef bswapSZs
326#undef SZ
327#define elfhdr elf64_hdr
328#define elf_phdr elf64_phdr
329#define elf_note elf64_note
330#define elf_shdr elf64_shdr
331#define elf_sym elf64_sym
5dce07e1 332#define elf_rela elf64_rela
5fe141fd 333#define elf_word uint64_t
82790064 334#define elf_sword int64_t
5fe141fd
FB
335#define bswapSZs bswap64s
336#define SZ 64
83c9f4ca 337#include "hw/elf_ops.h"
5fe141fd 338
18674b26
AK
339const char *load_elf_strerror(int error)
340{
341 switch (error) {
342 case 0:
343 return "No error";
344 case ELF_LOAD_FAILED:
345 return "Failed to load ELF";
346 case ELF_LOAD_NOT_ELF:
347 return "The image is not ELF";
348 case ELF_LOAD_WRONG_ARCH:
349 return "The image is from incompatible architecture";
350 case ELF_LOAD_WRONG_ENDIAN:
351 return "The image has incorrect endianness";
352 default:
353 return "Unknown error";
354 }
355}
356
5fe141fd 357/* return < 0 if error, otherwise the number of bytes loaded in memory */
409dbce5
AJ
358int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
359 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
360 uint64_t *highaddr, int big_endian, int elf_machine, int clear_lsb)
5fe141fd 361{
18674b26 362 int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED;
5fe141fd
FB
363 uint8_t e_ident[EI_NIDENT];
364
699e4642 365 fd = open(filename, O_RDONLY | O_BINARY);
5fe141fd
FB
366 if (fd < 0) {
367 perror(filename);
368 return -1;
369 }
370 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
371 goto fail;
372 if (e_ident[0] != ELFMAG0 ||
373 e_ident[1] != ELFMAG1 ||
374 e_ident[2] != ELFMAG2 ||
18674b26
AK
375 e_ident[3] != ELFMAG3) {
376 ret = ELF_LOAD_NOT_ELF;
5fe141fd 377 goto fail;
18674b26 378 }
e2542fe2 379#ifdef HOST_WORDS_BIGENDIAN
5fe141fd
FB
380 data_order = ELFDATA2MSB;
381#else
382 data_order = ELFDATA2LSB;
383#endif
384 must_swab = data_order != e_ident[EI_DATA];
ca20cf32
BS
385 if (big_endian) {
386 target_data_order = ELFDATA2MSB;
387 } else {
388 target_data_order = ELFDATA2LSB;
389 }
9042c0e2 390
cedf9a6f 391 if (target_data_order != e_ident[EI_DATA]) {
18674b26 392 ret = ELF_LOAD_WRONG_ENDIAN;
cedf9a6f
BS
393 goto fail;
394 }
9042c0e2 395
5fe141fd
FB
396 lseek(fd, 0, SEEK_SET);
397 if (e_ident[EI_CLASS] == ELFCLASS64) {
409dbce5
AJ
398 ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab,
399 pentry, lowaddr, highaddr, elf_machine, clear_lsb);
5fe141fd 400 } else {
409dbce5
AJ
401 ret = load_elf32(filename, fd, translate_fn, translate_opaque, must_swab,
402 pentry, lowaddr, highaddr, elf_machine, clear_lsb);
5fe141fd
FB
403 }
404
5fe141fd
FB
405 fail:
406 close(fd);
18674b26 407 return ret;
5fe141fd 408}
1c7b3754 409
c227f099 410static void bswap_uboot_header(uboot_image_header_t *hdr)
1c7b3754 411{
e2542fe2 412#ifndef HOST_WORDS_BIGENDIAN
1c7b3754
PB
413 bswap32s(&hdr->ih_magic);
414 bswap32s(&hdr->ih_hcrc);
415 bswap32s(&hdr->ih_time);
416 bswap32s(&hdr->ih_size);
417 bswap32s(&hdr->ih_load);
418 bswap32s(&hdr->ih_ep);
419 bswap32s(&hdr->ih_dcrc);
420#endif
421}
422
5a123577
AL
423
424#define ZALLOC_ALIGNMENT 16
425
426static void *zalloc(void *x, unsigned items, unsigned size)
427{
428 void *p;
429
430 size *= items;
431 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
432
7267c094 433 p = g_malloc(size);
5a123577
AL
434
435 return (p);
436}
437
d084eab6 438static void zfree(void *x, void *addr)
5a123577 439{
7267c094 440 g_free(addr);
5a123577
AL
441}
442
443
444#define HEAD_CRC 2
445#define EXTRA_FIELD 4
446#define ORIG_NAME 8
447#define COMMENT 0x10
448#define RESERVED 0xe0
449
450#define DEFLATED 8
451
5025d542 452/* This is the usual maximum in uboot, so if a uImage overflows this, it would
5a123577 453 * overflow on real hardware too. */
5025d542 454#define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
5a123577
AL
455
456static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
457 size_t srclen)
458{
459 z_stream s;
460 ssize_t dstbytes;
461 int r, i, flags;
462
463 /* skip header */
464 i = 10;
465 flags = src[3];
466 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
467 puts ("Error: Bad gzipped data\n");
468 return -1;
469 }
470 if ((flags & EXTRA_FIELD) != 0)
471 i = 12 + src[10] + (src[11] << 8);
472 if ((flags & ORIG_NAME) != 0)
473 while (src[i++] != 0)
474 ;
475 if ((flags & COMMENT) != 0)
476 while (src[i++] != 0)
477 ;
478 if ((flags & HEAD_CRC) != 0)
479 i += 2;
480 if (i >= srclen) {
481 puts ("Error: gunzip out of data in header\n");
482 return -1;
483 }
484
485 s.zalloc = zalloc;
d084eab6 486 s.zfree = zfree;
5a123577
AL
487
488 r = inflateInit2(&s, -MAX_WBITS);
489 if (r != Z_OK) {
490 printf ("Error: inflateInit2() returned %d\n", r);
491 return (-1);
492 }
493 s.next_in = src + i;
494 s.avail_in = srclen - i;
495 s.next_out = dst;
496 s.avail_out = dstlen;
497 r = inflate(&s, Z_FINISH);
498 if (r != Z_OK && r != Z_STREAM_END) {
499 printf ("Error: inflate() returned %d\n", r);
500 return -1;
501 }
502 dstbytes = s.next_out - (unsigned char *) dst;
503 inflateEnd(&s);
504
505 return dstbytes;
506}
507
1c7b3754 508/* Load a U-Boot image. */
84aee0de 509static int load_uboot_image(const char *filename, hwaddr *ep, hwaddr *loadaddr,
25bda50a
MF
510 int *is_linux, uint8_t image_type,
511 uint64_t (*translate_fn)(void *, uint64_t),
512 void *translate_opaque)
1c7b3754 513{
1c7b3754
PB
514 int fd;
515 int size;
84aee0de 516 hwaddr address;
c227f099
AL
517 uboot_image_header_t h;
518 uboot_image_header_t *hdr = &h;
1c7b3754 519 uint8_t *data = NULL;
265ca29a 520 int ret = -1;
84aee0de 521 int do_uncompress = 0;
1c7b3754
PB
522
523 fd = open(filename, O_RDONLY | O_BINARY);
524 if (fd < 0)
525 return -1;
526
c227f099 527 size = read(fd, hdr, sizeof(uboot_image_header_t));
1c7b3754 528 if (size < 0)
265ca29a 529 goto out;
1c7b3754
PB
530
531 bswap_uboot_header(hdr);
532
533 if (hdr->ih_magic != IH_MAGIC)
265ca29a 534 goto out;
1c7b3754 535
84aee0de
SB
536 if (hdr->ih_type != image_type) {
537 fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
538 image_type);
265ca29a 539 goto out;
1c7b3754
PB
540 }
541
84aee0de
SB
542 /* TODO: Implement other image types. */
543 switch (hdr->ih_type) {
544 case IH_TYPE_KERNEL:
545 address = hdr->ih_load;
25bda50a
MF
546 if (translate_fn) {
547 address = translate_fn(translate_opaque, address);
548 }
84aee0de
SB
549 if (loadaddr) {
550 *loadaddr = hdr->ih_load;
551 }
552
553 switch (hdr->ih_comp) {
554 case IH_COMP_NONE:
555 break;
556 case IH_COMP_GZIP:
557 do_uncompress = 1;
558 break;
559 default:
560 fprintf(stderr,
561 "Unable to load u-boot images with compression type %d\n",
562 hdr->ih_comp);
563 goto out;
564 }
565
566 if (ep) {
567 *ep = hdr->ih_ep;
568 }
569
570 /* TODO: Check CPU type. */
571 if (is_linux) {
572 if (hdr->ih_os == IH_OS_LINUX) {
573 *is_linux = 1;
574 } else {
575 *is_linux = 0;
576 }
577 }
578
579 break;
580 case IH_TYPE_RAMDISK:
581 address = *loadaddr;
5a123577
AL
582 break;
583 default:
84aee0de 584 fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
265ca29a 585 goto out;
1c7b3754
PB
586 }
587
7267c094 588 data = g_malloc(hdr->ih_size);
1c7b3754
PB
589
590 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
591 fprintf(stderr, "Error reading file\n");
265ca29a 592 goto out;
1c7b3754
PB
593 }
594
84aee0de 595 if (do_uncompress) {
5a123577
AL
596 uint8_t *compressed_data;
597 size_t max_bytes;
598 ssize_t bytes;
599
600 compressed_data = data;
601 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
7267c094 602 data = g_malloc(max_bytes);
5a123577
AL
603
604 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
7267c094 605 g_free(compressed_data);
5a123577
AL
606 if (bytes < 0) {
607 fprintf(stderr, "Unable to decompress gzipped image!\n");
608 goto out;
609 }
610 hdr->ih_size = bytes;
611 }
612
84aee0de 613 rom_add_blob_fixed(filename, data, hdr->ih_size, address);
21cafd08 614
265ca29a 615 ret = hdr->ih_size;
1c7b3754 616
265ca29a 617out:
ef1e1e07 618 g_free(data);
1c7b3754 619 close(fd);
265ca29a 620 return ret;
1c7b3754 621}
45a50b16 622
84aee0de 623int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
25bda50a
MF
624 int *is_linux,
625 uint64_t (*translate_fn)(void *, uint64_t),
626 void *translate_opaque)
84aee0de 627{
25bda50a
MF
628 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
629 translate_fn, translate_opaque);
84aee0de
SB
630}
631
632/* Load a ramdisk. */
633int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
634{
25bda50a
MF
635 return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
636 NULL, NULL);
84aee0de
SB
637}
638
7d48a0f7
LE
639/* Load a gzip-compressed kernel to a dynamically allocated buffer. */
640int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
641 uint8_t **buffer)
235e74af
RJ
642{
643 uint8_t *compressed_data = NULL;
644 uint8_t *data = NULL;
645 gsize len;
646 ssize_t bytes;
647 int ret = -1;
648
649 if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
650 NULL)) {
651 goto out;
652 }
653
654 /* Is it a gzip-compressed file? */
655 if (len < 2 ||
656 compressed_data[0] != 0x1f ||
657 compressed_data[1] != 0x8b) {
658 goto out;
659 }
660
661 if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
662 max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
663 }
664
665 data = g_malloc(max_sz);
666 bytes = gunzip(data, max_sz, compressed_data, len);
667 if (bytes < 0) {
668 fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
669 filename);
670 goto out;
671 }
672
7d48a0f7
LE
673 /* trim to actual size and return to caller */
674 *buffer = g_realloc(data, bytes);
235e74af 675 ret = bytes;
7d48a0f7
LE
676 /* ownership has been transferred to caller */
677 data = NULL;
235e74af
RJ
678
679 out:
680 g_free(compressed_data);
681 g_free(data);
682 return ret;
683}
684
7d48a0f7
LE
685/* Load a gzip-compressed kernel. */
686int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
687{
688 int bytes;
689 uint8_t *data;
690
691 bytes = load_image_gzipped_buffer(filename, max_sz, &data);
692 if (bytes != -1) {
693 rom_add_blob_fixed(filename, data, bytes, addr);
694 g_free(data);
695 }
696 return bytes;
697}
698
45a50b16
GH
699/*
700 * Functions for reboot-persistent memory regions.
701 * - used for vga bios and option roms.
702 * - also linux kernel (-kernel / -initrd).
703 */
704
705typedef struct Rom Rom;
706
707struct Rom {
708 char *name;
709 char *path;
d60fa42e
FC
710
711 /* datasize is the amount of memory allocated in "data". If datasize is less
712 * than romsize, it means that the area from datasize to romsize is filled
713 * with zeros.
714 */
45a50b16 715 size_t romsize;
d60fa42e
FC
716 size_t datasize;
717
45a50b16 718 uint8_t *data;
04920fc0 719 MemoryRegion *mr;
45a50b16 720 int isrom;
379526a4
GH
721 char *fw_dir;
722 char *fw_file;
45a50b16 723
a8170e5e 724 hwaddr addr;
45a50b16
GH
725 QTAILQ_ENTRY(Rom) next;
726};
727
8832cb80 728static FWCfgState *fw_cfg;
45a50b16
GH
729static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
730
731static void rom_insert(Rom *rom)
732{
733 Rom *item;
734
97fe84f5
PB
735 if (roms_loaded) {
736 hw_error ("ROM images must be loaded at startup\n");
737 }
738
45a50b16
GH
739 /* list is ordered by load address */
740 QTAILQ_FOREACH(item, &roms, next) {
632cf034 741 if (rom->addr >= item->addr)
45a50b16
GH
742 continue;
743 QTAILQ_INSERT_BEFORE(item, rom, next);
744 return;
745 }
746 QTAILQ_INSERT_TAIL(&roms, rom, next);
747}
748
a1666142
MT
749static void fw_cfg_resized(const char *id, uint64_t length, void *host)
750{
751 if (fw_cfg) {
752 fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
753 }
754}
755
04920fc0
MT
756static void *rom_set_mr(Rom *rom, Object *owner, const char *name)
757{
758 void *data;
759
760 rom->mr = g_malloc(sizeof(*rom->mr));
a1666142
MT
761 memory_region_init_resizeable_ram(rom->mr, owner, name,
762 rom->datasize, rom->romsize,
763 fw_cfg_resized,
df8abec8 764 &error_fatal);
04920fc0
MT
765 memory_region_set_readonly(rom->mr, true);
766 vmstate_register_ram_global(rom->mr);
767
768 data = memory_region_get_ram_ptr(rom->mr);
769 memcpy(data, rom->data, rom->datasize);
770
771 return data;
772}
773
bdb5ee30 774int rom_add_file(const char *file, const char *fw_dir,
ac41881b 775 hwaddr addr, int32_t bootindex,
76151cac 776 bool option_rom, MemoryRegion *mr)
45a50b16 777{
71ae9e94 778 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
45a50b16
GH
779 Rom *rom;
780 int rc, fd = -1;
2e55e842 781 char devpath[100];
45a50b16 782
7267c094
AL
783 rom = g_malloc0(sizeof(*rom));
784 rom->name = g_strdup(file);
45a50b16
GH
785 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
786 if (rom->path == NULL) {
7267c094 787 rom->path = g_strdup(file);
45a50b16
GH
788 }
789
cef290b8 790 fd = open(rom->path, O_RDONLY | O_BINARY);
45a50b16
GH
791 if (fd == -1) {
792 fprintf(stderr, "Could not open option rom '%s': %s\n",
793 rom->path, strerror(errno));
794 goto err;
795 }
796
bdb5ee30 797 if (fw_dir) {
7267c094
AL
798 rom->fw_dir = g_strdup(fw_dir);
799 rom->fw_file = g_strdup(file);
bdb5ee30 800 }
d60fa42e
FC
801 rom->addr = addr;
802 rom->romsize = lseek(fd, 0, SEEK_END);
ddd2eab7
GA
803 if (rom->romsize == -1) {
804 fprintf(stderr, "rom: file %-20s: get size error: %s\n",
805 rom->name, strerror(errno));
806 goto err;
807 }
808
d60fa42e
FC
809 rom->datasize = rom->romsize;
810 rom->data = g_malloc0(rom->datasize);
45a50b16 811 lseek(fd, 0, SEEK_SET);
d60fa42e
FC
812 rc = read(fd, rom->data, rom->datasize);
813 if (rc != rom->datasize) {
45a50b16 814 fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
d60fa42e 815 rom->name, rc, rom->datasize);
45a50b16
GH
816 goto err;
817 }
818 close(fd);
819 rom_insert(rom);
de1f34cb
GN
820 if (rom->fw_file && fw_cfg) {
821 const char *basename;
35c12e60 822 char fw_file_name[FW_CFG_MAX_FILE_PATH];
04920fc0 823 void *data;
de1f34cb
GN
824
825 basename = strrchr(rom->fw_file, '/');
826 if (basename) {
827 basename++;
828 } else {
829 basename = rom->fw_file;
830 }
831 snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
832 basename);
2e55e842 833 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
04920fc0 834
71ae9e94 835 if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
04920fc0
MT
836 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
837 } else {
838 data = rom->data;
839 }
840
841 fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
2e55e842 842 } else {
76151cac
PM
843 if (mr) {
844 rom->mr = mr;
845 snprintf(devpath, sizeof(devpath), "/rom@%s", file);
846 } else {
847 snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
848 }
de1f34cb 849 }
2e55e842
GN
850
851 add_boot_device_path(bootindex, NULL, devpath);
45a50b16
GH
852 return 0;
853
854err:
855 if (fd != -1)
856 close(fd);
7267c094
AL
857 g_free(rom->data);
858 g_free(rom->path);
859 g_free(rom->name);
860 g_free(rom);
45a50b16
GH
861 return -1;
862}
863
339240b5 864MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
a1666142 865 size_t max_len, hwaddr addr, const char *fw_file_name,
48354cc5 866 FWCfgReadCallback fw_callback, void *callback_opaque)
45a50b16 867{
71ae9e94 868 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
45a50b16 869 Rom *rom;
339240b5 870 MemoryRegion *mr = NULL;
45a50b16 871
d60fa42e
FC
872 rom = g_malloc0(sizeof(*rom));
873 rom->name = g_strdup(name);
874 rom->addr = addr;
a1666142 875 rom->romsize = max_len ? max_len : len;
d60fa42e
FC
876 rom->datasize = len;
877 rom->data = g_malloc0(rom->datasize);
45a50b16
GH
878 memcpy(rom->data, blob, len);
879 rom_insert(rom);
48354cc5
MT
880 if (fw_file_name && fw_cfg) {
881 char devpath[100];
ad5b88b1 882 void *data;
48354cc5
MT
883
884 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
885
71ae9e94 886 if (mc->rom_file_has_mr) {
48354cc5 887 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
339240b5 888 mr = rom->mr;
48354cc5
MT
889 } else {
890 data = rom->data;
891 }
892
893 fw_cfg_add_file_callback(fw_cfg, fw_file_name,
894 fw_callback, callback_opaque,
a1666142 895 data, rom->datasize);
48354cc5 896 }
339240b5 897 return mr;
45a50b16
GH
898}
899
d60fa42e
FC
900/* This function is specific for elf program because we don't need to allocate
901 * all the rom. We just allocate the first part and the rest is just zeros. This
902 * is why romsize and datasize are different. Also, this function seize the
903 * memory ownership of "data", so we don't have to allocate and copy the buffer.
904 */
905int rom_add_elf_program(const char *name, void *data, size_t datasize,
906 size_t romsize, hwaddr addr)
907{
908 Rom *rom;
909
910 rom = g_malloc0(sizeof(*rom));
911 rom->name = g_strdup(name);
912 rom->addr = addr;
913 rom->datasize = datasize;
914 rom->romsize = romsize;
915 rom->data = data;
916 rom_insert(rom);
917 return 0;
918}
919
de2aff17
GH
920int rom_add_vga(const char *file)
921{
76151cac 922 return rom_add_file(file, "vgaroms", 0, -1, true, NULL);
de2aff17
GH
923}
924
2e55e842 925int rom_add_option(const char *file, int32_t bootindex)
de2aff17 926{
76151cac 927 return rom_add_file(file, "genroms", 0, bootindex, true, NULL);
de2aff17
GH
928}
929
45a50b16
GH
930static void rom_reset(void *unused)
931{
932 Rom *rom;
933
934 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
935 if (rom->fw_file) {
936 continue;
937 }
bdb5ee30 938 if (rom->data == NULL) {
45a50b16 939 continue;
bdb5ee30 940 }
04920fc0
MT
941 if (rom->mr) {
942 void *host = memory_region_get_ram_ptr(rom->mr);
943 memcpy(host, rom->data, rom->datasize);
944 } else {
2a221651
EI
945 cpu_physical_memory_write_rom(&address_space_memory,
946 rom->addr, rom->data, rom->datasize);
04920fc0 947 }
45a50b16
GH
948 if (rom->isrom) {
949 /* rom needs to be written only once */
7267c094 950 g_free(rom->data);
45a50b16
GH
951 rom->data = NULL;
952 }
582b55a9
AG
953 /*
954 * The rom loader is really on the same level as firmware in the guest
955 * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
956 * that the instruction cache for that new region is clear, so that the
957 * CPU definitely fetches its instructions from the just written data.
958 */
959 cpu_flush_icache_range(rom->addr, rom->datasize);
45a50b16
GH
960 }
961}
962
6b3f7f63 963int rom_check_and_register_reset(void)
45a50b16 964{
a8170e5e 965 hwaddr addr = 0;
dcc5cd33 966 MemoryRegionSection section;
45a50b16
GH
967 Rom *rom;
968
969 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
970 if (rom->fw_file) {
971 continue;
972 }
632cf034
GH
973 if (addr > rom->addr) {
974 fprintf(stderr, "rom: requested regions overlap "
975 "(rom %s. free=0x" TARGET_FMT_plx
976 ", addr=0x" TARGET_FMT_plx ")\n",
977 rom->name, addr, rom->addr);
978 return -1;
45a50b16 979 }
632cf034 980 addr = rom->addr;
45a50b16 981 addr += rom->romsize;
dcc5cd33 982 section = memory_region_find(get_system_memory(), rom->addr, 1);
052e87b0 983 rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
dfde4e6e 984 memory_region_unref(section.mr);
45a50b16
GH
985 }
986 qemu_register_reset(rom_reset, NULL);
d916b464 987 roms_loaded = 1;
6b3f7f63 988 return 0;
d916b464
MT
989}
990
a88b362c 991void rom_set_fw(FWCfgState *f)
379526a4 992{
8832cb80 993 fw_cfg = f;
379526a4
GH
994}
995
a8170e5e 996static Rom *find_rom(hwaddr addr)
3c178e72
GH
997{
998 Rom *rom;
999
1000 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
1001 if (rom->fw_file) {
1002 continue;
1003 }
04920fc0
MT
1004 if (rom->mr) {
1005 continue;
1006 }
bdb5ee30 1007 if (rom->addr > addr) {
3c178e72 1008 continue;
bdb5ee30
GH
1009 }
1010 if (rom->addr + rom->romsize < addr) {
3c178e72 1011 continue;
bdb5ee30 1012 }
3c178e72
GH
1013 return rom;
1014 }
1015 return NULL;
1016}
1017
935effc2
KW
1018/*
1019 * Copies memory from registered ROMs to dest. Any memory that is contained in
1020 * a ROM between addr and addr + size is copied. Note that this can involve
1021 * multiple ROMs, which need not start at addr and need not end at addr + size.
1022 */
a8170e5e 1023int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
235f86ef 1024{
a8170e5e 1025 hwaddr end = addr + size;
235f86ef
AG
1026 uint8_t *s, *d = dest;
1027 size_t l = 0;
1028 Rom *rom;
1029
1030 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
1031 if (rom->fw_file) {
1032 continue;
1033 }
04920fc0
MT
1034 if (rom->mr) {
1035 continue;
1036 }
bdb5ee30 1037 if (rom->addr + rom->romsize < addr) {
632cf034 1038 continue;
bdb5ee30
GH
1039 }
1040 if (rom->addr > end) {
235f86ef 1041 break;
bdb5ee30 1042 }
235f86ef 1043
632cf034 1044 d = dest + (rom->addr - addr);
235f86ef 1045 s = rom->data;
d60fa42e 1046 l = rom->datasize;
235f86ef 1047
235f86ef
AG
1048 if ((d + l) > (dest + size)) {
1049 l = dest - d;
1050 }
1051
0dd5ce38
MB
1052 if (l > 0) {
1053 memcpy(d, s, l);
1054 }
d60fa42e
FC
1055
1056 if (rom->romsize > rom->datasize) {
1057 /* If datasize is less than romsize, it means that we didn't
1058 * allocate all the ROM because the trailing data are only zeros.
1059 */
1060
1061 d += l;
1062 l = rom->romsize - rom->datasize;
1063
1064 if ((d + l) > (dest + size)) {
1065 /* Rom size doesn't fit in the destination area. Adjust to avoid
1066 * overflow.
1067 */
1068 l = dest - d;
1069 }
1070
1071 if (l > 0) {
1072 memset(d, 0x0, l);
1073 }
1074 }
235f86ef
AG
1075 }
1076
1077 return (d + l) - dest;
1078}
1079
a8170e5e 1080void *rom_ptr(hwaddr addr)
3c178e72
GH
1081{
1082 Rom *rom;
1083
1084 rom = find_rom(addr);
1085 if (!rom || !rom->data)
1086 return NULL;
632cf034 1087 return rom->data + (addr - rom->addr);
3c178e72
GH
1088}
1089
1ce6be24 1090void hmp_info_roms(Monitor *mon, const QDict *qdict)
45a50b16
GH
1091{
1092 Rom *rom;
1093
1094 QTAILQ_FOREACH(rom, &roms, next) {
04920fc0
MT
1095 if (rom->mr) {
1096 monitor_printf(mon, "%s"
1097 " size=0x%06zx name=\"%s\"\n",
401cf7fd 1098 memory_region_name(rom->mr),
04920fc0
MT
1099 rom->romsize,
1100 rom->name);
1101 } else if (!rom->fw_file) {
632cf034 1102 monitor_printf(mon, "addr=" TARGET_FMT_plx
b2bedb21 1103 " size=0x%06zx mem=%s name=\"%s\"\n",
632cf034
GH
1104 rom->addr, rom->romsize,
1105 rom->isrom ? "rom" : "ram",
1106 rom->name);
1107 } else {
bdb5ee30 1108 monitor_printf(mon, "fw=%s/%s"
b2bedb21 1109 " size=0x%06zx name=\"%s\"\n",
bdb5ee30 1110 rom->fw_dir,
632cf034
GH
1111 rom->fw_file,
1112 rom->romsize,
1113 rom->name);
1114 }
45a50b16
GH
1115 }
1116}