2
0

loader.c 20 KB

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