image-fit.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407
  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. /* Let's assume that sandbox can load any architecture */
  1352. if (IS_ENABLED(CONFIG_SANDBOX))
  1353. return true;
  1354. if (IS_ENABLED(CONFIG_ARM64_SUPPORT_AARCH32))
  1355. aarch32_support = 1;
  1356. if (fit_image_get_arch(fit, noffset, &image_arch))
  1357. return 0;
  1358. return (arch == image_arch) ||
  1359. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
  1360. (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
  1361. aarch32_support);
  1362. }
  1363. /**
  1364. * fit_image_check_type - check whether image node is of a given type
  1365. * @fit: pointer to the FIT format image header
  1366. * @noffset: component image node offset
  1367. * @type: requested image type
  1368. *
  1369. * fit_image_check_type() reads image type property and compares its numeric
  1370. * id with the requested type. Comparison result is returned to the caller.
  1371. *
  1372. * returns:
  1373. * 1 if image is of given type
  1374. * 0 otherwise (or on error)
  1375. */
  1376. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1377. {
  1378. uint8_t image_type;
  1379. if (fit_image_get_type(fit, noffset, &image_type))
  1380. return 0;
  1381. return (type == image_type);
  1382. }
  1383. /**
  1384. * fit_image_check_comp - check whether image node uses given compression
  1385. * @fit: pointer to the FIT format image header
  1386. * @noffset: component image node offset
  1387. * @comp: requested image compression type
  1388. *
  1389. * fit_image_check_comp() reads image compression property and compares its
  1390. * numeric id with the requested compression type. Comparison result is
  1391. * returned to the caller.
  1392. *
  1393. * returns:
  1394. * 1 if image uses requested compression
  1395. * 0 otherwise (or on error)
  1396. */
  1397. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1398. {
  1399. uint8_t image_comp;
  1400. if (fit_image_get_comp(fit, noffset, &image_comp))
  1401. return 0;
  1402. return (comp == image_comp);
  1403. }
  1404. /**
  1405. * fdt_check_no_at() - Check for nodes whose names contain '@'
  1406. *
  1407. * This checks the parent node and all subnodes recursively
  1408. *
  1409. * @fit: FIT to check
  1410. * @parent: Parent node to check
  1411. * @return 0 if OK, -EADDRNOTAVAIL is a node has a name containing '@'
  1412. */
  1413. static int fdt_check_no_at(const void *fit, int parent)
  1414. {
  1415. const char *name;
  1416. int node;
  1417. int ret;
  1418. name = fdt_get_name(fit, parent, NULL);
  1419. if (!name || strchr(name, '@'))
  1420. return -EADDRNOTAVAIL;
  1421. fdt_for_each_subnode(node, fit, parent) {
  1422. ret = fdt_check_no_at(fit, node);
  1423. if (ret)
  1424. return ret;
  1425. }
  1426. return 0;
  1427. }
  1428. int fit_check_format(const void *fit, ulong size)
  1429. {
  1430. int ret;
  1431. /* A FIT image must be a valid FDT */
  1432. ret = fdt_check_header(fit);
  1433. if (ret) {
  1434. log_debug("Wrong FIT format: not a flattened device tree (err=%d)\n",
  1435. ret);
  1436. return -ENOEXEC;
  1437. }
  1438. if (CONFIG_IS_ENABLED(FIT_FULL_CHECK)) {
  1439. /*
  1440. * If we are not given the size, make do wtih calculating it.
  1441. * This is not as secure, so we should consider a flag to
  1442. * control this.
  1443. */
  1444. if (size == IMAGE_SIZE_INVAL)
  1445. size = fdt_totalsize(fit);
  1446. ret = fdt_check_full(fit, size);
  1447. if (ret)
  1448. ret = -EINVAL;
  1449. /*
  1450. * U-Boot stopped using unit addressed in 2017. Since libfdt
  1451. * can match nodes ignoring any unit address, signature
  1452. * verification can see the wrong node if one is inserted with
  1453. * the same name as a valid node but with a unit address
  1454. * attached. Protect against this by disallowing unit addresses.
  1455. */
  1456. if (!ret && CONFIG_IS_ENABLED(FIT_SIGNATURE)) {
  1457. ret = fdt_check_no_at(fit, 0);
  1458. if (ret) {
  1459. log_debug("FIT check error %d\n", ret);
  1460. return ret;
  1461. }
  1462. }
  1463. if (ret) {
  1464. log_debug("FIT check error %d\n", ret);
  1465. return ret;
  1466. }
  1467. }
  1468. /* mandatory / node 'description' property */
  1469. if (!fdt_getprop(fit, 0, FIT_DESC_PROP, NULL)) {
  1470. log_debug("Wrong FIT format: no description\n");
  1471. return -ENOMSG;
  1472. }
  1473. if (IMAGE_ENABLE_TIMESTAMP) {
  1474. /* mandatory / node 'timestamp' property */
  1475. if (!fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL)) {
  1476. log_debug("Wrong FIT format: no timestamp\n");
  1477. return -EBADMSG;
  1478. }
  1479. }
  1480. /* mandatory subimages parent '/images' node */
  1481. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1482. log_debug("Wrong FIT format: no images parent node\n");
  1483. return -ENOENT;
  1484. }
  1485. return 0;
  1486. }
  1487. /**
  1488. * fit_conf_find_compat
  1489. * @fit: pointer to the FIT format image header
  1490. * @fdt: pointer to the device tree to compare against
  1491. *
  1492. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1493. * most compatible with the passed in device tree.
  1494. *
  1495. * Example:
  1496. *
  1497. * / o image-tree
  1498. * |-o images
  1499. * | |-o fdt-1
  1500. * | |-o fdt-2
  1501. * |
  1502. * |-o configurations
  1503. * |-o config-1
  1504. * | |-fdt = fdt-1
  1505. * |
  1506. * |-o config-2
  1507. * |-fdt = fdt-2
  1508. *
  1509. * / o U-Boot fdt
  1510. * |-compatible = "foo,bar", "bim,bam"
  1511. *
  1512. * / o kernel fdt1
  1513. * |-compatible = "foo,bar",
  1514. *
  1515. * / o kernel fdt2
  1516. * |-compatible = "bim,bam", "baz,biz"
  1517. *
  1518. * Configuration 1 would be picked because the first string in U-Boot's
  1519. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1520. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1521. *
  1522. * As an optimization, the compatible property from the FDT's root node can be
  1523. * copied into the configuration node in the FIT image. This is required to
  1524. * match configurations with compressed FDTs.
  1525. *
  1526. * returns:
  1527. * offset to the configuration to use if one was found
  1528. * -1 otherwise
  1529. */
  1530. int fit_conf_find_compat(const void *fit, const void *fdt)
  1531. {
  1532. int ndepth = 0;
  1533. int noffset, confs_noffset, images_noffset;
  1534. const void *fdt_compat;
  1535. int fdt_compat_len;
  1536. int best_match_offset = 0;
  1537. int best_match_pos = 0;
  1538. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1539. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1540. if (confs_noffset < 0 || images_noffset < 0) {
  1541. debug("Can't find configurations or images nodes.\n");
  1542. return -1;
  1543. }
  1544. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1545. if (!fdt_compat) {
  1546. debug("Fdt for comparison has no \"compatible\" property.\n");
  1547. return -1;
  1548. }
  1549. /*
  1550. * Loop over the configurations in the FIT image.
  1551. */
  1552. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1553. (noffset >= 0) && (ndepth > 0);
  1554. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1555. const void *fdt;
  1556. const char *kfdt_name;
  1557. int kfdt_noffset, compat_noffset;
  1558. const char *cur_fdt_compat;
  1559. int len;
  1560. size_t sz;
  1561. int i;
  1562. if (ndepth > 1)
  1563. continue;
  1564. /* If there's a compat property in the config node, use that. */
  1565. if (fdt_getprop(fit, noffset, "compatible", NULL)) {
  1566. fdt = fit; /* search in FIT image */
  1567. compat_noffset = noffset; /* search under config node */
  1568. } else { /* Otherwise extract it from the kernel FDT. */
  1569. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1570. if (!kfdt_name) {
  1571. debug("No fdt property found.\n");
  1572. continue;
  1573. }
  1574. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1575. kfdt_name);
  1576. if (kfdt_noffset < 0) {
  1577. debug("No image node named \"%s\" found.\n",
  1578. kfdt_name);
  1579. continue;
  1580. }
  1581. if (!fit_image_check_comp(fit, kfdt_noffset,
  1582. IH_COMP_NONE)) {
  1583. debug("Can't extract compat from \"%s\" "
  1584. "(compressed)\n", kfdt_name);
  1585. continue;
  1586. }
  1587. /* search in this config's kernel FDT */
  1588. if (fit_image_get_data(fit, kfdt_noffset, &fdt, &sz)) {
  1589. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1590. continue;
  1591. }
  1592. compat_noffset = 0; /* search kFDT under root node */
  1593. }
  1594. len = fdt_compat_len;
  1595. cur_fdt_compat = fdt_compat;
  1596. /*
  1597. * Look for a match for each U-Boot compatibility string in
  1598. * turn in the compat string property.
  1599. */
  1600. for (i = 0; len > 0 &&
  1601. (!best_match_offset || best_match_pos > i); i++) {
  1602. int cur_len = strlen(cur_fdt_compat) + 1;
  1603. if (!fdt_node_check_compatible(fdt, compat_noffset,
  1604. cur_fdt_compat)) {
  1605. best_match_offset = noffset;
  1606. best_match_pos = i;
  1607. break;
  1608. }
  1609. len -= cur_len;
  1610. cur_fdt_compat += cur_len;
  1611. }
  1612. }
  1613. if (!best_match_offset) {
  1614. debug("No match found.\n");
  1615. return -1;
  1616. }
  1617. return best_match_offset;
  1618. }
  1619. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1620. {
  1621. int noffset, confs_noffset;
  1622. int len;
  1623. const char *s;
  1624. char *conf_uname_copy = NULL;
  1625. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1626. if (confs_noffset < 0) {
  1627. debug("Can't find configurations parent node '%s' (%s)\n",
  1628. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1629. return confs_noffset;
  1630. }
  1631. if (conf_uname == NULL) {
  1632. /* get configuration unit name from the default property */
  1633. debug("No configuration specified, trying default...\n");
  1634. if (!host_build() && IS_ENABLED(CONFIG_MULTI_DTB_FIT)) {
  1635. noffset = fit_find_config_node(fit);
  1636. if (noffset < 0)
  1637. return noffset;
  1638. conf_uname = fdt_get_name(fit, noffset, NULL);
  1639. } else {
  1640. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1641. FIT_DEFAULT_PROP, &len);
  1642. if (conf_uname == NULL) {
  1643. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1644. len);
  1645. return len;
  1646. }
  1647. }
  1648. debug("Found default configuration: '%s'\n", conf_uname);
  1649. }
  1650. s = strchr(conf_uname, '#');
  1651. if (s) {
  1652. len = s - conf_uname;
  1653. conf_uname_copy = malloc(len + 1);
  1654. if (!conf_uname_copy) {
  1655. debug("Can't allocate uname copy: '%s'\n",
  1656. conf_uname);
  1657. return -ENOMEM;
  1658. }
  1659. memcpy(conf_uname_copy, conf_uname, len);
  1660. conf_uname_copy[len] = '\0';
  1661. conf_uname = conf_uname_copy;
  1662. }
  1663. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1664. if (noffset < 0) {
  1665. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1666. conf_uname, fdt_strerror(noffset));
  1667. }
  1668. if (conf_uname_copy)
  1669. free(conf_uname_copy);
  1670. return noffset;
  1671. }
  1672. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1673. const char *prop_name)
  1674. {
  1675. return fdt_stringlist_count(fit, noffset, prop_name);
  1676. }
  1677. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1678. const char *prop_name, int index)
  1679. {
  1680. const char *uname;
  1681. int len;
  1682. /* get kernel image unit name from configuration kernel property */
  1683. uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
  1684. if (uname == NULL)
  1685. return len;
  1686. return fit_image_get_node(fit, uname);
  1687. }
  1688. int fit_conf_get_prop_node(const void *fit, int noffset,
  1689. const char *prop_name)
  1690. {
  1691. return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
  1692. }
  1693. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1694. {
  1695. fit_image_print(fit, rd_noffset, " ");
  1696. if (verify) {
  1697. puts(" Verifying Hash Integrity ... ");
  1698. if (!fit_image_verify(fit, rd_noffset)) {
  1699. puts("Bad Data Hash\n");
  1700. return -EACCES;
  1701. }
  1702. puts("OK\n");
  1703. }
  1704. return 0;
  1705. }
  1706. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1707. ulong addr)
  1708. {
  1709. int cfg_noffset;
  1710. void *fit_hdr;
  1711. int noffset;
  1712. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1713. prop_name, images->fit_uname_cfg, addr);
  1714. /* Check whether configuration has this property defined */
  1715. fit_hdr = map_sysmem(addr, 0);
  1716. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1717. if (cfg_noffset < 0) {
  1718. debug("* %s: no such config\n", prop_name);
  1719. return -EINVAL;
  1720. }
  1721. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1722. if (noffset < 0) {
  1723. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1724. return -ENOENT;
  1725. }
  1726. return noffset;
  1727. }
  1728. /**
  1729. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1730. *
  1731. * @return the properly name where we expect to find the image in the
  1732. * config node
  1733. */
  1734. static const char *fit_get_image_type_property(int type)
  1735. {
  1736. /*
  1737. * This is sort-of available in the uimage_type[] table in image.c
  1738. * but we don't have access to the short name, and "fdt" is different
  1739. * anyway. So let's just keep it here.
  1740. */
  1741. switch (type) {
  1742. case IH_TYPE_FLATDT:
  1743. return FIT_FDT_PROP;
  1744. case IH_TYPE_KERNEL:
  1745. return FIT_KERNEL_PROP;
  1746. case IH_TYPE_FIRMWARE:
  1747. return FIT_FIRMWARE_PROP;
  1748. case IH_TYPE_RAMDISK:
  1749. return FIT_RAMDISK_PROP;
  1750. case IH_TYPE_X86_SETUP:
  1751. return FIT_SETUP_PROP;
  1752. case IH_TYPE_LOADABLE:
  1753. return FIT_LOADABLE_PROP;
  1754. case IH_TYPE_FPGA:
  1755. return FIT_FPGA_PROP;
  1756. case IH_TYPE_STANDALONE:
  1757. return FIT_STANDALONE_PROP;
  1758. }
  1759. return "unknown";
  1760. }
  1761. int fit_image_load(bootm_headers_t *images, ulong addr,
  1762. const char **fit_unamep, const char **fit_uname_configp,
  1763. int arch, int image_type, int bootstage_id,
  1764. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1765. {
  1766. int cfg_noffset, noffset;
  1767. const char *fit_uname;
  1768. const char *fit_uname_config;
  1769. const char *fit_base_uname_config;
  1770. const void *fit;
  1771. void *buf;
  1772. void *loadbuf;
  1773. size_t size;
  1774. int type_ok, os_ok;
  1775. ulong load, load_end, data, len;
  1776. uint8_t os, comp;
  1777. #ifndef USE_HOSTCC
  1778. uint8_t os_arch;
  1779. #endif
  1780. const char *prop_name;
  1781. int ret;
  1782. fit = map_sysmem(addr, 0);
  1783. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1784. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1785. fit_base_uname_config = NULL;
  1786. prop_name = fit_get_image_type_property(image_type);
  1787. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1788. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1789. ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
  1790. if (ret) {
  1791. printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
  1792. if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
  1793. printf("Signature checking prevents use of unit addresses (@) in nodes\n");
  1794. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1795. return ret;
  1796. }
  1797. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1798. if (fit_uname) {
  1799. /* get FIT component image node offset */
  1800. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1801. noffset = fit_image_get_node(fit, fit_uname);
  1802. } else {
  1803. /*
  1804. * no image node unit name, try to get config
  1805. * node first. If config unit node name is NULL
  1806. * fit_conf_get_node() will try to find default config node
  1807. */
  1808. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1809. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1810. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1811. } else {
  1812. cfg_noffset = fit_conf_get_node(fit,
  1813. fit_uname_config);
  1814. }
  1815. if (cfg_noffset < 0) {
  1816. puts("Could not find configuration node\n");
  1817. bootstage_error(bootstage_id +
  1818. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1819. return -ENOENT;
  1820. }
  1821. fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1822. printf(" Using '%s' configuration\n", fit_base_uname_config);
  1823. /* Remember this config */
  1824. if (image_type == IH_TYPE_KERNEL)
  1825. images->fit_uname_cfg = fit_base_uname_config;
  1826. if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
  1827. puts(" Verifying Hash Integrity ... ");
  1828. if (fit_config_verify(fit, cfg_noffset)) {
  1829. puts("Bad Data Hash\n");
  1830. bootstage_error(bootstage_id +
  1831. BOOTSTAGE_SUB_HASH);
  1832. return -EACCES;
  1833. }
  1834. puts("OK\n");
  1835. }
  1836. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1837. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1838. prop_name);
  1839. fit_uname = fit_get_name(fit, noffset, NULL);
  1840. }
  1841. if (noffset < 0) {
  1842. printf("Could not find subimage node type '%s'\n", prop_name);
  1843. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1844. return -ENOENT;
  1845. }
  1846. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1847. ret = fit_image_select(fit, noffset, images->verify);
  1848. if (ret) {
  1849. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1850. return ret;
  1851. }
  1852. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1853. if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
  1854. if (!fit_image_check_target_arch(fit, noffset)) {
  1855. puts("Unsupported Architecture\n");
  1856. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1857. return -ENOEXEC;
  1858. }
  1859. }
  1860. #ifndef USE_HOSTCC
  1861. fit_image_get_arch(fit, noffset, &os_arch);
  1862. images->os.arch = os_arch;
  1863. #endif
  1864. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1865. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1866. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1867. fit_image_check_type(fit, noffset, IH_TYPE_TEE) ||
  1868. (image_type == IH_TYPE_KERNEL &&
  1869. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1870. os_ok = image_type == IH_TYPE_FLATDT ||
  1871. image_type == IH_TYPE_FPGA ||
  1872. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1873. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1874. fit_image_check_os(fit, noffset, IH_OS_TEE) ||
  1875. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
  1876. fit_image_check_os(fit, noffset, IH_OS_EFI) ||
  1877. fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
  1878. /*
  1879. * If either of the checks fail, we should report an error, but
  1880. * if the image type is coming from the "loadables" field, we
  1881. * don't care what it is
  1882. */
  1883. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1884. fit_image_get_os(fit, noffset, &os);
  1885. printf("No %s %s %s Image\n",
  1886. genimg_get_os_name(os),
  1887. genimg_get_arch_name(arch),
  1888. genimg_get_type_name(image_type));
  1889. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1890. return -EIO;
  1891. }
  1892. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1893. /* get image data address and length */
  1894. if (fit_image_get_data_and_size(fit, noffset,
  1895. (const void **)&buf, &size)) {
  1896. printf("Could not find %s subimage data!\n", prop_name);
  1897. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1898. return -ENOENT;
  1899. }
  1900. /* Decrypt data before uncompress/move */
  1901. if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
  1902. puts(" Decrypting Data ... ");
  1903. if (fit_image_uncipher(fit, noffset, &buf, &size)) {
  1904. puts("Error\n");
  1905. return -EACCES;
  1906. }
  1907. puts("OK\n");
  1908. }
  1909. /* perform any post-processing on the image data */
  1910. if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
  1911. board_fit_image_post_process(&buf, &size);
  1912. len = (ulong)size;
  1913. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1914. data = map_to_sysmem(buf);
  1915. load = data;
  1916. if (load_op == FIT_LOAD_IGNORED) {
  1917. /* Don't load */
  1918. } else if (fit_image_get_load(fit, noffset, &load)) {
  1919. if (load_op == FIT_LOAD_REQUIRED) {
  1920. printf("Can't get %s subimage load address!\n",
  1921. prop_name);
  1922. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1923. return -EBADF;
  1924. }
  1925. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1926. ulong image_start, image_end;
  1927. /*
  1928. * move image data to the load address,
  1929. * make sure we don't overwrite initial image
  1930. */
  1931. image_start = addr;
  1932. image_end = addr + fit_get_size(fit);
  1933. load_end = load + len;
  1934. if (image_type != IH_TYPE_KERNEL &&
  1935. load < image_end && load_end > image_start) {
  1936. printf("Error: %s overwritten\n", prop_name);
  1937. return -EXDEV;
  1938. }
  1939. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1940. prop_name, data, load);
  1941. } else {
  1942. load = data; /* No load address specified */
  1943. }
  1944. comp = IH_COMP_NONE;
  1945. loadbuf = buf;
  1946. /* Kernel images get decompressed later in bootm_load_os(). */
  1947. if (!fit_image_get_comp(fit, noffset, &comp) &&
  1948. comp != IH_COMP_NONE &&
  1949. !(image_type == IH_TYPE_KERNEL ||
  1950. image_type == IH_TYPE_KERNEL_NOLOAD ||
  1951. image_type == IH_TYPE_RAMDISK)) {
  1952. ulong max_decomp_len = len * 20;
  1953. if (load == data) {
  1954. loadbuf = malloc(max_decomp_len);
  1955. load = map_to_sysmem(loadbuf);
  1956. } else {
  1957. loadbuf = map_sysmem(load, max_decomp_len);
  1958. }
  1959. if (image_decomp(comp, load, data, image_type,
  1960. loadbuf, buf, len, max_decomp_len, &load_end)) {
  1961. printf("Error decompressing %s\n", prop_name);
  1962. return -ENOEXEC;
  1963. }
  1964. len = load_end - load;
  1965. } else if (load != data) {
  1966. loadbuf = map_sysmem(load, len);
  1967. memcpy(loadbuf, buf, len);
  1968. }
  1969. if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
  1970. puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
  1971. " please fix your .its file!\n");
  1972. /* verify that image data is a proper FDT blob */
  1973. if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
  1974. puts("Subimage data is not a FDT");
  1975. return -ENOEXEC;
  1976. }
  1977. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1978. *datap = load;
  1979. *lenp = len;
  1980. if (fit_unamep)
  1981. *fit_unamep = (char *)fit_uname;
  1982. if (fit_uname_configp)
  1983. *fit_uname_configp = (char *)(fit_uname_config ? :
  1984. fit_base_uname_config);
  1985. return noffset;
  1986. }
  1987. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1988. ulong *setup_start, ulong *setup_len)
  1989. {
  1990. int noffset;
  1991. ulong addr;
  1992. ulong len;
  1993. int ret;
  1994. addr = map_to_sysmem(images->fit_hdr_os);
  1995. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1996. if (noffset < 0)
  1997. return noffset;
  1998. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1999. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  2000. FIT_LOAD_REQUIRED, setup_start, &len);
  2001. return ret;
  2002. }
  2003. #ifndef USE_HOSTCC
  2004. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  2005. const char **fit_unamep, const char **fit_uname_configp,
  2006. int arch, ulong *datap, ulong *lenp)
  2007. {
  2008. int fdt_noffset, cfg_noffset, count;
  2009. const void *fit;
  2010. const char *fit_uname = NULL;
  2011. const char *fit_uname_config = NULL;
  2012. char *fit_uname_config_copy = NULL;
  2013. char *next_config = NULL;
  2014. ulong load, len;
  2015. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  2016. ulong image_start, image_end;
  2017. ulong ovload, ovlen;
  2018. const char *uconfig;
  2019. const char *uname;
  2020. void *base, *ov;
  2021. int i, err, noffset, ov_noffset;
  2022. #endif
  2023. fit_uname = fit_unamep ? *fit_unamep : NULL;
  2024. if (fit_uname_configp && *fit_uname_configp) {
  2025. fit_uname_config_copy = strdup(*fit_uname_configp);
  2026. if (!fit_uname_config_copy)
  2027. return -ENOMEM;
  2028. next_config = strchr(fit_uname_config_copy, '#');
  2029. if (next_config)
  2030. *next_config++ = '\0';
  2031. if (next_config - 1 > fit_uname_config_copy)
  2032. fit_uname_config = fit_uname_config_copy;
  2033. }
  2034. fdt_noffset = fit_image_load(images,
  2035. addr, &fit_uname, &fit_uname_config,
  2036. arch, IH_TYPE_FLATDT,
  2037. BOOTSTAGE_ID_FIT_FDT_START,
  2038. FIT_LOAD_OPTIONAL, &load, &len);
  2039. if (fdt_noffset < 0)
  2040. goto out;
  2041. debug("fit_uname=%s, fit_uname_config=%s\n",
  2042. fit_uname ? fit_uname : "<NULL>",
  2043. fit_uname_config ? fit_uname_config : "<NULL>");
  2044. fit = map_sysmem(addr, 0);
  2045. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  2046. /* single blob, or error just return as well */
  2047. count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
  2048. if (count <= 1 && !next_config)
  2049. goto out;
  2050. /* we need to apply overlays */
  2051. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  2052. image_start = addr;
  2053. image_end = addr + fit_get_size(fit);
  2054. /* verify that relocation took place by load address not being in fit */
  2055. if (load >= image_start && load < image_end) {
  2056. /* check is simplified; fit load checks for overlaps */
  2057. printf("Overlayed FDT requires relocation\n");
  2058. fdt_noffset = -EBADF;
  2059. goto out;
  2060. }
  2061. base = map_sysmem(load, len);
  2062. /* apply extra configs in FIT first, followed by args */
  2063. for (i = 1; ; i++) {
  2064. if (i < count) {
  2065. noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
  2066. FIT_FDT_PROP, i);
  2067. uname = fit_get_name(fit, noffset, NULL);
  2068. uconfig = NULL;
  2069. } else {
  2070. if (!next_config)
  2071. break;
  2072. uconfig = next_config;
  2073. next_config = strchr(next_config, '#');
  2074. if (next_config)
  2075. *next_config++ = '\0';
  2076. uname = NULL;
  2077. /*
  2078. * fit_image_load() would load the first FDT from the
  2079. * extra config only when uconfig is specified.
  2080. * Check if the extra config contains multiple FDTs and
  2081. * if so, load them.
  2082. */
  2083. cfg_noffset = fit_conf_get_node(fit, uconfig);
  2084. i = 0;
  2085. count = fit_conf_get_prop_node_count(fit, cfg_noffset,
  2086. FIT_FDT_PROP);
  2087. }
  2088. debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
  2089. ov_noffset = fit_image_load(images,
  2090. addr, &uname, &uconfig,
  2091. arch, IH_TYPE_FLATDT,
  2092. BOOTSTAGE_ID_FIT_FDT_START,
  2093. FIT_LOAD_REQUIRED, &ovload, &ovlen);
  2094. if (ov_noffset < 0) {
  2095. printf("load of %s failed\n", uname);
  2096. continue;
  2097. }
  2098. debug("%s loaded at 0x%08lx len=0x%08lx\n",
  2099. uname, ovload, ovlen);
  2100. ov = map_sysmem(ovload, ovlen);
  2101. base = map_sysmem(load, len + ovlen);
  2102. err = fdt_open_into(base, base, len + ovlen);
  2103. if (err < 0) {
  2104. printf("failed on fdt_open_into\n");
  2105. fdt_noffset = err;
  2106. goto out;
  2107. }
  2108. /* the verbose method prints out messages on error */
  2109. err = fdt_overlay_apply_verbose(base, ov);
  2110. if (err < 0) {
  2111. fdt_noffset = err;
  2112. goto out;
  2113. }
  2114. fdt_pack(base);
  2115. len = fdt_totalsize(base);
  2116. }
  2117. #else
  2118. printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
  2119. fdt_noffset = -EBADF;
  2120. #endif
  2121. out:
  2122. if (datap)
  2123. *datap = load;
  2124. if (lenp)
  2125. *lenp = len;
  2126. if (fit_unamep)
  2127. *fit_unamep = fit_uname;
  2128. if (fit_uname_configp)
  2129. *fit_uname_configp = fit_uname_config;
  2130. if (fit_uname_config_copy)
  2131. free(fit_uname_config_copy);
  2132. return fdt_noffset;
  2133. }
  2134. #endif