Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/core/loader.c
Line
Count
Source
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/datadir.h"
47
#include "qemu/error-report.h"
48
#include "qapi/error.h"
49
#include "qapi/qapi-commands-machine.h"
50
#include "qapi/type-helpers.h"
51
#include "qemu/units.h"
52
#include "trace.h"
53
#include "hw/core/hw-error.h"
54
#include "disas/disas.h"
55
#include "migration/cpr.h"
56
#include "migration/vmstate.h"
57
#include "monitor/monitor.h"
58
#include "system/reset.h"
59
#include "system/system.h"
60
#include "uboot_image.h"
61
#include "hw/core/loader.h"
62
#include "hw/nvram/fw_cfg.h"
63
#include "system/memory.h"
64
#include "hw/core/boards.h"
65
#include "qemu/cutils.h"
66
#include "system/runstate.h"
67
#include "tcg/debuginfo.h"
68
69
#include <zlib.h>
70
71
#ifdef CONFIG_ZSTD
72
#include <zstd.h>
73
#include <zstd_errors.h>
74
#endif
75
76
static int roms_loaded;
77
78
/* return the size or -1 if error */
79
int64_t get_image_size(const char *filename, Error **errp)
80
86
{
81
86
    int fd;
82
86
    int64_t size;
83
84
86
    fd = qemu_open(filename, O_RDONLY | O_BINARY, errp);
85
86
86
    if (fd < 0) {
87
0
        return -1;
88
0
    }
89
90
86
    size = lseek(fd, 0, SEEK_END);
91
92
86
    if (size < 0) {
93
0
        error_setg_errno(errp, errno, "lseek failure: %s", filename);
94
0
        close(fd);
95
0
        return -1;
96
0
    }
97
98
86
    close(fd);
99
86
    return size;
100
86
}
101
102
/* return the size or -1 if error */
103
ssize_t load_image_size(const char *filename, void *addr, size_t size)
104
25
{
105
25
    int fd;
106
25
    ssize_t actsize, l = 0;
107
108
25
    fd = open(filename, O_RDONLY | O_BINARY);
109
25
    if (fd < 0) {
110
0
        return -1;
111
0
    }
112
113
50
    while ((actsize = read(fd, addr + l, size - l)) > 0) {
114
25
        l += actsize;
115
25
    }
116
117
25
    close(fd);
118
119
25
    return actsize < 0 ? -1 : l;
120
25
}
121
122
/* read()-like version */
123
ssize_t read_targphys(const char *name,
124
                      int fd, hwaddr dst_addr, size_t nbytes)
125
0
{
126
0
    uint8_t *buf;
127
0
    ssize_t did;
128
129
0
    buf = g_malloc(nbytes);
130
0
    did = read(fd, buf, nbytes);
131
0
    if (did > 0)
132
0
        rom_add_blob_fixed("read", buf, did, dst_addr);
133
0
    g_free(buf);
134
0
    return did;
135
0
}
136
137
ssize_t load_image_targphys(const char *filename,
138
                            hwaddr addr, uint64_t max_sz, Error **errp)
139
0
{
140
0
    return load_image_targphys_as(filename, addr, max_sz, NULL, errp);
141
0
}
142
143
/* return the size or -1 if error */
144
ssize_t load_image_targphys_as(const char *filename,
145
                               hwaddr addr, uint64_t max_sz, AddressSpace *as,
146
                               Error **errp)
147
0
{
148
0
    ssize_t size;
149
150
0
    size = get_image_size(filename, errp);
151
0
    if (size < 0) {
152
0
        return -1;
153
0
    }
154
155
0
    if (size == 0) {
156
0
        error_setg(errp, "empty file: %s", filename);
157
0
        return -1;
158
0
    }
159
160
0
    if (size > max_sz) {
161
0
        char *size_str = size_to_str(max_sz);
162
163
0
        error_setg(errp, "%s exceeds maximum image size (%s)",
164
0
                   filename, size_str);
165
166
0
        g_free(size_str);
167
0
        return -1;
168
0
    }
169
170
0
    if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
171
0
        error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
172
0
                   filename, addr);
173
0
        return -1;
174
0
    }
175
0
    return size;
176
0
}
177
178
ssize_t load_image_mr(const char *filename, MemoryRegion *mr)
179
0
{
180
0
    ssize_t size;
181
182
0
    if (!memory_access_is_direct(mr, false, MEMTXATTRS_UNSPECIFIED)) {
183
        /* Can only load an image into RAM or ROM */
184
0
        return -1;
185
0
    }
186
187
0
    size = get_image_size(filename, NULL);
188
189
0
    if (size < 0 || size > memory_region_size(mr)) {
190
0
        return -1;
191
0
    }
192
0
    if (size > 0) {
193
0
        if (rom_add_file_mr(filename, mr, -1) < 0) {
194
0
            return -1;
195
0
        }
196
0
    }
197
0
    return size;
198
0
}
199
200
void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
201
                      const char *source)
202
0
{
203
0
    const char *nulp;
204
0
    char *ptr;
205
206
0
    if (buf_size <= 0) return;
207
0
    nulp = memchr(source, 0, buf_size);
208
0
    if (nulp) {
209
0
        rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
210
0
    } else {
211
0
        rom_add_blob_fixed(name, source, buf_size, dest);
212
0
        ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr));
213
0
        *ptr = 0;
214
0
    }
