image.c 85 KB

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