image.c 83 KB

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