image-fit.c 56 KB

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