image-fit.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #ifdef USE_HOSTCC
  11. #include "mkimage.h"
  12. #include <time.h>
  13. #else
  14. #include <linux/compiler.h>
  15. #include <linux/kconfig.h>
  16. #include <common.h>
  17. #include <errno.h>
  18. #include <mapmem.h>
  19. #include <asm/io.h>
  20. #include <malloc.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #endif /* !USE_HOSTCC*/
  23. #include <image.h>
  24. #include <bootstage.h>
  25. #include <u-boot/crc.h>
  26. #include <u-boot/md5.h>
  27. #include <u-boot/sha1.h>
  28. #include <u-boot/sha256.h>
  29. /*****************************************************************************/
  30. /* New uImage format routines */
  31. /*****************************************************************************/
  32. #ifndef USE_HOSTCC
  33. static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
  34. ulong *addr, const char **name)
  35. {
  36. const char *sep;
  37. *addr = addr_curr;
  38. *name = NULL;
  39. sep = strchr(spec, sepc);
  40. if (sep) {
  41. if (sep - spec > 0)
  42. *addr = simple_strtoul(spec, NULL, 16);
  43. *name = sep + 1;
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. /**
  49. * fit_parse_conf - parse FIT configuration spec
  50. * @spec: input string, containing configuration spec
  51. * @add_curr: current image address (to be used as a possible default)
  52. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  53. * configuration
  54. * @conf_name double pointer to a char, will hold pointer to a configuration
  55. * unit name
  56. *
  57. * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
  58. * where <addr> is a FIT image address that contains configuration
  59. * with a <conf> unit name.
  60. *
  61. * Address part is optional, and if omitted default add_curr will
  62. * be used instead.
  63. *
  64. * returns:
  65. * 1 if spec is a valid configuration string,
  66. * addr and conf_name are set accordingly
  67. * 0 otherwise
  68. */
  69. int fit_parse_conf(const char *spec, ulong addr_curr,
  70. ulong *addr, const char **conf_name)
  71. {
  72. return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
  73. }
  74. /**
  75. * fit_parse_subimage - parse FIT subimage spec
  76. * @spec: input string, containing subimage spec
  77. * @add_curr: current image address (to be used as a possible default)
  78. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  79. * subimage
  80. * @image_name: double pointer to a char, will hold pointer to a subimage name
  81. *
  82. * fit_parse_subimage() expects subimage spec in the form of
  83. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  84. * subimage with a <subimg> unit name.
  85. *
  86. * Address part is optional, and if omitted default add_curr will
  87. * be used instead.
  88. *
  89. * returns:
  90. * 1 if spec is a valid subimage string,
  91. * addr and image_name are set accordingly
  92. * 0 otherwise
  93. */
  94. int fit_parse_subimage(const char *spec, ulong addr_curr,
  95. ulong *addr, const char **image_name)
  96. {
  97. return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
  98. }
  99. #endif /* !USE_HOSTCC */
  100. static void fit_get_debug(const void *fit, int noffset,
  101. char *prop_name, int err)
  102. {
  103. debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
  104. prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
  105. fdt_strerror(err));
  106. }
  107. /**
  108. * fit_get_subimage_count - get component (sub-image) count
  109. * @fit: pointer to the FIT format image header
  110. * @images_noffset: offset of images node
  111. *
  112. * returns:
  113. * number of image components
  114. */
  115. int fit_get_subimage_count(const void *fit, int images_noffset)
  116. {
  117. int noffset;
  118. int ndepth;
  119. int count = 0;
  120. /* Process its subnodes, print out component images details */
  121. for (ndepth = 0, count = 0,
  122. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  123. (noffset >= 0) && (ndepth > 0);
  124. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  125. if (ndepth == 1) {
  126. count++;
  127. }
  128. }
  129. return count;
  130. }
  131. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
  132. /**
  133. * fit_image_print_data() - prints out the hash node details
  134. * @fit: pointer to the FIT format image header
  135. * @noffset: offset of the hash node
  136. * @p: pointer to prefix string
  137. * @type: Type of information to print ("hash" or "sign")
  138. *
  139. * fit_image_print_data() lists properties for the processed hash node
  140. *
  141. * This function avoid using puts() since it prints a newline on the host
  142. * but does not in U-Boot.
  143. *
  144. * returns:
  145. * no returned results
  146. */
  147. static void fit_image_print_data(const void *fit, int noffset, const char *p,
  148. const char *type)
  149. {
  150. const char *keyname;
  151. uint8_t *value;
  152. int value_len;
  153. char *algo;
  154. int required;
  155. int ret, i;
  156. debug("%s %s node: '%s'\n", p, type,
  157. fit_get_name(fit, noffset, NULL));
  158. printf("%s %s algo: ", p, type);
  159. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  160. printf("invalid/unsupported\n");
  161. return;
  162. }
  163. printf("%s", algo);
  164. keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
  165. required = fdt_getprop(fit, noffset, "required", NULL) != NULL;
  166. if (keyname)
  167. printf(":%s", keyname);
  168. if (required)
  169. printf(" (required)");
  170. printf("\n");
  171. ret = fit_image_hash_get_value(fit, noffset, &value,
  172. &value_len);
  173. printf("%s %s value: ", p, type);
  174. if (ret) {
  175. printf("unavailable\n");
  176. } else {
  177. for (i = 0; i < value_len; i++)
  178. printf("%02x", value[i]);
  179. printf("\n");
  180. }
  181. debug("%s %s len: %d\n", p, type, value_len);
  182. /* Signatures have a time stamp */
  183. if (IMAGE_ENABLE_TIMESTAMP && keyname) {
  184. time_t timestamp;
  185. printf("%s Timestamp: ", p);
  186. if (fit_get_timestamp(fit, noffset, &timestamp))
  187. printf("unavailable\n");
  188. else
  189. genimg_print_time(timestamp);
  190. }
  191. }
  192. /**
  193. * fit_image_print_verification_data() - prints out the hash/signature details
  194. * @fit: pointer to the FIT format image header
  195. * @noffset: offset of the hash or signature node
  196. * @p: pointer to prefix string
  197. *
  198. * This lists properties for the processed hash node
  199. *
  200. * returns:
  201. * no returned results
  202. */
  203. static void fit_image_print_verification_data(const void *fit, int noffset,
  204. const char *p)
  205. {
  206. const char *name;
  207. /*
  208. * Check subnode name, must be equal to "hash" or "signature".
  209. * Multiple hash/signature nodes require unique unit node
  210. * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
  211. */
  212. name = fit_get_name(fit, noffset, NULL);
  213. if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
  214. fit_image_print_data(fit, noffset, p, "Hash");
  215. } else if (!strncmp(name, FIT_SIG_NODENAME,
  216. strlen(FIT_SIG_NODENAME))) {
  217. fit_image_print_data(fit, noffset, p, "Sign");
  218. }
  219. }
  220. /**
  221. * fit_conf_print - prints out the FIT configuration details
  222. * @fit: pointer to the FIT format image header
  223. * @noffset: offset of the configuration node
  224. * @p: pointer to prefix string
  225. *
  226. * fit_conf_print() lists all mandatory properties for the processed
  227. * configuration node.
  228. *
  229. * returns:
  230. * no returned results
  231. */
  232. static void fit_conf_print(const void *fit, int noffset, const char *p)
  233. {
  234. char *desc;
  235. const char *uname;
  236. int ret;
  237. int fdt_index, loadables_index;
  238. int ndepth;
  239. /* Mandatory properties */
  240. ret = fit_get_desc(fit, noffset, &desc);
  241. printf("%s Description: ", p);
  242. if (ret)
  243. printf("unavailable\n");
  244. else
  245. printf("%s\n", desc);
  246. uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  247. printf("%s Kernel: ", p);
  248. if (!uname)
  249. printf("unavailable\n");
  250. else
  251. printf("%s\n", uname);
  252. /* Optional properties */
  253. uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  254. if (uname)
  255. printf("%s Init Ramdisk: %s\n", p, uname);
  256. uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
  257. if (uname)
  258. printf("%s Firmware: %s\n", p, uname);
  259. for (fdt_index = 0;
  260. uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
  261. fdt_index, NULL), uname;
  262. fdt_index++) {
  263. if (fdt_index == 0)
  264. printf("%s FDT: ", p);
  265. else
  266. printf("%s ", p);
  267. printf("%s\n", uname);
  268. }
  269. uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
  270. if (uname)
  271. printf("%s FPGA: %s\n", p, uname);
  272. /* Print out all of the specified loadables */
  273. for (loadables_index = 0;
  274. uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
  275. loadables_index, NULL), uname;
  276. loadables_index++) {
  277. if (loadables_index == 0) {
  278. printf("%s Loadables: ", p);
  279. } else {
  280. printf("%s ", p);
  281. }
  282. printf("%s\n", uname);
  283. }
  284. /* Process all hash subnodes of the component configuration node */
  285. for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
  286. (noffset >= 0) && (ndepth > 0);
  287. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  288. if (ndepth == 1) {
  289. /* Direct child node of the component configuration node */
  290. fit_image_print_verification_data(fit, noffset, p);
  291. }
  292. }
  293. }
  294. /**
  295. * fit_print_contents - prints out the contents of the FIT format image
  296. * @fit: pointer to the FIT format image header
  297. * @p: pointer to prefix string
  298. *
  299. * fit_print_contents() formats a multi line FIT image contents description.
  300. * The routine prints out FIT image properties (root node level) followed by
  301. * the details of each component image.
  302. *
  303. * returns:
  304. * no returned results
  305. */
  306. void fit_print_contents(const void *fit)
  307. {
  308. char *desc;
  309. char *uname;
  310. int images_noffset;
  311. int confs_noffset;
  312. int noffset;
  313. int ndepth;
  314. int count = 0;
  315. int ret;
  316. const char *p;
  317. time_t timestamp;
  318. /* Indent string is defined in header image.h */
  319. p = IMAGE_INDENT_STRING;
  320. /* Root node properties */
  321. ret = fit_get_desc(fit, 0, &desc);
  322. printf("%sFIT description: ", p);
  323. if (ret)
  324. printf("unavailable\n");
  325. else
  326. printf("%s\n", desc);
  327. if (IMAGE_ENABLE_TIMESTAMP) {
  328. ret = fit_get_timestamp(fit, 0, &timestamp);
  329. printf("%sCreated: ", p);
  330. if (ret)
  331. printf("unavailable\n");
  332. else
  333. genimg_print_time(timestamp);
  334. }
  335. /* Find images parent node offset */
  336. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  337. if (images_noffset < 0) {
  338. printf("Can't find images parent node '%s' (%s)\n",
  339. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  340. return;
  341. }
  342. /* Process its subnodes, print out component images details */
  343. for (ndepth = 0, count = 0,
  344. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  345. (noffset >= 0) && (ndepth > 0);
  346. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  347. if (ndepth == 1) {
  348. /*
  349. * Direct child node of the images parent node,
  350. * i.e. component image node.
  351. */
  352. printf("%s Image %u (%s)\n", p, count++,
  353. fit_get_name(fit, noffset, NULL));
  354. fit_image_print(fit, noffset, p);
  355. }
  356. }
  357. /* Find configurations parent node offset */
  358. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  359. if (confs_noffset < 0) {
  360. debug("Can't get configurations parent node '%s' (%s)\n",
  361. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  362. return;
  363. }
  364. /* get default configuration unit name from default property */
  365. uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
  366. if (uname)
  367. printf("%s Default Configuration: '%s'\n", p, uname);
  368. /* Process its subnodes, print out configurations details */
  369. for (ndepth = 0, count = 0,
  370. noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  371. (noffset >= 0) && (ndepth > 0);
  372. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  373. if (ndepth == 1) {
  374. /*
  375. * Direct child node of the configurations parent node,
  376. * i.e. configuration node.
  377. */
  378. printf("%s Configuration %u (%s)\n", p, count++,
  379. fit_get_name(fit, noffset, NULL));
  380. fit_conf_print(fit, noffset, p);
  381. }
  382. }
  383. }
  384. /**
  385. * fit_image_print - prints out the FIT component image details
  386. * @fit: pointer to the FIT format image header
  387. * @image_noffset: offset of the component image node
  388. * @p: pointer to prefix string
  389. *
  390. * fit_image_print() lists all mandatory properties for the processed component
  391. * image. If present, hash nodes are printed out as well. Load
  392. * address for images of type firmware is also printed out. Since the load
  393. * address is not mandatory for firmware images, it will be output as
  394. * "unavailable" when not present.
  395. *
  396. * returns:
  397. * no returned results
  398. */
  399. void fit_image_print(const void *fit, int image_noffset, const char *p)
  400. {
  401. char *desc;
  402. uint8_t type, arch, os, comp;
  403. size_t size;
  404. ulong load, entry;
  405. const void *data;
  406. int noffset;
  407. int ndepth;
  408. int ret;
  409. /* Mandatory properties */
  410. ret = fit_get_desc(fit, image_noffset, &desc);
  411. printf("%s Description: ", p);
  412. if (ret)
  413. printf("unavailable\n");
  414. else
  415. printf("%s\n", desc);
  416. if (IMAGE_ENABLE_TIMESTAMP) {
  417. time_t timestamp;
  418. ret = fit_get_timestamp(fit, 0, &timestamp);
  419. printf("%s Created: ", p);
  420. if (ret)
  421. printf("unavailable\n");
  422. else
  423. genimg_print_time(timestamp);
  424. }
  425. fit_image_get_type(fit, image_noffset, &type);
  426. printf("%s Type: %s\n", p, genimg_get_type_name(type));
  427. fit_image_get_comp(fit, image_noffset, &comp);
  428. printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
  429. ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
  430. #ifndef USE_HOSTCC
  431. printf("%s Data Start: ", p);
  432. if (ret) {
  433. printf("unavailable\n");
  434. } else {
  435. void *vdata = (void *)data;
  436. printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
  437. }
  438. #endif
  439. printf("%s Data Size: ", p);
  440. if (ret)
  441. printf("unavailable\n");
  442. else
  443. genimg_print_size(size);
  444. /* Remaining, type dependent properties */
  445. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  446. (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
  447. (type == IH_TYPE_FLATDT)) {
  448. fit_image_get_arch(fit, image_noffset, &arch);
  449. printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
  450. }
  451. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
  452. (type == IH_TYPE_FIRMWARE)) {
  453. fit_image_get_os(fit, image_noffset, &os);
  454. printf("%s OS: %s\n", p, genimg_get_os_name(os));
  455. }
  456. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  457. (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
  458. (type == IH_TYPE_FPGA)) {
  459. ret = fit_image_get_load(fit, image_noffset, &load);
  460. printf("%s Load Address: ", p);
  461. if (ret)
  462. printf("unavailable\n");
  463. else
  464. printf("0x%08lx\n", load);
  465. }
  466. /* optional load address for FDT */
  467. if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
  468. printf("%s Load Address: 0x%08lx\n", p, load);
  469. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  470. (type == IH_TYPE_RAMDISK)) {
  471. ret = fit_image_get_entry(fit, image_noffset, &entry);
  472. printf("%s Entry Point: ", p);
  473. if (ret)
  474. printf("unavailable\n");
  475. else
  476. printf("0x%08lx\n", entry);
  477. }
  478. /* Process all hash subnodes of the component image node */
  479. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  480. (noffset >= 0) && (ndepth > 0);
  481. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  482. if (ndepth == 1) {
  483. /* Direct child node of the component image node */
  484. fit_image_print_verification_data(fit, noffset, p);
  485. }
  486. }
  487. }
  488. #else
  489. void fit_print_contents(const void *fit) { }
  490. void fit_image_print(const void *fit, int image_noffset, const char *p) { }
  491. #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
  492. /**
  493. * fit_get_desc - get node description property
  494. * @fit: pointer to the FIT format image header
  495. * @noffset: node offset
  496. * @desc: double pointer to the char, will hold pointer to the description
  497. *
  498. * fit_get_desc() reads description property from a given node, if
  499. * description is found pointer to it is returned in third call argument.
  500. *
  501. * returns:
  502. * 0, on success
  503. * -1, on failure
  504. */
  505. int fit_get_desc(const void *fit, int noffset, char **desc)
  506. {
  507. int len;
  508. *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
  509. if (*desc == NULL) {
  510. fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
  511. return -1;
  512. }
  513. return 0;
  514. }
  515. /**
  516. * fit_get_timestamp - get node timestamp property
  517. * @fit: pointer to the FIT format image header
  518. * @noffset: node offset
  519. * @timestamp: pointer to the time_t, will hold read timestamp
  520. *
  521. * fit_get_timestamp() reads timestamp property from given node, if timestamp
  522. * is found and has a correct size its value is returned in third call
  523. * argument.
  524. *
  525. * returns:
  526. * 0, on success
  527. * -1, on property read failure
  528. * -2, on wrong timestamp size
  529. */
  530. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
  531. {
  532. int len;
  533. const void *data;
  534. data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
  535. if (data == NULL) {
  536. fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
  537. return -1;
  538. }
  539. if (len != sizeof(uint32_t)) {
  540. debug("FIT timestamp with incorrect size of (%u)\n", len);
  541. return -2;
  542. }
  543. *timestamp = uimage_to_cpu(*((uint32_t *)data));
  544. return 0;
  545. }
  546. /**
  547. * fit_image_get_node - get node offset for component image of a given unit name
  548. * @fit: pointer to the FIT format image header
  549. * @image_uname: component image node unit name
  550. *
  551. * fit_image_get_node() finds a component image (within the '/images'
  552. * node) of a provided unit name. If image is found its node offset is
  553. * returned to the caller.
  554. *
  555. * returns:
  556. * image node offset when found (>=0)
  557. * negative number on failure (FDT_ERR_* code)
  558. */
  559. int fit_image_get_node(const void *fit, const char *image_uname)
  560. {
  561. int noffset, images_noffset;
  562. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  563. if (images_noffset < 0) {
  564. debug("Can't find images parent node '%s' (%s)\n",
  565. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  566. return images_noffset;
  567. }
  568. noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
  569. if (noffset < 0) {
  570. debug("Can't get node offset for image unit name: '%s' (%s)\n",
  571. image_uname, fdt_strerror(noffset));
  572. }
  573. return noffset;
  574. }
  575. /**
  576. * fit_image_get_os - get os id for a given component image node
  577. * @fit: pointer to the FIT format image header
  578. * @noffset: component image node offset
  579. * @os: pointer to the uint8_t, will hold os numeric id
  580. *
  581. * fit_image_get_os() finds os property in a given component image node.
  582. * If the property is found, its (string) value is translated to the numeric
  583. * id which is returned to the caller.
  584. *
  585. * returns:
  586. * 0, on success
  587. * -1, on failure
  588. */
  589. int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  590. {
  591. int len;
  592. const void *data;
  593. /* Get OS name from property data */
  594. data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
  595. if (data == NULL) {
  596. fit_get_debug(fit, noffset, FIT_OS_PROP, len);
  597. *os = -1;
  598. return -1;
  599. }
  600. /* Translate OS name to id */
  601. *os = genimg_get_os_id(data);
  602. return 0;
  603. }
  604. /**
  605. * fit_image_get_arch - get arch id for a given component image node
  606. * @fit: pointer to the FIT format image header
  607. * @noffset: component image node offset
  608. * @arch: pointer to the uint8_t, will hold arch numeric id
  609. *
  610. * fit_image_get_arch() finds arch property in a given component image node.
  611. * If the property is found, its (string) value is translated to the numeric
  612. * id which is returned to the caller.
  613. *
  614. * returns:
  615. * 0, on success
  616. * -1, on failure
  617. */
  618. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
  619. {
  620. int len;
  621. const void *data;
  622. /* Get architecture name from property data */
  623. data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
  624. if (data == NULL) {
  625. fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
  626. *arch = -1;
  627. return -1;
  628. }
  629. /* Translate architecture name to id */
  630. *arch = genimg_get_arch_id(data);
  631. return 0;
  632. }
  633. /**
  634. * fit_image_get_type - get type id for a given component image node
  635. * @fit: pointer to the FIT format image header
  636. * @noffset: component image node offset
  637. * @type: pointer to the uint8_t, will hold type numeric id
  638. *
  639. * fit_image_get_type() finds type property in a given component image node.
  640. * If the property is found, its (string) value is translated to the numeric
  641. * id which is returned to the caller.
  642. *
  643. * returns:
  644. * 0, on success
  645. * -1, on failure
  646. */
  647. int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
  648. {
  649. int len;
  650. const void *data;
  651. /* Get image type name from property data */
  652. data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
  653. if (data == NULL) {
  654. fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
  655. *type = -1;
  656. return -1;
  657. }
  658. /* Translate image type name to id */
  659. *type = genimg_get_type_id(data);
  660. return 0;
  661. }
  662. /**
  663. * fit_image_get_comp - get comp id for a given component image node
  664. * @fit: pointer to the FIT format image header
  665. * @noffset: component image node offset
  666. * @comp: pointer to the uint8_t, will hold comp numeric id
  667. *
  668. * fit_image_get_comp() finds comp property in a given component image node.
  669. * If the property is found, its (string) value is translated to the numeric
  670. * id which is returned to the caller.
  671. *
  672. * returns:
  673. * 0, on success
  674. * -1, on failure
  675. */
  676. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
  677. {
  678. int len;
  679. const void *data;
  680. /* Get compression name from property data */
  681. data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
  682. if (data == NULL) {
  683. fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
  684. *comp = -1;
  685. return -1;
  686. }
  687. /* Translate compression name to id */
  688. *comp = genimg_get_comp_id(data);
  689. return 0;
  690. }
  691. static int fit_image_get_address(const void *fit, int noffset, char *name,
  692. ulong *load)
  693. {
  694. int len, cell_len;
  695. const fdt32_t *cell;
  696. uint64_t load64 = 0;
  697. cell = fdt_getprop(fit, noffset, name, &len);
  698. if (cell == NULL) {
  699. fit_get_debug(fit, noffset, name, len);
  700. return -1;
  701. }
  702. if (len > sizeof(ulong)) {
  703. printf("Unsupported %s address size\n", name);
  704. return -1;
  705. }
  706. cell_len = len >> 2;
  707. /* Use load64 to avoid compiling warning for 32-bit target */
  708. while (cell_len--) {
  709. load64 = (load64 << 32) | uimage_to_cpu(*cell);
  710. cell++;
  711. }
  712. *load = (ulong)load64;
  713. return 0;
  714. }
  715. /**
  716. * fit_image_get_load() - get load addr property for given component image node
  717. * @fit: pointer to the FIT format image header
  718. * @noffset: component image node offset
  719. * @load: pointer to the uint32_t, will hold load address
  720. *
  721. * fit_image_get_load() finds load address property in a given component
  722. * image node. If the property is found, its value is returned to the caller.
  723. *
  724. * returns:
  725. * 0, on success
  726. * -1, on failure
  727. */
  728. int fit_image_get_load(const void *fit, int noffset, ulong *load)
  729. {
  730. return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
  731. }
  732. /**
  733. * fit_image_get_entry() - get entry point address property
  734. * @fit: pointer to the FIT format image header
  735. * @noffset: component image node offset
  736. * @entry: pointer to the uint32_t, will hold entry point address
  737. *
  738. * This gets the entry point address property for a given component image
  739. * node.
  740. *
  741. * fit_image_get_entry() finds entry point address property in a given
  742. * component image node. If the property is found, its value is returned
  743. * to the caller.
  744. *
  745. * returns:
  746. * 0, on success
  747. * -1, on failure
  748. */
  749. int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
  750. {
  751. return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
  752. }
  753. /**
  754. * fit_image_get_data - get data property and its size for a given component image node
  755. * @fit: pointer to the FIT format image header
  756. * @noffset: component image node offset
  757. * @data: double pointer to void, will hold data property's data address
  758. * @size: pointer to size_t, will hold data property's data size
  759. *
  760. * fit_image_get_data() finds data property in a given component image node.
  761. * If the property is found its data start address and size are returned to
  762. * the caller.
  763. *
  764. * returns:
  765. * 0, on success
  766. * -1, on failure
  767. */
  768. int fit_image_get_data(const void *fit, int noffset,
  769. const void **data, size_t *size)
  770. {
  771. int len;
  772. *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
  773. if (*data == NULL) {
  774. fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
  775. *size = 0;
  776. return -1;
  777. }
  778. *size = len;
  779. return 0;
  780. }
  781. /**
  782. * Get 'data-offset' property from a given image node.
  783. *
  784. * @fit: pointer to the FIT image header
  785. * @noffset: component image node offset
  786. * @data_offset: holds the data-offset property
  787. *
  788. * returns:
  789. * 0, on success
  790. * -ENOENT if the property could not be found
  791. */
  792. int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
  793. {
  794. const fdt32_t *val;
  795. val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
  796. if (!val)
  797. return -ENOENT;
  798. *data_offset = fdt32_to_cpu(*val);
  799. return 0;
  800. }
  801. /**
  802. * Get 'data-position' property from a given image node.
  803. *
  804. * @fit: pointer to the FIT image header
  805. * @noffset: component image node offset
  806. * @data_position: holds the data-position property
  807. *
  808. * returns:
  809. * 0, on success
  810. * -ENOENT if the property could not be found
  811. */
  812. int fit_image_get_data_position(const void *fit, int noffset,
  813. int *data_position)
  814. {
  815. const fdt32_t *val;
  816. val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
  817. if (!val)
  818. return -ENOENT;
  819. *data_position = fdt32_to_cpu(*val);
  820. return 0;
  821. }
  822. /**
  823. * Get 'data-size' property from a given image node.
  824. *
  825. * @fit: pointer to the FIT image header
  826. * @noffset: component image node offset
  827. * @data_size: holds the data-size property
  828. *
  829. * returns:
  830. * 0, on success
  831. * -ENOENT if the property could not be found
  832. */
  833. int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
  834. {
  835. const fdt32_t *val;
  836. val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
  837. if (!val)
  838. return -ENOENT;
  839. *data_size = fdt32_to_cpu(*val);
  840. return 0;
  841. }
  842. /**
  843. * fit_image_get_data_and_size - get data and its size including
  844. * both embedded and external data
  845. * @fit: pointer to the FIT format image header
  846. * @noffset: component image node offset
  847. * @data: double pointer to void, will hold data property's data address
  848. * @size: pointer to size_t, will hold data property's data size
  849. *
  850. * fit_image_get_data_and_size() finds data and its size including
  851. * both embedded and external data. If the property is found
  852. * its data start address and size are returned to the caller.
  853. *
  854. * returns:
  855. * 0, on success
  856. * otherwise, on failure
  857. */
  858. int fit_image_get_data_and_size(const void *fit, int noffset,
  859. const void **data, size_t *size)
  860. {
  861. bool external_data = false;
  862. int offset;
  863. int len;
  864. int ret;
  865. if (!fit_image_get_data_position(fit, noffset, &offset)) {
  866. external_data = true;
  867. } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
  868. external_data = true;
  869. /*
  870. * For FIT with external data, figure out where
  871. * the external images start. This is the base
  872. * for the data-offset properties in each image.
  873. */
  874. offset += ((fdt_totalsize(fit) + 3) & ~3);
  875. }
  876. if (external_data) {
  877. debug("External Data\n");
  878. ret = fit_image_get_data_size(fit, noffset, &len);
  879. *data = fit + offset;
  880. *size = len;
  881. } else {
  882. ret = fit_image_get_data(fit, noffset, data, size);
  883. }
  884. return ret;
  885. }
  886. /**
  887. * fit_image_hash_get_algo - get hash algorithm name
  888. * @fit: pointer to the FIT format image header
  889. * @noffset: hash node offset
  890. * @algo: double pointer to char, will hold pointer to the algorithm name
  891. *
  892. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  893. * If the property is found its data start address is returned to the caller.
  894. *
  895. * returns:
  896. * 0, on success
  897. * -1, on failure
  898. */
  899. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
  900. {
  901. int len;
  902. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  903. if (*algo == NULL) {
  904. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  905. return -1;
  906. }
  907. return 0;
  908. }
  909. /**
  910. * fit_image_hash_get_value - get hash value and length
  911. * @fit: pointer to the FIT format image header
  912. * @noffset: hash node offset
  913. * @value: double pointer to uint8_t, will hold address of a hash value data
  914. * @value_len: pointer to an int, will hold hash data length
  915. *
  916. * fit_image_hash_get_value() finds hash value property in a given hash node.
  917. * If the property is found its data start address and size are returned to
  918. * the caller.
  919. *
  920. * returns:
  921. * 0, on success
  922. * -1, on failure
  923. */
  924. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  925. int *value_len)
  926. {
  927. int len;
  928. *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
  929. if (*value == NULL) {
  930. fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
  931. *value_len = 0;
  932. return -1;
  933. }
  934. *value_len = len;
  935. return 0;
  936. }
  937. /**
  938. * fit_image_hash_get_ignore - get hash ignore flag
  939. * @fit: pointer to the FIT format image header
  940. * @noffset: hash node offset
  941. * @ignore: pointer to an int, will hold hash ignore flag
  942. *
  943. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  944. * If the property is found and non-zero, the hash algorithm is not verified by
  945. * u-boot automatically.
  946. *
  947. * returns:
  948. * 0, on ignore not found
  949. * value, on ignore found
  950. */
  951. static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  952. {
  953. int len;
  954. int *value;
  955. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  956. if (value == NULL || len != sizeof(int))
  957. *ignore = 0;
  958. else
  959. *ignore = *value;
  960. return 0;
  961. }
  962. ulong fit_get_end(const void *fit)
  963. {
  964. return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
  965. }
  966. /**
  967. * fit_set_timestamp - set node timestamp property
  968. * @fit: pointer to the FIT format image header
  969. * @noffset: node offset
  970. * @timestamp: timestamp value to be set
  971. *
  972. * fit_set_timestamp() attempts to set timestamp property in the requested
  973. * node and returns operation status to the caller.
  974. *
  975. * returns:
  976. * 0, on success
  977. * -ENOSPC if no space in device tree, -1 for other error
  978. */
  979. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  980. {
  981. uint32_t t;
  982. int ret;
  983. t = cpu_to_uimage(timestamp);
  984. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  985. sizeof(uint32_t));
  986. if (ret) {
  987. debug("Can't set '%s' property for '%s' node (%s)\n",
  988. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  989. fdt_strerror(ret));
  990. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
  991. }
  992. return 0;
  993. }
  994. /**
  995. * calculate_hash - calculate and return hash for provided input data
  996. * @data: pointer to the input data
  997. * @data_len: data length
  998. * @algo: requested hash algorithm
  999. * @value: pointer to the char, will hold hash value data (caller must
  1000. * allocate enough free space)
  1001. * value_len: length of the calculated hash
  1002. *
  1003. * calculate_hash() computes input data hash according to the requested
  1004. * algorithm.
  1005. * Resulting hash value is placed in caller provided 'value' buffer, length
  1006. * of the calculated hash is returned via value_len pointer argument.
  1007. *
  1008. * returns:
  1009. * 0, on success
  1010. * -1, when algo is unsupported
  1011. */
  1012. int calculate_hash(const void *data, int data_len, const char *algo,
  1013. uint8_t *value, int *value_len)
  1014. {
  1015. if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  1016. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  1017. CHUNKSZ_CRC32);
  1018. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  1019. *value_len = 4;
  1020. } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
  1021. sha1_csum_wd((unsigned char *)data, data_len,
  1022. (unsigned char *)value, CHUNKSZ_SHA1);
  1023. *value_len = 20;
  1024. } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
  1025. sha256_csum_wd((unsigned char *)data, data_len,
  1026. (unsigned char *)value, CHUNKSZ_SHA256);
  1027. *value_len = SHA256_SUM_LEN;
  1028. } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
  1029. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  1030. *value_len = 16;
  1031. } else {
  1032. debug("Unsupported hash alogrithm\n");
  1033. return -1;
  1034. }
  1035. return 0;
  1036. }
  1037. static int fit_image_check_hash(const void *fit, int noffset, const void *data,
  1038. size_t size, char **err_msgp)
  1039. {
  1040. uint8_t value[FIT_MAX_HASH_LEN];
  1041. int value_len;
  1042. char *algo;
  1043. uint8_t *fit_value;
  1044. int fit_value_len;
  1045. int ignore;
  1046. *err_msgp = NULL;
  1047. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  1048. *err_msgp = "Can't get hash algo property";
  1049. return -1;
  1050. }
  1051. printf("%s", algo);
  1052. if (IMAGE_ENABLE_IGNORE) {
  1053. fit_image_hash_get_ignore(fit, noffset, &ignore);
  1054. if (ignore) {
  1055. printf("-skipped ");
  1056. return 0;
  1057. }
  1058. }
  1059. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  1060. &fit_value_len)) {
  1061. *err_msgp = "Can't get hash value property";
  1062. return -1;
  1063. }
  1064. if (calculate_hash(data, size, algo, value, &value_len)) {
  1065. *err_msgp = "Unsupported hash algorithm";
  1066. return -1;
  1067. }
  1068. if (value_len != fit_value_len) {
  1069. *err_msgp = "Bad hash value len";
  1070. return -1;
  1071. } else if (memcmp(value, fit_value, value_len) != 0) {
  1072. *err_msgp = "Bad hash value";
  1073. return -1;
  1074. }
  1075. return 0;
  1076. }
  1077. int fit_image_verify_with_data(const void *fit, int image_noffset,
  1078. const void *data, size_t size)
  1079. {
  1080. int noffset = 0;
  1081. char *err_msg = "";
  1082. int verify_all = 1;
  1083. int ret;
  1084. /* Verify all required signatures */
  1085. if (IMAGE_ENABLE_VERIFY &&
  1086. fit_image_verify_required_sigs(fit, image_noffset, data, size,
  1087. gd_fdt_blob(), &verify_all)) {
  1088. err_msg = "Unable to verify required signature";
  1089. goto error;
  1090. }
  1091. /* Process all hash subnodes of the component image node */
  1092. fdt_for_each_subnode(noffset, fit, image_noffset) {
  1093. const char *name = fit_get_name(fit, noffset, NULL);
  1094. /*
  1095. * Check subnode name, must be equal to "hash".
  1096. * Multiple hash nodes require unique unit node
  1097. * names, e.g. hash-1, hash-2, etc.
  1098. */
  1099. if (!strncmp(name, FIT_HASH_NODENAME,
  1100. strlen(FIT_HASH_NODENAME))) {
  1101. if (fit_image_check_hash(fit, noffset, data, size,
  1102. &err_msg))
  1103. goto error;
  1104. puts("+ ");
  1105. } else if (IMAGE_ENABLE_VERIFY && verify_all &&
  1106. !strncmp(name, FIT_SIG_NODENAME,
  1107. strlen(FIT_SIG_NODENAME))) {
  1108. ret = fit_image_check_sig(fit, noffset, data,
  1109. size, -1, &err_msg);
  1110. /*
  1111. * Show an indication on failure, but do not return
  1112. * an error. Only keys marked 'required' can cause
  1113. * an image validation failure. See the call to
  1114. * fit_image_verify_required_sigs() above.
  1115. */
  1116. if (ret)
  1117. puts("- ");
  1118. else
  1119. puts("+ ");
  1120. }
  1121. }
  1122. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  1123. err_msg = "Corrupted or truncated tree";
  1124. goto error;
  1125. }
  1126. return 1;
  1127. error:
  1128. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  1129. err_msg, fit_get_name(fit, noffset, NULL),
  1130. fit_get_name(fit, image_noffset, NULL));
  1131. return 0;
  1132. }
  1133. /**
  1134. * fit_image_verify - verify data integrity
  1135. * @fit: pointer to the FIT format image header
  1136. * @image_noffset: component image node offset
  1137. *
  1138. * fit_image_verify() goes over component image hash nodes,
  1139. * re-calculates each data hash and compares with the value stored in hash
  1140. * node.
  1141. *
  1142. * returns:
  1143. * 1, if all hashes are valid
  1144. * 0, otherwise (or on error)
  1145. */
  1146. int fit_image_verify(const void *fit, int image_noffset)
  1147. {
  1148. const void *data;
  1149. size_t size;
  1150. int noffset = 0;
  1151. char *err_msg = "";
  1152. /* Get image data and data length */
  1153. if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
  1154. err_msg = "Can't get image data/size";
  1155. printf("error!\n%s for '%s' hash node in '%s' image node\n",
  1156. err_msg, fit_get_name(fit, noffset, NULL),
  1157. fit_get_name(fit, image_noffset, NULL));
  1158. return 0;
  1159. }
  1160. return fit_image_verify_with_data(fit, image_noffset, data, size);
  1161. }
  1162. /**
  1163. * fit_all_image_verify - verify data integrity for all images
  1164. * @fit: pointer to the FIT format image header
  1165. *
  1166. * fit_all_image_verify() goes over all images in the FIT and
  1167. * for every images checks if all it's hashes are valid.
  1168. *
  1169. * returns:
  1170. * 1, if all hashes of all images are valid
  1171. * 0, otherwise (or on error)
  1172. */
  1173. int fit_all_image_verify(const void *fit)
  1174. {
  1175. int images_noffset;
  1176. int noffset;
  1177. int ndepth;
  1178. int count;
  1179. /* Find images parent node offset */
  1180. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1181. if (images_noffset < 0) {
  1182. printf("Can't find images parent node '%s' (%s)\n",
  1183. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  1184. return 0;
  1185. }
  1186. /* Process all image subnodes, check hashes for each */
  1187. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  1188. (ulong)fit);
  1189. for (ndepth = 0, count = 0,
  1190. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  1191. (noffset >= 0) && (ndepth > 0);
  1192. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1193. if (ndepth == 1) {
  1194. /*
  1195. * Direct child node of the images parent node,
  1196. * i.e. component image node.
  1197. */
  1198. printf(" Hash(es) for Image %u (%s): ", count,
  1199. fit_get_name(fit, noffset, NULL));
  1200. count++;
  1201. if (!fit_image_verify(fit, noffset))
  1202. return 0;
  1203. printf("\n");
  1204. }
  1205. }
  1206. return 1;
  1207. }
  1208. /**
  1209. * fit_image_check_os - check whether image node is of a given os type
  1210. * @fit: pointer to the FIT format image header
  1211. * @noffset: component image node offset
  1212. * @os: requested image os
  1213. *
  1214. * fit_image_check_os() reads image os property and compares its numeric
  1215. * id with the requested os. Comparison result is returned to the caller.
  1216. *
  1217. * returns:
  1218. * 1 if image is of given os type
  1219. * 0 otherwise (or on error)
  1220. */
  1221. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1222. {
  1223. uint8_t image_os;
  1224. if (fit_image_get_os(fit, noffset, &image_os))
  1225. return 0;
  1226. return (os == image_os);
  1227. }
  1228. /**
  1229. * fit_image_check_arch - check whether image node is of a given arch
  1230. * @fit: pointer to the FIT format image header
  1231. * @noffset: component image node offset
  1232. * @arch: requested imagearch
  1233. *
  1234. * fit_image_check_arch() reads image arch property and compares its numeric
  1235. * id with the requested arch. Comparison result is returned to the caller.
  1236. *
  1237. * returns:
  1238. * 1 if image is of given arch
  1239. * 0 otherwise (or on error)
  1240. */
  1241. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1242. {
  1243. uint8_t image_arch;
  1244. int aarch32_support = 0;
  1245. #ifdef CONFIG_ARM64_SUPPORT_AARCH32
  1246. aarch32_support = 1;
  1247. #endif
  1248. if (fit_image_get_arch(fit, noffset, &image_arch))
  1249. return 0;
  1250. return (arch == image_arch) ||
  1251. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
  1252. (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
  1253. aarch32_support);
  1254. }
  1255. /**
  1256. * fit_image_check_type - check whether image node is of a given type
  1257. * @fit: pointer to the FIT format image header
  1258. * @noffset: component image node offset
  1259. * @type: requested image type
  1260. *
  1261. * fit_image_check_type() reads image type property and compares its numeric
  1262. * id with the requested type. Comparison result is returned to the caller.
  1263. *
  1264. * returns:
  1265. * 1 if image is of given type
  1266. * 0 otherwise (or on error)
  1267. */
  1268. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1269. {
  1270. uint8_t image_type;
  1271. if (fit_image_get_type(fit, noffset, &image_type))
  1272. return 0;
  1273. return (type == image_type);
  1274. }
  1275. /**
  1276. * fit_image_check_comp - check whether image node uses given compression
  1277. * @fit: pointer to the FIT format image header
  1278. * @noffset: component image node offset
  1279. * @comp: requested image compression type
  1280. *
  1281. * fit_image_check_comp() reads image compression property and compares its
  1282. * numeric id with the requested compression type. Comparison result is
  1283. * returned to the caller.
  1284. *
  1285. * returns:
  1286. * 1 if image uses requested compression
  1287. * 0 otherwise (or on error)
  1288. */
  1289. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1290. {
  1291. uint8_t image_comp;
  1292. if (fit_image_get_comp(fit, noffset, &image_comp))
  1293. return 0;
  1294. return (comp == image_comp);
  1295. }
  1296. /**
  1297. * fit_check_format - sanity check FIT image format
  1298. * @fit: pointer to the FIT format image header
  1299. *
  1300. * fit_check_format() runs a basic sanity FIT image verification.
  1301. * Routine checks for mandatory properties, nodes, etc.
  1302. *
  1303. * returns:
  1304. * 1, on success
  1305. * 0, on failure
  1306. */
  1307. int fit_check_format(const void *fit)
  1308. {
  1309. /* mandatory / node 'description' property */
  1310. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1311. debug("Wrong FIT format: no description\n");
  1312. return 0;
  1313. }
  1314. if (IMAGE_ENABLE_TIMESTAMP) {
  1315. /* mandatory / node 'timestamp' property */
  1316. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1317. debug("Wrong FIT format: no timestamp\n");
  1318. return 0;
  1319. }
  1320. }
  1321. /* mandatory subimages parent '/images' node */
  1322. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1323. debug("Wrong FIT format: no images parent node\n");
  1324. return 0;
  1325. }
  1326. return 1;
  1327. }
  1328. /**
  1329. * fit_conf_find_compat
  1330. * @fit: pointer to the FIT format image header
  1331. * @fdt: pointer to the device tree to compare against
  1332. *
  1333. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1334. * most compatible with the passed in device tree.
  1335. *
  1336. * Example:
  1337. *
  1338. * / o image-tree
  1339. * |-o images
  1340. * | |-o fdt-1
  1341. * | |-o fdt-2
  1342. * |
  1343. * |-o configurations
  1344. * |-o config-1
  1345. * | |-fdt = fdt-1
  1346. * |
  1347. * |-o config-2
  1348. * |-fdt = fdt-2
  1349. *
  1350. * / o U-Boot fdt
  1351. * |-compatible = "foo,bar", "bim,bam"
  1352. *
  1353. * / o kernel fdt1
  1354. * |-compatible = "foo,bar",
  1355. *
  1356. * / o kernel fdt2
  1357. * |-compatible = "bim,bam", "baz,biz"
  1358. *
  1359. * Configuration 1 would be picked because the first string in U-Boot's
  1360. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1361. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1362. *
  1363. * returns:
  1364. * offset to the configuration to use if one was found
  1365. * -1 otherwise
  1366. */
  1367. int fit_conf_find_compat(const void *fit, const void *fdt)
  1368. {
  1369. int ndepth = 0;
  1370. int noffset, confs_noffset, images_noffset;
  1371. const void *fdt_compat;
  1372. int fdt_compat_len;
  1373. int best_match_offset = 0;
  1374. int best_match_pos = 0;
  1375. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1376. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1377. if (confs_noffset < 0 || images_noffset < 0) {
  1378. debug("Can't find configurations or images nodes.\n");
  1379. return -1;
  1380. }
  1381. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1382. if (!fdt_compat) {
  1383. debug("Fdt for comparison has no \"compatible\" property.\n");
  1384. return -1;
  1385. }
  1386. /*
  1387. * Loop over the configurations in the FIT image.
  1388. */
  1389. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1390. (noffset >= 0) && (ndepth > 0);
  1391. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1392. const void *kfdt;
  1393. const char *kfdt_name;
  1394. int kfdt_noffset;
  1395. const char *cur_fdt_compat;
  1396. int len;
  1397. size_t size;
  1398. int i;
  1399. if (ndepth > 1)
  1400. continue;
  1401. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1402. if (!kfdt_name) {
  1403. debug("No fdt property found.\n");
  1404. continue;
  1405. }
  1406. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1407. kfdt_name);
  1408. if (kfdt_noffset < 0) {
  1409. debug("No image node named \"%s\" found.\n",
  1410. kfdt_name);
  1411. continue;
  1412. }
  1413. /*
  1414. * Get a pointer to this configuration's fdt.
  1415. */
  1416. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1417. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1418. continue;
  1419. }
  1420. len = fdt_compat_len;
  1421. cur_fdt_compat = fdt_compat;
  1422. /*
  1423. * Look for a match for each U-Boot compatibility string in
  1424. * turn in this configuration's fdt.
  1425. */
  1426. for (i = 0; len > 0 &&
  1427. (!best_match_offset || best_match_pos > i); i++) {
  1428. int cur_len = strlen(cur_fdt_compat) + 1;
  1429. if (!fdt_node_check_compatible(kfdt, 0,
  1430. cur_fdt_compat)) {
  1431. best_match_offset = noffset;
  1432. best_match_pos = i;
  1433. break;
  1434. }
  1435. len -= cur_len;
  1436. cur_fdt_compat += cur_len;
  1437. }
  1438. }
  1439. if (!best_match_offset) {
  1440. debug("No match found.\n");
  1441. return -1;
  1442. }
  1443. return best_match_offset;
  1444. }
  1445. /**
  1446. * fit_conf_get_node - get node offset for configuration of a given unit name
  1447. * @fit: pointer to the FIT format image header
  1448. * @conf_uname: configuration node unit name
  1449. *
  1450. * fit_conf_get_node() finds a configuration (within the '/configurations'
  1451. * parent node) of a provided unit name. If configuration is found its node
  1452. * offset is returned to the caller.
  1453. *
  1454. * When NULL is provided in second argument fit_conf_get_node() will search
  1455. * for a default configuration node instead. Default configuration node unit
  1456. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1457. * node.
  1458. *
  1459. * returns:
  1460. * configuration node offset when found (>=0)
  1461. * negative number on failure (FDT_ERR_* code)
  1462. */
  1463. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1464. {
  1465. int noffset, confs_noffset;
  1466. int len;
  1467. const char *s;
  1468. char *conf_uname_copy = NULL;
  1469. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1470. if (confs_noffset < 0) {
  1471. debug("Can't find configurations parent node '%s' (%s)\n",
  1472. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1473. return confs_noffset;
  1474. }
  1475. if (conf_uname == NULL) {
  1476. /* get configuration unit name from the default property */
  1477. debug("No configuration specified, trying default...\n");
  1478. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1479. FIT_DEFAULT_PROP, &len);
  1480. if (conf_uname == NULL) {
  1481. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1482. len);
  1483. return len;
  1484. }
  1485. debug("Found default configuration: '%s'\n", conf_uname);
  1486. }
  1487. s = strchr(conf_uname, '#');
  1488. if (s) {
  1489. len = s - conf_uname;
  1490. conf_uname_copy = malloc(len + 1);
  1491. if (!conf_uname_copy) {
  1492. debug("Can't allocate uname copy: '%s'\n",
  1493. conf_uname);
  1494. return -ENOMEM;
  1495. }
  1496. memcpy(conf_uname_copy, conf_uname, len);
  1497. conf_uname_copy[len] = '\0';
  1498. conf_uname = conf_uname_copy;
  1499. }
  1500. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1501. if (noffset < 0) {
  1502. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1503. conf_uname, fdt_strerror(noffset));
  1504. }
  1505. if (conf_uname_copy)
  1506. free(conf_uname_copy);
  1507. return noffset;
  1508. }
  1509. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1510. const char *prop_name)
  1511. {
  1512. return fdt_stringlist_count(fit, noffset, prop_name);
  1513. }
  1514. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1515. const char *prop_name, int index)
  1516. {
  1517. const char *uname;
  1518. int len;
  1519. /* get kernel image unit name from configuration kernel property */
  1520. uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
  1521. if (uname == NULL)
  1522. return len;
  1523. return fit_image_get_node(fit, uname);
  1524. }
  1525. int fit_conf_get_prop_node(const void *fit, int noffset,
  1526. const char *prop_name)
  1527. {
  1528. return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
  1529. }
  1530. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1531. {
  1532. fit_image_print(fit, rd_noffset, " ");
  1533. if (verify) {
  1534. puts(" Verifying Hash Integrity ... ");
  1535. if (!fit_image_verify(fit, rd_noffset)) {
  1536. puts("Bad Data Hash\n");
  1537. return -EACCES;
  1538. }
  1539. puts("OK\n");
  1540. }
  1541. return 0;
  1542. }
  1543. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1544. ulong addr)
  1545. {
  1546. int cfg_noffset;
  1547. void *fit_hdr;
  1548. int noffset;
  1549. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1550. prop_name, images->fit_uname_cfg, addr);
  1551. /* Check whether configuration has this property defined */
  1552. fit_hdr = map_sysmem(addr, 0);
  1553. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1554. if (cfg_noffset < 0) {
  1555. debug("* %s: no such config\n", prop_name);
  1556. return -EINVAL;
  1557. }
  1558. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1559. if (noffset < 0) {
  1560. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1561. return -ENOENT;
  1562. }
  1563. return noffset;
  1564. }
  1565. /**
  1566. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1567. *
  1568. * @return the properly name where we expect to find the image in the
  1569. * config node
  1570. */
  1571. static const char *fit_get_image_type_property(int type)
  1572. {
  1573. /*
  1574. * This is sort-of available in the uimage_type[] table in image.c
  1575. * but we don't have access to the short name, and "fdt" is different
  1576. * anyway. So let's just keep it here.
  1577. */
  1578. switch (type) {
  1579. case IH_TYPE_FLATDT:
  1580. return FIT_FDT_PROP;
  1581. case IH_TYPE_KERNEL:
  1582. return FIT_KERNEL_PROP;
  1583. case IH_TYPE_RAMDISK:
  1584. return FIT_RAMDISK_PROP;
  1585. case IH_TYPE_X86_SETUP:
  1586. return FIT_SETUP_PROP;
  1587. case IH_TYPE_LOADABLE:
  1588. return FIT_LOADABLE_PROP;
  1589. case IH_TYPE_FPGA:
  1590. return FIT_FPGA_PROP;
  1591. case IH_TYPE_STANDALONE:
  1592. return FIT_STANDALONE_PROP;
  1593. }
  1594. return "unknown";
  1595. }
  1596. int fit_image_load(bootm_headers_t *images, ulong addr,
  1597. const char **fit_unamep, const char **fit_uname_configp,
  1598. int arch, int image_type, int bootstage_id,
  1599. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1600. {
  1601. int cfg_noffset, noffset;
  1602. const char *fit_uname;
  1603. const char *fit_uname_config;
  1604. const char *fit_base_uname_config;
  1605. const void *fit;
  1606. const void *buf;
  1607. size_t size;
  1608. int type_ok, os_ok;
  1609. ulong load, data, len;
  1610. uint8_t os;
  1611. #ifndef USE_HOSTCC
  1612. uint8_t os_arch;
  1613. #endif
  1614. const char *prop_name;
  1615. int ret;
  1616. fit = map_sysmem(addr, 0);
  1617. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1618. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1619. fit_base_uname_config = NULL;
  1620. prop_name = fit_get_image_type_property(image_type);
  1621. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1622. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1623. if (!fit_check_format(fit)) {
  1624. printf("Bad FIT %s image format!\n", prop_name);
  1625. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1626. return -ENOEXEC;
  1627. }
  1628. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1629. if (fit_uname) {
  1630. /* get FIT component image node offset */
  1631. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1632. noffset = fit_image_get_node(fit, fit_uname);
  1633. } else {
  1634. /*
  1635. * no image node unit name, try to get config
  1636. * node first. If config unit node name is NULL
  1637. * fit_conf_get_node() will try to find default config node
  1638. */
  1639. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1640. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1641. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1642. } else {
  1643. cfg_noffset = fit_conf_get_node(fit,
  1644. fit_uname_config);
  1645. }
  1646. if (cfg_noffset < 0) {
  1647. puts("Could not find configuration node\n");
  1648. bootstage_error(bootstage_id +
  1649. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1650. return -ENOENT;
  1651. }
  1652. fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1653. printf(" Using '%s' configuration\n", fit_base_uname_config);
  1654. /* Remember this config */
  1655. if (image_type == IH_TYPE_KERNEL)
  1656. images->fit_uname_cfg = fit_base_uname_config;
  1657. if (IMAGE_ENABLE_VERIFY && images->verify) {
  1658. puts(" Verifying Hash Integrity ... ");
  1659. if (fit_config_verify(fit, cfg_noffset)) {
  1660. puts("Bad Data Hash\n");
  1661. bootstage_error(bootstage_id +
  1662. BOOTSTAGE_SUB_HASH);
  1663. return -EACCES;
  1664. }
  1665. puts("OK\n");
  1666. }
  1667. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1668. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1669. prop_name);
  1670. fit_uname = fit_get_name(fit, noffset, NULL);
  1671. }
  1672. if (noffset < 0) {
  1673. puts("Could not find subimage node\n");
  1674. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1675. return -ENOENT;
  1676. }
  1677. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1678. ret = fit_image_select(fit, noffset, images->verify);
  1679. if (ret) {
  1680. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1681. return ret;
  1682. }
  1683. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1684. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1685. if (!fit_image_check_target_arch(fit, noffset)) {
  1686. puts("Unsupported Architecture\n");
  1687. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1688. return -ENOEXEC;
  1689. }
  1690. #endif
  1691. #ifndef USE_HOSTCC
  1692. fit_image_get_arch(fit, noffset, &os_arch);
  1693. images->os.arch = os_arch;
  1694. #endif
  1695. if (image_type == IH_TYPE_FLATDT &&
  1696. !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
  1697. puts("FDT image is compressed");
  1698. return -EPROTONOSUPPORT;
  1699. }
  1700. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1701. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1702. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1703. (image_type == IH_TYPE_KERNEL &&
  1704. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1705. os_ok = image_type == IH_TYPE_FLATDT ||
  1706. image_type == IH_TYPE_FPGA ||
  1707. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1708. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1709. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
  1710. /*
  1711. * If either of the checks fail, we should report an error, but
  1712. * if the image type is coming from the "loadables" field, we
  1713. * don't care what it is
  1714. */
  1715. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1716. fit_image_get_os(fit, noffset, &os);
  1717. printf("No %s %s %s Image\n",
  1718. genimg_get_os_name(os),
  1719. genimg_get_arch_name(arch),
  1720. genimg_get_type_name(image_type));
  1721. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1722. return -EIO;
  1723. }
  1724. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1725. /* get image data address and length */
  1726. if (fit_image_get_data_and_size(fit, noffset, &buf, &size)) {
  1727. printf("Could not find %s subimage data!\n", prop_name);
  1728. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1729. return -ENOENT;
  1730. }
  1731. #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1732. /* perform any post-processing on the image data */
  1733. board_fit_image_post_process((void **)&buf, &size);
  1734. #endif
  1735. len = (ulong)size;
  1736. /* verify that image data is a proper FDT blob */
  1737. if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
  1738. puts("Subimage data is not a FDT");
  1739. return -ENOEXEC;
  1740. }
  1741. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1742. /*
  1743. * Work-around for eldk-4.2 which gives this warning if we try to
  1744. * cast in the unmap_sysmem() call:
  1745. * warning: initialization discards qualifiers from pointer target type
  1746. */
  1747. {
  1748. void *vbuf = (void *)buf;
  1749. data = map_to_sysmem(vbuf);
  1750. }
  1751. if (load_op == FIT_LOAD_IGNORED) {
  1752. /* Don't load */
  1753. } else if (fit_image_get_load(fit, noffset, &load)) {
  1754. if (load_op == FIT_LOAD_REQUIRED) {
  1755. printf("Can't get %s subimage load address!\n",
  1756. prop_name);
  1757. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1758. return -EBADF;
  1759. }
  1760. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1761. ulong image_start, image_end;
  1762. ulong load_end;
  1763. void *dst;
  1764. /*
  1765. * move image data to the load address,
  1766. * make sure we don't overwrite initial image
  1767. */
  1768. image_start = addr;
  1769. image_end = addr + fit_get_size(fit);
  1770. load_end = load + len;
  1771. if (image_type != IH_TYPE_KERNEL &&
  1772. load < image_end && load_end > image_start) {
  1773. printf("Error: %s overwritten\n", prop_name);
  1774. return -EXDEV;
  1775. }
  1776. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1777. prop_name, data, load);
  1778. dst = map_sysmem(load, len);
  1779. memmove(dst, buf, len);
  1780. data = load;
  1781. }
  1782. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1783. *datap = data;
  1784. *lenp = len;
  1785. if (fit_unamep)
  1786. *fit_unamep = (char *)fit_uname;
  1787. if (fit_uname_configp)
  1788. *fit_uname_configp = (char *)(fit_uname_config ? :
  1789. fit_base_uname_config);
  1790. return noffset;
  1791. }
  1792. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1793. ulong *setup_start, ulong *setup_len)
  1794. {
  1795. int noffset;
  1796. ulong addr;
  1797. ulong len;
  1798. int ret;
  1799. addr = map_to_sysmem(images->fit_hdr_os);
  1800. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1801. if (noffset < 0)
  1802. return noffset;
  1803. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1804. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1805. FIT_LOAD_REQUIRED, setup_start, &len);
  1806. return ret;
  1807. }
  1808. #ifndef USE_HOSTCC
  1809. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  1810. const char **fit_unamep, const char **fit_uname_configp,
  1811. int arch, ulong *datap, ulong *lenp)
  1812. {
  1813. int fdt_noffset, cfg_noffset, count;
  1814. const void *fit;
  1815. const char *fit_uname = NULL;
  1816. const char *fit_uname_config = NULL;
  1817. char *fit_uname_config_copy = NULL;
  1818. char *next_config = NULL;
  1819. ulong load, len;
  1820. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1821. ulong image_start, image_end;
  1822. ulong ovload, ovlen;
  1823. const char *uconfig;
  1824. const char *uname;
  1825. void *base, *ov;
  1826. int i, err, noffset, ov_noffset;
  1827. #endif
  1828. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1829. if (fit_uname_configp && *fit_uname_configp) {
  1830. fit_uname_config_copy = strdup(*fit_uname_configp);
  1831. if (!fit_uname_config_copy)
  1832. return -ENOMEM;
  1833. next_config = strchr(fit_uname_config_copy, '#');
  1834. if (next_config)
  1835. *next_config++ = '\0';
  1836. if (next_config - 1 > fit_uname_config_copy)
  1837. fit_uname_config = fit_uname_config_copy;
  1838. }
  1839. fdt_noffset = fit_image_load(images,
  1840. addr, &fit_uname, &fit_uname_config,
  1841. arch, IH_TYPE_FLATDT,
  1842. BOOTSTAGE_ID_FIT_FDT_START,
  1843. FIT_LOAD_OPTIONAL, &load, &len);
  1844. if (fdt_noffset < 0)
  1845. goto out;
  1846. debug("fit_uname=%s, fit_uname_config=%s\n",
  1847. fit_uname ? fit_uname : "<NULL>",
  1848. fit_uname_config ? fit_uname_config : "<NULL>");
  1849. fit = map_sysmem(addr, 0);
  1850. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  1851. /* single blob, or error just return as well */
  1852. count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
  1853. if (count <= 1 && !next_config)
  1854. goto out;
  1855. /* we need to apply overlays */
  1856. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1857. image_start = addr;
  1858. image_end = addr + fit_get_size(fit);
  1859. /* verify that relocation took place by load address not being in fit */
  1860. if (load >= image_start && load < image_end) {
  1861. /* check is simplified; fit load checks for overlaps */
  1862. printf("Overlayed FDT requires relocation\n");
  1863. fdt_noffset = -EBADF;
  1864. goto out;
  1865. }
  1866. base = map_sysmem(load, len);
  1867. /* apply extra configs in FIT first, followed by args */
  1868. for (i = 1; ; i++) {
  1869. if (i < count) {
  1870. noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
  1871. FIT_FDT_PROP, i);
  1872. uname = fit_get_name(fit, noffset, NULL);
  1873. uconfig = NULL;
  1874. } else {
  1875. if (!next_config)
  1876. break;
  1877. uconfig = next_config;
  1878. next_config = strchr(next_config, '#');
  1879. if (next_config)
  1880. *next_config++ = '\0';
  1881. uname = NULL;
  1882. }
  1883. debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
  1884. ov_noffset = fit_image_load(images,
  1885. addr, &uname, &uconfig,
  1886. arch, IH_TYPE_FLATDT,
  1887. BOOTSTAGE_ID_FIT_FDT_START,
  1888. FIT_LOAD_REQUIRED, &ovload, &ovlen);
  1889. if (ov_noffset < 0) {
  1890. printf("load of %s failed\n", uname);
  1891. continue;
  1892. }
  1893. debug("%s loaded at 0x%08lx len=0x%08lx\n",
  1894. uname, ovload, ovlen);
  1895. ov = map_sysmem(ovload, ovlen);
  1896. base = map_sysmem(load, len + ovlen);
  1897. err = fdt_open_into(base, base, len + ovlen);
  1898. if (err < 0) {
  1899. printf("failed on fdt_open_into\n");
  1900. fdt_noffset = err;
  1901. goto out;
  1902. }
  1903. /* the verbose method prints out messages on error */
  1904. err = fdt_overlay_apply_verbose(base, ov);
  1905. if (err < 0) {
  1906. fdt_noffset = err;
  1907. goto out;
  1908. }
  1909. fdt_pack(base);
  1910. len = fdt_totalsize(base);
  1911. }
  1912. #else
  1913. printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
  1914. fdt_noffset = -EBADF;
  1915. #endif
  1916. out:
  1917. if (datap)
  1918. *datap = load;
  1919. if (lenp)
  1920. *lenp = len;
  1921. if (fit_unamep)
  1922. *fit_unamep = fit_uname;
  1923. if (fit_uname_configp)
  1924. *fit_uname_configp = fit_uname_config;
  1925. if (fit_uname_config_copy)
  1926. free(fit_uname_config_copy);
  1927. return fdt_noffset;
  1928. }
  1929. #endif