image.c 82 KB

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