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