215
0
}
216
217
/* A.OUT loader */
218
219
struct exec
220
{
221
  uint32_t a_info;   /* Use macros N_MAGIC, etc for access */
222
  uint32_t a_text;   /* length of text, in bytes */
223
  uint32_t a_data;   /* length of data, in bytes */
224
  uint32_t a_bss;    /* length of uninitialized data area, in bytes */
225
  uint32_t a_syms;   /* length of symbol table data in file, in bytes */
226
  uint32_t a_entry;  /* start address */
227
  uint32_t a_trsize; /* length of relocation info for text, in bytes */
228
  uint32_t a_drsize; /* length of relocation info for data, in bytes */
229
};
230
231
static void bswap_ahdr(struct exec *e)
232
0
{
233
0
    bswap32s(&e->a_info);
234
0
    bswap32s(&e->a_text);
235
0
    bswap32s(&e->a_data);
236
0
    bswap32s(&e->a_bss);
237
0
    bswap32s(&e->a_syms);
238
0
    bswap32s(&e->a_entry);
239
0
    bswap32s(&e->a_trsize);
240
0
    bswap32s(&e->a_drsize);
241
0
}
242
243
0
#define N_MAGIC(exec) ((exec).a_info & 0xffff)
244
0
#define OMAGIC 0407
245
0
#define NMAGIC 0410
246
0
#define ZMAGIC 0413
247
0
#define QMAGIC 0314
248
0
#define _N_HDROFF(x) (1024 - sizeof (struct exec))
249
#define N_TXTOFF(x)                                                 \
250
0
    (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
251
0
     (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
252
0
#define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
253
0
#define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
254
255
0
#define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
256
257
#define N_DATADDR(x, target_page_size) \
258
0
    (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
259
0
     : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
260
261
262
ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
263
                  bool big_endian, hwaddr target_page_size)
264
0
{
265
0
    int fd;
266
0
    ssize_t size, ret;
267
0
    struct exec e;
268
0
    uint32_t magic;
269
270
0
    fd = open(filename, O_RDONLY | O_BINARY);
271
0
    if (fd < 0)
272
0
        return -1;
273
274
0
    size = read(fd, &e, sizeof(e));
275
0
    if (size < 0)
276
0
        goto fail;
277
278
0
    if (big_endian != HOST_BIG_ENDIAN) {
279
0
        bswap_ahdr(&e);
280
0
    }
281
282
0
    magic = N_MAGIC(e);
283
0
    switch (magic) {
284
0
    case ZMAGIC:
285
0
    case QMAGIC:
286
0
    case OMAGIC:
287
0
        if (e.a_text + e.a_data > max_sz)
288
0
            goto fail;
289
0
        lseek(fd, N_TXTOFF(e), SEEK_SET);
290
0
        size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
291
0
        if (size < 0)
292
0
            goto fail;
293
0
        break;
294
0
    case NMAGIC:
295
0
        if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
296
0
            goto fail;
297
0
        lseek(fd, N_TXTOFF(e), SEEK_SET);
298
0
        size = read_targphys(filename, fd, addr, e.a_text);
299
0
        if (size < 0)
300
0
            goto fail;
301
0
        ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
302
0
                            e.a_data);
303
0
        if (ret < 0)
304
0
            goto fail;
305
0
        size += ret;
306
0
        break;
307
0
    default:
308
0
        goto fail;
309
0
    }
310
0
    close(fd);
311
0
    return size;
312
0
 fail:
313
0
    close(fd);
314
0
    return -1;
315
0
}
316
317
/* ELF loader */
318
319
static void *load_at(int fd, off_t offset, size_t size)
320
0
{
321
0
    void *ptr;
322
0
    if (lseek(fd, offset, SEEK_SET) < 0)
323
0
        return NULL;
324
0
    ptr = g_malloc(size);
325
0
    if (read(fd, ptr, size) != size) {
326
0
        g_free(ptr);
327
0
        return NULL;
328
0
    }
329
0
    return ptr;
330
0
}
331
332
#define ELF_CLASS   ELFCLASS32
333
#include "elf.h"
334
335
0
#define SZ              32
336
0
#define elf_word        uint32_t
337
#define elf_sword       int32_t
338
0
#define bswapSZs        bswap32s
339
#include "hw/elf_ops.h.inc"
340
341
#undef elfhdr
342
#undef elf_phdr
343
#undef elf_shdr
344
#undef elf_sym
345
#undef elf_rela
346
#undef elf_note
347
#undef elf_word
348
#undef elf_sword
349
#undef bswapSZs
350
#undef SZ
351
#define elfhdr          elf64_hdr
352
#define elf_phdr        elf64_phdr
353
#define elf_note        elf64_note
354
#define elf_shdr        elf64_shdr
355
#define elf_sym         elf64_sym
356
#define elf_rela        elf64_rela
357
0
#define elf_word        uint64_t
358
#define elf_sword       int64_t
359
0
#define bswapSZs        bswap64s
360
0
#define SZ              64
361
#include "hw/elf_ops.h.inc"
362
363
const char *load_elf_strerror(ssize_t error)
364
0
{
365
0
    switch (error) {
366
0
    case 0:
367
0
        return "No error";
368
0
    case ELF_LOAD_FAILED:
369
0
        return "Failed to load ELF";
370
0
    case ELF_LOAD_NOT_ELF:
371
0
        return "The image is not ELF";
372
0
    case ELF_LOAD_WRONG_ARCH:
373
0
        return "The image is from incompatible architecture";
374
0
    case ELF_LOAD_WRONG_ENDIAN:
375
0
        return "The image has incorrect endianness";
376
0
    case ELF_LOAD_TOO_BIG:
377
0
        return "The image segments are too big to load";
378
0
    default:
379
0
        return "Unknown error";
380
0
    }
381
0
}
382
383
bool load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
384
0
{
385
0
    bool ok = false;
386
0
    int fd;
387
0
    uint8_t e_ident_local[EI_NIDENT];
388
0
    uint8_t *e_ident;
389
0
    size_t hdr_size, off;
390
0
    bool is64l;
391
392
0
    if (!hdr) {
393
0
        hdr = e_ident_local;
394
0
    }
395
0
    e_ident = hdr;
396
397
0
    fd = open(filename, O_RDONLY | O_BINARY);
398
0
    if (fd < 0) {
399
0
        error_setg_file_open(errp, errno, filename);
400
0
        return false;
401
0
    }
402
0
    if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
403
0
        error_setg_errno(errp, errno, "Failed to read file: %s", filename);
404
0
        goto fail;
405
0
    }
406
0
    if (e_ident[0] != ELFMAG0 ||
407
0
        e_ident[1] != ELFMAG1 ||
408
0
        e_ident[2] != ELFMAG2 ||
409
0
        e_ident[3] != ELFMAG3) {
410
0
        error_setg(errp, "Bad ELF magic");
411
0
        goto fail;
412
0
    }
413
414
0
    is64l = e_ident[EI_CLASS] == ELFCLASS64;
415
0
    hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr);
416
0
    if (is64) {
417
0
        *is64 = is64l;
418
0
    }
419
420
0
    off = EI_NIDENT;
421
0
    while (hdr != e_ident_local && off < hdr_size) {
422
0
        size_t br = read(fd, hdr + off, hdr_size - off);
423
0
        switch (br) {
424
0
        case 0:
425
0
            error_setg(errp, "File too short: %s", filename);
426
0
            goto fail;
427
0
        case -1:
428
0
            error_setg_errno(errp, errno, "Failed to read file: %s",
429
0
                             filename);
430
0
            goto fail;
431
0
        }
432
0
        off += br;
433
0
    }
434
435
0
    ok = true;
436
437
0
fail:
438
0
    close(fd);
439
0
    return ok;
440
0
}
441
442
/* return < 0 if error, otherwise the number of bytes loaded in memory */
443
ssize_t load_elf(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, uint64_t *lowaddr,
447
                 uint64_t *highaddr, uint32_t *pflags, int elf_data_order,
448
                 int elf_machine, int clear_lsb, int data_swab)
449
0
{
450
0
    return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque,
451
0
                       pentry, lowaddr, highaddr, pflags, elf_data_order,
452
0
                       elf_machine, clear_lsb, data_swab, NULL);
453
0
}
454
455
/* return < 0 if error, otherwise the number of bytes loaded in memory */
456
ssize_t load_elf_as(const char *filename,
457
                    uint64_t (*elf_note_fn)(void *, void *, bool),
458
                    uint64_t (*translate_fn)(void *, uint64_t),
459
                    void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
460
                    uint64_t *highaddr, uint32_t *pflags, int elf_data_order,
461
                    int elf_machine, int clear_lsb, int data_swab,
462
                    AddressSpace *as)
463
0
{
464
0
    return load_elf_ram_sym(filename, elf_note_fn,
465
0
                            translate_fn, translate_opaque,
466
0
                            pentry, lowaddr, highaddr, pflags, elf_data_order,
467
0
                            elf_machine, clear_lsb, data_swab, as,
468
0
                            true, NULL);
469
0
}
470
471
/* return < 0 if error, otherwise the number of bytes loaded in memory */
472
ssize_t load_elf_ram_sym(const char *filename,
473
                         uint64_t (*elf_note_fn)(void *, void *, bool),
474
                         uint64_t (*translate_fn)(void *, uint64_t),
475
                         void *translate_opaque, uint64_t *pentry,
476
                         uint64_t *lowaddr, uint64_t *highaddr,
477
                         uint32_t *pflags, int elf_data_order, int elf_machine,
478
                         int clear_lsb, int data_swab,
479
                         AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
480
0
{
481
0
    const int host_data_order = HOST_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB;
482
0
    int fd, must_swab;
483
0
    ssize_t ret = ELF_LOAD_FAILED;
484
0
    uint8_t e_ident[EI_NIDENT];
485
486
0
    fd = open(filename, O_RDONLY | O_BINARY);
487
0
    if (fd < 0) {
488
0
        perror(filename);
489
0
        return -1;
490
0
    }
491
0
    if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
492
0
        goto fail;
493
0
    if (e_ident[0] != ELFMAG0 ||
494
0
        e_ident[1] != ELFMAG1 ||
495
0
        e_ident[2] != ELFMAG2 ||
496
0
        e_ident[3] != ELFMAG3) {
497
0
        ret = ELF_LOAD_NOT_ELF;
498
0
        goto fail;
499
0
    }
500
501
0
    if (elf_data_order != ELFDATANONE && elf_data_order != e_ident[EI_DATA]) {
502
0
        ret = ELF_LOAD_WRONG_ENDIAN;
503
0
        goto fail;
504
0
    }
505
506
0
    must_swab = host_data_order != e_ident[EI_DATA];
507
508
0
    lseek(fd, 0, SEEK_SET);
509
0
    if (e_ident[EI_CLASS] == ELFCLASS64) {
510
0
        ret = load_elf64(filename, fd, elf_note_fn,
511
0
                         translate_fn, translate_opaque, must_swab,
512
0
                         pentry, lowaddr, highaddr, pflags, elf_machine,
513
0
                         clear_lsb, data_swab, as, load_rom, sym_cb);
514
0
    } else {
515
0
        ret = load_elf32(filename, fd, elf_note_fn,
516
0
                         translate_fn, translate_opaque, must_swab,
517
0
                         pentry, lowaddr, highaddr, pflags, elf_machine,
518
0
                         clear_lsb, data_swab, as, load_rom, sym_cb);
519
0
    }
520
521
0
    if (ret > 0) {
522
0
        debuginfo_report_elf(filename, fd, 0);
523
0
    }
524
525
0
 fail:
526
0
    close(fd);
527
0
    return ret;
528
0
}
529
530
static void bswap_uboot_header(uboot_image_header_t *hdr)
531
0
{
532
0
#if !HOST_BIG_ENDIAN
533
0
    bswap32s(&hdr->ih_magic);
534
0
    bswap32s(&hdr->ih_hcrc);
535
0
    bswap32s(&hdr->ih_time);
536
0
    bswap32s(&hdr->ih_size);
537
0
    bswap32s(&hdr->ih_load);
538
0
    bswap32s(&hdr->ih_ep);
539
0
    bswap32s(&hdr->ih_dcrc);
540
0
#endif
541
0
}
542
543
544
0
#define ZALLOC_ALIGNMENT    16
545
546
static void *zalloc(void *x, unsigned items, unsigned size)
547
0
{
548
0
    void *p;
549
550
0
    size *= items;
551
0
    size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
552
553
0
    p = g_malloc(size);
554
555
0
    return (p);
556
0
}
557
558
static void zfree(void *x, void *addr)
559
0
{
560
0
    g_free(addr);
561
0
}
562
563
564
0
#define HEAD_CRC    2
565
0
#define EXTRA_FIELD 4
566
0
#define ORIG_NAME   8
567
0
#define COMMENT     0x10
568
0
#define RESERVED    0xe0
569
570
0
#define DEFLATED    8
571
572
ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen)
573
0
{
574
0
    z_stream s = {};
575
0
    ssize_t dstbytes;
576
0
    int r, i, flags;
577
578
    /* skip header */
579
0
    i = 10;
580
0
    if (srclen < 4) {
581
0
        goto toosmall;
582
0
    }
583
0
    flags = src[3];
584
0
    if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
585
0
        puts ("Error: Bad gzipped data\n");
586
0
        return -1;
587
0
    }
