image.c 59 KB

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