image-fit.c 49 KB

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