image.c 90 KB

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