588
0
    if ((flags & EXTRA_FIELD) != 0) {
589
0
        if (srclen < 12) {
590
0
            goto toosmall;
591
0
        }
592
0
        i = 12 + src[10] + (src[11] << 8);
593
0
    }
594
0
    if ((flags & ORIG_NAME) != 0) {
595
0
        while (i < srclen && src[i++] != 0) {
596
            /* do nothing */
597
0
        }
598
0
    }
599
0
    if ((flags & COMMENT) != 0) {
600
0
        while (i < srclen && src[i++] != 0) {
601
            /* do nothing */
602
0
        }
603
0
    }
604
0
    if ((flags & HEAD_CRC) != 0) {
605
0
        i += 2;
606
0
    }
607
0
    if (i >= srclen) {
608
0
        goto toosmall;
609
0
    }
610
611
0
    s.zalloc = zalloc;
612
0
    s.zfree = zfree;
613
614
0
    r = inflateInit2(&s, -MAX_WBITS);
615
0
    if (r != Z_OK) {
616
0
        printf ("Error: inflateInit2() returned %d\n", r);
617
0
        return (-1);
618
0
    }
619
0
    s.next_in = src + i;
620
0
    s.avail_in = srclen - i;
621
0
    s.next_out = dst;
622
0
    s.avail_out = dstlen;
623
0
    r = inflate(&s, Z_FINISH);
624
0
    if (r != Z_OK && r != Z_STREAM_END) {
625
0
        printf ("Error: inflate() returned %d\n", r);
626
0
        inflateEnd(&s);
627
0
        return -1;
628
0
    }
629
0
    dstbytes = s.next_out - (unsigned char *) dst;
630
0
    inflateEnd(&s);
631
632
0
    return dstbytes;
633
634
0
toosmall:
635
0
    puts("Error: gunzip out of data in header\n");
636
0
    return -1;
637
0
}
638
639
/* Load a U-Boot image.  */
640
static ssize_t load_uboot_image(const char *filename, hwaddr *ep,
641
                                hwaddr *loadaddr, int *is_linux,
642
                                uint8_t image_type,
643
                                uint64_t (*translate_fn)(void *, uint64_t),
644
                                void *translate_opaque, AddressSpace *as)
645
0
{
646
0
    int fd;
647
0
    ssize_t size;
648
0
    hwaddr address;
649
0
    uboot_image_header_t h;
650
0
    uboot_image_header_t *hdr = &h;
651
0
    uint8_t *data = NULL;
652
0
    int ret = -1;
653
0
    int do_uncompress = 0;
654
655
0
    fd = open(filename, O_RDONLY | O_BINARY);
656
0
    if (fd < 0)
657
0
        return -1;
658
659
0
    size = read(fd, hdr, sizeof(uboot_image_header_t));
660
0
    if (size < sizeof(uboot_image_header_t)) {
661
0
        goto out;
662
0
    }
663
664
0
    bswap_uboot_header(hdr);
665
666
0
    if (hdr->ih_magic != IH_MAGIC)
667
0
        goto out;
668
669
0
    if (hdr->ih_type != image_type) {
670
0
        if (!(image_type == IH_TYPE_KERNEL &&
671
0
            hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
672
0
            fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
673
0
                    image_type);
674
0
            goto out;
675
0
        }
676
0
    }
677
678
    /* TODO: Implement other image types.  */
679
0
    switch (hdr->ih_type) {
680
0
    case IH_TYPE_KERNEL_NOLOAD:
681
0
        if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
682
0
            fprintf(stderr, "this image format (kernel_noload) cannot be "
683
0
                    "loaded on this machine type");
684
0
            goto out;
685
0
        }
686
687
0
        hdr->ih_load = *loadaddr + sizeof(*hdr);
688
0
        hdr->ih_ep += hdr->ih_load;
689
        /* fall through */
690
0
    case IH_TYPE_KERNEL:
691
0
        address = hdr->ih_load;
692
0
        if (translate_fn) {
693
0
            address = translate_fn(translate_opaque, address);
694
0
        }
695
0
        if (loadaddr) {
696
0
            *loadaddr = hdr->ih_load;
697
0
        }
698
699
0
        switch (hdr->ih_comp) {
700
0
        case IH_COMP_NONE:
701
0
            break;
702
0
        case IH_COMP_GZIP:
703
0
            do_uncompress = 1;
704
0
            break;
705
0
        default:
706
0
            fprintf(stderr,
707
0
                    "Unable to load u-boot images with compression type %d\n",
708
0
                    hdr->ih_comp);
709
0
            goto out;
710
0
        }
711
712
0
        if (ep) {
713
0
            *ep = hdr->ih_ep;
714
0
        }
715
716
        /* TODO: Check CPU type.  */
717
0
        if (is_linux) {
718
0
            if (hdr->ih_os == IH_OS_LINUX) {
719
0
                *is_linux = 1;
720
0
            } else if (hdr->ih_os == IH_OS_VXWORKS) {
721
                /*
722
                 * VxWorks 7 uses the same boot interface as the Linux kernel
723
                 * on Arm (64-bit only), PowerPC and RISC-V architectures.
724
                 */
725
0
                switch (hdr->ih_arch) {
726
0
                case IH_ARCH_ARM64:
727
0
                case IH_ARCH_PPC:
728
0
                case IH_ARCH_RISCV:
729
0
                    *is_linux = 1;
730
0
                    break;
731
0
                default:
732
0
                    *is_linux = 0;
733
0
                    break;
734
0
                }
735
0
            } else {
736
0
                *is_linux = 0;
737
0
            }
738
0
        }
739
740
0
        break;
741
0
    case IH_TYPE_RAMDISK:
742
0
        address = *loadaddr;
743
0
        break;
744
0
    default:
745
0
        fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
746
0
        goto out;
747
0
    }
748
749
0
    data = g_malloc(hdr->ih_size);
750
751
0
    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
752
0
        fprintf(stderr, "Error reading file\n");
753
0
        goto out;
754
0
    }
755
756
0
    if (do_uncompress) {
757
0
        uint8_t *compressed_data;
758
0
        size_t max_bytes;
759
0
        ssize_t bytes;
760
761
0
        compressed_data = data;
762
0
        max_bytes = UBOOT_MAX_DECOMPRESSED_BYTES;
763
0
        data = g_malloc(max_bytes);
764
765
0
        bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
766
0
        g_free(compressed_data);
767
0
        if (bytes < 0) {
768
0
            fprintf(stderr, "Unable to decompress gzipped image!\n");
769
0
            goto out;
770
0
        }
771
0
        hdr->ih_size = bytes;
772
0
    }
773
774
0
    rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
775
776
0
    ret = hdr->ih_size;
777
778
0
out:
779
0
    g_free(data);
780
0
    close(fd);
781
0
    return ret;
782
0
}
783
784
ssize_t load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
785
                    int *is_linux,
786
                    uint64_t (*translate_fn)(void *, uint64_t),
787
                    void *translate_opaque)
788
0
{
789
0
    return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
790
0
                            translate_fn, translate_opaque, NULL);
791
0
}
792
793
ssize_t load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
794
                       int *is_linux,
