loader.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. #include "hw.h"
  45. #include "disas/disas.h"
  46. #include "monitor/monitor.h"
  47. #include "sysemu/sysemu.h"
  48. #include "uboot_image.h"
  49. #include "loader.h"
  50. #include "fw_cfg.h"
  51. #include "exec/memory.h"
  52. #include "exec/address-spaces.h"
  53. #include <zlib.h>
  54. static int roms_loaded;
  55. /* return the size or -1 if error */
  56. int get_image_size(const char *filename)
  57. {
  58. int fd, size;
  59. fd = open(filename, O_RDONLY | O_BINARY);
  60. if (fd < 0)
  61. return -1;
  62. size = lseek(fd, 0, SEEK_END);
  63. close(fd);
  64. return size;
  65. }
  66. /* return the size or -1 if error */
  67. /* deprecated, because caller does not specify buffer size! */
  68. int load_image(const char *filename, uint8_t *addr)
  69. {
  70. int fd, size;
  71. fd = open(filename, O_RDONLY | O_BINARY);
  72. if (fd < 0)
  73. return -1;
  74. size = lseek(fd, 0, SEEK_END);
  75. lseek(fd, 0, SEEK_SET);
  76. if (read(fd, addr, size) != size) {
  77. close(fd);
  78. return -1;
  79. }
  80. close(fd);
  81. return size;
  82. }
  83. /* read()-like version */
  84. ssize_t read_targphys(const char *name,
  85. int fd, hwaddr dst_addr, size_t nbytes)
  86. {
  87. uint8_t *buf;
  88. ssize_t did;
  89. buf = g_malloc(nbytes);
  90. did = read(fd, buf, nbytes);
  91. if (did > 0)
  92. rom_add_blob_fixed("read", buf, did, dst_addr);
  93. g_free(buf);
  94. return did;
  95. }
  96. /* return the size or -1 if error */
  97. int load_image_targphys(const char *filename,
  98. hwaddr addr, uint64_t max_sz)
  99. {
  100. int size;
  101. size = get_image_size(filename);
  102. if (size > max_sz) {
  103. return -1;
  104. }
  105. if (size > 0) {
  106. rom_add_file_fixed(filename, addr, -1);
  107. }
  108. return size;
  109. }
  110. void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
  111. const char *source)
  112. {
  113. const char *nulp;
  114. char *ptr;
  115. if (buf_size <= 0) return;
  116. nulp = memchr(source, 0, buf_size);
  117. if (nulp) {
  118. rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
  119. } else {
  120. rom_add_blob_fixed(name, source, buf_size, dest);
  121. ptr = rom_ptr(dest + buf_size - 1);
  122. *ptr = 0;
  123. }
  124. }
  125. /* A.OUT loader */
  126. struct exec
  127. {
  128. uint32_t a_info; /* Use macros N_MAGIC, etc for access */
  129. uint32_t a_text; /* length of text, in bytes */
  130. uint32_t a_data; /* length of data, in bytes */
  131. uint32_t a_bss; /* length of uninitialized data area, in bytes */
  132. uint32_t a_syms; /* length of symbol table data in file, in bytes */
  133. uint32_t a_entry; /* start address */
  134. uint32_t a_trsize; /* length of relocation info for text, in bytes */
  135. uint32_t a_drsize; /* length of relocation info for data, in bytes */
  136. };
  137. static void bswap_ahdr(struct exec *e)
  138. {
  139. bswap32s(&e->a_info);
  140. bswap32s(&e->a_text);
  141. bswap32s(&e->a_data);
  142. bswap32s(&e->a_bss);
  143. bswap32s(&e->a_syms);
  144. bswap32s(&e->a_entry);
  145. bswap32s(&e->a_trsize);
  146. bswap32s(&e->a_drsize);
  147. }
  148. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  149. #define OMAGIC 0407
  150. #define NMAGIC 0410
  151. #define ZMAGIC 0413
  152. #define QMAGIC 0314
  153. #define _N_HDROFF(x) (1024 - sizeof (struct exec))
  154. #define N_TXTOFF(x) \
  155. (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
  156. (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
  157. #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
  158. #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
  159. #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
  160. #define N_DATADDR(x, target_page_size) \
  161. (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
  162. : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
  163. int load_aout(const char *filename, hwaddr addr, int max_sz,
  164. int bswap_needed, hwaddr target_page_size)
  165. {
  166. int fd;
  167. ssize_t size, ret;
  168. struct exec e;
  169. uint32_t magic;
  170. fd = open(filename, O_RDONLY | O_BINARY);
  171. if (fd < 0)
  172. return -1;
  173. size = read(fd, &e, sizeof(e));
  174. if (size < 0)
  175. goto fail;
  176. if (bswap_needed) {
  177. bswap_ahdr(&e);
  178. }
  179. magic = N_MAGIC(e);
  180. switch (magic) {
  181. case ZMAGIC:
  182. case QMAGIC:
  183. case OMAGIC:
  184. if (e.a_text + e.a_data > max_sz)
  185. goto fail;
  186. lseek(fd, N_TXTOFF(e), SEEK_SET);
  187. size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
  188. if (size < 0)
  189. goto fail;
  190. break;
  191. case NMAGIC:
  192. if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
  193. goto fail;
  194. lseek(fd, N_TXTOFF(e), SEEK_SET);
  195. size = read_targphys(filename, fd, addr, e.a_text);
  196. if (size < 0)
  197. goto fail;
  198. ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
  199. e.a_data);
  200. if (ret < 0)
  201. goto fail;
  202. size += ret;
  203. break;
  204. default:
  205. goto fail;
  206. }
  207. close(fd);
  208. return size;
  209. fail:
  210. close(fd);
  211. return -1;
  212. }
  213. /* ELF loader */
  214. static void *load_at(int fd, int offset, int size)
  215. {
  216. void *ptr;
  217. if (lseek(fd, offset, SEEK_SET) < 0)
  218. return NULL;
  219. ptr = g_malloc(size);
  220. if (read(fd, ptr, size) != size) {
  221. g_free(ptr);
  222. return NULL;
  223. }
  224. return ptr;
  225. }
  226. #ifdef ELF_CLASS
  227. #undef ELF_CLASS
  228. #endif
  229. #define ELF_CLASS ELFCLASS32
  230. #include "elf.h"
  231. #define SZ 32
  232. #define elf_word uint32_t
  233. #define elf_sword int32_t
  234. #define bswapSZs bswap32s
  235. #include "elf_ops.h"
  236. #undef elfhdr
  237. #undef elf_phdr
  238. #undef elf_shdr
  239. #undef elf_sym
  240. #undef elf_note
  241. #undef elf_word
  242. #undef elf_sword
  243. #undef bswapSZs
  244. #undef SZ
  245. #define elfhdr elf64_hdr
  246. #define elf_phdr elf64_phdr
  247. #define elf_note elf64_note
  248. #define elf_shdr elf64_shdr
  249. #define elf_sym elf64_sym
  250. #define elf_word uint64_t
  251. #define elf_sword int64_t
  252. #define bswapSZs bswap64s
  253. #define SZ 64
  254. #include "elf_ops.h"
  255. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  256. int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),
  257. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  258. uint64_t *highaddr, int big_endian, int elf_machine, int clear_lsb)
  259. {
  260. int fd, data_order, target_data_order, must_swab, ret;
  261. uint8_t e_ident[EI_NIDENT];
  262. fd = open(filename, O_RDONLY | O_BINARY);
  263. if (fd < 0) {
  264. perror(filename);
  265. return -1;
  266. }
  267. if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
  268. goto fail;
  269. if (e_ident[0] != ELFMAG0 ||
  270. e_ident[1] != ELFMAG1 ||
  271. e_ident[2] != ELFMAG2 ||
  272. e_ident[3] != ELFMAG3)
  273. goto fail;
  274. #ifdef HOST_WORDS_BIGENDIAN
  275. data_order = ELFDATA2MSB;
  276. #else
  277. data_order = ELFDATA2LSB;
  278. #endif
  279. must_swab = data_order != e_ident[EI_DATA];
  280. if (big_endian) {
  281. target_data_order = ELFDATA2MSB;
  282. } else {
  283. target_data_order = ELFDATA2LSB;
  284. }
  285. if (target_data_order != e_ident[EI_DATA]) {
  286. goto fail;
  287. }
  288. lseek(fd, 0, SEEK_SET);
  289. if (e_ident[EI_CLASS] == ELFCLASS64) {
  290. ret = load_elf64(filename, fd, translate_fn, translate_opaque, must_swab,
  291. pentry, lowaddr, highaddr, elf_machine, clear_lsb);
  292. } else {
  293. ret = load_elf32(filename, fd, translate_fn, translate_opaque, must_swab,
  294. pentry, lowaddr, highaddr, elf_machine, clear_lsb);
  295. }
  296. close(fd);
  297. return ret;
  298. fail:
  299. close(fd);
  300. return -1;
  301. }
  302. static void bswap_uboot_header(uboot_image_header_t *hdr)
  303. {
  304. #ifndef HOST_WORDS_BIGENDIAN
  305. bswap32s(&hdr->ih_magic);
  306. bswap32s(&hdr->ih_hcrc);
  307. bswap32s(&hdr->ih_time);
  308. bswap32s(&hdr->ih_size);
  309. bswap32s(&hdr->ih_load);
  310. bswap32s(&hdr->ih_ep);
  311. bswap32s(&hdr->ih_dcrc);
  312. #endif
  313. }
  314. #define ZALLOC_ALIGNMENT 16
  315. static void *zalloc(void *x, unsigned items, unsigned size)
  316. {
  317. void *p;
  318. size *= items;
  319. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  320. p = g_malloc(size);
  321. return (p);
  322. }
  323. static void zfree(void *x, void *addr)
  324. {
  325. g_free(addr);
  326. }
  327. #define HEAD_CRC 2
  328. #define EXTRA_FIELD 4
  329. #define ORIG_NAME 8
  330. #define COMMENT 0x10
  331. #define RESERVED 0xe0
  332. #define DEFLATED 8
  333. /* This is the usual maximum in uboot, so if a uImage overflows this, it would
  334. * overflow on real hardware too. */
  335. #define UBOOT_MAX_GUNZIP_BYTES (64 << 20)
  336. static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
  337. size_t srclen)
  338. {
  339. z_stream s;
  340. ssize_t dstbytes;
  341. int r, i, flags;
  342. /* skip header */
  343. i = 10;
  344. flags = src[3];
  345. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  346. puts ("Error: Bad gzipped data\n");
  347. return -1;
  348. }
  349. if ((flags & EXTRA_FIELD) != 0)
  350. i = 12 + src[10] + (src[11] << 8);
  351. if ((flags & ORIG_NAME) != 0)
  352. while (src[i++] != 0)
  353. ;
  354. if ((flags & COMMENT) != 0)
  355. while (src[i++] != 0)
  356. ;
  357. if ((flags & HEAD_CRC) != 0)
  358. i += 2;
  359. if (i >= srclen) {
  360. puts ("Error: gunzip out of data in header\n");
  361. return -1;
  362. }
  363. s.zalloc = zalloc;
  364. s.zfree = zfree;
  365. r = inflateInit2(&s, -MAX_WBITS);
  366. if (r != Z_OK) {
  367. printf ("Error: inflateInit2() returned %d\n", r);
  368. return (-1);
  369. }
  370. s.next_in = src + i;
  371. s.avail_in = srclen - i;
  372. s.next_out = dst;
  373. s.avail_out = dstlen;
  374. r = inflate(&s, Z_FINISH);
  375. if (r != Z_OK && r != Z_STREAM_END) {
  376. printf ("Error: inflate() returned %d\n", r);
  377. return -1;
  378. }
  379. dstbytes = s.next_out - (unsigned char *) dst;
  380. inflateEnd(&s);
  381. return dstbytes;
  382. }
  383. /* Load a U-Boot image. */
  384. int load_uimage(const char *filename, hwaddr *ep,
  385. hwaddr *loadaddr, int *is_linux)
  386. {
  387. int fd;
  388. int size;
  389. uboot_image_header_t h;
  390. uboot_image_header_t *hdr = &h;
  391. uint8_t *data = NULL;
  392. int ret = -1;
  393. fd = open(filename, O_RDONLY | O_BINARY);
  394. if (fd < 0)
  395. return -1;
  396. size = read(fd, hdr, sizeof(uboot_image_header_t));
  397. if (size < 0)
  398. goto out;
  399. bswap_uboot_header(hdr);
  400. if (hdr->ih_magic != IH_MAGIC)
  401. goto out;
  402. /* TODO: Implement other image types. */
  403. if (hdr->ih_type != IH_TYPE_KERNEL) {
  404. fprintf(stderr, "Can only load u-boot image type \"kernel\"\n");
  405. goto out;
  406. }
  407. switch (hdr->ih_comp) {
  408. case IH_COMP_NONE:
  409. case IH_COMP_GZIP:
  410. break;
  411. default:
  412. fprintf(stderr,
  413. "Unable to load u-boot images with compression type %d\n",
  414. hdr->ih_comp);
  415. goto out;
  416. }
  417. /* TODO: Check CPU type. */
  418. if (is_linux) {
  419. if (hdr->ih_os == IH_OS_LINUX)
  420. *is_linux = 1;
  421. else
  422. *is_linux = 0;
  423. }
  424. *ep = hdr->ih_ep;
  425. data = g_malloc(hdr->ih_size);
  426. if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
  427. fprintf(stderr, "Error reading file\n");
  428. goto out;
  429. }
  430. if (hdr->ih_comp == IH_COMP_GZIP) {
  431. uint8_t *compressed_data;
  432. size_t max_bytes;
  433. ssize_t bytes;
  434. compressed_data = data;
  435. max_bytes = UBOOT_MAX_GUNZIP_BYTES;
  436. data = g_malloc(max_bytes);
  437. bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
  438. g_free(compressed_data);
  439. if (bytes < 0) {
  440. fprintf(stderr, "Unable to decompress gzipped image!\n");
  441. goto out;
  442. }
  443. hdr->ih_size = bytes;
  444. }
  445. rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load);
  446. if (loadaddr)
  447. *loadaddr = hdr->ih_load;
  448. ret = hdr->ih_size;
  449. out:
  450. if (data)
  451. g_free(data);
  452. close(fd);
  453. return ret;
  454. }
  455. /*
  456. * Functions for reboot-persistent memory regions.
  457. * - used for vga bios and option roms.
  458. * - also linux kernel (-kernel / -initrd).
  459. */
  460. typedef struct Rom Rom;
  461. struct Rom {
  462. char *name;
  463. char *path;
  464. size_t romsize;
  465. uint8_t *data;
  466. int isrom;
  467. char *fw_dir;
  468. char *fw_file;
  469. hwaddr addr;
  470. QTAILQ_ENTRY(Rom) next;
  471. };
  472. static FWCfgState *fw_cfg;
  473. static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
  474. static void rom_insert(Rom *rom)
  475. {
  476. Rom *item;
  477. if (roms_loaded) {
  478. hw_error ("ROM images must be loaded at startup\n");
  479. }
  480. /* list is ordered by load address */
  481. QTAILQ_FOREACH(item, &roms, next) {
  482. if (rom->addr >= item->addr)
  483. continue;
  484. QTAILQ_INSERT_BEFORE(item, rom, next);
  485. return;
  486. }
  487. QTAILQ_INSERT_TAIL(&roms, rom, next);
  488. }
  489. int rom_add_file(const char *file, const char *fw_dir,
  490. hwaddr addr, int32_t bootindex)
  491. {
  492. Rom *rom;
  493. int rc, fd = -1;
  494. char devpath[100];
  495. rom = g_malloc0(sizeof(*rom));
  496. rom->name = g_strdup(file);
  497. rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
  498. if (rom->path == NULL) {
  499. rom->path = g_strdup(file);
  500. }
  501. fd = open(rom->path, O_RDONLY | O_BINARY);
  502. if (fd == -1) {
  503. fprintf(stderr, "Could not open option rom '%s': %s\n",
  504. rom->path, strerror(errno));
  505. goto err;
  506. }
  507. if (fw_dir) {
  508. rom->fw_dir = g_strdup(fw_dir);
  509. rom->fw_file = g_strdup(file);
  510. }
  511. rom->addr = addr;
  512. rom->romsize = lseek(fd, 0, SEEK_END);
  513. rom->data = g_malloc0(rom->romsize);
  514. lseek(fd, 0, SEEK_SET);
  515. rc = read(fd, rom->data, rom->romsize);
  516. if (rc != rom->romsize) {
  517. fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
  518. rom->name, rc, rom->romsize);
  519. goto err;
  520. }
  521. close(fd);
  522. rom_insert(rom);
  523. if (rom->fw_file && fw_cfg) {
  524. const char *basename;
  525. char fw_file_name[56];
  526. basename = strrchr(rom->fw_file, '/');
  527. if (basename) {
  528. basename++;
  529. } else {
  530. basename = rom->fw_file;
  531. }
  532. snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
  533. basename);
  534. fw_cfg_add_file(fw_cfg, fw_file_name, rom->data, rom->romsize);
  535. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  536. } else {
  537. snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
  538. }
  539. add_boot_device_path(bootindex, NULL, devpath);
  540. return 0;
  541. err:
  542. if (fd != -1)
  543. close(fd);
  544. g_free(rom->data);
  545. g_free(rom->path);
  546. g_free(rom->name);
  547. g_free(rom);
  548. return -1;
  549. }
  550. int rom_add_blob(const char *name, const void *blob, size_t len,
  551. hwaddr addr)
  552. {
  553. Rom *rom;
  554. rom = g_malloc0(sizeof(*rom));
  555. rom->name = g_strdup(name);
  556. rom->addr = addr;
  557. rom->romsize = len;
  558. rom->data = g_malloc0(rom->romsize);
  559. memcpy(rom->data, blob, len);
  560. rom_insert(rom);
  561. return 0;
  562. }
  563. int rom_add_vga(const char *file)
  564. {
  565. return rom_add_file(file, "vgaroms", 0, -1);
  566. }
  567. int rom_add_option(const char *file, int32_t bootindex)
  568. {
  569. return rom_add_file(file, "genroms", 0, bootindex);
  570. }
  571. static void rom_reset(void *unused)
  572. {
  573. Rom *rom;
  574. QTAILQ_FOREACH(rom, &roms, next) {
  575. if (rom->fw_file) {
  576. continue;
  577. }
  578. if (rom->data == NULL) {
  579. continue;
  580. }
  581. cpu_physical_memory_write_rom(rom->addr, rom->data, rom->romsize);
  582. if (rom->isrom) {
  583. /* rom needs to be written only once */
  584. g_free(rom->data);
  585. rom->data = NULL;
  586. }
  587. }
  588. }
  589. int rom_load_all(void)
  590. {
  591. hwaddr addr = 0;
  592. MemoryRegionSection section;
  593. Rom *rom;
  594. QTAILQ_FOREACH(rom, &roms, next) {
  595. if (rom->fw_file) {
  596. continue;
  597. }
  598. if (addr > rom->addr) {
  599. fprintf(stderr, "rom: requested regions overlap "
  600. "(rom %s. free=0x" TARGET_FMT_plx
  601. ", addr=0x" TARGET_FMT_plx ")\n",
  602. rom->name, addr, rom->addr);
  603. return -1;
  604. }
  605. addr = rom->addr;
  606. addr += rom->romsize;
  607. section = memory_region_find(get_system_memory(), rom->addr, 1);
  608. rom->isrom = section.size && memory_region_is_rom(section.mr);
  609. }
  610. qemu_register_reset(rom_reset, NULL);
  611. roms_loaded = 1;
  612. return 0;
  613. }
  614. void rom_set_fw(void *f)
  615. {
  616. fw_cfg = f;
  617. }
  618. static Rom *find_rom(hwaddr addr)
  619. {
  620. Rom *rom;
  621. QTAILQ_FOREACH(rom, &roms, next) {
  622. if (rom->fw_file) {
  623. continue;
  624. }
  625. if (rom->addr > addr) {
  626. continue;
  627. }
  628. if (rom->addr + rom->romsize < addr) {
  629. continue;
  630. }
  631. return rom;
  632. }
  633. return NULL;
  634. }
  635. /*
  636. * Copies memory from registered ROMs to dest. Any memory that is contained in
  637. * a ROM between addr and addr + size is copied. Note that this can involve
  638. * multiple ROMs, which need not start at addr and need not end at addr + size.
  639. */
  640. int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
  641. {
  642. hwaddr end = addr + size;
  643. uint8_t *s, *d = dest;
  644. size_t l = 0;
  645. Rom *rom;
  646. QTAILQ_FOREACH(rom, &roms, next) {
  647. if (rom->fw_file) {
  648. continue;
  649. }
  650. if (rom->addr + rom->romsize < addr) {
  651. continue;
  652. }
  653. if (rom->addr > end) {
  654. break;
  655. }
  656. if (!rom->data) {
  657. continue;
  658. }
  659. d = dest + (rom->addr - addr);
  660. s = rom->data;
  661. l = rom->romsize;
  662. if ((d + l) > (dest + size)) {
  663. l = dest - d;
  664. }
  665. memcpy(d, s, l);
  666. }
  667. return (d + l) - dest;
  668. }
  669. void *rom_ptr(hwaddr addr)
  670. {
  671. Rom *rom;
  672. rom = find_rom(addr);
  673. if (!rom || !rom->data)
  674. return NULL;
  675. return rom->data + (addr - rom->addr);
  676. }
  677. void do_info_roms(Monitor *mon, const QDict *qdict)
  678. {
  679. Rom *rom;
  680. QTAILQ_FOREACH(rom, &roms, next) {
  681. if (!rom->fw_file) {
  682. monitor_printf(mon, "addr=" TARGET_FMT_plx
  683. " size=0x%06zx mem=%s name=\"%s\"\n",
  684. rom->addr, rom->romsize,
  685. rom->isrom ? "rom" : "ram",
  686. rom->name);
  687. } else {
  688. monitor_printf(mon, "fw=%s/%s"
  689. " size=0x%06zx name=\"%s\"\n",
  690. rom->fw_dir,
  691. rom->fw_file,
  692. rom->romsize,
  693. rom->name);
  694. }
  695. }
  696. }