image.c 90 KB

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