795
                       uint64_t (*translate_fn)(void *, uint64_t),
796
                       void *translate_opaque, AddressSpace *as)
797
0
{
798
0
    return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
799
0
                            translate_fn, translate_opaque, as);
800
0
}
801
802
/* Load a ramdisk.  */
803
ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
804
0
{
805
0
    return load_ramdisk_as(filename, addr, max_sz, NULL);
806
0
}
807
808
ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
809
                        AddressSpace *as)
810
0
{
811
0
    return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
812
0
                            NULL, NULL, as);
813
0
}
814
815
/* Load a gzip-compressed kernel to a dynamically allocated buffer. */
816
ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
817
                                  uint8_t **buffer)
818
0
{
819
0
    uint8_t *compressed_data = NULL;
820
0
    uint8_t *data = NULL;
821
0
    gsize len;
822
0
    ssize_t bytes;
823
0
    int ret = -1;
824
825
0
    if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
826
0
                             NULL)) {
827
0
        goto out;
828
0
    }
829
830
    /* Is it a gzip-compressed file? */
831
0
    if (len < 2 ||
832
0
        compressed_data[0] != 0x1f ||
833
0
        compressed_data[1] != 0x8b) {
834
0
        goto out;
835
0
    }
836
837
0
    if (max_sz > LOAD_IMAGE_MAX_DECOMPRESSED_BYTES) {
838
0
        max_sz = LOAD_IMAGE_MAX_DECOMPRESSED_BYTES;
839
0
    }
840
841
0
    data = g_malloc(max_sz);
842
0
    bytes = gunzip(data, max_sz, compressed_data, len);
843
0
    if (bytes < 0) {
844
0
        fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
845
0
                filename);
846
0
        goto out;
847
0
    }
848
849
    /* trim to actual size and return to caller */
850
0
    *buffer = g_realloc(data, bytes);
851
0
    ret = bytes;
852
    /* ownership has been transferred to caller */
853
0
    data = NULL;
854
855
0
 out:
856
0
    g_free(compressed_data);
857
0
    g_free(data);
858
0
    return ret;
859
0
}
860
861
862
/* The PE/COFF MS-DOS stub magic number */
863
0
#define EFI_PE_MSDOS_MAGIC        "MZ"
864
865
/*
866
 * The Linux header magic number for a EFI PE/COFF
867
 * image targeting an unspecified architecture.
868
 */
869
0
#define EFI_PE_LINUX_MAGIC        "\xcd\x23\x82\x81"
870
871
/*
872
 * Bootable Linux kernel images may be packaged as EFI zboot images, which are
873
 * self-decompressing executables when loaded via EFI. The compressed payload
874
 * can also be extracted from the image and decompressed by a non-EFI loader.
875
 *
876
 * The de facto specification for this format is at the following URL:
877
 *
878
 * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/firmware/efi/libstub/zboot-header.S
879
 *
880
 * This definition is based on Linux upstream commit 29636a5ce87beba.
881
 */
882
struct linux_efi_zboot_header {
883
    uint8_t     msdos_magic[2];         /* PE/COFF 'MZ' magic number */
884
    uint8_t     reserved0[2];
885
    uint8_t     zimg[4];                /* "zimg" for Linux EFI zboot images */
886
    uint32_t    payload_offset;         /* LE offset to compressed payload */
887
    uint32_t    payload_size;           /* LE size of the compressed payload */
888
    uint8_t     reserved1[8];
889
    char        compression_type[32];   /* Compression type, NUL terminated */
890
    uint8_t     linux_magic[4];         /* Linux header magic */
891
    uint32_t    pe_header_offset;       /* LE offset to the PE header */
892
};
893
894
/*
895
 * Check whether *buffer points to a Linux EFI zboot image in memory.
896
 *
897
 * If it does, attempt to decompress it to a new buffer, and free the old one.
898
 * If any of this fails, return an error to the caller.
899
 *
900
 * If the image is not a Linux EFI zboot image, do nothing and return success.
901
 */
902
ssize_t unpack_efi_zboot_image(uint8_t **buffer, ssize_t *size)
903
0
{
904
0
    const size_t max_bytes = LOAD_IMAGE_MAX_DECOMPRESSED_BYTES;
905
0
    const struct linux_efi_zboot_header *header;
906
0
    g_autofree uint8_t *data = NULL;
907
0
    ssize_t ploff, plsize;
908
0
    ssize_t bytes;
909
910
    /* ignore if this is too small to be a EFI zboot image */
911
0
    if (*size < sizeof(*header)) {
912
0
        return 0;
913
0
    }
914
915
0
    header = (struct linux_efi_zboot_header *)*buffer;
916
917
    /* ignore if this is not a Linux EFI zboot image */
918
0
    if (memcmp(&header->msdos_magic, EFI_PE_MSDOS_MAGIC, 2) != 0 ||
919
0
        memcmp(&header->zimg, "zimg", 4) != 0 ||
920
0
        memcmp(&header->linux_magic, EFI_PE_LINUX_MAGIC, 4) != 0) {
921
0
        return 0;
922
0
    }
923
924
0
    ploff = ldl_le_p(&header->payload_offset);
925
0
    plsize = ldl_le_p(&header->payload_size);
926
927
0
    if (ploff < 0 || plsize < 0 || ploff + plsize > *size) {
928
0
        fprintf(stderr, "unable to handle corrupt EFI zboot image\n");
929
0
        return -1;
930
0
    }
931
932
0
    data = g_malloc(max_bytes);
933
934
0
    if (strcmp(header->compression_type, "gzip") == 0) {
935
0
        bytes = gunzip(data, max_bytes, *buffer + ploff, plsize);
936
#ifdef CONFIG_ZSTD
937
    } else if (strcmp(header->compression_type, "zstd") == 0) {
938
        size_t ret = ZSTD_decompress(data, max_bytes, *buffer + ploff, plsize);
939
        bytes = ZSTD_isError(ret) ? -1 : (ssize_t) ret;
940
#endif
941
0
    } else {
942
0
        fprintf(stderr,
943
0
                "unable to handle EFI zboot image with \"%.*s\" compression\n",
944
0
                (int)sizeof(header->compression_type) - 1,
945
0
                header->compression_type);
946
0
        return -1;
947
0
    }
948
949
0
    if (bytes < 0) {
950
0
        fprintf(stderr, "failed to decompress EFI zboot image\n");
951
0
        return -1;
952
0
    }
953
954
0
    g_free(*buffer);
955
0
    *buffer = g_realloc(g_steal_pointer(&data), bytes);
956
0
    *size = bytes;
957
0
    return bytes;
958
0
}
959
960
/*
961
 * Functions for reboot-persistent memory regions.
962
 *  - used for vga bios and option roms.
963
 *  - also linux kernel (-kernel / -initrd).
964
 */
965
966
typedef struct Rom Rom;
967
968
struct Rom {
969
    char *name;
970
    char *path;
971
972
    /* datasize is the amount of memory allocated in "data". If datasize is less
973
     * than romsize, it means that the area from datasize to romsize is filled
974
     * with zeros.
975
     */
976
    size_t romsize;
977
    size_t datasize;
978
979
    uint8_t *data;
980
    MemoryRegion *mr;
981
    AddressSpace *as;
982
    int isrom;
983
    char *fw_dir;
984
    char *fw_file;
985
    GMappedFile *mapped_file;
986
987
    bool committed;
988
989
    hwaddr addr;
990
    QTAILQ_ENTRY(Rom) next;
991
};
992
993
static FWCfgState *fw_cfg;
994
static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
995
996
/*
997
 * rom->data can be heap-allocated or memory-mapped (e.g. when added with
998
 * rom_add_elf_program())
999
 */
