image.c 83 KB

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