2 * QEMU Executable loader
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 * Gunzip functionality in this file is derived from u-boot:
26 * (C) Copyright 2008 Semihalf
28 * (C) Copyright 2000-2005
29 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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.
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.
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, see <http://www.gnu.org/licenses/>.
45 #include "qemu/osdep.h"
46 #include "qemu-common.h"
47 #include "qemu/datadir.h"
48 #include "qapi/error.h"
51 #include "disas/disas.h"
52 #include "migration/vmstate.h"
53 #include "monitor/monitor.h"
54 #include "sysemu/reset.h"
55 #include "sysemu/sysemu.h"
56 #include "uboot_image.h"
57 #include "hw/loader.h"
58 #include "hw/nvram/fw_cfg.h"
59 #include "exec/memory.h"
60 #include "exec/address-spaces.h"
61 #include "hw/boards.h"
62 #include "qemu/cutils.h"
63 #include "sysemu/runstate.h"
67 static int roms_loaded
;
69 /* return the size or -1 if error */
70 int64_t get_image_size(const char *filename
)
74 fd
= open(filename
, O_RDONLY
| O_BINARY
);
77 size
= lseek(fd
, 0, SEEK_END
);
82 /* return the size or -1 if error */
83 ssize_t
load_image_size(const char *filename
, void *addr
, size_t size
)
86 ssize_t actsize
, l
= 0;
88 fd
= open(filename
, O_RDONLY
| O_BINARY
);
93 while ((actsize
= read(fd
, addr
+ l
, size
- l
)) > 0) {
99 return actsize
< 0 ? -1 : l
;
102 /* read()-like version */
103 ssize_t
read_targphys(const char *name
,
104 int fd
, hwaddr dst_addr
, size_t nbytes
)
109 buf
= g_malloc(nbytes
);
110 did
= read(fd
, buf
, nbytes
);
112 rom_add_blob_fixed("read", buf
, did
, dst_addr
);
117 int load_image_targphys(const char *filename
,
118 hwaddr addr
, uint64_t max_sz
)
120 return load_image_targphys_as(filename
, addr
, max_sz
, NULL
);
123 /* return the size or -1 if error */
124 int load_image_targphys_as(const char *filename
,
125 hwaddr addr
, uint64_t max_sz
, AddressSpace
*as
)
129 size
= get_image_size(filename
);
130 if (size
< 0 || size
> max_sz
) {
134 if (rom_add_file_fixed_as(filename
, addr
, -1, as
) < 0) {
141 int load_image_mr(const char *filename
, MemoryRegion
*mr
)
145 if (!memory_access_is_direct(mr
, false)) {
146 /* Can only load an image into RAM or ROM */
150 size
= get_image_size(filename
);
152 if (size
< 0 || size
> memory_region_size(mr
)) {
156 if (rom_add_file_mr(filename
, mr
, -1) < 0) {
163 void pstrcpy_targphys(const char *name
, hwaddr dest
, int buf_size
,
169 if (buf_size
<= 0) return;
170 nulp
= memchr(source
, 0, buf_size
);
172 rom_add_blob_fixed(name
, source
, (nulp
- source
) + 1, dest
);
174 rom_add_blob_fixed(name
, source
, buf_size
, dest
);
175 ptr
= rom_ptr(dest
+ buf_size
- 1, sizeof(*ptr
));
184 uint32_t a_info
; /* Use macros N_MAGIC, etc for access */
185 uint32_t a_text
; /* length of text, in bytes */
186 uint32_t a_data
; /* length of data, in bytes */
187 uint32_t a_bss
; /* length of uninitialized data area, in bytes */
188 uint32_t a_syms
; /* length of symbol table data in file, in bytes */
189 uint32_t a_entry
; /* start address */
190 uint32_t a_trsize
; /* length of relocation info for text, in bytes */
191 uint32_t a_drsize
; /* length of relocation info for data, in bytes */
194 static void bswap_ahdr(struct exec
*e
)
196 bswap32s(&e
->a_info
);
197 bswap32s(&e
->a_text
);
198 bswap32s(&e
->a_data
);
200 bswap32s(&e
->a_syms
);
201 bswap32s(&e
->a_entry
);
202 bswap32s(&e
->a_trsize
);
203 bswap32s(&e
->a_drsize
);
206 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
211 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
212 #define N_TXTOFF(x) \
213 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
214 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
215 #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
216 #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
218 #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
220 #define N_DATADDR(x, target_page_size) \
221 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
222 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
225 int load_aout(const char *filename
, hwaddr addr
, int max_sz
,
226 int bswap_needed
, hwaddr target_page_size
)
233 fd
= open(filename
, O_RDONLY
| O_BINARY
);
237 size
= read(fd
, &e
, sizeof(e
));
250 if (e
.a_text
+ e
.a_data
> max_sz
)
252 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
253 size
= read_targphys(filename
, fd
, addr
, e
.a_text
+ e
.a_data
);
258 if (N_DATADDR(e
, target_page_size
) + e
.a_data
> max_sz
)
260 lseek(fd
, N_TXTOFF(e
), SEEK_SET
);
261 size
= read_targphys(filename
, fd
, addr
, e
.a_text
);
264 ret
= read_targphys(filename
, fd
, addr
+ N_DATADDR(e
, target_page_size
),
282 static void *load_at(int fd
, off_t offset
, size_t size
)
285 if (lseek(fd
, offset
, SEEK_SET
) < 0)
287 ptr
= g_malloc(size
);
288 if (read(fd
, ptr
, size
) != size
) {
299 #define ELF_CLASS ELFCLASS32
303 #define elf_word uint32_t
304 #define elf_sword int32_t
305 #define bswapSZs bswap32s
306 #include "hw/elf_ops.h"
318 #define elfhdr elf64_hdr
319 #define elf_phdr elf64_phdr
320 #define elf_note elf64_note
321 #define elf_shdr elf64_shdr
322 #define elf_sym elf64_sym
323 #define elf_rela elf64_rela
324 #define elf_word uint64_t
325 #define elf_sword int64_t
326 #define bswapSZs bswap64s
328 #include "hw/elf_ops.h"
330 const char *load_elf_strerror(int error
)
335 case ELF_LOAD_FAILED
:
336 return "Failed to load ELF";
337 case ELF_LOAD_NOT_ELF
:
338 return "The image is not ELF";
339 case ELF_LOAD_WRONG_ARCH
:
340 return "The image is from incompatible architecture";
341 case ELF_LOAD_WRONG_ENDIAN
:
342 return "The image has incorrect endianness";
343 case ELF_LOAD_TOO_BIG
:
344 return "The image segments are too big to load";
346 return "Unknown error";
350 void load_elf_hdr(const char *filename
, void *hdr
, bool *is64
, Error
**errp
)
353 uint8_t e_ident_local
[EI_NIDENT
];
355 size_t hdr_size
, off
;
363 fd
= open(filename
, O_RDONLY
| O_BINARY
);
365 error_setg_errno(errp
, errno
, "Failed to open file: %s", filename
);
368 if (read(fd
, hdr
, EI_NIDENT
) != EI_NIDENT
) {
369 error_setg_errno(errp
, errno
, "Failed to read file: %s", filename
);
372 if (e_ident
[0] != ELFMAG0
||
373 e_ident
[1] != ELFMAG1
||
374 e_ident
[2] != ELFMAG2
||
375 e_ident
[3] != ELFMAG3
) {
376 error_setg(errp
, "Bad ELF magic");
380 is64l
= e_ident
[EI_CLASS
] == ELFCLASS64
;
381 hdr_size
= is64l
? sizeof(Elf64_Ehdr
) : sizeof(Elf32_Ehdr
);
387 while (hdr
!= e_ident_local
&& off
< hdr_size
) {
388 size_t br
= read(fd
, hdr
+ off
, hdr_size
- off
);
391 error_setg(errp
, "File too short: %s", filename
);
394 error_setg_errno(errp
, errno
, "Failed to read file: %s",
405 /* return < 0 if error, otherwise the number of bytes loaded in memory */
406 int load_elf(const char *filename
,
407 uint64_t (*elf_note_fn
)(void *, void *, bool),
408 uint64_t (*translate_fn
)(void *, uint64_t),
409 void *translate_opaque
, uint64_t *pentry
, uint64_t *lowaddr
,
410 uint64_t *highaddr
, uint32_t *pflags
, int big_endian
,
411 int elf_machine
, int clear_lsb
, int data_swab
)
413 return load_elf_as(filename
, elf_note_fn
, translate_fn
, translate_opaque
,
414 pentry
, lowaddr
, highaddr
, pflags
, big_endian
,
415 elf_machine
, clear_lsb
, data_swab
, NULL
);
418 /* return < 0 if error, otherwise the number of bytes loaded in memory */
419 int load_elf_as(const char *filename
,
420 uint64_t (*elf_note_fn
)(void *, void *, bool),
421 uint64_t (*translate_fn
)(void *, uint64_t),
422 void *translate_opaque
, uint64_t *pentry
, uint64_t *lowaddr
,
423 uint64_t *highaddr
, uint32_t *pflags
, int big_endian
,
424 int elf_machine
, int clear_lsb
, int data_swab
, AddressSpace
*as
)
426 return load_elf_ram(filename
, elf_note_fn
, translate_fn
, translate_opaque
,
427 pentry
, lowaddr
, highaddr
, pflags
, big_endian
,
428 elf_machine
, clear_lsb
, data_swab
, as
, true);
431 /* return < 0 if error, otherwise the number of bytes loaded in memory */
432 int load_elf_ram(const char *filename
,
433 uint64_t (*elf_note_fn
)(void *, void *, bool),
434 uint64_t (*translate_fn
)(void *, uint64_t),
435 void *translate_opaque
, uint64_t *pentry
, uint64_t *lowaddr
,
436 uint64_t *highaddr
, uint32_t *pflags
, int big_endian
,
437 int elf_machine
, int clear_lsb
, int data_swab
,
438 AddressSpace
*as
, bool load_rom
)
440 return load_elf_ram_sym(filename
, elf_note_fn
,
441 translate_fn
, translate_opaque
,
442 pentry
, lowaddr
, highaddr
, pflags
, big_endian
,
443 elf_machine
, clear_lsb
, data_swab
, as
,
447 /* return < 0 if error, otherwise the number of bytes loaded in memory */
448 int load_elf_ram_sym(const char *filename
,
449 uint64_t (*elf_note_fn
)(void *, void *, bool),
450 uint64_t (*translate_fn
)(void *, uint64_t),
451 void *translate_opaque
, uint64_t *pentry
,
452 uint64_t *lowaddr
, uint64_t *highaddr
, uint32_t *pflags
,
453 int big_endian
, int elf_machine
,
454 int clear_lsb
, int data_swab
,
455 AddressSpace
*as
, bool load_rom
, symbol_fn_t sym_cb
)
457 int fd
, data_order
, target_data_order
, must_swab
, ret
= ELF_LOAD_FAILED
;
458 uint8_t e_ident
[EI_NIDENT
];
460 fd
= open(filename
, O_RDONLY
| O_BINARY
);
465 if (read(fd
, e_ident
, sizeof(e_ident
)) != sizeof(e_ident
))
467 if (e_ident
[0] != ELFMAG0
||
468 e_ident
[1] != ELFMAG1
||
469 e_ident
[2] != ELFMAG2
||
470 e_ident
[3] != ELFMAG3
) {
471 ret
= ELF_LOAD_NOT_ELF
;
474 #ifdef HOST_WORDS_BIGENDIAN
475 data_order
= ELFDATA2MSB
;
477 data_order
= ELFDATA2LSB
;
479 must_swab
= data_order
!= e_ident
[EI_DATA
];
481 target_data_order
= ELFDATA2MSB
;
483 target_data_order
= ELFDATA2LSB
;
486 if (target_data_order
!= e_ident
[EI_DATA
]) {
487 ret
= ELF_LOAD_WRONG_ENDIAN
;
491 lseek(fd
, 0, SEEK_SET
);
492 if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
493 ret
= load_elf64(filename
, fd
, elf_note_fn
,
494 translate_fn
, translate_opaque
, must_swab
,
495 pentry
, lowaddr
, highaddr
, pflags
, elf_machine
,
496 clear_lsb
, data_swab
, as
, load_rom
, sym_cb
);
498 ret
= load_elf32(filename
, fd
, elf_note_fn
,
499 translate_fn
, translate_opaque
, must_swab
,
500 pentry
, lowaddr
, highaddr
, pflags
, elf_machine
,
501 clear_lsb
, data_swab
, as
, load_rom
, sym_cb
);
509 static void bswap_uboot_header(uboot_image_header_t
*hdr
)
511 #ifndef HOST_WORDS_BIGENDIAN
512 bswap32s(&hdr
->ih_magic
);
513 bswap32s(&hdr
->ih_hcrc
);
514 bswap32s(&hdr
->ih_time
);
515 bswap32s(&hdr
->ih_size
);
516 bswap32s(&hdr
->ih_load
);
517 bswap32s(&hdr
->ih_ep
);
518 bswap32s(&hdr
->ih_dcrc
);
523 #define ZALLOC_ALIGNMENT 16
525 static void *zalloc(void *x
, unsigned items
, unsigned size
)
530 size
= (size
+ ZALLOC_ALIGNMENT
- 1) & ~(ZALLOC_ALIGNMENT
- 1);
537 static void zfree(void *x
, void *addr
)
544 #define EXTRA_FIELD 4
547 #define RESERVED 0xe0
551 ssize_t
gunzip(void *dst
, size_t dstlen
, uint8_t *src
, size_t srclen
)
560 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
561 puts ("Error: Bad gzipped data\n");
564 if ((flags
& EXTRA_FIELD
) != 0)
565 i
= 12 + src
[10] + (src
[11] << 8);
566 if ((flags
& ORIG_NAME
) != 0)
567 while (src
[i
++] != 0)
569 if ((flags
& COMMENT
) != 0)
570 while (src
[i
++] != 0)
572 if ((flags
& HEAD_CRC
) != 0)
575 puts ("Error: gunzip out of data in header\n");
582 r
= inflateInit2(&s
, -MAX_WBITS
);
584 printf ("Error: inflateInit2() returned %d\n", r
);
588 s
.avail_in
= srclen
- i
;
590 s
.avail_out
= dstlen
;
591 r
= inflate(&s
, Z_FINISH
);
592 if (r
!= Z_OK
&& r
!= Z_STREAM_END
) {
593 printf ("Error: inflate() returned %d\n", r
);
596 dstbytes
= s
.next_out
- (unsigned char *) dst
;
602 /* Load a U-Boot image. */
603 static int load_uboot_image(const char *filename
, hwaddr
*ep
, hwaddr
*loadaddr
,
604 int *is_linux
, uint8_t image_type
,
605 uint64_t (*translate_fn
)(void *, uint64_t),
606 void *translate_opaque
, AddressSpace
*as
)
611 uboot_image_header_t h
;
612 uboot_image_header_t
*hdr
= &h
;
613 uint8_t *data
= NULL
;
615 int do_uncompress
= 0;
617 fd
= open(filename
, O_RDONLY
| O_BINARY
);
621 size
= read(fd
, hdr
, sizeof(uboot_image_header_t
));
622 if (size
< sizeof(uboot_image_header_t
)) {
626 bswap_uboot_header(hdr
);
628 if (hdr
->ih_magic
!= IH_MAGIC
)
631 if (hdr
->ih_type
!= image_type
) {
632 if (!(image_type
== IH_TYPE_KERNEL
&&
633 hdr
->ih_type
== IH_TYPE_KERNEL_NOLOAD
)) {
634 fprintf(stderr
, "Wrong image type %d, expected %d\n", hdr
->ih_type
,
640 /* TODO: Implement other image types. */
641 switch (hdr
->ih_type
) {
642 case IH_TYPE_KERNEL_NOLOAD
:
643 if (!loadaddr
|| *loadaddr
== LOAD_UIMAGE_LOADADDR_INVALID
) {
644 fprintf(stderr
, "this image format (kernel_noload) cannot be "
645 "loaded on this machine type");
649 hdr
->ih_load
= *loadaddr
+ sizeof(*hdr
);
650 hdr
->ih_ep
+= hdr
->ih_load
;
653 address
= hdr
->ih_load
;
655 address
= translate_fn(translate_opaque
, address
);
658 *loadaddr
= hdr
->ih_load
;
661 switch (hdr
->ih_comp
) {
669 "Unable to load u-boot images with compression type %d\n",
678 /* TODO: Check CPU type. */
680 if (hdr
->ih_os
== IH_OS_LINUX
) {
688 case IH_TYPE_RAMDISK
:
692 fprintf(stderr
, "Unsupported u-boot image type %d\n", hdr
->ih_type
);
696 data
= g_malloc(hdr
->ih_size
);
698 if (read(fd
, data
, hdr
->ih_size
) != hdr
->ih_size
) {
699 fprintf(stderr
, "Error reading file\n");
704 uint8_t *compressed_data
;
708 compressed_data
= data
;
709 max_bytes
= UBOOT_MAX_GUNZIP_BYTES
;
710 data
= g_malloc(max_bytes
);
712 bytes
= gunzip(data
, max_bytes
, compressed_data
, hdr
->ih_size
);
713 g_free(compressed_data
);
715 fprintf(stderr
, "Unable to decompress gzipped image!\n");
718 hdr
->ih_size
= bytes
;
721 rom_add_blob_fixed_as(filename
, data
, hdr
->ih_size
, address
, as
);
731 int load_uimage(const char *filename
, hwaddr
*ep
, hwaddr
*loadaddr
,
733 uint64_t (*translate_fn
)(void *, uint64_t),
734 void *translate_opaque
)
736 return load_uboot_image(filename
, ep
, loadaddr
, is_linux
, IH_TYPE_KERNEL
,
737 translate_fn
, translate_opaque
, NULL
);
740 int load_uimage_as(const char *filename
, hwaddr
*ep
, hwaddr
*loadaddr
,
742 uint64_t (*translate_fn
)(void *, uint64_t),
743 void *translate_opaque
, AddressSpace
*as
)
745 return load_uboot_image(filename
, ep
, loadaddr
, is_linux
, IH_TYPE_KERNEL
,
746 translate_fn
, translate_opaque
, as
);
749 /* Load a ramdisk. */
750 int load_ramdisk(const char *filename
, hwaddr addr
, uint64_t max_sz
)
752 return load_ramdisk_as(filename
, addr
, max_sz
, NULL
);
755 int load_ramdisk_as(const char *filename
, hwaddr addr
, uint64_t max_sz
,
758 return load_uboot_image(filename
, NULL
, &addr
, NULL
, IH_TYPE_RAMDISK
,
762 /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
763 int load_image_gzipped_buffer(const char *filename
, uint64_t max_sz
,
766 uint8_t *compressed_data
= NULL
;
767 uint8_t *data
= NULL
;
772 if (!g_file_get_contents(filename
, (char **) &compressed_data
, &len
,
777 /* Is it a gzip-compressed file? */
779 compressed_data
[0] != 0x1f ||
780 compressed_data
[1] != 0x8b) {
784 if (max_sz
> LOAD_IMAGE_MAX_GUNZIP_BYTES
) {
785 max_sz
= LOAD_IMAGE_MAX_GUNZIP_BYTES
;
788 data
= g_malloc(max_sz
);
789 bytes
= gunzip(data
, max_sz
, compressed_data
, len
);
791 fprintf(stderr
, "%s: unable to decompress gzipped kernel file\n",
796 /* trim to actual size and return to caller */
797 *buffer
= g_realloc(data
, bytes
);
799 /* ownership has been transferred to caller */
803 g_free(compressed_data
);
808 /* Load a gzip-compressed kernel. */
809 int load_image_gzipped(const char *filename
, hwaddr addr
, uint64_t max_sz
)
814 bytes
= load_image_gzipped_buffer(filename
, max_sz
, &data
);
816 rom_add_blob_fixed(filename
, data
, bytes
, addr
);
823 * Functions for reboot-persistent memory regions.
824 * - used for vga bios and option roms.
825 * - also linux kernel (-kernel / -initrd).
828 typedef struct Rom Rom
;
834 /* datasize is the amount of memory allocated in "data". If datasize is less
835 * than romsize, it means that the area from datasize to romsize is filled
847 GMappedFile
*mapped_file
;
852 QTAILQ_ENTRY(Rom
) next
;
855 static FWCfgState
*fw_cfg
;
856 static QTAILQ_HEAD(, Rom
) roms
= QTAILQ_HEAD_INITIALIZER(roms
);
859 * rom->data can be heap-allocated or memory-mapped (e.g. when added with
860 * rom_add_elf_program())
862 static void rom_free_data(Rom
*rom
)
864 if (rom
->mapped_file
) {
865 g_mapped_file_unref(rom
->mapped_file
);
866 rom
->mapped_file
= NULL
;
874 static void rom_free(Rom
*rom
)
880 g_free(rom
->fw_file
);
884 static inline bool rom_order_compare(Rom
*rom
, Rom
*item
)
886 return ((uintptr_t)(void *)rom
->as
> (uintptr_t)(void *)item
->as
) ||
887 (rom
->as
== item
->as
&& rom
->addr
>= item
->addr
);
890 static void rom_insert(Rom
*rom
)
895 hw_error ("ROM images must be loaded at startup\n");
898 /* The user didn't specify an address space, this is the default */
900 rom
->as
= &address_space_memory
;
903 rom
->committed
= false;
905 /* List is ordered by load address in the same address space */
906 QTAILQ_FOREACH(item
, &roms
, next
) {
907 if (rom_order_compare(rom
, item
)) {
910 QTAILQ_INSERT_BEFORE(item
, rom
, next
);
913 QTAILQ_INSERT_TAIL(&roms
, rom
, next
);
916 static void fw_cfg_resized(const char *id
, uint64_t length
, void *host
)
919 fw_cfg_modify_file(fw_cfg
, id
+ strlen("/rom@"), host
, length
);
923 static void *rom_set_mr(Rom
*rom
, Object
*owner
, const char *name
, bool ro
)
927 rom
->mr
= g_malloc(sizeof(*rom
->mr
));
928 memory_region_init_resizeable_ram(rom
->mr
, owner
, name
,
929 rom
->datasize
, rom
->romsize
,
932 memory_region_set_readonly(rom
->mr
, ro
);
933 vmstate_register_ram_global(rom
->mr
);
935 data
= memory_region_get_ram_ptr(rom
->mr
);
936 memcpy(data
, rom
->data
, rom
->datasize
);
941 int rom_add_file(const char *file
, const char *fw_dir
,
942 hwaddr addr
, int32_t bootindex
,
943 bool option_rom
, MemoryRegion
*mr
,
946 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
952 fprintf(stderr
, "Specifying an Address Space and Memory Region is " \
953 "not valid when loading a rom\n");
954 /* We haven't allocated anything so we don't need any cleanup */
958 rom
= g_malloc0(sizeof(*rom
));
959 rom
->name
= g_strdup(file
);
960 rom
->path
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, rom
->name
);
962 if (rom
->path
== NULL
) {
963 rom
->path
= g_strdup(file
);
966 fd
= open(rom
->path
, O_RDONLY
| O_BINARY
);
968 fprintf(stderr
, "Could not open option rom '%s': %s\n",
969 rom
->path
, strerror(errno
));
974 rom
->fw_dir
= g_strdup(fw_dir
);
975 rom
->fw_file
= g_strdup(file
);
978 rom
->romsize
= lseek(fd
, 0, SEEK_END
);
979 if (rom
->romsize
== -1) {
980 fprintf(stderr
, "rom: file %-20s: get size error: %s\n",
981 rom
->name
, strerror(errno
));
985 rom
->datasize
= rom
->romsize
;
986 rom
->data
= g_malloc0(rom
->datasize
);
987 lseek(fd
, 0, SEEK_SET
);
988 rc
= read(fd
, rom
->data
, rom
->datasize
);
989 if (rc
!= rom
->datasize
) {
990 fprintf(stderr
, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
991 rom
->name
, rc
, rom
->datasize
);
996 if (rom
->fw_file
&& fw_cfg
) {
997 const char *basename
;
998 char fw_file_name
[FW_CFG_MAX_FILE_PATH
];
1001 basename
= strrchr(rom
->fw_file
, '/');
1005 basename
= rom
->fw_file
;
1007 snprintf(fw_file_name
, sizeof(fw_file_name
), "%s/%s", rom
->fw_dir
,
1009 snprintf(devpath
, sizeof(devpath
), "/rom@%s", fw_file_name
);
1011 if ((!option_rom
|| mc
->option_rom_has_mr
) && mc
->rom_file_has_mr
) {
1012 data
= rom_set_mr(rom
, OBJECT(fw_cfg
), devpath
, true);
1017 fw_cfg_add_file(fw_cfg
, fw_file_name
, data
, rom
->romsize
);
1021 snprintf(devpath
, sizeof(devpath
), "/rom@%s", file
);
1023 snprintf(devpath
, sizeof(devpath
), "/rom@" TARGET_FMT_plx
, addr
);
1027 add_boot_device_path(bootindex
, NULL
, devpath
);
1038 MemoryRegion
*rom_add_blob(const char *name
, const void *blob
, size_t len
,
1039 size_t max_len
, hwaddr addr
, const char *fw_file_name
,
1040 FWCfgCallback fw_callback
, void *callback_opaque
,
1041 AddressSpace
*as
, bool read_only
)
1043 MachineClass
*mc
= MACHINE_GET_CLASS(qdev_get_machine());
1045 MemoryRegion
*mr
= NULL
;
1047 rom
= g_malloc0(sizeof(*rom
));
1048 rom
->name
= g_strdup(name
);
1051 rom
->romsize
= max_len
? max_len
: len
;
1052 rom
->datasize
= len
;
1053 g_assert(rom
->romsize
>= rom
->datasize
);
1054 rom
->data
= g_malloc0(rom
->datasize
);
1055 memcpy(rom
->data
, blob
, len
);
1057 if (fw_file_name
&& fw_cfg
) {
1062 snprintf(devpath
, sizeof(devpath
), "/rom@%s", fw_file_name
);
1064 snprintf(devpath
, sizeof(devpath
), "/ram@%s", fw_file_name
);
1067 if (mc
->rom_file_has_mr
) {
1068 data
= rom_set_mr(rom
, OBJECT(fw_cfg
), devpath
, read_only
);
1074 fw_cfg_add_file_callback(fw_cfg
, fw_file_name
,
1075 fw_callback
, NULL
, callback_opaque
,
1076 data
, rom
->datasize
, read_only
);
1081 /* This function is specific for elf program because we don't need to allocate
1082 * all the rom. We just allocate the first part and the rest is just zeros. This
1083 * is why romsize and datasize are different. Also, this function takes its own
1084 * reference to "mapped_file", so we don't have to allocate and copy the buffer.
1086 int rom_add_elf_program(const char *name
, GMappedFile
*mapped_file
, void *data
,
1087 size_t datasize
, size_t romsize
, hwaddr addr
,
1092 rom
= g_malloc0(sizeof(*rom
));
1093 rom
->name
= g_strdup(name
);
1095 rom
->datasize
= datasize
;
1096 rom
->romsize
= romsize
;
1100 if (mapped_file
&& data
) {
1101 g_mapped_file_ref(mapped_file
);
1102 rom
->mapped_file
= mapped_file
;
1109 int rom_add_vga(const char *file
)
1111 return rom_add_file(file
, "vgaroms", 0, -1, true, NULL
, NULL
);
1114 int rom_add_option(const char *file
, int32_t bootindex
)
1116 return rom_add_file(file
, "genroms", 0, bootindex
, true, NULL
, NULL
);
1119 static void rom_reset(void *unused
)
1123 QTAILQ_FOREACH(rom
, &roms
, next
) {
1128 * We don't need to fill in the RAM with ROM data because we'll fill
1129 * the data in during the next incoming migration in all cases. Note
1130 * that some of those RAMs can actually be modified by the guest.
1132 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1133 if (rom
->data
&& rom
->isrom
) {
1135 * Free it so that a rom_reset after migration doesn't
1136 * overwrite a potentially modified 'rom'.
1143 if (rom
->data
== NULL
) {
1147 void *host
= memory_region_get_ram_ptr(rom
->mr
);
1148 memcpy(host
, rom
->data
, rom
->datasize
);
1150 address_space_write_rom(rom
->as
, rom
->addr
, MEMTXATTRS_UNSPECIFIED
,
1151 rom
->data
, rom
->datasize
);
1154 /* rom needs to be written only once */
1158 * The rom loader is really on the same level as firmware in the guest
1159 * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
1160 * that the instruction cache for that new region is clear, so that the
1161 * CPU definitely fetches its instructions from the just written data.
1163 cpu_flush_icache_range(rom
->addr
, rom
->datasize
);
1165 trace_loader_write_rom(rom
->name
, rom
->addr
, rom
->datasize
, rom
->isrom
);
1169 /* Return true if two consecutive ROMs in the ROM list overlap */
1170 static bool roms_overlap(Rom
*last_rom
, Rom
*this_rom
)
1175 return last_rom
->as
== this_rom
->as
&&
1176 last_rom
->addr
+ last_rom
->romsize
> this_rom
->addr
;
1179 static const char *rom_as_name(Rom
*rom
)
1181 const char *name
= rom
->as
? rom
->as
->name
: NULL
;
1182 return name
?: "anonymous";
1185 static void rom_print_overlap_error_header(void)
1187 error_report("Some ROM regions are overlapping");
1189 "These ROM regions might have been loaded by "
1190 "direct user request or by default.\n"
1191 "They could be BIOS/firmware images, a guest kernel, "
1192 "initrd or some other file loaded into guest memory.\n"
1193 "Check whether you intended to load all this guest code, and "
1194 "whether it has been built to load to the correct addresses.\n");
1197 static void rom_print_one_overlap_error(Rom
*last_rom
, Rom
*rom
)
1200 "\nThe following two regions overlap (in the %s address space):\n",
1203 " %s (addresses 0x" TARGET_FMT_plx
" - 0x" TARGET_FMT_plx
")\n",
1204 last_rom
->name
, last_rom
->addr
, last_rom
->addr
+ last_rom
->romsize
);
1206 " %s (addresses 0x" TARGET_FMT_plx
" - 0x" TARGET_FMT_plx
")\n",
1207 rom
->name
, rom
->addr
, rom
->addr
+ rom
->romsize
);
1210 int rom_check_and_register_reset(void)
1212 MemoryRegionSection section
;
1213 Rom
*rom
, *last_rom
= NULL
;
1214 bool found_overlap
= false;
1216 QTAILQ_FOREACH(rom
, &roms
, next
) {
1221 if (roms_overlap(last_rom
, rom
)) {
1222 if (!found_overlap
) {
1223 found_overlap
= true;
1224 rom_print_overlap_error_header();
1226 rom_print_one_overlap_error(last_rom
, rom
);
1227 /* Keep going through the list so we report all overlaps */
1231 section
= memory_region_find(rom
->mr
? rom
->mr
: get_system_memory(),
1233 rom
->isrom
= int128_nz(section
.size
) && memory_region_is_rom(section
.mr
);
1234 memory_region_unref(section
.mr
);
1236 if (found_overlap
) {
1240 qemu_register_reset(rom_reset
, NULL
);
1245 void rom_set_fw(FWCfgState
*f
)
1250 void rom_set_order_override(int order
)
1254 fw_cfg_set_order_override(fw_cfg
, order
);
1257 void rom_reset_order_override(void)
1261 fw_cfg_reset_order_override(fw_cfg
);
1264 void rom_transaction_begin(void)
1268 /* Ignore ROMs added without the transaction API */
1269 QTAILQ_FOREACH(rom
, &roms
, next
) {
1270 rom
->committed
= true;
1274 void rom_transaction_end(bool commit
)
1279 QTAILQ_FOREACH_SAFE(rom
, &roms
, next
, tmp
) {
1280 if (rom
->committed
) {
1284 rom
->committed
= true;
1286 QTAILQ_REMOVE(&roms
, rom
, next
);
1292 static Rom
*find_rom(hwaddr addr
, size_t size
)
1296 QTAILQ_FOREACH(rom
, &roms
, next
) {
1303 if (rom
->addr
> addr
) {
1306 if (rom
->addr
+ rom
->romsize
< addr
+ size
) {
1315 * Copies memory from registered ROMs to dest. Any memory that is contained in
1316 * a ROM between addr and addr + size is copied. Note that this can involve
1317 * multiple ROMs, which need not start at addr and need not end at addr + size.
1319 int rom_copy(uint8_t *dest
, hwaddr addr
, size_t size
)
1321 hwaddr end
= addr
+ size
;
1322 uint8_t *s
, *d
= dest
;
1326 QTAILQ_FOREACH(rom
, &roms
, next
) {
1333 if (rom
->addr
+ rom
->romsize
< addr
) {
1336 if (rom
->addr
> end
|| rom
->addr
< addr
) {
1340 d
= dest
+ (rom
->addr
- addr
);
1344 if ((d
+ l
) > (dest
+ size
)) {
1352 if (rom
->romsize
> rom
->datasize
) {
1353 /* If datasize is less than romsize, it means that we didn't
1354 * allocate all the ROM because the trailing data are only zeros.
1358 l
= rom
->romsize
- rom
->datasize
;
1360 if ((d
+ l
) > (dest
+ size
)) {
1361 /* Rom size doesn't fit in the destination area. Adjust to avoid
1373 return (d
+ l
) - dest
;
1376 void *rom_ptr(hwaddr addr
, size_t size
)
1380 rom
= find_rom(addr
, size
);
1381 if (!rom
|| !rom
->data
)
1383 return rom
->data
+ (addr
- rom
->addr
);
1386 void hmp_info_roms(Monitor
*mon
, const QDict
*qdict
)
1390 QTAILQ_FOREACH(rom
, &roms
, next
) {
1392 monitor_printf(mon
, "%s"
1393 " size=0x%06zx name=\"%s\"\n",
1394 memory_region_name(rom
->mr
),
1397 } else if (!rom
->fw_file
) {
1398 monitor_printf(mon
, "addr=" TARGET_FMT_plx
1399 " size=0x%06zx mem=%s name=\"%s\"\n",
1400 rom
->addr
, rom
->romsize
,
1401 rom
->isrom
? "rom" : "ram",
1404 monitor_printf(mon
, "fw=%s/%s"
1405 " size=0x%06zx name=\"%s\"\n",
1414 typedef enum HexRecord HexRecord
;
1418 EXT_SEG_ADDR_RECORD
,
1419 START_SEG_ADDR_RECORD
,
1420 EXT_LINEAR_ADDR_RECORD
,
1421 START_LINEAR_ADDR_RECORD
,
1424 /* Each record contains a 16-bit address which is combined with the upper 16
1425 * bits of the implicit "next address" to form a 32-bit address.
1427 #define NEXT_ADDR_MASK 0xffff0000
1429 #define DATA_FIELD_MAX_LEN 0xff
1430 #define LEN_EXCEPT_DATA 0x5
1431 /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
1432 * sizeof(checksum) */
1436 uint8_t record_type
;
1437 uint8_t data
[DATA_FIELD_MAX_LEN
];
1441 /* return 0 or -1 if error */
1442 static bool parse_record(HexLine
*line
, uint8_t *our_checksum
, const uint8_t c
,
1443 uint32_t *index
, const bool in_process
)
1445 /* +-------+---------------+-------+---------------------+--------+
1446 * | byte | |record | | |
1447 * | count | address | type | data |checksum|
1448 * +-------+---------------+-------+---------------------+--------+
1450 * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
1453 uint32_t idx
= *index
;
1455 if (g_ascii_isspace(c
)) {
1458 if (!g_ascii_isxdigit(c
) || !in_process
) {
1461 value
= g_ascii_xdigit_value(c
);
1462 value
= (idx
& 0x1) ? (value
& 0xf) : (value
<< 4);
1464 line
->byte_count
|= value
;
1465 } else if (2 <= idx
&& idx
< 6) {
1466 line
->address
<<= 4;
1467 line
->address
+= g_ascii_xdigit_value(c
);
1468 } else if (6 <= idx
&& idx
< 8) {
1469 line
->record_type
|= value
;
1470 } else if (8 <= idx
&& idx
< 8 + 2 * line
->byte_count
) {
1471 line
->data
[(idx
- 8) >> 1] |= value
;
1472 } else if (8 + 2 * line
->byte_count
<= idx
&&
1473 idx
< 10 + 2 * line
->byte_count
) {
1474 line
->checksum
|= value
;
1478 *our_checksum
+= value
;
1484 const char *filename
;
1489 uint32_t next_address_to_write
;
1490 uint32_t current_address
;
1491 uint32_t current_rom_index
;
1492 uint32_t rom_start_address
;
1497 /* return size or -1 if error */
1498 static int handle_record_type(HexParser
*parser
)
1500 HexLine
*line
= &(parser
->line
);
1501 switch (line
->record_type
) {
1503 parser
->current_address
=
1504 (parser
->next_address_to_write
& NEXT_ADDR_MASK
) | line
->address
;
1505 /* verify this is a contiguous block of memory */
1506 if (parser
->current_address
!= parser
->next_address_to_write
) {
1507 if (parser
->current_rom_index
!= 0) {
1508 rom_add_blob_fixed_as(parser
->filename
, parser
->bin_buf
,
1509 parser
->current_rom_index
,
1510 parser
->rom_start_address
, parser
->as
);
1512 parser
->rom_start_address
= parser
->current_address
;
1513 parser
->current_rom_index
= 0;
1516 /* copy from line buffer to output bin_buf */
1517 memcpy(parser
->bin_buf
+ parser
->current_rom_index
, line
->data
,
1519 parser
->current_rom_index
+= line
->byte_count
;
1520 parser
->total_size
+= line
->byte_count
;
1521 /* save next address to write */
1522 parser
->next_address_to_write
=
1523 parser
->current_address
+ line
->byte_count
;
1527 if (parser
->current_rom_index
!= 0) {
1528 rom_add_blob_fixed_as(parser
->filename
, parser
->bin_buf
,
1529 parser
->current_rom_index
,
1530 parser
->rom_start_address
, parser
->as
);
1532 parser
->complete
= true;
1533 return parser
->total_size
;
1534 case EXT_SEG_ADDR_RECORD
:
1535 case EXT_LINEAR_ADDR_RECORD
:
1536 if (line
->byte_count
!= 2 && line
->address
!= 0) {
1540 if (parser
->current_rom_index
!= 0) {
1541 rom_add_blob_fixed_as(parser
->filename
, parser
->bin_buf
,
1542 parser
->current_rom_index
,
1543 parser
->rom_start_address
, parser
->as
);
1546 /* save next address to write,
1547 * in case of non-contiguous block of memory */
1548 parser
->next_address_to_write
= (line
->data
[0] << 12) |
1549 (line
->data
[1] << 4);
1550 if (line
->record_type
== EXT_LINEAR_ADDR_RECORD
) {
1551 parser
->next_address_to_write
<<= 12;
1554 parser
->rom_start_address
= parser
->next_address_to_write
;
1555 parser
->current_rom_index
= 0;
1558 case START_SEG_ADDR_RECORD
:
1559 if (line
->byte_count
!= 4 && line
->address
!= 0) {
1563 /* x86 16-bit CS:IP segmented addressing */
1564 *(parser
->start_addr
) = (((line
->data
[0] << 8) | line
->data
[1]) << 4) +
1565 ((line
->data
[2] << 8) | line
->data
[3]);
1568 case START_LINEAR_ADDR_RECORD
:
1569 if (line
->byte_count
!= 4 && line
->address
!= 0) {
1573 *(parser
->start_addr
) = ldl_be_p(line
->data
);
1580 return parser
->total_size
;
1583 /* return size or -1 if error */
1584 static int parse_hex_blob(const char *filename
, hwaddr
*addr
, uint8_t *hex_blob
,
1585 size_t hex_blob_size
, AddressSpace
*as
)
1587 bool in_process
= false; /* avoid re-enter and
1588 * check whether record begin with ':' */
1589 uint8_t *end
= hex_blob
+ hex_blob_size
;
1590 uint8_t our_checksum
= 0;
1591 uint32_t record_index
= 0;
1592 HexParser parser
= {
1593 .filename
= filename
,
1594 .bin_buf
= g_malloc(hex_blob_size
),
1600 rom_transaction_begin();
1602 for (; hex_blob
< end
&& !parser
.complete
; ++hex_blob
) {
1603 switch (*hex_blob
) {
1611 if ((LEN_EXCEPT_DATA
+ parser
.line
.byte_count
) * 2 !=
1613 our_checksum
!= 0) {
1614 parser
.total_size
= -1;
1618 if (handle_record_type(&parser
) == -1) {
1619 parser
.total_size
= -1;
1624 /* start of a new record. */
1626 memset(&parser
.line
, 0, sizeof(HexLine
));
1631 /* decoding lines */
1633 if (!parse_record(&parser
.line
, &our_checksum
, *hex_blob
,
1634 &record_index
, in_process
)) {
1635 parser
.total_size
= -1;
1643 g_free(parser
.bin_buf
);
1644 rom_transaction_end(parser
.total_size
!= -1);
1645 return parser
.total_size
;
1648 /* return size or -1 if error */
1649 int load_targphys_hex_as(const char *filename
, hwaddr
*entry
, AddressSpace
*as
)
1651 gsize hex_blob_size
;
1655 if (!g_file_get_contents(filename
, &hex_blob
, &hex_blob_size
, NULL
)) {
1659 total_size
= parse_hex_blob(filename
, entry
, (uint8_t *)hex_blob
,