image.c 82 KB

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