1000
static void rom_free_data(Rom *rom)
1001
61
{
1002
61
    if (rom->mapped_file) {
1003
0
        g_mapped_file_unref(rom->mapped_file);
1004
0
        rom->mapped_file = NULL;
1005
61
    } else {
1006
61
        g_free(rom->data);
1007
61
    }
1008
1009
61
    rom->data = NULL;
1010
61
}
1011
1012
static void rom_free(Rom *rom)
1013
0
{
1014
0
    rom_free_data(rom);
1015
0
    g_free(rom->path);
1016
0
    g_free(rom->name);
1017
0
    g_free(rom->fw_dir);
1018
0
    g_free(rom->fw_file);
1019
0
    g_free(rom);
1020
0
}
1021
1022
static inline bool rom_order_compare(Rom *rom, Rom *item)
1023
610
{
1024
610
    return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
1025
610
           (rom->as == item->as && rom->addr >= item->addr);
1026
610
}
1027
1028
static void rom_insert(Rom *rom)
1029
305
{
1030
305
    Rom *item;
1031
1032
305
    if (roms_loaded) {
1033
0
        hw_error ("ROM images must be loaded at startup\n");
1034
0
    }
1035
1036
    /* The user didn't specify an address space, this is the default */
1037
305
    if (!rom->as) {
1038
305
        rom->as = &address_space_memory;
1039
305
    }
1040
1041
305
    rom->committed = false;
1042
1043
    /* List is ordered by load address in the same address space */
1044
610
    QTAILQ_FOREACH(item, &roms, next) {
1045
610
        if (rom_order_compare(rom, item)) {
1046
549
            continue;
1047
549
        }
1048
61
        QTAILQ_INSERT_BEFORE(item, rom, next);
1049
61
        return;
1050
610
    }
1051
244
    QTAILQ_INSERT_TAIL(&roms, rom, next);
1052
244
}
1053
1054
static void fw_cfg_resized(const char *id, uint64_t length, void *host)
1055
0
{
1056
0
    if (fw_cfg) {
1057
0
        fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
1058
0
    }
1059
0
}
1060
1061
static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
1062
183
{
1063
183
    void *data;
1064
1065
183
    rom->mr = g_malloc(sizeof(*rom->mr));
1066
183
    memory_region_init_resizeable_ram(rom->mr, owner, name,
1067
183
                                      rom->datasize, rom->romsize,
1068
183
                                      fw_cfg_resized,
1069
183
                                      &error_fatal);
1070
183
    memory_region_set_readonly(rom->mr, ro);
1071
183
    vmstate_register_ram_global(rom->mr);
1072
1073
183
    data = memory_region_get_ram_ptr(rom->mr);
1074
183
    if (!cpr_is_incoming()) {
1075
183
        memcpy(data, rom->data, rom->datasize);
1076
183
    }
1077
1078
183
    return data;
1079
183
}
1080
1081
ssize_t rom_add_file(const char *file, const char *fw_dir,
1082
                     hwaddr addr, int32_t bootindex,
1083
                     bool has_option_rom, MemoryRegion *mr,
1084
                     AddressSpace *as)
1085
122
{
1086
122
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1087
122
    Rom *rom;
1088
122
    gsize size;
1089
122
    g_autoptr(GError) gerr = NULL;
1090
122
    char devpath[100];
1091
1092
122
    if (as && mr) {
1093
0
        fprintf(stderr, "Specifying an Address Space and Memory Region is " \
1094
0
                "not valid when loading a rom\n");
1095
        /* We haven't allocated anything so we don't need any cleanup */
1096
0
        return -1;
1097
0
    }
1098
1099
122
    rom = g_malloc0(sizeof(*rom));
1100
122
    rom->name = g_strdup(file);
1101
122
    rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
1102
122
    rom->as = as;
1103
122
    if (rom->path == NULL) {
1104
0
        rom->path = g_strdup(file);
1105
0
    }
1106
1107
122
    if (!g_file_get_contents(rom->path, (gchar **) &rom->data,
1108
122
                             &size, &gerr)) {
1109
0
        fprintf(stderr, "rom: file %-20s: error %s\n",
1110
0
                rom->name, gerr->message);
1111
0
        goto err;
1112
0
    }
1113
1114
122
    if (fw_dir) {
1115
61
        rom->fw_dir  = g_strdup(fw_dir);
1116
61
        rom->fw_file = g_strdup(file);
1117
61
    }
1118
122
    rom->addr     = addr;
1119
122
    rom->romsize  = size;
1120
122
    rom->datasize = rom->romsize;
1121
122
    rom_insert(rom);
1122
122
    if (rom->fw_file && fw_cfg) {
1123
61
        const char *basename;
1124
61
        char fw_file_name[FW_CFG_MAX_FILE_PATH];
1125
61
        void *data;
1126
1127
61
        basename = strrchr(rom->fw_file, '/');
1128
61
        if (basename) {
1129
0
            basename++;
1130
61
        } else {
1131
61
            basename = rom->fw_file;
1132
61
        }
1133
61
        snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
1134
61
                 basename);
1135
61
        snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
1136
1137
61
        if ((!has_option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
1138
0
            data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
1139
61
        } else {
1140
61
            data = rom->data;
1141
61
        }
1142
1143
61
        fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
1144
61
    } else {
1145
61
        if (mr) {
1146
0
            rom->mr = mr;
1147
0
            snprintf(devpath, sizeof(devpath), "/rom@%s", file);
1148
61
        } else {
1149
61
            snprintf(devpath, sizeof(devpath), "/rom@" HWADDR_FMT_plx, addr);
1150
61
        }
1151
61
    }
1152
1153
122
    add_boot_device_path(bootindex, NULL, devpath);
1154
122
    return 0;
1155
1156
0
err:
1157
0
    rom_free(rom);
1158
0
    return -1;
1159
122
}
1160
1161
MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
1162
                   size_t max_len, hwaddr addr, const char *fw_file_name,
1163
                   FWCfgCallback fw_callback, void *callback_opaque,
1164
                   AddressSpace *as, bool read_only)
1165
183
{
1166
183
    MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
1167
183
    Rom *rom;
1168
183
    MemoryRegion *mr = NULL;
1169
1170
183
    rom           = g_malloc0(sizeof(*rom));
1171
183
    rom->name     = g_strdup(name);
1172
183
    rom->as       = as;
1173
183
    rom->addr     = addr;
1174
183
    rom->romsize  = max_len ? max_len : len;
1175
183
    rom->datasize = len;
1176
183
    g_assert(rom->romsize >= rom->datasize);
1177
183
    rom->data     = g_malloc0(rom->datasize);
1178
183
    memcpy(rom->data, blob, len);
1179
183
    rom_insert(rom);
1180
183
    if (fw_file_name && fw_cfg) {
1181
183
        char devpath[100];
1182
183
        void *data;
1183
1184
183
        if (read_only) {
1185
183
            snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
1186
183
        } else {
1187
0
            snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
1188
0
        }
1189
1190
183
        if (mc->rom_file_has_mr) {
1191
183
            data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
1192
183
            mr = rom->mr;
1193
183
        } else {
1194
0
            data = rom->data;
1195
0
        }
1196
1197
183
        fw_cfg_add_file_callback(fw_cfg, fw_file_name,
1198
183
                                 fw_callback, NULL, callback_opaque,
1199
183
                                 data, rom->datasize, read_only);
1200
183
    }
1201
183
    return mr;
1202
183
}
1203
1204
/* This function is specific for elf program because we don't need to allocate
1205
 * all the rom. We just allocate the first part and the rest is just zeros. This
1206
 * is why romsize and datasize are different. Also, this function takes its own
1207
 * reference to "mapped_file", so we don't have to allocate and copy the buffer.
1208
 */
1209
int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
1210
                        size_t datasize, size_t romsize, hwaddr addr,
1211
                        AddressSpace *as)
1212
0
{
1213
0
    Rom *rom;
1214
1215
0
    rom           = g_malloc0(sizeof(*rom));
1216
0
    rom->name     = g_strdup(name);
1217
0
    rom->addr     = addr;
1218
0
    rom->datasize = datasize;
1219
0
    rom->romsize  = romsize;
1220
0
    rom->data     = data;
1221
0
    rom->as       = as;
1222
1223
0
    if (mapped_file && data) {
1224
0
        g_mapped_file_ref(mapped_file);
1225
0
        rom->mapped_file = mapped_file;
1226
0
    }
1227
1228
0
    rom_insert(rom);
1229
0
    return 0;
1230
0
}
1231
1232
ssize_t rom_add_vga(const char *file)
1233
0
{
1234
0
    return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
1235
0
}
1236
1237
ssize_t rom_add_option(const char *file, int32_t bootindex)
1238
61
{
1239
61
    return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
1240
61
}
1241
1242
static void rom_reset(void *unused)
1243
92.8k
{
1244
92.8k
    Rom *rom;
1245
1246
464k
    QTAILQ_FOREACH(rom, &roms, next) {
1247
464k
        if (rom->fw_file) {
1248
92.8k
            continue;
1249
92.8k
        }
1250
        /*
1251
         * We don't need to fill in the RAM with ROM data because we'll fill
1252
         * the data in during the next incoming migration in all cases.  Note
1253
         * that some of those RAMs can actually be modified by the guest.
1254
         */
1255
371k
        if (runstate_check(RUN_STATE_INMIGRATE)) {
1256
0
            if (rom->data && rom->isrom) {
1257
                /*
1258
                 * Free it so that a rom_reset after migration doesn't
1259
                 * overwrite a potentially modified 'rom'.
1260
                 */
1261
0
                rom_free_data(rom);
1262
0
            }
1263
0
            continue;
1264
0
        }
1265
1266
371k
        if (rom->data == NULL) {
1267
92.7k
            continue;
1268
92.7k
        }
1269
278k
        if (rom->mr) {
1270
278k
            void *host = memory_region_get_ram_ptr(rom->mr);
1271
278k
            memcpy(host, rom->data, rom->datasize);
1272
278k
            memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
1273
278k
        } else {
1274
61
            address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
1275
61
                                    rom->data, rom->datasize);
1276
61
            address_space_set(rom->as, rom->addr + rom->datasize, 0,
1277
61
                              rom->romsize - rom->datasize,
1278
61
                              MEMTXATTRS_UNSPECIFIED);
1279
61
        }
1280
278k
        if (rom->isrom) {
1281
            /* rom needs to be written only once */
1282
61
            rom_free_data(rom);
1283
61
        }
1284
        /*
1285
         * The rom loader is really on the same level as firmware in the guest
1286
         * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
1287
         * that the instruction cache for that new region is clear, so that the
1288
         * CPU definitely fetches its instructions from the just written data.
1289
         */
1290
278k
        address_space_flush_icache_range(rom->as, rom->addr, rom->datasize);
1291
1292
278k
        trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom);
