image.c 84 KB

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