image-fit.c 61 KB

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