image.c 83 KB

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