diff --git a/vpd-tool/src/vpd_tool.cpp b/vpd-tool/src/vpd_tool.cpp index 39ae9d8ac..cc7143bb9 100644 --- a/vpd-tool/src/vpd_tool.cpp +++ b/vpd-tool/src/vpd_tool.cpp @@ -197,7 +197,6 @@ int VpdTool::cleanSystemVpd() const noexcept int l_rc{constants::FAILURE}; try { - // TODO: // get the keyword map from backup_restore json // iterate through the keyword map get default value of // l_keywordName. @@ -205,13 +204,121 @@ int VpdTool::cleanSystemVpd() const noexcept // if hardware value != default value, // use writeKeyword API to update default value on hardware, // backup and D - Bus. + const nlohmann::json l_parsedBackupRestoreJson = + getBackupRestoreCfgJsonObj(); - l_rc = constants::SUCCESS; + // flag to track any failure + bool l_anyFailure{false}; + + // check for mandatory tags + if (l_parsedBackupRestoreJson.contains("source") && + l_parsedBackupRestoreJson.contains("backupMap") && + l_parsedBackupRestoreJson["source"].contains("hardwarePath") && + l_parsedBackupRestoreJson["backupMap"].is_array()) + { + // get the source hardware path + const auto& l_hardwarePath = + l_parsedBackupRestoreJson["source"]["hardwarePath"]; + + // iterate through the backup map + for (const auto& l_aRecordKwInfo : + l_parsedBackupRestoreJson["backupMap"]) + { + // check if Manufacturing Reset is required for this entry + const bool l_isMfgCleanRequired = + l_aRecordKwInfo.value("isManufactureResetRequired", false); + + if (l_isMfgCleanRequired) + { + // get the Record name and Keyword name + const std::string& l_srcRecordName = + l_aRecordKwInfo.value("sourceRecord", ""); + const std::string& l_srcKeywordName = + l_aRecordKwInfo.value("sourceKeyword", ""); + + // validate the Record name, Keyword name and the + // defaultValue + if (!l_srcRecordName.empty() && !l_srcKeywordName.empty() && + l_aRecordKwInfo.contains("defaultValue") && + l_aRecordKwInfo["defaultValue"].is_array()) + { + const types::BinaryVector l_defaultBinaryValue = + l_aRecordKwInfo["defaultValue"] + .get(); + + // get the value on hardware + const auto& l_keywordValue = + utils::readKeywordFromHardware( + l_hardwarePath, + std::make_tuple(l_srcRecordName, + l_srcKeywordName)); + + if (const auto l_keywordValueHW = + std::get_if( + &l_keywordValue); + l_keywordValueHW && !l_keywordValueHW->empty()) + { + // check if value on hardware is default, if + // not write default value to hardware + if (*l_keywordValueHW != l_defaultBinaryValue) + { + if (constants::FAILURE == + utils::writeKeyword( + l_hardwarePath, + std::make_tuple(l_srcRecordName, + l_srcKeywordName, + l_defaultBinaryValue))) + { + // TODO: Enable logging when verbose + // is enabled. + std::cerr << "Failed to update " + << l_srcRecordName << ":" + << l_srcKeywordName << std::endl; + l_anyFailure = true; + } + } + } + else + { + l_anyFailure = true; + // TODO: Enable logging when verbose is enabled. + std::cerr + << "Couldn't read default value for record name: " + + l_srcRecordName + + ", keyword name: " + l_srcKeywordName + + " from backup and restore config JSON file." + << std::endl; + } + } + else + { + std::cerr << "Backup Restore JSON is not valid" + << std::endl; + l_anyFailure = true; + } + } + } // keyword loop + } + else + { + std::cerr << "Backup Restore JSON is not valid" << std::endl; + l_anyFailure = true; + } + + if (!l_anyFailure) + { + l_rc = constants::SUCCESS; + std::cout + << "\n The critical keywords from system backplane VPD has " + "been reset successfully." + << std::endl; + } } catch (const std::exception& l_ex) { // TODO: Enable logging when verbose is enabled. - std::cerr << l_ex.what() << std::endl; + std::cerr << "Manufacturing clean failed.Error : " << l_ex.what() + << std::endl; } return l_rc; }