image-fit.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. else
  931. puts("+ ");
  932. }
  933. }
  934. if (noffset == -FDT_ERR_TRUNCATED || noffset == -FDT_ERR_BADSTRUCTURE) {
  935. err_msg = "Corrupted or truncated tree";
  936. goto error;
  937. }
  938. return 1;
  939. error:
  940. printf(" error!\n%s for '%s' hash node in '%s' image node\n",
  941. err_msg, fit_get_name(fit, noffset, NULL),
  942. fit_get_name(fit, image_noffset, NULL));
  943. return 0;
  944. }
  945. /**
  946. * fit_all_image_verify - verify data intergity for all images
  947. * @fit: pointer to the FIT format image header
  948. *
  949. * fit_all_image_verify() goes over all images in the FIT and
  950. * for every images checks if all it's hashes are valid.
  951. *
  952. * returns:
  953. * 1, if all hashes of all images are valid
  954. * 0, otherwise (or on error)
  955. */
  956. int fit_all_image_verify(const void *fit)
  957. {
  958. int images_noffset;
  959. int noffset;
  960. int ndepth;
  961. int count;
  962. /* Find images parent node offset */
  963. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  964. if (images_noffset < 0) {
  965. printf("Can't find images parent node '%s' (%s)\n",
  966. FIT_IMAGES_PATH, fdt_strerror(images_noffset));
  967. return 0;
  968. }
  969. /* Process all image subnodes, check hashes for each */
  970. printf("## Checking hash(es) for FIT Image at %08lx ...\n",
  971. (ulong)fit);
  972. for (ndepth = 0, count = 0,
  973. noffset = fdt_next_node(fit, images_noffset, &ndepth);
  974. (noffset >= 0) && (ndepth > 0);
  975. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  976. if (ndepth == 1) {
  977. /*
  978. * Direct child node of the images parent node,
  979. * i.e. component image node.
  980. */
  981. printf(" Hash(es) for Image %u (%s): ", count++,
  982. fit_get_name(fit, noffset, NULL));
  983. if (!fit_image_verify(fit, noffset))
  984. return 0;
  985. printf("\n");
  986. }
  987. }
  988. return 1;
  989. }
  990. /**
  991. * fit_image_check_os - check whether image node is of a given os type
  992. * @fit: pointer to the FIT format image header
  993. * @noffset: component image node offset
  994. * @os: requested image os
  995. *
  996. * fit_image_check_os() reads image os property and compares its numeric
  997. * id with the requested os. Comparison result is returned to the caller.
  998. *
  999. * returns:
  1000. * 1 if image is of given os type
  1001. * 0 otherwise (or on error)
  1002. */
  1003. int fit_image_check_os(const void *fit, int noffset, uint8_t os)
  1004. {
  1005. uint8_t image_os;
  1006. if (fit_image_get_os(fit, noffset, &image_os))
  1007. return 0;
  1008. return (os == image_os);
  1009. }
  1010. /**
  1011. * fit_image_check_arch - check whether image node is of a given arch
  1012. * @fit: pointer to the FIT format image header
  1013. * @noffset: component image node offset
  1014. * @arch: requested imagearch
  1015. *
  1016. * fit_image_check_arch() reads image arch property and compares its numeric
  1017. * id with the requested arch. Comparison result is returned to the caller.
  1018. *
  1019. * returns:
  1020. * 1 if image is of given arch
  1021. * 0 otherwise (or on error)
  1022. */
  1023. int fit_image_check_arch(const void *fit, int noffset, uint8_t arch)
  1024. {
  1025. uint8_t image_arch;
  1026. if (fit_image_get_arch(fit, noffset, &image_arch))
  1027. return 0;
  1028. return (arch == image_arch) ||
  1029. (arch == IH_ARCH_I386 && image_arch == IH_ARCH_X86_64);
  1030. }
  1031. /**
  1032. * fit_image_check_type - check whether image node is of a given type
  1033. * @fit: pointer to the FIT format image header
  1034. * @noffset: component image node offset
  1035. * @type: requested image type
  1036. *
  1037. * fit_image_check_type() reads image type property and compares its numeric
  1038. * id with the requested type. Comparison result is returned to the caller.
  1039. *
  1040. * returns:
  1041. * 1 if image is of given type
  1042. * 0 otherwise (or on error)
  1043. */
  1044. int fit_image_check_type(const void *fit, int noffset, uint8_t type)
  1045. {
  1046. uint8_t image_type;
  1047. if (fit_image_get_type(fit, noffset, &image_type))
  1048. return 0;
  1049. return (type == image_type);
  1050. }
  1051. /**
  1052. * fit_image_check_comp - check whether image node uses given compression
  1053. * @fit: pointer to the FIT format image header
  1054. * @noffset: component image node offset
  1055. * @comp: requested image compression type
  1056. *
  1057. * fit_image_check_comp() reads image compression property and compares its
  1058. * numeric id with the requested compression type. Comparison result is
  1059. * returned to the caller.
  1060. *
  1061. * returns:
  1062. * 1 if image uses requested compression
  1063. * 0 otherwise (or on error)
  1064. */
  1065. int fit_image_check_comp(const void *fit, int noffset, uint8_t comp)
  1066. {
  1067. uint8_t image_comp;
  1068. if (fit_image_get_comp(fit, noffset, &image_comp))
  1069. return 0;
  1070. return (comp == image_comp);
  1071. }
  1072. /**
  1073. * fit_check_format - sanity check FIT image format
  1074. * @fit: pointer to the FIT format image header
  1075. *
  1076. * fit_check_format() runs a basic sanity FIT image verification.
  1077. * Routine checks for mandatory properties, nodes, etc.
  1078. *
  1079. * returns:
  1080. * 1, on success
  1081. * 0, on failure
  1082. */
  1083. int fit_check_format(const void *fit)
  1084. {
  1085. /* mandatory / node 'description' property */
  1086. if (fdt_getprop(fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  1087. debug("Wrong FIT format: no description\n");
  1088. return 0;
  1089. }
  1090. if (IMAGE_ENABLE_TIMESTAMP) {
  1091. /* mandatory / node 'timestamp' property */
  1092. if (fdt_getprop(fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  1093. debug("Wrong FIT format: no timestamp\n");
  1094. return 0;
  1095. }
  1096. }
  1097. /* mandatory subimages parent '/images' node */
  1098. if (fdt_path_offset(fit, FIT_IMAGES_PATH) < 0) {
  1099. debug("Wrong FIT format: no images parent node\n");
  1100. return 0;
  1101. }
  1102. return 1;
  1103. }
  1104. /**
  1105. * fit_conf_find_compat
  1106. * @fit: pointer to the FIT format image header
  1107. * @fdt: pointer to the device tree to compare against
  1108. *
  1109. * fit_conf_find_compat() attempts to find the configuration whose fdt is the
  1110. * most compatible with the passed in device tree.
  1111. *
  1112. * Example:
  1113. *
  1114. * / o image-tree
  1115. * |-o images
  1116. * | |-o fdt@1
  1117. * | |-o fdt@2
  1118. * |
  1119. * |-o configurations
  1120. * |-o config@1
  1121. * | |-fdt = fdt@1
  1122. * |
  1123. * |-o config@2
  1124. * |-fdt = fdt@2
  1125. *
  1126. * / o U-Boot fdt
  1127. * |-compatible = "foo,bar", "bim,bam"
  1128. *
  1129. * / o kernel fdt1
  1130. * |-compatible = "foo,bar",
  1131. *
  1132. * / o kernel fdt2
  1133. * |-compatible = "bim,bam", "baz,biz"
  1134. *
  1135. * Configuration 1 would be picked because the first string in U-Boot's
  1136. * compatible list, "foo,bar", matches a compatible string in the root of fdt1.
  1137. * "bim,bam" in fdt2 matches the second string which isn't as good as fdt1.
  1138. *
  1139. * returns:
  1140. * offset to the configuration to use if one was found
  1141. * -1 otherwise
  1142. */
  1143. int fit_conf_find_compat(const void *fit, const void *fdt)
  1144. {
  1145. int ndepth = 0;
  1146. int noffset, confs_noffset, images_noffset;
  1147. const void *fdt_compat;
  1148. int fdt_compat_len;
  1149. int best_match_offset = 0;
  1150. int best_match_pos = 0;
  1151. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1152. images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
  1153. if (confs_noffset < 0 || images_noffset < 0) {
  1154. debug("Can't find configurations or images nodes.\n");
  1155. return -1;
  1156. }
  1157. fdt_compat = fdt_getprop(fdt, 0, "compatible", &fdt_compat_len);
  1158. if (!fdt_compat) {
  1159. debug("Fdt for comparison has no \"compatible\" property.\n");
  1160. return -1;
  1161. }
  1162. /*
  1163. * Loop over the configurations in the FIT image.
  1164. */
  1165. for (noffset = fdt_next_node(fit, confs_noffset, &ndepth);
  1166. (noffset >= 0) && (ndepth > 0);
  1167. noffset = fdt_next_node(fit, noffset, &ndepth)) {
  1168. const void *kfdt;
  1169. const char *kfdt_name;
  1170. int kfdt_noffset;
  1171. const char *cur_fdt_compat;
  1172. int len;
  1173. size_t size;
  1174. int i;
  1175. if (ndepth > 1)
  1176. continue;
  1177. kfdt_name = fdt_getprop(fit, noffset, "fdt", &len);
  1178. if (!kfdt_name) {
  1179. debug("No fdt property found.\n");
  1180. continue;
  1181. }
  1182. kfdt_noffset = fdt_subnode_offset(fit, images_noffset,
  1183. kfdt_name);
  1184. if (kfdt_noffset < 0) {
  1185. debug("No image node named \"%s\" found.\n",
  1186. kfdt_name);
  1187. continue;
  1188. }
  1189. /*
  1190. * Get a pointer to this configuration's fdt.
  1191. */
  1192. if (fit_image_get_data(fit, kfdt_noffset, &kfdt, &size)) {
  1193. debug("Failed to get fdt \"%s\".\n", kfdt_name);
  1194. continue;
  1195. }
  1196. len = fdt_compat_len;
  1197. cur_fdt_compat = fdt_compat;
  1198. /*
  1199. * Look for a match for each U-Boot compatibility string in
  1200. * turn in this configuration's fdt.
  1201. */
  1202. for (i = 0; len > 0 &&
  1203. (!best_match_offset || best_match_pos > i); i++) {
  1204. int cur_len = strlen(cur_fdt_compat) + 1;
  1205. if (!fdt_node_check_compatible(kfdt, 0,
  1206. cur_fdt_compat)) {
  1207. best_match_offset = noffset;
  1208. best_match_pos = i;
  1209. break;
  1210. }
  1211. len -= cur_len;
  1212. cur_fdt_compat += cur_len;
  1213. }
  1214. }
  1215. if (!best_match_offset) {
  1216. debug("No match found.\n");
  1217. return -1;
  1218. }
  1219. return best_match_offset;
  1220. }
  1221. /**
  1222. * fit_conf_get_node - get node offset for configuration of a given unit name
  1223. * @fit: pointer to the FIT format image header
  1224. * @conf_uname: configuration node unit name
  1225. *
  1226. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  1227. * parant node) of a provided unit name. If configuration is found its node
  1228. * offset is returned to the caller.
  1229. *
  1230. * When NULL is provided in second argument fit_conf_get_node() will search
  1231. * for a default configuration node instead. Default configuration node unit
  1232. * name is retrieved from FIT_DEFAULT_PROP property of the '/configurations'
  1233. * node.
  1234. *
  1235. * returns:
  1236. * configuration node offset when found (>=0)
  1237. * negative number on failure (FDT_ERR_* code)
  1238. */
  1239. int fit_conf_get_node(const void *fit, const char *conf_uname)
  1240. {
  1241. int noffset, confs_noffset;
  1242. int len;
  1243. confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
  1244. if (confs_noffset < 0) {
  1245. debug("Can't find configurations parent node '%s' (%s)\n",
  1246. FIT_CONFS_PATH, fdt_strerror(confs_noffset));
  1247. return confs_noffset;
  1248. }
  1249. if (conf_uname == NULL) {
  1250. /* get configuration unit name from the default property */
  1251. debug("No configuration specified, trying default...\n");
  1252. conf_uname = (char *)fdt_getprop(fit, confs_noffset,
  1253. FIT_DEFAULT_PROP, &len);
  1254. if (conf_uname == NULL) {
  1255. fit_get_debug(fit, confs_noffset, FIT_DEFAULT_PROP,
  1256. len);
  1257. return len;
  1258. }
  1259. debug("Found default configuration: '%s'\n", conf_uname);
  1260. }
  1261. noffset = fdt_subnode_offset(fit, confs_noffset, conf_uname);
  1262. if (noffset < 0) {
  1263. debug("Can't get node offset for configuration unit name: '%s' (%s)\n",
  1264. conf_uname, fdt_strerror(noffset));
  1265. }
  1266. return noffset;
  1267. }
  1268. int fit_conf_get_prop_node(const void *fit, int noffset,
  1269. const char *prop_name)
  1270. {
  1271. char *uname;
  1272. int len;
  1273. /* get kernel image unit name from configuration kernel property */
  1274. uname = (char *)fdt_getprop(fit, noffset, prop_name, &len);
  1275. if (uname == NULL)
  1276. return len;
  1277. return fit_image_get_node(fit, uname);
  1278. }
  1279. /**
  1280. * fit_conf_print - prints out the FIT configuration details
  1281. * @fit: pointer to the FIT format image header
  1282. * @noffset: offset of the configuration node
  1283. * @p: pointer to prefix string
  1284. *
  1285. * fit_conf_print() lists all mandatory properies for the processed
  1286. * configuration node.
  1287. *
  1288. * returns:
  1289. * no returned results
  1290. */
  1291. void fit_conf_print(const void *fit, int noffset, const char *p)
  1292. {
  1293. char *desc;
  1294. char *uname;
  1295. int ret;
  1296. /* Mandatory properties */
  1297. ret = fit_get_desc(fit, noffset, &desc);
  1298. printf("%s Description: ", p);
  1299. if (ret)
  1300. printf("unavailable\n");
  1301. else
  1302. printf("%s\n", desc);
  1303. uname = (char *)fdt_getprop(fit, noffset, FIT_KERNEL_PROP, NULL);
  1304. printf("%s Kernel: ", p);
  1305. if (uname == NULL)
  1306. printf("unavailable\n");
  1307. else
  1308. printf("%s\n", uname);
  1309. /* Optional properties */
  1310. uname = (char *)fdt_getprop(fit, noffset, FIT_RAMDISK_PROP, NULL);
  1311. if (uname)
  1312. printf("%s Init Ramdisk: %s\n", p, uname);
  1313. uname = (char *)fdt_getprop(fit, noffset, FIT_FDT_PROP, NULL);
  1314. if (uname)
  1315. printf("%s FDT: %s\n", p, uname);
  1316. }
  1317. static int fit_image_select(const void *fit, int rd_noffset, int verify)
  1318. {
  1319. fit_image_print(fit, rd_noffset, " ");
  1320. if (verify) {
  1321. puts(" Verifying Hash Integrity ... ");
  1322. if (!fit_image_verify(fit, rd_noffset)) {
  1323. puts("Bad Data Hash\n");
  1324. return -EACCES;
  1325. }
  1326. puts("OK\n");
  1327. }
  1328. return 0;
  1329. }
  1330. int fit_get_node_from_config(bootm_headers_t *images, const char *prop_name,
  1331. ulong addr)
  1332. {
  1333. int cfg_noffset;
  1334. void *fit_hdr;
  1335. int noffset;
  1336. debug("* %s: using config '%s' from image at 0x%08lx\n",
  1337. prop_name, images->fit_uname_cfg, addr);
  1338. /* Check whether configuration has this property defined */
  1339. fit_hdr = map_sysmem(addr, 0);
  1340. cfg_noffset = fit_conf_get_node(fit_hdr, images->fit_uname_cfg);
  1341. if (cfg_noffset < 0) {
  1342. debug("* %s: no such config\n", prop_name);
  1343. return -ENOENT;
  1344. }
  1345. noffset = fit_conf_get_prop_node(fit_hdr, cfg_noffset, prop_name);
  1346. if (noffset < 0) {
  1347. debug("* %s: no '%s' in config\n", prop_name, prop_name);
  1348. return -ENOLINK;
  1349. }
  1350. return noffset;
  1351. }
  1352. /**
  1353. * fit_get_image_type_property() - get property name for IH_TYPE_...
  1354. *
  1355. * @return the properly name where we expect to find the image in the
  1356. * config node
  1357. */
  1358. static const char *fit_get_image_type_property(int type)
  1359. {
  1360. /*
  1361. * This is sort-of available in the uimage_type[] table in image.c
  1362. * but we don't have access to the sohrt name, and "fdt" is different
  1363. * anyway. So let's just keep it here.
  1364. */
  1365. switch (type) {
  1366. case IH_TYPE_FLATDT:
  1367. return FIT_FDT_PROP;
  1368. case IH_TYPE_KERNEL:
  1369. return FIT_KERNEL_PROP;
  1370. case IH_TYPE_RAMDISK:
  1371. return FIT_RAMDISK_PROP;
  1372. case IH_TYPE_X86_SETUP:
  1373. return FIT_SETUP_PROP;
  1374. }
  1375. return "unknown";
  1376. }
  1377. int fit_image_load(bootm_headers_t *images, ulong addr,
  1378. const char **fit_unamep, const char **fit_uname_configp,
  1379. int arch, int image_type, int bootstage_id,
  1380. enum fit_load_op load_op, ulong *datap, ulong *lenp)
  1381. {
  1382. int cfg_noffset, noffset;
  1383. const char *fit_uname;
  1384. const char *fit_uname_config;
  1385. const void *fit;
  1386. const void *buf;
  1387. size_t size;
  1388. int type_ok, os_ok;
  1389. ulong load, data, len;
  1390. uint8_t os;
  1391. const char *prop_name;
  1392. int ret;
  1393. fit = map_sysmem(addr, 0);
  1394. fit_uname = fit_unamep ? *fit_unamep : NULL;
  1395. fit_uname_config = fit_uname_configp ? *fit_uname_configp : NULL;
  1396. prop_name = fit_get_image_type_property(image_type);
  1397. printf("## Loading %s from FIT Image at %08lx ...\n", prop_name, addr);
  1398. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1399. if (!fit_check_format(fit)) {
  1400. printf("Bad FIT %s image format!\n", prop_name);
  1401. bootstage_error(bootstage_id + BOOTSTAGE_SUB_FORMAT);
  1402. return -ENOEXEC;
  1403. }
  1404. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_FORMAT_OK);
  1405. if (fit_uname) {
  1406. /* get FIT component image node offset */
  1407. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_UNIT_NAME);
  1408. noffset = fit_image_get_node(fit, fit_uname);
  1409. } else {
  1410. /*
  1411. * no image node unit name, try to get config
  1412. * node first. If config unit node name is NULL
  1413. * fit_conf_get_node() will try to find default config node
  1414. */
  1415. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_NO_UNIT_NAME);
  1416. if (IMAGE_ENABLE_BEST_MATCH && !fit_uname_config) {
  1417. cfg_noffset = fit_conf_find_compat(fit, gd_fdt_blob());
  1418. } else {
  1419. cfg_noffset = fit_conf_get_node(fit,
  1420. fit_uname_config);
  1421. }
  1422. if (cfg_noffset < 0) {
  1423. puts("Could not find configuration node\n");
  1424. bootstage_error(bootstage_id +
  1425. BOOTSTAGE_SUB_NO_UNIT_NAME);
  1426. return -ENOENT;
  1427. }
  1428. fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
  1429. printf(" Using '%s' configuration\n", fit_uname_config);
  1430. if (image_type == IH_TYPE_KERNEL) {
  1431. /* Remember (and possibly verify) this config */
  1432. images->fit_uname_cfg = fit_uname_config;
  1433. if (IMAGE_ENABLE_VERIFY && images->verify) {
  1434. puts(" Verifying Hash Integrity ... ");
  1435. if (fit_config_verify(fit, cfg_noffset)) {
  1436. puts("Bad Data Hash\n");
  1437. bootstage_error(bootstage_id +
  1438. BOOTSTAGE_SUB_HASH);
  1439. return -EACCES;
  1440. }
  1441. puts("OK\n");
  1442. }
  1443. bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
  1444. }
  1445. noffset = fit_conf_get_prop_node(fit, cfg_noffset,
  1446. prop_name);
  1447. fit_uname = fit_get_name(fit, noffset, NULL);
  1448. }
  1449. if (noffset < 0) {
  1450. puts("Could not find subimage node\n");
  1451. bootstage_error(bootstage_id + BOOTSTAGE_SUB_SUBNODE);
  1452. return -ENOENT;
  1453. }
  1454. printf(" Trying '%s' %s subimage\n", fit_uname, prop_name);
  1455. ret = fit_image_select(fit, noffset, images->verify);
  1456. if (ret) {
  1457. bootstage_error(bootstage_id + BOOTSTAGE_SUB_HASH);
  1458. return ret;
  1459. }
  1460. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1461. #if !defined(USE_HOSTCC) && !defined(CONFIG_SANDBOX)
  1462. if (!fit_image_check_target_arch(fit, noffset)) {
  1463. puts("Unsupported Architecture\n");
  1464. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ARCH);
  1465. return -ENOEXEC;
  1466. }
  1467. #endif
  1468. if (image_type == IH_TYPE_FLATDT &&
  1469. !fit_image_check_comp(fit, noffset, IH_COMP_NONE)) {
  1470. puts("FDT image is compressed");
  1471. return -EPROTONOSUPPORT;
  1472. }
  1473. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1474. type_ok = fit_image_check_type(fit, noffset, image_type) ||
  1475. (image_type == IH_TYPE_KERNEL &&
  1476. fit_image_check_type(fit, noffset,
  1477. IH_TYPE_KERNEL_NOLOAD));
  1478. os_ok = image_type == IH_TYPE_FLATDT ||
  1479. fit_image_check_os(fit, noffset, IH_OS_LINUX) ||
  1480. fit_image_check_os(fit, noffset, IH_OS_OPENRTOS);
  1481. if (!type_ok || !os_ok) {
  1482. fit_image_get_os(fit, noffset, &os);
  1483. printf("No %s %s %s Image\n",
  1484. genimg_get_os_name(os),
  1485. genimg_get_arch_name(arch),
  1486. genimg_get_type_name(image_type));
  1487. bootstage_error(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL);
  1488. return -EIO;
  1489. }
  1490. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_CHECK_ALL_OK);
  1491. /* get image data address and length */
  1492. if (fit_image_get_data(fit, noffset, &buf, &size)) {
  1493. printf("Could not find %s subimage data!\n", prop_name);
  1494. bootstage_error(bootstage_id + BOOTSTAGE_SUB_GET_DATA);
  1495. return -ENOENT;
  1496. }
  1497. len = (ulong)size;
  1498. /* verify that image data is a proper FDT blob */
  1499. if (image_type == IH_TYPE_FLATDT && fdt_check_header(buf)) {
  1500. puts("Subimage data is not a FDT");
  1501. return -ENOEXEC;
  1502. }
  1503. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_GET_DATA_OK);
  1504. /*
  1505. * Work-around for eldk-4.2 which gives this warning if we try to
  1506. * cast in the unmap_sysmem() call:
  1507. * warning: initialization discards qualifiers from pointer target type
  1508. */
  1509. {
  1510. void *vbuf = (void *)buf;
  1511. data = map_to_sysmem(vbuf);
  1512. }
  1513. if (load_op == FIT_LOAD_IGNORED) {
  1514. /* Don't load */
  1515. } else if (fit_image_get_load(fit, noffset, &load)) {
  1516. if (load_op == FIT_LOAD_REQUIRED) {
  1517. printf("Can't get %s subimage load address!\n",
  1518. prop_name);
  1519. bootstage_error(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1520. return -EBADF;
  1521. }
  1522. } else if (load_op != FIT_LOAD_OPTIONAL_NON_ZERO || load) {
  1523. ulong image_start, image_end;
  1524. ulong load_end;
  1525. void *dst;
  1526. /*
  1527. * move image data to the load address,
  1528. * make sure we don't overwrite initial image
  1529. */
  1530. image_start = addr;
  1531. image_end = addr + fit_get_size(fit);
  1532. load_end = load + len;
  1533. if (image_type != IH_TYPE_KERNEL &&
  1534. load < image_end && load_end > image_start) {
  1535. printf("Error: %s overwritten\n", prop_name);
  1536. return -EXDEV;
  1537. }
  1538. printf(" Loading %s from 0x%08lx to 0x%08lx\n",
  1539. prop_name, data, load);
  1540. dst = map_sysmem(load, len);
  1541. memmove(dst, buf, len);
  1542. data = load;
  1543. }
  1544. bootstage_mark(bootstage_id + BOOTSTAGE_SUB_LOAD);
  1545. *datap = data;
  1546. *lenp = len;
  1547. if (fit_unamep)
  1548. *fit_unamep = (char *)fit_uname;
  1549. if (fit_uname_configp)
  1550. *fit_uname_configp = (char *)fit_uname_config;
  1551. return noffset;
  1552. }
  1553. int boot_get_setup_fit(bootm_headers_t *images, uint8_t arch,
  1554. ulong *setup_start, ulong *setup_len)
  1555. {
  1556. int noffset;
  1557. ulong addr;
  1558. ulong len;
  1559. int ret;
  1560. addr = map_to_sysmem(images->fit_hdr_os);
  1561. noffset = fit_get_node_from_config(images, FIT_SETUP_PROP, addr);
  1562. if (noffset < 0)
  1563. return noffset;
  1564. ret = fit_image_load(images, addr, NULL, NULL, arch,
  1565. IH_TYPE_X86_SETUP, BOOTSTAGE_ID_FIT_SETUP_START,
  1566. FIT_LOAD_REQUIRED, setup_start, &len);
  1567. return ret;
  1568. }