image-fit.c 64 KB

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