image-fit.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013, Google Inc.
  4. *
  5. * (C) Copyright 2008 Semihalf
  6. *
  7. * (C) Copyright 2000-2006
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. */
  10. #ifdef USE_HOSTCC
  11. #include "mkimage.h"
  12. #include <time.h>
  13. #include <u-boot/crc.h>
  14. #else
  15. #include <linux/compiler.h>
  16. #include <linux/kconfig.h>
  17. #include <common.h>
  18. #include <errno.h>
  19. #include <mapmem.h>
  20. #include <asm/io.h>
  21. #include <malloc.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #endif /* !USE_HOSTCC*/
  24. #include <bootm.h>
  25. #include <image.h>
  26. #include <bootstage.h>
  27. #include <u-boot/crc.h>
  28. #include <u-boot/md5.h>
  29. #include <u-boot/sha1.h>
  30. #include <u-boot/sha256.h>
  31. /*****************************************************************************/
  32. /* New uImage format routines */
  33. /*****************************************************************************/
  34. #ifndef USE_HOSTCC
  35. static int fit_parse_spec(const char *spec, char sepc, ulong addr_curr,
  36. ulong *addr, const char **name)
  37. {
  38. const char *sep;
  39. *addr = addr_curr;
  40. *name = NULL;
  41. sep = strchr(spec, sepc);
  42. if (sep) {
  43. if (sep - spec > 0)
  44. *addr = simple_strtoul(spec, NULL, 16);
  45. *name = sep + 1;
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. /**
  51. * fit_parse_conf - parse FIT configuration spec
  52. * @spec: input string, containing configuration spec
  53. * @add_curr: current image address (to be used as a possible default)
  54. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  55. * configuration
  56. * @conf_name double pointer to a char, will hold pointer to a configuration
  57. * unit name
  58. *
  59. * fit_parse_conf() expects configuration spec in the form of [<addr>]#<conf>,
  60. * where <addr> is a FIT image address that contains configuration
  61. * with a <conf> unit name.
  62. *
  63. * Address part is optional, and if omitted default add_curr will
  64. * be used instead.
  65. *
  66. * returns:
  67. * 1 if spec is a valid configuration string,
  68. * addr and conf_name are set accordingly
  69. * 0 otherwise
  70. */
  71. int fit_parse_conf(const char *spec, ulong addr_curr,
  72. ulong *addr, const char **conf_name)
  73. {
  74. return fit_parse_spec(spec, '#', addr_curr, addr, conf_name);
  75. }
  76. /**
  77. * fit_parse_subimage - parse FIT subimage spec
  78. * @spec: input string, containing subimage spec
  79. * @add_curr: current image address (to be used as a possible default)
  80. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  81. * subimage
  82. * @image_name: double pointer to a char, will hold pointer to a subimage name
  83. *
  84. * fit_parse_subimage() expects subimage spec in the form of
  85. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  86. * subimage with a <subimg> unit name.
  87. *
  88. * Address part is optional, and if omitted default add_curr will
  89. * be used instead.
  90. *
  91. * returns:
  92. * 1 if spec is a valid subimage string,
  93. * addr and image_name are set accordingly
  94. * 0 otherwise
  95. */
  96. int fit_parse_subimage(const char *spec, ulong addr_curr,
  97. ulong *addr, const char **image_name)
  98. {
  99. return fit_parse_spec(spec, ':', addr_curr, addr, image_name);
  100. }
  101. #endif /* !USE_HOSTCC */
  102. static void fit_get_debug(const void *fit, int noffset,
  103. char *prop_name, int err)
  104. {
  105. debug("Can't get '%s' property from FIT 0x%08lx, node: offset %d, name %s (%s)\n",
  106. prop_name, (ulong)fit, noffset, fit_get_name(fit, noffset, NULL),
  107. fdt_strerror(err));
  108. }
  109. /**
  110. * fit_get_subimage_count - get component (sub-image) count
  111. * @fit: pointer to the FIT format image header
  112. * @images_noffset: offset of images node
  113. *
  114. * returns:
  115. * number of image components
  116. */
  117. int fit_get_subimage_count(const void *fit, int images_noffset)
  118. {
  119. int noffset;
  120. int ndepth;
  121. int count = 0;
  122. /* Process its subnodes, print out component images details */
  123. for (ndepth = 0, count = 0,
  124. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  125. (noffset >= 0) && (ndepth > 0);
  126. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  127. if (ndepth == 1) {
  128. count++;
  129. }
  130. }
  131. return count;
  132. }
  133. #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT)
  134. /**
  135. * fit_image_print_data() - prints out the hash node details
  136. * @fit: pointer to the FIT format image header
  137. * @noffset: offset of the hash node
  138. * @p: pointer to prefix string
  139. * @type: Type of information to print ("hash" or "sign")
  140. *
  141. * fit_image_print_data() lists properties for the processed hash node
  142. *
  143. * This function avoid using puts() since it prints a newline on the host
  144. * but does not in U-Boot.
  145. *
  146. * returns:
  147. * no returned results
  148. */
  149. static void fit_image_print_data(const void *fit, int noffset, const char *p,
  150. const char *type)
  151. {
  152. const char *keyname;
  153. uint8_t *value;
  154. int value_len;
  155. char *algo;
  156. const char *padding;
  157. bool required;
  158. int ret, i;
  159. debug("%s %s node: '%s'\n", p, type,
  160. fit_get_name(fit, noffset, NULL));
  161. printf("%s %s algo: ", p, type);
  162. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  163. printf("invalid/unsupported\n");
  164. return;
  165. }
  166. printf("%s", algo);
  167. keyname = fdt_getprop(fit, noffset, FIT_KEY_HINT, NULL);
  168. required = fdt_getprop(fit, noffset, FIT_KEY_REQUIRED, NULL) != NULL;
  169. if (keyname)
  170. printf(":%s", keyname);
  171. if (required)
  172. printf(" (required)");
  173. printf("\n");
  174. padding = fdt_getprop(fit, noffset, "padding", NULL);
  175. if (padding)
  176. printf("%s %s padding: %s\n", p, type, padding);
  177. ret = fit_image_hash_get_value(fit, noffset, &value,
  178. &value_len);
  179. printf("%s %s value: ", p, type);
  180. if (ret) {
  181. printf("unavailable\n");
  182. } else {
  183. for (i = 0; i < value_len; i++)
  184. printf("%02x", value[i]);
  185. printf("\n");
  186. }
  187. debug("%s %s len: %d\n", p, type, value_len);
  188. /* Signatures have a time stamp */
  189. if (IMAGE_ENABLE_TIMESTAMP && keyname) {
  190. time_t timestamp;
  191. printf("%s Timestamp: ", p);
  192. if (fit_get_timestamp(fit, noffset, &timestamp))
  193. printf("unavailable\n");
  194. else
  195. genimg_print_time(timestamp);
  196. }
  197. }
  198. /**
  199. * fit_image_print_verification_data() - prints out the hash/signature details
  200. * @fit: pointer to the FIT format image header
  201. * @noffset: offset of the hash or signature node
  202. * @p: pointer to prefix string
  203. *
  204. * This lists properties for the processed hash node
  205. *
  206. * returns:
  207. * no returned results
  208. */
  209. static void fit_image_print_verification_data(const void *fit, int noffset,
  210. const char *p)
  211. {
  212. const char *name;
  213. /*
  214. * Check subnode name, must be equal to "hash" or "signature".
  215. * Multiple hash/signature nodes require unique unit node
  216. * names, e.g. hash-1, hash-2, signature-1, signature-2, etc.
  217. */
  218. name = fit_get_name(fit, noffset, NULL);
  219. if (!strncmp(name, FIT_HASH_NODENAME, strlen(FIT_HASH_NODENAME))) {
  220. fit_image_print_data(fit, noffset, p, "Hash");
  221. } else if (!strncmp(name, FIT_SIG_NODENAME,
  222. strlen(FIT_SIG_NODENAME))) {
  223. fit_image_print_data(fit, noffset, p, "Sign");
  224. }
  225. }
  226. /**
  227. * fit_conf_print - prints out the FIT configuration details
  228. * @fit: pointer to the FIT format image header
  229. * @noffset: offset of the configuration node
  230. * @p: pointer to prefix string
  231. *
  232. * fit_conf_print() lists all mandatory properties for the processed
  233. * configuration node.
  234. *
  235. * returns:
  236. * no returned results
  237. */
  238. static void fit_conf_print(const void *fit, int noffset, const char *p)
  239. {
  240. char *desc;
  241. const char *uname;
  242. int ret;
  243. int fdt_index, loadables_index;
  244. int ndepth;
  245. /* Mandatory properties */
  246. ret = fit_get_desc(fit, noffset, &desc);
  247. printf("%s Description: ", p);
  248. if (ret)
  249. printf("unavailable\n");
  250. else
  251. printf("%s\n", desc);
  252. uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  253. printf("%s Kernel: ", p);
  254. if (!uname)
  255. printf("unavailable\n");
  256. else
  257. printf("%s\n", uname);
  258. /* Optional properties */
  259. uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  260. if (uname)
  261. printf("%s Init Ramdisk: %s\n", p, uname);
  262. uname = fdt_getprop(fit, noffset, FIT_FIRMWARE_PROP, NULL);
  263. if (uname)
  264. printf("%s Firmware: %s\n", p, uname);
  265. for (fdt_index = 0;
  266. uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
  267. fdt_index, NULL), uname;
  268. fdt_index++) {
  269. if (fdt_index == 0)
  270. printf("%s FDT: ", p);
  271. else
  272. printf("%s ", p);
  273. printf("%s\n", uname);
  274. }
  275. uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
  276. if (uname)
  277. printf("%s FPGA: %s\n", p, uname);
  278. /* Print out all of the specified loadables */
  279. for (loadables_index = 0;
  280. uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
  281. loadables_index, NULL), uname;
  282. loadables_index++) {
  283. if (loadables_index == 0) {
  284. printf("%s Loadables: ", p);
  285. } else {
  286. printf("%s ", p);
  287. }
  288. printf("%s\n", uname);
  289. }
  290. /* Process all hash subnodes of the component configuration node */
  291. for (ndepth = 0, noffset = fdt_next_node(fit, noffset, &ndepth);
  292. (noffset >= 0) && (ndepth > 0);
  293. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  294. if (ndepth == 1) {
  295. /* Direct child node of the component configuration node */
  296. fit_image_print_verification_data(fit, noffset, p);
  297. }
  298. }
  299. }
  300. /**
  301. * fit_print_contents - prints out the contents of the FIT format image
  302. * @fit: pointer to the FIT format image header
  303. * @p: pointer to prefix string
  304. *
  305. * fit_print_contents() formats a multi line FIT image contents description.
  306. * The routine prints out FIT image properties (root node level) followed by
  307. * the details of each component image.
  308. *
  309. * returns:
  310. * no returned results
  311. */
  312. void fit_print_contents(const void *fit)
  313. {
  314. char *desc;
  315. char *uname;
  316. int images_noffset;
  317. int confs_noffset;
  318. int noffset;
  319. int ndepth;
  320. int count = 0;
  321. int ret;
  322. const char *p;
  323. time_t timestamp;
  324. /* Indent string is defined in header image.h */
  325. p = IMAGE_INDENT_STRING;
  326. /* Root node properties */
  327. ret = fit_get_desc(fit, 0, &desc);
  328. printf("%sFIT description: ", p);
  329. if (ret)
  330. printf("unavailable\n");
  331. else
  332. printf("%s\n", desc);
  333. if (IMAGE_ENABLE_TIMESTAMP) {
  334. ret = fit_get_timestamp(fit, 0, &timestamp);
  335. printf("%sCreated: ", p);
  336. if (ret)
  337. printf("unavailable\n");
  338. else
  339. genimg_print_time(timestamp);
  340. }
  341. /* Find images parent node offset */
  342. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  343. if (images_noffset < 0) {
  344. printf("Can't find images parent node '%s' (%s)\n",
  345. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  346. return;
  347. }
  348. /* Process its subnodes, print out component images details */
  349. for (ndepth = 0, count = 0,
  350. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  351. (noffset >= 0) && (ndepth > 0);
  352. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  353. if (ndepth == 1) {
  354. /*
  355. * Direct child node of the images parent node,
  356. * i.e. component image node.
  357. */
  358. printf("%s Image %u (%s)\n", p, count++,
  359. fit_get_name(fit, noffset, NULL));
  360. fit_image_print(fit, noffset, p);
  361. }
  362. }
  363. /* Find configurations parent node offset */
  364. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  365. if (confs_noffset < 0) {
  366. debug("Can't get configurations parent node '%s' (%s)\n",
  367. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  368. return;
  369. }
  370. /* get default configuration unit name from default property */
  371. uname = (char *)fdt_getprop(fit, noffset, FIT_DEFAULT_PROP, NULL);
  372. if (uname)
  373. printf("%s Default Configuration: '%s'\n", p, uname);
  374. /* Process its subnodes, print out configurations details */
  375. for (ndepth = 0, count = 0,
  376. noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  377. (noffset >= 0) && (ndepth > 0);
  378. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  379. if (ndepth == 1) {
  380. /*
  381. * Direct child node of the configurations parent node,
  382. * i.e. configuration node.
  383. */
  384. printf("%s Configuration %u (%s)\n", p, count++,
  385. fit_get_name(fit, noffset, NULL));
  386. fit_conf_print(fit, noffset, p);
  387. }
  388. }
  389. }
  390. /**
  391. * fit_image_print - prints out the FIT component image details
  392. * @fit: pointer to the FIT format image header
  393. * @image_noffset: offset of the component image node
  394. * @p: pointer to prefix string
  395. *
  396. * fit_image_print() lists all mandatory properties for the processed component
  397. * image. If present, hash nodes are printed out as well. Load
  398. * address for images of type firmware is also printed out. Since the load
  399. * address is not mandatory for firmware images, it will be output as
  400. * "unavailable" when not present.
  401. *
  402. * returns:
  403. * no returned results
  404. */
  405. void fit_image_print(const void *fit, int image_noffset, const char *p)
  406. {
  407. char *desc;
  408. uint8_t type, arch, os, comp;
  409. size_t size;
  410. ulong load, entry;
  411. const void *data;
  412. int noffset;
  413. int ndepth;
  414. int ret;
  415. /* Mandatory properties */
  416. ret = fit_get_desc(fit, image_noffset, &desc);
  417. printf("%s Description: ", p);
  418. if (ret)
  419. printf("unavailable\n");
  420. else
  421. printf("%s\n", desc);
  422. if (IMAGE_ENABLE_TIMESTAMP) {
  423. time_t timestamp;
  424. ret = fit_get_timestamp(fit, 0, &timestamp);
  425. printf("%s Created: ", p);
  426. if (ret)
  427. printf("unavailable\n");
  428. else
  429. genimg_print_time(timestamp);
  430. }
  431. fit_image_get_type(fit, image_noffset, &type);
  432. printf("%s Type: %s\n", p, genimg_get_type_name(type));
  433. fit_image_get_comp(fit, image_noffset, &comp);
  434. printf("%s Compression: %s\n", p, genimg_get_comp_name(comp));
  435. ret = fit_image_get_data_and_size(fit, image_noffset, &data, &size);
  436. #ifndef USE_HOSTCC
  437. printf("%s Data Start: ", p);
  438. if (ret) {
  439. printf("unavailable\n");
  440. } else {
  441. void *vdata = (void *)data;
  442. printf("0x%08lx\n", (ulong)map_to_sysmem(vdata));
  443. }
  444. #endif
  445. printf("%s Data Size: ", p);
  446. if (ret)
  447. printf("unavailable\n");
  448. else
  449. genimg_print_size(size);
  450. /* Remaining, type dependent properties */
  451. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  452. (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
  453. (type == IH_TYPE_FLATDT)) {
  454. fit_image_get_arch(fit, image_noffset, &arch);
  455. printf("%s Architecture: %s\n", p, genimg_get_arch_name(arch));
  456. }
  457. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_RAMDISK) ||
  458. (type == IH_TYPE_FIRMWARE)) {
  459. fit_image_get_os(fit, image_noffset, &os);
  460. printf("%s OS: %s\n", p, genimg_get_os_name(os));
  461. }
  462. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  463. (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
  464. (type == IH_TYPE_FPGA)) {
  465. ret = fit_image_get_load(fit, image_noffset, &load);
  466. printf("%s Load Address: ", p);
  467. if (ret)
  468. printf("unavailable\n");
  469. else
  470. printf("0x%08lx\n", load);
  471. }
  472. /* optional load address for FDT */
  473. if (type == IH_TYPE_FLATDT && !fit_image_get_load(fit, image_noffset, &load))
  474. printf("%s Load Address: 0x%08lx\n", p, load);
  475. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  476. (type == IH_TYPE_RAMDISK)) {
  477. ret = fit_image_get_entry(fit, image_noffset, &entry);
  478. printf("%s Entry Point: ", p);
  479. if (ret)
  480. printf("unavailable\n");
  481. else
  482. printf("0x%08lx\n", entry);
  483. }
  484. /* Process all hash subnodes of the component image node */
  485. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  486. (noffset >= 0) && (ndepth > 0);
  487. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  488. if (ndepth == 1) {
  489. /* Direct child node of the component image node */
  490. fit_image_print_verification_data(fit, noffset, p);
  491. }
  492. }
  493. }
  494. #else
  495. void fit_print_contents(const void *fit) { }
  496. void fit_image_print(const void *fit, int image_noffset, const char *p) { }
  497. #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_FIT_PRINT) */
  498. /**
  499. * fit_get_desc - get node description property
  500. * @fit: pointer to the FIT format image header
  501. * @noffset: node offset
  502. * @desc: double pointer to the char, will hold pointer to the description
  503. *
  504. * fit_get_desc() reads description property from a given node, if
  505. * description is found pointer to it is returned in third call argument.
  506. *
  507. * returns:
  508. * 0, on success
  509. * -1, on failure
  510. */
  511. int fit_get_desc(const void *fit, int noffset, char **desc)
  512. {
  513. int len;
  514. *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
  515. if (*desc == NULL) {
  516. fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. /**
  522. * fit_get_timestamp - get node timestamp property
  523. * @fit: pointer to the FIT format image header
  524. * @noffset: node offset
  525. * @timestamp: pointer to the time_t, will hold read timestamp
  526. *
  527. * fit_get_timestamp() reads timestamp property from given node, if timestamp
  528. * is found and has a correct size its value is returned in third call
  529. * argument.
  530. *
  531. * returns:
  532. * 0, on success
  533. * -1, on property read failure
  534. * -2, on wrong timestamp size
  535. */
  536. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
  537. {
  538. int len;
  539. const void *data;
  540. data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
  541. if (data == NULL) {
  542. fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
  543. return -1;
  544. }
  545. if (len != sizeof(uint32_t)) {
  546. debug("FIT timestamp with incorrect size of (%u)\n", len);
  547. return -2;
  548. }
  549. *timestamp = uimage_to_cpu(*((uint32_t *)data));
  550. return 0;
  551. }
  552. /**
  553. * fit_image_get_node - get node offset for component image of a given unit name
  554. * @fit: pointer to the FIT format image header
  555. * @image_uname: component image node unit name
  556. *
  557. * fit_image_get_node() finds a component image (within the '/images'
  558. * node) of a provided unit name. If image is found its node offset is
  559. * returned to the caller.
  560. *
  561. * returns:
  562. * image node offset when found (>=0)
  563. * negative number on failure (FDT_ERR_* code)
  564. */
  565. int fit_image_get_node(const void *fit, const char *image_uname)
  566. {
  567. int noffset, images_noffset;
  568. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  569. if (images_noffset < 0) {
  570. debug("Can't find images parent node '%s' (%s)\n",
  571. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  572. return images_noffset;
  573. }
  574. noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
  575. if (noffset < 0) {
  576. debug("Can't get node offset for image unit name: '%s' (%s)\n",
  577. image_uname, fdt_strerror(noffset));
  578. }
  579. return noffset;
  580. }
  581. /**
  582. * fit_image_get_os - get os id for a given component image node
  583. * @fit: pointer to the FIT format image header
  584. * @noffset: component image node offset
  585. * @os: pointer to the uint8_t, will hold os numeric id
  586. *
  587. * fit_image_get_os() finds os property in a given component image node.
  588. * If the property is found, its (string) value is translated to the numeric
  589. * id which is returned to the caller.
  590. *
  591. * returns:
  592. * 0, on success
  593. * -1, on failure
  594. */
  595. int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  596. {
  597. int len;
  598. const void *data;
  599. /* Get OS name from property data */
  600. data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
  601. if (data == NULL) {
  602. fit_get_debug(fit, noffset, FIT_OS_PROP, len);
  603. *os = -1;
  604. return -1;
  605. }
  606. /* Translate OS name to id */
  607. *os = genimg_get_os_id(data);
  608. return 0;
  609. }
  610. /**
  611. * fit_image_get_arch - get arch id for a given component image node
  612. * @fit: pointer to the FIT format image header
  613. * @noffset: component image node offset
  614. * @arch: pointer to the uint8_t, will hold arch numeric id
  615. *
  616. * fit_image_get_arch() finds arch property in a given component image node.
  617. * If the property is found, its (string) value is translated to the numeric
  618. * id which is returned to the caller.
  619. *
  620. * returns:
  621. * 0, on success
  622. * -1, on failure
  623. */
  624. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
  625. {
  626. int len;
  627. const void *data;
  628. /* Get architecture name from property data */
  629. data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
  630. if (data == NULL) {
  631. fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
  632. *arch = -1;
  633. return -1;
  634. }
  635. /* Translate architecture name to id */
  636. *arch = genimg_get_arch_id(data);
  637. return 0;
  638. }
  639. /**
  640. * fit_image_get_type - get type id for a given component image node
  641. * @fit: pointer to the FIT format image header
  642. * @noffset: component image node offset
  643. * @type: pointer to the uint8_t, will hold type numeric id
  644. *
  645. * fit_image_get_type() finds type property in a given component image node.
  646. * If the property is found, its (string) value is translated to the numeric
  647. * id which is returned to the caller.
  648. *
  649. * returns:
  650. * 0, on success
  651. * -1, on failure
  652. */
  653. int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
  654. {
  655. int len;
  656. const void *data;
  657. /* Get image type name from property data */
  658. data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
  659. if (data == NULL) {
  660. fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
  661. *type = -1;
  662. return -1;
  663. }
  664. /* Translate image type name to id */
  665. *type = genimg_get_type_id(data);
  666. return 0;
  667. }
  668. /**
  669. * fit_image_get_comp - get comp id for a given component image node
  670. * @fit: pointer to the FIT format image header
  671. * @noffset: component image node offset
  672. * @comp: pointer to the uint8_t, will hold comp numeric id
  673. *
  674. * fit_image_get_comp() finds comp property in a given component image node.
  675. * If the property is found, its (string) value is translated to the numeric
  676. * id which is returned to the caller.
  677. *
  678. * returns:
  679. * 0, on success
  680. * -1, on failure
  681. */
  682. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
  683. {
  684. int len;
  685. const void *data;
  686. /* Get compression name from property data */
  687. data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
  688. if (data == NULL) {
  689. fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
  690. *comp = -1;
  691. return -1;
  692. }
  693. /* Translate compression name to id */
  694. *comp = genimg_get_comp_id(data);
  695. return 0;
  696. }
  697. static int fit_image_get_address(const void *fit, int noffset, char *name,
  698. ulong *load)
  699. {
  700. int len, cell_len;
  701. const fdt32_t *cell;
  702. uint64_t load64 = 0;
  703. cell = fdt_getprop(fit, noffset, name, &len);
  704. if (cell == NULL) {
  705. fit_get_debug(fit, noffset, name, len);
  706. return -1;
  707. }
  708. if (len > sizeof(ulong)) {
  709. printf("Unsupported %s address size\n", name);
  710. return -1;
  711. }
  712. cell_len = len >> 2;
  713. /* Use load64 to avoid compiling warning for 32-bit target */
  714. while (cell_len--) {
  715. load64 = (load64 << 32) | uimage_to_cpu(*cell);
  716. cell++;
  717. }
  718. *load = (ulong)load64;
  719. return 0;
  720. }
  721. /**
  722. * fit_image_get_load() - get load addr property for given component image node
  723. * @fit: pointer to the FIT format image header
  724. * @noffset: component image node offset
  725. * @load: pointer to the uint32_t, will hold load address
  726. *
  727. * fit_image_get_load() finds load address property in a given component
  728. * image node. If the property is found, its value is returned to the caller.
  729. *
  730. * returns:
  731. * 0, on success
  732. * -1, on failure
  733. */
  734. int fit_image_get_load(const void *fit, int noffset, ulong *load)
  735. {
  736. return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
  737. }
  738. /**
  739. * fit_image_get_entry() - get entry point address property
  740. * @fit: pointer to the FIT format image header
  741. * @noffset: component image node offset
  742. * @entry: pointer to the uint32_t, will hold entry point address
  743. *
  744. * This gets the entry point address property for a given component image
  745. * node.
  746. *
  747. * fit_image_get_entry() finds entry point address property in a given
  748. * component image node. If the property is found, its value is returned
  749. * to the caller.
  750. *
  751. * returns:
  752. * 0, on success
  753. * -1, on failure
  754. */
  755. int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
  756. {
  757. return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
  758. }
  759. /**
  760. * fit_image_get_data - get data property and its size for a given component image node
  761. * @fit: pointer to the FIT format image header
  762. * @noffset: component image node offset
  763. * @data: double pointer to void, will hold data property's data address
  764. * @size: pointer to size_t, will hold data property's data size
  765. *
  766. * fit_image_get_data() finds data property in a given component image node.
  767. * If the property is found its data start address and size are returned to
  768. * the caller.
  769. *
  770. * returns:
  771. * 0, on success
  772. * -1, on failure
  773. */
  774. int fit_image_get_data(const void *fit, int noffset,
  775. const void **data, size_t *size)
  776. {
  777. int len;
  778. *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
  779. if (*data == NULL) {
  780. fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
  781. *size = 0;
  782. return -1;
  783. }
  784. *size = len;
  785. return 0;
  786. }
  787. /**
  788. * Get 'data-offset' property from a given image node.
  789. *
  790. * @fit: pointer to the FIT image header
  791. * @noffset: component image node offset
  792. * @data_offset: holds the data-offset property
  793. *
  794. * returns:
  795. * 0, on success
  796. * -ENOENT if the property could not be found
  797. */
  798. int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
  799. {
  800. const fdt32_t *val;
  801. val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
  802. if (!val)
  803. return -ENOENT;
  804. *data_offset = fdt32_to_cpu(*val);
  805. return 0;
  806. }
  807. /**
  808. * Get 'data-position' property from a given image node.
  809. *
  810. * @fit: pointer to the FIT image header
  811. * @noffset: component image node offset
  812. * @data_position: holds the data-position property
  813. *
  814. * returns:
  815. * 0, on success
  816. * -ENOENT if the property could not be found
  817. */
  818. int fit_image_get_data_position(const void *fit, int noffset,
  819. int *data_position)
  820. {
  821. const fdt32_t *val;
  822. val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
  823. if (!val)
  824. return -ENOENT;
  825. *data_position = fdt32_to_cpu(*val);
  826. return 0;
  827. }
  828. /**
  829. * Get 'data-size' property from a given image node.
  830. *
  831. * @fit: pointer to the FIT image header
  832. * @noffset: component image node offset
  833. * @data_size: holds the data-size property
  834. *
  835. * returns:
  836. * 0, on success
  837. * -ENOENT if the property could not be found
  838. */
  839. int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
  840. {
  841. const fdt32_t *val;
  842. val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
  843. if (!val)
  844. return -ENOENT;
  845. *data_size = fdt32_to_cpu(*val);
  846. return 0;
  847. }
  848. /**
  849. * Get 'data-size-unciphered' property from a given image node.
  850. *
  851. * @fit: pointer to the FIT image header
  852. * @noffset: component image node offset
  853. * @data_size: holds the data-size property
  854. *
  855. * returns:
  856. * 0, on success
  857. * -ENOENT if the property could not be found
  858. */
  859. int fit_image_get_data_size_unciphered(const void *fit, int noffset,
  860. size_t *data_size)
  861. {
  862. const fdt32_t *val;
  863. val = fdt_getprop(fit, noffset, "data-size-unciphered", NULL);
  864. if (!val)
  865. return -ENOENT;
  866. *data_size = (size_t)fdt32_to_cpu(*val);
  867. return 0;
  868. }
  869. /**
  870. * fit_image_get_data_and_size - get data and its size including
  871. * both embedded and external data
  872. * @fit: pointer to the FIT format image header
  873. * @noffset: component image node offset
  874. * @data: double pointer to void, will hold data property's data address
  875. * @size: pointer to size_t, will hold data property's data size
  876. *
  877. * fit_image_get_data_and_size() finds data and its size including
  878. * both embedded and external data. If the property is found
  879. * its data start address and size are returned to the caller.
  880. *
  881. * returns:
  882. * 0, on success
  883. * otherwise, on failure
  884. */
  885. int fit_image_get_data_and_size(const void *fit, int noffset,
  886. const void **data, size_t *size)
  887. {
  888. bool external_data = false;
  889. int offset;
  890. int len;
  891. int ret;
  892. if (!fit_image_get_data_position(fit, noffset, &offset)) {
  893. external_data = true;
  894. } else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
  895. external_data = true;
  896. /*
  897. * For FIT with external data, figure out where
  898. * the external images start. This is the base
  899. * for the data-offset properties in each image.
  900. */
  901. offset += ((fdt_totalsize(fit) + 3) & ~3);
  902. }
  903. if (external_data) {
  904. debug("External Data\n");
  905. ret = fit_image_get_data_size(fit, noffset, &len);
  906. if (!ret) {
  907. *data = fit + offset;
  908. *size = len;
  909. }
  910. } else {
  911. ret = fit_image_get_data(fit, noffset, data, size);
  912. }
  913. return ret;
  914. }
  915. /**
  916. * fit_image_hash_get_algo - get hash algorithm name
  917. * @fit: pointer to the FIT format image header
  918. * @noffset: hash node offset
  919. * @algo: double pointer to char, will hold pointer to the algorithm name
  920. *
  921. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  922. * If the property is found its data start address is returned to the caller.
  923. *
  924. * returns:
  925. * 0, on success
  926. * -1, on failure
  927. */
  928. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
  929. {
  930. int len;
  931. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  932. if (*algo == NULL) {
  933. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  934. return -1;
  935. }
  936. return 0;
  937. }
  938. /**
  939. * fit_image_hash_get_value - get hash value and length
  940. * @fit: pointer to the FIT format image header
  941. * @noffset: hash node offset
  942. * @value: double pointer to uint8_t, will hold address of a hash value data
  943. * @value_len: pointer to an int, will hold hash data length
  944. *
  945. * fit_image_hash_get_value() finds hash value property in a given hash node.
  946. * If the property is found its data start address and size are returned to
  947. * the caller.
  948. *
  949. * returns:
  950. * 0, on success
  951. * -1, on failure
  952. */
  953. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  954. int *value_len)
  955. {
  956. int len;
  957. *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
  958. if (*value == NULL) {
  959. fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
  960. *value_len = 0;
  961. return -1;
  962. }
  963. *value_len = len;
  964. return 0;
  965. }
  966. /**
  967. * fit_image_hash_get_ignore - get hash ignore flag
  968. * @fit: pointer to the FIT format image header
  969. * @noffset: hash node offset
  970. * @ignore: pointer to an int, will hold hash ignore flag
  971. *
  972. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  973. * If the property is found and non-zero, the hash algorithm is not verified by
  974. * u-boot automatically.
  975. *
  976. * returns:
  977. * 0, on ignore not found
  978. * value, on ignore found
  979. */
  980. static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  981. {
  982. int len;
  983. int *value;
  984. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  985. if (value == NULL || len != sizeof(int))
  986. *ignore = 0;
  987. else
  988. *ignore = *value;
  989. return 0;
  990. }
  991. /**
  992. * fit_image_cipher_get_algo - get cipher algorithm name
  993. * @fit: pointer to the FIT format image header
  994. * @noffset: cipher node offset
  995. * @algo: double pointer to char, will hold pointer to the algorithm name
  996. *
  997. * fit_image_cipher_get_algo() finds cipher algorithm property in a given
  998. * cipher node. If the property is found its data start address is returned
  999. * to the caller.
  1000. *
  1001. * returns:
  1002. * 0, on success
  1003. * -1, on failure
  1004. */
  1005. int fit_image_cipher_get_algo(const void *fit, int noffset, char **algo)
  1006. {
  1007. int len;
  1008. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  1009. if (!*algo) {
  1010. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  1011. return -1;
  1012. }
  1013. return 0;
  1014. }
  1015. ulong fit_get_end(const void *fit)
  1016. {
  1017. return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
  1018. }
  1019. /**
  1020. * fit_set_timestamp - set node timestamp property
  1021. * @fit: pointer to the FIT format image header
  1022. * @noffset: node offset
  1023. * @timestamp: timestamp value to be set
  1024. *
  1025. * fit_set_timestamp() attempts to set timestamp property in the requested
  1026. * node and returns operation status to the caller.
  1027. *
  1028. * returns:
  1029. * 0, on success
  1030. * -ENOSPC if no space in device tree, -1 for other error
  1031. */
  1032. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  1033. {
  1034. uint32_t t;
  1035. int ret;
  1036. t = cpu_to_uimage(timestamp);
  1037. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  1038. sizeof(uint32_t));
  1039. if (ret) {
  1040. debug("Can't set '%s' property for '%s' node (%s)\n",
  1041. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  1042. fdt_strerror(ret));
  1043. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
  1044. }
  1045. return 0;
  1046. }
  1047. /**
  1048. * calculate_hash - calculate and return hash for provided input data
  1049. * @data: pointer to the input data
  1050. * @data_len: data length
  1051. * @algo: requested hash algorithm
  1052. * @value: pointer to the char, will hold hash value data (caller must
  1053. * allocate enough free space)
  1054. * value_len: length of the calculated hash
  1055. *
  1056. * calculate_hash() computes input data hash according to the requested
  1057. * algorithm.
  1058. * Resulting hash value is placed in caller provided 'value' buffer, length
  1059. * of the calculated hash is returned via value_len pointer argument.
  1060. *
  1061. * returns:
  1062. * 0, on success
  1063. * -1, when algo is unsupported
  1064. */
  1065. int calculate_hash(const void *data, int data_len, const char *algo,
  1066. uint8_t *value, int *value_len)
  1067. {
  1068. if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  1069. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  1070. CHUNKSZ_CRC32);
  1071. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  1072. *value_len = 4;
  1073. } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
  1074. sha1_csum_wd((unsigned char *)data, data_len,
  1075. (unsigned char *)value, CHUNKSZ_SHA1);
  1076. *value_len = 20;
  1077. } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
  1078. sha256_csum_wd((unsigned char *)data, data_len,
  1079. (unsigned char *)value, CHUNKSZ_SHA256);
  1080. *value_len = SHA256_SUM_LEN;
  1081. } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
  1082. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  1083. *value_len = 16;
  1084. } else {
  1085. debug("Unsupported hash alogrithm\n");
  1086. return -1;
  1087. }
  1088. return 0;
  1089. }
  1090. static int fit_image_check_hash(const void *fit, int noffset, const void *data,
  1091. size_t size, char **err_msgp)
  1092. {
  1093. uint8_t value[FIT_MAX_HASH_LEN];
  1094. int value_len;
  1095. char *algo;
  1096. uint8_t *fit_value;
  1097. int fit_value_len;
  1098. int ignore;
  1099. *err_msgp = NULL;
  1100. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  1101. *err_msgp = "Can't get hash algo property";
  1102. return -1;
  1103. }
  1104. printf("%s", algo);
  1105. if (IMAGE_ENABLE_IGNORE) {
  1106. fit_image_hash_get_ignore(fit, noffset, &ignore);
  1107. if (ignore) {
  1108. printf("-skipped ");
  1109. return 0;
  1110. }
  1111. }
  1112. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  1113. &fit_value_len)) {
  1114. *err_msgp = "Can't get hash value property";
  1115. return -1;
  1116. }
  1117. if (calculate_hash(data, size, algo, value, &value_len)) {
  1118. *err_msgp = "Unsupported hash algorithm";
  1119. return -1;
  1120. }
  1121. if (value_len != fit_value_len) {
  1122. *err_msgp = "Bad hash value len";
  1123. return -1;
  1124. } else if (memcmp(value, fit_value, value_len) != 0) {
  1125. *err_msgp = "Bad hash value";
  1126. return -1;
  1127. }
  1128. return 0;
  1129. }
  1130. int fit_image_verify_with_data(const void *fit, int image_noffset,
  1131. const void *data, size_t size)
  1132. {
  1133. int noffset = 0;
  1134. char *err_msg = "";
  1135. int verify_all = 1;
  1136. int ret;
  1137. /* Verify all required signatures */
  1138. if (FIT_IMAGE_ENABLE_VERIFY &&
  1139. fit_image_verify_required_sigs(fit, image_noffset, data, size,
  1140. gd_fdt_blob(), &verify_all)) {
  1141. err_msg = "Unable to verify required signature";
  1142. goto error;
  1143. }
  1144. /* Process all hash subnodes of the component image node */
  1145. fdt_for_each_subnode(noffset, fit, image_noffset) {
  1146. const char *name = fit_get_name(fit, noffset, NULL);
  1147. /*
  1148. * Check subnode name, must be equal to "hash".
  1149. * Multiple hash nodes require unique unit node
  1150. * names, e.g. hash-1, hash-2, etc.
  1151. */
  1152. if (!strncmp(name, FIT_HASH_NODENAME,
  1153. strlen(FIT_HASH_NODENAME))) {
  1154. if (fit_image_check_hash(fit, noffset, data, size,
  1155. &err_msg))
  1156. goto error;
  1157. puts("+ ");
  1158. } else if (FIT_IMAGE_ENABLE_VERIFY && verify_all &&
  1159. !strncmp(name, FIT_SIG_NODENAME,
  1160. strlen(FIT_SIG_NODENAME))) {
  1161. ret = fit_image_check_sig(fit, noffset, data,
  1162. size, -1, &err_msg);
  1163. /*
  1164. * Show an indication on failure, but do not return
  1165. * an error. Only keys marked 'required' can cause
  1166. * an image validation failure. See the call to
  1167. * fit_image_verify_required_sigs() above.
  1168. */
  1169. if (ret)
  1170. puts("- ");
  1171. else
  1172. puts("+ ");
  1173. }
  1174. }
  1175. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  1176. err_msg = "Corrupted or truncated tree";
  1177. goto error;
  1178. }
  1179. return 1;
  1180. error:
  1181. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  1182. err_msg, fit_get_name(fit, noffset, NULL),
  1183. fit_get_name(fit, image_noffset, NULL));
  1184. return 0;
  1185. }
  1186. /**
  1187. * fit_image_verify - verify data integrity
  1188. * @fit: pointer to the FIT format image header
  1189. * @image_noffset: component image node offset
  1190. *
  1191. * fit_image_verify() goes over component image hash nodes,
  1192. * re-calculates each data hash and compares with the value stored in hash
  1193. * node.
  1194. *
  1195. * returns:
  1196. * 1, if all hashes are valid
  1197. * 0, otherwise (or on error)
  1198. */
  1199. int fit_image_verify(const void *fit, int image_noffset)
  1200. {
  1201. const void *data;
  1202. size_t size;
  1203. int noffset = 0;
  1204. char *err_msg = "";
  1205. /* Get image data and data length */
  1206. if (fit_image_get_data_and_size(fit, image_noffset, &data, &size)) {
  1207. err_msg = "Can't get image data/size";
  1208. printf("error!\n%s for '%s' hash node in '%s' image node\n",
  1209. err_msg, fit_get_name(fit, noffset, NULL),
  1210. fit_get_name(fit, image_noffset, NULL));
  1211. return 0;
  1212. }
  1213. return fit_image_verify_with_data(fit, image_noffset, data, size);
  1214. }
  1215. /**
  1216. * fit_all_image_verify - verify data integrity for all images
  1217. * @fit: pointer to the FIT format image header
  1218. *
  1219. * fit_all_image_verify() goes over all images in the FIT and
  1220. * for every images checks if all it's hashes are valid.
  1221. *
  1222. * returns:
  1223. * 1, if all hashes of all images are valid
  1224. * 0, otherwise (or on error)
  1225. */
  1226. int fit_all_image_verify(const void *fit)
  1227. {
  1228. int images_noffset;
  1229. int noffset;
  1230. int ndepth;
  1231. int count;
  1232. /* Find images parent node offset */
  1233. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1234. if (images_noffset < 0) {
  1235. printf("Can't find images parent node '%s' (%s)\n",
  1236. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  1237. return 0;
  1238. }
  1239. /* Process all image subnodes, check hashes for each */
  1240. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  1241. (ulong)fit);
  1242. for (ndepth = 0, count = 0,
  1243. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  1244. (noffset >= 0) && (ndepth > 0);
  1245. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1246. if (ndepth == 1) {
  1247. /*
  1248. * Direct child node of the images parent node,
  1249. * i.e. component image node.
  1250. */
  1251. printf(" Hash(es) for Image %u (%s): ", count,
  1252. fit_get_name(fit, noffset, NULL));
  1253. count++;
  1254. if (!fit_image_verify(fit, noffset))
  1255. return 0;
  1256. printf("\n");
  1257. }
  1258. }
  1259. return 1;
  1260. }
  1261. #ifdef CONFIG_FIT_CIPHER
  1262. static int fit_image_uncipher(const void *fit, int image_noffset,
  1263. void **data, size_t *size)
  1264. {
  1265. int cipher_noffset, ret;
  1266. void *dst;
  1267. size_t size_dst;
  1268. cipher_noffset = fdt_subnode_offset(fit, image_noffset,
  1269. FIT_CIPHER_NODENAME);
  1270. if (cipher_noffset < 0)
  1271. return 0;
  1272. ret = fit_image_decrypt_data(fit, image_noffset, cipher_noffset,
  1273. *data, *size, &dst, &size_dst);
  1274. if (ret)
  1275. goto out;
  1276. *data = dst;
  1277. *size = size_dst;
  1278. out:
  1279. return ret;
  1280. }
  1281. #endif /* CONFIG_FIT_CIPHER */
  1282. /**
  1283. * fit_image_check_os - check whether image node is of a given os type
  1284. * @fit: pointer to the FIT format image header
  1285. * @noffset: component image node offset
  1286. * @os: requested image os
  1287. *
  1288. * fit_image_check_os() reads image os property and compares its numeric
  1289. * id with the requested os. Comparison result is returned to the caller.
  1290. *
  1291. * returns:
  1292. * 1 if image is of given os type
  1293. * 0 otherwise (or on error)
  1294. */
  1295. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1296. {
  1297. uint8_t image_os;
  1298. if (fit_image_get_os(fit, noffset, &image_os))
  1299. return 0;
  1300. return (os == image_os);
  1301. }
  1302. /**
  1303. * fit_image_check_arch - check whether image node is of a given arch
  1304. * @fit: pointer to the FIT format image header
  1305. * @noffset: component image node offset
  1306. * @arch: requested imagearch
  1307. *
  1308. * fit_image_check_arch() reads image arch property and compares its numeric
  1309. * id with the requested arch. Comparison result is returned to the caller.
  1310. *
  1311. * returns:
  1312. * 1 if image is of given arch
  1313. * 0 otherwise (or on error)
  1314. */
  1315. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1316. {
  1317. uint8_t image_arch;
  1318. int aarch32_support = 0;
  1319. #ifdef CONFIG_ARM64_SUPPORT_AARCH32
  1320. aarch32_support = 1;
  1321. #endif
  1322. if (fit_image_get_arch(fit, noffset, &image_arch))
  1323. return 0;
  1324. return (arch == image_arch) ||
  1325. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
  1326. (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
  1327. aarch32_support);
  1328. }
  1329. /**
  1330. * fit_image_check_type - check whether image node is of a given type
  1331. * @fit: pointer to the FIT format image header
  1332. * @noffset: component image node offset
  1333. * @type: requested image type
  1334. *
  1335. * fit_image_check_type() reads image type property and compares its numeric
  1336. * id with the requested type. Comparison result is returned to the caller.
  1337. *
  1338. * returns:
  1339. * 1 if image is of given type
  1340. * 0 otherwise (or on error)
  1341. */
  1342. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1343. {
  1344. uint8_t image_type;
  1345. if (fit_image_get_type(fit, noffset, &image_type))
  1346. return 0;
  1347. return (type == image_type);
  1348. }
  1349. /**
  1350. * fit_image_check_comp - check whether image node uses given compression
  1351. * @fit: pointer to the FIT format image header
  1352. * @noffset: component image node offset
  1353. * @comp: requested image compression type
  1354. *
  1355. * fit_image_check_comp() reads image compression property and compares its
  1356. * numeric id with the requested compression type. Comparison result is
  1357. * returned to the caller.
  1358. *
  1359. * returns:
  1360. * 1 if image uses requested compression
  1361. * 0 otherwise (or on error)
  1362. */
  1363. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1364. {
  1365. uint8_t image_comp;
  1366. if (fit_image_get_comp(fit, noffset, &image_comp))
  1367. return 0;
  1368. return (comp == image_comp);
  1369. }
  1370. /**
  1371. * fit_check_format - sanity check FIT image format
  1372. * @fit: pointer to the FIT format image header
  1373. *
  1374. * fit_check_format() runs a basic sanity FIT image verification.
  1375. * Routine checks for mandatory properties, nodes, etc.
  1376. *
  1377. * returns:
  1378. * 1, on success
  1379. * 0, on failure
  1380. */
  1381. int fit_check_format(const void *fit)
  1382. {
  1383. /* mandatory / node 'description' property */
  1384. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1385. debug("Wrong FIT format: no description\n");
  1386. return 0;
  1387. }
  1388. if (IMAGE_ENABLE_TIMESTAMP) {
  1389. /* mandatory / node 'timestamp' property */
  1390. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1391. debug("Wrong FIT format: no timestamp\n");
  1392. return 0;
  1393. }
  1394. }
  1395. /* mandatory subimages parent '/images' node */
  1396. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1397. debug("Wrong FIT format: no images parent node\n");
  1398. return 0;
  1399. }
  1400. return 1;
  1401. }
  1402. /**
  1403. * fit_conf_find_compat
  1404. * @fit: pointer to the FIT format image header
  1405. * @fdt: pointer to the device tree to compare against
  1406. *
  1407. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1408. * most compatible with the passed in device tree.
  1409. *
  1410. * Example:
  1411. *
  1412. * / o image-tree
  1413. * |-o images
  1414. * | |-o fdt-1
  1415. * | |-o fdt-2
  1416. * |
  1417. * |-o configurations
  1418. * |-o config-1
  1419. * | |-fdt = fdt-1
  1420. * |
  1421. * |-o config-2
  1422. * |-fdt = fdt-2
  1423. *
  1424. * / o U-Boot fdt
  1425. * |-compatible = "foo,bar", "bim,bam"
  1426. *
  1427. * / o kernel fdt1
  1428. * |-compatible = "foo,bar",
  1429. *
  1430. * / o kernel fdt2
  1431. * |-compatible = "bim,bam", "baz,biz"
  1432. *
  1433. * Configuration 1 would be picked because the first string in U-Boot's
  1434. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1435. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1436. *
  1437. * As an optimization, the compatible property from the FDT's root node can be
  1438. * copied into the configuration node in the FIT image. This is required to
  1439. * match configurations with compressed FDTs.
  1440. *
  1441. * returns:
  1442. * offset to the configuration to use if one was found
  1443. * -1 otherwise
  1444. */
  1445. int fit_conf_find_compat(const void *fit, const void *fdt)
  1446. {
  1447. int ndepth = 0;
  1448. int noffset, confs_noffset, images_noffset;
  1449. const void *fdt_compat;
  1450. int fdt_compat_len;
  1451. int best_match_offset = 0;
  1452. int best_match_pos = 0;
  1453. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1454. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1455. if (confs_noffset < 0 || images_noffset < 0) {
  1456. debug("Can't find configurations or images nodes.\n");
  1457. return -1;
  1458. }
  1459. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1460. if (!fdt_compat) {
  1461. debug("Fdt for comparison has no \"compatible\" property.\n");
  1462. return -1;
  1463. }
  1464. /*
  1465. * Loop over the configurations in the FIT image.
  1466. */
  1467. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1468. (noffset >= 0) && (ndepth > 0);
  1469. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1470. const void *fdt;
  1471. const char *kfdt_name;
  1472. int kfdt_noffset, compat_noffset;
  1473. const char *cur_fdt_compat;
  1474. int len;
  1475. size_t sz;
  1476. int i;
  1477. if (ndepth > 1)
  1478. continue;
  1479. /* If there's a compat property in the config node, use that. */
  1480. if (fdt_getprop(fit, noffset, "compatible", NULL)) {
  1481. fdt = fit; /* search in FIT image */
  1482. compat_noffset = noffset; /* search under config node */
  1483. } else { /* Otherwise extract it from the kernel FDT. */
  1484. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1485. if (!kfdt_name) {
  1486. debug("No fdt property found.\n");
  1487. continue;
  1488. }
  1489. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1490. kfdt_name);
  1491. if (kfdt_noffset < 0) {
  1492. debug("No image node named \"%s\" found.\n",
  1493. kfdt_name);
  1494. continue;
  1495. }
  1496. if (!fit_image_check_comp(fit, kfdt_noffset,
  1497. IH_COMP_NONE)) {
  1498. debug("Can't extract compat from \"%s\" "
  1499. "(compressed)\n", kfdt_name);
  1500. continue;
  1501. }
  1502. /* search in this config's kernel FDT */
  1503. if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
  1504. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1505. continue;
  1506. }
  1507. compat_noffset = 0; /* search kFDT under root node */
  1508. }
  1509. len = fdt_compat_len;
  1510. cur_fdt_compat = fdt_compat;
  1511. /*
  1512. * Look for a match for each U-Boot compatibility string in
  1513. * turn in the compat string property.
  1514. */
  1515. for (i = 0; len > 0 &&
  1516. (!best_match_offset || best_match_pos > i); i++) {
  1517. int cur_len = strlen(cur_fdt_compat) + 1;
  1518. if (!fdt_node_check_compatible(fdt, compat_noffset,
  1519. cur_fdt_compat)) {
  1520. best_match_offset = noffset;
  1521. best_match_pos = i;
  1522. break;
  1523. }
  1524. len -= cur_len;
  1525. cur_fdt_compat += cur_len;
  1526. }
  1527. }
  1528. if (!best_match_offset) {
  1529. debug("No match found.\n");
  1530. return -1;
  1531. }
  1532. return best_match_offset;
  1533. }
  1534. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1535. {
  1536. int noffset, confs_noffset;
  1537. int len;
  1538. const char *s;
  1539. char *conf_uname_copy = NULL;
  1540. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1541. if (confs_noffset < 0) {
  1542. debug("Can't find configurations parent node '%s' (%s)\n",
  1543. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1544. return confs_noffset;
  1545. }
  1546. if (conf_uname == NULL) {
  1547. /* get configuration unit name from the default property */
  1548. debug("No configuration specified, trying default...\n");
  1549. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1550. FIT_DEFAULT_PROP, &len);
  1551. if (conf_uname == NULL) {
  1552. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1553. len);
  1554. return len;
  1555. }
  1556. debug("Found default configuration: '%s'\n", conf_uname);
  1557. }
  1558. s = strchr(conf_uname, '#');
  1559. if (s) {
  1560. len = s - conf_uname;
  1561. conf_uname_copy = malloc(len + 1);
  1562. if (!conf_uname_copy) {
  1563. debug("Can't allocate uname copy: '%s'\n",
  1564. conf_uname);
  1565. return -ENOMEM;
  1566. }
  1567. memcpy(conf_uname_copy, conf_uname, len);
  1568. conf_uname_copy[len] = '\0';
  1569. conf_uname = conf_uname_copy;
  1570. }
  1571. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1572. if (noffset < 0) {
  1573. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1574. conf_uname, fdt_strerror(noffset));
  1575. }
  1576. if (conf_uname_copy)
  1577. free(conf_uname_copy);
  1578. return noffset;
  1579. }
  1580. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1581. const char *prop_name)
  1582. {
  1583. return fdt_stringlist_count(fit, noffset, prop_name);
  1584. }
  1585. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1586. const char *prop_name, int index)
  1587. {
  1588. const char *uname;
  1589. int len;
  1590. /* get kernel image unit name from configuration kernel property */
  1591. uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
  1592. if (uname == NULL)
  1593. return len;
  1594. return fit_image_get_node(fit, uname);
  1595. }
  1596. int fit_conf_get_prop_node(const void *fit, int noffset,
  1597. const char *prop_name)
  1598. {
  1599. return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
  1600. }
  1601. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1602. {
  1603. fit_image_print(fit, rd_noffset, " ");
  1604. if (verify) {
  1605. puts(" Verifying Hash Integrity ... ");
  1606. if (!fit_image_verify(fit, rd_noffset)) {
  1607. puts("Bad Data Hash\n");
  1608. return -EACCES;
  1609. }
  1610. puts("OK\n");
  1611. }
  1612. return 0;
  1613. }
  1614. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1615. ulong addr)
  1616. {
  1617. int cfg_noffset;
  1618. void *fit_hdr;
  1619. int noffset;
  1620. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1621. prop_name, images->fit_uname_cfg, addr);
  1622. /* Check whether configuration has this property defined */
  1623. fit_hdr = map_sysmem(addr, 0);
  1624. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1625. if (cfg_noffset < 0) {
  1626. debug("* %s: no such config\n", prop_name);
  1627. return -EINVAL;
  1628. }
  1629. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1630. if (noffset < 0) {
  1631. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1632. return -ENOENT;
  1633. }
  1634. return noffset;
  1635. }
  1636. /**
  1637. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1638. *
  1639. * @return the properly name where we expect to find the image in the
  1640. * config node
  1641. */
  1642. static const char *fit_get_image_type_property(int type)
  1643. {
  1644. /*
  1645. * This is sort-of available in the uimage_type[] table in image.c
  1646. * but we don't have access to the short name, and "fdt" is different
  1647. * anyway. So let's just keep it here.
  1648. */
  1649. switch (type) {
  1650. case IH_TYPE_FLATDT:
  1651. return FIT_FDT_PROP;
  1652. case IH_TYPE_KERNEL:
  1653. return FIT_KERNEL_PROP;
  1654. case IH_TYPE_RAMDISK:
  1655. return FIT_RAMDISK_PROP;
  1656. case IH_TYPE_X86_SETUP:
  1657. return FIT_SETUP_PROP;
  1658. case IH_TYPE_LOADABLE:
  1659. return FIT_LOADABLE_PROP;
  1660. case IH_TYPE_FPGA:
  1661. return FIT_FPGA_PROP;
  1662. case IH_TYPE_STANDALONE:
  1663. return FIT_STANDALONE_PROP;
  1664. }
  1665. return "unknown";
  1666. }
  1667. int fit_image_load(bootm_headers_t *images, ulong addr,
  1668. const char **fit_unamep, const char **fit_uname_configp,
  1669. int arch, int image_type, int bootstage_id,
  1670. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1671. {
  1672. int cfg_noffset, noffset;
  1673. const char *fit_uname;
  1674. const char *fit_uname_config;
  1675. const char *fit_base_uname_config;
  1676. const void *fit;
  1677. void *buf;
  1678. void *loadbuf;
  1679. size_t size;
  1680. int type_ok, os_ok;
  1681. ulong load, load_end, data, len;
  1682. uint8_t os, comp;
  1683. #ifndef USE_HOSTCC
  1684. uint8_t os_arch;
  1685. #endif
  1686. const char *prop_name;
  1687. int ret;
  1688. fit = map_sysmem(addr, 0);
  1689. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1690. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1691. fit_base_uname_config = NULL;
  1692. prop_name = fit_get_image_type_property(image_type);
  1693. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1694. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1695. if (!fit_check_format(fit)) {
  1696. printf("Bad FIT %s image format!\n", prop_name);
  1697. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1698. return -ENOEXEC;
  1699. }
  1700. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1701. if (fit_uname) {
  1702. /* get FIT component image node offset */
  1703. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1704. noffset = fit_image_get_node(fit, fit_uname);
  1705. } else {
  1706. /*
  1707. * no image node unit name, try to get config
  1708. * node first. If config unit node name is NULL
  1709. * fit_conf_get_node() will try to find default config node
  1710. */
  1711. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1712. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1713. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1714. } else {
  1715. cfg_noffset = fit_conf_get_node(fit,
  1716. fit_uname_config);
  1717. }
  1718. if (cfg_noffset < 0) {
  1719. puts("Could not find configuration node\n");
  1720. bootstage_error(bootstage_id +
  1721. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1722. return -ENOENT;
  1723. }
  1724. fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1725. printf(" Using '%s' configuration\n", fit_base_uname_config);
  1726. /* Remember this config */
  1727. if (image_type == IH_TYPE_KERNEL)
  1728. images->fit_uname_cfg = fit_base_uname_config;
  1729. if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
  1730. puts(" Verifying Hash Integrity ... ");
  1731. if (fit_config_verify(fit, cfg_noffset)) {
  1732. puts("Bad Data Hash\n");
  1733. bootstage_error(bootstage_id +
  1734. BOOTSTAGE_SUB_HASH);
  1735. return -EACCES;
  1736. }
  1737. puts("OK\n");
  1738. }
  1739. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1740. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1741. prop_name);
  1742. fit_uname = fit_get_name(fit, noffset, NULL);
  1743. }
  1744. if (noffset < 0) {
  1745. printf("Could not find subimage node type '%s'\n", prop_name);
  1746. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1747. return -ENOENT;
  1748. }
  1749. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1750. ret = fit_image_select(fit, noffset, images->verify);
  1751. if (ret) {
  1752. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1753. return ret;
  1754. }
  1755. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1756. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1757. if (!fit_image_check_target_arch(fit, noffset)) {
  1758. puts("Unsupported Architecture\n");
  1759. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1760. return -ENOEXEC;
  1761. }
  1762. #endif
  1763. #ifndef USE_HOSTCC
  1764. fit_image_get_arch(fit, noffset, &os_arch);
  1765. images->os.arch = os_arch;
  1766. #endif
  1767. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1768. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1769. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1770. (image_type == IH_TYPE_KERNEL &&
  1771. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1772. os_ok = image_type == IH_TYPE_FLATDT ||
  1773. image_type == IH_TYPE_FPGA ||
  1774. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1775. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1776. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
  1777. fit_image_check_os(fit, noffset, IH_OS_EFI) ||
  1778. fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
  1779. /*
  1780. * If either of the checks fail, we should report an error, but
  1781. * if the image type is coming from the "loadables" field, we
  1782. * don't care what it is
  1783. */
  1784. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1785. fit_image_get_os(fit, noffset, &os);
  1786. printf("No %s %s %s Image\n",
  1787. genimg_get_os_name(os),
  1788. genimg_get_arch_name(arch),
  1789. genimg_get_type_name(image_type));
  1790. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1791. return -EIO;
  1792. }
  1793. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1794. /* get image data address and length */
  1795. if (fit_image_get_data_and_size(fit, noffset,
  1796. (const void **)&buf, &size)) {
  1797. printf("Could not find %s subimage data!\n", prop_name);
  1798. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1799. return -ENOENT;
  1800. }
  1801. #ifdef CONFIG_FIT_CIPHER
  1802. /* Decrypt data before uncompress/move */
  1803. if (IMAGE_ENABLE_DECRYPT) {
  1804. puts(" Decrypting Data ... ");
  1805. if (fit_image_uncipher(fit, noffset, &buf, &size)) {
  1806. puts("Error\n");
  1807. return -EACCES;
  1808. }
  1809. puts("OK\n");
  1810. }
  1811. #endif
  1812. #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1813. /* perform any post-processing on the image data */
  1814. board_fit_image_post_process(&buf, &size);
  1815. #endif
  1816. len = (ulong)size;
  1817. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1818. data = map_to_sysmem(buf);
  1819. load = data;
  1820. if (load_op == FIT_LOAD_IGNORED) {
  1821. /* Don't load */
  1822. } else if (fit_image_get_load(fit, noffset, &load)) {
  1823. if (load_op == FIT_LOAD_REQUIRED) {
  1824. printf("Can't get %s subimage load address!\n",
  1825. prop_name);
  1826. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1827. return -EBADF;
  1828. }
  1829. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1830. ulong image_start, image_end;
  1831. /*
  1832. * move image data to the load address,
  1833. * make sure we don't overwrite initial image
  1834. */
  1835. image_start = addr;
  1836. image_end = addr + fit_get_size(fit);
  1837. load_end = load + len;
  1838. if (image_type != IH_TYPE_KERNEL &&
  1839. load < image_end && load_end > image_start) {
  1840. printf("Error: %s overwritten\n", prop_name);
  1841. return -EXDEV;
  1842. }
  1843. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1844. prop_name, data, load);
  1845. } else {
  1846. load = data; /* No load address specified */
  1847. }
  1848. comp = IH_COMP_NONE;
  1849. loadbuf = buf;
  1850. /* Kernel images get decompressed later in bootm_load_os(). */
  1851. if (!fit_image_get_comp(fit, noffset, &comp) &&
  1852. comp != IH_COMP_NONE &&
  1853. !(image_type == IH_TYPE_KERNEL ||
  1854. image_type == IH_TYPE_KERNEL_NOLOAD ||
  1855. image_type == IH_TYPE_RAMDISK)) {
  1856. ulong max_decomp_len = len * 20;
  1857. if (load == data) {
  1858. loadbuf = malloc(max_decomp_len);
  1859. load = map_to_sysmem(loadbuf);
  1860. } else {
  1861. loadbuf = map_sysmem(load, max_decomp_len);
  1862. }
  1863. if (image_decomp(comp, load, data, image_type,
  1864. loadbuf, buf, len, max_decomp_len, &load_end)) {
  1865. printf("Error decompressing %s\n", prop_name);
  1866. return -ENOEXEC;
  1867. }
  1868. len = load_end - load;
  1869. } else if (load != data) {
  1870. loadbuf = map_sysmem(load, len);
  1871. memcpy(loadbuf, buf, len);
  1872. }
  1873. if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
  1874. puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
  1875. " please fix your .its file!\n");
  1876. /* verify that image data is a proper FDT blob */
  1877. if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
  1878. puts("Subimage data is not a FDT");
  1879. return -ENOEXEC;
  1880. }
  1881. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1882. *datap = load;
  1883. *lenp = len;
  1884. if (fit_unamep)
  1885. *fit_unamep = (char *)fit_uname;
  1886. if (fit_uname_configp)
  1887. *fit_uname_configp = (char *)(fit_uname_config ? :
  1888. fit_base_uname_config);
  1889. return noffset;
  1890. }
  1891. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1892. ulong *setup_start, ulong *setup_len)
  1893. {
  1894. int noffset;
  1895. ulong addr;
  1896. ulong len;
  1897. int ret;
  1898. addr = map_to_sysmem(images->fit_hdr_os);
  1899. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1900. if (noffset < 0)
  1901. return noffset;
  1902. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1903. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1904. FIT_LOAD_REQUIRED, setup_start, &len);
  1905. return ret;
  1906. }
  1907. #ifndef USE_HOSTCC
  1908. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  1909. const char **fit_unamep, const char **fit_uname_configp,
  1910. int arch, ulong *datap, ulong *lenp)
  1911. {
  1912. int fdt_noffset, cfg_noffset, count;
  1913. const void *fit;
  1914. const char *fit_uname = NULL;
  1915. const char *fit_uname_config = NULL;
  1916. char *fit_uname_config_copy = NULL;
  1917. char *next_config = NULL;
  1918. ulong load, len;
  1919. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1920. ulong image_start, image_end;
  1921. ulong ovload, ovlen;
  1922. const char *uconfig;
  1923. const char *uname;
  1924. void *base, *ov;
  1925. int i, err, noffset, ov_noffset;
  1926. #endif
  1927. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1928. if (fit_uname_configp && *fit_uname_configp) {
  1929. fit_uname_config_copy = strdup(*fit_uname_configp);
  1930. if (!fit_uname_config_copy)
  1931. return -ENOMEM;
  1932. next_config = strchr(fit_uname_config_copy, '#');
  1933. if (next_config)
  1934. *next_config++ = '\0';
  1935. if (next_config - 1 > fit_uname_config_copy)
  1936. fit_uname_config = fit_uname_config_copy;
  1937. }
  1938. fdt_noffset = fit_image_load(images,
  1939. addr, &fit_uname, &fit_uname_config,
  1940. arch, IH_TYPE_FLATDT,
  1941. BOOTSTAGE_ID_FIT_FDT_START,
  1942. FIT_LOAD_OPTIONAL, &load, &len);
  1943. if (fdt_noffset < 0)
  1944. goto out;
  1945. debug("fit_uname=%s, fit_uname_config=%s\n",
  1946. fit_uname ? fit_uname : "<NULL>",
  1947. fit_uname_config ? fit_uname_config : "<NULL>");
  1948. fit = map_sysmem(addr, 0);
  1949. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  1950. /* single blob, or error just return as well */
  1951. count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
  1952. if (count <= 1 && !next_config)
  1953. goto out;
  1954. /* we need to apply overlays */
  1955. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1956. image_start = addr;
  1957. image_end = addr + fit_get_size(fit);
  1958. /* verify that relocation took place by load address not being in fit */
  1959. if (load >= image_start && load < image_end) {
  1960. /* check is simplified; fit load checks for overlaps */
  1961. printf("Overlayed FDT requires relocation\n");
  1962. fdt_noffset = -EBADF;
  1963. goto out;
  1964. }
  1965. base = map_sysmem(load, len);
  1966. /* apply extra configs in FIT first, followed by args */
  1967. for (i = 1; ; i++) {
  1968. if (i < count) {
  1969. noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
  1970. FIT_FDT_PROP, i);
  1971. uname = fit_get_name(fit, noffset, NULL);
  1972. uconfig = NULL;
  1973. } else {
  1974. if (!next_config)
  1975. break;
  1976. uconfig = next_config;
  1977. next_config = strchr(next_config, '#');
  1978. if (next_config)
  1979. *next_config++ = '\0';
  1980. uname = NULL;
  1981. /*
  1982. * fit_image_load() would load the first FDT from the
  1983. * extra config only when uconfig is specified.
  1984. * Check if the extra config contains multiple FDTs and
  1985. * if so, load them.
  1986. */
  1987. cfg_noffset = fit_conf_get_node(fit, uconfig);
  1988. i = 0;
  1989. count = fit_conf_get_prop_node_count(fit, cfg_noffset,
  1990. FIT_FDT_PROP);
  1991. }
  1992. debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
  1993. ov_noffset = fit_image_load(images,
  1994. addr, &uname, &uconfig,
  1995. arch, IH_TYPE_FLATDT,
  1996. BOOTSTAGE_ID_FIT_FDT_START,
  1997. FIT_LOAD_REQUIRED, &ovload, &ovlen);
  1998. if (ov_noffset < 0) {
  1999. printf("load of %s failed\n", uname);
  2000. continue;
  2001. }
  2002. debug("%s loaded at 0x%08lx len=0x%08lx\n",
  2003. uname, ovload, ovlen);
  2004. ov = map_sysmem(ovload, ovlen);
  2005. base = map_sysmem(load, len + ovlen);
  2006. err = fdt_open_into(base, base, len + ovlen);
  2007. if (err < 0) {
  2008. printf("failed on fdt_open_into\n");
  2009. fdt_noffset = err;
  2010. goto out;
  2011. }
  2012. /* the verbose method prints out messages on error */
  2013. err = fdt_overlay_apply_verbose(base, ov);
  2014. if (err < 0) {
  2015. fdt_noffset = err;
  2016. goto out;
  2017. }
  2018. fdt_pack(base);
  2019. len = fdt_totalsize(base);
  2020. }
  2021. #else
  2022. printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
  2023. fdt_noffset = -EBADF;
  2024. #endif
  2025. out:
  2026. if (datap)
  2027. *datap = load;
  2028. if (lenp)
  2029. *lenp = len;
  2030. if (fit_unamep)
  2031. *fit_unamep = fit_uname;
  2032. if (fit_uname_configp)
  2033. *fit_uname_configp = fit_uname_config;
  2034. if (fit_uname_config_copy)
  2035. free(fit_uname_config_copy);
  2036. return fdt_noffset;
  2037. }
  2038. #endif