loader.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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 "qemu/osdep.h"
  45. #include "qemu-common.h"
  46. #include "qapi/error.h"
  47. #include "trace.h"
  48. #include "hw/hw.h"
  49. #include "disas/disas.h"
  50. #include "migration/vmstate.h"
  51. #include "monitor/monitor.h"
  52. #include "sysemu/reset.h"
  53. #include "sysemu/sysemu.h"
  54. #include "uboot_image.h"
  55. #include "hw/loader.h"
  56. #include "hw/nvram/fw_cfg.h"
  57. #include "exec/memory.h"
  58. #include "exec/address-spaces.h"
  59. #include "hw/boards.h"
  60. #include "qemu/cutils.h"
  61. #include "sysemu/runstate.h"
  62. #include <zlib.h>
  63. static int roms_loaded;
  64. /* return the size or -1 if error */
  65. int64_t get_image_size(const char *filename)
  66. {
  67. int fd;
  68. int64_t size;
  69. fd = open(filename, O_RDONLY | O_BINARY);
  70. if (fd < 0)
  71. return -1;
  72. size = lseek(fd, 0, SEEK_END);
  73. close(fd);
  74. return size;
  75. }
  76. /* return the size or -1 if error */
  77. ssize_t load_image_size(const char *filename, void *addr, size_t size)
  78. {
  79. int fd;
  80. ssize_t actsize, l = 0;
  81. fd = open(filename, O_RDONLY | O_BINARY);
  82. if (fd < 0) {
  83. return -1;
  84. }
  85. while ((actsize = read(fd, addr + l, size - l)) > 0) {
  86. l += actsize;
  87. }
  88. close(fd);
  89. return actsize < 0 ? -1 : l;
  90. }
  91. /* read()-like version */
  92. ssize_t read_targphys(const char *name,
  93. int fd, hwaddr dst_addr, size_t nbytes)
  94. {
  95. uint8_t *buf;
  96. ssize_t did;
  97. buf = g_malloc(nbytes);
  98. did = read(fd, buf, nbytes);
  99. if (did > 0)
  100. rom_add_blob_fixed("read", buf, did, dst_addr);
  101. g_free(buf);
  102. return did;
  103. }
  104. int load_image_targphys(const char *filename,
  105. hwaddr addr, uint64_t max_sz)
  106. {
  107. return load_image_targphys_as(filename, addr, max_sz, NULL);
  108. }
  109. /* return the size or -1 if error */
  110. int load_image_targphys_as(const char *filename,
  111. hwaddr addr, uint64_t max_sz, AddressSpace *as)
  112. {
  113. int size;
  114. size = get_image_size(filename);
  115. if (size < 0 || size > max_sz) {
  116. return -1;
  117. }
  118. if (size > 0) {
  119. if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
  120. return -1;
  121. }
  122. }
  123. return size;
  124. }
  125. int load_image_mr(const char *filename, MemoryRegion *mr)
  126. {
  127. int size;
  128. if (!memory_access_is_direct(mr, false)) {
  129. /* Can only load an image into RAM or ROM */
  130. return -1;
  131. }
  132. size = get_image_size(filename);
  133. if (size < 0 || size > memory_region_size(mr)) {
  134. return -1;
  135. }
  136. if (size > 0) {
  137. if (rom_add_file_mr(filename, mr, -1) < 0) {
  138. return -1;
  139. }
  140. }
  141. return size;
  142. }
  143. void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
  144. const char *source)
  145. {
  146. const char *nulp;
  147. char *ptr;
  148. if (buf_size <= 0) return;
  149. nulp = memchr(source, 0, buf_size);
  150. if (nulp) {
  151. rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
  152. } else {
  153. rom_add_blob_fixed(name, source, buf_size, dest);
  154. ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr));
  155. *ptr = 0;
  156. }
  157. }
  158. /* A.OUT loader */
  159. struct exec
  160. {
  161. uint32_t a_info; /* Use macros N_MAGIC, etc for access */
  162. uint32_t a_text; /* length of text, in bytes */
  163. uint32_t a_data; /* length of data, in bytes */
  164. uint32_t a_bss; /* length of uninitialized data area, in bytes */
  165. uint32_t a_syms; /* length of symbol table data in file, in bytes */
  166. uint32_t a_entry; /* start address */
  167. uint32_t a_trsize; /* length of relocation info for text, in bytes */
  168. uint32_t a_drsize; /* length of relocation info for data, in bytes */
  169. };
  170. static void bswap_ahdr(struct exec *e)
  171. {
  172. bswap32s(&e->a_info);
  173. bswap32s(&e->a_text);
  174. bswap32s(&e->a_data);
  175. bswap32s(&e->a_bss);
  176. bswap32s(&e->a_syms);
  177. bswap32s(&e->a_entry);
  178. bswap32s(&e->a_trsize);
  179. bswap32s(&e->a_drsize);
  180. }
  181. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  182. #define OMAGIC 0407
  183. #define NMAGIC 0410
  184. #define ZMAGIC 0413
  185. #define QMAGIC 0314
  186. #define _N_HDROFF(x) (1024 - sizeof (struct exec))
  187. #define N_TXTOFF(x) \
  188. (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
  189. (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
  190. #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
  191. #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
  192. #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
  193. #define N_DATADDR(x, target_page_size) \
  194. (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
  195. : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
  196. int load_aout(const char *filename, hwaddr addr, int max_sz,
  197. int bswap_needed, hwaddr target_page_size)
  198. {
  199. int fd;
  200. ssize_t size, ret;
  201. struct exec e;
  202. uint32_t magic;
  203. fd = open(filename, O_RDONLY | O_BINARY);
  204. if (fd < 0)
  205. return -1;
  206. size = read(fd, &e, sizeof(e));
  207. if (size < 0)
  208. goto fail;
  209. if (bswap_needed) {
  210. bswap_ahdr(&e);
  211. }
  212. magic = N_MAGIC(e);
  213. switch (magic) {
  214. case ZMAGIC:
  215. case QMAGIC:
  216. case OMAGIC:
  217. if (e.a_text + e.a_data > max_sz)
  218. goto fail;
  219. lseek(fd, N_TXTOFF(e), SEEK_SET);
  220. size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
  221. if (size < 0)
  222. goto fail;
  223. break;
  224. case NMAGIC:
  225. if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
  226. goto fail;
  227. lseek(fd, N_TXTOFF(e), SEEK_SET);
  228. size = read_targphys(filename, fd, addr, e.a_text);
  229. if (size < 0)
  230. goto fail;
  231. ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
  232. e.a_data);
  233. if (ret < 0)
  234. goto fail;
  235. size += ret;
  236. break;
  237. default:
  238. goto fail;
  239. }
  240. close(fd);
  241. return size;
  242. fail:
  243. close(fd);
  244. return -1;
  245. }
  246. /* ELF loader */
  247. static void *load_at(int fd, off_t offset, size_t size)
  248. {
  249. void *ptr;
  250. if (lseek(fd, offset, SEEK_SET) < 0)
  251. return NULL;
  252. ptr = g_malloc(size);
  253. if (read(fd, ptr, size) != size) {
  254. g_free(ptr);
  255. return NULL;
  256. }
  257. return ptr;
  258. }
  259. #ifdef ELF_CLASS
  260. #undef ELF_CLASS
  261. #endif
  262. #define ELF_CLASS ELFCLASS32
  263. #include "elf.h"
  264. #define SZ 32
  265. #define elf_word uint32_t
  266. #define elf_sword int32_t
  267. #define bswapSZs bswap32s
  268. #include "hw/elf_ops.h"
  269. #undef elfhdr
  270. #undef elf_phdr
  271. #undef elf_shdr
  272. #undef elf_sym
  273. #undef elf_rela
  274. #undef elf_note
  275. #undef elf_word
  276. #undef elf_sword
  277. #undef bswapSZs
  278. #undef SZ
  279. #define elfhdr elf64_hdr
  280. #define elf_phdr elf64_phdr
  281. #define elf_note elf64_note
  282. #define elf_shdr elf64_shdr
  283. #define elf_sym elf64_sym
  284. #define elf_rela elf64_rela
  285. #define elf_word uint64_t
  286. #define elf_sword int64_t
  287. #define bswapSZs bswap64s
  288. #define SZ 64
  289. #include "hw/elf_ops.h"
  290. const char *load_elf_strerror(int error)
  291. {
  292. switch (error) {
  293. case 0:
  294. return "No error";
  295. case ELF_LOAD_FAILED:
  296. return "Failed to load ELF";
  297. case ELF_LOAD_NOT_ELF:
  298. return "The image is not ELF";
  299. case ELF_LOAD_WRONG_ARCH:
  300. return "The image is from incompatible architecture";
  301. case ELF_LOAD_WRONG_ENDIAN:
  302. return "The image has incorrect endianness";
  303. case ELF_LOAD_TOO_BIG:
  304. return "The image segments are too big to load";
  305. default:
  306. return "Unknown error";
  307. }
  308. }
  309. void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
  310. {
  311. int fd;
  312. uint8_t e_ident_local[EI_NIDENT];
  313. uint8_t *e_ident;
  314. size_t hdr_size, off;
  315. bool is64l;
  316. if (!hdr) {
  317. hdr = e_ident_local;
  318. }
  319. e_ident = hdr;
  320. fd = open(filename, O_RDONLY | O_BINARY);
  321. if (fd < 0) {
  322. error_setg_errno(errp, errno, "Failed to open file: %s", filename);
  323. return;
  324. }
  325. if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
  326. error_setg_errno(errp, errno, "Failed to read file: %s", filename);
  327. goto fail;
  328. }
  329. if (e_ident[0] != ELFMAG0 ||
  330. e_ident[1] != ELFMAG1 ||
  331. e_ident[2] != ELFMAG2 ||
  332. e_ident[3] != ELFMAG3) {
  333. error_setg(errp, "Bad ELF magic");
  334. goto fail;
  335. }
  336. is64l = e_ident[EI_CLASS] == ELFCLASS64;
  337. hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr);
  338. if (is64) {
  339. *is64 = is64l;
  340. }
  341. off = EI_NIDENT;
  342. while (hdr != e_ident_local && off < hdr_size) {
  343. size_t br = read(fd, hdr + off, hdr_size - off);
  344. switch (br) {
  345. case 0:
  346. error_setg(errp, "File too short: %s", filename);
  347. goto fail;
  348. case -1:
  349. error_setg_errno(errp, errno, "Failed to read file: %s",
  350. filename);
  351. goto fail;
  352. }
  353. off += br;
  354. }
  355. fail:
  356. close(fd);
  357. }
  358. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  359. int load_elf(const char *filename,
  360. uint64_t (*elf_note_fn)(void *, void *, bool),
  361. uint64_t (*translate_fn)(void *, uint64_t),
  362. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  363. uint64_t *highaddr, uint32_t *pflags, int big_endian,
  364. int elf_machine, int clear_lsb, int data_swab)
  365. {
  366. return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque,
  367. pentry, lowaddr, highaddr, pflags, big_endian,
  368. elf_machine, clear_lsb, data_swab, NULL);
  369. }
  370. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  371. int load_elf_as(const char *filename,
  372. uint64_t (*elf_note_fn)(void *, void *, bool),
  373. uint64_t (*translate_fn)(void *, uint64_t),
  374. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  375. uint64_t *highaddr, uint32_t *pflags, int big_endian,
  376. int elf_machine, int clear_lsb, int data_swab, AddressSpace *as)
  377. {
  378. return load_elf_ram(filename, elf_note_fn, translate_fn, translate_opaque,
  379. pentry, lowaddr, highaddr, pflags, big_endian,
  380. elf_machine, clear_lsb, data_swab, as, true);
  381. }
  382. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  383. int load_elf_ram(const char *filename,
  384. uint64_t (*elf_note_fn)(void *, void *, bool),
  385. uint64_t (*translate_fn)(void *, uint64_t),
  386. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  387. uint64_t *highaddr, uint32_t *pflags, int big_endian,
  388. int elf_machine, int clear_lsb, int data_swab,
  389. AddressSpace *as, bool load_rom)
  390. {
  391. return load_elf_ram_sym(filename, elf_note_fn,
  392. translate_fn, translate_opaque,
  393. pentry, lowaddr, highaddr, pflags, big_endian,
  394. elf_machine, clear_lsb, data_swab, as,
  395. load_rom, NULL);
  396. }
  397. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  398. int load_elf_ram_sym(const char *filename,
  399. uint64_t (*elf_note_fn)(void *, void *, bool),
  400. uint64_t (*translate_fn)(void *, uint64_t),
  401. void *translate_opaque, uint64_t *pentry,
  402. uint64_t *lowaddr, uint64_t *highaddr, uint32_t *pflags,
  403. int big_endian, int elf_machine,
  404. int clear_lsb, int data_swab,
  405. AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
  406. {
  407. int fd, data_order, target_data_order, must_swab, ret = ELF_LOAD_FAILED;
  408. uint8_t e_ident[EI_NIDENT];
  409. fd = open(filename, O_RDONLY | O_BINARY);
  410. if (fd < 0) {
  411. perror(filename);
  412. return -1;
  413. }
  414. if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
  415. goto fail;
  416. if (e_ident[0] != ELFMAG0 ||
  417. e_ident[1] != ELFMAG1 ||
  418. e_ident[2] != ELFMAG2 ||
  419. e_ident[3] != ELFMAG3) {
  420. ret = ELF_LOAD_NOT_ELF;
  421. goto fail;
  422. }
  423. #ifdef HOST_WORDS_BIGENDIAN
  424. data_order = ELFDATA2MSB;
  425. #else
  426. data_order = ELFDATA2LSB;
  427. #endif
  428. must_swab = data_order != e_ident[EI_DATA];
  429. if (big_endian) {
  430. target_data_order = ELFDATA2MSB;
  431. } else {
  432. target_data_order = ELFDATA2LSB;
  433. }
  434. if (target_data_order != e_ident[EI_DATA]) {
  435. ret = ELF_LOAD_WRONG_ENDIAN;
  436. goto fail;
  437. }
  438. lseek(fd, 0, SEEK_SET);
  439. if (e_ident[EI_CLASS] == ELFCLASS64) {
  440. ret = load_elf64(filename, fd, elf_note_fn,
  441. translate_fn, translate_opaque, must_swab,
  442. pentry, lowaddr, highaddr, pflags, elf_machine,
  443. clear_lsb, data_swab, as, load_rom, sym_cb);
  444. } else {
  445. ret = load_elf32(filename, fd, elf_note_fn,
  446. translate_fn, translate_opaque, must_swab,
  447. pentry, lowaddr, highaddr, pflags, elf_machine,
  448. clear_lsb, data_swab, as, load_rom, sym_cb);
  449. }
  450. fail:
  451. close(fd);
  452. return ret;
  453. }
  454. static void bswap_uboot_header(uboot_image_header_t *hdr)
  455. {
  456. #ifndef HOST_WORDS_BIGENDIAN
  457. bswap32s(&hdr->ih_magic);
  458. bswap32s(&hdr->ih_hcrc);
  459. bswap32s(&hdr->ih_time);
  460. bswap32s(&hdr->ih_size);
  461. bswap32s(&hdr->ih_load);
  462. bswap32s(&hdr->ih_ep);
  463. bswap32s(&hdr->ih_dcrc);
  464. #endif
  465. }
  466. #define ZALLOC_ALIGNMENT 16
  467. static void *zalloc(void *x, unsigned items, unsigned size)
  468. {
  469. void *p;
  470. size *= items;
  471. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  472. p = g_malloc(size);
  473. return (p);
  474. }
  475. static void zfree(void *x, void *addr)
  476. {
  477. g_free(addr);
  478. }
  479. #define HEAD_CRC 2
  480. #define EXTRA_FIELD 4
  481. #define ORIG_NAME 8
  482. #define COMMENT 0x10
  483. #define RESERVED 0xe0
  484. #define DEFLATED 8
  485. ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen)
  486. {
  487. z_stream s;
  488. ssize_t dstbytes;
  489. int r, i, flags;
  490. /* skip header */
  491. i = 10;
  492. flags = src[3];
  493. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  494. puts ("Error: Bad gzipped data\n");
  495. return -1;
  496. }
  497. if ((flags & EXTRA_FIELD) != 0)
  498. i = 12 + src[10] + (src[11] << 8);
  499. if ((flags & ORIG_NAME) != 0)
  500. while (src[i++] != 0)
  501. ;
  502. if ((flags & COMMENT) != 0)
  503. while (src[i++] != 0)
  504. ;
  505. if ((flags & HEAD_CRC) != 0)
  506. i += 2;
  507. if (i >= srclen) {
  508. puts ("Error: gunzip out of data in header\n");
  509. return -1;
  510. }
  511. s.zalloc = zalloc;
  512. s.zfree = zfree;
  513. r = inflateInit2(&s, -MAX_WBITS);
  514. if (r != Z_OK) {
  515. printf ("Error: inflateInit2() returned %d\n", r);
  516. return (-1);
  517. }
  518. s.next_in = src + i;
  519. s.avail_in = srclen - i;
  520. s.next_out = dst;
  521. s.avail_out = dstlen;
  522. r = inflate(&s, Z_FINISH);
  523. if (r != Z_OK && r != Z_STREAM_END) {
  524. printf ("Error: inflate() returned %d\n", r);
  525. return -1;
  526. }
  527. dstbytes = s.next_out - (unsigned char *) dst;
  528. inflateEnd(&s);
  529. return dstbytes;
  530. }
  531. /* Load a U-Boot image. */
  532. static int load_uboot_image(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  533. int *is_linux, uint8_t image_type,
  534. uint64_t (*translate_fn)(void *, uint64_t),
  535. void *translate_opaque, AddressSpace *as)
  536. {
  537. int fd;
  538. int size;
  539. hwaddr address;
  540. uboot_image_header_t h;
  541. uboot_image_header_t *hdr = &h;
  542. uint8_t *data = NULL;
  543. int ret = -1;
  544. int do_uncompress = 0;
  545. fd = open(filename, O_RDONLY | O_BINARY);
  546. if (fd < 0)
  547. return -1;
  548. size = read(fd, hdr, sizeof(uboot_image_header_t));
  549. if (size < sizeof(uboot_image_header_t)) {
  550. goto out;
  551. }
  552. bswap_uboot_header(hdr);
  553. if (hdr->ih_magic != IH_MAGIC)
  554. goto out;
  555. if (hdr->ih_type != image_type) {
  556. if (!(image_type == IH_TYPE_KERNEL &&
  557. hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
  558. fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
  559. image_type);
  560. goto out;
  561. }
  562. }
  563. /* TODO: Implement other image types. */
  564. switch (hdr->ih_type) {
  565. case IH_TYPE_KERNEL_NOLOAD:
  566. if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
  567. fprintf(stderr, "this image format (kernel_noload) cannot be "
  568. "loaded on this machine type");
  569. goto out;
  570. }
  571. hdr->ih_load = *loadaddr + sizeof(*hdr);
  572. hdr->ih_ep += hdr->ih_load;
  573. /* fall through */
  574. case IH_TYPE_KERNEL:
  575. address = hdr->ih_load;
  576. if (translate_fn) {
  577. address = translate_fn(translate_opaque, address);
  578. }
  579. if (loadaddr) {
  580. *loadaddr = hdr->ih_load;
  581. }
  582. switch (hdr->ih_comp) {
  583. case IH_COMP_NONE:
  584. break;
  585. case IH_COMP_GZIP:
  586. do_uncompress = 1;
  587. break;
  588. default:
  589. fprintf(stderr,
  590. "Unable to load u-boot images with compression type %d\n",
  591. hdr->ih_comp);
  592. goto out;
  593. }
  594. if (ep) {
  595. *ep = hdr->ih_ep;
  596. }
  597. /* TODO: Check CPU type. */
  598. if (is_linux) {
  599. if (hdr->ih_os == IH_OS_LINUX) {
  600. *is_linux = 1;
  601. } else {
  602. *is_linux = 0;
  603. }
  604. }
  605. break;
  606. case IH_TYPE_RAMDISK:
  607. address = *loadaddr;
  608. break;
  609. default:
  610. fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
  611. goto out;
  612. }
  613. data = g_malloc(hdr->ih_size);
  614. if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
  615. fprintf(stderr, "Error reading file\n");
  616. goto out;
  617. }
  618. if (do_uncompress) {
  619. uint8_t *compressed_data;
  620. size_t max_bytes;
  621. ssize_t bytes;
  622. compressed_data = data;
  623. max_bytes = UBOOT_MAX_GUNZIP_BYTES;
  624. data = g_malloc(max_bytes);
  625. bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
  626. g_free(compressed_data);
  627. if (bytes < 0) {
  628. fprintf(stderr, "Unable to decompress gzipped image!\n");
  629. goto out;
  630. }
  631. hdr->ih_size = bytes;
  632. }
  633. rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
  634. ret = hdr->ih_size;
  635. out:
  636. g_free(data);
  637. close(fd);
  638. return ret;
  639. }
  640. int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  641. int *is_linux,
  642. uint64_t (*translate_fn)(void *, uint64_t),
  643. void *translate_opaque)
  644. {
  645. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  646. translate_fn, translate_opaque, NULL);
  647. }
  648. int load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  649. int *is_linux,
  650. uint64_t (*translate_fn)(void *, uint64_t),
  651. void *translate_opaque, AddressSpace *as)
  652. {
  653. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  654. translate_fn, translate_opaque, as);
  655. }
  656. /* Load a ramdisk. */
  657. int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
  658. {
  659. return load_ramdisk_as(filename, addr, max_sz, NULL);
  660. }
  661. int load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
  662. AddressSpace *as)
  663. {
  664. return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
  665. NULL, NULL, as);
  666. }
  667. /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
  668. int load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
  669. uint8_t **buffer)
  670. {
  671. uint8_t *compressed_data = NULL;
  672. uint8_t *data = NULL;
  673. gsize len;
  674. ssize_t bytes;
  675. int ret = -1;
  676. if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
  677. NULL)) {
  678. goto out;
  679. }
  680. /* Is it a gzip-compressed file? */
  681. if (len < 2 ||
  682. compressed_data[0] != 0x1f ||
  683. compressed_data[1] != 0x8b) {
  684. goto out;
  685. }
  686. if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
  687. max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
  688. }
  689. data = g_malloc(max_sz);
  690. bytes = gunzip(data, max_sz, compressed_data, len);
  691. if (bytes < 0) {
  692. fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
  693. filename);
  694. goto out;
  695. }
  696. /* trim to actual size and return to caller */
  697. *buffer = g_realloc(data, bytes);
  698. ret = bytes;
  699. /* ownership has been transferred to caller */
  700. data = NULL;
  701. out:
  702. g_free(compressed_data);
  703. g_free(data);
  704. return ret;
  705. }
  706. /* Load a gzip-compressed kernel. */
  707. int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
  708. {
  709. int bytes;
  710. uint8_t *data;
  711. bytes = load_image_gzipped_buffer(filename, max_sz, &data);
  712. if (bytes != -1) {
  713. rom_add_blob_fixed(filename, data, bytes, addr);
  714. g_free(data);
  715. }
  716. return bytes;
  717. }
  718. /*
  719. * Functions for reboot-persistent memory regions.
  720. * - used for vga bios and option roms.
  721. * - also linux kernel (-kernel / -initrd).
  722. */
  723. typedef struct Rom Rom;
  724. struct Rom {
  725. char *name;
  726. char *path;
  727. /* datasize is the amount of memory allocated in "data". If datasize is less
  728. * than romsize, it means that the area from datasize to romsize is filled
  729. * with zeros.
  730. */
  731. size_t romsize;
  732. size_t datasize;
  733. uint8_t *data;
  734. MemoryRegion *mr;
  735. AddressSpace *as;
  736. int isrom;
  737. char *fw_dir;
  738. char *fw_file;
  739. GMappedFile *mapped_file;
  740. bool committed;
  741. hwaddr addr;
  742. QTAILQ_ENTRY(Rom) next;
  743. };
  744. static FWCfgState *fw_cfg;
  745. static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
  746. /*
  747. * rom->data can be heap-allocated or memory-mapped (e.g. when added with
  748. * rom_add_elf_program())
  749. */
  750. static void rom_free_data(Rom *rom)
  751. {
  752. if (rom->mapped_file) {
  753. g_mapped_file_unref(rom->mapped_file);
  754. rom->mapped_file = NULL;
  755. } else {
  756. g_free(rom->data);
  757. }
  758. rom->data = NULL;
  759. }
  760. static void rom_free(Rom *rom)
  761. {
  762. rom_free_data(rom);
  763. g_free(rom->path);
  764. g_free(rom->name);
  765. g_free(rom->fw_dir);
  766. g_free(rom->fw_file);
  767. g_free(rom);
  768. }
  769. static inline bool rom_order_compare(Rom *rom, Rom *item)
  770. {
  771. return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
  772. (rom->as == item->as && rom->addr >= item->addr);
  773. }
  774. static void rom_insert(Rom *rom)
  775. {
  776. Rom *item;
  777. if (roms_loaded) {
  778. hw_error ("ROM images must be loaded at startup\n");
  779. }
  780. /* The user didn't specify an address space, this is the default */
  781. if (!rom->as) {
  782. rom->as = &address_space_memory;
  783. }
  784. rom->committed = false;
  785. /* List is ordered by load address in the same address space */
  786. QTAILQ_FOREACH(item, &roms, next) {
  787. if (rom_order_compare(rom, item)) {
  788. continue;
  789. }
  790. QTAILQ_INSERT_BEFORE(item, rom, next);
  791. return;
  792. }
  793. QTAILQ_INSERT_TAIL(&roms, rom, next);
  794. }
  795. static void fw_cfg_resized(const char *id, uint64_t length, void *host)
  796. {
  797. if (fw_cfg) {
  798. fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
  799. }
  800. }
  801. static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
  802. {
  803. void *data;
  804. rom->mr = g_malloc(sizeof(*rom->mr));
  805. memory_region_init_resizeable_ram(rom->mr, owner, name,
  806. rom->datasize, rom->romsize,
  807. fw_cfg_resized,
  808. &error_fatal);
  809. memory_region_set_readonly(rom->mr, ro);
  810. vmstate_register_ram_global(rom->mr);
  811. data = memory_region_get_ram_ptr(rom->mr);
  812. memcpy(data, rom->data, rom->datasize);
  813. return data;
  814. }
  815. int rom_add_file(const char *file, const char *fw_dir,
  816. hwaddr addr, int32_t bootindex,
  817. bool option_rom, MemoryRegion *mr,
  818. AddressSpace *as)
  819. {
  820. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  821. Rom *rom;
  822. int rc, fd = -1;
  823. char devpath[100];
  824. if (as && mr) {
  825. fprintf(stderr, "Specifying an Address Space and Memory Region is " \
  826. "not valid when loading a rom\n");
  827. /* We haven't allocated anything so we don't need any cleanup */
  828. return -1;
  829. }
  830. rom = g_malloc0(sizeof(*rom));
  831. rom->name = g_strdup(file);
  832. rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
  833. rom->as = as;
  834. if (rom->path == NULL) {
  835. rom->path = g_strdup(file);
  836. }
  837. fd = open(rom->path, O_RDONLY | O_BINARY);
  838. if (fd == -1) {
  839. fprintf(stderr, "Could not open option rom '%s': %s\n",
  840. rom->path, strerror(errno));
  841. goto err;
  842. }
  843. if (fw_dir) {
  844. rom->fw_dir = g_strdup(fw_dir);
  845. rom->fw_file = g_strdup(file);
  846. }
  847. rom->addr = addr;
  848. rom->romsize = lseek(fd, 0, SEEK_END);
  849. if (rom->romsize == -1) {
  850. fprintf(stderr, "rom: file %-20s: get size error: %s\n",
  851. rom->name, strerror(errno));
  852. goto err;
  853. }
  854. rom->datasize = rom->romsize;
  855. rom->data = g_malloc0(rom->datasize);
  856. lseek(fd, 0, SEEK_SET);
  857. rc = read(fd, rom->data, rom->datasize);
  858. if (rc != rom->datasize) {
  859. fprintf(stderr, "rom: file %-20s: read error: rc=%d (expected %zd)\n",
  860. rom->name, rc, rom->datasize);
  861. goto err;
  862. }
  863. close(fd);
  864. rom_insert(rom);
  865. if (rom->fw_file && fw_cfg) {
  866. const char *basename;
  867. char fw_file_name[FW_CFG_MAX_FILE_PATH];
  868. void *data;
  869. basename = strrchr(rom->fw_file, '/');
  870. if (basename) {
  871. basename++;
  872. } else {
  873. basename = rom->fw_file;
  874. }
  875. snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
  876. basename);
  877. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  878. if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
  879. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
  880. } else {
  881. data = rom->data;
  882. }
  883. fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
  884. } else {
  885. if (mr) {
  886. rom->mr = mr;
  887. snprintf(devpath, sizeof(devpath), "/rom@%s", file);
  888. } else {
  889. snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
  890. }
  891. }
  892. add_boot_device_path(bootindex, NULL, devpath);
  893. return 0;
  894. err:
  895. if (fd != -1)
  896. close(fd);
  897. rom_free(rom);
  898. return -1;
  899. }
  900. MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
  901. size_t max_len, hwaddr addr, const char *fw_file_name,
  902. FWCfgCallback fw_callback, void *callback_opaque,
  903. AddressSpace *as, bool read_only)
  904. {
  905. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  906. Rom *rom;
  907. MemoryRegion *mr = NULL;
  908. rom = g_malloc0(sizeof(*rom));
  909. rom->name = g_strdup(name);
  910. rom->as = as;
  911. rom->addr = addr;
  912. rom->romsize = max_len ? max_len : len;
  913. rom->datasize = len;
  914. g_assert(rom->romsize >= rom->datasize);
  915. rom->data = g_malloc0(rom->datasize);
  916. memcpy(rom->data, blob, len);
  917. rom_insert(rom);
  918. if (fw_file_name && fw_cfg) {
  919. char devpath[100];
  920. void *data;
  921. if (read_only) {
  922. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  923. } else {
  924. snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
  925. }
  926. if (mc->rom_file_has_mr) {
  927. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
  928. mr = rom->mr;
  929. } else {
  930. data = rom->data;
  931. }
  932. fw_cfg_add_file_callback(fw_cfg, fw_file_name,
  933. fw_callback, NULL, callback_opaque,
  934. data, rom->datasize, read_only);
  935. }
  936. return mr;
  937. }
  938. /* This function is specific for elf program because we don't need to allocate
  939. * all the rom. We just allocate the first part and the rest is just zeros. This
  940. * is why romsize and datasize are different. Also, this function takes its own
  941. * reference to "mapped_file", so we don't have to allocate and copy the buffer.
  942. */
  943. int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
  944. size_t datasize, size_t romsize, hwaddr addr,
  945. AddressSpace *as)
  946. {
  947. Rom *rom;
  948. rom = g_malloc0(sizeof(*rom));
  949. rom->name = g_strdup(name);
  950. rom->addr = addr;
  951. rom->datasize = datasize;
  952. rom->romsize = romsize;
  953. rom->data = data;
  954. rom->as = as;
  955. if (mapped_file && data) {
  956. g_mapped_file_ref(mapped_file);
  957. rom->mapped_file = mapped_file;
  958. }
  959. rom_insert(rom);
  960. return 0;
  961. }
  962. int rom_add_vga(const char *file)
  963. {
  964. return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
  965. }
  966. int rom_add_option(const char *file, int32_t bootindex)
  967. {
  968. return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
  969. }
  970. static void rom_reset(void *unused)
  971. {
  972. Rom *rom;
  973. QTAILQ_FOREACH(rom, &roms, next) {
  974. if (rom->fw_file) {
  975. continue;
  976. }
  977. /*
  978. * We don't need to fill in the RAM with ROM data because we'll fill
  979. * the data in during the next incoming migration in all cases. Note
  980. * that some of those RAMs can actually be modified by the guest.
  981. */
  982. if (runstate_check(RUN_STATE_INMIGRATE)) {
  983. if (rom->data && rom->isrom) {
  984. /*
  985. * Free it so that a rom_reset after migration doesn't
  986. * overwrite a potentially modified 'rom'.
  987. */
  988. rom_free_data(rom);
  989. }
  990. continue;
  991. }
  992. if (rom->data == NULL) {
  993. continue;
  994. }
  995. if (rom->mr) {
  996. void *host = memory_region_get_ram_ptr(rom->mr);
  997. memcpy(host, rom->data, rom->datasize);
  998. } else {
  999. address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
  1000. rom->data, rom->datasize);
  1001. }
  1002. if (rom->isrom) {
  1003. /* rom needs to be written only once */
  1004. rom_free_data(rom);
  1005. }
  1006. /*
  1007. * The rom loader is really on the same level as firmware in the guest
  1008. * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
  1009. * that the instruction cache for that new region is clear, so that the
  1010. * CPU definitely fetches its instructions from the just written data.
  1011. */
  1012. cpu_flush_icache_range(rom->addr, rom->datasize);
  1013. trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom);
  1014. }
  1015. }
  1016. int rom_check_and_register_reset(void)
  1017. {
  1018. hwaddr addr = 0;
  1019. MemoryRegionSection section;
  1020. Rom *rom;
  1021. AddressSpace *as = NULL;
  1022. QTAILQ_FOREACH(rom, &roms, next) {
  1023. if (rom->fw_file) {
  1024. continue;
  1025. }
  1026. if (!rom->mr) {
  1027. if ((addr > rom->addr) && (as == rom->as)) {
  1028. fprintf(stderr, "rom: requested regions overlap "
  1029. "(rom %s. free=0x" TARGET_FMT_plx
  1030. ", addr=0x" TARGET_FMT_plx ")\n",
  1031. rom->name, addr, rom->addr);
  1032. return -1;
  1033. }
  1034. addr = rom->addr;
  1035. addr += rom->romsize;
  1036. as = rom->as;
  1037. }
  1038. section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
  1039. rom->addr, 1);
  1040. rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
  1041. memory_region_unref(section.mr);
  1042. }
  1043. qemu_register_reset(rom_reset, NULL);
  1044. roms_loaded = 1;
  1045. return 0;
  1046. }
  1047. void rom_set_fw(FWCfgState *f)
  1048. {
  1049. fw_cfg = f;
  1050. }
  1051. void rom_set_order_override(int order)
  1052. {
  1053. if (!fw_cfg)
  1054. return;
  1055. fw_cfg_set_order_override(fw_cfg, order);
  1056. }
  1057. void rom_reset_order_override(void)
  1058. {
  1059. if (!fw_cfg)
  1060. return;
  1061. fw_cfg_reset_order_override(fw_cfg);
  1062. }
  1063. void rom_transaction_begin(void)
  1064. {
  1065. Rom *rom;
  1066. /* Ignore ROMs added without the transaction API */
  1067. QTAILQ_FOREACH(rom, &roms, next) {
  1068. rom->committed = true;
  1069. }
  1070. }
  1071. void rom_transaction_end(bool commit)
  1072. {
  1073. Rom *rom;
  1074. Rom *tmp;
  1075. QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
  1076. if (rom->committed) {
  1077. continue;
  1078. }
  1079. if (commit) {
  1080. rom->committed = true;
  1081. } else {
  1082. QTAILQ_REMOVE(&roms, rom, next);
  1083. rom_free(rom);
  1084. }
  1085. }
  1086. }
  1087. static Rom *find_rom(hwaddr addr, size_t size)
  1088. {
  1089. Rom *rom;
  1090. QTAILQ_FOREACH(rom, &roms, next) {
  1091. if (rom->fw_file) {
  1092. continue;
  1093. }
  1094. if (rom->mr) {
  1095. continue;
  1096. }
  1097. if (rom->addr > addr) {
  1098. continue;
  1099. }
  1100. if (rom->addr + rom->romsize < addr + size) {
  1101. continue;
  1102. }
  1103. return rom;
  1104. }
  1105. return NULL;
  1106. }
  1107. /*
  1108. * Copies memory from registered ROMs to dest. Any memory that is contained in
  1109. * a ROM between addr and addr + size is copied. Note that this can involve
  1110. * multiple ROMs, which need not start at addr and need not end at addr + size.
  1111. */
  1112. int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
  1113. {
  1114. hwaddr end = addr + size;
  1115. uint8_t *s, *d = dest;
  1116. size_t l = 0;
  1117. Rom *rom;
  1118. QTAILQ_FOREACH(rom, &roms, next) {
  1119. if (rom->fw_file) {
  1120. continue;
  1121. }
  1122. if (rom->mr) {
  1123. continue;
  1124. }
  1125. if (rom->addr + rom->romsize < addr) {
  1126. continue;
  1127. }
  1128. if (rom->addr > end || rom->addr < addr) {
  1129. break;
  1130. }
  1131. d = dest + (rom->addr - addr);
  1132. s = rom->data;
  1133. l = rom->datasize;
  1134. if ((d + l) > (dest + size)) {
  1135. l = dest - d;
  1136. }
  1137. if (l > 0) {
  1138. memcpy(d, s, l);
  1139. }
  1140. if (rom->romsize > rom->datasize) {
  1141. /* If datasize is less than romsize, it means that we didn't
  1142. * allocate all the ROM because the trailing data are only zeros.
  1143. */
  1144. d += l;
  1145. l = rom->romsize - rom->datasize;
  1146. if ((d + l) > (dest + size)) {
  1147. /* Rom size doesn't fit in the destination area. Adjust to avoid
  1148. * overflow.
  1149. */
  1150. l = dest - d;
  1151. }
  1152. if (l > 0) {
  1153. memset(d, 0x0, l);
  1154. }
  1155. }
  1156. }
  1157. return (d + l) - dest;
  1158. }
  1159. void *rom_ptr(hwaddr addr, size_t size)
  1160. {
  1161. Rom *rom;
  1162. rom = find_rom(addr, size);
  1163. if (!rom || !rom->data)
  1164. return NULL;
  1165. return rom->data + (addr - rom->addr);
  1166. }
  1167. void hmp_info_roms(Monitor *mon, const QDict *qdict)
  1168. {
  1169. Rom *rom;
  1170. QTAILQ_FOREACH(rom, &roms, next) {
  1171. if (rom->mr) {
  1172. monitor_printf(mon, "%s"
  1173. " size=0x%06zx name=\"%s\"\n",
  1174. memory_region_name(rom->mr),
  1175. rom->romsize,
  1176. rom->name);
  1177. } else if (!rom->fw_file) {
  1178. monitor_printf(mon, "addr=" TARGET_FMT_plx
  1179. " size=0x%06zx mem=%s name=\"%s\"\n",
  1180. rom->addr, rom->romsize,
  1181. rom->isrom ? "rom" : "ram",
  1182. rom->name);
  1183. } else {
  1184. monitor_printf(mon, "fw=%s/%s"
  1185. " size=0x%06zx name=\"%s\"\n",
  1186. rom->fw_dir,
  1187. rom->fw_file,
  1188. rom->romsize,
  1189. rom->name);
  1190. }
  1191. }
  1192. }
  1193. typedef enum HexRecord HexRecord;
  1194. enum HexRecord {
  1195. DATA_RECORD = 0,
  1196. EOF_RECORD,
  1197. EXT_SEG_ADDR_RECORD,
  1198. START_SEG_ADDR_RECORD,
  1199. EXT_LINEAR_ADDR_RECORD,
  1200. START_LINEAR_ADDR_RECORD,
  1201. };
  1202. /* Each record contains a 16-bit address which is combined with the upper 16
  1203. * bits of the implicit "next address" to form a 32-bit address.
  1204. */
  1205. #define NEXT_ADDR_MASK 0xffff0000
  1206. #define DATA_FIELD_MAX_LEN 0xff
  1207. #define LEN_EXCEPT_DATA 0x5
  1208. /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
  1209. * sizeof(checksum) */
  1210. typedef struct {
  1211. uint8_t byte_count;
  1212. uint16_t address;
  1213. uint8_t record_type;
  1214. uint8_t data[DATA_FIELD_MAX_LEN];
  1215. uint8_t checksum;
  1216. } HexLine;
  1217. /* return 0 or -1 if error */
  1218. static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
  1219. uint32_t *index, const bool in_process)
  1220. {
  1221. /* +-------+---------------+-------+---------------------+--------+
  1222. * | byte | |record | | |
  1223. * | count | address | type | data |checksum|
  1224. * +-------+---------------+-------+---------------------+--------+
  1225. * ^ ^ ^ ^ ^ ^
  1226. * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
  1227. */
  1228. uint8_t value = 0;
  1229. uint32_t idx = *index;
  1230. /* ignore space */
  1231. if (g_ascii_isspace(c)) {
  1232. return true;
  1233. }
  1234. if (!g_ascii_isxdigit(c) || !in_process) {
  1235. return false;
  1236. }
  1237. value = g_ascii_xdigit_value(c);
  1238. value = (idx & 0x1) ? (value & 0xf) : (value << 4);
  1239. if (idx < 2) {
  1240. line->byte_count |= value;
  1241. } else if (2 <= idx && idx < 6) {
  1242. line->address <<= 4;
  1243. line->address += g_ascii_xdigit_value(c);
  1244. } else if (6 <= idx && idx < 8) {
  1245. line->record_type |= value;
  1246. } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
  1247. line->data[(idx - 8) >> 1] |= value;
  1248. } else if (8 + 2 * line->byte_count <= idx &&
  1249. idx < 10 + 2 * line->byte_count) {
  1250. line->checksum |= value;
  1251. } else {
  1252. return false;
  1253. }
  1254. *our_checksum += value;
  1255. ++(*index);
  1256. return true;
  1257. }
  1258. typedef struct {
  1259. const char *filename;
  1260. HexLine line;
  1261. uint8_t *bin_buf;
  1262. hwaddr *start_addr;
  1263. int total_size;
  1264. uint32_t next_address_to_write;
  1265. uint32_t current_address;
  1266. uint32_t current_rom_index;
  1267. uint32_t rom_start_address;
  1268. AddressSpace *as;
  1269. bool complete;
  1270. } HexParser;
  1271. /* return size or -1 if error */
  1272. static int handle_record_type(HexParser *parser)
  1273. {
  1274. HexLine *line = &(parser->line);
  1275. switch (line->record_type) {
  1276. case DATA_RECORD:
  1277. parser->current_address =
  1278. (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
  1279. /* verify this is a contiguous block of memory */
  1280. if (parser->current_address != parser->next_address_to_write) {
  1281. if (parser->current_rom_index != 0) {
  1282. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1283. parser->current_rom_index,
  1284. parser->rom_start_address, parser->as);
  1285. }
  1286. parser->rom_start_address = parser->current_address;
  1287. parser->current_rom_index = 0;
  1288. }
  1289. /* copy from line buffer to output bin_buf */
  1290. memcpy(parser->bin_buf + parser->current_rom_index, line->data,
  1291. line->byte_count);
  1292. parser->current_rom_index += line->byte_count;
  1293. parser->total_size += line->byte_count;
  1294. /* save next address to write */
  1295. parser->next_address_to_write =
  1296. parser->current_address + line->byte_count;
  1297. break;
  1298. case EOF_RECORD:
  1299. if (parser->current_rom_index != 0) {
  1300. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1301. parser->current_rom_index,
  1302. parser->rom_start_address, parser->as);
  1303. }
  1304. parser->complete = true;
  1305. return parser->total_size;
  1306. case EXT_SEG_ADDR_RECORD:
  1307. case EXT_LINEAR_ADDR_RECORD:
  1308. if (line->byte_count != 2 && line->address != 0) {
  1309. return -1;
  1310. }
  1311. if (parser->current_rom_index != 0) {
  1312. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1313. parser->current_rom_index,
  1314. parser->rom_start_address, parser->as);
  1315. }
  1316. /* save next address to write,
  1317. * in case of non-contiguous block of memory */
  1318. parser->next_address_to_write = (line->data[0] << 12) |
  1319. (line->data[1] << 4);
  1320. if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
  1321. parser->next_address_to_write <<= 12;
  1322. }
  1323. parser->rom_start_address = parser->next_address_to_write;
  1324. parser->current_rom_index = 0;
  1325. break;
  1326. case START_SEG_ADDR_RECORD:
  1327. if (line->byte_count != 4 && line->address != 0) {
  1328. return -1;
  1329. }
  1330. /* x86 16-bit CS:IP segmented addressing */
  1331. *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
  1332. ((line->data[2] << 8) | line->data[3]);
  1333. break;
  1334. case START_LINEAR_ADDR_RECORD:
  1335. if (line->byte_count != 4 && line->address != 0) {
  1336. return -1;
  1337. }
  1338. *(parser->start_addr) = ldl_be_p(line->data);
  1339. break;
  1340. default:
  1341. return -1;
  1342. }
  1343. return parser->total_size;
  1344. }
  1345. /* return size or -1 if error */
  1346. static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
  1347. size_t hex_blob_size, AddressSpace *as)
  1348. {
  1349. bool in_process = false; /* avoid re-enter and
  1350. * check whether record begin with ':' */
  1351. uint8_t *end = hex_blob + hex_blob_size;
  1352. uint8_t our_checksum = 0;
  1353. uint32_t record_index = 0;
  1354. HexParser parser = {
  1355. .filename = filename,
  1356. .bin_buf = g_malloc(hex_blob_size),
  1357. .start_addr = addr,
  1358. .as = as,
  1359. .complete = false
  1360. };
  1361. rom_transaction_begin();
  1362. for (; hex_blob < end && !parser.complete; ++hex_blob) {
  1363. switch (*hex_blob) {
  1364. case '\r':
  1365. case '\n':
  1366. if (!in_process) {
  1367. break;
  1368. }
  1369. in_process = false;
  1370. if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
  1371. record_index ||
  1372. our_checksum != 0) {
  1373. parser.total_size = -1;
  1374. goto out;
  1375. }
  1376. if (handle_record_type(&parser) == -1) {
  1377. parser.total_size = -1;
  1378. goto out;
  1379. }
  1380. break;
  1381. /* start of a new record. */
  1382. case ':':
  1383. memset(&parser.line, 0, sizeof(HexLine));
  1384. in_process = true;
  1385. record_index = 0;
  1386. break;
  1387. /* decoding lines */
  1388. default:
  1389. if (!parse_record(&parser.line, &our_checksum, *hex_blob,
  1390. &record_index, in_process)) {
  1391. parser.total_size = -1;
  1392. goto out;
  1393. }
  1394. break;
  1395. }
  1396. }
  1397. out:
  1398. g_free(parser.bin_buf);
  1399. rom_transaction_end(parser.total_size != -1);
  1400. return parser.total_size;
  1401. }
  1402. /* return size or -1 if error */
  1403. int load_targphys_hex_as(const char *filename, hwaddr *entry, AddressSpace *as)
  1404. {
  1405. gsize hex_blob_size;
  1406. gchar *hex_blob;
  1407. int total_size = 0;
  1408. if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
  1409. return -1;
  1410. }
  1411. total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,
  1412. hex_blob_size, as);
  1413. g_free(hex_blob);
  1414. return total_size;
  1415. }