image.c 85 KB

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