]> git.proxmox.com Git - mirror_qemu.git/blame - hw/core/loader.c
Merge remote-tracking branch 'mst/tags/for_anthony' into stable-1.5
[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
83c9f4ca 45#include "hw/hw.h"
76cad711 46#include "disas/disas.h"
83c9089e 47#include "monitor/monitor.h"
9c17d615 48#include "sysemu/sysemu.h"
47b43a1f 49#include "uboot_image.h"
83c9f4ca 50#include "hw/loader.h"
0d09e41a 51#include "hw/nvram/fw_cfg.h"
022c62cb
PB
52#include "exec/memory.h"
53#include "exec/address-spaces.h"
5fe141fd 54
5a123577
AL
55#include <zlib.h>
56
04920fc0
MT
57bool rom_file_in_ram = true;
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);
82 lseek(fd, 0, SEEK_SET);
83 if (read(fd, addr, size) != size) {
84 close(fd);
85 return -1;
86 }
87 close(fd);
88 return size;
89}
90
293f78bc 91/* read()-like version */
725e14e9 92ssize_t read_targphys(const char *name,
a8170e5e 93 int fd, hwaddr dst_addr, size_t nbytes)
293f78bc 94{
45a50b16 95 uint8_t *buf;
725e14e9 96 ssize_t did;
45a50b16 97
7267c094 98 buf = g_malloc(nbytes);
45a50b16
GH
99 did = read(fd, buf, nbytes);
100 if (did > 0)
101 rom_add_blob_fixed("read", buf, did, dst_addr);
7267c094 102 g_free(buf);
45a50b16 103 return did;
293f78bc
BS
104}
105
106/* return the size or -1 if error */
107int load_image_targphys(const char *filename,
a8170e5e 108 hwaddr addr, uint64_t max_sz)
293f78bc 109{
45a50b16 110 int size;
293f78bc 111
45a50b16 112 size = get_image_size(filename);
17df768c
BH
113 if (size > max_sz) {
114 return -1;
115 }
116 if (size > 0) {
2e55e842 117 rom_add_file_fixed(filename, addr, -1);
17df768c 118 }
45a50b16 119 return size;
293f78bc
BS
120}
121
a8170e5e 122void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
293f78bc
BS
123 const char *source)
124{
293f78bc 125 const char *nulp;
3c178e72 126 char *ptr;
293f78bc
BS
127
128 if (buf_size <= 0) return;
129 nulp = memchr(source, 0, buf_size);
130 if (nulp) {
3c178e72 131 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
293f78bc 132 } else {
3c178e72
GH
133 rom_add_blob_fixed(name, source, buf_size, dest);
134 ptr = rom_ptr(dest + buf_size - 1);
135 *ptr = 0;
293f78bc
BS
136 }
137}
138
5fe141fd
FB
139/* A.OUT loader */
140
141struct exec
142{
143 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
144 uint32_t a_text; /* length of text, in bytes */
145 uint32_t a_data; /* length of data, in bytes */
146 uint32_t a_bss; /* length of uninitialized data area, in bytes */
147 uint32_t a_syms; /* length of symbol table data in file, in bytes */
148 uint32_t a_entry; /* start address */
149 uint32_t a_trsize; /* length of relocation info for text, in bytes */
150 uint32_t a_drsize; /* length of relocation info for data, in bytes */
151};
152
5fe141fd
FB
153static void bswap_ahdr(struct exec *e)
154{
155 bswap32s(&e->a_info);
156 bswap32s(&e->a_text);
157 bswap32s(&e->a_data);
158 bswap32s(&e->a_bss);
159 bswap32s(&e->a_syms);
160 bswap32s(&e->a_entry);
161 bswap32s(&e->a_trsize);
162 bswap32s(&e->a_drsize);
163}
5fe141fd
FB
164
165#define N_MAGIC(exec) ((exec).a_info & 0xffff)
166#define OMAGIC 0407
167#define NMAGIC 0410
168#define ZMAGIC 0413
169#define QMAGIC 0314
170#define _N_HDROFF(x) (1024 - sizeof (struct exec))
171#define N_TXTOFF(x) \
172 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
173 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
ca20cf32
BS
174#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
175#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
5fe141fd 176
ca20cf32 177#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
5fe141fd 178
ca20cf32
BS
179#define N_DATADDR(x, target_page_size) \
180 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
181 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
5fe141fd
FB
182
183
a8170e5e
AK
184int load_aout(const char *filename, hwaddr addr, int max_sz,
185 int bswap_needed, hwaddr target_page_size)
5fe141fd 186{
725e14e9
MA
187 int fd;
188 ssize_t size, ret;
5fe141fd
FB
189 struct exec e;
190 uint32_t magic;
191
192 fd = open(filename, O_RDONLY | O_BINARY);
193 if (fd < 0)
194 return -1;
195
196 size = read(fd, &e, sizeof(e));
197 if (size < 0)
198 goto fail;
199
ca20cf32
BS
200 if (bswap_needed) {
201 bswap_ahdr(&e);
202 }
5fe141fd
FB
203
204 magic = N_MAGIC(e);
205 switch (magic) {
206 case ZMAGIC:
207 case QMAGIC:
208 case OMAGIC:
293f78bc
BS
209 if (e.a_text + e.a_data > max_sz)
210 goto fail;
5fe141fd 211 lseek(fd, N_TXTOFF(e), SEEK_SET);
45a50b16 212 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
5fe141fd
FB
213 if (size < 0)
214 goto fail;
215 break;
216 case NMAGIC:
ca20cf32 217 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
293f78bc 218 goto fail;
5fe141fd 219 lseek(fd, N_TXTOFF(e), SEEK_SET);
45a50b16 220 size = read_targphys(filename, fd, addr, e.a_text);
5fe141fd
FB
221 if (size < 0)
222 goto fail;
45a50b16 223 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
ca20cf32 224 e.a_data);
5fe141fd
FB
225 if (ret < 0)
226 goto fail;
227 size += ret;
228 break;
229 default:
230 goto fail;
231 }
232 close(fd);
233 return size;
234 fail:
235 close(fd);
236 return -1;
237}
238
239/* ELF loader */
240
241static void *load_at(int fd, int offset, int size)
242{
243 void *ptr;
244 if (lseek(fd, offset, SEEK_SET) < 0)
245 return NULL;
7267c094 246 ptr = g_malloc(size);
5fe141fd 247 if (read(fd, ptr, size) != size) {
7267c094 248 g_free(ptr);
5fe141fd
FB
249 return NULL;
250 }
251 return ptr;
252}
253
3efa9a67 254#ifdef ELF_CLASS
255#undef ELF_CLASS
256#endif
5fe141fd
FB
257
258#define ELF_CLASS ELFCLASS32
259#include "elf.h"
260
261#define SZ 32
262#define elf_word uint32_t
82790064 263#define elf_sword int32_t
5fe141fd 264#define bswapSZs bswap32s
83c9f4ca 265#include "hw/elf_ops.h"
5fe141fd
FB
266
267#undef elfhdr
268#undef elf_phdr
269#undef elf_shdr
270#undef elf_sym
271#undef elf_note
272#undef elf_word
82790064 273#undef elf_sword
5fe141fd
FB
274#undef bswapSZs
275#undef SZ
276#define elfhdr elf64_hdr
277#define elf_phdr elf64_phdr
278#define elf_note elf64_note
279#define elf_shdr elf64_shdr
280#define elf_sym elf64_sym
281#define elf_word uint64_t
82790064 282#define elf_sword int64_t
5fe141fd
FB
283#define bswapSZs bswap64s
284#define SZ 64
83c9f4ca 285#include "hw/elf_ops.h"
5fe141fd
FB
286
287/* return < 0 if error, otherwise the number of bytes loaded in memory */
409dbce5
AJ
288int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
289 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
290 uint64_t *highaddr, int big_endian, int elf_machine, int clear_lsb)
5fe141fd 291{
ca20cf32 292 int fd, data_order, target_data_order, must_swab, ret;
5fe141fd
FB
293 uint8_t e_ident[EI_NIDENT];
294
699e4642 295 fd = open(filename, O_RDONLY | O_BINARY);
5fe141fd
FB
296 if (fd < 0) {
297 perror(filename);
298 return -1;
299 }
300 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
301 goto fail;
302 if (e_ident[0] != ELFMAG0 ||
303 e_ident[1] != ELFMAG1 ||
304 e_ident[2] != ELFMAG2 ||
305 e_ident[3] != ELFMAG3)
306 goto fail;
e2542fe2 307#ifdef HOST_WORDS_BIGENDIAN
5fe141fd
FB
308 data_order = ELFDATA2MSB;
309#else
310 data_order = ELFDATA2LSB;
311#endif
312 must_swab = data_order != e_ident[EI_DATA];
ca20cf32
BS
313 if (big_endian) {
314 target_data_order = ELFDATA2MSB;
315 } else {
316 target_data_order = ELFDATA2LSB;
317 }
9042c0e2 318
cedf9a6f
BS
319 if (target_data_order != e_ident[EI_DATA]) {
320 goto fail;
321 }
9042c0e2 322
5fe141fd
FB
323 lseek(fd, 0, SEEK_SET);
324 if (e_ident[EI_CLASS] == ELFCLASS64) {
409dbce5
AJ
325 ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab,
326 pentry, lowaddr, highaddr, elf_machine, clear_lsb);
5fe141fd 327 } else {
409dbce5
AJ
328 ret = load_elf32(filename, fd, translate_fn, translate_opaque, must_swab,
329 pentry, lowaddr, highaddr, elf_machine, clear_lsb);
5fe141fd
FB
330 }
331
332 close(fd);
333 return ret;
334
335 fail:
336 close(fd);
337 return -1;
338}
1c7b3754 339
c227f099 340static void bswap_uboot_header(uboot_image_header_t *hdr)
1c7b3754 341{
e2542fe2 342#ifndef HOST_WORDS_BIGENDIAN
1c7b3754
PB
343 bswap32s(&hdr->ih_magic);
344 bswap32s(&hdr->ih_hcrc);
345 bswap32s(&hdr->ih_time);
346 bswap32s(&hdr->ih_size);
347 bswap32s(&hdr->ih_load);
348 bswap32s(&hdr->ih_ep);
349 bswap32s(&hdr->ih_dcrc);
350#endif
351}
352
5a123577
AL
353
354#define ZALLOC_ALIGNMENT 16
355
356static void *zalloc(void *x, unsigned items, unsigned size)
357{
358 void *p;
359
360 size *= items;
361 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
362
7267c094 363 p = g_malloc(size);
5a123577
AL
364
365 return (p);
366}
367
d084eab6 368static void zfree(void *x, void *addr)
5a123577 369{
7267c094 370 g_free(addr);
5a123577
AL
371}
372
373
374#define HEAD_CRC 2
375#define EXTRA_FIELD 4
376#define ORIG_NAME 8
377#define COMMENT 0x10
378#define RESERVED 0xe0
379
380#define DEFLATED 8
381
5025d542 382/* This is the usual maximum in uboot, so if a uImage overflows this, it would
5a123577 383 * overflow on real hardware too. */
5025d542 384#define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
5a123577
AL
385
386static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
387 size_t srclen)
388{
389 z_stream s;
390 ssize_t dstbytes;
391 int r, i, flags;
392
393 /* skip header */
394 i = 10;
395 flags = src[3];
396 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
397 puts ("Error: Bad gzipped data\n");
398 return -1;
399 }
400 if ((flags & EXTRA_FIELD) != 0)
401 i = 12 + src[10] + (src[11] << 8);
402 if ((flags & ORIG_NAME) != 0)
403 while (src[i++] != 0)
404 ;
405 if ((flags & COMMENT) != 0)
406 while (src[i++] != 0)
407 ;
408 if ((flags & HEAD_CRC) != 0)
409 i += 2;
410 if (i >= srclen) {
411 puts ("Error: gunzip out of data in header\n");
412 return -1;
413 }
414
415 s.zalloc = zalloc;
d084eab6 416 s.zfree = zfree;
5a123577
AL
417
418 r = inflateInit2(&s, -MAX_WBITS);
419 if (r != Z_OK) {
420 printf ("Error: inflateInit2() returned %d\n", r);
421 return (-1);
422 }
423 s.next_in = src + i;
424 s.avail_in = srclen - i;
425 s.next_out = dst;
426 s.avail_out = dstlen;
427 r = inflate(&s, Z_FINISH);
428 if (r != Z_OK && r != Z_STREAM_END) {
429 printf ("Error: inflate() returned %d\n", r);
430 return -1;
431 }
432 dstbytes = s.next_out - (unsigned char *) dst;
433 inflateEnd(&s);
434
435 return dstbytes;
436}
437
1c7b3754 438/* Load a U-Boot image. */
84aee0de
SB
439static int load_uboot_image(const char *filename, hwaddr *ep, hwaddr *loadaddr,
440 int *is_linux, uint8_t image_type)
1c7b3754 441{
1c7b3754
PB
442 int fd;
443 int size;
84aee0de 444 hwaddr address;
c227f099
AL
445 uboot_image_header_t h;
446 uboot_image_header_t *hdr = &h;
1c7b3754 447 uint8_t *data = NULL;
265ca29a 448 int ret = -1;
84aee0de 449 int do_uncompress = 0;
1c7b3754
PB
450
451 fd = open(filename, O_RDONLY | O_BINARY);
452 if (fd < 0)
453 return -1;
454
c227f099 455 size = read(fd, hdr, sizeof(uboot_image_header_t));
1c7b3754 456 if (size < 0)
265ca29a 457 goto out;
1c7b3754
PB
458
459 bswap_uboot_header(hdr);
460
461 if (hdr->ih_magic != IH_MAGIC)
265ca29a 462 goto out;
1c7b3754 463
84aee0de
SB
464 if (hdr->ih_type != image_type) {
465 fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
466 image_type);
265ca29a 467 goto out;
1c7b3754
PB
468 }
469
84aee0de
SB
470 /* TODO: Implement other image types. */
471 switch (hdr->ih_type) {
472 case IH_TYPE_KERNEL:
473 address = hdr->ih_load;
474 if (loadaddr) {
475 *loadaddr = hdr->ih_load;
476 }
477
478 switch (hdr->ih_comp) {
479 case IH_COMP_NONE:
480 break;
481 case IH_COMP_GZIP:
482 do_uncompress = 1;
483 break;
484 default:
485 fprintf(stderr,
486 "Unable to load u-boot images with compression type %d\n",
487 hdr->ih_comp);
488 goto out;
489 }
490
491 if (ep) {
492 *ep = hdr->ih_ep;
493 }
494
495 /* TODO: Check CPU type. */
496 if (is_linux) {
497 if (hdr->ih_os == IH_OS_LINUX) {
498 *is_linux = 1;
499 } else {
500 *is_linux = 0;
501 }
502 }
503
504 break;
505 case IH_TYPE_RAMDISK:
506 address = *loadaddr;
5a123577
AL
507 break;
508 default:
84aee0de 509 fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
265ca29a 510 goto out;
1c7b3754
PB
511 }
512
7267c094 513 data = g_malloc(hdr->ih_size);
1c7b3754
PB
514
515 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
516 fprintf(stderr, "Error reading file\n");
265ca29a 517 goto out;
1c7b3754
PB
518 }
519
84aee0de 520 if (do_uncompress) {
5a123577
AL
521 uint8_t *compressed_data;
522 size_t max_bytes;
523 ssize_t bytes;
524
525 compressed_data = data;
526 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
7267c094 527 data = g_malloc(max_bytes);
5a123577
AL
528
529 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
7267c094 530 g_free(compressed_data);
5a123577
AL
531 if (bytes < 0) {
532 fprintf(stderr, "Unable to decompress gzipped image!\n");
533 goto out;
534 }
535 hdr->ih_size = bytes;
536 }
537
84aee0de 538 rom_add_blob_fixed(filename, data, hdr->ih_size, address);
21cafd08 539
265ca29a 540 ret = hdr->ih_size;
1c7b3754 541
265ca29a 542out:
1c7b3754 543 if (data)
7267c094 544 g_free(data);
1c7b3754 545 close(fd);
265ca29a 546 return ret;
1c7b3754 547}
45a50b16 548
84aee0de
SB
549int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
550 int *is_linux)
551{
552 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL);
553}
554
555/* Load a ramdisk. */
556int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
557{
558 return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK);
559}
560
45a50b16
GH
561/*
562 * Functions for reboot-persistent memory regions.
563 * - used for vga bios and option roms.
564 * - also linux kernel (-kernel / -initrd).
565 */
566
567typedef struct Rom Rom;
568
569struct Rom {
570 char *name;
571 char *path;
d60fa42e
FC
572
573 /* datasize is the amount of memory allocated in "data". If datasize is less
574 * than romsize, it means that the area from datasize to romsize is filled
575 * with zeros.
576 */
45a50b16 577 size_t romsize;
d60fa42e
FC
578 size_t datasize;
579
45a50b16 580 uint8_t *data;
04920fc0 581 MemoryRegion *mr;
45a50b16 582 int isrom;
379526a4
GH
583 char *fw_dir;
584 char *fw_file;
45a50b16 585
a8170e5e 586 hwaddr addr;
45a50b16
GH
587 QTAILQ_ENTRY(Rom) next;
588};
589
8832cb80 590static FWCfgState *fw_cfg;
45a50b16
GH
591static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
592
593static void rom_insert(Rom *rom)
594{
595 Rom *item;
596
97fe84f5
PB
597 if (roms_loaded) {
598 hw_error ("ROM images must be loaded at startup\n");
599 }
600
45a50b16
GH
601 /* list is ordered by load address */
602 QTAILQ_FOREACH(item, &roms, next) {
632cf034 603 if (rom->addr >= item->addr)
45a50b16
GH
604 continue;
605 QTAILQ_INSERT_BEFORE(item, rom, next);
606 return;
607 }
608 QTAILQ_INSERT_TAIL(&roms, rom, next);
609}
610
04920fc0
MT
611static void *rom_set_mr(Rom *rom, Object *owner, const char *name)
612{
613 void *data;
614
615 rom->mr = g_malloc(sizeof(*rom->mr));
616 memory_region_init_ram(rom->mr, owner, name, rom->datasize);
617 memory_region_set_readonly(rom->mr, true);
618 vmstate_register_ram_global(rom->mr);
619
620 data = memory_region_get_ram_ptr(rom->mr);
621 memcpy(data, rom->data, rom->datasize);
622
623 return data;
624}
625
bdb5ee30 626int rom_add_file(const char *file, const char *fw_dir,
a8170e5e 627 hwaddr addr, int32_t bootindex)
45a50b16
GH
628{
629 Rom *rom;
630 int rc, fd = -1;
2e55e842 631 char devpath[100];
45a50b16 632
7267c094
AL
633 rom = g_malloc0(sizeof(*rom));
634 rom->name = g_strdup(file);
45a50b16
GH
635 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
636 if (rom->path == NULL) {
7267c094 637 rom->path = g_strdup(file);
45a50b16
GH
638 }
639
cef290b8 640 fd = open(rom->path, O_RDONLY | O_BINARY);
45a50b16
GH
641 if (fd == -1) {
642 fprintf(stderr, "Could not open option rom '%s': %s\n",
643 rom->path, strerror(errno));
644 goto err;
645 }
646
bdb5ee30 647 if (fw_dir) {
7267c094
AL
648 rom->fw_dir = g_strdup(fw_dir);
649 rom->fw_file = g_strdup(file);
bdb5ee30 650 }
d60fa42e
FC
651 rom->addr = addr;
652 rom->romsize = lseek(fd, 0, SEEK_END);
653 rom->datasize = rom->romsize;
654 rom->data = g_malloc0(rom->datasize);
45a50b16 655 lseek(fd, 0, SEEK_SET);
d60fa42e
FC
656 rc = read(fd, rom->data, rom->datasize);
657 if (rc != rom->datasize) {
45a50b16 658 fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
d60fa42e 659 rom->name, rc, rom->datasize);
45a50b16
GH
660 goto err;
661 }
662 close(fd);
663 rom_insert(rom);
de1f34cb
GN
664 if (rom->fw_file && fw_cfg) {
665 const char *basename;
666 char fw_file_name[56];
04920fc0 667 void *data;
de1f34cb
GN
668
669 basename = strrchr(rom->fw_file, '/');
670 if (basename) {
671 basename++;
672 } else {
673 basename = rom->fw_file;
674 }
675 snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
676 basename);
2e55e842 677 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
04920fc0
MT
678
679 if (rom_file_in_ram) {
680 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
681 } else {
682 data = rom->data;
683 }
684
685 fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
2e55e842
GN
686 } else {
687 snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
de1f34cb 688 }
2e55e842
GN
689
690 add_boot_device_path(bootindex, NULL, devpath);
45a50b16
GH
691 return 0;
692
693err:
694 if (fd != -1)
695 close(fd);
7267c094
AL
696 g_free(rom->data);
697 g_free(rom->path);
698 g_free(rom->name);
699 g_free(rom);
45a50b16
GH
700 return -1;
701}
702
703int rom_add_blob(const char *name, const void *blob, size_t len,
a8170e5e 704 hwaddr addr)
45a50b16
GH
705{
706 Rom *rom;
707
d60fa42e
FC
708 rom = g_malloc0(sizeof(*rom));
709 rom->name = g_strdup(name);
710 rom->addr = addr;
711 rom->romsize = len;
712 rom->datasize = len;
713 rom->data = g_malloc0(rom->datasize);
45a50b16
GH
714 memcpy(rom->data, blob, len);
715 rom_insert(rom);
716 return 0;
717}
718
d60fa42e
FC
719/* This function is specific for elf program because we don't need to allocate
720 * all the rom. We just allocate the first part and the rest is just zeros. This
721 * is why romsize and datasize are different. Also, this function seize the
722 * memory ownership of "data", so we don't have to allocate and copy the buffer.
723 */
724int rom_add_elf_program(const char *name, void *data, size_t datasize,
725 size_t romsize, hwaddr addr)
726{
727 Rom *rom;
728
729 rom = g_malloc0(sizeof(*rom));
730 rom->name = g_strdup(name);
731 rom->addr = addr;
732 rom->datasize = datasize;
733 rom->romsize = romsize;
734 rom->data = data;
735 rom_insert(rom);
736 return 0;
737}
738
de2aff17
GH
739int rom_add_vga(const char *file)
740{
2e55e842 741 return rom_add_file(file, "vgaroms", 0, -1);
de2aff17
GH
742}
743
2e55e842 744int rom_add_option(const char *file, int32_t bootindex)
de2aff17 745{
2e55e842 746 return rom_add_file(file, "genroms", 0, bootindex);
de2aff17
GH
747}
748
45a50b16
GH
749static void rom_reset(void *unused)
750{
751 Rom *rom;
752
753 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
754 if (rom->fw_file) {
755 continue;
756 }
bdb5ee30 757 if (rom->data == NULL) {
45a50b16 758 continue;
bdb5ee30 759 }
04920fc0
MT
760 if (rom->mr) {
761 void *host = memory_region_get_ram_ptr(rom->mr);
762 memcpy(host, rom->data, rom->datasize);
763 } else {
764 cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize);
765 }
45a50b16
GH
766 if (rom->isrom) {
767 /* rom needs to be written only once */
7267c094 768 g_free(rom->data);
45a50b16
GH
769 rom->data = NULL;
770 }
771 }
772}
773
774int rom_load_all(void)
775{
a8170e5e 776 hwaddr addr = 0;
dcc5cd33 777 MemoryRegionSection section;
45a50b16
GH
778 Rom *rom;
779
780 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
781 if (rom->fw_file) {
782 continue;
783 }
632cf034
GH
784 if (addr > rom->addr) {
785 fprintf(stderr, "rom: requested regions overlap "
786 "(rom %s. free=0x" TARGET_FMT_plx
787 ", addr=0x" TARGET_FMT_plx ")\n",
788 rom->name, addr, rom->addr);
789 return -1;
45a50b16 790 }
632cf034 791 addr = rom->addr;
45a50b16 792 addr += rom->romsize;
dcc5cd33 793 section = memory_region_find(get_system_memory(), rom->addr, 1);
052e87b0 794 rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
dfde4e6e 795 memory_region_unref(section.mr);
45a50b16
GH
796 }
797 qemu_register_reset(rom_reset, NULL);
97fe84f5 798 roms_loaded = 1;
45a50b16
GH
799 return 0;
800}
801
a88b362c 802void rom_set_fw(FWCfgState *f)
379526a4 803{
8832cb80 804 fw_cfg = f;
379526a4
GH
805}
806
a8170e5e 807static Rom *find_rom(hwaddr addr)
3c178e72
GH
808{
809 Rom *rom;
810
811 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
812 if (rom->fw_file) {
813 continue;
814 }
04920fc0
MT
815 if (rom->mr) {
816 continue;
817 }
bdb5ee30 818 if (rom->addr > addr) {
3c178e72 819 continue;
bdb5ee30
GH
820 }
821 if (rom->addr + rom->romsize < addr) {
3c178e72 822 continue;
bdb5ee30 823 }
3c178e72
GH
824 return rom;
825 }
826 return NULL;
827}
828
935effc2
KW
829/*
830 * Copies memory from registered ROMs to dest. Any memory that is contained in
831 * a ROM between addr and addr + size is copied. Note that this can involve
832 * multiple ROMs, which need not start at addr and need not end at addr + size.
833 */
a8170e5e 834int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
235f86ef 835{
a8170e5e 836 hwaddr end = addr + size;
235f86ef
AG
837 uint8_t *s, *d = dest;
838 size_t l = 0;
839 Rom *rom;
840
841 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
842 if (rom->fw_file) {
843 continue;
844 }
04920fc0
MT
845 if (rom->mr) {
846 continue;
847 }
bdb5ee30 848 if (rom->addr + rom->romsize < addr) {
632cf034 849 continue;
bdb5ee30
GH
850 }
851 if (rom->addr > end) {
235f86ef 852 break;
bdb5ee30 853 }
235f86ef 854
632cf034 855 d = dest + (rom->addr - addr);
235f86ef 856 s = rom->data;
d60fa42e 857 l = rom->datasize;
235f86ef 858
235f86ef
AG
859 if ((d + l) > (dest + size)) {
860 l = dest - d;
861 }
862
0dd5ce38
MB
863 if (l > 0) {
864 memcpy(d, s, l);
865 }
d60fa42e
FC
866
867 if (rom->romsize > rom->datasize) {
868 /* If datasize is less than romsize, it means that we didn't
869 * allocate all the ROM because the trailing data are only zeros.
870 */
871
872 d += l;
873 l = rom->romsize - rom->datasize;
874
875 if ((d + l) > (dest + size)) {
876 /* Rom size doesn't fit in the destination area. Adjust to avoid
877 * overflow.
878 */
879 l = dest - d;
880 }
881
882 if (l > 0) {
883 memset(d, 0x0, l);
884 }
885 }
235f86ef
AG
886 }
887
888 return (d + l) - dest;
889}
890
a8170e5e 891void *rom_ptr(hwaddr addr)
3c178e72
GH
892{
893 Rom *rom;
894
895 rom = find_rom(addr);
896 if (!rom || !rom->data)
897 return NULL;
632cf034 898 return rom->data + (addr - rom->addr);
3c178e72
GH
899}
900
84f2d0ea 901void do_info_roms(Monitor *mon, const QDict *qdict)
45a50b16
GH
902{
903 Rom *rom;
904
905 QTAILQ_FOREACH(rom, &roms, next) {
04920fc0
MT
906 if (rom->mr) {
907 monitor_printf(mon, "%s"
908 " size=0x%06zx name=\"%s\"\n",
909 rom->mr->name,
910 rom->romsize,
911 rom->name);
912 } else if (!rom->fw_file) {
632cf034 913 monitor_printf(mon, "addr=" TARGET_FMT_plx
b2bedb21 914 " size=0x%06zx mem=%s name=\"%s\"\n",
632cf034
GH
915 rom->addr, rom->romsize,
916 rom->isrom ? "rom" : "ram",
917 rom->name);
918 } else {
bdb5ee30 919 monitor_printf(mon, "fw=%s/%s"
b2bedb21 920 " size=0x%06zx name=\"%s\"\n",
bdb5ee30 921 rom->fw_dir,
632cf034
GH
922 rom->fw_file,
923 rom->romsize,
924 rom->name);
925 }
45a50b16
GH
926 }
927}