| 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 | { |
| 81 | int fd; |
| 82 | int64_t size; |
| 83 | |
| 84 | fd = qemu_open(filename, O_RDONLY | O_BINARY, errp); |
| 85 | |
| 86 | if (fd < 0) { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | size = lseek(fd, 0, SEEK_END); |
| 91 | |
| 92 | if (size < 0) { |
| 93 | error_setg_errno(errp, errno, "lseek failure: %s", filename); |
| 94 | close(fd); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | close(fd); |
| 99 | return size; |
| 100 | } |
| 101 | |
| 102 | /* return the size or -1 if error */ |
| 103 | ssize_t load_image_size(const char *filename, void *addr, size_t size) |
| 104 | { |
| 105 | int fd; |
| 106 | ssize_t actsize, l = 0; |
| 107 | |
| 108 | fd = open(filename, O_RDONLY | O_BINARY); |
| 109 | if (fd < 0) { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | while ((actsize = read(fd, addr + l, size - l)) > 0) { |
| 114 | l += actsize; |
| 115 | } |
| 116 | |
| 117 | close(fd); |
| 118 | |
| 119 | return actsize < 0 ? -1 : l; |
| 120 | } |
| 121 | |
| 122 | /* read()-like version */ |
| 123 | ssize_t read_targphys(const char *name, |
| 124 | int fd, hwaddr dst_addr, size_t nbytes) |
| 125 | { |
| 126 | uint8_t *buf; |
| 127 | ssize_t did; |
| 128 | |
| 129 | buf = g_malloc(nbytes); |
| 130 | did = read(fd, buf, nbytes); |
| 131 | if (did > 0) |
| 132 | rom_add_blob_fixed("read", buf, did, dst_addr); |
| 133 | g_free(buf); |
| 134 | return did; |
| 135 | } |
| 136 | |
| 137 | ssize_t load_image_targphys(const char *filename, |
| 138 | hwaddr addr, uint64_t max_sz, Error **errp) |
| 139 | { |
| 140 | return load_image_targphys_as(filename, addr, max_sz, NULL, errp); |
| 141 | } |
| 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 | { |
| 148 | ssize_t size; |
| 149 | |
| 150 | size = get_image_size(filename, errp); |
| 151 | if (size < 0) { |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | if (size == 0) { |
| 156 | error_setg(errp, "empty file: %s", filename); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | if (size > max_sz) { |
| 161 | char *size_str = size_to_str(max_sz); |
| 162 | |
| 163 | error_setg(errp, "%s exceeds maximum image size (%s)", |
| 164 | filename, size_str); |
| 165 | |
| 166 | g_free(size_str); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) { |
| 171 | error_setg(errp, "could not load '%s' at %" HWADDR_PRIx, |
| 172 | filename, addr); |
| 173 | return -1; |
| 174 | } |
| 175 | return size; |
| 176 | } |
| 177 | |
| 178 | ssize_t load_image_mr(const char *filename, MemoryRegion *mr) |
| 179 | { |
| 180 | ssize_t size; |
| 181 | |
| 182 | if (!memory_access_is_direct(mr, false, MEMTXATTRS_UNSPECIFIED)) { |
| 183 | /* Can only load an image into RAM or ROM */ |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | size = get_image_size(filename, NULL); |
| 188 | |
| 189 | if (size < 0 || size > memory_region_size(mr)) { |
| 190 | return -1; |
| 191 | } |
| 192 | if (size > 0) { |
| 193 | if (rom_add_file_mr(filename, mr, -1) < 0) { |
| 194 | return -1; |
| 195 | } |
| 196 | } |
| 197 | return size; |
| 198 | } |
| 199 | |
| 200 | void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size, |
| 201 | const char *source) |
| 202 | { |
| 203 | const char *nulp; |
| 204 | char *ptr; |
| 205 | |
| 206 | if (buf_size <= 0) return; |
| 207 | nulp = memchr(source, 0, buf_size); |
| 208 | if (nulp) { |
| 209 | rom_add_blob_fixed(name, source, (nulp - source) + 1, dest); |
| 210 | } else { |
| 211 | rom_add_blob_fixed(name, source, buf_size, dest); |
| 212 | ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr)); |
| 213 | *ptr = 0; |
| 214 | } |
| 215 | } |
| 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 | { |
| 233 | bswap32s(&e->a_info); |
| 234 | bswap32s(&e->a_text); |
| 235 | bswap32s(&e->a_data); |
| 236 | bswap32s(&e->a_bss); |
| 237 | bswap32s(&e->a_syms); |
| 238 | bswap32s(&e->a_entry); |
| 239 | bswap32s(&e->a_trsize); |
| 240 | bswap32s(&e->a_drsize); |
| 241 | } |
| 242 | |
| 243 | #define N_MAGIC(exec) ((exec).a_info & 0xffff) |
| 244 | #define OMAGIC 0407 |
| 245 | #define NMAGIC 0410 |
| 246 | #define ZMAGIC 0413 |
| 247 | #define QMAGIC 0314 |
| 248 | #define _N_HDROFF(x) (1024 - sizeof (struct exec)) |
| 249 | #define N_TXTOFF(x) \ |
| 250 | (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \ |
| 251 | (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec))) |
| 252 | #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0) |
| 253 | #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1)) |
| 254 | |
| 255 | #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 | (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \ |
| 259 | : (_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 | { |
| 265 | int fd; |
| 266 | ssize_t size, ret; |
| 267 | struct exec e; |
| 268 | uint32_t magic; |
| 269 | |
| 270 | fd = open(filename, O_RDONLY | O_BINARY); |
| 271 | if (fd < 0) |
| 272 | return -1; |
| 273 | |
| 274 | size = read(fd, &e, sizeof(e)); |
| 275 | if (size < 0) |
| 276 | goto fail; |
| 277 | |
| 278 | if (big_endian != HOST_BIG_ENDIAN) { |
| 279 | bswap_ahdr(&e); |
| 280 | } |
| 281 | |
| 282 | magic = N_MAGIC(e); |
| 283 | switch (magic) { |
| 284 | case ZMAGIC: |
| 285 | case QMAGIC: |
| 286 | case OMAGIC: |
| 287 | if (e.a_text + e.a_data > max_sz) |
| 288 | goto fail; |
| 289 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
| 290 | size = read_targphys(filename, fd, addr, e.a_text + e.a_data); |
| 291 | if (size < 0) |
| 292 | goto fail; |
| 293 | break; |
| 294 | case NMAGIC: |
| 295 | if (N_DATADDR(e, target_page_size) + e.a_data > max_sz) |
| 296 | goto fail; |
| 297 | lseek(fd, N_TXTOFF(e), SEEK_SET); |
| 298 | size = read_targphys(filename, fd, addr, e.a_text); |
| 299 | if (size < 0) |
| 300 | goto fail; |
| 301 | ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size), |
| 302 | e.a_data); |
| 303 | if (ret < 0) |
| 304 | goto fail; |
| 305 | size += ret; |
| 306 | break; |
| 307 | default: |
| 308 | goto fail; |
| 309 | } |
| 310 | close(fd); |
| 311 | return size; |
| 312 | fail: |
| 313 | close(fd); |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | /* ELF loader */ |
| 318 | |
| 319 | static void *load_at(int fd, off_t offset, size_t size) |
| 320 | { |
| 321 | void *ptr; |
| 322 | if (lseek(fd, offset, SEEK_SET) < 0) |
| 323 | return NULL; |
| 324 | ptr = g_malloc(size); |
| 325 | if (read(fd, ptr, size) != size) { |
| 326 | g_free(ptr); |
| 327 | return NULL; |
| 328 | } |
| 329 | return ptr; |
| 330 | } |
| 331 | |
| 332 | #define ELF_CLASS ELFCLASS32 |
| 333 | #include "elf.h" |
| 334 | |
| 335 | #define SZ 32 |
| 336 | #define elf_word uint32_t |
| 337 | #define elf_sword int32_t |
| 338 | #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 | #define elf_word uint64_t |
| 358 | #define elf_sword int64_t |
| 359 | #define bswapSZs bswap64s |
| 360 | #define SZ 64 |
| 361 | #include "hw/elf_ops.h.inc" |
| 362 | |
| 363 | const char *load_elf_strerror(ssize_t error) |
| 364 | { |
| 365 | switch (error) { |
| 366 | case 0: |
| 367 | return "No error"; |
| 368 | case ELF_LOAD_FAILED: |
| 369 | return "Failed to load ELF"; |
| 370 | case ELF_LOAD_NOT_ELF: |
| 371 | return "The image is not ELF"; |
| 372 | case ELF_LOAD_WRONG_ARCH: |
| 373 | return "The image is from incompatible architecture"; |
| 374 | case ELF_LOAD_WRONG_ENDIAN: |
| 375 | return "The image has incorrect endianness"; |
| 376 | case ELF_LOAD_TOO_BIG: |
| 377 | return "The image segments are too big to load"; |
| 378 | default: |
| 379 | return "Unknown error"; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | bool load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp) |
| 384 | { |
| 385 | bool ok = false; |
| 386 | int fd; |
| 387 | uint8_t e_ident_local[EI_NIDENT]; |
| 388 | uint8_t *e_ident; |
| 389 | size_t hdr_size, off; |
| 390 | bool is64l; |
| 391 | |
| 392 | if (!hdr) { |
| 393 | hdr = e_ident_local; |
| 394 | } |
| 395 | e_ident = hdr; |
| 396 | |
| 397 | fd = open(filename, O_RDONLY | O_BINARY); |
| 398 | if (fd < 0) { |
| 399 | error_setg_file_open(errp, errno, filename); |
| 400 | return false; |
| 401 | } |
| 402 | if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) { |
| 403 | error_setg_errno(errp, errno, "Failed to read file: %s", filename); |
| 404 | goto fail; |
| 405 | } |
| 406 | if (e_ident[0] != ELFMAG0 || |
| 407 | e_ident[1] != ELFMAG1 || |
| 408 | e_ident[2] != ELFMAG2 || |
| 409 | e_ident[3] != ELFMAG3) { |
| 410 | error_setg(errp, "Bad ELF magic"); |
| 411 | goto fail; |
| 412 | } |
| 413 | |
| 414 | is64l = e_ident[EI_CLASS] == ELFCLASS64; |
| 415 | hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr); |
| 416 | if (is64) { |
| 417 | *is64 = is64l; |
| 418 | } |
| 419 | |
| 420 | off = EI_NIDENT; |
| 421 | while (hdr != e_ident_local && off < hdr_size) { |
| 422 | size_t br = read(fd, hdr + off, hdr_size - off); |
| 423 | switch (br) { |
| 424 | case 0: |
| 425 | error_setg(errp, "File too short: %s", filename); |
| 426 | goto fail; |
| 427 | case -1: |
| 428 | error_setg_errno(errp, errno, "Failed to read file: %s", |
| 429 | filename); |
| 430 | goto fail; |
| 431 | } |
| 432 | off += br; |
| 433 | } |
| 434 | |
| 435 | ok = true; |
| 436 | |
| 437 | fail: |
| 438 | close(fd); |
| 439 | return ok; |
| 440 | } |
| 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 | { |
| 450 | return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque, |
| 451 | pentry, lowaddr, highaddr, pflags, elf_data_order, |
| 452 | elf_machine, clear_lsb, data_swab, NULL); |
| 453 | } |
| 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 | { |
| 464 | return load_elf_ram_sym(filename, elf_note_fn, |
| 465 | translate_fn, translate_opaque, |
| 466 | pentry, lowaddr, highaddr, pflags, elf_data_order, |
| 467 | elf_machine, clear_lsb, data_swab, as, |
| 468 | true, NULL); |
| 469 | } |
| 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 | { |
| 481 | const int host_data_order = HOST_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB; |
| 482 | int fd, must_swab; |
| 483 | ssize_t ret = ELF_LOAD_FAILED; |
| 484 | uint8_t e_ident[EI_NIDENT]; |
| 485 | |
| 486 | fd = open(filename, O_RDONLY | O_BINARY); |
| 487 | if (fd < 0) { |
| 488 | perror(filename); |
| 489 | return -1; |
| 490 | } |
| 491 | if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
| 492 | goto fail; |
| 493 | if (e_ident[0] != ELFMAG0 || |
| 494 | e_ident[1] != ELFMAG1 || |
| 495 | e_ident[2] != ELFMAG2 || |
| 496 | e_ident[3] != ELFMAG3) { |
| 497 | ret = ELF_LOAD_NOT_ELF; |
| 498 | goto fail; |
| 499 | } |
| 500 | |
| 501 | if (elf_data_order != ELFDATANONE && elf_data_order != e_ident[EI_DATA]) { |
| 502 | ret = ELF_LOAD_WRONG_ENDIAN; |
| 503 | goto fail; |
| 504 | } |
| 505 | |
| 506 | must_swab = host_data_order != e_ident[EI_DATA]; |
| 507 | |
| 508 | lseek(fd, 0, SEEK_SET); |
| 509 | if (e_ident[EI_CLASS] == ELFCLASS64) { |
| 510 | ret = load_elf64(filename, fd, elf_note_fn, |
| 511 | translate_fn, translate_opaque, must_swab, |
| 512 | pentry, lowaddr, highaddr, pflags, elf_machine, |
| 513 | clear_lsb, data_swab, as, load_rom, sym_cb); |
| 514 | } else { |
| 515 | ret = load_elf32(filename, fd, elf_note_fn, |
| 516 | translate_fn, translate_opaque, must_swab, |
| 517 | pentry, lowaddr, highaddr, pflags, elf_machine, |
| 518 | clear_lsb, data_swab, as, load_rom, sym_cb); |
| 519 | } |
| 520 | |
| 521 | if (ret > 0) { |
| 522 | debuginfo_report_elf(filename, fd, 0); |
| 523 | } |
| 524 | |
| 525 | fail: |
| 526 | close(fd); |
| 527 | return ret; |
| 528 | } |
| 529 | |
| 530 | static void bswap_uboot_header(uboot_image_header_t *hdr) |
| 531 | { |
| 532 | #if !HOST_BIG_ENDIAN |
| 533 | bswap32s(&hdr->ih_magic); |
| 534 | bswap32s(&hdr->ih_hcrc); |
| 535 | bswap32s(&hdr->ih_time); |
| 536 | bswap32s(&hdr->ih_size); |
| 537 | bswap32s(&hdr->ih_load); |
| 538 | bswap32s(&hdr->ih_ep); |
| 539 | bswap32s(&hdr->ih_dcrc); |
| 540 | #endif |
| 541 | } |
| 542 | |
| 543 | |
| 544 | #define ZALLOC_ALIGNMENT 16 |
| 545 | |
| 546 | static void *zalloc(void *x, unsigned items, unsigned size) |
| 547 | { |
| 548 | void *p; |
| 549 | |
| 550 | size *= items; |
| 551 | size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1); |
| 552 | |
| 553 | p = g_malloc(size); |
| 554 | |
| 555 | return (p); |
| 556 | } |
| 557 | |
| 558 | static void zfree(void *x, void *addr) |
| 559 | { |
| 560 | g_free(addr); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | #define HEAD_CRC 2 |
| 565 | #define EXTRA_FIELD 4 |
| 566 | #define ORIG_NAME 8 |
| 567 | #define COMMENT 0x10 |
| 568 | #define RESERVED 0xe0 |
| 569 | |
| 570 | #define DEFLATED 8 |
| 571 | |
| 572 | ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen) |
| 573 | { |
| 574 | z_stream s = {}; |
| 575 | ssize_t dstbytes; |
| 576 | int r, i, flags; |
| 577 | |
| 578 | /* skip header */ |
| 579 | i = 10; |
| 580 | if (srclen < 4) { |
| 581 | goto toosmall; |
| 582 | } |
| 583 | flags = src[3]; |
| 584 | if (src[2] != DEFLATED || (flags & RESERVED) != 0) { |
| 585 | puts ("Error: Bad gzipped data\n"); |
| 586 | return -1; |
| 587 | } |
| 588 | if ((flags & EXTRA_FIELD) != 0) { |
| 589 | if (srclen < 12) { |
| 590 | goto toosmall; |
| 591 | } |
| 592 | i = 12 + src[10] + (src[11] << 8); |
| 593 | } |
| 594 | if ((flags & ORIG_NAME) != 0) { |
| 595 | while (i < srclen && src[i++] != 0) { |
| 596 | /* do nothing */ |
| 597 | } |
| 598 | } |
| 599 | if ((flags & COMMENT) != 0) { |
| 600 | while (i < srclen && src[i++] != 0) { |
| 601 | /* do nothing */ |
| 602 | } |
| 603 | } |
| 604 | if ((flags & HEAD_CRC) != 0) { |
| 605 | i += 2; |
| 606 | } |
| 607 | if (i >= srclen) { |
| 608 | goto toosmall; |
| 609 | } |
| 610 | |
| 611 | s.zalloc = zalloc; |
| 612 | s.zfree = zfree; |
| 613 | |
| 614 | r = inflateInit2(&s, -MAX_WBITS); |
| 615 | if (r != Z_OK) { |
| 616 | printf ("Error: inflateInit2() returned %d\n", r); |
| 617 | return (-1); |
| 618 | } |
| 619 | s.next_in = src + i; |
| 620 | s.avail_in = srclen - i; |
| 621 | s.next_out = dst; |
| 622 | s.avail_out = dstlen; |
| 623 | r = inflate(&s, Z_FINISH); |
| 624 | if (r != Z_OK && r != Z_STREAM_END) { |
| 625 | printf ("Error: inflate() returned %d\n", r); |
| 626 | inflateEnd(&s); |
| 627 | return -1; |
| 628 | } |
| 629 | dstbytes = s.next_out - (unsigned char *) dst; |
| 630 | inflateEnd(&s); |
| 631 | |
| 632 | return dstbytes; |
| 633 | |
| 634 | toosmall: |
| 635 | puts("Error: gunzip out of data in header\n"); |
| 636 | return -1; |
| 637 | } |
| 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 | { |
| 646 | int fd; |
| 647 | ssize_t size; |
| 648 | hwaddr address; |
| 649 | uboot_image_header_t h; |
| 650 | uboot_image_header_t *hdr = &h; |
| 651 | uint8_t *data = NULL; |
| 652 | int ret = -1; |
| 653 | int do_uncompress = 0; |
| 654 | |
| 655 | fd = open(filename, O_RDONLY | O_BINARY); |
| 656 | if (fd < 0) |
| 657 | return -1; |
| 658 | |
| 659 | size = read(fd, hdr, sizeof(uboot_image_header_t)); |
| 660 | if (size < sizeof(uboot_image_header_t)) { |
| 661 | goto out; |
| 662 | } |
| 663 | |
| 664 | bswap_uboot_header(hdr); |
| 665 | |
| 666 | if (hdr->ih_magic != IH_MAGIC) |
| 667 | goto out; |
| 668 | |
| 669 | if (hdr->ih_type != image_type) { |
| 670 | if (!(image_type == IH_TYPE_KERNEL && |
| 671 | hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) { |
| 672 | fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type, |
| 673 | image_type); |
| 674 | goto out; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /* TODO: Implement other image types. */ |
| 679 | switch (hdr->ih_type) { |
| 680 | case IH_TYPE_KERNEL_NOLOAD: |
| 681 | if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) { |
| 682 | fprintf(stderr, "this image format (kernel_noload) cannot be " |
| 683 | "loaded on this machine type"); |
| 684 | goto out; |
| 685 | } |
| 686 | |
| 687 | hdr->ih_load = *loadaddr + sizeof(*hdr); |
| 688 | hdr->ih_ep += hdr->ih_load; |
| 689 | /* fall through */ |
| 690 | case IH_TYPE_KERNEL: |
| 691 | address = hdr->ih_load; |
| 692 | if (translate_fn) { |
| 693 | address = translate_fn(translate_opaque, address); |
| 694 | } |
| 695 | if (loadaddr) { |
| 696 | *loadaddr = hdr->ih_load; |
| 697 | } |
| 698 | |
| 699 | switch (hdr->ih_comp) { |
| 700 | case IH_COMP_NONE: |
| 701 | break; |
| 702 | case IH_COMP_GZIP: |
| 703 | do_uncompress = 1; |
| 704 | break; |
| 705 | default: |
| 706 | fprintf(stderr, |
| 707 | "Unable to load u-boot images with compression type %d\n", |
| 708 | hdr->ih_comp); |
| 709 | goto out; |
| 710 | } |
| 711 | |
| 712 | if (ep) { |
| 713 | *ep = hdr->ih_ep; |
| 714 | } |
| 715 | |
| 716 | /* TODO: Check CPU type. */ |
| 717 | if (is_linux) { |
| 718 | if (hdr->ih_os == IH_OS_LINUX) { |
| 719 | *is_linux = 1; |
| 720 | } 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 | switch (hdr->ih_arch) { |
| 726 | case IH_ARCH_ARM64: |
| 727 | case IH_ARCH_PPC: |
| 728 | case IH_ARCH_RISCV: |
| 729 | *is_linux = 1; |
| 730 | break; |
| 731 | default: |
| 732 | *is_linux = 0; |
| 733 | break; |
| 734 | } |
| 735 | } else { |
| 736 | *is_linux = 0; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | break; |
| 741 | case IH_TYPE_RAMDISK: |
| 742 | address = *loadaddr; |
| 743 | break; |
| 744 | default: |
| 745 | fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type); |
| 746 | goto out; |
| 747 | } |
| 748 | |
| 749 | data = g_malloc(hdr->ih_size); |
| 750 | |
| 751 | if (read(fd, data, hdr->ih_size) != hdr->ih_size) { |
| 752 | fprintf(stderr, "Error reading file\n"); |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | if (do_uncompress) { |
| 757 | uint8_t *compressed_data; |
| 758 | size_t max_bytes; |
| 759 | ssize_t bytes; |
| 760 | |
| 761 | compressed_data = data; |
| 762 | max_bytes = UBOOT_MAX_DECOMPRESSED_BYTES; |
| 763 | data = g_malloc(max_bytes); |
| 764 | |
| 765 | bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size); |
| 766 | g_free(compressed_data); |
| 767 | if (bytes < 0) { |
| 768 | fprintf(stderr, "Unable to decompress gzipped image!\n"); |
| 769 | goto out; |
| 770 | } |
| 771 | hdr->ih_size = bytes; |
| 772 | } |
| 773 | |
| 774 | rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as); |
| 775 | |
| 776 | ret = hdr->ih_size; |
| 777 | |
| 778 | out: |
| 779 | g_free(data); |
| 780 | close(fd); |
| 781 | return ret; |
| 782 | } |
| 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 | { |
| 789 | return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL, |
| 790 | translate_fn, translate_opaque, NULL); |
| 791 | } |
| 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 | { |
| 798 | return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL, |
| 799 | translate_fn, translate_opaque, as); |
| 800 | } |
| 801 | |
| 802 | /* Load a ramdisk. */ |
| 803 | ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz) |
| 804 | { |
| 805 | return load_ramdisk_as(filename, addr, max_sz, NULL); |
| 806 | } |
| 807 | |
| 808 | ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz, |
| 809 | AddressSpace *as) |
| 810 | { |
| 811 | return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK, |
| 812 | NULL, NULL, as); |
| 813 | } |
| 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 | { |
| 819 | uint8_t *compressed_data = NULL; |
| 820 | uint8_t *data = NULL; |
| 821 | gsize len; |
| 822 | ssize_t bytes; |
| 823 | int ret = -1; |
| 824 | |
| 825 | if (!g_file_get_contents(filename, (char **) &compressed_data, &len, |
| 826 | NULL)) { |
| 827 | goto out; |
| 828 | } |
| 829 | |
| 830 | /* Is it a gzip-compressed file? */ |
| 831 | if (len < 2 || |
| 832 | compressed_data[0] != 0x1f || |
| 833 | compressed_data[1] != 0x8b) { |
| 834 | goto out; |
| 835 | } |
| 836 | |
| 837 | if (max_sz > LOAD_IMAGE_MAX_DECOMPRESSED_BYTES) { |
| 838 | max_sz = LOAD_IMAGE_MAX_DECOMPRESSED_BYTES; |
| 839 | } |
| 840 | |
| 841 | data = g_malloc(max_sz); |
| 842 | bytes = gunzip(data, max_sz, compressed_data, len); |
| 843 | if (bytes < 0) { |
| 844 | fprintf(stderr, "%s: unable to decompress gzipped kernel file\n", |
| 845 | filename); |
| 846 | goto out; |
| 847 | } |
| 848 | |
| 849 | /* trim to actual size and return to caller */ |
| 850 | *buffer = g_realloc(data, bytes); |
| 851 | ret = bytes; |
| 852 | /* ownership has been transferred to caller */ |
| 853 | data = NULL; |
| 854 | |
| 855 | out: |
| 856 | g_free(compressed_data); |
| 857 | g_free(data); |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | /* The PE/COFF MS-DOS stub magic number */ |
| 863 | #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 | #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 | { |
| 904 | const size_t max_bytes = LOAD_IMAGE_MAX_DECOMPRESSED_BYTES; |
| 905 | const struct linux_efi_zboot_header *header; |
| 906 | g_autofree uint8_t *data = NULL; |
| 907 | ssize_t ploff, plsize; |
| 908 | ssize_t bytes; |
| 909 | |
| 910 | /* ignore if this is too small to be a EFI zboot image */ |
| 911 | if (*size < sizeof(*header)) { |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | header = (struct linux_efi_zboot_header *)*buffer; |
| 916 | |
| 917 | /* ignore if this is not a Linux EFI zboot image */ |
| 918 | if (memcmp(&header->msdos_magic, EFI_PE_MSDOS_MAGIC, 2) != 0 || |
| 919 | memcmp(&header->zimg, "zimg", 4) != 0 || |
| 920 | memcmp(&header->linux_magic, EFI_PE_LINUX_MAGIC, 4) != 0) { |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | ploff = ldl_le_p(&header->payload_offset); |
| 925 | plsize = ldl_le_p(&header->payload_size); |
| 926 | |
| 927 | if (ploff < 0 || plsize < 0 || ploff + plsize > *size) { |
| 928 | fprintf(stderr, "unable to handle corrupt EFI zboot image\n"); |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | data = g_malloc(max_bytes); |
| 933 | |
| 934 | if (strcmp(header->compression_type, "gzip") == 0) { |
| 935 | 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 | } else { |
| 942 | fprintf(stderr, |
| 943 | "unable to handle EFI zboot image with \"%.*s\" compression\n", |
| 944 | (int)sizeof(header->compression_type) - 1, |
| 945 | header->compression_type); |
| 946 | return -1; |
| 947 | } |
| 948 | |
| 949 | if (bytes < 0) { |
| 950 | fprintf(stderr, "failed to decompress EFI zboot image\n"); |
| 951 | return -1; |
| 952 | } |
| 953 | |
| 954 | g_free(*buffer); |
| 955 | *buffer = g_realloc(g_steal_pointer(&data), bytes); |
| 956 | *size = bytes; |
| 957 | return bytes; |
| 958 | } |
| 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 | { |
| 1002 | if (rom->mapped_file) { |
| 1003 | g_mapped_file_unref(rom->mapped_file); |
| 1004 | rom->mapped_file = NULL; |
| 1005 | } else { |
| 1006 | g_free(rom->data); |
| 1007 | } |
| 1008 | |
| 1009 | rom->data = NULL; |
| 1010 | } |
| 1011 | |
| 1012 | static void rom_free(Rom *rom) |
| 1013 | { |
| 1014 | rom_free_data(rom); |
| 1015 | g_free(rom->path); |
| 1016 | g_free(rom->name); |
| 1017 | g_free(rom->fw_dir); |
| 1018 | g_free(rom->fw_file); |
| 1019 | g_free(rom); |
| 1020 | } |
| 1021 | |
| 1022 | static inline bool rom_order_compare(Rom *rom, Rom *item) |
| 1023 | { |
| 1024 | return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) || |
| 1025 | (rom->as == item->as && rom->addr >= item->addr); |
| 1026 | } |
| 1027 | |
| 1028 | static void rom_insert(Rom *rom) |
| 1029 | { |
| 1030 | Rom *item; |
| 1031 | |
| 1032 | if (roms_loaded) { |
| 1033 | hw_error ("ROM images must be loaded at startup\n"); |
| 1034 | } |
| 1035 | |
| 1036 | /* The user didn't specify an address space, this is the default */ |
| 1037 | if (!rom->as) { |
| 1038 | rom->as = &address_space_memory; |
| 1039 | } |
| 1040 | |
| 1041 | rom->committed = false; |
| 1042 | |
| 1043 | /* List is ordered by load address in the same address space */ |
| 1044 | QTAILQ_FOREACH(item, &roms, next) { |
| 1045 | if (rom_order_compare(rom, item)) { |
| 1046 | continue; |
| 1047 | } |
| 1048 | QTAILQ_INSERT_BEFORE(item, rom, next); |
| 1049 | return; |
| 1050 | } |
| 1051 | QTAILQ_INSERT_TAIL(&roms, rom, next); |
| 1052 | } |
| 1053 | |
| 1054 | static void fw_cfg_resized(const char *id, uint64_t length, void *host) |
| 1055 | { |
| 1056 | if (fw_cfg) { |
| 1057 | fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro) |
| 1062 | { |
| 1063 | void *data; |
| 1064 | |
| 1065 | rom->mr = g_malloc(sizeof(*rom->mr)); |
| 1066 | memory_region_init_resizeable_ram(rom->mr, owner, name, |
| 1067 | rom->datasize, rom->romsize, |
| 1068 | fw_cfg_resized, |
| 1069 | &error_fatal); |
| 1070 | memory_region_set_readonly(rom->mr, ro); |
| 1071 | vmstate_register_ram_global(rom->mr); |
| 1072 | |
| 1073 | data = memory_region_get_ram_ptr(rom->mr); |
| 1074 | if (!cpr_is_incoming()) { |
| 1075 | memcpy(data, rom->data, rom->datasize); |
| 1076 | } |
| 1077 | |
| 1078 | return data; |
| 1079 | } |
| 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 | { |
| 1086 | MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); |
| 1087 | Rom *rom; |
| 1088 | gsize size; |
| 1089 | g_autoptr(GError) gerr = NULL; |
| 1090 | char devpath[100]; |
| 1091 | |
| 1092 | if (as && mr) { |
| 1093 | fprintf(stderr, "Specifying an Address Space and Memory Region is " \ |
| 1094 | "not valid when loading a rom\n"); |
| 1095 | /* We haven't allocated anything so we don't need any cleanup */ |
| 1096 | return -1; |
| 1097 | } |
| 1098 | |
| 1099 | rom = g_malloc0(sizeof(*rom)); |
| 1100 | rom->name = g_strdup(file); |
| 1101 | rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name); |
| 1102 | rom->as = as; |
| 1103 | if (rom->path == NULL) { |
| 1104 | rom->path = g_strdup(file); |
| 1105 | } |
| 1106 | |
| 1107 | if (!g_file_get_contents(rom->path, (gchar **) &rom->data, |
| 1108 | &size, &gerr)) { |
| 1109 | fprintf(stderr, "rom: file %-20s: error %s\n", |
| 1110 | rom->name, gerr->message); |
| 1111 | goto err; |
| 1112 | } |
| 1113 | |
| 1114 | if (fw_dir) { |
| 1115 | rom->fw_dir = g_strdup(fw_dir); |
| 1116 | rom->fw_file = g_strdup(file); |
| 1117 | } |
| 1118 | rom->addr = addr; |
| 1119 | rom->romsize = size; |
| 1120 | rom->datasize = rom->romsize; |
| 1121 | rom_insert(rom); |
| 1122 | if (rom->fw_file && fw_cfg) { |
| 1123 | const char *basename; |
| 1124 | char fw_file_name[FW_CFG_MAX_FILE_PATH]; |
| 1125 | void *data; |
| 1126 | |
| 1127 | basename = strrchr(rom->fw_file, '/'); |
| 1128 | if (basename) { |
| 1129 | basename++; |
| 1130 | } else { |
| 1131 | basename = rom->fw_file; |
| 1132 | } |
| 1133 | snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir, |
| 1134 | basename); |
| 1135 | snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name); |
| 1136 | |
| 1137 | if ((!has_option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) { |
| 1138 | data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true); |
| 1139 | } else { |
| 1140 | data = rom->data; |
| 1141 | } |
| 1142 | |
| 1143 | fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize); |
| 1144 | } else { |
| 1145 | if (mr) { |
| 1146 | rom->mr = mr; |
| 1147 | snprintf(devpath, sizeof(devpath), "/rom@%s", file); |
| 1148 | } else { |
| 1149 | snprintf(devpath, sizeof(devpath), "/rom@" HWADDR_FMT_plx, addr); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | add_boot_device_path(bootindex, NULL, devpath); |
| 1154 | return 0; |
| 1155 | |
| 1156 | err: |
| 1157 | rom_free(rom); |
| 1158 | return -1; |
| 1159 | } |
| 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 | { |
| 1166 | MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine()); |
| 1167 | Rom *rom; |
| 1168 | MemoryRegion *mr = NULL; |
| 1169 | |
| 1170 | rom = g_malloc0(sizeof(*rom)); |
| 1171 | rom->name = g_strdup(name); |
| 1172 | rom->as = as; |
| 1173 | rom->addr = addr; |
| 1174 | rom->romsize = max_len ? max_len : len; |
| 1175 | rom->datasize = len; |
| 1176 | g_assert(rom->romsize >= rom->datasize); |
| 1177 | rom->data = g_malloc0(rom->datasize); |
| 1178 | memcpy(rom->data, blob, len); |
| 1179 | rom_insert(rom); |
| 1180 | if (fw_file_name && fw_cfg) { |
| 1181 | char devpath[100]; |
| 1182 | void *data; |
| 1183 | |
| 1184 | if (read_only) { |
| 1185 | snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name); |
| 1186 | } else { |
| 1187 | snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name); |
| 1188 | } |
| 1189 | |
| 1190 | if (mc->rom_file_has_mr) { |
| 1191 | data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only); |
| 1192 | mr = rom->mr; |
| 1193 | } else { |
| 1194 | data = rom->data; |
| 1195 | } |
| 1196 | |
| 1197 | fw_cfg_add_file_callback(fw_cfg, fw_file_name, |
| 1198 | fw_callback, NULL, callback_opaque, |
| 1199 | data, rom->datasize, read_only); |
| 1200 | } |
| 1201 | return mr; |
| 1202 | } |
| 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 | { |
| 1213 | Rom *rom; |
| 1214 | |
| 1215 | rom = g_malloc0(sizeof(*rom)); |
| 1216 | rom->name = g_strdup(name); |
| 1217 | rom->addr = addr; |
| 1218 | rom->datasize = datasize; |
| 1219 | rom->romsize = romsize; |
| 1220 | rom->data = data; |
| 1221 | rom->as = as; |
| 1222 | |
| 1223 | if (mapped_file && data) { |
| 1224 | g_mapped_file_ref(mapped_file); |
| 1225 | rom->mapped_file = mapped_file; |
| 1226 | } |
| 1227 | |
| 1228 | rom_insert(rom); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | ssize_t rom_add_vga(const char *file) |
| 1233 | { |
| 1234 | return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL); |
| 1235 | } |
| 1236 | |
| 1237 | ssize_t rom_add_option(const char *file, int32_t bootindex) |
| 1238 | { |
| 1239 | return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL); |
| 1240 | } |
| 1241 | |
| 1242 | static void rom_reset(void *unused) |
| 1243 | { |
| 1244 | Rom *rom; |
| 1245 | |
| 1246 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1247 | if (rom->fw_file) { |
| 1248 | continue; |
| 1249 | } |
| 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 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 1256 | 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 | rom_free_data(rom); |
| 1262 | } |
| 1263 | continue; |
| 1264 | } |
| 1265 | |
| 1266 | if (rom->data == NULL) { |
| 1267 | continue; |
| 1268 | } |
| 1269 | if (rom->mr) { |
| 1270 | void *host = memory_region_get_ram_ptr(rom->mr); |
| 1271 | memcpy(host, rom->data, rom->datasize); |
| 1272 | memset(host + rom->datasize, 0, rom->romsize - rom->datasize); |
| 1273 | } else { |
| 1274 | address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED, |
| 1275 | rom->data, rom->datasize); |
| 1276 | address_space_set(rom->as, rom->addr + rom->datasize, 0, |
| 1277 | rom->romsize - rom->datasize, |
| 1278 | MEMTXATTRS_UNSPECIFIED); |
| 1279 | } |
| 1280 | if (rom->isrom) { |
| 1281 | /* rom needs to be written only once */ |
| 1282 | rom_free_data(rom); |
| 1283 | } |
| 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 | address_space_flush_icache_range(rom->as, rom->addr, rom->datasize); |
| 1291 | |
| 1292 | trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom); |
| 1293 | } |
| 1294 | } |
| 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 | { |
| 1299 | if (!last_rom) { |
| 1300 | return false; |
| 1301 | } |
| 1302 | return last_rom->as == this_rom->as && |
| 1303 | last_rom->addr + last_rom->romsize > this_rom->addr; |
| 1304 | } |
| 1305 | |
| 1306 | static const char *rom_as_name(Rom *rom) |
| 1307 | { |
| 1308 | const char *name = rom->as ? rom->as->name : NULL; |
| 1309 | return name ?: "anonymous"; |
| 1310 | } |
| 1311 | |
| 1312 | static void rom_print_overlap_error_header(void) |
| 1313 | { |
| 1314 | error_report("Some ROM regions are overlapping"); |
| 1315 | error_printf( |
| 1316 | "These ROM regions might have been loaded by " |
| 1317 | "direct user request or by default.\n" |
| 1318 | "They could be BIOS/firmware images, a guest kernel, " |
| 1319 | "initrd or some other file loaded into guest memory.\n" |
| 1320 | "Check whether you intended to load all this guest code, and " |
| 1321 | "whether it has been built to load to the correct addresses.\n"); |
| 1322 | } |
| 1323 | |
| 1324 | static void rom_print_one_overlap_error(Rom *last_rom, Rom *rom) |
| 1325 | { |
| 1326 | error_printf( |
| 1327 | "\nThe following two regions overlap (in the %s address space):\n", |
| 1328 | rom_as_name(rom)); |
| 1329 | error_printf( |
| 1330 | " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n", |
| 1331 | last_rom->name, last_rom->addr, last_rom->addr + last_rom->romsize); |
| 1332 | error_printf( |
| 1333 | " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n", |
| 1334 | rom->name, rom->addr, rom->addr + rom->romsize); |
| 1335 | } |
| 1336 | |
| 1337 | int rom_check_and_register_reset(void) |
| 1338 | { |
| 1339 | MemoryRegionSection section; |
| 1340 | Rom *rom, *last_rom = NULL; |
| 1341 | bool found_overlap = false; |
| 1342 | |
| 1343 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1344 | if (rom->fw_file) { |
| 1345 | continue; |
| 1346 | } |
| 1347 | if (!rom->mr) { |
| 1348 | if (roms_overlap(last_rom, rom)) { |
| 1349 | if (!found_overlap) { |
| 1350 | found_overlap = true; |
| 1351 | rom_print_overlap_error_header(); |
| 1352 | } |
| 1353 | rom_print_one_overlap_error(last_rom, rom); |
| 1354 | /* Keep going through the list so we report all overlaps */ |
| 1355 | } |
| 1356 | last_rom = rom; |
| 1357 | } |
| 1358 | section = memory_region_find(rom->mr ? rom->mr : get_system_memory(), |
| 1359 | rom->addr, 1); |
| 1360 | rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr); |
| 1361 | memory_region_unref(section.mr); |
| 1362 | } |
| 1363 | if (found_overlap) { |
| 1364 | return -1; |
| 1365 | } |
| 1366 | |
| 1367 | qemu_register_reset(rom_reset, NULL); |
| 1368 | roms_loaded = 1; |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | void rom_set_fw(FWCfgState *f) |
| 1373 | { |
| 1374 | fw_cfg = f; |
| 1375 | } |
| 1376 | |
| 1377 | void rom_transaction_begin(void) |
| 1378 | { |
| 1379 | Rom *rom; |
| 1380 | |
| 1381 | /* Ignore ROMs added without the transaction API */ |
| 1382 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1383 | rom->committed = true; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | void rom_transaction_end(bool commit) |
| 1388 | { |
| 1389 | Rom *rom; |
| 1390 | Rom *tmp; |
| 1391 | |
| 1392 | QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) { |
| 1393 | if (rom->committed) { |
| 1394 | continue; |
| 1395 | } |
| 1396 | if (commit) { |
| 1397 | rom->committed = true; |
| 1398 | } else { |
| 1399 | QTAILQ_REMOVE(&roms, rom, next); |
| 1400 | rom_free(rom); |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | static Rom *find_rom(hwaddr addr, size_t size) |
| 1406 | { |
| 1407 | Rom *rom; |
| 1408 | |
| 1409 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1410 | if (rom->fw_file) { |
| 1411 | continue; |
| 1412 | } |
| 1413 | if (rom->mr) { |
| 1414 | continue; |
| 1415 | } |
| 1416 | if (rom->addr > addr) { |
| 1417 | continue; |
| 1418 | } |
| 1419 | if (rom->addr + rom->romsize < addr + size) { |
| 1420 | continue; |
| 1421 | } |
| 1422 | return rom; |
| 1423 | } |
| 1424 | return NULL; |
| 1425 | } |
| 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 | { |
| 1442 | RomSec *ra = (RomSec *) a; |
| 1443 | RomSec *rb = (RomSec *) b; |
| 1444 | |
| 1445 | if (ra->base == rb->base) { |
| 1446 | return ra->se - rb->se; |
| 1447 | } |
| 1448 | return ra->base > rb->base ? 1 : -1; |
| 1449 | } |
| 1450 | |
| 1451 | static GList *add_romsec_to_list(GList *secs, hwaddr base, int se) |
| 1452 | { |
| 1453 | RomSec *cand = g_new(RomSec, 1); |
| 1454 | cand->base = base; |
| 1455 | cand->se = se; |
| 1456 | return g_list_prepend(secs, cand); |
| 1457 | } |
| 1458 | |
| 1459 | RomGap rom_find_largest_gap_between(hwaddr base, size_t size) |
| 1460 | { |
| 1461 | Rom *rom; |
| 1462 | RomSec *cand; |
| 1463 | RomGap res = {0, 0}; |
| 1464 | hwaddr gapstart = base; |
| 1465 | GList *it, *secs = NULL; |
| 1466 | int count = 0; |
| 1467 | |
| 1468 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1469 | /* Ignore blobs being loaded to special places */ |
| 1470 | if (rom->mr || rom->fw_file) { |
| 1471 | continue; |
| 1472 | } |
| 1473 | /* ignore anything finishing below base */ |
| 1474 | if (rom->addr + rom->romsize <= base) { |
| 1475 | continue; |
| 1476 | } |
| 1477 | /* ignore anything starting above the region */ |
| 1478 | if (rom->addr >= base + size) { |
| 1479 | continue; |
| 1480 | } |
| 1481 | |
| 1482 | /* Save the start and end of each relevant ROM */ |
| 1483 | secs = add_romsec_to_list(secs, rom->addr, 1); |
| 1484 | |
| 1485 | if (rom->addr + rom->romsize < base + size) { |
| 1486 | secs = add_romsec_to_list(secs, rom->addr + rom->romsize, -1); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | /* sentinel */ |
| 1491 | secs = add_romsec_to_list(secs, base + size, 1); |
| 1492 | |
| 1493 | secs = g_list_sort_with_data(secs, sort_secs, NULL); |
| 1494 | |
| 1495 | for (it = g_list_first(secs); it; it = g_list_next(it)) { |
| 1496 | cand = (RomSec *) it->data; |
| 1497 | if (count == 0 && count + cand->se == 1) { |
| 1498 | size_t gap = cand->base - gapstart; |
| 1499 | if (gap > res.size) { |
| 1500 | res.base = gapstart; |
| 1501 | res.size = gap; |
| 1502 | } |
| 1503 | } else if (count == 1 && count + cand->se == 0) { |
| 1504 | gapstart = cand->base; |
| 1505 | } |
| 1506 | count += cand->se; |
| 1507 | } |
| 1508 | |
| 1509 | g_list_free_full(secs, g_free); |
| 1510 | return res; |
| 1511 | } |
| 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 | { |
| 1520 | hwaddr end = addr + size; |
| 1521 | uint8_t *s, *d = dest; |
| 1522 | size_t l = 0; |
| 1523 | Rom *rom; |
| 1524 | |
| 1525 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1526 | if (rom->fw_file) { |
| 1527 | continue; |
| 1528 | } |
| 1529 | if (rom->mr) { |
| 1530 | continue; |
| 1531 | } |
| 1532 | if (rom->addr + rom->romsize < addr) { |
| 1533 | continue; |
| 1534 | } |
| 1535 | if (rom->addr > end || rom->addr < addr) { |
| 1536 | break; |
| 1537 | } |
| 1538 | |
| 1539 | d = dest + (rom->addr - addr); |
| 1540 | s = rom->data; |
| 1541 | l = rom->datasize; |
| 1542 | |
| 1543 | if ((d + l) > (dest + size)) { |
| 1544 | l = dest - d; |
| 1545 | } |
| 1546 | |
| 1547 | if (l > 0) { |
| 1548 | memcpy(d, s, l); |
| 1549 | } |
| 1550 | |
| 1551 | 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 | d += l; |
| 1557 | l = rom->romsize - rom->datasize; |
| 1558 | |
| 1559 | if ((d + l) > (dest + size)) { |
| 1560 | /* Rom size doesn't fit in the destination area. Adjust to avoid |
| 1561 | * overflow. |
| 1562 | */ |
| 1563 | l = dest - d; |
| 1564 | } |
| 1565 | |
| 1566 | if (l > 0) { |
| 1567 | memset(d, 0x0, l); |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | return (d + l) - dest; |
| 1573 | } |
| 1574 | |
| 1575 | void *rom_ptr(hwaddr addr, size_t size) |
| 1576 | { |
| 1577 | Rom *rom; |
| 1578 | |
| 1579 | rom = find_rom(addr, size); |
| 1580 | if (!rom || !rom->data) |
| 1581 | return NULL; |
| 1582 | return rom->data + (addr - rom->addr); |
| 1583 | } |
| 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 | { |
| 1595 | FindRomCBData *cbdata = opaque; |
| 1596 | hwaddr alias_addr; |
| 1597 | |
| 1598 | if (mr != cbdata->mr) { |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
| 1602 | alias_addr = int128_get64(start) + cbdata->xlat - offset_in_region; |
| 1603 | cbdata->rom = rom_ptr(alias_addr, cbdata->size); |
| 1604 | if (!cbdata->rom) { |
| 1605 | return false; |
| 1606 | } |
| 1607 | /* Found a match, stop iterating */ |
| 1608 | return true; |
| 1609 | } |
| 1610 | |
| 1611 | void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size) |
| 1612 | { |
| 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 | FlatView *fv; |
| 1636 | void *rom; |
| 1637 | hwaddr len_unused; |
| 1638 | FindRomCBData cbdata = {}; |
| 1639 | |
| 1640 | /* Easy case: there's data at the actual address */ |
| 1641 | rom = rom_ptr(addr, size); |
| 1642 | if (rom) { |
| 1643 | return rom; |
| 1644 | } |
| 1645 | |
| 1646 | RCU_READ_LOCK_GUARD(); |
| 1647 | |
| 1648 | fv = address_space_to_flatview(as); |
| 1649 | cbdata.mr = flatview_translate(fv, addr, &cbdata.xlat, &len_unused, |
| 1650 | false, MEMTXATTRS_UNSPECIFIED); |
| 1651 | if (!cbdata.mr) { |
| 1652 | /* Nothing at this address, so there can't be any aliasing */ |
| 1653 | return NULL; |
| 1654 | } |
| 1655 | cbdata.size = size; |
| 1656 | flatview_for_each_range(fv, find_rom_cb, &cbdata); |
| 1657 | return cbdata.rom; |
| 1658 | } |
| 1659 | |
| 1660 | HumanReadableText *qmp_x_query_roms(Error **errp) |
| 1661 | { |
| 1662 | Rom *rom; |
| 1663 | g_autoptr(GString) buf = g_string_new(""); |
| 1664 | |
| 1665 | QTAILQ_FOREACH(rom, &roms, next) { |
| 1666 | if (rom->mr) { |
| 1667 | g_string_append_printf(buf, "%s" |
| 1668 | " size=0x%06zx name=\"%s\"\n", |
| 1669 | memory_region_name(rom->mr), |
| 1670 | rom->romsize, |
| 1671 | rom->name); |
| 1672 | } else if (!rom->fw_file) { |
| 1673 | g_string_append_printf(buf, "addr=" HWADDR_FMT_plx |
| 1674 | " size=0x%06zx mem=%s name=\"%s\"\n", |
| 1675 | rom->addr, rom->romsize, |
| 1676 | rom->isrom ? "rom" : "ram", |
| 1677 | rom->name); |
| 1678 | } else { |
| 1679 | g_string_append_printf(buf, "fw=%s/%s" |
| 1680 | " size=0x%06zx name=\"%s\"\n", |
| 1681 | rom->fw_dir, |
| 1682 | rom->fw_file, |
| 1683 | rom->romsize, |
| 1684 | rom->name); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | return human_readable_text_from_str(buf); |
| 1689 | } |
| 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 | #define NEXT_ADDR_MASK 0xffff0000 |
| 1705 | |
| 1706 | #define DATA_FIELD_MAX_LEN 0xff |
| 1707 | #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 | { |
| 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 | uint8_t value = 0; |
| 1730 | uint32_t idx = *index; |
| 1731 | /* ignore space */ |
| 1732 | if (g_ascii_isspace(c)) { |
| 1733 | return true; |
| 1734 | } |
| 1735 | if (!g_ascii_isxdigit(c) || !in_process) { |
| 1736 | return false; |
| 1737 | } |
| 1738 | value = g_ascii_xdigit_value(c); |
| 1739 | value = (idx & 0x1) ? (value & 0xf) : (value << 4); |
| 1740 | if (idx < 2) { |
| 1741 | line->byte_count |= value; |
| 1742 | } else if (2 <= idx && idx < 6) { |
| 1743 | line->address <<= 4; |
| 1744 | line->address += g_ascii_xdigit_value(c); |
| 1745 | } else if (6 <= idx && idx < 8) { |
| 1746 | line->record_type |= value; |
| 1747 | } else if (8 <= idx && idx < 8 + 2 * line->byte_count) { |
| 1748 | line->data[(idx - 8) >> 1] |= value; |
| 1749 | } else if (8 + 2 * line->byte_count <= idx && |
| 1750 | idx < 10 + 2 * line->byte_count) { |
| 1751 | line->checksum |= value; |
| 1752 | } else { |
| 1753 | return false; |
| 1754 | } |
| 1755 | *our_checksum += value; |
| 1756 | ++(*index); |
| 1757 | return true; |
| 1758 | } |
| 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 | { |
| 1777 | HexLine *line = &(parser->line); |
| 1778 | switch (line->record_type) { |
| 1779 | case DATA_RECORD: |
| 1780 | parser->current_address = |
| 1781 | (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address; |
| 1782 | /* verify this is a contiguous block of memory */ |
| 1783 | if (parser->current_address != parser->next_address_to_write) { |
| 1784 | if (parser->current_rom_index != 0) { |
| 1785 | rom_add_blob_fixed_as(parser->filename, parser->bin_buf, |
| 1786 | parser->current_rom_index, |
| 1787 | parser->rom_start_address, parser->as); |
| 1788 | } |
| 1789 | parser->rom_start_address = parser->current_address; |
| 1790 | parser->current_rom_index = 0; |
| 1791 | } |
| 1792 | |
| 1793 | /* copy from line buffer to output bin_buf */ |
| 1794 | memcpy(parser->bin_buf + parser->current_rom_index, line->data, |
| 1795 | line->byte_count); |
| 1796 | parser->current_rom_index += line->byte_count; |
| 1797 | parser->total_size += line->byte_count; |
| 1798 | /* save next address to write */ |
| 1799 | parser->next_address_to_write = |
| 1800 | parser->current_address + line->byte_count; |
| 1801 | break; |
| 1802 | |
| 1803 | case EOF_RECORD: |
| 1804 | if (parser->current_rom_index != 0) { |
| 1805 | rom_add_blob_fixed_as(parser->filename, parser->bin_buf, |
| 1806 | parser->current_rom_index, |
| 1807 | parser->rom_start_address, parser->as); |
| 1808 | } |
| 1809 | parser->complete = true; |
| 1810 | return parser->total_size; |
| 1811 | case EXT_SEG_ADDR_RECORD: |
| 1812 | case EXT_LINEAR_ADDR_RECORD: |
| 1813 | if (line->byte_count != 2 && line->address != 0) { |
| 1814 | return -1; |
| 1815 | } |
| 1816 | |
| 1817 | if (parser->current_rom_index != 0) { |
| 1818 | rom_add_blob_fixed_as(parser->filename, parser->bin_buf, |
| 1819 | parser->current_rom_index, |
| 1820 | parser->rom_start_address, parser->as); |
| 1821 | } |
| 1822 | |
| 1823 | /* save next address to write, |
| 1824 | * in case of non-contiguous block of memory */ |
| 1825 | parser->next_address_to_write = (line->data[0] << 12) | |
| 1826 | (line->data[1] << 4); |
| 1827 | if (line->record_type == EXT_LINEAR_ADDR_RECORD) { |
| 1828 | parser->next_address_to_write <<= 12; |
| 1829 | } |
| 1830 | |
| 1831 | parser->rom_start_address = parser->next_address_to_write; |
| 1832 | parser->current_rom_index = 0; |
| 1833 | break; |
| 1834 | |
| 1835 | case START_SEG_ADDR_RECORD: |
| 1836 | if (line->byte_count != 4 && line->address != 0) { |
| 1837 | return -1; |
| 1838 | } |
| 1839 | |
| 1840 | /* x86 16-bit CS:IP segmented addressing */ |
| 1841 | *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) + |
| 1842 | ((line->data[2] << 8) | line->data[3]); |
| 1843 | break; |
| 1844 | |
| 1845 | case START_LINEAR_ADDR_RECORD: |
| 1846 | if (line->byte_count != 4 && line->address != 0) { |
| 1847 | return -1; |
| 1848 | } |
| 1849 | |
| 1850 | *(parser->start_addr) = ldl_be_p(line->data); |
| 1851 | break; |
| 1852 | |
| 1853 | default: |
| 1854 | return -1; |
| 1855 | } |
| 1856 | |
| 1857 | return parser->total_size; |
| 1858 | } |
| 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 | { |
| 1864 | bool in_process = false; /* avoid re-enter and |
| 1865 | * check whether record begin with ':' */ |
| 1866 | uint8_t *end = hex_blob + hex_blob_size; |
| 1867 | uint8_t our_checksum = 0; |
| 1868 | uint32_t record_index = 0; |
| 1869 | HexParser parser = { |
| 1870 | .filename = filename, |
| 1871 | .bin_buf = g_malloc(hex_blob_size), |
| 1872 | .start_addr = addr, |
| 1873 | .as = as, |
| 1874 | .complete = false |
| 1875 | }; |
| 1876 | |
| 1877 | rom_transaction_begin(); |
| 1878 | |
| 1879 | for (; hex_blob < end && !parser.complete; ++hex_blob) { |
| 1880 | switch (*hex_blob) { |
| 1881 | case '\r': |
| 1882 | case '\n': |
| 1883 | if (!in_process) { |
| 1884 | break; |
| 1885 | } |
| 1886 | |
| 1887 | in_process = false; |
| 1888 | if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 != |
| 1889 | record_index || |
| 1890 | our_checksum != 0) { |
| 1891 | parser.total_size = -1; |
| 1892 | goto out; |
| 1893 | } |
| 1894 | |
| 1895 | if (handle_record_type(&parser) == -1) { |
| 1896 | parser.total_size = -1; |
| 1897 | goto out; |
| 1898 | } |
| 1899 | break; |
| 1900 | |
| 1901 | /* start of a new record. */ |
| 1902 | case ':': |
| 1903 | memset(&parser.line, 0, sizeof(HexLine)); |
| 1904 | in_process = true; |
| 1905 | record_index = 0; |
| 1906 | break; |
| 1907 | |
| 1908 | /* decoding lines */ |
| 1909 | default: |
| 1910 | if (!parse_record(&parser.line, &our_checksum, *hex_blob, |
| 1911 | &record_index, in_process)) { |
| 1912 | parser.total_size = -1; |
| 1913 | goto out; |
| 1914 | } |
| 1915 | break; |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | out: |
| 1920 | g_free(parser.bin_buf); |
| 1921 | rom_transaction_end(parser.total_size != -1); |
| 1922 | return parser.total_size; |
| 1923 | } |
| 1924 | |
| 1925 | /* return size or -1 if error */ |
| 1926 | ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry, |
| 1927 | AddressSpace *as) |
| 1928 | { |
| 1929 | gsize hex_blob_size; |
| 1930 | gchar *hex_blob; |
| 1931 | ssize_t total_size = 0; |
| 1932 | |
| 1933 | if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) { |
| 1934 | return -1; |
| 1935 | } |
| 1936 | |
| 1937 | total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob, |
| 1938 | hex_blob_size, as); |
| 1939 | |
| 1940 | g_free(hex_blob); |
| 1941 | return total_size; |
| 1942 | } |