image-fit.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  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. /**
  957. * fit_image_verify - verify data integrity
  958. * @fit: pointer to the FIT format image header
  959. * @image_noffset: component image node offset
  960. *
  961. * fit_image_verify() goes over component image hash nodes,
  962. * re-calculates each data hash and compares with the value stored in hash
  963. * node.
  964. *
  965. * returns:
  966. * 1, if all hashes are valid
  967. * 0, otherwise (or on error)
  968. */
  969. int fit_image_verify(const void *fit, int image_noffset)
  970. {
  971. const void *data;
  972. size_t size;
  973. int noffset = 0;
  974. char *err_msg = "";
  975. int verify_all = 1;
  976. int ret;
  977. /* Get image data and data length */
  978. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  979. err_msg = "Can't get image data/size";
  980. goto error;
  981. }
  982. /* Verify all required signatures */
  983. if (IMAGE_ENABLE_VERIFY &&
  984. fit_image_verify_required_sigs(fit, image_noffset, data, size,
  985. gd_fdt_blob(), &verify_all)) {
  986. err_msg = "Unable to verify required signature";
  987. goto error;
  988. }
  989. /* Process all hash subnodes of the component image node */
  990. fdt_for_each_subnode(noffset, fit, image_noffset) {
  991. const char *name = fit_get_name(fit, noffset, NULL);
  992. /*
  993. * Check subnode name, must be equal to "hash".
  994. * Multiple hash nodes require unique unit node
  995. * names, e.g. hash@1, hash@2, etc.
  996. */
  997. if (!strncmp(name, FIT_HASH_NODENAME,
  998. strlen(FIT_HASH_NODENAME))) {
  999. if (fit_image_check_hash(fit, noffset, data, size,
  1000. &err_msg))
  1001. goto error;
  1002. puts("+ ");
  1003. } else if (IMAGE_ENABLE_VERIFY && verify_all &&
  1004. !strncmp(name, FIT_SIG_NODENAME,
  1005. strlen(FIT_SIG_NODENAME))) {
  1006. ret = fit_image_check_sig(fit, noffset, data,
  1007. size, -1, &err_msg);
  1008. /*
  1009. * Show an indication on failure, but do not return
  1010. * an error. Only keys marked 'required' can cause
  1011. * an image validation failure. See the call to
  1012. * fit_image_verify_required_sigs() above.
  1013. */
  1014. if (ret)
  1015. puts("- ");
  1016. else
  1017. puts("+ ");
  1018. }
  1019. }
  1020. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  1021. err_msg = "Corrupted or truncated tree";
  1022. goto error;
  1023. }
  1024. return 1;
  1025. error:
  1026. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  1027. err_msg, fit_get_name(fit, noffset, NULL),
  1028. fit_get_name(fit, image_noffset, NULL));
  1029. return 0;
  1030. }
  1031. /**
  1032. * fit_all_image_verify - verify data integrity for all images
  1033. * @fit: pointer to the FIT format image header
  1034. *
  1035. * fit_all_image_verify() goes over all images in the FIT and
  1036. * for every images checks if all it's hashes are valid.
  1037. *
  1038. * returns:
  1039. * 1, if all hashes of all images are valid
  1040. * 0, otherwise (or on error)
  1041. */
  1042. int fit_all_image_verify(const void *fit)
  1043. {
  1044. int images_noffset;
  1045. int noffset;
  1046. int ndepth;
  1047. int count;
  1048. /* Find images parent node offset */
  1049. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1050. if (images_noffset < 0) {
  1051. printf("Can't find images parent node '%s' (%s)\n",
  1052. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  1053. return 0;
  1054. }
  1055. /* Process all image subnodes, check hashes for each */
  1056. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  1057. (ulong)fit);
  1058. for (ndepth = 0, count = 0,
  1059. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  1060. (noffset >= 0) && (ndepth > 0);
  1061. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1062. if (ndepth == 1) {
  1063. /*
  1064. * Direct child node of the images parent node,
  1065. * i.e. component image node.
  1066. */
  1067. printf(" Hash(es) for Image %u (%s): ", count,
  1068. fit_get_name(fit, noffset, NULL));
  1069. count++;
  1070. if (!fit_image_verify(fit, noffset))
  1071. return 0;
  1072. printf("\n");
  1073. }
  1074. }
  1075. return 1;
  1076. }
  1077. /**
  1078. * fit_image_check_os - check whether image node is of a given os type
  1079. * @fit: pointer to the FIT format image header
  1080. * @noffset: component image node offset
  1081. * @os: requested image os
  1082. *
  1083. * fit_image_check_os() reads image os property and compares its numeric
  1084. * id with the requested os. Comparison result is returned to the caller.
  1085. *
  1086. * returns:
  1087. * 1 if image is of given os type
  1088. * 0 otherwise (or on error)
  1089. */
  1090. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1091. {
  1092. uint8_t image_os;
  1093. if (fit_image_get_os(fit, noffset, &image_os))
  1094. return 0;
  1095. return (os == image_os);
  1096. }
  1097. /**
  1098. * fit_image_check_arch - check whether image node is of a given arch
  1099. * @fit: pointer to the FIT format image header
  1100. * @noffset: component image node offset
  1101. * @arch: requested imagearch
  1102. *
  1103. * fit_image_check_arch() reads image arch property and compares its numeric
  1104. * id with the requested arch. Comparison result is returned to the caller.
  1105. *
  1106. * returns:
  1107. * 1 if image is of given arch
  1108. * 0 otherwise (or on error)
  1109. */
  1110. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1111. {
  1112. uint8_t image_arch;
  1113. int aarch32_support = 0;
  1114. #ifdef CONFIG_ARM64_SUPPORT_AARCH32
  1115. aarch32_support = 1;
  1116. #endif
  1117. if (fit_image_get_arch(fit, noffset, &image_arch))
  1118. return 0;
  1119. return (arch == image_arch) ||
  1120. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
  1121. (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
  1122. aarch32_support);
  1123. }
  1124. /**
  1125. * fit_image_check_type - check whether image node is of a given type
  1126. * @fit: pointer to the FIT format image header
  1127. * @noffset: component image node offset
  1128. * @type: requested image type
  1129. *
  1130. * fit_image_check_type() reads image type property and compares its numeric
  1131. * id with the requested type. Comparison result is returned to the caller.
  1132. *
  1133. * returns:
  1134. * 1 if image is of given type
  1135. * 0 otherwise (or on error)
  1136. */
  1137. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1138. {
  1139. uint8_t image_type;
  1140. if (fit_image_get_type(fit, noffset, &image_type))
  1141. return 0;
  1142. return (type == image_type);
  1143. }
  1144. /**
  1145. * fit_image_check_comp - check whether image node uses given compression
  1146. * @fit: pointer to the FIT format image header
  1147. * @noffset: component image node offset
  1148. * @comp: requested image compression type
  1149. *
  1150. * fit_image_check_comp() reads image compression property and compares its
  1151. * numeric id with the requested compression type. Comparison result is
  1152. * returned to the caller.
  1153. *
  1154. * returns:
  1155. * 1 if image uses requested compression
  1156. * 0 otherwise (or on error)
  1157. */
  1158. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1159. {
  1160. uint8_t image_comp;
  1161. if (fit_image_get_comp(fit, noffset, &image_comp))
  1162. return 0;
  1163. return (comp == image_comp);
  1164. }
  1165. /**
  1166. * fit_check_format - sanity check FIT image format
  1167. * @fit: pointer to the FIT format image header
  1168. *
  1169. * fit_check_format() runs a basic sanity FIT image verification.
  1170. * Routine checks for mandatory properties, nodes, etc.
  1171. *
  1172. * returns:
  1173. * 1, on success
  1174. * 0, on failure
  1175. */
  1176. int fit_check_format(const void *fit)
  1177. {
  1178. /* mandatory / node 'description' property */
  1179. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1180. debug("Wrong FIT format: no description\n");
  1181. return 0;
  1182. }
  1183. if (IMAGE_ENABLE_TIMESTAMP) {
  1184. /* mandatory / node 'timestamp' property */
  1185. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1186. debug("Wrong FIT format: no timestamp\n");
  1187. return 0;
  1188. }
  1189. }
  1190. /* mandatory subimages parent '/images' node */
  1191. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1192. debug("Wrong FIT format: no images parent node\n");
  1193. return 0;
  1194. }
  1195. return 1;
  1196. }
  1197. /**
  1198. * fit_conf_find_compat
  1199. * @fit: pointer to the FIT format image header
  1200. * @fdt: pointer to the device tree to compare against
  1201. *
  1202. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1203. * most compatible with the passed in device tree.
  1204. *
  1205. * Example:
  1206. *
  1207. * / o image-tree
  1208. * |-o images
  1209. * | |-o fdt@1
  1210. * | |-o fdt@2
  1211. * |
  1212. * |-o configurations
  1213. * |-o config@1
  1214. * | |-fdt = fdt@1
  1215. * |
  1216. * |-o config@2
  1217. * |-fdt = fdt@2
  1218. *
  1219. * / o U-Boot fdt
  1220. * |-compatible = "foo,bar", "bim,bam"
  1221. *
  1222. * / o kernel fdt1
  1223. * |-compatible = "foo,bar",
  1224. *
  1225. * / o kernel fdt2
  1226. * |-compatible = "bim,bam", "baz,biz"
  1227. *
  1228. * Configuration 1 would be picked because the first string in U-Boot's
  1229. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1230. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1231. *
  1232. * returns:
  1233. * offset to the configuration to use if one was found
  1234. * -1 otherwise
  1235. */
  1236. int fit_conf_find_compat(const void *fit, const void *fdt)
  1237. {
  1238. int ndepth = 0;
  1239. int noffset, confs_noffset, images_noffset;
  1240. const void *fdt_compat;
  1241. int fdt_compat_len;
  1242. int best_match_offset = 0;
  1243. int best_match_pos = 0;
  1244. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1245. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1246. if (confs_noffset < 0 || images_noffset < 0) {
  1247. debug("Can't find configurations or images nodes.\n");
  1248. return -1;
  1249. }
  1250. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1251. if (!fdt_compat) {
  1252. debug("Fdt for comparison has no \"compatible\" property.\n");
  1253. return -1;
  1254. }
  1255. /*
  1256. * Loop over the configurations in the FIT image.
  1257. */
  1258. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1259. (noffset >= 0) && (ndepth > 0);
  1260. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1261. const void *kfdt;
  1262. const char *kfdt_name;
  1263. int kfdt_noffset;
  1264. const char *cur_fdt_compat;
  1265. int len;
  1266. size_t size;
  1267. int i;
  1268. if (ndepth > 1)
  1269. continue;
  1270. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1271. if (!kfdt_name) {
  1272. debug("No fdt property found.\n");
  1273. continue;
  1274. }
  1275. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1276. kfdt_name);
  1277. if (kfdt_noffset < 0) {
  1278. debug("No image node named \"%s\" found.\n",
  1279. kfdt_name);
  1280. continue;
  1281. }
  1282. /*
  1283. * Get a pointer to this configuration's fdt.
  1284. */
  1285. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1286. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1287. continue;
  1288. }
  1289. len = fdt_compat_len;
  1290. cur_fdt_compat = fdt_compat;
  1291. /*
  1292. * Look for a match for each U-Boot compatibility string in
  1293. * turn in this configuration's fdt.
  1294. */
  1295. for (i = 0; len > 0 &&
  1296. (!best_match_offset || best_match_pos > i); i++) {
  1297. int cur_len = strlen(cur_fdt_compat) + 1;
  1298. if (!fdt_node_check_compatible(kfdt, 0,
  1299. cur_fdt_compat)) {
  1300. best_match_offset = noffset;
  1301. best_match_pos = i;
  1302. break;
  1303. }
  1304. len -= cur_len;
  1305. cur_fdt_compat += cur_len;
  1306. }
  1307. }
  1308. if (!best_match_offset) {
  1309. debug("No match found.\n");
  1310. return -1;
  1311. }
  1312. return best_match_offset;
  1313. }
  1314. /**
  1315. * fit_conf_get_node - get node offset for configuration of a given unit name
  1316. * @fit: pointer to the FIT format image header
  1317. * @conf_uname: configuration node unit name
  1318. *
  1319. * fit_conf_get_node() finds a configuration (within the '/configurations'
  1320. * parent node) of a provided unit name. If configuration is found its node
  1321. * offset is returned to the caller.
  1322. *
  1323. * When NULL is provided in second argument fit_conf_get_node() will search
  1324. * for a default configuration node instead. Default configuration node unit
  1325. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1326. * node.
  1327. *
  1328. * returns:
  1329. * configuration node offset when found (>=0)
  1330. * negative number on failure (FDT_ERR_* code)
  1331. */
  1332. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1333. {
  1334. int noffset, confs_noffset;
  1335. int len;
  1336. const char *s;
  1337. char *conf_uname_copy = NULL;
  1338. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1339. if (confs_noffset < 0) {
  1340. debug("Can't find configurations parent node '%s' (%s)\n",
  1341. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1342. return confs_noffset;
  1343. }
  1344. if (conf_uname == NULL) {
  1345. /* get configuration unit name from the default property */
  1346. debug("No configuration specified, trying default...\n");
  1347. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1348. FIT_DEFAULT_PROP, &len);
  1349. if (conf_uname == NULL) {
  1350. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1351. len);
  1352. return len;
  1353. }
  1354. debug("Found default configuration: '%s'\n", conf_uname);
  1355. }
  1356. s = strchr(conf_uname, '#');
  1357. if (s) {
  1358. len = s - conf_uname;
  1359. conf_uname_copy = malloc(len + 1);
  1360. if (!conf_uname_copy) {
  1361. debug("Can't allocate uname copy: '%s'\n",
  1362. conf_uname);
  1363. return -ENOMEM;
  1364. }
  1365. memcpy(conf_uname_copy, conf_uname, len);
  1366. conf_uname_copy[len] = '\0';
  1367. conf_uname = conf_uname_copy;
  1368. }
  1369. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1370. if (noffset < 0) {
  1371. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1372. conf_uname, fdt_strerror(noffset));
  1373. }
  1374. if (conf_uname_copy)
  1375. free(conf_uname_copy);
  1376. return noffset;
  1377. }
  1378. int fit_conf_get_prop_node_count(const void *fit, int noffset,
  1379. const char *prop_name)
  1380. {
  1381. return fdt_stringlist_count(fit, noffset, prop_name);
  1382. }
  1383. int fit_conf_get_prop_node_index(const void *fit, int noffset,
  1384. const char *prop_name, int index)
  1385. {
  1386. const char *uname;
  1387. int len;
  1388. /* get kernel image unit name from configuration kernel property */
  1389. uname = fdt_stringlist_get(fit, noffset, prop_name, index, &len);
  1390. if (uname == NULL)
  1391. return len;
  1392. return fit_image_get_node(fit, uname);
  1393. }
  1394. int fit_conf_get_prop_node(const void *fit, int noffset,
  1395. const char *prop_name)
  1396. {
  1397. return fit_conf_get_prop_node_index(fit, noffset, prop_name, 0);
  1398. }
  1399. /**
  1400. * fit_conf_print - prints out the FIT configuration details
  1401. * @fit: pointer to the FIT format image header
  1402. * @noffset: offset of the configuration node
  1403. * @p: pointer to prefix string
  1404. *
  1405. * fit_conf_print() lists all mandatory properties for the processed
  1406. * configuration node.
  1407. *
  1408. * returns:
  1409. * no returned results
  1410. */
  1411. void fit_conf_print(const void *fit, int noffset, const char *p)
  1412. {
  1413. char *desc;
  1414. const char *uname;
  1415. int ret;
  1416. int fdt_index, loadables_index;
  1417. /* Mandatory properties */
  1418. ret = fit_get_desc(fit, noffset, &desc);
  1419. printf("%s Description: ", p);
  1420. if (ret)
  1421. printf("unavailable\n");
  1422. else
  1423. printf("%s\n", desc);
  1424. uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1425. printf("%s Kernel: ", p);
  1426. if (uname == NULL)
  1427. printf("unavailable\n");
  1428. else
  1429. printf("%s\n", uname);
  1430. /* Optional properties */
  1431. uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1432. if (uname)
  1433. printf("%s Init Ramdisk: %s\n", p, uname);
  1434. for (fdt_index = 0;
  1435. uname = fdt_stringlist_get(fit, noffset, FIT_FDT_PROP,
  1436. fdt_index, NULL), uname;
  1437. fdt_index++) {
  1438. if (fdt_index == 0)
  1439. printf("%s FDT: ", p);
  1440. else
  1441. printf("%s ", p);
  1442. printf("%s\n", uname);
  1443. }
  1444. uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
  1445. if (uname)
  1446. printf("%s FPGA: %s\n", p, uname);
  1447. /* Print out all of the specified loadables */
  1448. for (loadables_index = 0;
  1449. uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
  1450. loadables_index, NULL), uname;
  1451. loadables_index++) {
  1452. if (loadables_index == 0) {
  1453. printf("%s Loadables: ", p);
  1454. } else {
  1455. printf("%s ", p);
  1456. }
  1457. printf("%s\n", uname);
  1458. }
  1459. }
  1460. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1461. {
  1462. fit_image_print(fit, rd_noffset, " ");
  1463. if (verify) {
  1464. puts(" Verifying Hash Integrity ... ");
  1465. if (!fit_image_verify(fit, rd_noffset)) {
  1466. puts("Bad Data Hash\n");
  1467. return -EACCES;
  1468. }
  1469. puts("OK\n");
  1470. }
  1471. return 0;
  1472. }
  1473. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1474. ulong addr)
  1475. {
  1476. int cfg_noffset;
  1477. void *fit_hdr;
  1478. int noffset;
  1479. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1480. prop_name, images->fit_uname_cfg, addr);
  1481. /* Check whether configuration has this property defined */
  1482. fit_hdr = map_sysmem(addr, 0);
  1483. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1484. if (cfg_noffset < 0) {
  1485. debug("* %s: no such config\n", prop_name);
  1486. return -EINVAL;
  1487. }
  1488. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1489. if (noffset < 0) {
  1490. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1491. return -ENOENT;
  1492. }
  1493. return noffset;
  1494. }
  1495. /**
  1496. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1497. *
  1498. * @return the properly name where we expect to find the image in the
  1499. * config node
  1500. */
  1501. static const char *fit_get_image_type_property(int type)
  1502. {
  1503. /*
  1504. * This is sort-of available in the uimage_type[] table in image.c
  1505. * but we don't have access to the short name, and "fdt" is different
  1506. * anyway. So let's just keep it here.
  1507. */
  1508. switch (type) {
  1509. case IH_TYPE_FLATDT:
  1510. return FIT_FDT_PROP;
  1511. case IH_TYPE_KERNEL:
  1512. return FIT_KERNEL_PROP;
  1513. case IH_TYPE_RAMDISK:
  1514. return FIT_RAMDISK_PROP;
  1515. case IH_TYPE_X86_SETUP:
  1516. return FIT_SETUP_PROP;
  1517. case IH_TYPE_LOADABLE:
  1518. return FIT_LOADABLE_PROP;
  1519. case IH_TYPE_FPGA:
  1520. return FIT_FPGA_PROP;
  1521. }
  1522. return "unknown";
  1523. }
  1524. int fit_image_load(bootm_headers_t *images, ulong addr,
  1525. const char **fit_unamep, const char **fit_uname_configp,
  1526. int arch, int image_type, int bootstage_id,
  1527. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1528. {
  1529. int cfg_noffset, noffset;
  1530. const char *fit_uname;
  1531. const char *fit_uname_config;
  1532. const char *fit_base_uname_config;
  1533. const void *fit;
  1534. const void *buf;
  1535. size_t size;
  1536. int type_ok, os_ok;
  1537. ulong load, data, len;
  1538. uint8_t os;
  1539. #ifndef USE_HOSTCC
  1540. uint8_t os_arch;
  1541. #endif
  1542. const char *prop_name;
  1543. int ret;
  1544. fit = map_sysmem(addr, 0);
  1545. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1546. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1547. fit_base_uname_config = NULL;
  1548. prop_name = fit_get_image_type_property(image_type);
  1549. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1550. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1551. if (!fit_check_format(fit)) {
  1552. printf("Bad FIT %s image format!\n", prop_name);
  1553. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1554. return -ENOEXEC;
  1555. }
  1556. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1557. if (fit_uname) {
  1558. /* get FIT component image node offset */
  1559. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1560. noffset = fit_image_get_node(fit, fit_uname);
  1561. } else {
  1562. /*
  1563. * no image node unit name, try to get config
  1564. * node first. If config unit node name is NULL
  1565. * fit_conf_get_node() will try to find default config node
  1566. */
  1567. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1568. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1569. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1570. } else {
  1571. cfg_noffset = fit_conf_get_node(fit,
  1572. fit_uname_config);
  1573. }
  1574. if (cfg_noffset < 0) {
  1575. puts("Could not find configuration node\n");
  1576. bootstage_error(bootstage_id +
  1577. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1578. return -ENOENT;
  1579. }
  1580. fit_base_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1581. printf(" Using '%s' configuration\n", fit_base_uname_config);
  1582. if (image_type == IH_TYPE_KERNEL) {
  1583. /* Remember (and possibly verify) this config */
  1584. images->fit_uname_cfg = fit_base_uname_config;
  1585. if (IMAGE_ENABLE_VERIFY && images->verify) {
  1586. puts(" Verifying Hash Integrity ... ");
  1587. if (fit_config_verify(fit, cfg_noffset)) {
  1588. puts("Bad Data Hash\n");
  1589. bootstage_error(bootstage_id +
  1590. BOOTSTAGE_SUB_HASH);
  1591. return -EACCES;
  1592. }
  1593. puts("OK\n");
  1594. }
  1595. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1596. }
  1597. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1598. prop_name);
  1599. fit_uname = fit_get_name(fit, noffset, NULL);
  1600. }
  1601. if (noffset < 0) {
  1602. puts("Could not find subimage node\n");
  1603. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1604. return -ENOENT;
  1605. }
  1606. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1607. ret = fit_image_select(fit, noffset, images->verify);
  1608. if (ret) {
  1609. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1610. return ret;
  1611. }
  1612. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1613. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1614. if (!fit_image_check_target_arch(fit, noffset)) {
  1615. puts("Unsupported Architecture\n");
  1616. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1617. return -ENOEXEC;
  1618. }
  1619. #endif
  1620. #ifndef USE_HOSTCC
  1621. fit_image_get_arch(fit, noffset, &os_arch);
  1622. images->os.arch = os_arch;
  1623. #endif
  1624. if (image_type == IH_TYPE_FLATDT &&
  1625. !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
  1626. puts("FDT image is compressed");
  1627. return -EPROTONOSUPPORT;
  1628. }
  1629. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1630. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1631. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1632. (image_type == IH_TYPE_KERNEL &&
  1633. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1634. os_ok = image_type == IH_TYPE_FLATDT ||
  1635. image_type == IH_TYPE_FPGA ||
  1636. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1637. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1638. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
  1639. /*
  1640. * If either of the checks fail, we should report an error, but
  1641. * if the image type is coming from the "loadables" field, we
  1642. * don't care what it is
  1643. */
  1644. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1645. fit_image_get_os(fit, noffset, &os);
  1646. printf("No %s %s %s Image\n",
  1647. genimg_get_os_name(os),
  1648. genimg_get_arch_name(arch),
  1649. genimg_get_type_name(image_type));
  1650. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1651. return -EIO;
  1652. }
  1653. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1654. /* get image data address and length */
  1655. if (fit_image_get_data(fit, noffset, &buf, &size)) {
  1656. printf("Could not find %s subimage data!\n", prop_name);
  1657. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1658. return -ENOENT;
  1659. }
  1660. #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1661. /* perform any post-processing on the image data */
  1662. board_fit_image_post_process((void **)&buf, &size);
  1663. #endif
  1664. len = (ulong)size;
  1665. /* verify that image data is a proper FDT blob */
  1666. if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
  1667. puts("Subimage data is not a FDT");
  1668. return -ENOEXEC;
  1669. }
  1670. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1671. /*
  1672. * Work-around for eldk-4.2 which gives this warning if we try to
  1673. * cast in the unmap_sysmem() call:
  1674. * warning: initialization discards qualifiers from pointer target type
  1675. */
  1676. {
  1677. void *vbuf = (void *)buf;
  1678. data = map_to_sysmem(vbuf);
  1679. }
  1680. if (load_op == FIT_LOAD_IGNORED) {
  1681. /* Don't load */
  1682. } else if (fit_image_get_load(fit, noffset, &load)) {
  1683. if (load_op == FIT_LOAD_REQUIRED) {
  1684. printf("Can't get %s subimage load address!\n",
  1685. prop_name);
  1686. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1687. return -EBADF;
  1688. }
  1689. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1690. ulong image_start, image_end;
  1691. ulong load_end;
  1692. void *dst;
  1693. /*
  1694. * move image data to the load address,
  1695. * make sure we don't overwrite initial image
  1696. */
  1697. image_start = addr;
  1698. image_end = addr + fit_get_size(fit);
  1699. load_end = load + len;
  1700. if (image_type != IH_TYPE_KERNEL &&
  1701. load < image_end && load_end > image_start) {
  1702. printf("Error: %s overwritten\n", prop_name);
  1703. return -EXDEV;
  1704. }
  1705. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1706. prop_name, data, load);
  1707. dst = map_sysmem(load, len);
  1708. memmove(dst, buf, len);
  1709. data = load;
  1710. }
  1711. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1712. *datap = data;
  1713. *lenp = len;
  1714. if (fit_unamep)
  1715. *fit_unamep = (char *)fit_uname;
  1716. if (fit_uname_configp)
  1717. *fit_uname_configp = (char *)(fit_uname_config ? :
  1718. fit_base_uname_config);
  1719. return noffset;
  1720. }
  1721. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1722. ulong *setup_start, ulong *setup_len)
  1723. {
  1724. int noffset;
  1725. ulong addr;
  1726. ulong len;
  1727. int ret;
  1728. addr = map_to_sysmem(images->fit_hdr_os);
  1729. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1730. if (noffset < 0)
  1731. return noffset;
  1732. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1733. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1734. FIT_LOAD_REQUIRED, setup_start, &len);
  1735. return ret;
  1736. }
  1737. #ifndef USE_HOSTCC
  1738. int boot_get_fdt_fit(bootm_headers_t *images, ulong addr,
  1739. const char **fit_unamep, const char **fit_uname_configp,
  1740. int arch, ulong *datap, ulong *lenp)
  1741. {
  1742. int fdt_noffset, cfg_noffset, count;
  1743. const void *fit;
  1744. const char *fit_uname = NULL;
  1745. const char *fit_uname_config = NULL;
  1746. char *fit_uname_config_copy = NULL;
  1747. char *next_config = NULL;
  1748. ulong load, len;
  1749. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1750. ulong image_start, image_end;
  1751. ulong ovload, ovlen;
  1752. const char *uconfig;
  1753. const char *uname;
  1754. void *base, *ov;
  1755. int i, err, noffset, ov_noffset;
  1756. #endif
  1757. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1758. if (fit_uname_configp && *fit_uname_configp) {
  1759. fit_uname_config_copy = strdup(*fit_uname_configp);
  1760. if (!fit_uname_config_copy)
  1761. return -ENOMEM;
  1762. next_config = strchr(fit_uname_config_copy, '#');
  1763. if (next_config)
  1764. *next_config++ = '\0';
  1765. if (next_config - 1 > fit_uname_config_copy)
  1766. fit_uname_config = fit_uname_config_copy;
  1767. }
  1768. fdt_noffset = fit_image_load(images,
  1769. addr, &fit_uname, &fit_uname_config,
  1770. arch, IH_TYPE_FLATDT,
  1771. BOOTSTAGE_ID_FIT_FDT_START,
  1772. FIT_LOAD_OPTIONAL, &load, &len);
  1773. if (fdt_noffset < 0)
  1774. goto out;
  1775. debug("fit_uname=%s, fit_uname_config=%s\n",
  1776. fit_uname ? fit_uname : "<NULL>",
  1777. fit_uname_config ? fit_uname_config : "<NULL>");
  1778. fit = map_sysmem(addr, 0);
  1779. cfg_noffset = fit_conf_get_node(fit, fit_uname_config);
  1780. /* single blob, or error just return as well */
  1781. count = fit_conf_get_prop_node_count(fit, cfg_noffset, FIT_FDT_PROP);
  1782. if (count <= 1 && !next_config)
  1783. goto out;
  1784. /* we need to apply overlays */
  1785. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1786. image_start = addr;
  1787. image_end = addr + fit_get_size(fit);
  1788. /* verify that relocation took place by load address not being in fit */
  1789. if (load >= image_start && load < image_end) {
  1790. /* check is simplified; fit load checks for overlaps */
  1791. printf("Overlayed FDT requires relocation\n");
  1792. fdt_noffset = -EBADF;
  1793. goto out;
  1794. }
  1795. base = map_sysmem(load, len);
  1796. /* apply extra configs in FIT first, followed by args */
  1797. for (i = 1; ; i++) {
  1798. if (i < count) {
  1799. noffset = fit_conf_get_prop_node_index(fit, cfg_noffset,
  1800. FIT_FDT_PROP, i);
  1801. uname = fit_get_name(fit, noffset, NULL);
  1802. uconfig = NULL;
  1803. } else {
  1804. if (!next_config)
  1805. break;
  1806. uconfig = next_config;
  1807. next_config = strchr(next_config, '#');
  1808. if (next_config)
  1809. *next_config++ = '\0';
  1810. uname = NULL;
  1811. }
  1812. debug("%d: using uname=%s uconfig=%s\n", i, uname, uconfig);
  1813. ov_noffset = fit_image_load(images,
  1814. addr, &uname, &uconfig,
  1815. arch, IH_TYPE_FLATDT,
  1816. BOOTSTAGE_ID_FIT_FDT_START,
  1817. FIT_LOAD_REQUIRED, &ovload, &ovlen);
  1818. if (ov_noffset < 0) {
  1819. printf("load of %s failed\n", uname);
  1820. continue;
  1821. }
  1822. debug("%s loaded at 0x%08lx len=0x%08lx\n",
  1823. uname, ovload, ovlen);
  1824. ov = map_sysmem(ovload, ovlen);
  1825. base = map_sysmem(load, len + ovlen);
  1826. err = fdt_open_into(base, base, len + ovlen);
  1827. if (err < 0) {
  1828. printf("failed on fdt_open_into\n");
  1829. fdt_noffset = err;
  1830. goto out;
  1831. }
  1832. /* the verbose method prints out messages on error */
  1833. err = fdt_overlay_apply_verbose(base, ov);
  1834. if (err < 0) {
  1835. fdt_noffset = err;
  1836. goto out;
  1837. }
  1838. fdt_pack(base);
  1839. len = fdt_totalsize(base);
  1840. }
  1841. #else
  1842. printf("config with overlays but CONFIG_OF_LIBFDT_OVERLAY not set\n");
  1843. fdt_noffset = -EBADF;
  1844. #endif
  1845. out:
  1846. if (datap)
  1847. *datap = load;
  1848. if (lenp)
  1849. *lenp = len;
  1850. if (fit_unamep)
  1851. *fit_unamep = fit_uname;
  1852. if (fit_uname_configp)
  1853. *fit_uname_configp = fit_uname_config;
  1854. if (fit_uname_config_copy)
  1855. free(fit_uname_config_copy);
  1856. return fdt_noffset;
  1857. }
  1858. #endif