image-fit.c 46 KB

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