image-fit.c 59 KB

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