From f5237ecf184edd41dad02fccbb1b2bcaeccf6dbb Mon Sep 17 00:00:00 2001 From: Chirag Sharma Date: Sun, 15 May 2022 07:26:44 -0500 Subject: [PATCH 1/3] libguard: Adding GardFlags and GardFlagPnorWriteInProgress enums Change: Adding GardFlags and GardFlagPnorWriteInProgress enums to handle GUARD partition write. Signed-off-by: Chirag Sharma --- libguard/include/guard_record.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/libguard/include/guard_record.hpp b/libguard/include/guard_record.hpp index 6cda9fe..ff271b6 100644 --- a/libguard/include/guard_record.hpp +++ b/libguard/include/guard_record.hpp @@ -14,6 +14,18 @@ namespace guard const uint8_t CURRENT_GARD_VERSION_LAYOUT = 0x1; #define GUARD_RESOLVED 0xFFFFFFFF +enum GardFlags : uint8_t +{ + GARD_FLAG_MASK_PNOR_WRITE_IN_PROGRESS = 0x01, + GARD_FLAGS_DEFAULT = 0xFF +}; + +enum GardFlagPnorWriteInProgress : uint8_t +{ + GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS = 0x00, + GARD_FLAG_PNOR_WRITE_NOT_IN_PROGRESS = 0x01 +}; + #ifdef PGUARD /* From hostboot: src/include/usr/hwas/common/deconfigGard.H:GuardRecord */ struct GuardRecord @@ -67,9 +79,10 @@ struct GuardRecord struct GuardRecord_t { - uint8_t iv_magicNumber[8]; ///< GUARDREC - uint8_t iv_version; ///< Guard records version - uint8_t iv_padding[7]; ///< Padding + uint8_t iv_magicNumber[8]; ///< GUARDREC + uint8_t iv_version; ///< Guard records version + uint8_t iv_flags; ///< Gard flags (see the GardFlags enumeration) + uint8_t iv_padding[6]; ///< Padding GuardRecord* iv_guardRecords; ///< List of guard records }; From e98a1773a6d71409866c22fe410af40fe0f1a19f Mon Sep 17 00:00:00 2001 From: Chirag Sharma Date: Sun, 15 May 2022 07:44:29 -0500 Subject: [PATCH 2/3] libguard: Adding function to set/reset the write flag Change: Adding new function to set/reset the write flag. While creating guard record or re-writing a guard record write flag will be set to 0x00 and once the write completes, set the flag to 0x01. Tested: While writing the guard data the flag is set as 0x00 and once the write is finished its set back as 0x01. Created guard records to check if data is coming as expected or not. Signed-off-by: Chirag Sharma --- libguard/guard_interface.cpp | 45 +++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/libguard/guard_interface.cpp b/libguard/guard_interface.cpp index b581f0a..f061916 100644 --- a/libguard/guard_interface.cpp +++ b/libguard/guard_interface.cpp @@ -135,6 +135,43 @@ static GuardRecord getHostEndiannessRecord(const GuardRecord& record) return convertedRecord; } +/** + * @brief Function to set/reset write flag in guard header + * + * @param[in] GuardFile Guard file path + * @param[in] reset true if writing the guard record + * false to set the guard write flag + * + */ +void toggleWriteFlag(GuardFile& file, const bool reset = true) +{ + GuardRecord_t guardRecord; + constexpr size_t headerFlagPos = 9; + file.read(headerFlagPos, &guardRecord.iv_flags, + sizeof(guardRecord.iv_flags)); + uint8_t current_value = + guardRecord.iv_flags & GARD_FLAG_MASK_PNOR_WRITE_IN_PROGRESS; + + if (reset && current_value == GARD_FLAG_MASK_PNOR_WRITE_IN_PROGRESS) + { + // Resetting write bit in guard header + guardRecord.iv_flags = + guardRecord.iv_flags & GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS; + } + else if (!reset && current_value == GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS) + { + // Setting write bit in guard header once guard record is written. + guardRecord.iv_flags = GARD_FLAG_PNOR_WRITE_NOT_IN_PROGRESS; + } + else if (reset && current_value == GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS) + { + guardRecord.iv_flags = GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS; + } + + file.write(headerFlagPos, &guardRecord.iv_flags, + sizeof(guardRecord.iv_flags)); +} + #define for_each_guard(file, pos, guard) \ for (pos = guardNext(file, 0, guard); pos >= 0; \ pos = guardNext(file, ++pos, guard)) @@ -158,6 +195,7 @@ GuardRecord create(const EntityPath& entityPath, uint32_t eId, uint8_t eType, memset(&existGuard, 0xff, sizeOfGuard); GuardFile file(guardFilePath); + toggleWriteFlag(file); for_each_guard(file, pos, existGuard) { // Storing the oldest resolved guard record position. @@ -221,6 +259,7 @@ GuardRecord create(const EntityPath& entityPath, uint32_t eId, uint8_t eType, "Already guard record is available in the GUARD partition"); throw AlreadyGuarded("Guard record is already exist"); } + toggleWriteFlag(file, false); return getHostEndiannessRecord(existGuard); } @@ -269,7 +308,7 @@ GuardRecord create(const EntityPath& entityPath, uint32_t eId, uint8_t eType, memset(guard.u.s1.partNum, 0, sizeof(guard.u.s1.partNum)); #endif file.write(offset + headerSize, &guard, sizeOfGuard); - + toggleWriteFlag(file, false); return getHostEndiannessRecord(guard); } @@ -333,10 +372,12 @@ static void invalidateRecord(const guardRecordParam& value) (existGuard.targetId == entityPath)) && (existGuard.recordId != GUARD_RESOLVED)) { + toggleWriteFlag(file); offset = pos * sizeof(existGuard); existGuard.recordId = GUARD_RESOLVED; file.write(offset + headerSize, &existGuard, sizeof(existGuard)); found = true; + toggleWriteFlag(file, false); break; } } @@ -382,12 +423,14 @@ void invalidateAll() } else { + toggleWriteFlag(file); for_each_guard(file, pos, existGuard) { offset = pos * sizeof(existGuard); existGuard.recordId = GUARD_RESOLVED; file.write(offset + headerSize, &existGuard, sizeof(existGuard)); } + toggleWriteFlag(file, false); } } From 658570ad3a0436df1892fb6a5271c144bbcfc1f1 Mon Sep 17 00:00:00 2001 From: Chirag Sharma Date: Sun, 29 May 2022 03:03:16 -0500 Subject: [PATCH 3/3] libguard: Adding function to check write flag Change: Adding function checkWriteflag, which will read the write flag. Tested: In ipl code calling this function and its working as expected i.e. if flag is set to 0x00 return true else false. Signed-off-by: Chirag Sharma --- libguard/guard_interface.cpp | 15 +++++++++++++++ libguard/guard_interface.hpp | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/libguard/guard_interface.cpp b/libguard/guard_interface.cpp index f061916..655dd98 100644 --- a/libguard/guard_interface.cpp +++ b/libguard/guard_interface.cpp @@ -434,6 +434,21 @@ void invalidateAll() } } +bool checkWriteFlag() +{ + GuardFile file(guardFilePath); + GuardRecord_t guardRecord; + size_t headerFlagPos = 9; + file.read(headerPos, &guardRecord.iv_flags, sizeof(guardRecord.iv_flags)); + + if ((guardRecord.iv_flags & GARD_FLAG_MASK_PNOR_WRITE_IN_PROGRESS) == + GARD_FLAG_PNOR_WRITE_IS_IN_PROGRESS) + { + return true; + } + return false; +} + void libguard_init(bool enableDevtree) { initialize(); diff --git a/libguard/guard_interface.hpp b/libguard/guard_interface.hpp index 4d29266..2268c0d 100644 --- a/libguard/guard_interface.hpp +++ b/libguard/guard_interface.hpp @@ -149,6 +149,13 @@ const fs::path& getGuardFilePath(); */ bool isEphemeralType(const uint8_t recordType); +/** + * @brief Used to read the write flag of guard header + * + * @return true if bit is 0x00 else false. + */ +bool checkWriteFlag(); + namespace utest { void setGuardFile(const fs::path& file);