image.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #ifndef USE_HOSTCC
  26. #include <common.h>
  27. #include <watchdog.h>
  28. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  29. #include <status_led.h>
  30. #endif
  31. #ifdef CONFIG_HAS_DATAFLASH
  32. #include <dataflash.h>
  33. #endif
  34. #ifdef CONFIG_LOGBUFFER
  35. #include <logbuff.h>
  36. #endif
  37. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
  38. #include <rtc.h>
  39. #endif
  40. #include <image.h>
  41. #if defined(CONFIG_FIT) || defined (CONFIG_OF_LIBFDT)
  42. #include <fdt.h>
  43. #include <libfdt.h>
  44. #include <fdt_support.h>
  45. #endif
  46. #if defined(CONFIG_FIT)
  47. #include <u-boot/md5.h>
  48. #include <sha1.h>
  49. static int fit_check_ramdisk (const void *fit, int os_noffset,
  50. uint8_t arch, int verify);
  51. #endif
  52. #ifdef CONFIG_CMD_BDI
  53. extern int do_bdinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  54. #endif
  55. DECLARE_GLOBAL_DATA_PTR;
  56. static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
  57. int verify);
  58. #else
  59. #include "mkimage.h"
  60. #include <u-boot/md5.h>
  61. #include <time.h>
  62. #include <image.h>
  63. #endif /* !USE_HOSTCC*/
  64. typedef struct table_entry {
  65. int id; /* as defined in image.h */
  66. char *sname; /* short (input) name */
  67. char *lname; /* long (output) name */
  68. } table_entry_t;
  69. static table_entry_t uimage_arch[] = {
  70. { IH_ARCH_INVALID, NULL, "Invalid ARCH", },
  71. { IH_ARCH_ALPHA, "alpha", "Alpha", },
  72. { IH_ARCH_ARM, "arm", "ARM", },
  73. { IH_ARCH_I386, "x86", "Intel x86", },
  74. { IH_ARCH_IA64, "ia64", "IA64", },
  75. { IH_ARCH_M68K, "m68k", "M68K", },
  76. { IH_ARCH_MICROBLAZE, "microblaze", "MicroBlaze", },
  77. { IH_ARCH_MIPS, "mips", "MIPS", },
  78. { IH_ARCH_MIPS64, "mips64", "MIPS 64 Bit", },
  79. { IH_ARCH_NIOS, "nios", "NIOS", },
  80. { IH_ARCH_NIOS2, "nios2", "NIOS II", },
  81. { IH_ARCH_PPC, "ppc", "PowerPC", },
  82. { IH_ARCH_S390, "s390", "IBM S390", },
  83. { IH_ARCH_SH, "sh", "SuperH", },
  84. { IH_ARCH_SPARC, "sparc", "SPARC", },
  85. { IH_ARCH_SPARC64, "sparc64", "SPARC 64 Bit", },
  86. { IH_ARCH_BLACKFIN, "blackfin", "Blackfin", },
  87. { IH_ARCH_AVR32, "avr32", "AVR32", },
  88. { -1, "", "", },
  89. };
  90. static table_entry_t uimage_os[] = {
  91. { IH_OS_INVALID, NULL, "Invalid OS", },
  92. #if defined(CONFIG_ARTOS) || defined(USE_HOSTCC)
  93. { IH_OS_ARTOS, "artos", "ARTOS", },
  94. #endif
  95. { IH_OS_LINUX, "linux", "Linux", },
  96. #if defined(CONFIG_LYNXKDI) || defined(USE_HOSTCC)
  97. { IH_OS_LYNXOS, "lynxos", "LynxOS", },
  98. #endif
  99. { IH_OS_NETBSD, "netbsd", "NetBSD", },
  100. { IH_OS_RTEMS, "rtems", "RTEMS", },
  101. { IH_OS_U_BOOT, "u-boot", "U-Boot", },
  102. #if defined(CONFIG_CMD_ELF) || defined(USE_HOSTCC)
  103. { IH_OS_QNX, "qnx", "QNX", },
  104. { IH_OS_VXWORKS, "vxworks", "VxWorks", },
  105. #endif
  106. #ifdef USE_HOSTCC
  107. { IH_OS_4_4BSD, "4_4bsd", "4_4BSD", },
  108. { IH_OS_DELL, "dell", "Dell", },
  109. { IH_OS_ESIX, "esix", "Esix", },
  110. { IH_OS_FREEBSD, "freebsd", "FreeBSD", },
  111. { IH_OS_IRIX, "irix", "Irix", },
  112. { IH_OS_NCR, "ncr", "NCR", },
  113. { IH_OS_OPENBSD, "openbsd", "OpenBSD", },
  114. { IH_OS_PSOS, "psos", "pSOS", },
  115. { IH_OS_SCO, "sco", "SCO", },
  116. { IH_OS_SOLARIS, "solaris", "Solaris", },
  117. { IH_OS_SVR4, "svr4", "SVR4", },
  118. #endif
  119. { -1, "", "", },
  120. };
  121. static table_entry_t uimage_type[] = {
  122. { IH_TYPE_INVALID, NULL, "Invalid Image", },
  123. { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", },
  124. { IH_TYPE_FIRMWARE, "firmware", "Firmware", },
  125. { IH_TYPE_KERNEL, "kernel", "Kernel Image", },
  126. { IH_TYPE_MULTI, "multi", "Multi-File Image", },
  127. { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", },
  128. { IH_TYPE_SCRIPT, "script", "Script", },
  129. { IH_TYPE_STANDALONE, "standalone", "Standalone Program", },
  130. { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", },
  131. { -1, "", "", },
  132. };
  133. static table_entry_t uimage_comp[] = {
  134. { IH_COMP_NONE, "none", "uncompressed", },
  135. { IH_COMP_BZIP2, "bzip2", "bzip2 compressed", },
  136. { IH_COMP_GZIP, "gzip", "gzip compressed", },
  137. { -1, "", "", },
  138. };
  139. uint32_t crc32 (uint32_t, const unsigned char *, uint);
  140. static void genimg_print_size (uint32_t size);
  141. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  142. static void genimg_print_time (time_t timestamp);
  143. #endif
  144. /*****************************************************************************/
  145. /* Legacy format routines */
  146. /*****************************************************************************/
  147. int image_check_hcrc (image_header_t *hdr)
  148. {
  149. ulong hcrc;
  150. ulong len = image_get_header_size ();
  151. image_header_t header;
  152. /* Copy header so we can blank CRC field for re-calculation */
  153. memmove (&header, (char *)hdr, image_get_header_size ());
  154. image_set_hcrc (&header, 0);
  155. hcrc = crc32 (0, (unsigned char *)&header, len);
  156. return (hcrc == image_get_hcrc (hdr));
  157. }
  158. int image_check_dcrc (image_header_t *hdr)
  159. {
  160. ulong data = image_get_data (hdr);
  161. ulong len = image_get_data_size (hdr);
  162. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  163. return (dcrc == image_get_dcrc (hdr));
  164. }
  165. #ifndef USE_HOSTCC
  166. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  167. {
  168. ulong dcrc = 0;
  169. ulong len = image_get_data_size (hdr);
  170. ulong data = image_get_data (hdr);
  171. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  172. ulong cdata = data;
  173. ulong edata = cdata + len;
  174. while (cdata < edata) {
  175. ulong chunk = edata - cdata;
  176. if (chunk > chunksz)
  177. chunk = chunksz;
  178. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  179. cdata += chunk;
  180. WATCHDOG_RESET ();
  181. }
  182. #else
  183. dcrc = crc32 (0, (unsigned char *)data, len);
  184. #endif
  185. return (dcrc == image_get_dcrc (hdr));
  186. }
  187. #endif /* !USE_HOSTCC */
  188. /**
  189. * image_multi_count - get component (sub-image) count
  190. * @hdr: pointer to the header of the multi component image
  191. *
  192. * image_multi_count() returns number of components in a multi
  193. * component image.
  194. *
  195. * Note: no checking of the image type is done, caller must pass
  196. * a valid multi component image.
  197. *
  198. * returns:
  199. * number of components
  200. */
  201. ulong image_multi_count (image_header_t *hdr)
  202. {
  203. ulong i, count = 0;
  204. uint32_t *size;
  205. /* get start of the image payload, which in case of multi
  206. * component images that points to a table of component sizes */
  207. size = (uint32_t *)image_get_data (hdr);
  208. /* count non empty slots */
  209. for (i = 0; size[i]; ++i)
  210. count++;
  211. return count;
  212. }
  213. /**
  214. * image_multi_getimg - get component data address and size
  215. * @hdr: pointer to the header of the multi component image
  216. * @idx: index of the requested component
  217. * @data: pointer to a ulong variable, will hold component data address
  218. * @len: pointer to a ulong variable, will hold component size
  219. *
  220. * image_multi_getimg() returns size and data address for the requested
  221. * component in a multi component image.
  222. *
  223. * Note: no checking of the image type is done, caller must pass
  224. * a valid multi component image.
  225. *
  226. * returns:
  227. * data address and size of the component, if idx is valid
  228. * 0 in data and len, if idx is out of range
  229. */
  230. void image_multi_getimg (image_header_t *hdr, ulong idx,
  231. ulong *data, ulong *len)
  232. {
  233. int i;
  234. uint32_t *size;
  235. ulong offset, tail, count, img_data;
  236. /* get number of component */
  237. count = image_multi_count (hdr);
  238. /* get start of the image payload, which in case of multi
  239. * component images that points to a table of component sizes */
  240. size = (uint32_t *)image_get_data (hdr);
  241. /* get address of the proper component data start, which means
  242. * skipping sizes table (add 1 for last, null entry) */
  243. img_data = image_get_data (hdr) + (count + 1) * sizeof (uint32_t);
  244. if (idx < count) {
  245. *len = uimage_to_cpu (size[idx]);
  246. offset = 0;
  247. tail = 0;
  248. /* go over all indices preceding requested component idx */
  249. for (i = 0; i < idx; i++) {
  250. /* add up i-th component size */
  251. offset += uimage_to_cpu (size[i]);
  252. /* add up alignment for i-th component */
  253. tail += (4 - uimage_to_cpu (size[i]) % 4);
  254. }
  255. /* calculate idx-th component data address */
  256. *data = img_data + offset + tail;
  257. } else {
  258. *len = 0;
  259. *data = 0;
  260. }
  261. }
  262. static void image_print_type (image_header_t *hdr)
  263. {
  264. const char *os, *arch, *type, *comp;
  265. os = genimg_get_os_name (image_get_os (hdr));
  266. arch = genimg_get_arch_name (image_get_arch (hdr));
  267. type = genimg_get_type_name (image_get_type (hdr));
  268. comp = genimg_get_comp_name (image_get_comp (hdr));
  269. printf ("%s %s %s (%s)\n", arch, os, type, comp);
  270. }
  271. /**
  272. * image_print_contents - prints out the contents of the legacy format image
  273. * @hdr: pointer to the legacy format image header
  274. * @p: pointer to prefix string
  275. *
  276. * image_print_contents() formats a multi line legacy image contents description.
  277. * The routine prints out all header fields followed by the size/offset data
  278. * for MULTI/SCRIPT images.
  279. *
  280. * returns:
  281. * no returned results
  282. */
  283. void image_print_contents (image_header_t *hdr)
  284. {
  285. const char *p;
  286. #ifdef USE_HOSTCC
  287. p = "";
  288. #else
  289. p = " ";
  290. #endif
  291. printf ("%sImage Name: %.*s\n", p, IH_NMLEN, image_get_name (hdr));
  292. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  293. printf ("%sCreated: ", p);
  294. genimg_print_time ((time_t)image_get_time (hdr));
  295. #endif
  296. printf ("%sImage Type: ", p);
  297. image_print_type (hdr);
  298. printf ("%sData Size: ", p);
  299. genimg_print_size (image_get_data_size (hdr));
  300. printf ("%sLoad Address: %08x\n", p, image_get_load (hdr));
  301. printf ("%sEntry Point: %08x\n", p, image_get_ep (hdr));
  302. if (image_check_type (hdr, IH_TYPE_MULTI) ||
  303. image_check_type (hdr, IH_TYPE_SCRIPT)) {
  304. int i;
  305. ulong data, len;
  306. ulong count = image_multi_count (hdr);
  307. printf ("%sContents:\n", p);
  308. for (i = 0; i < count; i++) {
  309. image_multi_getimg (hdr, i, &data, &len);
  310. printf ("%s Image %d: ", p, i);
  311. genimg_print_size (len);
  312. if (image_check_type (hdr, IH_TYPE_SCRIPT) && i > 0) {
  313. /*
  314. * the user may need to know offsets
  315. * if planning to do something with
  316. * multiple files
  317. */
  318. printf ("%s Offset = 0x%08lx\n", p, data);
  319. }
  320. }
  321. }
  322. }
  323. #ifndef USE_HOSTCC
  324. /**
  325. * image_get_ramdisk - get and verify ramdisk image
  326. * @rd_addr: ramdisk image start address
  327. * @arch: expected ramdisk architecture
  328. * @verify: checksum verification flag
  329. *
  330. * image_get_ramdisk() returns a pointer to the verified ramdisk image
  331. * header. Routine receives image start address and expected architecture
  332. * flag. Verification done covers data and header integrity and os/type/arch
  333. * fields checking.
  334. *
  335. * If dataflash support is enabled routine checks for dataflash addresses
  336. * and handles required dataflash reads.
  337. *
  338. * returns:
  339. * pointer to a ramdisk image header, if image was found and valid
  340. * otherwise, return NULL
  341. */
  342. static image_header_t* image_get_ramdisk (ulong rd_addr, uint8_t arch,
  343. int verify)
  344. {
  345. image_header_t *rd_hdr = (image_header_t *)rd_addr;
  346. if (!image_check_magic (rd_hdr)) {
  347. puts ("Bad Magic Number\n");
  348. show_boot_progress (-10);
  349. return NULL;
  350. }
  351. if (!image_check_hcrc (rd_hdr)) {
  352. puts ("Bad Header Checksum\n");
  353. show_boot_progress (-11);
  354. return NULL;
  355. }
  356. show_boot_progress (10);
  357. image_print_contents (rd_hdr);
  358. if (verify) {
  359. puts(" Verifying Checksum ... ");
  360. if (!image_check_dcrc_wd (rd_hdr, CHUNKSZ)) {
  361. puts ("Bad Data CRC\n");
  362. show_boot_progress (-12);
  363. return NULL;
  364. }
  365. puts("OK\n");
  366. }
  367. show_boot_progress (11);
  368. if (!image_check_os (rd_hdr, IH_OS_LINUX) ||
  369. !image_check_arch (rd_hdr, arch) ||
  370. !image_check_type (rd_hdr, IH_TYPE_RAMDISK)) {
  371. printf ("No Linux %s Ramdisk Image\n",
  372. genimg_get_arch_name(arch));
  373. show_boot_progress (-13);
  374. return NULL;
  375. }
  376. return rd_hdr;
  377. }
  378. #endif /* !USE_HOSTCC */
  379. /*****************************************************************************/
  380. /* Shared dual-format routines */
  381. /*****************************************************************************/
  382. #ifndef USE_HOSTCC
  383. int getenv_yesno (char *var)
  384. {
  385. char *s = getenv (var);
  386. return (s && (*s == 'n')) ? 0 : 1;
  387. }
  388. ulong getenv_bootm_low(void)
  389. {
  390. char *s = getenv ("bootm_low");
  391. if (s) {
  392. ulong tmp = simple_strtoul (s, NULL, 16);
  393. return tmp;
  394. }
  395. #if defined(CFG_SDRAM_BASE)
  396. return CFG_SDRAM_BASE;
  397. #elif defined(CONFIG_ARM)
  398. return gd->bd->bi_dram[0].start;
  399. #else
  400. return 0;
  401. #endif
  402. }
  403. ulong getenv_bootm_size(void)
  404. {
  405. char *s = getenv ("bootm_size");
  406. if (s) {
  407. ulong tmp = simple_strtoul (s, NULL, 16);
  408. return tmp;
  409. }
  410. #if defined(CONFIG_ARM)
  411. return gd->bd->bi_dram[0].size;
  412. #else
  413. return gd->bd->bi_memsize;
  414. #endif
  415. }
  416. void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
  417. {
  418. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  419. while (len > 0) {
  420. size_t tail = (len > chunksz) ? chunksz : len;
  421. WATCHDOG_RESET ();
  422. memmove (to, from, tail);
  423. to += tail;
  424. from += tail;
  425. len -= tail;
  426. }
  427. #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
  428. memmove (to, from, len);
  429. #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
  430. }
  431. #endif /* !USE_HOSTCC */
  432. static void genimg_print_size (uint32_t size)
  433. {
  434. #ifndef USE_HOSTCC
  435. printf ("%d Bytes = ", size);
  436. print_size (size, "\n");
  437. #else
  438. printf ("%d Bytes = %.2f kB = %.2f MB\n",
  439. size, (double)size / 1.024e3,
  440. (double)size / 1.048576e6);
  441. #endif
  442. }
  443. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  444. static void genimg_print_time (time_t timestamp)
  445. {
  446. #ifndef USE_HOSTCC
  447. struct rtc_time tm;
  448. to_tm (timestamp, &tm);
  449. printf ("%4d-%02d-%02d %2d:%02d:%02d UTC\n",
  450. tm.tm_year, tm.tm_mon, tm.tm_mday,
  451. tm.tm_hour, tm.tm_min, tm.tm_sec);
  452. #else
  453. printf ("%s", ctime(&timestamp));
  454. #endif
  455. }
  456. #endif /* CONFIG_TIMESTAMP || CONFIG_CMD_DATE || USE_HOSTCC */
  457. /**
  458. * get_table_entry_name - translate entry id to long name
  459. * @table: pointer to a translation table for entries of a specific type
  460. * @msg: message to be returned when translation fails
  461. * @id: entry id to be translated
  462. *
  463. * get_table_entry_name() will go over translation table trying to find
  464. * entry that matches given id. If matching entry is found, its long
  465. * name is returned to the caller.
  466. *
  467. * returns:
  468. * long entry name if translation succeeds
  469. * msg otherwise
  470. */
  471. static char *get_table_entry_name (table_entry_t *table, char *msg, int id)
  472. {
  473. for (; table->id >= 0; ++table) {
  474. if (table->id == id)
  475. return (table->lname);
  476. }
  477. return (msg);
  478. }
  479. const char *genimg_get_os_name (uint8_t os)
  480. {
  481. return (get_table_entry_name (uimage_os, "Unknown OS", os));
  482. }
  483. const char *genimg_get_arch_name (uint8_t arch)
  484. {
  485. return (get_table_entry_name (uimage_arch, "Unknown Architecture", arch));
  486. }
  487. const char *genimg_get_type_name (uint8_t type)
  488. {
  489. return (get_table_entry_name (uimage_type, "Unknown Image", type));
  490. }
  491. const char *genimg_get_comp_name (uint8_t comp)
  492. {
  493. return (get_table_entry_name (uimage_comp, "Unknown Compression", comp));
  494. }
  495. /**
  496. * get_table_entry_id - translate short entry name to id
  497. * @table: pointer to a translation table for entries of a specific type
  498. * @table_name: to be used in case of error
  499. * @name: entry short name to be translated
  500. *
  501. * get_table_entry_id() will go over translation table trying to find
  502. * entry that matches given short name. If matching entry is found,
  503. * its id returned to the caller.
  504. *
  505. * returns:
  506. * entry id if translation succeeds
  507. * -1 otherwise
  508. */
  509. static int get_table_entry_id (table_entry_t *table,
  510. const char *table_name, const char *name)
  511. {
  512. table_entry_t *t;
  513. #ifdef USE_HOSTCC
  514. int first = 1;
  515. for (t = table; t->id >= 0; ++t) {
  516. if (t->sname && strcasecmp(t->sname, name) == 0)
  517. return (t->id);
  518. }
  519. fprintf (stderr, "\nInvalid %s Type - valid names are", table_name);
  520. for (t = table; t->id >= 0; ++t) {
  521. if (t->sname == NULL)
  522. continue;
  523. fprintf (stderr, "%c %s", (first) ? ':' : ',', t->sname);
  524. first = 0;
  525. }
  526. fprintf (stderr, "\n");
  527. #else
  528. for (t = table; t->id >= 0; ++t) {
  529. if (t->sname && strcmp(t->sname, name) == 0)
  530. return (t->id);
  531. }
  532. debug ("Invalid %s Type: %s\n", table_name, name);
  533. #endif /* USE_HOSTCC */
  534. return (-1);
  535. }
  536. int genimg_get_os_id (const char *name)
  537. {
  538. return (get_table_entry_id (uimage_os, "OS", name));
  539. }
  540. int genimg_get_arch_id (const char *name)
  541. {
  542. return (get_table_entry_id (uimage_arch, "CPU", name));
  543. }
  544. int genimg_get_type_id (const char *name)
  545. {
  546. return (get_table_entry_id (uimage_type, "Image", name));
  547. }
  548. int genimg_get_comp_id (const char *name)
  549. {
  550. return (get_table_entry_id (uimage_comp, "Compression", name));
  551. }
  552. #ifndef USE_HOSTCC
  553. /**
  554. * genimg_get_format - get image format type
  555. * @img_addr: image start address
  556. *
  557. * genimg_get_format() checks whether provided address points to a valid
  558. * legacy or FIT image.
  559. *
  560. * New uImage format and FDT blob are based on a libfdt. FDT blob
  561. * may be passed directly or embedded in a FIT image. In both situations
  562. * genimg_get_format() must be able to dectect libfdt header.
  563. *
  564. * returns:
  565. * image format type or IMAGE_FORMAT_INVALID if no image is present
  566. */
  567. int genimg_get_format (void *img_addr)
  568. {
  569. ulong format = IMAGE_FORMAT_INVALID;
  570. image_header_t *hdr;
  571. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  572. char *fit_hdr;
  573. #endif
  574. hdr = (image_header_t *)img_addr;
  575. if (image_check_magic(hdr))
  576. format = IMAGE_FORMAT_LEGACY;
  577. #if defined(CONFIG_FIT) || defined(CONFIG_OF_LIBFDT)
  578. else {
  579. fit_hdr = (char *)img_addr;
  580. if (fdt_check_header (fit_hdr) == 0)
  581. format = IMAGE_FORMAT_FIT;
  582. }
  583. #endif
  584. return format;
  585. }
  586. /**
  587. * genimg_get_image - get image from special storage (if necessary)
  588. * @img_addr: image start address
  589. *
  590. * genimg_get_image() checks if provided image start adddress is located
  591. * in a dataflash storage. If so, image is moved to a system RAM memory.
  592. *
  593. * returns:
  594. * image start address after possible relocation from special storage
  595. */
  596. ulong genimg_get_image (ulong img_addr)
  597. {
  598. ulong ram_addr = img_addr;
  599. #ifdef CONFIG_HAS_DATAFLASH
  600. ulong h_size, d_size;
  601. if (addr_dataflash (img_addr)){
  602. /* ger RAM address */
  603. ram_addr = CFG_LOAD_ADDR;
  604. /* get header size */
  605. h_size = image_get_header_size ();
  606. #if defined(CONFIG_FIT)
  607. if (sizeof(struct fdt_header) > h_size)
  608. h_size = sizeof(struct fdt_header);
  609. #endif
  610. /* read in header */
  611. debug (" Reading image header from dataflash address "
  612. "%08lx to RAM address %08lx\n", img_addr, ram_addr);
  613. read_dataflash (img_addr, h_size, (char *)ram_addr);
  614. /* get data size */
  615. switch (genimg_get_format ((void *)ram_addr)) {
  616. case IMAGE_FORMAT_LEGACY:
  617. d_size = image_get_data_size ((image_header_t *)ram_addr);
  618. debug (" Legacy format image found at 0x%08lx, size 0x%08lx\n",
  619. ram_addr, d_size);
  620. break;
  621. #if defined(CONFIG_FIT)
  622. case IMAGE_FORMAT_FIT:
  623. d_size = fit_get_size ((const void *)ram_addr) - h_size;
  624. debug (" FIT/FDT format image found at 0x%08lx, size 0x%08lx\n",
  625. ram_addr, d_size);
  626. break;
  627. #endif
  628. default:
  629. printf (" No valid image found at 0x%08lx\n", img_addr);
  630. return ram_addr;
  631. }
  632. /* read in image data */
  633. debug (" Reading image remaining data from dataflash address "
  634. "%08lx to RAM address %08lx\n", img_addr + h_size,
  635. ram_addr + h_size);
  636. read_dataflash (img_addr + h_size, d_size,
  637. (char *)(ram_addr + h_size));
  638. }
  639. #endif /* CONFIG_HAS_DATAFLASH */
  640. return ram_addr;
  641. }
  642. /**
  643. * fit_has_config - check if there is a valid FIT configuration
  644. * @images: pointer to the bootm command headers structure
  645. *
  646. * fit_has_config() checks if there is a FIT configuration in use
  647. * (if FTI support is present).
  648. *
  649. * returns:
  650. * 0, no FIT support or no configuration found
  651. * 1, configuration found
  652. */
  653. int genimg_has_config (bootm_headers_t *images)
  654. {
  655. #if defined(CONFIG_FIT)
  656. if (images->fit_uname_cfg)
  657. return 1;
  658. #endif
  659. return 0;
  660. }
  661. /**
  662. * boot_get_ramdisk - main ramdisk handling routine
  663. * @argc: command argument count
  664. * @argv: command argument list
  665. * @images: pointer to the bootm images structure
  666. * @arch: expected ramdisk architecture
  667. * @rd_start: pointer to a ulong variable, will hold ramdisk start address
  668. * @rd_end: pointer to a ulong variable, will hold ramdisk end
  669. *
  670. * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
  671. * Curently supported are the following ramdisk sources:
  672. * - multicomponent kernel/ramdisk image,
  673. * - commandline provided address of decicated ramdisk image.
  674. *
  675. * returns:
  676. * 0, if ramdisk image was found and valid, or skiped
  677. * rd_start and rd_end are set to ramdisk start/end addresses if
  678. * ramdisk image is found and valid
  679. *
  680. * 1, if ramdisk image is found but corrupted
  681. * rd_start and rd_end are set to 0 if no ramdisk exists
  682. */
  683. int boot_get_ramdisk (int argc, char *argv[], bootm_headers_t *images,
  684. uint8_t arch, ulong *rd_start, ulong *rd_end)
  685. {
  686. ulong rd_addr, rd_load;
  687. ulong rd_data, rd_len;
  688. image_header_t *rd_hdr;
  689. #if defined(CONFIG_FIT)
  690. void *fit_hdr;
  691. const char *fit_uname_config = NULL;
  692. const char *fit_uname_ramdisk = NULL;
  693. ulong default_addr;
  694. int rd_noffset;
  695. int cfg_noffset;
  696. const void *data;
  697. size_t size;
  698. #endif
  699. *rd_start = 0;
  700. *rd_end = 0;
  701. /*
  702. * Look for a '-' which indicates to ignore the
  703. * ramdisk argument
  704. */
  705. if ((argc >= 3) && (strcmp(argv[2], "-") == 0)) {
  706. debug ("## Skipping init Ramdisk\n");
  707. rd_len = rd_data = 0;
  708. } else if (argc >= 3 || genimg_has_config (images)) {
  709. #if defined(CONFIG_FIT)
  710. if (argc >= 3) {
  711. /*
  712. * If the init ramdisk comes from the FIT image and
  713. * the FIT image address is omitted in the command
  714. * line argument, try to use os FIT image address or
  715. * default load address.
  716. */
  717. if (images->fit_uname_os)
  718. default_addr = (ulong)images->fit_hdr_os;
  719. else
  720. default_addr = load_addr;
  721. if (fit_parse_conf (argv[2], default_addr,
  722. &rd_addr, &fit_uname_config)) {
  723. debug ("* ramdisk: config '%s' from image at 0x%08lx\n",
  724. fit_uname_config, rd_addr);
  725. } else if (fit_parse_subimage (argv[2], default_addr,
  726. &rd_addr, &fit_uname_ramdisk)) {
  727. debug ("* ramdisk: subimage '%s' from image at 0x%08lx\n",
  728. fit_uname_ramdisk, rd_addr);
  729. } else
  730. #endif
  731. {
  732. rd_addr = simple_strtoul(argv[2], NULL, 16);
  733. debug ("* ramdisk: cmdline image address = 0x%08lx\n",
  734. rd_addr);
  735. }
  736. #if defined(CONFIG_FIT)
  737. } else {
  738. /* use FIT configuration provided in first bootm
  739. * command argument
  740. */
  741. rd_addr = (ulong)images->fit_hdr_os;
  742. fit_uname_config = images->fit_uname_cfg;
  743. debug ("* ramdisk: using config '%s' from image at 0x%08lx\n",
  744. fit_uname_config, rd_addr);
  745. /*
  746. * Check whether configuration has ramdisk defined,
  747. * if not, don't try to use it, quit silently.
  748. */
  749. fit_hdr = (void *)rd_addr;
  750. cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
  751. if (cfg_noffset < 0) {
  752. debug ("* ramdisk: no such config\n");
  753. return 0;
  754. }
  755. rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
  756. if (rd_noffset < 0) {
  757. debug ("* ramdisk: no ramdisk in config\n");
  758. return 0;
  759. }
  760. }
  761. #endif
  762. /* copy from dataflash if needed */
  763. rd_addr = genimg_get_image (rd_addr);
  764. /*
  765. * Check if there is an initrd image at the
  766. * address provided in the second bootm argument
  767. * check image type, for FIT images get FIT node.
  768. */
  769. switch (genimg_get_format ((void *)rd_addr)) {
  770. case IMAGE_FORMAT_LEGACY:
  771. printf ("## Loading init Ramdisk from Legacy "
  772. "Image at %08lx ...\n", rd_addr);
  773. show_boot_progress (9);
  774. rd_hdr = image_get_ramdisk (rd_addr, arch,
  775. images->verify);
  776. if (rd_hdr == NULL)
  777. return 1;
  778. rd_data = image_get_data (rd_hdr);
  779. rd_len = image_get_data_size (rd_hdr);
  780. rd_load = image_get_load (rd_hdr);
  781. break;
  782. #if defined(CONFIG_FIT)
  783. case IMAGE_FORMAT_FIT:
  784. fit_hdr = (void *)rd_addr;
  785. printf ("## Loading init Ramdisk from FIT "
  786. "Image at %08lx ...\n", rd_addr);
  787. show_boot_progress (120);
  788. if (!fit_check_format (fit_hdr)) {
  789. puts ("Bad FIT ramdisk image format!\n");
  790. show_boot_progress (-120);
  791. return 0;
  792. }
  793. show_boot_progress (121);
  794. if (!fit_uname_ramdisk) {
  795. /*
  796. * no ramdisk image node unit name, try to get config
  797. * node first. If config unit node name is NULL
  798. * fit_conf_get_node() will try to find default config node
  799. */
  800. show_boot_progress (122);
  801. cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
  802. if (cfg_noffset < 0) {
  803. puts ("Could not find configuration node\n");
  804. show_boot_progress (-122);
  805. return 0;
  806. }
  807. fit_uname_config = fdt_get_name (fit_hdr, cfg_noffset, NULL);
  808. printf (" Using '%s' configuration\n", fit_uname_config);
  809. rd_noffset = fit_conf_get_ramdisk_node (fit_hdr, cfg_noffset);
  810. fit_uname_ramdisk = fit_get_name (fit_hdr, rd_noffset, NULL);
  811. } else {
  812. /* get ramdisk component image node offset */
  813. show_boot_progress (123);
  814. rd_noffset = fit_image_get_node (fit_hdr, fit_uname_ramdisk);
  815. }
  816. if (rd_noffset < 0) {
  817. puts ("Could not find subimage node\n");
  818. show_boot_progress (-124);
  819. return 0;
  820. }
  821. printf (" Trying '%s' ramdisk subimage\n", fit_uname_ramdisk);
  822. show_boot_progress (125);
  823. if (!fit_check_ramdisk (fit_hdr, rd_noffset, arch, images->verify))
  824. return 0;
  825. /* get ramdisk image data address and length */
  826. if (fit_image_get_data (fit_hdr, rd_noffset, &data, &size)) {
  827. puts ("Could not find ramdisk subimage data!\n");
  828. show_boot_progress (-127);
  829. return 0;
  830. }
  831. show_boot_progress (128);
  832. rd_data = (ulong)data;
  833. rd_len = size;
  834. if (fit_image_get_load (fit_hdr, rd_noffset, &rd_load)) {
  835. puts ("Can't get ramdisk subimage load address!\n");
  836. show_boot_progress (-129);
  837. return 0;
  838. }
  839. show_boot_progress (129);
  840. images->fit_hdr_rd = fit_hdr;
  841. images->fit_uname_rd = fit_uname_ramdisk;
  842. images->fit_noffset_rd = rd_noffset;
  843. break;
  844. #endif
  845. default:
  846. puts ("Wrong Ramdisk Image Format\n");
  847. rd_data = rd_len = rd_load = 0;
  848. }
  849. #if defined(CONFIG_B2) || defined(CONFIG_EVB4510) || defined(CONFIG_ARMADILLO)
  850. /*
  851. * We need to copy the ramdisk to SRAM to let Linux boot
  852. */
  853. if (rd_data) {
  854. memmove ((void *)rd_load, (uchar *)rd_data, rd_len);
  855. rd_data = rd_load;
  856. }
  857. #endif /* CONFIG_B2 || CONFIG_EVB4510 || CONFIG_ARMADILLO */
  858. } else if (images->legacy_hdr_valid &&
  859. image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
  860. /*
  861. * Now check if we have a legacy mult-component image,
  862. * get second entry data start address and len.
  863. */
  864. show_boot_progress (13);
  865. printf ("## Loading init Ramdisk from multi component "
  866. "Legacy Image at %08lx ...\n",
  867. (ulong)images->legacy_hdr_os);
  868. image_multi_getimg (images->legacy_hdr_os, 1, &rd_data, &rd_len);
  869. } else {
  870. /*
  871. * no initrd image
  872. */
  873. show_boot_progress (14);
  874. rd_len = rd_data = 0;
  875. }
  876. if (!rd_data) {
  877. debug ("## No init Ramdisk\n");
  878. } else {
  879. *rd_start = rd_data;
  880. *rd_end = rd_data + rd_len;
  881. }
  882. debug (" ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
  883. *rd_start, *rd_end);
  884. return 0;
  885. }
  886. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
  887. /**
  888. * boot_ramdisk_high - relocate init ramdisk
  889. * @lmb: pointer to lmb handle, will be used for memory mgmt
  890. * @rd_data: ramdisk data start address
  891. * @rd_len: ramdisk data length
  892. * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
  893. * start address (after possible relocation)
  894. * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
  895. * end address (after possible relocation)
  896. *
  897. * boot_ramdisk_high() takes a relocation hint from "initrd_high" environement
  898. * variable and if requested ramdisk data is moved to a specified location.
  899. *
  900. * Initrd_start and initrd_end are set to final (after relocation) ramdisk
  901. * start/end addresses if ramdisk image start and len were provided,
  902. * otherwise set initrd_start and initrd_end set to zeros.
  903. *
  904. * returns:
  905. * 0 - success
  906. * -1 - failure
  907. */
  908. int boot_ramdisk_high (struct lmb *lmb, ulong rd_data, ulong rd_len,
  909. ulong *initrd_start, ulong *initrd_end)
  910. {
  911. char *s;
  912. ulong initrd_high;
  913. int initrd_copy_to_ram = 1;
  914. if ((s = getenv ("initrd_high")) != NULL) {
  915. /* a value of "no" or a similar string will act like 0,
  916. * turning the "load high" feature off. This is intentional.
  917. */
  918. initrd_high = simple_strtoul (s, NULL, 16);
  919. if (initrd_high == ~0)
  920. initrd_copy_to_ram = 0;
  921. } else {
  922. /* not set, no restrictions to load high */
  923. initrd_high = ~0;
  924. }
  925. debug ("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
  926. initrd_high, initrd_copy_to_ram);
  927. if (rd_data) {
  928. if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
  929. debug (" in-place initrd\n");
  930. *initrd_start = rd_data;
  931. *initrd_end = rd_data + rd_len;
  932. lmb_reserve(lmb, rd_data, rd_len);
  933. } else {
  934. if (initrd_high)
  935. *initrd_start = lmb_alloc_base (lmb, rd_len, 0x1000, initrd_high);
  936. else
  937. *initrd_start = lmb_alloc (lmb, rd_len, 0x1000);
  938. if (*initrd_start == 0) {
  939. puts ("ramdisk - allocation error\n");
  940. goto error;
  941. }
  942. show_boot_progress (12);
  943. *initrd_end = *initrd_start + rd_len;
  944. printf (" Loading Ramdisk to %08lx, end %08lx ... ",
  945. *initrd_start, *initrd_end);
  946. memmove_wd ((void *)*initrd_start,
  947. (void *)rd_data, rd_len, CHUNKSZ);
  948. puts ("OK\n");
  949. }
  950. } else {
  951. *initrd_start = 0;
  952. *initrd_end = 0;
  953. }
  954. debug (" ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
  955. *initrd_start, *initrd_end);
  956. return 0;
  957. error:
  958. return -1;
  959. }
  960. /**
  961. * boot_get_cmdline - allocate and initialize kernel cmdline
  962. * @lmb: pointer to lmb handle, will be used for memory mgmt
  963. * @cmd_start: pointer to a ulong variable, will hold cmdline start
  964. * @cmd_end: pointer to a ulong variable, will hold cmdline end
  965. * @bootmap_base: ulong variable, holds offset in physical memory to
  966. * base of bootmap
  967. *
  968. * boot_get_cmdline() allocates space for kernel command line below
  969. * BOOTMAPSZ + bootmap_base address. If "bootargs" U-boot environemnt
  970. * variable is present its contents is copied to allocated kernel
  971. * command line.
  972. *
  973. * returns:
  974. * 0 - success
  975. * -1 - failure
  976. */
  977. int boot_get_cmdline (struct lmb *lmb, ulong *cmd_start, ulong *cmd_end,
  978. ulong bootmap_base)
  979. {
  980. char *cmdline;
  981. char *s;
  982. cmdline = (char *)lmb_alloc_base(lmb, CFG_BARGSIZE, 0xf,
  983. CFG_BOOTMAPSZ + bootmap_base);
  984. if (cmdline == NULL)
  985. return -1;
  986. if ((s = getenv("bootargs")) == NULL)
  987. s = "";
  988. strcpy(cmdline, s);
  989. *cmd_start = (ulong) & cmdline[0];
  990. *cmd_end = *cmd_start + strlen(cmdline);
  991. debug ("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
  992. return 0;
  993. }
  994. /**
  995. * boot_get_kbd - allocate and initialize kernel copy of board info
  996. * @lmb: pointer to lmb handle, will be used for memory mgmt
  997. * @kbd: double pointer to board info data
  998. * @bootmap_base: ulong variable, holds offset in physical memory to
  999. * base of bootmap
  1000. *
  1001. * boot_get_kbd() allocates space for kernel copy of board info data below
  1002. * BOOTMAPSZ + bootmap_base address and kernel board info is initialized with
  1003. * the current u-boot board info data.
  1004. *
  1005. * returns:
  1006. * 0 - success
  1007. * -1 - failure
  1008. */
  1009. int boot_get_kbd (struct lmb *lmb, bd_t **kbd, ulong bootmap_base)
  1010. {
  1011. *kbd = (bd_t *)lmb_alloc_base(lmb, sizeof(bd_t), 0xf,
  1012. CFG_BOOTMAPSZ + bootmap_base);
  1013. if (*kbd == NULL)
  1014. return -1;
  1015. **kbd = *(gd->bd);
  1016. debug ("## kernel board info at 0x%08lx\n", (ulong)*kbd);
  1017. #if defined(DEBUG) && defined(CONFIG_CMD_BDI)
  1018. do_bdinfo(NULL, 0, 0, NULL);
  1019. #endif
  1020. return 0;
  1021. }
  1022. #endif /* CONFIG_PPC || CONFIG_M68K */
  1023. #endif /* !USE_HOSTCC */
  1024. #if defined(CONFIG_FIT)
  1025. /*****************************************************************************/
  1026. /* New uImage format routines */
  1027. /*****************************************************************************/
  1028. #ifndef USE_HOSTCC
  1029. static int fit_parse_spec (const char *spec, char sepc, ulong addr_curr,
  1030. ulong *addr, const char **name)
  1031. {
  1032. const char *sep;
  1033. *addr = addr_curr;
  1034. *name = NULL;
  1035. sep = strchr (spec, sepc);
  1036. if (sep) {
  1037. if (sep - spec > 0)
  1038. *addr = simple_strtoul (spec, NULL, 16);
  1039. *name = sep + 1;
  1040. return 1;
  1041. }
  1042. return 0;
  1043. }
  1044. /**
  1045. * fit_parse_conf - parse FIT configuration spec
  1046. * @spec: input string, containing configuration spec
  1047. * @add_curr: current image address (to be used as a possible default)
  1048. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  1049. * configuration
  1050. * @conf_name double pointer to a char, will hold pointer to a configuration
  1051. * unit name
  1052. *
  1053. * fit_parse_conf() expects configuration spec in the for of [<addr>]#<conf>,
  1054. * where <addr> is a FIT image address that contains configuration
  1055. * with a <conf> unit name.
  1056. *
  1057. * Address part is optional, and if omitted default add_curr will
  1058. * be used instead.
  1059. *
  1060. * returns:
  1061. * 1 if spec is a valid configuration string,
  1062. * addr and conf_name are set accordingly
  1063. * 0 otherwise
  1064. */
  1065. inline int fit_parse_conf (const char *spec, ulong addr_curr,
  1066. ulong *addr, const char **conf_name)
  1067. {
  1068. return fit_parse_spec (spec, '#', addr_curr, addr, conf_name);
  1069. }
  1070. /**
  1071. * fit_parse_subimage - parse FIT subimage spec
  1072. * @spec: input string, containing subimage spec
  1073. * @add_curr: current image address (to be used as a possible default)
  1074. * @addr: pointer to a ulong variable, will hold FIT image address of a given
  1075. * subimage
  1076. * @image_name: double pointer to a char, will hold pointer to a subimage name
  1077. *
  1078. * fit_parse_subimage() expects subimage spec in the for of
  1079. * [<addr>]:<subimage>, where <addr> is a FIT image address that contains
  1080. * subimage with a <subimg> unit name.
  1081. *
  1082. * Address part is optional, and if omitted default add_curr will
  1083. * be used instead.
  1084. *
  1085. * returns:
  1086. * 1 if spec is a valid subimage string,
  1087. * addr and image_name are set accordingly
  1088. * 0 otherwise
  1089. */
  1090. inline int fit_parse_subimage (const char *spec, ulong addr_curr,
  1091. ulong *addr, const char **image_name)
  1092. {
  1093. return fit_parse_spec (spec, ':', addr_curr, addr, image_name);
  1094. }
  1095. #endif /* !USE_HOSTCC */
  1096. static void fit_get_debug (const void *fit, int noffset,
  1097. char *prop_name, int err)
  1098. {
  1099. debug ("Can't get '%s' property from FIT 0x%08lx, "
  1100. "node: offset %d, name %s (%s)\n",
  1101. prop_name, (ulong)fit, noffset,
  1102. fit_get_name (fit, noffset, NULL),
  1103. fdt_strerror (err));
  1104. }
  1105. /**
  1106. * fit_print_contents - prints out the contents of the FIT format image
  1107. * @fit: pointer to the FIT format image header
  1108. * @p: pointer to prefix string
  1109. *
  1110. * fit_print_contents() formats a multi line FIT image contents description.
  1111. * The routine prints out FIT image properties (root node level) follwed by
  1112. * the details of each component image.
  1113. *
  1114. * returns:
  1115. * no returned results
  1116. */
  1117. void fit_print_contents (const void *fit)
  1118. {
  1119. char *desc;
  1120. char *uname;
  1121. int images_noffset;
  1122. int confs_noffset;
  1123. int noffset;
  1124. int ndepth;
  1125. int count = 0;
  1126. int ret;
  1127. const char *p;
  1128. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  1129. time_t timestamp;
  1130. #endif
  1131. #ifdef USE_HOSTCC
  1132. p = "";
  1133. #else
  1134. p = " ";
  1135. #endif
  1136. /* Root node properties */
  1137. ret = fit_get_desc (fit, 0, &desc);
  1138. printf ("%sFIT description: ", p);
  1139. if (ret)
  1140. printf ("unavailable\n");
  1141. else
  1142. printf ("%s\n", desc);
  1143. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  1144. ret = fit_get_timestamp (fit, 0, &timestamp);
  1145. printf ("%sCreated: ", p);
  1146. if (ret)
  1147. printf ("unavailable\n");
  1148. else
  1149. genimg_print_time (timestamp);
  1150. #endif
  1151. /* Find images parent node offset */
  1152. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1153. if (images_noffset < 0) {
  1154. printf ("Can't find images parent node '%s' (%s)\n",
  1155. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1156. return;
  1157. }
  1158. /* Process its subnodes, print out component images details */
  1159. for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
  1160. (noffset >= 0) && (ndepth > 0);
  1161. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1162. if (ndepth == 1) {
  1163. /*
  1164. * Direct child node of the images parent node,
  1165. * i.e. component image node.
  1166. */
  1167. printf ("%s Image %u (%s)\n", p, count++,
  1168. fit_get_name(fit, noffset, NULL));
  1169. fit_image_print (fit, noffset, p);
  1170. }
  1171. }
  1172. /* Find configurations parent node offset */
  1173. confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
  1174. if (confs_noffset < 0) {
  1175. debug ("Can't get configurations parent node '%s' (%s)\n",
  1176. FIT_CONFS_PATH, fdt_strerror (confs_noffset));
  1177. return;
  1178. }
  1179. /* get default configuration unit name from default property */
  1180. uname = (char *)fdt_getprop (fit, noffset, FIT_DEFAULT_PROP, NULL);
  1181. if (uname)
  1182. printf ("%s Default Configuration: '%s'\n", p, uname);
  1183. /* Process its subnodes, print out configurations details */
  1184. for (ndepth = 0, count = 0, noffset = fdt_next_node (fit, confs_noffset, &ndepth);
  1185. (noffset >= 0) && (ndepth > 0);
  1186. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1187. if (ndepth == 1) {
  1188. /*
  1189. * Direct child node of the configurations parent node,
  1190. * i.e. configuration node.
  1191. */
  1192. printf ("%s Configuration %u (%s)\n", p, count++,
  1193. fit_get_name(fit, noffset, NULL));
  1194. fit_conf_print (fit, noffset, p);
  1195. }
  1196. }
  1197. }
  1198. /**
  1199. * fit_image_print - prints out the FIT component image details
  1200. * @fit: pointer to the FIT format image header
  1201. * @image_noffset: offset of the component image node
  1202. * @p: pointer to prefix string
  1203. *
  1204. * fit_image_print() lists all mandatory properies for the processed component
  1205. * image. If present, hash nodes are printed out as well.
  1206. *
  1207. * returns:
  1208. * no returned results
  1209. */
  1210. void fit_image_print (const void *fit, int image_noffset, const char *p)
  1211. {
  1212. char *desc;
  1213. uint8_t type, arch, os, comp;
  1214. size_t size;
  1215. ulong load, entry;
  1216. const void *data;
  1217. int noffset;
  1218. int ndepth;
  1219. int ret;
  1220. /* Mandatory properties */
  1221. ret = fit_get_desc (fit, image_noffset, &desc);
  1222. printf ("%s Description: ", p);
  1223. if (ret)
  1224. printf ("unavailable\n");
  1225. else
  1226. printf ("%s\n", desc);
  1227. fit_image_get_type (fit, image_noffset, &type);
  1228. printf ("%s Type: %s\n", p, genimg_get_type_name (type));
  1229. fit_image_get_comp (fit, image_noffset, &comp);
  1230. printf ("%s Compression: %s\n", p, genimg_get_comp_name (comp));
  1231. ret = fit_image_get_data (fit, image_noffset, &data, &size);
  1232. #ifndef USE_HOSTCC
  1233. printf ("%s Data Start: ", p);
  1234. if (ret)
  1235. printf ("unavailable\n");
  1236. else
  1237. printf ("0x%08lx\n", (ulong)data);
  1238. #endif
  1239. printf ("%s Data Size: ", p);
  1240. if (ret)
  1241. printf ("unavailable\n");
  1242. else
  1243. genimg_print_size (size);
  1244. /* Remaining, type dependent properties */
  1245. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE) ||
  1246. (type == IH_TYPE_RAMDISK) || (type == IH_TYPE_FIRMWARE) ||
  1247. (type == IH_TYPE_FLATDT)) {
  1248. fit_image_get_arch (fit, image_noffset, &arch);
  1249. printf ("%s Architecture: %s\n", p, genimg_get_arch_name (arch));
  1250. }
  1251. if (type == IH_TYPE_KERNEL) {
  1252. fit_image_get_os (fit, image_noffset, &os);
  1253. printf ("%s OS: %s\n", p, genimg_get_os_name (os));
  1254. }
  1255. if ((type == IH_TYPE_KERNEL) || (type == IH_TYPE_STANDALONE)) {
  1256. ret = fit_image_get_load (fit, image_noffset, &load);
  1257. printf ("%s Load Address: ", p);
  1258. if (ret)
  1259. printf ("unavailable\n");
  1260. else
  1261. printf ("0x%08lx\n", load);
  1262. fit_image_get_entry (fit, image_noffset, &entry);
  1263. printf ("%s Entry Point: ", p);
  1264. if (ret)
  1265. printf ("unavailable\n");
  1266. else
  1267. printf ("0x%08lx\n", entry);
  1268. }
  1269. /* Process all hash subnodes of the component image node */
  1270. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1271. (noffset >= 0) && (ndepth > 0);
  1272. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1273. if (ndepth == 1) {
  1274. /* Direct child node of the component image node */
  1275. fit_image_print_hash (fit, noffset, p);
  1276. }
  1277. }
  1278. }
  1279. /**
  1280. * fit_image_print_hash - prints out the hash node details
  1281. * @fit: pointer to the FIT format image header
  1282. * @noffset: offset of the hash node
  1283. * @p: pointer to prefix string
  1284. *
  1285. * fit_image_print_hash() lists properies for the processed hash node
  1286. *
  1287. * returns:
  1288. * no returned results
  1289. */
  1290. void fit_image_print_hash (const void *fit, int noffset, const char *p)
  1291. {
  1292. char *algo;
  1293. uint8_t *value;
  1294. int value_len;
  1295. int i, ret;
  1296. /*
  1297. * Check subnode name, must be equal to "hash".
  1298. * Multiple hash nodes require unique unit node
  1299. * names, e.g. hash@1, hash@2, etc.
  1300. */
  1301. if (strncmp (fit_get_name(fit, noffset, NULL),
  1302. FIT_HASH_NODENAME,
  1303. strlen(FIT_HASH_NODENAME)) != 0)
  1304. return;
  1305. debug ("%s Hash node: '%s'\n", p,
  1306. fit_get_name (fit, noffset, NULL));
  1307. printf ("%s Hash algo: ", p);
  1308. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1309. printf ("invalid/unsupported\n");
  1310. return;
  1311. }
  1312. printf ("%s\n", algo);
  1313. ret = fit_image_hash_get_value (fit, noffset, &value,
  1314. &value_len);
  1315. printf ("%s Hash value: ", p);
  1316. if (ret) {
  1317. printf ("unavailable\n");
  1318. } else {
  1319. for (i = 0; i < value_len; i++)
  1320. printf ("%02x", value[i]);
  1321. printf ("\n");
  1322. }
  1323. debug ("%s Hash len: %d\n", p, value_len);
  1324. }
  1325. /**
  1326. * fit_get_desc - get node description property
  1327. * @fit: pointer to the FIT format image header
  1328. * @noffset: node offset
  1329. * @desc: double pointer to the char, will hold pointer to the descrption
  1330. *
  1331. * fit_get_desc() reads description property from a given node, if
  1332. * description is found pointer to it is returened in third call argument.
  1333. *
  1334. * returns:
  1335. * 0, on success
  1336. * -1, on failure
  1337. */
  1338. int fit_get_desc (const void *fit, int noffset, char **desc)
  1339. {
  1340. int len;
  1341. *desc = (char *)fdt_getprop (fit, noffset, FIT_DESC_PROP, &len);
  1342. if (*desc == NULL) {
  1343. fit_get_debug (fit, noffset, FIT_DESC_PROP, len);
  1344. return -1;
  1345. }
  1346. return 0;
  1347. }
  1348. /**
  1349. * fit_get_timestamp - get node timestamp property
  1350. * @fit: pointer to the FIT format image header
  1351. * @noffset: node offset
  1352. * @timestamp: pointer to the time_t, will hold read timestamp
  1353. *
  1354. * fit_get_timestamp() reads timestamp poperty from given node, if timestamp
  1355. * is found and has a correct size its value is retured in third call
  1356. * argument.
  1357. *
  1358. * returns:
  1359. * 0, on success
  1360. * -1, on property read failure
  1361. * -2, on wrong timestamp size
  1362. */
  1363. int fit_get_timestamp (const void *fit, int noffset, time_t *timestamp)
  1364. {
  1365. int len;
  1366. const void *data;
  1367. data = fdt_getprop (fit, noffset, FIT_TIMESTAMP_PROP, &len);
  1368. if (data == NULL) {
  1369. fit_get_debug (fit, noffset, FIT_TIMESTAMP_PROP, len);
  1370. return -1;
  1371. }
  1372. if (len != sizeof (uint32_t)) {
  1373. debug ("FIT timestamp with incorrect size of (%u)\n", len);
  1374. return -2;
  1375. }
  1376. *timestamp = uimage_to_cpu (*((uint32_t *)data));
  1377. return 0;
  1378. }
  1379. /**
  1380. * fit_image_get_node - get node offset for component image of a given unit name
  1381. * @fit: pointer to the FIT format image header
  1382. * @image_uname: component image node unit name
  1383. *
  1384. * fit_image_get_node() finds a component image (withing the '/images'
  1385. * node) of a provided unit name. If image is found its node offset is
  1386. * returned to the caller.
  1387. *
  1388. * returns:
  1389. * image node offset when found (>=0)
  1390. * negative number on failure (FDT_ERR_* code)
  1391. */
  1392. int fit_image_get_node (const void *fit, const char *image_uname)
  1393. {
  1394. int noffset, images_noffset;
  1395. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1396. if (images_noffset < 0) {
  1397. debug ("Can't find images parent node '%s' (%s)\n",
  1398. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1399. return images_noffset;
  1400. }
  1401. noffset = fdt_subnode_offset (fit, images_noffset, image_uname);
  1402. if (noffset < 0) {
  1403. debug ("Can't get node offset for image unit name: '%s' (%s)\n",
  1404. image_uname, fdt_strerror (noffset));
  1405. }
  1406. return noffset;
  1407. }
  1408. /**
  1409. * fit_image_get_os - get os id for a given component image node
  1410. * @fit: pointer to the FIT format image header
  1411. * @noffset: component image node offset
  1412. * @os: pointer to the uint8_t, will hold os numeric id
  1413. *
  1414. * fit_image_get_os() finds os property in a given component image node.
  1415. * If the property is found, its (string) value is translated to the numeric
  1416. * id which is returned to the caller.
  1417. *
  1418. * returns:
  1419. * 0, on success
  1420. * -1, on failure
  1421. */
  1422. int fit_image_get_os (const void *fit, int noffset, uint8_t *os)
  1423. {
  1424. int len;
  1425. const void *data;
  1426. /* Get OS name from property data */
  1427. data = fdt_getprop (fit, noffset, FIT_OS_PROP, &len);
  1428. if (data == NULL) {
  1429. fit_get_debug (fit, noffset, FIT_OS_PROP, len);
  1430. *os = -1;
  1431. return -1;
  1432. }
  1433. /* Translate OS name to id */
  1434. *os = genimg_get_os_id (data);
  1435. return 0;
  1436. }
  1437. /**
  1438. * fit_image_get_arch - get arch id for a given component image node
  1439. * @fit: pointer to the FIT format image header
  1440. * @noffset: component image node offset
  1441. * @arch: pointer to the uint8_t, will hold arch numeric id
  1442. *
  1443. * fit_image_get_arch() finds arch property in a given component image node.
  1444. * If the property is found, its (string) value is translated to the numeric
  1445. * id which is returned to the caller.
  1446. *
  1447. * returns:
  1448. * 0, on success
  1449. * -1, on failure
  1450. */
  1451. int fit_image_get_arch (const void *fit, int noffset, uint8_t *arch)
  1452. {
  1453. int len;
  1454. const void *data;
  1455. /* Get architecture name from property data */
  1456. data = fdt_getprop (fit, noffset, FIT_ARCH_PROP, &len);
  1457. if (data == NULL) {
  1458. fit_get_debug (fit, noffset, FIT_ARCH_PROP, len);
  1459. *arch = -1;
  1460. return -1;
  1461. }
  1462. /* Translate architecture name to id */
  1463. *arch = genimg_get_arch_id (data);
  1464. return 0;
  1465. }
  1466. /**
  1467. * fit_image_get_type - get type id for a given component image node
  1468. * @fit: pointer to the FIT format image header
  1469. * @noffset: component image node offset
  1470. * @type: pointer to the uint8_t, will hold type numeric id
  1471. *
  1472. * fit_image_get_type() finds type property in a given component image node.
  1473. * If the property is found, its (string) value is translated to the numeric
  1474. * id which is returned to the caller.
  1475. *
  1476. * returns:
  1477. * 0, on success
  1478. * -1, on failure
  1479. */
  1480. int fit_image_get_type (const void *fit, int noffset, uint8_t *type)
  1481. {
  1482. int len;
  1483. const void *data;
  1484. /* Get image type name from property data */
  1485. data = fdt_getprop (fit, noffset, FIT_TYPE_PROP, &len);
  1486. if (data == NULL) {
  1487. fit_get_debug (fit, noffset, FIT_TYPE_PROP, len);
  1488. *type = -1;
  1489. return -1;
  1490. }
  1491. /* Translate image type name to id */
  1492. *type = genimg_get_type_id (data);
  1493. return 0;
  1494. }
  1495. /**
  1496. * fit_image_get_comp - get comp id for a given component image node
  1497. * @fit: pointer to the FIT format image header
  1498. * @noffset: component image node offset
  1499. * @comp: pointer to the uint8_t, will hold comp numeric id
  1500. *
  1501. * fit_image_get_comp() finds comp property in a given component image node.
  1502. * If the property is found, its (string) value is translated to the numeric
  1503. * id which is returned to the caller.
  1504. *
  1505. * returns:
  1506. * 0, on success
  1507. * -1, on failure
  1508. */
  1509. int fit_image_get_comp (const void *fit, int noffset, uint8_t *comp)
  1510. {
  1511. int len;
  1512. const void *data;
  1513. /* Get compression name from property data */
  1514. data = fdt_getprop (fit, noffset, FIT_COMP_PROP, &len);
  1515. if (data == NULL) {
  1516. fit_get_debug (fit, noffset, FIT_COMP_PROP, len);
  1517. *comp = -1;
  1518. return -1;
  1519. }
  1520. /* Translate compression name to id */
  1521. *comp = genimg_get_comp_id (data);
  1522. return 0;
  1523. }
  1524. /**
  1525. * fit_image_get_load - get load address property for a given component image node
  1526. * @fit: pointer to the FIT format image header
  1527. * @noffset: component image node offset
  1528. * @load: pointer to the uint32_t, will hold load address
  1529. *
  1530. * fit_image_get_load() finds load address property in a given component image node.
  1531. * If the property is found, its value is returned to the caller.
  1532. *
  1533. * returns:
  1534. * 0, on success
  1535. * -1, on failure
  1536. */
  1537. int fit_image_get_load (const void *fit, int noffset, ulong *load)
  1538. {
  1539. int len;
  1540. const uint32_t *data;
  1541. data = fdt_getprop (fit, noffset, FIT_LOAD_PROP, &len);
  1542. if (data == NULL) {
  1543. fit_get_debug (fit, noffset, FIT_LOAD_PROP, len);
  1544. return -1;
  1545. }
  1546. *load = uimage_to_cpu (*data);
  1547. return 0;
  1548. }
  1549. /**
  1550. * fit_image_get_entry - get entry point address property for a given component image node
  1551. * @fit: pointer to the FIT format image header
  1552. * @noffset: component image node offset
  1553. * @entry: pointer to the uint32_t, will hold entry point address
  1554. *
  1555. * fit_image_get_entry() finds entry point address property in a given component image node.
  1556. * If the property is found, its value is returned to the caller.
  1557. *
  1558. * returns:
  1559. * 0, on success
  1560. * -1, on failure
  1561. */
  1562. int fit_image_get_entry (const void *fit, int noffset, ulong *entry)
  1563. {
  1564. int len;
  1565. const uint32_t *data;
  1566. data = fdt_getprop (fit, noffset, FIT_ENTRY_PROP, &len);
  1567. if (data == NULL) {
  1568. fit_get_debug (fit, noffset, FIT_ENTRY_PROP, len);
  1569. return -1;
  1570. }
  1571. *entry = uimage_to_cpu (*data);
  1572. return 0;
  1573. }
  1574. /**
  1575. * fit_image_get_data - get data property and its size for a given component image node
  1576. * @fit: pointer to the FIT format image header
  1577. * @noffset: component image node offset
  1578. * @data: double pointer to void, will hold data property's data address
  1579. * @size: pointer to size_t, will hold data property's data size
  1580. *
  1581. * fit_image_get_data() finds data property in a given component image node.
  1582. * If the property is found its data start address and size are returned to
  1583. * the caller.
  1584. *
  1585. * returns:
  1586. * 0, on success
  1587. * -1, on failure
  1588. */
  1589. int fit_image_get_data (const void *fit, int noffset,
  1590. const void **data, size_t *size)
  1591. {
  1592. int len;
  1593. *data = fdt_getprop (fit, noffset, FIT_DATA_PROP, &len);
  1594. if (*data == NULL) {
  1595. fit_get_debug (fit, noffset, FIT_DATA_PROP, len);
  1596. *size = 0;
  1597. return -1;
  1598. }
  1599. *size = len;
  1600. return 0;
  1601. }
  1602. /**
  1603. * fit_image_hash_get_algo - get hash algorithm name
  1604. * @fit: pointer to the FIT format image header
  1605. * @noffset: hash node offset
  1606. * @algo: double pointer to char, will hold pointer to the algorithm name
  1607. *
  1608. * fit_image_hash_get_algo() finds hash algorithm property in a given hash node.
  1609. * If the property is found its data start address is returned to the caller.
  1610. *
  1611. * returns:
  1612. * 0, on success
  1613. * -1, on failure
  1614. */
  1615. int fit_image_hash_get_algo (const void *fit, int noffset, char **algo)
  1616. {
  1617. int len;
  1618. *algo = (char *)fdt_getprop (fit, noffset, FIT_ALGO_PROP, &len);
  1619. if (*algo == NULL) {
  1620. fit_get_debug (fit, noffset, FIT_ALGO_PROP, len);
  1621. return -1;
  1622. }
  1623. return 0;
  1624. }
  1625. /**
  1626. * fit_image_hash_get_value - get hash value and length
  1627. * @fit: pointer to the FIT format image header
  1628. * @noffset: hash node offset
  1629. * @value: double pointer to uint8_t, will hold address of a hash value data
  1630. * @value_len: pointer to an int, will hold hash data length
  1631. *
  1632. * fit_image_hash_get_value() finds hash value property in a given hash node.
  1633. * If the property is found its data start address and size are returned to
  1634. * the caller.
  1635. *
  1636. * returns:
  1637. * 0, on success
  1638. * -1, on failure
  1639. */
  1640. int fit_image_hash_get_value (const void *fit, int noffset, uint8_t **value,
  1641. int *value_len)
  1642. {
  1643. int len;
  1644. *value = (uint8_t *)fdt_getprop (fit, noffset, FIT_VALUE_PROP, &len);
  1645. if (*value == NULL) {
  1646. fit_get_debug (fit, noffset, FIT_VALUE_PROP, len);
  1647. *value_len = 0;
  1648. return -1;
  1649. }
  1650. *value_len = len;
  1651. return 0;
  1652. }
  1653. /**
  1654. * fit_set_timestamp - set node timestamp property
  1655. * @fit: pointer to the FIT format image header
  1656. * @noffset: node offset
  1657. * @timestamp: timestamp value to be set
  1658. *
  1659. * fit_set_timestamp() attempts to set timestamp property in the requested
  1660. * node and returns operation status to the caller.
  1661. *
  1662. * returns:
  1663. * 0, on success
  1664. * -1, on property read failure
  1665. */
  1666. int fit_set_timestamp (void *fit, int noffset, time_t timestamp)
  1667. {
  1668. uint32_t t;
  1669. int ret;
  1670. t = cpu_to_uimage (timestamp);
  1671. ret = fdt_setprop (fit, noffset, FIT_TIMESTAMP_PROP, &t,
  1672. sizeof (uint32_t));
  1673. if (ret) {
  1674. printf ("Can't set '%s' property for '%s' node (%s)\n",
  1675. FIT_TIMESTAMP_PROP, fit_get_name (fit, noffset, NULL),
  1676. fdt_strerror (ret));
  1677. return -1;
  1678. }
  1679. return 0;
  1680. }
  1681. /**
  1682. * calculate_hash - calculate and return hash for provided input data
  1683. * @data: pointer to the input data
  1684. * @data_len: data length
  1685. * @algo: requested hash algorithm
  1686. * @value: pointer to the char, will hold hash value data (caller must
  1687. * allocate enough free space)
  1688. * value_len: length of the calculated hash
  1689. *
  1690. * calculate_hash() computes input data hash according to the requested algorithm.
  1691. * Resulting hash value is placed in caller provided 'value' buffer, length
  1692. * of the calculated hash is returned via value_len pointer argument.
  1693. *
  1694. * returns:
  1695. * 0, on success
  1696. * -1, when algo is unsupported
  1697. */
  1698. static int calculate_hash (const void *data, int data_len, const char *algo,
  1699. uint8_t *value, int *value_len)
  1700. {
  1701. if (strcmp (algo, "crc32") == 0 ) {
  1702. *((uint32_t *)value) = crc32 (0, data, data_len);
  1703. *((uint32_t *)value) = cpu_to_uimage (*((uint32_t *)value));
  1704. *value_len = 4;
  1705. } else if (strcmp (algo, "sha1") == 0 ) {
  1706. sha1_csum ((unsigned char *) data, data_len,
  1707. (unsigned char *) value);
  1708. *value_len = 20;
  1709. } else if (strcmp (algo, "md5") == 0 ) {
  1710. md5 ((unsigned char *)data, data_len, value);
  1711. *value_len = 16;
  1712. } else {
  1713. debug ("Unsupported hash alogrithm\n");
  1714. return -1;
  1715. }
  1716. return 0;
  1717. }
  1718. #ifdef USE_HOSTCC
  1719. /**
  1720. * fit_set_hashes - process FIT component image nodes and calculate hashes
  1721. * @fit: pointer to the FIT format image header
  1722. *
  1723. * fit_set_hashes() adds hash values for all component images in the FIT blob.
  1724. * Hashes are calculated for all component images which have hash subnodes
  1725. * with algorithm property set to one of the supported hash algorithms.
  1726. *
  1727. * returns
  1728. * 0, on success
  1729. * libfdt error code, on failure
  1730. */
  1731. int fit_set_hashes (void *fit)
  1732. {
  1733. int images_noffset;
  1734. int noffset;
  1735. int ndepth;
  1736. int ret;
  1737. /* Find images parent node offset */
  1738. images_noffset = fdt_path_offset (fit, FIT_IMAGES_PATH);
  1739. if (images_noffset < 0) {
  1740. printf ("Can't find images parent node '%s' (%s)\n",
  1741. FIT_IMAGES_PATH, fdt_strerror (images_noffset));
  1742. return images_noffset;
  1743. }
  1744. /* Process its subnodes, print out component images details */
  1745. for (ndepth = 0, noffset = fdt_next_node (fit, images_noffset, &ndepth);
  1746. (noffset >= 0) && (ndepth > 0);
  1747. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1748. if (ndepth == 1) {
  1749. /*
  1750. * Direct child node of the images parent node,
  1751. * i.e. component image node.
  1752. */
  1753. ret = fit_image_set_hashes (fit, noffset);
  1754. if (ret)
  1755. return ret;
  1756. }
  1757. }
  1758. return 0;
  1759. }
  1760. /**
  1761. * fit_image_set_hashes - calculate/set hashes for given component image node
  1762. * @fit: pointer to the FIT format image header
  1763. * @image_noffset: requested component image node
  1764. *
  1765. * fit_image_set_hashes() adds hash values for an component image node. All
  1766. * existing hash subnodes are checked, if algorithm property is set to one of
  1767. * the supported hash algorithms, hash value is computed and corresponding
  1768. * hash node property is set, for example:
  1769. *
  1770. * Input component image node structure:
  1771. *
  1772. * o image@1 (at image_noffset)
  1773. * | - data = [binary data]
  1774. * o hash@1
  1775. * |- algo = "sha1"
  1776. *
  1777. * Output component image node structure:
  1778. *
  1779. * o image@1 (at image_noffset)
  1780. * | - data = [binary data]
  1781. * o hash@1
  1782. * |- algo = "sha1"
  1783. * |- value = sha1(data)
  1784. *
  1785. * returns:
  1786. * 0 on sucess
  1787. * <0 on failure
  1788. */
  1789. int fit_image_set_hashes (void *fit, int image_noffset)
  1790. {
  1791. const void *data;
  1792. size_t size;
  1793. char *algo;
  1794. uint8_t value[FIT_MAX_HASH_LEN];
  1795. int value_len;
  1796. int noffset;
  1797. int ndepth;
  1798. /* Get image data and data length */
  1799. if (fit_image_get_data (fit, image_noffset, &data, &size)) {
  1800. printf ("Can't get image data/size\n");
  1801. return -1;
  1802. }
  1803. /* Process all hash subnodes of the component image node */
  1804. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1805. (noffset >= 0) && (ndepth > 0);
  1806. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1807. if (ndepth == 1) {
  1808. /* Direct child node of the component image node */
  1809. /*
  1810. * Check subnode name, must be equal to "hash".
  1811. * Multiple hash nodes require unique unit node
  1812. * names, e.g. hash@1, hash@2, etc.
  1813. */
  1814. if (strncmp (fit_get_name(fit, noffset, NULL),
  1815. FIT_HASH_NODENAME,
  1816. strlen(FIT_HASH_NODENAME)) != 0) {
  1817. /* Not a hash subnode, skip it */
  1818. continue;
  1819. }
  1820. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1821. printf ("Can't get hash algo property for "
  1822. "'%s' hash node in '%s' image node\n",
  1823. fit_get_name (fit, noffset, NULL),
  1824. fit_get_name (fit, image_noffset, NULL));
  1825. return -1;
  1826. }
  1827. if (calculate_hash (data, size, algo, value, &value_len)) {
  1828. printf ("Unsupported hash algorithm (%s) for "
  1829. "'%s' hash node in '%s' image node\n",
  1830. algo, fit_get_name (fit, noffset, NULL),
  1831. fit_get_name (fit, image_noffset, NULL));
  1832. return -1;
  1833. }
  1834. if (fit_image_hash_set_value (fit, noffset, value,
  1835. value_len)) {
  1836. printf ("Can't set hash value for "
  1837. "'%s' hash node in '%s' image node\n",
  1838. fit_get_name (fit, noffset, NULL),
  1839. fit_get_name (fit, image_noffset, NULL));
  1840. return -1;
  1841. }
  1842. }
  1843. }
  1844. return 0;
  1845. }
  1846. /**
  1847. * fit_image_hash_set_value - set hash value in requested has node
  1848. * @fit: pointer to the FIT format image header
  1849. * @noffset: hash node offset
  1850. * @value: hash value to be set
  1851. * @value_len: hash value length
  1852. *
  1853. * fit_image_hash_set_value() attempts to set hash value in a node at offset
  1854. * given and returns operation status to the caller.
  1855. *
  1856. * returns
  1857. * 0, on success
  1858. * -1, on failure
  1859. */
  1860. int fit_image_hash_set_value (void *fit, int noffset, uint8_t *value,
  1861. int value_len)
  1862. {
  1863. int ret;
  1864. ret = fdt_setprop (fit, noffset, FIT_VALUE_PROP, value, value_len);
  1865. if (ret) {
  1866. printf ("Can't set hash '%s' property for '%s' node (%s)\n",
  1867. FIT_VALUE_PROP, fit_get_name (fit, noffset, NULL),
  1868. fdt_strerror (ret));
  1869. return -1;
  1870. }
  1871. return 0;
  1872. }
  1873. #endif /* USE_HOSTCC */
  1874. /**
  1875. * fit_image_check_hashes - verify data intergity
  1876. * @fit: pointer to the FIT format image header
  1877. * @image_noffset: component image node offset
  1878. *
  1879. * fit_image_check_hashes() goes over component image hash nodes,
  1880. * re-calculates each data hash and compares with the value stored in hash
  1881. * node.
  1882. *
  1883. * returns:
  1884. * 1, if all hashes are valid
  1885. * 0, otherwise (or on error)
  1886. */
  1887. int fit_image_check_hashes (const void *fit, int image_noffset)
  1888. {
  1889. const void *data;
  1890. size_t size;
  1891. char *algo;
  1892. uint8_t *fit_value;
  1893. int fit_value_len;
  1894. uint8_t value[FIT_MAX_HASH_LEN];
  1895. int value_len;
  1896. int noffset;
  1897. int ndepth;
  1898. char *err_msg = "";
  1899. /* Get image data and data length */
  1900. if (fit_image_get_data (fit, image_noffset, &data, &size)) {
  1901. printf ("Can't get image data/size\n");
  1902. return 0;
  1903. }
  1904. /* Process all hash subnodes of the component image node */
  1905. for (ndepth = 0, noffset = fdt_next_node (fit, image_noffset, &ndepth);
  1906. (noffset >= 0) && (ndepth > 0);
  1907. noffset = fdt_next_node (fit, noffset, &ndepth)) {
  1908. if (ndepth == 1) {
  1909. /* Direct child node of the component image node */
  1910. /*
  1911. * Check subnode name, must be equal to "hash".
  1912. * Multiple hash nodes require unique unit node
  1913. * names, e.g. hash@1, hash@2, etc.
  1914. */
  1915. if (strncmp (fit_get_name(fit, noffset, NULL),
  1916. FIT_HASH_NODENAME,
  1917. strlen(FIT_HASH_NODENAME)) != 0)
  1918. continue;
  1919. if (fit_image_hash_get_algo (fit, noffset, &algo)) {
  1920. err_msg = "Can't get hash algo property";
  1921. goto error;
  1922. }
  1923. printf ("%s", algo);
  1924. if (fit_image_hash_get_value (fit, noffset, &fit_value,
  1925. &fit_value_len)) {
  1926. err_msg = "Can't get hash value property";
  1927. goto error;
  1928. }
  1929. if (calculate_hash (data, size, algo, value, &value_len)) {
  1930. err_msg = "Unsupported hash algorithm";
  1931. goto error;
  1932. }
  1933. if (value_len != fit_value_len) {
  1934. err_msg = "Bad hash value len";
  1935. goto error;
  1936. } else if (memcmp (value, fit_value, value_len) != 0) {
  1937. err_msg = "Bad hash value";
  1938. goto error;
  1939. }
  1940. printf ("+ ");
  1941. }
  1942. }
  1943. return 1;
  1944. error:
  1945. printf ("%s for '%s' hash node in '%s' image node\n",
  1946. err_msg, fit_get_name (fit, noffset, NULL),
  1947. fit_get_name (fit, image_noffset, NULL));
  1948. return 0;
  1949. }
  1950. /**
  1951. * fit_image_check_os - check whether image node is of a given os type
  1952. * @fit: pointer to the FIT format image header
  1953. * @noffset: component image node offset
  1954. * @os: requested image os
  1955. *
  1956. * fit_image_check_os() reads image os property and compares its numeric
  1957. * id with the requested os. Comparison result is returned to the caller.
  1958. *
  1959. * returns:
  1960. * 1 if image is of given os type
  1961. * 0 otherwise (or on error)
  1962. */
  1963. int fit_image_check_os (const void *fit, int noffset, uint8_t os)
  1964. {
  1965. uint8_t image_os;
  1966. if (fit_image_get_os (fit, noffset, &image_os))
  1967. return 0;
  1968. return (os == image_os);
  1969. }
  1970. /**
  1971. * fit_image_check_arch - check whether image node is of a given arch
  1972. * @fit: pointer to the FIT format image header
  1973. * @noffset: component image node offset
  1974. * @arch: requested imagearch
  1975. *
  1976. * fit_image_check_arch() reads image arch property and compares its numeric
  1977. * id with the requested arch. Comparison result is returned to the caller.
  1978. *
  1979. * returns:
  1980. * 1 if image is of given arch
  1981. * 0 otherwise (or on error)
  1982. */
  1983. int fit_image_check_arch (const void *fit, int noffset, uint8_t arch)
  1984. {
  1985. uint8_t image_arch;
  1986. if (fit_image_get_arch (fit, noffset, &image_arch))
  1987. return 0;
  1988. return (arch == image_arch);
  1989. }
  1990. /**
  1991. * fit_image_check_type - check whether image node is of a given type
  1992. * @fit: pointer to the FIT format image header
  1993. * @noffset: component image node offset
  1994. * @type: requested image type
  1995. *
  1996. * fit_image_check_type() reads image type property and compares its numeric
  1997. * id with the requested type. Comparison result is returned to the caller.
  1998. *
  1999. * returns:
  2000. * 1 if image is of given type
  2001. * 0 otherwise (or on error)
  2002. */
  2003. int fit_image_check_type (const void *fit, int noffset, uint8_t type)
  2004. {
  2005. uint8_t image_type;
  2006. if (fit_image_get_type (fit, noffset, &image_type))
  2007. return 0;
  2008. return (type == image_type);
  2009. }
  2010. /**
  2011. * fit_image_check_comp - check whether image node uses given compression
  2012. * @fit: pointer to the FIT format image header
  2013. * @noffset: component image node offset
  2014. * @comp: requested image compression type
  2015. *
  2016. * fit_image_check_comp() reads image compression property and compares its
  2017. * numeric id with the requested compression type. Comparison result is
  2018. * returned to the caller.
  2019. *
  2020. * returns:
  2021. * 1 if image uses requested compression
  2022. * 0 otherwise (or on error)
  2023. */
  2024. int fit_image_check_comp (const void *fit, int noffset, uint8_t comp)
  2025. {
  2026. uint8_t image_comp;
  2027. if (fit_image_get_comp (fit, noffset, &image_comp))
  2028. return 0;
  2029. return (comp == image_comp);
  2030. }
  2031. /**
  2032. * fit_check_format - sanity check FIT image format
  2033. * @fit: pointer to the FIT format image header
  2034. *
  2035. * fit_check_format() runs a basic sanity FIT image verification.
  2036. * Routine checks for mandatory properties, nodes, etc.
  2037. *
  2038. * returns:
  2039. * 1, on success
  2040. * 0, on failure
  2041. */
  2042. int fit_check_format (const void *fit)
  2043. {
  2044. /* mandatory / node 'description' property */
  2045. if (fdt_getprop (fit, 0, FIT_DESC_PROP, NULL) == NULL) {
  2046. debug ("Wrong FIT format: no description\n");
  2047. return 0;
  2048. }
  2049. #if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE) || defined(USE_HOSTCC)
  2050. /* mandatory / node 'timestamp' property */
  2051. if (fdt_getprop (fit, 0, FIT_TIMESTAMP_PROP, NULL) == NULL) {
  2052. debug ("Wrong FIT format: no description\n");
  2053. return 0;
  2054. }
  2055. #endif
  2056. /* mandatory subimages parent '/images' node */
  2057. if (fdt_path_offset (fit, FIT_IMAGES_PATH) < 0) {
  2058. debug ("Wrong FIT format: no images parent node\n");
  2059. return 0;
  2060. }
  2061. return 1;
  2062. }
  2063. /**
  2064. * fit_conf_get_node - get node offset for configuration of a given unit name
  2065. * @fit: pointer to the FIT format image header
  2066. * @conf_uname: configuration node unit name
  2067. *
  2068. * fit_conf_get_node() finds a configuration (withing the '/configurations'
  2069. * parant node) of a provided unit name. If configuration is found its node offset
  2070. * is returned to the caller.
  2071. *
  2072. * When NULL is provided in second argument fit_conf_get_node() will search
  2073. * for a default configuration node instead. Default configuration node unit name
  2074. * is retrived from FIT_DEFAULT_PROP property of the '/configurations' node.
  2075. *
  2076. * returns:
  2077. * configuration node offset when found (>=0)
  2078. * negative number on failure (FDT_ERR_* code)
  2079. */
  2080. int fit_conf_get_node (const void *fit, const char *conf_uname)
  2081. {
  2082. int noffset, confs_noffset;
  2083. int len;
  2084. confs_noffset = fdt_path_offset (fit, FIT_CONFS_PATH);
  2085. if (confs_noffset < 0) {
  2086. debug ("Can't find configurations parent node '%s' (%s)\n",
  2087. FIT_CONFS_PATH, fdt_strerror (confs_noffset));
  2088. return confs_noffset;
  2089. }
  2090. if (conf_uname == NULL) {
  2091. /* get configuration unit name from the default property */
  2092. debug ("No configuration specified, trying default...\n");
  2093. conf_uname = (char *)fdt_getprop (fit, confs_noffset, FIT_DEFAULT_PROP, &len);
  2094. if (conf_uname == NULL) {
  2095. fit_get_debug (fit, confs_noffset, FIT_DEFAULT_PROP, len);
  2096. return len;
  2097. }
  2098. debug ("Found default configuration: '%s'\n", conf_uname);
  2099. }
  2100. noffset = fdt_subnode_offset (fit, confs_noffset, conf_uname);
  2101. if (noffset < 0) {
  2102. debug ("Can't get node offset for configuration unit name: '%s' (%s)\n",
  2103. conf_uname, fdt_strerror (noffset));
  2104. }
  2105. return noffset;
  2106. }
  2107. static int __fit_conf_get_prop_node (const void *fit, int noffset,
  2108. const char *prop_name)
  2109. {
  2110. char *uname;
  2111. int len;
  2112. /* get kernel image unit name from configuration kernel property */
  2113. uname = (char *)fdt_getprop (fit, noffset, prop_name, &len);
  2114. if (uname == NULL)
  2115. return len;
  2116. return fit_image_get_node (fit, uname);
  2117. }
  2118. /**
  2119. * fit_conf_get_kernel_node - get kernel image node offset that corresponds to
  2120. * a given configuration
  2121. * @fit: pointer to the FIT format image header
  2122. * @noffset: configuration node offset
  2123. *
  2124. * fit_conf_get_kernel_node() retrives kernel image node unit name from
  2125. * configuration FIT_KERNEL_PROP property and translates it to the node
  2126. * offset.
  2127. *
  2128. * returns:
  2129. * image node offset when found (>=0)
  2130. * negative number on failure (FDT_ERR_* code)
  2131. */
  2132. int fit_conf_get_kernel_node (const void *fit, int noffset)
  2133. {
  2134. return __fit_conf_get_prop_node (fit, noffset, FIT_KERNEL_PROP);
  2135. }
  2136. /**
  2137. * fit_conf_get_ramdisk_node - get ramdisk image node offset that corresponds to
  2138. * a given configuration
  2139. * @fit: pointer to the FIT format image header
  2140. * @noffset: configuration node offset
  2141. *
  2142. * fit_conf_get_ramdisk_node() retrives ramdisk image node unit name from
  2143. * configuration FIT_KERNEL_PROP property and translates it to the node
  2144. * offset.
  2145. *
  2146. * returns:
  2147. * image node offset when found (>=0)
  2148. * negative number on failure (FDT_ERR_* code)
  2149. */
  2150. int fit_conf_get_ramdisk_node (const void *fit, int noffset)
  2151. {
  2152. return __fit_conf_get_prop_node (fit, noffset, FIT_RAMDISK_PROP);
  2153. }
  2154. /**
  2155. * fit_conf_get_fdt_node - get fdt image node offset that corresponds to
  2156. * a given configuration
  2157. * @fit: pointer to the FIT format image header
  2158. * @noffset: configuration node offset
  2159. *
  2160. * fit_conf_get_fdt_node() retrives fdt image node unit name from
  2161. * configuration FIT_KERNEL_PROP property and translates it to the node
  2162. * offset.
  2163. *
  2164. * returns:
  2165. * image node offset when found (>=0)
  2166. * negative number on failure (FDT_ERR_* code)
  2167. */
  2168. int fit_conf_get_fdt_node (const void *fit, int noffset)
  2169. {
  2170. return __fit_conf_get_prop_node (fit, noffset, FIT_FDT_PROP);
  2171. }
  2172. /**
  2173. * fit_conf_print - prints out the FIT configuration details
  2174. * @fit: pointer to the FIT format image header
  2175. * @noffset: offset of the configuration node
  2176. * @p: pointer to prefix string
  2177. *
  2178. * fit_conf_print() lists all mandatory properies for the processed
  2179. * configuration node.
  2180. *
  2181. * returns:
  2182. * no returned results
  2183. */
  2184. void fit_conf_print (const void *fit, int noffset, const char *p)
  2185. {
  2186. char *desc;
  2187. char *uname;
  2188. int ret;
  2189. /* Mandatory properties */
  2190. ret = fit_get_desc (fit, noffset, &desc);
  2191. printf ("%s Description: ", p);
  2192. if (ret)
  2193. printf ("unavailable\n");
  2194. else
  2195. printf ("%s\n", desc);
  2196. uname = (char *)fdt_getprop (fit, noffset, FIT_KERNEL_PROP, NULL);
  2197. printf ("%s Kernel: ", p);
  2198. if (uname == NULL)
  2199. printf ("unavailable\n");
  2200. else
  2201. printf ("%s\n", uname);
  2202. /* Optional properties */
  2203. uname = (char *)fdt_getprop (fit, noffset, FIT_RAMDISK_PROP, NULL);
  2204. if (uname)
  2205. printf ("%s Init Ramdisk: %s\n", p, uname);
  2206. uname = (char *)fdt_getprop (fit, noffset, FIT_FDT_PROP, NULL);
  2207. if (uname)
  2208. printf ("%s FDT: %s\n", p, uname);
  2209. }
  2210. /**
  2211. * fit_check_ramdisk - verify FIT format ramdisk subimage
  2212. * @fit_hdr: pointer to the FIT ramdisk header
  2213. * @rd_noffset: ramdisk subimage node offset within FIT image
  2214. * @arch: requested ramdisk image architecture type
  2215. * @verify: data CRC verification flag
  2216. *
  2217. * fit_check_ramdisk() verifies integrity of the ramdisk subimage and from
  2218. * specified FIT image.
  2219. *
  2220. * returns:
  2221. * 1, on success
  2222. * 0, on failure
  2223. */
  2224. #ifndef USE_HOSTCC
  2225. static int fit_check_ramdisk (const void *fit, int rd_noffset, uint8_t arch, int verify)
  2226. {
  2227. fit_image_print (fit, rd_noffset, " ");
  2228. if (verify) {
  2229. puts (" Verifying Hash Integrity ... ");
  2230. if (!fit_image_check_hashes (fit, rd_noffset)) {
  2231. puts ("Bad Data Hash\n");
  2232. show_boot_progress (-125);
  2233. return 0;
  2234. }
  2235. puts ("OK\n");
  2236. }
  2237. show_boot_progress (126);
  2238. if (!fit_image_check_os (fit, rd_noffset, IH_OS_LINUX) ||
  2239. !fit_image_check_arch (fit, rd_noffset, arch) ||
  2240. !fit_image_check_type (fit, rd_noffset, IH_TYPE_RAMDISK)) {
  2241. printf ("No Linux %s Ramdisk Image\n",
  2242. genimg_get_arch_name(arch));
  2243. show_boot_progress (-126);
  2244. return 0;
  2245. }
  2246. show_boot_progress (127);
  2247. return 1;
  2248. }
  2249. #endif /* USE_HOSTCC */
  2250. #endif /* CONFIG_FIT */