Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion libguard/guard_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -333,10 +372,12 @@ static void invalidateRecord(const guardRecordParam& value)
(existGuard.targetId == entityPath)) &&
(existGuard.recordId != GUARD_RESOLVED))
{
toggleWriteFlag(file);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct, these chain operatoins are dangerous you look for CustoFD class you need to follow that, constructor of CustomFd opens the file and destructor closes the fd.

{
CustomFD fd;
.
.
}
when it reaches here fd will be closed

offset = pos * sizeof(existGuard);
existGuard.recordId = GUARD_RESOLVED;
file.write(offset + headerSize, &existGuard, sizeof(existGuard));
found = true;
toggleWriteFlag(file, false);
break;
}
}
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions libguard/guard_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 16 additions & 3 deletions libguard/include/guard_record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ namespace guard
const uint8_t CURRENT_GARD_VERSION_LAYOUT = 0x1;
#define GUARD_RESOLVED 0xFFFFFFFF

enum GardFlags : uint8_t
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
{
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
Expand Down Expand Up @@ -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
};

Expand Down