image.c 84 KB

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