loader.c 25 KB

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