1ca20cf32SBlue Swirl #ifndef LOADER_H 2ca20cf32SBlue Swirl #define LOADER_H 384f2d0eaSWenchao Xia #include "qapi/qmp/qdict.h" 4a88b362cSLaszlo Ersek #include "hw/nvram/fw_cfg.h" 5ca20cf32SBlue Swirl 6ca20cf32SBlue Swirl /* loader.c */ 7a1483f88SMichael S. Tsirkin /** 8a1483f88SMichael S. Tsirkin * get_image_size: retrieve size of an image file 9a1483f88SMichael S. Tsirkin * @filename: Path to the image file 10a1483f88SMichael S. Tsirkin * 11a1483f88SMichael S. Tsirkin * Returns the size of the image file on success, -1 otherwise. 12a1483f88SMichael S. Tsirkin * On error, errno is also set as appropriate. 13a1483f88SMichael S. Tsirkin */ 14ca20cf32SBlue Swirl int get_image_size(const char *filename); 15ca20cf32SBlue Swirl int load_image(const char *filename, uint8_t *addr); /* deprecated */ 16ea87616dSBenjamin Herrenschmidt ssize_t load_image_size(const char *filename, void *addr, size_t size); 1793ffc7c7SAlistair Francis 1893ffc7c7SAlistair Francis /**load_image_targphys_as: 1993ffc7c7SAlistair Francis * @filename: Path to the image file 2093ffc7c7SAlistair Francis * @addr: Address to load the image to 2193ffc7c7SAlistair Francis * @max_sz: The maximum size of the image to load 2293ffc7c7SAlistair Francis * @as: The AddressSpace to load the ELF to. The value of address_space_memory 2393ffc7c7SAlistair Francis * is used if nothing is supplied here. 2493ffc7c7SAlistair Francis * 2593ffc7c7SAlistair Francis * Load a fixed image into memory. 2693ffc7c7SAlistair Francis * 2793ffc7c7SAlistair Francis * Returns the size of the loaded image on success, -1 otherwise. 2893ffc7c7SAlistair Francis */ 2993ffc7c7SAlistair Francis int load_image_targphys_as(const char *filename, 3093ffc7c7SAlistair Francis hwaddr addr, uint64_t max_sz, AddressSpace *as); 3193ffc7c7SAlistair Francis 3293ffc7c7SAlistair Francis /** load_image_targphys: 3393ffc7c7SAlistair Francis * Same as load_image_targphys_as(), but doesn't allow the caller to specify 3493ffc7c7SAlistair Francis * an AddressSpace. 3593ffc7c7SAlistair Francis */ 36a8170e5eSAvi Kivity int load_image_targphys(const char *filename, hwaddr, 3780a2ba3dSMark Langsdorf uint64_t max_sz); 3893ffc7c7SAlistair Francis 3976151cacSPeter Maydell /** 4076151cacSPeter Maydell * load_image_mr: load an image into a memory region 4176151cacSPeter Maydell * @filename: Path to the image file 4276151cacSPeter Maydell * @mr: Memory Region to load into 4376151cacSPeter Maydell * 4476151cacSPeter Maydell * Load the specified file into the memory region. 4576151cacSPeter Maydell * The file loaded is registered as a ROM, so its contents will be 4676151cacSPeter Maydell * reinstated whenever the system is reset. 4776151cacSPeter Maydell * If the file is larger than the memory region's size the call will fail. 4876151cacSPeter Maydell * Returns -1 on failure, or the size of the file. 4976151cacSPeter Maydell */ 5076151cacSPeter Maydell int load_image_mr(const char *filename, MemoryRegion *mr); 517d48a0f7SLaszlo Ersek 527d48a0f7SLaszlo Ersek /* This is the limit on the maximum uncompressed image size that 537d48a0f7SLaszlo Ersek * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents 547d48a0f7SLaszlo Ersek * g_malloc() in those functions from allocating a huge amount of memory. 557d48a0f7SLaszlo Ersek */ 567d48a0f7SLaszlo Ersek #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20) 577d48a0f7SLaszlo Ersek 587d48a0f7SLaszlo Ersek int load_image_gzipped_buffer(const char *filename, uint64_t max_sz, 597d48a0f7SLaszlo Ersek uint8_t **buffer); 60235e74afSRichard W.M. Jones int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz); 6118674b26SAlexey Kardashevskiy 6218674b26SAlexey Kardashevskiy #define ELF_LOAD_FAILED -1 6318674b26SAlexey Kardashevskiy #define ELF_LOAD_NOT_ELF -2 6418674b26SAlexey Kardashevskiy #define ELF_LOAD_WRONG_ARCH -3 6518674b26SAlexey Kardashevskiy #define ELF_LOAD_WRONG_ENDIAN -4 6618674b26SAlexey Kardashevskiy const char *load_elf_strerror(int error); 67140b7ce5SPeter Crosthwaite 6870bb1d16SAlistair Francis /** load_elf_as: 69140b7ce5SPeter Crosthwaite * @filename: Path of ELF file 70140b7ce5SPeter Crosthwaite * @translate_fn: optional function to translate load addresses 71140b7ce5SPeter Crosthwaite * @translate_opaque: opaque data passed to @translate_fn 72140b7ce5SPeter Crosthwaite * @pentry: Populated with program entry point. Ignored if NULL. 73140b7ce5SPeter Crosthwaite * @lowaddr: Populated with lowest loaded address. Ignored if NULL. 74140b7ce5SPeter Crosthwaite * @highaddr: Populated with highest loaded address. Ignored if NULL. 75140b7ce5SPeter Crosthwaite * @bigendian: Expected ELF endianness. 0 for LE otherwise BE 76140b7ce5SPeter Crosthwaite * @elf_machine: Expected ELF machine type 77140b7ce5SPeter Crosthwaite * @clear_lsb: Set to mask off LSB of addresses (Some architectures use 78140b7ce5SPeter Crosthwaite * this for non-address data) 797ef295eaSPeter Crosthwaite * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1 807ef295eaSPeter Crosthwaite * for swapping bytes within halfwords, 2 for bytes within 817ef295eaSPeter Crosthwaite * words and 3 for within doublewords. 8270bb1d16SAlistair Francis * @as: The AddressSpace to load the ELF to. The value of address_space_memory 8370bb1d16SAlistair Francis * is used if nothing is supplied here. 84140b7ce5SPeter Crosthwaite * 85140b7ce5SPeter Crosthwaite * Load an ELF file's contents to the emulated system's address space. 86140b7ce5SPeter Crosthwaite * Clients may optionally specify a callback to perform address 87140b7ce5SPeter Crosthwaite * translations. @pentry, @lowaddr and @highaddr are optional pointers 88140b7ce5SPeter Crosthwaite * which will be populated with various load information. @bigendian and 89140b7ce5SPeter Crosthwaite * @elf_machine give the expected endianness and machine for the ELF the 90140b7ce5SPeter Crosthwaite * load will fail if the target ELF does not match. Some architectures 91140b7ce5SPeter Crosthwaite * have some architecture-specific behaviours that come into effect when 92140b7ce5SPeter Crosthwaite * their particular values for @elf_machine are set. 938cf6e9daSAlistair Francis * If @elf_machine is EM_NONE then the machine type will be read from the 948cf6e9daSAlistair Francis * ELF header and no checks will be carried out against the machine type. 95140b7ce5SPeter Crosthwaite */ 9670bb1d16SAlistair Francis int load_elf_as(const char *filename, 9770bb1d16SAlistair Francis uint64_t (*translate_fn)(void *, uint64_t), 9870bb1d16SAlistair Francis void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr, 9970bb1d16SAlistair Francis uint64_t *highaddr, int big_endian, int elf_machine, 10070bb1d16SAlistair Francis int clear_lsb, int data_swab, AddressSpace *as); 101140b7ce5SPeter Crosthwaite 10270bb1d16SAlistair Francis /** load_elf: 10370bb1d16SAlistair Francis * Same as load_elf_as(), but doesn't allow the caller to specify an 10470bb1d16SAlistair Francis * AddressSpace. 10570bb1d16SAlistair Francis */ 106409dbce5SAurelien Jarno int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t), 107409dbce5SAurelien Jarno void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr, 108409dbce5SAurelien Jarno uint64_t *highaddr, int big_endian, int elf_machine, 1097ef295eaSPeter Crosthwaite int clear_lsb, int data_swab); 11004ae712aSPeter Crosthwaite 11104ae712aSPeter Crosthwaite /** load_elf_hdr: 11204ae712aSPeter Crosthwaite * @filename: Path of ELF file 11304ae712aSPeter Crosthwaite * @hdr: Buffer to populate with header data. Header data will not be 11404ae712aSPeter Crosthwaite * filled if set to NULL. 11504ae712aSPeter Crosthwaite * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL 11604ae712aSPeter Crosthwaite * @errp: Populated with an error in failure cases 11704ae712aSPeter Crosthwaite * 11804ae712aSPeter Crosthwaite * Inspect an ELF file's header. Read its full header contents into a 11904ae712aSPeter Crosthwaite * buffer and/or determine if the ELF is 64bit. 12004ae712aSPeter Crosthwaite */ 12104ae712aSPeter Crosthwaite void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp); 12204ae712aSPeter Crosthwaite 123a8170e5eSAvi Kivity int load_aout(const char *filename, hwaddr addr, int max_sz, 124a8170e5eSAvi Kivity int bswap_needed, hwaddr target_page_size); 1255e774eb3SAlistair Francis 1265e774eb3SAlistair Francis /** load_uimage_as: 1275e774eb3SAlistair Francis * @filename: Path of uimage file 1285e774eb3SAlistair Francis * @ep: Populated with program entry point. Ignored if NULL. 1295e774eb3SAlistair Francis * @loadaddr: Populated with the load address. Ignored if NULL. 1305e774eb3SAlistair Francis * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL. 1315e774eb3SAlistair Francis * @translate_fn: optional function to translate load addresses 1325e774eb3SAlistair Francis * @translate_opaque: opaque data passed to @translate_fn 1335e774eb3SAlistair Francis * @as: The AddressSpace to load the ELF to. The value of address_space_memory 1345e774eb3SAlistair Francis * is used if nothing is supplied here. 1355e774eb3SAlistair Francis * 1365e774eb3SAlistair Francis * Loads a u-boot image into memory. 1375e774eb3SAlistair Francis * 1385e774eb3SAlistair Francis * Returns the size of the loaded image on success, -1 otherwise. 1395e774eb3SAlistair Francis */ 1405e774eb3SAlistair Francis int load_uimage_as(const char *filename, hwaddr *ep, 1415e774eb3SAlistair Francis hwaddr *loadaddr, int *is_linux, 1425e774eb3SAlistair Francis uint64_t (*translate_fn)(void *, uint64_t), 1435e774eb3SAlistair Francis void *translate_opaque, AddressSpace *as); 1445e774eb3SAlistair Francis 1455e774eb3SAlistair Francis /** load_uimage: 1465e774eb3SAlistair Francis * Same as load_uimage_as(), but doesn't allow the caller to specify an 1475e774eb3SAlistair Francis * AddressSpace. 1485e774eb3SAlistair Francis */ 149a8170e5eSAvi Kivity int load_uimage(const char *filename, hwaddr *ep, 15025bda50aSMax Filippov hwaddr *loadaddr, int *is_linux, 15125bda50aSMax Filippov uint64_t (*translate_fn)(void *, uint64_t), 15225bda50aSMax Filippov void *translate_opaque); 153ca20cf32SBlue Swirl 15484aee0deSSoren Brinkmann /** 15584aee0deSSoren Brinkmann * load_ramdisk: 15684aee0deSSoren Brinkmann * @filename: Path to the ramdisk image 15784aee0deSSoren Brinkmann * @addr: Memory address to load the ramdisk to 15884aee0deSSoren Brinkmann * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks) 15984aee0deSSoren Brinkmann * 16084aee0deSSoren Brinkmann * Load a ramdisk image with U-Boot header to the specified memory 16184aee0deSSoren Brinkmann * address. 16284aee0deSSoren Brinkmann * 16384aee0deSSoren Brinkmann * Returns the size of the loaded image on success, -1 otherwise. 16484aee0deSSoren Brinkmann */ 16584aee0deSSoren Brinkmann int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz); 16684aee0deSSoren Brinkmann 167725e14e9SMarkus Armbruster ssize_t read_targphys(const char *name, 168a8170e5eSAvi Kivity int fd, hwaddr dst_addr, size_t nbytes); 1693c178e72SGerd Hoffmann void pstrcpy_targphys(const char *name, 170a8170e5eSAvi Kivity hwaddr dest, int buf_size, 171ca20cf32SBlue Swirl const char *source); 17245a50b16SGerd Hoffmann 173ac41881bSMichael S. Tsirkin extern bool option_rom_has_mr; 17498bc3ab0SMichael S. Tsirkin extern bool rom_file_has_mr; 175bdb5ee30SGerd Hoffmann 176bdb5ee30SGerd Hoffmann int rom_add_file(const char *file, const char *fw_dir, 177ac41881bSMichael S. Tsirkin hwaddr addr, int32_t bootindex, 1783e76099aSAlistair Francis bool option_rom, MemoryRegion *mr, AddressSpace *as); 179339240b5SPaolo Bonzini MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len, 180339240b5SPaolo Bonzini size_t max_len, hwaddr addr, 181339240b5SPaolo Bonzini const char *fw_file_name, 182339240b5SPaolo Bonzini FWCfgReadCallback fw_callback, 183*baf2d5bfSMichael S. Tsirkin void *callback_opaque, AddressSpace *as, 184*baf2d5bfSMichael S. Tsirkin bool read_only); 185d60fa42eSFabien Chouteau int rom_add_elf_program(const char *name, void *data, size_t datasize, 1863e76099aSAlistair Francis size_t romsize, hwaddr addr, AddressSpace *as); 1876b3f7f63SEric Auger int rom_check_and_register_reset(void); 188a88b362cSLaszlo Ersek void rom_set_fw(FWCfgState *f); 189bab47d9aSGerd Hoffmann void rom_set_order_override(int order); 190bab47d9aSGerd Hoffmann void rom_reset_order_override(void); 191a8170e5eSAvi Kivity int rom_copy(uint8_t *dest, hwaddr addr, size_t size); 192a8170e5eSAvi Kivity void *rom_ptr(hwaddr addr); 1931ce6be24SMarkus Armbruster void hmp_info_roms(Monitor *mon, const QDict *qdict); 19445a50b16SGerd Hoffmann 1952e55e842SGleb Natapov #define rom_add_file_fixed(_f, _a, _i) \ 1963e76099aSAlistair Francis rom_add_file(_f, NULL, _a, _i, false, NULL, NULL) 19745a50b16SGerd Hoffmann #define rom_add_blob_fixed(_f, _b, _l, _a) \ 198*baf2d5bfSMichael S. Tsirkin rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true) 19976151cacSPeter Maydell #define rom_add_file_mr(_f, _mr, _i) \ 2003e76099aSAlistair Francis rom_add_file(_f, NULL, 0, _i, false, _mr, NULL) 2013e76099aSAlistair Francis #define rom_add_file_as(_f, _as, _i) \ 2023e76099aSAlistair Francis rom_add_file(_f, NULL, 0, _i, false, NULL, _as) 20393ffc7c7SAlistair Francis #define rom_add_file_fixed_as(_f, _a, _i, _as) \ 20493ffc7c7SAlistair Francis rom_add_file(_f, NULL, _a, _i, false, NULL, _as) 2055e774eb3SAlistair Francis #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as) \ 206*baf2d5bfSMichael S. Tsirkin rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true) 20745a50b16SGerd Hoffmann 20845a50b16SGerd Hoffmann #define PC_ROM_MIN_VGA 0xc0000 20945a50b16SGerd Hoffmann #define PC_ROM_MIN_OPTION 0xc8000 21045a50b16SGerd Hoffmann #define PC_ROM_MAX 0xe0000 21145a50b16SGerd Hoffmann #define PC_ROM_ALIGN 0x800 21245a50b16SGerd Hoffmann #define PC_ROM_SIZE (PC_ROM_MAX - PC_ROM_MIN_VGA) 21345a50b16SGerd Hoffmann 214de2aff17SGerd Hoffmann int rom_add_vga(const char *file); 2152e55e842SGleb Natapov int rom_add_option(const char *file, int32_t bootindex); 21645a50b16SGerd Hoffmann 217ca20cf32SBlue Swirl #endif 218