image-fit.c 62 KB

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