image-fit.c 49 KB

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