image-fit.c 54 KB

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