loader.c 44 KB

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