From 0ab6d8a99456f0314ca1d4497dfa1f185af9a2c4 Mon Sep 17 00:00:00 2001 From: jief666 Date: Mon, 12 Oct 2020 14:51:08 +0300 Subject: [PATCH] First unit test for memory Find-Replace. --- OpenCorePkg | 2 +- .../cpp_tests.xcodeproj/project.pbxproj | 72 ++++++- .../xcschemes/cpp_tests UTF16.xcscheme | 31 +-- Xcode/cpp_tests/src/Platform.cpp | 7 +- Xcode/cpp_tests/src/Platform.h | 20 +- Xcode/cpp_tests/src/XToolsConf.h | 5 +- rEFIt_UEFI/Platform/MemoryOperation.c | 198 ++++++++++++++++++ rEFIt_UEFI/Platform/MemoryOperation.h | 60 ++++++ rEFIt_UEFI/Platform/Posix/abort.cpp | 56 ++--- rEFIt_UEFI/Platform/kernel_patcher.cpp | 2 + rEFIt_UEFI/Platform/kernel_patcher.h | 21 -- rEFIt_UEFI/Platform/kext_inject.cpp | 5 +- rEFIt_UEFI/Platform/kext_patcher.cpp | 190 +---------------- rEFIt_UEFI/Platform/plist/plist.cpp | 2 +- rEFIt_UEFI/cpp_unit_test/all_tests.cpp | 150 +++++++------ .../find_replace_mask_Clover_tests.cpp | 86 ++++++++ .../find_replace_mask_Clover_tests.h | 1 + .../find_replace_mask_OC_tests.cpp | 99 +++++++++ .../find_replace_mask_OC_tests.h | 1 + rEFIt_UEFI/refit.inf | 6 + rEFIt_UEFI/refit/lib.cpp | 1 + 21 files changed, 666 insertions(+), 349 deletions(-) create mode 100644 rEFIt_UEFI/Platform/MemoryOperation.c create mode 100644 rEFIt_UEFI/Platform/MemoryOperation.h create mode 100755 rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.cpp create mode 100755 rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.h create mode 100755 rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.cpp create mode 100755 rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.h diff --git a/OpenCorePkg b/OpenCorePkg index b114709c9..f10d9d3ce 160000 --- a/OpenCorePkg +++ b/OpenCorePkg @@ -1 +1 @@ -Subproject commit b114709c9ae8f823951fda29a6d103523c1344a5 +Subproject commit f10d9d3ce19b84b553d65c1a8ed925a91354d8af diff --git a/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj b/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj index 65a2c995a..85a43eea8 100644 --- a/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj +++ b/Xcode/cpp_tests/cpp_tests.xcodeproj/project.pbxproj @@ -143,6 +143,19 @@ 9A670D1F24E535AB00B5D780 /* XBuffer_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A670D1B24E535AB00B5D780 /* XBuffer_tests.cpp */; }; 9A7D518424FC32F700FA1CC3 /* XBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A7D518124FC32F700FA1CC3 /* XBuffer.cpp */; }; 9A7D518524FC32F700FA1CC3 /* XRBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A7D518324FC32F700FA1CC3 /* XRBuffer.cpp */; }; + 9A838CA4253423F0008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */; }; + 9A838CA5253423F0008303F5 /* find_replace_mask_OC_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CA3253423F0008303F5 /* find_replace_mask_OC_tests.cpp */; }; + 9A838CA6253425A3008303F5 /* DataPatcher.c in Sources */ = {isa = PBXBuildFile; fileRef = 9A838A4A253423A5008303F5 /* DataPatcher.c */; settings = {COMPILER_FLAGS = "-w"; }; }; + 9A838CA7253425A3008303F5 /* DataPatcher.c in Sources */ = {isa = PBXBuildFile; fileRef = 9A838A4A253423A5008303F5 /* DataPatcher.c */; }; + 9A838CA8253425A4008303F5 /* DataPatcher.c in Sources */ = {isa = PBXBuildFile; fileRef = 9A838A4A253423A5008303F5 /* DataPatcher.c */; }; + 9A838CA9253425A4008303F5 /* DataPatcher.c in Sources */ = {isa = PBXBuildFile; fileRef = 9A838A4A253423A5008303F5 /* DataPatcher.c */; }; + 9A838CAC25342626008303F5 /* MemoryOperation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CAB25342626008303F5 /* MemoryOperation.cpp */; }; + 9A838CAD25342629008303F5 /* MemoryOperation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CAB25342626008303F5 /* MemoryOperation.cpp */; }; + 9A838CAE2534262A008303F5 /* MemoryOperation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CAB25342626008303F5 /* MemoryOperation.cpp */; }; + 9A838CAF2534262A008303F5 /* MemoryOperation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CAB25342626008303F5 /* MemoryOperation.cpp */; }; + 9A838CB025345E93008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */; }; + 9A838CB125345E93008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */; }; + 9A838CB225345E94008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */; }; 9A9223312402FD1000483CBA /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9223302402FD1000483CBA /* main.cpp */; }; 9A9AEB8D243F73CE00FBD7D8 /* unicode_conversions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9AEB8C243F73CE00FBD7D8 /* unicode_conversions.cpp */; }; 9A9AEB8E243F752C00FBD7D8 /* unicode_conversions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9A9AEB8C243F73CE00FBD7D8 /* unicode_conversions.cpp */; }; @@ -273,6 +286,13 @@ 9A7D518124FC32F700FA1CC3 /* XBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XBuffer.cpp; sourceTree = ""; }; 9A7D518224FC32F700FA1CC3 /* XRBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XRBuffer.h; sourceTree = ""; }; 9A7D518324FC32F700FA1CC3 /* XRBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = XRBuffer.cpp; sourceTree = ""; }; + 9A838A4A253423A5008303F5 /* DataPatcher.c */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp.preprocessed; path = DataPatcher.c; sourceTree = ""; }; + 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = find_replace_mask_Clover_tests.cpp; sourceTree = ""; }; + 9A838CA1253423F0008303F5 /* find_replace_mask_OC_tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = find_replace_mask_OC_tests.h; sourceTree = ""; }; + 9A838CA2253423F0008303F5 /* find_replace_mask_Clover_tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = find_replace_mask_Clover_tests.h; sourceTree = ""; }; + 9A838CA3253423F0008303F5 /* find_replace_mask_OC_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = find_replace_mask_OC_tests.cpp; sourceTree = ""; }; + 9A838CAA25342626008303F5 /* MemoryOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryOperation.h; sourceTree = ""; }; + 9A838CAB25342626008303F5 /* MemoryOperation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryOperation.cpp; sourceTree = ""; }; 9A92232D2402FD1000483CBA /* cpp_tests UTF16 signed char */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "cpp_tests UTF16 signed char"; sourceTree = BUILT_PRODUCTS_DIR; }; 9A9223302402FD1000483CBA /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 9A92234D2402FD9500483CBA /* Platform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Platform.h; sourceTree = ""; }; @@ -358,6 +378,10 @@ 9A0B084C2402FE9B00E2B470 /* cpp_unit_test */ = { isa = PBXGroup; children = ( + 9A838CA0253423F0008303F5 /* find_replace_mask_Clover_tests.cpp */, + 9A838CA2253423F0008303F5 /* find_replace_mask_Clover_tests.h */, + 9A838CA3253423F0008303F5 /* find_replace_mask_OC_tests.cpp */, + 9A838CA1253423F0008303F5 /* find_replace_mask_OC_tests.h */, 9A36E52A24F3C845007A1107 /* plist_tests.cpp */, 9A36E52B24F3C846007A1107 /* plist_tests.h */, 9A0B08512402FE9B00E2B470 /* all_tests.cpp */, @@ -409,6 +433,8 @@ 9A28CCAC241B816400F3D247 /* Platform */ = { isa = PBXGroup; children = ( + 9A838CAB25342626008303F5 /* MemoryOperation.cpp */, + 9A838CAA25342626008303F5 /* MemoryOperation.h */, 9A36E51E24F3B82A007A1107 /* b64cdecode.cpp */, 9A36E51D24F3B82A007A1107 /* b64cdecode.h */, 9A36E4D924F3B51C007A1107 /* plist */, @@ -480,9 +506,35 @@ path = libeg; sourceTree = ""; }; + 9A83876E253423A4008303F5 /* OpenCorePkg */ = { + isa = PBXGroup; + children = ( + 9A8389CD253423A5008303F5 /* Library */, + ); + name = OpenCorePkg; + path = ../../OpenCorePkg; + sourceTree = ""; + }; + 9A8389CD253423A5008303F5 /* Library */ = { + isa = PBXGroup; + children = ( + 9A838A44253423A5008303F5 /* OcMiscLib */, + ); + path = Library; + sourceTree = ""; + }; + 9A838A44253423A5008303F5 /* OcMiscLib */ = { + isa = PBXGroup; + children = ( + 9A838A4A253423A5008303F5 /* DataPatcher.c */, + ); + path = OcMiscLib; + sourceTree = ""; + }; 9A9223242402FD1000483CBA = { isa = PBXGroup; children = ( + 9A83876E253423A4008303F5 /* OpenCorePkg */, 9A36E51C24F3B5B4007A1107 /* rEFIt_UEFI */, 9A1A3FD82424BC22008C89EB /* MemLogLibDefault */, 9A92232F2402FD1000483CBA /* Main */, @@ -653,6 +705,7 @@ files = ( 9A36E50524F3B537007A1107 /* TagInt64.cpp in Sources */, 9A36E50124F3B537007A1107 /* TagDict.cpp in Sources */, + 9A838CAD25342629008303F5 /* MemoryOperation.cpp in Sources */, 9A36E50D24F3B537007A1107 /* TagData.cpp in Sources */, 9A4185B22439E4D600BEAFB8 /* LoadOptions_test.cpp in Sources */, 9A36E52D24F3C846007A1107 /* plist_tests.cpp in Sources */, @@ -664,6 +717,7 @@ 9A36E51924F3B537007A1107 /* TagKey.cpp in Sources */, 9A9EA7F9245AAB310076EC02 /* XToolsCommon_test.cpp in Sources */, 9A36E52724F3BB6B007A1107 /* FloatLib.cpp in Sources */, + 9A838CB125345E93008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */, 9A28CD23241BB61B00F3D247 /* strlen.cpp in Sources */, 9A28CD4C241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */, 9A0B08742403B08400E2B470 /* main.cpp in Sources */, @@ -676,6 +730,7 @@ 9A36E4FD24F3B537007A1107 /* TagBool.cpp in Sources */, 9A36E4F524F3B537007A1107 /* plist.cpp in Sources */, 9ACAB1192426255C00BDB3CF /* printf_lite.c in Sources */, + 9A838CA7253425A3008303F5 /* DataPatcher.c in Sources */, 9A36E4F924F3B537007A1107 /* xml.cpp in Sources */, 9A0B087D2403B08400E2B470 /* XStringArray_test.cpp in Sources */, 9A36E51124F3B537007A1107 /* TagArray.cpp in Sources */, @@ -697,6 +752,7 @@ files = ( 9A36E50724F3B537007A1107 /* TagInt64.cpp in Sources */, 9A36E50324F3B537007A1107 /* TagDict.cpp in Sources */, + 9A838CAF2534262A008303F5 /* MemoryOperation.cpp in Sources */, 9A36E50F24F3B537007A1107 /* TagData.cpp in Sources */, 9A2A7C6C24576CCE00422263 /* LoadOptions_test.cpp in Sources */, 9A36E52F24F3C846007A1107 /* plist_tests.cpp in Sources */, @@ -708,6 +764,7 @@ 9A36E51B24F3B537007A1107 /* TagKey.cpp in Sources */, 9A9EA7FB245AAB310076EC02 /* XToolsCommon_test.cpp in Sources */, 9A36E52924F3BB6B007A1107 /* FloatLib.cpp in Sources */, + 9A838CB225345E94008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */, 9A2A7C7124576CCE00422263 /* strlen.cpp in Sources */, 9A2A7C7224576CCE00422263 /* xcode_utf_fixed.cpp in Sources */, 9A2A7C7324576CCE00422263 /* main.cpp in Sources */, @@ -720,6 +777,7 @@ 9A36E4FF24F3B537007A1107 /* TagBool.cpp in Sources */, 9A36E4F724F3B537007A1107 /* plist.cpp in Sources */, 9A2A7C7A24576CCE00422263 /* printf_lite.c in Sources */, + 9A838CA9253425A4008303F5 /* DataPatcher.c in Sources */, 9A36E4FB24F3B537007A1107 /* xml.cpp in Sources */, 9A2A7C7B24576CCE00422263 /* XStringArray_test.cpp in Sources */, 9A36E51324F3B537007A1107 /* TagArray.cpp in Sources */, @@ -741,6 +799,7 @@ files = ( 9A36E50624F3B537007A1107 /* TagInt64.cpp in Sources */, 9A36E50224F3B537007A1107 /* TagDict.cpp in Sources */, + 9A838CAE2534262A008303F5 /* MemoryOperation.cpp in Sources */, 9A36E50E24F3B537007A1107 /* TagData.cpp in Sources */, 9A4185B32439E4D600BEAFB8 /* LoadOptions_test.cpp in Sources */, 9A36E52E24F3C846007A1107 /* plist_tests.cpp in Sources */, @@ -752,6 +811,7 @@ 9A36E51A24F3B537007A1107 /* TagKey.cpp in Sources */, 9A9EA7FA245AAB310076EC02 /* XToolsCommon_test.cpp in Sources */, 9A36E52824F3BB6B007A1107 /* FloatLib.cpp in Sources */, + 9A838CB025345E93008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */, 9A28CD24241BB61B00F3D247 /* strlen.cpp in Sources */, 9A28CD4D241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */, 9A57C21B2418B9A00029A39F /* main.cpp in Sources */, @@ -764,6 +824,7 @@ 9A36E4FE24F3B537007A1107 /* TagBool.cpp in Sources */, 9A36E4F624F3B537007A1107 /* plist.cpp in Sources */, 9ACAB11A2426255C00BDB3CF /* printf_lite.c in Sources */, + 9A838CA8253425A4008303F5 /* DataPatcher.c in Sources */, 9A36E4FA24F3B537007A1107 /* xml.cpp in Sources */, 9A57C2252418B9A00029A39F /* XStringArray_test.cpp in Sources */, 9A36E51224F3B537007A1107 /* TagArray.cpp in Sources */, @@ -783,6 +844,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9A838CAC25342626008303F5 /* MemoryOperation.cpp in Sources */, 9A36E50424F3B537007A1107 /* TagInt64.cpp in Sources */, 9A36E50024F3B537007A1107 /* TagDict.cpp in Sources */, 9A36E50C24F3B537007A1107 /* TagData.cpp in Sources */, @@ -794,9 +856,11 @@ 9A4FFA7E2451C8330050B38B /* XString.cpp in Sources */, 9A4FFA812451C88D0050B38B /* XString_test.cpp in Sources */, 9A28CD22241BB61B00F3D247 /* strlen.cpp in Sources */, + 9A838CA6253425A3008303F5 /* DataPatcher.c in Sources */, 9A36E51824F3B537007A1107 /* TagKey.cpp in Sources */, 9A9EA7F8245AAB310076EC02 /* XToolsCommon_test.cpp in Sources */, 9A36E52624F3BB6B007A1107 /* FloatLib.cpp in Sources */, + 9A838CA4253423F0008303F5 /* find_replace_mask_Clover_tests.cpp in Sources */, 9A28CD4B241F4CCE00F3D247 /* xcode_utf_fixed.cpp in Sources */, 9A9223312402FD1000483CBA /* main.cpp in Sources */, 9A28CD16241BACBB00F3D247 /* strlen_test.cpp in Sources */, @@ -811,6 +875,7 @@ 9A0B08582402FF7F00E2B470 /* XStringArray_test.cpp in Sources */, 9A36E4F824F3B537007A1107 /* xml.cpp in Sources */, 9A9AEB8D243F73CE00FBD7D8 /* unicode_conversions.cpp in Sources */, + 9A838CA5253423F0008303F5 /* find_replace_mask_OC_tests.cpp in Sources */, 9A7D518524FC32F700FA1CC3 /* XRBuffer.cpp in Sources */, 9A4185C02439F73A00BEAFB8 /* XStringArray.cpp in Sources */, 9A36E51024F3B537007A1107 /* TagArray.cpp in Sources */, @@ -951,7 +1016,10 @@ GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREFIX_HEADER = ""; - GCC_PREPROCESSOR_DEFINITIONS = JIEF_DEBUG; + GCC_PREPROCESSOR_DEFINITIONS = ( + JIEF_DEBUG, + UNIT_TESTS, + ); GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; @@ -1037,7 +1105,7 @@ GCC_C_LANGUAGE_STANDARD = gnu11; GCC_NO_COMMON_BLOCKS = YES; GCC_PREFIX_HEADER = ""; - GCC_PREPROCESSOR_DEFINITIONS = ""; + GCC_PREPROCESSOR_DEFINITIONS = UNIT_TESTS; GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; diff --git a/Xcode/cpp_tests/cpp_tests.xcodeproj/xcshareddata/xcschemes/cpp_tests UTF16.xcscheme b/Xcode/cpp_tests/cpp_tests.xcodeproj/xcshareddata/xcschemes/cpp_tests UTF16.xcscheme index 8a3a4ed74..ddb446c51 100644 --- a/Xcode/cpp_tests/cpp_tests.xcodeproj/xcshareddata/xcschemes/cpp_tests UTF16.xcscheme +++ b/Xcode/cpp_tests/cpp_tests.xcodeproj/xcshareddata/xcschemes/cpp_tests UTF16.xcscheme @@ -43,12 +43,12 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - enableUBSanitizer = "YES" + disableMainThreadChecker = "YES" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" debugDocumentVersioning = "YES" - stopOnEveryMainThreadCheckerIssue = "YES" + migratedStopOnEveryIssue = "YES" debugServiceExtension = "internal" allowLocationSimulation = "YES"> - - - - - - - - - - - - #include "posix.h" -#include "../../../rEFIt_UEFI/Platform/Posix/abort.h" -#include "../../../rEFIt_UEFI/cpp_foundation/unicode_conversions.h" -#include "../../../rEFIt_UEFI/cpp_foundation/XString.h" -#include "../../../rEFIt_UEFI/cpp_foundation/XObjArray.h" - -#include "xcode_utf_fixed.h" - - #ifndef __cplusplus //typedef uint16_t wchar_t; typedef uint32_t char32_t; @@ -149,6 +141,15 @@ typedef RETURN_STATUS EFI_STATUS; #define __typeof__(x) decltype(x) #endif + +#include "../../../rEFIt_UEFI/Platform/Posix/abort.h" +#include "../../../rEFIt_UEFI/cpp_foundation/unicode_conversions.h" +#include "../../../rEFIt_UEFI/cpp_foundation/XString.h" +#include "../../../rEFIt_UEFI/cpp_foundation/XObjArray.h" + +#include "xcode_utf_fixed.h" + + void CpuDeadLoop(void); void DebugLog(INTN DebugMode, const char *FormatString, ...); #define MsgLog ::printf @@ -190,7 +191,8 @@ void FreePool(const void* Buffer); void ZeroMem(void *Destination, UINTN Length); void SetMem(void *Destination, UINTN Length, char c); -void CopyMem(void *Destination, void *Source, UINTN Length); +void CopyMem(void *Destination, const void *Source, UINTN Length); +INTN CompareMem(const void* DestinationBuffer, const void* SourceBuffer, UINTN Length); CHAR16* EfiStrDuplicate (IN CONST CHAR16 *Src); CHAR16* StrStr (IN CONST CHAR16 *String, IN CONST CHAR16 *SearchString); diff --git a/Xcode/cpp_tests/src/XToolsConf.h b/Xcode/cpp_tests/src/XToolsConf.h index 9f9c00415..46bd8420c 100755 --- a/Xcode/cpp_tests/src/XToolsConf.h +++ b/Xcode/cpp_tests/src/XToolsConf.h @@ -1,8 +1,6 @@ #ifndef __XTOOLSCONF_H__ #define __XTOOLSCONF_H__ -#include - #define xsize size_t //#define xisize INTN @@ -25,6 +23,9 @@ #define Xrealloc(ptr, newsize, oldsize) realloc(ptr, newsize) +#include +#include + #endif diff --git a/rEFIt_UEFI/Platform/MemoryOperation.c b/rEFIt_UEFI/Platform/MemoryOperation.c new file mode 100644 index 000000000..d7056492e --- /dev/null +++ b/rEFIt_UEFI/Platform/MemoryOperation.c @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2011-2012 Frank Peng. All rights reserved. + * + */ + +#include "MemoryOperation.h" + +#include +#include +#include + + +// +// Searches Source for Search pattern of size SearchSize +// and returns the number of occurences. +// +UINTN SearchAndCount(const UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize) +{ + UINTN NumFounds = 0; + const UINT8 *End = Source + SourceSize; + + while (Source < End) { + if (CompareMem(Source, Search, SearchSize) == 0) { + NumFounds++; + Source += SearchSize; + } else { + Source++; + } + } + return NumFounds; +} + +// +// Searches Source for Search pattern of size SearchSize +// and replaces it with Replace up to MaxReplaces times. +// If MaxReplaces <= 0, then there is no restriction on number of replaces. +// Replace should have the same size as Search. +// Returns number of replaces done. +// +UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces) +{ + UINTN NumReplaces = 0; + BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; +// UINT8 *Begin = Source; + UINT8 *End = Source + SourceSize; + if (!Source || !Search || !Replace || !SearchSize) { + return 0; + } + + while ((Source < End) && (NoReplacesRestriction || (MaxReplaces > 0))) { + if (CompareMem(Source, Search, SearchSize) == 0) { + // printf(" found pattern at %llx\n", (UINTN)(Source - Begin)); + CopyMem(Source, Replace, SearchSize); + NumReplaces++; + MaxReplaces--; + Source += SearchSize; + } else { + Source++; + } + } + return NumReplaces; +} + +BOOLEAN CompareMemMask(const UINT8 *Source, const UINT8 *Search, UINTN SearchSize, const UINT8 *Mask, UINTN MaskSize) +{ + UINT8 M; + + if (!Mask || MaskSize == 0) { + return !CompareMem(Source, Search, SearchSize); + } + for (UINTN Ind = 0; Ind < SearchSize; Ind++) { + if (Ind < MaskSize) + M = *Mask++; + else M = 0xFF; + if ((*Source++ & M) != (*Search++ & M)) { + return FALSE; + } + } + return TRUE; +} + +void CopyMemMask(UINT8 *Dest, const UINT8 *Replace, const UINT8 *Mask, UINTN SearchSize) +{ + UINT8 M, D; + // the procedure is called from SearchAndReplaceMask with own check but for future it is better to check twice + if (!Dest || !Replace) { + return; + } + + if (!Mask) { + CopyMem(Dest, Replace, SearchSize); //old behavior + return; + } + for (UINTN Ind = 0; Ind < SearchSize; Ind++) { + M = *Mask++; + D = *Dest; + *Dest++ = ((D ^ *Replace++) & M) ^ D; + } +} + +UINTN FindMemMask(const UINT8 *Source, UINTN SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *MaskSearch, UINTN MaskSize) +{ + if (!Source || !Search || !SearchSize) { + return MAX_UINTN; + } + + for (UINTN i = 0; i < SourceSize - SearchSize; ++i) { + if (CompareMemMask(&Source[i], Search, SearchSize, MaskSearch, MaskSize)) { + return i; + } + } + return MAX_UINTN; +} + +UINTN SearchAndReplaceMask(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, const UINT8 *MaskSearch, UINTN SearchSize, + const UINT8 *Replace, const UINT8 *MaskReplace, INTN MaxReplaces) +{ + UINTN NumReplaces = 0; + BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; + UINT8 *End = Source + SourceSize; + if (!Source || !Search || !Replace || !SearchSize) { + return 0; + } + while ((Source < End) && (NoReplacesRestriction || (MaxReplaces > 0))) { + if (CompareMemMask((const UINT8 *)Source, Search, SearchSize, MaskSearch, SearchSize)) { + CopyMemMask(Source, Replace, MaskReplace, SearchSize); + NumReplaces++; + MaxReplaces--; + Source += SearchSize; + } else { + Source++; + } + + } + + return NumReplaces; +} + + +UINTN SearchAndReplaceTxt(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces) +{ + UINTN NumReplaces = 0; + UINTN Skip = 0; + BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; + UINT8 *End = Source + SourceSize; + const UINT8 *SearchEnd = Search + SearchSize; + const UINT8 *Pos = NULL; + UINT8 *SourcePos = NULL; + UINT8 *FirstMatch = Source; + if (!Source || !Search || !Replace || !SearchSize) { + return 0; + } + + while (((Source + SearchSize) <= End) && + (NoReplacesRestriction || (MaxReplaces > 0))) { // num replaces + while (*Source != '\0') { //comparison + Pos = Search; + FirstMatch = Source; + Skip = 0; + while (*Source != '\0' && Pos != SearchEnd) { + if (*Source <= 0x20) { //skip invisibles in sources + Source++; + Skip++; + continue; + } + if (*Source != *Pos) { + break; + } + // printf("%c", *Source); + Source++; + Pos++; + } + + if (Pos == SearchEnd) { // pattern found + SourcePos = FirstMatch; + break; + } + else + SourcePos = NULL; + + Source = FirstMatch + 1; +/* if (Pos != Search) { + printf("\n"); + } */ + + } + + if (!SourcePos) { + break; + } + CopyMem(SourcePos, Replace, SearchSize); + SetMem(SourcePos + SearchSize, Skip, 0x20); //fill skip places with spaces + NumReplaces++; + MaxReplaces--; + Source = FirstMatch + SearchSize + Skip; + } + return NumReplaces; +} diff --git a/rEFIt_UEFI/Platform/MemoryOperation.h b/rEFIt_UEFI/Platform/MemoryOperation.h new file mode 100644 index 000000000..39fd00065 --- /dev/null +++ b/rEFIt_UEFI/Platform/MemoryOperation.h @@ -0,0 +1,60 @@ +/* + * MemoryOperation.h + * + * Created on: Oct 12, 2020 + * Author: jief + */ + + +#ifndef MEMORYOPERATION_H_ +#define MEMORYOPERATION_H_ + + + + + +#ifdef __cplusplus +extern "C" { +#endif + + +//#include +//#include +//#include + + +// +// Searches Source for Search pattern of size SearchSize +// and returns the number of occurences. +// +UINTN SearchAndCount(const UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize); + +// +// Searches Source for Search pattern of size SearchSize +// and replaces it with Replace up to MaxReplaces times. +// If MaxReplaces <= 0, then there is no restriction on number of replaces. +// Replace should have the same size as Search. +// Returns number of replaces done. +// +UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces); + +BOOLEAN CompareMemMask(const UINT8 *Source, const UINT8 *Search, UINTN SearchSize, const UINT8 *Mask, UINTN MaskSize); + +void CopyMemMask(UINT8 *Dest, const UINT8 *Replace, const UINT8 *Mask, UINTN SearchSize); + +UINTN FindMemMask(const UINT8 *Source, UINTN SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *MaskSearch, UINTN MaskSize); + +UINTN SearchAndReplaceMask(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, const UINT8 *MaskSearch, UINTN SearchSize, + const UINT8 *Replace, const UINT8 *MaskReplace, INTN MaxReplaces); + + +UINTN SearchAndReplaceTxt(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces); + + +#ifdef __cplusplus +} +#endif + + + +#endif /* MEMORYOPERATION_H_ */ diff --git a/rEFIt_UEFI/Platform/Posix/abort.cpp b/rEFIt_UEFI/Platform/Posix/abort.cpp index 79bab77c2..307f89279 100755 --- a/rEFIt_UEFI/Platform/Posix/abort.cpp +++ b/rEFIt_UEFI/Platform/Posix/abort.cpp @@ -5,10 +5,10 @@ #if defined(CLOVER_BUILD) || !defined(_MSC_VER) void abort(void) { - printf("A fatal error happened. System halted\n"); - while (1) { // tis will avoid warning : Function declared 'noreturn' should not return - CpuDeadLoop(); - } + printf("A fatal error happened. System halted\n"); + while (1) { // tis will avoid warning : Function declared 'noreturn' should not return + CpuDeadLoop(); + } } #endif @@ -25,40 +25,44 @@ static void panic_(const char* format, VA_LIST va) #endif ; +#ifdef CLOVER_BUILD extern void egSetGraphicsModeEnabled(BOOLEAN); +#endif #define FATAL_ERROR_MSG "\nA fatal error happened. System halted.\n" static void panic_(const char* format, VA_LIST va) { -egSetGraphicsModeEnabled(false); - if ( format ) { - vprintf(format, va); - #ifdef DEBUG_ON_SERIAL_PORT - char buf[500]; - vsnprintf(buf, sizeof(buf)-1, format, va); - SerialPortWrite((UINT8*)buf, strlen(buf)); - #endif - } - printf(FATAL_ERROR_MSG); +#ifdef CLOVER_BUILD + egSetGraphicsModeEnabled(false); +#endif + if ( format ) { + vprintf(format, va); + #ifdef DEBUG_ON_SERIAL_PORT + char buf[500]; + vsnprintf(buf, sizeof(buf)-1, format, va); + SerialPortWrite((UINT8*)buf, strlen(buf)); + #endif + } + printf(FATAL_ERROR_MSG); #ifdef DEBUG_ON_SERIAL_PORT SerialPortWrite((UINT8*)FATAL_ERROR_MSG, strlen(FATAL_ERROR_MSG)); #endif - while (1) { // this will avoid warning : Function declared 'noreturn' should not return - CpuDeadLoop(); - } + while (1) { // this will avoid warning : Function declared 'noreturn' should not return + CpuDeadLoop(); + } } void panic(const char* format, ...) { #ifdef PANIC_CAN_RETURN - if ( stop_at_panic ) { - VA_LIST va; - VA_START(va, format); - panic_(format, va); // panic doesn't return -// VA_END(va); - }else{ - i_have_panicked = true; - } + if ( stop_at_panic ) { + VA_LIST va; + VA_START(va, format); + panic_(format, va); // panic doesn't return +// VA_END(va); + }else{ + i_have_panicked = true; + } #else VA_LIST va; VA_START(va, format); @@ -69,5 +73,5 @@ void panic(const char* format, ...) void panic(void) { - panic(nullptr); + panic(nullptr); } diff --git a/rEFIt_UEFI/Platform/kernel_patcher.cpp b/rEFIt_UEFI/Platform/kernel_patcher.cpp index 2f0a4749e..3c4405cad 100644 --- a/rEFIt_UEFI/Platform/kernel_patcher.cpp +++ b/rEFIt_UEFI/Platform/kernel_patcher.cpp @@ -19,6 +19,8 @@ #include "kext_inject.h" #include "kernel_patcher.h" +#include "MemoryOperation.h" + //#include "sse3_patcher.h" //#include "sse3_5_patcher.h" diff --git a/rEFIt_UEFI/Platform/kernel_patcher.h b/rEFIt_UEFI/Platform/kernel_patcher.h index b7b70e8e7..5765c8608 100644 --- a/rEFIt_UEFI/Platform/kernel_patcher.h +++ b/rEFIt_UEFI/Platform/kernel_patcher.h @@ -178,28 +178,7 @@ extern LIST_ENTRY gKextList; // Jief : globals variables... not great. // //void KextPatcherStart(LOADER_ENTRY *Entry); -// -// Searches Source for Search pattern of size SearchSize -// and returns the number of occurences. -// -UINTN SearchAndCount(const UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize); - -BOOLEAN CompareMemMask(const UINT8 *Source, const UINT8 *Search, UINTN SearchSize, const UINT8 *Mask, UINTN MaskSize); -void CopyMemMask(UINT8 *Dest, const UINT8 *Replace, const UINT8 *Mask, UINTN SearchSize); -UINTN FindMemMask(const UINT8 *Source, UINTN SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *MaskSearch, UINTN MaskSize); UINTN FindRelative32(const UINT8 *Source, UINTN Start, UINTN SourceSize, UINTN taskLocation); -//UINTN FindSection(const UINT8 *Source, UINTN len, const UINT8* seg, const UINT8* sec); -// -// Searches Source for Search pattern of size SearchSize -// and replaces it with Replace up to MaxReplaces times. -// If MaxReplaces <= 0, then there is no restriction on number of replaces. -// Replace should have the same size as Search. -// Returns number of replaces done. -// -UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces); - -UINTN SearchAndReplaceMask(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, const UINT8 *MaskSearch, UINTN SearchSize, - const UINT8 *Replace, const UINT8 *MaskReplace, INTN MaxReplaces); #endif /* !__LIBSAIO_KERNEL_PATCHER_H */ diff --git a/rEFIt_UEFI/Platform/kext_inject.cpp b/rEFIt_UEFI/Platform/kext_inject.cpp index 865cd754b..d2edb5a1d 100644 --- a/rEFIt_UEFI/Platform/kext_inject.cpp +++ b/rEFIt_UEFI/Platform/kext_inject.cpp @@ -10,6 +10,7 @@ extern "C" { #include "../Platform/plist/plist.h" #include "../Platform/Settings.h" #include "../Platform/guid.h" +#include "MemoryOperation.h" #ifndef DEBUG_ALL #define KEXT_INJECT_DEBUG 1 @@ -1127,7 +1128,7 @@ void EFIAPI LOADER_ENTRY::KernelBooterExtensionsPatch() patchLocation2 = FindMemMask(&KernelData[procLocation], 0x500, find3, sizeof(find3), mask3, sizeof(mask3)); DBG("IOTaskHasEntitlement at 0x%llx, loadExecutable at 0x%llx\n", taskLocation, procLocation); DBG("find3 at 0x%llx\n", patchLocation2); - if (patchLocation2 != KERNEL_MAX_SIZE) { + if (patchLocation2 != MAX_UINTN) { DBG_RT("=> patch SIP applied\n"); patchLocation2 += procLocation; KernelData[patchLocation2 + 3] = 0xEB; @@ -1247,7 +1248,7 @@ void EFIAPI LOADER_ENTRY::KernelBooterExtensionsPatch() } else { */ //The patch is not needed for bigsur - if (patchLocation3 != KERNEL_MAX_SIZE) { + if (patchLocation3 != MAX_UINTN) { DBG("==> patched KxldUnmap (10.14 - 10.15)\n"); // 00 0F 85 XX XX 00 00 48 // 00 90 E9 XX XX 00 00 48 diff --git a/rEFIt_UEFI/Platform/kext_patcher.cpp b/rEFIt_UEFI/Platform/kext_patcher.cpp index 33dd218f3..6e0b28917 100644 --- a/rEFIt_UEFI/Platform/kext_patcher.cpp +++ b/rEFIt_UEFI/Platform/kext_patcher.cpp @@ -21,6 +21,7 @@ extern "C" { #include "kernel_patcher.h" #include "kext_inject.h" #include "../gui/menu_items/menu_items.h" +#include "MemoryOperation.h" #define OLD_METHOD 0 @@ -40,97 +41,6 @@ extern "C" { #endif -// runtime debug -//#define DBG_RT(...) if ( KernelAndKextPatches.KPDebug ) { printf(__VA_ARGS__); } - -// -// Searches Source for Search pattern of size SearchSize -// and returns the number of occurences. -// -UINTN SearchAndCount(const UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize) -{ - UINTN NumFounds = 0; - const UINT8 *End = Source + SourceSize; - - while (Source < End) { - if (CompareMem(Source, Search, SearchSize) == 0) { - NumFounds++; - Source += SearchSize; - } else { - Source++; - } - } - return NumFounds; -} - -// -// Searches Source for Search pattern of size SearchSize -// and replaces it with Replace up to MaxReplaces times. -// If MaxReplaces <= 0, then there is no restriction on number of replaces. -// Replace should have the same size as Search. -// Returns number of replaces done. -// -UINTN SearchAndReplace(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces) -{ - UINTN NumReplaces = 0; - BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; -// UINT8 *Begin = Source; - UINT8 *End = Source + SourceSize; - if (!Source || !Search || !Replace || !SearchSize) { - return 0; - } - - while ((Source < End) && (NoReplacesRestriction || (MaxReplaces > 0))) { - if (CompareMem(Source, Search, SearchSize) == 0) { - // printf(" found pattern at %llx\n", (UINTN)(Source - Begin)); - CopyMem(Source, Replace, SearchSize); - NumReplaces++; - MaxReplaces--; - Source += SearchSize; - } else { - Source++; - } - } - return NumReplaces; -} - -BOOLEAN CompareMemMask(const UINT8 *Source, const UINT8 *Search, UINTN SearchSize, const UINT8 *Mask, UINTN MaskSize) -{ - UINT8 M; - - if (!Mask || MaskSize == 0) { - return !CompareMem(Source, Search, SearchSize); - } - for (UINTN Ind = 0; Ind < SearchSize; Ind++) { - if (Ind < MaskSize) - M = *Mask++; - else M = 0xFF; - if ((*Source++ & M) != (*Search++ & M)) { - return FALSE; - } - } - return TRUE; -} - -void CopyMemMask(UINT8 *Dest, const UINT8 *Replace, const UINT8 *Mask, UINTN SearchSize) -{ - UINT8 M, D; - // the procedure is called from SearchAndReplaceMask with own check but for future it is better to check twice - if (!Dest || !Replace) { - return; - } - - if (!Mask) { - CopyMem(Dest, Replace, SearchSize); //old behavior - return; - } - for (UINTN Ind = 0; Ind < SearchSize; Ind++) { - M = *Mask++; - D = *Dest; - *Dest++ = ((D ^ *Replace++) & M) ^ D; - } -} - // search a pattern like // call task or jmp address //return the address next to the command @@ -173,104 +83,6 @@ UINTN FindSection(const UINT8 *Source, UINTN len, const UINT8* seg, const UINT8* return 0; } */ -UINTN FindMemMask(const UINT8 *Source, UINTN SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *MaskSearch, UINTN MaskSize) -{ - if (!Source || !Search || !SearchSize) { - return KERNEL_MAX_SIZE; - } - - for (UINTN i = 0; i < SourceSize - SearchSize; ++i) { - if (CompareMemMask(&Source[i], Search, SearchSize, MaskSearch, MaskSize)) { - return i; - } - } - return KERNEL_MAX_SIZE; -} - -UINTN SearchAndReplaceMask(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, const UINT8 *MaskSearch, UINTN SearchSize, - const UINT8 *Replace, const UINT8 *MaskReplace, INTN MaxReplaces) -{ - UINTN NumReplaces = 0; - BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; - UINT8 *End = Source + SourceSize; - if (!Source || !Search || !Replace || !SearchSize) { - return 0; - } - while ((Source < End) && (NoReplacesRestriction || (MaxReplaces > 0))) { - if (CompareMemMask((const UINT8 *)Source, Search, SearchSize, MaskSearch, SearchSize)) { - CopyMemMask(Source, Replace, MaskReplace, SearchSize); - NumReplaces++; - MaxReplaces--; - Source += SearchSize; - } else { - Source++; - } - - } - - return NumReplaces; -} - - -UINTN SearchAndReplaceTxt(UINT8 *Source, UINT64 SourceSize, const UINT8 *Search, UINTN SearchSize, const UINT8 *Replace, INTN MaxReplaces) -{ - UINTN NumReplaces = 0; - UINTN Skip = 0; - BOOLEAN NoReplacesRestriction = MaxReplaces <= 0; - UINT8 *End = Source + SourceSize; - const UINT8 *SearchEnd = Search + SearchSize; - const UINT8 *Pos = NULL; - UINT8 *SourcePos = NULL; - UINT8 *FirstMatch = Source; - if (!Source || !Search || !Replace || !SearchSize) { - return 0; - } - - while (((Source + SearchSize) <= End) && - (NoReplacesRestriction || (MaxReplaces > 0))) { // num replaces - while (*Source != '\0') { //comparison - Pos = Search; - FirstMatch = Source; - Skip = 0; - while (*Source != '\0' && Pos != SearchEnd) { - if (*Source <= 0x20) { //skip invisibles in sources - Source++; - Skip++; - continue; - } - if (*Source != *Pos) { - break; - } - // printf("%c", *Source); - Source++; - Pos++; - } - - if (Pos == SearchEnd) { // pattern found - SourcePos = FirstMatch; - break; - } - else - SourcePos = NULL; - - Source = FirstMatch + 1; -/* if (Pos != Search) { - printf("\n"); - } */ - - } - - if (!SourcePos) { - break; - } - CopyMem(SourcePos, Replace, SearchSize); - SetMem(SourcePos + SearchSize, Skip, 0x20); //fill skip places with spaces - NumReplaces++; - MaxReplaces--; - Source = FirstMatch + SearchSize + Skip; - } - return NumReplaces; -} /** Global for storing KextBundleIdentifier */ CHAR8 gKextBundleIdentifier[256]; diff --git a/rEFIt_UEFI/Platform/plist/plist.cpp b/rEFIt_UEFI/Platform/plist/plist.cpp index 235a4501e..a14aa2ada 100755 --- a/rEFIt_UEFI/Platform/plist/plist.cpp +++ b/rEFIt_UEFI/Platform/plist/plist.cpp @@ -586,7 +586,7 @@ EFI_STATUS ParseTagInteger(CHAR8* buffer, TagStruct** tag, UINT32* lenPtr) integer = (integer * 16) + (*val++ - 'a' + 10); } else if ((*val >= 'A' && *val <= 'F')) { // A - F - integer = (integer * 16) + (*val++ - 'a' + 10); + integer = (integer * 16) + (*val++ - 'A' + 10); } else { MsgLog("ParseTagInteger hex error (0x%hhX) in buffer %s\n", *val, buffer); diff --git a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp index d5dc6aedb..d26e8ac9f 100755 --- a/rEFIt_UEFI/cpp_unit_test/all_tests.cpp +++ b/rEFIt_UEFI/cpp_unit_test/all_tests.cpp @@ -16,6 +16,8 @@ #include "XToolsCommon_test.h" #include "XBuffer_tests.h" #include "plist_tests.h" +#include "find_replace_mask_Clover_tests.h" +#include "find_replace_mask_OC_tests.h" #if defined(JIEF_DEBUG) && defined(CLOVER_BUILD) #include "printlib-test.h" @@ -33,12 +35,23 @@ bool all_tests() bool all_ok = true; int ret; -// ret = XString_tests(); -// if ( ret != 0 ) { -// printf("XString16_tests() failed at test %d\n", ret); -// all_ok = false; -// } +// ret = XString_tests(); +// if ( ret != 0 ) { +// printf("XString16_tests() failed at test %d\n", ret); +// all_ok = false; +// } #if defined(JIEF_DEBUG) + ret = find_replace_mask_Clover_tests(); + if ( ret != 0 ) { + printf("xml_lite_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = find_replace_mask_OC_tests(); + if ( ret != 0 ) { + printf("xml_lite_tests() failed at test %d\n", ret); + all_ok = false; + } +// return 1; ret = printf_lite_tests(); if ( ret != 0 ) { printf("printf_lite_tests() failed at test %d\n", ret); @@ -57,59 +70,59 @@ bool all_tests() // } #endif #if defined(JIEF_DEBUG) && defined(CLOVER_BUILD) - ret = printlib_tests(); - if ( ret != 0 ) { - printf("printlib_tests() failed at test %d\n", ret); - all_ok = false; - } + ret = printlib_tests(); + if ( ret != 0 ) { + printf("printlib_tests() failed at test %d\n", ret); + all_ok = false; + } #endif #ifndef _MSC_VER - ret = printf_lite_tests(); - if ( ret != 0 ) { - printf("printf_lite_tests() failed at test %d\n", ret); - all_ok = false; - } + ret = printf_lite_tests(); + if ( ret != 0 ) { + printf("printf_lite_tests() failed at test %d\n", ret); + all_ok = false; + } #endif - ret = strlen_tests(); - if ( ret != 0 ) { - printf("strlen_tests() failed at test %d\n", ret); - all_ok = false; - } - ret = strcmp_tests(); - if ( ret != 0 ) { - printf("strcmp_tests() failed at test %d\n", ret); - all_ok = false; - } - ret = strncmp_tests(); - if ( ret != 0 ) { - printf("strncmp_tests() failed at test %d\n", ret); - all_ok = false; - } + ret = strlen_tests(); + if ( ret != 0 ) { + printf("strlen_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = strcmp_tests(); + if ( ret != 0 ) { + printf("strcmp_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = strncmp_tests(); + if ( ret != 0 ) { + printf("strncmp_tests() failed at test %d\n", ret); + all_ok = false; + } ret = XToolsCommon_tests(); if ( ret != 0 ) { printf("printlib_tests() failed at test %d\n", ret); all_ok = false; } - ret = XArray_tests(); - if ( ret != 0 ) { - printf("XArray_tests() failed at test %d\n", ret); - all_ok = false; - } - ret = XObjArray_tests(); - if ( ret != 0 ) { - printf("XObjArray_tests() failed at test %d\n", ret); - all_ok = false; - } - ret = XString_tests(); - if ( ret != 0 ) { - printf("XString_tests() failed at test %d\n", ret); - all_ok = false; - } - ret = XStringArray_tests(); - if ( ret != 0 ) { - printf("XStringArray_tests() failed at test %d\n", ret); - all_ok = false; - } + ret = XArray_tests(); + if ( ret != 0 ) { + printf("XArray_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = XObjArray_tests(); + if ( ret != 0 ) { + printf("XObjArray_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = XString_tests(); + if ( ret != 0 ) { + printf("XString_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = XStringArray_tests(); + if ( ret != 0 ) { + printf("XStringArray_tests() failed at test %d\n", ret); + all_ok = false; + } ret = BootOptions_tests(); if ( ret != 0 ) { printf("BootOptions_tests() failed at test %d\n", ret); @@ -120,29 +133,34 @@ bool all_tests() printf("XBuffer_tests() failed at test %d\n", ret); all_ok = false; } - ret = plist_tests(); + ret = find_replace_mask_Clover_tests(); if ( ret != 0 ) { - printf("plist_tests() failed at test %d\n", ret); + printf("xml_lite_tests() failed at test %d\n", ret); + all_ok = false; + } + ret = find_replace_mask_OC_tests(); + if ( ret != 0 ) { + printf("xml_lite_tests() failed at test %d\n", ret); all_ok = false; } - if ( !all_ok ) { - printf("A test failed\n"); - } - + if ( !all_ok ) { + printf("A test failed\n"); + } + #if defined(JIEF_DEBUG) - if ( all_ok ) { - printf("All tests succeeded\n"); - } + if ( all_ok ) { + printf("All tests succeeded\n"); + } #endif - + #if defined(CLOVER_BUILD) && defined(JIEF_DEBUG) - if ( all_ok ) { -// PauseForKey(L"press"); - }else{ - PauseForKey(L"press"); - } + if ( all_ok ) { +// PauseForKey(L"press"); + }else{ + PauseForKey(L"press"); + } #endif - return all_ok; + return all_ok; } diff --git a/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.cpp b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.cpp new file mode 100755 index 000000000..216e979c7 --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.cpp @@ -0,0 +1,86 @@ +#include // Only use angled for Platform, else, xcode project won't compile +#include "../Platform/MemoryOperation.h" + +static int breakpoint(int i) +{ + return i; +} + +int find_replace_mask_Clover_tests() +{ +// int ret; +// INT32 int32; + UINTN uintn; + + uintn = FindMemMask((UINT8*)"\x01\x11\x02", 3, (UINT8*)"\x11", 1, NULL, 0); + if ( uintn != 1 ) breakpoint(1); + + uintn = FindMemMask((UINT8*)"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a", 10, (UINT8*)"\x13\x14", 2, NULL, 0); + if ( uintn != 2 ) breakpoint(1); + + uintn = FindMemMask((UINT8*)"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a", 10, (UINT8*)"\x14\x00\x16", 3, (UINT8*)"\xFF\x00\xFF", 3); + if ( uintn != 3 ) breakpoint(1); + + uintn = FindMemMask((UINT8*)"\x00\xCC\x00", 3, (UINT8*)"\xC0", 1, (UINT8*)"\xF0", 1); + if ( uintn != 1 ) breakpoint(1); + + // Simple patch of 3 bytes + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uintn = SearchAndReplaceMask(buf, 10, + (UINT8*)"\x13\x14\x15", NULL, 3, + (UINT8*)"\x23\x24\x25", NULL, + 0); + if ( uintn != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uintn = SearchAndReplaceMask(buf, 10, + (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, + (UINT8*)"\x23\x24\x25", NULL, + 0); + if ( uintn != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask and replacemask. Mask replace bits same place as Mask find + // Find x13x00x15/0xFFx00xFF Replace x23x24x25/xFFx00xFF + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x14, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uintn = SearchAndReplaceMask(buf, 10, + (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, + (UINT8*)"\x23\x24\x25", (UINT8*)"\xFF\x00\xFF", + 0); + if ( uintn != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask and replacemask. Mask replace bits NOT same place as Mask find + // Find x13x00x15/0xFFx00xFF Replace x23x24x25/xFFxFFx00 + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uintn = SearchAndReplaceMask(buf, 10, + (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, + (UINT8*)"\x23\x24\x25", (UINT8*)"\xFF\xFF\x00", + 0); + if ( uintn != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch half a byte + { + UINT8 buf[] = { 0x11, 0xCC, 0x13 }; + UINT8 expectedBuf[] = { 0x11, 0xC2, 0x13 }; + uintn = SearchAndReplaceMask(buf, 3, + (UINT8*)"\xC0", (UINT8*)"\xF0", 1, + (UINT8*)"\x22", (UINT8*)"\x0F", + 0); + if ( uintn != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 3) != 0 ) breakpoint(1); + } + + return 0; +} diff --git a/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.h b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.h new file mode 100755 index 000000000..3db98ffda --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_Clover_tests.h @@ -0,0 +1 @@ +int find_replace_mask_Clover_tests(); diff --git a/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.cpp b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.cpp new file mode 100755 index 000000000..460ec504e --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.cpp @@ -0,0 +1,99 @@ +#include // Only use angled for Platform, else, xcode project won't compile + +//#include "../../OpenCorePkg/Include/Acidanthera/Library/OcMiscLib.h" + + +INT32 +FindPattern ( + IN CONST UINT8 *Pattern, + IN CONST UINT8 *PatternMask OPTIONAL, + IN CONST UINT32 PatternSize, + IN CONST UINT8 *Data, + IN UINT32 DataSize, + IN INT32 DataOff + ); + +UINT32 +ApplyPatch ( + IN CONST UINT8 *Pattern, + IN CONST UINT8 *PatternMask OPTIONAL, + IN CONST UINT32 PatternSize, + IN CONST UINT8 *Replace, + IN CONST UINT8 *ReplaceMask OPTIONAL, + IN UINT8 *Data, + IN UINT32 DataSize, + IN UINT32 Count, + IN UINT32 Skip + ); + +static int breakpoint(int i) +{ + return i; +} + +int find_replace_mask_OC_tests() +{ +// int ret; + INT32 int32; + UINT32 uint32; + + int32 = FindPattern((UINT8*)"\x11", NULL, 1, (UINT8*)"\x01\x11\x02", 3, 0); + if ( int32 != 1 ) breakpoint(1); + + int32 = FindPattern((UINT8*)"\x13\x14", NULL, 2, (UINT8*)"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a", 10, 0); + if ( int32 != 2 ) breakpoint(1); + + int32 = FindPattern((UINT8*)"\x14\x00\x16", (UINT8*)"\xFF\x00\xFF", 3, (UINT8*)"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a", 10, 0); + if ( int32 != 3 ) breakpoint(1); + + int32 = FindPattern((UINT8*)"\xC0", (UINT8*)"\xF0", 1, (UINT8*)"\x00\xCC\x00", 3, 0); + if ( int32 != 1 ) breakpoint(1); + + // Simple patch of 3 bytes + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uint32 = ApplyPatch( (UINT8*)"\x13\x14\x15", NULL, 3, (UINT8*)"\x23\x24\x25", NULL, buf, 10, 0, 0); + if ( uint32 != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uint32 = ApplyPatch( (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, (UINT8*)"\x23\x24\x25", NULL, buf, 10, 0, 0); + if ( uint32 != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask and replacemask + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x14, 0x25, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uint32 = ApplyPatch( (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, (UINT8*)"\x23\x24\x25", (UINT8*)"\xFF\x00\xFF", buf, 10, 0, 0); + if ( uint32 != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch 3 bytes with find mask and replacemask + { + UINT8 buf[] = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + UINT8 expectedBuf[] = { 0x11, 0x12, 0x23, 0x24, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20 }; + uint32 = ApplyPatch( + (UINT8*)"\x13\x00\x15", (UINT8*)"\xFF\x00\xFF", 3, + (UINT8*)"\x23\x24\x25", (UINT8*)"\xFF\xFF\x00", + buf, 10, 0, 0); + if ( uint32 != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 10) != 0 ) breakpoint(1); + } + // Patch half a byte + { + UINT8 buf[] = { 0x11, 0xCC, 0x13 }; + UINT8 expectedBuf[] = { 0x11, 0xC2, 0x13 }; + uint32 = ApplyPatch((UINT8*)"\xC0", (UINT8*)"\xF0", 1, + (UINT8*)"\x22", (UINT8*)"\x0F", + buf, 3, 0, 0); + if ( uint32 != 1 ) breakpoint(1); + if ( memcmp(buf, expectedBuf, 3) != 0 ) breakpoint(1); + } + + return 0; +} diff --git a/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.h b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.h new file mode 100755 index 000000000..cff1fd1c7 --- /dev/null +++ b/rEFIt_UEFI/cpp_unit_test/find_replace_mask_OC_tests.h @@ -0,0 +1 @@ +int find_replace_mask_OC_tests(); diff --git a/rEFIt_UEFI/refit.inf b/rEFIt_UEFI/refit.inf index 66fa71c31..fb7e521de 100644 --- a/rEFIt_UEFI/refit.inf +++ b/rEFIt_UEFI/refit.inf @@ -180,6 +180,8 @@ Platform/LegacyBoot.h Platform/LegacyBoot.cpp Platform/memvendors.h + Platform/MemoryOperation.h + Platform/MemoryOperation.c Platform/nvidia.h Platform/nvidia.cpp Platform/Net.h @@ -286,6 +288,10 @@ # cpp_foundation/XUINTN.h cpp_unit_test/all_tests.h cpp_unit_test/all_tests.cpp + cpp_unit_test/find_replace_mask_Clover_tests.h + cpp_unit_test/find_replace_mask_Clover_tests.cpp + cpp_unit_test/find_replace_mask_OC_tests.h + cpp_unit_test/find_replace_mask_OC_tests.cpp cpp_unit_test/global_test.h cpp_unit_test/global_test.cpp cpp_unit_test/LoadOptions_test.cpp diff --git a/rEFIt_UEFI/refit/lib.cpp b/rEFIt_UEFI/refit/lib.cpp index b17ac555e..59cc3b241 100644 --- a/rEFIt_UEFI/refit/lib.cpp +++ b/rEFIt_UEFI/refit/lib.cpp @@ -186,6 +186,7 @@ EFI_STATUS ReinitRefitLib(void) self.reInitialize(); selfOem.reInitialize(); + ReinitVolumes(); ThemeX.openThemeDir(); return EFI_SUCCESS;