image-fit.c 46 KB

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