image-fit.c 60 KB

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