1293
278k
    }
1294
92.8k
}
1295
1296
/* Return true if two consecutive ROMs in the ROM list overlap */
1297
static bool roms_overlap(Rom *last_rom, Rom *this_rom)
1298
61
{
1299
61
    if (!last_rom) {
1300
61
        return false;
1301
61
    }
1302
0
    return last_rom->as == this_rom->as &&
1303
0
        last_rom->addr + last_rom->romsize > this_rom->addr;
1304
61
}
1305
1306
static const char *rom_as_name(Rom *rom)
1307
0
{
1308
0
    const char *name = rom->as ? rom->as->name : NULL;
1309
0
    return name ?: "anonymous";
1310
0
}
1311
1312
static void rom_print_overlap_error_header(void)
1313
0
{
1314
0
    error_report("Some ROM regions are overlapping");
1315
0
    error_printf(
1316
0
        "These ROM regions might have been loaded by "
1317
0
        "direct user request or by default.\n"
1318
0
        "They could be BIOS/firmware images, a guest kernel, "
1319
0
        "initrd or some other file loaded into guest memory.\n"
1320
0
        "Check whether you intended to load all this guest code, and "
1321
0
        "whether it has been built to load to the correct addresses.\n");
1322
0
}
1323
1324
static void rom_print_one_overlap_error(Rom *last_rom, Rom *rom)
1325
0
{
1326
0
    error_printf(
1327
0
        "\nThe following two regions overlap (in the %s address space):\n",
1328
0
        rom_as_name(rom));
1329
0
    error_printf(
1330
0
        "  %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
1331
0
        last_rom->name, last_rom->addr, last_rom->addr + last_rom->romsize);
1332
0
    error_printf(
1333
0
        "  %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
1334
0
        rom->name, rom->addr, rom->addr + rom->romsize);
1335
0
}
1336
1337
int rom_check_and_register_reset(void)
1338
61
{
1339
61
    MemoryRegionSection section;
1340
61
    Rom *rom, *last_rom = NULL;
1341
61
    bool found_overlap = false;
1342
1343
305
    QTAILQ_FOREACH(rom, &roms, next) {
1344
305
        if (rom->fw_file) {
1345
61
            continue;
1346
61
        }
1347
244
        if (!rom->mr) {
1348
61
            if (roms_overlap(last_rom, rom)) {
1349
0
                if (!found_overlap) {
1350
0
                    found_overlap = true;
1351
0
                    rom_print_overlap_error_header();
1352
0
                }
1353
0
                rom_print_one_overlap_error(last_rom, rom);
1354
                /* Keep going through the list so we report all overlaps */
1355
0
            }
1356
61
            last_rom = rom;
1357
61
        }
1358
244
        section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
1359
244
                                     rom->addr, 1);
1360
244
        rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
1361
244
        memory_region_unref(section.mr);
1362
244
    }
1363
61
    if (found_overlap) {
1364
0
        return -1;
1365
0
    }
1366
1367
61
    qemu_register_reset(rom_reset, NULL);
1368
61
    roms_loaded = 1;
1369
61
    return 0;
1370
61
}
1371
1372
void rom_set_fw(FWCfgState *f)
1373
61
{
1374
61
    fw_cfg = f;
1375
61
}
1376
1377
void rom_transaction_begin(void)
1378
0
{
1379
0
    Rom *rom;
1380
1381
    /* Ignore ROMs added without the transaction API */
1382
0
    QTAILQ_FOREACH(rom, &roms, next) {
1383
0
        rom->committed = true;
1384
0
    }
1385
0
}
1386
1387
void rom_transaction_end(bool commit)
1388
0
{
1389
0
    Rom *rom;
1390
0
    Rom *tmp;
1391
1392
0
    QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
1393
0
        if (rom->committed) {
1394
0
            continue;
1395
0
        }
1396
0
        if (commit) {
1397
0
            rom->committed = true;
1398
0
        } else {
1399
0
            QTAILQ_REMOVE(&roms, rom, next);
1400
0
            rom_free(rom);
1401
0
        }
1402
0
    }
1403
0
}
1404
1405
static Rom *find_rom(hwaddr addr, size_t size)
1406
0
{
1407
0
    Rom *rom;
1408
1409
0
    QTAILQ_FOREACH(rom, &roms, next) {
1410
0
        if (rom->fw_file) {
1411
0
            continue;
1412
0
        }
1413
0
        if (rom->mr) {
1414
0
            continue;
1415
0
        }
1416
0
        if (rom->addr > addr) {
1417
0
            continue;
1418
0
        }
1419
0
        if (rom->addr + rom->romsize < addr + size) {
1420
0
            continue;
1421
0
        }
1422
0
        return rom;
1423
0
    }
1424
0
    return NULL;
1425
0
}
1426
1427
typedef struct RomSec {
1428
    hwaddr base;
1429
    int se; /* start/end flag */
1430
} RomSec;
1431
1432
1433
/*
1434
 * Sort into address order. We break ties between rom-startpoints
1435
 * and rom-endpoints in favour of the startpoint, by sorting the 0->1
1436
 * transition before the 1->0 transition. Either way round would
1437
 * work, but this way saves a little work later by avoiding
1438
 * dealing with "gaps" of 0 length.
1439
 */
1440
static gint sort_secs(gconstpointer a, gconstpointer b, gpointer d)
1441
0
{
1442
0
    RomSec *ra = (RomSec *) a;
1443
0
    RomSec *rb = (RomSec *) b;
1444
1445
0
    if (ra->base == rb->base) {
1446
0
        return ra->se - rb->se;
1447
0
    }
1448
0
    return ra->base > rb->base ? 1 : -1;
1449
0
}
1450
1451
static GList *add_romsec_to_list(GList *secs, hwaddr base, int se)
1452
0
{
1453
0
   RomSec *cand = g_new(RomSec, 1);
1454
0
   cand->base = base;
1455
0
   cand->se = se;
1456
0
   return g_list_prepend(secs, cand);
1457
0
}
1458
1459
RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
1460
0
{
1461
0
    Rom *rom;
1462
0
    RomSec *cand;
1463
0
    RomGap res = {0, 0};
1464
0
    hwaddr gapstart = base;
1465
0
    GList *it, *secs = NULL;
1466
0
    int count = 0;
1467
1468
0
    QTAILQ_FOREACH(rom, &roms, next) {
1469
        /* Ignore blobs being loaded to special places */
1470
0
        if (rom->mr || rom->fw_file) {
1471
0
            continue;
1472
0
        }
1473
        /* ignore anything finishing below base */
1474
0
        if (rom->addr + rom->romsize <= base) {
1475
0
            continue;
1476
0
        }
1477
        /* ignore anything starting above the region */
1478
0
        if (rom->addr >= base + size) {
1479
0
            continue;
1480
0
        }
1481
1482
        /* Save the start and end of each relevant ROM */
1483
0
        secs = add_romsec_to_list(secs, rom->addr, 1);
1484
1485
0
        if (rom->addr + rom->romsize < base + size) {
1486
0
            secs = add_romsec_to_list(secs, rom->addr + rom->romsize, -1);
1487
0
        }
1488
0
    }
1489
1490
    /* sentinel */
1491
0
    secs = add_romsec_to_list(secs, base + size, 1);
1492
1493
0
    secs = g_list_sort_with_data(secs, sort_secs, NULL);
1494
1495
0
    for (it = g_list_first(secs); it; it = g_list_next(it)) {
1496
0
        cand = (RomSec *) it->data;
1497
0
        if (count == 0 && count + cand->se == 1) {
1498
0
            size_t gap = cand->base - gapstart;
1499
0
            if (gap > res.size) {
1500
0
                res.base = gapstart;
1501
0
                res.size = gap;
1502
0
            }
1503
0
        } else if (count == 1 && count + cand->se == 0) {
1504
0
            gapstart = cand->base;
1505
0
        }
1506
0
        count += cand->se;
1507
0
    }
1508
1509
0
    g_list_free_full(secs, g_free);
1510
0
    return res;
1511
0
}
1512
1513
/*
1514
 * Copies memory from registered ROMs to dest. Any memory that is contained in
1515
 * a ROM between addr and addr + size is copied. Note that this can involve
1516
 * multiple ROMs, which need not start at addr and need not end at addr + size.
1517
 */
