image.c 89 KB

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