image-fit.c 64 KB

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