image-fit.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. * Copyright (c) 2013, Google Inc.
  3. *
  4. * (C) Copyright 2008 Semihalf
  5. *
  6. * (C) Copyright 2000-2006
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #ifdef USE_HOSTCC
  12. #include "mkimage.h"
  13. #include <time.h>
  14. #else
  15. #include <linux/compiler.h>
  16. #include <linux/kconfig.h>
  17. #include <common.h>
  18. #include <errno.h>
  19. #include <mapmem.h>
  20. #include <asm/io.h>
  21. 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. fit_image_get_os(fit, image_noffset, &os);
  379. printf("%s OS: %s\n", p, genimg_get_os_name(os));
  380. }
  381. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  382. (type == IH_TYPE_FIRMWARE) || (type == IH_TYPE_RAMDISK) ||
  383. (type == IH_TYPE_FPGA)) {
  384. ret = fit_image_get_load(fit, image_noffset, &load);
  385. printf("%s Load Address: ", p);
  386. if (ret)
  387. printf("unavailable\n");
  388. else
  389. printf("0x%08lx\n", load);
  390. }
  391. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  392. (type == IH_TYPE_RAMDISK)) {
  393. ret = fit_image_get_entry(fit, image_noffset, &entry);
  394. printf("%s Entry Point: ", p);
  395. if (ret)
  396. printf("unavailable\n");
  397. else
  398. printf("0x%08lx\n", entry);
  399. }
  400. /* Process all hash subnodes of the component image node */
  401. for (ndepth = 0, noffset = fdt_next_node(fit, image_noffset, &ndepth);
  402. (noffset >= 0) && (ndepth > 0);
  403. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  404. if (ndepth == 1) {
  405. /* Direct child node of the component image node */
  406. fit_image_print_verification_data(fit, noffset, p);
  407. }
  408. }
  409. }
  410. #endif /* !defined(CONFIG_SPL_BUILD) || defined(CONFIG_FIT_SPL_PRINT) */
  411. /**
  412. * fit_get_desc - get node description property
  413. * @fit: pointer to the FIT format image header
  414. * @noffset: node offset
  415. * @desc: double pointer to the char, will hold pointer to the description
  416. *
  417. * fit_get_desc() reads description property from a given node, if
  418. * description is found pointer to it is returned in third call argument.
  419. *
  420. * returns:
  421. * 0, on success
  422. * -1, on failure
  423. */
  424. int fit_get_desc(const void *fit, int noffset, char **desc)
  425. {
  426. int len;
  427. *desc = (char *)fdt_getprop(fit, noffset, FIT_DESC_PROP, &len);
  428. if (*desc == NULL) {
  429. fit_get_debug(fit, noffset, FIT_DESC_PROP, len);
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. /**
  435. * fit_get_timestamp - get node timestamp property
  436. * @fit: pointer to the FIT format image header
  437. * @noffset: node offset
  438. * @timestamp: pointer to the time_t, will hold read timestamp
  439. *
  440. * fit_get_timestamp() reads timestamp property from given node, if timestamp
  441. * is found and has a correct size its value is returned in third call
  442. * argument.
  443. *
  444. * returns:
  445. * 0, on success
  446. * -1, on property read failure
  447. * -2, on wrong timestamp size
  448. */
  449. int fit_get_timestamp(const void *fit, int noffset, time_t *timestamp)
  450. {
  451. int len;
  452. const void *data;
  453. data = fdt_getprop(fit, noffset, FIT_TIMESTAMP_PROP, &len);
  454. if (data == NULL) {
  455. fit_get_debug(fit, noffset, FIT_TIMESTAMP_PROP, len);
  456. return -1;
  457. }
  458. if (len != sizeof(uint32_t)) {
  459. debug("FIT timestamp with incorrect size of (%u)\n", len);
  460. return -2;
  461. }
  462. *timestamp = uimage_to_cpu(*((uint32_t *)data));
  463. return 0;
  464. }
  465. /**
  466. * fit_image_get_node - get node offset for component image of a given unit name
  467. * @fit: pointer to the FIT format image header
  468. * @image_uname: component image node unit name
  469. *
  470. * fit_image_get_node() finds a component image (within the '/images'
  471. * node) of a provided unit name. If image is found its node offset is
  472. * returned to the caller.
  473. *
  474. * returns:
  475. * image node offset when found (>=0)
  476. * negative number on failure (FDT_ERR_* code)
  477. */
  478. int fit_image_get_node(const void *fit, const char *image_uname)
  479. {
  480. int noffset, images_noffset;
  481. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  482. if (images_noffset < 0) {
  483. debug("Can't find images parent node '%s' (%s)\n",
  484. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  485. return images_noffset;
  486. }
  487. noffset = fdt_subnode_offset(fit, images_noffset, image_uname);
  488. if (noffset < 0) {
  489. debug("Can't get node offset for image unit name: '%s' (%s)\n",
  490. image_uname, fdt_strerror(noffset));
  491. }
  492. return noffset;
  493. }
  494. /**
  495. * fit_image_get_os - get os id for a given component image node
  496. * @fit: pointer to the FIT format image header
  497. * @noffset: component image node offset
  498. * @os: pointer to the uint8_t, will hold os numeric id
  499. *
  500. * fit_image_get_os() finds os property in a given component image node.
  501. * If the property is found, its (string) value is translated to the numeric
  502. * id which is returned to the caller.
  503. *
  504. * returns:
  505. * 0, on success
  506. * -1, on failure
  507. */
  508. int fit_image_get_os(const void *fit, int noffset, uint8_t *os)
  509. {
  510. int len;
  511. const void *data;
  512. /* Get OS name from property data */
  513. data = fdt_getprop(fit, noffset, FIT_OS_PROP, &len);
  514. if (data == NULL) {
  515. fit_get_debug(fit, noffset, FIT_OS_PROP, len);
  516. *os = -1;
  517. return -1;
  518. }
  519. /* Translate OS name to id */
  520. *os = genimg_get_os_id(data);
  521. return 0;
  522. }
  523. /**
  524. * fit_image_get_arch - get arch id for a given component image node
  525. * @fit: pointer to the FIT format image header
  526. * @noffset: component image node offset
  527. * @arch: pointer to the uint8_t, will hold arch numeric id
  528. *
  529. * fit_image_get_arch() finds arch property in a given component image node.
  530. * If the property is found, its (string) value is translated to the numeric
  531. * id which is returned to the caller.
  532. *
  533. * returns:
  534. * 0, on success
  535. * -1, on failure
  536. */
  537. int fit_image_get_arch(const void *fit, int noffset, uint8_t *arch)
  538. {
  539. int len;
  540. const void *data;
  541. /* Get architecture name from property data */
  542. data = fdt_getprop(fit, noffset, FIT_ARCH_PROP, &len);
  543. if (data == NULL) {
  544. fit_get_debug(fit, noffset, FIT_ARCH_PROP, len);
  545. *arch = -1;
  546. return -1;
  547. }
  548. /* Translate architecture name to id */
  549. *arch = genimg_get_arch_id(data);
  550. return 0;
  551. }
  552. /**
  553. * fit_image_get_type - get type id for a given component image node
  554. * @fit: pointer to the FIT format image header
  555. * @noffset: component image node offset
  556. * @type: pointer to the uint8_t, will hold type numeric id
  557. *
  558. * fit_image_get_type() finds type property in a given component image node.
  559. * If the property is found, its (string) value is translated to the numeric
  560. * id which is returned to the caller.
  561. *
  562. * returns:
  563. * 0, on success
  564. * -1, on failure
  565. */
  566. int fit_image_get_type(const void *fit, int noffset, uint8_t *type)
  567. {
  568. int len;
  569. const void *data;
  570. /* Get image type name from property data */
  571. data = fdt_getprop(fit, noffset, FIT_TYPE_PROP, &len);
  572. if (data == NULL) {
  573. fit_get_debug(fit, noffset, FIT_TYPE_PROP, len);
  574. *type = -1;
  575. return -1;
  576. }
  577. /* Translate image type name to id */
  578. *type = genimg_get_type_id(data);
  579. return 0;
  580. }
  581. /**
  582. * fit_image_get_comp - get comp id for a given component image node
  583. * @fit: pointer to the FIT format image header
  584. * @noffset: component image node offset
  585. * @comp: pointer to the uint8_t, will hold comp numeric id
  586. *
  587. * fit_image_get_comp() finds comp property in a given component image node.
  588. * If the property is found, its (string) value is translated to the numeric
  589. * id which is returned to the caller.
  590. *
  591. * returns:
  592. * 0, on success
  593. * -1, on failure
  594. */
  595. int fit_image_get_comp(const void *fit, int noffset, uint8_t *comp)
  596. {
  597. int len;
  598. const void *data;
  599. /* Get compression name from property data */
  600. data = fdt_getprop(fit, noffset, FIT_COMP_PROP, &len);
  601. if (data == NULL) {
  602. fit_get_debug(fit, noffset, FIT_COMP_PROP, len);
  603. *comp = -1;
  604. return -1;
  605. }
  606. /* Translate compression name to id */
  607. *comp = genimg_get_comp_id(data);
  608. return 0;
  609. }
  610. static int fit_image_get_address(const void *fit, int noffset, char *name,
  611. ulong *load)
  612. {
  613. int len, cell_len;
  614. const fdt32_t *cell;
  615. uint64_t load64 = 0;
  616. cell = fdt_getprop(fit, noffset, name, &len);
  617. if (cell == NULL) {
  618. fit_get_debug(fit, noffset, name, len);
  619. return -1;
  620. }
  621. if (len > sizeof(ulong)) {
  622. printf("Unsupported %s address size\n", name);
  623. return -1;
  624. }
  625. cell_len = len >> 2;
  626. /* Use load64 to avoid compiling warning for 32-bit target */
  627. while (cell_len--) {
  628. load64 = (load64 << 32) | uimage_to_cpu(*cell);
  629. cell++;
  630. }
  631. *load = (ulong)load64;
  632. return 0;
  633. }
  634. /**
  635. * fit_image_get_load() - get load addr property for given component image node
  636. * @fit: pointer to the FIT format image header
  637. * @noffset: component image node offset
  638. * @load: pointer to the uint32_t, will hold load address
  639. *
  640. * fit_image_get_load() finds load address property in a given component
  641. * image node. If the property is found, its value is returned to the caller.
  642. *
  643. * returns:
  644. * 0, on success
  645. * -1, on failure
  646. */
  647. int fit_image_get_load(const void *fit, int noffset, ulong *load)
  648. {
  649. return fit_image_get_address(fit, noffset, FIT_LOAD_PROP, load);
  650. }
  651. /**
  652. * fit_image_get_entry() - get entry point address property
  653. * @fit: pointer to the FIT format image header
  654. * @noffset: component image node offset
  655. * @entry: pointer to the uint32_t, will hold entry point address
  656. *
  657. * This gets the entry point address property for a given component image
  658. * node.
  659. *
  660. * fit_image_get_entry() finds entry point address property in a given
  661. * component image node. If the property is found, its value is returned
  662. * to the caller.
  663. *
  664. * returns:
  665. * 0, on success
  666. * -1, on failure
  667. */
  668. int fit_image_get_entry(const void *fit, int noffset, ulong *entry)
  669. {
  670. return fit_image_get_address(fit, noffset, FIT_ENTRY_PROP, entry);
  671. }
  672. /**
  673. * fit_image_get_data - get data property and its size for a given component image node
  674. * @fit: pointer to the FIT format image header
  675. * @noffset: component image node offset
  676. * @data: double pointer to void, will hold data property's data address
  677. * @size: pointer to size_t, will hold data property's data size
  678. *
  679. * fit_image_get_data() finds data property in a given component image node.
  680. * If the property is found its data start address and size are returned to
  681. * the caller.
  682. *
  683. * returns:
  684. * 0, on success
  685. * -1, on failure
  686. */
  687. int fit_image_get_data(const void *fit, int noffset,
  688. const void **data, size_t *size)
  689. {
  690. int len;
  691. *data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
  692. if (*data == NULL) {
  693. fit_get_debug(fit, noffset, FIT_DATA_PROP, len);
  694. *size = 0;
  695. return -1;
  696. }
  697. *size = len;
  698. return 0;
  699. }
  700. /**
  701. * fit_image_hash_get_algo - get hash algorithm name
  702. * @fit: pointer to the FIT format image header
  703. * @noffset: hash node offset
  704. * @algo: double pointer to char, will hold pointer to the algorithm name
  705. *
  706. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  707. * If the property is found its data start address is returned to the caller.
  708. *
  709. * returns:
  710. * 0, on success
  711. * -1, on failure
  712. */
  713. int fit_image_hash_get_algo(const void *fit, int noffset, char **algo)
  714. {
  715. int len;
  716. *algo = (char *)fdt_getprop(fit, noffset, FIT_ALGO_PROP, &len);
  717. if (*algo == NULL) {
  718. fit_get_debug(fit, noffset, FIT_ALGO_PROP, len);
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * fit_image_hash_get_value - get hash value and length
  725. * @fit: pointer to the FIT format image header
  726. * @noffset: hash node offset
  727. * @value: double pointer to uint8_t, will hold address of a hash value data
  728. * @value_len: pointer to an int, will hold hash data length
  729. *
  730. * fit_image_hash_get_value() finds hash value property in a given hash node.
  731. * If the property is found its data start address and size are returned to
  732. * the caller.
  733. *
  734. * returns:
  735. * 0, on success
  736. * -1, on failure
  737. */
  738. int fit_image_hash_get_value(const void *fit, int noffset, uint8_t **value,
  739. int *value_len)
  740. {
  741. int len;
  742. *value = (uint8_t *)fdt_getprop(fit, noffset, FIT_VALUE_PROP, &len);
  743. if (*value == NULL) {
  744. fit_get_debug(fit, noffset, FIT_VALUE_PROP, len);
  745. *value_len = 0;
  746. return -1;
  747. }
  748. *value_len = len;
  749. return 0;
  750. }
  751. /**
  752. * fit_image_hash_get_ignore - get hash ignore flag
  753. * @fit: pointer to the FIT format image header
  754. * @noffset: hash node offset
  755. * @ignore: pointer to an int, will hold hash ignore flag
  756. *
  757. * fit_image_hash_get_ignore() finds hash ignore property in a given hash node.
  758. * If the property is found and non-zero, the hash algorithm is not verified by
  759. * u-boot automatically.
  760. *
  761. * returns:
  762. * 0, on ignore not found
  763. * value, on ignore found
  764. */
  765. static int fit_image_hash_get_ignore(const void *fit, int noffset, int *ignore)
  766. {
  767. int len;
  768. int *value;
  769. value = (int *)fdt_getprop(fit, noffset, FIT_IGNORE_PROP, &len);
  770. if (value == NULL || len != sizeof(int))
  771. *ignore = 0;
  772. else
  773. *ignore = *value;
  774. return 0;
  775. }
  776. ulong fit_get_end(const void *fit)
  777. {
  778. return map_to_sysmem((void *)(fit + fdt_totalsize(fit)));
  779. }
  780. /**
  781. * fit_set_timestamp - set node timestamp property
  782. * @fit: pointer to the FIT format image header
  783. * @noffset: node offset
  784. * @timestamp: timestamp value to be set
  785. *
  786. * fit_set_timestamp() attempts to set timestamp property in the requested
  787. * node and returns operation status to the caller.
  788. *
  789. * returns:
  790. * 0, on success
  791. * -ENOSPC if no space in device tree, -1 for other error
  792. */
  793. int fit_set_timestamp(void *fit, int noffset, time_t timestamp)
  794. {
  795. uint32_t t;
  796. int ret;
  797. t = cpu_to_uimage(timestamp);
  798. ret = fdt_setprop(fit, noffset, FIT_TIMESTAMP_PROP, &t,
  799. sizeof(uint32_t));
  800. if (ret) {
  801. debug("Can't set '%s' property for '%s' node (%s)\n",
  802. FIT_TIMESTAMP_PROP, fit_get_name(fit, noffset, NULL),
  803. fdt_strerror(ret));
  804. return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -1;
  805. }
  806. return 0;
  807. }
  808. /**
  809. * calculate_hash - calculate and return hash for provided input data
  810. * @data: pointer to the input data
  811. * @data_len: data length
  812. * @algo: requested hash algorithm
  813. * @value: pointer to the char, will hold hash value data (caller must
  814. * allocate enough free space)
  815. * value_len: length of the calculated hash
  816. *
  817. * calculate_hash() computes input data hash according to the requested
  818. * algorithm.
  819. * Resulting hash value is placed in caller provided 'value' buffer, length
  820. * of the calculated hash is returned via value_len pointer argument.
  821. *
  822. * returns:
  823. * 0, on success
  824. * -1, when algo is unsupported
  825. */
  826. int calculate_hash(const void *data, int data_len, const char *algo,
  827. uint8_t *value, int *value_len)
  828. {
  829. if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) {
  830. *((uint32_t *)value) = crc32_wd(0, data, data_len,
  831. CHUNKSZ_CRC32);
  832. *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value));
  833. *value_len = 4;
  834. } else if (IMAGE_ENABLE_SHA1 && strcmp(algo, "sha1") == 0) {
  835. sha1_csum_wd((unsigned char *)data, data_len,
  836. (unsigned char *)value, CHUNKSZ_SHA1);
  837. *value_len = 20;
  838. } else if (IMAGE_ENABLE_SHA256 && strcmp(algo, "sha256") == 0) {
  839. sha256_csum_wd((unsigned char *)data, data_len,
  840. (unsigned char *)value, CHUNKSZ_SHA256);
  841. *value_len = SHA256_SUM_LEN;
  842. } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) {
  843. md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5);
  844. *value_len = 16;
  845. } else {
  846. debug("Unsupported hash alogrithm\n");
  847. return -1;
  848. }
  849. return 0;
  850. }
  851. static int fit_image_check_hash(const void *fit, int noffset, const void *data,
  852. size_t size, char **err_msgp)
  853. {
  854. uint8_t value[FIT_MAX_HASH_LEN];
  855. int value_len;
  856. char *algo;
  857. uint8_t *fit_value;
  858. int fit_value_len;
  859. int ignore;
  860. *err_msgp = NULL;
  861. if (fit_image_hash_get_algo(fit, noffset, &algo)) {
  862. *err_msgp = "Can't get hash algo property";
  863. return -1;
  864. }
  865. printf("%s", algo);
  866. if (IMAGE_ENABLE_IGNORE) {
  867. fit_image_hash_get_ignore(fit, noffset, &ignore);
  868. if (ignore) {
  869. printf("-skipped ");
  870. return 0;
  871. }
  872. }
  873. if (fit_image_hash_get_value(fit, noffset, &fit_value,
  874. &fit_value_len)) {
  875. *err_msgp = "Can't get hash value property";
  876. return -1;
  877. }
  878. if (calculate_hash(data, size, algo, value, &value_len)) {
  879. *err_msgp = "Unsupported hash algorithm";
  880. return -1;
  881. }
  882. if (value_len != fit_value_len) {
  883. *err_msgp = "Bad hash value len";
  884. return -1;
  885. } else if (memcmp(value, fit_value, value_len) != 0) {
  886. *err_msgp = "Bad hash value";
  887. return -1;
  888. }
  889. return 0;
  890. }
  891. /**
  892. * fit_image_verify - verify data integrity
  893. * @fit: pointer to the FIT format image header
  894. * @image_noffset: component image node offset
  895. *
  896. * fit_image_verify() goes over component image hash nodes,
  897. * re-calculates each data hash and compares with the value stored in hash
  898. * node.
  899. *
  900. * returns:
  901. * 1, if all hashes are valid
  902. * 0, otherwise (or on error)
  903. */
  904. int fit_image_verify(const void *fit, int image_noffset)
  905. {
  906. const void *data;
  907. size_t size;
  908. int noffset = 0;
  909. char *err_msg = "";
  910. int verify_all = 1;
  911. int ret;
  912. /* Get image data and data length */
  913. if (fit_image_get_data(fit, image_noffset, &data, &size)) {
  914. err_msg = "Can't get image data/size";
  915. goto error;
  916. }
  917. /* Verify all required signatures */
  918. if (IMAGE_ENABLE_VERIFY &&
  919. fit_image_verify_required_sigs(fit, image_noffset, data, size,
  920. gd_fdt_blob(), &verify_all)) {
  921. err_msg = "Unable to verify required signature";
  922. goto error;
  923. }
  924. /* Process all hash subnodes of the component image node */
  925. fdt_for_each_subnode(noffset, fit, image_noffset) {
  926. const char *name = fit_get_name(fit, noffset, NULL);
  927. /*
  928. * Check subnode name, must be equal to "hash".
  929. * Multiple hash nodes require unique unit node
  930. * names, e.g. hash@1, hash@2, etc.
  931. */
  932. if (!strncmp(name, FIT_HASH_NODENAME,
  933. strlen(FIT_HASH_NODENAME))) {
  934. if (fit_image_check_hash(fit, noffset, data, size,
  935. &err_msg))
  936. goto error;
  937. puts("+ ");
  938. } else if (IMAGE_ENABLE_VERIFY && verify_all &&
  939. !strncmp(name, FIT_SIG_NODENAME,
  940. strlen(FIT_SIG_NODENAME))) {
  941. ret = fit_image_check_sig(fit, noffset, data,
  942. size, -1, &err_msg);
  943. /*
  944. * Show an indication on failure, but do not return
  945. * an error. Only keys marked 'required' can cause
  946. * an image validation failure. See the call to
  947. * fit_image_verify_required_sigs() above.
  948. */
  949. if (ret)
  950. puts("- ");
  951. else
  952. puts("+ ");
  953. }
  954. }
  955. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  956. err_msg = "Corrupted or truncated tree";
  957. goto error;
  958. }
  959. return 1;
  960. error:
  961. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  962. err_msg, fit_get_name(fit, noffset, NULL),
  963. fit_get_name(fit, image_noffset, NULL));
  964. return 0;
  965. }
  966. /**
  967. * fit_all_image_verify - verify data integrity for all images
  968. * @fit: pointer to the FIT format image header
  969. *
  970. * fit_all_image_verify() goes over all images in the FIT and
  971. * for every images checks if all it's hashes are valid.
  972. *
  973. * returns:
  974. * 1, if all hashes of all images are valid
  975. * 0, otherwise (or on error)
  976. */
  977. int fit_all_image_verify(const void *fit)
  978. {
  979. int images_noffset;
  980. int noffset;
  981. int ndepth;
  982. int count;
  983. /* Find images parent node offset */
  984. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  985. if (images_noffset < 0) {
  986. printf("Can't find images parent node '%s' (%s)\n",
  987. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  988. return 0;
  989. }
  990. /* Process all image subnodes, check hashes for each */
  991. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  992. (ulong)fit);
  993. for (ndepth = 0, count = 0,
  994. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  995. (noffset >= 0) && (ndepth > 0);
  996. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  997. if (ndepth == 1) {
  998. /*
  999. * Direct child node of the images parent node,
  1000. * i.e. component image node.
  1001. */
  1002. printf(" Hash(es) for Image %u (%s): ", count,
  1003. fit_get_name(fit, noffset, NULL));
  1004. count++;
  1005. if (!fit_image_verify(fit, noffset))
  1006. return 0;
  1007. printf("\n");
  1008. }
  1009. }
  1010. return 1;
  1011. }
  1012. /**
  1013. * fit_image_check_os - check whether image node is of a given os type
  1014. * @fit: pointer to the FIT format image header
  1015. * @noffset: component image node offset
  1016. * @os: requested image os
  1017. *
  1018. * fit_image_check_os() reads image os property and compares its numeric
  1019. * id with the requested os. Comparison result is returned to the caller.
  1020. *
  1021. * returns:
  1022. * 1 if image is of given os type
  1023. * 0 otherwise (or on error)
  1024. */
  1025. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1026. {
  1027. uint8_t image_os;
  1028. if (fit_image_get_os(fit, noffset, &image_os))
  1029. return 0;
  1030. return (os == image_os);
  1031. }
  1032. /**
  1033. * fit_image_check_arch - check whether image node is of a given arch
  1034. * @fit: pointer to the FIT format image header
  1035. * @noffset: component image node offset
  1036. * @arch: requested imagearch
  1037. *
  1038. * fit_image_check_arch() reads image arch property and compares its numeric
  1039. * id with the requested arch. Comparison result is returned to the caller.
  1040. *
  1041. * returns:
  1042. * 1 if image is of given arch
  1043. * 0 otherwise (or on error)
  1044. */
  1045. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1046. {
  1047. uint8_t image_arch;
  1048. int aarch32_support = 0;
  1049. #ifdef CONFIG_ARM64_SUPPORT_AARCH32
  1050. aarch32_support = 1;
  1051. #endif
  1052. if (fit_image_get_arch(fit, noffset, &image_arch))
  1053. return 0;
  1054. return (arch == image_arch) ||
  1055. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64) ||
  1056. (arch == IH_ARCH_ARM64 && image_arch == IH_ARCH_ARM &&
  1057. aarch32_support);
  1058. }
  1059. /**
  1060. * fit_image_check_type - check whether image node is of a given type
  1061. * @fit: pointer to the FIT format image header
  1062. * @noffset: component image node offset
  1063. * @type: requested image type
  1064. *
  1065. * fit_image_check_type() reads image type property and compares its numeric
  1066. * id with the requested type. Comparison result is returned to the caller.
  1067. *
  1068. * returns:
  1069. * 1 if image is of given type
  1070. * 0 otherwise (or on error)
  1071. */
  1072. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1073. {
  1074. uint8_t image_type;
  1075. if (fit_image_get_type(fit, noffset, &image_type))
  1076. return 0;
  1077. return (type == image_type);
  1078. }
  1079. /**
  1080. * fit_image_check_comp - check whether image node uses given compression
  1081. * @fit: pointer to the FIT format image header
  1082. * @noffset: component image node offset
  1083. * @comp: requested image compression type
  1084. *
  1085. * fit_image_check_comp() reads image compression property and compares its
  1086. * numeric id with the requested compression type. Comparison result is
  1087. * returned to the caller.
  1088. *
  1089. * returns:
  1090. * 1 if image uses requested compression
  1091. * 0 otherwise (or on error)
  1092. */
  1093. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1094. {
  1095. uint8_t image_comp;
  1096. if (fit_image_get_comp(fit, noffset, &image_comp))
  1097. return 0;
  1098. return (comp == image_comp);
  1099. }
  1100. /**
  1101. * fit_check_format - sanity check FIT image format
  1102. * @fit: pointer to the FIT format image header
  1103. *
  1104. * fit_check_format() runs a basic sanity FIT image verification.
  1105. * Routine checks for mandatory properties, nodes, etc.
  1106. *
  1107. * returns:
  1108. * 1, on success
  1109. * 0, on failure
  1110. */
  1111. int fit_check_format(const void *fit)
  1112. {
  1113. /* mandatory / node 'description' property */
  1114. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1115. debug("Wrong FIT format: no description\n");
  1116. return 0;
  1117. }
  1118. if (IMAGE_ENABLE_TIMESTAMP) {
  1119. /* mandatory / node 'timestamp' property */
  1120. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1121. debug("Wrong FIT format: no timestamp\n");
  1122. return 0;
  1123. }
  1124. }
  1125. /* mandatory subimages parent '/images' node */
  1126. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1127. debug("Wrong FIT format: no images parent node\n");
  1128. return 0;
  1129. }
  1130. return 1;
  1131. }
  1132. /**
  1133. * fit_conf_find_compat
  1134. * @fit: pointer to the FIT format image header
  1135. * @fdt: pointer to the device tree to compare against
  1136. *
  1137. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1138. * most compatible with the passed in device tree.
  1139. *
  1140. * Example:
  1141. *
  1142. * / o image-tree
  1143. * |-o images
  1144. * | |-o fdt@1
  1145. * | |-o fdt@2
  1146. * |
  1147. * |-o configurations
  1148. * |-o config@1
  1149. * | |-fdt = fdt@1
  1150. * |
  1151. * |-o config@2
  1152. * |-fdt = fdt@2
  1153. *
  1154. * / o U-Boot fdt
  1155. * |-compatible = "foo,bar", "bim,bam"
  1156. *
  1157. * / o kernel fdt1
  1158. * |-compatible = "foo,bar",
  1159. *
  1160. * / o kernel fdt2
  1161. * |-compatible = "bim,bam", "baz,biz"
  1162. *
  1163. * Configuration 1 would be picked because the first string in U-Boot's
  1164. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1165. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1166. *
  1167. * returns:
  1168. * offset to the configuration to use if one was found
  1169. * -1 otherwise
  1170. */
  1171. int fit_conf_find_compat(const void *fit, const void *fdt)
  1172. {
  1173. int ndepth = 0;
  1174. int noffset, confs_noffset, images_noffset;
  1175. const void *fdt_compat;
  1176. int fdt_compat_len;
  1177. int best_match_offset = 0;
  1178. int best_match_pos = 0;
  1179. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1180. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1181. if (confs_noffset < 0 || images_noffset < 0) {
  1182. debug("Can't find configurations or images nodes.\n");
  1183. return -1;
  1184. }
  1185. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1186. if (!fdt_compat) {
  1187. debug("Fdt for comparison has no \"compatible\" property.\n");
  1188. return -1;
  1189. }
  1190. /*
  1191. * Loop over the configurations in the FIT image.
  1192. */
  1193. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1194. (noffset >= 0) && (ndepth > 0);
  1195. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1196. const void *kfdt;
  1197. const char *kfdt_name;
  1198. int kfdt_noffset;
  1199. const char *cur_fdt_compat;
  1200. int len;
  1201. size_t size;
  1202. int i;
  1203. if (ndepth > 1)
  1204. continue;
  1205. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1206. if (!kfdt_name) {
  1207. debug("No fdt property found.\n");
  1208. continue;
  1209. }
  1210. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1211. kfdt_name);
  1212. if (kfdt_noffset < 0) {
  1213. debug("No image node named \"%s\" found.\n",
  1214. kfdt_name);
  1215. continue;
  1216. }
  1217. /*
  1218. * Get a pointer to this configuration's fdt.
  1219. */
  1220. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1221. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1222. continue;
  1223. }
  1224. len = fdt_compat_len;
  1225. cur_fdt_compat = fdt_compat;
  1226. /*
  1227. * Look for a match for each U-Boot compatibility string in
  1228. * turn in this configuration's fdt.
  1229. */
  1230. for (i = 0; len > 0 &&
  1231. (!best_match_offset || best_match_pos > i); i++) {
  1232. int cur_len = strlen(cur_fdt_compat) + 1;
  1233. if (!fdt_node_check_compatible(kfdt, 0,
  1234. cur_fdt_compat)) {
  1235. best_match_offset = noffset;
  1236. best_match_pos = i;
  1237. break;
  1238. }
  1239. len -= cur_len;
  1240. cur_fdt_compat += cur_len;
  1241. }
  1242. }
  1243. if (!best_match_offset) {
  1244. debug("No match found.\n");
  1245. return -1;
  1246. }
  1247. return best_match_offset;
  1248. }
  1249. /**
  1250. * fit_conf_get_node - get node offset for configuration of a given unit name
  1251. * @fit: pointer to the FIT format image header
  1252. * @conf_uname: configuration node unit name
  1253. *
  1254. * fit_conf_get_node() finds a configuration (within the '/configurations'
  1255. * parent node) of a provided unit name. If configuration is found its node
  1256. * offset is returned to the caller.
  1257. *
  1258. * When NULL is provided in second argument fit_conf_get_node() will search
  1259. * for a default configuration node instead. Default configuration node unit
  1260. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1261. * node.
  1262. *
  1263. * returns:
  1264. * configuration node offset when found (>=0)
  1265. * negative number on failure (FDT_ERR_* code)
  1266. */
  1267. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1268. {
  1269. int noffset, confs_noffset;
  1270. int len;
  1271. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1272. if (confs_noffset < 0) {
  1273. debug("Can't find configurations parent node '%s' (%s)\n",
  1274. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1275. return confs_noffset;
  1276. }
  1277. if (conf_uname == NULL) {
  1278. /* get configuration unit name from the default property */
  1279. debug("No configuration specified, trying default...\n");
  1280. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1281. FIT_DEFAULT_PROP, &len);
  1282. if (conf_uname == NULL) {
  1283. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1284. len);
  1285. return len;
  1286. }
  1287. debug("Found default configuration: '%s'\n", conf_uname);
  1288. }
  1289. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1290. if (noffset < 0) {
  1291. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1292. conf_uname, fdt_strerror(noffset));
  1293. }
  1294. return noffset;
  1295. }
  1296. int fit_conf_get_prop_node(const void *fit, int noffset,
  1297. const char *prop_name)
  1298. {
  1299. char *uname;
  1300. int len;
  1301. /* get kernel image unit name from configuration kernel property */
  1302. uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
  1303. if (uname == NULL)
  1304. return len;
  1305. return fit_image_get_node(fit, uname);
  1306. }
  1307. /**
  1308. * fit_conf_print - prints out the FIT configuration details
  1309. * @fit: pointer to the FIT format image header
  1310. * @noffset: offset of the configuration node
  1311. * @p: pointer to prefix string
  1312. *
  1313. * fit_conf_print() lists all mandatory properties for the processed
  1314. * configuration node.
  1315. *
  1316. * returns:
  1317. * no returned results
  1318. */
  1319. void fit_conf_print(const void *fit, int noffset, const char *p)
  1320. {
  1321. char *desc;
  1322. const char *uname;
  1323. int ret;
  1324. int loadables_index;
  1325. /* Mandatory properties */
  1326. ret = fit_get_desc(fit, noffset, &desc);
  1327. printf("%s Description: ", p);
  1328. if (ret)
  1329. printf("unavailable\n");
  1330. else
  1331. printf("%s\n", desc);
  1332. uname = fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1333. printf("%s Kernel: ", p);
  1334. if (uname == NULL)
  1335. printf("unavailable\n");
  1336. else
  1337. printf("%s\n", uname);
  1338. /* Optional properties */
  1339. uname = fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1340. if (uname)
  1341. printf("%s Init Ramdisk: %s\n", p, uname);
  1342. uname = fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
  1343. if (uname)
  1344. printf("%s FDT: %s\n", p, uname);
  1345. uname = fdt_getprop(fit, noffset, FIT_FPGA_PROP, NULL);
  1346. if (uname)
  1347. printf("%s FPGA: %s\n", p, uname);
  1348. /* Print out all of the specified loadables */
  1349. for (loadables_index = 0;
  1350. uname = fdt_stringlist_get(fit, noffset, FIT_LOADABLE_PROP,
  1351. loadables_index, NULL), uname;
  1352. loadables_index++) {
  1353. if (loadables_index == 0) {
  1354. printf("%s Loadables: ", p);
  1355. } else {
  1356. printf("%s ", p);
  1357. }
  1358. printf("%s\n", uname);
  1359. }
  1360. }
  1361. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1362. {
  1363. #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1364. const void *data;
  1365. size_t size;
  1366. int ret;
  1367. #endif
  1368. fit_image_print(fit, rd_noffset, " ");
  1369. if (verify) {
  1370. puts(" Verifying Hash Integrity ... ");
  1371. if (!fit_image_verify(fit, rd_noffset)) {
  1372. puts("Bad Data Hash\n");
  1373. return -EACCES;
  1374. }
  1375. puts("OK\n");
  1376. }
  1377. #if !defined(USE_HOSTCC) && defined(CONFIG_FIT_IMAGE_POST_PROCESS)
  1378. ret = fit_image_get_data(fit, rd_noffset, &data, &size);
  1379. if (ret)
  1380. return ret;
  1381. /* perform any post-processing on the image data */
  1382. board_fit_image_post_process((void **)&data, &size);
  1383. /*
  1384. * update U-Boot's understanding of the "data" property start address
  1385. * and size according to the performed post-processing
  1386. */
  1387. ret = fdt_setprop((void *)fit, rd_noffset, FIT_DATA_PROP, data, size);
  1388. if (ret)
  1389. return ret;
  1390. #endif
  1391. return 0;
  1392. }
  1393. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1394. ulong addr)
  1395. {
  1396. int cfg_noffset;
  1397. void *fit_hdr;
  1398. int noffset;
  1399. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1400. prop_name, images->fit_uname_cfg, addr);
  1401. /* Check whether configuration has this property defined */
  1402. fit_hdr = map_sysmem(addr, 0);
  1403. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1404. if (cfg_noffset < 0) {
  1405. debug("* %s: no such config\n", prop_name);
  1406. return -EINVAL;
  1407. }
  1408. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1409. if (noffset < 0) {
  1410. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1411. return -ENOENT;
  1412. }
  1413. return noffset;
  1414. }
  1415. /**
  1416. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1417. *
  1418. * @return the properly name where we expect to find the image in the
  1419. * config node
  1420. */
  1421. static const char *fit_get_image_type_property(int type)
  1422. {
  1423. /*
  1424. * This is sort-of available in the uimage_type[] table in image.c
  1425. * but we don't have access to the short name, and "fdt" is different
  1426. * anyway. So let's just keep it here.
  1427. */
  1428. switch (type) {
  1429. case IH_TYPE_FLATDT:
  1430. return FIT_FDT_PROP;
  1431. case IH_TYPE_KERNEL:
  1432. return FIT_KERNEL_PROP;
  1433. case IH_TYPE_RAMDISK:
  1434. return FIT_RAMDISK_PROP;
  1435. case IH_TYPE_X86_SETUP:
  1436. return FIT_SETUP_PROP;
  1437. case IH_TYPE_LOADABLE:
  1438. return FIT_LOADABLE_PROP;
  1439. case IH_TYPE_FPGA:
  1440. return FIT_FPGA_PROP;
  1441. }
  1442. return "unknown";
  1443. }
  1444. int fit_image_load(bootm_headers_t *images, ulong addr,
  1445. const char **fit_unamep, const char **fit_uname_configp,
  1446. int arch, int image_type, int bootstage_id,
  1447. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1448. {
  1449. int cfg_noffset, noffset;
  1450. const char *fit_uname;
  1451. const char *fit_uname_config;
  1452. const void *fit;
  1453. const void *buf;
  1454. size_t size;
  1455. int type_ok, os_ok;
  1456. ulong load, data, len;
  1457. uint8_t os;
  1458. #ifndef USE_HOSTCC
  1459. uint8_t os_arch;
  1460. #endif
  1461. const char *prop_name;
  1462. int ret;
  1463. fit = map_sysmem(addr, 0);
  1464. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1465. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1466. prop_name = fit_get_image_type_property(image_type);
  1467. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1468. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1469. if (!fit_check_format(fit)) {
  1470. printf("Bad FIT %s image format!\n", prop_name);
  1471. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1472. return -ENOEXEC;
  1473. }
  1474. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1475. if (fit_uname) {
  1476. /* get FIT component image node offset */
  1477. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1478. noffset = fit_image_get_node(fit, fit_uname);
  1479. } else {
  1480. /*
  1481. * no image node unit name, try to get config
  1482. * node first. If config unit node name is NULL
  1483. * fit_conf_get_node() will try to find default config node
  1484. */
  1485. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1486. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1487. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1488. } else {
  1489. cfg_noffset = fit_conf_get_node(fit,
  1490. fit_uname_config);
  1491. }
  1492. if (cfg_noffset < 0) {
  1493. puts("Could not find configuration node\n");
  1494. bootstage_error(bootstage_id +
  1495. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1496. return -ENOENT;
  1497. }
  1498. fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1499. printf(" Using '%s' configuration\n", fit_uname_config);
  1500. if (image_type == IH_TYPE_KERNEL) {
  1501. /* Remember (and possibly verify) this config */
  1502. images->fit_uname_cfg = fit_uname_config;
  1503. if (IMAGE_ENABLE_VERIFY && images->verify) {
  1504. puts(" Verifying Hash Integrity ... ");
  1505. if (fit_config_verify(fit, cfg_noffset)) {
  1506. puts("Bad Data Hash\n");
  1507. bootstage_error(bootstage_id +
  1508. BOOTSTAGE_SUB_HASH);
  1509. return -EACCES;
  1510. }
  1511. puts("OK\n");
  1512. }
  1513. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1514. }
  1515. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1516. prop_name);
  1517. fit_uname = fit_get_name(fit, noffset, NULL);
  1518. }
  1519. if (noffset < 0) {
  1520. puts("Could not find subimage node\n");
  1521. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1522. return -ENOENT;
  1523. }
  1524. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1525. ret = fit_image_select(fit, noffset, images->verify);
  1526. if (ret) {
  1527. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1528. return ret;
  1529. }
  1530. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1531. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1532. if (!fit_image_check_target_arch(fit, noffset)) {
  1533. puts("Unsupported Architecture\n");
  1534. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1535. return -ENOEXEC;
  1536. }
  1537. #endif
  1538. #ifndef USE_HOSTCC
  1539. fit_image_get_arch(fit, noffset, &os_arch);
  1540. images->os.arch = os_arch;
  1541. #endif
  1542. if (image_type == IH_TYPE_FLATDT &&
  1543. !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
  1544. puts("FDT image is compressed");
  1545. return -EPROTONOSUPPORT;
  1546. }
  1547. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1548. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1549. fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE) ||
  1550. (image_type == IH_TYPE_KERNEL &&
  1551. fit_image_check_type(fit, noffset, IH_TYPE_KERNEL_NOLOAD));
  1552. os_ok = image_type == IH_TYPE_FLATDT ||
  1553. image_type == IH_TYPE_FPGA ||
  1554. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1555. fit_image_check_os(fit, noffset, IH_OS_U_BOOT) ||
  1556. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
  1557. /*
  1558. * If either of the checks fail, we should report an error, but
  1559. * if the image type is coming from the "loadables" field, we
  1560. * don't care what it is
  1561. */
  1562. if ((!type_ok || !os_ok) && image_type != IH_TYPE_LOADABLE) {
  1563. fit_image_get_os(fit, noffset, &os);
  1564. printf("No %s %s %s Image\n",
  1565. genimg_get_os_name(os),
  1566. genimg_get_arch_name(arch),
  1567. genimg_get_type_name(image_type));
  1568. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1569. return -EIO;
  1570. }
  1571. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1572. /* get image data address and length */
  1573. if (fit_image_get_data(fit, noffset, &buf, &size)) {
  1574. printf("Could not find %s subimage data!\n", prop_name);
  1575. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1576. return -ENOENT;
  1577. }
  1578. len = (ulong)size;
  1579. /* verify that image data is a proper FDT blob */
  1580. if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
  1581. puts("Subimage data is not a FDT");
  1582. return -ENOEXEC;
  1583. }
  1584. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1585. /*
  1586. * Work-around for eldk-4.2 which gives this warning if we try to
  1587. * cast in the unmap_sysmem() call:
  1588. * warning: initialization discards qualifiers from pointer target type
  1589. */
  1590. {
  1591. void *vbuf = (void *)buf;
  1592. data = map_to_sysmem(vbuf);
  1593. }
  1594. if (load_op == FIT_LOAD_IGNORED) {
  1595. /* Don't load */
  1596. } else if (fit_image_get_load(fit, noffset, &load)) {
  1597. if (load_op == FIT_LOAD_REQUIRED) {
  1598. printf("Can't get %s subimage load address!\n",
  1599. prop_name);
  1600. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1601. return -EBADF;
  1602. }
  1603. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1604. ulong image_start, image_end;
  1605. ulong load_end;
  1606. void *dst;
  1607. /*
  1608. * move image data to the load address,
  1609. * make sure we don't overwrite initial image
  1610. */
  1611. image_start = addr;
  1612. image_end = addr + fit_get_size(fit);
  1613. load_end = load + len;
  1614. if (image_type != IH_TYPE_KERNEL &&
  1615. load < image_end && load_end > image_start) {
  1616. printf("Error: %s overwritten\n", prop_name);
  1617. return -EXDEV;
  1618. }
  1619. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1620. prop_name, data, load);
  1621. dst = map_sysmem(load, len);
  1622. memmove(dst, buf, len);
  1623. data = load;
  1624. }
  1625. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1626. *datap = data;
  1627. *lenp = len;
  1628. if (fit_unamep)
  1629. *fit_unamep = (char *)fit_uname;
  1630. if (fit_uname_configp)
  1631. *fit_uname_configp = (char *)fit_uname_config;
  1632. return noffset;
  1633. }
  1634. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1635. ulong *setup_start, ulong *setup_len)
  1636. {
  1637. int noffset;
  1638. ulong addr;
  1639. ulong len;
  1640. int ret;
  1641. addr = map_to_sysmem(images->fit_hdr_os);
  1642. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1643. if (noffset < 0)
  1644. return noffset;
  1645. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1646. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1647. FIT_LOAD_REQUIRED, setup_start, &len);
  1648. return ret;
  1649. }