image-fit.c 64 KB

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