image-fit.c 43 KB

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