image-fit.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #ifdef USE_HOSTCC
  28. #include "mkimage.h"
  29. #include <image.h>
  30. #include <time.h>
  31. #else
  32. #include <common.h>
  33. #endif /* !USE_HOSTCC*/
  34. #include <bootstage.h>
  35. #include <sha1.h>
  36. #include <u-boot/crc.h>
  37. #include <u-boot/md5.h>
  38. /*****************************************************************************/
  39. /* New uImage format routines */
  40. /*****************************************************************************/
  41. #ifndef USE_HOSTCC
  42. static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
  43. ulong *addr, const char **name)
  44. {
  45. const char *sep;
  46. *addr = addr_curr;
  47. *name = NULL;
  48. sep = strchr(spec, sepc);
  49. if (sep) {
  50. if (sep - spec > 0)
  51. *addr = simple_strtoul(spec, NULL, 16);
  52. *name = sep + 1;
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. /**
  58. * fit_parse_conf - parse FIT configuration spec
  59. * @spec: input string, containing configuration spec
  60. * @add_curr: current image address (to be used as a possible default)
  61. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  62. * configuration
  63. * @conf_name double pointer to a char, will hold pointer to a configuration
  64. * unit name
  65. *
  66. * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
  67. * where <addr> is a FIT image address that contains configuration
  68. * with a <conf> unit name.
  69. *
  70. * Address part is optional, and if omitted default add_curr will
  71. * be used instead.
  72. *
  73. * returns:
  74. * 1 if spec is a valid configuration string,
  75. * addr and conf_name are set accordingly
  76. * 0 otherwise
  77. */
  78. int fit_parse_conf(const char *spec, ulong addr_curr,
  79. ulong *addr, const char **conf_name)
  80. {
  81. return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
  82. }
  83. /**
  84. * fit_parse_subimage - parse FIT subimage spec
  85. * @spec: input string, containing subimage spec
  86. * @add_curr: current image address (to be used as a possible default)
  87. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  88. * subimage
  89. * @image_name: double pointer to a char, will hold pointer to a subimage name
  90. *
  91. * fit_parse_subimage() expects subimage spec in the for of
  92. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  93. * subimage with a <subimg> unit name.
  94. *
  95. * Address part is optional, and if omitted default add_curr will
  96. * be used instead.
  97. *
  98. * returns:
  99. * 1 if spec is a valid subimage string,
  100. * addr and image_name are set accordingly
  101. * 0 otherwise
  102. */
  103. int fit_parse_subimage(const char *spec, ulong addr_curr,
  104. ulong *addr, const char **image_name)
  105. {
  106. return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
  107. }
  108. #endif /* !USE_HOSTCC */
  109. static void fit_get_debug(const void *fit, int noffset,
  110. char *prop_name, int err)
  111. {
  112. debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
  113. prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
  114. fdt_strerror(err));
  115. }
  116. /**
  117. * fit_print_contents - prints out the contents of the FIT format image
  118. * @fit: pointer to the FIT format image header
  119. * @p: pointer to prefix string
  120. *
  121. * fit_print_contents() formats a multi line FIT image contents description.
  122. * The routine prints out FIT image properties (root node level) follwed by
  123. * the details of each component image.
  124. *
  125. * returns:
  126. * no returned results
  127. */
  128. void fit_print_contents(const void *fit)
  129. {
  130. char *desc;
  131. char *uname;
  132. int images_noffset;
  133. int confs_noffset;
  134. int noffset;
  135. int ndepth;
  136. int count = 0;
  137. int ret;
  138. const char *p;
  139. time_t timestamp;
  140. #ifdef USE_HOSTCC
  141. p = "";
  142. #else
  143. p = " ";
  144. #endif
  145. /* Root node properties */
  146. ret = fit_get_desc(fit, 0, &desc);
  147. printf("%sFIT description: ", p);
  148. if (ret)
  149. printf("unavailable\n");
  150. else
  151. printf("%s\n", desc);
  152. if (IMAGE_ENABLE_TIMESTAMP) {
  153. ret = fit_get_timestamp(fit, 0, &timestamp);
  154. printf("%sCreated: ", p);
  155. if (ret)
  156. printf("unavailable\n");
  157. else
  158. genimg_print_time(timestamp);
  159. }
  160. /* Find images parent node offset */
  161. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  162. if (images_noffset < 0) {
  163. printf("Can't find images parent node '%s' (%s)\n",
  164. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  165. return;
  166. }
  167. /* Process its subnodes, print out component images details */
  168. for (ndepth = 0, count = 0,
  169. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  170. (noffset >= 0) && (ndepth > 0);
  171. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  172. if (ndepth == 1) {
  173. /*
  174. * Direct child node of the images parent node,
  175. * i.e. component image node.
  176. */
  177. printf("%s Image %u (%s)\n", p, count++,
  178. fit_get_name(fit, noffset, NULL));
  179. fit_image_print(fit, noffset, p);
  180. }
  181. }
  182. /* Find configurations parent node offset */
  183. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  184. if (confs_noffset < 0) {
  185. debug("Can't get configurations parent node '%s' (%s)\n",
  186. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  187. return;
  188. }
  189. /* get default configuration unit name from default property */
  190. uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
  191. if (uname)
  192. printf("%s Default Configuration: '%s'\n", p, uname);
  193. /* Process its subnodes, print out configurations details */
  194. for (ndepth = 0, count = 0,
  195. noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  196. (noffset >= 0) && (ndepth > 0);
  197. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  198. if (ndepth == 1) {
  199. /*
  200. * Direct child node of the configurations parent node,
  201. * i.e. configuration node.
  202. */
  203. printf("%s Configuration %u (%s)\n", p, count++,
  204. fit_get_name(fit, noffset, NULL));
  205. fit_conf_print(fit, noffset, p);
  206. }
  207. }
  208. }
  209. /**
  210. * fit_image_print - prints out the FIT component image details
  211. * @fit: pointer to the FIT format image header
  212. * @image_noffset: offset of the component image node
  213. * @p: pointer to prefix string
  214. *
  215. * fit_image_print() lists all mandatory properies for the processed component
  216. * image. If present, hash nodes are printed out as well. Load
  217. * address for images of type firmware is also printed out. Since the load
  218. * address is not mandatory for firmware images, it will be output as
  219. * "unavailable" when not present.
  220. *
  221. * returns:
  222. * no returned results
  223. */
  224. void fit_image_print(const void *fit, int image_noffset, const char *p)
  225. {
  226. char *desc;
  227. uint8_t type, arch, os, comp;
  228. size_t size;
  229. ulong load, entry;
  230. const void *data;
  231. int noffset;
  232. int ndepth;
  233. int ret;
  234. /* Mandatory properties */
  235. ret = fit_get_desc(fit, image_noffset, &desc);
  236. printf("%s Description: ", p);
  237. if (ret)
  238. printf("unavailable\n");
  239. else
  240. printf("%s\n", desc);
  241. fit_image_get_type(fit, image_noffset, &type);
  242. printf("%s Type: %s\n", p, genimg_get_type_name(type));
  243. fit_image_get_comp(fit, image_noffset, &comp);
  244. printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
  245. ret = fit_image_get_data(fit, image_noffset, &data, &size);
  246. #ifndef USE_HOSTCC
  247. printf("%s Data Start: ", p);
  248. if (ret)
  249. printf("unavailable\n");
  250. else
  251. printf("0x%08lx\n", (ulong)data);
  252. #endif
  253. printf("%s Data Size: ", p);
  254. if (ret)
  255. printf("unavailable\n");
  256. else
  257. genimg_print_size(size);
  258. /* Remaining, type dependent properties */
  259. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  260. (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
  261. (type == IH_TYPE_FLATDT)) {
  262. fit_image_get_arch(fit, image_noffset, &arch);
  263. printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
  264. }
  265. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK)) {
  266. fit_image_get_os(fit, image_noffset, &os);
  267. printf("%s OS: %s\n", p, genimg_get_os_name(os));
  268. }
  269. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  270. (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK)) {
  271. ret = fit_image_get_load(fit, image_noffset, &load);
  272. printf("%s Load Address: ", p);
  273. if (ret)
  274. printf("unavailable\n");
  275. else
  276. printf("0x%08lx\n", load);
  277. }
  278. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  279. (type == IH_TYPE_RAMDISK)) {
  280. fit_image_get_entry(fit, image_noffset, &entry);
  281. printf("%s Entry Point: ", p);
  282. if (ret)
  283. printf("unavailable\n");
  284. else
  285. printf("0x%08lx\n", entry);
  286. }
  287. /* Process all hash subnodes of the component image node */
  288. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  289. (noffset >= 0) && (ndepth > 0);
  290. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  291. if (ndepth == 1) {
  292. /* Direct child node of the component image node */
  293. fit_image_print_hash(fit, noffset, p);
  294. }
  295. }
  296. }
  297. /**
  298. * fit_image_print_hash - prints out the hash node details
  299. * @fit: pointer to the FIT format image header
  300. * @noffset: offset of the hash node
  301. * @p: pointer to prefix string
  302. *
  303. * fit_image_print_hash() lists properies for the processed hash node
  304. *
  305. * returns:
  306. * no returned results
  307. */
  308. void fit_image_print_hash(const void *fit, int noffset, const char *p)
  309. {
  310. char *algo;
  311. uint8_t *value;
  312. int value_len;
  313. int i, ret;
  314. /*
  315. * Check subnode name, must be equal to "hash".
  316. * Multiple hash nodes require unique unit node
  317. * names, e.g. hash@1, hash@2, etc.
  318. */
  319. if (strncmp(fit_get_name(fit, noffset, NULL),
  320. FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME)) != 0)
  321. return;
  322. debug("%s Hash node: '%s'\n", p,
  323. fit_get_name(fit, noffset, NULL));
  324. printf("%s Hash algo: ", p);
  325. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  326. printf("invalid/unsupported\n");
  327. return;
  328. }
  329. printf("%s\n", algo);
  330. ret = fit_image_hash_get_value(fit, noffset, &value,
  331. &value_len);
  332. printf("%s Hash value: ", p);
  333. if (ret) {
  334. printf("unavailable\n");
  335. } else {
  336. for (i = 0; i < value_len; i++)
  337. printf("%02x", value[i]);
  338. printf("\n");
  339. }
  340. debug("%s Hash len: %d\n", p, value_len);
  341. }
  342. /**
  343. * fit_get_desc - get node description property
  344. * @fit: pointer to the FIT format image header
  345. * @noffset: node offset
  346. * @desc: double pointer to the char, will hold pointer to the descrption
  347. *
  348. * fit_get_desc() reads description property from a given node, if
  349. * description is found pointer to it is returened in third call argument.
  350. *
  351. * returns:
  352. * 0, on success
  353. * -1, on failure
  354. */
  355. int fit_get_desc(const void *fit, int noffset, char **desc)
  356. {
  357. int len;
  358. *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
  359. if (*desc == NULL) {
  360. fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. /**
  366. * fit_get_timestamp - get node timestamp property
  367. * @fit: pointer to the FIT format image header
  368. * @noffset: node offset
  369. * @timestamp: pointer to the time_t, will hold read timestamp
  370. *
  371. * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
  372. * is found and has a correct size its value is retured in third call
  373. * argument.
  374. *
  375. * returns:
  376. * 0, on success
  377. * -1, on property read failure
  378. * -2, on wrong timestamp size
  379. */
  380. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
  381. {
  382. int len;
  383. const void *data;
  384. data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
  385. if (data == NULL) {
  386. fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
  387. return -1;
  388. }
  389. if (len != sizeof(uint32_t)) {
  390. debug("FIT timestamp with incorrect size of (%u)\n", len);
  391. return -2;
  392. }
  393. *timestamp = uimage_to_cpu(*((uint32_t *)data));
  394. return 0;
  395. }
  396. /**
  397. * fit_image_get_node - get node offset for component image of a given unit name
  398. * @fit: pointer to the FIT format image header
  399. * @image_uname: component image node unit name
  400. *
  401. * fit_image_get_node() finds a component image (withing the '/images'
  402. * node) of a provided unit name. If image is found its node offset is
  403. * returned to the caller.
  404. *
  405. * returns:
  406. * image node offset when found (>=0)
  407. * negative number on failure (FDT_ERR_* code)
  408. */
  409. int fit_image_get_node(const void *fit, const char *image_uname)
  410. {
  411. int noffset, images_noffset;
  412. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  413. if (images_noffset < 0) {
  414. debug("Can't find images parent node '%s' (%s)\n",
  415. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  416. return images_noffset;
  417. }
  418. noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
  419. if (noffset < 0) {
  420. debug("Can't get node offset for image unit name: '%s' (%s)\n",
  421. image_uname, fdt_strerror(noffset));
  422. }
  423. return noffset;
  424. }
  425. /**
  426. * fit_image_get_os - get os id for a given component image node
  427. * @fit: pointer to the FIT format image header
  428. * @noffset: component image node offset
  429. * @os: pointer to the uint8_t, will hold os numeric id
  430. *
  431. * fit_image_get_os() finds os property in a given component image node.
  432. * If the property is found, its (string) value is translated to the numeric
  433. * id which is returned to the caller.
  434. *
  435. * returns:
  436. * 0, on success
  437. * -1, on failure
  438. */
  439. int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  440. {
  441. int len;
  442. const void *data;
  443. /* Get OS name from property data */
  444. data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
  445. if (data == NULL) {
  446. fit_get_debug(fit, noffset, FIT_OS_PROP, len);
  447. *os = -1;
  448. return -1;
  449. }
  450. /* Translate OS name to id */
  451. *os = genimg_get_os_id(data);
  452. return 0;
  453. }
  454. /**
  455. * fit_image_get_arch - get arch id for a given component image node
  456. * @fit: pointer to the FIT format image header
  457. * @noffset: component image node offset
  458. * @arch: pointer to the uint8_t, will hold arch numeric id
  459. *
  460. * fit_image_get_arch() finds arch property in a given component image node.
  461. * If the property is found, its (string) value is translated to the numeric
  462. * id which is returned to the caller.
  463. *
  464. * returns:
  465. * 0, on success
  466. * -1, on failure
  467. */
  468. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
  469. {
  470. int len;
  471. const void *data;
  472. /* Get architecture name from property data */
  473. data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
  474. if (data == NULL) {
  475. fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
  476. *arch = -1;
  477. return -1;
  478. }
  479. /* Translate architecture name to id */
  480. *arch = genimg_get_arch_id(data);
  481. return 0;
  482. }
  483. /**
  484. * fit_image_get_type - get type id for a given component image node
  485. * @fit: pointer to the FIT format image header
  486. * @noffset: component image node offset
  487. * @type: pointer to the uint8_t, will hold type numeric id
  488. *
  489. * fit_image_get_type() finds type property in a given component image node.
  490. * If the property is found, its (string) value is translated to the numeric
  491. * id which is returned to the caller.
  492. *
  493. * returns:
  494. * 0, on success
  495. * -1, on failure
  496. */
  497. int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
  498. {
  499. int len;
  500. const void *data;
  501. /* Get image type name from property data */
  502. data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
  503. if (data == NULL) {
  504. fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
  505. *type = -1;
  506. return -1;
  507. }
  508. /* Translate image type name to id */
  509. *type = genimg_get_type_id(data);
  510. return 0;
  511. }
  512. /**
  513. * fit_image_get_comp - get comp id for a given component image node
  514. * @fit: pointer to the FIT format image header
  515. * @noffset: component image node offset
  516. * @comp: pointer to the uint8_t, will hold comp numeric id
  517. *
  518. * fit_image_get_comp() finds comp property in a given component image node.
  519. * If the property is found, its (string) value is translated to the numeric
  520. * id which is returned to the caller.
  521. *
  522. * returns:
  523. * 0, on success
  524. * -1, on failure
  525. */
  526. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
  527. {
  528. int len;
  529. const void *data;
  530. /* Get compression name from property data */
  531. data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
  532. if (data == NULL) {
  533. fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
  534. *comp = -1;
  535. return -1;
  536. }
  537. /* Translate compression name to id */
  538. *comp = genimg_get_comp_id(data);
  539. return 0;
  540. }
  541. /**
  542. * fit_image_get_load() - get load addr property for given component image node
  543. * @fit: pointer to the FIT format image header
  544. * @noffset: component image node offset
  545. * @load: pointer to the uint32_t, will hold load address
  546. *
  547. * fit_image_get_load() finds load address property in a given component
  548. * image node. If the property is found, its value is returned to the caller.
  549. *
  550. * returns:
  551. * 0, on success
  552. * -1, on failure
  553. */
  554. int fit_image_get_load(const void *fit, int noffset, ulong *load)
  555. {
  556. int len;
  557. const uint32_t *data;
  558. data = fdt_getprop(fit, noffset, FIT_LOAD_PROP, &len);
  559. if (data == NULL) {
  560. fit_get_debug(fit, noffset, FIT_LOAD_PROP, len);
  561. return -1;
  562. }
  563. *load = uimage_to_cpu(*data);
  564. return 0;
  565. }
  566. /**
  567. * fit_image_get_entry() - get entry point address property
  568. * @fit: pointer to the FIT format image header
  569. * @noffset: component image node offset
  570. * @entry: pointer to the uint32_t, will hold entry point address
  571. *
  572. * This gets the entry point address property for a given component image
  573. * node.
  574. *
  575. * fit_image_get_entry() finds entry point address property in a given
  576. * component image node. If the property is found, its value is returned
  577. * to the caller.
  578. *
  579. * returns:
  580. * 0, on success
  581. * -1, on failure
  582. */
  583. int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
  584. {
  585. int len;
  586. const uint32_t *data;
  587. data = fdt_getprop(fit, noffset, FIT_ENTRY_PROP, &len);
  588. if (data == NULL) {
  589. fit_get_debug(fit, noffset, FIT_ENTRY_PROP, len);
  590. return -1;
  591. }
  592. *entry = uimage_to_cpu(*data);
  593. return 0;
  594. }
  595. /**
  596. * fit_image_get_data - get data property and its size for a given component image node
  597. * @fit: pointer to the FIT format image header
  598. * @noffset: component image node offset
  599. * @data: double pointer to void, will hold data property's data address
  600. * @size: pointer to size_t, will hold data property's data size
  601. *
  602. * fit_image_get_data() finds data property in a given component image node.
  603. * If the property is found its data start address and size are returned to
  604. * the caller.
  605. *
  606. * returns:
  607. * 0, on success
  608. * -1, on failure
  609. */
  610. int fit_image_get_data(const void *fit, int noffset,
  611. const void **data, size_t *size)
  612. {
  613. int len;
  614. *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
  615. if (*data == NULL) {
  616. fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
  617. *size = 0;
  618. return -1;
  619. }
  620. *size = len;
  621. return 0;
  622. }
  623. /**
  624. * fit_image_hash_get_algo - get hash algorithm name
  625. * @fit: pointer to the FIT format image header
  626. * @noffset: hash node offset
  627. * @algo: double pointer to char, will hold pointer to the algorithm name
  628. *
  629. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  630. * If the property is found its data start address is returned to the caller.
  631. *
  632. * returns:
  633. * 0, on success
  634. * -1, on failure
  635. */
  636. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
  637. {
  638. int len;
  639. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  640. if (*algo == NULL) {
  641. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  642. return -1;
  643. }
  644. return 0;
  645. }
  646. /**
  647. * fit_image_hash_get_value - get hash value and length
  648. * @fit: pointer to the FIT format image header
  649. * @noffset: hash node offset
  650. * @value: double pointer to uint8_t, will hold address of a hash value data
  651. * @value_len: pointer to an int, will hold hash data length
  652. *
  653. * fit_image_hash_get_value() finds hash value property in a given hash node.
  654. * If the property is found its data start address and size are returned to
  655. * the caller.
  656. *
  657. * returns:
  658. * 0, on success
  659. * -1, on failure
  660. */
  661. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  662. int *value_len)
  663. {
  664. int len;
  665. *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
  666. if (*value == NULL) {
  667. fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
  668. *value_len = 0;
  669. return -1;
  670. }
  671. *value_len = len;
  672. return 0;
  673. }
  674. #ifndef USE_HOSTCC
  675. /**
  676. * fit_image_hash_get_ignore - get hash ignore flag
  677. * @fit: pointer to the FIT format image header
  678. * @noffset: hash node offset
  679. * @ignore: pointer to an int, will hold hash ignore flag
  680. *
  681. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  682. * If the property is found and non-zero, the hash algorithm is not verified by
  683. * u-boot automatically.
  684. *
  685. * returns:
  686. * 0, on ignore not found
  687. * value, on ignore found
  688. */
  689. int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  690. {
  691. int len;
  692. int *value;
  693. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  694. if (value == NULL || len != sizeof(int))
  695. *ignore = 0;
  696. else
  697. *ignore = *value;
  698. return 0;
  699. }
  700. #endif
  701. /**
  702. * fit_set_timestamp - set node timestamp property
  703. * @fit: pointer to the FIT format image header
  704. * @noffset: node offset
  705. * @timestamp: timestamp value to be set
  706. *
  707. * fit_set_timestamp() attempts to set timestamp property in the requested
  708. * node and returns operation status to the caller.
  709. *
  710. * returns:
  711. * 0, on success
  712. * -1, on property read failure
  713. */
  714. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  715. {
  716. uint32_t t;
  717. int ret;
  718. t = cpu_to_uimage(timestamp);
  719. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  720. sizeof(uint32_t));
  721. if (ret) {
  722. printf("Can't set '%s' property for '%s' node (%s)\n",
  723. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  724. fdt_strerror(ret));
  725. return -1;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * calculate_hash - calculate and return hash for provided input data
  731. * @data: pointer to the input data
  732. * @data_len: data length
  733. * @algo: requested hash algorithm
  734. * @value: pointer to the char, will hold hash value data (caller must
  735. * allocate enough free space)
  736. * value_len: length of the calculated hash
  737. *
  738. * calculate_hash() computes input data hash according to the requested
  739. * algorithm.
  740. * Resulting hash value is placed in caller provided 'value' buffer, length
  741. * of the calculated hash is returned via value_len pointer argument.
  742. *
  743. * returns:
  744. * 0, on success
  745. * -1, when algo is unsupported
  746. */
  747. int calculate_hash(const void *data, int data_len, const char *algo,
  748. uint8_t *value, int *value_len)
  749. {
  750. if (strcmp(algo, "crc32") == 0) {
  751. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  752. CHUNKSZ_CRC32);
  753. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  754. *value_len = 4;
  755. } else if (strcmp(algo, "sha1") == 0) {
  756. sha1_csum_wd((unsigned char *)data, data_len,
  757. (unsigned char *)value, CHUNKSZ_SHA1);
  758. *value_len = 20;
  759. } else if (strcmp(algo, "md5") == 0) {
  760. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  761. *value_len = 16;
  762. } else {
  763. debug("Unsupported hash alogrithm\n");
  764. return -1;
  765. }
  766. return 0;
  767. }
  768. /**
  769. * fit_image_check_hashes - verify data intergity
  770. * @fit: pointer to the FIT format image header
  771. * @image_noffset: component image node offset
  772. *
  773. * fit_image_check_hashes() goes over component image hash nodes,
  774. * re-calculates each data hash and compares with the value stored in hash
  775. * node.
  776. *
  777. * returns:
  778. * 1, if all hashes are valid
  779. * 0, otherwise (or on error)
  780. */
  781. int fit_image_check_hashes(const void *fit, int image_noffset)
  782. {
  783. const void *data;
  784. size_t size;
  785. char *algo;
  786. uint8_t *fit_value;
  787. int fit_value_len;
  788. #ifndef USE_HOSTCC
  789. int ignore;
  790. #endif
  791. uint8_t value[FIT_MAX_HASH_LEN];
  792. int value_len;
  793. int noffset;
  794. int ndepth;
  795. char *err_msg = "";
  796. /* Get image data and data length */
  797. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  798. printf("Can't get image data/size\n");
  799. return 0;
  800. }
  801. /* Process all hash subnodes of the component image node */
  802. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  803. (noffset >= 0) && (ndepth > 0);
  804. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  805. if (ndepth == 1) {
  806. /* Direct child node of the component image node */
  807. /*
  808. * Check subnode name, must be equal to "hash".
  809. * Multiple hash nodes require unique unit node
  810. * names, e.g. hash@1, hash@2, etc.
  811. */
  812. if (strncmp(fit_get_name(fit, noffset, NULL),
  813. FIT_HASH_NODENAME,
  814. strlen(FIT_HASH_NODENAME)) != 0)
  815. continue;
  816. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  817. err_msg = " error!\nCan't get hash algo property";
  818. goto error;
  819. }
  820. printf("%s", algo);
  821. #ifndef USE_HOSTCC
  822. fit_image_hash_get_ignore(fit, noffset, &ignore);
  823. if (ignore) {
  824. printf("-skipped ");
  825. continue;
  826. }
  827. #endif
  828. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  829. &fit_value_len)) {
  830. err_msg = " error!\nCan't get hash value "
  831. "property";
  832. goto error;
  833. }
  834. if (calculate_hash(data, size, algo, value,
  835. &value_len)) {
  836. err_msg = " error!\n"
  837. "Unsupported hash algorithm";
  838. goto error;
  839. }
  840. if (value_len != fit_value_len) {
  841. err_msg = " error !\nBad hash value len";
  842. goto error;
  843. } else if (memcmp(value, fit_value, value_len) != 0) {
  844. err_msg = " error!\nBad hash value";
  845. goto error;
  846. }
  847. printf("+ ");
  848. }
  849. }
  850. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  851. err_msg = " error!\nCorrupted or truncated tree";
  852. goto error;
  853. }
  854. return 1;
  855. error:
  856. printf("%s for '%s' hash node in '%s' image node\n",
  857. err_msg, fit_get_name(fit, noffset, NULL),
  858. fit_get_name(fit, image_noffset, NULL));
  859. return 0;
  860. }
  861. /**
  862. * fit_all_image_check_hashes - verify data intergity for all images
  863. * @fit: pointer to the FIT format image header
  864. *
  865. * fit_all_image_check_hashes() goes over all images in the FIT and
  866. * for every images checks if all it's hashes are valid.
  867. *
  868. * returns:
  869. * 1, if all hashes of all images are valid
  870. * 0, otherwise (or on error)
  871. */
  872. int fit_all_image_check_hashes(const void *fit)
  873. {
  874. int images_noffset;
  875. int noffset;
  876. int ndepth;
  877. int count;
  878. /* Find images parent node offset */
  879. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  880. if (images_noffset < 0) {
  881. printf("Can't find images parent node '%s' (%s)\n",
  882. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  883. return 0;
  884. }
  885. /* Process all image subnodes, check hashes for each */
  886. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  887. (ulong)fit);
  888. for (ndepth = 0, count = 0,
  889. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  890. (noffset >= 0) && (ndepth > 0);
  891. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  892. if (ndepth == 1) {
  893. /*
  894. * Direct child node of the images parent node,
  895. * i.e. component image node.
  896. */
  897. printf(" Hash(es) for Image %u (%s): ", count++,
  898. fit_get_name(fit, noffset, NULL));
  899. if (!fit_image_check_hashes(fit, noffset))
  900. return 0;
  901. printf("\n");
  902. }
  903. }
  904. return 1;
  905. }
  906. /**
  907. * fit_image_check_os - check whether image node is of a given os type
  908. * @fit: pointer to the FIT format image header
  909. * @noffset: component image node offset
  910. * @os: requested image os
  911. *
  912. * fit_image_check_os() reads image os property and compares its numeric
  913. * id with the requested os. Comparison result is returned to the caller.
  914. *
  915. * returns:
  916. * 1 if image is of given os type
  917. * 0 otherwise (or on error)
  918. */
  919. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  920. {
  921. uint8_t image_os;
  922. if (fit_image_get_os(fit, noffset, &image_os))
  923. return 0;
  924. return (os == image_os);
  925. }
  926. /**
  927. * fit_image_check_arch - check whether image node is of a given arch
  928. * @fit: pointer to the FIT format image header
  929. * @noffset: component image node offset
  930. * @arch: requested imagearch
  931. *
  932. * fit_image_check_arch() reads image arch property and compares its numeric
  933. * id with the requested arch. Comparison result is returned to the caller.
  934. *
  935. * returns:
  936. * 1 if image is of given arch
  937. * 0 otherwise (or on error)
  938. */
  939. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  940. {
  941. uint8_t image_arch;
  942. if (fit_image_get_arch(fit, noffset, &image_arch))
  943. return 0;
  944. return (arch == image_arch);
  945. }
  946. /**
  947. * fit_image_check_type - check whether image node is of a given type
  948. * @fit: pointer to the FIT format image header
  949. * @noffset: component image node offset
  950. * @type: requested image type
  951. *
  952. * fit_image_check_type() reads image type property and compares its numeric
  953. * id with the requested type. Comparison result is returned to the caller.
  954. *
  955. * returns:
  956. * 1 if image is of given type
  957. * 0 otherwise (or on error)
  958. */
  959. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  960. {
  961. uint8_t image_type;
  962. if (fit_image_get_type(fit, noffset, &image_type))
  963. return 0;
  964. return (type == image_type);
  965. }
  966. /**
  967. * fit_image_check_comp - check whether image node uses given compression
  968. * @fit: pointer to the FIT format image header
  969. * @noffset: component image node offset
  970. * @comp: requested image compression type
  971. *
  972. * fit_image_check_comp() reads image compression property and compares its
  973. * numeric id with the requested compression type. Comparison result is
  974. * returned to the caller.
  975. *
  976. * returns:
  977. * 1 if image uses requested compression
  978. * 0 otherwise (or on error)
  979. */
  980. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  981. {
  982. uint8_t image_comp;
  983. if (fit_image_get_comp(fit, noffset, &image_comp))
  984. return 0;
  985. return (comp == image_comp);
  986. }
  987. /**
  988. * fit_check_format - sanity check FIT image format
  989. * @fit: pointer to the FIT format image header
  990. *
  991. * fit_check_format() runs a basic sanity FIT image verification.
  992. * Routine checks for mandatory properties, nodes, etc.
  993. *
  994. * returns:
  995. * 1, on success
  996. * 0, on failure
  997. */
  998. int fit_check_format(const void *fit)
  999. {
  1000. /* mandatory / node 'description' property */
  1001. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1002. debug("Wrong FIT format: no description\n");
  1003. return 0;
  1004. }
  1005. if (IMAGE_ENABLE_TIMESTAMP) {
  1006. /* mandatory / node 'timestamp' property */
  1007. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1008. debug("Wrong FIT format: no timestamp\n");
  1009. return 0;
  1010. }
  1011. }
  1012. /* mandatory subimages parent '/images' node */
  1013. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1014. debug("Wrong FIT format: no images parent node\n");
  1015. return 0;
  1016. }
  1017. return 1;
  1018. }
  1019. /**
  1020. * fit_conf_find_compat
  1021. * @fit: pointer to the FIT format image header
  1022. * @fdt: pointer to the device tree to compare against
  1023. *
  1024. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1025. * most compatible with the passed in device tree.
  1026. *
  1027. * Example:
  1028. *
  1029. * / o image-tree
  1030. * |-o images
  1031. * | |-o fdt@1
  1032. * | |-o fdt@2
  1033. * |
  1034. * |-o configurations
  1035. * |-o config@1
  1036. * | |-fdt = fdt@1
  1037. * |
  1038. * |-o config@2
  1039. * |-fdt = fdt@2
  1040. *
  1041. * / o U-Boot fdt
  1042. * |-compatible = "foo,bar", "bim,bam"
  1043. *
  1044. * / o kernel fdt1
  1045. * |-compatible = "foo,bar",
  1046. *
  1047. * / o kernel fdt2
  1048. * |-compatible = "bim,bam", "baz,biz"
  1049. *
  1050. * Configuration 1 would be picked because the first string in U-Boot's
  1051. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1052. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1053. *
  1054. * returns:
  1055. * offset to the configuration to use if one was found
  1056. * -1 otherwise
  1057. */
  1058. int fit_conf_find_compat(const void *fit, const void *fdt)
  1059. {
  1060. int ndepth = 0;
  1061. int noffset, confs_noffset, images_noffset;
  1062. const void *fdt_compat;
  1063. int fdt_compat_len;
  1064. int best_match_offset = 0;
  1065. int best_match_pos = 0;
  1066. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1067. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1068. if (confs_noffset < 0 || images_noffset < 0) {
  1069. debug("Can't find configurations or images nodes.\n");
  1070. return -1;
  1071. }
  1072. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1073. if (!fdt_compat) {
  1074. debug("Fdt for comparison has no \"compatible\" property.\n");
  1075. return -1;
  1076. }
  1077. /*
  1078. * Loop over the configurations in the FIT image.
  1079. */
  1080. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1081. (noffset >= 0) && (ndepth > 0);
  1082. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1083. const void *kfdt;
  1084. const char *kfdt_name;
  1085. int kfdt_noffset;
  1086. const char *cur_fdt_compat;
  1087. int len;
  1088. size_t size;
  1089. int i;
  1090. if (ndepth > 1)
  1091. continue;
  1092. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1093. if (!kfdt_name) {
  1094. debug("No fdt property found.\n");
  1095. continue;
  1096. }
  1097. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1098. kfdt_name);
  1099. if (kfdt_noffset < 0) {
  1100. debug("No image node named \"%s\" found.\n",
  1101. kfdt_name);
  1102. continue;
  1103. }
  1104. /*
  1105. * Get a pointer to this configuration's fdt.
  1106. */
  1107. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1108. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1109. continue;
  1110. }
  1111. len = fdt_compat_len;
  1112. cur_fdt_compat = fdt_compat;
  1113. /*
  1114. * Look for a match for each U-Boot compatibility string in
  1115. * turn in this configuration's fdt.
  1116. */
  1117. for (i = 0; len > 0 &&
  1118. (!best_match_offset || best_match_pos > i); i++) {
  1119. int cur_len = strlen(cur_fdt_compat) + 1;
  1120. if (!fdt_node_check_compatible(kfdt, 0,
  1121. cur_fdt_compat)) {
  1122. best_match_offset = noffset;
  1123. best_match_pos = i;
  1124. break;
  1125. }
  1126. len -= cur_len;
  1127. cur_fdt_compat += cur_len;
  1128. }
  1129. }
  1130. if (!best_match_offset) {
  1131. debug("No match found.\n");
  1132. return -1;
  1133. }
  1134. return best_match_offset;
  1135. }
  1136. /**
  1137. * fit_conf_get_node - get node offset for configuration of a given unit name
  1138. * @fit: pointer to the FIT format image header
  1139. * @conf_uname: configuration node unit name
  1140. *
  1141. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  1142. * parant node) of a provided unit name. If configuration is found its node
  1143. * offset is returned to the caller.
  1144. *
  1145. * When NULL is provided in second argument fit_conf_get_node() will search
  1146. * for a default configuration node instead. Default configuration node unit
  1147. * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
  1148. * node.
  1149. *
  1150. * returns:
  1151. * configuration node offset when found (>=0)
  1152. * negative number on failure (FDT_ERR_* code)
  1153. */
  1154. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1155. {
  1156. int noffset, confs_noffset;
  1157. int len;
  1158. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1159. if (confs_noffset < 0) {
  1160. debug("Can't find configurations parent node '%s' (%s)\n",
  1161. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1162. return confs_noffset;
  1163. }
  1164. if (conf_uname == NULL) {
  1165. /* get configuration unit name from the default property */
  1166. debug("No configuration specified, trying default...\n");
  1167. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1168. FIT_DEFAULT_PROP, &len);
  1169. if (conf_uname == NULL) {
  1170. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1171. len);
  1172. return len;
  1173. }
  1174. debug("Found default configuration: '%s'\n", conf_uname);
  1175. }
  1176. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1177. if (noffset < 0) {
  1178. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1179. conf_uname, fdt_strerror(noffset));
  1180. }
  1181. return noffset;
  1182. }
  1183. static int __fit_conf_get_prop_node(const void *fit, int noffset,
  1184. const char *prop_name)
  1185. {
  1186. char *uname;
  1187. int len;
  1188. /* get kernel image unit name from configuration kernel property */
  1189. uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
  1190. if (uname == NULL)
  1191. return len;
  1192. return fit_image_get_node(fit, uname);
  1193. }
  1194. /**
  1195. * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
  1196. * a given configuration
  1197. * @fit: pointer to the FIT format image header
  1198. * @noffset: configuration node offset
  1199. *
  1200. * fit_conf_get_kernel_node() retrives kernel image node unit name from
  1201. * configuration FIT_KERNEL_PROP property and translates it to the node
  1202. * offset.
  1203. *
  1204. * returns:
  1205. * image node offset when found (>=0)
  1206. * negative number on failure (FDT_ERR_* code)
  1207. */
  1208. int fit_conf_get_kernel_node(const void *fit, int noffset)
  1209. {
  1210. return __fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
  1211. }
  1212. /**
  1213. * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
  1214. * a given configuration
  1215. * @fit: pointer to the FIT format image header
  1216. * @noffset: configuration node offset
  1217. *
  1218. * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
  1219. * configuration FIT_KERNEL_PROP property and translates it to the node
  1220. * offset.
  1221. *
  1222. * returns:
  1223. * image node offset when found (>=0)
  1224. * negative number on failure (FDT_ERR_* code)
  1225. */
  1226. int fit_conf_get_ramdisk_node(const void *fit, int noffset)
  1227. {
  1228. return __fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
  1229. }
  1230. /**
  1231. * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
  1232. * a given configuration
  1233. * @fit: pointer to the FIT format image header
  1234. * @noffset: configuration node offset
  1235. *
  1236. * fit_conf_get_fdt_node() retrives fdt image node unit name from
  1237. * configuration FIT_KERNEL_PROP property and translates it to the node
  1238. * offset.
  1239. *
  1240. * returns:
  1241. * image node offset when found (>=0)
  1242. * negative number on failure (FDT_ERR_* code)
  1243. */
  1244. int fit_conf_get_fdt_node(const void *fit, int noffset)
  1245. {
  1246. return __fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
  1247. }
  1248. /**
  1249. * fit_conf_print - prints out the FIT configuration details
  1250. * @fit: pointer to the FIT format image header
  1251. * @noffset: offset of the configuration node
  1252. * @p: pointer to prefix string
  1253. *
  1254. * fit_conf_print() lists all mandatory properies for the processed
  1255. * configuration node.
  1256. *
  1257. * returns:
  1258. * no returned results
  1259. */
  1260. void fit_conf_print(const void *fit, int noffset, const char *p)
  1261. {
  1262. char *desc;
  1263. char *uname;
  1264. int ret;
  1265. /* Mandatory properties */
  1266. ret = fit_get_desc(fit, noffset, &desc);
  1267. printf("%s Description: ", p);
  1268. if (ret)
  1269. printf("unavailable\n");
  1270. else
  1271. printf("%s\n", desc);
  1272. uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1273. printf("%s Kernel: ", p);
  1274. if (uname == NULL)
  1275. printf("unavailable\n");
  1276. else
  1277. printf("%s\n", uname);
  1278. /* Optional properties */
  1279. uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1280. if (uname)
  1281. printf("%s Init Ramdisk: %s\n", p, uname);
  1282. uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
  1283. if (uname)
  1284. printf("%s FDT: %s\n", p, uname);
  1285. }
  1286. /**
  1287. * fit_check_ramdisk - verify FIT format ramdisk subimage
  1288. * @fit_hdr: pointer to the FIT ramdisk header
  1289. * @rd_noffset: ramdisk subimage node offset within FIT image
  1290. * @arch: requested ramdisk image architecture type
  1291. * @verify: data CRC verification flag
  1292. *
  1293. * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
  1294. * specified FIT image.
  1295. *
  1296. * returns:
  1297. * 1, on success
  1298. * 0, on failure
  1299. */
  1300. #ifndef USE_HOSTCC
  1301. int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
  1302. int verify)
  1303. {
  1304. fit_image_print(fit, rd_noffset, " ");
  1305. if (verify) {
  1306. puts(" Verifying Hash Integrity ... ");
  1307. if (!fit_image_check_hashes(fit, rd_noffset)) {
  1308. puts("Bad Data Hash\n");
  1309. bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
  1310. return 0;
  1311. }
  1312. puts("OK\n");
  1313. }
  1314. bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
  1315. if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
  1316. !fit_image_check_arch(fit, rd_noffset, arch) ||
  1317. !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
  1318. printf("No Linux %s Ramdisk Image\n",
  1319. genimg_get_arch_name(arch));
  1320. bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
  1321. return 0;
  1322. }
  1323. bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
  1324. return 1;
  1325. }
  1326. #endif /* USE_HOSTCC */