image.c 89 KB

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