ftest.py 185 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2016 Google, Inc
  3. # Written by Simon Glass <sjg@chromium.org>
  4. #
  5. # To run a single test, change to this directory, and:
  6. #
  7. # python -m unittest func_test.TestFunctional.testHelp
  8. import collections
  9. import gzip
  10. import hashlib
  11. from optparse import OptionParser
  12. import os
  13. import re
  14. import shutil
  15. import struct
  16. import sys
  17. import tempfile
  18. import unittest
  19. from binman import cbfs_util
  20. from binman import cmdline
  21. from binman import control
  22. from binman import elf
  23. from binman import elf_test
  24. from binman import fmap_util
  25. from binman import state
  26. from dtoc import fdt
  27. from dtoc import fdt_util
  28. from binman.etype import fdtmap
  29. from binman.etype import image_header
  30. from binman.image import Image
  31. from patman import command
  32. from patman import test_util
  33. from patman import tools
  34. from patman import tout
  35. # Contents of test files, corresponding to different entry types
  36. U_BOOT_DATA = b'1234'
  37. U_BOOT_IMG_DATA = b'img'
  38. U_BOOT_SPL_DATA = b'56780123456789abcdefghi'
  39. U_BOOT_TPL_DATA = b'tpl9876543210fedcbazyw'
  40. BLOB_DATA = b'89'
  41. ME_DATA = b'0abcd'
  42. VGA_DATA = b'vga'
  43. U_BOOT_DTB_DATA = b'udtb'
  44. U_BOOT_SPL_DTB_DATA = b'spldtb'
  45. U_BOOT_TPL_DTB_DATA = b'tpldtb'
  46. X86_START16_DATA = b'start16'
  47. X86_START16_SPL_DATA = b'start16spl'
  48. X86_START16_TPL_DATA = b'start16tpl'
  49. X86_RESET16_DATA = b'reset16'
  50. X86_RESET16_SPL_DATA = b'reset16spl'
  51. X86_RESET16_TPL_DATA = b'reset16tpl'
  52. PPC_MPC85XX_BR_DATA = b'ppcmpc85xxbr'
  53. U_BOOT_NODTB_DATA = b'nodtb with microcode pointer somewhere in here'
  54. U_BOOT_SPL_NODTB_DATA = b'splnodtb with microcode pointer somewhere in here'
  55. U_BOOT_TPL_NODTB_DATA = b'tplnodtb with microcode pointer somewhere in here'
  56. FSP_DATA = b'fsp'
  57. CMC_DATA = b'cmc'
  58. VBT_DATA = b'vbt'
  59. MRC_DATA = b'mrc'
  60. TEXT_DATA = 'text'
  61. TEXT_DATA2 = 'text2'
  62. TEXT_DATA3 = 'text3'
  63. CROS_EC_RW_DATA = b'ecrw'
  64. GBB_DATA = b'gbbd'
  65. BMPBLK_DATA = b'bmp'
  66. VBLOCK_DATA = b'vblk'
  67. FILES_DATA = (b"sorry I'm late\nOh, don't bother apologising, I'm " +
  68. b"sorry you're alive\n")
  69. COMPRESS_DATA = b'compress xxxxxxxxxxxxxxxxxxxxxx data'
  70. COMPRESS_DATA_BIG = COMPRESS_DATA * 2
  71. REFCODE_DATA = b'refcode'
  72. FSP_M_DATA = b'fsp_m'
  73. FSP_S_DATA = b'fsp_s'
  74. FSP_T_DATA = b'fsp_t'
  75. ATF_BL31_DATA = b'bl31'
  76. OPENSBI_DATA = b'opensbi'
  77. SCP_DATA = b'scp'
  78. TEST_FDT1_DATA = b'fdt1'
  79. TEST_FDT2_DATA = b'test-fdt2'
  80. ENV_DATA = b'var1=1\nvar2="2"'
  81. # Subdirectory of the input dir to use to put test FDTs
  82. TEST_FDT_SUBDIR = 'fdts'
  83. # The expected size for the device tree in some tests
  84. EXTRACT_DTB_SIZE = 0x3c9
  85. # Properties expected to be in the device tree when update_dtb is used
  86. BASE_DTB_PROPS = ['offset', 'size', 'image-pos']
  87. # Extra properties expected to be in the device tree when allow-repack is used
  88. REPACK_DTB_PROPS = ['orig-offset', 'orig-size']
  89. class TestFunctional(unittest.TestCase):
  90. """Functional tests for binman
  91. Most of these use a sample .dts file to build an image and then check
  92. that it looks correct. The sample files are in the test/ subdirectory
  93. and are numbered.
  94. For each entry type a very small test file is created using fixed
  95. string contents. This makes it easy to test that things look right, and
  96. debug problems.
  97. In some cases a 'real' file must be used - these are also supplied in
  98. the test/ diurectory.
  99. """
  100. @classmethod
  101. def setUpClass(cls):
  102. global entry
  103. from binman import entry
  104. # Handle the case where argv[0] is 'python'
  105. cls._binman_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  106. cls._binman_pathname = os.path.join(cls._binman_dir, 'binman')
  107. # Create a temporary directory for input files
  108. cls._indir = tempfile.mkdtemp(prefix='binmant.')
  109. # Create some test files
  110. TestFunctional._MakeInputFile('u-boot.bin', U_BOOT_DATA)
  111. TestFunctional._MakeInputFile('u-boot.img', U_BOOT_IMG_DATA)
  112. TestFunctional._MakeInputFile('spl/u-boot-spl.bin', U_BOOT_SPL_DATA)
  113. TestFunctional._MakeInputFile('tpl/u-boot-tpl.bin', U_BOOT_TPL_DATA)
  114. TestFunctional._MakeInputFile('blobfile', BLOB_DATA)
  115. TestFunctional._MakeInputFile('me.bin', ME_DATA)
  116. TestFunctional._MakeInputFile('vga.bin', VGA_DATA)
  117. cls._ResetDtbs()
  118. TestFunctional._MakeInputFile('u-boot-br.bin', PPC_MPC85XX_BR_DATA)
  119. TestFunctional._MakeInputFile('u-boot-x86-start16.bin', X86_START16_DATA)
  120. TestFunctional._MakeInputFile('spl/u-boot-x86-start16-spl.bin',
  121. X86_START16_SPL_DATA)
  122. TestFunctional._MakeInputFile('tpl/u-boot-x86-start16-tpl.bin',
  123. X86_START16_TPL_DATA)
  124. TestFunctional._MakeInputFile('u-boot-x86-reset16.bin',
  125. X86_RESET16_DATA)
  126. TestFunctional._MakeInputFile('spl/u-boot-x86-reset16-spl.bin',
  127. X86_RESET16_SPL_DATA)
  128. TestFunctional._MakeInputFile('tpl/u-boot-x86-reset16-tpl.bin',
  129. X86_RESET16_TPL_DATA)
  130. TestFunctional._MakeInputFile('u-boot-nodtb.bin', U_BOOT_NODTB_DATA)
  131. TestFunctional._MakeInputFile('spl/u-boot-spl-nodtb.bin',
  132. U_BOOT_SPL_NODTB_DATA)
  133. TestFunctional._MakeInputFile('tpl/u-boot-tpl-nodtb.bin',
  134. U_BOOT_TPL_NODTB_DATA)
  135. TestFunctional._MakeInputFile('fsp.bin', FSP_DATA)
  136. TestFunctional._MakeInputFile('cmc.bin', CMC_DATA)
  137. TestFunctional._MakeInputFile('vbt.bin', VBT_DATA)
  138. TestFunctional._MakeInputFile('mrc.bin', MRC_DATA)
  139. TestFunctional._MakeInputFile('ecrw.bin', CROS_EC_RW_DATA)
  140. TestFunctional._MakeInputDir('devkeys')
  141. TestFunctional._MakeInputFile('bmpblk.bin', BMPBLK_DATA)
  142. TestFunctional._MakeInputFile('refcode.bin', REFCODE_DATA)
  143. TestFunctional._MakeInputFile('fsp_m.bin', FSP_M_DATA)
  144. TestFunctional._MakeInputFile('fsp_s.bin', FSP_S_DATA)
  145. TestFunctional._MakeInputFile('fsp_t.bin', FSP_T_DATA)
  146. cls._elf_testdir = os.path.join(cls._indir, 'elftest')
  147. elf_test.BuildElfTestFiles(cls._elf_testdir)
  148. # ELF file with a '_dt_ucode_base_size' symbol
  149. TestFunctional._MakeInputFile('u-boot',
  150. tools.ReadFile(cls.ElfTestFile('u_boot_ucode_ptr')))
  151. # Intel flash descriptor file
  152. cls._SetupDescriptor()
  153. shutil.copytree(cls.TestFile('files'),
  154. os.path.join(cls._indir, 'files'))
  155. TestFunctional._MakeInputFile('compress', COMPRESS_DATA)
  156. TestFunctional._MakeInputFile('compress_big', COMPRESS_DATA_BIG)
  157. TestFunctional._MakeInputFile('bl31.bin', ATF_BL31_DATA)
  158. TestFunctional._MakeInputFile('fw_dynamic.bin', OPENSBI_DATA)
  159. TestFunctional._MakeInputFile('scp.bin', SCP_DATA)
  160. # Add a few .dtb files for testing
  161. TestFunctional._MakeInputFile('%s/test-fdt1.dtb' % TEST_FDT_SUBDIR,
  162. TEST_FDT1_DATA)
  163. TestFunctional._MakeInputFile('%s/test-fdt2.dtb' % TEST_FDT_SUBDIR,
  164. TEST_FDT2_DATA)
  165. TestFunctional._MakeInputFile('env.txt', ENV_DATA)
  166. # Travis-CI may have an old lz4
  167. cls.have_lz4 = True
  168. try:
  169. tools.Run('lz4', '--no-frame-crc', '-c',
  170. os.path.join(cls._indir, 'u-boot.bin'), binary=True)
  171. except:
  172. cls.have_lz4 = False
  173. @classmethod
  174. def tearDownClass(cls):
  175. """Remove the temporary input directory and its contents"""
  176. if cls.preserve_indir:
  177. print('Preserving input dir: %s' % cls._indir)
  178. else:
  179. if cls._indir:
  180. shutil.rmtree(cls._indir)
  181. cls._indir = None
  182. @classmethod
  183. def setup_test_args(cls, preserve_indir=False, preserve_outdirs=False,
  184. toolpath=None, verbosity=None):
  185. """Accept arguments controlling test execution
  186. Args:
  187. preserve_indir: Preserve the shared input directory used by all
  188. tests in this class.
  189. preserve_outdir: Preserve the output directories used by tests. Each
  190. test has its own, so this is normally only useful when running a
  191. single test.
  192. toolpath: ist of paths to use for tools
  193. """
  194. cls.preserve_indir = preserve_indir
  195. cls.preserve_outdirs = preserve_outdirs
  196. cls.toolpath = toolpath
  197. cls.verbosity = verbosity
  198. def _CheckLz4(self):
  199. if not self.have_lz4:
  200. self.skipTest('lz4 --no-frame-crc not available')
  201. def _CleanupOutputDir(self):
  202. """Remove the temporary output directory"""
  203. if self.preserve_outdirs:
  204. print('Preserving output dir: %s' % tools.outdir)
  205. else:
  206. tools._FinaliseForTest()
  207. def setUp(self):
  208. # Enable this to turn on debugging output
  209. # tout.Init(tout.DEBUG)
  210. command.test_result = None
  211. def tearDown(self):
  212. """Remove the temporary output directory"""
  213. self._CleanupOutputDir()
  214. def _SetupImageInTmpdir(self):
  215. """Set up the output image in a new temporary directory
  216. This is used when an image has been generated in the output directory,
  217. but we want to run binman again. This will create a new output
  218. directory and fail to delete the original one.
  219. This creates a new temporary directory, copies the image to it (with a
  220. new name) and removes the old output directory.
  221. Returns:
  222. Tuple:
  223. Temporary directory to use
  224. New image filename
  225. """
  226. image_fname = tools.GetOutputFilename('image.bin')
  227. tmpdir = tempfile.mkdtemp(prefix='binman.')
  228. updated_fname = os.path.join(tmpdir, 'image-updated.bin')
  229. tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
  230. self._CleanupOutputDir()
  231. return tmpdir, updated_fname
  232. @classmethod
  233. def _ResetDtbs(cls):
  234. TestFunctional._MakeInputFile('u-boot.dtb', U_BOOT_DTB_DATA)
  235. TestFunctional._MakeInputFile('spl/u-boot-spl.dtb', U_BOOT_SPL_DTB_DATA)
  236. TestFunctional._MakeInputFile('tpl/u-boot-tpl.dtb', U_BOOT_TPL_DTB_DATA)
  237. def _RunBinman(self, *args, **kwargs):
  238. """Run binman using the command line
  239. Args:
  240. Arguments to pass, as a list of strings
  241. kwargs: Arguments to pass to Command.RunPipe()
  242. """
  243. result = command.RunPipe([[self._binman_pathname] + list(args)],
  244. capture=True, capture_stderr=True, raise_on_error=False)
  245. if result.return_code and kwargs.get('raise_on_error', True):
  246. raise Exception("Error running '%s': %s" % (' '.join(args),
  247. result.stdout + result.stderr))
  248. return result
  249. def _DoBinman(self, *argv):
  250. """Run binman using directly (in the same process)
  251. Args:
  252. Arguments to pass, as a list of strings
  253. Returns:
  254. Return value (0 for success)
  255. """
  256. argv = list(argv)
  257. args = cmdline.ParseArgs(argv)
  258. args.pager = 'binman-invalid-pager'
  259. args.build_dir = self._indir
  260. # For testing, you can force an increase in verbosity here
  261. # args.verbosity = tout.DEBUG
  262. return control.Binman(args)
  263. def _DoTestFile(self, fname, debug=False, map=False, update_dtb=False,
  264. entry_args=None, images=None, use_real_dtb=False,
  265. use_expanded=False, verbosity=None, allow_missing=False,
  266. extra_indirs=None, threads=None,
  267. test_section_timeout=False):
  268. """Run binman with a given test file
  269. Args:
  270. fname: Device-tree source filename to use (e.g. 005_simple.dts)
  271. debug: True to enable debugging output
  272. map: True to output map files for the images
  273. update_dtb: Update the offset and size of each entry in the device
  274. tree before packing it into the image
  275. entry_args: Dict of entry args to supply to binman
  276. key: arg name
  277. value: value of that arg
  278. images: List of image names to build
  279. use_real_dtb: True to use the test file as the contents of
  280. the u-boot-dtb entry. Normally this is not needed and the
  281. test contents (the U_BOOT_DTB_DATA string) can be used.
  282. But in some test we need the real contents.
  283. use_expanded: True to use expanded entries where available, e.g.
  284. 'u-boot-expanded' instead of 'u-boot'
  285. verbosity: Verbosity level to use (0-3, None=don't set it)
  286. allow_missing: Set the '--allow-missing' flag so that missing
  287. external binaries just produce a warning instead of an error
  288. extra_indirs: Extra input directories to add using -I
  289. threads: Number of threads to use (None for default, 0 for
  290. single-threaded)
  291. """
  292. args = []
  293. if debug:
  294. args.append('-D')
  295. if verbosity is not None:
  296. args.append('-v%d' % verbosity)
  297. elif self.verbosity:
  298. args.append('-v%d' % self.verbosity)
  299. if self.toolpath:
  300. for path in self.toolpath:
  301. args += ['--toolpath', path]
  302. if threads is not None:
  303. args.append('-T%d' % threads)
  304. if test_section_timeout:
  305. args.append('--test-section-timeout')
  306. args += ['build', '-p', '-I', self._indir, '-d', self.TestFile(fname)]
  307. if map:
  308. args.append('-m')
  309. if update_dtb:
  310. args.append('-u')
  311. if not use_real_dtb:
  312. args.append('--fake-dtb')
  313. if not use_expanded:
  314. args.append('--no-expanded')
  315. if entry_args:
  316. for arg, value in entry_args.items():
  317. args.append('-a%s=%s' % (arg, value))
  318. if allow_missing:
  319. args.append('-M')
  320. if images:
  321. for image in images:
  322. args += ['-i', image]
  323. if extra_indirs:
  324. for indir in extra_indirs:
  325. args += ['-I', indir]
  326. return self._DoBinman(*args)
  327. def _SetupDtb(self, fname, outfile='u-boot.dtb'):
  328. """Set up a new test device-tree file
  329. The given file is compiled and set up as the device tree to be used
  330. for ths test.
  331. Args:
  332. fname: Filename of .dts file to read
  333. outfile: Output filename for compiled device-tree binary
  334. Returns:
  335. Contents of device-tree binary
  336. """
  337. tmpdir = tempfile.mkdtemp(prefix='binmant.')
  338. dtb = fdt_util.EnsureCompiled(self.TestFile(fname), tmpdir)
  339. with open(dtb, 'rb') as fd:
  340. data = fd.read()
  341. TestFunctional._MakeInputFile(outfile, data)
  342. shutil.rmtree(tmpdir)
  343. return data
  344. def _GetDtbContentsForSplTpl(self, dtb_data, name):
  345. """Create a version of the main DTB for SPL or SPL
  346. For testing we don't actually have different versions of the DTB. With
  347. U-Boot we normally run fdtgrep to remove unwanted nodes, but for tests
  348. we don't normally have any unwanted nodes.
  349. We still want the DTBs for SPL and TPL to be different though, since
  350. otherwise it is confusing to know which one we are looking at. So add
  351. an 'spl' or 'tpl' property to the top-level node.
  352. Args:
  353. dtb_data: dtb data to modify (this should be a value devicetree)
  354. name: Name of a new property to add
  355. Returns:
  356. New dtb data with the property added
  357. """
  358. dtb = fdt.Fdt.FromData(dtb_data)
  359. dtb.Scan()
  360. dtb.GetNode('/binman').AddZeroProp(name)
  361. dtb.Sync(auto_resize=True)
  362. dtb.Pack()
  363. return dtb.GetContents()
  364. def _DoReadFileDtb(self, fname, use_real_dtb=False, use_expanded=False,
  365. map=False, update_dtb=False, entry_args=None,
  366. reset_dtbs=True, extra_indirs=None, threads=None):
  367. """Run binman and return the resulting image
  368. This runs binman with a given test file and then reads the resulting
  369. output file. It is a shortcut function since most tests need to do
  370. these steps.
  371. Raises an assertion failure if binman returns a non-zero exit code.
  372. Args:
  373. fname: Device-tree source filename to use (e.g. 005_simple.dts)
  374. use_real_dtb: True to use the test file as the contents of
  375. the u-boot-dtb entry. Normally this is not needed and the
  376. test contents (the U_BOOT_DTB_DATA string) can be used.
  377. But in some test we need the real contents.
  378. use_expanded: True to use expanded entries where available, e.g.
  379. 'u-boot-expanded' instead of 'u-boot'
  380. map: True to output map files for the images
  381. update_dtb: Update the offset and size of each entry in the device
  382. tree before packing it into the image
  383. entry_args: Dict of entry args to supply to binman
  384. key: arg name
  385. value: value of that arg
  386. reset_dtbs: With use_real_dtb the test dtb is overwritten by this
  387. function. If reset_dtbs is True, then the original test dtb
  388. is written back before this function finishes
  389. extra_indirs: Extra input directories to add using -I
  390. threads: Number of threads to use (None for default, 0 for
  391. single-threaded)
  392. Returns:
  393. Tuple:
  394. Resulting image contents
  395. Device tree contents
  396. Map data showing contents of image (or None if none)
  397. Output device tree binary filename ('u-boot.dtb' path)
  398. """
  399. dtb_data = None
  400. # Use the compiled test file as the u-boot-dtb input
  401. if use_real_dtb:
  402. dtb_data = self._SetupDtb(fname)
  403. # For testing purposes, make a copy of the DT for SPL and TPL. Add
  404. # a node indicating which it is, so aid verification.
  405. for name in ['spl', 'tpl']:
  406. dtb_fname = '%s/u-boot-%s.dtb' % (name, name)
  407. outfile = os.path.join(self._indir, dtb_fname)
  408. TestFunctional._MakeInputFile(dtb_fname,
  409. self._GetDtbContentsForSplTpl(dtb_data, name))
  410. try:
  411. retcode = self._DoTestFile(fname, map=map, update_dtb=update_dtb,
  412. entry_args=entry_args, use_real_dtb=use_real_dtb,
  413. use_expanded=use_expanded, extra_indirs=extra_indirs,
  414. threads=threads)
  415. self.assertEqual(0, retcode)
  416. out_dtb_fname = tools.GetOutputFilename('u-boot.dtb.out')
  417. # Find the (only) image, read it and return its contents
  418. image = control.images['image']
  419. image_fname = tools.GetOutputFilename('image.bin')
  420. self.assertTrue(os.path.exists(image_fname))
  421. if map:
  422. map_fname = tools.GetOutputFilename('image.map')
  423. with open(map_fname) as fd:
  424. map_data = fd.read()
  425. else:
  426. map_data = None
  427. with open(image_fname, 'rb') as fd:
  428. return fd.read(), dtb_data, map_data, out_dtb_fname
  429. finally:
  430. # Put the test file back
  431. if reset_dtbs and use_real_dtb:
  432. self._ResetDtbs()
  433. def _DoReadFileRealDtb(self, fname):
  434. """Run binman with a real .dtb file and return the resulting data
  435. Args:
  436. fname: DT source filename to use (e.g. 082_fdt_update_all.dts)
  437. Returns:
  438. Resulting image contents
  439. """
  440. return self._DoReadFileDtb(fname, use_real_dtb=True, update_dtb=True)[0]
  441. def _DoReadFile(self, fname, use_real_dtb=False):
  442. """Helper function which discards the device-tree binary
  443. Args:
  444. fname: Device-tree source filename to use (e.g. 005_simple.dts)
  445. use_real_dtb: True to use the test file as the contents of
  446. the u-boot-dtb entry. Normally this is not needed and the
  447. test contents (the U_BOOT_DTB_DATA string) can be used.
  448. But in some test we need the real contents.
  449. Returns:
  450. Resulting image contents
  451. """
  452. return self._DoReadFileDtb(fname, use_real_dtb)[0]
  453. @classmethod
  454. def _MakeInputFile(cls, fname, contents):
  455. """Create a new test input file, creating directories as needed
  456. Args:
  457. fname: Filename to create
  458. contents: File contents to write in to the file
  459. Returns:
  460. Full pathname of file created
  461. """
  462. pathname = os.path.join(cls._indir, fname)
  463. dirname = os.path.dirname(pathname)
  464. if dirname and not os.path.exists(dirname):
  465. os.makedirs(dirname)
  466. with open(pathname, 'wb') as fd:
  467. fd.write(contents)
  468. return pathname
  469. @classmethod
  470. def _MakeInputDir(cls, dirname):
  471. """Create a new test input directory, creating directories as needed
  472. Args:
  473. dirname: Directory name to create
  474. Returns:
  475. Full pathname of directory created
  476. """
  477. pathname = os.path.join(cls._indir, dirname)
  478. if not os.path.exists(pathname):
  479. os.makedirs(pathname)
  480. return pathname
  481. @classmethod
  482. def _SetupSplElf(cls, src_fname='bss_data'):
  483. """Set up an ELF file with a '_dt_ucode_base_size' symbol
  484. Args:
  485. Filename of ELF file to use as SPL
  486. """
  487. TestFunctional._MakeInputFile('spl/u-boot-spl',
  488. tools.ReadFile(cls.ElfTestFile(src_fname)))
  489. @classmethod
  490. def _SetupTplElf(cls, src_fname='bss_data'):
  491. """Set up an ELF file with a '_dt_ucode_base_size' symbol
  492. Args:
  493. Filename of ELF file to use as TPL
  494. """
  495. TestFunctional._MakeInputFile('tpl/u-boot-tpl',
  496. tools.ReadFile(cls.ElfTestFile(src_fname)))
  497. @classmethod
  498. def _SetupDescriptor(cls):
  499. with open(cls.TestFile('descriptor.bin'), 'rb') as fd:
  500. TestFunctional._MakeInputFile('descriptor.bin', fd.read())
  501. @classmethod
  502. def TestFile(cls, fname):
  503. return os.path.join(cls._binman_dir, 'test', fname)
  504. @classmethod
  505. def ElfTestFile(cls, fname):
  506. return os.path.join(cls._elf_testdir, fname)
  507. def AssertInList(self, grep_list, target):
  508. """Assert that at least one of a list of things is in a target
  509. Args:
  510. grep_list: List of strings to check
  511. target: Target string
  512. """
  513. for grep in grep_list:
  514. if grep in target:
  515. return
  516. self.fail("Error: '%s' not found in '%s'" % (grep_list, target))
  517. def CheckNoGaps(self, entries):
  518. """Check that all entries fit together without gaps
  519. Args:
  520. entries: List of entries to check
  521. """
  522. offset = 0
  523. for entry in entries.values():
  524. self.assertEqual(offset, entry.offset)
  525. offset += entry.size
  526. def GetFdtLen(self, dtb):
  527. """Get the totalsize field from a device-tree binary
  528. Args:
  529. dtb: Device-tree binary contents
  530. Returns:
  531. Total size of device-tree binary, from the header
  532. """
  533. return struct.unpack('>L', dtb[4:8])[0]
  534. def _GetPropTree(self, dtb, prop_names, prefix='/binman/'):
  535. def AddNode(node, path):
  536. if node.name != '/':
  537. path += '/' + node.name
  538. for prop in node.props.values():
  539. if prop.name in prop_names:
  540. prop_path = path + ':' + prop.name
  541. tree[prop_path[len(prefix):]] = fdt_util.fdt32_to_cpu(
  542. prop.value)
  543. for subnode in node.subnodes:
  544. AddNode(subnode, path)
  545. tree = {}
  546. AddNode(dtb.GetRoot(), '')
  547. return tree
  548. def testRun(self):
  549. """Test a basic run with valid args"""
  550. result = self._RunBinman('-h')
  551. def testFullHelp(self):
  552. """Test that the full help is displayed with -H"""
  553. result = self._RunBinman('-H')
  554. help_file = os.path.join(self._binman_dir, 'README.rst')
  555. # Remove possible extraneous strings
  556. extra = '::::::::::::::\n' + help_file + '\n::::::::::::::\n'
  557. gothelp = result.stdout.replace(extra, '')
  558. self.assertEqual(len(gothelp), os.path.getsize(help_file))
  559. self.assertEqual(0, len(result.stderr))
  560. self.assertEqual(0, result.return_code)
  561. def testFullHelpInternal(self):
  562. """Test that the full help is displayed with -H"""
  563. try:
  564. command.test_result = command.CommandResult()
  565. result = self._DoBinman('-H')
  566. help_file = os.path.join(self._binman_dir, 'README.rst')
  567. finally:
  568. command.test_result = None
  569. def testHelp(self):
  570. """Test that the basic help is displayed with -h"""
  571. result = self._RunBinman('-h')
  572. self.assertTrue(len(result.stdout) > 200)
  573. self.assertEqual(0, len(result.stderr))
  574. self.assertEqual(0, result.return_code)
  575. def testBoard(self):
  576. """Test that we can run it with a specific board"""
  577. self._SetupDtb('005_simple.dts', 'sandbox/u-boot.dtb')
  578. TestFunctional._MakeInputFile('sandbox/u-boot.bin', U_BOOT_DATA)
  579. result = self._DoBinman('build', '-n', '-b', 'sandbox')
  580. self.assertEqual(0, result)
  581. def testNeedBoard(self):
  582. """Test that we get an error when no board ius supplied"""
  583. with self.assertRaises(ValueError) as e:
  584. result = self._DoBinman('build')
  585. self.assertIn("Must provide a board to process (use -b <board>)",
  586. str(e.exception))
  587. def testMissingDt(self):
  588. """Test that an invalid device-tree file generates an error"""
  589. with self.assertRaises(Exception) as e:
  590. self._RunBinman('build', '-d', 'missing_file')
  591. # We get one error from libfdt, and a different one from fdtget.
  592. self.AssertInList(["Couldn't open blob from 'missing_file'",
  593. 'No such file or directory'], str(e.exception))
  594. def testBrokenDt(self):
  595. """Test that an invalid device-tree source file generates an error
  596. Since this is a source file it should be compiled and the error
  597. will come from the device-tree compiler (dtc).
  598. """
  599. with self.assertRaises(Exception) as e:
  600. self._RunBinman('build', '-d', self.TestFile('001_invalid.dts'))
  601. self.assertIn("FATAL ERROR: Unable to parse input tree",
  602. str(e.exception))
  603. def testMissingNode(self):
  604. """Test that a device tree without a 'binman' node generates an error"""
  605. with self.assertRaises(Exception) as e:
  606. self._DoBinman('build', '-d', self.TestFile('002_missing_node.dts'))
  607. self.assertIn("does not have a 'binman' node", str(e.exception))
  608. def testEmpty(self):
  609. """Test that an empty binman node works OK (i.e. does nothing)"""
  610. result = self._RunBinman('build', '-d', self.TestFile('003_empty.dts'))
  611. self.assertEqual(0, len(result.stderr))
  612. self.assertEqual(0, result.return_code)
  613. def testInvalidEntry(self):
  614. """Test that an invalid entry is flagged"""
  615. with self.assertRaises(Exception) as e:
  616. result = self._RunBinman('build', '-d',
  617. self.TestFile('004_invalid_entry.dts'))
  618. self.assertIn("Unknown entry type 'not-a-valid-type' in node "
  619. "'/binman/not-a-valid-type'", str(e.exception))
  620. def testSimple(self):
  621. """Test a simple binman with a single file"""
  622. data = self._DoReadFile('005_simple.dts')
  623. self.assertEqual(U_BOOT_DATA, data)
  624. def testSimpleDebug(self):
  625. """Test a simple binman run with debugging enabled"""
  626. self._DoTestFile('005_simple.dts', debug=True)
  627. def testDual(self):
  628. """Test that we can handle creating two images
  629. This also tests image padding.
  630. """
  631. retcode = self._DoTestFile('006_dual_image.dts')
  632. self.assertEqual(0, retcode)
  633. image = control.images['image1']
  634. self.assertEqual(len(U_BOOT_DATA), image.size)
  635. fname = tools.GetOutputFilename('image1.bin')
  636. self.assertTrue(os.path.exists(fname))
  637. with open(fname, 'rb') as fd:
  638. data = fd.read()
  639. self.assertEqual(U_BOOT_DATA, data)
  640. image = control.images['image2']
  641. self.assertEqual(3 + len(U_BOOT_DATA) + 5, image.size)
  642. fname = tools.GetOutputFilename('image2.bin')
  643. self.assertTrue(os.path.exists(fname))
  644. with open(fname, 'rb') as fd:
  645. data = fd.read()
  646. self.assertEqual(U_BOOT_DATA, data[3:7])
  647. self.assertEqual(tools.GetBytes(0, 3), data[:3])
  648. self.assertEqual(tools.GetBytes(0, 5), data[7:])
  649. def testBadAlign(self):
  650. """Test that an invalid alignment value is detected"""
  651. with self.assertRaises(ValueError) as e:
  652. self._DoTestFile('007_bad_align.dts')
  653. self.assertIn("Node '/binman/u-boot': Alignment 23 must be a power "
  654. "of two", str(e.exception))
  655. def testPackSimple(self):
  656. """Test that packing works as expected"""
  657. retcode = self._DoTestFile('008_pack.dts')
  658. self.assertEqual(0, retcode)
  659. self.assertIn('image', control.images)
  660. image = control.images['image']
  661. entries = image.GetEntries()
  662. self.assertEqual(5, len(entries))
  663. # First u-boot
  664. self.assertIn('u-boot', entries)
  665. entry = entries['u-boot']
  666. self.assertEqual(0, entry.offset)
  667. self.assertEqual(len(U_BOOT_DATA), entry.size)
  668. # Second u-boot, aligned to 16-byte boundary
  669. self.assertIn('u-boot-align', entries)
  670. entry = entries['u-boot-align']
  671. self.assertEqual(16, entry.offset)
  672. self.assertEqual(len(U_BOOT_DATA), entry.size)
  673. # Third u-boot, size 23 bytes
  674. self.assertIn('u-boot-size', entries)
  675. entry = entries['u-boot-size']
  676. self.assertEqual(20, entry.offset)
  677. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  678. self.assertEqual(23, entry.size)
  679. # Fourth u-boot, placed immediate after the above
  680. self.assertIn('u-boot-next', entries)
  681. entry = entries['u-boot-next']
  682. self.assertEqual(43, entry.offset)
  683. self.assertEqual(len(U_BOOT_DATA), entry.size)
  684. # Fifth u-boot, placed at a fixed offset
  685. self.assertIn('u-boot-fixed', entries)
  686. entry = entries['u-boot-fixed']
  687. self.assertEqual(61, entry.offset)
  688. self.assertEqual(len(U_BOOT_DATA), entry.size)
  689. self.assertEqual(65, image.size)
  690. def testPackExtra(self):
  691. """Test that extra packing feature works as expected"""
  692. data, _, _, out_dtb_fname = self._DoReadFileDtb('009_pack_extra.dts',
  693. update_dtb=True)
  694. self.assertIn('image', control.images)
  695. image = control.images['image']
  696. entries = image.GetEntries()
  697. self.assertEqual(5, len(entries))
  698. # First u-boot with padding before and after
  699. self.assertIn('u-boot', entries)
  700. entry = entries['u-boot']
  701. self.assertEqual(0, entry.offset)
  702. self.assertEqual(3, entry.pad_before)
  703. self.assertEqual(3 + 5 + len(U_BOOT_DATA), entry.size)
  704. self.assertEqual(U_BOOT_DATA, entry.data)
  705. self.assertEqual(tools.GetBytes(0, 3) + U_BOOT_DATA +
  706. tools.GetBytes(0, 5), data[:entry.size])
  707. pos = entry.size
  708. # Second u-boot has an aligned size, but it has no effect
  709. self.assertIn('u-boot-align-size-nop', entries)
  710. entry = entries['u-boot-align-size-nop']
  711. self.assertEqual(pos, entry.offset)
  712. self.assertEqual(len(U_BOOT_DATA), entry.size)
  713. self.assertEqual(U_BOOT_DATA, entry.data)
  714. self.assertEqual(U_BOOT_DATA, data[pos:pos + entry.size])
  715. pos += entry.size
  716. # Third u-boot has an aligned size too
  717. self.assertIn('u-boot-align-size', entries)
  718. entry = entries['u-boot-align-size']
  719. self.assertEqual(pos, entry.offset)
  720. self.assertEqual(32, entry.size)
  721. self.assertEqual(U_BOOT_DATA, entry.data)
  722. self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 32 - len(U_BOOT_DATA)),
  723. data[pos:pos + entry.size])
  724. pos += entry.size
  725. # Fourth u-boot has an aligned end
  726. self.assertIn('u-boot-align-end', entries)
  727. entry = entries['u-boot-align-end']
  728. self.assertEqual(48, entry.offset)
  729. self.assertEqual(16, entry.size)
  730. self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)])
  731. self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 16 - len(U_BOOT_DATA)),
  732. data[pos:pos + entry.size])
  733. pos += entry.size
  734. # Fifth u-boot immediately afterwards
  735. self.assertIn('u-boot-align-both', entries)
  736. entry = entries['u-boot-align-both']
  737. self.assertEqual(64, entry.offset)
  738. self.assertEqual(64, entry.size)
  739. self.assertEqual(U_BOOT_DATA, entry.data[:len(U_BOOT_DATA)])
  740. self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 64 - len(U_BOOT_DATA)),
  741. data[pos:pos + entry.size])
  742. self.CheckNoGaps(entries)
  743. self.assertEqual(128, image.size)
  744. dtb = fdt.Fdt(out_dtb_fname)
  745. dtb.Scan()
  746. props = self._GetPropTree(dtb, ['size', 'offset', 'image-pos'])
  747. expected = {
  748. 'image-pos': 0,
  749. 'offset': 0,
  750. 'size': 128,
  751. 'u-boot:image-pos': 0,
  752. 'u-boot:offset': 0,
  753. 'u-boot:size': 3 + 5 + len(U_BOOT_DATA),
  754. 'u-boot-align-size-nop:image-pos': 12,
  755. 'u-boot-align-size-nop:offset': 12,
  756. 'u-boot-align-size-nop:size': 4,
  757. 'u-boot-align-size:image-pos': 16,
  758. 'u-boot-align-size:offset': 16,
  759. 'u-boot-align-size:size': 32,
  760. 'u-boot-align-end:image-pos': 48,
  761. 'u-boot-align-end:offset': 48,
  762. 'u-boot-align-end:size': 16,
  763. 'u-boot-align-both:image-pos': 64,
  764. 'u-boot-align-both:offset': 64,
  765. 'u-boot-align-both:size': 64,
  766. }
  767. self.assertEqual(expected, props)
  768. def testPackAlignPowerOf2(self):
  769. """Test that invalid entry alignment is detected"""
  770. with self.assertRaises(ValueError) as e:
  771. self._DoTestFile('010_pack_align_power2.dts')
  772. self.assertIn("Node '/binman/u-boot': Alignment 5 must be a power "
  773. "of two", str(e.exception))
  774. def testPackAlignSizePowerOf2(self):
  775. """Test that invalid entry size alignment is detected"""
  776. with self.assertRaises(ValueError) as e:
  777. self._DoTestFile('011_pack_align_size_power2.dts')
  778. self.assertIn("Node '/binman/u-boot': Alignment size 55 must be a "
  779. "power of two", str(e.exception))
  780. def testPackInvalidAlign(self):
  781. """Test detection of an offset that does not match its alignment"""
  782. with self.assertRaises(ValueError) as e:
  783. self._DoTestFile('012_pack_inv_align.dts')
  784. self.assertIn("Node '/binman/u-boot': Offset 0x5 (5) does not match "
  785. "align 0x4 (4)", str(e.exception))
  786. def testPackInvalidSizeAlign(self):
  787. """Test that invalid entry size alignment is detected"""
  788. with self.assertRaises(ValueError) as e:
  789. self._DoTestFile('013_pack_inv_size_align.dts')
  790. self.assertIn("Node '/binman/u-boot': Size 0x5 (5) does not match "
  791. "align-size 0x4 (4)", str(e.exception))
  792. def testPackOverlap(self):
  793. """Test that overlapping regions are detected"""
  794. with self.assertRaises(ValueError) as e:
  795. self._DoTestFile('014_pack_overlap.dts')
  796. self.assertIn("Node '/binman/u-boot-align': Offset 0x3 (3) overlaps "
  797. "with previous entry '/binman/u-boot' ending at 0x4 (4)",
  798. str(e.exception))
  799. def testPackEntryOverflow(self):
  800. """Test that entries that overflow their size are detected"""
  801. with self.assertRaises(ValueError) as e:
  802. self._DoTestFile('015_pack_overflow.dts')
  803. self.assertIn("Node '/binman/u-boot': Entry contents size is 0x4 (4) "
  804. "but entry size is 0x3 (3)", str(e.exception))
  805. def testPackImageOverflow(self):
  806. """Test that entries which overflow the image size are detected"""
  807. with self.assertRaises(ValueError) as e:
  808. self._DoTestFile('016_pack_image_overflow.dts')
  809. self.assertIn("Section '/binman': contents size 0x4 (4) exceeds section "
  810. "size 0x3 (3)", str(e.exception))
  811. def testPackImageSize(self):
  812. """Test that the image size can be set"""
  813. retcode = self._DoTestFile('017_pack_image_size.dts')
  814. self.assertEqual(0, retcode)
  815. self.assertIn('image', control.images)
  816. image = control.images['image']
  817. self.assertEqual(7, image.size)
  818. def testPackImageSizeAlign(self):
  819. """Test that image size alignemnt works as expected"""
  820. retcode = self._DoTestFile('018_pack_image_align.dts')
  821. self.assertEqual(0, retcode)
  822. self.assertIn('image', control.images)
  823. image = control.images['image']
  824. self.assertEqual(16, image.size)
  825. def testPackInvalidImageAlign(self):
  826. """Test that invalid image alignment is detected"""
  827. with self.assertRaises(ValueError) as e:
  828. self._DoTestFile('019_pack_inv_image_align.dts')
  829. self.assertIn("Section '/binman': Size 0x7 (7) does not match "
  830. "align-size 0x8 (8)", str(e.exception))
  831. def testPackAlignPowerOf2(self):
  832. """Test that invalid image alignment is detected"""
  833. with self.assertRaises(ValueError) as e:
  834. self._DoTestFile('020_pack_inv_image_align_power2.dts')
  835. self.assertIn("Image '/binman': Alignment size 131 must be a power of "
  836. "two", str(e.exception))
  837. def testImagePadByte(self):
  838. """Test that the image pad byte can be specified"""
  839. self._SetupSplElf()
  840. data = self._DoReadFile('021_image_pad.dts')
  841. self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0xff, 1) +
  842. U_BOOT_DATA, data)
  843. def testImageName(self):
  844. """Test that image files can be named"""
  845. retcode = self._DoTestFile('022_image_name.dts')
  846. self.assertEqual(0, retcode)
  847. image = control.images['image1']
  848. fname = tools.GetOutputFilename('test-name')
  849. self.assertTrue(os.path.exists(fname))
  850. image = control.images['image2']
  851. fname = tools.GetOutputFilename('test-name.xx')
  852. self.assertTrue(os.path.exists(fname))
  853. def testBlobFilename(self):
  854. """Test that generic blobs can be provided by filename"""
  855. data = self._DoReadFile('023_blob.dts')
  856. self.assertEqual(BLOB_DATA, data)
  857. def testPackSorted(self):
  858. """Test that entries can be sorted"""
  859. self._SetupSplElf()
  860. data = self._DoReadFile('024_sorted.dts')
  861. self.assertEqual(tools.GetBytes(0, 1) + U_BOOT_SPL_DATA +
  862. tools.GetBytes(0, 2) + U_BOOT_DATA, data)
  863. def testPackZeroOffset(self):
  864. """Test that an entry at offset 0 is not given a new offset"""
  865. with self.assertRaises(ValueError) as e:
  866. self._DoTestFile('025_pack_zero_size.dts')
  867. self.assertIn("Node '/binman/u-boot-spl': Offset 0x0 (0) overlaps "
  868. "with previous entry '/binman/u-boot' ending at 0x4 (4)",
  869. str(e.exception))
  870. def testPackUbootDtb(self):
  871. """Test that a device tree can be added to U-Boot"""
  872. data = self._DoReadFile('026_pack_u_boot_dtb.dts')
  873. self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA, data)
  874. def testPackX86RomNoSize(self):
  875. """Test that the end-at-4gb property requires a size property"""
  876. with self.assertRaises(ValueError) as e:
  877. self._DoTestFile('027_pack_4gb_no_size.dts')
  878. self.assertIn("Image '/binman': Section size must be provided when "
  879. "using end-at-4gb", str(e.exception))
  880. def test4gbAndSkipAtStartTogether(self):
  881. """Test that the end-at-4gb and skip-at-size property can't be used
  882. together"""
  883. with self.assertRaises(ValueError) as e:
  884. self._DoTestFile('098_4gb_and_skip_at_start_together.dts')
  885. self.assertIn("Image '/binman': Provide either 'end-at-4gb' or "
  886. "'skip-at-start'", str(e.exception))
  887. def testPackX86RomOutside(self):
  888. """Test that the end-at-4gb property checks for offset boundaries"""
  889. with self.assertRaises(ValueError) as e:
  890. self._DoTestFile('028_pack_4gb_outside.dts')
  891. self.assertIn("Node '/binman/u-boot': Offset 0x0 (0) size 0x4 (4) "
  892. "is outside the section '/binman' starting at "
  893. '0xffffffe0 (4294967264) of size 0x20 (32)',
  894. str(e.exception))
  895. def testPackX86Rom(self):
  896. """Test that a basic x86 ROM can be created"""
  897. self._SetupSplElf()
  898. data = self._DoReadFile('029_x86_rom.dts')
  899. self.assertEqual(U_BOOT_DATA + tools.GetBytes(0, 3) + U_BOOT_SPL_DATA +
  900. tools.GetBytes(0, 2), data)
  901. def testPackX86RomMeNoDesc(self):
  902. """Test that an invalid Intel descriptor entry is detected"""
  903. try:
  904. TestFunctional._MakeInputFile('descriptor-empty.bin', b'')
  905. with self.assertRaises(ValueError) as e:
  906. self._DoTestFile('163_x86_rom_me_empty.dts')
  907. self.assertIn("Node '/binman/intel-descriptor': Cannot find Intel Flash Descriptor (FD) signature",
  908. str(e.exception))
  909. finally:
  910. self._SetupDescriptor()
  911. def testPackX86RomBadDesc(self):
  912. """Test that the Intel requires a descriptor entry"""
  913. with self.assertRaises(ValueError) as e:
  914. self._DoTestFile('030_x86_rom_me_no_desc.dts')
  915. self.assertIn("Node '/binman/intel-me': No offset set with "
  916. "offset-unset: should another entry provide this correct "
  917. "offset?", str(e.exception))
  918. def testPackX86RomMe(self):
  919. """Test that an x86 ROM with an ME region can be created"""
  920. data = self._DoReadFile('031_x86_rom_me.dts')
  921. expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
  922. if data[:0x1000] != expected_desc:
  923. self.fail('Expected descriptor binary at start of image')
  924. self.assertEqual(ME_DATA, data[0x1000:0x1000 + len(ME_DATA)])
  925. def testPackVga(self):
  926. """Test that an image with a VGA binary can be created"""
  927. data = self._DoReadFile('032_intel_vga.dts')
  928. self.assertEqual(VGA_DATA, data[:len(VGA_DATA)])
  929. def testPackStart16(self):
  930. """Test that an image with an x86 start16 region can be created"""
  931. data = self._DoReadFile('033_x86_start16.dts')
  932. self.assertEqual(X86_START16_DATA, data[:len(X86_START16_DATA)])
  933. def testPackPowerpcMpc85xxBootpgResetvec(self):
  934. """Test that an image with powerpc-mpc85xx-bootpg-resetvec can be
  935. created"""
  936. data = self._DoReadFile('150_powerpc_mpc85xx_bootpg_resetvec.dts')
  937. self.assertEqual(PPC_MPC85XX_BR_DATA, data[:len(PPC_MPC85XX_BR_DATA)])
  938. def _RunMicrocodeTest(self, dts_fname, nodtb_data, ucode_second=False):
  939. """Handle running a test for insertion of microcode
  940. Args:
  941. dts_fname: Name of test .dts file
  942. nodtb_data: Data that we expect in the first section
  943. ucode_second: True if the microsecond entry is second instead of
  944. third
  945. Returns:
  946. Tuple:
  947. Contents of first region (U-Boot or SPL)
  948. Offset and size components of microcode pointer, as inserted
  949. in the above (two 4-byte words)
  950. """
  951. data = self._DoReadFile(dts_fname, True)
  952. # Now check the device tree has no microcode
  953. if ucode_second:
  954. ucode_content = data[len(nodtb_data):]
  955. ucode_pos = len(nodtb_data)
  956. dtb_with_ucode = ucode_content[16:]
  957. fdt_len = self.GetFdtLen(dtb_with_ucode)
  958. else:
  959. dtb_with_ucode = data[len(nodtb_data):]
  960. fdt_len = self.GetFdtLen(dtb_with_ucode)
  961. ucode_content = dtb_with_ucode[fdt_len:]
  962. ucode_pos = len(nodtb_data) + fdt_len
  963. fname = tools.GetOutputFilename('test.dtb')
  964. with open(fname, 'wb') as fd:
  965. fd.write(dtb_with_ucode)
  966. dtb = fdt.FdtScan(fname)
  967. ucode = dtb.GetNode('/microcode')
  968. self.assertTrue(ucode)
  969. for node in ucode.subnodes:
  970. self.assertFalse(node.props.get('data'))
  971. # Check that the microcode appears immediately after the Fdt
  972. # This matches the concatenation of the data properties in
  973. # the /microcode/update@xxx nodes in 34_x86_ucode.dts.
  974. ucode_data = struct.pack('>4L', 0x12345678, 0x12345679, 0xabcd0000,
  975. 0x78235609)
  976. self.assertEqual(ucode_data, ucode_content[:len(ucode_data)])
  977. # Check that the microcode pointer was inserted. It should match the
  978. # expected offset and size
  979. pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
  980. len(ucode_data))
  981. u_boot = data[:len(nodtb_data)]
  982. return u_boot, pos_and_size
  983. def testPackUbootMicrocode(self):
  984. """Test that x86 microcode can be handled correctly
  985. We expect to see the following in the image, in order:
  986. u-boot-nodtb.bin with a microcode pointer inserted at the correct
  987. place
  988. u-boot.dtb with the microcode removed
  989. the microcode
  990. """
  991. first, pos_and_size = self._RunMicrocodeTest('034_x86_ucode.dts',
  992. U_BOOT_NODTB_DATA)
  993. self.assertEqual(b'nodtb with microcode' + pos_and_size +
  994. b' somewhere in here', first)
  995. def _RunPackUbootSingleMicrocode(self):
  996. """Test that x86 microcode can be handled correctly
  997. We expect to see the following in the image, in order:
  998. u-boot-nodtb.bin with a microcode pointer inserted at the correct
  999. place
  1000. u-boot.dtb with the microcode
  1001. an empty microcode region
  1002. """
  1003. # We need the libfdt library to run this test since only that allows
  1004. # finding the offset of a property. This is required by
  1005. # Entry_u_boot_dtb_with_ucode.ObtainContents().
  1006. data = self._DoReadFile('035_x86_single_ucode.dts', True)
  1007. second = data[len(U_BOOT_NODTB_DATA):]
  1008. fdt_len = self.GetFdtLen(second)
  1009. third = second[fdt_len:]
  1010. second = second[:fdt_len]
  1011. ucode_data = struct.pack('>2L', 0x12345678, 0x12345679)
  1012. self.assertIn(ucode_data, second)
  1013. ucode_pos = second.find(ucode_data) + len(U_BOOT_NODTB_DATA)
  1014. # Check that the microcode pointer was inserted. It should match the
  1015. # expected offset and size
  1016. pos_and_size = struct.pack('<2L', 0xfffffe00 + ucode_pos,
  1017. len(ucode_data))
  1018. first = data[:len(U_BOOT_NODTB_DATA)]
  1019. self.assertEqual(b'nodtb with microcode' + pos_and_size +
  1020. b' somewhere in here', first)
  1021. def testPackUbootSingleMicrocode(self):
  1022. """Test that x86 microcode can be handled correctly with fdt_normal.
  1023. """
  1024. self._RunPackUbootSingleMicrocode()
  1025. def testUBootImg(self):
  1026. """Test that u-boot.img can be put in a file"""
  1027. data = self._DoReadFile('036_u_boot_img.dts')
  1028. self.assertEqual(U_BOOT_IMG_DATA, data)
  1029. def testNoMicrocode(self):
  1030. """Test that a missing microcode region is detected"""
  1031. with self.assertRaises(ValueError) as e:
  1032. self._DoReadFile('037_x86_no_ucode.dts', True)
  1033. self.assertIn("Node '/binman/u-boot-dtb-with-ucode': No /microcode "
  1034. "node found in ", str(e.exception))
  1035. def testMicrocodeWithoutNode(self):
  1036. """Test that a missing u-boot-dtb-with-ucode node is detected"""
  1037. with self.assertRaises(ValueError) as e:
  1038. self._DoReadFile('038_x86_ucode_missing_node.dts', True)
  1039. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
  1040. "microcode region u-boot-dtb-with-ucode", str(e.exception))
  1041. def testMicrocodeWithoutNode2(self):
  1042. """Test that a missing u-boot-ucode node is detected"""
  1043. with self.assertRaises(ValueError) as e:
  1044. self._DoReadFile('039_x86_ucode_missing_node2.dts', True)
  1045. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot find "
  1046. "microcode region u-boot-ucode", str(e.exception))
  1047. def testMicrocodeWithoutPtrInElf(self):
  1048. """Test that a U-Boot binary without the microcode symbol is detected"""
  1049. # ELF file without a '_dt_ucode_base_size' symbol
  1050. try:
  1051. TestFunctional._MakeInputFile('u-boot',
  1052. tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr')))
  1053. with self.assertRaises(ValueError) as e:
  1054. self._RunPackUbootSingleMicrocode()
  1055. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Cannot locate "
  1056. "_dt_ucode_base_size symbol in u-boot", str(e.exception))
  1057. finally:
  1058. # Put the original file back
  1059. TestFunctional._MakeInputFile('u-boot',
  1060. tools.ReadFile(self.ElfTestFile('u_boot_ucode_ptr')))
  1061. def testMicrocodeNotInImage(self):
  1062. """Test that microcode must be placed within the image"""
  1063. with self.assertRaises(ValueError) as e:
  1064. self._DoReadFile('040_x86_ucode_not_in_image.dts', True)
  1065. self.assertIn("Node '/binman/u-boot-with-ucode-ptr': Microcode "
  1066. "pointer _dt_ucode_base_size at fffffe14 is outside the "
  1067. "section ranging from 00000000 to 0000002e", str(e.exception))
  1068. def testWithoutMicrocode(self):
  1069. """Test that we can cope with an image without microcode (e.g. qemu)"""
  1070. TestFunctional._MakeInputFile('u-boot',
  1071. tools.ReadFile(self.ElfTestFile('u_boot_no_ucode_ptr')))
  1072. data, dtb, _, _ = self._DoReadFileDtb('044_x86_optional_ucode.dts', True)
  1073. # Now check the device tree has no microcode
  1074. self.assertEqual(U_BOOT_NODTB_DATA, data[:len(U_BOOT_NODTB_DATA)])
  1075. second = data[len(U_BOOT_NODTB_DATA):]
  1076. fdt_len = self.GetFdtLen(second)
  1077. self.assertEqual(dtb, second[:fdt_len])
  1078. used_len = len(U_BOOT_NODTB_DATA) + fdt_len
  1079. third = data[used_len:]
  1080. self.assertEqual(tools.GetBytes(0, 0x200 - used_len), third)
  1081. def testUnknownPosSize(self):
  1082. """Test that microcode must be placed within the image"""
  1083. with self.assertRaises(ValueError) as e:
  1084. self._DoReadFile('041_unknown_pos_size.dts', True)
  1085. self.assertIn("Section '/binman': Unable to set offset/size for unknown "
  1086. "entry 'invalid-entry'", str(e.exception))
  1087. def testPackFsp(self):
  1088. """Test that an image with a FSP binary can be created"""
  1089. data = self._DoReadFile('042_intel_fsp.dts')
  1090. self.assertEqual(FSP_DATA, data[:len(FSP_DATA)])
  1091. def testPackCmc(self):
  1092. """Test that an image with a CMC binary can be created"""
  1093. data = self._DoReadFile('043_intel_cmc.dts')
  1094. self.assertEqual(CMC_DATA, data[:len(CMC_DATA)])
  1095. def testPackVbt(self):
  1096. """Test that an image with a VBT binary can be created"""
  1097. data = self._DoReadFile('046_intel_vbt.dts')
  1098. self.assertEqual(VBT_DATA, data[:len(VBT_DATA)])
  1099. def testSplBssPad(self):
  1100. """Test that we can pad SPL's BSS with zeros"""
  1101. # ELF file with a '__bss_size' symbol
  1102. self._SetupSplElf()
  1103. data = self._DoReadFile('047_spl_bss_pad.dts')
  1104. self.assertEqual(U_BOOT_SPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA,
  1105. data)
  1106. def testSplBssPadMissing(self):
  1107. """Test that a missing symbol is detected"""
  1108. self._SetupSplElf('u_boot_ucode_ptr')
  1109. with self.assertRaises(ValueError) as e:
  1110. self._DoReadFile('047_spl_bss_pad.dts')
  1111. self.assertIn('Expected __bss_size symbol in spl/u-boot-spl',
  1112. str(e.exception))
  1113. def testPackStart16Spl(self):
  1114. """Test that an image with an x86 start16 SPL region can be created"""
  1115. data = self._DoReadFile('048_x86_start16_spl.dts')
  1116. self.assertEqual(X86_START16_SPL_DATA, data[:len(X86_START16_SPL_DATA)])
  1117. def _PackUbootSplMicrocode(self, dts, ucode_second=False):
  1118. """Helper function for microcode tests
  1119. We expect to see the following in the image, in order:
  1120. u-boot-spl-nodtb.bin with a microcode pointer inserted at the
  1121. correct place
  1122. u-boot.dtb with the microcode removed
  1123. the microcode
  1124. Args:
  1125. dts: Device tree file to use for test
  1126. ucode_second: True if the microsecond entry is second instead of
  1127. third
  1128. """
  1129. self._SetupSplElf('u_boot_ucode_ptr')
  1130. first, pos_and_size = self._RunMicrocodeTest(dts, U_BOOT_SPL_NODTB_DATA,
  1131. ucode_second=ucode_second)
  1132. self.assertEqual(b'splnodtb with microc' + pos_and_size +
  1133. b'ter somewhere in here', first)
  1134. def testPackUbootSplMicrocode(self):
  1135. """Test that x86 microcode can be handled correctly in SPL"""
  1136. self._PackUbootSplMicrocode('049_x86_ucode_spl.dts')
  1137. def testPackUbootSplMicrocodeReorder(self):
  1138. """Test that order doesn't matter for microcode entries
  1139. This is the same as testPackUbootSplMicrocode but when we process the
  1140. u-boot-ucode entry we have not yet seen the u-boot-dtb-with-ucode
  1141. entry, so we reply on binman to try later.
  1142. """
  1143. self._PackUbootSplMicrocode('058_x86_ucode_spl_needs_retry.dts',
  1144. ucode_second=True)
  1145. def testPackMrc(self):
  1146. """Test that an image with an MRC binary can be created"""
  1147. data = self._DoReadFile('050_intel_mrc.dts')
  1148. self.assertEqual(MRC_DATA, data[:len(MRC_DATA)])
  1149. def testSplDtb(self):
  1150. """Test that an image with spl/u-boot-spl.dtb can be created"""
  1151. data = self._DoReadFile('051_u_boot_spl_dtb.dts')
  1152. self.assertEqual(U_BOOT_SPL_DTB_DATA, data[:len(U_BOOT_SPL_DTB_DATA)])
  1153. def testSplNoDtb(self):
  1154. """Test that an image with spl/u-boot-spl-nodtb.bin can be created"""
  1155. self._SetupSplElf()
  1156. data = self._DoReadFile('052_u_boot_spl_nodtb.dts')
  1157. self.assertEqual(U_BOOT_SPL_NODTB_DATA, data[:len(U_BOOT_SPL_NODTB_DATA)])
  1158. def checkSymbols(self, dts, base_data, u_boot_offset, entry_args=None,
  1159. use_expanded=False):
  1160. """Check the image contains the expected symbol values
  1161. Args:
  1162. dts: Device tree file to use for test
  1163. base_data: Data before and after 'u-boot' section
  1164. u_boot_offset: Offset of 'u-boot' section in image
  1165. entry_args: Dict of entry args to supply to binman
  1166. key: arg name
  1167. value: value of that arg
  1168. use_expanded: True to use expanded entries where available, e.g.
  1169. 'u-boot-expanded' instead of 'u-boot'
  1170. """
  1171. elf_fname = self.ElfTestFile('u_boot_binman_syms')
  1172. syms = elf.GetSymbols(elf_fname, ['binman', 'image'])
  1173. addr = elf.GetSymbolAddress(elf_fname, '__image_copy_start')
  1174. self.assertEqual(syms['_binman_u_boot_spl_any_prop_offset'].address,
  1175. addr)
  1176. self._SetupSplElf('u_boot_binman_syms')
  1177. data = self._DoReadFileDtb(dts, entry_args=entry_args,
  1178. use_expanded=use_expanded)[0]
  1179. # The image should contain the symbols from u_boot_binman_syms.c
  1180. # Note that image_pos is adjusted by the base address of the image,
  1181. # which is 0x10 in our test image
  1182. sym_values = struct.pack('<LQLL', 0x00,
  1183. u_boot_offset + len(U_BOOT_DATA),
  1184. 0x10 + u_boot_offset, 0x04)
  1185. expected = (sym_values + base_data[20:] +
  1186. tools.GetBytes(0xff, 1) + U_BOOT_DATA + sym_values +
  1187. base_data[20:])
  1188. self.assertEqual(expected, data)
  1189. def testSymbols(self):
  1190. """Test binman can assign symbols embedded in U-Boot"""
  1191. self.checkSymbols('053_symbols.dts', U_BOOT_SPL_DATA, 0x18)
  1192. def testSymbolsNoDtb(self):
  1193. """Test binman can assign symbols embedded in U-Boot SPL"""
  1194. self.checkSymbols('196_symbols_nodtb.dts',
  1195. U_BOOT_SPL_NODTB_DATA + U_BOOT_SPL_DTB_DATA,
  1196. 0x38)
  1197. def testPackUnitAddress(self):
  1198. """Test that we support multiple binaries with the same name"""
  1199. data = self._DoReadFile('054_unit_address.dts')
  1200. self.assertEqual(U_BOOT_DATA + U_BOOT_DATA, data)
  1201. def testSections(self):
  1202. """Basic test of sections"""
  1203. data = self._DoReadFile('055_sections.dts')
  1204. expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
  1205. U_BOOT_DATA + tools.GetBytes(ord('a'), 12) +
  1206. U_BOOT_DATA + tools.GetBytes(ord('&'), 4))
  1207. self.assertEqual(expected, data)
  1208. def testMap(self):
  1209. """Tests outputting a map of the images"""
  1210. _, _, map_data, _ = self._DoReadFileDtb('055_sections.dts', map=True)
  1211. self.assertEqual('''ImagePos Offset Size Name
  1212. 00000000 00000000 00000028 main-section
  1213. 00000000 00000000 00000010 section@0
  1214. 00000000 00000000 00000004 u-boot
  1215. 00000010 00000010 00000010 section@1
  1216. 00000010 00000000 00000004 u-boot
  1217. 00000020 00000020 00000004 section@2
  1218. 00000020 00000000 00000004 u-boot
  1219. ''', map_data)
  1220. def testNamePrefix(self):
  1221. """Tests that name prefixes are used"""
  1222. _, _, map_data, _ = self._DoReadFileDtb('056_name_prefix.dts', map=True)
  1223. self.assertEqual('''ImagePos Offset Size Name
  1224. 00000000 00000000 00000028 main-section
  1225. 00000000 00000000 00000010 section@0
  1226. 00000000 00000000 00000004 ro-u-boot
  1227. 00000010 00000010 00000010 section@1
  1228. 00000010 00000000 00000004 rw-u-boot
  1229. ''', map_data)
  1230. def testUnknownContents(self):
  1231. """Test that obtaining the contents works as expected"""
  1232. with self.assertRaises(ValueError) as e:
  1233. self._DoReadFile('057_unknown_contents.dts', True)
  1234. self.assertIn("Image '/binman': Internal error: Could not complete "
  1235. "processing of contents: remaining ["
  1236. "<binman.etype._testing.Entry__testing ", str(e.exception))
  1237. def testBadChangeSize(self):
  1238. """Test that trying to change the size of an entry fails"""
  1239. try:
  1240. state.SetAllowEntryExpansion(False)
  1241. with self.assertRaises(ValueError) as e:
  1242. self._DoReadFile('059_change_size.dts', True)
  1243. self.assertIn("Node '/binman/_testing': Cannot update entry size from 2 to 3",
  1244. str(e.exception))
  1245. finally:
  1246. state.SetAllowEntryExpansion(True)
  1247. def testUpdateFdt(self):
  1248. """Test that we can update the device tree with offset/size info"""
  1249. _, _, _, out_dtb_fname = self._DoReadFileDtb('060_fdt_update.dts',
  1250. update_dtb=True)
  1251. dtb = fdt.Fdt(out_dtb_fname)
  1252. dtb.Scan()
  1253. props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS)
  1254. self.assertEqual({
  1255. 'image-pos': 0,
  1256. 'offset': 0,
  1257. '_testing:offset': 32,
  1258. '_testing:size': 2,
  1259. '_testing:image-pos': 32,
  1260. 'section@0/u-boot:offset': 0,
  1261. 'section@0/u-boot:size': len(U_BOOT_DATA),
  1262. 'section@0/u-boot:image-pos': 0,
  1263. 'section@0:offset': 0,
  1264. 'section@0:size': 16,
  1265. 'section@0:image-pos': 0,
  1266. 'section@1/u-boot:offset': 0,
  1267. 'section@1/u-boot:size': len(U_BOOT_DATA),
  1268. 'section@1/u-boot:image-pos': 16,
  1269. 'section@1:offset': 16,
  1270. 'section@1:size': 16,
  1271. 'section@1:image-pos': 16,
  1272. 'size': 40
  1273. }, props)
  1274. def testUpdateFdtBad(self):
  1275. """Test that we detect when ProcessFdt never completes"""
  1276. with self.assertRaises(ValueError) as e:
  1277. self._DoReadFileDtb('061_fdt_update_bad.dts', update_dtb=True)
  1278. self.assertIn('Could not complete processing of Fdt: remaining '
  1279. '[<binman.etype._testing.Entry__testing',
  1280. str(e.exception))
  1281. def testEntryArgs(self):
  1282. """Test passing arguments to entries from the command line"""
  1283. entry_args = {
  1284. 'test-str-arg': 'test1',
  1285. 'test-int-arg': '456',
  1286. }
  1287. self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
  1288. self.assertIn('image', control.images)
  1289. entry = control.images['image'].GetEntries()['_testing']
  1290. self.assertEqual('test0', entry.test_str_fdt)
  1291. self.assertEqual('test1', entry.test_str_arg)
  1292. self.assertEqual(123, entry.test_int_fdt)
  1293. self.assertEqual(456, entry.test_int_arg)
  1294. def testEntryArgsMissing(self):
  1295. """Test missing arguments and properties"""
  1296. entry_args = {
  1297. 'test-int-arg': '456',
  1298. }
  1299. self._DoReadFileDtb('063_entry_args_missing.dts', entry_args=entry_args)
  1300. entry = control.images['image'].GetEntries()['_testing']
  1301. self.assertEqual('test0', entry.test_str_fdt)
  1302. self.assertEqual(None, entry.test_str_arg)
  1303. self.assertEqual(None, entry.test_int_fdt)
  1304. self.assertEqual(456, entry.test_int_arg)
  1305. def testEntryArgsRequired(self):
  1306. """Test missing arguments and properties"""
  1307. entry_args = {
  1308. 'test-int-arg': '456',
  1309. }
  1310. with self.assertRaises(ValueError) as e:
  1311. self._DoReadFileDtb('064_entry_args_required.dts')
  1312. self.assertIn("Node '/binman/_testing': "
  1313. 'Missing required properties/entry args: test-str-arg, '
  1314. 'test-int-fdt, test-int-arg',
  1315. str(e.exception))
  1316. def testEntryArgsInvalidFormat(self):
  1317. """Test that an invalid entry-argument format is detected"""
  1318. args = ['build', '-d', self.TestFile('064_entry_args_required.dts'),
  1319. '-ano-value']
  1320. with self.assertRaises(ValueError) as e:
  1321. self._DoBinman(*args)
  1322. self.assertIn("Invalid entry arguemnt 'no-value'", str(e.exception))
  1323. def testEntryArgsInvalidInteger(self):
  1324. """Test that an invalid entry-argument integer is detected"""
  1325. entry_args = {
  1326. 'test-int-arg': 'abc',
  1327. }
  1328. with self.assertRaises(ValueError) as e:
  1329. self._DoReadFileDtb('062_entry_args.dts', entry_args=entry_args)
  1330. self.assertIn("Node '/binman/_testing': Cannot convert entry arg "
  1331. "'test-int-arg' (value 'abc') to integer",
  1332. str(e.exception))
  1333. def testEntryArgsInvalidDatatype(self):
  1334. """Test that an invalid entry-argument datatype is detected
  1335. This test could be written in entry_test.py except that it needs
  1336. access to control.entry_args, which seems more than that module should
  1337. be able to see.
  1338. """
  1339. entry_args = {
  1340. 'test-bad-datatype-arg': '12',
  1341. }
  1342. with self.assertRaises(ValueError) as e:
  1343. self._DoReadFileDtb('065_entry_args_unknown_datatype.dts',
  1344. entry_args=entry_args)
  1345. self.assertIn('GetArg() internal error: Unknown data type ',
  1346. str(e.exception))
  1347. def testText(self):
  1348. """Test for a text entry type"""
  1349. entry_args = {
  1350. 'test-id': TEXT_DATA,
  1351. 'test-id2': TEXT_DATA2,
  1352. 'test-id3': TEXT_DATA3,
  1353. }
  1354. data, _, _, _ = self._DoReadFileDtb('066_text.dts',
  1355. entry_args=entry_args)
  1356. expected = (tools.ToBytes(TEXT_DATA) +
  1357. tools.GetBytes(0, 8 - len(TEXT_DATA)) +
  1358. tools.ToBytes(TEXT_DATA2) + tools.ToBytes(TEXT_DATA3) +
  1359. b'some text' + b'more text')
  1360. self.assertEqual(expected, data)
  1361. def testEntryDocs(self):
  1362. """Test for creation of entry documentation"""
  1363. with test_util.capture_sys_output() as (stdout, stderr):
  1364. control.WriteEntryDocs(control.GetEntryModules())
  1365. self.assertTrue(len(stdout.getvalue()) > 0)
  1366. def testEntryDocsMissing(self):
  1367. """Test handling of missing entry documentation"""
  1368. with self.assertRaises(ValueError) as e:
  1369. with test_util.capture_sys_output() as (stdout, stderr):
  1370. control.WriteEntryDocs(control.GetEntryModules(), 'u_boot')
  1371. self.assertIn('Documentation is missing for modules: u_boot',
  1372. str(e.exception))
  1373. def testFmap(self):
  1374. """Basic test of generation of a flashrom fmap"""
  1375. data = self._DoReadFile('067_fmap.dts')
  1376. fhdr, fentries = fmap_util.DecodeFmap(data[32:])
  1377. expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
  1378. U_BOOT_DATA + tools.GetBytes(ord('a'), 12))
  1379. self.assertEqual(expected, data[:32])
  1380. self.assertEqual(b'__FMAP__', fhdr.signature)
  1381. self.assertEqual(1, fhdr.ver_major)
  1382. self.assertEqual(0, fhdr.ver_minor)
  1383. self.assertEqual(0, fhdr.base)
  1384. expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 5
  1385. self.assertEqual(16 + 16 + expect_size, fhdr.image_size)
  1386. self.assertEqual(b'FMAP', fhdr.name)
  1387. self.assertEqual(5, fhdr.nareas)
  1388. fiter = iter(fentries)
  1389. fentry = next(fiter)
  1390. self.assertEqual(b'SECTION0', fentry.name)
  1391. self.assertEqual(0, fentry.offset)
  1392. self.assertEqual(16, fentry.size)
  1393. self.assertEqual(0, fentry.flags)
  1394. fentry = next(fiter)
  1395. self.assertEqual(b'RO_U_BOOT', fentry.name)
  1396. self.assertEqual(0, fentry.offset)
  1397. self.assertEqual(4, fentry.size)
  1398. self.assertEqual(0, fentry.flags)
  1399. fentry = next(fiter)
  1400. self.assertEqual(b'SECTION1', fentry.name)
  1401. self.assertEqual(16, fentry.offset)
  1402. self.assertEqual(16, fentry.size)
  1403. self.assertEqual(0, fentry.flags)
  1404. fentry = next(fiter)
  1405. self.assertEqual(b'RW_U_BOOT', fentry.name)
  1406. self.assertEqual(16, fentry.offset)
  1407. self.assertEqual(4, fentry.size)
  1408. self.assertEqual(0, fentry.flags)
  1409. fentry = next(fiter)
  1410. self.assertEqual(b'FMAP', fentry.name)
  1411. self.assertEqual(32, fentry.offset)
  1412. self.assertEqual(expect_size, fentry.size)
  1413. self.assertEqual(0, fentry.flags)
  1414. def testBlobNamedByArg(self):
  1415. """Test we can add a blob with the filename coming from an entry arg"""
  1416. entry_args = {
  1417. 'cros-ec-rw-path': 'ecrw.bin',
  1418. }
  1419. self._DoReadFileDtb('068_blob_named_by_arg.dts', entry_args=entry_args)
  1420. def testFill(self):
  1421. """Test for an fill entry type"""
  1422. data = self._DoReadFile('069_fill.dts')
  1423. expected = tools.GetBytes(0xff, 8) + tools.GetBytes(0, 8)
  1424. self.assertEqual(expected, data)
  1425. def testFillNoSize(self):
  1426. """Test for an fill entry type with no size"""
  1427. with self.assertRaises(ValueError) as e:
  1428. self._DoReadFile('070_fill_no_size.dts')
  1429. self.assertIn("'fill' entry must have a size property",
  1430. str(e.exception))
  1431. def _HandleGbbCommand(self, pipe_list):
  1432. """Fake calls to the futility utility"""
  1433. if pipe_list[0][0] == 'futility':
  1434. fname = pipe_list[0][-1]
  1435. # Append our GBB data to the file, which will happen every time the
  1436. # futility command is called.
  1437. with open(fname, 'ab') as fd:
  1438. fd.write(GBB_DATA)
  1439. return command.CommandResult()
  1440. def testGbb(self):
  1441. """Test for the Chromium OS Google Binary Block"""
  1442. command.test_result = self._HandleGbbCommand
  1443. entry_args = {
  1444. 'keydir': 'devkeys',
  1445. 'bmpblk': 'bmpblk.bin',
  1446. }
  1447. data, _, _, _ = self._DoReadFileDtb('071_gbb.dts', entry_args=entry_args)
  1448. # Since futility
  1449. expected = (GBB_DATA + GBB_DATA + tools.GetBytes(0, 8) +
  1450. tools.GetBytes(0, 0x2180 - 16))
  1451. self.assertEqual(expected, data)
  1452. def testGbbTooSmall(self):
  1453. """Test for the Chromium OS Google Binary Block being large enough"""
  1454. with self.assertRaises(ValueError) as e:
  1455. self._DoReadFileDtb('072_gbb_too_small.dts')
  1456. self.assertIn("Node '/binman/gbb': GBB is too small",
  1457. str(e.exception))
  1458. def testGbbNoSize(self):
  1459. """Test for the Chromium OS Google Binary Block having a size"""
  1460. with self.assertRaises(ValueError) as e:
  1461. self._DoReadFileDtb('073_gbb_no_size.dts')
  1462. self.assertIn("Node '/binman/gbb': GBB must have a fixed size",
  1463. str(e.exception))
  1464. def _HandleVblockCommand(self, pipe_list):
  1465. """Fake calls to the futility utility
  1466. The expected pipe is:
  1467. [('futility', 'vbutil_firmware', '--vblock',
  1468. 'vblock.vblock', '--keyblock', 'devkeys/firmware.keyblock',
  1469. '--signprivate', 'devkeys/firmware_data_key.vbprivk',
  1470. '--version', '1', '--fv', 'input.vblock', '--kernelkey',
  1471. 'devkeys/kernel_subkey.vbpubk', '--flags', '1')]
  1472. This writes to the output file (here, 'vblock.vblock'). If
  1473. self._hash_data is False, it writes VBLOCK_DATA, else it writes a hash
  1474. of the input data (here, 'input.vblock').
  1475. """
  1476. if pipe_list[0][0] == 'futility':
  1477. fname = pipe_list[0][3]
  1478. with open(fname, 'wb') as fd:
  1479. if self._hash_data:
  1480. infile = pipe_list[0][11]
  1481. m = hashlib.sha256()
  1482. data = tools.ReadFile(infile)
  1483. m.update(data)
  1484. fd.write(m.digest())
  1485. else:
  1486. fd.write(VBLOCK_DATA)
  1487. return command.CommandResult()
  1488. def testVblock(self):
  1489. """Test for the Chromium OS Verified Boot Block"""
  1490. self._hash_data = False
  1491. command.test_result = self._HandleVblockCommand
  1492. entry_args = {
  1493. 'keydir': 'devkeys',
  1494. }
  1495. data, _, _, _ = self._DoReadFileDtb('074_vblock.dts',
  1496. entry_args=entry_args)
  1497. expected = U_BOOT_DATA + VBLOCK_DATA + U_BOOT_DTB_DATA
  1498. self.assertEqual(expected, data)
  1499. def testVblockNoContent(self):
  1500. """Test we detect a vblock which has no content to sign"""
  1501. with self.assertRaises(ValueError) as e:
  1502. self._DoReadFile('075_vblock_no_content.dts')
  1503. self.assertIn("Node '/binman/vblock': Collection must have a 'content' "
  1504. 'property', str(e.exception))
  1505. def testVblockBadPhandle(self):
  1506. """Test that we detect a vblock with an invalid phandle in contents"""
  1507. with self.assertRaises(ValueError) as e:
  1508. self._DoReadFile('076_vblock_bad_phandle.dts')
  1509. self.assertIn("Node '/binman/vblock': Cannot find node for phandle "
  1510. '1000', str(e.exception))
  1511. def testVblockBadEntry(self):
  1512. """Test that we detect an entry that points to a non-entry"""
  1513. with self.assertRaises(ValueError) as e:
  1514. self._DoReadFile('077_vblock_bad_entry.dts')
  1515. self.assertIn("Node '/binman/vblock': Cannot find entry for node "
  1516. "'other'", str(e.exception))
  1517. def testVblockContent(self):
  1518. """Test that the vblock signs the right data"""
  1519. self._hash_data = True
  1520. command.test_result = self._HandleVblockCommand
  1521. entry_args = {
  1522. 'keydir': 'devkeys',
  1523. }
  1524. data = self._DoReadFileDtb(
  1525. '189_vblock_content.dts', use_real_dtb=True, update_dtb=True,
  1526. entry_args=entry_args)[0]
  1527. hashlen = 32 # SHA256 hash is 32 bytes
  1528. self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
  1529. hashval = data[-hashlen:]
  1530. dtb = data[len(U_BOOT_DATA):-hashlen]
  1531. expected_data = U_BOOT_DATA + dtb
  1532. # The hashval should be a hash of the dtb
  1533. m = hashlib.sha256()
  1534. m.update(expected_data)
  1535. expected_hashval = m.digest()
  1536. self.assertEqual(expected_hashval, hashval)
  1537. def testTpl(self):
  1538. """Test that an image with TPL and its device tree can be created"""
  1539. # ELF file with a '__bss_size' symbol
  1540. self._SetupTplElf()
  1541. data = self._DoReadFile('078_u_boot_tpl.dts')
  1542. self.assertEqual(U_BOOT_TPL_DATA + U_BOOT_TPL_DTB_DATA, data)
  1543. def testUsesPos(self):
  1544. """Test that the 'pos' property cannot be used anymore"""
  1545. with self.assertRaises(ValueError) as e:
  1546. data = self._DoReadFile('079_uses_pos.dts')
  1547. self.assertIn("Node '/binman/u-boot': Please use 'offset' instead of "
  1548. "'pos'", str(e.exception))
  1549. def testFillZero(self):
  1550. """Test for an fill entry type with a size of 0"""
  1551. data = self._DoReadFile('080_fill_empty.dts')
  1552. self.assertEqual(tools.GetBytes(0, 16), data)
  1553. def testTextMissing(self):
  1554. """Test for a text entry type where there is no text"""
  1555. with self.assertRaises(ValueError) as e:
  1556. self._DoReadFileDtb('066_text.dts',)
  1557. self.assertIn("Node '/binman/text': No value provided for text label "
  1558. "'test-id'", str(e.exception))
  1559. def testPackStart16Tpl(self):
  1560. """Test that an image with an x86 start16 TPL region can be created"""
  1561. data = self._DoReadFile('081_x86_start16_tpl.dts')
  1562. self.assertEqual(X86_START16_TPL_DATA, data[:len(X86_START16_TPL_DATA)])
  1563. def testSelectImage(self):
  1564. """Test that we can select which images to build"""
  1565. expected = 'Skipping images: image1'
  1566. # We should only get the expected message in verbose mode
  1567. for verbosity in (0, 2):
  1568. with test_util.capture_sys_output() as (stdout, stderr):
  1569. retcode = self._DoTestFile('006_dual_image.dts',
  1570. verbosity=verbosity,
  1571. images=['image2'])
  1572. self.assertEqual(0, retcode)
  1573. if verbosity:
  1574. self.assertIn(expected, stdout.getvalue())
  1575. else:
  1576. self.assertNotIn(expected, stdout.getvalue())
  1577. self.assertFalse(os.path.exists(tools.GetOutputFilename('image1.bin')))
  1578. self.assertTrue(os.path.exists(tools.GetOutputFilename('image2.bin')))
  1579. self._CleanupOutputDir()
  1580. def testUpdateFdtAll(self):
  1581. """Test that all device trees are updated with offset/size info"""
  1582. data = self._DoReadFileRealDtb('082_fdt_update_all.dts')
  1583. base_expected = {
  1584. 'section:image-pos': 0,
  1585. 'u-boot-tpl-dtb:size': 513,
  1586. 'u-boot-spl-dtb:size': 513,
  1587. 'u-boot-spl-dtb:offset': 493,
  1588. 'image-pos': 0,
  1589. 'section/u-boot-dtb:image-pos': 0,
  1590. 'u-boot-spl-dtb:image-pos': 493,
  1591. 'section/u-boot-dtb:size': 493,
  1592. 'u-boot-tpl-dtb:image-pos': 1006,
  1593. 'section/u-boot-dtb:offset': 0,
  1594. 'section:size': 493,
  1595. 'offset': 0,
  1596. 'section:offset': 0,
  1597. 'u-boot-tpl-dtb:offset': 1006,
  1598. 'size': 1519
  1599. }
  1600. # We expect three device-tree files in the output, one after the other.
  1601. # Read them in sequence. We look for an 'spl' property in the SPL tree,
  1602. # and 'tpl' in the TPL tree, to make sure they are distinct from the
  1603. # main U-Boot tree. All three should have the same postions and offset.
  1604. start = 0
  1605. for item in ['', 'spl', 'tpl']:
  1606. dtb = fdt.Fdt.FromData(data[start:])
  1607. dtb.Scan()
  1608. props = self._GetPropTree(dtb, BASE_DTB_PROPS + REPACK_DTB_PROPS +
  1609. ['spl', 'tpl'])
  1610. expected = dict(base_expected)
  1611. if item:
  1612. expected[item] = 0
  1613. self.assertEqual(expected, props)
  1614. start += dtb._fdt_obj.totalsize()
  1615. def testUpdateFdtOutput(self):
  1616. """Test that output DTB files are updated"""
  1617. try:
  1618. data, dtb_data, _, _ = self._DoReadFileDtb('082_fdt_update_all.dts',
  1619. use_real_dtb=True, update_dtb=True, reset_dtbs=False)
  1620. # Unfortunately, compiling a source file always results in a file
  1621. # called source.dtb (see fdt_util.EnsureCompiled()). The test
  1622. # source file (e.g. test/075_fdt_update_all.dts) thus does not enter
  1623. # binman as a file called u-boot.dtb. To fix this, copy the file
  1624. # over to the expected place.
  1625. start = 0
  1626. for fname in ['u-boot.dtb.out', 'spl/u-boot-spl.dtb.out',
  1627. 'tpl/u-boot-tpl.dtb.out']:
  1628. dtb = fdt.Fdt.FromData(data[start:])
  1629. size = dtb._fdt_obj.totalsize()
  1630. pathname = tools.GetOutputFilename(os.path.split(fname)[1])
  1631. outdata = tools.ReadFile(pathname)
  1632. name = os.path.split(fname)[0]
  1633. if name:
  1634. orig_indata = self._GetDtbContentsForSplTpl(dtb_data, name)
  1635. else:
  1636. orig_indata = dtb_data
  1637. self.assertNotEqual(outdata, orig_indata,
  1638. "Expected output file '%s' be updated" % pathname)
  1639. self.assertEqual(outdata, data[start:start + size],
  1640. "Expected output file '%s' to match output image" %
  1641. pathname)
  1642. start += size
  1643. finally:
  1644. self._ResetDtbs()
  1645. def _decompress(self, data):
  1646. return tools.Decompress(data, 'lz4')
  1647. def testCompress(self):
  1648. """Test compression of blobs"""
  1649. self._CheckLz4()
  1650. data, _, _, out_dtb_fname = self._DoReadFileDtb('083_compress.dts',
  1651. use_real_dtb=True, update_dtb=True)
  1652. dtb = fdt.Fdt(out_dtb_fname)
  1653. dtb.Scan()
  1654. props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
  1655. orig = self._decompress(data)
  1656. self.assertEquals(COMPRESS_DATA, orig)
  1657. # Do a sanity check on various fields
  1658. image = control.images['image']
  1659. entries = image.GetEntries()
  1660. self.assertEqual(1, len(entries))
  1661. entry = entries['blob']
  1662. self.assertEqual(COMPRESS_DATA, entry.uncomp_data)
  1663. self.assertEqual(len(COMPRESS_DATA), entry.uncomp_size)
  1664. orig = self._decompress(entry.data)
  1665. self.assertEqual(orig, entry.uncomp_data)
  1666. self.assertEqual(image.data, entry.data)
  1667. expected = {
  1668. 'blob:uncomp-size': len(COMPRESS_DATA),
  1669. 'blob:size': len(data),
  1670. 'size': len(data),
  1671. }
  1672. self.assertEqual(expected, props)
  1673. def testFiles(self):
  1674. """Test bringing in multiple files"""
  1675. data = self._DoReadFile('084_files.dts')
  1676. self.assertEqual(FILES_DATA, data)
  1677. def testFilesCompress(self):
  1678. """Test bringing in multiple files and compressing them"""
  1679. self._CheckLz4()
  1680. data = self._DoReadFile('085_files_compress.dts')
  1681. image = control.images['image']
  1682. entries = image.GetEntries()
  1683. files = entries['files']
  1684. entries = files._entries
  1685. orig = b''
  1686. for i in range(1, 3):
  1687. key = '%d.dat' % i
  1688. start = entries[key].image_pos
  1689. len = entries[key].size
  1690. chunk = data[start:start + len]
  1691. orig += self._decompress(chunk)
  1692. self.assertEqual(FILES_DATA, orig)
  1693. def testFilesMissing(self):
  1694. """Test missing files"""
  1695. with self.assertRaises(ValueError) as e:
  1696. data = self._DoReadFile('086_files_none.dts')
  1697. self.assertIn("Node '/binman/files': Pattern \'files/*.none\' matched "
  1698. 'no files', str(e.exception))
  1699. def testFilesNoPattern(self):
  1700. """Test missing files"""
  1701. with self.assertRaises(ValueError) as e:
  1702. data = self._DoReadFile('087_files_no_pattern.dts')
  1703. self.assertIn("Node '/binman/files': Missing 'pattern' property",
  1704. str(e.exception))
  1705. def testExpandSize(self):
  1706. """Test an expanding entry"""
  1707. data, _, map_data, _ = self._DoReadFileDtb('088_expand_size.dts',
  1708. map=True)
  1709. expect = (tools.GetBytes(ord('a'), 8) + U_BOOT_DATA +
  1710. MRC_DATA + tools.GetBytes(ord('b'), 1) + U_BOOT_DATA +
  1711. tools.GetBytes(ord('c'), 8) + U_BOOT_DATA +
  1712. tools.GetBytes(ord('d'), 8))
  1713. self.assertEqual(expect, data)
  1714. self.assertEqual('''ImagePos Offset Size Name
  1715. 00000000 00000000 00000028 main-section
  1716. 00000000 00000000 00000008 fill
  1717. 00000008 00000008 00000004 u-boot
  1718. 0000000c 0000000c 00000004 section
  1719. 0000000c 00000000 00000003 intel-mrc
  1720. 00000010 00000010 00000004 u-boot2
  1721. 00000014 00000014 0000000c section2
  1722. 00000014 00000000 00000008 fill
  1723. 0000001c 00000008 00000004 u-boot
  1724. 00000020 00000020 00000008 fill2
  1725. ''', map_data)
  1726. def testExpandSizeBad(self):
  1727. """Test an expanding entry which fails to provide contents"""
  1728. with test_util.capture_sys_output() as (stdout, stderr):
  1729. with self.assertRaises(ValueError) as e:
  1730. self._DoReadFileDtb('089_expand_size_bad.dts', map=True)
  1731. self.assertIn("Node '/binman/_testing': Cannot obtain contents when "
  1732. 'expanding entry', str(e.exception))
  1733. def testHash(self):
  1734. """Test hashing of the contents of an entry"""
  1735. _, _, _, out_dtb_fname = self._DoReadFileDtb('090_hash.dts',
  1736. use_real_dtb=True, update_dtb=True)
  1737. dtb = fdt.Fdt(out_dtb_fname)
  1738. dtb.Scan()
  1739. hash_node = dtb.GetNode('/binman/u-boot/hash').props['value']
  1740. m = hashlib.sha256()
  1741. m.update(U_BOOT_DATA)
  1742. self.assertEqual(m.digest(), b''.join(hash_node.value))
  1743. def testHashNoAlgo(self):
  1744. with self.assertRaises(ValueError) as e:
  1745. self._DoReadFileDtb('091_hash_no_algo.dts', update_dtb=True)
  1746. self.assertIn("Node \'/binman/u-boot\': Missing \'algo\' property for "
  1747. 'hash node', str(e.exception))
  1748. def testHashBadAlgo(self):
  1749. with self.assertRaises(ValueError) as e:
  1750. self._DoReadFileDtb('092_hash_bad_algo.dts', update_dtb=True)
  1751. self.assertIn("Node '/binman/u-boot': Unknown hash algorithm",
  1752. str(e.exception))
  1753. def testHashSection(self):
  1754. """Test hashing of the contents of an entry"""
  1755. _, _, _, out_dtb_fname = self._DoReadFileDtb('099_hash_section.dts',
  1756. use_real_dtb=True, update_dtb=True)
  1757. dtb = fdt.Fdt(out_dtb_fname)
  1758. dtb.Scan()
  1759. hash_node = dtb.GetNode('/binman/section/hash').props['value']
  1760. m = hashlib.sha256()
  1761. m.update(U_BOOT_DATA)
  1762. m.update(tools.GetBytes(ord('a'), 16))
  1763. self.assertEqual(m.digest(), b''.join(hash_node.value))
  1764. def testPackUBootTplMicrocode(self):
  1765. """Test that x86 microcode can be handled correctly in TPL
  1766. We expect to see the following in the image, in order:
  1767. u-boot-tpl-nodtb.bin with a microcode pointer inserted at the correct
  1768. place
  1769. u-boot-tpl.dtb with the microcode removed
  1770. the microcode
  1771. """
  1772. self._SetupTplElf('u_boot_ucode_ptr')
  1773. first, pos_and_size = self._RunMicrocodeTest('093_x86_tpl_ucode.dts',
  1774. U_BOOT_TPL_NODTB_DATA)
  1775. self.assertEqual(b'tplnodtb with microc' + pos_and_size +
  1776. b'ter somewhere in here', first)
  1777. def testFmapX86(self):
  1778. """Basic test of generation of a flashrom fmap"""
  1779. data = self._DoReadFile('094_fmap_x86.dts')
  1780. fhdr, fentries = fmap_util.DecodeFmap(data[32:])
  1781. expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('a'), 32 - 7)
  1782. self.assertEqual(expected, data[:32])
  1783. fhdr, fentries = fmap_util.DecodeFmap(data[32:])
  1784. self.assertEqual(0x100, fhdr.image_size)
  1785. self.assertEqual(0, fentries[0].offset)
  1786. self.assertEqual(4, fentries[0].size)
  1787. self.assertEqual(b'U_BOOT', fentries[0].name)
  1788. self.assertEqual(4, fentries[1].offset)
  1789. self.assertEqual(3, fentries[1].size)
  1790. self.assertEqual(b'INTEL_MRC', fentries[1].name)
  1791. self.assertEqual(32, fentries[2].offset)
  1792. self.assertEqual(fmap_util.FMAP_HEADER_LEN +
  1793. fmap_util.FMAP_AREA_LEN * 3, fentries[2].size)
  1794. self.assertEqual(b'FMAP', fentries[2].name)
  1795. def testFmapX86Section(self):
  1796. """Basic test of generation of a flashrom fmap"""
  1797. data = self._DoReadFile('095_fmap_x86_section.dts')
  1798. expected = U_BOOT_DATA + MRC_DATA + tools.GetBytes(ord('b'), 32 - 7)
  1799. self.assertEqual(expected, data[:32])
  1800. fhdr, fentries = fmap_util.DecodeFmap(data[36:])
  1801. self.assertEqual(0x180, fhdr.image_size)
  1802. expect_size = fmap_util.FMAP_HEADER_LEN + fmap_util.FMAP_AREA_LEN * 4
  1803. fiter = iter(fentries)
  1804. fentry = next(fiter)
  1805. self.assertEqual(b'U_BOOT', fentry.name)
  1806. self.assertEqual(0, fentry.offset)
  1807. self.assertEqual(4, fentry.size)
  1808. fentry = next(fiter)
  1809. self.assertEqual(b'SECTION', fentry.name)
  1810. self.assertEqual(4, fentry.offset)
  1811. self.assertEqual(0x20 + expect_size, fentry.size)
  1812. fentry = next(fiter)
  1813. self.assertEqual(b'INTEL_MRC', fentry.name)
  1814. self.assertEqual(4, fentry.offset)
  1815. self.assertEqual(3, fentry.size)
  1816. fentry = next(fiter)
  1817. self.assertEqual(b'FMAP', fentry.name)
  1818. self.assertEqual(36, fentry.offset)
  1819. self.assertEqual(expect_size, fentry.size)
  1820. def testElf(self):
  1821. """Basic test of ELF entries"""
  1822. self._SetupSplElf()
  1823. self._SetupTplElf()
  1824. with open(self.ElfTestFile('bss_data'), 'rb') as fd:
  1825. TestFunctional._MakeInputFile('-boot', fd.read())
  1826. data = self._DoReadFile('096_elf.dts')
  1827. def testElfStrip(self):
  1828. """Basic test of ELF entries"""
  1829. self._SetupSplElf()
  1830. with open(self.ElfTestFile('bss_data'), 'rb') as fd:
  1831. TestFunctional._MakeInputFile('-boot', fd.read())
  1832. data = self._DoReadFile('097_elf_strip.dts')
  1833. def testPackOverlapMap(self):
  1834. """Test that overlapping regions are detected"""
  1835. with test_util.capture_sys_output() as (stdout, stderr):
  1836. with self.assertRaises(ValueError) as e:
  1837. self._DoTestFile('014_pack_overlap.dts', map=True)
  1838. map_fname = tools.GetOutputFilename('image.map')
  1839. self.assertEqual("Wrote map file '%s' to show errors\n" % map_fname,
  1840. stdout.getvalue())
  1841. # We should not get an inmage, but there should be a map file
  1842. self.assertFalse(os.path.exists(tools.GetOutputFilename('image.bin')))
  1843. self.assertTrue(os.path.exists(map_fname))
  1844. map_data = tools.ReadFile(map_fname, binary=False)
  1845. self.assertEqual('''ImagePos Offset Size Name
  1846. <none> 00000000 00000008 main-section
  1847. <none> 00000000 00000004 u-boot
  1848. <none> 00000003 00000004 u-boot-align
  1849. ''', map_data)
  1850. def testPackRefCode(self):
  1851. """Test that an image with an Intel Reference code binary works"""
  1852. data = self._DoReadFile('100_intel_refcode.dts')
  1853. self.assertEqual(REFCODE_DATA, data[:len(REFCODE_DATA)])
  1854. def testSectionOffset(self):
  1855. """Tests use of a section with an offset"""
  1856. data, _, map_data, _ = self._DoReadFileDtb('101_sections_offset.dts',
  1857. map=True)
  1858. self.assertEqual('''ImagePos Offset Size Name
  1859. 00000000 00000000 00000038 main-section
  1860. 00000004 00000004 00000010 section@0
  1861. 00000004 00000000 00000004 u-boot
  1862. 00000018 00000018 00000010 section@1
  1863. 00000018 00000000 00000004 u-boot
  1864. 0000002c 0000002c 00000004 section@2
  1865. 0000002c 00000000 00000004 u-boot
  1866. ''', map_data)
  1867. self.assertEqual(data,
  1868. tools.GetBytes(0x26, 4) + U_BOOT_DATA +
  1869. tools.GetBytes(0x21, 12) +
  1870. tools.GetBytes(0x26, 4) + U_BOOT_DATA +
  1871. tools.GetBytes(0x61, 12) +
  1872. tools.GetBytes(0x26, 4) + U_BOOT_DATA +
  1873. tools.GetBytes(0x26, 8))
  1874. def testCbfsRaw(self):
  1875. """Test base handling of a Coreboot Filesystem (CBFS)
  1876. The exact contents of the CBFS is verified by similar tests in
  1877. cbfs_util_test.py. The tests here merely check that the files added to
  1878. the CBFS can be found in the final image.
  1879. """
  1880. data = self._DoReadFile('102_cbfs_raw.dts')
  1881. size = 0xb0
  1882. cbfs = cbfs_util.CbfsReader(data)
  1883. self.assertEqual(size, cbfs.rom_size)
  1884. self.assertIn('u-boot-dtb', cbfs.files)
  1885. cfile = cbfs.files['u-boot-dtb']
  1886. self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
  1887. def testCbfsArch(self):
  1888. """Test on non-x86 architecture"""
  1889. data = self._DoReadFile('103_cbfs_raw_ppc.dts')
  1890. size = 0x100
  1891. cbfs = cbfs_util.CbfsReader(data)
  1892. self.assertEqual(size, cbfs.rom_size)
  1893. self.assertIn('u-boot-dtb', cbfs.files)
  1894. cfile = cbfs.files['u-boot-dtb']
  1895. self.assertEqual(U_BOOT_DTB_DATA, cfile.data)
  1896. def testCbfsStage(self):
  1897. """Tests handling of a Coreboot Filesystem (CBFS)"""
  1898. if not elf.ELF_TOOLS:
  1899. self.skipTest('Python elftools not available')
  1900. elf_fname = os.path.join(self._indir, 'cbfs-stage.elf')
  1901. elf.MakeElf(elf_fname, U_BOOT_DATA, U_BOOT_DTB_DATA)
  1902. size = 0xb0
  1903. data = self._DoReadFile('104_cbfs_stage.dts')
  1904. cbfs = cbfs_util.CbfsReader(data)
  1905. self.assertEqual(size, cbfs.rom_size)
  1906. self.assertIn('u-boot', cbfs.files)
  1907. cfile = cbfs.files['u-boot']
  1908. self.assertEqual(U_BOOT_DATA + U_BOOT_DTB_DATA, cfile.data)
  1909. def testCbfsRawCompress(self):
  1910. """Test handling of compressing raw files"""
  1911. self._CheckLz4()
  1912. data = self._DoReadFile('105_cbfs_raw_compress.dts')
  1913. size = 0x140
  1914. cbfs = cbfs_util.CbfsReader(data)
  1915. self.assertIn('u-boot', cbfs.files)
  1916. cfile = cbfs.files['u-boot']
  1917. self.assertEqual(COMPRESS_DATA, cfile.data)
  1918. def testCbfsBadArch(self):
  1919. """Test handling of a bad architecture"""
  1920. with self.assertRaises(ValueError) as e:
  1921. self._DoReadFile('106_cbfs_bad_arch.dts')
  1922. self.assertIn("Invalid architecture 'bad-arch'", str(e.exception))
  1923. def testCbfsNoSize(self):
  1924. """Test handling of a missing size property"""
  1925. with self.assertRaises(ValueError) as e:
  1926. self._DoReadFile('107_cbfs_no_size.dts')
  1927. self.assertIn('entry must have a size property', str(e.exception))
  1928. def testCbfsNoCOntents(self):
  1929. """Test handling of a CBFS entry which does not provide contentsy"""
  1930. with self.assertRaises(ValueError) as e:
  1931. self._DoReadFile('108_cbfs_no_contents.dts')
  1932. self.assertIn('Could not complete processing of contents',
  1933. str(e.exception))
  1934. def testCbfsBadCompress(self):
  1935. """Test handling of a bad architecture"""
  1936. with self.assertRaises(ValueError) as e:
  1937. self._DoReadFile('109_cbfs_bad_compress.dts')
  1938. self.assertIn("Invalid compression in 'u-boot': 'invalid-algo'",
  1939. str(e.exception))
  1940. def testCbfsNamedEntries(self):
  1941. """Test handling of named entries"""
  1942. data = self._DoReadFile('110_cbfs_name.dts')
  1943. cbfs = cbfs_util.CbfsReader(data)
  1944. self.assertIn('FRED', cbfs.files)
  1945. cfile1 = cbfs.files['FRED']
  1946. self.assertEqual(U_BOOT_DATA, cfile1.data)
  1947. self.assertIn('hello', cbfs.files)
  1948. cfile2 = cbfs.files['hello']
  1949. self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
  1950. def _SetupIfwi(self, fname):
  1951. """Set up to run an IFWI test
  1952. Args:
  1953. fname: Filename of input file to provide (fitimage.bin or ifwi.bin)
  1954. """
  1955. self._SetupSplElf()
  1956. self._SetupTplElf()
  1957. # Intel Integrated Firmware Image (IFWI) file
  1958. with gzip.open(self.TestFile('%s.gz' % fname), 'rb') as fd:
  1959. data = fd.read()
  1960. TestFunctional._MakeInputFile(fname,data)
  1961. def _CheckIfwi(self, data):
  1962. """Check that an image with an IFWI contains the correct output
  1963. Args:
  1964. data: Conents of output file
  1965. """
  1966. expected_desc = tools.ReadFile(self.TestFile('descriptor.bin'))
  1967. if data[:0x1000] != expected_desc:
  1968. self.fail('Expected descriptor binary at start of image')
  1969. # We expect to find the TPL wil in subpart IBBP entry IBBL
  1970. image_fname = tools.GetOutputFilename('image.bin')
  1971. tpl_fname = tools.GetOutputFilename('tpl.out')
  1972. tools.RunIfwiTool(image_fname, tools.CMD_EXTRACT, fname=tpl_fname,
  1973. subpart='IBBP', entry_name='IBBL')
  1974. tpl_data = tools.ReadFile(tpl_fname)
  1975. self.assertEqual(U_BOOT_TPL_DATA, tpl_data[:len(U_BOOT_TPL_DATA)])
  1976. def testPackX86RomIfwi(self):
  1977. """Test that an x86 ROM with Integrated Firmware Image can be created"""
  1978. self._SetupIfwi('fitimage.bin')
  1979. data = self._DoReadFile('111_x86_rom_ifwi.dts')
  1980. self._CheckIfwi(data)
  1981. def testPackX86RomIfwiNoDesc(self):
  1982. """Test that an x86 ROM with IFWI can be created from an ifwi.bin file"""
  1983. self._SetupIfwi('ifwi.bin')
  1984. data = self._DoReadFile('112_x86_rom_ifwi_nodesc.dts')
  1985. self._CheckIfwi(data)
  1986. def testPackX86RomIfwiNoData(self):
  1987. """Test that an x86 ROM with IFWI handles missing data"""
  1988. self._SetupIfwi('ifwi.bin')
  1989. with self.assertRaises(ValueError) as e:
  1990. data = self._DoReadFile('113_x86_rom_ifwi_nodata.dts')
  1991. self.assertIn('Could not complete processing of contents',
  1992. str(e.exception))
  1993. def testCbfsOffset(self):
  1994. """Test a CBFS with files at particular offsets
  1995. Like all CFBS tests, this is just checking the logic that calls
  1996. cbfs_util. See cbfs_util_test for fully tests (e.g. test_cbfs_offset()).
  1997. """
  1998. data = self._DoReadFile('114_cbfs_offset.dts')
  1999. size = 0x200
  2000. cbfs = cbfs_util.CbfsReader(data)
  2001. self.assertEqual(size, cbfs.rom_size)
  2002. self.assertIn('u-boot', cbfs.files)
  2003. cfile = cbfs.files['u-boot']
  2004. self.assertEqual(U_BOOT_DATA, cfile.data)
  2005. self.assertEqual(0x40, cfile.cbfs_offset)
  2006. self.assertIn('u-boot-dtb', cbfs.files)
  2007. cfile2 = cbfs.files['u-boot-dtb']
  2008. self.assertEqual(U_BOOT_DTB_DATA, cfile2.data)
  2009. self.assertEqual(0x140, cfile2.cbfs_offset)
  2010. def testFdtmap(self):
  2011. """Test an FDT map can be inserted in the image"""
  2012. data = self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
  2013. fdtmap_data = data[len(U_BOOT_DATA):]
  2014. magic = fdtmap_data[:8]
  2015. self.assertEqual(b'_FDTMAP_', magic)
  2016. self.assertEqual(tools.GetBytes(0, 8), fdtmap_data[8:16])
  2017. fdt_data = fdtmap_data[16:]
  2018. dtb = fdt.Fdt.FromData(fdt_data)
  2019. dtb.Scan()
  2020. props = self._GetPropTree(dtb, BASE_DTB_PROPS, prefix='/')
  2021. self.assertEqual({
  2022. 'image-pos': 0,
  2023. 'offset': 0,
  2024. 'u-boot:offset': 0,
  2025. 'u-boot:size': len(U_BOOT_DATA),
  2026. 'u-boot:image-pos': 0,
  2027. 'fdtmap:image-pos': 4,
  2028. 'fdtmap:offset': 4,
  2029. 'fdtmap:size': len(fdtmap_data),
  2030. 'size': len(data),
  2031. }, props)
  2032. def testFdtmapNoMatch(self):
  2033. """Check handling of an FDT map when the section cannot be found"""
  2034. self.data = self._DoReadFileRealDtb('115_fdtmap.dts')
  2035. # Mangle the section name, which should cause a mismatch between the
  2036. # correct FDT path and the one expected by the section
  2037. image = control.images['image']
  2038. image._node.path += '-suffix'
  2039. entries = image.GetEntries()
  2040. fdtmap = entries['fdtmap']
  2041. with self.assertRaises(ValueError) as e:
  2042. fdtmap._GetFdtmap()
  2043. self.assertIn("Cannot locate node for path '/binman-suffix'",
  2044. str(e.exception))
  2045. def testFdtmapHeader(self):
  2046. """Test an FDT map and image header can be inserted in the image"""
  2047. data = self.data = self._DoReadFileRealDtb('116_fdtmap_hdr.dts')
  2048. fdtmap_pos = len(U_BOOT_DATA)
  2049. fdtmap_data = data[fdtmap_pos:]
  2050. fdt_data = fdtmap_data[16:]
  2051. dtb = fdt.Fdt.FromData(fdt_data)
  2052. fdt_size = dtb.GetFdtObj().totalsize()
  2053. hdr_data = data[-8:]
  2054. self.assertEqual(b'BinM', hdr_data[:4])
  2055. offset = struct.unpack('<I', hdr_data[4:])[0] & 0xffffffff
  2056. self.assertEqual(fdtmap_pos - 0x400, offset - (1 << 32))
  2057. def testFdtmapHeaderStart(self):
  2058. """Test an image header can be inserted at the image start"""
  2059. data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
  2060. fdtmap_pos = 0x100 + len(U_BOOT_DATA)
  2061. hdr_data = data[:8]
  2062. self.assertEqual(b'BinM', hdr_data[:4])
  2063. offset = struct.unpack('<I', hdr_data[4:])[0]
  2064. self.assertEqual(fdtmap_pos, offset)
  2065. def testFdtmapHeaderPos(self):
  2066. """Test an image header can be inserted at a chosen position"""
  2067. data = self.data = self._DoReadFileRealDtb('118_fdtmap_hdr_pos.dts')
  2068. fdtmap_pos = 0x100 + len(U_BOOT_DATA)
  2069. hdr_data = data[0x80:0x88]
  2070. self.assertEqual(b'BinM', hdr_data[:4])
  2071. offset = struct.unpack('<I', hdr_data[4:])[0]
  2072. self.assertEqual(fdtmap_pos, offset)
  2073. def testHeaderMissingFdtmap(self):
  2074. """Test an image header requires an fdtmap"""
  2075. with self.assertRaises(ValueError) as e:
  2076. self.data = self._DoReadFileRealDtb('119_fdtmap_hdr_missing.dts')
  2077. self.assertIn("'image_header' section must have an 'fdtmap' sibling",
  2078. str(e.exception))
  2079. def testHeaderNoLocation(self):
  2080. """Test an image header with a no specified location is detected"""
  2081. with self.assertRaises(ValueError) as e:
  2082. self.data = self._DoReadFileRealDtb('120_hdr_no_location.dts')
  2083. self.assertIn("Invalid location 'None', expected 'start' or 'end'",
  2084. str(e.exception))
  2085. def testEntryExpand(self):
  2086. """Test expanding an entry after it is packed"""
  2087. data = self._DoReadFile('121_entry_expand.dts')
  2088. self.assertEqual(b'aaa', data[:3])
  2089. self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)])
  2090. self.assertEqual(b'aaa', data[-3:])
  2091. def testEntryExpandBad(self):
  2092. """Test expanding an entry after it is packed, twice"""
  2093. with self.assertRaises(ValueError) as e:
  2094. self._DoReadFile('122_entry_expand_twice.dts')
  2095. self.assertIn("Image '/binman': Entries changed size after packing",
  2096. str(e.exception))
  2097. def testEntryExpandSection(self):
  2098. """Test expanding an entry within a section after it is packed"""
  2099. data = self._DoReadFile('123_entry_expand_section.dts')
  2100. self.assertEqual(b'aaa', data[:3])
  2101. self.assertEqual(U_BOOT_DATA, data[3:3 + len(U_BOOT_DATA)])
  2102. self.assertEqual(b'aaa', data[-3:])
  2103. def testCompressDtb(self):
  2104. """Test that compress of device-tree files is supported"""
  2105. self._CheckLz4()
  2106. data = self.data = self._DoReadFileRealDtb('124_compress_dtb.dts')
  2107. self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
  2108. comp_data = data[len(U_BOOT_DATA):]
  2109. orig = self._decompress(comp_data)
  2110. dtb = fdt.Fdt.FromData(orig)
  2111. dtb.Scan()
  2112. props = self._GetPropTree(dtb, ['size', 'uncomp-size'])
  2113. expected = {
  2114. 'u-boot:size': len(U_BOOT_DATA),
  2115. 'u-boot-dtb:uncomp-size': len(orig),
  2116. 'u-boot-dtb:size': len(comp_data),
  2117. 'size': len(data),
  2118. }
  2119. self.assertEqual(expected, props)
  2120. def testCbfsUpdateFdt(self):
  2121. """Test that we can update the device tree with CBFS offset/size info"""
  2122. self._CheckLz4()
  2123. data, _, _, out_dtb_fname = self._DoReadFileDtb('125_cbfs_update.dts',
  2124. update_dtb=True)
  2125. dtb = fdt.Fdt(out_dtb_fname)
  2126. dtb.Scan()
  2127. props = self._GetPropTree(dtb, BASE_DTB_PROPS + ['uncomp-size'])
  2128. del props['cbfs/u-boot:size']
  2129. self.assertEqual({
  2130. 'offset': 0,
  2131. 'size': len(data),
  2132. 'image-pos': 0,
  2133. 'cbfs:offset': 0,
  2134. 'cbfs:size': len(data),
  2135. 'cbfs:image-pos': 0,
  2136. 'cbfs/u-boot:offset': 0x38,
  2137. 'cbfs/u-boot:uncomp-size': len(U_BOOT_DATA),
  2138. 'cbfs/u-boot:image-pos': 0x38,
  2139. 'cbfs/u-boot-dtb:offset': 0xb8,
  2140. 'cbfs/u-boot-dtb:size': len(U_BOOT_DATA),
  2141. 'cbfs/u-boot-dtb:image-pos': 0xb8,
  2142. }, props)
  2143. def testCbfsBadType(self):
  2144. """Test an image header with a no specified location is detected"""
  2145. with self.assertRaises(ValueError) as e:
  2146. self._DoReadFile('126_cbfs_bad_type.dts')
  2147. self.assertIn("Unknown cbfs-type 'badtype'", str(e.exception))
  2148. def testList(self):
  2149. """Test listing the files in an image"""
  2150. self._CheckLz4()
  2151. data = self._DoReadFile('127_list.dts')
  2152. image = control.images['image']
  2153. entries = image.BuildEntryList()
  2154. self.assertEqual(7, len(entries))
  2155. ent = entries[0]
  2156. self.assertEqual(0, ent.indent)
  2157. self.assertEqual('main-section', ent.name)
  2158. self.assertEqual('section', ent.etype)
  2159. self.assertEqual(len(data), ent.size)
  2160. self.assertEqual(0, ent.image_pos)
  2161. self.assertEqual(None, ent.uncomp_size)
  2162. self.assertEqual(0, ent.offset)
  2163. ent = entries[1]
  2164. self.assertEqual(1, ent.indent)
  2165. self.assertEqual('u-boot', ent.name)
  2166. self.assertEqual('u-boot', ent.etype)
  2167. self.assertEqual(len(U_BOOT_DATA), ent.size)
  2168. self.assertEqual(0, ent.image_pos)
  2169. self.assertEqual(None, ent.uncomp_size)
  2170. self.assertEqual(0, ent.offset)
  2171. ent = entries[2]
  2172. self.assertEqual(1, ent.indent)
  2173. self.assertEqual('section', ent.name)
  2174. self.assertEqual('section', ent.etype)
  2175. section_size = ent.size
  2176. self.assertEqual(0x100, ent.image_pos)
  2177. self.assertEqual(None, ent.uncomp_size)
  2178. self.assertEqual(0x100, ent.offset)
  2179. ent = entries[3]
  2180. self.assertEqual(2, ent.indent)
  2181. self.assertEqual('cbfs', ent.name)
  2182. self.assertEqual('cbfs', ent.etype)
  2183. self.assertEqual(0x400, ent.size)
  2184. self.assertEqual(0x100, ent.image_pos)
  2185. self.assertEqual(None, ent.uncomp_size)
  2186. self.assertEqual(0, ent.offset)
  2187. ent = entries[4]
  2188. self.assertEqual(3, ent.indent)
  2189. self.assertEqual('u-boot', ent.name)
  2190. self.assertEqual('u-boot', ent.etype)
  2191. self.assertEqual(len(U_BOOT_DATA), ent.size)
  2192. self.assertEqual(0x138, ent.image_pos)
  2193. self.assertEqual(None, ent.uncomp_size)
  2194. self.assertEqual(0x38, ent.offset)
  2195. ent = entries[5]
  2196. self.assertEqual(3, ent.indent)
  2197. self.assertEqual('u-boot-dtb', ent.name)
  2198. self.assertEqual('text', ent.etype)
  2199. self.assertGreater(len(COMPRESS_DATA), ent.size)
  2200. self.assertEqual(0x178, ent.image_pos)
  2201. self.assertEqual(len(COMPRESS_DATA), ent.uncomp_size)
  2202. self.assertEqual(0x78, ent.offset)
  2203. ent = entries[6]
  2204. self.assertEqual(2, ent.indent)
  2205. self.assertEqual('u-boot-dtb', ent.name)
  2206. self.assertEqual('u-boot-dtb', ent.etype)
  2207. self.assertEqual(0x500, ent.image_pos)
  2208. self.assertEqual(len(U_BOOT_DTB_DATA), ent.uncomp_size)
  2209. dtb_size = ent.size
  2210. # Compressing this data expands it since headers are added
  2211. self.assertGreater(dtb_size, len(U_BOOT_DTB_DATA))
  2212. self.assertEqual(0x400, ent.offset)
  2213. self.assertEqual(len(data), 0x100 + section_size)
  2214. self.assertEqual(section_size, 0x400 + dtb_size)
  2215. def testFindFdtmap(self):
  2216. """Test locating an FDT map in an image"""
  2217. self._CheckLz4()
  2218. data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
  2219. image = control.images['image']
  2220. entries = image.GetEntries()
  2221. entry = entries['fdtmap']
  2222. self.assertEqual(entry.image_pos, fdtmap.LocateFdtmap(data))
  2223. def testFindFdtmapMissing(self):
  2224. """Test failing to locate an FDP map"""
  2225. data = self._DoReadFile('005_simple.dts')
  2226. self.assertEqual(None, fdtmap.LocateFdtmap(data))
  2227. def testFindImageHeader(self):
  2228. """Test locating a image header"""
  2229. self._CheckLz4()
  2230. data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
  2231. image = control.images['image']
  2232. entries = image.GetEntries()
  2233. entry = entries['fdtmap']
  2234. # The header should point to the FDT map
  2235. self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data))
  2236. def testFindImageHeaderStart(self):
  2237. """Test locating a image header located at the start of an image"""
  2238. data = self.data = self._DoReadFileRealDtb('117_fdtmap_hdr_start.dts')
  2239. image = control.images['image']
  2240. entries = image.GetEntries()
  2241. entry = entries['fdtmap']
  2242. # The header should point to the FDT map
  2243. self.assertEqual(entry.image_pos, image_header.LocateHeaderOffset(data))
  2244. def testFindImageHeaderMissing(self):
  2245. """Test failing to locate an image header"""
  2246. data = self._DoReadFile('005_simple.dts')
  2247. self.assertEqual(None, image_header.LocateHeaderOffset(data))
  2248. def testReadImage(self):
  2249. """Test reading an image and accessing its FDT map"""
  2250. self._CheckLz4()
  2251. data = self.data = self._DoReadFileRealDtb('128_decode_image.dts')
  2252. image_fname = tools.GetOutputFilename('image.bin')
  2253. orig_image = control.images['image']
  2254. image = Image.FromFile(image_fname)
  2255. self.assertEqual(orig_image.GetEntries().keys(),
  2256. image.GetEntries().keys())
  2257. orig_entry = orig_image.GetEntries()['fdtmap']
  2258. entry = image.GetEntries()['fdtmap']
  2259. self.assertEquals(orig_entry.offset, entry.offset)
  2260. self.assertEquals(orig_entry.size, entry.size)
  2261. self.assertEquals(orig_entry.image_pos, entry.image_pos)
  2262. def testReadImageNoHeader(self):
  2263. """Test accessing an image's FDT map without an image header"""
  2264. self._CheckLz4()
  2265. data = self._DoReadFileRealDtb('129_decode_image_nohdr.dts')
  2266. image_fname = tools.GetOutputFilename('image.bin')
  2267. image = Image.FromFile(image_fname)
  2268. self.assertTrue(isinstance(image, Image))
  2269. self.assertEqual('image', image.image_name[-5:])
  2270. def testReadImageFail(self):
  2271. """Test failing to read an image image's FDT map"""
  2272. self._DoReadFile('005_simple.dts')
  2273. image_fname = tools.GetOutputFilename('image.bin')
  2274. with self.assertRaises(ValueError) as e:
  2275. image = Image.FromFile(image_fname)
  2276. self.assertIn("Cannot find FDT map in image", str(e.exception))
  2277. def testListCmd(self):
  2278. """Test listing the files in an image using an Fdtmap"""
  2279. self._CheckLz4()
  2280. data = self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2281. # lz4 compression size differs depending on the version
  2282. image = control.images['image']
  2283. entries = image.GetEntries()
  2284. section_size = entries['section'].size
  2285. fdt_size = entries['section'].GetEntries()['u-boot-dtb'].size
  2286. fdtmap_offset = entries['fdtmap'].offset
  2287. try:
  2288. tmpdir, updated_fname = self._SetupImageInTmpdir()
  2289. with test_util.capture_sys_output() as (stdout, stderr):
  2290. self._DoBinman('ls', '-i', updated_fname)
  2291. finally:
  2292. shutil.rmtree(tmpdir)
  2293. lines = stdout.getvalue().splitlines()
  2294. expected = [
  2295. 'Name Image-pos Size Entry-type Offset Uncomp-size',
  2296. '----------------------------------------------------------------------',
  2297. 'main-section 0 c00 section 0',
  2298. ' u-boot 0 4 u-boot 0',
  2299. ' section 100 %x section 100' % section_size,
  2300. ' cbfs 100 400 cbfs 0',
  2301. ' u-boot 138 4 u-boot 38',
  2302. ' u-boot-dtb 180 105 u-boot-dtb 80 3c9',
  2303. ' u-boot-dtb 500 %x u-boot-dtb 400 3c9' % fdt_size,
  2304. ' fdtmap %x 3bd fdtmap %x' %
  2305. (fdtmap_offset, fdtmap_offset),
  2306. ' image-header bf8 8 image-header bf8',
  2307. ]
  2308. self.assertEqual(expected, lines)
  2309. def testListCmdFail(self):
  2310. """Test failing to list an image"""
  2311. self._DoReadFile('005_simple.dts')
  2312. try:
  2313. tmpdir, updated_fname = self._SetupImageInTmpdir()
  2314. with self.assertRaises(ValueError) as e:
  2315. self._DoBinman('ls', '-i', updated_fname)
  2316. finally:
  2317. shutil.rmtree(tmpdir)
  2318. self.assertIn("Cannot find FDT map in image", str(e.exception))
  2319. def _RunListCmd(self, paths, expected):
  2320. """List out entries and check the result
  2321. Args:
  2322. paths: List of paths to pass to the list command
  2323. expected: Expected list of filenames to be returned, in order
  2324. """
  2325. self._CheckLz4()
  2326. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2327. image_fname = tools.GetOutputFilename('image.bin')
  2328. image = Image.FromFile(image_fname)
  2329. lines = image.GetListEntries(paths)[1]
  2330. files = [line[0].strip() for line in lines[1:]]
  2331. self.assertEqual(expected, files)
  2332. def testListCmdSection(self):
  2333. """Test listing the files in a section"""
  2334. self._RunListCmd(['section'],
  2335. ['section', 'cbfs', 'u-boot', 'u-boot-dtb', 'u-boot-dtb'])
  2336. def testListCmdFile(self):
  2337. """Test listing a particular file"""
  2338. self._RunListCmd(['*u-boot-dtb'], ['u-boot-dtb', 'u-boot-dtb'])
  2339. def testListCmdWildcard(self):
  2340. """Test listing a wildcarded file"""
  2341. self._RunListCmd(['*boot*'],
  2342. ['u-boot', 'u-boot', 'u-boot-dtb', 'u-boot-dtb'])
  2343. def testListCmdWildcardMulti(self):
  2344. """Test listing a wildcarded file"""
  2345. self._RunListCmd(['*cb*', '*head*'],
  2346. ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header'])
  2347. def testListCmdEmpty(self):
  2348. """Test listing a wildcarded file"""
  2349. self._RunListCmd(['nothing'], [])
  2350. def testListCmdPath(self):
  2351. """Test listing the files in a sub-entry of a section"""
  2352. self._RunListCmd(['section/cbfs'], ['cbfs', 'u-boot', 'u-boot-dtb'])
  2353. def _RunExtractCmd(self, entry_name, decomp=True):
  2354. """Extract an entry from an image
  2355. Args:
  2356. entry_name: Entry name to extract
  2357. decomp: True to decompress the data if compressed, False to leave
  2358. it in its raw uncompressed format
  2359. Returns:
  2360. data from entry
  2361. """
  2362. self._CheckLz4()
  2363. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2364. image_fname = tools.GetOutputFilename('image.bin')
  2365. return control.ReadEntry(image_fname, entry_name, decomp)
  2366. def testExtractSimple(self):
  2367. """Test extracting a single file"""
  2368. data = self._RunExtractCmd('u-boot')
  2369. self.assertEqual(U_BOOT_DATA, data)
  2370. def testExtractSection(self):
  2371. """Test extracting the files in a section"""
  2372. data = self._RunExtractCmd('section')
  2373. cbfs_data = data[:0x400]
  2374. cbfs = cbfs_util.CbfsReader(cbfs_data)
  2375. self.assertEqual(['u-boot', 'u-boot-dtb', ''], list(cbfs.files.keys()))
  2376. dtb_data = data[0x400:]
  2377. dtb = self._decompress(dtb_data)
  2378. self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
  2379. def testExtractCompressed(self):
  2380. """Test extracting compressed data"""
  2381. data = self._RunExtractCmd('section/u-boot-dtb')
  2382. self.assertEqual(EXTRACT_DTB_SIZE, len(data))
  2383. def testExtractRaw(self):
  2384. """Test extracting compressed data without decompressing it"""
  2385. data = self._RunExtractCmd('section/u-boot-dtb', decomp=False)
  2386. dtb = self._decompress(data)
  2387. self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
  2388. def testExtractCbfs(self):
  2389. """Test extracting CBFS data"""
  2390. data = self._RunExtractCmd('section/cbfs/u-boot')
  2391. self.assertEqual(U_BOOT_DATA, data)
  2392. def testExtractCbfsCompressed(self):
  2393. """Test extracting CBFS compressed data"""
  2394. data = self._RunExtractCmd('section/cbfs/u-boot-dtb')
  2395. self.assertEqual(EXTRACT_DTB_SIZE, len(data))
  2396. def testExtractCbfsRaw(self):
  2397. """Test extracting CBFS compressed data without decompressing it"""
  2398. data = self._RunExtractCmd('section/cbfs/u-boot-dtb', decomp=False)
  2399. dtb = tools.Decompress(data, 'lzma', with_header=False)
  2400. self.assertEqual(EXTRACT_DTB_SIZE, len(dtb))
  2401. def testExtractBadEntry(self):
  2402. """Test extracting a bad section path"""
  2403. with self.assertRaises(ValueError) as e:
  2404. self._RunExtractCmd('section/does-not-exist')
  2405. self.assertIn("Entry 'does-not-exist' not found in '/section'",
  2406. str(e.exception))
  2407. def testExtractMissingFile(self):
  2408. """Test extracting file that does not exist"""
  2409. with self.assertRaises(IOError) as e:
  2410. control.ReadEntry('missing-file', 'name')
  2411. def testExtractBadFile(self):
  2412. """Test extracting an invalid file"""
  2413. fname = os.path.join(self._indir, 'badfile')
  2414. tools.WriteFile(fname, b'')
  2415. with self.assertRaises(ValueError) as e:
  2416. control.ReadEntry(fname, 'name')
  2417. def testExtractCmd(self):
  2418. """Test extracting a file fron an image on the command line"""
  2419. self._CheckLz4()
  2420. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2421. fname = os.path.join(self._indir, 'output.extact')
  2422. try:
  2423. tmpdir, updated_fname = self._SetupImageInTmpdir()
  2424. with test_util.capture_sys_output() as (stdout, stderr):
  2425. self._DoBinman('extract', '-i', updated_fname, 'u-boot',
  2426. '-f', fname)
  2427. finally:
  2428. shutil.rmtree(tmpdir)
  2429. data = tools.ReadFile(fname)
  2430. self.assertEqual(U_BOOT_DATA, data)
  2431. def testExtractOneEntry(self):
  2432. """Test extracting a single entry fron an image """
  2433. self._CheckLz4()
  2434. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2435. image_fname = tools.GetOutputFilename('image.bin')
  2436. fname = os.path.join(self._indir, 'output.extact')
  2437. control.ExtractEntries(image_fname, fname, None, ['u-boot'])
  2438. data = tools.ReadFile(fname)
  2439. self.assertEqual(U_BOOT_DATA, data)
  2440. def _CheckExtractOutput(self, decomp):
  2441. """Helper to test file output with and without decompression
  2442. Args:
  2443. decomp: True to decompress entry data, False to output it raw
  2444. """
  2445. def _CheckPresent(entry_path, expect_data, expect_size=None):
  2446. """Check and remove expected file
  2447. This checks the data/size of a file and removes the file both from
  2448. the outfiles set and from the output directory. Once all files are
  2449. processed, both the set and directory should be empty.
  2450. Args:
  2451. entry_path: Entry path
  2452. expect_data: Data to expect in file, or None to skip check
  2453. expect_size: Size of data to expect in file, or None to skip
  2454. """
  2455. path = os.path.join(outdir, entry_path)
  2456. data = tools.ReadFile(path)
  2457. os.remove(path)
  2458. if expect_data:
  2459. self.assertEqual(expect_data, data)
  2460. elif expect_size:
  2461. self.assertEqual(expect_size, len(data))
  2462. outfiles.remove(path)
  2463. def _CheckDirPresent(name):
  2464. """Remove expected directory
  2465. This gives an error if the directory does not exist as expected
  2466. Args:
  2467. name: Name of directory to remove
  2468. """
  2469. path = os.path.join(outdir, name)
  2470. os.rmdir(path)
  2471. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2472. image_fname = tools.GetOutputFilename('image.bin')
  2473. outdir = os.path.join(self._indir, 'extract')
  2474. einfos = control.ExtractEntries(image_fname, None, outdir, [], decomp)
  2475. # Create a set of all file that were output (should be 9)
  2476. outfiles = set()
  2477. for root, dirs, files in os.walk(outdir):
  2478. outfiles |= set([os.path.join(root, fname) for fname in files])
  2479. self.assertEqual(9, len(outfiles))
  2480. self.assertEqual(9, len(einfos))
  2481. image = control.images['image']
  2482. entries = image.GetEntries()
  2483. # Check the 9 files in various ways
  2484. section = entries['section']
  2485. section_entries = section.GetEntries()
  2486. cbfs_entries = section_entries['cbfs'].GetEntries()
  2487. _CheckPresent('u-boot', U_BOOT_DATA)
  2488. _CheckPresent('section/cbfs/u-boot', U_BOOT_DATA)
  2489. dtb_len = EXTRACT_DTB_SIZE
  2490. if not decomp:
  2491. dtb_len = cbfs_entries['u-boot-dtb'].size
  2492. _CheckPresent('section/cbfs/u-boot-dtb', None, dtb_len)
  2493. if not decomp:
  2494. dtb_len = section_entries['u-boot-dtb'].size
  2495. _CheckPresent('section/u-boot-dtb', None, dtb_len)
  2496. fdtmap = entries['fdtmap']
  2497. _CheckPresent('fdtmap', fdtmap.data)
  2498. hdr = entries['image-header']
  2499. _CheckPresent('image-header', hdr.data)
  2500. _CheckPresent('section/root', section.data)
  2501. cbfs = section_entries['cbfs']
  2502. _CheckPresent('section/cbfs/root', cbfs.data)
  2503. data = tools.ReadFile(image_fname)
  2504. _CheckPresent('root', data)
  2505. # There should be no files left. Remove all the directories to check.
  2506. # If there are any files/dirs remaining, one of these checks will fail.
  2507. self.assertEqual(0, len(outfiles))
  2508. _CheckDirPresent('section/cbfs')
  2509. _CheckDirPresent('section')
  2510. _CheckDirPresent('')
  2511. self.assertFalse(os.path.exists(outdir))
  2512. def testExtractAllEntries(self):
  2513. """Test extracting all entries"""
  2514. self._CheckLz4()
  2515. self._CheckExtractOutput(decomp=True)
  2516. def testExtractAllEntriesRaw(self):
  2517. """Test extracting all entries without decompressing them"""
  2518. self._CheckLz4()
  2519. self._CheckExtractOutput(decomp=False)
  2520. def testExtractSelectedEntries(self):
  2521. """Test extracting some entries"""
  2522. self._CheckLz4()
  2523. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2524. image_fname = tools.GetOutputFilename('image.bin')
  2525. outdir = os.path.join(self._indir, 'extract')
  2526. einfos = control.ExtractEntries(image_fname, None, outdir,
  2527. ['*cb*', '*head*'])
  2528. # File output is tested by testExtractAllEntries(), so just check that
  2529. # the expected entries are selected
  2530. names = [einfo.name for einfo in einfos]
  2531. self.assertEqual(names,
  2532. ['cbfs', 'u-boot', 'u-boot-dtb', 'image-header'])
  2533. def testExtractNoEntryPaths(self):
  2534. """Test extracting some entries"""
  2535. self._CheckLz4()
  2536. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2537. image_fname = tools.GetOutputFilename('image.bin')
  2538. with self.assertRaises(ValueError) as e:
  2539. control.ExtractEntries(image_fname, 'fname', None, [])
  2540. self.assertIn('Must specify an entry path to write with -f',
  2541. str(e.exception))
  2542. def testExtractTooManyEntryPaths(self):
  2543. """Test extracting some entries"""
  2544. self._CheckLz4()
  2545. self._DoReadFileRealDtb('130_list_fdtmap.dts')
  2546. image_fname = tools.GetOutputFilename('image.bin')
  2547. with self.assertRaises(ValueError) as e:
  2548. control.ExtractEntries(image_fname, 'fname', None, ['a', 'b'])
  2549. self.assertIn('Must specify exactly one entry path to write with -f',
  2550. str(e.exception))
  2551. def testPackAlignSection(self):
  2552. """Test that sections can have alignment"""
  2553. self._DoReadFile('131_pack_align_section.dts')
  2554. self.assertIn('image', control.images)
  2555. image = control.images['image']
  2556. entries = image.GetEntries()
  2557. self.assertEqual(3, len(entries))
  2558. # First u-boot
  2559. self.assertIn('u-boot', entries)
  2560. entry = entries['u-boot']
  2561. self.assertEqual(0, entry.offset)
  2562. self.assertEqual(0, entry.image_pos)
  2563. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  2564. self.assertEqual(len(U_BOOT_DATA), entry.size)
  2565. # Section0
  2566. self.assertIn('section0', entries)
  2567. section0 = entries['section0']
  2568. self.assertEqual(0x10, section0.offset)
  2569. self.assertEqual(0x10, section0.image_pos)
  2570. self.assertEqual(len(U_BOOT_DATA), section0.size)
  2571. # Second u-boot
  2572. section_entries = section0.GetEntries()
  2573. self.assertIn('u-boot', section_entries)
  2574. entry = section_entries['u-boot']
  2575. self.assertEqual(0, entry.offset)
  2576. self.assertEqual(0x10, entry.image_pos)
  2577. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  2578. self.assertEqual(len(U_BOOT_DATA), entry.size)
  2579. # Section1
  2580. self.assertIn('section1', entries)
  2581. section1 = entries['section1']
  2582. self.assertEqual(0x14, section1.offset)
  2583. self.assertEqual(0x14, section1.image_pos)
  2584. self.assertEqual(0x20, section1.size)
  2585. # Second u-boot
  2586. section_entries = section1.GetEntries()
  2587. self.assertIn('u-boot', section_entries)
  2588. entry = section_entries['u-boot']
  2589. self.assertEqual(0, entry.offset)
  2590. self.assertEqual(0x14, entry.image_pos)
  2591. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  2592. self.assertEqual(len(U_BOOT_DATA), entry.size)
  2593. # Section2
  2594. self.assertIn('section2', section_entries)
  2595. section2 = section_entries['section2']
  2596. self.assertEqual(0x4, section2.offset)
  2597. self.assertEqual(0x18, section2.image_pos)
  2598. self.assertEqual(4, section2.size)
  2599. # Third u-boot
  2600. section_entries = section2.GetEntries()
  2601. self.assertIn('u-boot', section_entries)
  2602. entry = section_entries['u-boot']
  2603. self.assertEqual(0, entry.offset)
  2604. self.assertEqual(0x18, entry.image_pos)
  2605. self.assertEqual(len(U_BOOT_DATA), entry.contents_size)
  2606. self.assertEqual(len(U_BOOT_DATA), entry.size)
  2607. def _RunReplaceCmd(self, entry_name, data, decomp=True, allow_resize=True,
  2608. dts='132_replace.dts'):
  2609. """Replace an entry in an image
  2610. This writes the entry data to update it, then opens the updated file and
  2611. returns the value that it now finds there.
  2612. Args:
  2613. entry_name: Entry name to replace
  2614. data: Data to replace it with
  2615. decomp: True to compress the data if needed, False if data is
  2616. already compressed so should be used as is
  2617. allow_resize: True to allow entries to change size, False to raise
  2618. an exception
  2619. Returns:
  2620. Tuple:
  2621. data from entry
  2622. data from fdtmap (excluding header)
  2623. Image object that was modified
  2624. """
  2625. dtb_data = self._DoReadFileDtb(dts, use_real_dtb=True,
  2626. update_dtb=True)[1]
  2627. self.assertIn('image', control.images)
  2628. image = control.images['image']
  2629. entries = image.GetEntries()
  2630. orig_dtb_data = entries['u-boot-dtb'].data
  2631. orig_fdtmap_data = entries['fdtmap'].data
  2632. image_fname = tools.GetOutputFilename('image.bin')
  2633. updated_fname = tools.GetOutputFilename('image-updated.bin')
  2634. tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
  2635. image = control.WriteEntry(updated_fname, entry_name, data, decomp,
  2636. allow_resize)
  2637. data = control.ReadEntry(updated_fname, entry_name, decomp)
  2638. # The DT data should not change unless resized:
  2639. if not allow_resize:
  2640. new_dtb_data = entries['u-boot-dtb'].data
  2641. self.assertEqual(new_dtb_data, orig_dtb_data)
  2642. new_fdtmap_data = entries['fdtmap'].data
  2643. self.assertEqual(new_fdtmap_data, orig_fdtmap_data)
  2644. return data, orig_fdtmap_data[fdtmap.FDTMAP_HDR_LEN:], image
  2645. def testReplaceSimple(self):
  2646. """Test replacing a single file"""
  2647. expected = b'x' * len(U_BOOT_DATA)
  2648. data, expected_fdtmap, _ = self._RunReplaceCmd('u-boot', expected,
  2649. allow_resize=False)
  2650. self.assertEqual(expected, data)
  2651. # Test that the state looks right. There should be an FDT for the fdtmap
  2652. # that we jsut read back in, and it should match what we find in the
  2653. # 'control' tables. Checking for an FDT that does not exist should
  2654. # return None.
  2655. path, fdtmap = state.GetFdtContents('fdtmap')
  2656. self.assertIsNotNone(path)
  2657. self.assertEqual(expected_fdtmap, fdtmap)
  2658. dtb = state.GetFdtForEtype('fdtmap')
  2659. self.assertEqual(dtb.GetContents(), fdtmap)
  2660. missing_path, missing_fdtmap = state.GetFdtContents('missing')
  2661. self.assertIsNone(missing_path)
  2662. self.assertIsNone(missing_fdtmap)
  2663. missing_dtb = state.GetFdtForEtype('missing')
  2664. self.assertIsNone(missing_dtb)
  2665. self.assertEqual('/binman', state.fdt_path_prefix)
  2666. def testReplaceResizeFail(self):
  2667. """Test replacing a file by something larger"""
  2668. expected = U_BOOT_DATA + b'x'
  2669. with self.assertRaises(ValueError) as e:
  2670. self._RunReplaceCmd('u-boot', expected, allow_resize=False,
  2671. dts='139_replace_repack.dts')
  2672. self.assertIn("Node '/u-boot': Entry data size does not match, but resize is disabled",
  2673. str(e.exception))
  2674. def testReplaceMulti(self):
  2675. """Test replacing entry data where multiple images are generated"""
  2676. data = self._DoReadFileDtb('133_replace_multi.dts', use_real_dtb=True,
  2677. update_dtb=True)[0]
  2678. expected = b'x' * len(U_BOOT_DATA)
  2679. updated_fname = tools.GetOutputFilename('image-updated.bin')
  2680. tools.WriteFile(updated_fname, data)
  2681. entry_name = 'u-boot'
  2682. control.WriteEntry(updated_fname, entry_name, expected,
  2683. allow_resize=False)
  2684. data = control.ReadEntry(updated_fname, entry_name)
  2685. self.assertEqual(expected, data)
  2686. # Check the state looks right.
  2687. self.assertEqual('/binman/image', state.fdt_path_prefix)
  2688. # Now check we can write the first image
  2689. image_fname = tools.GetOutputFilename('first-image.bin')
  2690. updated_fname = tools.GetOutputFilename('first-updated.bin')
  2691. tools.WriteFile(updated_fname, tools.ReadFile(image_fname))
  2692. entry_name = 'u-boot'
  2693. control.WriteEntry(updated_fname, entry_name, expected,
  2694. allow_resize=False)
  2695. data = control.ReadEntry(updated_fname, entry_name)
  2696. self.assertEqual(expected, data)
  2697. # Check the state looks right.
  2698. self.assertEqual('/binman/first-image', state.fdt_path_prefix)
  2699. def testUpdateFdtAllRepack(self):
  2700. """Test that all device trees are updated with offset/size info"""
  2701. data = self._DoReadFileRealDtb('134_fdt_update_all_repack.dts')
  2702. SECTION_SIZE = 0x300
  2703. DTB_SIZE = 602
  2704. FDTMAP_SIZE = 608
  2705. base_expected = {
  2706. 'offset': 0,
  2707. 'size': SECTION_SIZE + DTB_SIZE * 2 + FDTMAP_SIZE,
  2708. 'image-pos': 0,
  2709. 'section:offset': 0,
  2710. 'section:size': SECTION_SIZE,
  2711. 'section:image-pos': 0,
  2712. 'section/u-boot-dtb:offset': 4,
  2713. 'section/u-boot-dtb:size': 636,
  2714. 'section/u-boot-dtb:image-pos': 4,
  2715. 'u-boot-spl-dtb:offset': SECTION_SIZE,
  2716. 'u-boot-spl-dtb:size': DTB_SIZE,
  2717. 'u-boot-spl-dtb:image-pos': SECTION_SIZE,
  2718. 'u-boot-tpl-dtb:offset': SECTION_SIZE + DTB_SIZE,
  2719. 'u-boot-tpl-dtb:image-pos': SECTION_SIZE + DTB_SIZE,
  2720. 'u-boot-tpl-dtb:size': DTB_SIZE,
  2721. 'fdtmap:offset': SECTION_SIZE + DTB_SIZE * 2,
  2722. 'fdtmap:size': FDTMAP_SIZE,
  2723. 'fdtmap:image-pos': SECTION_SIZE + DTB_SIZE * 2,
  2724. }
  2725. main_expected = {
  2726. 'section:orig-size': SECTION_SIZE,
  2727. 'section/u-boot-dtb:orig-offset': 4,
  2728. }
  2729. # We expect three device-tree files in the output, with the first one
  2730. # within a fixed-size section.
  2731. # Read them in sequence. We look for an 'spl' property in the SPL tree,
  2732. # and 'tpl' in the TPL tree, to make sure they are distinct from the
  2733. # main U-Boot tree. All three should have the same positions and offset
  2734. # except that the main tree should include the main_expected properties
  2735. start = 4
  2736. for item in ['', 'spl', 'tpl', None]:
  2737. if item is None:
  2738. start += 16 # Move past fdtmap header
  2739. dtb = fdt.Fdt.FromData(data[start:])
  2740. dtb.Scan()
  2741. props = self._GetPropTree(dtb,
  2742. BASE_DTB_PROPS + REPACK_DTB_PROPS + ['spl', 'tpl'],
  2743. prefix='/' if item is None else '/binman/')
  2744. expected = dict(base_expected)
  2745. if item:
  2746. expected[item] = 0
  2747. else:
  2748. # Main DTB and fdtdec should include the 'orig-' properties
  2749. expected.update(main_expected)
  2750. # Helpful for debugging:
  2751. #for prop in sorted(props):
  2752. #print('prop %s %s %s' % (prop, props[prop], expected[prop]))
  2753. self.assertEqual(expected, props)
  2754. if item == '':
  2755. start = SECTION_SIZE
  2756. else:
  2757. start += dtb._fdt_obj.totalsize()
  2758. def testFdtmapHeaderMiddle(self):
  2759. """Test an FDT map in the middle of an image when it should be at end"""
  2760. with self.assertRaises(ValueError) as e:
  2761. self._DoReadFileRealDtb('135_fdtmap_hdr_middle.dts')
  2762. self.assertIn("Invalid sibling order 'middle' for image-header: Must be at 'end' to match location",
  2763. str(e.exception))
  2764. def testFdtmapHeaderStartBad(self):
  2765. """Test an FDT map in middle of an image when it should be at start"""
  2766. with self.assertRaises(ValueError) as e:
  2767. self._DoReadFileRealDtb('136_fdtmap_hdr_startbad.dts')
  2768. self.assertIn("Invalid sibling order 'end' for image-header: Must be at 'start' to match location",
  2769. str(e.exception))
  2770. def testFdtmapHeaderEndBad(self):
  2771. """Test an FDT map at the start of an image when it should be at end"""
  2772. with self.assertRaises(ValueError) as e:
  2773. self._DoReadFileRealDtb('137_fdtmap_hdr_endbad.dts')
  2774. self.assertIn("Invalid sibling order 'start' for image-header: Must be at 'end' to match location",
  2775. str(e.exception))
  2776. def testFdtmapHeaderNoSize(self):
  2777. """Test an image header at the end of an image with undefined size"""
  2778. self._DoReadFileRealDtb('138_fdtmap_hdr_nosize.dts')
  2779. def testReplaceResize(self):
  2780. """Test replacing a single file in an entry with a larger file"""
  2781. expected = U_BOOT_DATA + b'x'
  2782. data, _, image = self._RunReplaceCmd('u-boot', expected,
  2783. dts='139_replace_repack.dts')
  2784. self.assertEqual(expected, data)
  2785. entries = image.GetEntries()
  2786. dtb_data = entries['u-boot-dtb'].data
  2787. dtb = fdt.Fdt.FromData(dtb_data)
  2788. dtb.Scan()
  2789. # The u-boot section should now be larger in the dtb
  2790. node = dtb.GetNode('/binman/u-boot')
  2791. self.assertEqual(len(expected), fdt_util.GetInt(node, 'size'))
  2792. # Same for the fdtmap
  2793. fdata = entries['fdtmap'].data
  2794. fdtb = fdt.Fdt.FromData(fdata[fdtmap.FDTMAP_HDR_LEN:])
  2795. fdtb.Scan()
  2796. fnode = fdtb.GetNode('/u-boot')
  2797. self.assertEqual(len(expected), fdt_util.GetInt(fnode, 'size'))
  2798. def testReplaceResizeNoRepack(self):
  2799. """Test replacing an entry with a larger file when not allowed"""
  2800. expected = U_BOOT_DATA + b'x'
  2801. with self.assertRaises(ValueError) as e:
  2802. self._RunReplaceCmd('u-boot', expected)
  2803. self.assertIn('Entry data size does not match, but allow-repack is not present for this image',
  2804. str(e.exception))
  2805. def testEntryShrink(self):
  2806. """Test contracting an entry after it is packed"""
  2807. try:
  2808. state.SetAllowEntryContraction(True)
  2809. data = self._DoReadFileDtb('140_entry_shrink.dts',
  2810. update_dtb=True)[0]
  2811. finally:
  2812. state.SetAllowEntryContraction(False)
  2813. self.assertEqual(b'a', data[:1])
  2814. self.assertEqual(U_BOOT_DATA, data[1:1 + len(U_BOOT_DATA)])
  2815. self.assertEqual(b'a', data[-1:])
  2816. def testEntryShrinkFail(self):
  2817. """Test not being allowed to contract an entry after it is packed"""
  2818. data = self._DoReadFileDtb('140_entry_shrink.dts', update_dtb=True)[0]
  2819. # In this case there is a spare byte at the end of the data. The size of
  2820. # the contents is only 1 byte but we still have the size before it
  2821. # shrunk.
  2822. self.assertEqual(b'a\0', data[:2])
  2823. self.assertEqual(U_BOOT_DATA, data[2:2 + len(U_BOOT_DATA)])
  2824. self.assertEqual(b'a\0', data[-2:])
  2825. def testDescriptorOffset(self):
  2826. """Test that the Intel descriptor is always placed at at the start"""
  2827. data = self._DoReadFileDtb('141_descriptor_offset.dts')
  2828. image = control.images['image']
  2829. entries = image.GetEntries()
  2830. desc = entries['intel-descriptor']
  2831. self.assertEqual(0xff800000, desc.offset);
  2832. self.assertEqual(0xff800000, desc.image_pos);
  2833. def testReplaceCbfs(self):
  2834. """Test replacing a single file in CBFS without changing the size"""
  2835. self._CheckLz4()
  2836. expected = b'x' * len(U_BOOT_DATA)
  2837. data = self._DoReadFileRealDtb('142_replace_cbfs.dts')
  2838. updated_fname = tools.GetOutputFilename('image-updated.bin')
  2839. tools.WriteFile(updated_fname, data)
  2840. entry_name = 'section/cbfs/u-boot'
  2841. control.WriteEntry(updated_fname, entry_name, expected,
  2842. allow_resize=True)
  2843. data = control.ReadEntry(updated_fname, entry_name)
  2844. self.assertEqual(expected, data)
  2845. def testReplaceResizeCbfs(self):
  2846. """Test replacing a single file in CBFS with one of a different size"""
  2847. self._CheckLz4()
  2848. expected = U_BOOT_DATA + b'x'
  2849. data = self._DoReadFileRealDtb('142_replace_cbfs.dts')
  2850. updated_fname = tools.GetOutputFilename('image-updated.bin')
  2851. tools.WriteFile(updated_fname, data)
  2852. entry_name = 'section/cbfs/u-boot'
  2853. control.WriteEntry(updated_fname, entry_name, expected,
  2854. allow_resize=True)
  2855. data = control.ReadEntry(updated_fname, entry_name)
  2856. self.assertEqual(expected, data)
  2857. def _SetupForReplace(self):
  2858. """Set up some files to use to replace entries
  2859. This generates an image, copies it to a new file, extracts all the files
  2860. in it and updates some of them
  2861. Returns:
  2862. List
  2863. Image filename
  2864. Output directory
  2865. Expected values for updated entries, each a string
  2866. """
  2867. data = self._DoReadFileRealDtb('143_replace_all.dts')
  2868. updated_fname = tools.GetOutputFilename('image-updated.bin')
  2869. tools.WriteFile(updated_fname, data)
  2870. outdir = os.path.join(self._indir, 'extract')
  2871. einfos = control.ExtractEntries(updated_fname, None, outdir, [])
  2872. expected1 = b'x' + U_BOOT_DATA + b'y'
  2873. u_boot_fname1 = os.path.join(outdir, 'u-boot')
  2874. tools.WriteFile(u_boot_fname1, expected1)
  2875. expected2 = b'a' + U_BOOT_DATA + b'b'
  2876. u_boot_fname2 = os.path.join(outdir, 'u-boot2')
  2877. tools.WriteFile(u_boot_fname2, expected2)
  2878. expected_text = b'not the same text'
  2879. text_fname = os.path.join(outdir, 'text')
  2880. tools.WriteFile(text_fname, expected_text)
  2881. dtb_fname = os.path.join(outdir, 'u-boot-dtb')
  2882. dtb = fdt.FdtScan(dtb_fname)
  2883. node = dtb.GetNode('/binman/text')
  2884. node.AddString('my-property', 'the value')
  2885. dtb.Sync(auto_resize=True)
  2886. dtb.Flush()
  2887. return updated_fname, outdir, expected1, expected2, expected_text
  2888. def _CheckReplaceMultiple(self, entry_paths):
  2889. """Handle replacing the contents of multiple entries
  2890. Args:
  2891. entry_paths: List of entry paths to replace
  2892. Returns:
  2893. List
  2894. Dict of entries in the image:
  2895. key: Entry name
  2896. Value: Entry object
  2897. Expected values for updated entries, each a string
  2898. """
  2899. updated_fname, outdir, expected1, expected2, expected_text = (
  2900. self._SetupForReplace())
  2901. control.ReplaceEntries(updated_fname, None, outdir, entry_paths)
  2902. image = Image.FromFile(updated_fname)
  2903. image.LoadData()
  2904. return image.GetEntries(), expected1, expected2, expected_text
  2905. def testReplaceAll(self):
  2906. """Test replacing the contents of all entries"""
  2907. entries, expected1, expected2, expected_text = (
  2908. self._CheckReplaceMultiple([]))
  2909. data = entries['u-boot'].data
  2910. self.assertEqual(expected1, data)
  2911. data = entries['u-boot2'].data
  2912. self.assertEqual(expected2, data)
  2913. data = entries['text'].data
  2914. self.assertEqual(expected_text, data)
  2915. # Check that the device tree is updated
  2916. data = entries['u-boot-dtb'].data
  2917. dtb = fdt.Fdt.FromData(data)
  2918. dtb.Scan()
  2919. node = dtb.GetNode('/binman/text')
  2920. self.assertEqual('the value', node.props['my-property'].value)
  2921. def testReplaceSome(self):
  2922. """Test replacing the contents of a few entries"""
  2923. entries, expected1, expected2, expected_text = (
  2924. self._CheckReplaceMultiple(['u-boot2', 'text']))
  2925. # This one should not change
  2926. data = entries['u-boot'].data
  2927. self.assertEqual(U_BOOT_DATA, data)
  2928. data = entries['u-boot2'].data
  2929. self.assertEqual(expected2, data)
  2930. data = entries['text'].data
  2931. self.assertEqual(expected_text, data)
  2932. def testReplaceCmd(self):
  2933. """Test replacing a file fron an image on the command line"""
  2934. self._DoReadFileRealDtb('143_replace_all.dts')
  2935. try:
  2936. tmpdir, updated_fname = self._SetupImageInTmpdir()
  2937. fname = os.path.join(tmpdir, 'update-u-boot.bin')
  2938. expected = b'x' * len(U_BOOT_DATA)
  2939. tools.WriteFile(fname, expected)
  2940. self._DoBinman('replace', '-i', updated_fname, 'u-boot', '-f', fname)
  2941. data = tools.ReadFile(updated_fname)
  2942. self.assertEqual(expected, data[:len(expected)])
  2943. map_fname = os.path.join(tmpdir, 'image-updated.map')
  2944. self.assertFalse(os.path.exists(map_fname))
  2945. finally:
  2946. shutil.rmtree(tmpdir)
  2947. def testReplaceCmdSome(self):
  2948. """Test replacing some files fron an image on the command line"""
  2949. updated_fname, outdir, expected1, expected2, expected_text = (
  2950. self._SetupForReplace())
  2951. self._DoBinman('replace', '-i', updated_fname, '-I', outdir,
  2952. 'u-boot2', 'text')
  2953. tools.PrepareOutputDir(None)
  2954. image = Image.FromFile(updated_fname)
  2955. image.LoadData()
  2956. entries = image.GetEntries()
  2957. # This one should not change
  2958. data = entries['u-boot'].data
  2959. self.assertEqual(U_BOOT_DATA, data)
  2960. data = entries['u-boot2'].data
  2961. self.assertEqual(expected2, data)
  2962. data = entries['text'].data
  2963. self.assertEqual(expected_text, data)
  2964. def testReplaceMissing(self):
  2965. """Test replacing entries where the file is missing"""
  2966. updated_fname, outdir, expected1, expected2, expected_text = (
  2967. self._SetupForReplace())
  2968. # Remove one of the files, to generate a warning
  2969. u_boot_fname1 = os.path.join(outdir, 'u-boot')
  2970. os.remove(u_boot_fname1)
  2971. with test_util.capture_sys_output() as (stdout, stderr):
  2972. control.ReplaceEntries(updated_fname, None, outdir, [])
  2973. self.assertIn("Skipping entry '/u-boot' from missing file",
  2974. stderr.getvalue())
  2975. def testReplaceCmdMap(self):
  2976. """Test replacing a file fron an image on the command line"""
  2977. self._DoReadFileRealDtb('143_replace_all.dts')
  2978. try:
  2979. tmpdir, updated_fname = self._SetupImageInTmpdir()
  2980. fname = os.path.join(self._indir, 'update-u-boot.bin')
  2981. expected = b'x' * len(U_BOOT_DATA)
  2982. tools.WriteFile(fname, expected)
  2983. self._DoBinman('replace', '-i', updated_fname, 'u-boot',
  2984. '-f', fname, '-m')
  2985. map_fname = os.path.join(tmpdir, 'image-updated.map')
  2986. self.assertTrue(os.path.exists(map_fname))
  2987. finally:
  2988. shutil.rmtree(tmpdir)
  2989. def testReplaceNoEntryPaths(self):
  2990. """Test replacing an entry without an entry path"""
  2991. self._DoReadFileRealDtb('143_replace_all.dts')
  2992. image_fname = tools.GetOutputFilename('image.bin')
  2993. with self.assertRaises(ValueError) as e:
  2994. control.ReplaceEntries(image_fname, 'fname', None, [])
  2995. self.assertIn('Must specify an entry path to read with -f',
  2996. str(e.exception))
  2997. def testReplaceTooManyEntryPaths(self):
  2998. """Test extracting some entries"""
  2999. self._DoReadFileRealDtb('143_replace_all.dts')
  3000. image_fname = tools.GetOutputFilename('image.bin')
  3001. with self.assertRaises(ValueError) as e:
  3002. control.ReplaceEntries(image_fname, 'fname', None, ['a', 'b'])
  3003. self.assertIn('Must specify exactly one entry path to write with -f',
  3004. str(e.exception))
  3005. def testPackReset16(self):
  3006. """Test that an image with an x86 reset16 region can be created"""
  3007. data = self._DoReadFile('144_x86_reset16.dts')
  3008. self.assertEqual(X86_RESET16_DATA, data[:len(X86_RESET16_DATA)])
  3009. def testPackReset16Spl(self):
  3010. """Test that an image with an x86 reset16-spl region can be created"""
  3011. data = self._DoReadFile('145_x86_reset16_spl.dts')
  3012. self.assertEqual(X86_RESET16_SPL_DATA, data[:len(X86_RESET16_SPL_DATA)])
  3013. def testPackReset16Tpl(self):
  3014. """Test that an image with an x86 reset16-tpl region can be created"""
  3015. data = self._DoReadFile('146_x86_reset16_tpl.dts')
  3016. self.assertEqual(X86_RESET16_TPL_DATA, data[:len(X86_RESET16_TPL_DATA)])
  3017. def testPackIntelFit(self):
  3018. """Test that an image with an Intel FIT and pointer can be created"""
  3019. data = self._DoReadFile('147_intel_fit.dts')
  3020. self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
  3021. fit = data[16:32];
  3022. self.assertEqual(b'_FIT_ \x01\x00\x00\x00\x00\x01\x80}' , fit)
  3023. ptr = struct.unpack('<i', data[0x40:0x44])[0]
  3024. image = control.images['image']
  3025. entries = image.GetEntries()
  3026. expected_ptr = entries['intel-fit'].image_pos - (1 << 32)
  3027. self.assertEqual(expected_ptr, ptr)
  3028. def testPackIntelFitMissing(self):
  3029. """Test detection of a FIT pointer with not FIT region"""
  3030. with self.assertRaises(ValueError) as e:
  3031. self._DoReadFile('148_intel_fit_missing.dts')
  3032. self.assertIn("'intel-fit-ptr' section must have an 'intel-fit' sibling",
  3033. str(e.exception))
  3034. def _CheckSymbolsTplSection(self, dts, expected_vals):
  3035. data = self._DoReadFile(dts)
  3036. sym_values = struct.pack('<LQLL', *expected_vals)
  3037. upto1 = 4 + len(U_BOOT_SPL_DATA)
  3038. expected1 = tools.GetBytes(0xff, 4) + sym_values + U_BOOT_SPL_DATA[20:]
  3039. self.assertEqual(expected1, data[:upto1])
  3040. upto2 = upto1 + 1 + len(U_BOOT_SPL_DATA)
  3041. expected2 = tools.GetBytes(0xff, 1) + sym_values + U_BOOT_SPL_DATA[20:]
  3042. self.assertEqual(expected2, data[upto1:upto2])
  3043. upto3 = 0x34 + len(U_BOOT_DATA)
  3044. expected3 = tools.GetBytes(0xff, 1) + U_BOOT_DATA
  3045. self.assertEqual(expected3, data[upto2:upto3])
  3046. expected4 = sym_values + U_BOOT_TPL_DATA[20:]
  3047. self.assertEqual(expected4, data[upto3:upto3 + len(U_BOOT_TPL_DATA)])
  3048. def testSymbolsTplSection(self):
  3049. """Test binman can assign symbols embedded in U-Boot TPL in a section"""
  3050. self._SetupSplElf('u_boot_binman_syms')
  3051. self._SetupTplElf('u_boot_binman_syms')
  3052. self._CheckSymbolsTplSection('149_symbols_tpl.dts',
  3053. [0x04, 0x1c, 0x10 + 0x34, 0x04])
  3054. def testSymbolsTplSectionX86(self):
  3055. """Test binman can assign symbols in a section with end-at-4gb"""
  3056. self._SetupSplElf('u_boot_binman_syms_x86')
  3057. self._SetupTplElf('u_boot_binman_syms_x86')
  3058. self._CheckSymbolsTplSection('155_symbols_tpl_x86.dts',
  3059. [0xffffff04, 0xffffff1c, 0xffffff34,
  3060. 0x04])
  3061. def testPackX86RomIfwiSectiom(self):
  3062. """Test that a section can be placed in an IFWI region"""
  3063. self._SetupIfwi('fitimage.bin')
  3064. data = self._DoReadFile('151_x86_rom_ifwi_section.dts')
  3065. self._CheckIfwi(data)
  3066. def testPackFspM(self):
  3067. """Test that an image with a FSP memory-init binary can be created"""
  3068. data = self._DoReadFile('152_intel_fsp_m.dts')
  3069. self.assertEqual(FSP_M_DATA, data[:len(FSP_M_DATA)])
  3070. def testPackFspS(self):
  3071. """Test that an image with a FSP silicon-init binary can be created"""
  3072. data = self._DoReadFile('153_intel_fsp_s.dts')
  3073. self.assertEqual(FSP_S_DATA, data[:len(FSP_S_DATA)])
  3074. def testPackFspT(self):
  3075. """Test that an image with a FSP temp-ram-init binary can be created"""
  3076. data = self._DoReadFile('154_intel_fsp_t.dts')
  3077. self.assertEqual(FSP_T_DATA, data[:len(FSP_T_DATA)])
  3078. def testMkimage(self):
  3079. """Test using mkimage to build an image"""
  3080. data = self._DoReadFile('156_mkimage.dts')
  3081. # Just check that the data appears in the file somewhere
  3082. self.assertIn(U_BOOT_SPL_DATA, data)
  3083. def testExtblob(self):
  3084. """Test an image with an external blob"""
  3085. data = self._DoReadFile('157_blob_ext.dts')
  3086. self.assertEqual(REFCODE_DATA, data)
  3087. def testExtblobMissing(self):
  3088. """Test an image with a missing external blob"""
  3089. with self.assertRaises(ValueError) as e:
  3090. self._DoReadFile('158_blob_ext_missing.dts')
  3091. self.assertIn("Filename 'missing-file' not found in input path",
  3092. str(e.exception))
  3093. def testExtblobMissingOk(self):
  3094. """Test an image with an missing external blob that is allowed"""
  3095. with test_util.capture_sys_output() as (stdout, stderr):
  3096. self._DoTestFile('158_blob_ext_missing.dts', allow_missing=True)
  3097. err = stderr.getvalue()
  3098. self.assertRegex(err, "Image 'main-section'.*missing.*: blob-ext")
  3099. def testExtblobMissingOkSect(self):
  3100. """Test an image with an missing external blob that is allowed"""
  3101. with test_util.capture_sys_output() as (stdout, stderr):
  3102. self._DoTestFile('159_blob_ext_missing_sect.dts',
  3103. allow_missing=True)
  3104. err = stderr.getvalue()
  3105. self.assertRegex(err, "Image 'main-section'.*missing.*: "
  3106. "blob-ext blob-ext2")
  3107. def testPackX86RomMeMissingDesc(self):
  3108. """Test that an missing Intel descriptor entry is allowed"""
  3109. with test_util.capture_sys_output() as (stdout, stderr):
  3110. self._DoTestFile('164_x86_rom_me_missing.dts', allow_missing=True)
  3111. err = stderr.getvalue()
  3112. self.assertRegex(err,
  3113. "Image 'main-section'.*missing.*: intel-descriptor")
  3114. def testPackX86RomMissingIfwi(self):
  3115. """Test that an x86 ROM with Integrated Firmware Image can be created"""
  3116. self._SetupIfwi('fitimage.bin')
  3117. pathname = os.path.join(self._indir, 'fitimage.bin')
  3118. os.remove(pathname)
  3119. with test_util.capture_sys_output() as (stdout, stderr):
  3120. self._DoTestFile('111_x86_rom_ifwi.dts', allow_missing=True)
  3121. err = stderr.getvalue()
  3122. self.assertRegex(err, "Image 'main-section'.*missing.*: intel-ifwi")
  3123. def testPackOverlap(self):
  3124. """Test that zero-size overlapping regions are ignored"""
  3125. self._DoTestFile('160_pack_overlap_zero.dts')
  3126. def testSimpleFit(self):
  3127. """Test an image with a FIT inside"""
  3128. data = self._DoReadFile('161_fit.dts')
  3129. self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
  3130. self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):])
  3131. fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)]
  3132. # The data should be inside the FIT
  3133. dtb = fdt.Fdt.FromData(fit_data)
  3134. dtb.Scan()
  3135. fnode = dtb.GetNode('/images/kernel')
  3136. self.assertIn('data', fnode.props)
  3137. fname = os.path.join(self._indir, 'fit_data.fit')
  3138. tools.WriteFile(fname, fit_data)
  3139. out = tools.Run('dumpimage', '-l', fname)
  3140. # Check a few features to make sure the plumbing works. We don't need
  3141. # to test the operation of mkimage or dumpimage here. First convert the
  3142. # output into a dict where the keys are the fields printed by dumpimage
  3143. # and the values are a list of values for each field
  3144. lines = out.splitlines()
  3145. # Converts "Compression: gzip compressed" into two groups:
  3146. # 'Compression' and 'gzip compressed'
  3147. re_line = re.compile(r'^ *([^:]*)(?:: *(.*))?$')
  3148. vals = collections.defaultdict(list)
  3149. for line in lines:
  3150. mat = re_line.match(line)
  3151. vals[mat.group(1)].append(mat.group(2))
  3152. self.assertEquals('FIT description: test-desc', lines[0])
  3153. self.assertIn('Created:', lines[1])
  3154. self.assertIn('Image 0 (kernel)', vals)
  3155. self.assertIn('Hash value', vals)
  3156. data_sizes = vals.get('Data Size')
  3157. self.assertIsNotNone(data_sizes)
  3158. self.assertEqual(2, len(data_sizes))
  3159. # Format is "4 Bytes = 0.00 KiB = 0.00 MiB" so take the first word
  3160. self.assertEqual(len(U_BOOT_DATA), int(data_sizes[0].split()[0]))
  3161. self.assertEqual(len(U_BOOT_SPL_DTB_DATA), int(data_sizes[1].split()[0]))
  3162. def testFitExternal(self):
  3163. """Test an image with an FIT with external images"""
  3164. data = self._DoReadFile('162_fit_external.dts')
  3165. fit_data = data[len(U_BOOT_DATA):-2] # _testing is 2 bytes
  3166. # The data should be outside the FIT
  3167. dtb = fdt.Fdt.FromData(fit_data)
  3168. dtb.Scan()
  3169. fnode = dtb.GetNode('/images/kernel')
  3170. self.assertNotIn('data', fnode.props)
  3171. def testSectionIgnoreHashSignature(self):
  3172. """Test that sections ignore hash, signature nodes for its data"""
  3173. data = self._DoReadFile('165_section_ignore_hash_signature.dts')
  3174. expected = (U_BOOT_DATA + U_BOOT_DATA)
  3175. self.assertEqual(expected, data)
  3176. def testPadInSections(self):
  3177. """Test pad-before, pad-after for entries in sections"""
  3178. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3179. '166_pad_in_sections.dts', update_dtb=True)
  3180. expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
  3181. U_BOOT_DATA + tools.GetBytes(ord('!'), 6) +
  3182. U_BOOT_DATA)
  3183. self.assertEqual(expected, data)
  3184. dtb = fdt.Fdt(out_dtb_fname)
  3185. dtb.Scan()
  3186. props = self._GetPropTree(dtb, ['size', 'image-pos', 'offset'])
  3187. expected = {
  3188. 'image-pos': 0,
  3189. 'offset': 0,
  3190. 'size': 12 + 6 + 3 * len(U_BOOT_DATA),
  3191. 'section:image-pos': 0,
  3192. 'section:offset': 0,
  3193. 'section:size': 12 + 6 + 3 * len(U_BOOT_DATA),
  3194. 'section/before:image-pos': 0,
  3195. 'section/before:offset': 0,
  3196. 'section/before:size': len(U_BOOT_DATA),
  3197. 'section/u-boot:image-pos': 4,
  3198. 'section/u-boot:offset': 4,
  3199. 'section/u-boot:size': 12 + len(U_BOOT_DATA) + 6,
  3200. 'section/after:image-pos': 26,
  3201. 'section/after:offset': 26,
  3202. 'section/after:size': len(U_BOOT_DATA),
  3203. }
  3204. self.assertEqual(expected, props)
  3205. def testFitImageSubentryAlignment(self):
  3206. """Test relative alignability of FIT image subentries"""
  3207. entry_args = {
  3208. 'test-id': TEXT_DATA,
  3209. }
  3210. data, _, _, _ = self._DoReadFileDtb('167_fit_image_subentry_alignment.dts',
  3211. entry_args=entry_args)
  3212. dtb = fdt.Fdt.FromData(data)
  3213. dtb.Scan()
  3214. node = dtb.GetNode('/images/kernel')
  3215. data = dtb.GetProps(node)["data"].bytes
  3216. align_pad = 0x10 - (len(U_BOOT_SPL_DATA) % 0x10)
  3217. expected = (tools.GetBytes(0, 0x20) + U_BOOT_SPL_DATA +
  3218. tools.GetBytes(0, align_pad) + U_BOOT_DATA)
  3219. self.assertEqual(expected, data)
  3220. node = dtb.GetNode('/images/fdt-1')
  3221. data = dtb.GetProps(node)["data"].bytes
  3222. expected = (U_BOOT_SPL_DTB_DATA + tools.GetBytes(0, 20) +
  3223. tools.ToBytes(TEXT_DATA) + tools.GetBytes(0, 30) +
  3224. U_BOOT_DTB_DATA)
  3225. self.assertEqual(expected, data)
  3226. def testFitExtblobMissingOk(self):
  3227. """Test a FIT with a missing external blob that is allowed"""
  3228. with test_util.capture_sys_output() as (stdout, stderr):
  3229. self._DoTestFile('168_fit_missing_blob.dts',
  3230. allow_missing=True)
  3231. err = stderr.getvalue()
  3232. self.assertRegex(err, "Image 'main-section'.*missing.*: atf-bl31")
  3233. def testBlobNamedByArgMissing(self):
  3234. """Test handling of a missing entry arg"""
  3235. with self.assertRaises(ValueError) as e:
  3236. self._DoReadFile('068_blob_named_by_arg.dts')
  3237. self.assertIn("Missing required properties/entry args: cros-ec-rw-path",
  3238. str(e.exception))
  3239. def testPackBl31(self):
  3240. """Test that an image with an ATF BL31 binary can be created"""
  3241. data = self._DoReadFile('169_atf_bl31.dts')
  3242. self.assertEqual(ATF_BL31_DATA, data[:len(ATF_BL31_DATA)])
  3243. def testPackScp(self):
  3244. """Test that an image with an SCP binary can be created"""
  3245. data = self._DoReadFile('172_scp.dts')
  3246. self.assertEqual(SCP_DATA, data[:len(SCP_DATA)])
  3247. def testFitFdt(self):
  3248. """Test an image with an FIT with multiple FDT images"""
  3249. def _CheckFdt(seq, expected_data):
  3250. """Check the FDT nodes
  3251. Args:
  3252. seq: Sequence number to check (0 or 1)
  3253. expected_data: Expected contents of 'data' property
  3254. """
  3255. name = 'fdt-%d' % seq
  3256. fnode = dtb.GetNode('/images/%s' % name)
  3257. self.assertIsNotNone(fnode)
  3258. self.assertEqual({'description','type', 'compression', 'data'},
  3259. set(fnode.props.keys()))
  3260. self.assertEqual(expected_data, fnode.props['data'].bytes)
  3261. self.assertEqual('fdt-test-fdt%d.dtb' % seq,
  3262. fnode.props['description'].value)
  3263. def _CheckConfig(seq, expected_data):
  3264. """Check the configuration nodes
  3265. Args:
  3266. seq: Sequence number to check (0 or 1)
  3267. expected_data: Expected contents of 'data' property
  3268. """
  3269. cnode = dtb.GetNode('/configurations')
  3270. self.assertIn('default', cnode.props)
  3271. self.assertEqual('config-2', cnode.props['default'].value)
  3272. name = 'config-%d' % seq
  3273. fnode = dtb.GetNode('/configurations/%s' % name)
  3274. self.assertIsNotNone(fnode)
  3275. self.assertEqual({'description','firmware', 'loadables', 'fdt'},
  3276. set(fnode.props.keys()))
  3277. self.assertEqual('conf-test-fdt%d.dtb' % seq,
  3278. fnode.props['description'].value)
  3279. self.assertEqual('fdt-%d' % seq, fnode.props['fdt'].value)
  3280. entry_args = {
  3281. 'of-list': 'test-fdt1 test-fdt2',
  3282. 'default-dt': 'test-fdt2',
  3283. }
  3284. data = self._DoReadFileDtb(
  3285. '170_fit_fdt.dts',
  3286. entry_args=entry_args,
  3287. extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0]
  3288. self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):])
  3289. fit_data = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)]
  3290. dtb = fdt.Fdt.FromData(fit_data)
  3291. dtb.Scan()
  3292. fnode = dtb.GetNode('/images/kernel')
  3293. self.assertIn('data', fnode.props)
  3294. # Check all the properties in fdt-1 and fdt-2
  3295. _CheckFdt(1, TEST_FDT1_DATA)
  3296. _CheckFdt(2, TEST_FDT2_DATA)
  3297. # Check configurations
  3298. _CheckConfig(1, TEST_FDT1_DATA)
  3299. _CheckConfig(2, TEST_FDT2_DATA)
  3300. def testFitFdtMissingList(self):
  3301. """Test handling of a missing 'of-list' entry arg"""
  3302. with self.assertRaises(ValueError) as e:
  3303. self._DoReadFile('170_fit_fdt.dts')
  3304. self.assertIn("Generator node requires 'of-list' entry argument",
  3305. str(e.exception))
  3306. def testFitFdtEmptyList(self):
  3307. """Test handling of an empty 'of-list' entry arg"""
  3308. entry_args = {
  3309. 'of-list': '',
  3310. }
  3311. data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0]
  3312. def testFitFdtMissingProp(self):
  3313. """Test handling of a missing 'fit,fdt-list' property"""
  3314. with self.assertRaises(ValueError) as e:
  3315. self._DoReadFile('171_fit_fdt_missing_prop.dts')
  3316. self.assertIn("Generator node requires 'fit,fdt-list' property",
  3317. str(e.exception))
  3318. def testFitFdtEmptyList(self):
  3319. """Test handling of an empty 'of-list' entry arg"""
  3320. entry_args = {
  3321. 'of-list': '',
  3322. }
  3323. data = self._DoReadFileDtb('170_fit_fdt.dts', entry_args=entry_args)[0]
  3324. def testFitFdtMissing(self):
  3325. """Test handling of a missing 'default-dt' entry arg"""
  3326. entry_args = {
  3327. 'of-list': 'test-fdt1 test-fdt2',
  3328. }
  3329. with self.assertRaises(ValueError) as e:
  3330. self._DoReadFileDtb(
  3331. '170_fit_fdt.dts',
  3332. entry_args=entry_args,
  3333. extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0]
  3334. self.assertIn("Generated 'default' node requires default-dt entry argument",
  3335. str(e.exception))
  3336. def testFitFdtNotInList(self):
  3337. """Test handling of a default-dt that is not in the of-list"""
  3338. entry_args = {
  3339. 'of-list': 'test-fdt1 test-fdt2',
  3340. 'default-dt': 'test-fdt3',
  3341. }
  3342. with self.assertRaises(ValueError) as e:
  3343. self._DoReadFileDtb(
  3344. '170_fit_fdt.dts',
  3345. entry_args=entry_args,
  3346. extra_indirs=[os.path.join(self._indir, TEST_FDT_SUBDIR)])[0]
  3347. self.assertIn("default-dt entry argument 'test-fdt3' not found in fdt list: test-fdt1, test-fdt2",
  3348. str(e.exception))
  3349. def testFitExtblobMissingHelp(self):
  3350. """Test display of help messages when an external blob is missing"""
  3351. control.missing_blob_help = control._ReadMissingBlobHelp()
  3352. control.missing_blob_help['wibble'] = 'Wibble test'
  3353. control.missing_blob_help['another'] = 'Another test'
  3354. with test_util.capture_sys_output() as (stdout, stderr):
  3355. self._DoTestFile('168_fit_missing_blob.dts',
  3356. allow_missing=True)
  3357. err = stderr.getvalue()
  3358. # We can get the tag from the name, the type or the missing-msg
  3359. # property. Check all three.
  3360. self.assertIn('You may need to build ARM Trusted', err)
  3361. self.assertIn('Wibble test', err)
  3362. self.assertIn('Another test', err)
  3363. def testMissingBlob(self):
  3364. """Test handling of a blob containing a missing file"""
  3365. with self.assertRaises(ValueError) as e:
  3366. self._DoTestFile('173_missing_blob.dts', allow_missing=True)
  3367. self.assertIn("Filename 'missing' not found in input path",
  3368. str(e.exception))
  3369. def testEnvironment(self):
  3370. """Test adding a U-Boot environment"""
  3371. data = self._DoReadFile('174_env.dts')
  3372. self.assertEqual(U_BOOT_DATA, data[:len(U_BOOT_DATA)])
  3373. self.assertEqual(U_BOOT_NODTB_DATA, data[-len(U_BOOT_NODTB_DATA):])
  3374. env = data[len(U_BOOT_DATA):-len(U_BOOT_NODTB_DATA)]
  3375. self.assertEqual(b'\x1b\x97\x22\x7c\x01var1=1\0var2="2"\0\0\xff\xff',
  3376. env)
  3377. def testEnvironmentNoSize(self):
  3378. """Test that a missing 'size' property is detected"""
  3379. with self.assertRaises(ValueError) as e:
  3380. self._DoTestFile('175_env_no_size.dts')
  3381. self.assertIn("'u-boot-env' entry must have a size property",
  3382. str(e.exception))
  3383. def testEnvironmentTooSmall(self):
  3384. """Test handling of an environment that does not fit"""
  3385. with self.assertRaises(ValueError) as e:
  3386. self._DoTestFile('176_env_too_small.dts')
  3387. # checksum, start byte, environment with \0 terminator, final \0
  3388. need = 4 + 1 + len(ENV_DATA) + 1 + 1
  3389. short = need - 0x8
  3390. self.assertIn("too small to hold data (need %#x more bytes)" % short,
  3391. str(e.exception))
  3392. def testSkipAtStart(self):
  3393. """Test handling of skip-at-start section"""
  3394. data = self._DoReadFile('177_skip_at_start.dts')
  3395. self.assertEqual(U_BOOT_DATA, data)
  3396. image = control.images['image']
  3397. entries = image.GetEntries()
  3398. section = entries['section']
  3399. self.assertEqual(0, section.offset)
  3400. self.assertEqual(len(U_BOOT_DATA), section.size)
  3401. self.assertEqual(U_BOOT_DATA, section.GetData())
  3402. entry = section.GetEntries()['u-boot']
  3403. self.assertEqual(16, entry.offset)
  3404. self.assertEqual(len(U_BOOT_DATA), entry.size)
  3405. self.assertEqual(U_BOOT_DATA, entry.data)
  3406. def testSkipAtStartPad(self):
  3407. """Test handling of skip-at-start section with padded entry"""
  3408. data = self._DoReadFile('178_skip_at_start_pad.dts')
  3409. before = tools.GetBytes(0, 8)
  3410. after = tools.GetBytes(0, 4)
  3411. all = before + U_BOOT_DATA + after
  3412. self.assertEqual(all, data)
  3413. image = control.images['image']
  3414. entries = image.GetEntries()
  3415. section = entries['section']
  3416. self.assertEqual(0, section.offset)
  3417. self.assertEqual(len(all), section.size)
  3418. self.assertEqual(all, section.GetData())
  3419. entry = section.GetEntries()['u-boot']
  3420. self.assertEqual(16, entry.offset)
  3421. self.assertEqual(len(all), entry.size)
  3422. self.assertEqual(U_BOOT_DATA, entry.data)
  3423. def testSkipAtStartSectionPad(self):
  3424. """Test handling of skip-at-start section with padding"""
  3425. data = self._DoReadFile('179_skip_at_start_section_pad.dts')
  3426. before = tools.GetBytes(0, 8)
  3427. after = tools.GetBytes(0, 4)
  3428. all = before + U_BOOT_DATA + after
  3429. self.assertEqual(all, data)
  3430. image = control.images['image']
  3431. entries = image.GetEntries()
  3432. section = entries['section']
  3433. self.assertEqual(0, section.offset)
  3434. self.assertEqual(len(all), section.size)
  3435. self.assertEqual(U_BOOT_DATA, section.data)
  3436. self.assertEqual(all, section.GetPaddedData())
  3437. entry = section.GetEntries()['u-boot']
  3438. self.assertEqual(16, entry.offset)
  3439. self.assertEqual(len(U_BOOT_DATA), entry.size)
  3440. self.assertEqual(U_BOOT_DATA, entry.data)
  3441. def testSectionPad(self):
  3442. """Testing padding with sections"""
  3443. data = self._DoReadFile('180_section_pad.dts')
  3444. expected = (tools.GetBytes(ord('&'), 3) +
  3445. tools.GetBytes(ord('!'), 5) +
  3446. U_BOOT_DATA +
  3447. tools.GetBytes(ord('!'), 1) +
  3448. tools.GetBytes(ord('&'), 2))
  3449. self.assertEqual(expected, data)
  3450. def testSectionAlign(self):
  3451. """Testing alignment with sections"""
  3452. data = self._DoReadFileDtb('181_section_align.dts', map=True)[0]
  3453. expected = (b'\0' + # fill section
  3454. tools.GetBytes(ord('&'), 1) + # padding to section align
  3455. b'\0' + # fill section
  3456. tools.GetBytes(ord('!'), 3) + # padding to u-boot align
  3457. U_BOOT_DATA +
  3458. tools.GetBytes(ord('!'), 4) + # padding to u-boot size
  3459. tools.GetBytes(ord('!'), 4)) # padding to section size
  3460. self.assertEqual(expected, data)
  3461. def testCompressImage(self):
  3462. """Test compression of the entire image"""
  3463. self._CheckLz4()
  3464. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3465. '182_compress_image.dts', use_real_dtb=True, update_dtb=True)
  3466. dtb = fdt.Fdt(out_dtb_fname)
  3467. dtb.Scan()
  3468. props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
  3469. 'uncomp-size'])
  3470. orig = self._decompress(data)
  3471. self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig)
  3472. # Do a sanity check on various fields
  3473. image = control.images['image']
  3474. entries = image.GetEntries()
  3475. self.assertEqual(2, len(entries))
  3476. entry = entries['blob']
  3477. self.assertEqual(COMPRESS_DATA, entry.data)
  3478. self.assertEqual(len(COMPRESS_DATA), entry.size)
  3479. entry = entries['u-boot']
  3480. self.assertEqual(U_BOOT_DATA, entry.data)
  3481. self.assertEqual(len(U_BOOT_DATA), entry.size)
  3482. self.assertEqual(len(data), image.size)
  3483. self.assertEqual(COMPRESS_DATA + U_BOOT_DATA, image.uncomp_data)
  3484. self.assertEqual(len(COMPRESS_DATA + U_BOOT_DATA), image.uncomp_size)
  3485. orig = self._decompress(image.data)
  3486. self.assertEqual(orig, image.uncomp_data)
  3487. expected = {
  3488. 'blob:offset': 0,
  3489. 'blob:size': len(COMPRESS_DATA),
  3490. 'u-boot:offset': len(COMPRESS_DATA),
  3491. 'u-boot:size': len(U_BOOT_DATA),
  3492. 'uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA),
  3493. 'offset': 0,
  3494. 'image-pos': 0,
  3495. 'size': len(data),
  3496. }
  3497. self.assertEqual(expected, props)
  3498. def testCompressImageLess(self):
  3499. """Test compression where compression reduces the image size"""
  3500. self._CheckLz4()
  3501. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3502. '183_compress_image_less.dts', use_real_dtb=True, update_dtb=True)
  3503. dtb = fdt.Fdt(out_dtb_fname)
  3504. dtb.Scan()
  3505. props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
  3506. 'uncomp-size'])
  3507. orig = self._decompress(data)
  3508. self.assertEquals(COMPRESS_DATA + COMPRESS_DATA + U_BOOT_DATA, orig)
  3509. # Do a sanity check on various fields
  3510. image = control.images['image']
  3511. entries = image.GetEntries()
  3512. self.assertEqual(2, len(entries))
  3513. entry = entries['blob']
  3514. self.assertEqual(COMPRESS_DATA_BIG, entry.data)
  3515. self.assertEqual(len(COMPRESS_DATA_BIG), entry.size)
  3516. entry = entries['u-boot']
  3517. self.assertEqual(U_BOOT_DATA, entry.data)
  3518. self.assertEqual(len(U_BOOT_DATA), entry.size)
  3519. self.assertEqual(len(data), image.size)
  3520. self.assertEqual(COMPRESS_DATA_BIG + U_BOOT_DATA, image.uncomp_data)
  3521. self.assertEqual(len(COMPRESS_DATA_BIG + U_BOOT_DATA),
  3522. image.uncomp_size)
  3523. orig = self._decompress(image.data)
  3524. self.assertEqual(orig, image.uncomp_data)
  3525. expected = {
  3526. 'blob:offset': 0,
  3527. 'blob:size': len(COMPRESS_DATA_BIG),
  3528. 'u-boot:offset': len(COMPRESS_DATA_BIG),
  3529. 'u-boot:size': len(U_BOOT_DATA),
  3530. 'uncomp-size': len(COMPRESS_DATA_BIG + U_BOOT_DATA),
  3531. 'offset': 0,
  3532. 'image-pos': 0,
  3533. 'size': len(data),
  3534. }
  3535. self.assertEqual(expected, props)
  3536. def testCompressSectionSize(self):
  3537. """Test compression of a section with a fixed size"""
  3538. self._CheckLz4()
  3539. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3540. '184_compress_section_size.dts', use_real_dtb=True, update_dtb=True)
  3541. dtb = fdt.Fdt(out_dtb_fname)
  3542. dtb.Scan()
  3543. props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
  3544. 'uncomp-size'])
  3545. orig = self._decompress(data)
  3546. self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig)
  3547. expected = {
  3548. 'section/blob:offset': 0,
  3549. 'section/blob:size': len(COMPRESS_DATA),
  3550. 'section/u-boot:offset': len(COMPRESS_DATA),
  3551. 'section/u-boot:size': len(U_BOOT_DATA),
  3552. 'section:offset': 0,
  3553. 'section:image-pos': 0,
  3554. 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA),
  3555. 'section:size': 0x30,
  3556. 'offset': 0,
  3557. 'image-pos': 0,
  3558. 'size': 0x30,
  3559. }
  3560. self.assertEqual(expected, props)
  3561. def testCompressSection(self):
  3562. """Test compression of a section with no fixed size"""
  3563. self._CheckLz4()
  3564. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3565. '185_compress_section.dts', use_real_dtb=True, update_dtb=True)
  3566. dtb = fdt.Fdt(out_dtb_fname)
  3567. dtb.Scan()
  3568. props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
  3569. 'uncomp-size'])
  3570. orig = self._decompress(data)
  3571. self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, orig)
  3572. expected = {
  3573. 'section/blob:offset': 0,
  3574. 'section/blob:size': len(COMPRESS_DATA),
  3575. 'section/u-boot:offset': len(COMPRESS_DATA),
  3576. 'section/u-boot:size': len(U_BOOT_DATA),
  3577. 'section:offset': 0,
  3578. 'section:image-pos': 0,
  3579. 'section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA),
  3580. 'section:size': len(data),
  3581. 'offset': 0,
  3582. 'image-pos': 0,
  3583. 'size': len(data),
  3584. }
  3585. self.assertEqual(expected, props)
  3586. def testCompressExtra(self):
  3587. """Test compression of a section with no fixed size"""
  3588. self._CheckLz4()
  3589. data, _, _, out_dtb_fname = self._DoReadFileDtb(
  3590. '186_compress_extra.dts', use_real_dtb=True, update_dtb=True)
  3591. dtb = fdt.Fdt(out_dtb_fname)
  3592. dtb.Scan()
  3593. props = self._GetPropTree(dtb, ['offset', 'image-pos', 'size',
  3594. 'uncomp-size'])
  3595. base = data[len(U_BOOT_DATA):]
  3596. self.assertEquals(U_BOOT_DATA, base[:len(U_BOOT_DATA)])
  3597. rest = base[len(U_BOOT_DATA):]
  3598. # Check compressed data
  3599. section1 = self._decompress(rest)
  3600. expect1 = tools.Compress(COMPRESS_DATA + U_BOOT_DATA, 'lz4')
  3601. self.assertEquals(expect1, rest[:len(expect1)])
  3602. self.assertEquals(COMPRESS_DATA + U_BOOT_DATA, section1)
  3603. rest1 = rest[len(expect1):]
  3604. section2 = self._decompress(rest1)
  3605. expect2 = tools.Compress(COMPRESS_DATA + COMPRESS_DATA, 'lz4')
  3606. self.assertEquals(expect2, rest1[:len(expect2)])
  3607. self.assertEquals(COMPRESS_DATA + COMPRESS_DATA, section2)
  3608. rest2 = rest1[len(expect2):]
  3609. expect_size = (len(U_BOOT_DATA) + len(U_BOOT_DATA) + len(expect1) +
  3610. len(expect2) + len(U_BOOT_DATA))
  3611. #self.assertEquals(expect_size, len(data))
  3612. #self.assertEquals(U_BOOT_DATA, rest2)
  3613. self.maxDiff = None
  3614. expected = {
  3615. 'u-boot:offset': 0,
  3616. 'u-boot:image-pos': 0,
  3617. 'u-boot:size': len(U_BOOT_DATA),
  3618. 'base:offset': len(U_BOOT_DATA),
  3619. 'base:image-pos': len(U_BOOT_DATA),
  3620. 'base:size': len(data) - len(U_BOOT_DATA),
  3621. 'base/u-boot:offset': 0,
  3622. 'base/u-boot:image-pos': len(U_BOOT_DATA),
  3623. 'base/u-boot:size': len(U_BOOT_DATA),
  3624. 'base/u-boot2:offset': len(U_BOOT_DATA) + len(expect1) +
  3625. len(expect2),
  3626. 'base/u-boot2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1) +
  3627. len(expect2),
  3628. 'base/u-boot2:size': len(U_BOOT_DATA),
  3629. 'base/section:offset': len(U_BOOT_DATA),
  3630. 'base/section:image-pos': len(U_BOOT_DATA) * 2,
  3631. 'base/section:size': len(expect1),
  3632. 'base/section:uncomp-size': len(COMPRESS_DATA + U_BOOT_DATA),
  3633. 'base/section/blob:offset': 0,
  3634. 'base/section/blob:size': len(COMPRESS_DATA),
  3635. 'base/section/u-boot:offset': len(COMPRESS_DATA),
  3636. 'base/section/u-boot:size': len(U_BOOT_DATA),
  3637. 'base/section2:offset': len(U_BOOT_DATA) + len(expect1),
  3638. 'base/section2:image-pos': len(U_BOOT_DATA) * 2 + len(expect1),
  3639. 'base/section2:size': len(expect2),
  3640. 'base/section2:uncomp-size': len(COMPRESS_DATA + COMPRESS_DATA),
  3641. 'base/section2/blob:offset': 0,
  3642. 'base/section2/blob:size': len(COMPRESS_DATA),
  3643. 'base/section2/blob2:offset': len(COMPRESS_DATA),
  3644. 'base/section2/blob2:size': len(COMPRESS_DATA),
  3645. 'offset': 0,
  3646. 'image-pos': 0,
  3647. 'size': len(data),
  3648. }
  3649. self.assertEqual(expected, props)
  3650. def testSymbolsSubsection(self):
  3651. """Test binman can assign symbols from a subsection"""
  3652. self.checkSymbols('187_symbols_sub.dts', U_BOOT_SPL_DATA, 0x18)
  3653. def testReadImageEntryArg(self):
  3654. """Test reading an image that would need an entry arg to generate"""
  3655. entry_args = {
  3656. 'cros-ec-rw-path': 'ecrw.bin',
  3657. }
  3658. data = self.data = self._DoReadFileDtb(
  3659. '188_image_entryarg.dts',use_real_dtb=True, update_dtb=True,
  3660. entry_args=entry_args)
  3661. image_fname = tools.GetOutputFilename('image.bin')
  3662. orig_image = control.images['image']
  3663. # This should not generate an error about the missing 'cros-ec-rw-path'
  3664. # since we are reading the image from a file. Compare with
  3665. # testEntryArgsRequired()
  3666. image = Image.FromFile(image_fname)
  3667. self.assertEqual(orig_image.GetEntries().keys(),
  3668. image.GetEntries().keys())
  3669. def testFilesAlign(self):
  3670. """Test alignment with files"""
  3671. data = self._DoReadFile('190_files_align.dts')
  3672. # The first string is 15 bytes so will align to 16
  3673. expect = FILES_DATA[:15] + b'\0' + FILES_DATA[15:]
  3674. self.assertEqual(expect, data)
  3675. def testReadImageSkip(self):
  3676. """Test reading an image and accessing its FDT map"""
  3677. data = self.data = self._DoReadFileRealDtb('191_read_image_skip.dts')
  3678. image_fname = tools.GetOutputFilename('image.bin')
  3679. orig_image = control.images['image']
  3680. image = Image.FromFile(image_fname)
  3681. self.assertEqual(orig_image.GetEntries().keys(),
  3682. image.GetEntries().keys())
  3683. orig_entry = orig_image.GetEntries()['fdtmap']
  3684. entry = image.GetEntries()['fdtmap']
  3685. self.assertEqual(orig_entry.offset, entry.offset)
  3686. self.assertEqual(orig_entry.size, entry.size)
  3687. self.assertEqual(16, entry.image_pos)
  3688. u_boot = image.GetEntries()['section'].GetEntries()['u-boot']
  3689. self.assertEquals(U_BOOT_DATA, u_boot.ReadData())
  3690. def testTplNoDtb(self):
  3691. """Test that an image with tpl/u-boot-tpl-nodtb.bin can be created"""
  3692. self._SetupTplElf()
  3693. data = self._DoReadFile('192_u_boot_tpl_nodtb.dts')
  3694. self.assertEqual(U_BOOT_TPL_NODTB_DATA,
  3695. data[:len(U_BOOT_TPL_NODTB_DATA)])
  3696. def testTplBssPad(self):
  3697. """Test that we can pad TPL's BSS with zeros"""
  3698. # ELF file with a '__bss_size' symbol
  3699. self._SetupTplElf()
  3700. data = self._DoReadFile('193_tpl_bss_pad.dts')
  3701. self.assertEqual(U_BOOT_TPL_DATA + tools.GetBytes(0, 10) + U_BOOT_DATA,
  3702. data)
  3703. def testTplBssPadMissing(self):
  3704. """Test that a missing symbol is detected"""
  3705. self._SetupTplElf('u_boot_ucode_ptr')
  3706. with self.assertRaises(ValueError) as e:
  3707. self._DoReadFile('193_tpl_bss_pad.dts')
  3708. self.assertIn('Expected __bss_size symbol in tpl/u-boot-tpl',
  3709. str(e.exception))
  3710. def checkDtbSizes(self, data, pad_len, start):
  3711. """Check the size arguments in a dtb embedded in an image
  3712. Args:
  3713. data: The image data
  3714. pad_len: Length of the pad section in the image, in bytes
  3715. start: Start offset of the devicetree to examine, within the image
  3716. Returns:
  3717. Size of the devicetree in bytes
  3718. """
  3719. dtb_data = data[start:]
  3720. dtb = fdt.Fdt.FromData(dtb_data)
  3721. fdt_size = dtb.GetFdtObj().totalsize()
  3722. dtb.Scan()
  3723. props = self._GetPropTree(dtb, 'size')
  3724. self.assertEqual({
  3725. 'size': len(data),
  3726. 'u-boot-spl/u-boot-spl-bss-pad:size': pad_len,
  3727. 'u-boot-spl/u-boot-spl-dtb:size': 801,
  3728. 'u-boot-spl/u-boot-spl-nodtb:size': len(U_BOOT_SPL_NODTB_DATA),
  3729. 'u-boot-spl:size': 860,
  3730. 'u-boot-tpl:size': len(U_BOOT_TPL_DATA),
  3731. 'u-boot/u-boot-dtb:size': 781,
  3732. 'u-boot/u-boot-nodtb:size': len(U_BOOT_NODTB_DATA),
  3733. 'u-boot:size': 827,
  3734. }, props)
  3735. return fdt_size
  3736. def testExpanded(self):
  3737. """Test that an expanded entry type is selected when needed"""
  3738. self._SetupSplElf()
  3739. self._SetupTplElf()
  3740. # SPL has a devicetree, TPL does not
  3741. entry_args = {
  3742. 'spl-dtb': '1',
  3743. 'spl-bss-pad': 'y',
  3744. 'tpl-dtb': '',
  3745. }
  3746. self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True,
  3747. entry_args=entry_args)
  3748. image = control.images['image']
  3749. entries = image.GetEntries()
  3750. self.assertEqual(3, len(entries))
  3751. # First, u-boot, which should be expanded into u-boot-nodtb and dtb
  3752. self.assertIn('u-boot', entries)
  3753. entry = entries['u-boot']
  3754. self.assertEqual('u-boot-expanded', entry.etype)
  3755. subent = entry.GetEntries()
  3756. self.assertEqual(2, len(subent))
  3757. self.assertIn('u-boot-nodtb', subent)
  3758. self.assertIn('u-boot-dtb', subent)
  3759. # Second, u-boot-spl, which should be expanded into three parts
  3760. self.assertIn('u-boot-spl', entries)
  3761. entry = entries['u-boot-spl']
  3762. self.assertEqual('u-boot-spl-expanded', entry.etype)
  3763. subent = entry.GetEntries()
  3764. self.assertEqual(3, len(subent))
  3765. self.assertIn('u-boot-spl-nodtb', subent)
  3766. self.assertIn('u-boot-spl-bss-pad', subent)
  3767. self.assertIn('u-boot-spl-dtb', subent)
  3768. # Third, u-boot-tpl, which should be not be expanded, since TPL has no
  3769. # devicetree
  3770. self.assertIn('u-boot-tpl', entries)
  3771. entry = entries['u-boot-tpl']
  3772. self.assertEqual('u-boot-tpl', entry.etype)
  3773. self.assertEqual(None, entry.GetEntries())
  3774. def testExpandedTpl(self):
  3775. """Test that an expanded entry type is selected for TPL when needed"""
  3776. self._SetupTplElf()
  3777. entry_args = {
  3778. 'tpl-bss-pad': 'y',
  3779. 'tpl-dtb': 'y',
  3780. }
  3781. self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True,
  3782. entry_args=entry_args)
  3783. image = control.images['image']
  3784. entries = image.GetEntries()
  3785. self.assertEqual(1, len(entries))
  3786. # We only have u-boot-tpl, which be expanded
  3787. self.assertIn('u-boot-tpl', entries)
  3788. entry = entries['u-boot-tpl']
  3789. self.assertEqual('u-boot-tpl-expanded', entry.etype)
  3790. subent = entry.GetEntries()
  3791. self.assertEqual(3, len(subent))
  3792. self.assertIn('u-boot-tpl-nodtb', subent)
  3793. self.assertIn('u-boot-tpl-bss-pad', subent)
  3794. self.assertIn('u-boot-tpl-dtb', subent)
  3795. def testExpandedNoPad(self):
  3796. """Test an expanded entry without BSS pad enabled"""
  3797. self._SetupSplElf()
  3798. self._SetupTplElf()
  3799. # SPL has a devicetree, TPL does not
  3800. entry_args = {
  3801. 'spl-dtb': 'something',
  3802. 'spl-bss-pad': 'n',
  3803. 'tpl-dtb': '',
  3804. }
  3805. self._DoReadFileDtb('194_fdt_incl.dts', use_expanded=True,
  3806. entry_args=entry_args)
  3807. image = control.images['image']
  3808. entries = image.GetEntries()
  3809. # Just check u-boot-spl, which should be expanded into two parts
  3810. self.assertIn('u-boot-spl', entries)
  3811. entry = entries['u-boot-spl']
  3812. self.assertEqual('u-boot-spl-expanded', entry.etype)
  3813. subent = entry.GetEntries()
  3814. self.assertEqual(2, len(subent))
  3815. self.assertIn('u-boot-spl-nodtb', subent)
  3816. self.assertIn('u-boot-spl-dtb', subent)
  3817. def testExpandedTplNoPad(self):
  3818. """Test that an expanded entry type with padding disabled in TPL"""
  3819. self._SetupTplElf()
  3820. entry_args = {
  3821. 'tpl-bss-pad': '',
  3822. 'tpl-dtb': 'y',
  3823. }
  3824. self._DoReadFileDtb('195_fdt_incl_tpl.dts', use_expanded=True,
  3825. entry_args=entry_args)
  3826. image = control.images['image']
  3827. entries = image.GetEntries()
  3828. self.assertEqual(1, len(entries))
  3829. # We only have u-boot-tpl, which be expanded
  3830. self.assertIn('u-boot-tpl', entries)
  3831. entry = entries['u-boot-tpl']
  3832. self.assertEqual('u-boot-tpl-expanded', entry.etype)
  3833. subent = entry.GetEntries()
  3834. self.assertEqual(2, len(subent))
  3835. self.assertIn('u-boot-tpl-nodtb', subent)
  3836. self.assertIn('u-boot-tpl-dtb', subent)
  3837. def testFdtInclude(self):
  3838. """Test that an Fdt is update within all binaries"""
  3839. self._SetupSplElf()
  3840. self._SetupTplElf()
  3841. # SPL has a devicetree, TPL does not
  3842. self.maxDiff = None
  3843. entry_args = {
  3844. 'spl-dtb': '1',
  3845. 'spl-bss-pad': 'y',
  3846. 'tpl-dtb': '',
  3847. }
  3848. # Build the image. It includes two separate devicetree binaries, each
  3849. # with their own contents, but all contain the binman definition.
  3850. data = self._DoReadFileDtb(
  3851. '194_fdt_incl.dts', use_real_dtb=True, use_expanded=True,
  3852. update_dtb=True, entry_args=entry_args)[0]
  3853. pad_len = 10
  3854. # Check the U-Boot dtb
  3855. start = len(U_BOOT_NODTB_DATA)
  3856. fdt_size = self.checkDtbSizes(data, pad_len, start)
  3857. # Now check SPL
  3858. start += fdt_size + len(U_BOOT_SPL_NODTB_DATA) + pad_len
  3859. fdt_size = self.checkDtbSizes(data, pad_len, start)
  3860. # TPL has no devicetree
  3861. start += fdt_size + len(U_BOOT_TPL_DATA)
  3862. self.assertEqual(len(data), start)
  3863. def testSymbolsExpanded(self):
  3864. """Test binman can assign symbols in expanded entries"""
  3865. entry_args = {
  3866. 'spl-dtb': '1',
  3867. }
  3868. self.checkSymbols('197_symbols_expand.dts', U_BOOT_SPL_NODTB_DATA +
  3869. U_BOOT_SPL_DTB_DATA, 0x38,
  3870. entry_args=entry_args, use_expanded=True)
  3871. def testCollection(self):
  3872. """Test a collection"""
  3873. data = self._DoReadFile('198_collection.dts')
  3874. self.assertEqual(U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA +
  3875. tools.GetBytes(0xff, 2) + U_BOOT_NODTB_DATA +
  3876. tools.GetBytes(0xfe, 3) + U_BOOT_DTB_DATA,
  3877. data)
  3878. def testCollectionSection(self):
  3879. """Test a collection where a section must be built first"""
  3880. # Sections never have their contents when GetData() is called, but when
  3881. # _BuildSectionData() is called with required=True, a section will force
  3882. # building the contents, producing an error is anything is still
  3883. # missing.
  3884. data = self._DoReadFile('199_collection_section.dts')
  3885. section = U_BOOT_NODTB_DATA + U_BOOT_DTB_DATA
  3886. self.assertEqual(section + U_BOOT_DATA + tools.GetBytes(0xff, 2) +
  3887. section + tools.GetBytes(0xfe, 3) + U_BOOT_DATA,
  3888. data)
  3889. def testAlignDefault(self):
  3890. """Test that default alignment works on sections"""
  3891. data = self._DoReadFile('200_align_default.dts')
  3892. expected = (U_BOOT_DATA + tools.GetBytes(0, 8 - len(U_BOOT_DATA)) +
  3893. U_BOOT_DATA)
  3894. # Special alignment for section
  3895. expected += tools.GetBytes(0, 32 - len(expected))
  3896. # No alignment within the nested section
  3897. expected += U_BOOT_DATA + U_BOOT_NODTB_DATA;
  3898. # Now the final piece, which should be default-aligned
  3899. expected += tools.GetBytes(0, 88 - len(expected)) + U_BOOT_NODTB_DATA
  3900. self.assertEqual(expected, data)
  3901. def testPackOpenSBI(self):
  3902. """Test that an image with an OpenSBI binary can be created"""
  3903. data = self._DoReadFile('201_opensbi.dts')
  3904. self.assertEqual(OPENSBI_DATA, data[:len(OPENSBI_DATA)])
  3905. def testSectionsSingleThread(self):
  3906. """Test sections without multithreading"""
  3907. data = self._DoReadFileDtb('055_sections.dts', threads=0)[0]
  3908. expected = (U_BOOT_DATA + tools.GetBytes(ord('!'), 12) +
  3909. U_BOOT_DATA + tools.GetBytes(ord('a'), 12) +
  3910. U_BOOT_DATA + tools.GetBytes(ord('&'), 4))
  3911. self.assertEqual(expected, data)
  3912. def testThreadTimeout(self):
  3913. """Test handling a thread that takes too long"""
  3914. with self.assertRaises(ValueError) as e:
  3915. self._DoTestFile('202_section_timeout.dts',
  3916. test_section_timeout=True)
  3917. self.assertIn("Node '/binman/section@0': Timed out obtaining contents",
  3918. str(e.exception))
  3919. def testTiming(self):
  3920. """Test output of timing information"""
  3921. data = self._DoReadFile('055_sections.dts')
  3922. with test_util.capture_sys_output() as (stdout, stderr):
  3923. state.TimingShow()
  3924. self.assertIn('read:', stdout.getvalue())
  3925. self.assertIn('compress:', stdout.getvalue())
  3926. if __name__ == "__main__":
  3927. unittest.main()