]>
Commit | Line | Data |
---|---|---|
ca20cf32 BS |
1 | #ifndef LOADER_H |
2 | #define LOADER_H | |
a88b362c | 3 | #include "hw/nvram/fw_cfg.h" |
ca20cf32 BS |
4 | |
5 | /* loader.c */ | |
a1483f88 MT |
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 | */ | |
f3839fda | 13 | int64_t get_image_size(const char *filename); |
06192329 PM |
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 | */ | |
ea87616d | 30 | ssize_t load_image_size(const char *filename, void *addr, size_t size); |
93ffc7c7 AF |
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 | int load_image_targphys_as(const char *filename, | |
44 | hwaddr addr, uint64_t max_sz, AddressSpace *as); | |
45 | ||
e4a25ed9 SH |
46 | /**load_targphys_hex_as: |
47 | * @filename: Path to the .hex file | |
48 | * @entry: Store the entry point given by the .hex file | |
49 | * @as: The AddressSpace to load the .hex file to. The value of | |
50 | * address_space_memory is used if nothing is supplied here. | |
51 | * | |
52 | * Load a fixed .hex file into memory. | |
53 | * | |
54 | * Returns the size of the loaded .hex file on success, -1 otherwise. | |
55 | */ | |
56 | int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as); | |
57 | ||
93ffc7c7 AF |
58 | /** load_image_targphys: |
59 | * Same as load_image_targphys_as(), but doesn't allow the caller to specify | |
60 | * an AddressSpace. | |
61 | */ | |
a8170e5e | 62 | int load_image_targphys(const char *filename, hwaddr, |
80a2ba3d | 63 | uint64_t max_sz); |
93ffc7c7 | 64 | |
76151cac PM |
65 | /** |
66 | * load_image_mr: load an image into a memory region | |
67 | * @filename: Path to the image file | |
68 | * @mr: Memory Region to load into | |
69 | * | |
70 | * Load the specified file into the memory region. | |
71 | * The file loaded is registered as a ROM, so its contents will be | |
72 | * reinstated whenever the system is reset. | |
73 | * If the file is larger than the memory region's size the call will fail. | |
74 | * Returns -1 on failure, or the size of the file. | |
75 | */ | |
76 | int load_image_mr(const char *filename, MemoryRegion *mr); | |
7d48a0f7 LE |
77 | |
78 | /* This is the limit on the maximum uncompressed image size that | |
79 | * load_image_gzipped_buffer() and load_image_gzipped() will read. It prevents | |
80 | * g_malloc() in those functions from allocating a huge amount of memory. | |
81 | */ | |
82 | #define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20) | |
83 | ||
84 | int load_image_gzipped_buffer(const char *filename, uint64_t max_sz, | |
85 | uint8_t **buffer); | |
235e74af | 86 | int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz); |
18674b26 AK |
87 | |
88 | #define ELF_LOAD_FAILED -1 | |
89 | #define ELF_LOAD_NOT_ELF -2 | |
90 | #define ELF_LOAD_WRONG_ARCH -3 | |
91 | #define ELF_LOAD_WRONG_ENDIAN -4 | |
92 | const char *load_elf_strerror(int error); | |
140b7ce5 | 93 | |
a2480ffa | 94 | /** load_elf_ram_sym: |
140b7ce5 PC |
95 | * @filename: Path of ELF file |
96 | * @translate_fn: optional function to translate load addresses | |
97 | * @translate_opaque: opaque data passed to @translate_fn | |
98 | * @pentry: Populated with program entry point. Ignored if NULL. | |
99 | * @lowaddr: Populated with lowest loaded address. Ignored if NULL. | |
100 | * @highaddr: Populated with highest loaded address. Ignored if NULL. | |
101 | * @bigendian: Expected ELF endianness. 0 for LE otherwise BE | |
102 | * @elf_machine: Expected ELF machine type | |
103 | * @clear_lsb: Set to mask off LSB of addresses (Some architectures use | |
104 | * this for non-address data) | |
7ef295ea PC |
105 | * @data_swab: Set to order of byte swapping for data. 0 for no swap, 1 |
106 | * for swapping bytes within halfwords, 2 for bytes within | |
107 | * words and 3 for within doublewords. | |
70bb1d16 AF |
108 | * @as: The AddressSpace to load the ELF to. The value of address_space_memory |
109 | * is used if nothing is supplied here. | |
34f1b23f | 110 | * @load_rom : Load ELF binary as ROM |
a2480ffa | 111 | * @sym_cb: Callback function for symbol table entries |
140b7ce5 PC |
112 | * |
113 | * Load an ELF file's contents to the emulated system's address space. | |
114 | * Clients may optionally specify a callback to perform address | |
115 | * translations. @pentry, @lowaddr and @highaddr are optional pointers | |
116 | * which will be populated with various load information. @bigendian and | |
117 | * @elf_machine give the expected endianness and machine for the ELF the | |
118 | * load will fail if the target ELF does not match. Some architectures | |
119 | * have some architecture-specific behaviours that come into effect when | |
120 | * their particular values for @elf_machine are set. | |
8cf6e9da AF |
121 | * If @elf_machine is EM_NONE then the machine type will be read from the |
122 | * ELF header and no checks will be carried out against the machine type. | |
140b7ce5 | 123 | */ |
a2480ffa MC |
124 | typedef void (*symbol_fn_t)(const char *st_name, int st_info, |
125 | uint64_t st_value, uint64_t st_size); | |
126 | ||
127 | int load_elf_ram_sym(const char *filename, | |
128 | uint64_t (*translate_fn)(void *, uint64_t), | |
129 | void *translate_opaque, uint64_t *pentry, | |
130 | uint64_t *lowaddr, uint64_t *highaddr, int big_endian, | |
131 | int elf_machine, int clear_lsb, int data_swab, | |
132 | AddressSpace *as, bool load_rom, symbol_fn_t sym_cb); | |
133 | ||
134 | /** load_elf_ram: | |
135 | * Same as load_elf_ram_sym(), but doesn't allow the caller to specify a | |
136 | * symbol callback function | |
137 | */ | |
34f1b23f FA |
138 | int load_elf_ram(const char *filename, |
139 | uint64_t (*translate_fn)(void *, uint64_t), | |
140 | void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr, | |
141 | uint64_t *highaddr, int big_endian, int elf_machine, | |
142 | int clear_lsb, int data_swab, AddressSpace *as, | |
143 | bool load_rom); | |
144 | ||
145 | /** load_elf_as: | |
146 | * Same as load_elf_ram(), but always loads the elf as ROM | |
147 | */ | |
70bb1d16 AF |
148 | int load_elf_as(const char *filename, |
149 | uint64_t (*translate_fn)(void *, uint64_t), | |
150 | void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr, | |
151 | uint64_t *highaddr, int big_endian, int elf_machine, | |
152 | int clear_lsb, int data_swab, AddressSpace *as); | |
140b7ce5 | 153 | |
70bb1d16 AF |
154 | /** load_elf: |
155 | * Same as load_elf_as(), but doesn't allow the caller to specify an | |
156 | * AddressSpace. | |
157 | */ | |
409dbce5 AJ |
158 | int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t), |
159 | void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr, | |
160 | uint64_t *highaddr, int big_endian, int elf_machine, | |
7ef295ea | 161 | int clear_lsb, int data_swab); |
04ae712a PC |
162 | |
163 | /** load_elf_hdr: | |
164 | * @filename: Path of ELF file | |
165 | * @hdr: Buffer to populate with header data. Header data will not be | |
166 | * filled if set to NULL. | |
167 | * @is64: Set to true if the ELF is 64bit. Ignored if set to NULL | |
168 | * @errp: Populated with an error in failure cases | |
169 | * | |
170 | * Inspect an ELF file's header. Read its full header contents into a | |
171 | * buffer and/or determine if the ELF is 64bit. | |
172 | */ | |
173 | void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp); | |
174 | ||
a8170e5e AK |
175 | int load_aout(const char *filename, hwaddr addr, int max_sz, |
176 | int bswap_needed, hwaddr target_page_size); | |
5e774eb3 | 177 | |
f831f955 NH |
178 | #define LOAD_UIMAGE_LOADADDR_INVALID (-1) |
179 | ||
5e774eb3 AF |
180 | /** load_uimage_as: |
181 | * @filename: Path of uimage file | |
182 | * @ep: Populated with program entry point. Ignored if NULL. | |
f831f955 NH |
183 | * @loadaddr: load address if none specified in the image or when loading a |
184 | * ramdisk. Populated with the load address. Ignored if NULL or | |
185 | * LOAD_UIMAGE_LOADADDR_INVALID (images which do not specify a load | |
186 | * address will not be loadable). | |
5e774eb3 AF |
187 | * @is_linux: Is set to true if the image loaded is Linux. Ignored if NULL. |
188 | * @translate_fn: optional function to translate load addresses | |
189 | * @translate_opaque: opaque data passed to @translate_fn | |
190 | * @as: The AddressSpace to load the ELF to. The value of address_space_memory | |
191 | * is used if nothing is supplied here. | |
192 | * | |
193 | * Loads a u-boot image into memory. | |
194 | * | |
195 | * Returns the size of the loaded image on success, -1 otherwise. | |
196 | */ | |
197 | int load_uimage_as(const char *filename, hwaddr *ep, | |
198 | hwaddr *loadaddr, int *is_linux, | |
199 | uint64_t (*translate_fn)(void *, uint64_t), | |
200 | void *translate_opaque, AddressSpace *as); | |
201 | ||
202 | /** load_uimage: | |
203 | * Same as load_uimage_as(), but doesn't allow the caller to specify an | |
204 | * AddressSpace. | |
205 | */ | |
a8170e5e | 206 | int load_uimage(const char *filename, hwaddr *ep, |
25bda50a MF |
207 | hwaddr *loadaddr, int *is_linux, |
208 | uint64_t (*translate_fn)(void *, uint64_t), | |
209 | void *translate_opaque); | |
ca20cf32 | 210 | |
84aee0de | 211 | /** |
97df5fee | 212 | * load_ramdisk_as: |
84aee0de SB |
213 | * @filename: Path to the ramdisk image |
214 | * @addr: Memory address to load the ramdisk to | |
215 | * @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks) | |
97df5fee PM |
216 | * @as: The AddressSpace to load the ELF to. The value of address_space_memory |
217 | * is used if nothing is supplied here. | |
84aee0de SB |
218 | * |
219 | * Load a ramdisk image with U-Boot header to the specified memory | |
220 | * address. | |
221 | * | |
222 | * Returns the size of the loaded image on success, -1 otherwise. | |
223 | */ | |
97df5fee PM |
224 | int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz, |
225 | AddressSpace *as); | |
226 | ||
227 | /** | |
228 | * load_ramdisk: | |
229 | * Same as load_ramdisk_as(), but doesn't allow the caller to specify | |
230 | * an AddressSpace. | |
231 | */ | |
84aee0de SB |
232 | int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz); |
233 | ||
51b58561 PB |
234 | ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen); |
235 | ||
725e14e9 | 236 | ssize_t read_targphys(const char *name, |
a8170e5e | 237 | int fd, hwaddr dst_addr, size_t nbytes); |
3c178e72 | 238 | void pstrcpy_targphys(const char *name, |
a8170e5e | 239 | hwaddr dest, int buf_size, |
ca20cf32 | 240 | const char *source); |
45a50b16 | 241 | |
ac41881b | 242 | extern bool option_rom_has_mr; |
98bc3ab0 | 243 | extern bool rom_file_has_mr; |
bdb5ee30 GH |
244 | |
245 | int rom_add_file(const char *file, const char *fw_dir, | |
ac41881b | 246 | hwaddr addr, int32_t bootindex, |
3e76099a | 247 | bool option_rom, MemoryRegion *mr, AddressSpace *as); |
339240b5 PB |
248 | MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len, |
249 | size_t max_len, hwaddr addr, | |
250 | const char *fw_file_name, | |
6f6f4aec | 251 | FWCfgCallback fw_callback, |
baf2d5bf MT |
252 | void *callback_opaque, AddressSpace *as, |
253 | bool read_only); | |
d60fa42e | 254 | int rom_add_elf_program(const char *name, void *data, size_t datasize, |
3e76099a | 255 | size_t romsize, hwaddr addr, AddressSpace *as); |
6b3f7f63 | 256 | int rom_check_and_register_reset(void); |
a88b362c | 257 | void rom_set_fw(FWCfgState *f); |
bab47d9a GH |
258 | void rom_set_order_override(int order); |
259 | void rom_reset_order_override(void); | |
e2336043 SH |
260 | |
261 | /** | |
262 | * rom_transaction_begin: | |
263 | * | |
264 | * Call this before of a series of rom_add_*() calls. Call | |
265 | * rom_transaction_end() afterwards to commit or abort. These functions are | |
266 | * useful for undoing a series of rom_add_*() calls if image file loading fails | |
267 | * partway through. | |
268 | */ | |
269 | void rom_transaction_begin(void); | |
270 | ||
271 | /** | |
272 | * rom_transaction_end: | |
273 | * @commit: true to commit added roms, false to drop added roms | |
274 | * | |
275 | * Call this after a series of rom_add_*() calls. See rom_transaction_begin(). | |
276 | */ | |
277 | void rom_transaction_end(bool commit); | |
278 | ||
a8170e5e | 279 | int rom_copy(uint8_t *dest, hwaddr addr, size_t size); |
0f0f8b61 | 280 | void *rom_ptr(hwaddr addr, size_t size); |
1ce6be24 | 281 | void hmp_info_roms(Monitor *mon, const QDict *qdict); |
45a50b16 | 282 | |
2e55e842 | 283 | #define rom_add_file_fixed(_f, _a, _i) \ |
3e76099a | 284 | rom_add_file(_f, NULL, _a, _i, false, NULL, NULL) |
45a50b16 | 285 | #define rom_add_blob_fixed(_f, _b, _l, _a) \ |
baf2d5bf | 286 | rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, NULL, true) |
76151cac | 287 | #define rom_add_file_mr(_f, _mr, _i) \ |
3e76099a AF |
288 | rom_add_file(_f, NULL, 0, _i, false, _mr, NULL) |
289 | #define rom_add_file_as(_f, _as, _i) \ | |
290 | rom_add_file(_f, NULL, 0, _i, false, NULL, _as) | |
93ffc7c7 AF |
291 | #define rom_add_file_fixed_as(_f, _a, _i, _as) \ |
292 | rom_add_file(_f, NULL, _a, _i, false, NULL, _as) | |
5e774eb3 | 293 | #define rom_add_blob_fixed_as(_f, _b, _l, _a, _as) \ |
baf2d5bf | 294 | rom_add_blob(_f, _b, _l, _l, _a, NULL, NULL, NULL, _as, true) |
45a50b16 GH |
295 | |
296 | #define PC_ROM_MIN_VGA 0xc0000 | |
297 | #define PC_ROM_MIN_OPTION 0xc8000 | |
298 | #define PC_ROM_MAX 0xe0000 | |
299 | #define PC_ROM_ALIGN 0x800 | |
300 | #define PC_ROM_SIZE (PC_ROM_MAX - PC_ROM_MIN_VGA) | |
301 | ||
de2aff17 | 302 | int rom_add_vga(const char *file); |
2e55e842 | 303 | int rom_add_option(const char *file, int32_t bootindex); |
45a50b16 | 304 | |
51b58561 PB |
305 | /* This is the usual maximum in uboot, so if a uImage overflows this, it would |
306 | * overflow on real hardware too. */ | |
307 | #define UBOOT_MAX_GUNZIP_BYTES (64 << 20) | |
308 | ||
ca20cf32 | 309 | #endif |