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