image-fit.c 57 KB

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