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/datadir.h"
47 #include "qapi/error.h"
48 #include "qapi/qapi-commands-machine.h"
49 #include "qapi/type-helpers.h"
52 #include "disas/disas.h"
53 #include "migration/vmstate.h"
54 #include "monitor/monitor.h"
55 #include "sysemu/reset.h"
56 #include "sysemu/sysemu.h"
57 #include "uboot_image.h"
58 #include "hw/loader.h"
59 #include "hw/nvram/fw_cfg.h"
60 #include "exec/memory.h"
61 #include "hw/boards.h"
62 #include "qemu/cutils.h"
63 #include "sysemu/runstate.h"
64 #include "accel/tcg/debuginfo.h"
68 static int roms_loaded;
70 /* return the size or -1 if error */
71 int64_t get_image_size(const char *filename)
75 fd = open(filename, O_RDONLY | O_BINARY);
78 size = lseek(fd, 0, SEEK_END);
83 /* return the size or -1 if error */
84 ssize_t load_image_size(const char *filename, void *addr, size_t size)
87 ssize_t actsize, l = 0;
89 fd = open(filename, O_RDONLY | O_BINARY);
94 while ((actsize = read(fd, addr + l, size - l)) > 0) {
100 return actsize < 0 ? -1 : l;
103 /* read()-like version */
104 ssize_t read_targphys(const char *name,
105 int fd, hwaddr dst_addr, size_t nbytes)
110 buf = g_malloc(nbytes);
111 did = read(fd, buf, nbytes);
113 rom_add_blob_fixed("read", buf, did, dst_addr);
118 ssize_t load_image_targphys(const char *filename,
119 hwaddr addr, uint64_t max_sz)
121 return load_image_targphys_as(filename, addr, max_sz, NULL);
124 /* return the size or -1 if error */
125 ssize_t load_image_targphys_as(const char *filename,
126 hwaddr addr, uint64_t max_sz, AddressSpace *as)
130 size = get_image_size(filename);
131 if (size < 0 || size > max_sz) {
135 if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
142 ssize_t load_image_mr(const char *filename, MemoryRegion *mr)
146 if (!memory_access_is_direct(mr, false)) {
147 /* Can only load an image into RAM or ROM */
151 size = get_image_size(filename);
153 if (size < 0 || size > memory_region_size(mr)) {
157 if (rom_add_file_mr(filename, mr, -1) < 0) {
164 void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
170 if (buf_size <= 0) return;
171 nulp = memchr(source, 0, buf_size);
173 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
175 rom_add_blob_fixed(name, source, buf_size, dest);
176 ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr));
185 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
186 uint32_t a_text; /* length of text, in bytes */
187 uint32_t a_data; /* length of data, in bytes */
188 uint32_t a_bss; /* length of uninitialized data area, in bytes */
189 uint32_t a_syms; /* length of symbol table data in file, in bytes */
190 uint32_t a_entry; /* start address */
191 uint32_t a_trsize; /* length of relocation info for text, in bytes */
192 uint32_t a_drsize; /* length of relocation info for data, in bytes */
195 static void bswap_ahdr(struct exec *e)
197 bswap32s(&e->a_info);
198 bswap32s(&e->a_text);
199 bswap32s(&e->a_data);
201 bswap32s(&e->a_syms);
202 bswap32s(&e->a_entry);
203 bswap32s(&e->a_trsize);
204 bswap32s(&e->a_drsize);
207 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
212 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
213 #define N_TXTOFF(x) \
214 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
215 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
216 #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
217 #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
219 #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
221 #define N_DATADDR(x, target_page_size) \
222 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
223 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
226 ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
227 int bswap_needed, hwaddr target_page_size)
234 fd = open(filename, O_RDONLY | O_BINARY);
238 size = read(fd, &e, sizeof(e));
251 if (e.a_text + e.a_data > max_sz)
253 lseek(fd, N_TXTOFF(e), SEEK_SET);
254 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
259 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
261 lseek(fd, N_TXTOFF(e), SEEK_SET);
262 size = read_targphys(filename, fd, addr, e.a_text);
265 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
283 static void *load_at(int fd, off_t offset, size_t size)
286 if (lseek(fd, offset, SEEK_SET) < 0)
288 ptr = g_malloc(size);
289 if (read(fd, ptr, size) != size) {
300 #define ELF_CLASS ELFCLASS32
304 #define elf_word uint32_t
305 #define elf_sword int32_t
306 #define bswapSZs bswap32s
307 #include "hw/elf_ops.h"
319 #define elfhdr elf64_hdr
320 #define elf_phdr elf64_phdr
321 #define elf_note elf64_note
322 #define elf_shdr elf64_shdr
323 #define elf_sym elf64_sym
324 #define elf_rela elf64_rela
325 #define elf_word uint64_t
326 #define elf_sword int64_t
327 #define bswapSZs bswap64s
329 #include "hw/elf_ops.h"
331 const char *load_elf_strerror(ssize_t error)
336 case ELF_LOAD_FAILED:
337 return "Failed to load ELF";
338 case ELF_LOAD_NOT_ELF:
339 return "The image is not ELF";
340 case ELF_LOAD_WRONG_ARCH:
341 return "The image is from incompatible architecture";
342 case ELF_LOAD_WRONG_ENDIAN:
343 return "The image has incorrect endianness";
344 case ELF_LOAD_TOO_BIG:
345 return "The image segments are too big to load";
347 return "Unknown error";
351 void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
354 uint8_t e_ident_local[EI_NIDENT];
356 size_t hdr_size, off;
364 fd = open(filename, O_RDONLY | O_BINARY);
366 error_setg_errno(errp, errno, "Failed to open file: %s", filename);
369 if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
370 error_setg_errno(errp, errno, "Failed to read file: %s", filename);
373 if (e_ident[0] != ELFMAG0 ||
374 e_ident[1] != ELFMAG1 ||
375 e_ident[2] != ELFMAG2 ||
376 e_ident[3] != ELFMAG3) {
377 error_setg(errp, "Bad ELF magic");
381 is64l = e_ident[EI_CLASS] == ELFCLASS64;
382 hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr);
388 while (hdr != e_ident_local && off < hdr_size) {
389 size_t br = read(fd, hdr + off, hdr_size - off);
392 error_setg(errp, "File too short: %s", filename);
395 error_setg_errno(errp, errno, "Failed to read file: %s",
406 /* return < 0 if error, otherwise the number of bytes loaded in memory */
407 ssize_t load_elf(const char *filename,
408 uint64_t (*elf_note_fn)(void *, void *, bool),
409 uint64_t (*translate_fn)(void *, uint64_t),
410 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
411 uint64_t *highaddr, uint32_t *pflags, int big_endian,
412 int elf_machine, int clear_lsb, int data_swab)
414 return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque,
415 pentry, lowaddr, highaddr, pflags, big_endian,
416 elf_machine, clear_lsb, data_swab, NULL);
419 /* return < 0 if error, otherwise the number of bytes loaded in memory */
420 ssize_t load_elf_as(const char *filename,
421 uint64_t (*elf_note_fn)(void *, void *, bool),
422 uint64_t (*translate_fn)(void *, uint64_t),
423 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
424 uint64_t *highaddr, uint32_t *pflags, int big_endian,
425 int elf_machine, int clear_lsb, int data_swab,
428 return load_elf_ram(filename, elf_note_fn, translate_fn, translate_opaque,
429 pentry, lowaddr, highaddr, pflags, big_endian,
430 elf_machine, clear_lsb, data_swab, as, true);
433 /* return < 0 if error, otherwise the number of bytes loaded in memory */
434 ssize_t load_elf_ram(const char *filename,
435 uint64_t (*elf_note_fn)(void *, void *, bool),
436 uint64_t (*translate_fn)(void *, uint64_t),
437 void *translate_opaque, uint64_t *pentry,
438 uint64_t *lowaddr, uint64_t *highaddr, uint32_t *pflags,
439 int big_endian, int elf_machine, int clear_lsb,
440 int data_swab, AddressSpace *as, bool load_rom)
442 return load_elf_ram_sym(filename, elf_note_fn,
443 translate_fn, translate_opaque,
444 pentry, lowaddr, highaddr, pflags, big_endian,
445 elf_machine, clear_lsb, data_swab, as,
449 /* return < 0 if error, otherwise the number of bytes loaded in memory */
450 ssize_t load_elf_ram_sym(const char *filename,
451 uint64_t (*elf_note_fn)(void *, void *, bool),
452 uint64_t (*translate_fn)(void *, uint64_t),
453 void *translate_opaque, uint64_t *pentry,
454 uint64_t *lowaddr, uint64_t *highaddr,
455 uint32_t *pflags, int big_endian, int elf_machine,
456 int clear_lsb, int data_swab,
457 AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
459 int fd, data_order, target_data_order, must_swab;
460 ssize_t ret = ELF_LOAD_FAILED;
461 uint8_t e_ident[EI_NIDENT];
463 fd = open(filename, O_RDONLY | O_BINARY);
468 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
470 if (e_ident[0] != ELFMAG0 ||
471 e_ident[1] != ELFMAG1 ||
472 e_ident[2] != ELFMAG2 ||
473 e_ident[3] != ELFMAG3) {
474 ret = ELF_LOAD_NOT_ELF;
478 data_order = ELFDATA2MSB;
480 data_order = ELFDATA2LSB;
482 must_swab = data_order != e_ident[EI_DATA];
484 target_data_order = ELFDATA2MSB;
486 target_data_order = ELFDATA2LSB;
489 if (target_data_order != e_ident[EI_DATA]) {
490 ret = ELF_LOAD_WRONG_ENDIAN;
494 lseek(fd, 0, SEEK_SET);
495 if (e_ident[EI_CLASS] == ELFCLASS64) {
496 ret = load_elf64(filename, fd, elf_note_fn,
497 translate_fn, translate_opaque, must_swab,
498 pentry, lowaddr, highaddr, pflags, elf_machine,
499 clear_lsb, data_swab, as, load_rom, sym_cb);
501 ret = load_elf32(filename, fd, elf_note_fn,
502 translate_fn, translate_opaque, must_swab,
503 pentry, lowaddr, highaddr, pflags, elf_machine,
504 clear_lsb, data_swab, as, load_rom, sym_cb);
507 if (ret != ELF_LOAD_FAILED) {
508 debuginfo_report_elf(filename, fd, 0);
516 static void bswap_uboot_header(uboot_image_header_t *hdr)
519 bswap32s(&hdr->ih_magic);
520 bswap32s(&hdr->ih_hcrc);
521 bswap32s(&hdr->ih_time);
522 bswap32s(&hdr->ih_size);
523 bswap32s(&hdr->ih_load);
524 bswap32s(&hdr->ih_ep);
525 bswap32s(&hdr->ih_dcrc);
530 #define ZALLOC_ALIGNMENT 16
532 static void *zalloc(void *x, unsigned items, unsigned size)
537 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
544 static void zfree(void *x, void *addr)
551 #define EXTRA_FIELD 4
554 #define RESERVED 0xe0
558 ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen)
570 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
571 puts ("Error: Bad gzipped data\n");
574 if ((flags & EXTRA_FIELD) != 0) {
578 i = 12 + src[10] + (src[11] << 8);
580 if ((flags & ORIG_NAME) != 0) {
581 while (i < srclen && src[i++] != 0) {
585 if ((flags & COMMENT) != 0) {
586 while (i < srclen && src[i++] != 0) {
590 if ((flags & HEAD_CRC) != 0) {
600 r = inflateInit2(&s, -MAX_WBITS);
602 printf ("Error: inflateInit2() returned %d\n", r);
606 s.avail_in = srclen - i;
608 s.avail_out = dstlen;
609 r = inflate(&s, Z_FINISH);
610 if (r != Z_OK && r != Z_STREAM_END) {
611 printf ("Error: inflate() returned %d\n", r);
614 dstbytes = s.next_out - (unsigned char *) dst;
620 puts("Error: gunzip out of data in header\n");
624 /* Load a U-Boot image. */
625 static ssize_t load_uboot_image(const char *filename, hwaddr *ep,
626 hwaddr *loadaddr, int *is_linux,
628 uint64_t (*translate_fn)(void *, uint64_t),
629 void *translate_opaque, AddressSpace *as)
634 uboot_image_header_t h;
635 uboot_image_header_t *hdr = &h;
636 uint8_t *data = NULL;
638 int do_uncompress = 0;
640 fd = open(filename, O_RDONLY | O_BINARY);
644 size = read(fd, hdr, sizeof(uboot_image_header_t));
645 if (size < sizeof(uboot_image_header_t)) {
649 bswap_uboot_header(hdr);
651 if (hdr->ih_magic != IH_MAGIC)
654 if (hdr->ih_type != image_type) {
655 if (!(image_type == IH_TYPE_KERNEL &&
656 hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
657 fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
663 /* TODO: Implement other image types. */
664 switch (hdr->ih_type) {
665 case IH_TYPE_KERNEL_NOLOAD:
666 if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
667 fprintf(stderr, "this image format (kernel_noload) cannot be "
668 "loaded on this machine type");
672 hdr->ih_load = *loadaddr + sizeof(*hdr);
673 hdr->ih_ep += hdr->ih_load;
676 address = hdr->ih_load;
678 address = translate_fn(translate_opaque, address);
681 *loadaddr = hdr->ih_load;
684 switch (hdr->ih_comp) {
692 "Unable to load u-boot images with compression type %d\n",
701 /* TODO: Check CPU type. */
703 if (hdr->ih_os == IH_OS_LINUX) {
705 } else if (hdr->ih_os == IH_OS_VXWORKS) {
707 * VxWorks 7 uses the same boot interface as the Linux kernel
708 * on Arm (64-bit only), PowerPC and RISC-V architectures.
710 switch (hdr->ih_arch) {
726 case IH_TYPE_RAMDISK:
730 fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
734 data = g_malloc(hdr->ih_size);
736 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
737 fprintf(stderr, "Error reading file\n");
742 uint8_t *compressed_data;
746 compressed_data = data;
747 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
748 data = g_malloc(max_bytes);
750 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
751 g_free(compressed_data);
753 fprintf(stderr, "Unable to decompress gzipped image!\n");
756 hdr->ih_size = bytes;
759 rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
769 ssize_t load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
771 uint64_t (*translate_fn)(void *, uint64_t),
772 void *translate_opaque)
774 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
775 translate_fn, translate_opaque, NULL);
778 ssize_t load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
780 uint64_t (*translate_fn)(void *, uint64_t),
781 void *translate_opaque, AddressSpace *as)
783 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
784 translate_fn, translate_opaque, as);
787 /* Load a ramdisk. */
788 ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
790 return load_ramdisk_as(filename, addr, max_sz, NULL);
793 ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
796 return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
800 /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
801 ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
804 uint8_t *compressed_data = NULL;
805 uint8_t *data = NULL;
810 if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
815 /* Is it a gzip-compressed file? */
817 compressed_data[0] != 0x1f ||
818 compressed_data[1] != 0x8b) {
822 if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
823 max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
826 data = g_malloc(max_sz);
827 bytes = gunzip(data, max_sz, compressed_data, len);
829 fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
834 /* trim to actual size and return to caller */
835 *buffer = g_realloc(data, bytes);
837 /* ownership has been transferred to caller */
841 g_free(compressed_data);
846 /* Load a gzip-compressed kernel. */
847 ssize_t load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
852 bytes = load_image_gzipped_buffer(filename, max_sz, &data);
854 rom_add_blob_fixed(filename, data, bytes, addr);
860 /* The PE/COFF MS-DOS stub magic number */
861 #define EFI_PE_MSDOS_MAGIC "MZ"
864 * The Linux header magic number for a EFI PE/COFF
865 * image targetting an unspecified architecture.
867 #define EFI_PE_LINUX_MAGIC "\xcd\x23\x82\x81"
870 * Bootable Linux kernel images may be packaged as EFI zboot images, which are
871 * self-decompressing executables when loaded via EFI. The compressed payload
872 * can also be extracted from the image and decompressed by a non-EFI loader.
874 * The de facto specification for this format is at the following URL:
876 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/firmware/efi/libstub/zboot-header.S
878 * This definition is based on Linux upstream commit 29636a5ce87beba.
880 struct linux_efi_zboot_header {
881 uint8_t msdos_magic[2]; /* PE/COFF 'MZ' magic number */
882 uint8_t reserved0[2];
883 uint8_t zimg[4]; /* "zimg" for Linux EFI zboot images */
884 uint32_t payload_offset; /* LE offset to compressed payload */
885 uint32_t payload_size; /* LE size of the compressed payload */
886 uint8_t reserved1[8];
887 char compression_type[32]; /* Compression type, NUL terminated */
888 uint8_t linux_magic[4]; /* Linux header magic */
889 uint32_t pe_header_offset; /* LE offset to the PE header */
893 * Check whether *buffer points to a Linux EFI zboot image in memory.
895 * If it does, attempt to decompress it to a new buffer, and free the old one.
896 * If any of this fails, return an error to the caller.
898 * If the image is not a Linux EFI zboot image, do nothing and return success.
900 ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size)
902 const struct linux_efi_zboot_header *header;
903 uint8_t *data = NULL;
907 /* ignore if this is too small to be a EFI zboot image */
908 if (*size < sizeof(*header)) {
912 header = (struct linux_efi_zboot_header *)*buffer;
914 /* ignore if this is not a Linux EFI zboot image */
915 if (memcmp(&header->msdos_magic, EFI_PE_MSDOS_MAGIC, 2) != 0 ||
916 memcmp(&header->zimg, "zimg", 4) != 0 ||
917 memcmp(&header->linux_magic, EFI_PE_LINUX_MAGIC, 4) != 0) {
921 if (strcmp(header->compression_type, "gzip") != 0) {
923 "unable to handle EFI zboot image with \"%.*s\" compression\n",
924 (int)sizeof(header->compression_type) - 1,
925 header->compression_type);
929 ploff = ldl_le_p(&header->payload_offset);
930 plsize = ldl_le_p(&header->payload_size);
932 if (ploff < 0 || plsize < 0 || ploff + plsize > *size) {
933 fprintf(stderr, "unable to handle corrupt EFI zboot image\n");
937 data = g_malloc(LOAD_IMAGE_MAX_GUNZIP_BYTES);
938 bytes = gunzip(data, LOAD_IMAGE_MAX_GUNZIP_BYTES, *buffer + ploff, plsize);
940 fprintf(stderr, "failed to decompress EFI zboot image\n");
946 *buffer = g_realloc(data, bytes);
952 * Functions for reboot-persistent memory regions.
953 * - used for vga bios and option roms.
954 * - also linux kernel (-kernel / -initrd).
957 typedef struct Rom Rom;
963 /* datasize is the amount of memory allocated in "data". If datasize is less
964 * than romsize, it means that the area from datasize to romsize is filled
976 GMappedFile *mapped_file;
981 QTAILQ_ENTRY(Rom) next;
984 static FWCfgState *fw_cfg;
985 static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
988 * rom->data can be heap-allocated or memory-mapped (e.g. when added with
989 * rom_add_elf_program())
991 static void rom_free_data(Rom *rom)
993 if (rom->mapped_file) {
994 g_mapped_file_unref(rom->mapped_file);
995 rom->mapped_file = NULL;
1003 static void rom_free(Rom *rom)
1008 g_free(rom->fw_dir);
1009 g_free(rom->fw_file);
1013 static inline bool rom_order_compare(Rom *rom, Rom *item)
1015 return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
1016 (rom->as == item->as && rom->addr >= item->addr);
1019 static void rom_insert(Rom *rom)
1024 hw_error ("ROM images must be loaded at startup\n");
1027 /* The user didn't specify an address space, this is the default */
1029 rom->as = &address_space_memory;
1032 rom->committed = false;
1034 /* List is ordered by load address in the same address space */
1035 QTAILQ_FOREACH(item, &roms, next) {
1036 if (rom_order_compare(rom, item)) {
1039 QTAILQ_INSERT_BEFORE(item, rom, next);
1042 QTAILQ_INSERT_TAIL(&roms, rom, next);
1045 static void fw_cfg_resized(const char *id, uint64_t length, void *host)
1048 fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
1052 static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
1056 rom->mr = g_malloc(sizeof(*rom->mr));
1057 memory_region_init_resizeable_ram(rom->mr, owner, name,
1058 rom->datasize, rom->romsize,
1061 memory_region_set_readonly(rom->mr, ro);
1062 vmstate_register_ram_global(rom->mr);
1064 data = memory_region_get_ram_ptr(rom->mr);
1065 memcpy(data, rom->data, rom->datasize);
1070 ssize_t rom_add_file(const char *file, const char *fw_dir,
1071 hwaddr addr, int32_t bootindex,
1072 bool option_rom, MemoryRegion *mr,
1075 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1082 fprintf(stderr, "Specifying an Address Space and Memory Region is " \
1083 "not valid when loading a rom\n");
1084 /* We haven't allocated anything so we don't need any cleanup */
1088 rom = g_malloc0(sizeof(*rom));
1089 rom->name = g_strdup(file);
1090 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
1092 if (rom->path == NULL) {
1093 rom->path = g_strdup(file);
1096 fd = open(rom->path, O_RDONLY | O_BINARY);
1098 fprintf(stderr, "Could not open option rom '%s': %s\n",
1099 rom->path, strerror(errno));
1104 rom->fw_dir = g_strdup(fw_dir);
1105 rom->fw_file = g_strdup(file);
1108 rom->romsize = lseek(fd, 0, SEEK_END);
1109 if (rom->romsize == -1) {
1110 fprintf(stderr, "rom: file %-20s: get size error: %s\n",
1111 rom->name, strerror(errno));
1115 rom->datasize = rom->romsize;
1116 rom->data = g_malloc0(rom->datasize);
1117 lseek(fd, 0, SEEK_SET);
1118 rc = read(fd, rom->data, rom->datasize);
1119 if (rc != rom->datasize) {
1120 fprintf(stderr, "rom: file %-20s: read error: rc=%zd (expected %zd)\n",
1121 rom->name, rc, rom->datasize);
1126 if (rom->fw_file && fw_cfg) {
1127 const char *basename;
1128 char fw_file_name[FW_CFG_MAX_FILE_PATH];
1131 basename = strrchr(rom->fw_file, '/');
1135 basename = rom->fw_file;
1137 snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
1139 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
1141 if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
1142 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
1147 fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
1151 snprintf(devpath, sizeof(devpath), "/rom@%s", file);
1153 snprintf(devpath, sizeof(devpath), "/rom@" HWADDR_FMT_plx, addr);
1157 add_boot_device_path(bootindex, NULL, devpath);
1168 MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
1169 size_t max_len, hwaddr addr, const char *fw_file_name,
1170 FWCfgCallback fw_callback, void *callback_opaque,
1171 AddressSpace *as, bool read_only)
1173 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1175 MemoryRegion *mr = NULL;
1177 rom = g_malloc0(sizeof(*rom));
1178 rom->name = g_strdup(name);
1181 rom->romsize = max_len ? max_len : len;
1182 rom->datasize = len;
1183 g_assert(rom->romsize >= rom->datasize);
1184 rom->data = g_malloc0(rom->datasize);
1185 memcpy(rom->data, blob, len);
1187 if (fw_file_name && fw_cfg) {
1192 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
1194 snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
1197 if (mc->rom_file_has_mr) {
1198 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
1204 fw_cfg_add_file_callback(fw_cfg, fw_file_name,
1205 fw_callback, NULL, callback_opaque,
1206 data, rom->datasize, read_only);
1211 /* This function is specific for elf program because we don't need to allocate
1212 * all the rom. We just allocate the first part and the rest is just zeros. This
1213 * is why romsize and datasize are different. Also, this function takes its own
1214 * reference to "mapped_file", so we don't have to allocate and copy the buffer.
1216 int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
1217 size_t datasize, size_t romsize, hwaddr addr,
1222 rom = g_malloc0(sizeof(*rom));
1223 rom->name = g_strdup(name);
1225 rom->datasize = datasize;
1226 rom->romsize = romsize;
1230 if (mapped_file && data) {
1231 g_mapped_file_ref(mapped_file);
1232 rom->mapped_file = mapped_file;
1239 ssize_t rom_add_vga(const char *file)
1241 return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
1244 ssize_t rom_add_option(const char *file, int32_t bootindex)
1246 return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
1249 static void rom_reset(void *unused)
1253 QTAILQ_FOREACH(rom, &roms, next) {
1258 * We don't need to fill in the RAM with ROM data because we'll fill
1259 * the data in during the next incoming migration in all cases. Note
1260 * that some of those RAMs can actually be modified by the guest.
1262 if (runstate_check(RUN_STATE_INMIGRATE)) {
1263 if (rom->data && rom->isrom) {
1265 * Free it so that a rom_reset after migration doesn't
1266 * overwrite a potentially modified 'rom'.
1273 if (rom->data == NULL) {
1277 void *host = memory_region_get_ram_ptr(rom->mr);
1278 memcpy(host, rom->data, rom->datasize);
1279 memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
1281 address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
1282 rom->data, rom->datasize);
1283 address_space_set(rom->as, rom->addr + rom->datasize, 0,
1284 rom->romsize - rom->datasize,
1285 MEMTXATTRS_UNSPECIFIED);
1288 /* rom needs to be written only once */
1292 * The rom loader is really on the same level as firmware in the guest
1293 * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
1294 * that the instruction cache for that new region is clear, so that the
1295 * CPU definitely fetches its instructions from the just written data.
1297 cpu_flush_icache_range(rom->addr, rom->datasize);
1299 trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom);
1303 /* Return true if two consecutive ROMs in the ROM list overlap */
1304 static bool roms_overlap(Rom *last_rom, Rom *this_rom)
1309 return last_rom->as == this_rom->as &&
1310 last_rom->addr + last_rom->romsize > this_rom->addr;
1313 static const char *rom_as_name(Rom *rom)
1315 const char *name = rom->as ? rom->as->name : NULL;
1316 return name ?: "anonymous";
1319 static void rom_print_overlap_error_header(void)
1321 error_report("Some ROM regions are overlapping");
1323 "These ROM regions might have been loaded by "
1324 "direct user request or by default.\n"
1325 "They could be BIOS/firmware images, a guest kernel, "
1326 "initrd or some other file loaded into guest memory.\n"
1327 "Check whether you intended to load all this guest code, and "
1328 "whether it has been built to load to the correct addresses.\n");
1331 static void rom_print_one_overlap_error(Rom *last_rom, Rom *rom)
1334 "\nThe following two regions overlap (in the %s address space):\n",
1337 " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
1338 last_rom->name, last_rom->addr, last_rom->addr + last_rom->romsize);
1340 " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
1341 rom->name, rom->addr, rom->addr + rom->romsize);
1344 int rom_check_and_register_reset(void)
1346 MemoryRegionSection section;
1347 Rom *rom, *last_rom = NULL;
1348 bool found_overlap = false;
1350 QTAILQ_FOREACH(rom, &roms, next) {
1355 if (roms_overlap(last_rom, rom)) {
1356 if (!found_overlap) {
1357 found_overlap = true;
1358 rom_print_overlap_error_header();
1360 rom_print_one_overlap_error(last_rom, rom);
1361 /* Keep going through the list so we report all overlaps */
1365 section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
1367 rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
1368 memory_region_unref(section.mr);
1370 if (found_overlap) {
1374 qemu_register_reset(rom_reset, NULL);
1379 void rom_set_fw(FWCfgState *f)
1384 void rom_set_order_override(int order)
1388 fw_cfg_set_order_override(fw_cfg, order);
1391 void rom_reset_order_override(void)
1395 fw_cfg_reset_order_override(fw_cfg);
1398 void rom_transaction_begin(void)
1402 /* Ignore ROMs added without the transaction API */
1403 QTAILQ_FOREACH(rom, &roms, next) {
1404 rom->committed = true;
1408 void rom_transaction_end(bool commit)
1413 QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
1414 if (rom->committed) {
1418 rom->committed = true;
1420 QTAILQ_REMOVE(&roms, rom, next);
1426 static Rom *find_rom(hwaddr addr, size_t size)
1430 QTAILQ_FOREACH(rom, &roms, next) {
1437 if (rom->addr > addr) {
1440 if (rom->addr + rom->romsize < addr + size) {
1448 typedef struct RomSec {
1450 int se; /* start/end flag */
1455 * Sort into address order. We break ties between rom-startpoints
1456 * and rom-endpoints in favour of the startpoint, by sorting the 0->1
1457 * transition before the 1->0 transition. Either way round would
1458 * work, but this way saves a little work later by avoiding
1459 * dealing with "gaps" of 0 length.
1461 static gint sort_secs(gconstpointer a, gconstpointer b)
1463 RomSec *ra = (RomSec *) a;
1464 RomSec *rb = (RomSec *) b;
1466 if (ra->base == rb->base) {
1467 return ra->se - rb->se;
1469 return ra->base > rb->base ? 1 : -1;
1472 static GList *add_romsec_to_list(GList *secs, hwaddr base, int se)
1474 RomSec *cand = g_new(RomSec, 1);
1477 return g_list_prepend(secs, cand);
1480 RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
1484 RomGap res = {0, 0};
1485 hwaddr gapstart = base;
1486 GList *it, *secs = NULL;
1489 QTAILQ_FOREACH(rom, &roms, next) {
1490 /* Ignore blobs being loaded to special places */
1491 if (rom->mr || rom->fw_file) {
1494 /* ignore anything finishing bellow base */
1495 if (rom->addr + rom->romsize <= base) {
1498 /* ignore anything starting above the region */
1499 if (rom->addr >= base + size) {
1503 /* Save the start and end of each relevant ROM */
1504 secs = add_romsec_to_list(secs, rom->addr, 1);
1506 if (rom->addr + rom->romsize < base + size) {
1507 secs = add_romsec_to_list(secs, rom->addr + rom->romsize, -1);
1512 secs = add_romsec_to_list(secs, base + size, 1);
1514 secs = g_list_sort(secs, sort_secs);
1516 for (it = g_list_first(secs); it; it = g_list_next(it)) {
1517 cand = (RomSec *) it->data;
1518 if (count == 0 && count + cand->se == 1) {
1519 size_t gap = cand->base - gapstart;
1520 if (gap > res.size) {
1521 res.base = gapstart;
1524 } else if (count == 1 && count + cand->se == 0) {
1525 gapstart = cand->base;
1530 g_list_free_full(secs, g_free);
1535 * Copies memory from registered ROMs to dest. Any memory that is contained in
1536 * a ROM between addr and addr + size is copied. Note that this can involve
1537 * multiple ROMs, which need not start at addr and need not end at addr + size.
1539 int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
1541 hwaddr end = addr + size;
1542 uint8_t *s, *d = dest;
1546 QTAILQ_FOREACH(rom, &roms, next) {
1553 if (rom->addr + rom->romsize < addr) {
1556 if (rom->addr > end || rom->addr < addr) {
1560 d = dest + (rom->addr - addr);
1564 if ((d + l) > (dest + size)) {
1572 if (rom->romsize > rom->datasize) {
1573 /* If datasize is less than romsize, it means that we didn't
1574 * allocate all the ROM because the trailing data are only zeros.
1578 l = rom->romsize - rom->datasize;
1580 if ((d + l) > (dest + size)) {
1581 /* Rom size doesn't fit in the destination area. Adjust to avoid
1593 return (d + l) - dest;
1596 void *rom_ptr(hwaddr addr, size_t size)
1600 rom = find_rom(addr, size);
1601 if (!rom || !rom->data)
1603 return rom->data + (addr - rom->addr);
1606 typedef struct FindRomCBData {
1607 size_t size; /* Amount of data we want from ROM, in bytes */
1608 MemoryRegion *mr; /* MR at the unaliased guest addr */
1609 hwaddr xlat; /* Offset of addr within mr */
1610 void *rom; /* Output: rom data pointer, if found */
1613 static bool find_rom_cb(Int128 start, Int128 len, const MemoryRegion *mr,
1614 hwaddr offset_in_region, void *opaque)
1616 FindRomCBData *cbdata = opaque;
1619 if (mr != cbdata->mr) {
1623 alias_addr = int128_get64(start) + cbdata->xlat - offset_in_region;
1624 cbdata->rom = rom_ptr(alias_addr, cbdata->size);
1628 /* Found a match, stop iterating */
1632 void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size)
1635 * Find any ROM data for the given guest address range. If there
1636 * is a ROM blob then return a pointer to the host memory
1637 * corresponding to 'addr'; otherwise return NULL.
1639 * We look not only for ROM blobs that were loaded directly to
1640 * addr, but also for ROM blobs that were loaded to aliases of
1641 * that memory at other addresses within the AddressSpace.
1643 * Note that we do not check @as against the 'as' member in the
1644 * 'struct Rom' returned by rom_ptr(). The Rom::as is the
1645 * AddressSpace which the rom blob should be written to, whereas
1646 * our @as argument is the AddressSpace which we are (effectively)
1647 * reading from, and the same underlying RAM will often be visible
1648 * in multiple AddressSpaces. (A common example is a ROM blob
1649 * written to the 'system' address space but then read back via a
1650 * CPU's cpu->as pointer.) This does mean we might potentially
1651 * return a false-positive match if a ROM blob was loaded into an
1652 * AS which is entirely separate and distinct from the one we're
1653 * querying, but this issue exists also for rom_ptr() and hasn't
1654 * caused any problems in practice.
1659 FindRomCBData cbdata = {};
1661 /* Easy case: there's data at the actual address */
1662 rom = rom_ptr(addr, size);
1667 RCU_READ_LOCK_GUARD();
1669 fv = address_space_to_flatview(as);
1670 cbdata.mr = flatview_translate(fv, addr, &cbdata.xlat, &len_unused,
1671 false, MEMTXATTRS_UNSPECIFIED);
1673 /* Nothing at this address, so there can't be any aliasing */
1677 flatview_for_each_range(fv, find_rom_cb, &cbdata);
1681 HumanReadableText *qmp_x_query_roms(Error **errp)
1684 g_autoptr(GString) buf = g_string_new("");
1686 QTAILQ_FOREACH(rom, &roms, next) {
1688 g_string_append_printf(buf, "%s"
1689 " size=0x%06zx name=\"%s\"\n",
1690 memory_region_name(rom->mr),
1693 } else if (!rom->fw_file) {
1694 g_string_append_printf(buf, "addr=" HWADDR_FMT_plx
1695 " size=0x%06zx mem=%s name=\"%s\"\n",
1696 rom->addr, rom->romsize,
1697 rom->isrom ? "rom" : "ram",
1700 g_string_append_printf(buf, "fw=%s/%s"
1701 " size=0x%06zx name=\"%s\"\n",
1709 return human_readable_text_from_str(buf);
1712 typedef enum HexRecord HexRecord;
1716 EXT_SEG_ADDR_RECORD,
1717 START_SEG_ADDR_RECORD,
1718 EXT_LINEAR_ADDR_RECORD,
1719 START_LINEAR_ADDR_RECORD,
1722 /* Each record contains a 16-bit address which is combined with the upper 16
1723 * bits of the implicit "next address" to form a 32-bit address.
1725 #define NEXT_ADDR_MASK 0xffff0000
1727 #define DATA_FIELD_MAX_LEN 0xff
1728 #define LEN_EXCEPT_DATA 0x5
1729 /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
1730 * sizeof(checksum) */
1734 uint8_t record_type;
1735 uint8_t data[DATA_FIELD_MAX_LEN];
1739 /* return 0 or -1 if error */
1740 static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
1741 uint32_t *index, const bool in_process)
1743 /* +-------+---------------+-------+---------------------+--------+
1744 * | byte | |record | | |
1745 * | count | address | type | data |checksum|
1746 * +-------+---------------+-------+---------------------+--------+
1748 * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
1751 uint32_t idx = *index;
1753 if (g_ascii_isspace(c)) {
1756 if (!g_ascii_isxdigit(c) || !in_process) {
1759 value = g_ascii_xdigit_value(c);
1760 value = (idx & 0x1) ? (value & 0xf) : (value << 4);
1762 line->byte_count |= value;
1763 } else if (2 <= idx && idx < 6) {
1764 line->address <<= 4;
1765 line->address += g_ascii_xdigit_value(c);
1766 } else if (6 <= idx && idx < 8) {
1767 line->record_type |= value;
1768 } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
1769 line->data[(idx - 8) >> 1] |= value;
1770 } else if (8 + 2 * line->byte_count <= idx &&
1771 idx < 10 + 2 * line->byte_count) {
1772 line->checksum |= value;
1776 *our_checksum += value;
1782 const char *filename;
1787 uint32_t next_address_to_write;
1788 uint32_t current_address;
1789 uint32_t current_rom_index;
1790 uint32_t rom_start_address;
1795 /* return size or -1 if error */
1796 static int handle_record_type(HexParser *parser)
1798 HexLine *line = &(parser->line);
1799 switch (line->record_type) {
1801 parser->current_address =
1802 (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
1803 /* verify this is a contiguous block of memory */
1804 if (parser->current_address != parser->next_address_to_write) {
1805 if (parser->current_rom_index != 0) {
1806 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1807 parser->current_rom_index,
1808 parser->rom_start_address, parser->as);
1810 parser->rom_start_address = parser->current_address;
1811 parser->current_rom_index = 0;
1814 /* copy from line buffer to output bin_buf */
1815 memcpy(parser->bin_buf + parser->current_rom_index, line->data,
1817 parser->current_rom_index += line->byte_count;
1818 parser->total_size += line->byte_count;
1819 /* save next address to write */
1820 parser->next_address_to_write =
1821 parser->current_address + line->byte_count;
1825 if (parser->current_rom_index != 0) {
1826 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1827 parser->current_rom_index,
1828 parser->rom_start_address, parser->as);
1830 parser->complete = true;
1831 return parser->total_size;
1832 case EXT_SEG_ADDR_RECORD:
1833 case EXT_LINEAR_ADDR_RECORD:
1834 if (line->byte_count != 2 && line->address != 0) {
1838 if (parser->current_rom_index != 0) {
1839 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1840 parser->current_rom_index,
1841 parser->rom_start_address, parser->as);
1844 /* save next address to write,
1845 * in case of non-contiguous block of memory */
1846 parser->next_address_to_write = (line->data[0] << 12) |
1847 (line->data[1] << 4);
1848 if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
1849 parser->next_address_to_write <<= 12;
1852 parser->rom_start_address = parser->next_address_to_write;
1853 parser->current_rom_index = 0;
1856 case START_SEG_ADDR_RECORD:
1857 if (line->byte_count != 4 && line->address != 0) {
1861 /* x86 16-bit CS:IP segmented addressing */
1862 *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
1863 ((line->data[2] << 8) | line->data[3]);
1866 case START_LINEAR_ADDR_RECORD:
1867 if (line->byte_count != 4 && line->address != 0) {
1871 *(parser->start_addr) = ldl_be_p(line->data);
1878 return parser->total_size;
1881 /* return size or -1 if error */
1882 static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
1883 size_t hex_blob_size, AddressSpace *as)
1885 bool in_process = false; /* avoid re-enter and
1886 * check whether record begin with ':' */
1887 uint8_t *end = hex_blob + hex_blob_size;
1888 uint8_t our_checksum = 0;
1889 uint32_t record_index = 0;
1890 HexParser parser = {
1891 .filename = filename,
1892 .bin_buf = g_malloc(hex_blob_size),
1898 rom_transaction_begin();
1900 for (; hex_blob < end && !parser.complete; ++hex_blob) {
1901 switch (*hex_blob) {
1909 if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
1911 our_checksum != 0) {
1912 parser.total_size = -1;
1916 if (handle_record_type(&parser) == -1) {
1917 parser.total_size = -1;
1922 /* start of a new record. */
1924 memset(&parser.line, 0, sizeof(HexLine));
1929 /* decoding lines */
1931 if (!parse_record(&parser.line, &our_checksum, *hex_blob,
1932 &record_index, in_process)) {
1933 parser.total_size = -1;
1941 g_free(parser.bin_buf);
1942 rom_transaction_end(parser.total_size != -1);
1943 return parser.total_size;
1946 /* return size or -1 if error */
1947 ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
1950 gsize hex_blob_size;
1952 ssize_t total_size = 0;
1954 if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
1958 total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,