image-fit.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. /**
  675. * fit_image_hash_get_ignore - get hash ignore flag
  676. * @fit: pointer to the FIT format image header
  677. * @noffset: hash node offset
  678. * @ignore: pointer to an int, will hold hash ignore flag
  679. *
  680. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  681. * If the property is found and non-zero, the hash algorithm is not verified by
  682. * u-boot automatically.
  683. *
  684. * returns:
  685. * 0, on ignore not found
  686. * value, on ignore found
  687. */
  688. static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  689. {
  690. int len;
  691. int *value;
  692. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  693. if (value == NULL || len != sizeof(int))
  694. *ignore = 0;
  695. else
  696. *ignore = *value;
  697. return 0;
  698. }
  699. /**
  700. * fit_set_timestamp - set node timestamp property
  701. * @fit: pointer to the FIT format image header
  702. * @noffset: node offset
  703. * @timestamp: timestamp value to be set
  704. *
  705. * fit_set_timestamp() attempts to set timestamp property in the requested
  706. * node and returns operation status to the caller.
  707. *
  708. * returns:
  709. * 0, on success
  710. * -1, on property read failure
  711. */
  712. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  713. {
  714. uint32_t t;
  715. int ret;
  716. t = cpu_to_uimage(timestamp);
  717. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  718. sizeof(uint32_t));
  719. if (ret) {
  720. printf("Can't set '%s' property for '%s' node (%s)\n",
  721. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  722. fdt_strerror(ret));
  723. return -1;
  724. }
  725. return 0;
  726. }
  727. /**
  728. * calculate_hash - calculate and return hash for provided input data
  729. * @data: pointer to the input data
  730. * @data_len: data length
  731. * @algo: requested hash algorithm
  732. * @value: pointer to the char, will hold hash value data (caller must
  733. * allocate enough free space)
  734. * value_len: length of the calculated hash
  735. *
  736. * calculate_hash() computes input data hash according to the requested
  737. * algorithm.
  738. * Resulting hash value is placed in caller provided 'value' buffer, length
  739. * of the calculated hash is returned via value_len pointer argument.
  740. *
  741. * returns:
  742. * 0, on success
  743. * -1, when algo is unsupported
  744. */
  745. int calculate_hash(const void *data, int data_len, const char *algo,
  746. uint8_t *value, int *value_len)
  747. {
  748. if (strcmp(algo, "crc32") == 0) {
  749. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  750. CHUNKSZ_CRC32);
  751. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  752. *value_len = 4;
  753. } else if (strcmp(algo, "sha1") == 0) {
  754. sha1_csum_wd((unsigned char *)data, data_len,
  755. (unsigned char *)value, CHUNKSZ_SHA1);
  756. *value_len = 20;
  757. } else if (strcmp(algo, "md5") == 0) {
  758. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  759. *value_len = 16;
  760. } else {
  761. debug("Unsupported hash alogrithm\n");
  762. return -1;
  763. }
  764. return 0;
  765. }
  766. static int fit_image_check_hash(const void *fit, int noffset, const void *data,
  767. size_t size, char **err_msgp)
  768. {
  769. uint8_t value[FIT_MAX_HASH_LEN];
  770. int value_len;
  771. char *algo;
  772. uint8_t *fit_value;
  773. int fit_value_len;
  774. int ignore;
  775. *err_msgp = NULL;
  776. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  777. *err_msgp = "Can't get hash algo property";
  778. return -1;
  779. }
  780. printf("%s", algo);
  781. if (IMAGE_ENABLE_IGNORE) {
  782. fit_image_hash_get_ignore(fit, noffset, &ignore);
  783. if (ignore) {
  784. printf("-skipped ");
  785. return 0;
  786. }
  787. }
  788. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  789. &fit_value_len)) {
  790. *err_msgp = "Can't get hash value property";
  791. return -1;
  792. }
  793. if (calculate_hash(data, size, algo, value, &value_len)) {
  794. *err_msgp = "Unsupported hash algorithm";
  795. return -1;
  796. }
  797. if (value_len != fit_value_len) {
  798. *err_msgp = "Bad hash value len";
  799. return -1;
  800. } else if (memcmp(value, fit_value, value_len) != 0) {
  801. *err_msgp = "Bad hash value";
  802. return -1;
  803. }
  804. return 0;
  805. }
  806. /**
  807. * fit_image_verify - verify data intergity
  808. * @fit: pointer to the FIT format image header
  809. * @image_noffset: component image node offset
  810. *
  811. * fit_image_verify() goes over component image hash nodes,
  812. * re-calculates each data hash and compares with the value stored in hash
  813. * node.
  814. *
  815. * returns:
  816. * 1, if all hashes are valid
  817. * 0, otherwise (or on error)
  818. */
  819. int fit_image_verify(const void *fit, int image_noffset)
  820. {
  821. const void *data;
  822. size_t size;
  823. int noffset;
  824. char *err_msg = "";
  825. /* Get image data and data length */
  826. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  827. err_msg = "Can't get image data/size";
  828. return 0;
  829. }
  830. /* Process all hash subnodes of the component image node */
  831. for (noffset = fdt_first_subnode(fit, image_noffset);
  832. noffset >= 0;
  833. noffset = fdt_next_subnode(fit, noffset)) {
  834. const char *name = fit_get_name(fit, noffset, NULL);
  835. /*
  836. * Check subnode name, must be equal to "hash".
  837. * Multiple hash nodes require unique unit node
  838. * names, e.g. hash@1, hash@2, etc.
  839. */
  840. if (!strncmp(name, FIT_HASH_NODENAME,
  841. strlen(FIT_HASH_NODENAME))) {
  842. if (fit_image_check_hash(fit, noffset, data, size,
  843. &err_msg))
  844. goto error;
  845. puts("+ ");
  846. }
  847. }
  848. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  849. err_msg = "Corrupted or truncated tree";
  850. goto error;
  851. }
  852. return 1;
  853. error:
  854. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  855. err_msg, fit_get_name(fit, noffset, NULL),
  856. fit_get_name(fit, image_noffset, NULL));
  857. return 0;
  858. }
  859. /**
  860. * fit_all_image_verify - verify data intergity for all images
  861. * @fit: pointer to the FIT format image header
  862. *
  863. * fit_all_image_verify() goes over all images in the FIT and
  864. * for every images checks if all it's hashes are valid.
  865. *
  866. * returns:
  867. * 1, if all hashes of all images are valid
  868. * 0, otherwise (or on error)
  869. */
  870. int fit_all_image_verify(const void *fit)
  871. {
  872. int images_noffset;
  873. int noffset;
  874. int ndepth;
  875. int count;
  876. /* Find images parent node offset */
  877. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  878. if (images_noffset < 0) {
  879. printf("Can't find images parent node '%s' (%s)\n",
  880. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  881. return 0;
  882. }
  883. /* Process all image subnodes, check hashes for each */
  884. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  885. (ulong)fit);
  886. for (ndepth = 0, count = 0,
  887. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  888. (noffset >= 0) && (ndepth > 0);
  889. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  890. if (ndepth == 1) {
  891. /*
  892. * Direct child node of the images parent node,
  893. * i.e. component image node.
  894. */
  895. printf(" Hash(es) for Image %u (%s): ", count++,
  896. fit_get_name(fit, noffset, NULL));
  897. if (!fit_image_verify(fit, noffset))
  898. return 0;
  899. printf("\n");
  900. }
  901. }
  902. return 1;
  903. }
  904. /**
  905. * fit_image_check_os - check whether image node is of a given os type
  906. * @fit: pointer to the FIT format image header
  907. * @noffset: component image node offset
  908. * @os: requested image os
  909. *
  910. * fit_image_check_os() reads image os property and compares its numeric
  911. * id with the requested os. Comparison result is returned to the caller.
  912. *
  913. * returns:
  914. * 1 if image is of given os type
  915. * 0 otherwise (or on error)
  916. */
  917. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  918. {
  919. uint8_t image_os;
  920. if (fit_image_get_os(fit, noffset, &image_os))
  921. return 0;
  922. return (os == image_os);
  923. }
  924. /**
  925. * fit_image_check_arch - check whether image node is of a given arch
  926. * @fit: pointer to the FIT format image header
  927. * @noffset: component image node offset
  928. * @arch: requested imagearch
  929. *
  930. * fit_image_check_arch() reads image arch property and compares its numeric
  931. * id with the requested arch. Comparison result is returned to the caller.
  932. *
  933. * returns:
  934. * 1 if image is of given arch
  935. * 0 otherwise (or on error)
  936. */
  937. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  938. {
  939. uint8_t image_arch;
  940. if (fit_image_get_arch(fit, noffset, &image_arch))
  941. return 0;
  942. return (arch == image_arch);
  943. }
  944. /**
  945. * fit_image_check_type - check whether image node is of a given type
  946. * @fit: pointer to the FIT format image header
  947. * @noffset: component image node offset
  948. * @type: requested image type
  949. *
  950. * fit_image_check_type() reads image type property and compares its numeric
  951. * id with the requested type. Comparison result is returned to the caller.
  952. *
  953. * returns:
  954. * 1 if image is of given type
  955. * 0 otherwise (or on error)
  956. */
  957. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  958. {
  959. uint8_t image_type;
  960. if (fit_image_get_type(fit, noffset, &image_type))
  961. return 0;
  962. return (type == image_type);
  963. }
  964. /**
  965. * fit_image_check_comp - check whether image node uses given compression
  966. * @fit: pointer to the FIT format image header
  967. * @noffset: component image node offset
  968. * @comp: requested image compression type
  969. *
  970. * fit_image_check_comp() reads image compression property and compares its
  971. * numeric id with the requested compression type. Comparison result is
  972. * returned to the caller.
  973. *
  974. * returns:
  975. * 1 if image uses requested compression
  976. * 0 otherwise (or on error)
  977. */
  978. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  979. {
  980. uint8_t image_comp;
  981. if (fit_image_get_comp(fit, noffset, &image_comp))
  982. return 0;
  983. return (comp == image_comp);
  984. }
  985. /**
  986. * fit_check_format - sanity check FIT image format
  987. * @fit: pointer to the FIT format image header
  988. *
  989. * fit_check_format() runs a basic sanity FIT image verification.
  990. * Routine checks for mandatory properties, nodes, etc.
  991. *
  992. * returns:
  993. * 1, on success
  994. * 0, on failure
  995. */
  996. int fit_check_format(const void *fit)
  997. {
  998. /* mandatory / node 'description' property */
  999. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1000. debug("Wrong FIT format: no description\n");
  1001. return 0;
  1002. }
  1003. if (IMAGE_ENABLE_TIMESTAMP) {
  1004. /* mandatory / node 'timestamp' property */
  1005. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1006. debug("Wrong FIT format: no timestamp\n");
  1007. return 0;
  1008. }
  1009. }
  1010. /* mandatory subimages parent '/images' node */
  1011. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1012. debug("Wrong FIT format: no images parent node\n");
  1013. return 0;
  1014. }
  1015. return 1;
  1016. }
  1017. /**
  1018. * fit_conf_find_compat
  1019. * @fit: pointer to the FIT format image header
  1020. * @fdt: pointer to the device tree to compare against
  1021. *
  1022. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1023. * most compatible with the passed in device tree.
  1024. *
  1025. * Example:
  1026. *
  1027. * / o image-tree
  1028. * |-o images
  1029. * | |-o fdt@1
  1030. * | |-o fdt@2
  1031. * |
  1032. * |-o configurations
  1033. * |-o config@1
  1034. * | |-fdt = fdt@1
  1035. * |
  1036. * |-o config@2
  1037. * |-fdt = fdt@2
  1038. *
  1039. * / o U-Boot fdt
  1040. * |-compatible = "foo,bar", "bim,bam"
  1041. *
  1042. * / o kernel fdt1
  1043. * |-compatible = "foo,bar",
  1044. *
  1045. * / o kernel fdt2
  1046. * |-compatible = "bim,bam", "baz,biz"
  1047. *
  1048. * Configuration 1 would be picked because the first string in U-Boot's
  1049. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1050. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1051. *
  1052. * returns:
  1053. * offset to the configuration to use if one was found
  1054. * -1 otherwise
  1055. */
  1056. int fit_conf_find_compat(const void *fit, const void *fdt)
  1057. {
  1058. int ndepth = 0;
  1059. int noffset, confs_noffset, images_noffset;
  1060. const void *fdt_compat;
  1061. int fdt_compat_len;
  1062. int best_match_offset = 0;
  1063. int best_match_pos = 0;
  1064. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1065. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1066. if (confs_noffset < 0 || images_noffset < 0) {
  1067. debug("Can't find configurations or images nodes.\n");
  1068. return -1;
  1069. }
  1070. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1071. if (!fdt_compat) {
  1072. debug("Fdt for comparison has no \"compatible\" property.\n");
  1073. return -1;
  1074. }
  1075. /*
  1076. * Loop over the configurations in the FIT image.
  1077. */
  1078. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1079. (noffset >= 0) && (ndepth > 0);
  1080. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1081. const void *kfdt;
  1082. const char *kfdt_name;
  1083. int kfdt_noffset;
  1084. const char *cur_fdt_compat;
  1085. int len;
  1086. size_t size;
  1087. int i;
  1088. if (ndepth > 1)
  1089. continue;
  1090. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1091. if (!kfdt_name) {
  1092. debug("No fdt property found.\n");
  1093. continue;
  1094. }
  1095. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1096. kfdt_name);
  1097. if (kfdt_noffset < 0) {
  1098. debug("No image node named \"%s\" found.\n",
  1099. kfdt_name);
  1100. continue;
  1101. }
  1102. /*
  1103. * Get a pointer to this configuration's fdt.
  1104. */
  1105. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1106. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1107. continue;
  1108. }
  1109. len = fdt_compat_len;
  1110. cur_fdt_compat = fdt_compat;
  1111. /*
  1112. * Look for a match for each U-Boot compatibility string in
  1113. * turn in this configuration's fdt.
  1114. */
  1115. for (i = 0; len > 0 &&
  1116. (!best_match_offset || best_match_pos > i); i++) {
  1117. int cur_len = strlen(cur_fdt_compat) + 1;
  1118. if (!fdt_node_check_compatible(kfdt, 0,
  1119. cur_fdt_compat)) {
  1120. best_match_offset = noffset;
  1121. best_match_pos = i;
  1122. break;
  1123. }
  1124. len -= cur_len;
  1125. cur_fdt_compat += cur_len;
  1126. }
  1127. }
  1128. if (!best_match_offset) {
  1129. debug("No match found.\n");
  1130. return -1;
  1131. }
  1132. return best_match_offset;
  1133. }
  1134. /**
  1135. * fit_conf_get_node - get node offset for configuration of a given unit name
  1136. * @fit: pointer to the FIT format image header
  1137. * @conf_uname: configuration node unit name
  1138. *
  1139. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  1140. * parant node) of a provided unit name. If configuration is found its node
  1141. * offset is returned to the caller.
  1142. *
  1143. * When NULL is provided in second argument fit_conf_get_node() will search
  1144. * for a default configuration node instead. Default configuration node unit
  1145. * name is retrived from FIT_DEFAULT_PROP property of the '/configurations'
  1146. * node.
  1147. *
  1148. * returns:
  1149. * configuration node offset when found (>=0)
  1150. * negative number on failure (FDT_ERR_* code)
  1151. */
  1152. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1153. {
  1154. int noffset, confs_noffset;
  1155. int len;
  1156. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1157. if (confs_noffset < 0) {
  1158. debug("Can't find configurations parent node '%s' (%s)\n",
  1159. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1160. return confs_noffset;
  1161. }
  1162. if (conf_uname == NULL) {
  1163. /* get configuration unit name from the default property */
  1164. debug("No configuration specified, trying default...\n");
  1165. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1166. FIT_DEFAULT_PROP, &len);
  1167. if (conf_uname == NULL) {
  1168. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1169. len);
  1170. return len;
  1171. }
  1172. debug("Found default configuration: '%s'\n", conf_uname);
  1173. }
  1174. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1175. if (noffset < 0) {
  1176. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1177. conf_uname, fdt_strerror(noffset));
  1178. }
  1179. return noffset;
  1180. }
  1181. int fit_conf_get_prop_node(const void *fit, int noffset,
  1182. const char *prop_name)
  1183. {
  1184. char *uname;
  1185. int len;
  1186. /* get kernel image unit name from configuration kernel property */
  1187. uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
  1188. if (uname == NULL)
  1189. return len;
  1190. return fit_image_get_node(fit, uname);
  1191. }
  1192. /**
  1193. * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
  1194. * a given configuration
  1195. * @fit: pointer to the FIT format image header
  1196. * @noffset: configuration node offset
  1197. *
  1198. * fit_conf_get_kernel_node() retrives kernel image node unit name from
  1199. * configuration FIT_KERNEL_PROP property and translates it to the node
  1200. * offset.
  1201. *
  1202. * returns:
  1203. * image node offset when found (>=0)
  1204. * negative number on failure (FDT_ERR_* code)
  1205. */
  1206. int fit_conf_get_kernel_node(const void *fit, int noffset)
  1207. {
  1208. return fit_conf_get_prop_node(fit, noffset, FIT_KERNEL_PROP);
  1209. }
  1210. /**
  1211. * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
  1212. * a given configuration
  1213. * @fit: pointer to the FIT format image header
  1214. * @noffset: configuration node offset
  1215. *
  1216. * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
  1217. * configuration FIT_KERNEL_PROP property and translates it to the node
  1218. * offset.
  1219. *
  1220. * returns:
  1221. * image node offset when found (>=0)
  1222. * negative number on failure (FDT_ERR_* code)
  1223. */
  1224. int fit_conf_get_ramdisk_node(const void *fit, int noffset)
  1225. {
  1226. return fit_conf_get_prop_node(fit, noffset, FIT_RAMDISK_PROP);
  1227. }
  1228. /**
  1229. * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
  1230. * a given configuration
  1231. * @fit: pointer to the FIT format image header
  1232. * @noffset: configuration node offset
  1233. *
  1234. * fit_conf_get_fdt_node() retrives fdt image node unit name from
  1235. * configuration FIT_KERNEL_PROP property and translates it to the node
  1236. * offset.
  1237. *
  1238. * returns:
  1239. * image node offset when found (>=0)
  1240. * negative number on failure (FDT_ERR_* code)
  1241. */
  1242. int fit_conf_get_fdt_node(const void *fit, int noffset)
  1243. {
  1244. return fit_conf_get_prop_node(fit, noffset, FIT_FDT_PROP);
  1245. }
  1246. /**
  1247. * fit_conf_print - prints out the FIT configuration details
  1248. * @fit: pointer to the FIT format image header
  1249. * @noffset: offset of the configuration node
  1250. * @p: pointer to prefix string
  1251. *
  1252. * fit_conf_print() lists all mandatory properies for the processed
  1253. * configuration node.
  1254. *
  1255. * returns:
  1256. * no returned results
  1257. */
  1258. void fit_conf_print(const void *fit, int noffset, const char *p)
  1259. {
  1260. char *desc;
  1261. char *uname;
  1262. int ret;
  1263. /* Mandatory properties */
  1264. ret = fit_get_desc(fit, noffset, &desc);
  1265. printf("%s Description: ", p);
  1266. if (ret)
  1267. printf("unavailable\n");
  1268. else
  1269. printf("%s\n", desc);
  1270. uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1271. printf("%s Kernel: ", p);
  1272. if (uname == NULL)
  1273. printf("unavailable\n");
  1274. else
  1275. printf("%s\n", uname);
  1276. /* Optional properties */
  1277. uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1278. if (uname)
  1279. printf("%s Init Ramdisk: %s\n", p, uname);
  1280. uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
  1281. if (uname)
  1282. printf("%s FDT: %s\n", p, uname);
  1283. }
  1284. /**
  1285. * fit_check_ramdisk - verify FIT format ramdisk subimage
  1286. * @fit_hdr: pointer to the FIT ramdisk header
  1287. * @rd_noffset: ramdisk subimage node offset within FIT image
  1288. * @arch: requested ramdisk image architecture type
  1289. * @verify: data CRC verification flag
  1290. *
  1291. * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
  1292. * specified FIT image.
  1293. *
  1294. * returns:
  1295. * 1, on success
  1296. * 0, on failure
  1297. */
  1298. #ifndef USE_HOSTCC
  1299. int fit_check_ramdisk(const void *fit, int rd_noffset, uint8_t arch,
  1300. int verify)
  1301. {
  1302. fit_image_print(fit, rd_noffset, " ");
  1303. if (verify) {
  1304. puts(" Verifying Hash Integrity ... ");
  1305. if (!fit_image_verify(fit, rd_noffset)) {
  1306. puts("Bad Data Hash\n");
  1307. bootstage_error(BOOTSTAGE_ID_FIT_RD_HASH);
  1308. return 0;
  1309. }
  1310. puts("OK\n");
  1311. }
  1312. bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
  1313. if (!fit_image_check_os(fit, rd_noffset, IH_OS_LINUX) ||
  1314. !fit_image_check_arch(fit, rd_noffset, arch) ||
  1315. !fit_image_check_type(fit, rd_noffset, IH_TYPE_RAMDISK)) {
  1316. printf("No Linux %s Ramdisk Image\n",
  1317. genimg_get_arch_name(arch));
  1318. bootstage_error(BOOTSTAGE_ID_FIT_RD_CHECK_ALL);
  1319. return 0;
  1320. }
  1321. bootstage_mark(BOOTSTAGE_ID_FIT_RD_CHECK_ALL_OK);
  1322. return 1;
  1323. }
  1324. #endif /* USE_HOSTCC */