image.c 83 KB

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