image-fit.c 44 KB

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