]> git.proxmox.com Git - mirror_qemu.git/blob - hw/core/loader.c
Include migration/vmstate.h less
[mirror_qemu.git] / hw / core / loader.c
1 /*
2 * QEMU Executable loader
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * Gunzip functionality in this file is derived from u-boot:
25 *
26 * (C) Copyright 2008 Semihalf
27 *
28 * (C) Copyright 2000-2005
29 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License as
33 * published by the Free Software Foundation; either version 2 of
34 * the License, or (at your option) any later version.
35 *
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
40 *
41 * You should have received a copy of the GNU General Public License along
42 * with this program; if not, see <http://www.gnu.org/licenses/>.
43 */
44
45 #include "qemu/osdep.h"
46 #include "qemu-common.h"
47 #include "qapi/error.h"
48 #include "hw/hw.h"
49 #include "disas/disas.h"
50 #include "migration/vmstate.h"
51 #include "monitor/monitor.h"
52 #include "sysemu/reset.h"
53 #include "sysemu/sysemu.h"
54 #include "uboot_image.h"
55 #include "hw/loader.h"
56 #include "hw/nvram/fw_cfg.h"
57 #include "exec/memory.h"
58 #include "exec/address-spaces.h"
59 #include "hw/boards.h"
60 #include "qemu/cutils.h"
61
62 #include <zlib.h>
63
64 static int roms_loaded;
65
66 /* return the size or -1 if error */
67 int64_t get_image_size(const char *filename)
68 {
69 int fd;
70 int64_t size;
71 fd = open(filename, O_RDONLY | O_BINARY);
72 if (fd < 0)
73 return -1;
74 size = lseek(fd, 0, SEEK_END);
75 close(fd);
76 return size;
77 }
78
79 /* return the size or -1 if error */
80 ssize_t load_image_size(const char *filename, void *addr, size_t size)
81 {
82 int fd;
83 ssize_t actsize, l = 0;
84
85 fd = open(filename, O_RDONLY | O_BINARY);
86 if (fd < 0) {
87 return -1;
88 }
89
90 while ((actsize = read(fd, addr + l, size - l)) > 0) {
91 l += actsize;
92 }
93
94 close(fd);
95
96 return actsize < 0 ? -1 : l;
97 }
98
99 /* read()-like version */
100 ssize_t read_targphys(const char *name,
101 int fd, hwaddr dst_addr, size_t nbytes)
102 {
103 uint8_t *buf;
104 ssize_t did;
105
106 buf = g_malloc(nbytes);
107 did = read(fd, buf, nbytes);
108 if (did > 0)
109 rom_add_blob_fixed("read", buf, did, dst_addr);
110 g_free(buf);
111 return did;
112 }
113
114 int load_image_targphys(const char *filename,
115 hwaddr addr, uint64_t max_sz)
116 {
117 return load_image_targphys_as(filename, addr, max_sz, NULL);
118 }
119
120 /* return the size or -1 if error */
121 int load_image_targphys_as(const char *filename,
122 hwaddr addr, uint64_t max_sz, AddressSpace *as)
123 {
124 int size;
125
126 size = get_image_size(filename);
127 if (size < 0 || size > max_sz) {
128 return -1;
129 }
130 if (size > 0) {
131 if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
132 return -1;
133 }
134 }
135 return size;
136 }
137
138 int load_image_mr(const char *filename, MemoryRegion *mr)
139 {
140 int size;
141
142 if (!memory_access_is_direct(mr, false)) {
143 /* Can only load an image into RAM or ROM */
144 return -1;
145 }
146
147 size = get_image_size(filename);
148
149 if (size < 0 || size > memory_region_size(mr)) {
150 return -1;
151 }
152 if (size > 0) {
153 if (rom_add_file_mr(filename, mr, -1) < 0) {
154 return -1;
155 }
156 }
157 return size;
158 }
159
160 void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
161 const char *source)
162 {
163 const char *nulp;
164 char *ptr;
165
166 if (buf_size <= 0) return;
167 nulp = memchr(source, 0, buf_size);
168 if (nulp) {
169 rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
170 } else {
171 rom_add_blob_fixed(name, source, buf_size, dest);
172 ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr));
173 *ptr = 0;
174 }
175 }
176
177 /* A.OUT loader */
178
179 struct exec
180 {
181 uint32_t a_info; /* Use macros N_MAGIC, etc for access */
182 uint32_t a_text; /* length of text, in bytes */
183 uint32_t a_data; /* length of data, in bytes */
184 uint32_t a_bss; /* length of uninitialized data area, in bytes */
185 uint32_t a_syms; /* length of symbol table data in file, in bytes */
186 uint32_t a_entry; /* start address */
187 uint32_t a_trsize; /* length of relocation info for text, in bytes */
188 uint32_t a_drsize; /* length of relocation info for data, in bytes */
189 };
190
191 static void bswap_ahdr(struct exec *e)
192 {
193 bswap32s(&e->a_info);
194 bswap32s(&e->a_text);
195 bswap32s(&e->a_data);
196 bswap32s(&e->a_bss);
197 bswap32s(&e->a_syms);
198 bswap32s(&e->a_entry);
199 bswap32s(&e->a_trsize);
200 bswap32s(&e->a_drsize);
201 }
202
203 #define N_MAGIC(exec) ((exec).a_info & 0xffff)
204 #define OMAGIC 0407
205 #define NMAGIC 0410
206 #define ZMAGIC 0413
207 #define QMAGIC 0314
208 #define _N_HDROFF(x) (1024 - sizeof (struct exec))
209 #define N_TXTOFF(x) \
210 (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
211 (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
212 #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
213 #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
214
215 #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
216
217 #define N_DATADDR(x, target_page_size) \
218 (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
219 : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
220
221
222 int load_aout(const char *filename, hwaddr addr, int max_sz,
223 int bswap_needed, hwaddr target_page_size)
224 {
225 int fd;
226 ssize_t size, ret;
227 struct exec e;
228 uint32_t magic;
229
230 fd = open(filename, O_RDONLY | O_BINARY);
231 if (fd < 0)
232 return -1;
233
234 size = read(fd, &e, sizeof(e));
235 if (size < 0)
236 goto fail;
237
238 if (bswap_needed) {
239 bswap_ahdr(&e);
240 }
241
242 magic = N_MAGIC(e);
243 switch (magic) {
244 case ZMAGIC:
245 case QMAGIC:
246 case OMAGIC:
247 if (e.a_text + e.a_data > max_sz)
248 goto fail;
249 lseek(fd, N_TXTOFF(e), SEEK_SET);
250 size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
251 if (size < 0)
252 goto fail;
253 break;
254 case NMAGIC:
255 if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
256 goto fail;
257 lseek(fd, N_TXTOFF(e), SEEK_SET);
258 size = read_targphys(filename, fd, addr, e.a_text);
259 if (size < 0)
260 goto fail;
261 ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
262 e.a_data);
263 if (ret < 0)
264 goto fail;
265 size += ret;
266 break;
267 default:
268 goto fail;
269 }
270 close(fd);
271 return size;
272 fail:
273 close(fd);
274 return -1;
275 }
276
277 /* ELF loader */
278
279 static void *load_at(int fd, off_t offset, size_t size)
280 {
281 void *ptr;
282 if (lseek(fd, offset, SEEK_SET) < 0)
283 return NULL;
284 ptr = g_malloc(size);
285 if (read(fd, ptr, size) != size) {
286 g_free(ptr);
287 return NULL;
288 }
289 return ptr;
290 }
291
292 #ifdef ELF_CLASS
293 #undef ELF_CLASS
294 #endif
295
296 #define ELF_CLASS ELFCLASS32
297 #include "elf.h"
298
299 #define SZ 32
300 #define elf_word uint32_t
301 #define elf_sword int32_t
302 #define bswapSZs bswap32s
303 #include "hw/elf_ops.h"
304
305 #undef elfhdr
306 #undef elf_phdr
307 #undef elf_shdr
308 #undef elf_sym
309 #undef elf_rela
310 #undef elf_note
311 #undef elf_word
312 #undef elf_sword
313 #undef bswapSZs
314 #undef SZ
315 #define elfhdr elf64_hdr
316 #define elf_phdr elf64_phdr
317 #define elf_note elf64_note
318 #define elf_shdr elf64_shdr
319 #define elf_sym elf64_sym
320 #define elf_rela elf64_rela
321 #define elf_word uint64_t
322 #define elf_sword int64_t
323 #define bswapSZs bswap64s
324 #define SZ 64
325 #include "hw/elf_ops.h"
326
327 const char *load_elf_strerror(int error)
328 {
329 switch (error) {
330 case 0:
331 return "No error";
332 case ELF_LOAD_FAILED:
333 return "Failed to load ELF";
334 case ELF_LOAD_NOT_ELF:
335 return "The image is not ELF";
336 case ELF_LOAD_WRONG_ARCH:
337 return "The image is from incompatible architecture";
338 case ELF_LOAD_WRONG_ENDIAN:
339 return "The image has incorrect endianness";
340 default:
341 return "Unknown error";
342 }
343 }
344
345 void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
346 {
347 int fd;
348 uint8_t e_ident_local[EI_NIDENT];
349 uint8_t *e_ident;
350 size_t hdr_size, off;
351 bool is64l;
352
353 if (!hdr) {
354 hdr = e_ident_local;
355 }
356 e_ident = hdr;
357
358 fd = open(filename, O_RDONLY | O_BINARY);
359 if (fd < 0) {
360 error_setg_errno(errp, errno, "Failed to open file: %s", filename);
361 return;
362 }
363 if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
364 error_setg_errno(errp, errno, "Failed to read file: %s", filename);
365 goto fail;
366 }
367 if (e_ident[0] != ELFMAG0 ||
368 e_ident[1] != ELFMAG1 ||
369 e_ident[2] != ELFMAG2 ||
370 e_ident[3] != ELFMAG3) {
371 error_setg(errp, "Bad ELF magic");
372 goto fail;
373 }
374
375 is64l = e_ident[EI_CLASS] == ELFCLASS64;
376 hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr);
377 if (is64) {
378 *is64 = is64l;
379 }
380
381 off = EI_NIDENT;
382 while (hdr != e_ident_local && off < hdr_size) {
383 size_t br = read(fd, hdr + off, hdr_size - off);
384 switch (br) {
385 case 0:
386 error_setg(errp, "File too short: %s", filename);
387 goto fail;
388 case -1:
389 error_setg_errno(errp, errno, "Failed to read file: %s",
390 filename);
391 goto fail;
392 }
393 off += br;
394 }
395
396 fail:
397 close(fd);
398 }
399
400 /* return < 0 if error, otherwise the number of bytes loaded in memory */
401 int load_elf(const char *filename,
402 uint64_t (*elf_note_fn)(void *, void *, bool),
403 uint64_t (*translate_fn)(void *, uint64_t),
404 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
405 uint64_t *highaddr, int big_endian, int elf_machine,
406 int clear_lsb, int data_swab)
407 {
408 return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque,
409 pentry, lowaddr, highaddr, big_endian, elf_machine,
410 clear_lsb, data_swab, NULL);
411 }
412
413 /* return < 0 if error, otherwise the number of bytes loaded in memory */
414 int load_elf_as(const char *filename,
415 uint64_t (*elf_note_fn)(void *, void *, bool),
416 uint64_t (*translate_fn)(void *, uint64_t),
417 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
418 uint64_t *highaddr, int big_endian, int elf_machine,
419 int clear_lsb, int data_swab, AddressSpace *as)
420 {
421 return load_elf_ram(filename, elf_note_fn, translate_fn, translate_opaque,
422 pentry, lowaddr, highaddr, big_endian, elf_machine,
423 clear_lsb, data_swab, as, true);
424 }
425
426 /* return < 0 if error, otherwise the number of bytes loaded in memory */
427 int load_elf_ram(const char *filename,
428 uint64_t (*elf_note_fn)(void *, void *, bool),
429 uint64_t (*translate_fn)(void *, uint64_t),
430 void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
431 uint64_t *highaddr, int big_endian, int elf_machine,
432 int clear_lsb, int data_swab, AddressSpace *as,
433 bool load_rom)
434 {
435 return load_elf_ram_sym(filename, elf_note_fn,
436 translate_fn, translate_opaque,
437 pentry, lowaddr, highaddr, big_endian,
438 elf_machine, clear_lsb, data_swab, as,
439 load_rom, NULL);
440 }
441
442 /* return < 0 if error, otherwise the number of bytes loaded in memory */
443 int load_elf_ram_sym(const char *filename,
444 uint64_t (*elf_note_fn)(void *, void *, bool),
445 uint64_t (*translate_fn)(void *, uint64_t),
446 void *translate_opaque, uint64_t *pentry,
447 uint64_t *lowaddr, uint64_t *highaddr, int big_endian,
448 int elf_machine, int clear_lsb, int data_swab,
449 AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
450 {
451 int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED;
452 uint8_t e_ident[EI_NIDENT];
453
454 fd = open(filename, O_RDONLY | O_BINARY);
455 if (fd < 0) {
456 perror(filename);
457 return -1;
458 }
459 if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
460 goto fail;
461 if (e_ident[0] != ELFMAG0 ||
462 e_ident[1] != ELFMAG1 ||
463 e_ident[2] != ELFMAG2 ||
464 e_ident[3] != ELFMAG3) {
465 ret = ELF_LOAD_NOT_ELF;
466 goto fail;
467 }
468 #ifdef HOST_WORDS_BIGENDIAN
469 data_order = ELFDATA2MSB;
470 #else
471 data_order = ELFDATA2LSB;
472 #endif
473 must_swab = data_order != e_ident[EI_DATA];
474 if (big_endian) {
475 target_data_order = ELFDATA2MSB;
476 } else {
477 target_data_order = ELFDATA2LSB;
478 }
479
480 if (target_data_order != e_ident[EI_DATA]) {
481 ret = ELF_LOAD_WRONG_ENDIAN;
482 goto fail;
483 }
484
485 lseek(fd, 0, SEEK_SET);
486 if (e_ident[EI_CLASS] == ELFCLASS64) {
487 ret = load_elf64(filename, fd, elf_note_fn,
488 translate_fn, translate_opaque, must_swab,
489 pentry, lowaddr, highaddr, elf_machine, clear_lsb,
490 data_swab, as, load_rom, sym_cb);
491 } else {
492 ret = load_elf32(filename, fd, elf_note_fn,
493 translate_fn, translate_opaque, must_swab,
494 pentry, lowaddr, highaddr, elf_machine, clear_lsb,
495 data_swab, as, load_rom, sym_cb);
496 }
497
498 fail:
499 close(fd);
500 return ret;
501 }
502
503 static void bswap_uboot_header(uboot_image_header_t *hdr)
504 {
505 #ifndef HOST_WORDS_BIGENDIAN
506 bswap32s(&hdr->ih_magic);
507 bswap32s(&hdr->ih_hcrc);
508 bswap32s(&hdr->ih_time);
509 bswap32s(&hdr->ih_size);
510 bswap32s(&hdr->ih_load);
511 bswap32s(&hdr->ih_ep);
512 bswap32s(&hdr->ih_dcrc);
513 #endif
514 }
515
516
517 #define ZALLOC_ALIGNMENT 16
518
519 static void *zalloc(void *x, unsigned items, unsigned size)
520 {
521 void *p;
522
523 size *= items;
524 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
525
526 p = g_malloc(size);
527
528 return (p);
529 }
530
531 static void zfree(void *x, void *addr)
532 {
533 g_free(addr);
534 }
535
536
537 #define HEAD_CRC 2
538 #define EXTRA_FIELD 4
539 #define ORIG_NAME 8
540 #define COMMENT 0x10
541 #define RESERVED 0xe0
542
543 #define DEFLATED 8
544
545 ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen)
546 {
547 z_stream s;
548 ssize_t dstbytes;
549 int r, i, flags;
550
551 /* skip header */
552 i = 10;
553 flags = src[3];
554 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
555 puts ("Error: Bad gzipped data\n");
556 return -1;
557 }
558 if ((flags & EXTRA_FIELD) != 0)
559 i = 12 + src[10] + (src[11] << 8);
560 if ((flags & ORIG_NAME) != 0)
561 while (src[i++] != 0)
562 ;
563 if ((flags & COMMENT) != 0)
564 while (src[i++] != 0)
565 ;
566 if ((flags & HEAD_CRC) != 0)
567 i += 2;
568 if (i >= srclen) {
569 puts ("Error: gunzip out of data in header\n");
570 return -1;
571 }
572
573 s.zalloc = zalloc;
574 s.zfree = zfree;
575
576 r = inflateInit2(&s, -MAX_WBITS);
577 if (r != Z_OK) {
578 printf ("Error: inflateInit2() returned %d\n", r);
579 return (-1);
580 }
581 s.next_in = src + i;
582 s.avail_in = srclen - i;
583 s.next_out = dst;
584 s.avail_out = dstlen;
585 r = inflate(&s, Z_FINISH);
586 if (r != Z_OK && r != Z_STREAM_END) {
587 printf ("Error: inflate() returned %d\n", r);
588 return -1;
589 }
590 dstbytes = s.next_out - (unsigned char *) dst;
591 inflateEnd(&s);
592
593 return dstbytes;
594 }
595
596 /* Load a U-Boot image. */
597 static int load_uboot_image(const char *filename, hwaddr *ep, hwaddr *loadaddr,
598 int *is_linux, uint8_t image_type,
599 uint64_t (*translate_fn)(void *, uint64_t),
600 void *translate_opaque, AddressSpace *as)
601 {
602 int fd;
603 int size;
604 hwaddr address;
605 uboot_image_header_t h;
606 uboot_image_header_t *hdr = &h;
607 uint8_t *data = NULL;
608 int ret = -1;
609 int do_uncompress = 0;
610
611 fd = open(filename, O_RDONLY | O_BINARY);
612 if (fd < 0)
613 return -1;
614
615 size = read(fd, hdr, sizeof(uboot_image_header_t));
616 if (size < sizeof(uboot_image_header_t)) {
617 goto out;
618 }
619
620 bswap_uboot_header(hdr);
621
622 if (hdr->ih_magic != IH_MAGIC)
623 goto out;
624
625 if (hdr->ih_type != image_type) {
626 if (!(image_type == IH_TYPE_KERNEL &&
627 hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
628 fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
629 image_type);
630 goto out;
631 }
632 }
633
634 /* TODO: Implement other image types. */
635 switch (hdr->ih_type) {
636 case IH_TYPE_KERNEL_NOLOAD:
637 if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
638 fprintf(stderr, "this image format (kernel_noload) cannot be "
639 "loaded on this machine type");
640 goto out;
641 }
642
643 hdr->ih_load = *loadaddr + sizeof(*hdr);
644 hdr->ih_ep += hdr->ih_load;
645 /* fall through */
646 case IH_TYPE_KERNEL:
647 address = hdr->ih_load;
648 if (translate_fn) {
649 address = translate_fn(translate_opaque, address);
650 }
651 if (loadaddr) {
652 *loadaddr = hdr->ih_load;
653 }
654
655 switch (hdr->ih_comp) {
656 case IH_COMP_NONE:
657 break;
658 case IH_COMP_GZIP:
659 do_uncompress = 1;
660 break;
661 default:
662 fprintf(stderr,
663 "Unable to load u-boot images with compression type %d\n",
664 hdr->ih_comp);
665 goto out;
666 }
667
668 if (ep) {
669 *ep = hdr->ih_ep;
670 }
671
672 /* TODO: Check CPU type. */
673 if (is_linux) {
674 if (hdr->ih_os == IH_OS_LINUX) {
675 *is_linux = 1;
676 } else {
677 *is_linux = 0;
678 }
679 }
680
681 break;
682 case IH_TYPE_RAMDISK:
683 address = *loadaddr;
684 break;
685 default:
686 fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
687 goto out;
688 }
689
690 data = g_malloc(hdr->ih_size);
691
692 if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
693 fprintf(stderr, "Error reading file\n");
694 goto out;
695 }
696
697 if (do_uncompress) {
698 uint8_t *compressed_data;
699 size_t max_bytes;
700 ssize_t bytes;
701
702 compressed_data = data;
703 max_bytes = UBOOT_MAX_GUNZIP_BYTES;
704 data = g_malloc(max_bytes);
705
706 bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
707 g_free(compressed_data);
708 if (bytes < 0) {
709 fprintf(stderr, "Unable to decompress gzipped image!\n");
710 goto out;
711 }
712 hdr->ih_size = bytes;
713 }
714
715 rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
716
717 ret = hdr->ih_size;
718
719 out:
720 g_free(data);
721 close(fd);
722 return ret;
723 }
724
725 int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
726 int *is_linux,
727 uint64_t (*translate_fn)(void *, uint64_t),
728 void *translate_opaque)
729 {
730 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
731 translate_fn, translate_opaque, NULL);
732 }
733
734 int load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
735 int *is_linux,
736 uint64_t (*translate_fn)(void *, uint64_t),
737 void *translate_opaque, AddressSpace *as)
738 {
739 return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
740 translate_fn, translate_opaque, as);
741 }
742
743 /* Load a ramdisk. */
744 int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
745 {
746 return load_ramdisk_as(filename, addr, max_sz, NULL);
747 }
748
749 int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
750 AddressSpace *as)
751 {
752 return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
753 NULL, NULL, as);
754 }
755
756 /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
757 int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
758 uint8_t **buffer)
759 {
760 uint8_t *compressed_data = NULL;
761 uint8_t *data = NULL;
762 gsize len;
763 ssize_t bytes;
764 int ret = -1;
765
766 if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
767 NULL)) {
768 goto out;
769 }
770
771 /* Is it a gzip-compressed file? */
772 if (len < 2 ||
773 compressed_data[0] != 0x1f ||
774 compressed_data[1] != 0x8b) {
775 goto out;
776 }
777
778 if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
779 max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
780 }
781
782 data = g_malloc(max_sz);
783 bytes = gunzip(data, max_sz, compressed_data, len);
784 if (bytes < 0) {
785 fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
786 filename);
787 goto out;
788 }
789
790 /* trim to actual size and return to caller */
791 *buffer = g_realloc(data, bytes);
792 ret = bytes;
793 /* ownership has been transferred to caller */
794 data = NULL;
795
796 out:
797 g_free(compressed_data);
798 g_free(data);
799 return ret;
800 }
801
802 /* Load a gzip-compressed kernel. */
803 int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
804 {
805 int bytes;
806 uint8_t *data;
807
808 bytes = load_image_gzipped_buffer(filename, max_sz, &data);
809 if (bytes != -1) {
810 rom_add_blob_fixed(filename, data, bytes, addr);
811 g_free(data);
812 }
813 return bytes;
814 }
815
816 /*
817 * Functions for reboot-persistent memory regions.
818 * - used for vga bios and option roms.
819 * - also linux kernel (-kernel / -initrd).
820 */
821
822 typedef struct Rom Rom;
823
824 struct Rom {
825 char *name;
826 char *path;
827
828 /* datasize is the amount of memory allocated in "data". If datasize is less
829 * than romsize, it means that the area from datasize to romsize is filled
830 * with zeros.
831 */
832 size_t romsize;
833 size_t datasize;
834
835 uint8_t *data;
836 MemoryRegion *mr;
837 AddressSpace *as;
838 int isrom;
839 char *fw_dir;
840 char *fw_file;
841
842 bool committed;
843
844 hwaddr addr;
845 QTAILQ_ENTRY(Rom) next;
846 };
847
848 static FWCfgState *fw_cfg;
849 static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
850
851 /* rom->data must be heap-allocated (do not use with rom_add_elf_program()) */
852 static void rom_free(Rom *rom)
853 {
854 g_free(rom->data);
855 g_free(rom->path);
856 g_free(rom->name);
857 g_free(rom->fw_dir);
858 g_free(rom->fw_file);
859 g_free(rom);
860 }
861
862 static inline bool rom_order_compare(Rom *rom, Rom *item)
863 {
864 return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
865 (rom->as == item->as && rom->addr >= item->addr);
866 }
867
868 static void rom_insert(Rom *rom)
869 {
870 Rom *item;
871
872 if (roms_loaded) {
873 hw_error ("ROM images must be loaded at startup\n");
874 }
875
876 /* The user didn't specify an address space, this is the default */
877 if (!rom->as) {
878 rom->as = &address_space_memory;
879 }
880
881 rom->committed = false;
882
883 /* List is ordered by load address in the same address space */
884 QTAILQ_FOREACH(item, &roms, next) {
885 if (rom_order_compare(rom, item)) {
886 continue;
887 }
888 QTAILQ_INSERT_BEFORE(item, rom, next);
889 return;
890 }
891 QTAILQ_INSERT_TAIL(&roms, rom, next);
892 }
893
894 static void fw_cfg_resized(const char *id, uint64_t length, void *host)
895 {
896 if (fw_cfg) {
897 fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
898 }
899 }
900
901 static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
902 {
903 void *data;
904
905 rom->mr = g_malloc(sizeof(*rom->mr));
906 memory_region_init_resizeable_ram(rom->mr, owner, name,
907 rom->datasize, rom->romsize,
908 fw_cfg_resized,
909 &error_fatal);
910 memory_region_set_readonly(rom->mr, ro);
911 vmstate_register_ram_global(rom->mr);
912
913 data = memory_region_get_ram_ptr(rom->mr);
914 memcpy(data, rom->data, rom->datasize);
915
916 return data;
917 }
918
919 int rom_add_file(const char *file, const char *fw_dir,
920 hwaddr addr, int32_t bootindex,
921 bool option_rom, MemoryRegion *mr,
922 AddressSpace *as)
923 {
924 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
925 Rom *rom;
926 int rc, fd = -1;
927 char devpath[100];
928
929 if (as && mr) {
930 fprintf(stderr, "Specifying an Address Space and Memory Region is " \
931 "not valid when loading a rom\n");
932 /* We haven't allocated anything so we don't need any cleanup */
933 return -1;
934 }
935
936 rom = g_malloc0(sizeof(*rom));
937 rom->name = g_strdup(file);
938 rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
939 rom->as = as;
940 if (rom->path == NULL) {
941 rom->path = g_strdup(file);
942 }
943
944 fd = open(rom->path, O_RDONLY | O_BINARY);
945 if (fd == -1) {
946 fprintf(stderr, "Could not open option rom '%s': %s\n",
947 rom->path, strerror(errno));
948 goto err;
949 }
950
951 if (fw_dir) {
952 rom->fw_dir = g_strdup(fw_dir);
953 rom->fw_file = g_strdup(file);
954 }
955 rom->addr = addr;
956 rom->romsize = lseek(fd, 0, SEEK_END);
957 if (rom->romsize == -1) {
958 fprintf(stderr, "rom: file %-20s: get size error: %s\n",
959 rom->name, strerror(errno));
960 goto err;
961 }
962
963 rom->datasize = rom->romsize;
964 rom->data = g_malloc0(rom->datasize);
965 lseek(fd, 0, SEEK_SET);
966 rc = read(fd, rom->data, rom->datasize);
967 if (rc != rom->datasize) {
968 fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
969 rom->name, rc, rom->datasize);
970 goto err;
971 }
972 close(fd);
973 rom_insert(rom);
974 if (rom->fw_file && fw_cfg) {
975 const char *basename;
976 char fw_file_name[FW_CFG_MAX_FILE_PATH];
977 void *data;
978
979 basename = strrchr(rom->fw_file, '/');
980 if (basename) {
981 basename++;
982 } else {
983 basename = rom->fw_file;
984 }
985 snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
986 basename);
987 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
988
989 if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
990 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
991 } else {
992 data = rom->data;
993 }
994
995 fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
996 } else {
997 if (mr) {
998 rom->mr = mr;
999 snprintf(devpath, sizeof(devpath), "/rom@%s", file);
1000 } else {
1001 snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
1002 }
1003 }
1004
1005 add_boot_device_path(bootindex, NULL, devpath);
1006 return 0;
1007
1008 err:
1009 if (fd != -1)
1010 close(fd);
1011
1012 rom_free(rom);
1013 return -1;
1014 }
1015
1016 MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
1017 size_t max_len, hwaddr addr, const char *fw_file_name,
1018 FWCfgCallback fw_callback, void *callback_opaque,
1019 AddressSpace *as, bool read_only)
1020 {
1021 MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1022 Rom *rom;
1023 MemoryRegion *mr = NULL;
1024
1025 rom = g_malloc0(sizeof(*rom));
1026 rom->name = g_strdup(name);
1027 rom->as = as;
1028 rom->addr = addr;
1029 rom->romsize = max_len ? max_len : len;
1030 rom->datasize = len;
1031 g_assert(rom->romsize >= rom->datasize);
1032 rom->data = g_malloc0(rom->datasize);
1033 memcpy(rom->data, blob, len);
1034 rom_insert(rom);
1035 if (fw_file_name && fw_cfg) {
1036 char devpath[100];
1037 void *data;
1038
1039 if (read_only) {
1040 snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
1041 } else {
1042 snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
1043 }
1044
1045 if (mc->rom_file_has_mr) {
1046 data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
1047 mr = rom->mr;
1048 } else {
1049 data = rom->data;
1050 }
1051
1052 fw_cfg_add_file_callback(fw_cfg, fw_file_name,
1053 fw_callback, NULL, callback_opaque,
1054 data, rom->datasize, read_only);
1055 }
1056 return mr;
1057 }
1058
1059 /* This function is specific for elf program because we don't need to allocate
1060 * all the rom. We just allocate the first part and the rest is just zeros. This
1061 * is why romsize and datasize are different. Also, this function seize the
1062 * memory ownership of "data", so we don't have to allocate and copy the buffer.
1063 */
1064 int rom_add_elf_program(const char *name, void *data, size_t datasize,
1065 size_t romsize, hwaddr addr, AddressSpace *as)
1066 {
1067 Rom *rom;
1068
1069 rom = g_malloc0(sizeof(*rom));
1070 rom->name = g_strdup(name);
1071 rom->addr = addr;
1072 rom->datasize = datasize;
1073 rom->romsize = romsize;
1074 rom->data = data;
1075 rom->as = as;
1076 rom_insert(rom);
1077 return 0;
1078 }
1079
1080 int rom_add_vga(const char *file)
1081 {
1082 return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
1083 }
1084
1085 int rom_add_option(const char *file, int32_t bootindex)
1086 {
1087 return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
1088 }
1089
1090 static void rom_reset(void *unused)
1091 {
1092 Rom *rom;
1093
1094 QTAILQ_FOREACH(rom, &roms, next) {
1095 if (rom->fw_file) {
1096 continue;
1097 }
1098 if (rom->data == NULL) {
1099 continue;
1100 }
1101 if (rom->mr) {
1102 void *host = memory_region_get_ram_ptr(rom->mr);
1103 memcpy(host, rom->data, rom->datasize);
1104 } else {
1105 address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
1106 rom->data, rom->datasize);
1107 }
1108 if (rom->isrom) {
1109 /* rom needs to be written only once */
1110 g_free(rom->data);
1111 rom->data = NULL;
1112 }
1113 /*
1114 * The rom loader is really on the same level as firmware in the guest
1115 * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
1116 * that the instruction cache for that new region is clear, so that the
1117 * CPU definitely fetches its instructions from the just written data.
1118 */
1119 cpu_flush_icache_range(rom->addr, rom->datasize);
1120 }
1121 }
1122
1123 int rom_check_and_register_reset(void)
1124 {
1125 hwaddr addr = 0;
1126 MemoryRegionSection section;
1127 Rom *rom;
1128 AddressSpace *as = NULL;
1129
1130 QTAILQ_FOREACH(rom, &roms, next) {
1131 if (rom->fw_file) {
1132 continue;
1133 }
1134 if (!rom->mr) {
1135 if ((addr > rom->addr) && (as == rom->as)) {
1136 fprintf(stderr, "rom: requested regions overlap "
1137 "(rom %s. free=0x" TARGET_FMT_plx
1138 ", addr=0x" TARGET_FMT_plx ")\n",
1139 rom->name, addr, rom->addr);
1140 return -1;
1141 }
1142 addr = rom->addr;
1143 addr += rom->romsize;
1144 as = rom->as;
1145 }
1146 section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
1147 rom->addr, 1);
1148 rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
1149 memory_region_unref(section.mr);
1150 }
1151 qemu_register_reset(rom_reset, NULL);
1152 roms_loaded = 1;
1153 return 0;
1154 }
1155
1156 void rom_set_fw(FWCfgState *f)
1157 {
1158 fw_cfg = f;
1159 }
1160
1161 void rom_set_order_override(int order)
1162 {
1163 if (!fw_cfg)
1164 return;
1165 fw_cfg_set_order_override(fw_cfg, order);
1166 }
1167
1168 void rom_reset_order_override(void)
1169 {
1170 if (!fw_cfg)
1171 return;
1172 fw_cfg_reset_order_override(fw_cfg);
1173 }
1174
1175 void rom_transaction_begin(void)
1176 {
1177 Rom *rom;
1178
1179 /* Ignore ROMs added without the transaction API */
1180 QTAILQ_FOREACH(rom, &roms, next) {
1181 rom->committed = true;
1182 }
1183 }
1184
1185 void rom_transaction_end(bool commit)
1186 {
1187 Rom *rom;
1188 Rom *tmp;
1189
1190 QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
1191 if (rom->committed) {
1192 continue;
1193 }
1194 if (commit) {
1195 rom->committed = true;
1196 } else {
1197 QTAILQ_REMOVE(&roms, rom, next);
1198 rom_free(rom);
1199 }
1200 }
1201 }
1202
1203 static Rom *find_rom(hwaddr addr, size_t size)
1204 {
1205 Rom *rom;
1206
1207 QTAILQ_FOREACH(rom, &roms, next) {
1208 if (rom->fw_file) {
1209 continue;
1210 }
1211 if (rom->mr) {
1212 continue;
1213 }
1214 if (rom->addr > addr) {
1215 continue;
1216 }
1217 if (rom->addr + rom->romsize < addr + size) {
1218 continue;
1219 }
1220 return rom;
1221 }
1222 return NULL;
1223 }
1224
1225 /*
1226 * Copies memory from registered ROMs to dest. Any memory that is contained in
1227 * a ROM between addr and addr + size is copied. Note that this can involve
1228 * multiple ROMs, which need not start at addr and need not end at addr + size.
1229 */
1230 int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
1231 {
1232 hwaddr end = addr + size;
1233 uint8_t *s, *d = dest;
1234 size_t l = 0;
1235 Rom *rom;
1236
1237 QTAILQ_FOREACH(rom, &roms, next) {
1238 if (rom->fw_file) {
1239 continue;
1240 }
1241 if (rom->mr) {
1242 continue;
1243 }
1244 if (rom->addr + rom->romsize < addr) {
1245 continue;
1246 }
1247 if (rom->addr > end) {
1248 break;
1249 }
1250
1251 d = dest + (rom->addr - addr);
1252 s = rom->data;
1253 l = rom->datasize;
1254
1255 if ((d + l) > (dest + size)) {
1256 l = dest - d;
1257 }
1258
1259 if (l > 0) {
1260 memcpy(d, s, l);
1261 }
1262
1263 if (rom->romsize > rom->datasize) {
1264 /* If datasize is less than romsize, it means that we didn't
1265 * allocate all the ROM because the trailing data are only zeros.
1266 */
1267
1268 d += l;
1269 l = rom->romsize - rom->datasize;
1270
1271 if ((d + l) > (dest + size)) {
1272 /* Rom size doesn't fit in the destination area. Adjust to avoid
1273 * overflow.
1274 */
1275 l = dest - d;
1276 }
1277
1278 if (l > 0) {
1279 memset(d, 0x0, l);
1280 }
1281 }
1282 }
1283
1284 return (d + l) - dest;
1285 }
1286
1287 void *rom_ptr(hwaddr addr, size_t size)
1288 {
1289 Rom *rom;
1290
1291 rom = find_rom(addr, size);
1292 if (!rom || !rom->data)
1293 return NULL;
1294 return rom->data + (addr - rom->addr);
1295 }
1296
1297 void hmp_info_roms(Monitor *mon, const QDict *qdict)
1298 {
1299 Rom *rom;
1300
1301 QTAILQ_FOREACH(rom, &roms, next) {
1302 if (rom->mr) {
1303 monitor_printf(mon, "%s"
1304 " size=0x%06zx name=\"%s\"\n",
1305 memory_region_name(rom->mr),
1306 rom->romsize,
1307 rom->name);
1308 } else if (!rom->fw_file) {
1309 monitor_printf(mon, "addr=" TARGET_FMT_plx
1310 " size=0x%06zx mem=%s name=\"%s\"\n",
1311 rom->addr, rom->romsize,
1312 rom->isrom ? "rom" : "ram",
1313 rom->name);
1314 } else {
1315 monitor_printf(mon, "fw=%s/%s"
1316 " size=0x%06zx name=\"%s\"\n",
1317 rom->fw_dir,
1318 rom->fw_file,
1319 rom->romsize,
1320 rom->name);
1321 }
1322 }
1323 }
1324
1325 typedef enum HexRecord HexRecord;
1326 enum HexRecord {
1327 DATA_RECORD = 0,
1328 EOF_RECORD,
1329 EXT_SEG_ADDR_RECORD,
1330 START_SEG_ADDR_RECORD,
1331 EXT_LINEAR_ADDR_RECORD,
1332 START_LINEAR_ADDR_RECORD,
1333 };
1334
1335 /* Each record contains a 16-bit address which is combined with the upper 16
1336 * bits of the implicit "next address" to form a 32-bit address.
1337 */
1338 #define NEXT_ADDR_MASK 0xffff0000
1339
1340 #define DATA_FIELD_MAX_LEN 0xff
1341 #define LEN_EXCEPT_DATA 0x5
1342 /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
1343 * sizeof(checksum) */
1344 typedef struct {
1345 uint8_t byte_count;
1346 uint16_t address;
1347 uint8_t record_type;
1348 uint8_t data[DATA_FIELD_MAX_LEN];
1349 uint8_t checksum;
1350 } HexLine;
1351
1352 /* return 0 or -1 if error */
1353 static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
1354 uint32_t *index, const bool in_process)
1355 {
1356 /* +-------+---------------+-------+---------------------+--------+
1357 * | byte | |record | | |
1358 * | count | address | type | data |checksum|
1359 * +-------+---------------+-------+---------------------+--------+
1360 * ^ ^ ^ ^ ^ ^
1361 * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
1362 */
1363 uint8_t value = 0;
1364 uint32_t idx = *index;
1365 /* ignore space */
1366 if (g_ascii_isspace(c)) {
1367 return true;
1368 }
1369 if (!g_ascii_isxdigit(c) || !in_process) {
1370 return false;
1371 }
1372 value = g_ascii_xdigit_value(c);
1373 value = (idx & 0x1) ? (value & 0xf) : (value << 4);
1374 if (idx < 2) {
1375 line->byte_count |= value;
1376 } else if (2 <= idx && idx < 6) {
1377 line->address <<= 4;
1378 line->address += g_ascii_xdigit_value(c);
1379 } else if (6 <= idx && idx < 8) {
1380 line->record_type |= value;
1381 } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
1382 line->data[(idx - 8) >> 1] |= value;
1383 } else if (8 + 2 * line->byte_count <= idx &&
1384 idx < 10 + 2 * line->byte_count) {
1385 line->checksum |= value;
1386 } else {
1387 return false;
1388 }
1389 *our_checksum += value;
1390 ++(*index);
1391 return true;
1392 }
1393
1394 typedef struct {
1395 const char *filename;
1396 HexLine line;
1397 uint8_t *bin_buf;
1398 hwaddr *start_addr;
1399 int total_size;
1400 uint32_t next_address_to_write;
1401 uint32_t current_address;
1402 uint32_t current_rom_index;
1403 uint32_t rom_start_address;
1404 AddressSpace *as;
1405 } HexParser;
1406
1407 /* return size or -1 if error */
1408 static int handle_record_type(HexParser *parser)
1409 {
1410 HexLine *line = &(parser->line);
1411 switch (line->record_type) {
1412 case DATA_RECORD:
1413 parser->current_address =
1414 (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
1415 /* verify this is a contiguous block of memory */
1416 if (parser->current_address != parser->next_address_to_write) {
1417 if (parser->current_rom_index != 0) {
1418 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1419 parser->current_rom_index,
1420 parser->rom_start_address, parser->as);
1421 }
1422 parser->rom_start_address = parser->current_address;
1423 parser->current_rom_index = 0;
1424 }
1425
1426 /* copy from line buffer to output bin_buf */
1427 memcpy(parser->bin_buf + parser->current_rom_index, line->data,
1428 line->byte_count);
1429 parser->current_rom_index += line->byte_count;
1430 parser->total_size += line->byte_count;
1431 /* save next address to write */
1432 parser->next_address_to_write =
1433 parser->current_address + line->byte_count;
1434 break;
1435
1436 case EOF_RECORD:
1437 if (parser->current_rom_index != 0) {
1438 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1439 parser->current_rom_index,
1440 parser->rom_start_address, parser->as);
1441 }
1442 return parser->total_size;
1443 case EXT_SEG_ADDR_RECORD:
1444 case EXT_LINEAR_ADDR_RECORD:
1445 if (line->byte_count != 2 && line->address != 0) {
1446 return -1;
1447 }
1448
1449 if (parser->current_rom_index != 0) {
1450 rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1451 parser->current_rom_index,
1452 parser->rom_start_address, parser->as);
1453 }
1454
1455 /* save next address to write,
1456 * in case of non-contiguous block of memory */
1457 parser->next_address_to_write = (line->data[0] << 12) |
1458 (line->data[1] << 4);
1459 if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
1460 parser->next_address_to_write <<= 12;
1461 }
1462
1463 parser->rom_start_address = parser->next_address_to_write;
1464 parser->current_rom_index = 0;
1465 break;
1466
1467 case START_SEG_ADDR_RECORD:
1468 if (line->byte_count != 4 && line->address != 0) {
1469 return -1;
1470 }
1471
1472 /* x86 16-bit CS:IP segmented addressing */
1473 *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
1474 ((line->data[2] << 8) | line->data[3]);
1475 break;
1476
1477 case START_LINEAR_ADDR_RECORD:
1478 if (line->byte_count != 4 && line->address != 0) {
1479 return -1;
1480 }
1481
1482 *(parser->start_addr) = ldl_be_p(line->data);
1483 break;
1484
1485 default:
1486 return -1;
1487 }
1488
1489 return parser->total_size;
1490 }
1491
1492 /* return size or -1 if error */
1493 static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
1494 size_t hex_blob_size, AddressSpace *as)
1495 {
1496 bool in_process = false; /* avoid re-enter and
1497 * check whether record begin with ':' */
1498 uint8_t *end = hex_blob + hex_blob_size;
1499 uint8_t our_checksum = 0;
1500 uint32_t record_index = 0;
1501 HexParser parser = {
1502 .filename = filename,
1503 .bin_buf = g_malloc(hex_blob_size),
1504 .start_addr = addr,
1505 .as = as,
1506 };
1507
1508 rom_transaction_begin();
1509
1510 for (; hex_blob < end; ++hex_blob) {
1511 switch (*hex_blob) {
1512 case '\r':
1513 case '\n':
1514 if (!in_process) {
1515 break;
1516 }
1517
1518 in_process = false;
1519 if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
1520 record_index ||
1521 our_checksum != 0) {
1522 parser.total_size = -1;
1523 goto out;
1524 }
1525
1526 if (handle_record_type(&parser) == -1) {
1527 parser.total_size = -1;
1528 goto out;
1529 }
1530 break;
1531
1532 /* start of a new record. */
1533 case ':':
1534 memset(&parser.line, 0, sizeof(HexLine));
1535 in_process = true;
1536 record_index = 0;
1537 break;
1538
1539 /* decoding lines */
1540 default:
1541 if (!parse_record(&parser.line, &our_checksum, *hex_blob,
1542 &record_index, in_process)) {
1543 parser.total_size = -1;
1544 goto out;
1545 }
1546 break;
1547 }
1548 }
1549
1550 out:
1551 g_free(parser.bin_buf);
1552 rom_transaction_end(parser.total_size != -1);
1553 return parser.total_size;
1554 }
1555
1556 /* return size or -1 if error */
1557 int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as)
1558 {
1559 gsize hex_blob_size;
1560 gchar *hex_blob;
1561 int total_size = 0;
1562
1563 if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
1564 return -1;
1565 }
1566
1567 total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,
1568 hex_blob_size, as);
1569
1570 g_free(hex_blob);
1571 return total_size;
1572 }