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