image-fit.c 61 KB

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