loader.c 20 KB

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