image-fit.c 44 KB

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