image.c 82 KB

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