image-fit.c 57 KB

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