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