image-fit.c 48 KB

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