image.c 85 KB

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