image.c 89 KB

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