image-fit.c 45 KB

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