1518
int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
1519
0
{
1520
0
    hwaddr end = addr + size;
1521
0
    uint8_t *s, *d = dest;
1522
0
    size_t l = 0;
1523
0
    Rom *rom;
1524
1525
0
    QTAILQ_FOREACH(rom, &roms, next) {
1526
0
        if (rom->fw_file) {
1527
0
            continue;
1528
0
        }
1529
0
        if (rom->mr) {
1530
0
            continue;
1531
0
        }
1532
0
        if (rom->addr + rom->romsize < addr) {
1533
0
            continue;
1534
0
        }
1535
0
        if (rom->addr > end || rom->addr < addr) {
1536
0
            break;
1537
0
        }
1538
1539
0
        d = dest + (rom->addr - addr);
1540
0
        s = rom->data;
1541
0
        l = rom->datasize;
1542
1543
0
        if ((d + l) > (dest + size)) {
1544
0
            l = dest - d;
1545
0
        }
1546
1547
0
        if (l > 0) {
1548
0
            memcpy(d, s, l);
1549
0
        }
1550
1551
0
        if (rom->romsize > rom->datasize) {
1552
            /* If datasize is less than romsize, it means that we didn't
1553
             * allocate all the ROM because the trailing data are only zeros.
1554
             */
1555
1556
0
            d += l;
1557
0
            l = rom->romsize - rom->datasize;
1558
1559
0
            if ((d + l) > (dest + size)) {
1560
                /* Rom size doesn't fit in the destination area. Adjust to avoid
1561
                 * overflow.
1562
                 */
1563
0
                l = dest - d;
1564
0
            }
1565
1566
0
            if (l > 0) {
1567
0
                memset(d, 0x0, l);
1568
0
            }
1569
0
        }
1570
0
    }
1571
1572
0
    return (d + l) - dest;
1573
0
}
1574
1575
void *rom_ptr(hwaddr addr, size_t size)
1576
0
{
1577
0
    Rom *rom;
1578
1579
0
    rom = find_rom(addr, size);
1580
0
    if (!rom || !rom->data)
1581
0
        return NULL;
1582
0
    return rom->data + (addr - rom->addr);
1583
0
}
1584
1585
typedef struct FindRomCBData {
1586
    size_t size; /* Amount of data we want from ROM, in bytes */
1587
    MemoryRegion *mr; /* MR at the unaliased guest addr */
1588
    hwaddr xlat; /* Offset of addr within mr */
1589
    void *rom; /* Output: rom data pointer, if found */
1590
} FindRomCBData;
1591
1592
static bool find_rom_cb(Int128 start, Int128 len, const MemoryRegion *mr,
1593
                        hwaddr offset_in_region, void *opaque)
1594
0
{
1595
0
    FindRomCBData *cbdata = opaque;
1596
0
    hwaddr alias_addr;
1597
1598
0
    if (mr != cbdata->mr) {
1599
0
        return false;
1600
0
    }
1601
1602
0
    alias_addr = int128_get64(start) + cbdata->xlat - offset_in_region;
1603
0
    cbdata->rom = rom_ptr(alias_addr, cbdata->size);
1604
0
    if (!cbdata->rom) {
1605
0
        return false;
1606
0
    }
1607
    /* Found a match, stop iterating */
1608
0
    return true;
1609
0
}
1610
1611
void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size)
1612
0
{
1613
    /*
1614
     * Find any ROM data for the given guest address range.  If there
1615
     * is a ROM blob then return a pointer to the host memory
1616
     * corresponding to 'addr'; otherwise return NULL.
1617
     *
1618
     * We look not only for ROM blobs that were loaded directly to
1619
     * addr, but also for ROM blobs that were loaded to aliases of
1620
     * that memory at other addresses within the AddressSpace.
1621
     *
1622
     * Note that we do not check @as against the 'as' member in the
1623
     * 'struct Rom' returned by rom_ptr(). The Rom::as is the
1624
     * AddressSpace which the rom blob should be written to, whereas
1625
     * our @as argument is the AddressSpace which we are (effectively)
1626
     * reading from, and the same underlying RAM will often be visible
1627
     * in multiple AddressSpaces. (A common example is a ROM blob
1628
     * written to the 'system' address space but then read back via a
1629
     * CPU's cpu->as pointer.) This does mean we might potentially
1630
     * return a false-positive match if a ROM blob was loaded into an
1631
     * AS which is entirely separate and distinct from the one we're
1632
     * querying, but this issue exists also for rom_ptr() and hasn't
1633
     * caused any problems in practice.
1634
     */
1635
0
    FlatView *fv;
1636
0
    void *rom;
1637
0
    hwaddr len_unused;
1638
0
    FindRomCBData cbdata = {};
1639
1640
    /* Easy case: there's data at the actual address */
1641
0
    rom = rom_ptr(addr, size);
1642
0
    if (rom) {
1643
0
        return rom;
1644
0
    }
1645
1646
0
    RCU_READ_LOCK_GUARD();
1647
1648
0
    fv = address_space_to_flatview(as);
1649
0
    cbdata.mr = flatview_translate(fv, addr, &cbdata.xlat, &len_unused,
1650
0
                                   false, MEMTXATTRS_UNSPECIFIED);
1651
0
    if (!cbdata.mr) {
1652
        /* Nothing at this address, so there can't be any aliasing */
1653
0
        return NULL;
1654
0
    }
1655
0
    cbdata.size = size;
1656
0
    flatview_for_each_range(fv, find_rom_cb, &cbdata);
1657
0
    return cbdata.rom;
1658
0
}
1659
1660
HumanReadableText *qmp_x_query_roms(Error **errp)
1661
0
{
1662
0
    Rom *rom;
1663
0
    g_autoptr(GString) buf = g_string_new("");
1664
1665
0
    QTAILQ_FOREACH(rom, &roms, next) {
1666
0
        if (rom->mr) {
1667
0
            g_string_append_printf(buf, "%s"
1668
0
                                   " size=0x%06zx name=\"%s\"\n",
1669
0
                                   memory_region_name(rom->mr),
1670
0
                                   rom->romsize,
1671
0
                                   rom->name);
1672
0
        } else if (!rom->fw_file) {
1673
0
            g_string_append_printf(buf, "addr=" HWADDR_FMT_plx
1674
0
                                   " size=0x%06zx mem=%s name=\"%s\"\n",
1675
0
                                   rom->addr, rom->romsize,
1676
0
                                   rom->isrom ? "rom" : "ram",
1677
0
                                   rom->name);
1678
0
        } else {
1679
0
            g_string_append_printf(buf, "fw=%s/%s"
1680
0
                                   " size=0x%06zx name=\"%s\"\n",
1681
0
                                   rom->fw_dir,
1682
0
                                   rom->fw_file,
1683
0
                                   rom->romsize,
1684
0
                                   rom->name);
1685
0
        }
1686
0
    }
1687
1688
0
    return human_readable_text_from_str(buf);
1689
0
}
1690
1691
typedef enum HexRecord HexRecord;
1692
enum HexRecord {
1693
    DATA_RECORD = 0,
1694
    EOF_RECORD,
1695
    EXT_SEG_ADDR_RECORD,
1696
    START_SEG_ADDR_RECORD,
1697
    EXT_LINEAR_ADDR_RECORD,
1698
    START_LINEAR_ADDR_RECORD,
1699
};
1700
1701
/* Each record contains a 16-bit address which is combined with the upper 16
1702
 * bits of the implicit "next address" to form a 32-bit address.
1703
 */
