image.c 85 KB

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