image-fit.c 63 KB

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