image.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. image.c
  9. Abstract:
  10. This module implements UEFI core image services.
  11. Author:
  12. Evan Green 10-Mar-2014
  13. Environment:
  14. Firmware
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "ueficore.h"
  20. #include "fileinfo.h"
  21. #include "hii.h"
  22. #include "imagep.h"
  23. #include "efiimg.h"
  24. #include "fv2.h"
  25. #include <minoca/uefi/protocol/loadfil.h>
  26. #include <minoca/uefi/protocol/loadfil2.h>
  27. #include <minoca/uefi/protocol/sfilesys.h>
  28. #include <minoca/kernel/hmod.h>
  29. #include <minoca/kernel/kdebug.h>
  30. //
  31. // ---------------------------------------------------------------- Definitions
  32. //
  33. //
  34. // ------------------------------------------------------ Data Type Definitions
  35. //
  36. //
  37. // ----------------------------------------------- Internal Function Prototypes
  38. //
  39. EFI_STATUS
  40. EfipCoreLoadImage (
  41. BOOLEAN BootPolicy,
  42. EFI_HANDLE ParentImageHandle,
  43. EFI_DEVICE_PATH_PROTOCOL *FilePath,
  44. VOID *SourceBuffer,
  45. UINTN SourceSize,
  46. EFI_PHYSICAL_ADDRESS DestinationBuffer,
  47. UINTN *PageCount,
  48. EFI_HANDLE *ImageHandle,
  49. EFI_PHYSICAL_ADDRESS *EntryPoint,
  50. UINT32 Attributes
  51. );
  52. EFIAPI
  53. VOID *
  54. EfipCoreGetFileBufferByFilePath (
  55. BOOLEAN BootPolicy,
  56. CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,
  57. CHAR16 **FileName,
  58. UINTN *FileSize,
  59. UINT32 *AuthenticationStatus
  60. );
  61. EFI_STATUS
  62. EfipCoreLoadPeImage (
  63. BOOLEAN BootPolicy,
  64. VOID *PeHandle,
  65. PEFI_IMAGE_DATA Image,
  66. EFI_PHYSICAL_ADDRESS DestinationBuffer,
  67. EFI_PHYSICAL_ADDRESS *EntryPoint,
  68. UINT32 Attribute
  69. );
  70. VOID
  71. EfipCoreUnloadAndCloseImage (
  72. PEFI_IMAGE_DATA Image,
  73. BOOLEAN FreePages
  74. );
  75. PEFI_IMAGE_DATA
  76. EfipCoreGetImageDataFromHandle (
  77. EFI_HANDLE ImageHandle
  78. );
  79. EFIAPI
  80. EFI_STATUS
  81. EfipCoreReadImageFile (
  82. VOID *FileHandle,
  83. UINTN FileOffset,
  84. UINTN *ReadSize,
  85. VOID *Buffer
  86. );
  87. CHAR8 *
  88. EfipCoreConvertFileNameToAscii (
  89. CHAR16 *FileName,
  90. UINTN *AsciiNameSize
  91. );
  92. //
  93. // -------------------------------------------------------------------- Globals
  94. //
  95. PEFI_IMAGE_DATA EfiCurrentImage;
  96. EFI_IMAGE_DATA EfiFirmwareLoadedImage = {
  97. EFI_IMAGE_DATA_MAGIC,
  98. NULL,
  99. EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER,
  100. TRUE,
  101. (EFI_IMAGE_ENTRY_POINT)EfiCoreMain,
  102. {
  103. EFI_LOADED_IMAGE_INFORMATION_REVISION,
  104. NULL,
  105. NULL,
  106. NULL,
  107. NULL,
  108. NULL,
  109. 0,
  110. NULL,
  111. NULL,
  112. 0,
  113. EfiBootServicesCode,
  114. EfiBootServicesData
  115. },
  116. 0,
  117. 0,
  118. NULL,
  119. 0,
  120. EFI_SUCCESS,
  121. 0,
  122. NULL,
  123. NULL,
  124. NULL,
  125. 0,
  126. NULL,
  127. NULL,
  128. {0},
  129. EFI_SUCCESS
  130. };
  131. EFI_GUID EfiLoadedImageProtocolGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  132. EFI_GUID EfiLoadFile2ProtocolGuid = EFI_LOAD_FILE2_PROTOCOL_GUID;
  133. EFI_GUID EfiLoadFileProtocolGuid = EFI_LOAD_FILE_PROTOCOL_GUID;
  134. EFI_GUID EfiLoadedImageDevicePathProtocolGuid =
  135. EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
  136. EFI_GUID EfiHiiPackageListProtocolGuid = EFI_HII_PACKAGE_LIST_PROTOCOL_GUID;
  137. //
  138. // ------------------------------------------------------------------ Functions
  139. //
  140. EFIAPI
  141. EFI_STATUS
  142. EfiCoreLoadImage (
  143. BOOLEAN BootPolicy,
  144. EFI_HANDLE ParentImageHandle,
  145. EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  146. VOID *SourceBuffer,
  147. UINTN SourceSize,
  148. EFI_HANDLE *ImageHandle
  149. )
  150. /*++
  151. Routine Description:
  152. This routine loads an EFI image into memory.
  153. Arguments:
  154. BootPolicy - Supplies a boolean indicating that the request originates
  155. from the boot manager, and that the boot manager is attempting to load
  156. the given file path as a boot selection. This is ignored if the source
  157. buffer is NULL.
  158. ParentImageHandle - Supplies the caller's image handle.
  159. DevicePath - Supplies a pointer to the device path from which the image is
  160. loaded.
  161. SourceBuffer - Supplies an optional pointer to the memory location
  162. containing a copy of the image to be loaded.
  163. SourceSize - Supplies the size in bytes of the source buffer.
  164. ImageHandle - Supplies a pointer where the loaded image handle will be
  165. returned on success.
  166. Return Value:
  167. EFI_SUCCESS on success.
  168. EFI_NOT_FOUND if both the source buffer and device path are NULL.
  169. EFI_INVALID_PARAMETER if one or more parameters are not valid.
  170. EFI_UNSUPPORTED if the image type is unsupported.
  171. EFI_OUT_OF_RESOURCES if an allocation failed.
  172. EFI_LOAD_ERROR if the image format was corrupt or not understood.
  173. EFI_DEVICE_ERROR if the underlying device returned a read error.
  174. EFI_ACCESS_DENIED if the platform policy prohibits the image from being
  175. loaded.
  176. EFI_SECURITY_VIOLATION if the image was successfully loaded, but the
  177. platform policy indicates the image should not be started.
  178. --*/
  179. {
  180. UINT32 Attributes;
  181. EFI_STATUS Status;
  182. Attributes =
  183. EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION |
  184. EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION;
  185. Status = EfipCoreLoadImage(BootPolicy,
  186. ParentImageHandle,
  187. DevicePath,
  188. SourceBuffer,
  189. SourceSize,
  190. (EFI_PHYSICAL_ADDRESS)(UINTN)NULL,
  191. NULL,
  192. ImageHandle,
  193. NULL,
  194. Attributes);
  195. return Status;
  196. }
  197. EFIAPI
  198. EFI_STATUS
  199. EfiCoreUnloadImage (
  200. EFI_HANDLE ImageHandle
  201. )
  202. /*++
  203. Routine Description:
  204. This routine unloads an image.
  205. Arguments:
  206. ImageHandle - Supplies the handle of the image to unload.
  207. ExitStatus - Supplies the exit code.
  208. ExitDataSize - Supplies the size of the exit data. This is ignored if the
  209. exit status code is EFI_SUCCESS.
  210. ExitData - Supplies an optional pointer where a pointer will be returned
  211. that includes a null-terminated string describing the reason the
  212. application exited, optionally followed by additional binary data. This
  213. buffer must be allocated from AllocatePool.
  214. Return Value:
  215. EFI_SUCCESS if the image was unloaded.
  216. EFI_INVALID_PARAMETER if the image handle is not valid.
  217. --*/
  218. {
  219. PEFI_IMAGE_DATA Image;
  220. EFI_STATUS Status;
  221. Image = EfipCoreGetImageDataFromHandle(ImageHandle);
  222. if (Image == NULL) {
  223. Status = EFI_INVALID_PARAMETER;
  224. goto CoreUnloadImageEnd;
  225. }
  226. //
  227. // If the image has been started, request that it unload.
  228. //
  229. if (Image->Started != FALSE) {
  230. Status = EFI_UNSUPPORTED;
  231. if (Image->Information.Unload != NULL) {
  232. Status = Image->Information.Unload(ImageHandle);
  233. }
  234. //
  235. // The image has not been started, so unloading it is always okay.
  236. //
  237. } else {
  238. Status = EFI_SUCCESS;
  239. }
  240. if (!EFI_ERROR(Status)) {
  241. EfipCoreUnloadAndCloseImage(Image, TRUE);
  242. }
  243. CoreUnloadImageEnd:
  244. return Status;
  245. }
  246. EFIAPI
  247. EFI_STATUS
  248. EfiCoreStartImage (
  249. EFI_HANDLE ImageHandle,
  250. UINTN *ExitDataSize,
  251. CHAR16 **ExitData
  252. )
  253. /*++
  254. Routine Description:
  255. This routine transfers control to a loaded image's entry point.
  256. Arguments:
  257. ImageHandle - Supplies the handle of the image to run.
  258. ExitDataSize - Supplies a pointer to the size, in bytes, of the exit data.
  259. ExitData - Supplies an optional pointer where a pointer will be returned
  260. that includes a null-terminated string, optionally followed by
  261. additional binary data.
  262. Return Value:
  263. EFI_INVALID_PARAMETER if the image handle is invalid or the image has
  264. already been started.
  265. EFI_SECURITY_VIOLATION if the platform policy specifies the image should
  266. not be started.
  267. Otherwise, returns the exit code from the image.
  268. --*/
  269. {
  270. UINT64 HandleDatabaseKey;
  271. PEFI_IMAGE_DATA Image;
  272. PEFI_IMAGE_DATA LastImage;
  273. UINTN SetJumpFlag;
  274. EFI_STATUS Status;
  275. Image = EfipCoreGetImageDataFromHandle(ImageHandle);
  276. if ((Image == NULL) || (Image->Started != FALSE)) {
  277. return EFI_INVALID_PARAMETER;
  278. }
  279. if (EFI_ERROR(Image->LoadImageStatus)) {
  280. return Image->LoadImageStatus;
  281. }
  282. if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Image->Machine)) {
  283. return EFI_UNSUPPORTED;
  284. }
  285. //
  286. // Push the current start image context, and link the current image to the
  287. // head. This is the only image that can call exit.
  288. //
  289. HandleDatabaseKey = EfipCoreGetHandleDatabaseKey();
  290. LastImage = EfiCurrentImage;
  291. EfiCurrentImage = Image;
  292. Image->Tpl = EfiCurrentTpl;
  293. //
  294. // Allocate the jump buffer and set the jump target. This is needed because
  295. // the caller may call Exit several functions in on the stack and
  296. // exit needs to get back here.
  297. //
  298. Image->JumpBuffer = EfiCoreAllocateBootPool(
  299. sizeof(EFI_JUMP_BUFFER) + EFI_JUMP_BUFFER_ALIGNMENT);
  300. if (Image->JumpBuffer == NULL) {
  301. return EFI_OUT_OF_RESOURCES;
  302. }
  303. Image->JumpContext = ALIGN_POINTER(Image->JumpBuffer,
  304. EFI_JUMP_BUFFER_ALIGNMENT);
  305. SetJumpFlag = EfipArchSetJump(Image->JumpContext);
  306. //
  307. // The initial call to set jump always returns zero. Subsequent calls to
  308. // long jump cause a non-zero value to be returned here. The return
  309. // value of the set jump function is where exit jumps to.
  310. //
  311. if (SetJumpFlag == 0) {
  312. Image->Started = TRUE;
  313. Image->Status = Image->EntryPoint(ImageHandle,
  314. Image->Information.SystemTable);
  315. //
  316. // If the image returned, call exit for it.
  317. //
  318. EfiCoreExit(ImageHandle, Image->Status, 0, NULL);
  319. }
  320. //
  321. // The image has exited. Verify the TPL is the same.
  322. //
  323. ASSERT(Image->Tpl == EfiCurrentTpl);
  324. EfiCoreRestoreTpl(Image->Tpl);
  325. EfiCoreFreePool(Image->JumpBuffer);
  326. EfiCurrentImage = LastImage;
  327. EfipCoreConnectHandlesByKey(HandleDatabaseKey);
  328. //
  329. // Return the exit data to the caller, or discard it.
  330. //
  331. if ((ExitData != NULL) && (ExitDataSize != NULL)) {
  332. *ExitDataSize = Image->ExitDataSize;
  333. *ExitData = Image->ExitData;
  334. } else {
  335. EfiCoreFreePool(Image->ExitData);
  336. Image->ExitData = NULL;
  337. Image->ExitDataSize = 0;
  338. }
  339. //
  340. // If the image returned an error or the image is an application, unload it.
  341. //
  342. Status = Image->Status;
  343. if ((EFI_ERROR(Image->Status)) ||
  344. (Image->Type == EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION)) {
  345. EfipCoreUnloadAndCloseImage(Image, TRUE);
  346. }
  347. return Status;
  348. }
  349. EFIAPI
  350. EFI_STATUS
  351. EfiCoreExit (
  352. EFI_HANDLE ImageHandle,
  353. EFI_STATUS ExitStatus,
  354. UINTN ExitDataSize,
  355. CHAR16 *ExitData
  356. )
  357. /*++
  358. Routine Description:
  359. This routine terminates an loaded EFI image and returns control to boot
  360. services.
  361. Arguments:
  362. ImageHandle - Supplies the handle of the image passed upon entry.
  363. ExitStatus - Supplies the exit code.
  364. ExitDataSize - Supplies the size of the exit data. This is ignored if the
  365. exit status code is EFI_SUCCESS.
  366. ExitData - Supplies an optional pointer where a pointer will be returned
  367. that includes a null-terminated string describing the reason the
  368. application exited, optionally followed by additional binary data. This
  369. buffer must be allocated from AllocatePool.
  370. Return Value:
  371. EFI_SUCCESS if the image was unloaded.
  372. EFI_INVALID_PARAMETER if the image has been loaded and started with
  373. LoadImage and StartImage, but the image is not currently executing.
  374. --*/
  375. {
  376. PEFI_IMAGE_DATA Image;
  377. EFI_TPL OldTpl;
  378. //
  379. // Prevent possible reentrance to this function for the same image handle.
  380. //
  381. OldTpl = EfiCoreRaiseTpl(TPL_NOTIFY);
  382. Image = EfipCoreGetImageDataFromHandle(ImageHandle);
  383. if (Image == NULL) {
  384. ExitStatus = EFI_INVALID_PARAMETER;
  385. goto CoreExitEnd;
  386. }
  387. //
  388. // If the image has not yet been started, just free its resources.
  389. //
  390. if (Image->Started == FALSE) {
  391. EfipCoreUnloadAndCloseImage(Image, TRUE);
  392. ExitStatus = EFI_SUCCESS;
  393. goto CoreExitEnd;
  394. }
  395. //
  396. // If the image has been started, verify it can exit.
  397. //
  398. if (Image != EfiCurrentImage) {
  399. RtlDebugPrint("Error: Image cannot exit while in the middle of "
  400. "starting another image.\n");
  401. ExitStatus = EFI_INVALID_PARAMETER;
  402. goto CoreExitEnd;
  403. }
  404. Image->Status = ExitStatus;
  405. if (ExitData != NULL) {
  406. Image->ExitDataSize = ExitDataSize;
  407. Image->ExitData = EfiCoreAllocateBootPool(Image->ExitDataSize);
  408. if (Image->ExitData == NULL) {
  409. ExitStatus = EFI_OUT_OF_RESOURCES;
  410. goto CoreExitEnd;
  411. }
  412. EfiCoreCopyMemory(Image->ExitData, ExitData, Image->ExitDataSize);
  413. }
  414. EfiCoreRestoreTpl(OldTpl);
  415. //
  416. // Return to the set jump in start image.
  417. //
  418. EfipArchLongJump(Image->JumpContext, -1);
  419. //
  420. // There should be no way to return from a long jump.
  421. //
  422. ASSERT(FALSE);
  423. ExitStatus = EFI_ACCESS_DENIED;
  424. CoreExitEnd:
  425. //
  426. // Something bizarre happened, return from the exit.
  427. //
  428. EfiCoreRestoreTpl(OldTpl);
  429. return ExitStatus;
  430. }
  431. EFI_STATUS
  432. EfiCoreInitializeImageServices (
  433. VOID *FirmwareBaseAddress,
  434. VOID *FirmwareLowestAddress,
  435. UINTN FirmwareSize
  436. )
  437. /*++
  438. Routine Description:
  439. This routine initializes image service support for the UEFI core.
  440. Arguments:
  441. FirmwareBaseAddress - Supplies the base address where the firmware was
  442. loaded into memory. Supply -1 to indicate that the image is loaded at
  443. its preferred base address and was not relocated.
  444. FirmwareLowestAddress - Supplies the lowest address where the firmware was
  445. loaded into memory.
  446. FirmwareSize - Supplies the size of the firmware image in memory, in bytes.
  447. Return Value:
  448. EFI Status code.
  449. --*/
  450. {
  451. PEFI_IMAGE_DATA Image;
  452. EFI_STATUS Status;
  453. //
  454. // Initialize the firmware image data.
  455. //
  456. Image = &EfiFirmwareLoadedImage;
  457. Image->ImageBasePage = (EFI_PHYSICAL_ADDRESS)(UINTN)FirmwareLowestAddress;
  458. Image->ImagePageCount = EFI_SIZE_TO_PAGES(FirmwareSize);
  459. Image->Tpl = EfiCurrentTpl;
  460. Image->Information.SystemTable = EfiSystemTable;
  461. Image->Information.ImageBase = FirmwareLowestAddress;
  462. Image->Information.ImageSize = FirmwareSize;
  463. //
  464. // Install the loaded image protocol on a new handle representing the
  465. // firmware image.
  466. //
  467. Image->Handle = NULL;
  468. Status = EfiCoreInstallProtocolInterface(&(Image->Handle),
  469. &EfiLoadedImageProtocolGuid,
  470. EFI_NATIVE_INTERFACE,
  471. &(Image->Information));
  472. if (EFI_ERROR(Status)) {
  473. ASSERT(FALSE);
  474. return Status;
  475. }
  476. EfiFirmwareImageHandle = Image->Handle;
  477. return Status;
  478. }
  479. //
  480. // --------------------------------------------------------- Internal Functions
  481. //
  482. EFI_STATUS
  483. EfipCoreLoadImage (
  484. BOOLEAN BootPolicy,
  485. EFI_HANDLE ParentImageHandle,
  486. EFI_DEVICE_PATH_PROTOCOL *FilePath,
  487. VOID *SourceBuffer,
  488. UINTN SourceSize,
  489. EFI_PHYSICAL_ADDRESS DestinationBuffer,
  490. UINTN *PageCount,
  491. EFI_HANDLE *ImageHandle,
  492. EFI_PHYSICAL_ADDRESS *EntryPoint,
  493. UINT32 Attributes
  494. )
  495. /*++
  496. Routine Description:
  497. This routine loads an EFI image into memory.
  498. Arguments:
  499. BootPolicy - Supplies a boolean indicating that the request originates
  500. from the boot manager, and that the boot manager is attempting to load
  501. the given file path as a boot selection. This is ignored if the source
  502. buffer is NULL.
  503. ParentImageHandle - Supplies the caller's image handle.
  504. FilePath - Supplies a pointer to the device path from which the image is
  505. loaded.
  506. SourceBuffer - Supplies an optional pointer to the memory location
  507. containing a copy of the image to be loaded.
  508. SourceSize - Supplies the size in bytes of the source buffer.
  509. DestinationBuffer - Supplies an optional address to load the image at.
  510. PageCount - Supplies a pointer that on input contains the size of the
  511. destination buffer in pages. On output, will contain the number of
  512. pages in the loaded image.
  513. ImageHandle - Supplies a pointer where the loaded image handle will be
  514. returned on success.
  515. EntryPoint - Supplies a pointer where the image entry point will be
  516. returned.
  517. Attributes - Supplies a bitfield of flags governing the behavior of the
  518. load. See EFI_LOAD_PE_IMAGE_ATTRIBUTE_* definitions.
  519. Return Value:
  520. EFI Status code.
  521. --*/
  522. {
  523. UINTN AllocationSize;
  524. CHAR8 *AsciiFileName;
  525. UINTN AsciiFileNameSize;
  526. UINT32 AuthenticationStatus;
  527. PDEBUG_MODULE DebuggerModule;
  528. EFI_HANDLE DeviceHandle;
  529. EFI_IMAGE_FILE_HANDLE FileHandle;
  530. CHAR16 *FileName;
  531. UINTN FilePathSize;
  532. BOOLEAN FreePage;
  533. EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
  534. PEFI_IMAGE_DATA Image;
  535. EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
  536. PEFI_IMAGE_DATA ParentImage;
  537. EFI_STATUS Status;
  538. ASSERT(EfiCurrentTpl < TPL_NOTIFY);
  539. ParentImage = NULL;
  540. if ((ImageHandle == NULL) || (ParentImageHandle == NULL)) {
  541. return EFI_INVALID_PARAMETER;
  542. }
  543. ParentImage = EfipCoreGetImageDataFromHandle(ParentImageHandle);
  544. if (ParentImage == NULL) {
  545. RtlDebugPrint("LoadImage: Invalid Parent image handle.\n");
  546. return EFI_INVALID_PARAMETER;
  547. }
  548. EfiCoreSetMemory(&FileHandle, 0, sizeof(EFI_IMAGE_FILE_HANDLE));
  549. FileHandle.Magic = EFI_IMAGE_FILE_HANDLE_MAGIC;
  550. FileName = NULL;
  551. OriginalFilePath = FilePath;
  552. HandleFilePath = FilePath;
  553. DeviceHandle = NULL;
  554. Status = EFI_SUCCESS;
  555. //
  556. // If the caller passed a copy of the file, then just use it.
  557. //
  558. if (SourceBuffer != NULL) {
  559. FileHandle.Source = SourceBuffer;
  560. FileHandle.SourceSize = SourceSize;
  561. Status = EfiCoreLocateDevicePath(&EfiDevicePathProtocolGuid,
  562. &HandleFilePath,
  563. &DeviceHandle);
  564. if (EFI_ERROR(Status)) {
  565. DeviceHandle = NULL;
  566. }
  567. if (SourceSize > 0) {
  568. Status = EFI_SUCCESS;
  569. } else {
  570. Status = EFI_LOAD_ERROR;
  571. }
  572. //
  573. // An image source was not supplied, go find it.
  574. //
  575. } else {
  576. if (FilePath == NULL) {
  577. return EFI_INVALID_PARAMETER;
  578. }
  579. FileHandle.Source = EfipCoreGetFileBufferByFilePath(
  580. BootPolicy,
  581. FilePath,
  582. &FileName,
  583. &(FileHandle.SourceSize),
  584. &AuthenticationStatus);
  585. if (FileHandle.Source == NULL) {
  586. Status = EFI_NOT_FOUND;
  587. } else {
  588. FileHandle.FreeBuffer = TRUE;
  589. Status = EfiCoreLocateDevicePath(&EfiFirmwareVolume2ProtocolGuid,
  590. &HandleFilePath,
  591. &DeviceHandle);
  592. if (EFI_ERROR(Status)) {
  593. HandleFilePath = FilePath;
  594. Status = EfiCoreLocateDevicePath(
  595. &EfiSimpleFileSystemProtocolGuid,
  596. &HandleFilePath,
  597. &DeviceHandle);
  598. if (EFI_ERROR(Status)) {
  599. if (BootPolicy == FALSE) {
  600. Status = EfiCoreLocateDevicePath(
  601. &EfiLoadFile2ProtocolGuid,
  602. &HandleFilePath,
  603. &DeviceHandle);
  604. }
  605. if (EFI_ERROR(Status)) {
  606. HandleFilePath = FilePath;
  607. Status = EfiCoreLocateDevicePath(
  608. &EfiLoadFileProtocolGuid,
  609. &HandleFilePath,
  610. &DeviceHandle);
  611. }
  612. }
  613. }
  614. }
  615. }
  616. if (EFI_ERROR(Status)) {
  617. Image = NULL;
  618. goto CoreLoadImageEnd;
  619. }
  620. //
  621. // Allocate a new image data structure.
  622. //
  623. Image = EfiCoreAllocateBootPool(sizeof(EFI_IMAGE_DATA));
  624. if (Image == NULL) {
  625. Status = EFI_OUT_OF_RESOURCES;
  626. goto CoreLoadImageEnd;
  627. }
  628. EfiCoreSetMemory(Image, sizeof(EFI_IMAGE_DATA), 0);
  629. //
  630. // Pull out just the file portion of the device path for the loaded image
  631. // file path.
  632. //
  633. FilePath = OriginalFilePath;
  634. if (DeviceHandle != NULL) {
  635. Status = EfiCoreHandleProtocol(DeviceHandle,
  636. &EfiDevicePathProtocolGuid,
  637. (VOID **)&HandleFilePath);
  638. if (!EFI_ERROR(Status)) {
  639. FilePathSize = EfiCoreGetDevicePathSize(HandleFilePath) -
  640. sizeof(EFI_DEVICE_PATH_PROTOCOL);
  641. FilePath = (EFI_DEVICE_PATH_PROTOCOL *)(((UINT8 *)FilePath) +
  642. FilePathSize);
  643. }
  644. }
  645. Image->Magic = EFI_IMAGE_DATA_MAGIC;
  646. Image->Information.SystemTable = EfiSystemTable;
  647. Image->Information.DeviceHandle = DeviceHandle;
  648. Image->Information.Revision = EFI_LOADED_IMAGE_PROTOCOL_REVISION;
  649. Image->Information.FilePath = EfiCoreDuplicateDevicePath(FilePath);
  650. Image->Information.ParentHandle = ParentImageHandle;
  651. if (PageCount != NULL) {
  652. Image->ImagePageCount = *PageCount;
  653. } else {
  654. Image->ImagePageCount = 0;
  655. }
  656. //
  657. // Install the protocol interfaces for this image, but don't fire the
  658. // notifications just yet.
  659. //
  660. Status = EfipCoreInstallProtocolInterfaceNotify(&(Image->Handle),
  661. &EfiLoadedImageProtocolGuid,
  662. EFI_NATIVE_INTERFACE,
  663. &(Image->Information),
  664. FALSE);
  665. if (EFI_ERROR(Status)) {
  666. goto CoreLoadImageEnd;
  667. }
  668. //
  669. // Load up the image.
  670. //
  671. Status = EfipCoreLoadPeImage(BootPolicy,
  672. &FileHandle,
  673. Image,
  674. DestinationBuffer,
  675. EntryPoint,
  676. Attributes);
  677. if (EFI_ERROR(Status)) {
  678. if ((Status == EFI_BUFFER_TOO_SMALL) ||
  679. (Status == EFI_OUT_OF_RESOURCES)) {
  680. if (PageCount != NULL) {
  681. *PageCount = Image->ImagePageCount;
  682. }
  683. }
  684. goto CoreLoadImageEnd;
  685. }
  686. if (PageCount != NULL) {
  687. *PageCount = Image->ImagePageCount;
  688. }
  689. //
  690. // Register the image with the debugger unless asked not to.
  691. //
  692. if (((Attributes &
  693. EFI_LOAD_PE_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION) !=
  694. 0) &&
  695. (FileName != NULL)) {
  696. AsciiFileName = EfipCoreConvertFileNameToAscii(FileName,
  697. &AsciiFileNameSize);
  698. if (AsciiFileName != NULL) {
  699. AllocationSize = sizeof(DEBUG_MODULE) + AsciiFileNameSize;
  700. DebuggerModule = EfiCoreAllocateBootPool(AllocationSize);
  701. if (DebuggerModule != NULL) {
  702. EfiSetMem(DebuggerModule, AllocationSize, 0);
  703. DebuggerModule->StructureSize = AllocationSize;
  704. DebuggerModule->LowestAddress =
  705. (PVOID)(UINTN)Image->ImageBasePage;
  706. DebuggerModule->Size = Image->ImagePageCount << EFI_PAGE_SHIFT;
  707. DebuggerModule->EntryPoint = Image->EntryPoint;
  708. RtlStringCopy(DebuggerModule->BinaryName,
  709. AsciiFileName,
  710. AsciiFileNameSize);
  711. Image->DebuggerData = DebuggerModule;
  712. KdReportModuleChange(DebuggerModule, TRUE);
  713. }
  714. EfiCoreFreePool(AsciiFileName);
  715. }
  716. }
  717. //
  718. // Reinstall the loaded image protocol to fire any notifications.
  719. //
  720. Status = EfiCoreReinstallProtocolInterface(Image->Handle,
  721. &EfiLoadedImageProtocolGuid,
  722. &(Image->Information),
  723. &(Image->Information));
  724. if (EFI_ERROR(Status)) {
  725. goto CoreLoadImageEnd;
  726. }
  727. //
  728. // If the device path parameter is not NULL, make a copy of the device
  729. // path. Otherwise the loaded image device path protocol is installed with
  730. // a NULL interface pointer.
  731. //
  732. if (OriginalFilePath != NULL) {
  733. Image->LoadedImageDevicePath =
  734. EfiCoreDuplicateDevicePath(OriginalFilePath);
  735. }
  736. //
  737. // Install the loaded image device path protocol.
  738. //
  739. Status = EfiCoreInstallProtocolInterface(
  740. &(Image->Handle),
  741. &EfiLoadedImageDevicePathProtocolGuid,
  742. EFI_NATIVE_INTERFACE,
  743. Image->LoadedImageDevicePath);
  744. if (EFI_ERROR(Status)) {
  745. goto CoreLoadImageEnd;
  746. }
  747. //
  748. // Install the HII package list protocol onto the image handle.
  749. //
  750. if (Image->ImageContext.HiiResourceData != 0) {
  751. Status = EfiCoreInstallProtocolInterface(
  752. &(Image->Handle),
  753. &EfiHiiPackageListProtocolGuid,
  754. EFI_NATIVE_INTERFACE,
  755. (VOID *)(UINTN)Image->ImageContext.HiiResourceData);
  756. if (EFI_ERROR(Status)) {
  757. goto CoreLoadImageEnd;
  758. }
  759. }
  760. *ImageHandle = Image->Handle;
  761. CoreLoadImageEnd:
  762. if (FileHandle.FreeBuffer != FALSE) {
  763. EfiCoreFreePool(FileHandle.Source);
  764. }
  765. if (FileName != NULL) {
  766. EfiCoreFreePool(FileName);
  767. }
  768. if (EFI_ERROR(Status)) {
  769. if (Image != NULL) {
  770. FreePage = FALSE;
  771. if (DestinationBuffer == 0) {
  772. FreePage = TRUE;
  773. }
  774. EfipCoreUnloadAndCloseImage(Image, FreePage);
  775. Image = NULL;
  776. }
  777. }
  778. //
  779. // Track the return status from this call.
  780. //
  781. if (Image != NULL) {
  782. Image->LoadImageStatus = Status;
  783. }
  784. return Status;
  785. }
  786. EFIAPI
  787. VOID *
  788. EfipCoreGetFileBufferByFilePath (
  789. BOOLEAN BootPolicy,
  790. CONST EFI_DEVICE_PATH_PROTOCOL *FilePath,
  791. CHAR16 **FileName,
  792. UINTN *FileSize,
  793. UINT32 *AuthenticationStatus
  794. )
  795. /*++
  796. Routine Description:
  797. This routine loads a file either from a firmware image, file system
  798. interface, or from the load file interface.
  799. Arguments:
  800. BootPolicy - Supplies the boot policy. if TRUE, indicates that the request
  801. originates from a boot manager trying to make a boot selection. If
  802. FALSE, the file path must match exactly with the file to be loaded.
  803. FilePath - Supplies a pointer to the device path of the file to load.
  804. FileName - Supplies a pointer where a pointer to the file name will be
  805. returned. The caller is responsible for freeing this buffer.
  806. FileSize - Supplies a pointer where the size of the loaded file buffer will
  807. be returned on success.
  808. AuthenticationStatus - Supplies a pointer to the authentication status.
  809. Return Value:
  810. Returns a pointer to the image contents. The caller is responsible for
  811. freeing this memory from pool.
  812. --*/
  813. {
  814. EFI_FV_FILE_ATTRIBUTES Attributes;
  815. EFI_DEVICE_PATH_PROTOCOL *DevicePathNode;
  816. EFI_DEVICE_PATH_PROTOCOL *DevicePathNodeCopy;
  817. EFI_FILE_HANDLE FileHandle;
  818. EFI_FILE_INFO *FileInformation;
  819. UINTN FileInformationSize;
  820. UINTN FileNameSize;
  821. EFI_FIRMWARE_VOLUME2_PROTOCOL *FirmwareVolume;
  822. EFI_HANDLE Handle;
  823. UINT8 *ImageBuffer;
  824. UINTN ImageBufferSize;
  825. EFI_FILE_HANDLE LastHandle;
  826. EFI_LOAD_FILE_PROTOCOL *LoadFile;
  827. EFI_LOAD_FILE2_PROTOCOL *LoadFile2;
  828. EFI_GUID *NameGuid;
  829. EFI_DEVICE_PATH_PROTOCOL *OriginalDevicePathNode;
  830. EFI_SECTION_TYPE SectionType;
  831. EFI_STATUS Status;
  832. EFI_FV_FILETYPE Type;
  833. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
  834. if ((FilePath == NULL) || (FileSize == NULL) ||
  835. (AuthenticationStatus == NULL)) {
  836. return NULL;
  837. }
  838. DevicePathNodeCopy = NULL;
  839. NameGuid = NULL;
  840. FileInformation = NULL;
  841. FileHandle = NULL;
  842. ImageBuffer = NULL;
  843. ImageBufferSize = 0;
  844. *AuthenticationStatus = 0;
  845. *FileName = NULL;
  846. OriginalDevicePathNode = EfiCoreDuplicateDevicePath(FilePath);
  847. if (OriginalDevicePathNode == NULL) {
  848. return NULL;
  849. }
  850. //
  851. // See if the device path supports the Firmware Volume 2 protocol.
  852. //
  853. DevicePathNode = OriginalDevicePathNode;
  854. Status = EfiLocateDevicePath(&EfiFirmwareVolume2ProtocolGuid,
  855. &DevicePathNode,
  856. &Handle);
  857. if (!EFI_ERROR(Status)) {
  858. NameGuid = EfiCoreGetNameGuidFromFirmwareVolumeDevicePathNode(
  859. (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)DevicePathNode);
  860. if (NameGuid == NULL) {
  861. Status = EFI_INVALID_PARAMETER;
  862. } else {
  863. //
  864. // Read the image from the firmware file.
  865. //
  866. Status = EfiHandleProtocol(Handle,
  867. &EfiFirmwareVolume2ProtocolGuid,
  868. (VOID **)&FirmwareVolume);
  869. if (!EFI_ERROR(Status)) {
  870. SectionType = EFI_SECTION_PE32;
  871. ImageBuffer = NULL;
  872. Status = FirmwareVolume->ReadSection(FirmwareVolume,
  873. NameGuid,
  874. SectionType,
  875. 0,
  876. (VOID **)&ImageBuffer,
  877. &ImageBufferSize,
  878. AuthenticationStatus);
  879. //
  880. // If that succeeded, try to read the UI description out as
  881. // well.
  882. //
  883. if (!EFI_ERROR(Status)) {
  884. FirmwareVolume->ReadSection(FirmwareVolume,
  885. NameGuid,
  886. EFI_SECTION_USER_INTERFACE,
  887. 0,
  888. (VOID **)FileName,
  889. &FileNameSize,
  890. AuthenticationStatus);
  891. //
  892. // Null terminate the string just to be safe.
  893. //
  894. if (FileNameSize / sizeof(CHAR16) != 0) {
  895. (*FileName)[(FileNameSize / sizeof(CHAR16)) - 1] =
  896. L'\0';
  897. }
  898. //
  899. // If reading the PE32 section failed, try a raw file type.
  900. //
  901. } else {
  902. if (ImageBuffer != NULL) {
  903. EfiCoreFreePool(ImageBuffer);
  904. *AuthenticationStatus = 0;
  905. ImageBuffer = NULL;
  906. }
  907. Status = FirmwareVolume->ReadFile(FirmwareVolume,
  908. NameGuid,
  909. (VOID **)&ImageBuffer,
  910. &ImageBufferSize,
  911. &Type,
  912. &Attributes,
  913. AuthenticationStatus);
  914. }
  915. }
  916. }
  917. if (!EFI_ERROR(Status)) {
  918. goto CoreGetFileBufferByFilePathEnd;
  919. }
  920. }
  921. //
  922. // Try to access the file via a file system interface.
  923. //
  924. DevicePathNode = OriginalDevicePathNode;
  925. Status = EfiLocateDevicePath(&EfiSimpleFileSystemProtocolGuid,
  926. &DevicePathNode,
  927. &Handle);
  928. if (!EFI_ERROR(Status)) {
  929. Status = EfiHandleProtocol(Handle,
  930. &EfiSimpleFileSystemProtocolGuid,
  931. (VOID **)&Volume);
  932. if (!EFI_ERROR(Status)) {
  933. //
  934. // Open the volume to get the file system handle.
  935. //
  936. Status = Volume->OpenVolume(Volume, &FileHandle);
  937. if (!EFI_ERROR(Status)) {
  938. //
  939. // Duplicate the device path to avoid access to an unaligned
  940. // device path node.
  941. //
  942. DevicePathNodeCopy = EfiCoreDuplicateDevicePath(DevicePathNode);
  943. if (DevicePathNodeCopy == NULL) {
  944. FileHandle->Close(FileHandle);
  945. Status = EFI_OUT_OF_RESOURCES;
  946. }
  947. DevicePathNode = DevicePathNodeCopy;
  948. while ((!EFI_ERROR(Status)) &&
  949. (EfiCoreIsDevicePathEnd(DevicePathNode) == FALSE)) {
  950. if ((EfiCoreGetDevicePathType(DevicePathNode) !=
  951. MEDIA_DEVICE_PATH) ||
  952. (EfiCoreGetDevicePathSubType(DevicePathNode) !=
  953. MEDIA_FILEPATH_DP)) {
  954. Status = EFI_UNSUPPORTED;
  955. break;
  956. }
  957. LastHandle = FileHandle;
  958. FileHandle = NULL;
  959. Status = LastHandle->Open(
  960. LastHandle,
  961. &FileHandle,
  962. ((FILEPATH_DEVICE_PATH *)DevicePathNode)->PathName,
  963. EFI_FILE_MODE_READ,
  964. 0);
  965. LastHandle->Close(LastHandle);
  966. DevicePathNode =
  967. EfiCoreGetNextDevicePathNode(DevicePathNode);
  968. }
  969. //
  970. // If no error occurred, then the file was found. Load the
  971. // file.
  972. //
  973. if (!EFI_ERROR(Status)) {
  974. FileInformation = NULL;
  975. FileInformationSize = 0;
  976. Status = FileHandle->GetInfo(FileHandle,
  977. &EfiFileInformationGuid,
  978. &FileInformationSize,
  979. FileInformation);
  980. if (Status == EFI_BUFFER_TOO_SMALL) {
  981. FileInformation =
  982. EfiCoreAllocateBootPool(FileInformationSize);
  983. if (FileInformation == NULL) {
  984. Status = EFI_OUT_OF_RESOURCES;
  985. } else {
  986. Status = FileHandle->GetInfo(
  987. FileHandle,
  988. &EfiFileInformationGuid,
  989. &FileInformationSize,
  990. FileInformation);
  991. }
  992. }
  993. if ((!EFI_ERROR(Status)) && (FileInformation != NULL)) {
  994. //
  995. // Fail if it's a directory.
  996. //
  997. if ((FileInformation->Attribute & EFI_FILE_DIRECTORY) !=
  998. 0) {
  999. Status = EFI_LOAD_ERROR;
  1000. //
  1001. // Allocate space for the file and read it in.
  1002. //
  1003. } else {
  1004. ImageBuffer = EfiCoreAllocateBootPool(
  1005. (UINTN)(FileInformation->FileSize));
  1006. if (ImageBuffer == NULL) {
  1007. Status = EFI_OUT_OF_RESOURCES;
  1008. } else {
  1009. ImageBufferSize = FileInformation->FileSize;
  1010. Status = FileHandle->Read(FileHandle,
  1011. &ImageBufferSize,
  1012. ImageBuffer);
  1013. if (!EFI_ERROR(Status)) {
  1014. //
  1015. // Also read in the file name.
  1016. //
  1017. FileNameSize = EfiCoreStringLength(
  1018. FileInformation->FileName);
  1019. FileNameSize = (FileNameSize + 1) *
  1020. sizeof(CHAR16);
  1021. *FileName = EfiCoreAllocateBootPool(
  1022. FileNameSize);
  1023. if (*FileName != NULL) {
  1024. EfiCopyMem(*FileName,
  1025. FileInformation->FileName,
  1026. FileNameSize);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. if (FileInformation != NULL) {
  1034. EfiCoreFreePool(FileInformation);
  1035. }
  1036. if (FileHandle != NULL) {
  1037. FileHandle->Close(FileHandle);
  1038. }
  1039. if (DevicePathNodeCopy != NULL) {
  1040. EfiCoreFreePool(DevicePathNodeCopy);
  1041. }
  1042. }
  1043. }
  1044. if (!EFI_ERROR(Status)) {
  1045. goto CoreGetFileBufferByFilePathEnd;
  1046. }
  1047. }
  1048. //
  1049. // Attempt to access the file using the Load File 2 protocol.
  1050. //
  1051. if (BootPolicy == FALSE) {
  1052. DevicePathNode = OriginalDevicePathNode;
  1053. Status = EfiLocateDevicePath(&EfiLoadFile2ProtocolGuid,
  1054. &DevicePathNode,
  1055. &Handle);
  1056. if (!EFI_ERROR(Status)) {
  1057. Status = EfiHandleProtocol(Handle,
  1058. &EfiLoadFile2ProtocolGuid,
  1059. (VOID **)&LoadFile2);
  1060. if (!EFI_ERROR(Status)) {
  1061. //
  1062. // Call once to figure out the buffer size.
  1063. //
  1064. ImageBufferSize = 0;
  1065. ImageBuffer = NULL;
  1066. Status = LoadFile2->LoadFile(LoadFile2,
  1067. DevicePathNode,
  1068. FALSE,
  1069. &ImageBufferSize,
  1070. ImageBuffer);
  1071. if (Status == EFI_BUFFER_TOO_SMALL) {
  1072. ImageBuffer = EfiCoreAllocateBootPool(ImageBufferSize);
  1073. if (ImageBuffer == NULL) {
  1074. Status = EFI_OUT_OF_RESOURCES;
  1075. } else {
  1076. Status = LoadFile2->LoadFile(LoadFile2,
  1077. DevicePathNode,
  1078. FALSE,
  1079. &ImageBufferSize,
  1080. ImageBuffer);
  1081. }
  1082. }
  1083. }
  1084. }
  1085. if (!EFI_ERROR(Status)) {
  1086. goto CoreGetFileBufferByFilePathEnd;
  1087. }
  1088. }
  1089. //
  1090. // Attempt to access the file using the Load File protocol.
  1091. //
  1092. if (BootPolicy == FALSE) {
  1093. DevicePathNode = OriginalDevicePathNode;
  1094. Status = EfiLocateDevicePath(&EfiLoadFileProtocolGuid,
  1095. &DevicePathNode,
  1096. &Handle);
  1097. if (!EFI_ERROR(Status)) {
  1098. Status = EfiHandleProtocol(Handle,
  1099. &EfiLoadFileProtocolGuid,
  1100. (VOID **)&LoadFile);
  1101. if (!EFI_ERROR(Status)) {
  1102. //
  1103. // Call once to figure out the buffer size.
  1104. //
  1105. ImageBufferSize = 0;
  1106. ImageBuffer = NULL;
  1107. Status = LoadFile->LoadFile(LoadFile,
  1108. DevicePathNode,
  1109. FALSE,
  1110. &ImageBufferSize,
  1111. ImageBuffer);
  1112. if (Status == EFI_BUFFER_TOO_SMALL) {
  1113. ImageBuffer = EfiCoreAllocateBootPool(ImageBufferSize);
  1114. if (ImageBuffer == NULL) {
  1115. Status = EFI_OUT_OF_RESOURCES;
  1116. } else {
  1117. Status = LoadFile->LoadFile(LoadFile,
  1118. DevicePathNode,
  1119. FALSE,
  1120. &ImageBufferSize,
  1121. ImageBuffer);
  1122. }
  1123. }
  1124. }
  1125. }
  1126. if (!EFI_ERROR(Status)) {
  1127. goto CoreGetFileBufferByFilePathEnd;
  1128. }
  1129. }
  1130. CoreGetFileBufferByFilePathEnd:
  1131. if (EFI_ERROR(Status)) {
  1132. if (ImageBuffer != NULL) {
  1133. EfiCoreFreePool(ImageBuffer);
  1134. ImageBuffer = NULL;
  1135. }
  1136. ImageBufferSize = 0;
  1137. }
  1138. *FileSize = ImageBufferSize;
  1139. EfiCoreFreePool(OriginalDevicePathNode);
  1140. return ImageBuffer;
  1141. }
  1142. EFI_STATUS
  1143. EfipCoreLoadPeImage (
  1144. BOOLEAN BootPolicy,
  1145. VOID *PeHandle,
  1146. PEFI_IMAGE_DATA Image,
  1147. EFI_PHYSICAL_ADDRESS DestinationBuffer,
  1148. EFI_PHYSICAL_ADDRESS *EntryPoint,
  1149. UINT32 Attribute
  1150. )
  1151. /*++
  1152. Routine Description:
  1153. This routine loads, relocates, and invokes a PE/COFF image.
  1154. Arguments:
  1155. BootPolicy - Supplies the boot policy. if TRUE, indicates that the request
  1156. originates from a boot manager trying to make a boot selection. If
  1157. FALSE, the file path must match exactly with the file to be loaded.
  1158. PeHandle - Supplies the handle of the PE image.
  1159. Image - Supplies the PE image to be loaded.
  1160. DestinationBuffer - Supplies the buffer to store the image at.
  1161. EntryPoint - Supplies a pointer where the entry point of the image will
  1162. be returned.
  1163. Attribute - Supplies the bitmask of attributes governing the load process.
  1164. Return Value:
  1165. EFI_SUCCESS if the image was loaded, relocated, and invoked.
  1166. EFI_OUT_OF_RESOURCES on allocation failure.
  1167. EFI_INVALID_PARAMETER on invalid parameter.
  1168. EFI_BUFFER_TOO_SMALL if the buffer for the image is too small.
  1169. --*/
  1170. {
  1171. BOOLEAN DestinationAllocated;
  1172. UINTN NeededPages;
  1173. UINTN Size;
  1174. EFI_STATUS Status;
  1175. EfiCoreSetMemory(&(Image->ImageContext), sizeof(Image->ImageContext), 0);
  1176. Image->ImageContext.Handle = PeHandle;
  1177. Image->ImageContext.ImageRead = EfipCoreReadImageFile;
  1178. //
  1179. // Get information about the image being loaded.
  1180. //
  1181. Status = EfiPeLoaderGetImageInfo(&(Image->ImageContext));
  1182. if (EFI_ERROR(Status)) {
  1183. return Status;
  1184. }
  1185. if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Image->ImageContext.Machine)) {
  1186. RtlDebugPrint("Image Type 0x%x can't be loaded.\n",
  1187. Image->ImageContext.Machine);
  1188. return EFI_UNSUPPORTED;
  1189. }
  1190. //
  1191. // Set the memory type based on the image type.
  1192. //
  1193. switch (Image->ImageContext.ImageType) {
  1194. case EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION:
  1195. Image->ImageContext.ImageCodeMemoryType = EfiLoaderCode;
  1196. Image->ImageContext.ImageDataMemoryType = EfiLoaderData;
  1197. break;
  1198. case EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  1199. Image->ImageContext.ImageCodeMemoryType = EfiBootServicesCode;
  1200. Image->ImageContext.ImageDataMemoryType = EfiBootServicesData;
  1201. break;
  1202. case EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  1203. case EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
  1204. Image->ImageContext.ImageCodeMemoryType = EfiRuntimeServicesCode;
  1205. Image->ImageContext.ImageDataMemoryType = EfiRuntimeServicesData;
  1206. break;
  1207. default:
  1208. Image->ImageContext.ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
  1209. return EFI_UNSUPPORTED;
  1210. }
  1211. //
  1212. // Allocate memory of the correct type aligned on the required image
  1213. // boundary.
  1214. //
  1215. DestinationAllocated = FALSE;
  1216. if (DestinationBuffer == 0) {
  1217. Size = (UINTN)Image->ImageContext.ImageSize;
  1218. if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {
  1219. Size += Image->ImageContext.SectionAlignment;
  1220. }
  1221. Image->ImagePageCount = EFI_SIZE_TO_PAGES(Size);
  1222. //
  1223. // If image relocations have not been stripped, then load at any
  1224. // address. Otherwise, load at the address the image was linked at.
  1225. //
  1226. Status = EFI_OUT_OF_RESOURCES;
  1227. if (Image->ImageContext.RelocationsStripped != FALSE) {
  1228. Status = EfiCoreAllocatePages(
  1229. AllocateAddress,
  1230. (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType),
  1231. Image->ImagePageCount,
  1232. &(Image->ImageContext.ImageAddress));
  1233. }
  1234. if ((EFI_ERROR(Status)) &&
  1235. (Image->ImageContext.RelocationsStripped == FALSE)) {
  1236. Status = EfiCoreAllocatePages(
  1237. AllocateAnyPages,
  1238. (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType),
  1239. Image->ImagePageCount,
  1240. &(Image->ImageContext.ImageAddress));
  1241. }
  1242. if (EFI_ERROR(Status)) {
  1243. return Status;
  1244. }
  1245. DestinationAllocated = TRUE;
  1246. //
  1247. // The caller provided a destination buffer.
  1248. //
  1249. } else {
  1250. if ((Image->ImageContext.RelocationsStripped != FALSE) &&
  1251. (Image->ImageContext.ImageAddress != DestinationBuffer)) {
  1252. RtlDebugPrint("Image must be loaded at 0x%x.\n",
  1253. (UINTN)Image->ImageContext.ImageAddress);
  1254. return EFI_INVALID_PARAMETER;
  1255. }
  1256. Size = (UINTN)Image->ImageContext.ImageSize +
  1257. Image->ImageContext.SectionAlignment;
  1258. NeededPages = EFI_SIZE_TO_PAGES(Size);
  1259. if ((Image->ImagePageCount != 0) &&
  1260. (Image->ImagePageCount < NeededPages)) {
  1261. return EFI_BUFFER_TOO_SMALL;
  1262. }
  1263. Image->ImagePageCount = NeededPages;
  1264. Image->ImageContext.ImageAddress = DestinationBuffer;
  1265. }
  1266. Image->ImageBasePage = Image->ImageContext.ImageAddress;
  1267. if (Image->ImageContext.IsTeImage == FALSE) {
  1268. Image->ImageContext.ImageAddress =
  1269. ALIGN_VALUE(Image->ImageContext.ImageAddress,
  1270. Image->ImageContext.SectionAlignment);
  1271. }
  1272. //
  1273. // Load the image from the file.
  1274. //
  1275. Status = EfiPeLoaderLoadImage(&(Image->ImageContext));
  1276. if (EFI_ERROR(Status)) {
  1277. goto CoreLoadPeImageEnd;
  1278. }
  1279. //
  1280. // If this is a runtime driver, allocate memory for the fixup data used to
  1281. // relocate the image when SetVirtualAddressMap is called.
  1282. //
  1283. if (((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) &&
  1284. (Image->ImageContext.ImageType ==
  1285. EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)) {
  1286. Image->ImageContext.FixupData = EfiCoreAllocateRuntimePool(
  1287. (UINTN)Image->ImageContext.FixupDataSize);
  1288. if (Image->ImageContext.FixupData == NULL) {
  1289. Status = EFI_OUT_OF_RESOURCES;
  1290. goto CoreLoadPeImageEnd;
  1291. }
  1292. }
  1293. //
  1294. // Relocate the image in memory.
  1295. //
  1296. Status = EfiPeLoaderRelocateImage(&(Image->ImageContext));
  1297. if (EFI_ERROR(Status)) {
  1298. goto CoreLoadPeImageEnd;
  1299. }
  1300. EfiCoreInvalidateInstructionCacheRange(
  1301. (VOID *)(UINTN)(Image->ImageContext.ImageAddress),
  1302. (UINTN)(Image->ImageContext.ImageSize));
  1303. Image->Machine = Image->ImageContext.Machine;
  1304. //
  1305. // Get the image entry point.
  1306. //
  1307. Image->EntryPoint =
  1308. (EFI_IMAGE_ENTRY_POINT)(UINTN)(Image->ImageContext.EntryPoint);
  1309. //
  1310. // Fill in the image information for the Loaded Image Protocol.
  1311. //
  1312. Image->Type = Image->ImageContext.ImageType;
  1313. Image->Information.ImageBase =
  1314. (VOID *)(UINTN)(Image->ImageContext.ImageAddress);
  1315. Image->Information.ImageSize = Image->ImageContext.ImageSize;
  1316. Image->Information.ImageCodeType =
  1317. (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType);
  1318. Image->Information.ImageDataType =
  1319. (EFI_MEMORY_TYPE)(Image->ImageContext.ImageDataMemoryType);
  1320. //
  1321. // Create the runtime image entry as well if needed.
  1322. //
  1323. if (((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) &&
  1324. (Image->ImageContext.ImageType ==
  1325. EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)) {
  1326. Image->RuntimeData = EfiCoreAllocateRuntimePool(
  1327. sizeof(EFI_RUNTIME_IMAGE_ENTRY));
  1328. if (Image->RuntimeData == NULL) {
  1329. Status = EFI_OUT_OF_RESOURCES;
  1330. goto CoreLoadPeImageEnd;
  1331. }
  1332. Image->RuntimeData->ImageBase = Image->Information.ImageBase;
  1333. Image->RuntimeData->ImageSize = Image->Information.ImageSize;
  1334. Image->RuntimeData->RelocationData = Image->ImageContext.FixupData;
  1335. Image->RuntimeData->Handle = Image->Handle;
  1336. INSERT_BEFORE(&(Image->RuntimeData->ListEntry),
  1337. &(EfiRuntimeProtocol->ImageListHead));
  1338. }
  1339. if (EntryPoint != NULL) {
  1340. *EntryPoint = Image->ImageContext.EntryPoint;
  1341. }
  1342. Status = EFI_SUCCESS;
  1343. CoreLoadPeImageEnd:
  1344. if (EFI_ERROR(Status)) {
  1345. if (DestinationAllocated != FALSE) {
  1346. EfiCoreFreePages(Image->ImageContext.ImageAddress,
  1347. Image->ImagePageCount);
  1348. }
  1349. if (Image->ImageContext.FixupData != NULL) {
  1350. EfiCoreFreePool(Image->ImageContext.FixupData);
  1351. }
  1352. }
  1353. return Status;
  1354. }
  1355. VOID
  1356. EfipCoreUnloadAndCloseImage (
  1357. PEFI_IMAGE_DATA Image,
  1358. BOOLEAN FreePages
  1359. )
  1360. /*++
  1361. Routine Description:
  1362. This routine unloads an EFI image from memory.
  1363. Arguments:
  1364. Image - Supplies a pointer to the image data.
  1365. FreePages - Supplies a boolean indicating whether or not to free the
  1366. allocated pages.
  1367. Return Value:
  1368. None.
  1369. --*/
  1370. {
  1371. UINTN ArrayCount;
  1372. EFI_HANDLE *HandleBuffer;
  1373. UINTN HandleCount;
  1374. UINTN HandleIndex;
  1375. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInformation;
  1376. UINTN OpenInformationCount;
  1377. EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInformationEntry;
  1378. UINTN OpenInformationIndex;
  1379. EFI_GUID **ProtocolGuidArray;
  1380. UINTN ProtocolIndex;
  1381. EFI_STATUS Status;
  1382. ASSERT(Image->Magic == EFI_IMAGE_DATA_MAGIC);
  1383. EfiPeLoaderUnloadImage(&(Image->ImageContext));
  1384. //
  1385. // Free references to the image handle.
  1386. //
  1387. if (Image->Handle != NULL) {
  1388. Status = EfiCoreLocateHandleBuffer(AllHandles,
  1389. NULL,
  1390. NULL,
  1391. &HandleCount,
  1392. &HandleBuffer);
  1393. if (!EFI_ERROR(Status)) {
  1394. for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex += 1) {
  1395. Status = EfiCoreProtocolsPerHandle(HandleBuffer[HandleIndex],
  1396. &ProtocolGuidArray,
  1397. &ArrayCount);
  1398. if (!EFI_ERROR(Status)) {
  1399. for (ProtocolIndex = 0;
  1400. ProtocolIndex < ArrayCount;
  1401. ProtocolIndex += 1) {
  1402. Status = EfiCoreOpenProtocolInformation(
  1403. HandleBuffer[HandleIndex],
  1404. ProtocolGuidArray[ProtocolIndex],
  1405. &OpenInformation,
  1406. &OpenInformationCount);
  1407. if (!EFI_ERROR(Status)) {
  1408. for (OpenInformationIndex = 0;
  1409. OpenInformationIndex < OpenInformationCount;
  1410. OpenInformationIndex += 1) {
  1411. OpenInformationEntry =
  1412. &(OpenInformation[OpenInformationIndex]);
  1413. if (OpenInformationEntry->AgentHandle ==
  1414. Image->Handle) {
  1415. EfiCoreCloseProtocol(
  1416. HandleBuffer[HandleIndex],
  1417. ProtocolGuidArray[ProtocolIndex],
  1418. Image->Handle,
  1419. OpenInformationEntry->ControllerHandle);
  1420. }
  1421. }
  1422. if (OpenInformation != NULL) {
  1423. EfiCoreFreePool(OpenInformation);
  1424. }
  1425. }
  1426. }
  1427. if (ProtocolGuidArray != NULL) {
  1428. EfiCoreFreePool(ProtocolGuidArray);
  1429. }
  1430. }
  1431. }
  1432. if (HandleBuffer != NULL) {
  1433. EfiCoreFreePool(HandleBuffer);
  1434. }
  1435. }
  1436. //
  1437. // Let the debugger know the image is being unloaded.
  1438. //
  1439. if (Image->DebuggerData != NULL) {
  1440. KdReportModuleChange(Image->DebuggerData, FALSE);
  1441. EfiCoreFreePool(Image->DebuggerData);
  1442. }
  1443. EfiCoreUninstallProtocolInterface(Image->Handle,
  1444. &EfiLoadedImageDevicePathProtocolGuid,
  1445. Image->LoadedImageDevicePath);
  1446. EfiCoreUninstallProtocolInterface(Image->Handle,
  1447. &EfiLoadedImageProtocolGuid,
  1448. &(Image->Information));
  1449. if (Image->ImageContext.HiiResourceData != 0) {
  1450. EfiCoreUninstallProtocolInterface(
  1451. Image->Handle,
  1452. &EfiHiiPackageListProtocolGuid,
  1453. (VOID *)(UINTN)Image->ImageContext.HiiResourceData);
  1454. }
  1455. }
  1456. if (Image->RuntimeData != NULL) {
  1457. if (Image->RuntimeData->ListEntry.Next != NULL) {
  1458. LIST_REMOVE(&(Image->RuntimeData->ListEntry));
  1459. }
  1460. EfiCoreFreePool(Image->RuntimeData);
  1461. }
  1462. //
  1463. // Free the image from memory.
  1464. //
  1465. if ((Image->ImageBasePage != 0) && (FreePages != FALSE)) {
  1466. EfiCoreFreePages(Image->ImageBasePage, Image->ImagePageCount);
  1467. }
  1468. if (Image->Information.FilePath != NULL) {
  1469. EfiCoreFreePool(Image->Information.FilePath);
  1470. }
  1471. if (Image->LoadedImageDevicePath != NULL) {
  1472. EfiCoreFreePool(Image->LoadedImageDevicePath);
  1473. }
  1474. if (Image->FixupData != NULL) {
  1475. EfiCoreFreePool(Image->FixupData);
  1476. }
  1477. Image->Magic = 0;
  1478. EfiCoreFreePool(Image);
  1479. return;
  1480. }
  1481. PEFI_IMAGE_DATA
  1482. EfipCoreGetImageDataFromHandle (
  1483. EFI_HANDLE ImageHandle
  1484. )
  1485. /*++
  1486. Routine Description:
  1487. This routine returns the private image data from a handle.
  1488. Arguments:
  1489. ImageHandle - Supplies the image handle.
  1490. Return Value:
  1491. Returns a pointer to the private image data.
  1492. --*/
  1493. {
  1494. PEFI_IMAGE_DATA Image;
  1495. EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  1496. EFI_STATUS Status;
  1497. Status = EfiCoreHandleProtocol(ImageHandle,
  1498. &EfiLoadedImageProtocolGuid,
  1499. (VOID **)&LoadedImage);
  1500. if (!EFI_ERROR(Status)) {
  1501. Image = PARENT_STRUCTURE(LoadedImage, EFI_IMAGE_DATA, Information);
  1502. ASSERT(Image->Magic == EFI_IMAGE_DATA_MAGIC);
  1503. } else {
  1504. ASSERT(FALSE);
  1505. Image = NULL;
  1506. }
  1507. return Image;
  1508. }
  1509. EFIAPI
  1510. EFI_STATUS
  1511. EfipCoreReadImageFile (
  1512. VOID *FileHandle,
  1513. UINTN FileOffset,
  1514. UINTN *ReadSize,
  1515. VOID *Buffer
  1516. )
  1517. /*++
  1518. Routine Description:
  1519. This routine reads contents of the PE/COFF image file.
  1520. Arguments:
  1521. FileHandle - Supplies a pointer to the file handle to read from.
  1522. FileOffset - Supplies an offset in bytes from the beginning of the file to
  1523. read.
  1524. ReadSize - Supplies a pointer that on input contains the number of bytes to
  1525. read. On output, returns the number of bytes read.
  1526. Buffer - Supplies the buffer where the read data will be returned.
  1527. Return Value:
  1528. EFI status code.
  1529. --*/
  1530. {
  1531. UINTN EndPosition;
  1532. PEFI_IMAGE_FILE_HANDLE ImageHandle;
  1533. if ((FileHandle == NULL) || (ReadSize == NULL) || (Buffer == NULL)) {
  1534. return EFI_INVALID_PARAMETER;
  1535. }
  1536. if (MAX_ADDRESS - FileOffset < *ReadSize) {
  1537. return EFI_INVALID_PARAMETER;
  1538. }
  1539. ImageHandle = (EFI_IMAGE_FILE_HANDLE *)FileHandle;
  1540. ASSERT(ImageHandle->Magic == EFI_IMAGE_FILE_HANDLE_MAGIC);
  1541. EndPosition = FileOffset + *ReadSize;
  1542. if (EndPosition > ImageHandle->SourceSize) {
  1543. *ReadSize = (UINT32)(ImageHandle->SourceSize - FileOffset);
  1544. }
  1545. if (FileOffset >= ImageHandle->SourceSize) {
  1546. *ReadSize = 0;
  1547. }
  1548. if (*ReadSize != 0) {
  1549. EfiCoreCopyMemory(Buffer,
  1550. (CHAR8 *)ImageHandle->Source + FileOffset,
  1551. *ReadSize);
  1552. }
  1553. return EFI_SUCCESS;
  1554. }
  1555. CHAR8 *
  1556. EfipCoreConvertFileNameToAscii (
  1557. CHAR16 *FileName,
  1558. UINTN *AsciiNameSize
  1559. )
  1560. /*++
  1561. Routine Description:
  1562. This routine creates an ASCII version of the given wide string.
  1563. Arguments:
  1564. FileName - Supplies a pointer to the wide string.
  1565. AsciiNameSize - Supplies a pointer where the size of the ASCII string in
  1566. bytes including the NULL terminator will be returned.
  1567. Return Value:
  1568. Returns a pointer to the ASCII string. The caller is responsible for
  1569. freeing this memory.
  1570. NULL on allocation failure.
  1571. --*/
  1572. {
  1573. CHAR8 *AsciiCharacter;
  1574. CHAR8 *AsciiString;
  1575. UINTN StringSize;
  1576. StringSize = EfiCoreStringLength(FileName) + 1;
  1577. AsciiString = EfiCoreAllocateBootPool(StringSize);
  1578. if (AsciiString == NULL) {
  1579. *AsciiNameSize = 0;
  1580. return NULL;
  1581. }
  1582. AsciiCharacter = AsciiString;
  1583. while (TRUE) {
  1584. *AsciiCharacter = *FileName;
  1585. if (*AsciiCharacter == '\0') {
  1586. break;
  1587. }
  1588. FileName += 1;
  1589. AsciiCharacter += 1;
  1590. }
  1591. *AsciiNameSize = StringSize;
  1592. return AsciiString;
  1593. }