image-fit.c 49 KB

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