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