diff --git a/libguard/guard_interface.cpp b/libguard/guard_interface.cpp index b581f0a..655dd98 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,15 +423,32 @@ 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); } } +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); 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 };