image-fit.c 40 KB

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