image-fit.c 59 KB

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