image.c 81 KB

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