root / hw / core / loader.c @ f487b677
History | View | Annotate | Download (21.3 kB)
1 | 5fe141fd | bellard | /*
|
---|---|---|---|
2 | 5fe141fd | bellard | * QEMU Executable loader
|
3 | 5fafdf24 | ths | *
|
4 | 5fe141fd | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 5fe141fd | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 5fe141fd | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 5fe141fd | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 5fe141fd | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 5fe141fd | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 5fe141fd | bellard | * furnished to do so, subject to the following conditions:
|
12 | 5fe141fd | bellard | *
|
13 | 5fe141fd | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 5fe141fd | bellard | * all copies or substantial portions of the Software.
|
15 | 5fe141fd | bellard | *
|
16 | 5fe141fd | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 5fe141fd | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 5fe141fd | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 5fe141fd | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 5fe141fd | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 5fe141fd | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 5fe141fd | bellard | * THE SOFTWARE.
|
23 | 5a123577 | aliguori | *
|
24 | 5a123577 | aliguori | * Gunzip functionality in this file is derived from u-boot:
|
25 | 5a123577 | aliguori | *
|
26 | 5a123577 | aliguori | * (C) Copyright 2008 Semihalf
|
27 | 5a123577 | aliguori | *
|
28 | 5a123577 | aliguori | * (C) Copyright 2000-2005
|
29 | 5a123577 | aliguori | * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
30 | 5a123577 | aliguori | *
|
31 | 5a123577 | aliguori | * This program is free software; you can redistribute it and/or
|
32 | 5a123577 | aliguori | * modify it under the terms of the GNU General Public License as
|
33 | 5a123577 | aliguori | * published by the Free Software Foundation; either version 2 of
|
34 | 5a123577 | aliguori | * the License, or (at your option) any later version.
|
35 | 5a123577 | aliguori | *
|
36 | 5a123577 | aliguori | * This program is distributed in the hope that it will be useful,
|
37 | 5a123577 | aliguori | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
38 | 5a123577 | aliguori | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
39 | 5a123577 | aliguori | * GNU General Public License for more details.
|
40 | 5a123577 | aliguori | *
|
41 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
42 | 8167ee88 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
43 | 5fe141fd | bellard | */
|
44 | 5a123577 | aliguori | |
45 | 83c9f4ca | Paolo Bonzini | #include "hw/hw.h" |
46 | 76cad711 | Paolo Bonzini | #include "disas/disas.h" |
47 | 83c9089e | Paolo Bonzini | #include "monitor/monitor.h" |
48 | 9c17d615 | Paolo Bonzini | #include "sysemu/sysemu.h" |
49 | 47b43a1f | Paolo Bonzini | #include "uboot_image.h" |
50 | 83c9f4ca | Paolo Bonzini | #include "hw/loader.h" |
51 | 0d09e41a | Paolo Bonzini | #include "hw/nvram/fw_cfg.h" |
52 | 022c62cb | Paolo Bonzini | #include "exec/memory.h" |
53 | 022c62cb | Paolo Bonzini | #include "exec/address-spaces.h" |
54 | 5fe141fd | bellard | |
55 | 5a123577 | aliguori | #include <zlib.h> |
56 | 5a123577 | aliguori | |
57 | 97fe84f5 | Paul Brook | static int roms_loaded; |
58 | 97fe84f5 | Paul Brook | |
59 | 5fe141fd | bellard | /* return the size or -1 if error */
|
60 | 5fe141fd | bellard | int get_image_size(const char *filename) |
61 | 5fe141fd | bellard | { |
62 | 5fe141fd | bellard | int fd, size;
|
63 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
64 | 5fe141fd | bellard | if (fd < 0) |
65 | 5fe141fd | bellard | return -1; |
66 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
67 | 5fe141fd | bellard | close(fd); |
68 | 5fe141fd | bellard | return size;
|
69 | 5fe141fd | bellard | } |
70 | 5fe141fd | bellard | |
71 | 5fe141fd | bellard | /* return the size or -1 if error */
|
72 | 293f78bc | blueswir1 | /* deprecated, because caller does not specify buffer size! */
|
73 | 5fe141fd | bellard | int load_image(const char *filename, uint8_t *addr) |
74 | 5fe141fd | bellard | { |
75 | 5fe141fd | bellard | int fd, size;
|
76 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
77 | 5fe141fd | bellard | if (fd < 0) |
78 | 5fe141fd | bellard | return -1; |
79 | 5fe141fd | bellard | size = lseek(fd, 0, SEEK_END);
|
80 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
81 | 5fe141fd | bellard | if (read(fd, addr, size) != size) {
|
82 | 5fe141fd | bellard | close(fd); |
83 | 5fe141fd | bellard | return -1; |
84 | 5fe141fd | bellard | } |
85 | 5fe141fd | bellard | close(fd); |
86 | 5fe141fd | bellard | return size;
|
87 | 5fe141fd | bellard | } |
88 | 5fe141fd | bellard | |
89 | 293f78bc | blueswir1 | /* read()-like version */
|
90 | 725e14e9 | Markus Armbruster | ssize_t read_targphys(const char *name, |
91 | a8170e5e | Avi Kivity | int fd, hwaddr dst_addr, size_t nbytes)
|
92 | 293f78bc | blueswir1 | { |
93 | 45a50b16 | Gerd Hoffmann | uint8_t *buf; |
94 | 725e14e9 | Markus Armbruster | ssize_t did; |
95 | 45a50b16 | Gerd Hoffmann | |
96 | 7267c094 | Anthony Liguori | buf = g_malloc(nbytes); |
97 | 45a50b16 | Gerd Hoffmann | did = read(fd, buf, nbytes); |
98 | 45a50b16 | Gerd Hoffmann | if (did > 0) |
99 | 45a50b16 | Gerd Hoffmann | rom_add_blob_fixed("read", buf, did, dst_addr);
|
100 | 7267c094 | Anthony Liguori | g_free(buf); |
101 | 45a50b16 | Gerd Hoffmann | return did;
|
102 | 293f78bc | blueswir1 | } |
103 | 293f78bc | blueswir1 | |
104 | 293f78bc | blueswir1 | /* return the size or -1 if error */
|
105 | 293f78bc | blueswir1 | int load_image_targphys(const char *filename, |
106 | a8170e5e | Avi Kivity | hwaddr addr, uint64_t max_sz) |
107 | 293f78bc | blueswir1 | { |
108 | 45a50b16 | Gerd Hoffmann | int size;
|
109 | 293f78bc | blueswir1 | |
110 | 45a50b16 | Gerd Hoffmann | size = get_image_size(filename); |
111 | 17df768c | Benjamin Herrenschmidt | if (size > max_sz) {
|
112 | 17df768c | Benjamin Herrenschmidt | return -1; |
113 | 17df768c | Benjamin Herrenschmidt | } |
114 | 17df768c | Benjamin Herrenschmidt | if (size > 0) { |
115 | 2e55e842 | Gleb Natapov | rom_add_file_fixed(filename, addr, -1);
|
116 | 17df768c | Benjamin Herrenschmidt | } |
117 | 45a50b16 | Gerd Hoffmann | return size;
|
118 | 293f78bc | blueswir1 | } |
119 | 293f78bc | blueswir1 | |
120 | a8170e5e | Avi Kivity | void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size, |
121 | 293f78bc | blueswir1 | const char *source) |
122 | 293f78bc | blueswir1 | { |
123 | 293f78bc | blueswir1 | const char *nulp; |
124 | 3c178e72 | Gerd Hoffmann | char *ptr;
|
125 | 293f78bc | blueswir1 | |
126 | 293f78bc | blueswir1 | if (buf_size <= 0) return; |
127 | 293f78bc | blueswir1 | nulp = memchr(source, 0, buf_size);
|
128 | 293f78bc | blueswir1 | if (nulp) {
|
129 | 3c178e72 | Gerd Hoffmann | rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
|
130 | 293f78bc | blueswir1 | } else {
|
131 | 3c178e72 | Gerd Hoffmann | rom_add_blob_fixed(name, source, buf_size, dest); |
132 | 3c178e72 | Gerd Hoffmann | ptr = rom_ptr(dest + buf_size - 1);
|
133 | 3c178e72 | Gerd Hoffmann | *ptr = 0;
|
134 | 293f78bc | blueswir1 | } |
135 | 293f78bc | blueswir1 | } |
136 | 293f78bc | blueswir1 | |
137 | 5fe141fd | bellard | /* A.OUT loader */
|
138 | 5fe141fd | bellard | |
139 | 5fe141fd | bellard | struct exec
|
140 | 5fe141fd | bellard | { |
141 | 5fe141fd | bellard | uint32_t a_info; /* Use macros N_MAGIC, etc for access */
|
142 | 5fe141fd | bellard | uint32_t a_text; /* length of text, in bytes */
|
143 | 5fe141fd | bellard | uint32_t a_data; /* length of data, in bytes */
|
144 | 5fe141fd | bellard | uint32_t a_bss; /* length of uninitialized data area, in bytes */
|
145 | 5fe141fd | bellard | uint32_t a_syms; /* length of symbol table data in file, in bytes */
|
146 | 5fe141fd | bellard | uint32_t a_entry; /* start address */
|
147 | 5fe141fd | bellard | uint32_t a_trsize; /* length of relocation info for text, in bytes */
|
148 | 5fe141fd | bellard | uint32_t a_drsize; /* length of relocation info for data, in bytes */
|
149 | 5fe141fd | bellard | }; |
150 | 5fe141fd | bellard | |
151 | 5fe141fd | bellard | static void bswap_ahdr(struct exec *e) |
152 | 5fe141fd | bellard | { |
153 | 5fe141fd | bellard | bswap32s(&e->a_info); |
154 | 5fe141fd | bellard | bswap32s(&e->a_text); |
155 | 5fe141fd | bellard | bswap32s(&e->a_data); |
156 | 5fe141fd | bellard | bswap32s(&e->a_bss); |
157 | 5fe141fd | bellard | bswap32s(&e->a_syms); |
158 | 5fe141fd | bellard | bswap32s(&e->a_entry); |
159 | 5fe141fd | bellard | bswap32s(&e->a_trsize); |
160 | 5fe141fd | bellard | bswap32s(&e->a_drsize); |
161 | 5fe141fd | bellard | } |
162 | 5fe141fd | bellard | |
163 | 5fe141fd | bellard | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
164 | 5fe141fd | bellard | #define OMAGIC 0407 |
165 | 5fe141fd | bellard | #define NMAGIC 0410 |
166 | 5fe141fd | bellard | #define ZMAGIC 0413 |
167 | 5fe141fd | bellard | #define QMAGIC 0314 |
168 | 5fe141fd | bellard | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
169 | 5fe141fd | bellard | #define N_TXTOFF(x) \
|
170 | 5fe141fd | bellard | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
171 | 5fe141fd | bellard | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
172 | ca20cf32 | Blue Swirl | #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0) |
173 | ca20cf32 | Blue Swirl | #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1)) |
174 | 5fe141fd | bellard | |
175 | ca20cf32 | Blue Swirl | #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
|
176 | 5fe141fd | bellard | |
177 | ca20cf32 | Blue Swirl | #define N_DATADDR(x, target_page_size) \
|
178 | ca20cf32 | Blue Swirl | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \ |
179 | ca20cf32 | Blue Swirl | : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size))) |
180 | 5fe141fd | bellard | |
181 | 5fe141fd | bellard | |
182 | a8170e5e | Avi Kivity | int load_aout(const char *filename, hwaddr addr, int max_sz, |
183 | a8170e5e | Avi Kivity | int bswap_needed, hwaddr target_page_size)
|
184 | 5fe141fd | bellard | { |
185 | 725e14e9 | Markus Armbruster | int fd;
|
186 | 725e14e9 | Markus Armbruster | ssize_t size, ret; |
187 | 5fe141fd | bellard | struct exec e;
|
188 | 5fe141fd | bellard | uint32_t magic; |
189 | 5fe141fd | bellard | |
190 | 5fe141fd | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
191 | 5fe141fd | bellard | if (fd < 0) |
192 | 5fe141fd | bellard | return -1; |
193 | 5fe141fd | bellard | |
194 | 5fe141fd | bellard | size = read(fd, &e, sizeof(e));
|
195 | 5fe141fd | bellard | if (size < 0) |
196 | 5fe141fd | bellard | goto fail;
|
197 | 5fe141fd | bellard | |
198 | ca20cf32 | Blue Swirl | if (bswap_needed) {
|
199 | ca20cf32 | Blue Swirl | bswap_ahdr(&e); |
200 | ca20cf32 | Blue Swirl | } |
201 | 5fe141fd | bellard | |
202 | 5fe141fd | bellard | magic = N_MAGIC(e); |
203 | 5fe141fd | bellard | switch (magic) {
|
204 | 5fe141fd | bellard | case ZMAGIC:
|
205 | 5fe141fd | bellard | case QMAGIC:
|
206 | 5fe141fd | bellard | case OMAGIC:
|
207 | 293f78bc | blueswir1 | if (e.a_text + e.a_data > max_sz)
|
208 | 293f78bc | blueswir1 | goto fail;
|
209 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
210 | 45a50b16 | Gerd Hoffmann | size = read_targphys(filename, fd, addr, e.a_text + e.a_data); |
211 | 5fe141fd | bellard | if (size < 0) |
212 | 5fe141fd | bellard | goto fail;
|
213 | 5fe141fd | bellard | break;
|
214 | 5fe141fd | bellard | case NMAGIC:
|
215 | ca20cf32 | Blue Swirl | if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
|
216 | 293f78bc | blueswir1 | goto fail;
|
217 | 5fe141fd | bellard | lseek(fd, N_TXTOFF(e), SEEK_SET); |
218 | 45a50b16 | Gerd Hoffmann | size = read_targphys(filename, fd, addr, e.a_text); |
219 | 5fe141fd | bellard | if (size < 0) |
220 | 5fe141fd | bellard | goto fail;
|
221 | 45a50b16 | Gerd Hoffmann | ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size), |
222 | ca20cf32 | Blue Swirl | e.a_data); |
223 | 5fe141fd | bellard | if (ret < 0) |
224 | 5fe141fd | bellard | goto fail;
|
225 | 5fe141fd | bellard | size += ret; |
226 | 5fe141fd | bellard | break;
|
227 | 5fe141fd | bellard | default:
|
228 | 5fe141fd | bellard | goto fail;
|
229 | 5fe141fd | bellard | } |
230 | 5fe141fd | bellard | close(fd); |
231 | 5fe141fd | bellard | return size;
|
232 | 5fe141fd | bellard | fail:
|
233 | 5fe141fd | bellard | close(fd); |
234 | 5fe141fd | bellard | return -1; |
235 | 5fe141fd | bellard | } |
236 | 5fe141fd | bellard | |
237 | 5fe141fd | bellard | /* ELF loader */
|
238 | 5fe141fd | bellard | |
239 | 5fe141fd | bellard | static void *load_at(int fd, int offset, int size) |
240 | 5fe141fd | bellard | { |
241 | 5fe141fd | bellard | void *ptr;
|
242 | 5fe141fd | bellard | if (lseek(fd, offset, SEEK_SET) < 0) |
243 | 5fe141fd | bellard | return NULL; |
244 | 7267c094 | Anthony Liguori | ptr = g_malloc(size); |
245 | 5fe141fd | bellard | if (read(fd, ptr, size) != size) {
|
246 | 7267c094 | Anthony Liguori | g_free(ptr); |
247 | 5fe141fd | bellard | return NULL; |
248 | 5fe141fd | bellard | } |
249 | 5fe141fd | bellard | return ptr;
|
250 | 5fe141fd | bellard | } |
251 | 5fe141fd | bellard | |
252 | 3efa9a67 | malc | #ifdef ELF_CLASS
|
253 | 3efa9a67 | malc | #undef ELF_CLASS
|
254 | 3efa9a67 | malc | #endif
|
255 | 5fe141fd | bellard | |
256 | 5fe141fd | bellard | #define ELF_CLASS ELFCLASS32
|
257 | 5fe141fd | bellard | #include "elf.h" |
258 | 5fe141fd | bellard | |
259 | 5fe141fd | bellard | #define SZ 32 |
260 | 5fe141fd | bellard | #define elf_word uint32_t
|
261 | 82790064 | ths | #define elf_sword int32_t
|
262 | 5fe141fd | bellard | #define bswapSZs bswap32s
|
263 | 83c9f4ca | Paolo Bonzini | #include "hw/elf_ops.h" |
264 | 5fe141fd | bellard | |
265 | 5fe141fd | bellard | #undef elfhdr
|
266 | 5fe141fd | bellard | #undef elf_phdr
|
267 | 5fe141fd | bellard | #undef elf_shdr
|
268 | 5fe141fd | bellard | #undef elf_sym
|
269 | 5fe141fd | bellard | #undef elf_note
|
270 | 5fe141fd | bellard | #undef elf_word
|
271 | 82790064 | ths | #undef elf_sword
|
272 | 5fe141fd | bellard | #undef bswapSZs
|
273 | 5fe141fd | bellard | #undef SZ
|
274 | 5fe141fd | bellard | #define elfhdr elf64_hdr
|
275 | 5fe141fd | bellard | #define elf_phdr elf64_phdr
|
276 | 5fe141fd | bellard | #define elf_note elf64_note
|
277 | 5fe141fd | bellard | #define elf_shdr elf64_shdr
|
278 | 5fe141fd | bellard | #define elf_sym elf64_sym
|
279 | 5fe141fd | bellard | #define elf_word uint64_t
|
280 | 82790064 | ths | #define elf_sword int64_t
|
281 | 5fe141fd | bellard | #define bswapSZs bswap64s
|
282 | 5fe141fd | bellard | #define SZ 64 |
283 | 83c9f4ca | Paolo Bonzini | #include "hw/elf_ops.h" |
284 | 5fe141fd | bellard | |
285 | 5fe141fd | bellard | /* return < 0 if error, otherwise the number of bytes loaded in memory */
|
286 | 409dbce5 | Aurelien Jarno | int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t), |
287 | 409dbce5 | Aurelien Jarno | void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
|
288 | 409dbce5 | Aurelien Jarno | uint64_t *highaddr, int big_endian, int elf_machine, int clear_lsb) |
289 | 5fe141fd | bellard | { |
290 | ca20cf32 | Blue Swirl | int fd, data_order, target_data_order, must_swab, ret;
|
291 | 5fe141fd | bellard | uint8_t e_ident[EI_NIDENT]; |
292 | 5fe141fd | bellard | |
293 | 699e4642 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
294 | 5fe141fd | bellard | if (fd < 0) { |
295 | 5fe141fd | bellard | perror(filename); |
296 | 5fe141fd | bellard | return -1; |
297 | 5fe141fd | bellard | } |
298 | 5fe141fd | bellard | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
299 | 5fe141fd | bellard | goto fail;
|
300 | 5fe141fd | bellard | if (e_ident[0] != ELFMAG0 || |
301 | 5fe141fd | bellard | e_ident[1] != ELFMAG1 ||
|
302 | 5fe141fd | bellard | e_ident[2] != ELFMAG2 ||
|
303 | 5fe141fd | bellard | e_ident[3] != ELFMAG3)
|
304 | 5fe141fd | bellard | goto fail;
|
305 | e2542fe2 | Juan Quintela | #ifdef HOST_WORDS_BIGENDIAN
|
306 | 5fe141fd | bellard | data_order = ELFDATA2MSB; |
307 | 5fe141fd | bellard | #else
|
308 | 5fe141fd | bellard | data_order = ELFDATA2LSB; |
309 | 5fe141fd | bellard | #endif
|
310 | 5fe141fd | bellard | must_swab = data_order != e_ident[EI_DATA]; |
311 | ca20cf32 | Blue Swirl | if (big_endian) {
|
312 | ca20cf32 | Blue Swirl | target_data_order = ELFDATA2MSB; |
313 | ca20cf32 | Blue Swirl | } else {
|
314 | ca20cf32 | Blue Swirl | target_data_order = ELFDATA2LSB; |
315 | ca20cf32 | Blue Swirl | } |
316 | 9042c0e2 | ths | |
317 | cedf9a6f | Blue Swirl | if (target_data_order != e_ident[EI_DATA]) {
|
318 | cedf9a6f | Blue Swirl | goto fail;
|
319 | cedf9a6f | Blue Swirl | } |
320 | 9042c0e2 | ths | |
321 | 5fe141fd | bellard | lseek(fd, 0, SEEK_SET);
|
322 | 5fe141fd | bellard | if (e_ident[EI_CLASS] == ELFCLASS64) {
|
323 | 409dbce5 | Aurelien Jarno | ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab, |
324 | 409dbce5 | Aurelien Jarno | pentry, lowaddr, highaddr, elf_machine, clear_lsb); |
325 | 5fe141fd | bellard | } else {
|
326 | 409dbce5 | Aurelien Jarno | ret = load_elf32(filename, fd, translate_fn, translate_opaque, must_swab, |
327 | 409dbce5 | Aurelien Jarno | pentry, lowaddr, highaddr, elf_machine, clear_lsb); |
328 | 5fe141fd | bellard | } |
329 | 5fe141fd | bellard | |
330 | 5fe141fd | bellard | close(fd); |
331 | 5fe141fd | bellard | return ret;
|
332 | 5fe141fd | bellard | |
333 | 5fe141fd | bellard | fail:
|
334 | 5fe141fd | bellard | close(fd); |
335 | 5fe141fd | bellard | return -1; |
336 | 5fe141fd | bellard | } |
337 | 1c7b3754 | pbrook | |
338 | c227f099 | Anthony Liguori | static void bswap_uboot_header(uboot_image_header_t *hdr) |
339 | 1c7b3754 | pbrook | { |
340 | e2542fe2 | Juan Quintela | #ifndef HOST_WORDS_BIGENDIAN
|
341 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_magic); |
342 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_hcrc); |
343 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_time); |
344 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_size); |
345 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_load); |
346 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_ep); |
347 | 1c7b3754 | pbrook | bswap32s(&hdr->ih_dcrc); |
348 | 1c7b3754 | pbrook | #endif
|
349 | 1c7b3754 | pbrook | } |
350 | 1c7b3754 | pbrook | |
351 | 5a123577 | aliguori | |
352 | 5a123577 | aliguori | #define ZALLOC_ALIGNMENT 16 |
353 | 5a123577 | aliguori | |
354 | 5a123577 | aliguori | static void *zalloc(void *x, unsigned items, unsigned size) |
355 | 5a123577 | aliguori | { |
356 | 5a123577 | aliguori | void *p;
|
357 | 5a123577 | aliguori | |
358 | 5a123577 | aliguori | size *= items; |
359 | 5a123577 | aliguori | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); |
360 | 5a123577 | aliguori | |
361 | 7267c094 | Anthony Liguori | p = g_malloc(size); |
362 | 5a123577 | aliguori | |
363 | 5a123577 | aliguori | return (p);
|
364 | 5a123577 | aliguori | } |
365 | 5a123577 | aliguori | |
366 | d084eab6 | Stefan Weil | static void zfree(void *x, void *addr) |
367 | 5a123577 | aliguori | { |
368 | 7267c094 | Anthony Liguori | g_free(addr); |
369 | 5a123577 | aliguori | } |
370 | 5a123577 | aliguori | |
371 | 5a123577 | aliguori | |
372 | 5a123577 | aliguori | #define HEAD_CRC 2 |
373 | 5a123577 | aliguori | #define EXTRA_FIELD 4 |
374 | 5a123577 | aliguori | #define ORIG_NAME 8 |
375 | 5a123577 | aliguori | #define COMMENT 0x10 |
376 | 5a123577 | aliguori | #define RESERVED 0xe0 |
377 | 5a123577 | aliguori | |
378 | 5a123577 | aliguori | #define DEFLATED 8 |
379 | 5a123577 | aliguori | |
380 | 5025d542 | Alexander Graf | /* This is the usual maximum in uboot, so if a uImage overflows this, it would
|
381 | 5a123577 | aliguori | * overflow on real hardware too. */
|
382 | 5025d542 | Alexander Graf | #define UBOOT_MAX_GUNZIP_BYTES (64 << 20) |
383 | 5a123577 | aliguori | |
384 | 5a123577 | aliguori | static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, |
385 | 5a123577 | aliguori | size_t srclen) |
386 | 5a123577 | aliguori | { |
387 | 5a123577 | aliguori | z_stream s; |
388 | 5a123577 | aliguori | ssize_t dstbytes; |
389 | 5a123577 | aliguori | int r, i, flags;
|
390 | 5a123577 | aliguori | |
391 | 5a123577 | aliguori | /* skip header */
|
392 | 5a123577 | aliguori | i = 10;
|
393 | 5a123577 | aliguori | flags = src[3];
|
394 | 5a123577 | aliguori | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { |
395 | 5a123577 | aliguori | puts ("Error: Bad gzipped data\n");
|
396 | 5a123577 | aliguori | return -1; |
397 | 5a123577 | aliguori | } |
398 | 5a123577 | aliguori | if ((flags & EXTRA_FIELD) != 0) |
399 | 5a123577 | aliguori | i = 12 + src[10] + (src[11] << 8); |
400 | 5a123577 | aliguori | if ((flags & ORIG_NAME) != 0) |
401 | 5a123577 | aliguori | while (src[i++] != 0) |
402 | 5a123577 | aliguori | ; |
403 | 5a123577 | aliguori | if ((flags & COMMENT) != 0) |
404 | 5a123577 | aliguori | while (src[i++] != 0) |
405 | 5a123577 | aliguori | ; |
406 | 5a123577 | aliguori | if ((flags & HEAD_CRC) != 0) |
407 | 5a123577 | aliguori | i += 2;
|
408 | 5a123577 | aliguori | if (i >= srclen) {
|
409 | 5a123577 | aliguori | puts ("Error: gunzip out of data in header\n");
|
410 | 5a123577 | aliguori | return -1; |
411 | 5a123577 | aliguori | } |
412 | 5a123577 | aliguori | |
413 | 5a123577 | aliguori | s.zalloc = zalloc; |
414 | d084eab6 | Stefan Weil | s.zfree = zfree; |
415 | 5a123577 | aliguori | |
416 | 5a123577 | aliguori | r = inflateInit2(&s, -MAX_WBITS); |
417 | 5a123577 | aliguori | if (r != Z_OK) {
|
418 | 5a123577 | aliguori | printf ("Error: inflateInit2() returned %d\n", r);
|
419 | 5a123577 | aliguori | return (-1); |
420 | 5a123577 | aliguori | } |
421 | 5a123577 | aliguori | s.next_in = src + i; |
422 | 5a123577 | aliguori | s.avail_in = srclen - i; |
423 | 5a123577 | aliguori | s.next_out = dst; |
424 | 5a123577 | aliguori | s.avail_out = dstlen; |
425 | 5a123577 | aliguori | r = inflate(&s, Z_FINISH); |
426 | 5a123577 | aliguori | if (r != Z_OK && r != Z_STREAM_END) {
|
427 | 5a123577 | aliguori | printf ("Error: inflate() returned %d\n", r);
|
428 | 5a123577 | aliguori | return -1; |
429 | 5a123577 | aliguori | } |
430 | 5a123577 | aliguori | dstbytes = s.next_out - (unsigned char *) dst; |
431 | 5a123577 | aliguori | inflateEnd(&s); |
432 | 5a123577 | aliguori | |
433 | 5a123577 | aliguori | return dstbytes;
|
434 | 5a123577 | aliguori | } |
435 | 5a123577 | aliguori | |
436 | 1c7b3754 | pbrook | /* Load a U-Boot image. */
|
437 | a8170e5e | Avi Kivity | int load_uimage(const char *filename, hwaddr *ep, |
438 | a8170e5e | Avi Kivity | hwaddr *loadaddr, int *is_linux)
|
439 | 1c7b3754 | pbrook | { |
440 | 1c7b3754 | pbrook | int fd;
|
441 | 1c7b3754 | pbrook | int size;
|
442 | c227f099 | Anthony Liguori | uboot_image_header_t h; |
443 | c227f099 | Anthony Liguori | uboot_image_header_t *hdr = &h; |
444 | 1c7b3754 | pbrook | uint8_t *data = NULL;
|
445 | 265ca29a | aliguori | int ret = -1; |
446 | 1c7b3754 | pbrook | |
447 | 1c7b3754 | pbrook | fd = open(filename, O_RDONLY | O_BINARY); |
448 | 1c7b3754 | pbrook | if (fd < 0) |
449 | 1c7b3754 | pbrook | return -1; |
450 | 1c7b3754 | pbrook | |
451 | c227f099 | Anthony Liguori | size = read(fd, hdr, sizeof(uboot_image_header_t));
|
452 | 1c7b3754 | pbrook | if (size < 0) |
453 | 265ca29a | aliguori | goto out;
|
454 | 1c7b3754 | pbrook | |
455 | 1c7b3754 | pbrook | bswap_uboot_header(hdr); |
456 | 1c7b3754 | pbrook | |
457 | 1c7b3754 | pbrook | if (hdr->ih_magic != IH_MAGIC)
|
458 | 265ca29a | aliguori | goto out;
|
459 | 1c7b3754 | pbrook | |
460 | 7e5f90fa | aliguori | /* TODO: Implement other image types. */
|
461 | 7e5f90fa | aliguori | if (hdr->ih_type != IH_TYPE_KERNEL) {
|
462 | 7e5f90fa | aliguori | fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
|
463 | 265ca29a | aliguori | goto out;
|
464 | 1c7b3754 | pbrook | } |
465 | 1c7b3754 | pbrook | |
466 | 5a123577 | aliguori | switch (hdr->ih_comp) {
|
467 | 5a123577 | aliguori | case IH_COMP_NONE:
|
468 | 5a123577 | aliguori | case IH_COMP_GZIP:
|
469 | 5a123577 | aliguori | break;
|
470 | 5a123577 | aliguori | default:
|
471 | 5a123577 | aliguori | fprintf(stderr, |
472 | 5a123577 | aliguori | "Unable to load u-boot images with compression type %d\n",
|
473 | 5a123577 | aliguori | hdr->ih_comp); |
474 | 265ca29a | aliguori | goto out;
|
475 | 1c7b3754 | pbrook | } |
476 | 1c7b3754 | pbrook | |
477 | 1c7b3754 | pbrook | /* TODO: Check CPU type. */
|
478 | 1c7b3754 | pbrook | if (is_linux) {
|
479 | 7e5f90fa | aliguori | if (hdr->ih_os == IH_OS_LINUX)
|
480 | 1c7b3754 | pbrook | *is_linux = 1;
|
481 | 1c7b3754 | pbrook | else
|
482 | 1c7b3754 | pbrook | *is_linux = 0;
|
483 | 1c7b3754 | pbrook | } |
484 | 1c7b3754 | pbrook | |
485 | 1c7b3754 | pbrook | *ep = hdr->ih_ep; |
486 | 7267c094 | Anthony Liguori | data = g_malloc(hdr->ih_size); |
487 | 1c7b3754 | pbrook | |
488 | 1c7b3754 | pbrook | if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
|
489 | 1c7b3754 | pbrook | fprintf(stderr, "Error reading file\n");
|
490 | 265ca29a | aliguori | goto out;
|
491 | 1c7b3754 | pbrook | } |
492 | 1c7b3754 | pbrook | |
493 | 5a123577 | aliguori | if (hdr->ih_comp == IH_COMP_GZIP) {
|
494 | 5a123577 | aliguori | uint8_t *compressed_data; |
495 | 5a123577 | aliguori | size_t max_bytes; |
496 | 5a123577 | aliguori | ssize_t bytes; |
497 | 5a123577 | aliguori | |
498 | 5a123577 | aliguori | compressed_data = data; |
499 | 5a123577 | aliguori | max_bytes = UBOOT_MAX_GUNZIP_BYTES; |
500 | 7267c094 | Anthony Liguori | data = g_malloc(max_bytes); |
501 | 5a123577 | aliguori | |
502 | 5a123577 | aliguori | bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size); |
503 | 7267c094 | Anthony Liguori | g_free(compressed_data); |
504 | 5a123577 | aliguori | if (bytes < 0) { |
505 | 5a123577 | aliguori | fprintf(stderr, "Unable to decompress gzipped image!\n");
|
506 | 5a123577 | aliguori | goto out;
|
507 | 5a123577 | aliguori | } |
508 | 5a123577 | aliguori | hdr->ih_size = bytes; |
509 | 5a123577 | aliguori | } |
510 | 5a123577 | aliguori | |
511 | 45a50b16 | Gerd Hoffmann | rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load); |
512 | 1c7b3754 | pbrook | |
513 | 21cafd08 | aliguori | if (loadaddr)
|
514 | 21cafd08 | aliguori | *loadaddr = hdr->ih_load; |
515 | 21cafd08 | aliguori | |
516 | 265ca29a | aliguori | ret = hdr->ih_size; |
517 | 1c7b3754 | pbrook | |
518 | 265ca29a | aliguori | out:
|
519 | 1c7b3754 | pbrook | if (data)
|
520 | 7267c094 | Anthony Liguori | g_free(data); |
521 | 1c7b3754 | pbrook | close(fd); |
522 | 265ca29a | aliguori | return ret;
|
523 | 1c7b3754 | pbrook | } |
524 | 45a50b16 | Gerd Hoffmann | |
525 | 45a50b16 | Gerd Hoffmann | /*
|
526 | 45a50b16 | Gerd Hoffmann | * Functions for reboot-persistent memory regions.
|
527 | 45a50b16 | Gerd Hoffmann | * - used for vga bios and option roms.
|
528 | 45a50b16 | Gerd Hoffmann | * - also linux kernel (-kernel / -initrd).
|
529 | 45a50b16 | Gerd Hoffmann | */
|
530 | 45a50b16 | Gerd Hoffmann | |
531 | 45a50b16 | Gerd Hoffmann | typedef struct Rom Rom; |
532 | 45a50b16 | Gerd Hoffmann | |
533 | 45a50b16 | Gerd Hoffmann | struct Rom {
|
534 | 45a50b16 | Gerd Hoffmann | char *name;
|
535 | 45a50b16 | Gerd Hoffmann | char *path;
|
536 | d60fa42e | Fabien Chouteau | |
537 | d60fa42e | Fabien Chouteau | /* datasize is the amount of memory allocated in "data". If datasize is less
|
538 | d60fa42e | Fabien Chouteau | * than romsize, it means that the area from datasize to romsize is filled
|
539 | d60fa42e | Fabien Chouteau | * with zeros.
|
540 | d60fa42e | Fabien Chouteau | */
|
541 | 45a50b16 | Gerd Hoffmann | size_t romsize; |
542 | d60fa42e | Fabien Chouteau | size_t datasize; |
543 | d60fa42e | Fabien Chouteau | |
544 | 45a50b16 | Gerd Hoffmann | uint8_t *data; |
545 | 45a50b16 | Gerd Hoffmann | int isrom;
|
546 | 379526a4 | Gerd Hoffmann | char *fw_dir;
|
547 | 379526a4 | Gerd Hoffmann | char *fw_file;
|
548 | 45a50b16 | Gerd Hoffmann | |
549 | a8170e5e | Avi Kivity | hwaddr addr; |
550 | 45a50b16 | Gerd Hoffmann | QTAILQ_ENTRY(Rom) next; |
551 | 45a50b16 | Gerd Hoffmann | }; |
552 | 45a50b16 | Gerd Hoffmann | |
553 | 8832cb80 | Gerd Hoffmann | static FWCfgState *fw_cfg;
|
554 | 45a50b16 | Gerd Hoffmann | static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
|
555 | 45a50b16 | Gerd Hoffmann | |
556 | 45a50b16 | Gerd Hoffmann | static void rom_insert(Rom *rom) |
557 | 45a50b16 | Gerd Hoffmann | { |
558 | 45a50b16 | Gerd Hoffmann | Rom *item; |
559 | 45a50b16 | Gerd Hoffmann | |
560 | 97fe84f5 | Paul Brook | if (roms_loaded) {
|
561 | 97fe84f5 | Paul Brook | hw_error ("ROM images must be loaded at startup\n");
|
562 | 97fe84f5 | Paul Brook | } |
563 | 97fe84f5 | Paul Brook | |
564 | 45a50b16 | Gerd Hoffmann | /* list is ordered by load address */
|
565 | 45a50b16 | Gerd Hoffmann | QTAILQ_FOREACH(item, &roms, next) { |
566 | 632cf034 | Gerd Hoffmann | if (rom->addr >= item->addr)
|
567 | 45a50b16 | Gerd Hoffmann | continue;
|
568 | 45a50b16 | Gerd Hoffmann | QTAILQ_INSERT_BEFORE(item, rom, next); |
569 | 45a50b16 | Gerd Hoffmann | return;
|
570 | 45a50b16 | Gerd Hoffmann | } |
571 | 45a50b16 | Gerd Hoffmann | QTAILQ_INSERT_TAIL(&roms, rom, next); |
572 | 45a50b16 | Gerd Hoffmann | } |
573 | 45a50b16 | Gerd Hoffmann | |
574 | bdb5ee30 | Gerd Hoffmann | int rom_add_file(const char *file, const char *fw_dir, |
575 | a8170e5e | Avi Kivity | hwaddr addr, int32_t bootindex) |
576 | 45a50b16 | Gerd Hoffmann | { |
577 | 45a50b16 | Gerd Hoffmann | Rom *rom; |
578 | 45a50b16 | Gerd Hoffmann | int rc, fd = -1; |
579 | 2e55e842 | Gleb Natapov | char devpath[100]; |
580 | 45a50b16 | Gerd Hoffmann | |
581 | 7267c094 | Anthony Liguori | rom = g_malloc0(sizeof(*rom));
|
582 | 7267c094 | Anthony Liguori | rom->name = g_strdup(file); |
583 | 45a50b16 | Gerd Hoffmann | rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name); |
584 | 45a50b16 | Gerd Hoffmann | if (rom->path == NULL) { |
585 | 7267c094 | Anthony Liguori | rom->path = g_strdup(file); |
586 | 45a50b16 | Gerd Hoffmann | } |
587 | 45a50b16 | Gerd Hoffmann | |
588 | cef290b8 | malc | fd = open(rom->path, O_RDONLY | O_BINARY); |
589 | 45a50b16 | Gerd Hoffmann | if (fd == -1) { |
590 | 45a50b16 | Gerd Hoffmann | fprintf(stderr, "Could not open option rom '%s': %s\n",
|
591 | 45a50b16 | Gerd Hoffmann | rom->path, strerror(errno)); |
592 | 45a50b16 | Gerd Hoffmann | goto err;
|
593 | 45a50b16 | Gerd Hoffmann | } |
594 | 45a50b16 | Gerd Hoffmann | |
595 | bdb5ee30 | Gerd Hoffmann | if (fw_dir) {
|
596 | 7267c094 | Anthony Liguori | rom->fw_dir = g_strdup(fw_dir); |
597 | 7267c094 | Anthony Liguori | rom->fw_file = g_strdup(file); |
598 | bdb5ee30 | Gerd Hoffmann | } |
599 | d60fa42e | Fabien Chouteau | rom->addr = addr; |
600 | d60fa42e | Fabien Chouteau | rom->romsize = lseek(fd, 0, SEEK_END);
|
601 | d60fa42e | Fabien Chouteau | rom->datasize = rom->romsize; |
602 | d60fa42e | Fabien Chouteau | rom->data = g_malloc0(rom->datasize); |
603 | 45a50b16 | Gerd Hoffmann | lseek(fd, 0, SEEK_SET);
|
604 | d60fa42e | Fabien Chouteau | rc = read(fd, rom->data, rom->datasize); |
605 | d60fa42e | Fabien Chouteau | if (rc != rom->datasize) {
|
606 | 45a50b16 | Gerd Hoffmann | fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
|
607 | d60fa42e | Fabien Chouteau | rom->name, rc, rom->datasize); |
608 | 45a50b16 | Gerd Hoffmann | goto err;
|
609 | 45a50b16 | Gerd Hoffmann | } |
610 | 45a50b16 | Gerd Hoffmann | close(fd); |
611 | 45a50b16 | Gerd Hoffmann | rom_insert(rom); |
612 | de1f34cb | Gleb Natapov | if (rom->fw_file && fw_cfg) {
|
613 | de1f34cb | Gleb Natapov | const char *basename; |
614 | de1f34cb | Gleb Natapov | char fw_file_name[56]; |
615 | de1f34cb | Gleb Natapov | |
616 | de1f34cb | Gleb Natapov | basename = strrchr(rom->fw_file, '/');
|
617 | de1f34cb | Gleb Natapov | if (basename) {
|
618 | de1f34cb | Gleb Natapov | basename++; |
619 | de1f34cb | Gleb Natapov | } else {
|
620 | de1f34cb | Gleb Natapov | basename = rom->fw_file; |
621 | de1f34cb | Gleb Natapov | } |
622 | de1f34cb | Gleb Natapov | snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir, |
623 | de1f34cb | Gleb Natapov | basename); |
624 | de1f34cb | Gleb Natapov | fw_cfg_add_file(fw_cfg, fw_file_name, rom->data, rom->romsize); |
625 | 2e55e842 | Gleb Natapov | snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name); |
626 | 2e55e842 | Gleb Natapov | } else {
|
627 | 2e55e842 | Gleb Natapov | snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr); |
628 | de1f34cb | Gleb Natapov | } |
629 | 2e55e842 | Gleb Natapov | |
630 | 2e55e842 | Gleb Natapov | add_boot_device_path(bootindex, NULL, devpath);
|
631 | 45a50b16 | Gerd Hoffmann | return 0; |
632 | 45a50b16 | Gerd Hoffmann | |
633 | 45a50b16 | Gerd Hoffmann | err:
|
634 | 45a50b16 | Gerd Hoffmann | if (fd != -1) |
635 | 45a50b16 | Gerd Hoffmann | close(fd); |
636 | 7267c094 | Anthony Liguori | g_free(rom->data); |
637 | 7267c094 | Anthony Liguori | g_free(rom->path); |
638 | 7267c094 | Anthony Liguori | g_free(rom->name); |
639 | 7267c094 | Anthony Liguori | g_free(rom); |
640 | 45a50b16 | Gerd Hoffmann | return -1; |
641 | 45a50b16 | Gerd Hoffmann | } |
642 | 45a50b16 | Gerd Hoffmann | |
643 | 45a50b16 | Gerd Hoffmann | int rom_add_blob(const char *name, const void *blob, size_t len, |
644 | a8170e5e | Avi Kivity | hwaddr addr) |
645 | 45a50b16 | Gerd Hoffmann | { |
646 | 45a50b16 | Gerd Hoffmann | Rom *rom; |
647 | 45a50b16 | Gerd Hoffmann | |
648 | d60fa42e | Fabien Chouteau | rom = g_malloc0(sizeof(*rom));
|
649 | d60fa42e | Fabien Chouteau | rom->name = g_strdup(name); |
650 | d60fa42e | Fabien Chouteau | rom->addr = addr; |
651 | d60fa42e | Fabien Chouteau | rom->romsize = len; |
652 | d60fa42e | Fabien Chouteau | rom->datasize = len; |
653 | d60fa42e | Fabien Chouteau | rom->data = g_malloc0(rom->datasize); |
654 | 45a50b16 | Gerd Hoffmann | memcpy(rom->data, blob, len); |
655 | 45a50b16 | Gerd Hoffmann | rom_insert(rom); |
656 | 45a50b16 | Gerd Hoffmann | return 0; |
657 | 45a50b16 | Gerd Hoffmann | } |
658 | 45a50b16 | Gerd Hoffmann | |
659 | d60fa42e | Fabien Chouteau | /* This function is specific for elf program because we don't need to allocate
|
660 | d60fa42e | Fabien Chouteau | * all the rom. We just allocate the first part and the rest is just zeros. This
|
661 | d60fa42e | Fabien Chouteau | * is why romsize and datasize are different. Also, this function seize the
|
662 | d60fa42e | Fabien Chouteau | * memory ownership of "data", so we don't have to allocate and copy the buffer.
|
663 | d60fa42e | Fabien Chouteau | */
|
664 | d60fa42e | Fabien Chouteau | int rom_add_elf_program(const char *name, void *data, size_t datasize, |
665 | d60fa42e | Fabien Chouteau | size_t romsize, hwaddr addr) |
666 | d60fa42e | Fabien Chouteau | { |
667 | d60fa42e | Fabien Chouteau | Rom *rom; |
668 | d60fa42e | Fabien Chouteau | |
669 | d60fa42e | Fabien Chouteau | rom = g_malloc0(sizeof(*rom));
|
670 | d60fa42e | Fabien Chouteau | rom->name = g_strdup(name); |
671 | d60fa42e | Fabien Chouteau | rom->addr = addr; |
672 | d60fa42e | Fabien Chouteau | rom->datasize = datasize; |
673 | d60fa42e | Fabien Chouteau | rom->romsize = romsize; |
674 | d60fa42e | Fabien Chouteau | rom->data = data; |
675 | d60fa42e | Fabien Chouteau | rom_insert(rom); |
676 | d60fa42e | Fabien Chouteau | return 0; |
677 | d60fa42e | Fabien Chouteau | } |
678 | d60fa42e | Fabien Chouteau | |
679 | de2aff17 | Gerd Hoffmann | int rom_add_vga(const char *file) |
680 | de2aff17 | Gerd Hoffmann | { |
681 | 2e55e842 | Gleb Natapov | return rom_add_file(file, "vgaroms", 0, -1); |
682 | de2aff17 | Gerd Hoffmann | } |
683 | de2aff17 | Gerd Hoffmann | |
684 | 2e55e842 | Gleb Natapov | int rom_add_option(const char *file, int32_t bootindex) |
685 | de2aff17 | Gerd Hoffmann | { |
686 | 2e55e842 | Gleb Natapov | return rom_add_file(file, "genroms", 0, bootindex); |
687 | de2aff17 | Gerd Hoffmann | } |
688 | de2aff17 | Gerd Hoffmann | |
689 | 45a50b16 | Gerd Hoffmann | static void rom_reset(void *unused) |
690 | 45a50b16 | Gerd Hoffmann | { |
691 | 45a50b16 | Gerd Hoffmann | Rom *rom; |
692 | 45a50b16 | Gerd Hoffmann | |
693 | 45a50b16 | Gerd Hoffmann | QTAILQ_FOREACH(rom, &roms, next) { |
694 | e405a2ba | Avi Kivity | if (rom->fw_file) {
|
695 | e405a2ba | Avi Kivity | continue;
|
696 | e405a2ba | Avi Kivity | } |
697 | bdb5ee30 | Gerd Hoffmann | if (rom->data == NULL) { |
698 | 45a50b16 | Gerd Hoffmann | continue;
|
699 | bdb5ee30 | Gerd Hoffmann | } |
700 | d60fa42e | Fabien Chouteau | cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize); |
701 | 45a50b16 | Gerd Hoffmann | if (rom->isrom) {
|
702 | 45a50b16 | Gerd Hoffmann | /* rom needs to be written only once */
|
703 | 7267c094 | Anthony Liguori | g_free(rom->data); |
704 | 45a50b16 | Gerd Hoffmann | rom->data = NULL;
|
705 | 45a50b16 | Gerd Hoffmann | } |
706 | 45a50b16 | Gerd Hoffmann | } |
707 | 45a50b16 | Gerd Hoffmann | } |
708 | 45a50b16 | Gerd Hoffmann | |
709 | 45a50b16 | Gerd Hoffmann | int rom_load_all(void) |
710 | 45a50b16 | Gerd Hoffmann | { |
711 | a8170e5e | Avi Kivity | hwaddr addr = 0;
|
712 | dcc5cd33 | Avi Kivity | MemoryRegionSection section; |
713 | 45a50b16 | Gerd Hoffmann | Rom *rom; |
714 | 45a50b16 | Gerd Hoffmann | |
715 | 45a50b16 | Gerd Hoffmann | QTAILQ_FOREACH(rom, &roms, next) { |
716 | e405a2ba | Avi Kivity | if (rom->fw_file) {
|
717 | e405a2ba | Avi Kivity | continue;
|
718 | e405a2ba | Avi Kivity | } |
719 | 632cf034 | Gerd Hoffmann | if (addr > rom->addr) {
|
720 | 632cf034 | Gerd Hoffmann | fprintf(stderr, "rom: requested regions overlap "
|
721 | 632cf034 | Gerd Hoffmann | "(rom %s. free=0x" TARGET_FMT_plx
|
722 | 632cf034 | Gerd Hoffmann | ", addr=0x" TARGET_FMT_plx ")\n", |
723 | 632cf034 | Gerd Hoffmann | rom->name, addr, rom->addr); |
724 | 632cf034 | Gerd Hoffmann | return -1; |
725 | 45a50b16 | Gerd Hoffmann | } |
726 | 632cf034 | Gerd Hoffmann | addr = rom->addr; |
727 | 45a50b16 | Gerd Hoffmann | addr += rom->romsize; |
728 | dcc5cd33 | Avi Kivity | section = memory_region_find(get_system_memory(), rom->addr, 1);
|
729 | 052e87b0 | Paolo Bonzini | rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr); |
730 | 45a50b16 | Gerd Hoffmann | } |
731 | 45a50b16 | Gerd Hoffmann | qemu_register_reset(rom_reset, NULL);
|
732 | 97fe84f5 | Paul Brook | roms_loaded = 1;
|
733 | 45a50b16 | Gerd Hoffmann | return 0; |
734 | 45a50b16 | Gerd Hoffmann | } |
735 | 45a50b16 | Gerd Hoffmann | |
736 | a88b362c | Laszlo Ersek | void rom_set_fw(FWCfgState *f)
|
737 | 379526a4 | Gerd Hoffmann | { |
738 | 8832cb80 | Gerd Hoffmann | fw_cfg = f; |
739 | 379526a4 | Gerd Hoffmann | } |
740 | 379526a4 | Gerd Hoffmann | |
741 | a8170e5e | Avi Kivity | static Rom *find_rom(hwaddr addr)
|
742 | 3c178e72 | Gerd Hoffmann | { |
743 | 3c178e72 | Gerd Hoffmann | Rom *rom; |
744 | 3c178e72 | Gerd Hoffmann | |
745 | 3c178e72 | Gerd Hoffmann | QTAILQ_FOREACH(rom, &roms, next) { |
746 | f21a59c2 | Aurelien Jarno | if (rom->fw_file) {
|
747 | f21a59c2 | Aurelien Jarno | continue;
|
748 | f21a59c2 | Aurelien Jarno | } |
749 | bdb5ee30 | Gerd Hoffmann | if (rom->addr > addr) {
|
750 | 3c178e72 | Gerd Hoffmann | continue;
|
751 | bdb5ee30 | Gerd Hoffmann | } |
752 | bdb5ee30 | Gerd Hoffmann | if (rom->addr + rom->romsize < addr) {
|
753 | 3c178e72 | Gerd Hoffmann | continue;
|
754 | bdb5ee30 | Gerd Hoffmann | } |
755 | 3c178e72 | Gerd Hoffmann | return rom;
|
756 | 3c178e72 | Gerd Hoffmann | } |
757 | 3c178e72 | Gerd Hoffmann | return NULL; |
758 | 3c178e72 | Gerd Hoffmann | } |
759 | 3c178e72 | Gerd Hoffmann | |
760 | 935effc2 | Kevin Wolf | /*
|
761 | 935effc2 | Kevin Wolf | * Copies memory from registered ROMs to dest. Any memory that is contained in
|
762 | 935effc2 | Kevin Wolf | * a ROM between addr and addr + size is copied. Note that this can involve
|
763 | 935effc2 | Kevin Wolf | * multiple ROMs, which need not start at addr and need not end at addr + size.
|
764 | 935effc2 | Kevin Wolf | */
|
765 | a8170e5e | Avi Kivity | int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
|
766 | 235f86ef | Alexander Graf | { |
767 | a8170e5e | Avi Kivity | hwaddr end = addr + size; |
768 | 235f86ef | Alexander Graf | uint8_t *s, *d = dest; |
769 | 235f86ef | Alexander Graf | size_t l = 0;
|
770 | 235f86ef | Alexander Graf | Rom *rom; |
771 | 235f86ef | Alexander Graf | |
772 | 235f86ef | Alexander Graf | QTAILQ_FOREACH(rom, &roms, next) { |
773 | f21a59c2 | Aurelien Jarno | if (rom->fw_file) {
|
774 | f21a59c2 | Aurelien Jarno | continue;
|
775 | f21a59c2 | Aurelien Jarno | } |
776 | bdb5ee30 | Gerd Hoffmann | if (rom->addr + rom->romsize < addr) {
|
777 | 632cf034 | Gerd Hoffmann | continue;
|
778 | bdb5ee30 | Gerd Hoffmann | } |
779 | bdb5ee30 | Gerd Hoffmann | if (rom->addr > end) {
|
780 | 235f86ef | Alexander Graf | break;
|
781 | bdb5ee30 | Gerd Hoffmann | } |
782 | bdb5ee30 | Gerd Hoffmann | if (!rom->data) {
|
783 | 235f86ef | Alexander Graf | continue;
|
784 | bdb5ee30 | Gerd Hoffmann | } |
785 | 235f86ef | Alexander Graf | |
786 | 632cf034 | Gerd Hoffmann | d = dest + (rom->addr - addr); |
787 | 235f86ef | Alexander Graf | s = rom->data; |
788 | d60fa42e | Fabien Chouteau | l = rom->datasize; |
789 | 235f86ef | Alexander Graf | |
790 | 235f86ef | Alexander Graf | if ((d + l) > (dest + size)) {
|
791 | 235f86ef | Alexander Graf | l = dest - d; |
792 | 235f86ef | Alexander Graf | } |
793 | 235f86ef | Alexander Graf | |
794 | 235f86ef | Alexander Graf | memcpy(d, s, l); |
795 | d60fa42e | Fabien Chouteau | |
796 | d60fa42e | Fabien Chouteau | if (rom->romsize > rom->datasize) {
|
797 | d60fa42e | Fabien Chouteau | /* If datasize is less than romsize, it means that we didn't
|
798 | d60fa42e | Fabien Chouteau | * allocate all the ROM because the trailing data are only zeros.
|
799 | d60fa42e | Fabien Chouteau | */
|
800 | d60fa42e | Fabien Chouteau | |
801 | d60fa42e | Fabien Chouteau | d += l; |
802 | d60fa42e | Fabien Chouteau | l = rom->romsize - rom->datasize; |
803 | d60fa42e | Fabien Chouteau | |
804 | d60fa42e | Fabien Chouteau | if ((d + l) > (dest + size)) {
|
805 | d60fa42e | Fabien Chouteau | /* Rom size doesn't fit in the destination area. Adjust to avoid
|
806 | d60fa42e | Fabien Chouteau | * overflow.
|
807 | d60fa42e | Fabien Chouteau | */
|
808 | d60fa42e | Fabien Chouteau | l = dest - d; |
809 | d60fa42e | Fabien Chouteau | } |
810 | d60fa42e | Fabien Chouteau | |
811 | d60fa42e | Fabien Chouteau | if (l > 0) { |
812 | d60fa42e | Fabien Chouteau | memset(d, 0x0, l);
|
813 | d60fa42e | Fabien Chouteau | } |
814 | d60fa42e | Fabien Chouteau | } |
815 | 235f86ef | Alexander Graf | } |
816 | 235f86ef | Alexander Graf | |
817 | 235f86ef | Alexander Graf | return (d + l) - dest;
|
818 | 235f86ef | Alexander Graf | } |
819 | 235f86ef | Alexander Graf | |
820 | a8170e5e | Avi Kivity | void *rom_ptr(hwaddr addr)
|
821 | 3c178e72 | Gerd Hoffmann | { |
822 | 3c178e72 | Gerd Hoffmann | Rom *rom; |
823 | 3c178e72 | Gerd Hoffmann | |
824 | 3c178e72 | Gerd Hoffmann | rom = find_rom(addr); |
825 | 3c178e72 | Gerd Hoffmann | if (!rom || !rom->data)
|
826 | 3c178e72 | Gerd Hoffmann | return NULL; |
827 | 632cf034 | Gerd Hoffmann | return rom->data + (addr - rom->addr);
|
828 | 3c178e72 | Gerd Hoffmann | } |
829 | 3c178e72 | Gerd Hoffmann | |
830 | 84f2d0ea | Wenchao Xia | void do_info_roms(Monitor *mon, const QDict *qdict) |
831 | 45a50b16 | Gerd Hoffmann | { |
832 | 45a50b16 | Gerd Hoffmann | Rom *rom; |
833 | 45a50b16 | Gerd Hoffmann | |
834 | 45a50b16 | Gerd Hoffmann | QTAILQ_FOREACH(rom, &roms, next) { |
835 | e405a2ba | Avi Kivity | if (!rom->fw_file) {
|
836 | 632cf034 | Gerd Hoffmann | monitor_printf(mon, "addr=" TARGET_FMT_plx
|
837 | b2bedb21 | Stefan Weil | " size=0x%06zx mem=%s name=\"%s\"\n",
|
838 | 632cf034 | Gerd Hoffmann | rom->addr, rom->romsize, |
839 | 632cf034 | Gerd Hoffmann | rom->isrom ? "rom" : "ram", |
840 | 632cf034 | Gerd Hoffmann | rom->name); |
841 | 632cf034 | Gerd Hoffmann | } else {
|
842 | bdb5ee30 | Gerd Hoffmann | monitor_printf(mon, "fw=%s/%s"
|
843 | b2bedb21 | Stefan Weil | " size=0x%06zx name=\"%s\"\n",
|
844 | bdb5ee30 | Gerd Hoffmann | rom->fw_dir, |
845 | 632cf034 | Gerd Hoffmann | rom->fw_file, |
846 | 632cf034 | Gerd Hoffmann | rom->romsize, |
847 | 632cf034 | Gerd Hoffmann | rom->name); |
848 | 632cf034 | Gerd Hoffmann | } |
849 | 45a50b16 | Gerd Hoffmann | } |
850 | 45a50b16 | Gerd Hoffmann | } |