image.c 83 KB

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