1704
0
#define NEXT_ADDR_MASK 0xffff0000
1705
1706
#define DATA_FIELD_MAX_LEN 0xff
1707
0
#define LEN_EXCEPT_DATA 0x5
1708
/* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
1709
 *       sizeof(checksum) */
1710
typedef struct {
1711
    uint8_t byte_count;
1712
    uint16_t address;
1713
    uint8_t record_type;
1714
    uint8_t data[DATA_FIELD_MAX_LEN];
1715
    uint8_t checksum;
1716
} HexLine;
1717
1718
/* return 0 or -1 if error */
1719
static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
1720
                         uint32_t *index, const bool in_process)
1721
0
{
1722
    /* +-------+---------------+-------+---------------------+--------+
1723
     * | byte  |               |record |                     |        |
1724
     * | count |    address    | type  |        data         |checksum|
1725
     * +-------+---------------+-------+---------------------+--------+
1726
     * ^       ^               ^       ^                     ^        ^
1727
     * |1 byte |    2 bytes    |1 byte |     0-255 bytes     | 1 byte |
1728
     */
1729
0
    uint8_t value = 0;
1730
0
    uint32_t idx = *index;
1731
    /* ignore space */
1732
0
    if (g_ascii_isspace(c)) {
1733
0
        return true;
1734
0
    }
1735
0
    if (!g_ascii_isxdigit(c) || !in_process) {
1736
0
        return false;
1737
0
    }
1738
0
    value = g_ascii_xdigit_value(c);
1739
0
    value = (idx & 0x1) ? (value & 0xf) : (value << 4);
1740
0
    if (idx < 2) {
1741
0
        line->byte_count |= value;
1742
0
    } else if (2 <= idx && idx < 6) {
1743
0
        line->address <<= 4;
1744
0
        line->address += g_ascii_xdigit_value(c);
1745
0
    } else if (6 <= idx && idx < 8) {
1746
0
        line->record_type |= value;
1747
0
    } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
1748
0
        line->data[(idx - 8) >> 1] |= value;
1749
0
    } else if (8 + 2 * line->byte_count <= idx &&
1750
0
               idx < 10 + 2 * line->byte_count) {
1751
0
        line->checksum |= value;
1752
0
    } else {
1753
0
        return false;
1754
0
    }
1755
0
    *our_checksum += value;
1756
0
    ++(*index);
1757
0
    return true;
1758
0
}
1759
1760
typedef struct {
1761
    const char *filename;
1762
    HexLine line;
1763
    uint8_t *bin_buf;
1764
    hwaddr *start_addr;
1765
    int total_size;
1766
    uint32_t next_address_to_write;
1767
    uint32_t current_address;
1768
    uint32_t current_rom_index;
1769
    uint32_t rom_start_address;
1770
    AddressSpace *as;
1771
    bool complete;
1772
} HexParser;
1773
1774
/* return size or -1 if error */
1775
static int handle_record_type(HexParser *parser)
1776
0
{
1777
0
    HexLine *line = &(parser->line);
1778
0
    switch (line->record_type) {
1779
0
    case DATA_RECORD:
1780
0
        parser->current_address =
1781
0
            (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
1782
        /* verify this is a contiguous block of memory */
1783
0
        if (parser->current_address != parser->next_address_to_write) {
1784
0
            if (parser->current_rom_index != 0) {
1785
0
                rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1786
0
                                      parser->current_rom_index,
1787
0
                                      parser->rom_start_address, parser->as);
1788
0
            }
1789
0
            parser->rom_start_address = parser->current_address;
1790
0
            parser->current_rom_index = 0;
1791
0
        }
1792
1793
        /* copy from line buffer to output bin_buf */
1794
0
        memcpy(parser->bin_buf + parser->current_rom_index, line->data,
1795
0
               line->byte_count);
1796
0
        parser->current_rom_index += line->byte_count;
1797
0
        parser->total_size += line->byte_count;
1798
        /* save next address to write */
1799
0
        parser->next_address_to_write =
1800
0
            parser->current_address + line->byte_count;
1801
0
        break;
1802
1803
0
    case EOF_RECORD:
1804
0
        if (parser->current_rom_index != 0) {
1805
0
            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1806
0
                                  parser->current_rom_index,
1807
0
                                  parser->rom_start_address, parser->as);
1808
0
        }
1809
0
        parser->complete = true;
1810
0
        return parser->total_size;
1811
0
    case EXT_SEG_ADDR_RECORD:
1812
0
    case EXT_LINEAR_ADDR_RECORD:
1813
0
        if (line->byte_count != 2 && line->address != 0) {
1814
0
            return -1;
1815
0
        }
1816
1817
0
        if (parser->current_rom_index != 0) {
1818
0
            rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
1819
0
                                  parser->current_rom_index,
1820
0
                                  parser->rom_start_address, parser->as);
1821
0
        }
1822
1823
        /* save next address to write,
1824
         * in case of non-contiguous block of memory */
1825
0
        parser->next_address_to_write = (line->data[0] << 12) |
1826
0
                                        (line->data[1] << 4);
1827
0
        if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
1828
0
            parser->next_address_to_write <<= 12;
1829
0
        }
1830
1831
0
        parser->rom_start_address = parser->next_address_to_write;
1832
0
        parser->current_rom_index = 0;
1833
0
        break;
1834
1835
0
    case START_SEG_ADDR_RECORD:
1836
0
        if (line->byte_count != 4 && line->address != 0) {
1837
0
            return -1;
1838
0
        }
1839
1840
        /* x86 16-bit CS:IP segmented addressing */
1841
0
        *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
1842
0
                                ((line->data[2] << 8) | line->data[3]);
1843
0
        break;
1844
1845
0
    case START_LINEAR_ADDR_RECORD:
1846
0
        if (line->byte_count != 4 && line->address != 0) {
1847
0
            return -1;
1848
0
        }
1849
1850
0
        *(parser->start_addr) = ldl_be_p(line->data);
1851
0
        break;
1852
1853
0
    default:
1854
0
        return -1;
1855
0
    }
1856
1857
0
    return parser->total_size;
1858
0
}
1859
1860
/* return size or -1 if error */
1861
static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
1862
                          size_t hex_blob_size, AddressSpace *as)
1863
0
{
1864
0
    bool in_process = false; /* avoid re-enter and
1865
                              * check whether record begin with ':' */
1866
0
    uint8_t *end = hex_blob + hex_blob_size;
1867
0
    uint8_t our_checksum = 0;
1868
0
    uint32_t record_index = 0;
1869
0
    HexParser parser = {
1870
0
        .filename = filename,
1871
0
        .bin_buf = g_malloc(hex_blob_size),
1872
0
        .start_addr = addr,
1873
0
        .as = as,
1874
0
        .complete = false
1875
0
    };
1876
1877
0
    rom_transaction_begin();
1878
1879
0
    for (; hex_blob < end && !parser.complete; ++hex_blob) {
1880
0
        switch (*hex_blob) {
1881
0
        case '\r':
1882
0
        case '\n':
1883
0
            if (!in_process) {
1884
0
                break;
1885
0
            }
1886
1887
0
            in_process = false;
1888
0
            if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
1889
0
                    record_index ||
1890
0
                our_checksum != 0) {
1891
0
                parser.total_size = -1;
1892
0
                goto out;
1893
0
            }
1894
1895
0
            if (handle_record_type(&parser) == -1) {
1896
0
                parser.total_size = -1;
1897
0
                goto out;
1898
0
            }
1899
0
            break;
1900
1901
        /* start of a new record. */
1902
0
        case ':':
1903
0
            memset(&parser.line, 0, sizeof(HexLine));
1904
0
            in_process = true;
1905
0
            record_index = 0;
1906
0
            break;
1907
1908
        /* decoding lines */
1909
0
        default:
1910
0
            if (!parse_record(&parser.line, &our_checksum, *hex_blob,
1911
0
                              &record_index, in_process)) {
1912
0
                parser.total_size = -1;
1913
0
                goto out;
1914
0
            }
1915
0
            break;
1916
0
        }
1917
0
    }
1918
1919
0
out:
1920
0
    g_free(parser.bin_buf);
1921
0
    rom_transaction_end(parser.total_size != -1);
1922
0
    return parser.total_size;
1923
0
}
1924
1925
/* return size or -1 if error */
1926
ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
1927
                             AddressSpace *as)
1928
0
{
1929
0
    gsize hex_blob_size;
1930
0
    gchar *hex_blob;
1931
0
    ssize_t total_size = 0;
1932
1933
0
    if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
1934
0
        return -1;
1935
0
    }
1936
1937
0
    total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,
1938
0
                                hex_blob_size, as);
1939
1940
0
    g_free(hex_blob);
1941
0
    return total_size;
1942
0
}