image-fit.c 47 KB

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