image-fit.c 55 KB

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