image-fit.c 48 KB

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