image.c 81 KB

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