image-fit.c 65 KB

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