basepe.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. basepe.c
  5. Abstract:
  6. This module implements basic PE/COFF file loader support.
  7. Author:
  8. Evan Green 13-Mar-2014
  9. Environment:
  10. Firmware
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "ueficore.h"
  16. #include "imagep.h"
  17. //
  18. // ---------------------------------------------------------------- Definitions
  19. //
  20. //
  21. // ------------------------------------------------------ Data Type Definitions
  22. //
  23. //
  24. // ----------------------------------------------- Internal Function Prototypes
  25. //
  26. VOID *
  27. EfipPeLoaderGetAddress (
  28. PEFI_PE_LOADER_CONTEXT Context,
  29. UINTN Address,
  30. UINTN TeStrippedOffset
  31. );
  32. //
  33. // -------------------------------------------------------------------- Globals
  34. //
  35. //
  36. // ------------------------------------------------------------------ Functions
  37. //
  38. EFIAPI
  39. RETURN_STATUS
  40. EfiPeLoaderLoadImage (
  41. PEFI_PE_LOADER_CONTEXT Context
  42. )
  43. /*++
  44. Routine Description:
  45. This routine loads a PE/COFF image.
  46. Arguments:
  47. Context - Supplies a pointer to the image context. Before calling this
  48. function the caller must have allocated the load buffer and filled in
  49. the image address and size fields.
  50. Return Value:
  51. RETURN_SUCCESS on success.
  52. RETURN_INVALID_PARAMETER if the image address is invalid.
  53. RETURN_LOAD_ERROR if the image is a PE/COFF runtime image with no
  54. relocations.
  55. RETURN_BUFFER_TOO_SMALL if the caller provided buffer was not large enough.
  56. --*/
  57. {
  58. CHAR8 *Base;
  59. EFI_PE_LOADER_CONTEXT CheckContext;
  60. EFI_IMAGE_DATA_DIRECTORY *Directories;
  61. EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
  62. CHAR8 *End;
  63. UINTN EntryPoint;
  64. EFI_IMAGE_SECTION_HEADER *FirstSection;
  65. UINTN FirstSectionOffset;
  66. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header;
  67. UINTN Index;
  68. UINT16 Magic;
  69. UINT32 NumberOfRvaAndSizes;
  70. UINT32 Offset;
  71. EFI_IMAGE_RESOURCE_DATA_ENTRY *ResourceDataEntry;
  72. EFI_IMAGE_RESOURCE_DIRECTORY *ResourceDirectory;
  73. EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *ResourceDirectoryEntry;
  74. EFI_IMAGE_RESOURCE_DIRECTORY_STRING *ResourceDirectoryString;
  75. EFI_IMAGE_SECTION_HEADER *Section;
  76. UINTN SectionCount;
  77. UINTN Size;
  78. RETURN_STATUS Status;
  79. CHAR16 *String;
  80. UINT32 TeStrippedOffset;
  81. ASSERT(Context != NULL);
  82. Context->ImageError = IMAGE_ERROR_SUCCESS;
  83. NumberOfRvaAndSizes = 0;
  84. Directories = NULL;
  85. //
  86. // Copy the provided context information into the local version.
  87. //
  88. EfiCoreCopyMemory(&CheckContext, Context, sizeof(EFI_PE_LOADER_CONTEXT));
  89. Status = EfiPeLoaderGetImageInfo(&CheckContext);
  90. if (RETURN_ERROR(Status)) {
  91. return Status;
  92. }
  93. //
  94. // Make sure there is enough allocated space for the image being loaded.
  95. //
  96. if (Context->ImageSize < CheckContext.ImageSize) {
  97. Context->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
  98. return RETURN_BUFFER_TOO_SMALL;
  99. }
  100. if (Context->ImageAddress == 0) {
  101. Context->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
  102. return RETURN_INVALID_PARAMETER;
  103. }
  104. //
  105. // If there are no relocations, it had better be loaded at its linked
  106. // address and not be a runtime driver.
  107. //
  108. if (CheckContext.RelocationsStripped != FALSE) {
  109. if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
  110. Context->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
  111. return RETURN_LOAD_ERROR;
  112. }
  113. if (CheckContext.ImageAddress != Context->ImageAddress) {
  114. Context->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
  115. return RETURN_INVALID_PARAMETER;
  116. }
  117. }
  118. //
  119. // Make sure the allocated space has the proper alignment.
  120. //
  121. if (Context->IsTeImage == FALSE) {
  122. if (Context->ImageAddress !=
  123. ALIGN_VALUE(Context->ImageAddress, CheckContext.SectionAlignment)) {
  124. Context->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
  125. return RETURN_INVALID_PARAMETER;
  126. }
  127. }
  128. //
  129. // Read the entire PE or TE header into memory.
  130. //
  131. Status = Context->ImageRead(Context->Handle,
  132. 0,
  133. &(Context->SizeOfHeaders),
  134. (VOID *)(UINTN)(Context->ImageAddress));
  135. if (Context->IsTeImage == FALSE) {
  136. Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Context->ImageAddress +
  137. Context->PeCoffHeaderOffset);
  138. FirstSectionOffset = Context->PeCoffHeaderOffset + sizeof(UINT32) +
  139. sizeof(EFI_IMAGE_FILE_HEADER) +
  140. Header.Pe32->FileHeader.SizeOfOptionalHeader;
  141. FirstSection =
  142. (EFI_IMAGE_SECTION_HEADER *)((UINTN)Context->ImageAddress +
  143. FirstSectionOffset);
  144. SectionCount = (UINTN)(Header.Pe32->FileHeader.NumberOfSections);
  145. TeStrippedOffset = 0;
  146. } else {
  147. Header.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(Context->ImageAddress);
  148. FirstSection =
  149. (EFI_IMAGE_SECTION_HEADER *)((UINTN)Context->ImageAddress +
  150. sizeof(EFI_TE_IMAGE_HEADER));
  151. SectionCount = (UINTN)(Header.Te->NumberOfSections);
  152. TeStrippedOffset = (UINT32)(Header.Te->StrippedSize) -
  153. sizeof(EFI_TE_IMAGE_HEADER);
  154. }
  155. if (RETURN_ERROR(Status)) {
  156. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  157. return RETURN_LOAD_ERROR;
  158. }
  159. //
  160. // Load each section of the image.
  161. //
  162. Section = FirstSection;
  163. for (Index = 0; Index < SectionCount; Index += 1) {
  164. Size = (UINTN)(Section->Misc.VirtualSize);
  165. if ((Size == 0) || (Size > Section->SizeOfRawData)) {
  166. Size = (UINTN)Section->SizeOfRawData;
  167. }
  168. //
  169. // Compute the section addresses.
  170. //
  171. Base = EfipPeLoaderGetAddress(Context,
  172. Section->VirtualAddress,
  173. TeStrippedOffset);
  174. End = EfipPeLoaderGetAddress(
  175. Context,
  176. Section->VirtualAddress + Section->Misc.VirtualSize - 1,
  177. TeStrippedOffset);
  178. if ((Size > 0) && ((Base == NULL) || (End == NULL))) {
  179. Context->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
  180. return RETURN_LOAD_ERROR;
  181. }
  182. if (Section->SizeOfRawData > 0) {
  183. Status = Context->ImageRead(
  184. Context->Handle,
  185. Section->PointerToRawData - TeStrippedOffset,
  186. &Size,
  187. Base);
  188. if (RETURN_ERROR(Status)) {
  189. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  190. return RETURN_LOAD_ERROR;
  191. }
  192. }
  193. //
  194. // If the raw size is less than the virtual size, zero fill the
  195. // remainder.
  196. //
  197. if (Size < Section->Misc.VirtualSize) {
  198. EfiSetMem(Base + Size, Section->Misc.VirtualSize - Size, 0);
  199. }
  200. Section += 1;
  201. }
  202. //
  203. // Get the image entry point.
  204. //
  205. Magic = EfiPeLoaderGetPeHeaderMagicValue(Header);
  206. if (Context->IsTeImage == FALSE) {
  207. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  208. EntryPoint =
  209. (UINTN)(Header.Pe32->OptionalHeader.AddressOfEntryPoint);
  210. } else {
  211. EntryPoint =
  212. (UINTN)(Header.Pe32Plus->OptionalHeader.AddressOfEntryPoint);
  213. }
  214. } else {
  215. EntryPoint = (UINTN)(Header.Te->AddressOfEntryPoint);
  216. }
  217. Context->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)EfipPeLoaderGetAddress(
  218. Context,
  219. EntryPoint,
  220. TeStrippedOffset);
  221. //
  222. // Determine the size of the fixup data.
  223. //
  224. if (Context->IsTeImage == FALSE) {
  225. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  226. NumberOfRvaAndSizes =
  227. Header.Pe32->OptionalHeader.NumberOfRvaAndSizes;
  228. Directories = &(Header.Pe32->OptionalHeader.DataDirectory[0]);
  229. DirectoryEntry =
  230. &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  231. } else {
  232. NumberOfRvaAndSizes =
  233. Header.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
  234. Directories = &(Header.Pe32Plus->OptionalHeader.DataDirectory[0]);
  235. DirectoryEntry =
  236. &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  237. }
  238. Context->FixupDataSize = 0;
  239. if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
  240. Context->FixupDataSize =
  241. DirectoryEntry->Size / sizeof(UINT16) * sizeof(UINTN);
  242. }
  243. } else {
  244. DirectoryEntry = &(Header.Te->DataDirectory[0]);
  245. Context->FixupDataSize =
  246. DirectoryEntry->Size / sizeof(UINT16) * sizeof(UINTN);
  247. }
  248. //
  249. // The consumer must allocate a buffer for the relocation fixup log.
  250. // This is used by the runtime relocation code.
  251. //
  252. Context->FixupData = NULL;
  253. //
  254. // Get the image's HII resource section.
  255. //
  256. Context->HiiResourceData = 0;
  257. if (Context->IsTeImage == FALSE) {
  258. DirectoryEntry = &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE]);
  259. if ((NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE) &&
  260. (DirectoryEntry->Size != 0)) {
  261. ASSERT(Directories != NULL);
  262. Base = EfipPeLoaderGetAddress(Context,
  263. DirectoryEntry->VirtualAddress,
  264. 0);
  265. if (Base != NULL) {
  266. ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *)Base;
  267. Offset = sizeof(EFI_IMAGE_RESOURCE_DIRECTORY) +
  268. (sizeof(EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) *
  269. (ResourceDirectory->NumberOfNamedEntries +
  270. ResourceDirectory->NumberOfIdEntries));
  271. if (Offset > DirectoryEntry->Size) {
  272. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  273. return RETURN_UNSUPPORTED;
  274. }
  275. //
  276. // Loop through every directory entry.
  277. //
  278. ResourceDirectoryEntry =
  279. (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY *)(ResourceDirectory +
  280. 1);
  281. for (Index = 0;
  282. Index < ResourceDirectory->NumberOfNamedEntries;
  283. Index += 1) {
  284. //
  285. // Skip entries whose names are not strings.
  286. //
  287. if (ResourceDirectoryEntry->u1.s.NameIsString == 0) {
  288. ResourceDirectoryEntry += 1;
  289. continue;
  290. }
  291. if (ResourceDirectoryEntry->u1.s.NameOffset >=
  292. DirectoryEntry->Size) {
  293. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  294. return RETURN_UNSUPPORTED;
  295. }
  296. //
  297. // Skip entries not named "HII".
  298. //
  299. Offset = ResourceDirectoryEntry->u1.s.NameOffset;
  300. ResourceDirectoryString =
  301. (EFI_IMAGE_RESOURCE_DIRECTORY_STRING *)(Base + Offset);
  302. String = &(ResourceDirectoryString->String[0]);
  303. if ((ResourceDirectoryString->Length != 3) ||
  304. (String[0] != L'H') ||
  305. (String[1] != L'I') ||
  306. (String[2] != L'I')) {
  307. ResourceDirectoryEntry += 1;
  308. continue;
  309. }
  310. //
  311. // A HII resource was found.
  312. //
  313. if (ResourceDirectoryEntry->u2.s.DataIsDirectory != 0) {
  314. //
  315. // Move to the next level - Resource Name.
  316. //
  317. if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >=
  318. DirectoryEntry->Size) {
  319. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  320. return RETURN_UNSUPPORTED;
  321. }
  322. Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory;
  323. ResourceDirectory =
  324. (EFI_IMAGE_RESOURCE_DIRECTORY *)(Base + Offset);
  325. Offset += sizeof(EFI_IMAGE_RESOURCE_DIRECTORY) +
  326. (sizeof(EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) *
  327. (ResourceDirectory->NumberOfNamedEntries +
  328. ResourceDirectory->NumberOfIdEntries));
  329. if (Offset > DirectoryEntry->Size) {
  330. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  331. return RETURN_UNSUPPORTED;
  332. }
  333. ResourceDirectoryEntry =
  334. (VOID *)(ResourceDirectory + 1);
  335. if (ResourceDirectoryEntry->u2.s.DataIsDirectory != 0) {
  336. //
  337. // Move to the next level - Resource Language.
  338. //
  339. Offset =
  340. ResourceDirectoryEntry->u2.s.OffsetToDirectory;
  341. if (Offset >= DirectoryEntry->Size) {
  342. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  343. return RETURN_UNSUPPORTED;
  344. }
  345. ResourceDirectory = (VOID *)(Base + Offset);
  346. Offset +=
  347. sizeof(EFI_IMAGE_RESOURCE_DIRECTORY) +
  348. (sizeof(EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) *
  349. (ResourceDirectory->NumberOfNamedEntries +
  350. ResourceDirectory->NumberOfIdEntries));
  351. if (Offset > DirectoryEntry->Size) {
  352. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  353. return RETURN_UNSUPPORTED;
  354. }
  355. ResourceDirectoryEntry =
  356. (VOID *)(ResourceDirectory + 1);
  357. }
  358. }
  359. //
  360. // Now it ought to be resource data.
  361. //
  362. if (ResourceDirectoryEntry->u2.s.DataIsDirectory != 0) {
  363. if (ResourceDirectoryEntry->u2.OffsetToData >=
  364. DirectoryEntry->Size) {
  365. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  366. return RETURN_UNSUPPORTED;
  367. }
  368. Offset = ResourceDirectoryEntry->u2.OffsetToData;
  369. ResourceDataEntry =
  370. (EFI_IMAGE_RESOURCE_DATA_ENTRY *)(Base + Offset);
  371. Offset = ResourceDataEntry->OffsetToData;
  372. Context->HiiResourceData =
  373. (PHYSICAL_ADDRESS)(UINTN)EfipPeLoaderGetAddress(
  374. Context,
  375. Offset,
  376. 0);
  377. break;
  378. }
  379. ResourceDirectoryEntry += 1;
  380. }
  381. }
  382. }
  383. }
  384. return Status;
  385. }
  386. EFIAPI
  387. RETURN_STATUS
  388. EfiPeLoaderRelocateImage (
  389. PEFI_PE_LOADER_CONTEXT Context
  390. )
  391. /*++
  392. Routine Description:
  393. This routine relocates a loaded PE image.
  394. Arguments:
  395. Context - Supplies a pointer to the image context.
  396. Return Value:
  397. RETURN_SUCCESS on success.
  398. RETURN_LOAD_ERROR if the image is not valid.
  399. RETURN_UNSUPPORTED if an unsupported relocation type was encountered.
  400. --*/
  401. {
  402. UINT64 Adjust;
  403. PHYSICAL_ADDRESS BaseAddress;
  404. EFI_IMAGE_DATA_DIRECTORY *Directories;
  405. UINTN EndOffset;
  406. CHAR8 *Fixup;
  407. UINT16 *Fixup16;
  408. UINT32 *Fixup32;
  409. UINT64 *Fixup64;
  410. CHAR8 *FixupBase;
  411. CHAR8 *FixupData;
  412. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header;
  413. UINT16 Magic;
  414. UINT32 NumberOfRvaAndSizes;
  415. UINT16 *Relocation;
  416. EFI_IMAGE_BASE_RELOCATION *RelocationBase;
  417. EFI_IMAGE_BASE_RELOCATION *RelocationBaseEnd;
  418. EFI_IMAGE_DATA_DIRECTORY *RelocationDirectory;
  419. UINT16 *RelocationEnd;
  420. UINT32 TeStrippedOffset;
  421. ASSERT(Context != NULL);
  422. Context->ImageError = IMAGE_ERROR_SUCCESS;
  423. if (Context->RelocationsStripped != FALSE) {
  424. return RETURN_SUCCESS;
  425. }
  426. //
  427. // If the destination address is not zero, use that rather than the image
  428. // address.
  429. //
  430. BaseAddress = Context->ImageAddress;
  431. if (Context->DestinationAddress != 0) {
  432. BaseAddress = Context->DestinationAddress;
  433. }
  434. if (Context->IsTeImage == FALSE) {
  435. Header.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Context->ImageAddress +
  436. Context->PeCoffHeaderOffset);
  437. TeStrippedOffset = 0;
  438. Magic = EfiPeLoaderGetPeHeaderMagicValue(Header);
  439. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  440. Adjust = (UINT64)BaseAddress -
  441. Header.Pe32->OptionalHeader.ImageBase;
  442. if (Adjust != 0) {
  443. Header.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;
  444. }
  445. NumberOfRvaAndSizes =
  446. Header.Pe32->OptionalHeader.NumberOfRvaAndSizes;
  447. Directories = &(Header.Pe32->OptionalHeader.DataDirectory[0]);
  448. RelocationDirectory =
  449. &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  450. } else {
  451. Adjust = (UINT64)BaseAddress -
  452. Header.Pe32Plus->OptionalHeader.ImageBase;
  453. if (Adjust != 0) {
  454. Header.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;
  455. }
  456. NumberOfRvaAndSizes =
  457. Header.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
  458. Directories = &(Header.Pe32Plus->OptionalHeader.DataDirectory[0]);
  459. RelocationDirectory =
  460. &(Directories[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC]);
  461. }
  462. if (NumberOfRvaAndSizes < EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
  463. RelocationDirectory = NULL;
  464. }
  465. } else {
  466. Header.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(Context->ImageAddress);
  467. TeStrippedOffset = (UINT32)(Header.Te->StrippedSize) -
  468. sizeof(EFI_TE_IMAGE_HEADER);
  469. Adjust = (UINT64)(BaseAddress -
  470. (Header.Te->ImageBase + TeStrippedOffset));
  471. if (Adjust != 0) {
  472. Header.Te->ImageBase = (UINT64)(BaseAddress - TeStrippedOffset);
  473. }
  474. RelocationDirectory = &(Header.Te->DataDirectory[0]);
  475. }
  476. if ((RelocationDirectory != NULL) && (RelocationDirectory->Size != 0)) {
  477. RelocationBase = (EFI_IMAGE_BASE_RELOCATION *)EfipPeLoaderGetAddress(
  478. Context,
  479. RelocationDirectory->VirtualAddress,
  480. TeStrippedOffset);
  481. EndOffset = RelocationDirectory->VirtualAddress +
  482. RelocationDirectory->Size - 1;
  483. RelocationBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)EfipPeLoaderGetAddress(
  484. Context,
  485. EndOffset,
  486. TeStrippedOffset);
  487. if ((RelocationBase == NULL) || (RelocationBaseEnd == NULL)) {
  488. Context->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
  489. return RETURN_LOAD_ERROR;
  490. }
  491. } else {
  492. RelocationBase = NULL;
  493. RelocationBaseEnd = NULL;
  494. }
  495. //
  496. // If there are adjustments to be made, relocate the image.
  497. //
  498. if (Adjust != 0) {
  499. FixupData = Context->FixupData;
  500. //
  501. // Loop across every relocation page.
  502. //
  503. while (RelocationBase < RelocationBaseEnd) {
  504. Relocation = (UINT16 *)((CHAR8 *)RelocationBase +
  505. sizeof(EFI_IMAGE_BASE_RELOCATION));
  506. if ((RelocationBase->SizeOfBlock == 0) ||
  507. (RelocationBase->SizeOfBlock > RelocationDirectory->Size)) {
  508. Context->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
  509. return RETURN_LOAD_ERROR;
  510. }
  511. RelocationEnd = (UINT16 *)((CHAR8 *)RelocationBase +
  512. RelocationBase->SizeOfBlock);
  513. FixupBase = EfipPeLoaderGetAddress(Context,
  514. RelocationBase->VirtualAddress,
  515. TeStrippedOffset);
  516. if (FixupBase == NULL) {
  517. Context->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
  518. return RETURN_LOAD_ERROR;
  519. }
  520. //
  521. // Run every relocation in the page.
  522. //
  523. while (Relocation < RelocationEnd) {
  524. Fixup = FixupBase + (*Relocation & 0xFFF);
  525. switch ((*Relocation) >> 12) {
  526. case EFI_IMAGE_REL_BASED_ABSOLUTE:
  527. break;
  528. case EFI_IMAGE_REL_BASED_HIGH:
  529. Fixup16 = (UINT16 *)Fixup;
  530. *Fixup16 =
  531. (UINT16)(*Fixup + ((UINT16)((UINT32)Adjust >> 16)));
  532. if (FixupData != NULL) {
  533. *(UINT16 *)FixupData = *Fixup16;
  534. FixupData = FixupData + sizeof(UINT16);
  535. }
  536. break;
  537. case EFI_IMAGE_REL_BASED_LOW:
  538. Fixup16 = (UINT16 *)Fixup;
  539. *Fixup16 = (UINT16)(*Fixup + (UINT16)Adjust);
  540. if (FixupData != NULL) {
  541. *(UINT16 *)FixupData = *Fixup16;
  542. FixupData = FixupData + sizeof(UINT16);
  543. }
  544. break;
  545. case EFI_IMAGE_REL_BASED_HIGHLOW:
  546. Fixup32 = (UINT32 *)Fixup;
  547. *Fixup32 = *Fixup32 + (UINT32)Adjust;
  548. if (FixupData != NULL) {
  549. FixupData = ALIGN_POINTER(FixupData, sizeof(UINT32));
  550. *(UINT32 *)FixupData = *Fixup32;
  551. FixupData = FixupData + sizeof(UINT32);
  552. }
  553. break;
  554. case EFI_IMAGE_REL_BASED_DIR64:
  555. Fixup64 = (UINT64 *)Fixup;
  556. *Fixup64 = *Fixup64 + (UINT64)Adjust;
  557. if (FixupData != NULL) {
  558. FixupData = ALIGN_POINTER(FixupData, sizeof(UINT64));
  559. *(UINT64 *)FixupData = *Fixup64;
  560. FixupData = FixupData + sizeof(UINT64);
  561. }
  562. break;
  563. default:
  564. RtlDebugPrint("Error: Unknown relocation type.\n");
  565. Context->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
  566. return RETURN_LOAD_ERROR;
  567. }
  568. //
  569. // Move to the next relocation record.
  570. //
  571. Relocation += 1;
  572. }
  573. //
  574. // Move to the next relocation page.
  575. //
  576. RelocationBase = (EFI_IMAGE_BASE_RELOCATION *)RelocationEnd;
  577. }
  578. //
  579. // Adjust the entry point.
  580. //
  581. if (Context->DestinationAddress != 0) {
  582. Context->EntryPoint -= (UINT64)Context->ImageAddress;
  583. Context->EntryPoint += (UINT64)Context->DestinationAddress;
  584. }
  585. }
  586. return RETURN_SUCCESS;
  587. }
  588. EFIAPI
  589. RETURN_STATUS
  590. EfiPeLoaderGetImageInfo (
  591. PEFI_PE_LOADER_CONTEXT Context
  592. )
  593. /*++
  594. Routine Description:
  595. This routine extracts information about the given PE/COFF image.
  596. Arguments:
  597. Context - Supplies a pointer to the image context.
  598. Return Value:
  599. RETURN_SUCCESS on success.
  600. RETURN_INVALID_PARAMETER if the image context is NULL.
  601. RETURN_UNSUPPORTED if the image format is not supported.
  602. --*/
  603. {
  604. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header;
  605. EFI_IMAGE_OPTIONAL_HEADER_UNION HeaderData;
  606. UINT16 Magic;
  607. RETURN_STATUS Status;
  608. UINT32 TeStrippedOffset;
  609. if (Context == NULL) {
  610. return EFI_INVALID_PARAMETER;
  611. }
  612. Context->ImageError = IMAGE_ERROR_SUCCESS;
  613. Header.Union = &HeaderData;
  614. Status = EfiPeLoaderGetPeHeader(Context, Header);
  615. if (RETURN_ERROR(Status)) {
  616. return Status;
  617. }
  618. Magic = EfiPeLoaderGetPeHeaderMagicValue(Header);
  619. //
  620. // Get the base address of the image.
  621. //
  622. if (Context->IsTeImage == FALSE) {
  623. TeStrippedOffset = 0;
  624. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  625. Context->ImageAddress = Header.Pe32->OptionalHeader.ImageBase;
  626. } else {
  627. Context->ImageAddress = Header.Pe32Plus->OptionalHeader.ImageBase;
  628. }
  629. } else {
  630. TeStrippedOffset = (UINT32)(Header.Te->StrippedSize -
  631. sizeof(EFI_TE_IMAGE_HEADER));
  632. Context->ImageAddress = (EFI_PHYSICAL_ADDRESS)(Header.Te->ImageBase +
  633. TeStrippedOffset);
  634. }
  635. Context->DestinationAddress = 0;
  636. Context->DebugDirectoryEntryRva = 0;
  637. Context->CodeView = NULL;
  638. Context->PdbPointer = NULL;
  639. //
  640. // Look at the file header to determine if relocations have been stripped.
  641. //
  642. if ((Context->IsTeImage == FALSE) &&
  643. ((Header.Pe32->FileHeader.Characteristics &
  644. EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {
  645. Context->RelocationsStripped = TRUE;
  646. } else if ((Context->IsTeImage != FALSE) &&
  647. (Header.Te->DataDirectory[0].Size == 0) &&
  648. (Header.Te->DataDirectory[0].VirtualAddress == 0)) {
  649. Context->RelocationsStripped = TRUE;
  650. } else {
  651. Context->RelocationsStripped = FALSE;
  652. }
  653. return EFI_SUCCESS;
  654. }
  655. EFIAPI
  656. RETURN_STATUS
  657. EfiPeLoaderUnloadImage (
  658. PEFI_PE_LOADER_CONTEXT Context
  659. )
  660. /*++
  661. Routine Description:
  662. This routine unloads the PE/COFF image.
  663. Arguments:
  664. Context - Supplies a pointer to the image context.
  665. Return Value:
  666. RETURN_* status code.
  667. --*/
  668. {
  669. return RETURN_SUCCESS;
  670. }
  671. UINT16
  672. EfiPeLoaderGetPeHeaderMagicValue (
  673. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header
  674. )
  675. /*++
  676. Routine Description:
  677. This routine returns the magic value out of the PE/COFF header.
  678. Arguments:
  679. Header - Supplies a pointer to the header.
  680. Return Value:
  681. Returns the magic value from the header.
  682. --*/
  683. {
  684. return Header.Pe32->OptionalHeader.Magic;
  685. }
  686. RETURN_STATUS
  687. EfiPeLoaderGetPeHeader (
  688. PEFI_PE_LOADER_CONTEXT Context,
  689. EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Header
  690. )
  691. /*++
  692. Routine Description:
  693. This routine retrieves the PE or TE header from a PE/COFF or TE image.
  694. Arguments:
  695. Context - Supplies a pointer to the loader context.
  696. Header - Supplies a pointer to the header.
  697. Return Value:
  698. RETURN_* error code.
  699. --*/
  700. {
  701. CHAR8 BufferData;
  702. EFI_IMAGE_DOS_HEADER DosHeader;
  703. UINT32 HeaderWithoutDataDirectory;
  704. UINT32 Index;
  705. UINTN LastByteOffset;
  706. UINT16 Magic;
  707. UINTN ReadSize;
  708. UINTN SectionCount;
  709. EFI_IMAGE_SECTION_HEADER SectionHeader;
  710. UINT32 SectionHeaderOffset;
  711. UINTN Size;
  712. RETURN_STATUS Status;
  713. UINT32 TeStrippedOffset;
  714. //
  715. // Read the DOS image header to check for its existence.
  716. //
  717. ASSERT(Context->ImageRead != NULL);
  718. Size = sizeof(EFI_IMAGE_DOS_HEADER);
  719. ReadSize = Size;
  720. Status = Context->ImageRead(Context->Handle,
  721. 0,
  722. &Size,
  723. &DosHeader);
  724. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  725. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  726. if (Size != ReadSize) {
  727. Status = RETURN_UNSUPPORTED;
  728. }
  729. return Status;
  730. }
  731. //
  732. // Assume the PE header is at the beginning of the image. If the DOS
  733. // header is valid, then the PE header comes at some point after the DOS
  734. // header.
  735. //
  736. Context->PeCoffHeaderOffset = 0;
  737. if (DosHeader.e_magic == EFI_IMAGE_DOS_SIGNATURE) {
  738. Context->PeCoffHeaderOffset = DosHeader.e_lfanew;
  739. }
  740. //
  741. // Read the PE/COFF header. This may read too much, but that's alright.
  742. //
  743. Size = sizeof(EFI_IMAGE_OPTIONAL_HEADER_UNION);
  744. ReadSize = Size;
  745. Status = Context->ImageRead(Context->Handle,
  746. Context->PeCoffHeaderOffset,
  747. &Size,
  748. Header.Pe32);
  749. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  750. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  751. if (Size != ReadSize) {
  752. Status = RETURN_UNSUPPORTED;
  753. }
  754. return Status;
  755. }
  756. //
  757. // Use the signature to figure out the image offset. Start with TE images.
  758. //
  759. if (Header.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
  760. Context->IsTeImage = TRUE;
  761. Context->Machine = Header.Te->Machine;
  762. Context->ImageType = (UINT16)(Header.Te->Subsystem);
  763. Context->ImageSize = 0;
  764. Context->SectionAlignment = 0;
  765. Context->SizeOfHeaders = sizeof(EFI_TE_IMAGE_HEADER) +
  766. (UINTN)(Header.Te->BaseOfCode) -
  767. (UINTN)(Header.Te->StrippedSize);
  768. if ((sizeof(EFI_TE_IMAGE_HEADER) >= (UINT32)Header.Te->StrippedSize) ||
  769. (Header.Te->BaseOfCode <= Header.Te->StrippedSize)) {
  770. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  771. return RETURN_UNSUPPORTED;
  772. }
  773. //
  774. // Read the last byte of the header from the file.
  775. //
  776. Size = 1;
  777. ReadSize = Size;
  778. Status = Context->ImageRead(Context->Handle,
  779. Context->SizeOfHeaders - 1,
  780. &Size,
  781. &BufferData);
  782. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  783. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  784. if (Size != ReadSize) {
  785. Status = RETURN_UNSUPPORTED;
  786. }
  787. return Status;
  788. }
  789. //
  790. // If the TE image data directory entry size is non-zero, but the data
  791. // directory VA is zero, then that's not valid.
  792. //
  793. if (((Header.Te->DataDirectory[0].Size != 0) &&
  794. (Header.Te->DataDirectory[0].VirtualAddress == 0)) ||
  795. ((Header.Te->DataDirectory[1].Size != 0) &&
  796. (Header.Te->DataDirectory[1].VirtualAddress == 0))) {
  797. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  798. return RETURN_UNSUPPORTED;
  799. }
  800. //
  801. // It's not a TE image, handle it being a PE image.
  802. //
  803. } else if (Header.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
  804. Context->IsTeImage = FALSE;
  805. Context->Machine = Header.Pe32->FileHeader.Machine;
  806. Magic = EfiPeLoaderGetPeHeaderMagicValue(Header);
  807. //
  808. // Handle a 32-bit image.
  809. //
  810. if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  811. //
  812. // Check the number of RVA and sizes.
  813. //
  814. if (Header.Pe32->OptionalHeader.NumberOfRvaAndSizes >
  815. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES) {
  816. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  817. return RETURN_UNSUPPORTED;
  818. }
  819. //
  820. // Check the size of the optional header.
  821. //
  822. HeaderWithoutDataDirectory =
  823. sizeof(EFI_IMAGE_OPTIONAL_HEADER32) -
  824. (sizeof(EFI_IMAGE_DATA_DIRECTORY) *
  825. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES);
  826. if ((UINT32)(Header.Pe32->FileHeader.SizeOfOptionalHeader) -
  827. HeaderWithoutDataDirectory !=
  828. Header.Pe32->OptionalHeader.NumberOfRvaAndSizes *
  829. sizeof(EFI_IMAGE_DATA_DIRECTORY)) {
  830. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  831. return RETURN_UNSUPPORTED;
  832. }
  833. //
  834. // Check the number of sections.
  835. //
  836. SectionHeaderOffset = Context->PeCoffHeaderOffset +
  837. sizeof(UINT32) +
  838. sizeof(EFI_IMAGE_FILE_HEADER) +
  839. Header.Pe32->FileHeader.SizeOfOptionalHeader;
  840. if (Header.Pe32->OptionalHeader.SizeOfImage < SectionHeaderOffset) {
  841. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  842. return RETURN_UNSUPPORTED;
  843. }
  844. if ((Header.Pe32->OptionalHeader.SizeOfImage -
  845. SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <=
  846. Header.Pe32->FileHeader.NumberOfSections) {
  847. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  848. return RETURN_UNSUPPORTED;
  849. }
  850. //
  851. // Check the size of headers field.
  852. //
  853. if ((Header.Pe32->OptionalHeader.SizeOfHeaders <
  854. SectionHeaderOffset) ||
  855. (Header.Pe32->OptionalHeader.SizeOfHeaders >=
  856. Header.Pe32->OptionalHeader.SizeOfImage) ||
  857. ((Header.Pe32->OptionalHeader.SizeOfHeaders -
  858. SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <
  859. (UINT32)(Header.Pe32->FileHeader.NumberOfSections))) {
  860. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  861. return RETURN_UNSUPPORTED;
  862. }
  863. //
  864. // Read the last byte of the headers from the file.
  865. //
  866. Size = 1;
  867. ReadSize = Size;
  868. Status = Context->ImageRead(
  869. Context->Handle,
  870. Header.Pe32->OptionalHeader.SizeOfHeaders - 1,
  871. &Size,
  872. &BufferData);
  873. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  874. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  875. if (Size != ReadSize) {
  876. Status = RETURN_UNSUPPORTED;
  877. }
  878. return Status;
  879. }
  880. Context->ImageType = Header.Pe32->OptionalHeader.Subsystem;
  881. Context->ImageSize =
  882. (UINT64)(Header.Pe32->OptionalHeader.SizeOfImage);
  883. Context->SectionAlignment =
  884. Header.Pe32->OptionalHeader.SectionAlignment;
  885. Context->SizeOfHeaders = Header.Pe32->OptionalHeader.SizeOfHeaders;
  886. //
  887. // Handle a 64-bit image.
  888. //
  889. } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  890. //
  891. // Check the number of RVA and sizes.
  892. //
  893. if (Header.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes >
  894. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES) {
  895. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  896. return RETURN_UNSUPPORTED;
  897. }
  898. //
  899. // Check the size of the optional header.
  900. //
  901. HeaderWithoutDataDirectory =
  902. sizeof(EFI_IMAGE_OPTIONAL_HEADER64) -
  903. (sizeof(EFI_IMAGE_DATA_DIRECTORY) *
  904. EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES);
  905. if ((UINT32)(Header.Pe32Plus->FileHeader.SizeOfOptionalHeader) -
  906. HeaderWithoutDataDirectory !=
  907. Header.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes *
  908. sizeof(EFI_IMAGE_DATA_DIRECTORY)) {
  909. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  910. return RETURN_UNSUPPORTED;
  911. }
  912. //
  913. // Check the number of sections.
  914. //
  915. SectionHeaderOffset =
  916. Context->PeCoffHeaderOffset +
  917. sizeof(UINT32) +
  918. sizeof(EFI_IMAGE_FILE_HEADER) +
  919. Header.Pe32Plus->FileHeader.SizeOfOptionalHeader;
  920. if (Header.Pe32Plus->OptionalHeader.SizeOfImage <
  921. SectionHeaderOffset) {
  922. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  923. return RETURN_UNSUPPORTED;
  924. }
  925. if ((Header.Pe32Plus->OptionalHeader.SizeOfImage -
  926. SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <=
  927. Header.Pe32Plus->FileHeader.NumberOfSections) {
  928. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  929. return RETURN_UNSUPPORTED;
  930. }
  931. //
  932. // Check the size of headers field.
  933. //
  934. if ((Header.Pe32Plus->OptionalHeader.SizeOfHeaders <
  935. SectionHeaderOffset) ||
  936. (Header.Pe32Plus->OptionalHeader.SizeOfHeaders >=
  937. Header.Pe32Plus->OptionalHeader.SizeOfImage) ||
  938. ((Header.Pe32Plus->OptionalHeader.SizeOfHeaders -
  939. SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER <
  940. (UINT32)(Header.Pe32Plus->FileHeader.NumberOfSections))) {
  941. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  942. return RETURN_UNSUPPORTED;
  943. }
  944. //
  945. // Read the last byte of the headers from the file.
  946. //
  947. Size = 1;
  948. ReadSize = Size;
  949. Status = Context->ImageRead(
  950. Context->Handle,
  951. Header.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,
  952. &Size,
  953. &BufferData);
  954. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  955. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  956. if (Size != ReadSize) {
  957. Status = RETURN_UNSUPPORTED;
  958. }
  959. return Status;
  960. }
  961. Context->ImageType = Header.Pe32Plus->OptionalHeader.Subsystem;
  962. Context->ImageSize =
  963. (UINT64)(Header.Pe32Plus->OptionalHeader.SizeOfImage);
  964. Context->SectionAlignment =
  965. Header.Pe32Plus->OptionalHeader.SectionAlignment;
  966. Context->SizeOfHeaders =
  967. Header.Pe32Plus->OptionalHeader.SizeOfHeaders;
  968. //
  969. // The magic isn't known.
  970. //
  971. } else {
  972. Context->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
  973. return RETURN_UNSUPPORTED;
  974. }
  975. //
  976. // This header signature is not recognized.
  977. //
  978. } else {
  979. Context->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
  980. return RETURN_UNSUPPORTED;
  981. }
  982. if (!EFI_IMAGE_MACHINE_TYPE_SUPPORTED(Context->Machine)) {
  983. return RETURN_UNSUPPORTED;
  984. }
  985. //
  986. // Check each section field.
  987. //
  988. if (Context->IsTeImage != FALSE) {
  989. SectionHeaderOffset = sizeof(EFI_TE_IMAGE_HEADER);
  990. SectionCount = (UINTN)(Header.Te->NumberOfSections);
  991. } else {
  992. SectionHeaderOffset = Context->PeCoffHeaderOffset + sizeof(UINT32) +
  993. sizeof(EFI_IMAGE_FILE_HEADER) +
  994. Header.Pe32->FileHeader.SizeOfOptionalHeader;
  995. SectionCount = (UINTN)(Header.Pe32->FileHeader.NumberOfSections);
  996. }
  997. for (Index = 0; Index < SectionCount; Index += 1) {
  998. //
  999. // Read the section header from the file.
  1000. //
  1001. Size = sizeof(EFI_IMAGE_SECTION_HEADER);
  1002. ReadSize = Size;
  1003. Status = Context->ImageRead(Context->Handle,
  1004. SectionHeaderOffset,
  1005. &Size,
  1006. &SectionHeader);
  1007. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  1008. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  1009. if (Size != ReadSize) {
  1010. Status = RETURN_UNSUPPORTED;
  1011. }
  1012. return Status;
  1013. }
  1014. //
  1015. // Adjust the offsets in the section header for TE images.
  1016. //
  1017. if (Context->IsTeImage != FALSE) {
  1018. TeStrippedOffset = (UINT32)Header.Te->StrippedSize -
  1019. sizeof(EFI_TE_IMAGE_HEADER);
  1020. SectionHeader.VirtualAddress -= TeStrippedOffset;
  1021. SectionHeader.PointerToRawData -= TeStrippedOffset;
  1022. }
  1023. if (SectionHeader.SizeOfRawData != 0) {
  1024. //
  1025. // Section data should be beyond the PE header, and shouldn't
  1026. // overflow.
  1027. //
  1028. if ((SectionHeader.VirtualAddress < Context->SizeOfHeaders) ||
  1029. (SectionHeader.PointerToRawData < Context->SizeOfHeaders) ||
  1030. ((UINT32)(~0) - SectionHeader.PointerToRawData <
  1031. SectionHeader.SizeOfRawData)) {
  1032. Context->ImageError = IMAGE_ERROR_UNSUPPORTED;
  1033. return RETURN_UNSUPPORTED;
  1034. }
  1035. //
  1036. // Read the last byte of the section data.
  1037. //
  1038. Size = 1;
  1039. ReadSize = Size;
  1040. LastByteOffset = SectionHeader.PointerToRawData +
  1041. SectionHeader.SizeOfRawData - 1;
  1042. Status = Context->ImageRead(Context->Handle,
  1043. LastByteOffset,
  1044. &Size,
  1045. &BufferData);
  1046. if ((RETURN_ERROR(Status)) || (Size != ReadSize)) {
  1047. Context->ImageError = IMAGE_ERROR_IMAGE_READ;
  1048. if (Size != ReadSize) {
  1049. Status = RETURN_UNSUPPORTED;
  1050. }
  1051. return Status;
  1052. }
  1053. }
  1054. //
  1055. // Move to the next section.
  1056. //
  1057. SectionHeaderOffset += sizeof(EFI_IMAGE_SECTION_HEADER);
  1058. }
  1059. return RETURN_SUCCESS;
  1060. }
  1061. //
  1062. // --------------------------------------------------------- Internal Functions
  1063. //
  1064. VOID *
  1065. EfipPeLoaderGetAddress (
  1066. PEFI_PE_LOADER_CONTEXT Context,
  1067. UINTN Address,
  1068. UINTN TeStrippedOffset
  1069. )
  1070. /*++
  1071. Routine Description:
  1072. This routine converts an image address into a loaded in-memory address.
  1073. Arguments:
  1074. Context - Supplies a pointer to the loader context.
  1075. Address - Supplies the address to translate.
  1076. TeStrippedOffset - Supplies the stripped offset for TE images.
  1077. Return Value:
  1078. Returns the in memory address.
  1079. NULL on failure.
  1080. --*/
  1081. {
  1082. if (Address >= Context->ImageSize + TeStrippedOffset) {
  1083. Context->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
  1084. return NULL;
  1085. }
  1086. return (CHAR8 *)((UINTN)Context->ImageAddress + Address - TeStrippedOffset);
  1087. }