image-fit.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. 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. /**
  608. * fit_image_get_load() - get load addr property for given component image node
  609. * @fit: pointer to the FIT format image header
  610. * @noffset: component image node offset
  611. * @load: pointer to the uint32_t, will hold load address
  612. *
  613. * fit_image_get_load() finds load address property in a given component
  614. * image node. If the property is found, its value is returned to the caller.
  615. *
  616. * returns:
  617. * 0, on success
  618. * -1, on failure
  619. */
  620. int fit_image_get_load(const void *fit, int noffset, ulong *load)
  621. {
  622. int len;
  623. const uint32_t *data;
  624. data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
  625. if (data == NULL) {
  626. fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
  627. return -1;
  628. }
  629. *load = uimage_to_cpu(*data);
  630. return 0;
  631. }
  632. /**
  633. * fit_image_get_entry() - get entry point address property
  634. * @fit: pointer to the FIT format image header
  635. * @noffset: component image node offset
  636. * @entry: pointer to the uint32_t, will hold entry point address
  637. *
  638. * This gets the entry point address property for a given component image
  639. * node.
  640. *
  641. * fit_image_get_entry() finds entry point address property in a given
  642. * component image node. If the property is found, its value is returned
  643. * to the caller.
  644. *
  645. * returns:
  646. * 0, on success
  647. * -1, on failure
  648. */
  649. int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
  650. {
  651. int len;
  652. const uint32_t *data;
  653. data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
  654. if (data == NULL) {
  655. fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
  656. return -1;
  657. }
  658. *entry = uimage_to_cpu(*data);
  659. return 0;
  660. }
  661. /**
  662. * fit_image_get_data - get data property and its size for a given component image node
  663. * @fit: pointer to the FIT format image header
  664. * @noffset: component image node offset
  665. * @data: double pointer to void, will hold data property's data address
  666. * @size: pointer to size_t, will hold data property's data size
  667. *
  668. * fit_image_get_data() finds data property in a given component image node.
  669. * If the property is found its data start address and size are returned to
  670. * the caller.
  671. *
  672. * returns:
  673. * 0, on success
  674. * -1, on failure
  675. */
  676. int fit_image_get_data(const void *fit, int noffset,
  677. const void **data, size_t *size)
  678. {
  679. int len;
  680. *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
  681. if (*data == NULL) {
  682. fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
  683. *size = 0;
  684. return -1;
  685. }
  686. *size = len;
  687. return 0;
  688. }
  689. /**
  690. * fit_image_hash_get_algo - get hash algorithm name
  691. * @fit: pointer to the FIT format image header
  692. * @noffset: hash node offset
  693. * @algo: double pointer to char, will hold pointer to the algorithm name
  694. *
  695. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  696. * If the property is found its data start address is returned to the caller.
  697. *
  698. * returns:
  699. * 0, on success
  700. * -1, on failure
  701. */
  702. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
  703. {
  704. int len;
  705. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  706. if (*algo == NULL) {
  707. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  708. return -1;
  709. }
  710. return 0;
  711. }
  712. /**
  713. * fit_image_hash_get_value - get hash value and length
  714. * @fit: pointer to the FIT format image header
  715. * @noffset: hash node offset
  716. * @value: double pointer to uint8_t, will hold address of a hash value data
  717. * @value_len: pointer to an int, will hold hash data length
  718. *
  719. * fit_image_hash_get_value() finds hash value property in a given hash node.
  720. * If the property is found its data start address and size are returned to
  721. * the caller.
  722. *
  723. * returns:
  724. * 0, on success
  725. * -1, on failure
  726. */
  727. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  728. int *value_len)
  729. {
  730. int len;
  731. *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
  732. if (*value == NULL) {
  733. fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
  734. *value_len = 0;
  735. return -1;
  736. }
  737. *value_len = len;
  738. return 0;
  739. }
  740. /**
  741. * fit_image_hash_get_ignore - get hash ignore flag
  742. * @fit: pointer to the FIT format image header
  743. * @noffset: hash node offset
  744. * @ignore: pointer to an int, will hold hash ignore flag
  745. *
  746. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  747. * If the property is found and non-zero, the hash algorithm is not verified by
  748. * u-boot automatically.
  749. *
  750. * returns:
  751. * 0, on ignore not found
  752. * value, on ignore found
  753. */
  754. static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  755. {
  756. int len;
  757. int *value;
  758. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  759. if (value == NULL || len != sizeof(int))
  760. *ignore = 0;
  761. else
  762. *ignore = *value;
  763. return 0;
  764. }
  765. /**
  766. * fit_set_timestamp - set node timestamp property
  767. * @fit: pointer to the FIT format image header
  768. * @noffset: node offset
  769. * @timestamp: timestamp value to be set
  770. *
  771. * fit_set_timestamp() attempts to set timestamp property in the requested
  772. * node and returns operation status to the caller.
  773. *
  774. * returns:
  775. * 0, on success
  776. * -ENOSPC if no space in device tree, -1 for other error
  777. */
  778. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  779. {
  780. uint32_t t;
  781. int ret;
  782. t = cpu_to_uimage(timestamp);
  783. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  784. sizeof(uint32_t));
  785. if (ret) {
  786. printf("Can't set '%s' property for '%s' node (%s)\n",
  787. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  788. fdt_strerror(ret));
  789. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
  790. }
  791. return 0;
  792. }
  793. /**
  794. * calculate_hash - calculate and return hash for provided input data
  795. * @data: pointer to the input data
  796. * @data_len: data length
  797. * @algo: requested hash algorithm
  798. * @value: pointer to the char, will hold hash value data (caller must
  799. * allocate enough free space)
  800. * value_len: length of the calculated hash
  801. *
  802. * calculate_hash() computes input data hash according to the requested
  803. * algorithm.
  804. * Resulting hash value is placed in caller provided 'value' buffer, length
  805. * of the calculated hash is returned via value_len pointer argument.
  806. *
  807. * returns:
  808. * 0, on success
  809. * -1, when algo is unsupported
  810. */
  811. int calculate_hash(const void *data, int data_len, const char *algo,
  812. uint8_t *value, int *value_len)
  813. {
  814. if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  815. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  816. CHUNKSZ_CRC32);
  817. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  818. *value_len = 4;
  819. } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
  820. sha1_csum_wd((unsigned char *)data, data_len,
  821. (unsigned char *)value, CHUNKSZ_SHA1);
  822. *value_len = 20;
  823. } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
  824. sha256_csum_wd((unsigned char *)data, data_len,
  825. (unsigned char *)value, CHUNKSZ_SHA256);
  826. *value_len = SHA256_SUM_LEN;
  827. } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
  828. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  829. *value_len = 16;
  830. } else {
  831. debug("Unsupported hash alogrithm\n");
  832. return -1;
  833. }
  834. return 0;
  835. }
  836. static int fit_image_check_hash(const void *fit, int noffset, const void *data,
  837. size_t size, char **err_msgp)
  838. {
  839. uint8_t value[FIT_MAX_HASH_LEN];
  840. int value_len;
  841. char *algo;
  842. uint8_t *fit_value;
  843. int fit_value_len;
  844. int ignore;
  845. *err_msgp = NULL;
  846. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  847. *err_msgp = "Can't get hash algo property";
  848. return -1;
  849. }
  850. printf("%s", algo);
  851. if (IMAGE_ENABLE_IGNORE) {
  852. fit_image_hash_get_ignore(fit, noffset, &ignore);
  853. if (ignore) {
  854. printf("-skipped ");
  855. return 0;
  856. }
  857. }
  858. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  859. &fit_value_len)) {
  860. *err_msgp = "Can't get hash value property";
  861. return -1;
  862. }
  863. if (calculate_hash(data, size, algo, value, &value_len)) {
  864. *err_msgp = "Unsupported hash algorithm";
  865. return -1;
  866. }
  867. if (value_len != fit_value_len) {
  868. *err_msgp = "Bad hash value len";
  869. return -1;
  870. } else if (memcmp(value, fit_value, value_len) != 0) {
  871. *err_msgp = "Bad hash value";
  872. return -1;
  873. }
  874. return 0;
  875. }
  876. /**
  877. * fit_image_verify - verify data intergity
  878. * @fit: pointer to the FIT format image header
  879. * @image_noffset: component image node offset
  880. *
  881. * fit_image_verify() goes over component image hash nodes,
  882. * re-calculates each data hash and compares with the value stored in hash
  883. * node.
  884. *
  885. * returns:
  886. * 1, if all hashes are valid
  887. * 0, otherwise (or on error)
  888. */
  889. int fit_image_verify(const void *fit, int image_noffset)
  890. {
  891. const void *data;
  892. size_t size;
  893. int noffset = 0;
  894. char *err_msg = "";
  895. int verify_all = 1;
  896. int ret;
  897. /* Get image data and data length */
  898. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  899. err_msg = "Can't get image data/size";
  900. goto error;
  901. }
  902. /* Verify all required signatures */
  903. if (IMAGE_ENABLE_VERIFY &&
  904. fit_image_verify_required_sigs(fit, image_noffset, data, size,
  905. gd_fdt_blob(), &verify_all)) {
  906. err_msg = "Unable to verify required signature";
  907. goto error;
  908. }
  909. /* Process all hash subnodes of the component image node */
  910. fdt_for_each_subnode(fit, noffset, image_noffset) {
  911. const char *name = fit_get_name(fit, noffset, NULL);
  912. /*
  913. * Check subnode name, must be equal to "hash".
  914. * Multiple hash nodes require unique unit node
  915. * names, e.g. hash@1, hash@2, etc.
  916. */
  917. if (!strncmp(name, FIT_HASH_NODENAME,
  918. strlen(FIT_HASH_NODENAME))) {
  919. if (fit_image_check_hash(fit, noffset, data, size,
  920. &err_msg))
  921. goto error;
  922. puts("+ ");
  923. } else if (IMAGE_ENABLE_VERIFY && verify_all &&
  924. !strncmp(name, FIT_SIG_NODENAME,
  925. strlen(FIT_SIG_NODENAME))) {
  926. ret = fit_image_check_sig(fit, noffset, data,
  927. size, -1, &err_msg);
  928. if (ret)
  929. puts("- ");
  930. else
  931. puts("+ ");
  932. }
  933. }
  934. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  935. err_msg = "Corrupted or truncated tree";
  936. goto error;
  937. }
  938. return 1;
  939. error:
  940. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  941. err_msg, fit_get_name(fit, noffset, NULL),
  942. fit_get_name(fit, image_noffset, NULL));
  943. return 0;
  944. }
  945. /**
  946. * fit_all_image_verify - verify data intergity for all images
  947. * @fit: pointer to the FIT format image header
  948. *
  949. * fit_all_image_verify() goes over all images in the FIT and
  950. * for every images checks if all it's hashes are valid.
  951. *
  952. * returns:
  953. * 1, if all hashes of all images are valid
  954. * 0, otherwise (or on error)
  955. */
  956. int fit_all_image_verify(const void *fit)
  957. {
  958. int images_noffset;
  959. int noffset;
  960. int ndepth;
  961. int count;
  962. /* Find images parent node offset */
  963. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  964. if (images_noffset < 0) {
  965. printf("Can't find images parent node '%s' (%s)\n",
  966. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  967. return 0;
  968. }
  969. /* Process all image subnodes, check hashes for each */
  970. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  971. (ulong)fit);
  972. for (ndepth = 0, count = 0,
  973. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  974. (noffset >= 0) && (ndepth > 0);
  975. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  976. if (ndepth == 1) {
  977. /*
  978. * Direct child node of the images parent node,
  979. * i.e. component image node.
  980. */
  981. printf(" Hash(es) for Image %u (%s): ", count++,
  982. fit_get_name(fit, noffset, NULL));
  983. if (!fit_image_verify(fit, noffset))
  984. return 0;
  985. printf("\n");
  986. }
  987. }
  988. return 1;
  989. }
  990. /**
  991. * fit_image_check_os - check whether image node is of a given os type
  992. * @fit: pointer to the FIT format image header
  993. * @noffset: component image node offset
  994. * @os: requested image os
  995. *
  996. * fit_image_check_os() reads image os property and compares its numeric
  997. * id with the requested os. Comparison result is returned to the caller.
  998. *
  999. * returns:
  1000. * 1 if image is of given os type
  1001. * 0 otherwise (or on error)
  1002. */
  1003. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1004. {
  1005. uint8_t image_os;
  1006. if (fit_image_get_os(fit, noffset, &image_os))
  1007. return 0;
  1008. return (os == image_os);
  1009. }
  1010. /**
  1011. * fit_image_check_arch - check whether image node is of a given arch
  1012. * @fit: pointer to the FIT format image header
  1013. * @noffset: component image node offset
  1014. * @arch: requested imagearch
  1015. *
  1016. * fit_image_check_arch() reads image arch property and compares its numeric
  1017. * id with the requested arch. Comparison result is returned to the caller.
  1018. *
  1019. * returns:
  1020. * 1 if image is of given arch
  1021. * 0 otherwise (or on error)
  1022. */
  1023. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1024. {
  1025. uint8_t image_arch;
  1026. if (fit_image_get_arch(fit, noffset, &image_arch))
  1027. return 0;
  1028. return (arch == image_arch) ||
  1029. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
  1030. }
  1031. /**
  1032. * fit_image_check_type - check whether image node is of a given type
  1033. * @fit: pointer to the FIT format image header
  1034. * @noffset: component image node offset
  1035. * @type: requested image type
  1036. *
  1037. * fit_image_check_type() reads image type property and compares its numeric
  1038. * id with the requested type. Comparison result is returned to the caller.
  1039. *
  1040. * returns:
  1041. * 1 if image is of given type
  1042. * 0 otherwise (or on error)
  1043. */
  1044. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1045. {
  1046. uint8_t image_type;
  1047. if (fit_image_get_type(fit, noffset, &image_type))
  1048. return 0;
  1049. return (type == image_type);
  1050. }
  1051. /**
  1052. * fit_image_check_comp - check whether image node uses given compression
  1053. * @fit: pointer to the FIT format image header
  1054. * @noffset: component image node offset
  1055. * @comp: requested image compression type
  1056. *
  1057. * fit_image_check_comp() reads image compression property and compares its
  1058. * numeric id with the requested compression type. Comparison result is
  1059. * returned to the caller.
  1060. *
  1061. * returns:
  1062. * 1 if image uses requested compression
  1063. * 0 otherwise (or on error)
  1064. */
  1065. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1066. {
  1067. uint8_t image_comp;
  1068. if (fit_image_get_comp(fit, noffset, &image_comp))
  1069. return 0;
  1070. return (comp == image_comp);
  1071. }
  1072. /**
  1073. * fit_check_format - sanity check FIT image format
  1074. * @fit: pointer to the FIT format image header
  1075. *
  1076. * fit_check_format() runs a basic sanity FIT image verification.
  1077. * Routine checks for mandatory properties, nodes, etc.
  1078. *
  1079. * returns:
  1080. * 1, on success
  1081. * 0, on failure
  1082. */
  1083. int fit_check_format(const void *fit)
  1084. {
  1085. /* mandatory / node 'description' property */
  1086. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1087. debug("Wrong FIT format: no description\n");
  1088. return 0;
  1089. }
  1090. if (IMAGE_ENABLE_TIMESTAMP) {
  1091. /* mandatory / node 'timestamp' property */
  1092. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1093. debug("Wrong FIT format: no timestamp\n");
  1094. return 0;
  1095. }
  1096. }
  1097. /* mandatory subimages parent '/images' node */
  1098. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1099. debug("Wrong FIT format: no images parent node\n");
  1100. return 0;
  1101. }
  1102. return 1;
  1103. }
  1104. /**
  1105. * fit_conf_find_compat
  1106. * @fit: pointer to the FIT format image header
  1107. * @fdt: pointer to the device tree to compare against
  1108. *
  1109. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1110. * most compatible with the passed in device tree.
  1111. *
  1112. * Example:
  1113. *
  1114. * / o image-tree
  1115. * |-o images
  1116. * | |-o fdt@1
  1117. * | |-o fdt@2
  1118. * |
  1119. * |-o configurations
  1120. * |-o config@1
  1121. * | |-fdt = fdt@1
  1122. * |
  1123. * |-o config@2
  1124. * |-fdt = fdt@2
  1125. *
  1126. * / o U-Boot fdt
  1127. * |-compatible = "foo,bar", "bim,bam"
  1128. *
  1129. * / o kernel fdt1
  1130. * |-compatible = "foo,bar",
  1131. *
  1132. * / o kernel fdt2
  1133. * |-compatible = "bim,bam", "baz,biz"
  1134. *
  1135. * Configuration 1 would be picked because the first string in U-Boot's
  1136. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1137. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1138. *
  1139. * returns:
  1140. * offset to the configuration to use if one was found
  1141. * -1 otherwise
  1142. */
  1143. int fit_conf_find_compat(const void *fit, const void *fdt)
  1144. {
  1145. int ndepth = 0;
  1146. int noffset, confs_noffset, images_noffset;
  1147. const void *fdt_compat;
  1148. int fdt_compat_len;
  1149. int best_match_offset = 0;
  1150. int best_match_pos = 0;
  1151. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1152. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1153. if (confs_noffset < 0 || images_noffset < 0) {
  1154. debug("Can't find configurations or images nodes.\n");
  1155. return -1;
  1156. }
  1157. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1158. if (!fdt_compat) {
  1159. debug("Fdt for comparison has no \"compatible\" property.\n");
  1160. return -1;
  1161. }
  1162. /*
  1163. * Loop over the configurations in the FIT image.
  1164. */
  1165. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1166. (noffset >= 0) && (ndepth > 0);
  1167. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1168. const void *kfdt;
  1169. const char *kfdt_name;
  1170. int kfdt_noffset;
  1171. const char *cur_fdt_compat;
  1172. int len;
  1173. size_t size;
  1174. int i;
  1175. if (ndepth > 1)
  1176. continue;
  1177. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1178. if (!kfdt_name) {
  1179. debug("No fdt property found.\n");
  1180. continue;
  1181. }
  1182. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1183. kfdt_name);
  1184. if (kfdt_noffset < 0) {
  1185. debug("No image node named \"%s\" found.\n",
  1186. kfdt_name);
  1187. continue;
  1188. }
  1189. /*
  1190. * Get a pointer to this configuration's fdt.
  1191. */
  1192. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1193. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1194. continue;
  1195. }
  1196. len = fdt_compat_len;
  1197. cur_fdt_compat = fdt_compat;
  1198. /*
  1199. * Look for a match for each U-Boot compatibility string in
  1200. * turn in this configuration's fdt.
  1201. */
  1202. for (i = 0; len > 0 &&
  1203. (!best_match_offset || best_match_pos > i); i++) {
  1204. int cur_len = strlen(cur_fdt_compat) + 1;
  1205. if (!fdt_node_check_compatible(kfdt, 0,
  1206. cur_fdt_compat)) {
  1207. best_match_offset = noffset;
  1208. best_match_pos = i;
  1209. break;
  1210. }
  1211. len -= cur_len;
  1212. cur_fdt_compat += cur_len;
  1213. }
  1214. }
  1215. if (!best_match_offset) {
  1216. debug("No match found.\n");
  1217. return -1;
  1218. }
  1219. return best_match_offset;
  1220. }
  1221. /**
  1222. * fit_conf_get_node - get node offset for configuration of a given unit name
  1223. * @fit: pointer to the FIT format image header
  1224. * @conf_uname: configuration node unit name
  1225. *
  1226. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  1227. * parant node) of a provided unit name. If configuration is found its node
  1228. * offset is returned to the caller.
  1229. *
  1230. * When NULL is provided in second argument fit_conf_get_node() will search
  1231. * for a default configuration node instead. Default configuration node unit
  1232. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1233. * node.
  1234. *
  1235. * returns:
  1236. * configuration node offset when found (>=0)
  1237. * negative number on failure (FDT_ERR_* code)
  1238. */
  1239. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1240. {
  1241. int noffset, confs_noffset;
  1242. int len;
  1243. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1244. if (confs_noffset < 0) {
  1245. debug("Can't find configurations parent node '%s' (%s)\n",
  1246. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1247. return confs_noffset;
  1248. }
  1249. if (conf_uname == NULL) {
  1250. /* get configuration unit name from the default property */
  1251. debug("No configuration specified, trying default...\n");
  1252. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1253. FIT_DEFAULT_PROP, &len);
  1254. if (conf_uname == NULL) {
  1255. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1256. len);
  1257. return len;
  1258. }
  1259. debug("Found default configuration: '%s'\n", conf_uname);
  1260. }
  1261. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1262. if (noffset < 0) {
  1263. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1264. conf_uname, fdt_strerror(noffset));
  1265. }
  1266. return noffset;
  1267. }
  1268. int fit_conf_get_prop_node(const void *fit, int noffset,
  1269. const char *prop_name)
  1270. {
  1271. char *uname;
  1272. int len;
  1273. /* get kernel image unit name from configuration kernel property */
  1274. uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
  1275. if (uname == NULL)
  1276. return len;
  1277. return fit_image_get_node(fit, uname);
  1278. }
  1279. /**
  1280. * fit_conf_print - prints out the FIT configuration details
  1281. * @fit: pointer to the FIT format image header
  1282. * @noffset: offset of the configuration node
  1283. * @p: pointer to prefix string
  1284. *
  1285. * fit_conf_print() lists all mandatory properies for the processed
  1286. * configuration node.
  1287. *
  1288. * returns:
  1289. * no returned results
  1290. */
  1291. void fit_conf_print(const void *fit, int noffset, const char *p)
  1292. {
  1293. char *desc;
  1294. char *uname;
  1295. int ret;
  1296. int loadables_index;
  1297. /* Mandatory properties */
  1298. ret = fit_get_desc(fit, noffset, &desc);
  1299. printf("%s Description: ", p);
  1300. if (ret)
  1301. printf("unavailable\n");
  1302. else
  1303. printf("%s\n", desc);
  1304. uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1305. printf("%s Kernel: ", p);
  1306. if (uname == NULL)
  1307. printf("unavailable\n");
  1308. else
  1309. printf("%s\n", uname);
  1310. /* Optional properties */
  1311. uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1312. if (uname)
  1313. printf("%s Init Ramdisk: %s\n", p, uname);
  1314. uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
  1315. if (uname)
  1316. printf("%s FDT: %s\n", p, uname);
  1317. /* Print out all of the specified loadables */
  1318. for (loadables_index = 0;
  1319. fdt_get_string_index(fit, noffset,
  1320. FIT_LOADABLE_PROP,
  1321. loadables_index,
  1322. (const char **)&uname) == 0;
  1323. loadables_index++)
  1324. {
  1325. if (loadables_index == 0) {
  1326. printf("%s Loadables: ", p);
  1327. } else {
  1328. printf("%s ", p);
  1329. }
  1330. printf("%s\n", uname);
  1331. }
  1332. }
  1333. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1334. {
  1335. fit_image_print(fit, rd_noffset, " ");
  1336. if (verify) {
  1337. puts(" Verifying Hash Integrity ... ");
  1338. if (!fit_image_verify(fit, rd_noffset)) {
  1339. puts("Bad Data Hash\n");
  1340. return -EACCES;
  1341. }
  1342. puts("OK\n");
  1343. }
  1344. return 0;
  1345. }
  1346. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1347. ulong addr)
  1348. {
  1349. int cfg_noffset;
  1350. void *fit_hdr;
  1351. int noffset;
  1352. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1353. prop_name, images->fit_uname_cfg, addr);
  1354. /* Check whether configuration has this property defined */
  1355. fit_hdr = map_sysmem(addr, 0);
  1356. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1357. if (cfg_noffset < 0) {
  1358. debug("* %s: no such config\n", prop_name);
  1359. return -ENOENT;
  1360. }
  1361. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1362. if (noffset < 0) {
  1363. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1364. return -ENOLINK;
  1365. }
  1366. return noffset;
  1367. }
  1368. /**
  1369. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1370. *
  1371. * @return the properly name where we expect to find the image in the
  1372. * config node
  1373. */
  1374. static const char *fit_get_image_type_property(int type)
  1375. {
  1376. /*
  1377. * This is sort-of available in the uimage_type[] table in image.c
  1378. * but we don't have access to the sohrt name, and "fdt" is different
  1379. * anyway. So let's just keep it here.
  1380. */
  1381. switch (type) {
  1382. case IH_TYPE_FLATDT:
  1383. return FIT_FDT_PROP;
  1384. case IH_TYPE_KERNEL:
  1385. return FIT_KERNEL_PROP;
  1386. case IH_TYPE_RAMDISK:
  1387. return FIT_RAMDISK_PROP;
  1388. case IH_TYPE_X86_SETUP:
  1389. return FIT_SETUP_PROP;
  1390. case IH_TYPE_LOADABLE:
  1391. return FIT_LOADABLE_PROP;
  1392. }
  1393. return "unknown";
  1394. }
  1395. int fit_image_load(bootm_headers_t *images, ulong addr,
  1396. const char **fit_unamep, const char **fit_uname_configp,
  1397. int arch, int image_type, int bootstage_id,
  1398. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1399. {
  1400. int cfg_noffset, noffset;
  1401. const char *fit_uname;
  1402. const char *fit_uname_config;
  1403. const void *fit;
  1404. const void *buf;
  1405. size_t size;
  1406. int type_ok, os_ok;
  1407. ulong load, data, len;
  1408. uint8_t os;
  1409. const char *prop_name;
  1410. int ret;
  1411. fit = map_sysmem(addr, 0);
  1412. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1413. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1414. prop_name = fit_get_image_type_property(image_type);
  1415. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1416. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1417. if (!fit_check_format(fit)) {
  1418. printf("Bad FIT %s image format!\n", prop_name);
  1419. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1420. return -ENOEXEC;
  1421. }
  1422. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1423. if (fit_uname) {
  1424. /* get FIT component image node offset */
  1425. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1426. noffset = fit_image_get_node(fit, fit_uname);
  1427. } else {
  1428. /*
  1429. * no image node unit name, try to get config
  1430. * node first. If config unit node name is NULL
  1431. * fit_conf_get_node() will try to find default config node
  1432. */
  1433. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1434. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1435. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1436. } else {
  1437. cfg_noffset = fit_conf_get_node(fit,
  1438. fit_uname_config);
  1439. }
  1440. if (cfg_noffset < 0) {
  1441. puts("Could not find configuration node\n");
  1442. bootstage_error(bootstage_id +
  1443. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1444. return -ENOENT;
  1445. }
  1446. fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1447. printf(" Using '%s' configuration\n", fit_uname_config);
  1448. if (image_type == IH_TYPE_KERNEL) {
  1449. /* Remember (and possibly verify) this config */
  1450. images->fit_uname_cfg = fit_uname_config;
  1451. if (IMAGE_ENABLE_VERIFY && images->verify) {
  1452. puts(" Verifying Hash Integrity ... ");
  1453. if (fit_config_verify(fit, cfg_noffset)) {
  1454. puts("Bad Data Hash\n");
  1455. bootstage_error(bootstage_id +
  1456. BOOTSTAGE_SUB_HASH);
  1457. return -EACCES;
  1458. }
  1459. puts("OK\n");
  1460. }
  1461. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1462. }
  1463. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1464. prop_name);
  1465. fit_uname = fit_get_name(fit, noffset, NULL);
  1466. }
  1467. if (noffset < 0) {
  1468. puts("Could not find subimage node\n");
  1469. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1470. return -ENOENT;
  1471. }
  1472. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1473. ret = fit_image_select(fit, noffset, images->verify);
  1474. if (ret) {
  1475. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1476. return ret;
  1477. }
  1478. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1479. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1480. if (!fit_image_check_target_arch(fit, noffset)) {
  1481. puts("Unsupported Architecture\n");
  1482. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1483. return -ENOEXEC;
  1484. }
  1485. #endif
  1486. if (image_type == IH_TYPE_FLATDT &&
  1487. !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
  1488. puts("FDT image is compressed");
  1489. return -EPROTONOSUPPORT;
  1490. }
  1491. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1492. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1493. (image_type == IH_TYPE_KERNEL &&
  1494. fit_image_check_type(fit, noffset,
  1495. IH_TYPE_KERNEL_NOLOAD));
  1496. os_ok = image_type == IH_TYPE_FLATDT ||
  1497. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1498. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
  1499. /*
  1500. * If either of the checks fail, we should report an error, but
  1501. * if the image type is coming from the "loadables" field, we
  1502. * don't care what it is
  1503. */
  1504. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1505. fit_image_get_os(fit, noffset, &os);
  1506. printf("No %s %s %s Image\n",
  1507. genimg_get_os_name(os),
  1508. genimg_get_arch_name(arch),
  1509. genimg_get_type_name(image_type));
  1510. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1511. return -EIO;
  1512. }
  1513. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1514. /* get image data address and length */
  1515. if (fit_image_get_data(fit, noffset, &buf, &size)) {
  1516. printf("Could not find %s subimage data!\n", prop_name);
  1517. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1518. return -ENOENT;
  1519. }
  1520. len = (ulong)size;
  1521. /* verify that image data is a proper FDT blob */
  1522. if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
  1523. puts("Subimage data is not a FDT");
  1524. return -ENOEXEC;
  1525. }
  1526. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1527. /*
  1528. * Work-around for eldk-4.2 which gives this warning if we try to
  1529. * cast in the unmap_sysmem() call:
  1530. * warning: initialization discards qualifiers from pointer target type
  1531. */
  1532. {
  1533. void *vbuf = (void *)buf;
  1534. data = map_to_sysmem(vbuf);
  1535. }
  1536. if (load_op == FIT_LOAD_IGNORED) {
  1537. /* Don't load */
  1538. } else if (fit_image_get_load(fit, noffset, &load)) {
  1539. if (load_op == FIT_LOAD_REQUIRED) {
  1540. printf("Can't get %s subimage load address!\n",
  1541. prop_name);
  1542. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1543. return -EBADF;
  1544. }
  1545. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1546. ulong image_start, image_end;
  1547. ulong load_end;
  1548. void *dst;
  1549. /*
  1550. * move image data to the load address,
  1551. * make sure we don't overwrite initial image
  1552. */
  1553. image_start = addr;
  1554. image_end = addr + fit_get_size(fit);
  1555. load_end = load + len;
  1556. if (image_type != IH_TYPE_KERNEL &&
  1557. load < image_end && load_end > image_start) {
  1558. printf("Error: %s overwritten\n", prop_name);
  1559. return -EXDEV;
  1560. }
  1561. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1562. prop_name, data, load);
  1563. dst = map_sysmem(load, len);
  1564. memmove(dst, buf, len);
  1565. data = load;
  1566. }
  1567. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1568. *datap = data;
  1569. *lenp = len;
  1570. if (fit_unamep)
  1571. *fit_unamep = (char *)fit_uname;
  1572. if (fit_uname_configp)
  1573. *fit_uname_configp = (char *)fit_uname_config;
  1574. return noffset;
  1575. }
  1576. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1577. ulong *setup_start, ulong *setup_len)
  1578. {
  1579. int noffset;
  1580. ulong addr;
  1581. ulong len;
  1582. int ret;
  1583. addr = map_to_sysmem(images->fit_hdr_os);
  1584. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1585. if (noffset < 0)
  1586. return noffset;
  1587. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1588. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1589. FIT_LOAD_REQUIRED, setup_start, &len);
  1590. return ret;
  1591. }