Success to build first Nt32 module.
[mirror_edk2.git] / Nt32Pkg / Library / EdkNt32PeiPeCoffGetEntryPointLib / PeCoffGetEntryPoint.c
CommitLineData
869f8e34 1/*++\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 PeCoffGetEntryPoint.c\r
15\r
16Abstract:\r
17\r
18 Tiano PE/COFF loader\r
19\r
20Revision History\r
21\r
22--*/\r
23\r
24\r
25//\r
26// Include common header file for this module.\r
27//\r
28#include "CommonHeader.h"\r
29\r
30RETURN_STATUS\r
31EFIAPI\r
32PeCoffLoaderGetEntryPoint (\r
33 IN VOID *Pe32Data,\r
34 IN OUT VOID **EntryPoint\r
35 )\r
36/*++\r
37\r
38Routine Description:\r
39\r
40 Loads a PE/COFF image into memory, this is not follow the original purpose of \r
41 PeCoffGetEntryPoint library class. But it's ok that Unix package not run on a real \r
42 platform and this is for source level debug.\r
43\r
44Arguments:\r
45\r
46 Pe32Data - Pointer to a PE/COFF Image\r
47\r
48 EntryPoint - Pointer to the entry point of the PE/COFF image\r
49\r
50Returns:\r
51\r
52 EFI_SUCCESS if the EntryPoint was returned\r
53 EFI_INVALID_PARAMETER if the EntryPoint could not be found from Pe32Data\r
54\r
55--*/\r
56{\r
57 EFI_STATUS Status;\r
58 EFI_PEI_PPI_DESCRIPTOR *PpiDescriptor;\r
59 NT_PEI_LOAD_FILE_PPI *PeiNtService;\r
60 EFI_PHYSICAL_ADDRESS ImageAddress;\r
61 UINT64 ImageSize;\r
62 EFI_PHYSICAL_ADDRESS ImageEntryPoint;\r
63\r
64 Status = PeiServicesLocatePpi (\r
65 &gNtPeiLoadFilePpiGuid,\r
66 0,\r
67 &PpiDescriptor,\r
68 &PeiNtService\r
69 );\r
70 if (EFI_ERROR (Status)) {\r
71 return Status;\r
72 }\r
73\r
74 Status = PeiNtService->PeiLoadFileService (\r
75 Pe32Data,\r
76 &ImageAddress,\r
77 &ImageSize,\r
78 &ImageEntryPoint\r
79 );\r
80 *EntryPoint = (VOID*)(UINTN)ImageEntryPoint;\r
81 return Status;\r
82}\r
83\r
84/**\r
85 Returns the machine type of PE/COFF image. \r
86 This is copied from MDE BasePeCoffGetEntryPointLib, the code should be sync with it.\r
87 The reason is NT32 package needs to load the image to memory to support source\r
88 level debug.\r
89 \r
90\r
91 @param Image Pointer to a PE/COFF header\r
92\r
93 @return Machine type or zero if not a valid iamge\r
94\r
95**/\r
96UINT16\r
97EFIAPI\r
98PeCoffLoaderGetMachineType (\r
99 IN VOID *Pe32Data\r
100 )\r
101{ \r
102 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
103 EFI_IMAGE_DOS_HEADER *DosHdr;\r
104\r
105 DosHdr = (EFI_IMAGE_DOS_HEADER *)Pe32Data;\r
106 if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
107 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data + DosHdr->e_lfanew);\r
108 } else {\r
109 Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Pe32Data);\r
110 }\r
111\r
112 if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r
113 return Hdr.Pe32->FileHeader.Machine;\r
114 }\r
115\r
116 return 0x0000;\r
117}\r
118\r