GenFvInternalLib.c 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280
  1. /** @file
  2. This file contains the internal functions required to generate a Firmware Volume.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. Portions Copyright (c) 2011 - 2013, ARM Ltd. All rights reserved.<BR>
  5. Portions Copyright (c) 2016 HP Development Company, L.P.<BR>
  6. This program and the accompanying materials
  7. are licensed and made available under the terms and conditions of the BSD License
  8. which accompanies this distribution. The full text of the license may be found at
  9. http://opensource.org/licenses/bsd-license.php
  10. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  12. **/
  13. //
  14. // Include files
  15. //
  16. #if defined(__FreeBSD__)
  17. #include <uuid.h>
  18. #elif defined(__GNUC__)
  19. #include <uuid/uuid.h>
  20. #endif
  21. #ifdef __GNUC__
  22. #include <sys/stat.h>
  23. #endif
  24. #include <string.h>
  25. #ifndef __GNUC__
  26. #include <io.h>
  27. #endif
  28. #include <assert.h>
  29. #include <Guid/FfsSectionAlignmentPadding.h>
  30. #include "WinNtInclude.h"
  31. #include "GenFvInternalLib.h"
  32. #include "FvLib.h"
  33. #include "PeCoffLib.h"
  34. #define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION 0xEB000000
  35. #define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION 0x14000000
  36. BOOLEAN mArm = FALSE;
  37. STATIC UINT32 MaxFfsAlignment = 0;
  38. BOOLEAN VtfFileFlag = FALSE;
  39. EFI_GUID mEfiFirmwareVolumeTopFileGuid = EFI_FFS_VOLUME_TOP_FILE_GUID;
  40. EFI_GUID mFileGuidArray [MAX_NUMBER_OF_FILES_IN_FV];
  41. EFI_GUID mZeroGuid = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
  42. EFI_GUID mDefaultCapsuleGuid = {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};
  43. EFI_GUID mEfiFfsSectionAlignmentPaddingGuid = EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID;
  44. CHAR8 *mFvbAttributeName[] = {
  45. EFI_FVB2_READ_DISABLED_CAP_STRING,
  46. EFI_FVB2_READ_ENABLED_CAP_STRING,
  47. EFI_FVB2_READ_STATUS_STRING,
  48. EFI_FVB2_WRITE_DISABLED_CAP_STRING,
  49. EFI_FVB2_WRITE_ENABLED_CAP_STRING,
  50. EFI_FVB2_WRITE_STATUS_STRING,
  51. EFI_FVB2_LOCK_CAP_STRING,
  52. EFI_FVB2_LOCK_STATUS_STRING,
  53. NULL,
  54. EFI_FVB2_STICKY_WRITE_STRING,
  55. EFI_FVB2_MEMORY_MAPPED_STRING,
  56. EFI_FVB2_ERASE_POLARITY_STRING,
  57. EFI_FVB2_READ_LOCK_CAP_STRING,
  58. EFI_FVB2_READ_LOCK_STATUS_STRING,
  59. EFI_FVB2_WRITE_LOCK_CAP_STRING,
  60. EFI_FVB2_WRITE_LOCK_STATUS_STRING
  61. };
  62. CHAR8 *mFvbAlignmentName[] = {
  63. EFI_FVB2_ALIGNMENT_1_STRING,
  64. EFI_FVB2_ALIGNMENT_2_STRING,
  65. EFI_FVB2_ALIGNMENT_4_STRING,
  66. EFI_FVB2_ALIGNMENT_8_STRING,
  67. EFI_FVB2_ALIGNMENT_16_STRING,
  68. EFI_FVB2_ALIGNMENT_32_STRING,
  69. EFI_FVB2_ALIGNMENT_64_STRING,
  70. EFI_FVB2_ALIGNMENT_128_STRING,
  71. EFI_FVB2_ALIGNMENT_256_STRING,
  72. EFI_FVB2_ALIGNMENT_512_STRING,
  73. EFI_FVB2_ALIGNMENT_1K_STRING,
  74. EFI_FVB2_ALIGNMENT_2K_STRING,
  75. EFI_FVB2_ALIGNMENT_4K_STRING,
  76. EFI_FVB2_ALIGNMENT_8K_STRING,
  77. EFI_FVB2_ALIGNMENT_16K_STRING,
  78. EFI_FVB2_ALIGNMENT_32K_STRING,
  79. EFI_FVB2_ALIGNMENT_64K_STRING,
  80. EFI_FVB2_ALIGNMENT_128K_STRING,
  81. EFI_FVB2_ALIGNMENT_256K_STRING,
  82. EFI_FVB2_ALIGNMENT_512K_STRING,
  83. EFI_FVB2_ALIGNMENT_1M_STRING,
  84. EFI_FVB2_ALIGNMENT_2M_STRING,
  85. EFI_FVB2_ALIGNMENT_4M_STRING,
  86. EFI_FVB2_ALIGNMENT_8M_STRING,
  87. EFI_FVB2_ALIGNMENT_16M_STRING,
  88. EFI_FVB2_ALIGNMENT_32M_STRING,
  89. EFI_FVB2_ALIGNMENT_64M_STRING,
  90. EFI_FVB2_ALIGNMENT_128M_STRING,
  91. EFI_FVB2_ALIGNMENT_256M_STRING,
  92. EFI_FVB2_ALIGNMENT_512M_STRING,
  93. EFI_FVB2_ALIGNMENT_1G_STRING,
  94. EFI_FVB2_ALIGNMENT_2G_STRING
  95. };
  96. //
  97. // This data array will be located at the base of the Firmware Volume Header (FVH)
  98. // in the boot block. It must not exceed 14 bytes of code. The last 2 bytes
  99. // will be used to keep the FVH checksum consistent.
  100. // This code will be run in response to a startup IPI for HT-enabled systems.
  101. //
  102. #define SIZEOF_STARTUP_DATA_ARRAY 0x10
  103. UINT8 m128kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
  104. //
  105. // EA D0 FF 00 F0 ; far jmp F000:FFD0
  106. // 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
  107. // 0, 0 ; Checksum Padding
  108. //
  109. 0xEA,
  110. 0xD0,
  111. 0xFF,
  112. 0x0,
  113. 0xF0,
  114. 0x00,
  115. 0x00,
  116. 0x00,
  117. 0x00,
  118. 0x00,
  119. 0x00,
  120. 0x00,
  121. 0x00,
  122. 0x00,
  123. 0x00,
  124. 0x00
  125. };
  126. UINT8 m64kRecoveryStartupApDataArray[SIZEOF_STARTUP_DATA_ARRAY] = {
  127. //
  128. // EB CE ; jmp short ($-0x30)
  129. // ; (from offset 0x0 to offset 0xFFD0)
  130. // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ; Reserved bytes
  131. // 0, 0 ; Checksum Padding
  132. //
  133. 0xEB,
  134. 0xCE,
  135. 0x00,
  136. 0x00,
  137. 0x00,
  138. 0x00,
  139. 0x00,
  140. 0x00,
  141. 0x00,
  142. 0x00,
  143. 0x00,
  144. 0x00,
  145. 0x00,
  146. 0x00,
  147. 0x00,
  148. 0x00
  149. };
  150. FV_INFO mFvDataInfo;
  151. CAP_INFO mCapDataInfo;
  152. BOOLEAN mIsLargeFfs = FALSE;
  153. EFI_PHYSICAL_ADDRESS mFvBaseAddress[0x10];
  154. UINT32 mFvBaseAddressNumber = 0;
  155. EFI_STATUS
  156. ParseFvInf (
  157. IN MEMORY_FILE *InfFile,
  158. OUT FV_INFO *FvInfo
  159. )
  160. /*++
  161. Routine Description:
  162. This function parses a FV.INF file and copies info into a FV_INFO structure.
  163. Arguments:
  164. InfFile Memory file image.
  165. FvInfo Information read from INF file.
  166. Returns:
  167. EFI_SUCCESS INF file information successfully retrieved.
  168. EFI_ABORTED INF file has an invalid format.
  169. EFI_NOT_FOUND A required string was not found in the INF file.
  170. --*/
  171. {
  172. CHAR8 Value[MAX_LONG_FILE_PATH];
  173. UINT64 Value64;
  174. UINTN Index;
  175. UINTN Number;
  176. EFI_STATUS Status;
  177. EFI_GUID GuidValue;
  178. //
  179. // Read the FV base address
  180. //
  181. if (!mFvDataInfo.BaseAddressSet) {
  182. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_BASE_ADDRESS_STRING, 0, Value);
  183. if (Status == EFI_SUCCESS) {
  184. //
  185. // Get the base address
  186. //
  187. Status = AsciiStringToUint64 (Value, FALSE, &Value64);
  188. if (EFI_ERROR (Status)) {
  189. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_BASE_ADDRESS_STRING, Value);
  190. return EFI_ABORTED;
  191. }
  192. DebugMsg (NULL, 0, 9, "rebase address", "%s = %s", EFI_FV_BASE_ADDRESS_STRING, Value);
  193. FvInfo->BaseAddress = Value64;
  194. FvInfo->BaseAddressSet = TRUE;
  195. }
  196. }
  197. //
  198. // Read the FV File System Guid
  199. //
  200. if (!FvInfo->FvFileSystemGuidSet) {
  201. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILESYSTEMGUID_STRING, 0, Value);
  202. if (Status == EFI_SUCCESS) {
  203. //
  204. // Get the guid value
  205. //
  206. Status = StringToGuid (Value, &GuidValue);
  207. if (EFI_ERROR (Status)) {
  208. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_FV_FILESYSTEMGUID_STRING, Value);
  209. return EFI_ABORTED;
  210. }
  211. memcpy (&FvInfo->FvFileSystemGuid, &GuidValue, sizeof (EFI_GUID));
  212. FvInfo->FvFileSystemGuidSet = TRUE;
  213. }
  214. }
  215. //
  216. // Read the FV Extension Header File Name
  217. //
  218. Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FV_EXT_HEADER_FILE_NAME, 0, Value);
  219. if (Status == EFI_SUCCESS) {
  220. strcpy (FvInfo->FvExtHeaderFile, Value);
  221. }
  222. //
  223. // Read the FV file name
  224. //
  225. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FV_FILE_NAME_STRING, 0, Value);
  226. if (Status == EFI_SUCCESS) {
  227. //
  228. // copy the file name
  229. //
  230. strcpy (FvInfo->FvName, Value);
  231. }
  232. //
  233. // Read Fv Attribute
  234. //
  235. for (Index = 0; Index < sizeof (mFvbAttributeName)/sizeof (CHAR8 *); Index ++) {
  236. if ((mFvbAttributeName [Index] != NULL) && \
  237. (FindToken (InfFile, ATTRIBUTES_SECTION_STRING, mFvbAttributeName [Index], 0, Value) == EFI_SUCCESS)) {
  238. if ((strcmp (Value, TRUE_STRING) == 0) || (strcmp (Value, ONE_STRING) == 0)) {
  239. FvInfo->FvAttributes |= 1 << Index;
  240. } else if ((strcmp (Value, FALSE_STRING) != 0) && (strcmp (Value, ZERO_STRING) != 0)) {
  241. Error (NULL, 0, 2000, "Invalid parameter", "%s expected %s | %s", mFvbAttributeName [Index], TRUE_STRING, FALSE_STRING);
  242. return EFI_ABORTED;
  243. }
  244. }
  245. }
  246. //
  247. // Read Fv Alignment
  248. //
  249. for (Index = 0; Index < sizeof (mFvbAlignmentName)/sizeof (CHAR8 *); Index ++) {
  250. if (FindToken (InfFile, ATTRIBUTES_SECTION_STRING, mFvbAlignmentName [Index], 0, Value) == EFI_SUCCESS) {
  251. if (strcmp (Value, TRUE_STRING) == 0) {
  252. FvInfo->FvAttributes |= Index << 16;
  253. DebugMsg (NULL, 0, 9, "FV file alignment", "Align = %s", mFvbAlignmentName [Index]);
  254. break;
  255. }
  256. }
  257. }
  258. //
  259. // Read weak alignment flag
  260. //
  261. Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FV_WEAK_ALIGNMENT_STRING, 0, Value);
  262. if (Status == EFI_SUCCESS) {
  263. if ((strcmp (Value, TRUE_STRING) == 0) || (strcmp (Value, ONE_STRING) == 0)) {
  264. FvInfo->FvAttributes |= EFI_FVB2_WEAK_ALIGNMENT;
  265. } else if ((strcmp (Value, FALSE_STRING) != 0) && (strcmp (Value, ZERO_STRING) != 0)) {
  266. Error (NULL, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");
  267. return EFI_ABORTED;
  268. }
  269. }
  270. //
  271. // Read block maps
  272. //
  273. for (Index = 0; Index < MAX_NUMBER_OF_FV_BLOCKS; Index++) {
  274. if (FvInfo->FvBlocks[Index].Length == 0) {
  275. //
  276. // Read block size
  277. //
  278. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_BLOCK_SIZE_STRING, Index, Value);
  279. if (Status == EFI_SUCCESS) {
  280. //
  281. // Update the size of block
  282. //
  283. Status = AsciiStringToUint64 (Value, FALSE, &Value64);
  284. if (EFI_ERROR (Status)) {
  285. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_BLOCK_SIZE_STRING, Value);
  286. return EFI_ABORTED;
  287. }
  288. FvInfo->FvBlocks[Index].Length = (UINT32) Value64;
  289. DebugMsg (NULL, 0, 9, "FV Block Size", "%s = %s", EFI_BLOCK_SIZE_STRING, Value);
  290. } else {
  291. //
  292. // If there is no blocks size, but there is the number of block, then we have a mismatched pair
  293. // and should return an error.
  294. //
  295. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);
  296. if (!EFI_ERROR (Status)) {
  297. Error (NULL, 0, 2000, "Invalid parameter", "both %s and %s must be specified.", EFI_NUM_BLOCKS_STRING, EFI_BLOCK_SIZE_STRING);
  298. return EFI_ABORTED;
  299. } else {
  300. //
  301. // We are done
  302. //
  303. break;
  304. }
  305. }
  306. //
  307. // Read blocks number
  308. //
  309. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_NUM_BLOCKS_STRING, Index, Value);
  310. if (Status == EFI_SUCCESS) {
  311. //
  312. // Update the number of blocks
  313. //
  314. Status = AsciiStringToUint64 (Value, FALSE, &Value64);
  315. if (EFI_ERROR (Status)) {
  316. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_NUM_BLOCKS_STRING, Value);
  317. return EFI_ABORTED;
  318. }
  319. FvInfo->FvBlocks[Index].NumBlocks = (UINT32) Value64;
  320. DebugMsg (NULL, 0, 9, "FV Block Number", "%s = %s", EFI_NUM_BLOCKS_STRING, Value);
  321. }
  322. }
  323. }
  324. if (Index == 0) {
  325. Error (NULL, 0, 2001, "Missing required argument", "block size.");
  326. return EFI_ABORTED;
  327. }
  328. //
  329. // Read files
  330. //
  331. Number = 0;
  332. for (Number = 0; Number < MAX_NUMBER_OF_FILES_IN_FV; Number ++) {
  333. if (FvInfo->FvFiles[Number][0] == '\0') {
  334. break;
  335. }
  336. }
  337. for (Index = 0; Number + Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {
  338. //
  339. // Read the FFS file list
  340. //
  341. Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Index, Value);
  342. if (Status == EFI_SUCCESS) {
  343. //
  344. // Add the file
  345. //
  346. strcpy (FvInfo->FvFiles[Number + Index], Value);
  347. DebugMsg (NULL, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index, Value);
  348. } else {
  349. break;
  350. }
  351. }
  352. if ((Index + Number) == 0) {
  353. Warning (NULL, 0, 0, "FV components are not specified.", NULL);
  354. }
  355. return EFI_SUCCESS;
  356. }
  357. VOID
  358. UpdateFfsFileState (
  359. IN EFI_FFS_FILE_HEADER *FfsFile,
  360. IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
  361. )
  362. /*++
  363. Routine Description:
  364. This function changes the FFS file attributes based on the erase polarity
  365. of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.
  366. Arguments:
  367. FfsFile File header.
  368. FvHeader FV header.
  369. Returns:
  370. None
  371. --*/
  372. {
  373. if (FvHeader->Attributes & EFI_FVB2_ERASE_POLARITY) {
  374. FfsFile->State = (UINT8)~(FfsFile->State);
  375. // FfsFile->State |= ~(UINT8) EFI_FILE_ALL_STATE_BITS;
  376. }
  377. }
  378. EFI_STATUS
  379. ReadFfsAlignment (
  380. IN EFI_FFS_FILE_HEADER *FfsFile,
  381. IN OUT UINT32 *Alignment
  382. )
  383. /*++
  384. Routine Description:
  385. This function determines the alignment of the FFS input file from the file
  386. attributes.
  387. Arguments:
  388. FfsFile FFS file to parse
  389. Alignment The minimum required alignment offset of the FFS file
  390. Returns:
  391. EFI_SUCCESS The function completed successfully.
  392. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  393. EFI_ABORTED An error occurred.
  394. --*/
  395. {
  396. //
  397. // Verify input parameters.
  398. //
  399. if (FfsFile == NULL || Alignment == NULL) {
  400. return EFI_INVALID_PARAMETER;
  401. }
  402. switch ((FfsFile->Attributes >> 3) & 0x07) {
  403. case 0:
  404. //
  405. // 1 byte alignment
  406. //if bit 1 have set, 128K byte alignment
  407. //
  408. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  409. *Alignment = 17;
  410. } else {
  411. *Alignment = 0;
  412. }
  413. break;
  414. case 1:
  415. //
  416. // 16 byte alignment
  417. //if bit 1 have set, 256K byte alignment
  418. //
  419. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  420. *Alignment = 18;
  421. } else {
  422. *Alignment = 4;
  423. }
  424. break;
  425. case 2:
  426. //
  427. // 128 byte alignment
  428. //if bit 1 have set, 512K byte alignment
  429. //
  430. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  431. *Alignment = 19;
  432. } else {
  433. *Alignment = 7;
  434. }
  435. break;
  436. case 3:
  437. //
  438. // 512 byte alignment
  439. //if bit 1 have set, 1M byte alignment
  440. //
  441. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  442. *Alignment = 20;
  443. } else {
  444. *Alignment = 9;
  445. }
  446. break;
  447. case 4:
  448. //
  449. // 1K byte alignment
  450. //if bit 1 have set, 2M byte alignment
  451. //
  452. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  453. *Alignment = 21;
  454. } else {
  455. *Alignment = 10;
  456. }
  457. break;
  458. case 5:
  459. //
  460. // 4K byte alignment
  461. //if bit 1 have set, 4M byte alignment
  462. //
  463. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  464. *Alignment = 22;
  465. } else {
  466. *Alignment = 12;
  467. }
  468. break;
  469. case 6:
  470. //
  471. // 32K byte alignment
  472. //if bit 1 have set , 8M byte alignment
  473. //
  474. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  475. *Alignment = 23;
  476. } else {
  477. *Alignment = 15;
  478. }
  479. break;
  480. case 7:
  481. //
  482. // 64K byte alignment
  483. //if bit 1 have set, 16M alignment
  484. //
  485. if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {
  486. *Alignment = 24;
  487. } else {
  488. *Alignment = 16;
  489. }
  490. break;
  491. default:
  492. break;
  493. }
  494. return EFI_SUCCESS;
  495. }
  496. EFI_STATUS
  497. AddPadFile (
  498. IN OUT MEMORY_FILE *FvImage,
  499. IN UINT32 DataAlignment,
  500. IN VOID *FvEnd,
  501. IN EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader,
  502. IN UINT32 NextFfsSize
  503. )
  504. /*++
  505. Routine Description:
  506. This function adds a pad file to the FV image if it required to align the
  507. data of the next file.
  508. Arguments:
  509. FvImage The memory image of the FV to add it to.
  510. The current offset must be valid.
  511. DataAlignment The data alignment of the next FFS file.
  512. FvEnd End of the empty data in FvImage.
  513. ExtHeader PI FvExtHeader Optional
  514. Returns:
  515. EFI_SUCCESS The function completed successfully.
  516. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  517. EFI_OUT_OF_RESOURCES Insufficient resources exist in the FV to complete
  518. the pad file add.
  519. --*/
  520. {
  521. EFI_FFS_FILE_HEADER *PadFile;
  522. UINTN PadFileSize;
  523. UINT32 NextFfsHeaderSize;
  524. UINT32 CurFfsHeaderSize;
  525. UINT32 Index;
  526. Index = 0;
  527. CurFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  528. //
  529. // Verify input parameters.
  530. //
  531. if (FvImage == NULL) {
  532. return EFI_INVALID_PARAMETER;
  533. }
  534. //
  535. // Calculate the pad file size
  536. //
  537. //
  538. // Append extension header size
  539. //
  540. if (ExtHeader != NULL) {
  541. PadFileSize = ExtHeader->ExtHeaderSize;
  542. if (PadFileSize + sizeof (EFI_FFS_FILE_HEADER) >= MAX_FFS_SIZE) {
  543. CurFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  544. }
  545. PadFileSize += CurFfsHeaderSize;
  546. } else {
  547. NextFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER);
  548. if (NextFfsSize >= MAX_FFS_SIZE) {
  549. NextFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);
  550. }
  551. //
  552. // Check if a pad file is necessary
  553. //
  554. if (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + NextFfsHeaderSize) % DataAlignment == 0) {
  555. return EFI_SUCCESS;
  556. }
  557. PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER) + NextFfsHeaderSize;
  558. //
  559. // Add whatever it takes to get to the next aligned address
  560. //
  561. while ((PadFileSize % DataAlignment) != 0) {
  562. PadFileSize++;
  563. }
  564. //
  565. // Subtract the next file header size
  566. //
  567. PadFileSize -= NextFfsHeaderSize;
  568. //
  569. // Subtract the starting offset to get size
  570. //
  571. PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;
  572. }
  573. //
  574. // Verify that we have enough space for the file header
  575. //
  576. if (((UINTN) FvImage->CurrentFilePointer + PadFileSize) > (UINTN) FvEnd) {
  577. return EFI_OUT_OF_RESOURCES;
  578. }
  579. //
  580. // Write pad file header
  581. //
  582. PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;
  583. //
  584. // Write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
  585. //
  586. PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;
  587. PadFile->Attributes = 0;
  588. //
  589. // Write pad file size (calculated size minus next file header size)
  590. //
  591. if (PadFileSize >= MAX_FFS_SIZE) {
  592. memset(PadFile->Size, 0, sizeof(UINT8) * 3);
  593. ((EFI_FFS_FILE_HEADER2 *)PadFile)->ExtendedSize = PadFileSize;
  594. PadFile->Attributes |= FFS_ATTRIB_LARGE_FILE;
  595. } else {
  596. PadFile->Size[0] = (UINT8) (PadFileSize & 0xFF);
  597. PadFile->Size[1] = (UINT8) ((PadFileSize >> 8) & 0xFF);
  598. PadFile->Size[2] = (UINT8) ((PadFileSize >> 16) & 0xFF);
  599. }
  600. //
  601. // Fill in checksums and state, they must be 0 for checksumming.
  602. //
  603. PadFile->IntegrityCheck.Checksum.Header = 0;
  604. PadFile->IntegrityCheck.Checksum.File = 0;
  605. PadFile->State = 0;
  606. PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, CurFfsHeaderSize);
  607. PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  608. PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;
  609. UpdateFfsFileState (
  610. (EFI_FFS_FILE_HEADER *) PadFile,
  611. (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
  612. );
  613. //
  614. // Update the current FV pointer
  615. //
  616. FvImage->CurrentFilePointer += PadFileSize;
  617. if (ExtHeader != NULL) {
  618. //
  619. // Copy Fv Extension Header and Set Fv Extension header offset
  620. //
  621. if (ExtHeader->ExtHeaderSize > sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER)) {
  622. for (Index = sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER); Index < ExtHeader->ExtHeaderSize;) {
  623. if (((EFI_FIRMWARE_VOLUME_EXT_ENTRY *)((UINT8 *)ExtHeader + Index))-> ExtEntryType == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {
  624. if (VtfFileFlag) {
  625. ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *)((UINT8 *)ExtHeader + Index))->UsedSize = mFvTotalSize;
  626. } else {
  627. ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *)((UINT8 *)ExtHeader + Index))->UsedSize = mFvTakenSize;
  628. }
  629. break;
  630. }
  631. Index += ((EFI_FIRMWARE_VOLUME_EXT_ENTRY *)((UINT8 *)ExtHeader + Index))-> ExtEntrySize;
  632. }
  633. }
  634. memcpy ((UINT8 *)PadFile + CurFfsHeaderSize, ExtHeader, ExtHeader->ExtHeaderSize);
  635. ((EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage)->ExtHeaderOffset = (UINT16) ((UINTN) ((UINT8 *)PadFile + CurFfsHeaderSize) - (UINTN) FvImage->FileImage);
  636. //
  637. // Make next file start at QWord Boundary
  638. //
  639. while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {
  640. FvImage->CurrentFilePointer++;
  641. }
  642. }
  643. return EFI_SUCCESS;
  644. }
  645. BOOLEAN
  646. IsVtfFile (
  647. IN EFI_FFS_FILE_HEADER *FileBuffer
  648. )
  649. /*++
  650. Routine Description:
  651. This function checks the header to validate if it is a VTF file
  652. Arguments:
  653. FileBuffer Buffer in which content of a file has been read.
  654. Returns:
  655. TRUE If this is a VTF file
  656. FALSE If this is not a VTF file
  657. --*/
  658. {
  659. if (!memcmp (&FileBuffer->Name, &mEfiFirmwareVolumeTopFileGuid, sizeof (EFI_GUID))) {
  660. return TRUE;
  661. } else {
  662. return FALSE;
  663. }
  664. }
  665. EFI_STATUS
  666. WriteMapFile (
  667. IN OUT FILE *FvMapFile,
  668. IN CHAR8 *FileName,
  669. IN EFI_FFS_FILE_HEADER *FfsFile,
  670. IN EFI_PHYSICAL_ADDRESS ImageBaseAddress,
  671. IN PE_COFF_LOADER_IMAGE_CONTEXT *pImageContext
  672. )
  673. /*++
  674. Routine Description:
  675. This function gets the basic debug information (entrypoint, baseaddress, .text, .data section base address)
  676. from PE/COFF image and abstracts Pe Map file information and add them into FvMap file for Debug.
  677. Arguments:
  678. FvMapFile A pointer to FvMap File
  679. FileName Ffs File PathName
  680. FfsFile A pointer to Ffs file image.
  681. ImageBaseAddress PeImage Base Address.
  682. pImageContext Image Context Information.
  683. Returns:
  684. EFI_SUCCESS Added required map information.
  685. --*/
  686. {
  687. CHAR8 PeMapFileName [MAX_LONG_FILE_PATH];
  688. CHAR8 *Cptr, *Cptr2;
  689. CHAR8 FileGuidName [MAX_LINE_LEN];
  690. FILE *PeMapFile;
  691. CHAR8 Line [MAX_LINE_LEN];
  692. CHAR8 KeyWord [MAX_LINE_LEN];
  693. CHAR8 FunctionName [MAX_LINE_LEN];
  694. EFI_PHYSICAL_ADDRESS FunctionAddress;
  695. UINT32 FunctionType;
  696. CHAR8 FunctionTypeName [MAX_LINE_LEN];
  697. UINT32 Index;
  698. UINT32 AddressOfEntryPoint;
  699. UINT32 Offset;
  700. EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
  701. EFI_TE_IMAGE_HEADER *TEImageHeader;
  702. EFI_IMAGE_SECTION_HEADER *SectionHeader;
  703. long long TempLongAddress;
  704. UINT32 TextVirtualAddress;
  705. UINT32 DataVirtualAddress;
  706. EFI_PHYSICAL_ADDRESS LinkTimeBaseAddress;
  707. //
  708. // Init local variable
  709. //
  710. FunctionType = 0;
  711. //
  712. // Print FileGuid to string buffer.
  713. //
  714. PrintGuidToBuffer (&FfsFile->Name, (UINT8 *)FileGuidName, MAX_LINE_LEN, TRUE);
  715. //
  716. // Construct Map file Name
  717. //
  718. if (strlen (FileName) >= MAX_LONG_FILE_PATH) {
  719. return EFI_ABORTED;
  720. }
  721. strncpy (PeMapFileName, FileName, MAX_LONG_FILE_PATH - 1);
  722. PeMapFileName[MAX_LONG_FILE_PATH - 1] = 0;
  723. //
  724. // Change '\\' to '/', unified path format.
  725. //
  726. Cptr = PeMapFileName;
  727. while (*Cptr != '\0') {
  728. if (*Cptr == '\\') {
  729. *Cptr = FILE_SEP_CHAR;
  730. }
  731. Cptr ++;
  732. }
  733. //
  734. // Get Map file
  735. //
  736. Cptr = PeMapFileName + strlen (PeMapFileName);
  737. while ((*Cptr != '.') && (Cptr >= PeMapFileName)) {
  738. Cptr --;
  739. }
  740. if (Cptr < PeMapFileName) {
  741. return EFI_NOT_FOUND;
  742. } else {
  743. *(Cptr + 1) = 'm';
  744. *(Cptr + 2) = 'a';
  745. *(Cptr + 3) = 'p';
  746. *(Cptr + 4) = '\0';
  747. }
  748. //
  749. // Get module Name
  750. //
  751. Cptr2 = Cptr;
  752. while ((*Cptr != FILE_SEP_CHAR) && (Cptr >= PeMapFileName)) {
  753. Cptr --;
  754. }
  755. *Cptr2 = '\0';
  756. if (strlen (Cptr + 1) >= MAX_LINE_LEN) {
  757. return EFI_ABORTED;
  758. }
  759. strncpy (KeyWord, Cptr + 1, MAX_LINE_LEN - 1);
  760. KeyWord[MAX_LINE_LEN - 1] = 0;
  761. *Cptr2 = '.';
  762. //
  763. // AddressOfEntryPoint and Offset in Image
  764. //
  765. if (!pImageContext->IsTeImage) {
  766. ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINT8 *) pImageContext->Handle + pImageContext->PeCoffHeaderOffset);
  767. AddressOfEntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
  768. Offset = 0;
  769. SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (
  770. (UINT8 *) ImgHdr +
  771. sizeof (UINT32) +
  772. sizeof (EFI_IMAGE_FILE_HEADER) +
  773. ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
  774. );
  775. Index = ImgHdr->Pe32.FileHeader.NumberOfSections;
  776. } else {
  777. TEImageHeader = (EFI_TE_IMAGE_HEADER *) pImageContext->Handle;
  778. AddressOfEntryPoint = TEImageHeader->AddressOfEntryPoint;
  779. Offset = TEImageHeader->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);
  780. SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);
  781. Index = TEImageHeader->NumberOfSections;
  782. }
  783. //
  784. // module information output
  785. //
  786. if (ImageBaseAddress == 0) {
  787. fprintf (FvMapFile, "%s (dummy) (", KeyWord);
  788. fprintf (FvMapFile, "BaseAddress=%010llx, ", (unsigned long long) ImageBaseAddress);
  789. } else {
  790. fprintf (FvMapFile, "%s (Fixed Flash Address, ", KeyWord);
  791. fprintf (FvMapFile, "BaseAddress=0x%010llx, ", (unsigned long long) (ImageBaseAddress + Offset));
  792. }
  793. fprintf (FvMapFile, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress + AddressOfEntryPoint));
  794. fprintf (FvMapFile, ")\n");
  795. fprintf (FvMapFile, "(GUID=%s", FileGuidName);
  796. TextVirtualAddress = 0;
  797. DataVirtualAddress = 0;
  798. for (; Index > 0; Index --, SectionHeader ++) {
  799. if (stricmp ((CHAR8 *)SectionHeader->Name, ".text") == 0) {
  800. TextVirtualAddress = SectionHeader->VirtualAddress;
  801. } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".data") == 0) {
  802. DataVirtualAddress = SectionHeader->VirtualAddress;
  803. } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".sdata") == 0) {
  804. DataVirtualAddress = SectionHeader->VirtualAddress;
  805. }
  806. }
  807. fprintf (FvMapFile, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + TextVirtualAddress));
  808. fprintf (FvMapFile, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + DataVirtualAddress));
  809. fprintf (FvMapFile, ")\n\n");
  810. //
  811. // Open PeMapFile
  812. //
  813. PeMapFile = fopen (LongFilePath (PeMapFileName), "r");
  814. if (PeMapFile == NULL) {
  815. // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);
  816. return EFI_ABORTED;
  817. }
  818. VerboseMsg ("The map file is %s", PeMapFileName);
  819. //
  820. // Output Functions information into Fv Map file
  821. //
  822. LinkTimeBaseAddress = 0;
  823. while (fgets (Line, MAX_LINE_LEN, PeMapFile) != NULL) {
  824. //
  825. // Skip blank line
  826. //
  827. if (Line[0] == 0x0a) {
  828. FunctionType = 0;
  829. continue;
  830. }
  831. //
  832. // By Address and Static keyword
  833. //
  834. if (FunctionType == 0) {
  835. sscanf (Line, "%s", KeyWord);
  836. if (stricmp (KeyWord, "Address") == 0) {
  837. //
  838. // function list
  839. //
  840. FunctionType = 1;
  841. fgets (Line, MAX_LINE_LEN, PeMapFile);
  842. } else if (stricmp (KeyWord, "Static") == 0) {
  843. //
  844. // static function list
  845. //
  846. FunctionType = 2;
  847. fgets (Line, MAX_LINE_LEN, PeMapFile);
  848. } else if (stricmp (KeyWord, "Preferred") ==0) {
  849. sscanf (Line + strlen (" Preferred load address is"), "%llx", &TempLongAddress);
  850. LinkTimeBaseAddress = (UINT64) TempLongAddress;
  851. }
  852. continue;
  853. }
  854. //
  855. // Printf Function Information
  856. //
  857. if (FunctionType == 1) {
  858. sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);
  859. FunctionAddress = (UINT64) TempLongAddress;
  860. if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {
  861. fprintf (FvMapFile, " 0x%010llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress - LinkTimeBaseAddress));
  862. fprintf (FvMapFile, "%s\n", FunctionName);
  863. }
  864. } else if (FunctionType == 2) {
  865. sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);
  866. FunctionAddress = (UINT64) TempLongAddress;
  867. if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {
  868. fprintf (FvMapFile, " 0x%010llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress - LinkTimeBaseAddress));
  869. fprintf (FvMapFile, "%s\n", FunctionName);
  870. }
  871. }
  872. }
  873. //
  874. // Close PeMap file
  875. //
  876. fprintf (FvMapFile, "\n\n");
  877. fclose (PeMapFile);
  878. return EFI_SUCCESS;
  879. }
  880. STATIC
  881. BOOLEAN
  882. AdjustInternalFfsPadding (
  883. IN OUT EFI_FFS_FILE_HEADER *FfsFile,
  884. IN OUT MEMORY_FILE *FvImage,
  885. IN UINTN Alignment,
  886. IN OUT UINTN *FileSize
  887. )
  888. /*++
  889. Routine Description:
  890. This function looks for a dedicated alignment padding section in the FFS, and
  891. shrinks it to the size required to line up subsequent sections correctly.
  892. Arguments:
  893. FfsFile A pointer to Ffs file image.
  894. FvImage The memory image of the FV to adjust it to.
  895. Alignment Current file alignment
  896. FileSize Reference to a variable holding the size of the FFS file
  897. Returns:
  898. TRUE Padding section was found and updated successfully
  899. FALSE Otherwise
  900. --*/
  901. {
  902. EFI_FILE_SECTION_POINTER PadSection;
  903. UINT8 *Remainder;
  904. EFI_STATUS Status;
  905. UINT32 FfsHeaderLength;
  906. UINT32 FfsFileLength;
  907. UINT32 PadSize;
  908. UINTN Misalignment;
  909. EFI_FFS_INTEGRITY_CHECK *IntegrityCheck;
  910. //
  911. // Figure out the misalignment: all FFS sections are aligned relative to the
  912. // start of the FFS payload, so use that as the base of the misalignment
  913. // computation.
  914. //
  915. FfsHeaderLength = GetFfsHeaderLength(FfsFile);
  916. Misalignment = (UINTN) FvImage->CurrentFilePointer -
  917. (UINTN) FvImage->FileImage + FfsHeaderLength;
  918. Misalignment &= Alignment - 1;
  919. if (Misalignment == 0) {
  920. // Nothing to do, return success
  921. return TRUE;
  922. }
  923. //
  924. // We only apply this optimization to FFS files with the FIXED attribute set,
  925. // since the FFS will not be loadable at arbitrary offsets anymore after
  926. // we adjust the size of the padding section.
  927. //
  928. if ((FfsFile->Attributes & FFS_ATTRIB_FIXED) == 0) {
  929. return FALSE;
  930. }
  931. //
  932. // Look for a dedicated padding section that we can adjust to compensate
  933. // for the misalignment. If such a padding section exists, it precedes all
  934. // sections with alignment requirements, and so the adjustment will correct
  935. // all of them.
  936. //
  937. Status = GetSectionByType (FfsFile, EFI_SECTION_FREEFORM_SUBTYPE_GUID, 1,
  938. &PadSection);
  939. if (EFI_ERROR (Status) ||
  940. CompareGuid (&PadSection.FreeformSubtypeSection->SubTypeGuid,
  941. &mEfiFfsSectionAlignmentPaddingGuid) != 0) {
  942. return FALSE;
  943. }
  944. //
  945. // Find out if the size of the padding section is sufficient to compensate
  946. // for the misalignment.
  947. //
  948. PadSize = GetSectionFileLength (PadSection.CommonHeader);
  949. if (Misalignment > PadSize - sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION)) {
  950. return FALSE;
  951. }
  952. //
  953. // Move the remainder of the FFS file towards the front, and adjust the
  954. // file size output parameter.
  955. //
  956. Remainder = (UINT8 *) PadSection.CommonHeader + PadSize;
  957. memmove (Remainder - Misalignment, Remainder,
  958. *FileSize - (UINTN) (Remainder - (UINTN) FfsFile));
  959. *FileSize -= Misalignment;
  960. //
  961. // Update the padding section's length with the new values. Note that the
  962. // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2
  963. // ExtendedSize.
  964. //
  965. PadSize -= Misalignment;
  966. PadSection.CommonHeader->Size[0] = (UINT8) (PadSize & 0xff);
  967. PadSection.CommonHeader->Size[1] = (UINT8) ((PadSize & 0xff00) >> 8);
  968. PadSection.CommonHeader->Size[2] = (UINT8) ((PadSize & 0xff0000) >> 16);
  969. //
  970. // Update the FFS header with the new overall length
  971. //
  972. FfsFileLength = GetFfsFileLength (FfsFile) - Misalignment;
  973. if (FfsHeaderLength > sizeof(EFI_FFS_FILE_HEADER)) {
  974. ((EFI_FFS_FILE_HEADER2 *)FfsFile)->ExtendedSize = FfsFileLength;
  975. } else {
  976. FfsFile->Size[0] = (UINT8) (FfsFileLength & 0x000000FF);
  977. FfsFile->Size[1] = (UINT8) ((FfsFileLength & 0x0000FF00) >> 8);
  978. FfsFile->Size[2] = (UINT8) ((FfsFileLength & 0x00FF0000) >> 16);
  979. }
  980. //
  981. // Clear the alignment bits: these have become meaningless now that we have
  982. // adjusted the padding section.
  983. //
  984. FfsFile->Attributes &= ~(FFS_ATTRIB_DATA_ALIGNMENT | FFS_ATTRIB_DATA_ALIGNMENT2);
  985. //
  986. // Recalculate the FFS header checksum. Instead of setting Header and State
  987. // both to zero, set Header to (UINT8)(-State) so State preserves its original
  988. // value
  989. //
  990. IntegrityCheck = &FfsFile->IntegrityCheck;
  991. IntegrityCheck->Checksum.Header = (UINT8) (0x100 - FfsFile->State);
  992. IntegrityCheck->Checksum.File = 0;
  993. IntegrityCheck->Checksum.Header = CalculateChecksum8 (
  994. (UINT8 *) FfsFile, FfsHeaderLength);
  995. if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
  996. //
  997. // Ffs header checksum = zero, so only need to calculate ffs body.
  998. //
  999. IntegrityCheck->Checksum.File = CalculateChecksum8 (
  1000. (UINT8 *) FfsFile + FfsHeaderLength,
  1001. FfsFileLength - FfsHeaderLength);
  1002. } else {
  1003. IntegrityCheck->Checksum.File = FFS_FIXED_CHECKSUM;
  1004. }
  1005. return TRUE;
  1006. }
  1007. EFI_STATUS
  1008. AddFile (
  1009. IN OUT MEMORY_FILE *FvImage,
  1010. IN FV_INFO *FvInfo,
  1011. IN UINTN Index,
  1012. IN OUT EFI_FFS_FILE_HEADER **VtfFileImage,
  1013. IN FILE *FvMapFile,
  1014. IN FILE *FvReportFile
  1015. )
  1016. /*++
  1017. Routine Description:
  1018. This function adds a file to the FV image. The file will pad to the
  1019. appropriate alignment if required.
  1020. Arguments:
  1021. FvImage The memory image of the FV to add it to. The current offset
  1022. must be valid.
  1023. FvInfo Pointer to information about the FV.
  1024. Index The file in the FvInfo file list to add.
  1025. VtfFileImage A pointer to the VTF file within the FvImage. If this is equal
  1026. to the end of the FvImage then no VTF previously found.
  1027. FvMapFile Pointer to FvMap File
  1028. FvReportFile Pointer to FvReport File
  1029. Returns:
  1030. EFI_SUCCESS The function completed successfully.
  1031. EFI_INVALID_PARAMETER One of the input parameters was invalid.
  1032. EFI_ABORTED An error occurred.
  1033. EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the add.
  1034. --*/
  1035. {
  1036. FILE *NewFile;
  1037. UINTN FileSize;
  1038. UINT8 *FileBuffer;
  1039. UINTN NumBytesRead;
  1040. UINT32 CurrentFileAlignment;
  1041. EFI_STATUS Status;
  1042. UINTN Index1;
  1043. UINT8 FileGuidString[PRINTED_GUID_BUFFER_SIZE];
  1044. Index1 = 0;
  1045. //
  1046. // Verify input parameters.
  1047. //
  1048. if (FvImage == NULL || FvInfo == NULL || FvInfo->FvFiles[Index][0] == 0 || VtfFileImage == NULL) {
  1049. return EFI_INVALID_PARAMETER;
  1050. }
  1051. //
  1052. // Read the file to add
  1053. //
  1054. NewFile = fopen (LongFilePath (FvInfo->FvFiles[Index]), "rb");
  1055. if (NewFile == NULL) {
  1056. Error (NULL, 0, 0001, "Error opening file", FvInfo->FvFiles[Index]);
  1057. return EFI_ABORTED;
  1058. }
  1059. //
  1060. // Get the file size
  1061. //
  1062. FileSize = _filelength (fileno (NewFile));
  1063. //
  1064. // Read the file into a buffer
  1065. //
  1066. FileBuffer = malloc (FileSize);
  1067. if (FileBuffer == NULL) {
  1068. fclose (NewFile);
  1069. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  1070. return EFI_OUT_OF_RESOURCES;
  1071. }
  1072. NumBytesRead = fread (FileBuffer, sizeof (UINT8), FileSize, NewFile);
  1073. //
  1074. // Done with the file, from this point on we will just use the buffer read.
  1075. //
  1076. fclose (NewFile);
  1077. //
  1078. // Verify read successful
  1079. //
  1080. if (NumBytesRead != sizeof (UINT8) * FileSize) {
  1081. free (FileBuffer);
  1082. Error (NULL, 0, 0004, "Error reading file", FvInfo->FvFiles[Index]);
  1083. return EFI_ABORTED;
  1084. }
  1085. //
  1086. // For None PI Ffs file, directly add them into FvImage.
  1087. //
  1088. if (!FvInfo->IsPiFvImage) {
  1089. memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);
  1090. if (FvInfo->SizeofFvFiles[Index] > FileSize) {
  1091. FvImage->CurrentFilePointer += FvInfo->SizeofFvFiles[Index];
  1092. } else {
  1093. FvImage->CurrentFilePointer += FileSize;
  1094. }
  1095. goto Done;
  1096. }
  1097. //
  1098. // Verify Ffs file
  1099. //
  1100. Status = VerifyFfsFile ((EFI_FFS_FILE_HEADER *)FileBuffer);
  1101. if (EFI_ERROR (Status)) {
  1102. free (FileBuffer);
  1103. Error (NULL, 0, 3000, "Invalid", "%s is not a valid FFS file.", FvInfo->FvFiles[Index]);
  1104. return EFI_INVALID_PARAMETER;
  1105. }
  1106. //
  1107. // Verify space exists to add the file
  1108. //
  1109. if (FileSize > (UINTN) ((UINTN) *VtfFileImage - (UINTN) FvImage->CurrentFilePointer)) {
  1110. free (FileBuffer);
  1111. Error (NULL, 0, 4002, "Resource", "FV space is full, not enough room to add file %s.", FvInfo->FvFiles[Index]);
  1112. return EFI_OUT_OF_RESOURCES;
  1113. }
  1114. //
  1115. // Verify the input file is the duplicated file in this Fv image
  1116. //
  1117. for (Index1 = 0; Index1 < Index; Index1 ++) {
  1118. if (CompareGuid ((EFI_GUID *) FileBuffer, &mFileGuidArray [Index1]) == 0) {
  1119. Error (NULL, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1 + 1, (unsigned) Index + 1);
  1120. PrintGuid ((EFI_GUID *) FileBuffer);
  1121. free (FileBuffer);
  1122. return EFI_INVALID_PARAMETER;
  1123. }
  1124. }
  1125. CopyMem (&mFileGuidArray [Index], FileBuffer, sizeof (EFI_GUID));
  1126. //
  1127. // Update the file state based on polarity of the FV.
  1128. //
  1129. UpdateFfsFileState (
  1130. (EFI_FFS_FILE_HEADER *) FileBuffer,
  1131. (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
  1132. );
  1133. //
  1134. // Check if alignment is required
  1135. //
  1136. ReadFfsAlignment ((EFI_FFS_FILE_HEADER *) FileBuffer, &CurrentFileAlignment);
  1137. //
  1138. // Find the largest alignment of all the FFS files in the FV
  1139. //
  1140. if (CurrentFileAlignment > MaxFfsAlignment) {
  1141. MaxFfsAlignment = CurrentFileAlignment;
  1142. }
  1143. //
  1144. // If we have a VTF file, add it at the top.
  1145. //
  1146. if (IsVtfFile ((EFI_FFS_FILE_HEADER *) FileBuffer)) {
  1147. if ((UINTN) *VtfFileImage == (UINTN) FvImage->Eof) {
  1148. //
  1149. // No previous VTF, add this one.
  1150. //
  1151. *VtfFileImage = (EFI_FFS_FILE_HEADER *) (UINTN) ((UINTN) FvImage->FileImage + FvInfo->Size - FileSize);
  1152. //
  1153. // Sanity check. The file MUST align appropriately
  1154. //
  1155. if (((UINTN) *VtfFileImage + GetFfsHeaderLength((EFI_FFS_FILE_HEADER *)FileBuffer) - (UINTN) FvImage->FileImage) % (1 << CurrentFileAlignment)) {
  1156. Error (NULL, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment));
  1157. free (FileBuffer);
  1158. return EFI_ABORTED;
  1159. }
  1160. //
  1161. // Rebase the PE or TE image in FileBuffer of FFS file for XIP
  1162. // Rebase for the debug genfvmap tool
  1163. //
  1164. Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) *VtfFileImage - (UINTN) FvImage->FileImage, FvMapFile);
  1165. if (EFI_ERROR (Status)) {
  1166. Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);
  1167. return Status;
  1168. }
  1169. //
  1170. // copy VTF File
  1171. //
  1172. memcpy (*VtfFileImage, FileBuffer, FileSize);
  1173. PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);
  1174. fprintf (FvReportFile, "0x%08X %s\n", (unsigned)(UINTN) (((UINT8 *)*VtfFileImage) - (UINTN)FvImage->FileImage), FileGuidString);
  1175. free (FileBuffer);
  1176. DebugMsg (NULL, 0, 9, "Add VTF FFS file in FV image", NULL);
  1177. return EFI_SUCCESS;
  1178. } else {
  1179. //
  1180. // Already found a VTF file.
  1181. //
  1182. Error (NULL, 0, 3000, "Invalid", "multiple VTF files are not permitted within a single FV.");
  1183. free (FileBuffer);
  1184. return EFI_ABORTED;
  1185. }
  1186. }
  1187. //
  1188. // Add pad file if necessary
  1189. //
  1190. if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER *) FileBuffer, FvImage,
  1191. 1 << CurrentFileAlignment, &FileSize)) {
  1192. Status = AddPadFile (FvImage, 1 << CurrentFileAlignment, *VtfFileImage, NULL, FileSize);
  1193. if (EFI_ERROR (Status)) {
  1194. Error (NULL, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");
  1195. free (FileBuffer);
  1196. return EFI_ABORTED;
  1197. }
  1198. }
  1199. //
  1200. // Add file
  1201. //
  1202. if ((UINTN) (FvImage->CurrentFilePointer + FileSize) <= (UINTN) (*VtfFileImage)) {
  1203. //
  1204. // Rebase the PE or TE image in FileBuffer of FFS file for XIP.
  1205. // Rebase Bs and Rt drivers for the debug genfvmap tool.
  1206. //
  1207. Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage, FvMapFile);
  1208. if (EFI_ERROR (Status)) {
  1209. Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);
  1210. return Status;
  1211. }
  1212. //
  1213. // Copy the file
  1214. //
  1215. memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);
  1216. PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);
  1217. fprintf (FvReportFile, "0x%08X %s\n", (unsigned) (FvImage->CurrentFilePointer - FvImage->FileImage), FileGuidString);
  1218. FvImage->CurrentFilePointer += FileSize;
  1219. } else {
  1220. Error (NULL, 0, 4002, "Resource", "FV space is full, cannot add file %s.", FvInfo->FvFiles[Index]);
  1221. free (FileBuffer);
  1222. return EFI_ABORTED;
  1223. }
  1224. //
  1225. // Make next file start at QWord Boundary
  1226. //
  1227. while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {
  1228. FvImage->CurrentFilePointer++;
  1229. }
  1230. Done:
  1231. //
  1232. // Free allocated memory.
  1233. //
  1234. free (FileBuffer);
  1235. return EFI_SUCCESS;
  1236. }
  1237. EFI_STATUS
  1238. PadFvImage (
  1239. IN MEMORY_FILE *FvImage,
  1240. IN EFI_FFS_FILE_HEADER *VtfFileImage
  1241. )
  1242. /*++
  1243. Routine Description:
  1244. This function places a pad file between the last file in the FV and the VTF
  1245. file if the VTF file exists.
  1246. Arguments:
  1247. FvImage Memory file for the FV memory image
  1248. VtfFileImage The address of the VTF file. If this is the end of the FV
  1249. image, no VTF exists and no pad file is needed.
  1250. Returns:
  1251. EFI_SUCCESS Completed successfully.
  1252. EFI_INVALID_PARAMETER One of the input parameters was NULL.
  1253. --*/
  1254. {
  1255. EFI_FFS_FILE_HEADER *PadFile;
  1256. UINTN FileSize;
  1257. UINT32 FfsHeaderSize;
  1258. //
  1259. // If there is no VTF or the VTF naturally follows the previous file without a
  1260. // pad file, then there's nothing to do
  1261. //
  1262. if ((UINTN) VtfFileImage == (UINTN) FvImage->Eof || \
  1263. ((UINTN) VtfFileImage == (UINTN) FvImage->CurrentFilePointer)) {
  1264. return EFI_SUCCESS;
  1265. }
  1266. if ((UINTN) VtfFileImage < (UINTN) FvImage->CurrentFilePointer) {
  1267. return EFI_INVALID_PARAMETER;
  1268. }
  1269. //
  1270. // Pad file starts at beginning of free space
  1271. //
  1272. PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;
  1273. //
  1274. // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.
  1275. //
  1276. PadFile->Type = EFI_FV_FILETYPE_FFS_PAD;
  1277. PadFile->Attributes = 0;
  1278. //
  1279. // FileSize includes the EFI_FFS_FILE_HEADER
  1280. //
  1281. FileSize = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;
  1282. if (FileSize >= MAX_FFS_SIZE) {
  1283. PadFile->Attributes |= FFS_ATTRIB_LARGE_FILE;
  1284. memset(PadFile->Size, 0, sizeof(UINT8) * 3);
  1285. ((EFI_FFS_FILE_HEADER2 *)PadFile)->ExtendedSize = FileSize;
  1286. FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER2);
  1287. mIsLargeFfs = TRUE;
  1288. } else {
  1289. PadFile->Size[0] = (UINT8) (FileSize & 0x000000FF);
  1290. PadFile->Size[1] = (UINT8) ((FileSize & 0x0000FF00) >> 8);
  1291. PadFile->Size[2] = (UINT8) ((FileSize & 0x00FF0000) >> 16);
  1292. FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER);
  1293. }
  1294. //
  1295. // Fill in checksums and state, must be zero during checksum calculation.
  1296. //
  1297. PadFile->IntegrityCheck.Checksum.Header = 0;
  1298. PadFile->IntegrityCheck.Checksum.File = 0;
  1299. PadFile->State = 0;
  1300. PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, FfsHeaderSize);
  1301. PadFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  1302. PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;
  1303. UpdateFfsFileState (
  1304. (EFI_FFS_FILE_HEADER *) PadFile,
  1305. (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage
  1306. );
  1307. //
  1308. // Update the current FV pointer
  1309. //
  1310. FvImage->CurrentFilePointer = FvImage->Eof;
  1311. return EFI_SUCCESS;
  1312. }
  1313. EFI_STATUS
  1314. UpdateResetVector (
  1315. IN MEMORY_FILE *FvImage,
  1316. IN FV_INFO *FvInfo,
  1317. IN EFI_FFS_FILE_HEADER *VtfFile
  1318. )
  1319. /*++
  1320. Routine Description:
  1321. This parses the FV looking for the PEI core and then plugs the address into
  1322. the SALE_ENTRY point of the BSF/VTF for IPF and does BUGBUG TBD action to
  1323. complete an IA32 Bootstrap FV.
  1324. Arguments:
  1325. FvImage Memory file for the FV memory image
  1326. FvInfo Information read from INF file.
  1327. VtfFile Pointer to the VTF file in the FV image.
  1328. Returns:
  1329. EFI_SUCCESS Function Completed successfully.
  1330. EFI_ABORTED Error encountered.
  1331. EFI_INVALID_PARAMETER A required parameter was NULL.
  1332. EFI_NOT_FOUND PEI Core file not found.
  1333. --*/
  1334. {
  1335. EFI_FFS_FILE_HEADER *PeiCoreFile;
  1336. EFI_FFS_FILE_HEADER *SecCoreFile;
  1337. EFI_STATUS Status;
  1338. EFI_FILE_SECTION_POINTER Pe32Section;
  1339. UINT32 EntryPoint;
  1340. UINT32 BaseOfCode;
  1341. UINT16 MachineType;
  1342. EFI_PHYSICAL_ADDRESS PeiCorePhysicalAddress;
  1343. EFI_PHYSICAL_ADDRESS SecCorePhysicalAddress;
  1344. INT32 Ia32SecEntryOffset;
  1345. UINT32 *Ia32ResetAddressPtr;
  1346. UINT8 *BytePointer;
  1347. UINT8 *BytePointer2;
  1348. UINT16 *WordPointer;
  1349. UINT16 CheckSum;
  1350. UINT32 IpiVector;
  1351. UINTN Index;
  1352. EFI_FFS_FILE_STATE SavedState;
  1353. BOOLEAN Vtf0Detected;
  1354. UINT32 FfsHeaderSize;
  1355. UINT32 SecHeaderSize;
  1356. //
  1357. // Verify input parameters
  1358. //
  1359. if (FvImage == NULL || FvInfo == NULL || VtfFile == NULL) {
  1360. return EFI_INVALID_PARAMETER;
  1361. }
  1362. //
  1363. // Initialize FV library
  1364. //
  1365. InitializeFvLib (FvImage->FileImage, FvInfo->Size);
  1366. //
  1367. // Verify VTF file
  1368. //
  1369. Status = VerifyFfsFile (VtfFile);
  1370. if (EFI_ERROR (Status)) {
  1371. return EFI_INVALID_PARAMETER;
  1372. }
  1373. if (
  1374. (((UINTN)FvImage->Eof - (UINTN)FvImage->FileImage) >=
  1375. IA32_X64_VTF_SIGNATURE_OFFSET) &&
  1376. (*(UINT32 *)(VOID*)((UINTN) FvImage->Eof -
  1377. IA32_X64_VTF_SIGNATURE_OFFSET) ==
  1378. IA32_X64_VTF0_SIGNATURE)
  1379. ) {
  1380. Vtf0Detected = TRUE;
  1381. } else {
  1382. Vtf0Detected = FALSE;
  1383. }
  1384. //
  1385. // Find the Sec Core
  1386. //
  1387. Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);
  1388. if (EFI_ERROR (Status) || SecCoreFile == NULL) {
  1389. if (Vtf0Detected) {
  1390. //
  1391. // If the SEC core file is not found, but the VTF-0 signature
  1392. // is found, we'll treat it as a VTF-0 'Volume Top File'.
  1393. // This means no modifications are required to the VTF.
  1394. //
  1395. return EFI_SUCCESS;
  1396. }
  1397. Error (NULL, 0, 3000, "Invalid", "could not find the SEC core file in the FV.");
  1398. return EFI_ABORTED;
  1399. }
  1400. //
  1401. // Sec Core found, now find PE32 section
  1402. //
  1403. Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
  1404. if (Status == EFI_NOT_FOUND) {
  1405. Status = GetSectionByType (SecCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
  1406. }
  1407. if (EFI_ERROR (Status)) {
  1408. Error (NULL, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");
  1409. return EFI_ABORTED;
  1410. }
  1411. SecHeaderSize = GetSectionHeaderLength(Pe32Section.CommonHeader);
  1412. Status = GetPe32Info (
  1413. (VOID *) ((UINTN) Pe32Section.Pe32Section + SecHeaderSize),
  1414. &EntryPoint,
  1415. &BaseOfCode,
  1416. &MachineType
  1417. );
  1418. if (EFI_ERROR (Status)) {
  1419. Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");
  1420. return EFI_ABORTED;
  1421. }
  1422. if (
  1423. Vtf0Detected &&
  1424. (MachineType == EFI_IMAGE_MACHINE_IA32 ||
  1425. MachineType == EFI_IMAGE_MACHINE_X64)
  1426. ) {
  1427. //
  1428. // If the SEC core code is IA32 or X64 and the VTF-0 signature
  1429. // is found, we'll treat it as a VTF-0 'Volume Top File'.
  1430. // This means no modifications are required to the VTF.
  1431. //
  1432. return EFI_SUCCESS;
  1433. }
  1434. //
  1435. // Physical address is FV base + offset of PE32 + offset of the entry point
  1436. //
  1437. SecCorePhysicalAddress = FvInfo->BaseAddress;
  1438. SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + SecHeaderSize - (UINTN) FvImage->FileImage;
  1439. SecCorePhysicalAddress += EntryPoint;
  1440. DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress);
  1441. //
  1442. // Find the PEI Core
  1443. //
  1444. PeiCorePhysicalAddress = 0;
  1445. Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);
  1446. if (!EFI_ERROR (Status) && (PeiCoreFile != NULL)) {
  1447. //
  1448. // PEI Core found, now find PE32 or TE section
  1449. //
  1450. Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);
  1451. if (Status == EFI_NOT_FOUND) {
  1452. Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);
  1453. }
  1454. if (EFI_ERROR (Status)) {
  1455. Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file.");
  1456. return EFI_ABORTED;
  1457. }
  1458. SecHeaderSize = GetSectionHeaderLength(Pe32Section.CommonHeader);
  1459. Status = GetPe32Info (
  1460. (VOID *) ((UINTN) Pe32Section.Pe32Section + SecHeaderSize),
  1461. &EntryPoint,
  1462. &BaseOfCode,
  1463. &MachineType
  1464. );
  1465. if (EFI_ERROR (Status)) {
  1466. Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core.");
  1467. return EFI_ABORTED;
  1468. }
  1469. //
  1470. // Physical address is FV base + offset of PE32 + offset of the entry point
  1471. //
  1472. PeiCorePhysicalAddress = FvInfo->BaseAddress;
  1473. PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + SecHeaderSize - (UINTN) FvImage->FileImage;
  1474. PeiCorePhysicalAddress += EntryPoint;
  1475. DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);
  1476. }
  1477. if (MachineType == EFI_IMAGE_MACHINE_IA32 || MachineType == EFI_IMAGE_MACHINE_X64) {
  1478. if (PeiCorePhysicalAddress != 0) {
  1479. //
  1480. // Get the location to update
  1481. //
  1482. Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_PEI_CORE_ENTRY_OFFSET);
  1483. //
  1484. // Write lower 32 bits of physical address for Pei Core entry
  1485. //
  1486. *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;
  1487. }
  1488. //
  1489. // Write SecCore Entry point relative address into the jmp instruction in reset vector.
  1490. //
  1491. Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - IA32_SEC_CORE_ENTRY_OFFSET);
  1492. Ia32SecEntryOffset = (INT32) (SecCorePhysicalAddress - (FV_IMAGES_TOP_ADDRESS - IA32_SEC_CORE_ENTRY_OFFSET + 2));
  1493. if (Ia32SecEntryOffset <= -65536) {
  1494. Error (NULL, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");
  1495. return STATUS_ERROR;
  1496. }
  1497. *(UINT16 *) Ia32ResetAddressPtr = (UINT16) Ia32SecEntryOffset;
  1498. //
  1499. // Update the BFV base address
  1500. //
  1501. Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 4);
  1502. *Ia32ResetAddressPtr = (UINT32) (FvInfo->BaseAddress);
  1503. DebugMsg (NULL, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo->BaseAddress);
  1504. //
  1505. // Update the Startup AP in the FVH header block ZeroVector region.
  1506. //
  1507. BytePointer = (UINT8 *) ((UINTN) FvImage->FileImage);
  1508. if (FvInfo->Size <= 0x10000) {
  1509. BytePointer2 = m64kRecoveryStartupApDataArray;
  1510. } else if (FvInfo->Size <= 0x20000) {
  1511. BytePointer2 = m128kRecoveryStartupApDataArray;
  1512. } else {
  1513. BytePointer2 = m128kRecoveryStartupApDataArray;
  1514. //
  1515. // Find the position to place Ap reset vector, the offset
  1516. // between the position and the end of Fvrecovery.fv file
  1517. // should not exceed 128kB to prevent Ap reset vector from
  1518. // outside legacy E and F segment
  1519. //
  1520. Status = FindApResetVectorPosition (FvImage, &BytePointer);
  1521. if (EFI_ERROR (Status)) {
  1522. Error (NULL, 0, 3000, "Invalid", "FV image does not have enough space to place AP reset vector. The FV image needs to reserve at least 4KB of unused space.");
  1523. return EFI_ABORTED;
  1524. }
  1525. }
  1526. for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY; Index++) {
  1527. BytePointer[Index] = BytePointer2[Index];
  1528. }
  1529. //
  1530. // Calculate the checksum
  1531. //
  1532. CheckSum = 0x0000;
  1533. WordPointer = (UINT16 *) (BytePointer);
  1534. for (Index = 0; Index < SIZEOF_STARTUP_DATA_ARRAY / 2; Index++) {
  1535. CheckSum = (UINT16) (CheckSum + ((UINT16) *WordPointer));
  1536. WordPointer++;
  1537. }
  1538. //
  1539. // Update the checksum field
  1540. //
  1541. WordPointer = (UINT16 *) (BytePointer + SIZEOF_STARTUP_DATA_ARRAY - 2);
  1542. *WordPointer = (UINT16) (0x10000 - (UINT32) CheckSum);
  1543. //
  1544. // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.
  1545. //
  1546. IpiVector = (UINT32) (FV_IMAGES_TOP_ADDRESS - ((UINTN) FvImage->Eof - (UINTN) BytePointer));
  1547. DebugMsg (NULL, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector);
  1548. if ((IpiVector & 0xFFF) != 0) {
  1549. Error (NULL, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");
  1550. return EFI_ABORTED;
  1551. }
  1552. IpiVector = IpiVector >> 12;
  1553. IpiVector = IpiVector & 0xFF;
  1554. //
  1555. // Write IPI Vector at Offset FvrecoveryFileSize - 8
  1556. //
  1557. Ia32ResetAddressPtr = (UINT32 *) ((UINTN) FvImage->Eof - 8);
  1558. *Ia32ResetAddressPtr = IpiVector;
  1559. } else if (MachineType == EFI_IMAGE_MACHINE_ARMT) {
  1560. //
  1561. // Since the ARM reset vector is in the FV Header you really don't need a
  1562. // Volume Top File, but if you have one for some reason don't crash...
  1563. //
  1564. } else if (MachineType == EFI_IMAGE_MACHINE_AARCH64) {
  1565. //
  1566. // Since the AArch64 reset vector is in the FV Header you really don't need a
  1567. // Volume Top File, but if you have one for some reason don't crash...
  1568. //
  1569. } else {
  1570. Error (NULL, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType);
  1571. return EFI_ABORTED;
  1572. }
  1573. //
  1574. // Now update file checksum
  1575. //
  1576. SavedState = VtfFile->State;
  1577. VtfFile->IntegrityCheck.Checksum.File = 0;
  1578. VtfFile->State = 0;
  1579. if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {
  1580. FfsHeaderSize = GetFfsHeaderLength(VtfFile);
  1581. VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
  1582. (UINT8 *) ((UINT8 *)VtfFile + FfsHeaderSize),
  1583. GetFfsFileLength (VtfFile) - FfsHeaderSize
  1584. );
  1585. } else {
  1586. VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;
  1587. }
  1588. VtfFile->State = SavedState;
  1589. return EFI_SUCCESS;
  1590. }
  1591. EFI_STATUS
  1592. FindCorePeSection(
  1593. IN VOID *FvImageBuffer,
  1594. IN UINT64 FvSize,
  1595. IN EFI_FV_FILETYPE FileType,
  1596. OUT EFI_FILE_SECTION_POINTER *Pe32Section
  1597. )
  1598. /*++
  1599. Routine Description:
  1600. Recursively searches the FV for the FFS file of specified type (typically
  1601. SEC or PEI core) and extracts the PE32 section for further processing.
  1602. Arguments:
  1603. FvImageBuffer Buffer containing FV data
  1604. FvSize Size of the FV
  1605. FileType Type of FFS file to search for
  1606. Pe32Section PE32 section pointer when FFS file is found.
  1607. Returns:
  1608. EFI_SUCCESS Function Completed successfully.
  1609. EFI_ABORTED Error encountered.
  1610. EFI_INVALID_PARAMETER A required parameter was NULL.
  1611. EFI_NOT_FOUND Core file not found.
  1612. --*/
  1613. {
  1614. EFI_STATUS Status;
  1615. EFI_FIRMWARE_VOLUME_HEADER *OrigFvHeader;
  1616. UINT32 OrigFvLength;
  1617. EFI_FFS_FILE_HEADER *CoreFfsFile;
  1618. UINTN FvImageFileCount;
  1619. EFI_FFS_FILE_HEADER *FvImageFile;
  1620. UINTN EncapFvSectionCount;
  1621. EFI_FILE_SECTION_POINTER EncapFvSection;
  1622. EFI_FIRMWARE_VOLUME_HEADER *EncapsulatedFvHeader;
  1623. if (Pe32Section == NULL) {
  1624. return EFI_INVALID_PARAMETER;
  1625. }
  1626. //
  1627. // Initialize FV library, saving previous values
  1628. //
  1629. OrigFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)NULL;
  1630. GetFvHeader (&OrigFvHeader, &OrigFvLength);
  1631. InitializeFvLib(FvImageBuffer, (UINT32)FvSize);
  1632. //
  1633. // First see if we can obtain the file directly in outer FV
  1634. //
  1635. Status = GetFileByType(FileType, 1, &CoreFfsFile);
  1636. if (!EFI_ERROR(Status) && (CoreFfsFile != NULL) ) {
  1637. //
  1638. // Core found, now find PE32 or TE section
  1639. //
  1640. Status = GetSectionByType(CoreFfsFile, EFI_SECTION_PE32, 1, Pe32Section);
  1641. if (EFI_ERROR(Status)) {
  1642. Status = GetSectionByType(CoreFfsFile, EFI_SECTION_TE, 1, Pe32Section);
  1643. }
  1644. if (EFI_ERROR(Status)) {
  1645. Error(NULL, 0, 3000, "Invalid", "could not find a PE32 section in the core file.");
  1646. return EFI_ABORTED;
  1647. }
  1648. //
  1649. // Core PE/TE section, found, return
  1650. //
  1651. Status = EFI_SUCCESS;
  1652. goto EarlyExit;
  1653. }
  1654. //
  1655. // File was not found, look for FV Image file
  1656. //
  1657. // iterate through all FV image files in outer FV
  1658. for (FvImageFileCount = 1;; FvImageFileCount++) {
  1659. Status = GetFileByType(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, FvImageFileCount, &FvImageFile);
  1660. if (EFI_ERROR(Status) || (FvImageFile == NULL) ) {
  1661. // exit FV image file loop, no more found
  1662. break;
  1663. }
  1664. // Found an fv image file, look for an FV image section. The PI spec does not
  1665. // preclude multiple FV image sections so we loop accordingly.
  1666. for (EncapFvSectionCount = 1;; EncapFvSectionCount++) {
  1667. // Look for the next FV image section. The section search code will
  1668. // iterate into encapsulation sections. For example, it will iterate
  1669. // into an EFI_SECTION_GUID_DEFINED encapsulation section to find the
  1670. // EFI_SECTION_FIRMWARE_VOLUME_IMAGE sections contained therein.
  1671. Status = GetSectionByType(FvImageFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, EncapFvSectionCount, &EncapFvSection);
  1672. if (EFI_ERROR(Status)) {
  1673. // exit section inner loop, no more found
  1674. break;
  1675. }
  1676. EncapsulatedFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINT8 *)EncapFvSection.FVImageSection + GetSectionHeaderLength(EncapFvSection.FVImageSection));
  1677. // recurse to search the encapsulated FV for this core file type
  1678. Status = FindCorePeSection(EncapsulatedFvHeader, EncapsulatedFvHeader->FvLength, FileType, Pe32Section);
  1679. if (!EFI_ERROR(Status)) {
  1680. // we found the core in the capsulated image, success
  1681. goto EarlyExit;
  1682. }
  1683. } // end encapsulated fv image section loop
  1684. } // end fv image file loop
  1685. // core was not found
  1686. Status = EFI_NOT_FOUND;
  1687. EarlyExit:
  1688. // restore FV lib values
  1689. if(OrigFvHeader != NULL) {
  1690. InitializeFvLib(OrigFvHeader, OrigFvLength);
  1691. }
  1692. return Status;
  1693. }
  1694. EFI_STATUS
  1695. GetCoreMachineType(
  1696. IN EFI_FILE_SECTION_POINTER Pe32Section,
  1697. OUT UINT16 *CoreMachineType
  1698. )
  1699. /*++
  1700. Routine Description:
  1701. Returns the machine type of a P32 image, typically SEC or PEI core.
  1702. Arguments:
  1703. Pe32Section PE32 section data
  1704. CoreMachineType The extracted machine type
  1705. Returns:
  1706. EFI_SUCCESS Function Completed successfully.
  1707. EFI_ABORTED Error encountered.
  1708. EFI_INVALID_PARAMETER A required parameter was NULL.
  1709. --*/
  1710. {
  1711. EFI_STATUS Status;
  1712. UINT32 EntryPoint;
  1713. UINT32 BaseOfCode;
  1714. if (CoreMachineType == NULL) {
  1715. return EFI_INVALID_PARAMETER;
  1716. }
  1717. Status = GetPe32Info(
  1718. (VOID *)((UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader)),
  1719. &EntryPoint,
  1720. &BaseOfCode,
  1721. CoreMachineType
  1722. );
  1723. if (EFI_ERROR(Status)) {
  1724. Error(NULL, 0, 3000, "Invalid", "could not get the PE32 machine type for the core.");
  1725. return EFI_ABORTED;
  1726. }
  1727. return EFI_SUCCESS;
  1728. }
  1729. EFI_STATUS
  1730. GetCoreEntryPointAddress(
  1731. IN VOID *FvImageBuffer,
  1732. IN FV_INFO *FvInfo,
  1733. IN EFI_FILE_SECTION_POINTER Pe32Section,
  1734. OUT EFI_PHYSICAL_ADDRESS *CoreEntryAddress
  1735. )
  1736. /*++
  1737. Routine Description:
  1738. Returns the physical address of the core (SEC or PEI) entry point.
  1739. Arguments:
  1740. FvImageBuffer Pointer to buffer containing FV data
  1741. FvInfo Info for the parent FV
  1742. Pe32Section PE32 section data
  1743. CoreEntryAddress The extracted core entry physical address
  1744. Returns:
  1745. EFI_SUCCESS Function Completed successfully.
  1746. EFI_ABORTED Error encountered.
  1747. EFI_INVALID_PARAMETER A required parameter was NULL.
  1748. --*/
  1749. {
  1750. EFI_STATUS Status;
  1751. UINT32 EntryPoint;
  1752. UINT32 BaseOfCode;
  1753. UINT16 MachineType;
  1754. EFI_PHYSICAL_ADDRESS EntryPhysicalAddress;
  1755. if (CoreEntryAddress == NULL) {
  1756. return EFI_INVALID_PARAMETER;
  1757. }
  1758. Status = GetPe32Info(
  1759. (VOID *)((UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader)),
  1760. &EntryPoint,
  1761. &BaseOfCode,
  1762. &MachineType
  1763. );
  1764. if (EFI_ERROR(Status)) {
  1765. Error(NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the core.");
  1766. return EFI_ABORTED;
  1767. }
  1768. //
  1769. // Physical address is FV base + offset of PE32 + offset of the entry point
  1770. //
  1771. EntryPhysicalAddress = FvInfo->BaseAddress;
  1772. EntryPhysicalAddress += (UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader) - (UINTN)FvImageBuffer;
  1773. EntryPhysicalAddress += EntryPoint;
  1774. *CoreEntryAddress = EntryPhysicalAddress;
  1775. return EFI_SUCCESS;
  1776. }
  1777. EFI_STATUS
  1778. UpdateArmResetVectorIfNeeded (
  1779. IN MEMORY_FILE *FvImage,
  1780. IN FV_INFO *FvInfo
  1781. )
  1782. /*++
  1783. Routine Description:
  1784. This parses the FV looking for SEC and patches that address into the
  1785. beginning of the FV header.
  1786. For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.
  1787. For AArch64 the reset vector is at 0x00000000.
  1788. This would commonly map to the first entry in the ROM.
  1789. ARM32 Exceptions:
  1790. Reset +0
  1791. Undefined +4
  1792. SWI +8
  1793. Prefetch Abort +12
  1794. Data Abort +16
  1795. IRQ +20
  1796. FIQ +24
  1797. We support two schemes on ARM.
  1798. 1) Beginning of the FV is the reset vector
  1799. 2) Reset vector is data bytes FDF file and that code branches to reset vector
  1800. in the beginning of the FV (fixed size offset).
  1801. Need to have the jump for the reset vector at location zero.
  1802. We also need to store the address or PEI (if it exists).
  1803. We stub out a return from interrupt in case the debugger
  1804. is using SWI (not done for AArch64, not enough space in struct).
  1805. The optional entry to the common exception handler is
  1806. to support full featured exception handling from ROM and is currently
  1807. not support by this tool.
  1808. Arguments:
  1809. FvImage Memory file for the FV memory image
  1810. FvInfo Information read from INF file.
  1811. Returns:
  1812. EFI_SUCCESS Function Completed successfully.
  1813. EFI_ABORTED Error encountered.
  1814. EFI_INVALID_PARAMETER A required parameter was NULL.
  1815. EFI_NOT_FOUND PEI Core file not found.
  1816. --*/
  1817. {
  1818. EFI_STATUS Status;
  1819. EFI_FILE_SECTION_POINTER SecPe32;
  1820. EFI_FILE_SECTION_POINTER PeiPe32;
  1821. BOOLEAN UpdateVectorSec = FALSE;
  1822. BOOLEAN UpdateVectorPei = FALSE;
  1823. UINT16 MachineType = 0;
  1824. EFI_PHYSICAL_ADDRESS SecCoreEntryAddress = 0;
  1825. UINT16 PeiMachineType = 0;
  1826. EFI_PHYSICAL_ADDRESS PeiCoreEntryAddress = 0;
  1827. //
  1828. // Verify input parameters
  1829. //
  1830. if (FvImage == NULL || FvInfo == NULL) {
  1831. return EFI_INVALID_PARAMETER;
  1832. }
  1833. //
  1834. // Locate an SEC Core instance and if found extract the machine type and entry point address
  1835. //
  1836. Status = FindCorePeSection(FvImage->FileImage, FvInfo->Size, EFI_FV_FILETYPE_SECURITY_CORE, &SecPe32);
  1837. if (!EFI_ERROR(Status)) {
  1838. Status = GetCoreMachineType(SecPe32, &MachineType);
  1839. if (EFI_ERROR(Status)) {
  1840. Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC Core.");
  1841. return EFI_ABORTED;
  1842. }
  1843. Status = GetCoreEntryPointAddress(FvImage->FileImage, FvInfo, SecPe32, &SecCoreEntryAddress);
  1844. if (EFI_ERROR(Status)) {
  1845. Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 entry point address for SEC Core.");
  1846. return EFI_ABORTED;
  1847. }
  1848. VerboseMsg("UpdateArmResetVectorIfNeeded found SEC core entry at 0x%llx", (unsigned long long)SecCoreEntryAddress);
  1849. UpdateVectorSec = TRUE;
  1850. }
  1851. //
  1852. // Locate a PEI Core instance and if found extract the machine type and entry point address
  1853. //
  1854. Status = FindCorePeSection(FvImage->FileImage, FvInfo->Size, EFI_FV_FILETYPE_PEI_CORE, &PeiPe32);
  1855. if (!EFI_ERROR(Status)) {
  1856. Status = GetCoreMachineType(PeiPe32, &PeiMachineType);
  1857. if (EFI_ERROR(Status)) {
  1858. Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for PEI Core.");
  1859. return EFI_ABORTED;
  1860. }
  1861. Status = GetCoreEntryPointAddress(FvImage->FileImage, FvInfo, PeiPe32, &PeiCoreEntryAddress);
  1862. if (EFI_ERROR(Status)) {
  1863. Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 entry point address for PEI Core.");
  1864. return EFI_ABORTED;
  1865. }
  1866. VerboseMsg("UpdateArmResetVectorIfNeeded found PEI core entry at 0x%llx", (unsigned long long)PeiCoreEntryAddress);
  1867. // if we previously found an SEC Core make sure machine types match
  1868. if (UpdateVectorSec && (MachineType != PeiMachineType)) {
  1869. Error(NULL, 0, 3000, "Invalid", "SEC and PEI machine types do not match, can't update reset vector");
  1870. return EFI_ABORTED;
  1871. }
  1872. else {
  1873. MachineType = PeiMachineType;
  1874. }
  1875. UpdateVectorPei = TRUE;
  1876. }
  1877. if (!UpdateVectorSec && !UpdateVectorPei) {
  1878. return EFI_SUCCESS;
  1879. }
  1880. if (MachineType == EFI_IMAGE_MACHINE_ARMT) {
  1881. // ARM: Array of 4 UINT32s:
  1882. // 0 - is branch relative to SEC entry point
  1883. // 1 - PEI Entry Point
  1884. // 2 - movs pc,lr for a SWI handler
  1885. // 3 - Place holder for Common Exception Handler
  1886. UINT32 ResetVector[4];
  1887. memset(ResetVector, 0, sizeof (ResetVector));
  1888. // if we found an SEC core entry point then generate a branch instruction
  1889. // to it and populate a debugger SWI entry as well
  1890. if (UpdateVectorSec) {
  1891. VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector");
  1892. // B SecEntryPoint - signed_immed_24 part +/-32MB offset
  1893. // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8
  1894. ResetVector[0] = (INT32)(SecCoreEntryAddress - FvInfo->BaseAddress - 8) >> 2;
  1895. if (ResetVector[0] > 0x00FFFFFF) {
  1896. Error(NULL, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");
  1897. return EFI_ABORTED;
  1898. }
  1899. // Add opcode for an unconditional branch with no link. i.e.: " B SecEntryPoint"
  1900. ResetVector[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION;
  1901. // SWI handler movs pc,lr. Just in case a debugger uses SWI
  1902. ResetVector[2] = 0xE1B0F07E;
  1903. // Place holder to support a common interrupt handler from ROM.
  1904. // Currently not supported. For this to be used the reset vector would not be in this FV
  1905. // and the exception vectors would be hard coded in the ROM and just through this address
  1906. // to find a common handler in the a module in the FV.
  1907. ResetVector[3] = 0;
  1908. }
  1909. // if a PEI core entry was found place its address in the vector area
  1910. if (UpdateVectorPei) {
  1911. VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM PEI address");
  1912. // Address of PEI Core, if we have one
  1913. ResetVector[1] = (UINT32)PeiCoreEntryAddress;
  1914. }
  1915. //
  1916. // Copy to the beginning of the FV
  1917. //
  1918. memcpy(FvImage->FileImage, ResetVector, sizeof (ResetVector));
  1919. } else if (MachineType == EFI_IMAGE_MACHINE_AARCH64) {
  1920. // AArch64: Used as UINT64 ResetVector[2]
  1921. // 0 - is branch relative to SEC entry point
  1922. // 1 - PEI Entry Point
  1923. UINT64 ResetVector[2];
  1924. memset(ResetVector, 0, sizeof (ResetVector));
  1925. /* NOTE:
  1926. ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector
  1927. array at the moment, for AArch64, does not allow us space for this as the header only
  1928. allows for a fixed amount of bytes at the start. If we are sure that UEFI will live
  1929. within the first 4GB of addressable RAM we could potentially adopt the same ResetVector
  1930. layout as above. But for the moment we replace the four 32bit vectors with two 64bit
  1931. vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit
  1932. base.
  1933. */
  1934. // if we found an SEC core entry point then generate a branch instruction to it
  1935. if (UpdateVectorSec) {
  1936. VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 SEC vector");
  1937. ResetVector[0] = (UINT64)(SecCoreEntryAddress - FvInfo->BaseAddress) >> 2;
  1938. // B SecEntryPoint - signed_immed_26 part +/-128MB offset
  1939. if (ResetVector[0] > 0x03FFFFFF) {
  1940. Error(NULL, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");
  1941. return EFI_ABORTED;
  1942. }
  1943. // Add opcode for an unconditional branch with no link. i.e.: " B SecEntryPoint"
  1944. ResetVector[0] |= ARM64_UNCONDITIONAL_JUMP_INSTRUCTION;
  1945. }
  1946. // if a PEI core entry was found place its address in the vector area
  1947. if (UpdateVectorPei) {
  1948. VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 PEI address");
  1949. // Address of PEI Core, if we have one
  1950. ResetVector[1] = (UINT64)PeiCoreEntryAddress;
  1951. }
  1952. //
  1953. // Copy to the beginning of the FV
  1954. //
  1955. memcpy(FvImage->FileImage, ResetVector, sizeof (ResetVector));
  1956. } else {
  1957. Error(NULL, 0, 3000, "Invalid", "Unknown machine type");
  1958. return EFI_ABORTED;
  1959. }
  1960. return EFI_SUCCESS;
  1961. }
  1962. EFI_STATUS
  1963. GetPe32Info (
  1964. IN UINT8 *Pe32,
  1965. OUT UINT32 *EntryPoint,
  1966. OUT UINT32 *BaseOfCode,
  1967. OUT UINT16 *MachineType
  1968. )
  1969. /*++
  1970. Routine Description:
  1971. Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
  1972. See EfiImage.h for machine types. The entry point offset is from the beginning
  1973. of the PE32 buffer passed in.
  1974. Arguments:
  1975. Pe32 Beginning of the PE32.
  1976. EntryPoint Offset from the beginning of the PE32 to the image entry point.
  1977. BaseOfCode Base address of code.
  1978. MachineType Magic number for the machine type.
  1979. Returns:
  1980. EFI_SUCCESS Function completed successfully.
  1981. EFI_ABORTED Error encountered.
  1982. EFI_INVALID_PARAMETER A required parameter was NULL.
  1983. EFI_UNSUPPORTED The operation is unsupported.
  1984. --*/
  1985. {
  1986. EFI_IMAGE_DOS_HEADER *DosHeader;
  1987. EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
  1988. EFI_TE_IMAGE_HEADER *TeHeader;
  1989. //
  1990. // Verify input parameters
  1991. //
  1992. if (Pe32 == NULL) {
  1993. return EFI_INVALID_PARAMETER;
  1994. }
  1995. //
  1996. // First check whether it is one TE Image.
  1997. //
  1998. TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;
  1999. if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
  2000. //
  2001. // By TeImage Header to get output
  2002. //
  2003. *EntryPoint = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
  2004. *BaseOfCode = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
  2005. *MachineType = TeHeader->Machine;
  2006. } else {
  2007. //
  2008. // Then check whether
  2009. // First is the DOS header
  2010. //
  2011. DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;
  2012. //
  2013. // Verify DOS header is expected
  2014. //
  2015. if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
  2016. Error (NULL, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader->e_magic);
  2017. return EFI_UNSUPPORTED;
  2018. }
  2019. //
  2020. // Immediately following is the NT header.
  2021. //
  2022. ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) Pe32 + DosHeader->e_lfanew);
  2023. //
  2024. // Verify NT header is expected
  2025. //
  2026. if (ImgHdr->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {
  2027. Error (NULL, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr->Pe32.Signature);
  2028. return EFI_UNSUPPORTED;
  2029. }
  2030. //
  2031. // Get output
  2032. //
  2033. *EntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
  2034. *BaseOfCode = ImgHdr->Pe32.OptionalHeader.BaseOfCode;
  2035. *MachineType = ImgHdr->Pe32.FileHeader.Machine;
  2036. }
  2037. //
  2038. // Verify machine type is supported
  2039. //
  2040. if ((*MachineType != EFI_IMAGE_MACHINE_IA32) && (*MachineType != EFI_IMAGE_MACHINE_X64) && (*MachineType != EFI_IMAGE_MACHINE_EBC) &&
  2041. (*MachineType != EFI_IMAGE_MACHINE_ARMT) && (*MachineType != EFI_IMAGE_MACHINE_AARCH64)) {
  2042. Error (NULL, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
  2043. return EFI_UNSUPPORTED;
  2044. }
  2045. return EFI_SUCCESS;
  2046. }
  2047. EFI_STATUS
  2048. GenerateFvImage (
  2049. IN CHAR8 *InfFileImage,
  2050. IN UINTN InfFileSize,
  2051. IN CHAR8 *FvFileName,
  2052. IN CHAR8 *MapFileName
  2053. )
  2054. /*++
  2055. Routine Description:
  2056. This is the main function which will be called from application.
  2057. Arguments:
  2058. InfFileImage Buffer containing the INF file contents.
  2059. InfFileSize Size of the contents of the InfFileImage buffer.
  2060. FvFileName Requested name for the FV file.
  2061. MapFileName Fv map file to log fv driver information.
  2062. Returns:
  2063. EFI_SUCCESS Function completed successfully.
  2064. EFI_OUT_OF_RESOURCES Could not allocate required resources.
  2065. EFI_ABORTED Error encountered.
  2066. EFI_INVALID_PARAMETER A required parameter was NULL.
  2067. --*/
  2068. {
  2069. EFI_STATUS Status;
  2070. MEMORY_FILE InfMemoryFile;
  2071. MEMORY_FILE FvImageMemoryFile;
  2072. UINTN Index;
  2073. EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
  2074. EFI_FFS_FILE_HEADER *VtfFileImage;
  2075. UINT8 *FvBufferHeader; // to make sure fvimage header 8 type alignment.
  2076. UINT8 *FvImage;
  2077. UINTN FvImageSize;
  2078. FILE *FvFile;
  2079. CHAR8 *FvMapName;
  2080. FILE *FvMapFile;
  2081. EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
  2082. FILE *FvExtHeaderFile;
  2083. UINTN FileSize;
  2084. CHAR8 *FvReportName;
  2085. FILE *FvReportFile;
  2086. FvBufferHeader = NULL;
  2087. FvFile = NULL;
  2088. FvMapName = NULL;
  2089. FvMapFile = NULL;
  2090. FvReportName = NULL;
  2091. FvReportFile = NULL;
  2092. if (InfFileImage != NULL) {
  2093. //
  2094. // Initialize file structures
  2095. //
  2096. InfMemoryFile.FileImage = InfFileImage;
  2097. InfMemoryFile.CurrentFilePointer = InfFileImage;
  2098. InfMemoryFile.Eof = InfFileImage + InfFileSize;
  2099. //
  2100. // Parse the FV inf file for header information
  2101. //
  2102. Status = ParseFvInf (&InfMemoryFile, &mFvDataInfo);
  2103. if (EFI_ERROR (Status)) {
  2104. Error (NULL, 0, 0003, "Error parsing file", "the input FV INF file.");
  2105. return Status;
  2106. }
  2107. }
  2108. //
  2109. // Update the file name return values
  2110. //
  2111. if (FvFileName == NULL && mFvDataInfo.FvName[0] != '\0') {
  2112. FvFileName = mFvDataInfo.FvName;
  2113. }
  2114. if (FvFileName == NULL) {
  2115. Error (NULL, 0, 1001, "Missing option", "Output file name");
  2116. return EFI_ABORTED;
  2117. }
  2118. if (mFvDataInfo.FvBlocks[0].Length == 0) {
  2119. Error (NULL, 0, 1001, "Missing required argument", "Block Size");
  2120. return EFI_ABORTED;
  2121. }
  2122. //
  2123. // Debug message Fv File System Guid
  2124. //
  2125. if (mFvDataInfo.FvFileSystemGuidSet) {
  2126. DebugMsg (NULL, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  2127. (unsigned) mFvDataInfo.FvFileSystemGuid.Data1,
  2128. mFvDataInfo.FvFileSystemGuid.Data2,
  2129. mFvDataInfo.FvFileSystemGuid.Data3,
  2130. mFvDataInfo.FvFileSystemGuid.Data4[0],
  2131. mFvDataInfo.FvFileSystemGuid.Data4[1],
  2132. mFvDataInfo.FvFileSystemGuid.Data4[2],
  2133. mFvDataInfo.FvFileSystemGuid.Data4[3],
  2134. mFvDataInfo.FvFileSystemGuid.Data4[4],
  2135. mFvDataInfo.FvFileSystemGuid.Data4[5],
  2136. mFvDataInfo.FvFileSystemGuid.Data4[6],
  2137. mFvDataInfo.FvFileSystemGuid.Data4[7]);
  2138. }
  2139. //
  2140. // Add PI FV extension header
  2141. //
  2142. FvExtHeader = NULL;
  2143. FvExtHeaderFile = NULL;
  2144. if (mFvDataInfo.FvExtHeaderFile[0] != 0) {
  2145. //
  2146. // Open the FV Extension Header file
  2147. //
  2148. FvExtHeaderFile = fopen (LongFilePath (mFvDataInfo.FvExtHeaderFile), "rb");
  2149. if (FvExtHeaderFile == NULL) {
  2150. Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);
  2151. return EFI_ABORTED;
  2152. }
  2153. //
  2154. // Get the file size
  2155. //
  2156. FileSize = _filelength (fileno (FvExtHeaderFile));
  2157. //
  2158. // Allocate a buffer for the FV Extension Header
  2159. //
  2160. FvExtHeader = malloc(FileSize);
  2161. if (FvExtHeader == NULL) {
  2162. fclose (FvExtHeaderFile);
  2163. return EFI_OUT_OF_RESOURCES;
  2164. }
  2165. //
  2166. // Read the FV Extension Header
  2167. //
  2168. fread (FvExtHeader, sizeof (UINT8), FileSize, FvExtHeaderFile);
  2169. fclose (FvExtHeaderFile);
  2170. //
  2171. // See if there is an override for the FV Name GUID
  2172. //
  2173. if (mFvDataInfo.FvNameGuidSet) {
  2174. memcpy (&FvExtHeader->FvName, &mFvDataInfo.FvNameGuid, sizeof (EFI_GUID));
  2175. }
  2176. memcpy (&mFvDataInfo.FvNameGuid, &FvExtHeader->FvName, sizeof (EFI_GUID));
  2177. mFvDataInfo.FvNameGuidSet = TRUE;
  2178. } else if (mFvDataInfo.FvNameGuidSet) {
  2179. //
  2180. // Allocate a buffer for the FV Extension Header
  2181. //
  2182. FvExtHeader = malloc(sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER));
  2183. if (FvExtHeader == NULL) {
  2184. return EFI_OUT_OF_RESOURCES;
  2185. }
  2186. memcpy (&FvExtHeader->FvName, &mFvDataInfo.FvNameGuid, sizeof (EFI_GUID));
  2187. FvExtHeader->ExtHeaderSize = sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
  2188. }
  2189. //
  2190. // Debug message Fv Name Guid
  2191. //
  2192. if (mFvDataInfo.FvNameGuidSet) {
  2193. DebugMsg (NULL, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  2194. (unsigned) mFvDataInfo.FvNameGuid.Data1,
  2195. mFvDataInfo.FvNameGuid.Data2,
  2196. mFvDataInfo.FvNameGuid.Data3,
  2197. mFvDataInfo.FvNameGuid.Data4[0],
  2198. mFvDataInfo.FvNameGuid.Data4[1],
  2199. mFvDataInfo.FvNameGuid.Data4[2],
  2200. mFvDataInfo.FvNameGuid.Data4[3],
  2201. mFvDataInfo.FvNameGuid.Data4[4],
  2202. mFvDataInfo.FvNameGuid.Data4[5],
  2203. mFvDataInfo.FvNameGuid.Data4[6],
  2204. mFvDataInfo.FvNameGuid.Data4[7]);
  2205. }
  2206. if (CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0 ||
  2207. CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem3Guid) == 0) {
  2208. mFvDataInfo.IsPiFvImage = TRUE;
  2209. }
  2210. //
  2211. // FvMap file to log the function address of all modules in one Fvimage
  2212. //
  2213. if (MapFileName != NULL) {
  2214. if (strlen (MapFileName) > MAX_LONG_FILE_PATH - 1) {
  2215. Error (NULL, 0, 1003, "Invalid option value", "MapFileName %s is too long!", MapFileName);
  2216. Status = EFI_ABORTED;
  2217. goto Finish;
  2218. }
  2219. FvMapName = malloc (strlen (MapFileName) + 1);
  2220. if (FvMapName == NULL) {
  2221. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  2222. Status = EFI_OUT_OF_RESOURCES;
  2223. goto Finish;
  2224. }
  2225. strcpy (FvMapName, MapFileName);
  2226. } else {
  2227. if (strlen (FvFileName) + strlen (".map") > MAX_LONG_FILE_PATH - 1) {
  2228. Error (NULL, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName);
  2229. Status = EFI_ABORTED;
  2230. goto Finish;
  2231. }
  2232. FvMapName = malloc (strlen (FvFileName) + strlen (".map") + 1);
  2233. if (FvMapName == NULL) {
  2234. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  2235. Status = EFI_OUT_OF_RESOURCES;
  2236. goto Finish;
  2237. }
  2238. strcpy (FvMapName, FvFileName);
  2239. strcat (FvMapName, ".map");
  2240. }
  2241. VerboseMsg ("FV Map file name is %s", FvMapName);
  2242. //
  2243. // FvReport file to log the FV information in one Fvimage
  2244. //
  2245. if (strlen (FvFileName) + strlen (".txt") > MAX_LONG_FILE_PATH - 1) {
  2246. Error (NULL, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName);
  2247. Status = EFI_ABORTED;
  2248. goto Finish;
  2249. }
  2250. FvReportName = malloc (strlen (FvFileName) + strlen (".txt") + 1);
  2251. if (FvReportName == NULL) {
  2252. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");
  2253. Status = EFI_OUT_OF_RESOURCES;
  2254. goto Finish;
  2255. }
  2256. strcpy (FvReportName, FvFileName);
  2257. strcat (FvReportName, ".txt");
  2258. //
  2259. // Calculate the FV size and Update Fv Size based on the actual FFS files.
  2260. // And Update mFvDataInfo data.
  2261. //
  2262. Status = CalculateFvSize (&mFvDataInfo);
  2263. if (EFI_ERROR (Status)) {
  2264. goto Finish;
  2265. }
  2266. VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo.Size);
  2267. //
  2268. // support fv image and empty fv image
  2269. //
  2270. FvImageSize = mFvDataInfo.Size;
  2271. //
  2272. // Allocate the FV, assure FvImage Header 8 byte alignment
  2273. //
  2274. FvBufferHeader = malloc (FvImageSize + sizeof (UINT64));
  2275. if (FvBufferHeader == NULL) {
  2276. Status = EFI_OUT_OF_RESOURCES;
  2277. goto Finish;
  2278. }
  2279. FvImage = (UINT8 *) (((UINTN) FvBufferHeader + 7) & ~7);
  2280. //
  2281. // Initialize the FV to the erase polarity
  2282. //
  2283. if (mFvDataInfo.FvAttributes == 0) {
  2284. //
  2285. // Set Default Fv Attribute
  2286. //
  2287. mFvDataInfo.FvAttributes = FV_DEFAULT_ATTRIBUTE;
  2288. }
  2289. if (mFvDataInfo.FvAttributes & EFI_FVB2_ERASE_POLARITY) {
  2290. memset (FvImage, -1, FvImageSize);
  2291. } else {
  2292. memset (FvImage, 0, FvImageSize);
  2293. }
  2294. //
  2295. // Initialize FV header
  2296. //
  2297. FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) FvImage;
  2298. //
  2299. // Initialize the zero vector to all zeros.
  2300. //
  2301. memset (FvHeader->ZeroVector, 0, 16);
  2302. //
  2303. // Copy the Fv file system GUID
  2304. //
  2305. memcpy (&FvHeader->FileSystemGuid, &mFvDataInfo.FvFileSystemGuid, sizeof (EFI_GUID));
  2306. FvHeader->FvLength = FvImageSize;
  2307. FvHeader->Signature = EFI_FVH_SIGNATURE;
  2308. FvHeader->Attributes = mFvDataInfo.FvAttributes;
  2309. FvHeader->Revision = EFI_FVH_REVISION;
  2310. FvHeader->ExtHeaderOffset = 0;
  2311. FvHeader->Reserved[0] = 0;
  2312. //
  2313. // Copy firmware block map
  2314. //
  2315. for (Index = 0; mFvDataInfo.FvBlocks[Index].Length != 0; Index++) {
  2316. FvHeader->BlockMap[Index].NumBlocks = mFvDataInfo.FvBlocks[Index].NumBlocks;
  2317. FvHeader->BlockMap[Index].Length = mFvDataInfo.FvBlocks[Index].Length;
  2318. }
  2319. //
  2320. // Add block map terminator
  2321. //
  2322. FvHeader->BlockMap[Index].NumBlocks = 0;
  2323. FvHeader->BlockMap[Index].Length = 0;
  2324. //
  2325. // Complete the header
  2326. //
  2327. FvHeader->HeaderLength = (UINT16) (((UINTN) &(FvHeader->BlockMap[Index + 1])) - (UINTN) FvImage);
  2328. FvHeader->Checksum = 0;
  2329. FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  2330. //
  2331. // If there is no FFS file, generate one empty FV
  2332. //
  2333. if (mFvDataInfo.FvFiles[0][0] == 0 && !mFvDataInfo.FvNameGuidSet) {
  2334. goto WriteFile;
  2335. }
  2336. //
  2337. // Initialize our "file" view of the buffer
  2338. //
  2339. FvImageMemoryFile.FileImage = (CHAR8 *)FvImage;
  2340. FvImageMemoryFile.CurrentFilePointer = (CHAR8 *)FvImage + FvHeader->HeaderLength;
  2341. FvImageMemoryFile.Eof = (CHAR8 *)FvImage + FvImageSize;
  2342. //
  2343. // Initialize the FV library.
  2344. //
  2345. InitializeFvLib (FvImageMemoryFile.FileImage, FvImageSize);
  2346. //
  2347. // Initialize the VTF file address.
  2348. //
  2349. VtfFileImage = (EFI_FFS_FILE_HEADER *) FvImageMemoryFile.Eof;
  2350. //
  2351. // Open FvMap file
  2352. //
  2353. FvMapFile = fopen (LongFilePath (FvMapName), "w");
  2354. if (FvMapFile == NULL) {
  2355. Error (NULL, 0, 0001, "Error opening file", FvMapName);
  2356. Status = EFI_ABORTED;
  2357. goto Finish;
  2358. }
  2359. //
  2360. // Open FvReport file
  2361. //
  2362. FvReportFile = fopen (LongFilePath (FvReportName), "w");
  2363. if (FvReportFile == NULL) {
  2364. Error (NULL, 0, 0001, "Error opening file", FvReportName);
  2365. Status = EFI_ABORTED;
  2366. goto Finish;
  2367. }
  2368. //
  2369. // record FV size information into FvMap file.
  2370. //
  2371. if (mFvTotalSize != 0) {
  2372. fprintf (FvMapFile, EFI_FV_TOTAL_SIZE_STRING);
  2373. fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTotalSize);
  2374. }
  2375. if (mFvTakenSize != 0) {
  2376. fprintf (FvMapFile, EFI_FV_TAKEN_SIZE_STRING);
  2377. fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTakenSize);
  2378. }
  2379. if (mFvTotalSize != 0 && mFvTakenSize != 0) {
  2380. fprintf (FvMapFile, EFI_FV_SPACE_SIZE_STRING);
  2381. fprintf (FvMapFile, " = 0x%x\n\n", (unsigned) (mFvTotalSize - mFvTakenSize));
  2382. }
  2383. //
  2384. // record FV size information to FvReportFile.
  2385. //
  2386. fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TOTAL_SIZE_STRING, (unsigned) mFvTotalSize);
  2387. fprintf (FvReportFile, "%s = 0x%x\n", EFI_FV_TAKEN_SIZE_STRING, (unsigned) mFvTakenSize);
  2388. //
  2389. // Add PI FV extension header
  2390. //
  2391. if (FvExtHeader != NULL) {
  2392. //
  2393. // Add FV Extended Header contents to the FV as a PAD file
  2394. //
  2395. AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, FvExtHeader, 0);
  2396. //
  2397. // Fv Extension header change update Fv Header Check sum
  2398. //
  2399. FvHeader->Checksum = 0;
  2400. FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  2401. }
  2402. //
  2403. // Add files to FV
  2404. //
  2405. for (Index = 0; mFvDataInfo.FvFiles[Index][0] != 0; Index++) {
  2406. //
  2407. // Add the file
  2408. //
  2409. Status = AddFile (&FvImageMemoryFile, &mFvDataInfo, Index, &VtfFileImage, FvMapFile, FvReportFile);
  2410. //
  2411. // Exit if error detected while adding the file
  2412. //
  2413. if (EFI_ERROR (Status)) {
  2414. goto Finish;
  2415. }
  2416. }
  2417. //
  2418. // If there is a VTF file, some special actions need to occur.
  2419. //
  2420. if ((UINTN) VtfFileImage != (UINTN) FvImageMemoryFile.Eof) {
  2421. //
  2422. // Pad from the end of the last file to the beginning of the VTF file.
  2423. // If the left space is less than sizeof (EFI_FFS_FILE_HEADER)?
  2424. //
  2425. Status = PadFvImage (&FvImageMemoryFile, VtfFileImage);
  2426. if (EFI_ERROR (Status)) {
  2427. Error (NULL, 0, 4002, "Resource", "FV space is full, cannot add pad file between the last file and the VTF file.");
  2428. goto Finish;
  2429. }
  2430. if (!mArm) {
  2431. //
  2432. // Update reset vector (SALE_ENTRY for IPF)
  2433. // Now for IA32 and IA64 platform, the fv which has bsf file must have the
  2434. // EndAddress of 0xFFFFFFFF (unless the section was rebased).
  2435. // Thus, only this type fv needs to update the reset vector.
  2436. // If the PEI Core is found, the VTF file will probably get
  2437. // corrupted by updating the entry point.
  2438. //
  2439. if (mFvDataInfo.ForceRebase == 1 ||
  2440. (mFvDataInfo.BaseAddress + mFvDataInfo.Size) == FV_IMAGES_TOP_ADDRESS) {
  2441. Status = UpdateResetVector (&FvImageMemoryFile, &mFvDataInfo, VtfFileImage);
  2442. if (EFI_ERROR(Status)) {
  2443. Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
  2444. goto Finish;
  2445. }
  2446. DebugMsg (NULL, 0, 9, "Update Reset vector in VTF file", NULL);
  2447. }
  2448. }
  2449. }
  2450. if (mArm) {
  2451. Status = UpdateArmResetVectorIfNeeded (&FvImageMemoryFile, &mFvDataInfo);
  2452. if (EFI_ERROR (Status)) {
  2453. Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");
  2454. goto Finish;
  2455. }
  2456. //
  2457. // Update Checksum for FvHeader
  2458. //
  2459. FvHeader->Checksum = 0;
  2460. FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  2461. }
  2462. //
  2463. // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV
  2464. //
  2465. if (((FvHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) &&
  2466. (((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16)) < MaxFfsAlignment) {
  2467. FvHeader->Attributes = ((MaxFfsAlignment << 16) | (FvHeader->Attributes & 0xFFFF));
  2468. //
  2469. // Update Checksum for FvHeader
  2470. //
  2471. FvHeader->Checksum = 0;
  2472. FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  2473. }
  2474. //
  2475. // If there are large FFS in FV, the file system GUID should set to system 3 GUID.
  2476. //
  2477. if (mIsLargeFfs && CompareGuid (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0) {
  2478. memcpy (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem3Guid, sizeof (EFI_GUID));
  2479. FvHeader->Checksum = 0;
  2480. FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));
  2481. }
  2482. WriteFile:
  2483. //
  2484. // Write fv file
  2485. //
  2486. FvFile = fopen (LongFilePath (FvFileName), "wb");
  2487. if (FvFile == NULL) {
  2488. Error (NULL, 0, 0001, "Error opening file", FvFileName);
  2489. Status = EFI_ABORTED;
  2490. goto Finish;
  2491. }
  2492. if (fwrite (FvImage, 1, FvImageSize, FvFile) != FvImageSize) {
  2493. Error (NULL, 0, 0002, "Error writing file", FvFileName);
  2494. Status = EFI_ABORTED;
  2495. goto Finish;
  2496. }
  2497. Finish:
  2498. if (FvBufferHeader != NULL) {
  2499. free (FvBufferHeader);
  2500. }
  2501. if (FvExtHeader != NULL) {
  2502. free (FvExtHeader);
  2503. }
  2504. if (FvMapName != NULL) {
  2505. free (FvMapName);
  2506. }
  2507. if (FvReportName != NULL) {
  2508. free (FvReportName);
  2509. }
  2510. if (FvFile != NULL) {
  2511. fflush (FvFile);
  2512. fclose (FvFile);
  2513. }
  2514. if (FvMapFile != NULL) {
  2515. fflush (FvMapFile);
  2516. fclose (FvMapFile);
  2517. }
  2518. if (FvReportFile != NULL) {
  2519. fflush (FvReportFile);
  2520. fclose (FvReportFile);
  2521. }
  2522. return Status;
  2523. }
  2524. EFI_STATUS
  2525. UpdatePeiCoreEntryInFit (
  2526. IN FIT_TABLE *FitTablePtr,
  2527. IN UINT64 PeiCorePhysicalAddress
  2528. )
  2529. /*++
  2530. Routine Description:
  2531. This function is used to update the Pei Core address in FIT, this can be used by Sec core to pass control from
  2532. Sec to Pei Core
  2533. Arguments:
  2534. FitTablePtr - The pointer of FIT_TABLE.
  2535. PeiCorePhysicalAddress - The address of Pei Core entry.
  2536. Returns:
  2537. EFI_SUCCESS - The PEI_CORE FIT entry was updated successfully.
  2538. EFI_NOT_FOUND - Not found the PEI_CORE FIT entry.
  2539. --*/
  2540. {
  2541. FIT_TABLE *TmpFitPtr;
  2542. UINTN Index;
  2543. UINTN NumFitComponents;
  2544. TmpFitPtr = FitTablePtr;
  2545. NumFitComponents = TmpFitPtr->CompSize;
  2546. for (Index = 0; Index < NumFitComponents; Index++) {
  2547. if ((TmpFitPtr->CvAndType & FIT_TYPE_MASK) == COMP_TYPE_FIT_PEICORE) {
  2548. TmpFitPtr->CompAddress = PeiCorePhysicalAddress;
  2549. return EFI_SUCCESS;
  2550. }
  2551. TmpFitPtr++;
  2552. }
  2553. return EFI_NOT_FOUND;
  2554. }
  2555. VOID
  2556. UpdateFitCheckSum (
  2557. IN FIT_TABLE *FitTablePtr
  2558. )
  2559. /*++
  2560. Routine Description:
  2561. This function is used to update the checksum for FIT.
  2562. Arguments:
  2563. FitTablePtr - The pointer of FIT_TABLE.
  2564. Returns:
  2565. None.
  2566. --*/
  2567. {
  2568. if ((FitTablePtr->CvAndType & CHECKSUM_BIT_MASK) >> 7) {
  2569. FitTablePtr->CheckSum = 0;
  2570. FitTablePtr->CheckSum = CalculateChecksum8 ((UINT8 *) FitTablePtr, FitTablePtr->CompSize * 16);
  2571. }
  2572. }
  2573. EFI_STATUS
  2574. CalculateFvSize (
  2575. FV_INFO *FvInfoPtr
  2576. )
  2577. /*++
  2578. Routine Description:
  2579. Calculate the FV size and Update Fv Size based on the actual FFS files.
  2580. And Update FvInfo data.
  2581. Arguments:
  2582. FvInfoPtr - The pointer to FV_INFO structure.
  2583. Returns:
  2584. EFI_ABORTED - Ffs Image Error
  2585. EFI_SUCCESS - Successfully update FvSize
  2586. --*/
  2587. {
  2588. UINTN CurrentOffset;
  2589. UINTN Index;
  2590. FILE *fpin;
  2591. UINTN FfsFileSize;
  2592. UINTN FvExtendHeaderSize;
  2593. UINT32 FfsAlignment;
  2594. UINT32 FfsHeaderSize;
  2595. EFI_FFS_FILE_HEADER FfsHeader;
  2596. UINTN VtfFileSize;
  2597. FvExtendHeaderSize = 0;
  2598. VtfFileSize = 0;
  2599. fpin = NULL;
  2600. Index = 0;
  2601. //
  2602. // Compute size for easy access later
  2603. //
  2604. FvInfoPtr->Size = 0;
  2605. for (Index = 0; FvInfoPtr->FvBlocks[Index].NumBlocks > 0 && FvInfoPtr->FvBlocks[Index].Length > 0; Index++) {
  2606. FvInfoPtr->Size += FvInfoPtr->FvBlocks[Index].NumBlocks * FvInfoPtr->FvBlocks[Index].Length;
  2607. }
  2608. //
  2609. // Calculate the required sizes for all FFS files.
  2610. //
  2611. CurrentOffset = sizeof (EFI_FIRMWARE_VOLUME_HEADER);
  2612. for (Index = 1;; Index ++) {
  2613. CurrentOffset += sizeof (EFI_FV_BLOCK_MAP_ENTRY);
  2614. if (FvInfoPtr->FvBlocks[Index].NumBlocks == 0 || FvInfoPtr->FvBlocks[Index].Length == 0) {
  2615. break;
  2616. }
  2617. }
  2618. //
  2619. // Calculate PI extension header
  2620. //
  2621. if (mFvDataInfo.FvExtHeaderFile[0] != '\0') {
  2622. fpin = fopen (LongFilePath (mFvDataInfo.FvExtHeaderFile), "rb");
  2623. if (fpin == NULL) {
  2624. Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);
  2625. return EFI_ABORTED;
  2626. }
  2627. FvExtendHeaderSize = _filelength (fileno (fpin));
  2628. fclose (fpin);
  2629. if (sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize >= MAX_FFS_SIZE) {
  2630. CurrentOffset += sizeof (EFI_FFS_FILE_HEADER2) + FvExtendHeaderSize;
  2631. mIsLargeFfs = TRUE;
  2632. } else {
  2633. CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize;
  2634. }
  2635. CurrentOffset = (CurrentOffset + 7) & (~7);
  2636. } else if (mFvDataInfo.FvNameGuidSet) {
  2637. CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);
  2638. CurrentOffset = (CurrentOffset + 7) & (~7);
  2639. }
  2640. //
  2641. // Accumulate every FFS file size.
  2642. //
  2643. for (Index = 0; FvInfoPtr->FvFiles[Index][0] != 0; Index++) {
  2644. //
  2645. // Open FFS file
  2646. //
  2647. fpin = NULL;
  2648. fpin = fopen (LongFilePath (FvInfoPtr->FvFiles[Index]), "rb");
  2649. if (fpin == NULL) {
  2650. Error (NULL, 0, 0001, "Error opening file", FvInfoPtr->FvFiles[Index]);
  2651. return EFI_ABORTED;
  2652. }
  2653. //
  2654. // Get the file size
  2655. //
  2656. FfsFileSize = _filelength (fileno (fpin));
  2657. if (FfsFileSize >= MAX_FFS_SIZE) {
  2658. FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER2);
  2659. mIsLargeFfs = TRUE;
  2660. } else {
  2661. FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER);
  2662. }
  2663. //
  2664. // Read Ffs File header
  2665. //
  2666. fread (&FfsHeader, sizeof (UINT8), sizeof (EFI_FFS_FILE_HEADER), fpin);
  2667. //
  2668. // close file
  2669. //
  2670. fclose (fpin);
  2671. if (FvInfoPtr->IsPiFvImage) {
  2672. //
  2673. // Check whether this ffs file is vtf file
  2674. //
  2675. if (IsVtfFile (&FfsHeader)) {
  2676. if (VtfFileFlag) {
  2677. //
  2678. // One Fv image can't have two vtf files.
  2679. //
  2680. Error (NULL, 0, 3000,"Invalid", "One Fv image can't have two vtf files.");
  2681. return EFI_ABORTED;
  2682. }
  2683. VtfFileFlag = TRUE;
  2684. VtfFileSize = FfsFileSize;
  2685. continue;
  2686. }
  2687. //
  2688. // Get the alignment of FFS file
  2689. //
  2690. ReadFfsAlignment (&FfsHeader, &FfsAlignment);
  2691. FfsAlignment = 1 << FfsAlignment;
  2692. //
  2693. // Add Pad file
  2694. //
  2695. if (((CurrentOffset + FfsHeaderSize) % FfsAlignment) != 0) {
  2696. //
  2697. // Only EFI_FFS_FILE_HEADER is needed for a pad section.
  2698. //
  2699. CurrentOffset = (CurrentOffset + FfsHeaderSize + sizeof(EFI_FFS_FILE_HEADER) + FfsAlignment - 1) & ~(FfsAlignment - 1);
  2700. CurrentOffset -= FfsHeaderSize;
  2701. }
  2702. }
  2703. //
  2704. // Add ffs file size
  2705. //
  2706. if (FvInfoPtr->SizeofFvFiles[Index] > FfsFileSize) {
  2707. CurrentOffset += FvInfoPtr->SizeofFvFiles[Index];
  2708. } else {
  2709. CurrentOffset += FfsFileSize;
  2710. }
  2711. //
  2712. // Make next ffs file start at QWord Boundary
  2713. //
  2714. if (FvInfoPtr->IsPiFvImage) {
  2715. CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
  2716. }
  2717. }
  2718. CurrentOffset += VtfFileSize;
  2719. DebugMsg (NULL, 0, 9, "FvImage size", "The calculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);
  2720. if (FvInfoPtr->Size == 0) {
  2721. //
  2722. // Update FvInfo data
  2723. //
  2724. FvInfoPtr->FvBlocks[0].NumBlocks = CurrentOffset / FvInfoPtr->FvBlocks[0].Length + ((CurrentOffset % FvInfoPtr->FvBlocks[0].Length)?1:0);
  2725. FvInfoPtr->Size = FvInfoPtr->FvBlocks[0].NumBlocks * FvInfoPtr->FvBlocks[0].Length;
  2726. FvInfoPtr->FvBlocks[1].NumBlocks = 0;
  2727. FvInfoPtr->FvBlocks[1].Length = 0;
  2728. } else if (FvInfoPtr->Size < CurrentOffset) {
  2729. //
  2730. // Not invalid
  2731. //
  2732. Error (NULL, 0, 3000, "Invalid", "the required fv image size 0x%x exceeds the set fv image size 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);
  2733. return EFI_INVALID_PARAMETER;
  2734. }
  2735. //
  2736. // Set Fv Size Information
  2737. //
  2738. mFvTotalSize = FvInfoPtr->Size;
  2739. mFvTakenSize = CurrentOffset;
  2740. return EFI_SUCCESS;
  2741. }
  2742. EFI_STATUS
  2743. FfsRebaseImageRead (
  2744. IN VOID *FileHandle,
  2745. IN UINTN FileOffset,
  2746. IN OUT UINT32 *ReadSize,
  2747. OUT VOID *Buffer
  2748. )
  2749. /*++
  2750. Routine Description:
  2751. Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
  2752. Arguments:
  2753. FileHandle - The handle to the PE/COFF file
  2754. FileOffset - The offset, in bytes, into the file to read
  2755. ReadSize - The number of bytes to read from the file starting at FileOffset
  2756. Buffer - A pointer to the buffer to read the data into.
  2757. Returns:
  2758. EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
  2759. --*/
  2760. {
  2761. CHAR8 *Destination8;
  2762. CHAR8 *Source8;
  2763. UINT32 Length;
  2764. Destination8 = Buffer;
  2765. Source8 = (CHAR8 *) ((UINTN) FileHandle + FileOffset);
  2766. Length = *ReadSize;
  2767. while (Length--) {
  2768. *(Destination8++) = *(Source8++);
  2769. }
  2770. return EFI_SUCCESS;
  2771. }
  2772. EFI_STATUS
  2773. GetChildFvFromFfs (
  2774. IN FV_INFO *FvInfo,
  2775. IN EFI_FFS_FILE_HEADER *FfsFile,
  2776. IN UINTN XipOffset
  2777. )
  2778. /*++
  2779. Routine Description:
  2780. This function gets all child FvImages in the input FfsFile, and records
  2781. their base address to the parent image.
  2782. Arguments:
  2783. FvInfo A pointer to FV_INFO structure.
  2784. FfsFile A pointer to Ffs file image that may contain FvImage.
  2785. XipOffset The offset address to the parent FvImage base.
  2786. Returns:
  2787. EFI_SUCCESS Base address of child Fv image is recorded.
  2788. --*/
  2789. {
  2790. EFI_STATUS Status;
  2791. UINTN Index;
  2792. EFI_FILE_SECTION_POINTER SubFvSection;
  2793. EFI_FIRMWARE_VOLUME_HEADER *SubFvImageHeader;
  2794. EFI_PHYSICAL_ADDRESS SubFvBaseAddress;
  2795. EFI_FILE_SECTION_POINTER CorePe32;
  2796. UINT16 MachineType;
  2797. for (Index = 1;; Index++) {
  2798. //
  2799. // Find FV section
  2800. //
  2801. Status = GetSectionByType (FfsFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Index, &SubFvSection);
  2802. if (EFI_ERROR (Status)) {
  2803. break;
  2804. }
  2805. SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + GetSectionHeaderLength(SubFvSection.FVImageSection));
  2806. //
  2807. // See if there's an SEC core in the child FV
  2808. Status = FindCorePeSection(SubFvImageHeader, SubFvImageHeader->FvLength, EFI_FV_FILETYPE_SECURITY_CORE, &CorePe32);
  2809. // if we couldn't find the SEC core, look for a PEI core
  2810. if (EFI_ERROR(Status)) {
  2811. Status = FindCorePeSection(SubFvImageHeader, SubFvImageHeader->FvLength, EFI_FV_FILETYPE_PEI_CORE, &CorePe32);
  2812. }
  2813. if (!EFI_ERROR(Status)) {
  2814. Status = GetCoreMachineType(CorePe32, &MachineType);
  2815. if (EFI_ERROR(Status)) {
  2816. Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC/PEI Core.");
  2817. return EFI_ABORTED;
  2818. }
  2819. // machine type is ARM, set a flag so ARM reset vector processing occurs
  2820. if ((MachineType == EFI_IMAGE_MACHINE_ARMT) || (MachineType == EFI_IMAGE_MACHINE_AARCH64)) {
  2821. VerboseMsg("Located ARM/AArch64 SEC/PEI core in child FV");
  2822. mArm = TRUE;
  2823. }
  2824. }
  2825. //
  2826. // Rebase on Flash
  2827. //
  2828. SubFvBaseAddress = FvInfo->BaseAddress + (UINTN) SubFvImageHeader - (UINTN) FfsFile + XipOffset;
  2829. mFvBaseAddress[mFvBaseAddressNumber ++ ] = SubFvBaseAddress;
  2830. }
  2831. return EFI_SUCCESS;
  2832. }
  2833. EFI_STATUS
  2834. FfsRebase (
  2835. IN OUT FV_INFO *FvInfo,
  2836. IN CHAR8 *FileName,
  2837. IN OUT EFI_FFS_FILE_HEADER *FfsFile,
  2838. IN UINTN XipOffset,
  2839. IN FILE *FvMapFile
  2840. )
  2841. /*++
  2842. Routine Description:
  2843. This function determines if a file is XIP and should be rebased. It will
  2844. rebase any PE32 sections found in the file using the base address.
  2845. Arguments:
  2846. FvInfo A pointer to FV_INFO structure.
  2847. FileName Ffs File PathName
  2848. FfsFile A pointer to Ffs file image.
  2849. XipOffset The offset address to use for rebasing the XIP file image.
  2850. FvMapFile FvMapFile to record the function address in one Fvimage
  2851. Returns:
  2852. EFI_SUCCESS The image was properly rebased.
  2853. EFI_INVALID_PARAMETER An input parameter is invalid.
  2854. EFI_ABORTED An error occurred while rebasing the input file image.
  2855. EFI_OUT_OF_RESOURCES Could not allocate a required resource.
  2856. EFI_NOT_FOUND No compressed sections could be found.
  2857. --*/
  2858. {
  2859. EFI_STATUS Status;
  2860. PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  2861. PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext;
  2862. EFI_PHYSICAL_ADDRESS XipBase;
  2863. EFI_PHYSICAL_ADDRESS NewPe32BaseAddress;
  2864. UINTN Index;
  2865. EFI_FILE_SECTION_POINTER CurrentPe32Section;
  2866. EFI_FFS_FILE_STATE SavedState;
  2867. EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
  2868. EFI_TE_IMAGE_HEADER *TEImageHeader;
  2869. UINT8 *MemoryImagePointer;
  2870. EFI_IMAGE_SECTION_HEADER *SectionHeader;
  2871. CHAR8 PeFileName [MAX_LONG_FILE_PATH];
  2872. CHAR8 *Cptr;
  2873. FILE *PeFile;
  2874. UINT8 *PeFileBuffer;
  2875. UINT32 PeFileSize;
  2876. CHAR8 *PdbPointer;
  2877. UINT32 FfsHeaderSize;
  2878. UINT32 CurSecHdrSize;
  2879. Index = 0;
  2880. MemoryImagePointer = NULL;
  2881. TEImageHeader = NULL;
  2882. ImgHdr = NULL;
  2883. SectionHeader = NULL;
  2884. Cptr = NULL;
  2885. PeFile = NULL;
  2886. PeFileBuffer = NULL;
  2887. //
  2888. // Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
  2889. //
  2890. if ((FvInfo->BaseAddress == 0) && (FvInfo->ForceRebase == -1)) {
  2891. return EFI_SUCCESS;
  2892. }
  2893. //
  2894. // If ForceRebase Flag specified to FALSE, will always not take rebase action.
  2895. //
  2896. if (FvInfo->ForceRebase == 0) {
  2897. return EFI_SUCCESS;
  2898. }
  2899. XipBase = FvInfo->BaseAddress + XipOffset;
  2900. //
  2901. // We only process files potentially containing PE32 sections.
  2902. //
  2903. switch (FfsFile->Type) {
  2904. case EFI_FV_FILETYPE_SECURITY_CORE:
  2905. case EFI_FV_FILETYPE_PEI_CORE:
  2906. case EFI_FV_FILETYPE_PEIM:
  2907. case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
  2908. case EFI_FV_FILETYPE_DRIVER:
  2909. case EFI_FV_FILETYPE_DXE_CORE:
  2910. break;
  2911. case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:
  2912. //
  2913. // Rebase the inside FvImage.
  2914. //
  2915. GetChildFvFromFfs (FvInfo, FfsFile, XipOffset);
  2916. //
  2917. // Search PE/TE section in FV sectin.
  2918. //
  2919. break;
  2920. default:
  2921. return EFI_SUCCESS;
  2922. }
  2923. FfsHeaderSize = GetFfsHeaderLength(FfsFile);
  2924. //
  2925. // Rebase each PE32 section
  2926. //
  2927. Status = EFI_SUCCESS;
  2928. for (Index = 1;; Index++) {
  2929. //
  2930. // Init Value
  2931. //
  2932. NewPe32BaseAddress = 0;
  2933. //
  2934. // Find Pe Image
  2935. //
  2936. Status = GetSectionByType (FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
  2937. if (EFI_ERROR (Status)) {
  2938. break;
  2939. }
  2940. CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
  2941. //
  2942. // Initialize context
  2943. //
  2944. memset (&ImageContext, 0, sizeof (ImageContext));
  2945. ImageContext.Handle = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize);
  2946. ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
  2947. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  2948. if (EFI_ERROR (Status)) {
  2949. Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
  2950. return Status;
  2951. }
  2952. if ( (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
  2953. (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64) ) {
  2954. mArm = TRUE;
  2955. }
  2956. //
  2957. // Keep Image Context for PE image in FV
  2958. //
  2959. memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
  2960. //
  2961. // Get File PdbPointer
  2962. //
  2963. PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
  2964. //
  2965. // Get PeHeader pointer
  2966. //
  2967. ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize + ImageContext.PeCoffHeaderOffset);
  2968. //
  2969. // Calculate the PE32 base address, based on file type
  2970. //
  2971. switch (FfsFile->Type) {
  2972. case EFI_FV_FILETYPE_SECURITY_CORE:
  2973. case EFI_FV_FILETYPE_PEI_CORE:
  2974. case EFI_FV_FILETYPE_PEIM:
  2975. case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
  2976. //
  2977. // Check if section-alignment and file-alignment match or not
  2978. //
  2979. if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
  2980. //
  2981. // Xip module has the same section alignment and file alignment.
  2982. //
  2983. Error (NULL, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName);
  2984. return EFI_ABORTED;
  2985. }
  2986. //
  2987. // PeImage has no reloc section. It will try to get reloc data from the original EFI image.
  2988. //
  2989. if (ImageContext.RelocationsStripped) {
  2990. //
  2991. // Construct the original efi file Name
  2992. //
  2993. if (strlen (FileName) >= MAX_LONG_FILE_PATH) {
  2994. Error (NULL, 0, 2000, "Invalid", "The file name %s is too long.", FileName);
  2995. return EFI_ABORTED;
  2996. }
  2997. strncpy (PeFileName, FileName, MAX_LONG_FILE_PATH - 1);
  2998. PeFileName[MAX_LONG_FILE_PATH - 1] = 0;
  2999. Cptr = PeFileName + strlen (PeFileName);
  3000. while (*Cptr != '.') {
  3001. Cptr --;
  3002. }
  3003. if (*Cptr != '.') {
  3004. Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
  3005. return EFI_ABORTED;
  3006. } else {
  3007. *(Cptr + 1) = 'e';
  3008. *(Cptr + 2) = 'f';
  3009. *(Cptr + 3) = 'i';
  3010. *(Cptr + 4) = '\0';
  3011. }
  3012. PeFile = fopen (LongFilePath (PeFileName), "rb");
  3013. if (PeFile == NULL) {
  3014. Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
  3015. //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
  3016. //return EFI_ABORTED;
  3017. break;
  3018. }
  3019. //
  3020. // Get the file size
  3021. //
  3022. PeFileSize = _filelength (fileno (PeFile));
  3023. PeFileBuffer = (UINT8 *) malloc (PeFileSize);
  3024. if (PeFileBuffer == NULL) {
  3025. fclose (PeFile);
  3026. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
  3027. return EFI_OUT_OF_RESOURCES;
  3028. }
  3029. //
  3030. // Read Pe File
  3031. //
  3032. fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
  3033. //
  3034. // close file
  3035. //
  3036. fclose (PeFile);
  3037. //
  3038. // Handle pointer to the original efi image.
  3039. //
  3040. ImageContext.Handle = PeFileBuffer;
  3041. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  3042. if (EFI_ERROR (Status)) {
  3043. Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
  3044. return Status;
  3045. }
  3046. ImageContext.RelocationsStripped = FALSE;
  3047. }
  3048. NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
  3049. break;
  3050. case EFI_FV_FILETYPE_DRIVER:
  3051. case EFI_FV_FILETYPE_DXE_CORE:
  3052. //
  3053. // Check if section-alignment and file-alignment match or not
  3054. //
  3055. if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
  3056. //
  3057. // Xip module has the same section alignment and file alignment.
  3058. //
  3059. Error (NULL, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName);
  3060. return EFI_ABORTED;
  3061. }
  3062. NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
  3063. break;
  3064. default:
  3065. //
  3066. // Not supported file type
  3067. //
  3068. return EFI_SUCCESS;
  3069. }
  3070. //
  3071. // Relocation doesn't exist
  3072. //
  3073. if (ImageContext.RelocationsStripped) {
  3074. Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
  3075. continue;
  3076. }
  3077. //
  3078. // Relocation exist and rebase
  3079. //
  3080. //
  3081. // Load and Relocate Image Data
  3082. //
  3083. MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
  3084. if (MemoryImagePointer == NULL) {
  3085. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
  3086. return EFI_OUT_OF_RESOURCES;
  3087. }
  3088. memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
  3089. ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
  3090. Status = PeCoffLoaderLoadImage (&ImageContext);
  3091. if (EFI_ERROR (Status)) {
  3092. Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
  3093. free ((VOID *) MemoryImagePointer);
  3094. return Status;
  3095. }
  3096. ImageContext.DestinationAddress = NewPe32BaseAddress;
  3097. Status = PeCoffLoaderRelocateImage (&ImageContext);
  3098. if (EFI_ERROR (Status)) {
  3099. Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName);
  3100. free ((VOID *) MemoryImagePointer);
  3101. return Status;
  3102. }
  3103. //
  3104. // Copy Relocated data to raw image file.
  3105. //
  3106. SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (
  3107. (UINTN) ImgHdr +
  3108. sizeof (UINT32) +
  3109. sizeof (EFI_IMAGE_FILE_HEADER) +
  3110. ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
  3111. );
  3112. for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {
  3113. CopyMem (
  3114. (UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize + SectionHeader->PointerToRawData,
  3115. (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
  3116. SectionHeader->SizeOfRawData
  3117. );
  3118. }
  3119. free ((VOID *) MemoryImagePointer);
  3120. MemoryImagePointer = NULL;
  3121. if (PeFileBuffer != NULL) {
  3122. free (PeFileBuffer);
  3123. PeFileBuffer = NULL;
  3124. }
  3125. //
  3126. // Update Image Base Address
  3127. //
  3128. if (ImgHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  3129. ImgHdr->Pe32.OptionalHeader.ImageBase = (UINT32) NewPe32BaseAddress;
  3130. } else if (ImgHdr->Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  3131. ImgHdr->Pe32Plus.OptionalHeader.ImageBase = NewPe32BaseAddress;
  3132. } else {
  3133. Error (NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
  3134. ImgHdr->Pe32.OptionalHeader.Magic,
  3135. FileName
  3136. );
  3137. return EFI_ABORTED;
  3138. }
  3139. //
  3140. // Now update file checksum
  3141. //
  3142. if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
  3143. SavedState = FfsFile->State;
  3144. FfsFile->IntegrityCheck.Checksum.File = 0;
  3145. FfsFile->State = 0;
  3146. FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
  3147. (UINT8 *) ((UINT8 *)FfsFile + FfsHeaderSize),
  3148. GetFfsFileLength (FfsFile) - FfsHeaderSize
  3149. );
  3150. FfsFile->State = SavedState;
  3151. }
  3152. //
  3153. // Get this module function address from ModulePeMapFile and add them into FvMap file
  3154. //
  3155. //
  3156. // Default use FileName as map file path
  3157. //
  3158. if (PdbPointer == NULL) {
  3159. PdbPointer = FileName;
  3160. }
  3161. WriteMapFile (FvMapFile, PdbPointer, FfsFile, NewPe32BaseAddress, &OrigImageContext);
  3162. }
  3163. if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
  3164. FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
  3165. FfsFile->Type != EFI_FV_FILETYPE_PEIM &&
  3166. FfsFile->Type != EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER &&
  3167. FfsFile->Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
  3168. ) {
  3169. //
  3170. // Only Peim code may have a TE section
  3171. //
  3172. return EFI_SUCCESS;
  3173. }
  3174. //
  3175. // Now process TE sections
  3176. //
  3177. for (Index = 1;; Index++) {
  3178. NewPe32BaseAddress = 0;
  3179. //
  3180. // Find Te Image
  3181. //
  3182. Status = GetSectionByType (FfsFile, EFI_SECTION_TE, Index, &CurrentPe32Section);
  3183. if (EFI_ERROR (Status)) {
  3184. break;
  3185. }
  3186. CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
  3187. //
  3188. // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
  3189. // by GenTEImage
  3190. //
  3191. TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize);
  3192. //
  3193. // Initialize context, load image info.
  3194. //
  3195. memset (&ImageContext, 0, sizeof (ImageContext));
  3196. ImageContext.Handle = (VOID *) TEImageHeader;
  3197. ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;
  3198. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  3199. if (EFI_ERROR (Status)) {
  3200. Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
  3201. return Status;
  3202. }
  3203. if ( (ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
  3204. (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64) ) {
  3205. mArm = TRUE;
  3206. }
  3207. //
  3208. // Keep Image Context for TE image in FV
  3209. //
  3210. memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));
  3211. //
  3212. // Get File PdbPointer
  3213. //
  3214. PdbPointer = PeCoffLoaderGetPdbPointer (ImageContext.Handle);
  3215. //
  3216. // Set new rebased address.
  3217. //
  3218. NewPe32BaseAddress = XipBase + (UINTN) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) \
  3219. - TEImageHeader->StrippedSize - (UINTN) FfsFile;
  3220. //
  3221. // if reloc is stripped, try to get the original efi image to get reloc info.
  3222. //
  3223. if (ImageContext.RelocationsStripped) {
  3224. //
  3225. // Construct the original efi file name
  3226. //
  3227. if (strlen (FileName) >= MAX_LONG_FILE_PATH) {
  3228. Error (NULL, 0, 2000, "Invalid", "The file name %s is too long.", FileName);
  3229. return EFI_ABORTED;
  3230. }
  3231. strncpy (PeFileName, FileName, MAX_LONG_FILE_PATH - 1);
  3232. PeFileName[MAX_LONG_FILE_PATH - 1] = 0;
  3233. Cptr = PeFileName + strlen (PeFileName);
  3234. while (*Cptr != '.') {
  3235. Cptr --;
  3236. }
  3237. if (*Cptr != '.') {
  3238. Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
  3239. return EFI_ABORTED;
  3240. } else {
  3241. *(Cptr + 1) = 'e';
  3242. *(Cptr + 2) = 'f';
  3243. *(Cptr + 3) = 'i';
  3244. *(Cptr + 4) = '\0';
  3245. }
  3246. PeFile = fopen (LongFilePath (PeFileName), "rb");
  3247. if (PeFile == NULL) {
  3248. Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
  3249. //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
  3250. //return EFI_ABORTED;
  3251. } else {
  3252. //
  3253. // Get the file size
  3254. //
  3255. PeFileSize = _filelength (fileno (PeFile));
  3256. PeFileBuffer = (UINT8 *) malloc (PeFileSize);
  3257. if (PeFileBuffer == NULL) {
  3258. fclose (PeFile);
  3259. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
  3260. return EFI_OUT_OF_RESOURCES;
  3261. }
  3262. //
  3263. // Read Pe File
  3264. //
  3265. fread (PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
  3266. //
  3267. // close file
  3268. //
  3269. fclose (PeFile);
  3270. //
  3271. // Append reloc section into TeImage
  3272. //
  3273. ImageContext.Handle = PeFileBuffer;
  3274. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  3275. if (EFI_ERROR (Status)) {
  3276. Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);
  3277. return Status;
  3278. }
  3279. ImageContext.RelocationsStripped = FALSE;
  3280. }
  3281. }
  3282. //
  3283. // Relocation doesn't exist
  3284. //
  3285. if (ImageContext.RelocationsStripped) {
  3286. Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
  3287. continue;
  3288. }
  3289. //
  3290. // Relocation exist and rebase
  3291. //
  3292. //
  3293. // Load and Relocate Image Data
  3294. //
  3295. MemoryImagePointer = (UINT8 *) malloc ((UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
  3296. if (MemoryImagePointer == NULL) {
  3297. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
  3298. return EFI_OUT_OF_RESOURCES;
  3299. }
  3300. memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);
  3301. ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));
  3302. Status = PeCoffLoaderLoadImage (&ImageContext);
  3303. if (EFI_ERROR (Status)) {
  3304. Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
  3305. free ((VOID *) MemoryImagePointer);
  3306. return Status;
  3307. }
  3308. //
  3309. // Reloacate TeImage
  3310. //
  3311. ImageContext.DestinationAddress = NewPe32BaseAddress;
  3312. Status = PeCoffLoaderRelocateImage (&ImageContext);
  3313. if (EFI_ERROR (Status)) {
  3314. Error (NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName);
  3315. free ((VOID *) MemoryImagePointer);
  3316. return Status;
  3317. }
  3318. //
  3319. // Copy the relocated image into raw image file.
  3320. //
  3321. SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);
  3322. for (Index = 0; Index < TEImageHeader->NumberOfSections; Index ++, SectionHeader ++) {
  3323. if (!ImageContext.IsTeImage) {
  3324. CopyMem (
  3325. (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
  3326. (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),
  3327. SectionHeader->SizeOfRawData
  3328. );
  3329. } else {
  3330. CopyMem (
  3331. (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
  3332. (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->VirtualAddress),
  3333. SectionHeader->SizeOfRawData
  3334. );
  3335. }
  3336. }
  3337. //
  3338. // Free the allocated memory resource
  3339. //
  3340. free ((VOID *) MemoryImagePointer);
  3341. MemoryImagePointer = NULL;
  3342. if (PeFileBuffer != NULL) {
  3343. free (PeFileBuffer);
  3344. PeFileBuffer = NULL;
  3345. }
  3346. //
  3347. // Update Image Base Address
  3348. //
  3349. TEImageHeader->ImageBase = NewPe32BaseAddress;
  3350. //
  3351. // Now update file checksum
  3352. //
  3353. if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
  3354. SavedState = FfsFile->State;
  3355. FfsFile->IntegrityCheck.Checksum.File = 0;
  3356. FfsFile->State = 0;
  3357. FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (
  3358. (UINT8 *)((UINT8 *)FfsFile + FfsHeaderSize),
  3359. GetFfsFileLength (FfsFile) - FfsHeaderSize
  3360. );
  3361. FfsFile->State = SavedState;
  3362. }
  3363. //
  3364. // Get this module function address from ModulePeMapFile and add them into FvMap file
  3365. //
  3366. //
  3367. // Default use FileName as map file path
  3368. //
  3369. if (PdbPointer == NULL) {
  3370. PdbPointer = FileName;
  3371. }
  3372. WriteMapFile (
  3373. FvMapFile,
  3374. PdbPointer,
  3375. FfsFile,
  3376. NewPe32BaseAddress,
  3377. &OrigImageContext
  3378. );
  3379. }
  3380. return EFI_SUCCESS;
  3381. }
  3382. EFI_STATUS
  3383. FindApResetVectorPosition (
  3384. IN MEMORY_FILE *FvImage,
  3385. OUT UINT8 **Pointer
  3386. )
  3387. /*++
  3388. Routine Description:
  3389. Find the position in this FvImage to place Ap reset vector.
  3390. Arguments:
  3391. FvImage Memory file for the FV memory image.
  3392. Pointer Pointer to pointer to position.
  3393. Returns:
  3394. EFI_NOT_FOUND - No satisfied position is found.
  3395. EFI_SUCCESS - The suitable position is return.
  3396. --*/
  3397. {
  3398. EFI_FFS_FILE_HEADER *PadFile;
  3399. UINT32 Index;
  3400. EFI_STATUS Status;
  3401. UINT8 *FixPoint;
  3402. UINT32 FileLength;
  3403. for (Index = 1; ;Index ++) {
  3404. //
  3405. // Find Pad File to add ApResetVector info
  3406. //
  3407. Status = GetFileByType (EFI_FV_FILETYPE_FFS_PAD, Index, &PadFile);
  3408. if (EFI_ERROR (Status) || (PadFile == NULL)) {
  3409. //
  3410. // No Pad file to be found.
  3411. //
  3412. break;
  3413. }
  3414. //
  3415. // Get Pad file size.
  3416. //
  3417. FileLength = GetFfsFileLength(PadFile);
  3418. FileLength = (FileLength + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);
  3419. //
  3420. // FixPoint must be align on 0x1000 relative to FvImage Header
  3421. //
  3422. FixPoint = (UINT8*) PadFile + GetFfsHeaderLength(PadFile);
  3423. FixPoint = FixPoint + 0x1000 - (((UINTN) FixPoint - (UINTN) FvImage->FileImage) & 0xFFF);
  3424. //
  3425. // FixPoint be larger at the last place of one fv image.
  3426. //
  3427. while (((UINTN) FixPoint + SIZEOF_STARTUP_DATA_ARRAY - (UINTN) PadFile) <= FileLength) {
  3428. FixPoint += 0x1000;
  3429. }
  3430. FixPoint -= 0x1000;
  3431. if ((UINTN) FixPoint < ((UINTN) PadFile + GetFfsHeaderLength(PadFile))) {
  3432. //
  3433. // No alignment FixPoint in this Pad File.
  3434. //
  3435. continue;
  3436. }
  3437. if ((UINTN) FvImage->Eof - (UINTN)FixPoint <= 0x20000) {
  3438. //
  3439. // Find the position to place ApResetVector
  3440. //
  3441. *Pointer = FixPoint;
  3442. return EFI_SUCCESS;
  3443. }
  3444. }
  3445. return EFI_NOT_FOUND;
  3446. }
  3447. EFI_STATUS
  3448. ParseCapInf (
  3449. IN MEMORY_FILE *InfFile,
  3450. OUT CAP_INFO *CapInfo
  3451. )
  3452. /*++
  3453. Routine Description:
  3454. This function parses a Cap.INF file and copies info into a CAP_INFO structure.
  3455. Arguments:
  3456. InfFile Memory file image.
  3457. CapInfo Information read from INF file.
  3458. Returns:
  3459. EFI_SUCCESS INF file information successfully retrieved.
  3460. EFI_ABORTED INF file has an invalid format.
  3461. EFI_NOT_FOUND A required string was not found in the INF file.
  3462. --*/
  3463. {
  3464. CHAR8 Value[MAX_LONG_FILE_PATH];
  3465. UINT64 Value64;
  3466. UINTN Index, Number;
  3467. EFI_STATUS Status;
  3468. //
  3469. // Initialize Cap info
  3470. //
  3471. // memset (CapInfo, 0, sizeof (CAP_INFO));
  3472. //
  3473. //
  3474. // Read the Capsule Guid
  3475. //
  3476. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_GUID_STRING, 0, Value);
  3477. if (Status == EFI_SUCCESS) {
  3478. //
  3479. // Get the Capsule Guid
  3480. //
  3481. Status = StringToGuid (Value, &CapInfo->CapGuid);
  3482. if (EFI_ERROR (Status)) {
  3483. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
  3484. return EFI_ABORTED;
  3485. }
  3486. DebugMsg (NULL, 0, 9, "Capsule Guid", "%s = %s", EFI_CAPSULE_GUID_STRING, Value);
  3487. }
  3488. //
  3489. // Read the Capsule Header Size
  3490. //
  3491. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_HEADER_SIZE_STRING, 0, Value);
  3492. if (Status == EFI_SUCCESS) {
  3493. Status = AsciiStringToUint64 (Value, FALSE, &Value64);
  3494. if (EFI_ERROR (Status)) {
  3495. Error (NULL, 0, 2000, "Invalid parameter", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
  3496. return EFI_ABORTED;
  3497. }
  3498. CapInfo->HeaderSize = (UINT32) Value64;
  3499. DebugMsg (NULL, 0, 9, "Capsule Header size", "%s = %s", EFI_CAPSULE_HEADER_SIZE_STRING, Value);
  3500. }
  3501. //
  3502. // Read the Capsule Flag
  3503. //
  3504. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_CAPSULE_FLAGS_STRING, 0, Value);
  3505. if (Status == EFI_SUCCESS) {
  3506. if (strstr (Value, "PopulateSystemTable") != NULL) {
  3507. CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE;
  3508. if (strstr (Value, "InitiateReset") != NULL) {
  3509. CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
  3510. }
  3511. } else if (strstr (Value, "PersistAcrossReset") != NULL) {
  3512. CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET;
  3513. if (strstr (Value, "InitiateReset") != NULL) {
  3514. CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;
  3515. }
  3516. } else {
  3517. Error (NULL, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING);
  3518. return EFI_ABORTED;
  3519. }
  3520. DebugMsg (NULL, 0, 9, "Capsule Flag", Value);
  3521. }
  3522. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_OEM_CAPSULE_FLAGS_STRING, 0, Value);
  3523. if (Status == EFI_SUCCESS) {
  3524. Status = AsciiStringToUint64 (Value, FALSE, &Value64);
  3525. if (EFI_ERROR (Status) || Value64 > 0xffff) {
  3526. Error (NULL, 0, 2000, "Invalid parameter",
  3527. "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",
  3528. EFI_OEM_CAPSULE_FLAGS_STRING);
  3529. return EFI_ABORTED;
  3530. }
  3531. CapInfo->Flags |= Value64;
  3532. DebugMsg (NULL, 0, 9, "Capsule Extend Flag", Value);
  3533. }
  3534. //
  3535. // Read Capsule File name
  3536. //
  3537. Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_FILE_NAME_STRING, 0, Value);
  3538. if (Status == EFI_SUCCESS) {
  3539. //
  3540. // Get output file name
  3541. //
  3542. strcpy (CapInfo->CapName, Value);
  3543. }
  3544. //
  3545. // Read the Capsule FileImage
  3546. //
  3547. Number = 0;
  3548. for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_CAP; Index++) {
  3549. if (CapInfo->CapFiles[Index][0] != '\0') {
  3550. continue;
  3551. }
  3552. //
  3553. // Read the capsule file name
  3554. //
  3555. Status = FindToken (InfFile, FILES_SECTION_STRING, EFI_FILE_NAME_STRING, Number++, Value);
  3556. if (Status == EFI_SUCCESS) {
  3557. //
  3558. // Add the file
  3559. //
  3560. strcpy (CapInfo->CapFiles[Index], Value);
  3561. DebugMsg (NULL, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index, CapInfo->CapFiles[Index]);
  3562. } else {
  3563. break;
  3564. }
  3565. }
  3566. if (Index == 0) {
  3567. Warning (NULL, 0, 0, "Capsule components are not specified.", NULL);
  3568. }
  3569. return EFI_SUCCESS;
  3570. }
  3571. EFI_STATUS
  3572. GenerateCapImage (
  3573. IN CHAR8 *InfFileImage,
  3574. IN UINTN InfFileSize,
  3575. IN CHAR8 *CapFileName
  3576. )
  3577. /*++
  3578. Routine Description:
  3579. This is the main function which will be called from application to create UEFI Capsule image.
  3580. Arguments:
  3581. InfFileImage Buffer containing the INF file contents.
  3582. InfFileSize Size of the contents of the InfFileImage buffer.
  3583. CapFileName Requested name for the Cap file.
  3584. Returns:
  3585. EFI_SUCCESS Function completed successfully.
  3586. EFI_OUT_OF_RESOURCES Could not allocate required resources.
  3587. EFI_ABORTED Error encountered.
  3588. EFI_INVALID_PARAMETER A required parameter was NULL.
  3589. --*/
  3590. {
  3591. UINT32 CapSize;
  3592. UINT8 *CapBuffer;
  3593. EFI_CAPSULE_HEADER *CapsuleHeader;
  3594. MEMORY_FILE InfMemoryFile;
  3595. UINT32 FileSize;
  3596. UINT32 Index;
  3597. FILE *fpin, *fpout;
  3598. EFI_STATUS Status;
  3599. if (InfFileImage != NULL) {
  3600. //
  3601. // Initialize file structures
  3602. //
  3603. InfMemoryFile.FileImage = InfFileImage;
  3604. InfMemoryFile.CurrentFilePointer = InfFileImage;
  3605. InfMemoryFile.Eof = InfFileImage + InfFileSize;
  3606. //
  3607. // Parse the Cap inf file for header information
  3608. //
  3609. Status = ParseCapInf (&InfMemoryFile, &mCapDataInfo);
  3610. if (Status != EFI_SUCCESS) {
  3611. return Status;
  3612. }
  3613. }
  3614. if (mCapDataInfo.HeaderSize == 0) {
  3615. //
  3616. // make header size align 16 bytes.
  3617. //
  3618. mCapDataInfo.HeaderSize = sizeof (EFI_CAPSULE_HEADER);
  3619. mCapDataInfo.HeaderSize = (mCapDataInfo.HeaderSize + 0xF) & ~0xF;
  3620. }
  3621. if (mCapDataInfo.HeaderSize < sizeof (EFI_CAPSULE_HEADER)) {
  3622. Error (NULL, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");
  3623. return EFI_INVALID_PARAMETER;
  3624. }
  3625. if (CapFileName == NULL && mCapDataInfo.CapName[0] != '\0') {
  3626. CapFileName = mCapDataInfo.CapName;
  3627. }
  3628. if (CapFileName == NULL) {
  3629. Error (NULL, 0, 2001, "Missing required argument", "Output Capsule file name");
  3630. return EFI_INVALID_PARAMETER;
  3631. }
  3632. //
  3633. // Set Default Capsule Guid value
  3634. //
  3635. if (CompareGuid (&mCapDataInfo.CapGuid, &mZeroGuid) == 0) {
  3636. memcpy (&mCapDataInfo.CapGuid, &mDefaultCapsuleGuid, sizeof (EFI_GUID));
  3637. }
  3638. //
  3639. // Calculate the size of capsule image.
  3640. //
  3641. Index = 0;
  3642. FileSize = 0;
  3643. CapSize = mCapDataInfo.HeaderSize;
  3644. while (mCapDataInfo.CapFiles [Index][0] != '\0') {
  3645. fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");
  3646. if (fpin == NULL) {
  3647. Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
  3648. return EFI_ABORTED;
  3649. }
  3650. FileSize = _filelength (fileno (fpin));
  3651. CapSize += FileSize;
  3652. fclose (fpin);
  3653. Index ++;
  3654. }
  3655. //
  3656. // Allocate buffer for capsule image.
  3657. //
  3658. CapBuffer = (UINT8 *) malloc (CapSize);
  3659. if (CapBuffer == NULL) {
  3660. Error (NULL, 0, 4001, "Resource", "memory cannot be allocated for creating the capsule.");
  3661. return EFI_OUT_OF_RESOURCES;
  3662. }
  3663. //
  3664. // Initialize the capsule header to zero
  3665. //
  3666. memset (CapBuffer, 0, mCapDataInfo.HeaderSize);
  3667. //
  3668. // create capsule header and get capsule body
  3669. //
  3670. CapsuleHeader = (EFI_CAPSULE_HEADER *) CapBuffer;
  3671. memcpy (&CapsuleHeader->CapsuleGuid, &mCapDataInfo.CapGuid, sizeof (EFI_GUID));
  3672. CapsuleHeader->HeaderSize = mCapDataInfo.HeaderSize;
  3673. CapsuleHeader->Flags = mCapDataInfo.Flags;
  3674. CapsuleHeader->CapsuleImageSize = CapSize;
  3675. Index = 0;
  3676. FileSize = 0;
  3677. CapSize = CapsuleHeader->HeaderSize;
  3678. while (mCapDataInfo.CapFiles [Index][0] != '\0') {
  3679. fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");
  3680. if (fpin == NULL) {
  3681. Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);
  3682. free (CapBuffer);
  3683. return EFI_ABORTED;
  3684. }
  3685. FileSize = _filelength (fileno (fpin));
  3686. fread (CapBuffer + CapSize, 1, FileSize, fpin);
  3687. fclose (fpin);
  3688. Index ++;
  3689. CapSize += FileSize;
  3690. }
  3691. //
  3692. // write capsule data into the output file
  3693. //
  3694. fpout = fopen (LongFilePath (CapFileName), "wb");
  3695. if (fpout == NULL) {
  3696. Error (NULL, 0, 0001, "Error opening file", CapFileName);
  3697. free (CapBuffer);
  3698. return EFI_ABORTED;
  3699. }
  3700. fwrite (CapBuffer, 1, CapSize, fpout);
  3701. fclose (fpout);
  3702. free (CapBuffer);
  3703. VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize);
  3704. return EFI_SUCCESS;
  3705. }