8f07f895 |
1 | /** @file\r |
2 | CPU Exception Hanlder Library common functions.\r |
3 | \r |
4 | Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r |
5 | This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r |
8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
12 | \r |
13 | **/\r |
14 | \r |
15 | #include "CpuExceptionCommon.h"\r |
16 | \r |
17 | //\r |
18 | // Error code flag indicating whether or not an error code will be \r |
19 | // pushed on the stack if an exception occurs.\r |
20 | //\r |
21 | // 1 means an error code will be pushed, otherwise 0\r |
22 | //\r |
23 | UINT32 mErrorCodeFlag = 0x00027d00;\r |
24 | \r |
25 | //\r |
26 | // Define the maximum message length \r |
27 | //\r |
28 | #define MAX_DEBUG_MESSAGE_LENGTH 0x100\r |
29 | \r |
30 | /**\r |
31 | Prints a message to the serial port.\r |
32 | \r |
33 | @param Format Format string for the message to print.\r |
34 | @param ... Variable argument list whose contents are accessed \r |
35 | based on the format string specified by Format.\r |
36 | \r |
37 | **/\r |
38 | VOID\r |
39 | EFIAPI\r |
40 | InternalPrintMessage (\r |
41 | IN CONST CHAR8 *Format,\r |
42 | ...\r |
43 | )\r |
44 | {\r |
45 | CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH];\r |
46 | VA_LIST Marker;\r |
47 | \r |
48 | //\r |
49 | // Convert the message to an ASCII String\r |
50 | //\r |
51 | VA_START (Marker, Format);\r |
52 | AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);\r |
53 | VA_END (Marker);\r |
54 | \r |
55 | //\r |
56 | // Send the print string to a Serial Port \r |
57 | //\r |
58 | SerialPortWrite ((UINT8 *)Buffer, AsciiStrLen (Buffer));\r |
59 | }\r |
60 | \r |
61 | /**\r |
62 | Find and display image base address and return image base and its entry point.\r |
63 | \r |
a9c7ab95 |
64 | @param CurrentEip Current instruction pointer.\r |
65 | @param EntryPoint Return module entry point if module header is found.\r |
66 | \r |
67 | @return !0 Image base address.\r |
68 | @return 0 Image header cannot be found.\r |
8f07f895 |
69 | **/\r |
70 | UINTN \r |
71 | FindModuleImageBase (\r |
72 | IN UINTN CurrentEip,\r |
73 | OUT UINTN *EntryPoint\r |
74 | )\r |
75 | {\r |
76 | UINTN Pe32Data;\r |
77 | EFI_IMAGE_DOS_HEADER *DosHdr;\r |
78 | EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r |
79 | VOID *PdbPointer;\r |
80 | \r |
81 | //\r |
82 | // Find Image Base\r |
83 | //\r |
84 | Pe32Data = CurrentEip & ~(mImageAlignSize - 1);\r |
85 | while (Pe32Data != 0) {\r |
86 | DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;\r |
87 | if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r |
88 | //\r |
89 | // DOS image header is present, so read the PE header after the DOS image header.\r |
90 | //\r |
91 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN) ((DosHdr->e_lfanew) & 0x0ffff));\r |
92 | if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {\r |
93 | //\r |
94 | // It's PE image.\r |
95 | //\r |
96 | InternalPrintMessage ("!!!! Find PE image ");\r |
97 | *EntryPoint = (UINTN)Pe32Data + (UINTN)(Hdr.Pe32->OptionalHeader.AddressOfEntryPoint & 0x0ffffffff);\r |
98 | break;\r |
99 | }\r |
100 | } else {\r |
101 | //\r |
102 | // DOS image header is not present, TE header is at the image base.\r |
103 | //\r |
104 | Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;\r |
105 | if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&\r |
106 | ((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) {\r |
107 | //\r |
108 | // It's TE image, it TE header and Machine type match\r |
109 | //\r |
110 | InternalPrintMessage ("!!!! Find TE image ");\r |
111 | *EntryPoint = (UINTN)Pe32Data + (UINTN)(Hdr.Te->AddressOfEntryPoint & 0x0ffffffff) + sizeof(EFI_TE_IMAGE_HEADER) - Hdr.Te->StrippedSize;\r |
112 | break;\r |
113 | }\r |
114 | }\r |
115 | \r |
116 | //\r |
117 | // Not found the image base, check the previous aligned address\r |
118 | // \r |
119 | Pe32Data -= mImageAlignSize;\r |
120 | }\r |
121 | \r |
122 | if (Pe32Data != 0) {\r |
123 | PdbPointer = PeCoffLoaderGetPdbPointer ((VOID *) Pe32Data);\r |
124 | if (PdbPointer != NULL) {\r |
125 | InternalPrintMessage ("%a", PdbPointer);\r |
126 | } else {\r |
127 | InternalPrintMessage ("(No PDB) " );\r |
128 | }\r |
129 | } else {\r |
130 | InternalPrintMessage ("!!!! Can't find image information. !!!!\n");\r |
131 | }\r |
132 | \r |
133 | return Pe32Data;\r |
134 | }\r |
135 | \r |