From f07f66ba03dfeed222f419e1bae274e95cbcd2d4 Mon Sep 17 00:00:00 2001 From: Zichuan Li <34680029+river-li@users.noreply.github.com> Date: Thu, 15 Aug 2024 18:23:43 -0400 Subject: misc: formated code and fixed several small issues 1. Fix code related to SMI handlers 2. also parse handlers registered by SxDispatch and parse them together 3. Set Progress Text properly --- platform/efi/efi_resolver/include/DxeResolver.h | 31 ++++---- platform/efi/efi_resolver/include/GuidRenderer.h | 15 ++-- platform/efi/efi_resolver/include/ModuleType.h | 23 +++--- platform/efi/efi_resolver/include/PeiResolver.h | 27 ++++--- platform/efi/efi_resolver/include/Resolver.h | 93 +++++++++++----------- .../efi/efi_resolver/include/TypePropagation.h | 15 ++-- platform/efi/efi_resolver/include/Utils.h | 46 ++++++----- 7 files changed, 128 insertions(+), 122 deletions(-) (limited to 'platform/efi/efi_resolver/include') diff --git a/platform/efi/efi_resolver/include/DxeResolver.h b/platform/efi/efi_resolver/include/DxeResolver.h index 3515b94a..eef6e6c2 100644 --- a/platform/efi/efi_resolver/include/DxeResolver.h +++ b/platform/efi/efi_resolver/include/DxeResolver.h @@ -2,24 +2,25 @@ #include "Resolver.h" -class DxeResolver : Resolver { - bool resolveBootServices(); - bool resolveRuntimeServices(); +class DxeResolver : Resolver +{ + bool resolveBootServices(); + bool resolveRuntimeServices(); - bool resolveSmmTables(string serviceName, string tableName); - bool resolveSmmServices(); - bool resolveSmiHandlers(); + bool resolveSmmTables(string serviceName, string tableName); + bool resolveSmmServices(); + bool resolveSmiHandlers(); public: - /*! - resolve BootServices and RuntimeServices, define protocol types that loaded by BootServices - */ - bool resolveDxe(); + /*! + resolve BootServices and RuntimeServices, define protocol types that loaded by BootServices + */ + bool resolveDxe(); - /*! - Define MMST/SMMST and resolve SMM related protocols - */ - bool resolveSmm(); + /*! + Define MMST/SMMST and resolve SMM related protocols + */ + bool resolveSmm(); - DxeResolver(Ref view, Ref task); + DxeResolver(Ref view, Ref task); }; \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/GuidRenderer.h b/platform/efi/efi_resolver/include/GuidRenderer.h index 30bd9126..e30aa880 100644 --- a/platform/efi/efi_resolver/include/GuidRenderer.h +++ b/platform/efi/efi_resolver/include/GuidRenderer.h @@ -6,16 +6,15 @@ using namespace BinaryNinja; using namespace std; -class EfiGuidRenderer : public BinaryNinja::DataRenderer { - EfiGuidRenderer() = default; +class EfiGuidRenderer : public BinaryNinja::DataRenderer +{ + EfiGuidRenderer() = default; public: - bool IsValidForData(BinaryView*, uint64_t address, Type*, - vector>&) override; + bool IsValidForData(BinaryView*, uint64_t address, Type*, vector>&) override; - vector GetLinesForData( - BinaryView*, uint64_t address, Type*, const vector& prefix, - size_t width, vector>&) override; + vector GetLinesForData(BinaryView*, uint64_t address, Type*, + const vector& prefix, size_t width, vector>&) override; - static void Register(); + static void Register(); }; \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/ModuleType.h b/platform/efi/efi_resolver/include/ModuleType.h index 81b579b9..187dcc36 100644 --- a/platform/efi/efi_resolver/include/ModuleType.h +++ b/platform/efi/efi_resolver/include/ModuleType.h @@ -4,19 +4,20 @@ using namespace BinaryNinja; -enum EFIModuleType { - UNKNOWN, - PEI, - DXE, +enum EFIModuleType +{ + UNKNOWN, + PEI, + DXE, }; static inline EFIModuleType identifyModuleType(BinaryView* bv) { - std::string viewType = bv->GetCurrentView(); - if (viewType == "Linear:PE") - return DXE; - else if (viewType == "Linear:TE") - return PEI; - else - return UNKNOWN; + std::string viewType = bv->GetCurrentView(); + if (viewType == "Linear:PE") + return DXE; + else if (viewType == "Linear:TE") + return PEI; + else + return UNKNOWN; } \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/PeiResolver.h b/platform/efi/efi_resolver/include/PeiResolver.h index 76827a1a..2ecf8ace 100644 --- a/platform/efi/efi_resolver/include/PeiResolver.h +++ b/platform/efi/efi_resolver/include/PeiResolver.h @@ -2,19 +2,20 @@ #include "Resolver.h" -class PeiResolver : Resolver { - bool resolvePeiIdt(); - bool resolvePeiMrc(); - bool resolvePeiMrs(); - bool resolvePlatformPointers(); - bool resolvePeiDescriptors(); - bool resolvePeiServices(); +class PeiResolver : Resolver +{ + bool resolvePeiIdt(); + bool resolvePeiMrc(); + bool resolvePeiMrs(); + bool resolvePlatformPointers(); + bool resolvePeiDescriptors(); + bool resolvePeiServices(); public: - /*! - resolve Pei related types and PPIs, this function will also resolve processor-specific pointers - and tried to define the EFI_PEI_DESCRIPTORS - */ - bool resolvePei(); - PeiResolver(Ref view, Ref task); + /*! + resolve Pei related types and PPIs, this function will also resolve processor-specific pointers + and tried to define the EFI_PEI_DESCRIPTORS + */ + bool resolvePei(); + PeiResolver(Ref view, Ref task); }; \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/Resolver.h b/platform/efi/efi_resolver/include/Resolver.h index 7bbd3b99..91bb1862 100644 --- a/platform/efi/efi_resolver/include/Resolver.h +++ b/platform/efi/efi_resolver/include/Resolver.h @@ -16,61 +16,60 @@ using namespace std; typedef array EFI_GUID; -class Resolver { +class Resolver +{ protected: - Ref m_view; - Ref m_task; - size_t m_width; - map> m_protocol; - map m_user_guids; + Ref m_view; + Ref m_task; + size_t m_width; + map> m_protocol; + map m_user_guids; - vector> m_service_usages; - vector> m_protocol_usages; - vector> m_guid_usages; - vector> m_variable_usages; + vector> m_service_usages; + vector> m_protocol_usages; + vector> m_guid_usages; + vector> m_variable_usages; - bool parseUserGuidIfExists(const string& filePath); - bool parseProtocolMapping(const string& filePath); + bool parseUserGuidIfExists(const string& filePath); + bool parseProtocolMapping(const string& filePath); - /*! - For backward compatibility, if a user saved a bndb with older version Binary Ninja - this function will try to retrieve types from Platform Types if it doesn't find one - in BinaryView - */ - Ref GetTypeFromViewAndPlatform(string type_name); - void initProtocolMapping(); + /*! + For backward compatibility, if a user saved a bndb with older version Binary Ninja + this function will try to retrieve types from Platform Types if it doesn't find one + in BinaryView + */ + Ref GetTypeFromViewAndPlatform(string type_name); + void initProtocolMapping(); public: - bool setModuleEntry(EFIModuleType fileType); - bool resolveGuidInterface(Ref func, uint64_t addr, int guid_pos, int interface_pos); - Resolver(Ref view, Ref task); + bool setModuleEntry(EFIModuleType fileType); + bool resolveGuidInterface(Ref func, uint64_t addr, int guid_pos, int interface_pos); + Resolver(Ref view, Ref task); - pair lookupGuid(EFI_GUID guidBytes); - pair defineAndLookupGuid(uint64_t addr); + pair lookupGuid(EFI_GUID guidBytes); + pair defineAndLookupGuid(uint64_t addr); - string nonConflictingName(const string& basename); - static string nonConflictingLocalName(Ref func, const string& basename); + string nonConflictingName(const string& basename); + static string nonConflictingLocalName(Ref func, const string& basename); - /*! - Define the structure used at the callsite with type `typeName`, propagate it to the data section. If it's a structure type, define it fields - according to the `followFields` parameter. The input `addr` should be a call instruction - \param func the function that contains the callsite (it's parent function) - \param addr address of the callsite - \param typeName the type that need to define - \param paramIdx the parameter index that want to define - \param followFields whether to define the structure's fields if they are pointers - \return False if failed + /*! + Define the structure used at the callsite with type `typeName`, propagate it to the data section. If it's a + structure type, define it fields according to the `followFields` parameter. The input `addr` should be a call + instruction \param func the function that contains the callsite (it's parent function) \param addr address of the + callsite \param typeName the type that need to define \param paramIdx the parameter index that want to define \param + followFields whether to define the structure's fields if they are pointers \return False if failed - \b Example: - \code{.cpp} - refs = bv->GetCodeReferencesForType(QualifiedName("EFI_GET_VARIABLE")); - for (auto ref : refs) - { - // ... some checking, need to make sure is a call instruction - bool ok = defineTypeAtCallsite(ref.func, ref.addr, "EFI_GUID", 2, false); - } - \endcode - */ - bool defineTypeAtCallsite(Ref func, uint64_t addr, string typeName, int paramIdx, bool followFields = false); - vector HighLevelILExprsAt(Ref func, Ref arch, uint64_t addr); + \b Example: + \code{.cpp} + refs = bv->GetCodeReferencesForType(QualifiedName("EFI_GET_VARIABLE")); + for (auto ref : refs) + { + // ... some checking, need to make sure is a call instruction + bool ok = defineTypeAtCallsite(ref.func, ref.addr, "EFI_GUID", 2, false); + } + \endcode + */ + bool defineTypeAtCallsite( + Ref func, uint64_t addr, string typeName, int paramIdx, bool followFields = false); + vector HighLevelILExprsAt(Ref func, Ref arch, uint64_t addr); }; \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/TypePropagation.h b/platform/efi/efi_resolver/include/TypePropagation.h index 2ba131ba..1985e2ab 100644 --- a/platform/efi/efi_resolver/include/TypePropagation.h +++ b/platform/efi/efi_resolver/include/TypePropagation.h @@ -5,13 +5,14 @@ using namespace BinaryNinja; -class TypePropagation { - Ref m_view; - std::deque m_queue; - Ref m_platform; +class TypePropagation +{ + Ref m_view; + std::deque m_queue; + Ref m_platform; public: - TypePropagation(BinaryView* view); - bool propagateFuncParamTypes(Function* func); - bool propagateFuncParamTypes(Function* func, SSAVariable ssa_var); + TypePropagation(BinaryView* view); + bool propagateFuncParamTypes(Function* func); + bool propagateFuncParamTypes(Function* func, SSAVariable ssa_var); }; \ No newline at end of file diff --git a/platform/efi/efi_resolver/include/Utils.h b/platform/efi/efi_resolver/include/Utils.h index f2c371a8..981908b2 100644 --- a/platform/efi/efi_resolver/include/Utils.h +++ b/platform/efi/efi_resolver/include/Utils.h @@ -6,31 +6,35 @@ using namespace BinaryNinja; static inline std::string GetOriginalTypeName(Ref type) { - std::string result; - if (type->IsPointer()) { - if (type->GetChildType().GetValue()->IsNamedTypeRefer()) { - return type->GetChildType().GetValue()->GetNamedTypeReference()->GetName().GetString(); - } - return type->GetTypeName().GetString(); - } - if (type->IsNamedTypeRefer()) - return type->GetNamedTypeReference()->GetName().GetString(); + std::string result; + if (type->IsPointer()) + { + if (type->GetChildType().GetValue()->IsNamedTypeRefer()) + { + return type->GetChildType().GetValue()->GetNamedTypeReference()->GetName().GetString(); + } + return type->GetTypeName().GetString(); + } + if (type->IsNamedTypeRefer()) + return type->GetNamedTypeReference()->GetName().GetString(); - return type->GetTypeName().GetString(); + return type->GetTypeName().GetString(); } static inline std::string GetVarNameForTypeStr(const std::string typeStr) { - std::istringstream iss(typeStr); - std::string word; - std::string result; + std::istringstream iss(typeStr); + std::string word; + std::string result; - while (std::getline(iss, word, '_')) { - if (!word.empty()) { - word[0] = std::toupper(word[0]); - std::transform(word.begin() + 1, word.end(), word.begin() + 1, ::tolower); - result += word; - } - } - return result; + while (std::getline(iss, word, '_')) + { + if (!word.empty()) + { + word[0] = std::toupper(word[0]); + std::transform(word.begin() + 1, word.end(), word.begin() + 1, ::tolower); + result += word; + } + } + return result; } -- cgit v1.3.1