2
0

loader.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #ifndef LOADER_H
  2. #define LOADER_H
  3. #include "hw/nvram/fw_cfg.h"
  4. /* loader.c */
  5. /**
  6. * get_image_size: retrieve size of an image file
  7. * @filename: Path to the image file
  8. *
  9. * Returns the size of the image file on success, -1 otherwise.
  10. * On error, errno is also set as appropriate.
  11. */
  12. int64_t get_image_size(const char *filename);
  13. /**
  14. * load_image_size: load an image file into specified buffer
  15. * @filename: Path to the image file
  16. * @addr: Buffer to load image into
  17. * @size: Size of buffer in bytes
  18. *
  19. * Load an image file from disk into the specified buffer.
  20. * If the image is larger than the specified buffer, only
  21. * @size bytes are read (this is not considered an error).
  22. *
  23. * Prefer to use the GLib function g_file_get_contents() rather
  24. * than a "get_image_size()/g_malloc()/load_image_size()" sequence.
  25. *
  26. * Returns the number of bytes read, or -1 on error. On error,
  27. * errno is also set as appropriate.
  28. */
  29. ssize_t load_image_size(const char *filename, void *addr, size_t size);
  30. /**load_image_targphys_as:
  31. * @filename: Path to the image file
  32. * @addr: Address to load the image to
  33. * @max_sz: The maximum size of the image to load
  34. * @as: The AddressSpace to load the ELF to. The value of address_space_memory
  35. * is used if nothing is supplied here.
  36. *
  37. * Load a fixed image into memory.
  38. *
  39. * Returns the size of the loaded image on success, -1 otherwise.
  40. */
  41. ssize_t load_image_targphys_as(const char *filename,
  42. hwaddr addr, uint64_t max_sz, AddressSpace *as);
  43. /**load_targphys_hex_as:
  44. * @filename: Path to the .hex file
  45. * @entry: Store the entry point given by the .hex file
  46. * @as: The AddressSpace to load the .hex file to. The value of
  47. * address_space_memory is used if nothing is supplied here.
  48. *
  49. * Load a fixed .hex file into memory.
  50. *
  51. * Returns the size of the loaded .hex file on success, -1 otherwise.
  52. */
  53. ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
  54. AddressSpace *as);
  55. /** load_image_targphys:
  56. * Same as load_image_targphys_as(), but doesn't allow the caller to specify
  57. * an AddressSpace.
  58. */
  59. ssize_t load_image_targphys(const char *filename, hwaddr,
  60. uint64_t max_sz);
  61. /**
  62. * load_image_mr: load an image into a memory region
  63. * @filename: Path to the image file
  64. * @mr: Memory Region to load into
  65. *
  66. * Load the specified file into the memory region.
  67. * The file loaded is registered as a ROM, so its contents will be
  68. * reinstated whenever the system is reset.
  69. * If the file is larger than the memory region's size the call will fail.
  70. * Returns -1 on failure, or the size of the file.
  71. */
  72. ssize_t load_image_mr(const char *filename, MemoryRegion *mr);
  73. /* This is the limit on the maximum uncompressed image size that
  74. * load_image_gzipped_buffer() will read. It prevents
  75. * g_malloc() in those functions from allocating a huge amount of memory.
  76. */
  77. #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20)
  78. ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
  79. uint8_t **buffer);
  80. /**
  81. * unpack_efi_zboot_image:
  82. * @buffer: pointer to a variable holding the address of a buffer containing the
  83. * image
  84. * @size: pointer to a variable holding the size of the buffer
  85. *
  86. * Check whether the buffer contains a EFI zboot image, and if it does, extract
  87. * the compressed payload and decompress it into a new buffer. If successful,
  88. * the old buffer is freed, and the *buffer and size variables pointed to by the
  89. * function arguments are updated to refer to the newly populated buffer.
  90. *
  91. * Returns 0 if the image could not be identified as a EFI zboot image.
  92. * Returns -1 if the buffer contents were identified as a EFI zboot image, but
  93. * unpacking failed for any reason.
  94. * Returns the size of the decompressed payload if decompression was performed
  95. * successfully.
  96. */
  97. ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size);
  98. #define ELF_LOAD_FAILED -1
  99. #define ELF_LOAD_NOT_ELF -2
  100. #define ELF_LOAD_WRONG_ARCH -3
  101. #define ELF_LOAD_WRONG_ENDIAN -4
  102. #define ELF_LOAD_TOO_BIG -5
  103. const char *load_elf_strerror(ssize_t error);
  104. /** load_elf_ram_sym:
  105. * @filename: Path of ELF file
  106. * @elf_note_fn: optional function to parse ELF Note type
  107. * passed via @translate_opaque
  108. * @translate_fn: optional function to translate load addresses
  109. * @translate_opaque: opaque data passed to @translate_fn
  110. * @pentry: Populated with program entry point. Ignored if NULL.
  111. * @lowaddr: Populated with lowest loaded address. Ignored if NULL.
  112. * @highaddr: Populated with highest loaded address. Ignored if NULL.
  113. * @pflags: Populated with ELF processor-specific flags. Ignore if NULL.
  114. * @elf_data_order: Expected ELF endianness (ELFDATA2LSB or ELFDATA2MSB).
  115. * @elf_machine: Expected ELF machine type
  116. * @clear_lsb: Set to mask off LSB of addresses (Some architectures use
  117. * this for non-address data)
  118. * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1
  119. * for swapping bytes within halfwords, 2 for bytes within
  120. * words and 3 for within doublewords.
  121. * @as: The AddressSpace to load the ELF to. The value of address_space_memory
  122. * is used if nothing is supplied here.
  123. * @load_rom : Load ELF binary as ROM
  124. * @sym_cb: Callback function for symbol table entries
  125. *
  126. * Load an ELF file's contents to the emulated system's address space.
  127. * Clients may optionally specify a callback to perform address
  128. * translations. @pentry, @lowaddr and @highaddr are optional pointers
  129. * which will be populated with various load information. @bigendian and
  130. * @elf_machine give the expected endianness and machine for the ELF the
  131. * load will fail if the target ELF does not match. Some architectures
  132. * have some architecture-specific behaviours that come into effect when
  133. * their particular values for @elf_machine are set.
  134. * If @elf_machine is EM_NONE then the machine type will be read from the
  135. * ELF header and no checks will be carried out against the machine type.
  136. */
  137. typedef void (*symbol_fn_t)(const char *st_name, int st_info,
  138. uint64_t st_value, uint64_t st_size);
  139. ssize_t load_elf_ram_sym(const char *filename,
  140. uint64_t (*elf_note_fn)(void *, void *, bool),
  141. uint64_t (*translate_fn)(void *, uint64_t),
  142. void *translate_opaque, uint64_t *pentry,
  143. uint64_t *lowaddr, uint64_t *highaddr,
  144. uint32_t *pflags, int elf_data_order, int elf_machine,
  145. int clear_lsb, int data_swab,
  146. AddressSpace *as, bool load_rom, symbol_fn_t sym_cb);
  147. /** load_elf_as:
  148. * Same as load_elf_ram_sym(), but always loads the elf as ROM
  149. */
  150. ssize_t load_elf_as(const char *filename,
  151. uint64_t (*elf_note_fn)(void *, void *, bool),
  152. uint64_t (*translate_fn)(void *, uint64_t),
  153. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  154. uint64_t *highaddr, uint32_t *pflags, int elf_data_order,
  155. int elf_machine, int clear_lsb, int data_swab,
  156. AddressSpace *as);
  157. /** load_elf:
  158. * Same as load_elf_as(), but doesn't allow the caller to specify an
  159. * AddressSpace.
  160. */
  161. ssize_t load_elf(const char *filename,
  162. uint64_t (*elf_note_fn)(void *, void *, bool),
  163. uint64_t (*translate_fn)(void *, uint64_t),
  164. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  165. uint64_t *highaddr, uint32_t *pflags, int elf_data_order,
  166. int elf_machine, int clear_lsb, int data_swab);
  167. /** load_elf_hdr:
  168. * @filename: Path of ELF file
  169. * @hdr: Buffer to populate with header data. Header data will not be
  170. * filled if set to NULL.
  171. * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL
  172. * @errp: Populated with an error in failure cases
  173. *
  174. * Inspect an ELF file's header. Read its full header contents into a
  175. * buffer and/or determine if the ELF is 64bit.
  176. */
  177. void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp);
  178. ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
  179. bool big_endian, hwaddr target_page_size);
  180. #define LOAD_UIMAGE_LOADADDR_INVALID (-1)
  181. /** load_uimage_as:
  182. * @filename: Path of uimage file
  183. * @ep: Populated with program entry point. Ignored if NULL.
  184. * @loadaddr: load address if none specified in the image or when loading a
  185. * ramdisk. Populated with the load address. Ignored if NULL or
  186. * LOAD_UIMAGE_LOADADDR_INVALID (images which do not specify a load
  187. * address will not be loadable).
  188. * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL.
  189. * @translate_fn: optional function to translate load addresses
  190. * @translate_opaque: opaque data passed to @translate_fn
  191. * @as: The AddressSpace to load the ELF to. The value of address_space_memory
  192. * is used if nothing is supplied here.
  193. *
  194. * Loads a u-boot image into memory.
  195. *
  196. * Returns the size of the loaded image on success, -1 otherwise.
  197. */
  198. ssize_t load_uimage_as(const char *filename, hwaddr *ep,
  199. hwaddr *loadaddr, int *is_linux,
  200. uint64_t (*translate_fn)(void *, uint64_t),
  201. void *translate_opaque, AddressSpace *as);
  202. /** load_uimage:
  203. * Same as load_uimage_as(), but doesn't allow the caller to specify an
  204. * AddressSpace.
  205. */
  206. ssize_t load_uimage(const char *filename, hwaddr *ep,
  207. hwaddr *loadaddr, int *is_linux,
  208. uint64_t (*translate_fn)(void *, uint64_t),
  209. void *translate_opaque);
  210. /**
  211. * load_ramdisk_as:
  212. * @filename: Path to the ramdisk image
  213. * @addr: Memory address to load the ramdisk to
  214. * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks)
  215. * @as: The AddressSpace to load the ELF to. The value of address_space_memory
  216. * is used if nothing is supplied here.
  217. *
  218. * Load a ramdisk image with U-Boot header to the specified memory
  219. * address.
  220. *
  221. * Returns the size of the loaded image on success, -1 otherwise.
  222. */
  223. ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
  224. AddressSpace *as);
  225. /**
  226. * load_ramdisk:
  227. * Same as load_ramdisk_as(), but doesn't allow the caller to specify
  228. * an AddressSpace.
  229. */
  230. ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz);
  231. ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen);
  232. ssize_t read_targphys(const char *name,
  233. int fd, hwaddr dst_addr, size_t nbytes);
  234. void pstrcpy_targphys(const char *name,
  235. hwaddr dest, int buf_size,
  236. const char *source);
  237. ssize_t rom_add_file(const char *file, const char *fw_dir,
  238. hwaddr addr, int32_t bootindex,
  239. bool has_option_rom, MemoryRegion *mr, AddressSpace *as);
  240. MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
  241. size_t max_len, hwaddr addr,
  242. const char *fw_file_name,
  243. FWCfgCallback fw_callback,
  244. void *callback_opaque, AddressSpace *as,
  245. bool read_only);
  246. int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
  247. size_t datasize, size_t romsize, hwaddr addr,
  248. AddressSpace *as);
  249. int rom_check_and_register_reset(void);
  250. void rom_set_fw(FWCfgState *f);
  251. void rom_set_order_override(int order);
  252. void rom_reset_order_override(void);
  253. /**
  254. * rom_transaction_begin:
  255. *
  256. * Call this before of a series of rom_add_*() calls. Call
  257. * rom_transaction_end() afterwards to commit or abort. These functions are
  258. * useful for undoing a series of rom_add_*() calls if image file loading fails
  259. * partway through.
  260. */
  261. void rom_transaction_begin(void);
  262. /**
  263. * rom_transaction_end:
  264. * @commit: true to commit added roms, false to drop added roms
  265. *
  266. * Call this after a series of rom_add_*() calls. See rom_transaction_begin().
  267. */
  268. void rom_transaction_end(bool commit);
  269. int rom_copy(uint8_t *dest, hwaddr addr, size_t size);
  270. void *rom_ptr(hwaddr addr, size_t size);
  271. /**
  272. * rom_ptr_for_as: Return a pointer to ROM blob data for the address
  273. * @as: AddressSpace to look for the ROM blob in
  274. * @addr: Address within @as
  275. * @size: size of data required in bytes
  276. *
  277. * Returns: pointer into the data which backs the matching ROM blob,
  278. * or NULL if no blob covers the address range.
  279. *
  280. * This function looks for a ROM blob which covers the specified range
  281. * of bytes of length @size starting at @addr within the address space
  282. * @as. This is useful for code which runs as part of board
  283. * initialization or CPU reset which wants to read data that is part
  284. * of a user-supplied guest image or other guest memory contents, but
  285. * which runs before the ROM loader's reset function has copied the
  286. * blobs into guest memory.
  287. *
  288. * rom_ptr_for_as() will look not just for blobs loaded directly to
  289. * the specified address, but also for blobs which were loaded to an
  290. * alias of the region at a different location in the AddressSpace.
  291. * In other words, if a machine model has RAM at address 0x0000_0000
  292. * which is aliased to also appear at 0x1000_0000, rom_ptr_for_as()
  293. * will return the correct data whether the guest image was linked and
  294. * loaded at 0x0000_0000 or 0x1000_0000. Contrast rom_ptr(), which
  295. * will only return data if the image load address is an exact match
  296. * with the queried address.
  297. *
  298. * New code should prefer to use rom_ptr_for_as() instead of
  299. * rom_ptr().
  300. */
  301. void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size);
  302. #define rom_add_file_fixed(_f, _a, _i) \
  303. rom_add_file(_f, NULL, _a, _i, false, NULL, NULL)
  304. #define rom_add_blob_fixed(_f, _b, _l, _a) \
  305. rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true)
  306. #define rom_add_file_mr(_f, _mr, _i) \
  307. rom_add_file(_f, NULL, 0, _i, false, _mr, NULL)
  308. #define rom_add_file_as(_f, _as, _i) \
  309. rom_add_file(_f, NULL, 0, _i, false, NULL, _as)
  310. #define rom_add_file_fixed_as(_f, _a, _i, _as) \
  311. rom_add_file(_f, NULL, _a, _i, false, NULL, _as)
  312. #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as) \
  313. rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true)
  314. ssize_t rom_add_vga(const char *file);
  315. ssize_t rom_add_option(const char *file, int32_t bootindex);
  316. /* This is the usual maximum in uboot, so if a uImage overflows this, it would
  317. * overflow on real hardware too. */
  318. #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
  319. typedef struct RomGap {
  320. hwaddr base;
  321. size_t size;
  322. } RomGap;
  323. /**
  324. * rom_find_largest_gap_between: return largest gap between ROMs in given range
  325. *
  326. * Given a range of addresses, this function finds the largest
  327. * contiguous subrange which has no ROMs loaded to it. That is,
  328. * it finds the biggest gap which is free for use for other things.
  329. */
  330. RomGap rom_find_largest_gap_between(hwaddr base, size_t size);
  331. #endif