image-fit.c 61 KB

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