image-fit.c 62 KB

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