image.c 59 KB

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