image-fit.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  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_RAMDISK:
  1747. return FIT_RAMDISK_PROP;
  1748. case IH_TYPE_X86_SETUP:
  1749. return FIT_SETUP_PROP;
  1750. case IH_TYPE_LOADABLE:
  1751. return FIT_LOADABLE_PROP;
  1752. case IH_TYPE_FPGA:
  1753. return FIT_FPGA_PROP;
  1754. case IH_TYPE_STANDALONE:
  1755. return FIT_STANDALONE_PROP;
  1756. }
  1757. return "unknown";
  1758. }
  1759. int fit_image_load(bootm_headers_t *images, ulong addr,
  1760. const char **fit_unamep, const char **fit_uname_configp,
  1761. int arch, int image_type, int bootstage_id,
  1762. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1763. {
  1764. int cfg_noffset, noffset;
  1765. const char *fit_uname;
  1766. const char *fit_uname_config;
  1767. const char *fit_base_uname_config;
  1768. const void *fit;
  1769. void *buf;
  1770. void *loadbuf;
  1771. size_t size;
  1772. int type_ok, os_ok;
  1773. ulong load, load_end, data, len;
  1774. uint8_t os, comp;
  1775. #ifndef USE_HOSTCC
  1776. uint8_t os_arch;
  1777. #endif
  1778. const char *prop_name;
  1779. int ret;
  1780. fit = map_sysmem(addr, 0);
  1781. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1782. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1783. fit_base_uname_config = NULL;
  1784. prop_name = fit_get_image_type_property(image_type);
  1785. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1786. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1787. ret = fit_check_format(fit, IMAGE_SIZE_INVAL);
  1788. if (ret) {
  1789. printf("Bad FIT %s image format! (err=%d)\n", prop_name, ret);
  1790. if (CONFIG_IS_ENABLED(FIT_SIGNATURE) && ret == -EADDRNOTAVAIL)
  1791. printf("Signature checking prevents use of unit addresses (@) in nodes\n");
  1792. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1793. return ret;
  1794. }
  1795. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1796. if (fit_uname) {
  1797. /* get FIT component image node offset */
  1798. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1799. noffset = fit_image_get_node(fit, fit_uname);
  1800. } else {
  1801. /*
  1802. * no image node unit name, try to get config
  1803. * node first. If config unit node name is NULL
  1804. * fit_conf_get_node() will try to find default config node
  1805. */
  1806. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1807. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1808. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1809. } else {
  1810. cfg_noffset = fit_conf_get_node(fit,
  1811. fit_uname_config);
  1812. }
  1813. if (cfg_noffset < 0) {
  1814. puts("Could not find configuration node\n");
  1815. bootstage_error(bootstage_id +
  1816. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1817. return -ENOENT;
  1818. }
  1819. fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1820. printf(" Using '%s' configuration\n", fit_base_uname_config);
  1821. /* Remember this config */
  1822. if (image_type == IH_TYPE_KERNEL)
  1823. images->fit_uname_cfg = fit_base_uname_config;
  1824. if (FIT_IMAGE_ENABLE_VERIFY && images->verify) {
  1825. puts(" Verifying Hash Integrity ... ");
  1826. if (fit_config_verify(fit, cfg_noffset)) {
  1827. puts("Bad Data Hash\n");
  1828. bootstage_error(bootstage_id +
  1829. BOOTSTAGE_SUB_HASH);
  1830. return -EACCES;
  1831. }
  1832. puts("OK\n");
  1833. }
  1834. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1835. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1836. prop_name);
  1837. fit_uname = fit_get_name(fit, noffset, NULL);
  1838. }
  1839. if (noffset < 0) {
  1840. printf("Could not find subimage node type '%s'\n", prop_name);
  1841. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1842. return -ENOENT;
  1843. }
  1844. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1845. ret = fit_image_select(fit, noffset, images->verify);
  1846. if (ret) {
  1847. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1848. return ret;
  1849. }
  1850. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1851. if (!host_build() && IS_ENABLED(CONFIG_SANDBOX)) {
  1852. if (!fit_image_check_target_arch(fit, noffset)) {
  1853. puts("Unsupported Architecture\n");
  1854. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1855. return -ENOEXEC;
  1856. }
  1857. }
  1858. #ifndef USE_HOSTCC
  1859. fit_image_get_arch(fit, noffset, &os_arch);
  1860. images->os.arch = os_arch;
  1861. #endif
  1862. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1863. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1864. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1865. (image_type == IH_TYPE_KERNEL &&
  1866. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1867. os_ok = image_type == IH_TYPE_FLATDT ||
  1868. image_type == IH_TYPE_FPGA ||
  1869. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1870. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1871. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS) ||
  1872. fit_image_check_os(fit, noffset, IH_OS_EFI) ||
  1873. fit_image_check_os(fit, noffset, IH_OS_VXWORKS);
  1874. /*
  1875. * If either of the checks fail, we should report an error, but
  1876. * if the image type is coming from the "loadables" field, we
  1877. * don't care what it is
  1878. */
  1879. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1880. fit_image_get_os(fit, noffset, &os);
  1881. printf("No %s %s %s Image\n",
  1882. genimg_get_os_name(os),
  1883. genimg_get_arch_name(arch),
  1884. genimg_get_type_name(image_type));
  1885. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1886. return -EIO;
  1887. }
  1888. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1889. /* get image data address and length */
  1890. if (fit_image_get_data_and_size(fit, noffset,
  1891. (const void **)&buf, &size)) {
  1892. printf("Could not find %s subimage data!\n", prop_name);
  1893. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1894. return -ENOENT;
  1895. }
  1896. /* Decrypt data before uncompress/move */
  1897. if (IS_ENABLED(CONFIG_FIT_CIPHER) && IMAGE_ENABLE_DECRYPT) {
  1898. puts(" Decrypting Data ... ");
  1899. if (fit_image_uncipher(fit, noffset, &buf, &size)) {
  1900. puts("Error\n");
  1901. return -EACCES;
  1902. }
  1903. puts("OK\n");
  1904. }
  1905. /* perform any post-processing on the image data */
  1906. if (!host_build() && IS_ENABLED(CONFIG_FIT_IMAGE_POST_PROCESS))
  1907. board_fit_image_post_process(&buf, &size);
  1908. len = (ulong)size;
  1909. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1910. data = map_to_sysmem(buf);
  1911. load = data;
  1912. if (load_op == FIT_LOAD_IGNORED) {
  1913. /* Don't load */
  1914. } else if (fit_image_get_load(fit, noffset, &load)) {
  1915. if (load_op == FIT_LOAD_REQUIRED) {
  1916. printf("Can't get %s subimage load address!\n",
  1917. prop_name);
  1918. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1919. return -EBADF;
  1920. }
  1921. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1922. ulong image_start, image_end;
  1923. /*
  1924. * move image data to the load address,
  1925. * make sure we don't overwrite initial image
  1926. */
  1927. image_start = addr;
  1928. image_end = addr + fit_get_size(fit);
  1929. load_end = load + len;
  1930. if (image_type != IH_TYPE_KERNEL &&
  1931. load < image_end && load_end > image_start) {
  1932. printf("Error: %s overwritten\n", prop_name);
  1933. return -EXDEV;
  1934. }
  1935. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1936. prop_name, data, load);
  1937. } else {
  1938. load = data; /* No load address specified */
  1939. }
  1940. comp = IH_COMP_NONE;
  1941. loadbuf = buf;
  1942. /* Kernel images get decompressed later in bootm_load_os(). */
  1943. if (!fit_image_get_comp(fit, noffset, &comp) &&
  1944. comp != IH_COMP_NONE &&
  1945. !(image_type == IH_TYPE_KERNEL ||
  1946. image_type == IH_TYPE_KERNEL_NOLOAD ||
  1947. image_type == IH_TYPE_RAMDISK)) {
  1948. ulong max_decomp_len = len * 20;
  1949. if (load == data) {
  1950. loadbuf = malloc(max_decomp_len);
  1951. load = map_to_sysmem(loadbuf);
  1952. } else {
  1953. loadbuf = map_sysmem(load, max_decomp_len);
  1954. }
  1955. if (image_decomp(comp, load, data, image_type,
  1956. loadbuf, buf, len, max_decomp_len, &load_end)) {
  1957. printf("Error decompressing %s\n", prop_name);
  1958. return -ENOEXEC;
  1959. }
  1960. len = load_end - load;
  1961. } else if (load != data) {
  1962. loadbuf = map_sysmem(load, len);
  1963. memcpy(loadbuf, buf, len);
  1964. }
  1965. if (image_type == IH_TYPE_RAMDISK && comp != IH_COMP_NONE)
  1966. puts("WARNING: 'compression' nodes for ramdisks are deprecated,"
  1967. " please fix your .its file!\n");
  1968. /* verify that image data is a proper FDT blob */
  1969. if (image_type == IH_TYPE_FLATDT && fdt_check_header(loadbuf)) {
  1970. puts("Subimage data is not a FDT");
  1971. return -ENOEXEC;
  1972. }
  1973. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1974. *datap = load;
  1975. *lenp = len;
  1976. if (fit_unamep)
  1977. *fit_unamep = (char *)fit_uname;
  1978. if (fit_uname_configp)
  1979. *fit_uname_configp = (char *)(fit_uname_config ? :
  1980. fit_base_uname_config);
  1981. return noffset;
  1982. }
  1983. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1984. ulong *setup_start, ulong *setup_len)
  1985. {
  1986. int noffset;
  1987. ulong addr;
  1988. ulong len;
  1989. int ret;
  1990. addr = map_to_sysmem(images->fit_hdr_os);
  1991. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1992. if (noffset < 0)
  1993. return noffset;
  1994. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1995. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1996. FIT_LOAD_REQUIRED, setup_start, &len);
  1997. return ret;
  1998. }
  1999. #ifndef USE_HOSTCC
  2000. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  2001. const char **fit_unamep, const char **fit_uname_configp,
  2002. int arch, ulong *datap, ulong *lenp)
  2003. {
  2004. int fdt_noffset, cfg_noffset, count;
  2005. const void *fit;
  2006. const char *fit_uname = NULL;
  2007. const char *fit_uname_config = NULL;
  2008. char *fit_uname_config_copy = NULL;
  2009. char *next_config = NULL;
  2010. ulong load, len;
  2011. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  2012. ulong image_start, image_end;
  2013. ulong ovload, ovlen;
  2014. const char *uconfig;
  2015. const char *uname;
  2016. void *base, *ov;
  2017. int i, err, noffset, ov_noffset;
  2018. #endif
  2019. fit_uname = fit_unamep ? *fit_unamep : NULL;
  2020. if (fit_uname_configp && *fit_uname_configp) {
  2021. fit_uname_config_copy = strdup(*fit_uname_configp);
  2022. if (!fit_uname_config_copy)
  2023. return -ENOMEM;
  2024. next_config = strchr(fit_uname_config_copy, '#');
  2025. if (next_config)
  2026. *next_config++ = '\0';
  2027. if (next_config - 1 > fit_uname_config_copy)
  2028. fit_uname_config = fit_uname_config_copy;
  2029. }
  2030. fdt_noffset = fit_image_load(images,
  2031. addr, &fit_uname, &fit_uname_config,
  2032. arch, IH_TYPE_FLATDT,
  2033. BOOTSTAGE_ID_FIT_FDT_START,
  2034. FIT_LOAD_OPTIONAL, &load, &len);
  2035. if (fdt_noffset < 0)
  2036. goto out;
  2037. debug("fit_uname=%s, fit_uname_config=%s\n",
  2038. fit_uname ? fit_uname : "<NULL>",
  2039. fit_uname_config ? fit_uname_config : "<NULL>");
  2040. fit = map_sysmem(addr, 0);
  2041. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  2042. /* single blob, or error just return as well */
  2043. count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
  2044. if (count <= 1 && !next_config)
  2045. goto out;
  2046. /* we need to apply overlays */
  2047. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  2048. image_start = addr;
  2049. image_end = addr + fit_get_size(fit);
  2050. /* verify that relocation took place by load address not being in fit */
  2051. if (load >= image_start && load < image_end) {
  2052. /* check is simplified; fit load checks for overlaps */
  2053. printf("Overlayed FDT requires relocation\n");
  2054. fdt_noffset = -EBADF;
  2055. goto out;
  2056. }
  2057. base = map_sysmem(load, len);
  2058. /* apply extra configs in FIT first, followed by args */
  2059. for (i = 1; ; i++) {
  2060. if (i < count) {
  2061. noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
  2062. FIT_FDT_PROP, i);
  2063. uname = fit_get_name(fit, noffset, NULL);
  2064. uconfig = NULL;
  2065. } else {
  2066. if (!next_config)
  2067. break;
  2068. uconfig = next_config;
  2069. next_config = strchr(next_config, '#');
  2070. if (next_config)
  2071. *next_config++ = '\0';
  2072. uname = NULL;
  2073. /*
  2074. * fit_image_load() would load the first FDT from the
  2075. * extra config only when uconfig is specified.
  2076. * Check if the extra config contains multiple FDTs and
  2077. * if so, load them.
  2078. */
  2079. cfg_noffset = fit_conf_get_node(fit, uconfig);
  2080. i = 0;
  2081. count = fit_conf_get_prop_node_count(fit, cfg_noffset,
  2082. FIT_FDT_PROP);
  2083. }
  2084. debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
  2085. ov_noffset = fit_image_load(images,
  2086. addr, &uname, &uconfig,
  2087. arch, IH_TYPE_FLATDT,
  2088. BOOTSTAGE_ID_FIT_FDT_START,
  2089. FIT_LOAD_REQUIRED, &ovload, &ovlen);
  2090. if (ov_noffset < 0) {
  2091. printf("load of %s failed\n", uname);
  2092. continue;
  2093. }
  2094. debug("%s loaded at 0x%08lx len=0x%08lx\n",
  2095. uname, ovload, ovlen);
  2096. ov = map_sysmem(ovload, ovlen);
  2097. base = map_sysmem(load, len + ovlen);
  2098. err = fdt_open_into(base, base, len + ovlen);
  2099. if (err < 0) {
  2100. printf("failed on fdt_open_into\n");
  2101. fdt_noffset = err;
  2102. goto out;
  2103. }
  2104. /* the verbose method prints out messages on error */
  2105. err = fdt_overlay_apply_verbose(base, ov);
  2106. if (err < 0) {
  2107. fdt_noffset = err;
  2108. goto out;
  2109. }
  2110. fdt_pack(base);
  2111. len = fdt_totalsize(base);
  2112. }
  2113. #else
  2114. printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
  2115. fdt_noffset = -EBADF;
  2116. #endif
  2117. out:
  2118. if (datap)
  2119. *datap = load;
  2120. if (lenp)
  2121. *lenp = len;
  2122. if (fit_unamep)
  2123. *fit_unamep = fit_uname;
  2124. if (fit_uname_configp)
  2125. *fit_uname_configp = fit_uname_config;
  2126. if (fit_uname_config_copy)
  2127. free(fit_uname_config_copy);
  2128. return fdt_noffset;
  2129. }
  2130. #endif