loader.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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 "accel/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"
  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"
  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 != ELF_LOAD_FAILED) {
  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. return -1;
  545. }
  546. dstbytes = s.next_out - (unsigned char *) dst;
  547. inflateEnd(&s);
  548. return dstbytes;
  549. toosmall:
  550. puts("Error: gunzip out of data in header\n");
  551. return -1;
  552. }
  553. /* Load a U-Boot image. */
  554. static ssize_t load_uboot_image(const char *filename, hwaddr *ep,
  555. hwaddr *loadaddr, int *is_linux,
  556. uint8_t image_type,
  557. uint64_t (*translate_fn)(void *, uint64_t),
  558. void *translate_opaque, AddressSpace *as)
  559. {
  560. int fd;
  561. ssize_t size;
  562. hwaddr address;
  563. uboot_image_header_t h;
  564. uboot_image_header_t *hdr = &h;
  565. uint8_t *data = NULL;
  566. int ret = -1;
  567. int do_uncompress = 0;
  568. fd = open(filename, O_RDONLY | O_BINARY);
  569. if (fd < 0)
  570. return -1;
  571. size = read(fd, hdr, sizeof(uboot_image_header_t));
  572. if (size < sizeof(uboot_image_header_t)) {
  573. goto out;
  574. }
  575. bswap_uboot_header(hdr);
  576. if (hdr->ih_magic != IH_MAGIC)
  577. goto out;
  578. if (hdr->ih_type != image_type) {
  579. if (!(image_type == IH_TYPE_KERNEL &&
  580. hdr->ih_type == IH_TYPE_KERNEL_NOLOAD)) {
  581. fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type,
  582. image_type);
  583. goto out;
  584. }
  585. }
  586. /* TODO: Implement other image types. */
  587. switch (hdr->ih_type) {
  588. case IH_TYPE_KERNEL_NOLOAD:
  589. if (!loadaddr || *loadaddr == LOAD_UIMAGE_LOADADDR_INVALID) {
  590. fprintf(stderr, "this image format (kernel_noload) cannot be "
  591. "loaded on this machine type");
  592. goto out;
  593. }
  594. hdr->ih_load = *loadaddr + sizeof(*hdr);
  595. hdr->ih_ep += hdr->ih_load;
  596. /* fall through */
  597. case IH_TYPE_KERNEL:
  598. address = hdr->ih_load;
  599. if (translate_fn) {
  600. address = translate_fn(translate_opaque, address);
  601. }
  602. if (loadaddr) {
  603. *loadaddr = hdr->ih_load;
  604. }
  605. switch (hdr->ih_comp) {
  606. case IH_COMP_NONE:
  607. break;
  608. case IH_COMP_GZIP:
  609. do_uncompress = 1;
  610. break;
  611. default:
  612. fprintf(stderr,
  613. "Unable to load u-boot images with compression type %d\n",
  614. hdr->ih_comp);
  615. goto out;
  616. }
  617. if (ep) {
  618. *ep = hdr->ih_ep;
  619. }
  620. /* TODO: Check CPU type. */
  621. if (is_linux) {
  622. if (hdr->ih_os == IH_OS_LINUX) {
  623. *is_linux = 1;
  624. } else if (hdr->ih_os == IH_OS_VXWORKS) {
  625. /*
  626. * VxWorks 7 uses the same boot interface as the Linux kernel
  627. * on Arm (64-bit only), PowerPC and RISC-V architectures.
  628. */
  629. switch (hdr->ih_arch) {
  630. case IH_ARCH_ARM64:
  631. case IH_ARCH_PPC:
  632. case IH_ARCH_RISCV:
  633. *is_linux = 1;
  634. break;
  635. default:
  636. *is_linux = 0;
  637. break;
  638. }
  639. } else {
  640. *is_linux = 0;
  641. }
  642. }
  643. break;
  644. case IH_TYPE_RAMDISK:
  645. address = *loadaddr;
  646. break;
  647. default:
  648. fprintf(stderr, "Unsupported u-boot image type %d\n", hdr->ih_type);
  649. goto out;
  650. }
  651. data = g_malloc(hdr->ih_size);
  652. if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
  653. fprintf(stderr, "Error reading file\n");
  654. goto out;
  655. }
  656. if (do_uncompress) {
  657. uint8_t *compressed_data;
  658. size_t max_bytes;
  659. ssize_t bytes;
  660. compressed_data = data;
  661. max_bytes = UBOOT_MAX_GUNZIP_BYTES;
  662. data = g_malloc(max_bytes);
  663. bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
  664. g_free(compressed_data);
  665. if (bytes < 0) {
  666. fprintf(stderr, "Unable to decompress gzipped image!\n");
  667. goto out;
  668. }
  669. hdr->ih_size = bytes;
  670. }
  671. rom_add_blob_fixed_as(filename, data, hdr->ih_size, address, as);
  672. ret = hdr->ih_size;
  673. out:
  674. g_free(data);
  675. close(fd);
  676. return ret;
  677. }
  678. ssize_t load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  679. int *is_linux,
  680. uint64_t (*translate_fn)(void *, uint64_t),
  681. void *translate_opaque)
  682. {
  683. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  684. translate_fn, translate_opaque, NULL);
  685. }
  686. ssize_t load_uimage_as(const char *filename, hwaddr *ep, hwaddr *loadaddr,
  687. int *is_linux,
  688. uint64_t (*translate_fn)(void *, uint64_t),
  689. void *translate_opaque, AddressSpace *as)
  690. {
  691. return load_uboot_image(filename, ep, loadaddr, is_linux, IH_TYPE_KERNEL,
  692. translate_fn, translate_opaque, as);
  693. }
  694. /* Load a ramdisk. */
  695. ssize_t load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz)
  696. {
  697. return load_ramdisk_as(filename, addr, max_sz, NULL);
  698. }
  699. ssize_t load_ramdisk_as(const char *filename, hwaddr addr, uint64_t max_sz,
  700. AddressSpace *as)
  701. {
  702. return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK,
  703. NULL, NULL, as);
  704. }
  705. /* Load a gzip-compressed kernel to a dynamically allocated buffer. */
  706. ssize_t load_image_gzipped_buffer(const char *filename, uint64_t max_sz,
  707. uint8_t **buffer)
  708. {
  709. uint8_t *compressed_data = NULL;
  710. uint8_t *data = NULL;
  711. gsize len;
  712. ssize_t bytes;
  713. int ret = -1;
  714. if (!g_file_get_contents(filename, (char **) &compressed_data, &len,
  715. NULL)) {
  716. goto out;
  717. }
  718. /* Is it a gzip-compressed file? */
  719. if (len < 2 ||
  720. compressed_data[0] != 0x1f ||
  721. compressed_data[1] != 0x8b) {
  722. goto out;
  723. }
  724. if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) {
  725. max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES;
  726. }
  727. data = g_malloc(max_sz);
  728. bytes = gunzip(data, max_sz, compressed_data, len);
  729. if (bytes < 0) {
  730. fprintf(stderr, "%s: unable to decompress gzipped kernel file\n",
  731. filename);
  732. goto out;
  733. }
  734. /* trim to actual size and return to caller */
  735. *buffer = g_realloc(data, bytes);
  736. ret = bytes;
  737. /* ownership has been transferred to caller */
  738. data = NULL;
  739. out:
  740. g_free(compressed_data);
  741. g_free(data);
  742. return ret;
  743. }
  744. /* Load a gzip-compressed kernel. */
  745. ssize_t load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz)
  746. {
  747. ssize_t bytes;
  748. uint8_t *data;
  749. bytes = load_image_gzipped_buffer(filename, max_sz, &data);
  750. if (bytes != -1) {
  751. rom_add_blob_fixed(filename, data, bytes, addr);
  752. g_free(data);
  753. }
  754. return bytes;
  755. }
  756. /* The PE/COFF MS-DOS stub magic number */
  757. #define EFI_PE_MSDOS_MAGIC "MZ"
  758. /*
  759. * The Linux header magic number for a EFI PE/COFF
  760. * image targeting an unspecified architecture.
  761. */
  762. #define EFI_PE_LINUX_MAGIC "\xcd\x23\x82\x81"
  763. /*
  764. * Bootable Linux kernel images may be packaged as EFI zboot images, which are
  765. * self-decompressing executables when loaded via EFI. The compressed payload
  766. * can also be extracted from the image and decompressed by a non-EFI loader.
  767. *
  768. * The de facto specification for this format is at the following URL:
  769. *
  770. * https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/firmware/efi/libstub/zboot-header.S
  771. *
  772. * This definition is based on Linux upstream commit 29636a5ce87beba.
  773. */
  774. struct linux_efi_zboot_header {
  775. uint8_t msdos_magic[2]; /* PE/COFF 'MZ' magic number */
  776. uint8_t reserved0[2];
  777. uint8_t zimg[4]; /* "zimg" for Linux EFI zboot images */
  778. uint32_t payload_offset; /* LE offset to compressed payload */
  779. uint32_t payload_size; /* LE size of the compressed payload */
  780. uint8_t reserved1[8];
  781. char compression_type[32]; /* Compression type, NUL terminated */
  782. uint8_t linux_magic[4]; /* Linux header magic */
  783. uint32_t pe_header_offset; /* LE offset to the PE header */
  784. };
  785. /*
  786. * Check whether *buffer points to a Linux EFI zboot image in memory.
  787. *
  788. * If it does, attempt to decompress it to a new buffer, and free the old one.
  789. * If any of this fails, return an error to the caller.
  790. *
  791. * If the image is not a Linux EFI zboot image, do nothing and return success.
  792. */
  793. ssize_t unpack_efi_zboot_image(uint8_t **buffer, int *size)
  794. {
  795. const struct linux_efi_zboot_header *header;
  796. uint8_t *data = NULL;
  797. int ploff, plsize;
  798. ssize_t bytes;
  799. /* ignore if this is too small to be a EFI zboot image */
  800. if (*size < sizeof(*header)) {
  801. return 0;
  802. }
  803. header = (struct linux_efi_zboot_header *)*buffer;
  804. /* ignore if this is not a Linux EFI zboot image */
  805. if (memcmp(&header->msdos_magic, EFI_PE_MSDOS_MAGIC, 2) != 0 ||
  806. memcmp(&header->zimg, "zimg", 4) != 0 ||
  807. memcmp(&header->linux_magic, EFI_PE_LINUX_MAGIC, 4) != 0) {
  808. return 0;
  809. }
  810. if (strcmp(header->compression_type, "gzip") != 0) {
  811. fprintf(stderr,
  812. "unable to handle EFI zboot image with \"%.*s\" compression\n",
  813. (int)sizeof(header->compression_type) - 1,
  814. header->compression_type);
  815. return -1;
  816. }
  817. ploff = ldl_le_p(&header->payload_offset);
  818. plsize = ldl_le_p(&header->payload_size);
  819. if (ploff < 0 || plsize < 0 || ploff + plsize > *size) {
  820. fprintf(stderr, "unable to handle corrupt EFI zboot image\n");
  821. return -1;
  822. }
  823. data = g_malloc(LOAD_IMAGE_MAX_GUNZIP_BYTES);
  824. bytes = gunzip(data, LOAD_IMAGE_MAX_GUNZIP_BYTES, *buffer + ploff, plsize);
  825. if (bytes < 0) {
  826. fprintf(stderr, "failed to decompress EFI zboot image\n");
  827. g_free(data);
  828. return -1;
  829. }
  830. g_free(*buffer);
  831. *buffer = g_realloc(data, bytes);
  832. *size = bytes;
  833. return bytes;
  834. }
  835. /*
  836. * Functions for reboot-persistent memory regions.
  837. * - used for vga bios and option roms.
  838. * - also linux kernel (-kernel / -initrd).
  839. */
  840. typedef struct Rom Rom;
  841. struct Rom {
  842. char *name;
  843. char *path;
  844. /* datasize is the amount of memory allocated in "data". If datasize is less
  845. * than romsize, it means that the area from datasize to romsize is filled
  846. * with zeros.
  847. */
  848. size_t romsize;
  849. size_t datasize;
  850. uint8_t *data;
  851. MemoryRegion *mr;
  852. AddressSpace *as;
  853. int isrom;
  854. char *fw_dir;
  855. char *fw_file;
  856. GMappedFile *mapped_file;
  857. bool committed;
  858. hwaddr addr;
  859. QTAILQ_ENTRY(Rom) next;
  860. };
  861. static FWCfgState *fw_cfg;
  862. static QTAILQ_HEAD(, Rom) roms = QTAILQ_HEAD_INITIALIZER(roms);
  863. /*
  864. * rom->data can be heap-allocated or memory-mapped (e.g. when added with
  865. * rom_add_elf_program())
  866. */
  867. static void rom_free_data(Rom *rom)
  868. {
  869. if (rom->mapped_file) {
  870. g_mapped_file_unref(rom->mapped_file);
  871. rom->mapped_file = NULL;
  872. } else {
  873. g_free(rom->data);
  874. }
  875. rom->data = NULL;
  876. }
  877. static void rom_free(Rom *rom)
  878. {
  879. rom_free_data(rom);
  880. g_free(rom->path);
  881. g_free(rom->name);
  882. g_free(rom->fw_dir);
  883. g_free(rom->fw_file);
  884. g_free(rom);
  885. }
  886. static inline bool rom_order_compare(Rom *rom, Rom *item)
  887. {
  888. return ((uintptr_t)(void *)rom->as > (uintptr_t)(void *)item->as) ||
  889. (rom->as == item->as && rom->addr >= item->addr);
  890. }
  891. static void rom_insert(Rom *rom)
  892. {
  893. Rom *item;
  894. if (roms_loaded) {
  895. hw_error ("ROM images must be loaded at startup\n");
  896. }
  897. /* The user didn't specify an address space, this is the default */
  898. if (!rom->as) {
  899. rom->as = &address_space_memory;
  900. }
  901. rom->committed = false;
  902. /* List is ordered by load address in the same address space */
  903. QTAILQ_FOREACH(item, &roms, next) {
  904. if (rom_order_compare(rom, item)) {
  905. continue;
  906. }
  907. QTAILQ_INSERT_BEFORE(item, rom, next);
  908. return;
  909. }
  910. QTAILQ_INSERT_TAIL(&roms, rom, next);
  911. }
  912. static void fw_cfg_resized(const char *id, uint64_t length, void *host)
  913. {
  914. if (fw_cfg) {
  915. fw_cfg_modify_file(fw_cfg, id + strlen("/rom@"), host, length);
  916. }
  917. }
  918. static void *rom_set_mr(Rom *rom, Object *owner, const char *name, bool ro)
  919. {
  920. void *data;
  921. rom->mr = g_malloc(sizeof(*rom->mr));
  922. memory_region_init_resizeable_ram(rom->mr, owner, name,
  923. rom->datasize, rom->romsize,
  924. fw_cfg_resized,
  925. &error_fatal);
  926. memory_region_set_readonly(rom->mr, ro);
  927. vmstate_register_ram_global(rom->mr);
  928. data = memory_region_get_ram_ptr(rom->mr);
  929. memcpy(data, rom->data, rom->datasize);
  930. return data;
  931. }
  932. ssize_t rom_add_file(const char *file, const char *fw_dir,
  933. hwaddr addr, int32_t bootindex,
  934. bool option_rom, MemoryRegion *mr,
  935. AddressSpace *as)
  936. {
  937. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  938. Rom *rom;
  939. ssize_t rc;
  940. int fd = -1;
  941. char devpath[100];
  942. if (as && mr) {
  943. fprintf(stderr, "Specifying an Address Space and Memory Region is " \
  944. "not valid when loading a rom\n");
  945. /* We haven't allocated anything so we don't need any cleanup */
  946. return -1;
  947. }
  948. rom = g_malloc0(sizeof(*rom));
  949. rom->name = g_strdup(file);
  950. rom->path = qemu_find_file(QEMU_FILE_TYPE_BIOS, rom->name);
  951. rom->as = as;
  952. if (rom->path == NULL) {
  953. rom->path = g_strdup(file);
  954. }
  955. fd = open(rom->path, O_RDONLY | O_BINARY);
  956. if (fd == -1) {
  957. fprintf(stderr, "Could not open option rom '%s': %s\n",
  958. rom->path, strerror(errno));
  959. goto err;
  960. }
  961. if (fw_dir) {
  962. rom->fw_dir = g_strdup(fw_dir);
  963. rom->fw_file = g_strdup(file);
  964. }
  965. rom->addr = addr;
  966. rom->romsize = lseek(fd, 0, SEEK_END);
  967. if (rom->romsize == -1) {
  968. fprintf(stderr, "rom: file %-20s: get size error: %s\n",
  969. rom->name, strerror(errno));
  970. goto err;
  971. }
  972. rom->datasize = rom->romsize;
  973. rom->data = g_malloc0(rom->datasize);
  974. lseek(fd, 0, SEEK_SET);
  975. rc = read(fd, rom->data, rom->datasize);
  976. if (rc != rom->datasize) {
  977. fprintf(stderr, "rom: file %-20s: read error: rc=%zd (expected %zd)\n",
  978. rom->name, rc, rom->datasize);
  979. goto err;
  980. }
  981. close(fd);
  982. rom_insert(rom);
  983. if (rom->fw_file && fw_cfg) {
  984. const char *basename;
  985. char fw_file_name[FW_CFG_MAX_FILE_PATH];
  986. void *data;
  987. basename = strrchr(rom->fw_file, '/');
  988. if (basename) {
  989. basename++;
  990. } else {
  991. basename = rom->fw_file;
  992. }
  993. snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
  994. basename);
  995. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  996. if ((!option_rom || mc->option_rom_has_mr) && mc->rom_file_has_mr) {
  997. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, true);
  998. } else {
  999. data = rom->data;
  1000. }
  1001. fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
  1002. } else {
  1003. if (mr) {
  1004. rom->mr = mr;
  1005. snprintf(devpath, sizeof(devpath), "/rom@%s", file);
  1006. } else {
  1007. snprintf(devpath, sizeof(devpath), "/rom@" HWADDR_FMT_plx, addr);
  1008. }
  1009. }
  1010. add_boot_device_path(bootindex, NULL, devpath);
  1011. return 0;
  1012. err:
  1013. if (fd != -1)
  1014. close(fd);
  1015. rom_free(rom);
  1016. return -1;
  1017. }
  1018. MemoryRegion *rom_add_blob(const char *name, const void *blob, size_t len,
  1019. size_t max_len, hwaddr addr, const char *fw_file_name,
  1020. FWCfgCallback fw_callback, void *callback_opaque,
  1021. AddressSpace *as, bool read_only)
  1022. {
  1023. MachineClass *mc = MACHINE_GET_CLASS(qdev_get_machine());
  1024. Rom *rom;
  1025. MemoryRegion *mr = NULL;
  1026. rom = g_malloc0(sizeof(*rom));
  1027. rom->name = g_strdup(name);
  1028. rom->as = as;
  1029. rom->addr = addr;
  1030. rom->romsize = max_len ? max_len : len;
  1031. rom->datasize = len;
  1032. g_assert(rom->romsize >= rom->datasize);
  1033. rom->data = g_malloc0(rom->datasize);
  1034. memcpy(rom->data, blob, len);
  1035. rom_insert(rom);
  1036. if (fw_file_name && fw_cfg) {
  1037. char devpath[100];
  1038. void *data;
  1039. if (read_only) {
  1040. snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
  1041. } else {
  1042. snprintf(devpath, sizeof(devpath), "/ram@%s", fw_file_name);
  1043. }
  1044. if (mc->rom_file_has_mr) {
  1045. data = rom_set_mr(rom, OBJECT(fw_cfg), devpath, read_only);
  1046. mr = rom->mr;
  1047. } else {
  1048. data = rom->data;
  1049. }
  1050. fw_cfg_add_file_callback(fw_cfg, fw_file_name,
  1051. fw_callback, NULL, callback_opaque,
  1052. data, rom->datasize, read_only);
  1053. }
  1054. return mr;
  1055. }
  1056. /* This function is specific for elf program because we don't need to allocate
  1057. * all the rom. We just allocate the first part and the rest is just zeros. This
  1058. * is why romsize and datasize are different. Also, this function takes its own
  1059. * reference to "mapped_file", so we don't have to allocate and copy the buffer.
  1060. */
  1061. int rom_add_elf_program(const char *name, GMappedFile *mapped_file, void *data,
  1062. size_t datasize, size_t romsize, hwaddr addr,
  1063. AddressSpace *as)
  1064. {
  1065. Rom *rom;
  1066. rom = g_malloc0(sizeof(*rom));
  1067. rom->name = g_strdup(name);
  1068. rom->addr = addr;
  1069. rom->datasize = datasize;
  1070. rom->romsize = romsize;
  1071. rom->data = data;
  1072. rom->as = as;
  1073. if (mapped_file && data) {
  1074. g_mapped_file_ref(mapped_file);
  1075. rom->mapped_file = mapped_file;
  1076. }
  1077. rom_insert(rom);
  1078. return 0;
  1079. }
  1080. ssize_t rom_add_vga(const char *file)
  1081. {
  1082. return rom_add_file(file, "vgaroms", 0, -1, true, NULL, NULL);
  1083. }
  1084. ssize_t rom_add_option(const char *file, int32_t bootindex)
  1085. {
  1086. return rom_add_file(file, "genroms", 0, bootindex, true, NULL, NULL);
  1087. }
  1088. static void rom_reset(void *unused)
  1089. {
  1090. Rom *rom;
  1091. QTAILQ_FOREACH(rom, &roms, next) {
  1092. if (rom->fw_file) {
  1093. continue;
  1094. }
  1095. /*
  1096. * We don't need to fill in the RAM with ROM data because we'll fill
  1097. * the data in during the next incoming migration in all cases. Note
  1098. * that some of those RAMs can actually be modified by the guest.
  1099. */
  1100. if (runstate_check(RUN_STATE_INMIGRATE)) {
  1101. if (rom->data && rom->isrom) {
  1102. /*
  1103. * Free it so that a rom_reset after migration doesn't
  1104. * overwrite a potentially modified 'rom'.
  1105. */
  1106. rom_free_data(rom);
  1107. }
  1108. continue;
  1109. }
  1110. if (rom->data == NULL) {
  1111. continue;
  1112. }
  1113. if (rom->mr) {
  1114. void *host = memory_region_get_ram_ptr(rom->mr);
  1115. memcpy(host, rom->data, rom->datasize);
  1116. memset(host + rom->datasize, 0, rom->romsize - rom->datasize);
  1117. } else {
  1118. address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
  1119. rom->data, rom->datasize);
  1120. address_space_set(rom->as, rom->addr + rom->datasize, 0,
  1121. rom->romsize - rom->datasize,
  1122. MEMTXATTRS_UNSPECIFIED);
  1123. }
  1124. if (rom->isrom) {
  1125. /* rom needs to be written only once */
  1126. rom_free_data(rom);
  1127. }
  1128. /*
  1129. * The rom loader is really on the same level as firmware in the guest
  1130. * shadowing a ROM into RAM. Such a shadowing mechanism needs to ensure
  1131. * that the instruction cache for that new region is clear, so that the
  1132. * CPU definitely fetches its instructions from the just written data.
  1133. */
  1134. cpu_flush_icache_range(rom->addr, rom->datasize);
  1135. trace_loader_write_rom(rom->name, rom->addr, rom->datasize, rom->isrom);
  1136. }
  1137. }
  1138. /* Return true if two consecutive ROMs in the ROM list overlap */
  1139. static bool roms_overlap(Rom *last_rom, Rom *this_rom)
  1140. {
  1141. if (!last_rom) {
  1142. return false;
  1143. }
  1144. return last_rom->as == this_rom->as &&
  1145. last_rom->addr + last_rom->romsize > this_rom->addr;
  1146. }
  1147. static const char *rom_as_name(Rom *rom)
  1148. {
  1149. const char *name = rom->as ? rom->as->name : NULL;
  1150. return name ?: "anonymous";
  1151. }
  1152. static void rom_print_overlap_error_header(void)
  1153. {
  1154. error_report("Some ROM regions are overlapping");
  1155. error_printf(
  1156. "These ROM regions might have been loaded by "
  1157. "direct user request or by default.\n"
  1158. "They could be BIOS/firmware images, a guest kernel, "
  1159. "initrd or some other file loaded into guest memory.\n"
  1160. "Check whether you intended to load all this guest code, and "
  1161. "whether it has been built to load to the correct addresses.\n");
  1162. }
  1163. static void rom_print_one_overlap_error(Rom *last_rom, Rom *rom)
  1164. {
  1165. error_printf(
  1166. "\nThe following two regions overlap (in the %s address space):\n",
  1167. rom_as_name(rom));
  1168. error_printf(
  1169. " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
  1170. last_rom->name, last_rom->addr, last_rom->addr + last_rom->romsize);
  1171. error_printf(
  1172. " %s (addresses 0x" HWADDR_FMT_plx " - 0x" HWADDR_FMT_plx ")\n",
  1173. rom->name, rom->addr, rom->addr + rom->romsize);
  1174. }
  1175. int rom_check_and_register_reset(void)
  1176. {
  1177. MemoryRegionSection section;
  1178. Rom *rom, *last_rom = NULL;
  1179. bool found_overlap = false;
  1180. QTAILQ_FOREACH(rom, &roms, next) {
  1181. if (rom->fw_file) {
  1182. continue;
  1183. }
  1184. if (!rom->mr) {
  1185. if (roms_overlap(last_rom, rom)) {
  1186. if (!found_overlap) {
  1187. found_overlap = true;
  1188. rom_print_overlap_error_header();
  1189. }
  1190. rom_print_one_overlap_error(last_rom, rom);
  1191. /* Keep going through the list so we report all overlaps */
  1192. }
  1193. last_rom = rom;
  1194. }
  1195. section = memory_region_find(rom->mr ? rom->mr : get_system_memory(),
  1196. rom->addr, 1);
  1197. rom->isrom = int128_nz(section.size) && memory_region_is_rom(section.mr);
  1198. memory_region_unref(section.mr);
  1199. }
  1200. if (found_overlap) {
  1201. return -1;
  1202. }
  1203. qemu_register_reset(rom_reset, NULL);
  1204. roms_loaded = 1;
  1205. return 0;
  1206. }
  1207. void rom_set_fw(FWCfgState *f)
  1208. {
  1209. fw_cfg = f;
  1210. }
  1211. void rom_set_order_override(int order)
  1212. {
  1213. if (!fw_cfg)
  1214. return;
  1215. fw_cfg_set_order_override(fw_cfg, order);
  1216. }
  1217. void rom_reset_order_override(void)
  1218. {
  1219. if (!fw_cfg)
  1220. return;
  1221. fw_cfg_reset_order_override(fw_cfg);
  1222. }
  1223. void rom_transaction_begin(void)
  1224. {
  1225. Rom *rom;
  1226. /* Ignore ROMs added without the transaction API */
  1227. QTAILQ_FOREACH(rom, &roms, next) {
  1228. rom->committed = true;
  1229. }
  1230. }
  1231. void rom_transaction_end(bool commit)
  1232. {
  1233. Rom *rom;
  1234. Rom *tmp;
  1235. QTAILQ_FOREACH_SAFE(rom, &roms, next, tmp) {
  1236. if (rom->committed) {
  1237. continue;
  1238. }
  1239. if (commit) {
  1240. rom->committed = true;
  1241. } else {
  1242. QTAILQ_REMOVE(&roms, rom, next);
  1243. rom_free(rom);
  1244. }
  1245. }
  1246. }
  1247. static Rom *find_rom(hwaddr addr, size_t size)
  1248. {
  1249. Rom *rom;
  1250. QTAILQ_FOREACH(rom, &roms, next) {
  1251. if (rom->fw_file) {
  1252. continue;
  1253. }
  1254. if (rom->mr) {
  1255. continue;
  1256. }
  1257. if (rom->addr > addr) {
  1258. continue;
  1259. }
  1260. if (rom->addr + rom->romsize < addr + size) {
  1261. continue;
  1262. }
  1263. return rom;
  1264. }
  1265. return NULL;
  1266. }
  1267. typedef struct RomSec {
  1268. hwaddr base;
  1269. int se; /* start/end flag */
  1270. } RomSec;
  1271. /*
  1272. * Sort into address order. We break ties between rom-startpoints
  1273. * and rom-endpoints in favour of the startpoint, by sorting the 0->1
  1274. * transition before the 1->0 transition. Either way round would
  1275. * work, but this way saves a little work later by avoiding
  1276. * dealing with "gaps" of 0 length.
  1277. */
  1278. static gint sort_secs(gconstpointer a, gconstpointer b)
  1279. {
  1280. RomSec *ra = (RomSec *) a;
  1281. RomSec *rb = (RomSec *) b;
  1282. if (ra->base == rb->base) {
  1283. return ra->se - rb->se;
  1284. }
  1285. return ra->base > rb->base ? 1 : -1;
  1286. }
  1287. static GList *add_romsec_to_list(GList *secs, hwaddr base, int se)
  1288. {
  1289. RomSec *cand = g_new(RomSec, 1);
  1290. cand->base = base;
  1291. cand->se = se;
  1292. return g_list_prepend(secs, cand);
  1293. }
  1294. RomGap rom_find_largest_gap_between(hwaddr base, size_t size)
  1295. {
  1296. Rom *rom;
  1297. RomSec *cand;
  1298. RomGap res = {0, 0};
  1299. hwaddr gapstart = base;
  1300. GList *it, *secs = NULL;
  1301. int count = 0;
  1302. QTAILQ_FOREACH(rom, &roms, next) {
  1303. /* Ignore blobs being loaded to special places */
  1304. if (rom->mr || rom->fw_file) {
  1305. continue;
  1306. }
  1307. /* ignore anything finishing below base */
  1308. if (rom->addr + rom->romsize <= base) {
  1309. continue;
  1310. }
  1311. /* ignore anything starting above the region */
  1312. if (rom->addr >= base + size) {
  1313. continue;
  1314. }
  1315. /* Save the start and end of each relevant ROM */
  1316. secs = add_romsec_to_list(secs, rom->addr, 1);
  1317. if (rom->addr + rom->romsize < base + size) {
  1318. secs = add_romsec_to_list(secs, rom->addr + rom->romsize, -1);
  1319. }
  1320. }
  1321. /* sentinel */
  1322. secs = add_romsec_to_list(secs, base + size, 1);
  1323. secs = g_list_sort(secs, sort_secs);
  1324. for (it = g_list_first(secs); it; it = g_list_next(it)) {
  1325. cand = (RomSec *) it->data;
  1326. if (count == 0 && count + cand->se == 1) {
  1327. size_t gap = cand->base - gapstart;
  1328. if (gap > res.size) {
  1329. res.base = gapstart;
  1330. res.size = gap;
  1331. }
  1332. } else if (count == 1 && count + cand->se == 0) {
  1333. gapstart = cand->base;
  1334. }
  1335. count += cand->se;
  1336. }
  1337. g_list_free_full(secs, g_free);
  1338. return res;
  1339. }
  1340. /*
  1341. * Copies memory from registered ROMs to dest. Any memory that is contained in
  1342. * a ROM between addr and addr + size is copied. Note that this can involve
  1343. * multiple ROMs, which need not start at addr and need not end at addr + size.
  1344. */
  1345. int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
  1346. {
  1347. hwaddr end = addr + size;
  1348. uint8_t *s, *d = dest;
  1349. size_t l = 0;
  1350. Rom *rom;
  1351. QTAILQ_FOREACH(rom, &roms, next) {
  1352. if (rom->fw_file) {
  1353. continue;
  1354. }
  1355. if (rom->mr) {
  1356. continue;
  1357. }
  1358. if (rom->addr + rom->romsize < addr) {
  1359. continue;
  1360. }
  1361. if (rom->addr > end || rom->addr < addr) {
  1362. break;
  1363. }
  1364. d = dest + (rom->addr - addr);
  1365. s = rom->data;
  1366. l = rom->datasize;
  1367. if ((d + l) > (dest + size)) {
  1368. l = dest - d;
  1369. }
  1370. if (l > 0) {
  1371. memcpy(d, s, l);
  1372. }
  1373. if (rom->romsize > rom->datasize) {
  1374. /* If datasize is less than romsize, it means that we didn't
  1375. * allocate all the ROM because the trailing data are only zeros.
  1376. */
  1377. d += l;
  1378. l = rom->romsize - rom->datasize;
  1379. if ((d + l) > (dest + size)) {
  1380. /* Rom size doesn't fit in the destination area. Adjust to avoid
  1381. * overflow.
  1382. */
  1383. l = dest - d;
  1384. }
  1385. if (l > 0) {
  1386. memset(d, 0x0, l);
  1387. }
  1388. }
  1389. }
  1390. return (d + l) - dest;
  1391. }
  1392. void *rom_ptr(hwaddr addr, size_t size)
  1393. {
  1394. Rom *rom;
  1395. rom = find_rom(addr, size);
  1396. if (!rom || !rom->data)
  1397. return NULL;
  1398. return rom->data + (addr - rom->addr);
  1399. }
  1400. typedef struct FindRomCBData {
  1401. size_t size; /* Amount of data we want from ROM, in bytes */
  1402. MemoryRegion *mr; /* MR at the unaliased guest addr */
  1403. hwaddr xlat; /* Offset of addr within mr */
  1404. void *rom; /* Output: rom data pointer, if found */
  1405. } FindRomCBData;
  1406. static bool find_rom_cb(Int128 start, Int128 len, const MemoryRegion *mr,
  1407. hwaddr offset_in_region, void *opaque)
  1408. {
  1409. FindRomCBData *cbdata = opaque;
  1410. hwaddr alias_addr;
  1411. if (mr != cbdata->mr) {
  1412. return false;
  1413. }
  1414. alias_addr = int128_get64(start) + cbdata->xlat - offset_in_region;
  1415. cbdata->rom = rom_ptr(alias_addr, cbdata->size);
  1416. if (!cbdata->rom) {
  1417. return false;
  1418. }
  1419. /* Found a match, stop iterating */
  1420. return true;
  1421. }
  1422. void *rom_ptr_for_as(AddressSpace *as, hwaddr addr, size_t size)
  1423. {
  1424. /*
  1425. * Find any ROM data for the given guest address range. If there
  1426. * is a ROM blob then return a pointer to the host memory
  1427. * corresponding to 'addr'; otherwise return NULL.
  1428. *
  1429. * We look not only for ROM blobs that were loaded directly to
  1430. * addr, but also for ROM blobs that were loaded to aliases of
  1431. * that memory at other addresses within the AddressSpace.
  1432. *
  1433. * Note that we do not check @as against the 'as' member in the
  1434. * 'struct Rom' returned by rom_ptr(). The Rom::as is the
  1435. * AddressSpace which the rom blob should be written to, whereas
  1436. * our @as argument is the AddressSpace which we are (effectively)
  1437. * reading from, and the same underlying RAM will often be visible
  1438. * in multiple AddressSpaces. (A common example is a ROM blob
  1439. * written to the 'system' address space but then read back via a
  1440. * CPU's cpu->as pointer.) This does mean we might potentially
  1441. * return a false-positive match if a ROM blob was loaded into an
  1442. * AS which is entirely separate and distinct from the one we're
  1443. * querying, but this issue exists also for rom_ptr() and hasn't
  1444. * caused any problems in practice.
  1445. */
  1446. FlatView *fv;
  1447. void *rom;
  1448. hwaddr len_unused;
  1449. FindRomCBData cbdata = {};
  1450. /* Easy case: there's data at the actual address */
  1451. rom = rom_ptr(addr, size);
  1452. if (rom) {
  1453. return rom;
  1454. }
  1455. RCU_READ_LOCK_GUARD();
  1456. fv = address_space_to_flatview(as);
  1457. cbdata.mr = flatview_translate(fv, addr, &cbdata.xlat, &len_unused,
  1458. false, MEMTXATTRS_UNSPECIFIED);
  1459. if (!cbdata.mr) {
  1460. /* Nothing at this address, so there can't be any aliasing */
  1461. return NULL;
  1462. }
  1463. cbdata.size = size;
  1464. flatview_for_each_range(fv, find_rom_cb, &cbdata);
  1465. return cbdata.rom;
  1466. }
  1467. HumanReadableText *qmp_x_query_roms(Error **errp)
  1468. {
  1469. Rom *rom;
  1470. g_autoptr(GString) buf = g_string_new("");
  1471. QTAILQ_FOREACH(rom, &roms, next) {
  1472. if (rom->mr) {
  1473. g_string_append_printf(buf, "%s"
  1474. " size=0x%06zx name=\"%s\"\n",
  1475. memory_region_name(rom->mr),
  1476. rom->romsize,
  1477. rom->name);
  1478. } else if (!rom->fw_file) {
  1479. g_string_append_printf(buf, "addr=" HWADDR_FMT_plx
  1480. " size=0x%06zx mem=%s name=\"%s\"\n",
  1481. rom->addr, rom->romsize,
  1482. rom->isrom ? "rom" : "ram",
  1483. rom->name);
  1484. } else {
  1485. g_string_append_printf(buf, "fw=%s/%s"
  1486. " size=0x%06zx name=\"%s\"\n",
  1487. rom->fw_dir,
  1488. rom->fw_file,
  1489. rom->romsize,
  1490. rom->name);
  1491. }
  1492. }
  1493. return human_readable_text_from_str(buf);
  1494. }
  1495. typedef enum HexRecord HexRecord;
  1496. enum HexRecord {
  1497. DATA_RECORD = 0,
  1498. EOF_RECORD,
  1499. EXT_SEG_ADDR_RECORD,
  1500. START_SEG_ADDR_RECORD,
  1501. EXT_LINEAR_ADDR_RECORD,
  1502. START_LINEAR_ADDR_RECORD,
  1503. };
  1504. /* Each record contains a 16-bit address which is combined with the upper 16
  1505. * bits of the implicit "next address" to form a 32-bit address.
  1506. */
  1507. #define NEXT_ADDR_MASK 0xffff0000
  1508. #define DATA_FIELD_MAX_LEN 0xff
  1509. #define LEN_EXCEPT_DATA 0x5
  1510. /* 0x5 = sizeof(byte_count) + sizeof(address) + sizeof(record_type) +
  1511. * sizeof(checksum) */
  1512. typedef struct {
  1513. uint8_t byte_count;
  1514. uint16_t address;
  1515. uint8_t record_type;
  1516. uint8_t data[DATA_FIELD_MAX_LEN];
  1517. uint8_t checksum;
  1518. } HexLine;
  1519. /* return 0 or -1 if error */
  1520. static bool parse_record(HexLine *line, uint8_t *our_checksum, const uint8_t c,
  1521. uint32_t *index, const bool in_process)
  1522. {
  1523. /* +-------+---------------+-------+---------------------+--------+
  1524. * | byte | |record | | |
  1525. * | count | address | type | data |checksum|
  1526. * +-------+---------------+-------+---------------------+--------+
  1527. * ^ ^ ^ ^ ^ ^
  1528. * |1 byte | 2 bytes |1 byte | 0-255 bytes | 1 byte |
  1529. */
  1530. uint8_t value = 0;
  1531. uint32_t idx = *index;
  1532. /* ignore space */
  1533. if (g_ascii_isspace(c)) {
  1534. return true;
  1535. }
  1536. if (!g_ascii_isxdigit(c) || !in_process) {
  1537. return false;
  1538. }
  1539. value = g_ascii_xdigit_value(c);
  1540. value = (idx & 0x1) ? (value & 0xf) : (value << 4);
  1541. if (idx < 2) {
  1542. line->byte_count |= value;
  1543. } else if (2 <= idx && idx < 6) {
  1544. line->address <<= 4;
  1545. line->address += g_ascii_xdigit_value(c);
  1546. } else if (6 <= idx && idx < 8) {
  1547. line->record_type |= value;
  1548. } else if (8 <= idx && idx < 8 + 2 * line->byte_count) {
  1549. line->data[(idx - 8) >> 1] |= value;
  1550. } else if (8 + 2 * line->byte_count <= idx &&
  1551. idx < 10 + 2 * line->byte_count) {
  1552. line->checksum |= value;
  1553. } else {
  1554. return false;
  1555. }
  1556. *our_checksum += value;
  1557. ++(*index);
  1558. return true;
  1559. }
  1560. typedef struct {
  1561. const char *filename;
  1562. HexLine line;
  1563. uint8_t *bin_buf;
  1564. hwaddr *start_addr;
  1565. int total_size;
  1566. uint32_t next_address_to_write;
  1567. uint32_t current_address;
  1568. uint32_t current_rom_index;
  1569. uint32_t rom_start_address;
  1570. AddressSpace *as;
  1571. bool complete;
  1572. } HexParser;
  1573. /* return size or -1 if error */
  1574. static int handle_record_type(HexParser *parser)
  1575. {
  1576. HexLine *line = &(parser->line);
  1577. switch (line->record_type) {
  1578. case DATA_RECORD:
  1579. parser->current_address =
  1580. (parser->next_address_to_write & NEXT_ADDR_MASK) | line->address;
  1581. /* verify this is a contiguous block of memory */
  1582. if (parser->current_address != parser->next_address_to_write) {
  1583. if (parser->current_rom_index != 0) {
  1584. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1585. parser->current_rom_index,
  1586. parser->rom_start_address, parser->as);
  1587. }
  1588. parser->rom_start_address = parser->current_address;
  1589. parser->current_rom_index = 0;
  1590. }
  1591. /* copy from line buffer to output bin_buf */
  1592. memcpy(parser->bin_buf + parser->current_rom_index, line->data,
  1593. line->byte_count);
  1594. parser->current_rom_index += line->byte_count;
  1595. parser->total_size += line->byte_count;
  1596. /* save next address to write */
  1597. parser->next_address_to_write =
  1598. parser->current_address + line->byte_count;
  1599. break;
  1600. case EOF_RECORD:
  1601. if (parser->current_rom_index != 0) {
  1602. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1603. parser->current_rom_index,
  1604. parser->rom_start_address, parser->as);
  1605. }
  1606. parser->complete = true;
  1607. return parser->total_size;
  1608. case EXT_SEG_ADDR_RECORD:
  1609. case EXT_LINEAR_ADDR_RECORD:
  1610. if (line->byte_count != 2 && line->address != 0) {
  1611. return -1;
  1612. }
  1613. if (parser->current_rom_index != 0) {
  1614. rom_add_blob_fixed_as(parser->filename, parser->bin_buf,
  1615. parser->current_rom_index,
  1616. parser->rom_start_address, parser->as);
  1617. }
  1618. /* save next address to write,
  1619. * in case of non-contiguous block of memory */
  1620. parser->next_address_to_write = (line->data[0] << 12) |
  1621. (line->data[1] << 4);
  1622. if (line->record_type == EXT_LINEAR_ADDR_RECORD) {
  1623. parser->next_address_to_write <<= 12;
  1624. }
  1625. parser->rom_start_address = parser->next_address_to_write;
  1626. parser->current_rom_index = 0;
  1627. break;
  1628. case START_SEG_ADDR_RECORD:
  1629. if (line->byte_count != 4 && line->address != 0) {
  1630. return -1;
  1631. }
  1632. /* x86 16-bit CS:IP segmented addressing */
  1633. *(parser->start_addr) = (((line->data[0] << 8) | line->data[1]) << 4) +
  1634. ((line->data[2] << 8) | line->data[3]);
  1635. break;
  1636. case START_LINEAR_ADDR_RECORD:
  1637. if (line->byte_count != 4 && line->address != 0) {
  1638. return -1;
  1639. }
  1640. *(parser->start_addr) = ldl_be_p(line->data);
  1641. break;
  1642. default:
  1643. return -1;
  1644. }
  1645. return parser->total_size;
  1646. }
  1647. /* return size or -1 if error */
  1648. static int parse_hex_blob(const char *filename, hwaddr *addr, uint8_t *hex_blob,
  1649. size_t hex_blob_size, AddressSpace *as)
  1650. {
  1651. bool in_process = false; /* avoid re-enter and
  1652. * check whether record begin with ':' */
  1653. uint8_t *end = hex_blob + hex_blob_size;
  1654. uint8_t our_checksum = 0;
  1655. uint32_t record_index = 0;
  1656. HexParser parser = {
  1657. .filename = filename,
  1658. .bin_buf = g_malloc(hex_blob_size),
  1659. .start_addr = addr,
  1660. .as = as,
  1661. .complete = false
  1662. };
  1663. rom_transaction_begin();
  1664. for (; hex_blob < end && !parser.complete; ++hex_blob) {
  1665. switch (*hex_blob) {
  1666. case '\r':
  1667. case '\n':
  1668. if (!in_process) {
  1669. break;
  1670. }
  1671. in_process = false;
  1672. if ((LEN_EXCEPT_DATA + parser.line.byte_count) * 2 !=
  1673. record_index ||
  1674. our_checksum != 0) {
  1675. parser.total_size = -1;
  1676. goto out;
  1677. }
  1678. if (handle_record_type(&parser) == -1) {
  1679. parser.total_size = -1;
  1680. goto out;
  1681. }
  1682. break;
  1683. /* start of a new record. */
  1684. case ':':
  1685. memset(&parser.line, 0, sizeof(HexLine));
  1686. in_process = true;
  1687. record_index = 0;
  1688. break;
  1689. /* decoding lines */
  1690. default:
  1691. if (!parse_record(&parser.line, &our_checksum, *hex_blob,
  1692. &record_index, in_process)) {
  1693. parser.total_size = -1;
  1694. goto out;
  1695. }
  1696. break;
  1697. }
  1698. }
  1699. out:
  1700. g_free(parser.bin_buf);
  1701. rom_transaction_end(parser.total_size != -1);
  1702. return parser.total_size;
  1703. }
  1704. /* return size or -1 if error */
  1705. ssize_t load_targphys_hex_as(const char *filename, hwaddr *entry,
  1706. AddressSpace *as)
  1707. {
  1708. gsize hex_blob_size;
  1709. gchar *hex_blob;
  1710. ssize_t total_size = 0;
  1711. if (!g_file_get_contents(filename, &hex_blob, &hex_blob_size, NULL)) {
  1712. return -1;
  1713. }
  1714. total_size = parse_hex_blob(filename, entry, (uint8_t *)hex_blob,
  1715. hex_blob_size, as);
  1716. g_free(hex_blob);
  1717. return total_size;
  1718. }