]> Git Repo - qemu.git/blame - hw/core/loader.c
Merge remote-tracking branch 'pmaydell/tags/pull-target-arm-20140107' into staging
[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, [email protected].
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;
35c12e60 666 char fw_file_name[FW_CFG_MAX_FILE_PATH];
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
48354cc5
MT
703void *rom_add_blob(const char *name, const void *blob, size_t len,
704 hwaddr addr, const char *fw_file_name,
705 FWCfgReadCallback fw_callback, void *callback_opaque)
45a50b16
GH
706{
707 Rom *rom;
48354cc5 708 void *data = NULL;
45a50b16 709
d60fa42e
FC
710 rom = g_malloc0(sizeof(*rom));
711 rom->name = g_strdup(name);
712 rom->addr = addr;
713 rom->romsize = len;
714 rom->datasize = len;
715 rom->data = g_malloc0(rom->datasize);
45a50b16
GH
716 memcpy(rom->data, blob, len);
717 rom_insert(rom);
48354cc5
MT
718 if (fw_file_name && fw_cfg) {
719 char devpath[100];
720
721 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
722
723 if (rom_file_in_ram) {
724 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
725 } else {
726 data = rom->data;
727 }
728
729 fw_cfg_add_file_callback(fw_cfg, fw_file_name,
730 fw_callback, callback_opaque,
731 data, rom->romsize);
732 }
733 return data;
45a50b16
GH
734}
735
d60fa42e
FC
736/* This function is specific for elf program because we don't need to allocate
737 * all the rom. We just allocate the first part and the rest is just zeros. This
738 * is why romsize and datasize are different. Also, this function seize the
739 * memory ownership of "data", so we don't have to allocate and copy the buffer.
740 */
741int rom_add_elf_program(const char *name, void *data, size_t datasize,
742 size_t romsize, hwaddr addr)
743{
744 Rom *rom;
745
746 rom = g_malloc0(sizeof(*rom));
747 rom->name = g_strdup(name);
748 rom->addr = addr;
749 rom->datasize = datasize;
750 rom->romsize = romsize;
751 rom->data = data;
752 rom_insert(rom);
753 return 0;
754}
755
de2aff17
GH
756int rom_add_vga(const char *file)
757{
2e55e842 758 return rom_add_file(file, "vgaroms", 0, -1);
de2aff17
GH
759}
760
2e55e842 761int rom_add_option(const char *file, int32_t bootindex)
de2aff17 762{
2e55e842 763 return rom_add_file(file, "genroms", 0, bootindex);
de2aff17
GH
764}
765
45a50b16
GH
766static void rom_reset(void *unused)
767{
768 Rom *rom;
769
770 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
771 if (rom->fw_file) {
772 continue;
773 }
bdb5ee30 774 if (rom->data == NULL) {
45a50b16 775 continue;
bdb5ee30 776 }
04920fc0
MT
777 if (rom->mr) {
778 void *host = memory_region_get_ram_ptr(rom->mr);
779 memcpy(host, rom->data, rom->datasize);
780 } else {
781 cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize);
782 }
45a50b16
GH
783 if (rom->isrom) {
784 /* rom needs to be written only once */
7267c094 785 g_free(rom->data);
45a50b16
GH
786 rom->data = NULL;
787 }
582b55a9
AG
788 /*
789 * The rom loader is really on the same level as firmware in the guest
790 * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
791 * that the instruction cache for that new region is clear, so that the
792 * CPU definitely fetches its instructions from the just written data.
793 */
794 cpu_flush_icache_range(rom->addr, rom->datasize);
45a50b16
GH
795 }
796}
797
798int rom_load_all(void)
799{
a8170e5e 800 hwaddr addr = 0;
dcc5cd33 801 MemoryRegionSection section;
45a50b16
GH
802 Rom *rom;
803
804 QTAILQ_FOREACH(rom, &roms, next) {
e405a2ba
AK
805 if (rom->fw_file) {
806 continue;
807 }
632cf034
GH
808 if (addr > rom->addr) {
809 fprintf(stderr, "rom: requested regions overlap "
810 "(rom %s. free=0x" TARGET_FMT_plx
811 ", addr=0x" TARGET_FMT_plx ")\n",
812 rom->name, addr, rom->addr);
813 return -1;
45a50b16 814 }
632cf034 815 addr = rom->addr;
45a50b16 816 addr += rom->romsize;
dcc5cd33 817 section = memory_region_find(get_system_memory(), rom->addr, 1);
052e87b0 818 rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
dfde4e6e 819 memory_region_unref(section.mr);
45a50b16
GH
820 }
821 qemu_register_reset(rom_reset, NULL);
45a50b16
GH
822 return 0;
823}
824
d916b464
MT
825void rom_load_done(void)
826{
827 roms_loaded = 1;
828}
829
a88b362c 830void rom_set_fw(FWCfgState *f)
379526a4 831{
8832cb80 832 fw_cfg = f;
379526a4
GH
833}
834
a8170e5e 835static Rom *find_rom(hwaddr addr)
3c178e72
GH
836{
837 Rom *rom;
838
839 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
840 if (rom->fw_file) {
841 continue;
842 }
04920fc0
MT
843 if (rom->mr) {
844 continue;
845 }
bdb5ee30 846 if (rom->addr > addr) {
3c178e72 847 continue;
bdb5ee30
GH
848 }
849 if (rom->addr + rom->romsize < addr) {
3c178e72 850 continue;
bdb5ee30 851 }
3c178e72
GH
852 return rom;
853 }
854 return NULL;
855}
856
935effc2
KW
857/*
858 * Copies memory from registered ROMs to dest. Any memory that is contained in
859 * a ROM between addr and addr + size is copied. Note that this can involve
860 * multiple ROMs, which need not start at addr and need not end at addr + size.
861 */
a8170e5e 862int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
235f86ef 863{
a8170e5e 864 hwaddr end = addr + size;
235f86ef
AG
865 uint8_t *s, *d = dest;
866 size_t l = 0;
867 Rom *rom;
868
869 QTAILQ_FOREACH(rom, &roms, next) {
f21a59c2
AJ
870 if (rom->fw_file) {
871 continue;
872 }
04920fc0
MT
873 if (rom->mr) {
874 continue;
875 }
bdb5ee30 876 if (rom->addr + rom->romsize < addr) {
632cf034 877 continue;
bdb5ee30
GH
878 }
879 if (rom->addr > end) {
235f86ef 880 break;
bdb5ee30 881 }
235f86ef 882
632cf034 883 d = dest + (rom->addr - addr);
235f86ef 884 s = rom->data;
d60fa42e 885 l = rom->datasize;
235f86ef 886
235f86ef
AG
887 if ((d + l) > (dest + size)) {
888 l = dest - d;
889 }
890
0dd5ce38
MB
891 if (l > 0) {
892 memcpy(d, s, l);
893 }
d60fa42e
FC
894
895 if (rom->romsize > rom->datasize) {
896 /* If datasize is less than romsize, it means that we didn't
897 * allocate all the ROM because the trailing data are only zeros.
898 */
899
900 d += l;
901 l = rom->romsize - rom->datasize;
902
903 if ((d + l) > (dest + size)) {
904 /* Rom size doesn't fit in the destination area. Adjust to avoid
905 * overflow.
906 */
907 l = dest - d;
908 }
909
910 if (l > 0) {
911 memset(d, 0x0, l);
912 }
913 }
235f86ef
AG
914 }
915
916 return (d + l) - dest;
917}
918
a8170e5e 919void *rom_ptr(hwaddr addr)
3c178e72
GH
920{
921 Rom *rom;
922
923 rom = find_rom(addr);
924 if (!rom || !rom->data)
925 return NULL;
632cf034 926 return rom->data + (addr - rom->addr);
3c178e72
GH
927}
928
84f2d0ea 929void do_info_roms(Monitor *mon, const QDict *qdict)
45a50b16
GH
930{
931 Rom *rom;
932
933 QTAILQ_FOREACH(rom, &roms, next) {
04920fc0
MT
934 if (rom->mr) {
935 monitor_printf(mon, "%s"
936 " size=0x%06zx name=\"%s\"\n",
937 rom->mr->name,
938 rom->romsize,
939 rom->name);
940 } else if (!rom->fw_file) {
632cf034 941 monitor_printf(mon, "addr=" TARGET_FMT_plx
b2bedb21 942 " size=0x%06zx mem=%s name=\"%s\"\n",
632cf034
GH
943 rom->addr, rom->romsize,
944 rom->isrom ? "rom" : "ram",
945 rom->name);
946 } else {
bdb5ee30 947 monitor_printf(mon, "fw=%s/%s"
b2bedb21 948 " size=0x%06zx name=\"%s\"\n",
bdb5ee30 949 rom->fw_dir,
632cf034
GH
950 rom->fw_file,
951 rom->romsize,
952 rom->name);
953 }
45a50b16
GH
954 }
955}
This page took 0.815239 seconds and 4 git commands to generate.