image-fit.c 59 KB

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