Skip to content

Commit

Permalink
MdePkg/Library/BaseUeImageLib: Fixed Windows 10 BlueScreen issue for …
Browse files Browse the repository at this point in the history
…X64.
  • Loading branch information
Mikhail Krichanov committed Sep 5, 2023
1 parent b3daec3 commit 9ff4f89
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
[FixedPcd]
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRelocTypePolicy
gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask
gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRtRelocAllowTargetMismatch
61 changes: 56 additions & 5 deletions MdePkg/Library/BaseUeImageLib/UeImageLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/PcdLib.h>
#include <Library/PeCoffLib2.h>
#include <Library/UefiImageLib.h>
Expand All @@ -23,6 +24,8 @@
struct UE_LOADER_RUNTIME_CONTEXT_ {
UINT8 Machine;
UINT8 Reserved[7];
UINT32 FixupSize;
UINT64 *FixupData;
UINT32 RelocTableSize;
UINT8 RelocTable[];
};
Expand Down Expand Up @@ -457,7 +460,9 @@ InternalApplyRelocation (
IN UINT8 Machine,
IN UINT16 RelocType,
IN UINT32 *RelocTarget,
IN UINT64 Adjust
IN UINT64 Adjust,
OUT UINT64 *FixupData,
IN BOOLEAN IsRuntime
)
{
BOOLEAN Overflow;
Expand Down Expand Up @@ -497,8 +502,23 @@ InternalApplyRelocation (
// Relocate the target instruction.
//
FixupValue.Value32 = ReadUnaligned32 (Fixup);
//
// If the Image relocation target value mismatches, skip or abort.
//
// if (IsRuntime && (FixupValue.Value32 != (UINT32)*FixupData)) {
// if (PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch)) {
// return RETURN_SUCCESS;
// }
//
// return RETURN_VOLUME_CORRUPTED;
// }

FixupValue.Value32 += (UINT32) Adjust;
WriteUnaligned32 (Fixup, FixupValue.Value32);

// if (!IsRuntime) {
// *FixupData = FixupValue.Value32;
// }
} else {
ASSERT (RelocType == UeReloc64);

Expand All @@ -515,8 +535,23 @@ InternalApplyRelocation (
// Relocate target the instruction.
//
FixupValue.Value64 = ReadUnaligned64 (Fixup);
//
// If the Image relocation target value mismatches, skip or abort.
//
if (IsRuntime && (FixupValue.Value64 != *FixupData)) {
if (PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch)) {
return RETURN_SUCCESS;
}

return RETURN_VOLUME_CORRUPTED;
}

FixupValue.Value64 += Adjust;
WriteUnaligned64 (Fixup, FixupValue.Value64);

if (!IsRuntime) {
*FixupData = FixupValue.Value64;
}
}
} else {
#if 0
Expand Down Expand Up @@ -668,7 +703,9 @@ InternaRelocateImage (
IN CONST VOID *RelocTable,
IN UINT32 RelocTableSize,
IN BOOLEAN Chaining,
IN UINT64 BaseAddress
IN UINT64 BaseAddress,
OUT UINT64 *FixupData,
IN BOOLEAN IsRuntime
)
{
RETURN_STATUS Status;
Expand Down Expand Up @@ -769,8 +806,12 @@ InternaRelocateImage (
Machine,
RelocType,
&RelocTarget,
Adjust
Adjust,
FixupData,
IsRuntime
);

++FixupData;
}

if (RETURN_ERROR (Status)) {
Expand Down Expand Up @@ -851,6 +892,12 @@ UeRelocateImage (
Context->FileBuffer + Context->LoadTablesFileOffset,
Context->RelocTableSize
);

RuntimeContext->FixupSize = Context->RelocTableSize / sizeof (UINT16) * sizeof (UINT64);
RuntimeContext->FixupData = AllocateRuntimeZeroPool (RuntimeContext->FixupSize);
if (RuntimeContext->FixupData == NULL) {
ASSERT (FALSE);
}
}

return InternaRelocateImage (
Expand All @@ -861,7 +908,9 @@ UeRelocateImage (
RelocTable,
Context->RelocTableSize,
Chaining,
BaseAddress
BaseAddress,
RuntimeContext->FixupData,
FALSE
);
}

Expand All @@ -884,7 +933,9 @@ UeRelocateImageForRuntime (
RuntimeContext->RelocTable,
RuntimeContext->RelocTableSize,
FALSE,
BaseAddress
BaseAddress,
RuntimeContext->FixupData,
TRUE
);
}

Expand Down
2 changes: 1 addition & 1 deletion OvmfPkg/OvmfPkgIa32X64.fdf
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ FILE FV_IMAGE = 9E21FD93-9C72-4c15-8C4B-E77F1DB2D792 {
[Rule.Common.DXE_RUNTIME_DRIVER]
FILE DRIVER = $(NAMED_GUID) {
DXE_DEPEX DXE_DEPEX Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
PE32 PE32 $(INF_OUTPUT)/$(MODULE_NAME).efi
UE UE $(INF_OUTPUT)/$(MODULE_NAME).efi
UI STRING="$(MODULE_NAME)" Optional
VERSION STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
}
Expand Down
2 changes: 1 addition & 1 deletion OvmfPkg/OvmfPkgX64.fdf
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ FILE FV_IMAGE = 9E21FD93-9C72-4c15-8C4B-E77F1DB2D792 {
[Rule.Common.DXE_RUNTIME_DRIVER]
FILE DRIVER = $(NAMED_GUID) {
DXE_DEPEX DXE_DEPEX Optional $(INF_OUTPUT)/$(MODULE_NAME).depex
PE32 PE32 $(INF_OUTPUT)/$(MODULE_NAME).efi
UE UE $(INF_OUTPUT)/$(MODULE_NAME).efi
UI STRING="$(MODULE_NAME)" Optional
VERSION STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER)
}
Expand Down

0 comments on commit 9ff4f89

Please sign in to comment.