image.c 83 KB

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