loader.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  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/datadir.h"
  46. #include "qemu/error-report.h"
  47. #include "qapi/error.h"
  48. #include "qapi/qapi-commands-machine.h"
  49. #include "qapi/type-helpers.h"
  50. #include "trace.h"
  51. #include "hw/hw.h"
  52. #include "disas/disas.h"
  53. #include "migration/vmstate.h"
  54. #include "monitor/monitor.h"
  55. #include "sysemu/reset.h"
  56. #include "sysemu/sysemu.h"
  57. #include "uboot_image.h"
  58. #include "hw/loader.h"
  59. #include "hw/nvram/fw_cfg.h"
  60. #include "exec/memory.h"
  61. #include "hw/boards.h"
  62. #include "qemu/cutils.h"
  63. #include "sysemu/runstate.h"
  64. #include "tcg/debuginfo.h"
  65. #include <zlib.h>
  66. static int roms_loaded;
  67. /* return the size or -1 if error */
  68. int64_t get_image_size(const char *filename)
  69. {
  70. int fd;
  71. int64_t size;
  72. fd = open(filename, O_RDONLY | O_BINARY);
  73. if (fd < 0)
  74. return -1;
  75. size = lseek(fd, 0, SEEK_END);
  76. close(fd);
  77. return size;
  78. }
  79. /* return the size or -1 if error */
  80. ssize_t load_image_size(const char *filename, void *addr, size_t size)
  81. {
  82. int fd;
  83. ssize_t actsize, l = 0;
  84. fd = open(filename, O_RDONLY | O_BINARY);
  85. if (fd < 0) {
  86. return -1;
  87. }
  88. while ((actsize = read(fd, addr + l, size - l)) > 0) {
  89. l += actsize;
  90. }
  91. close(fd);
  92. return actsize < 0 ? -1 : l;
  93. }
  94. /* read()-like version */
  95. ssize_t read_targphys(const char *name,
  96. int fd, hwaddr dst_addr, size_t nbytes)
  97. {
  98. uint8_t *buf;
  99. ssize_t did;
  100. buf = g_malloc(nbytes);
  101. did = read(fd, buf, nbytes);
  102. if (did > 0)
  103. rom_add_blob_fixed("read", buf, did, dst_addr);
  104. g_free(buf);
  105. return did;
  106. }
  107. ssize_t load_image_targphys(const char *filename,
  108. hwaddr addr, uint64_t max_sz)
  109. {
  110. return load_image_targphys_as(filename, addr, max_sz, NULL);
  111. }
  112. /* return the size or -1 if error */
  113. ssize_t load_image_targphys_as(const char *filename,
  114. hwaddr addr, uint64_t max_sz, AddressSpace *as)
  115. {
  116. ssize_t size;
  117. size = get_image_size(filename);
  118. if (size < 0 || size > max_sz) {
  119. return -1;
  120. }
  121. if (size > 0) {
  122. if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
  123. return -1;
  124. }
  125. }
  126. return size;
  127. }
  128. ssize_t load_image_mr(const char *filename, MemoryRegion *mr)
  129. {
  130. ssize_t size;
  131. if (!memory_access_is_direct(mr, false)) {
  132. /* Can only load an image into RAM or ROM */
  133. return -1;
  134. }
  135. size = get_image_size(filename);
  136. if (size < 0 || size > memory_region_size(mr)) {
  137. return -1;
  138. }
  139. if (size > 0) {
  140. if (rom_add_file_mr(filename, mr, -1) < 0) {
  141. return -1;
  142. }
  143. }
  144. return size;
  145. }
  146. void pstrcpy_targphys(const char *name, hwaddr dest, int buf_size,
  147. const char *source)
  148. {
  149. const char *nulp;
  150. char *ptr;
  151. if (buf_size <= 0) return;
  152. nulp = memchr(source, 0, buf_size);
  153. if (nulp) {
  154. rom_add_blob_fixed(name, source, (nulp - source) + 1, dest);
  155. } else {
  156. rom_add_blob_fixed(name, source, buf_size, dest);
  157. ptr = rom_ptr(dest + buf_size - 1, sizeof(*ptr));
  158. *ptr = 0;
  159. }
  160. }
  161. /* A.OUT loader */
  162. struct exec
  163. {
  164. uint32_t a_info; /* Use macros N_MAGIC, etc for access */
  165. uint32_t a_text; /* length of text, in bytes */
  166. uint32_t a_data; /* length of data, in bytes */
  167. uint32_t a_bss; /* length of uninitialized data area, in bytes */
  168. uint32_t a_syms; /* length of symbol table data in file, in bytes */
  169. uint32_t a_entry; /* start address */
  170. uint32_t a_trsize; /* length of relocation info for text, in bytes */
  171. uint32_t a_drsize; /* length of relocation info for data, in bytes */
  172. };
  173. static void bswap_ahdr(struct exec *e)
  174. {
  175. bswap32s(&e->a_info);
  176. bswap32s(&e->a_text);
  177. bswap32s(&e->a_data);
  178. bswap32s(&e->a_bss);
  179. bswap32s(&e->a_syms);
  180. bswap32s(&e->a_entry);
  181. bswap32s(&e->a_trsize);
  182. bswap32s(&e->a_drsize);
  183. }
  184. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  185. #define OMAGIC 0407
  186. #define NMAGIC 0410
  187. #define ZMAGIC 0413
  188. #define QMAGIC 0314
  189. #define _N_HDROFF(x) (1024 - sizeof (struct exec))
  190. #define N_TXTOFF(x) \
  191. (N_MAGIC(x) == ZMAGIC ? _N_HDROFF((x)) + sizeof (struct exec) : \
  192. (N_MAGIC(x) == QMAGIC ? 0 : sizeof (struct exec)))
  193. #define N_TXTADDR(x, target_page_size) (N_MAGIC(x) == QMAGIC ? target_page_size : 0)
  194. #define _N_SEGMENT_ROUND(x, target_page_size) (((x) + target_page_size - 1) & ~(target_page_size - 1))
  195. #define _N_TXTENDADDR(x, target_page_size) (N_TXTADDR(x, target_page_size)+(x).a_text)
  196. #define N_DATADDR(x, target_page_size) \
  197. (N_MAGIC(x)==OMAGIC? (_N_TXTENDADDR(x, target_page_size)) \
  198. : (_N_SEGMENT_ROUND (_N_TXTENDADDR(x, target_page_size), target_page_size)))
  199. ssize_t load_aout(const char *filename, hwaddr addr, int max_sz,
  200. int bswap_needed, hwaddr target_page_size)
  201. {
  202. int fd;
  203. ssize_t size, ret;
  204. struct exec e;
  205. uint32_t magic;
  206. fd = open(filename, O_RDONLY | O_BINARY);
  207. if (fd < 0)
  208. return -1;
  209. size = read(fd, &e, sizeof(e));
  210. if (size < 0)
  211. goto fail;
  212. if (bswap_needed) {
  213. bswap_ahdr(&e);
  214. }
  215. magic = N_MAGIC(e);
  216. switch (magic) {
  217. case ZMAGIC:
  218. case QMAGIC:
  219. case OMAGIC:
  220. if (e.a_text + e.a_data > max_sz)
  221. goto fail;
  222. lseek(fd, N_TXTOFF(e), SEEK_SET);
  223. size = read_targphys(filename, fd, addr, e.a_text + e.a_data);
  224. if (size < 0)
  225. goto fail;
  226. break;
  227. case NMAGIC:
  228. if (N_DATADDR(e, target_page_size) + e.a_data > max_sz)
  229. goto fail;
  230. lseek(fd, N_TXTOFF(e), SEEK_SET);
  231. size = read_targphys(filename, fd, addr, e.a_text);
  232. if (size < 0)
  233. goto fail;
  234. ret = read_targphys(filename, fd, addr + N_DATADDR(e, target_page_size),
  235. e.a_data);
  236. if (ret < 0)
  237. goto fail;
  238. size += ret;
  239. break;
  240. default:
  241. goto fail;
  242. }
  243. close(fd);
  244. return size;
  245. fail:
  246. close(fd);
  247. return -1;
  248. }
  249. /* ELF loader */
  250. static void *load_at(int fd, off_t offset, size_t size)
  251. {
  252. void *ptr;
  253. if (lseek(fd, offset, SEEK_SET) < 0)
  254. return NULL;
  255. ptr = g_malloc(size);
  256. if (read(fd, ptr, size) != size) {
  257. g_free(ptr);
  258. return NULL;
  259. }
  260. return ptr;
  261. }
  262. #ifdef ELF_CLASS
  263. #undef ELF_CLASS
  264. #endif
  265. #define ELF_CLASS ELFCLASS32
  266. #include "elf.h"
  267. #define SZ 32
  268. #define elf_word uint32_t
  269. #define elf_sword int32_t
  270. #define bswapSZs bswap32s
  271. #include "hw/elf_ops.h.inc"
  272. #undef elfhdr
  273. #undef elf_phdr
  274. #undef elf_shdr
  275. #undef elf_sym
  276. #undef elf_rela
  277. #undef elf_note
  278. #undef elf_word
  279. #undef elf_sword
  280. #undef bswapSZs
  281. #undef SZ
  282. #define elfhdr elf64_hdr
  283. #define elf_phdr elf64_phdr
  284. #define elf_note elf64_note
  285. #define elf_shdr elf64_shdr
  286. #define elf_sym elf64_sym
  287. #define elf_rela elf64_rela
  288. #define elf_word uint64_t
  289. #define elf_sword int64_t
  290. #define bswapSZs bswap64s
  291. #define SZ 64
  292. #include "hw/elf_ops.h.inc"
  293. const char *load_elf_strerror(ssize_t error)
  294. {
  295. switch (error) {
  296. case 0:
  297. return "No error";
  298. case ELF_LOAD_FAILED:
  299. return "Failed to load ELF";
  300. case ELF_LOAD_NOT_ELF:
  301. return "The image is not ELF";
  302. case ELF_LOAD_WRONG_ARCH:
  303. return "The image is from incompatible architecture";
  304. case ELF_LOAD_WRONG_ENDIAN:
  305. return "The image has incorrect endianness";
  306. case ELF_LOAD_TOO_BIG:
  307. return "The image segments are too big to load";
  308. default:
  309. return "Unknown error";
  310. }
  311. }
  312. void load_elf_hdr(const char *filename, void *hdr, bool *is64, Error **errp)
  313. {
  314. int fd;
  315. uint8_t e_ident_local[EI_NIDENT];
  316. uint8_t *e_ident;
  317. size_t hdr_size, off;
  318. bool is64l;
  319. if (!hdr) {
  320. hdr = e_ident_local;
  321. }
  322. e_ident = hdr;
  323. fd = open(filename, O_RDONLY | O_BINARY);
  324. if (fd < 0) {
  325. error_setg_errno(errp, errno, "Failed to open file: %s", filename);
  326. return;
  327. }
  328. if (read(fd, hdr, EI_NIDENT) != EI_NIDENT) {
  329. error_setg_errno(errp, errno, "Failed to read file: %s", filename);
  330. goto fail;
  331. }
  332. if (e_ident[0] != ELFMAG0 ||
  333. e_ident[1] != ELFMAG1 ||
  334. e_ident[2] != ELFMAG2 ||
  335. e_ident[3] != ELFMAG3) {
  336. error_setg(errp, "Bad ELF magic");
  337. goto fail;
  338. }
  339. is64l = e_ident[EI_CLASS] == ELFCLASS64;
  340. hdr_size = is64l ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr);
  341. if (is64) {
  342. *is64 = is64l;
  343. }
  344. off = EI_NIDENT;
  345. while (hdr != e_ident_local && off < hdr_size) {
  346. size_t br = read(fd, hdr + off, hdr_size - off);
  347. switch (br) {
  348. case 0:
  349. error_setg(errp, "File too short: %s", filename);
  350. goto fail;
  351. case -1:
  352. error_setg_errno(errp, errno, "Failed to read file: %s",
  353. filename);
  354. goto fail;
  355. }
  356. off += br;
  357. }
  358. fail:
  359. close(fd);
  360. }
  361. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  362. ssize_t load_elf(const char *filename,
  363. uint64_t (*elf_note_fn)(void *, void *, bool),
  364. uint64_t (*translate_fn)(void *, uint64_t),
  365. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  366. uint64_t *highaddr, uint32_t *pflags, int big_endian,
  367. int elf_machine, int clear_lsb, int data_swab)
  368. {
  369. return load_elf_as(filename, elf_note_fn, translate_fn, translate_opaque,
  370. pentry, lowaddr, highaddr, pflags, big_endian,
  371. elf_machine, clear_lsb, data_swab, NULL);
  372. }
  373. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  374. ssize_t load_elf_as(const char *filename,
  375. uint64_t (*elf_note_fn)(void *, void *, bool),
  376. uint64_t (*translate_fn)(void *, uint64_t),
  377. void *translate_opaque, uint64_t *pentry, uint64_t *lowaddr,
  378. uint64_t *highaddr, uint32_t *pflags, int big_endian,
  379. int elf_machine, int clear_lsb, int data_swab,
  380. AddressSpace *as)
  381. {
  382. return load_elf_ram(filename, elf_note_fn, translate_fn, translate_opaque,
  383. pentry, lowaddr, highaddr, pflags, big_endian,
  384. elf_machine, clear_lsb, data_swab, as, true);
  385. }
  386. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  387. ssize_t load_elf_ram(const char *filename,
  388. uint64_t (*elf_note_fn)(void *, void *, bool),
  389. uint64_t (*translate_fn)(void *, uint64_t),
  390. void *translate_opaque, uint64_t *pentry,
  391. uint64_t *lowaddr, uint64_t *highaddr, uint32_t *pflags,
  392. int big_endian, int elf_machine, int clear_lsb,
  393. int data_swab, AddressSpace *as, bool load_rom)
  394. {
  395. return load_elf_ram_sym(filename, elf_note_fn,
  396. translate_fn, translate_opaque,
  397. pentry, lowaddr, highaddr, pflags, big_endian,
  398. elf_machine, clear_lsb, data_swab, as,
  399. load_rom, NULL);
  400. }
  401. /* return < 0 if error, otherwise the number of bytes loaded in memory */
  402. ssize_t load_elf_ram_sym(const char *filename,
  403. uint64_t (*elf_note_fn)(void *, void *, bool),
  404. uint64_t (*translate_fn)(void *, uint64_t),
  405. void *translate_opaque, uint64_t *pentry,
  406. uint64_t *lowaddr, uint64_t *highaddr,
  407. uint32_t *pflags, int big_endian, int elf_machine,
  408. int clear_lsb, int data_swab,
  409. AddressSpace *as, bool load_rom, symbol_fn_t sym_cb)
  410. {
  411. int fd, data_order, target_data_order, must_swab;
  412. ssize_t ret = ELF_LOAD_FAILED;
  413. uint8_t e_ident[EI_NIDENT];
  414. fd = open(filename, O_RDONLY | O_BINARY);
  415. if (fd < 0) {
  416. perror(filename);
  417. return -1;
  418. }
  419. if (read(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
  420. goto fail;
  421. if (e_ident[0] != ELFMAG0 ||
  422. e_ident[1] != ELFMAG1 ||
  423. e_ident[2] != ELFMAG2 ||
  424. e_ident[3] != ELFMAG3) {
  425. ret = ELF_LOAD_NOT_ELF;
  426. goto fail;
  427. }
  428. #if HOST_BIG_ENDIAN
  429. data_order = ELFDATA2MSB;
  430. #else
  431. data_order = ELFDATA2LSB;
  432. #endif
  433. must_swab = data_order != e_ident[EI_DATA];
  434. if (big_endian) {
  435. target_data_order = ELFDATA2MSB;
  436. } else {
  437. target_data_order = ELFDATA2LSB;
  438. }
  439. if (target_data_order != e_ident[EI_DATA]) {
  440. ret = ELF_LOAD_WRONG_ENDIAN;
  441. goto fail;
  442. }
  443. lseek(fd, 0, SEEK_SET);
  444. if (e_ident[EI_CLASS] == ELFCLASS64) {
  445. ret = load_elf64(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. } else {
  450. ret = load_elf32(filename, fd, elf_note_fn,
  451. translate_fn, translate_opaque, must_swab,
  452. pentry, lowaddr, highaddr, pflags, elf_machine,
  453. clear_lsb, data_swab, as, load_rom, sym_cb);
  454. }
  455. if (ret > 0) {
  456. debuginfo_report_elf(filename, fd, 0);
  457. }
  458. fail:
  459. close(fd);
  460. return ret;
  461. }
  462. static void bswap_uboot_header(uboot_image_header_t *hdr)
  463. {
  464. #if !HOST_BIG_ENDIAN
  465. bswap32s(&hdr->ih_magic);
  466. bswap32s(&hdr->ih_hcrc);
  467. bswap32s(&hdr->ih_time);
  468. bswap32s(&hdr->ih_size);
  469. bswap32s(&hdr->ih_load);
  470. bswap32s(&hdr->ih_ep);
  471. bswap32s(&hdr->ih_dcrc);
  472. #endif
  473. }
  474. #define ZALLOC_ALIGNMENT 16
  475. static void *zalloc(void *x, unsigned items, unsigned size)
  476. {
  477. void *p;
  478. size *= items;
  479. size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
  480. p = g_malloc(size);
  481. return (p);
  482. }
  483. static void zfree(void *x, void *addr)
  484. {
  485. g_free(addr);
  486. }
  487. #define HEAD_CRC 2
  488. #define EXTRA_FIELD 4
  489. #define ORIG_NAME 8
  490. #define COMMENT 0x10
  491. #define RESERVED 0xe0
  492. #define DEFLATED 8
  493. ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, size_t srclen)
  494. {
  495. z_stream s = {};
  496. ssize_t dstbytes;
  497. int r, i, flags;
  498. /* skip header */
  499. i = 10;
  500. if (srclen < 4) {
  501. goto toosmall;
  502. }
  503. flags = src[3];
  504. if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
  505. puts ("Error: Bad gzipped data\n");
  506. return -1;
  507. }
  508. if ((flags & EXTRA_FIELD) != 0) {
  509. if (srclen < 12) {
  510. goto toosmall;
  511. }
  512. i = 12 + src[10] + (src[11] << 8);
  513. }
  514. if ((flags & ORIG_NAME) != 0) {
  515. while (i < srclen && src[i++] != 0) {
  516. /* do nothing */
  517. }
  518. }
  519. if ((flags & COMMENT) != 0) {
  520. while (i < srclen && src[i++] != 0) {
  521. /* do nothing */
  522. }
  523. }
  524. if ((flags & HEAD_CRC) != 0) {
  525. i += 2;
  526. }
  527. if (i >= srclen) {
  528. goto toosmall;
  529. }
  530. s.zalloc = zalloc;
  531. s.zfree = zfree;
  532. r = inflateInit2(&s, -MAX_WBITS);
  533. if (r != Z_OK) {
  534. printf ("Error: inflateInit2() returned %d\n", r);
  535. return (-1);
  536. }
  537. s.next_in = src + i;
  538. s.avail_in = srclen - i;
  539. s.next_out = dst;
  540. s.avail_out = dstlen;
  541. r = inflate(&s, Z_FINISH);
  542. if (r != Z_OK && r != Z_STREAM_END) {
  543. printf ("Error: inflate() returned %d\n", r);
  544. inflateEnd(&s);
  545. return -1;
  546. }
  547. dstbytes = s.next_out - (unsigned char *) dst;
  548. inflateEnd(&s);
  549. return dstbytes;
  550. toosmall:
  551. puts("Error: gunzip out of data in header\n");
  552. return -1;
  553. }
  554. /* Load a U-Boot image. */
  555. static ssize_t load_uboot_image(const char *filename, hwaddr *ep,
  556. hwaddr *loadaddr, int *is_linux,
  557. uint8_t image_type,
  558. uint64_t (*translate_fn)(void *, uint64_t),
  559. void *translate_opaque, AddressSpace *as)
  560. {
  561. int fd;
  562. ssize_t size;
  563. hwaddr address;
  564. uboot_image_header_t h;
  565. uboot_image_header_t *hdr = &h;
  566. uint8_t *data = NULL;
  567. int ret = -1;
  568. int do_uncompress = 0;
  569. fd = open(filename, O_RDONLY | O_BINARY);
  570. if (fd < 0)
  571. return -1;
  572. size = read(fd, hdr, sizeof(uboot_image_header_t));
  573. if (size < sizeof(uboot_image_header_t)) {
  574. goto out;
  575. }
  576. bswap_uboot_header(hdr);
  577. if (hdr->ih_magic != IH_MAGIC)
  578. goto out;
  579. if (hdr->ih_type != image_type) {
  580. if (!(image_type == IH_TYPE_KERNEL &&
  581. hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
  582. fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
  583. image_type);
  584. goto out;
  585. }
  586. }
  587. /* TODO: Implement other image types. */
  588. switch (hdr->ih_type) {
  589. case IH_TYPE_KERNEL_NOLOAD:
  590. if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
  591. fprintf(stderr, "this image format (kernel_noload) cannot be "
  592. "loaded on this machine type");
  593. goto out;
  594. }
  595. hdr->ih_load = *loadaddr + sizeof(*hdr);
  596. hdr->ih_ep += hdr->ih_load;
  597. /* fall through */
  598. case IH_TYPE_KERNEL:
  599. address = hdr->ih_load;
  600. if (translate_fn) {
  601. address = translate_fn(translate_opaque, address);
  602. }
  603. if (loadaddr) {
  604. *loadaddr = hdr->ih_load;
  605. }
  606. switch (hdr->ih_comp) {
  607. case IH_COMP_NONE:
  608. break;
  609. case IH_COMP_GZIP:
  610. do_uncompress = 1;
  611. break;
  612. default:
  613. fprintf(stderr,
  614. "Unable to load u-boot images with compression type %d\n",
  615. hdr->ih_comp);
  616. goto out;
  617. }
  618. if (ep) {
  619. *ep = hdr->ih_ep;
  620. }
  621. /* TODO: Check CPU type. */
  622. if (is_linux) {
  623. if (hdr->ih_os == IH_OS_LINUX) {
  624. *is_linux = 1;
  625. } else if (hdr->ih_os == IH_OS_VXWORKS) {
  626. /*
  627. * VxWorks 7 uses the same boot interface as the Linux kernel
  628. * on Arm (64-bit only), PowerPC and RISC-V architectures.
  629. */
  630. switch (hdr->ih_arch) {
  631. case IH_ARCH_ARM64:
  632. case IH_ARCH_PPC:
  633. case IH_ARCH_RISCV:
  634. *is_linux = 1;
  635. break;
  636. default:
  637. *is_linux = 0;
  638. break;
  639. }
  640. } else {
  641. *is_linux = 0;
  642. }
  643. }
  644. break;
  645. case IH_TYPE_RAMDISK:
  646. address = *loadaddr;
  647. break;
  648. default:
  649. fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
  650. goto out;
  651. }
  652. data = g_malloc(hdr->ih_size);
  653. if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
  654. fprintf(stderr, "Error reading file\n");
  655. goto out;
  656. }
  657. if (do_uncompress) {
  658. uint8_t *compressed_data;
  659. size_t max_bytes;
  660. ssize_t bytes;
  661. compressed_data = data;
  662. max_bytes = UBOOT_MAX_GUNZIP_BYTES;
  663. data = g_malloc(max_bytes);
  664. bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
  665. g_free(compressed_data);
  666. if (bytes < 0) {
  667. fprintf(stderr, "Unable to decompress gzipped image!\n");
  668. goto out;
  669. }
  670. hdr->ih_size = bytes;
  671. }
  672. rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
  673. ret = hdr->ih_size;
  674. out:
  675. g_free(data);
  676. close(fd);
  677. return ret;
  678. }
  679. ssize_t load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  680. int *is_linux,
  681. uint64_t (*translate_fn)(void *, uint64_t),
  682. void *translate_opaque)
  683. {
  684. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  685. translate_fn, translate_opaque, NULL);
  686. }
  687. ssize_t load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  688. int *is_linux,
  689. uint64_t (*translate_fn)(void *, uint64_t),
  690. void *translate_opaque, AddressSpace *as)
  691. {
  692. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  693. translate_fn, translate_opaque, as);
  694. }
  695. /* Load a ramdisk. */
  696. ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
  697. {
  698. return load_ramdisk_as(filename, addr, max_sz, NULL);
  699. }
  700. ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
  701. AddressSpace *as)
  702. {
  703. return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
  704. NULL, NULL, as);
  705. }
  706. /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
  707. ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
  708. uint8_t **buffer)
  709. {
  710. uint8_t *compressed_data = NULL;
  711. uint8_t *data = NULL;
  712. gsize len;
  713. ssize_t bytes;
  714. int ret = -1;
  715. if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
  716. NULL)) {
  717. goto out;
  718. }
  719. /* Is it a gzip-compressed file? */
  720. if (len < 2 ||
  721. compressed_data[0] != 0x1f ||
  722. compressed_data[1] != 0x8b) {
  723. goto out;
  724. }
  725. if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
  726. max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
  727. }
  728. data = g_malloc(max_sz);
  729. bytes = gunzip(data, max_sz, compressed_data, len);
  730. if (bytes < 0) {
  731. fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
  732. filename);
  733. goto out;
  734. }
  735. /* trim to actual size and return to caller */
  736. *buffer = g_realloc(data, bytes);
  737. ret = bytes;
  738. /* ownership has been transferred to caller */
  739. data = NULL;
  740. out:
  741. g_free(compressed_data);
  742. g_free(data);
  743. return ret;
  744. }
  745. /* Load a gzip-compressed kernel. */
  746. ssize_t load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
  747. {
  748. ssize_t bytes;
  749. uint8_t *data;
  750. bytes = load_image_gzipped_buffer(filename, max_sz, &data);
  751. if (bytes != -1) {
  752. rom_add_blob_fixed(filename, data, bytes, addr);
  753. g_free(data);
  754. }
  755. return bytes;
  756. }
  757. /* The PE/COFF MS-DOS stub magic number */
  758. #define EFI_PE_MSDOS_MAGIC "MZ"
  759. /*
  760. * The Linux header magic number for a EFI PE/COFF
  761. * image targeting an unspecified architecture.
  762. */
  763. #define EFI_PE_LINUX_MAGIC "\xcd\x23\x82\x81"
  764. /*
  765. * Bootable Linux kernel images may be packaged as EFI zboot images, which are
  766. * self-decompressing executables when loaded via EFI. The compressed payload
  767. * can also be extracted from the image and decompressed by a non-EFI loader.
  768. *
  769. * The de facto specification for this format is at the following URL:
  770. *
  771. * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/firmware/efi/libstub/zboot-header.S
  772. *
  773. * This definition is based on Linux upstream commit 29636a5ce87beba.
  774. */
  775. struct linux_efi_zboot_header {
  776. uint8_t msdos_magic[2]; /* PE/COFF 'MZ' magic number */
  777. uint8_t reserved0[2];
  778. uint8_t zimg[4]; /* "zimg" for Linux EFI zboot images */
  779. uint32_t payload_offset; /* LE offset to compressed payload */
  780. uint32_t payload_size; /* LE size of the compressed payload */
  781. uint8_t reserved1[8];
  782. char compression_type[32]; /* Compression type, NUL terminated */
  783. uint8_t linux_magic[4]; /* Linux header magic */
  784. uint32_t pe_header_offset; /* LE offset to the PE header */
  785. };
  786. /*
  787. * Check whether *buffer points to a Linux EFI zboot image in memory.
  788. *
  789. * If it does, attempt to decompress it to a new buffer, and free the old one.
  790. * If any of this fails, return an error to the caller.
  791. *
  792. * If the image is not a Linux EFI zboot image, do nothing and return success.
  793. */
  794. ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size)
  795. {
  796. const struct linux_efi_zboot_header *header;
  797. uint8_t *data = NULL;
  798. int ploff, plsize;
  799. ssize_t bytes;
  800. /* ignore if this is too small to be a EFI zboot image */
  801. if (*size < sizeof(*header)) {
  802. return 0;
  803. }
  804. header = (struct linux_efi_zboot_header *)*buffer;
  805. /* ignore if this is not a Linux EFI zboot image */
  806. if (memcmp(&header->msdos_magic, EFI_PE_MSDOS_MAGIC, 2) != 0 ||
  807. memcmp(&header->zimg, "zimg", 4) != 0 ||
  808. memcmp(&header->linux_magic, EFI_PE_LINUX_MAGIC, 4) != 0) {
  809. return 0;
  810. }
  811. if (strcmp(header->compression_type, "gzip") != 0) {
  812. fprintf(stderr,
  813. "unable to handle EFI zboot image with \"%.*s\" compression\n",
  814. (int)sizeof(header->compression_type) - 1,
  815. header->compression_type);
  816. return -1;
  817. }
  818. ploff = ldl_le_p(&header->payload_offset);
  819. plsize = ldl_le_p(&header->payload_size);
  820. if (ploff < 0 || plsize < 0 || ploff + plsize > *size) {
  821. fprintf(stderr, "unable to handle corrupt EFI zboot image\n");
  822. return -1;
  823. }
  824. data = g_malloc(LOAD_IMAGE_MAX_GUNZIP_BYTES);
  825. bytes = gunzip(data, LOAD_IMAGE_MAX_GUNZIP_BYTES, *buffer + ploff, plsize);
  826. if (bytes < 0) {
  827. fprintf(stderr, "failed to decompress EFI zboot image\n");
  828. g_free(data);
  829. return -1;
  830. }
  831. g_free(*buffer);
  832. *buffer = g_realloc(data, bytes);
  833. *size = bytes;
  834. return bytes;
  835. }
  836. /*
  837. * Functions for reboot-persistent memory regions.
  838. * - used for vga bios and option roms.
  839. * - also linux kernel (-kernel / -initrd).
  840. */
  841. typedef struct Rom Rom;
  842. struct Rom {
  843. char *name;
  844. char *path;
  845. /* datasize is the amount of memory allocated in "data". If datasize is less
  846. * than romsize, it means that the area from datasize to romsize is filled
  847. * with zeros.
  848. */
  849. size_t romsize;
  850. size_t datasize;
  851. uint8_t *data;
  852. MemoryRegion *mr;
  853. AddressSpace *as;
  854. int isrom;
  855. char *fw_dir;
  856. char *fw_file;
  857. GMappedFile *mapped_file;
  858. bool committed;
  859. hwaddr addr;
  860. QTAILQ_ENTRY(Rom) next;
  861. };
  862. static FWCfgState *fw_cfg;
  863. static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
  864. /*
  865. * rom->data can be heap-allocated or memory-mapped (e.g. when added with
  866. * rom_add_elf_program())
  867. */
  868. static void rom_free_data(Rom *rom)
  869. {
  870. if (rom->mapped_file) {
  871. g_mapped_file_unref(rom->mapped_file);
  872. rom->mapped_file = NULL;
  873. } else {
  874. g_free(rom->data);
  875. }
  876. rom->data = NULL;
  877. }
  878. static void rom_free(Rom *rom)
  879. {
  880. rom_free_data(rom);
  881. g_free(rom->path);
  882. g_free(rom->name);
  883. g_free(rom->fw_dir);
  884. g_free(rom->fw_file);
  885. g_free(rom);
  886. }
  887. static inline bool rom_order_compare(Rom *rom, Rom *item)
  888. {
  889. return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
  890. (rom->as == item->as && rom->addr >= item->addr);
  891. }
  892. static void rom_insert(Rom *rom)
  893. {
  894. Rom *item;
  895. if (roms_loaded) {
  896. hw_error ("ROM images must be loaded at startup\n");
  897. }
  898. /* The user didn't specify an address space, this is the default */
  899. if (!rom->as) {
  900. rom->as = &address_space_memory;
  901. }
  902. rom->committed = false;
  903. /* List is ordered by load address in the same address space */
  904. QTAILQ_FOREACH(item, &roms, next) {
  905. if (rom_order_compare(rom, item)) {
  906. continue;
  907. }
  908. QTAILQ_INSERT_BEFORE(item, rom, next);
  909. return;
  910. }
  911. QTAILQ_INSERT_TAIL(&roms, rom, next);
  912. }
  913. static void fw_cfg_resized(const char *id, uint64_t length, void *host)
  914. {
  915. if (fw_cfg) {
  916. fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
  917. }
  918. }
  919. static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
  920. {
  921. void *data;
  922. rom->mr = g_malloc(sizeof(*rom->mr));
  923. memory_region_init_resizeable_ram(rom->mr, owner, name,
  924. rom->datasize, rom->romsize,
  925. fw_cfg_resized,
  926. &error_fatal);
  927. memory_region_set_readonly(rom->mr, ro);
  928. vmstate_register_ram_global(rom->mr);
  929. data = memory_region_get_ram_ptr(rom->mr);
  930. memcpy(data, rom->data, rom->datasize);
  931. return data;
  932. }
  933. ssize_t rom_add_file(const char *file, const char *fw_dir,
  934. hwaddr addr, int32_t bootindex,
  935. bool has_option_rom, MemoryRegion *mr,
  936. AddressSpace *as)
  937. {
  938. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  939. Rom *rom;
  940. ssize_t rc;
  941. int fd = -1;
  942. char devpath[100];
  943. if (as && mr) {
  944. fprintf(stderr, "Specifying an Address Space and Memory Region is " \
  945. "not valid when loading a rom\n");
  946. /* We haven't allocated anything so we don't need any cleanup */
  947. return -1;
  948. }
  949. rom = g_malloc0(sizeof(*rom));
  950. rom->name = g_strdup(file);
  951. rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
  952. rom->as = as;
  953. if (rom->path == NULL) {
  954. rom->path = g_strdup(file);
  955. }
  956. fd = open(rom->path, O_RDONLY | O_BINARY);
  957. if (fd == -1) {
  958. fprintf(stderr, "Could not open option rom '%s': %s\n",
  959. rom->path, strerror(errno));
  960. goto err;
  961. }
  962. if (fw_dir) {
  963. rom->fw_dir = g_strdup(fw_dir);
  964. rom->fw_file = g_strdup(file);
  965. }
  966. rom->addr = addr;
  967. rom->romsize = lseek(fd, 0, SEEK_END);
  968. if (rom->romsize == -1) {
  969. fprintf(stderr, "rom: file %-20s: get size error: %s\n",
  970. rom->name, strerror(errno));
  971. goto err;
  972. }
  973. rom->datasize = rom->romsize;
  974. rom->data = g_malloc0(rom->datasize);
  975. lseek(fd, 0, SEEK_SET);
  976. rc = read(fd, rom->data, rom->datasize);
  977. if (rc != rom->datasize) {
  978. fprintf(stderr, "rom: file %-20s: read error: rc=%zd (expected %zd)\n",
  979. rom->name, rc, rom->datasize);
  980. goto err;
  981. }
  982. close(fd);
  983. rom_insert(rom);
  984. if (rom->fw_file && fw_cfg) {
  985. const char *basename;
  986. char fw_file_name[FW_CFG_MAX_FILE_PATH];
  987. void *data;
  988. basename = strrchr(rom->fw_file, '/');
  989. if (basename) {
  990. basename++;
  991. } else {
  992. basename = rom->fw_file;
  993. }
  994. snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
  995. basename);
  996. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  997. if ((!has_option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
  998. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
  999. } else {
  1000. data = rom->data;
  1001. }
  1002. fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
  1003. } else {
  1004. if (mr) {
  1005. rom->mr = mr;
  1006. snprintf(devpath, sizeof(devpath), "/rom@%s", file);
  1007. } else {
  1008. snprintf(devpath, sizeof(devpath), "/rom@" HWADDR_FMT_plx, addr);
  1009. }
  1010. }
  1011. add_boot_device_path(bootindex, NULL, devpath);
  1012. return 0;
  1013. err:
  1014. if (fd != -1)
  1015. close(fd);
  1016. rom_free(rom);
  1017. return -1;
  1018. }
  1019. MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
  1020. size_t max_len, hwaddr addr, const char *fw_file_name,
  1021. FWCfgCallback fw_callback, void *callback_opaque,
  1022. AddressSpace *as, bool read_only)
  1023. {
  1024. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  1025. Rom *rom;
  1026. MemoryRegion *mr = NULL;
  1027. rom = g_malloc0(sizeof(*rom));
  1028. rom->name = g_strdup(name);
  1029. rom->as = as;
  1030. rom->addr = addr;
  1031. rom->romsize = max_len ? max_len : len;
  1032. rom->datasize = len;
  1033. g_assert(rom->romsize >= rom->datasize);
  1034. rom->data = g_malloc0(rom->datasize);
  1035. memcpy(rom->data, blob, len);
  1036. rom_insert(rom);
  1037. if (fw_file_name && fw_cfg) {
  1038. char devpath[100];
  1039. void *data;
  1040. if (read_only) {
  1041. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  1042. } else {
  1043. snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
  1044. }
  1045. if (mc->rom_file_has_mr) {
  1046. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
  1047. mr = rom->mr;
  1048. } else {
  1049. data = rom->data;
  1050. }
  1051. fw_cfg_add_file_callback(fw_cfg, fw_file_name,
  1052. fw_callback, NULL, callback_opaque,
  1053. data, rom->datasize, read_only);
  1054. }
  1055. return mr;
  1056. }
  1057. /* This function is specific for elf program because we don't need to allocate
  1058. * all the rom. We just allocate the first part and the rest is just zeros. This
  1059. * is why romsize and datasize are different. Also, this function takes its own
  1060. * reference to "mapped_file", so we don't have to allocate and copy the buffer.
  1061. */
  1062. int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
  1063. size_t datasize, size_t romsize, hwaddr addr,
  1064. AddressSpace *as)
  1065. {
  1066. Rom *rom;
  1067. rom = g_malloc0(sizeof(*rom));
  1068. rom->name = g_strdup(name);
  1069. rom->addr = addr;
  1070. rom->datasize = datasize;
  1071. rom->romsize = romsize;
  1072. rom->data = data;
  1073. rom->as = as;
  1074. if (mapped_file && data) {
  1075. g_mapped_file_ref(mapped_file);
  1076. rom->mapped_file = mapped_file;
  1077. }
  1078. rom_insert(rom);
  1079. return 0;
  1080. }
  1081. ssize_t rom_add_vga(const char *file)
  1082. {
  1083. return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
  1084. }
  1085. ssize_t rom_add_option(const char *file, int32_t bootindex)
  1086. {
  1087. return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
  1088. }
  1089. static void rom_reset(void *unused)
  1090. {
  1091. Rom *rom;
  1092. QTAILQ_FOREACH(rom, &roms, next) {
  1093. if (rom->fw_file) {
  1094. continue;
  1095. }
  1096. /*
  1097. * We don't need to fill in the RAM with ROM data because we'll fill
  1098. * the data in during the next incoming migration in all cases. Note
  1099. * that some of those RAMs can actually be modified by the guest.
  1100. */
  1101. if (runstate_check(RUN_STATE_INMIGRATE)) {
  1102. if (rom->data && rom->isrom) {
  1103. /*
  1104. * Free it so that a rom_reset after migration doesn't
  1105. * overwrite a potentially modified 'rom'.
  1106. */
  1107. rom_free_data(rom);
  1108. }
  1109. continue;
  1110. }
  1111. if (rom->data == NULL) {
  1112. continue;
  1113. }
  1114. if (rom->mr) {
  1115. void *host = memory_region_get_ram_ptr(rom->mr);
  1116. memcpy(host, rom->data, rom->datasize);
  1117. memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
  1118. } else {
  1119. address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
  1120. rom->data, rom->datasize);
  1121. address_space_set(rom->as, rom->addr + rom->datasize, 0,
  1122. rom->romsize - rom->datasize,
  1123. MEMTXATTRS_UNSPECIFIED);
  1124. }
  1125. if (rom->isrom) {
  1126. /* rom needs to be written only once */
  1127. rom_free_data(rom);
  1128. }
  1129. /*
  1130. * The rom loader is really on the same level as firmware in the guest
  1131. * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
  1132. * that the instruction cache for that new region is clear, so that the
  1133. * CPU definitely fetches its instructions from the just written data.
  1134. */
  1135. cpu_flush_icache_range(rom->addr, rom->datasize);
  1136. trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom);
  1137. }
  1138. }
  1139. /* Return true if two consecutive ROMs in the ROM list overlap */
  1140. static bool roms_overlap(Rom *last_rom, Rom *this_rom)
  1141. {
  1142. if (!last_rom) {
  1143. return false;
  1144. }
  1145. return last_rom->as == this_rom->as &&
  1146. last_rom->addr + last_rom->romsize > this_rom->addr;
  1147. }
  1148. static const char *rom_as_name(Rom *rom)
  1149. {
  1150. const char *name = rom->as ? rom->as->name : NULL;
  1151. return name ?: "anonymous";
  1152. }
  1153. static void rom_print_overlap_error_header(void)
  1154. {
  1155. error_report("Some ROM regions are overlapping");
  1156. error_printf(
  1157. "These ROM regions might have been loaded by "
  1158. "direct user request or by default.\n"
  1159. "They could be BIOS/firmware images, a guest kernel, "
  1160. "initrd or some other file loaded into guest memory.\n"
  1161. "Check whether you intended to load all this guest code, and "
  1162. "whether it has been built to load to the correct addresses.\n");
  1163. }
  1164. static void rom_print_one_overlap_error(Rom *last_rom, Rom *rom)
  1165. {
  1166. error_printf(
  1167. "\nThe following two regions overlap (in the %s address space):\n",
  1168. rom_as_name(rom));
  1169. error_printf(
  1170. " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
  1171. last_rom->name, last_rom->addr, last_rom->addr + last_rom->romsize);
  1172. error_printf(
  1173. " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
  1174. rom->name, rom->addr, rom->addr + rom->romsize);
  1175. }
  1176. int rom_check_and_register_reset(void)
  1177. {
  1178. MemoryRegionSection section;
  1179. Rom *rom, *last_rom = NULL;
  1180. bool found_overlap = false;
  1181. QTAILQ_FOREACH(rom, &roms, next) {
  1182. if (rom->fw_file) {
  1183. continue;
  1184. }
  1185. if (!rom->mr) {
  1186. if (roms_overlap(last_rom, rom)) {
  1187. if (!found_overlap) {
  1188. found_overlap = true;
  1189. rom_print_overlap_error_header();
  1190. }
  1191. rom_print_one_overlap_error(last_rom, rom);
  1192. /* Keep going through the list so we report all overlaps */
  1193. }
  1194. last_rom = rom;
  1195. }
  1196. section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
  1197. rom->addr, 1);
  1198. rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
  1199. memory_region_unref(section.mr);
  1200. }
  1201. if (found_overlap) {
  1202. return -1;
  1203. }
  1204. qemu_register_reset(rom_reset, NULL);
  1205. roms_loaded = 1;
  1206. return 0;
  1207. }
  1208. void rom_set_fw(FWCfgState *f)
  1209. {
  1210. fw_cfg = f;
  1211. }
  1212. void rom_set_order_override(int order)
  1213. {
  1214. if (!fw_cfg)
  1215. return;
  1216. fw_cfg_set_order_override(fw_cfg, order);
  1217. }
  1218. void rom_reset_order_override(void)
  1219. {
  1220. if (!fw_cfg)
  1221. return;
  1222. fw_cfg_reset_order_override(fw_cfg);
  1223. }
  1224. void rom_transaction_begin(void)
  1225. {
  1226. Rom *rom;
  1227. /* Ignore ROMs added without the transaction API */
  1228. QTAILQ_FOREACH(rom, &roms, next) {
  1229. rom->committed = true;
  1230. }
  1231. }
  1232. void rom_transaction_end(bool commit)
  1233. {
  1234. Rom *rom;
  1235. Rom *tmp;
  1236. QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
  1237. if (rom->committed) {
  1238. continue;
  1239. }
  1240. if (commit) {
  1241. rom->committed = true;
  1242. } else {
  1243. QTAILQ_REMOVE(&roms, rom, next);
  1244. rom_free(rom);
  1245. }
  1246. }
  1247. }
  1248. static Rom *find_rom(hwaddr addr, size_t size)
  1249. {
  1250. Rom *rom;
  1251. QTAILQ_FOREACH(rom, &roms, next) {
  1252. if (rom->fw_file) {
  1253. continue;
  1254. }
  1255. if (rom->mr) {
  1256. continue;
  1257. }
  1258. if (rom->addr > addr) {
  1259. continue;
  1260. }
  1261. if (rom->addr + rom->romsize < addr + size) {
  1262. continue;
  1263. }
  1264. return rom;
  1265. }
  1266. return NULL;
  1267. }
  1268. typedef struct RomSec {
  1269. hwaddr base;
  1270. int se; /* start/end flag */
  1271. } RomSec;
  1272. /*
  1273. * Sort into address order. We break ties between rom-startpoints
  1274. * and rom-endpoints in favour of the startpoint, by sorting the 0->1
  1275. * transition before the 1->0 transition. Either way round would
  1276. * work, but this way saves a little work later by avoiding
  1277. * dealing with "gaps" of 0 length.
  1278. */
  1279. static gint sort_secs(gconstpointer a, gconstpointer b)
  1280. {
  1281. RomSec *ra = (RomSec *) a;
  1282. RomSec *rb = (RomSec *) b;
  1283. if (ra->base == rb->base) {
  1284. return ra->se - rb->se;
  1285. }
  1286. return ra->base > rb->base ? 1 : -1;
  1287. }
  1288. static GList *add_romsec_to_list(GList *secs, hwaddr base, int se)
  1289. {
  1290. RomSec *cand = g_new(RomSec, 1);
  1291. cand->base = base;
  1292. cand->se = se;
  1293. return g_list_prepend(secs, cand);
  1294. }
  1295. RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
  1296. {
  1297. Rom *rom;
  1298. RomSec *cand;
  1299. RomGap res = {0, 0};
  1300. hwaddr gapstart = base;
  1301. GList *it, *secs = NULL;
  1302. int count = 0;
  1303. QTAILQ_FOREACH(rom, &roms, next) {
  1304. /* Ignore blobs being loaded to special places */
  1305. if (rom->mr || rom->fw_file) {
  1306. continue;
  1307. }
  1308. /* ignore anything finishing below base */
  1309. if (rom->addr + rom->romsize <= base) {
  1310. continue;
  1311. }
  1312. /* ignore anything starting above the region */
  1313. if (rom->addr >= base + size) {
  1314. continue;
  1315. }
  1316. /* Save the start and end of each relevant ROM */
  1317. secs = add_romsec_to_list(secs, rom->addr, 1);
  1318. if (rom->addr + rom->romsize < base + size) {
  1319. secs = add_romsec_to_list(secs, rom->addr + rom->romsize, -1);
  1320. }
  1321. }
  1322. /* sentinel */
  1323. secs = add_romsec_to_list(secs, base + size, 1);
  1324. secs = g_list_sort(secs, sort_secs);
  1325. for (it = g_list_first(secs); it; it = g_list_next(it)) {
  1326. cand = (RomSec *) it->data;
  1327. if (count == 0 && count + cand->se == 1) {
  1328. size_t gap = cand->base - gapstart;
  1329. if (gap > res.size) {
  1330. res.base = gapstart;
  1331. res.size = gap;
  1332. }
  1333. } else if (count == 1 && count + cand->se == 0) {
  1334. gapstart = cand->base;
  1335. }
  1336. count += cand->se;
  1337. }
  1338. g_list_free_full(secs, g_free);
  1339. return res;
  1340. }
  1341. /*
  1342. * Copies memory from registered ROMs to dest. Any memory that is contained in
  1343. * a ROM between addr and addr + size is copied. Note that this can involve
  1344. * multiple ROMs, which need not start at addr and need not end at addr + size.
  1345. */
  1346. int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
  1347. {
  1348. hwaddr end = addr + size;
  1349. uint8_t *s, *d = dest;
  1350. size_t l = 0;
  1351. Rom *rom;
  1352. QTAILQ_FOREACH(rom, &roms, next) {
  1353. if (rom->fw_file) {
  1354. continue;
  1355. }
  1356. if (rom->mr) {
  1357. continue;
  1358. }
  1359. if (rom->addr + rom->romsize < addr) {
  1360. continue;
  1361. }
  1362. if (rom->addr > end || rom->addr < addr) {
  1363. break;
  1364. }
  1365. d = dest + (rom->addr - addr);
  1366. s = rom->data;
  1367. l = rom->datasize;
  1368. if ((d + l) > (dest + size)) {
  1369. l = dest - d;
  1370. }
  1371. if (l > 0) {
  1372. memcpy(d, s, l);
  1373. }
  1374. if (rom->romsize > rom->datasize) {
  1375. /* If datasize is less than romsize, it means that we didn't
  1376. * allocate all the ROM because the trailing data are only zeros.
  1377. */
  1378. d += l;
  1379. l = rom->romsize - rom->datasize;
  1380. if ((d + l) > (dest + size)) {
  1381. /* Rom size doesn't fit in the destination area. Adjust to avoid
  1382. * overflow.
  1383. */
  1384. l = dest - d;
  1385. }
  1386. if (l > 0) {
  1387. memset(d, 0x0, l);
  1388. }
  1389. }
  1390. }
  1391. return (d + l) - dest;
  1392. }
  1393. void *rom_ptr(hwaddr addr, size_t size)
  1394. {
  1395. Rom *rom;
  1396. rom = find_rom(addr, size);
  1397. if (!rom || !rom->data)
  1398. return NULL;
  1399. return rom->data + (addr - rom->addr);
  1400. }
  1401. typedef struct FindRomCBData {
  1402. size_t size; /* Amount of data we want from ROM, in bytes */
  1403. MemoryRegion *mr; /* MR at the unaliased guest addr */
  1404. hwaddr xlat; /* Offset of addr within mr */
  1405. void *rom; /* Output: rom data pointer, if found */
  1406. } FindRomCBData;
  1407. static bool find_rom_cb(Int128 start, Int128 len, const MemoryRegion *mr,
  1408. hwaddr offset_in_region, void *opaque)
  1409. {
  1410. FindRomCBData *cbdata = opaque;
  1411. hwaddr alias_addr;
  1412. if (mr != cbdata->mr) {
  1413. return false;
  1414. }
  1415. alias_addr = int128_get64(start) + cbdata->xlat - offset_in_region;
  1416. cbdata->rom = rom_ptr(alias_addr, cbdata->size);
  1417. if (!cbdata->rom) {
  1418. return false;
  1419. }
  1420. /* Found a match, stop iterating */
  1421. return true;
  1422. }
  1423. void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size)
  1424. {
  1425. /*
  1426. * Find any ROM data for the given guest address range. If there
  1427. * is a ROM blob then return a pointer to the host memory
  1428. * corresponding to 'addr'; otherwise return NULL.
  1429. *
  1430. * We look not only for ROM blobs that were loaded directly to
  1431. * addr, but also for ROM blobs that were loaded to aliases of
  1432. * that memory at other addresses within the AddressSpace.
  1433. *
  1434. * Note that we do not check @as against the 'as' member in the
  1435. * 'struct Rom' returned by rom_ptr(). The Rom::as is the
  1436. * AddressSpace which the rom blob should be written to, whereas
  1437. * our @as argument is the AddressSpace which we are (effectively)
  1438. * reading from, and the same underlying RAM will often be visible
  1439. * in multiple AddressSpaces. (A common example is a ROM blob
  1440. * written to the 'system' address space but then read back via a
  1441. * CPU's cpu->as pointer.) This does mean we might potentially
  1442. * return a false-positive match if a ROM blob was loaded into an
  1443. * AS which is entirely separate and distinct from the one we're
  1444. * querying, but this issue exists also for rom_ptr() and hasn't
  1445. * caused any problems in practice.
  1446. */
  1447. FlatView *fv;
  1448. void *rom;
  1449. hwaddr len_unused;
  1450. FindRomCBData cbdata = {};
  1451. /* Easy case: there's data at the actual address */
  1452. rom = rom_ptr(addr, size);
  1453. if (rom) {
  1454. return rom;
  1455. }
  1456. RCU_READ_LOCK_GUARD();
  1457. fv = address_space_to_flatview(as);
  1458. cbdata.mr = flatview_translate(fv, addr, &cbdata.xlat, &len_unused,
  1459. false, MEMTXATTRS_UNSPECIFIED);
  1460. if (!cbdata.mr) {
  1461. /* Nothing at this address, so there can't be any aliasing */
  1462. return NULL;
  1463. }
  1464. cbdata.size = size;
  1465. flatview_for_each_range(fv, find_rom_cb, &cbdata);
  1466. return cbdata.rom;
  1467. }
  1468. HumanReadableText *qmp_x_query_roms(Error **errp)
  1469. {
  1470. Rom *rom;
  1471. g_autoptr(GString) buf = g_string_new("");
  1472. QTAILQ_FOREACH(rom, &roms, next) {
  1473. if (rom->mr) {
  1474. g_string_append_printf(buf, "%s"
  1475. " size=0x%06zx name=\"%s\"\n",
  1476. memory_region_name(rom->mr),
  1477. rom->romsize,
  1478. rom->name);
  1479. } else if (!rom->fw_file) {
  1480. g_string_append_printf(buf, "addr=" HWADDR_FMT_plx
  1481. " size=0x%06zx mem=%s name=\"%s\"\n",
  1482. rom->addr, rom->romsize,
  1483. rom->isrom ? "rom" : "ram",
  1484. rom->name);
  1485. } else {
  1486. g_string_append_printf(buf, "fw=%s/%s"
  1487. " size=0x%06zx name=\"%s\"\n",
  1488. rom->fw_dir,
  1489. rom->fw_file,
  1490. rom->romsize,
  1491. rom->name);
  1492. }
  1493. }
  1494. return human_readable_text_from_str(buf);
  1495. }
  1496. typedef enum HexRecord HexRecord;
  1497. enum HexRecord {
  1498. DATA_RECORD = 0,
  1499. EOF_RECORD,
  1500. EXT_SEG_ADDR_RECORD,
  1501. START_SEG_ADDR_RECORD,
  1502. EXT_LINEAR_ADDR_RECORD,
  1503. START_LINEAR_ADDR_RECORD,
  1504. };
  1505. /* Each record contains a 16-bit address which is combined with the upper 16
  1506. * bits of the implicit "next address" to form a 32-bit address.
  1507. */
  1508. #define NEXT_ADDR_MASK 0xffff0000
  1509. #define DATA_FIELD_MAX_LEN 0xff
  1510. #define LEN_EXCEPT_DATA 0x5
  1511. /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
  1512. * sizeof(checksum) */
  1513. typedef struct {
  1514. uint8_t byte_count;
  1515. uint16_t address;
  1516. uint8_t record_type;
  1517. uint8_t data[DATA_FIELD_MAX_LEN];
  1518. uint8_t checksum;
  1519. } HexLine;
  1520. /* return 0 or -1 if error */
  1521. static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
  1522. uint32_t *index, const bool in_process)
  1523. {
  1524. /* +-------+---------------+-------+---------------------+--------+
  1525. * | byte | |record | | |
  1526. * | count | address | type | data |checksum|
  1527. * +-------+---------------+-------+---------------------+--------+
  1528. * ^ ^ ^ ^ ^ ^
  1529. * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
  1530. */
  1531. uint8_t value = 0;
  1532. uint32_t idx = *index;
  1533. /* ignore space */
  1534. if (g_ascii_isspace(c)) {
  1535. return true;
  1536. }
  1537. if (!g_ascii_isxdigit(c) || !in_process) {
  1538. return false;
  1539. }
  1540. value = g_ascii_xdigit_value(c);
  1541. value = (idx & 0x1) ? (value & 0xf) : (value << 4);
  1542. if (idx < 2) {
  1543. line->byte_count |= value;
  1544. } else if (2 <= idx && idx < 6) {
  1545. line->address <<= 4;
  1546. line->address += g_ascii_xdigit_value(c);
  1547. } else if (6 <= idx && idx < 8) {
  1548. line->record_type |= value;
  1549. } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
  1550. line->data[(idx - 8) >> 1] |= value;
  1551. } else if (8 + 2 * line->byte_count <= idx &&
  1552. idx < 10 + 2 * line->byte_count) {
  1553. line->checksum |= value;
  1554. } else {
  1555. return false;
  1556. }
  1557. *our_checksum += value;
  1558. ++(*index);
  1559. return true;
  1560. }
  1561. typedef struct {
  1562. const char *filename;
  1563. HexLine line;
  1564. uint8_t *bin_buf;
  1565. hwaddr *start_addr;
  1566. int total_size;
  1567. uint32_t next_address_to_write;
  1568. uint32_t current_address;
  1569. uint32_t current_rom_index;
  1570. uint32_t rom_start_address;
  1571. AddressSpace *as;
  1572. bool complete;
  1573. } HexParser;
  1574. /* return size or -1 if error */
  1575. static int handle_record_type(HexParser *parser)
  1576. {
  1577. HexLine *line = &(parser->line);
  1578. switch (line->record_type) {
  1579. case DATA_RECORD:
  1580. parser->current_address =
  1581. (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
  1582. /* verify this is a contiguous block of memory */
  1583. if (parser->current_address != parser->next_address_to_write) {
  1584. if (parser->current_rom_index != 0) {
  1585. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1586. parser->current_rom_index,
  1587. parser->rom_start_address, parser->as);
  1588. }
  1589. parser->rom_start_address = parser->current_address;
  1590. parser->current_rom_index = 0;
  1591. }
  1592. /* copy from line buffer to output bin_buf */
  1593. memcpy(parser->bin_buf + parser->current_rom_index, line->data,
  1594. line->byte_count);
  1595. parser->current_rom_index += line->byte_count;
  1596. parser->total_size += line->byte_count;
  1597. /* save next address to write */
  1598. parser->next_address_to_write =
  1599. parser->current_address + line->byte_count;
  1600. break;
  1601. case EOF_RECORD:
  1602. if (parser->current_rom_index != 0) {
  1603. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1604. parser->current_rom_index,
  1605. parser->rom_start_address, parser->as);
  1606. }
  1607. parser->complete = true;
  1608. return parser->total_size;
  1609. case EXT_SEG_ADDR_RECORD:
  1610. case EXT_LINEAR_ADDR_RECORD:
  1611. if (line->byte_count != 2 && line->address != 0) {
  1612. return -1;
  1613. }
  1614. if (parser->current_rom_index != 0) {
  1615. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1616. parser->current_rom_index,
  1617. parser->rom_start_address, parser->as);
  1618. }
  1619. /* save next address to write,
  1620. * in case of non-contiguous block of memory */
  1621. parser->next_address_to_write = (line->data[0] << 12) |
  1622. (line->data[1] << 4);
  1623. if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
  1624. parser->next_address_to_write <<= 12;
  1625. }
  1626. parser->rom_start_address = parser->next_address_to_write;
  1627. parser->current_rom_index = 0;
  1628. break;
  1629. case START_SEG_ADDR_RECORD:
  1630. if (line->byte_count != 4 && line->address != 0) {
  1631. return -1;
  1632. }
  1633. /* x86 16-bit CS:IP segmented addressing */
  1634. *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
  1635. ((line->data[2] << 8) | line->data[3]);
  1636. break;
  1637. case START_LINEAR_ADDR_RECORD:
  1638. if (line->byte_count != 4 && line->address != 0) {
  1639. return -1;
  1640. }
  1641. *(parser->start_addr) = ldl_be_p(line->data);
  1642. break;
  1643. default:
  1644. return -1;
  1645. }
  1646. return parser->total_size;
  1647. }
  1648. /* return size or -1 if error */
  1649. static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
  1650. size_t hex_blob_size, AddressSpace *as)
  1651. {
  1652. bool in_process = false; /* avoid re-enter and
  1653. * check whether record begin with ':' */
  1654. uint8_t *end = hex_blob + hex_blob_size;
  1655. uint8_t our_checksum = 0;
  1656. uint32_t record_index = 0;
  1657. HexParser parser = {
  1658. .filename = filename,
  1659. .bin_buf = g_malloc(hex_blob_size),
  1660. .start_addr = addr,
  1661. .as = as,
  1662. .complete = false
  1663. };
  1664. rom_transaction_begin();
  1665. for (; hex_blob < end && !parser.complete; ++hex_blob) {
  1666. switch (*hex_blob) {
  1667. case '\r':
  1668. case '\n':
  1669. if (!in_process) {
  1670. break;
  1671. }
  1672. in_process = false;
  1673. if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
  1674. record_index ||
  1675. our_checksum != 0) {
  1676. parser.total_size = -1;
  1677. goto out;
  1678. }
  1679. if (handle_record_type(&parser) == -1) {
  1680. parser.total_size = -1;
  1681. goto out;
  1682. }
  1683. break;
  1684. /* start of a new record. */
  1685. case ':':
  1686. memset(&parser.line, 0, sizeof(HexLine));
  1687. in_process = true;
  1688. record_index = 0;
  1689. break;
  1690. /* decoding lines */
  1691. default:
  1692. if (!parse_record(&parser.line, &our_checksum, *hex_blob,
  1693. &record_index, in_process)) {
  1694. parser.total_size = -1;
  1695. goto out;
  1696. }
  1697. break;
  1698. }
  1699. }
  1700. out:
  1701. g_free(parser.bin_buf);
  1702. rom_transaction_end(parser.total_size != -1);
  1703. return parser.total_size;
  1704. }
  1705. /* return size or -1 if error */
  1706. ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
  1707. AddressSpace *as)
  1708. {
  1709. gsize hex_blob_size;
  1710. gchar *hex_blob;
  1711. ssize_t total_size = 0;
  1712. if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
  1713. return -1;
  1714. }
  1715. total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,
  1716. hex_blob_size, as);
  1717. g_free(hex_blob);
  1718. return total_size;
  1719. }