Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "worker.hpp" |
| 4 | |
| 5 | #include "backup_restore.hpp" |
| 6 | #include "configuration.hpp" |
| 7 | #include "constants.hpp" |
| 8 | #include "event_logger.hpp" |
| 9 | #include "exceptions.hpp" |
| 10 | #include "logger.hpp" |
| 11 | #include "parser.hpp" |
| 12 | #include "parser_factory.hpp" |
| 13 | #include "parser_interface.hpp" |
| 14 | |
| 15 | #include <utility/dbus_utility.hpp> |
| 16 | #include <utility/json_utility.hpp> |
| 17 | #include <utility/vpd_specific_utility.hpp> |
| 18 | |
| 19 | #include <filesystem> |
| 20 | #include <fstream> |
| 21 | #include <future> |
| 22 | #include <typeindex> |
| 23 | #include <unordered_set> |
| 24 | |
| 25 | namespace vpd |
| 26 | { |
| 27 | |
Sunny Srivastava | 765cf7b | 2025-02-04 05:24:11 -0600 | [diff] [blame] | 28 | Worker::Worker(std::string pathToConfigJson, uint8_t i_maxThreadCount) : |
| 29 | m_configJsonPath(pathToConfigJson), m_semaphore(i_maxThreadCount) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 30 | { |
| 31 | // Implies the processing is based on some config JSON |
| 32 | if (!m_configJsonPath.empty()) |
| 33 | { |
| 34 | // Check if symlink is already there to confirm fresh boot/factory |
| 35 | // reset. |
| 36 | if (std::filesystem::exists(INVENTORY_JSON_SYM_LINK)) |
| 37 | { |
| 38 | logging::logMessage("Sym Link already present"); |
| 39 | m_configJsonPath = INVENTORY_JSON_SYM_LINK; |
| 40 | m_isSymlinkPresent = true; |
| 41 | } |
| 42 | |
| 43 | try |
| 44 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 45 | uint16_t l_errCode = 0; |
| 46 | m_parsedJson = |
| 47 | jsonUtility::getParsedJson(m_configJsonPath, l_errCode); |
| 48 | |
| 49 | if (l_errCode) |
| 50 | { |
| 51 | throw std::runtime_error( |
| 52 | "JSON parsing failed for file [ " + m_configJsonPath + |
| 53 | " ], error : " + |
| 54 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 55 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 56 | |
| 57 | // check for mandatory fields at this point itself. |
| 58 | if (!m_parsedJson.contains("frus")) |
| 59 | { |
| 60 | throw std::runtime_error("Mandatory tag(s) missing from JSON"); |
| 61 | } |
| 62 | } |
| 63 | catch (const std::exception& ex) |
| 64 | { |
| 65 | throw(JsonException(ex.what(), m_configJsonPath)); |
| 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | logging::logMessage("Processing in not based on any config JSON"); |
| 71 | } |
| 72 | } |
| 73 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 74 | static std::string readFitConfigValue() |
| 75 | { |
| 76 | std::vector<std::string> output = |
| 77 | commonUtility::executeCmd("/sbin/fw_printenv"); |
| 78 | std::string fitConfigValue; |
| 79 | |
| 80 | for (const auto& entry : output) |
| 81 | { |
| 82 | auto pos = entry.find("="); |
| 83 | auto key = entry.substr(0, pos); |
| 84 | if (key != "fitconfig") |
| 85 | { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (pos + 1 < entry.size()) |
| 90 | { |
| 91 | fitConfigValue = entry.substr(pos + 1); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return fitConfigValue; |
| 96 | } |
| 97 | |
| 98 | bool Worker::isSystemVPDOnDBus() const |
| 99 | { |
| 100 | const std::string& mboardPath = |
| 101 | m_parsedJson["frus"][SYSTEM_VPD_FILE_PATH].at(0).value( |
| 102 | "inventoryPath", ""); |
| 103 | |
| 104 | if (mboardPath.empty()) |
| 105 | { |
| 106 | throw JsonException("System vpd file path missing in JSON", |
| 107 | INVENTORY_JSON_SYM_LINK); |
| 108 | } |
| 109 | |
| 110 | std::array<const char*, 1> interfaces = { |
| 111 | "xyz.openbmc_project.Inventory.Item.Board.Motherboard"}; |
| 112 | |
| 113 | const types::MapperGetObject& objectMap = |
| 114 | dbusUtility::getObjectMap(mboardPath, interfaces); |
| 115 | |
| 116 | if (objectMap.empty()) |
| 117 | { |
| 118 | return false; |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | std::string Worker::getIMValue(const types::IPZVpdMap& parsedVpd) const |
| 124 | { |
| 125 | if (parsedVpd.empty()) |
| 126 | { |
| 127 | throw std::runtime_error("Empty VPD map. Can't Extract IM value"); |
| 128 | } |
| 129 | |
| 130 | const auto& itrToVSBP = parsedVpd.find("VSBP"); |
| 131 | if (itrToVSBP == parsedVpd.end()) |
| 132 | { |
| 133 | throw DataException("VSBP record missing."); |
| 134 | } |
| 135 | |
| 136 | const auto& itrToIM = (itrToVSBP->second).find("IM"); |
| 137 | if (itrToIM == (itrToVSBP->second).end()) |
| 138 | { |
| 139 | throw DataException("IM keyword missing."); |
| 140 | } |
| 141 | |
| 142 | types::BinaryVector imVal; |
| 143 | std::copy(itrToIM->second.begin(), itrToIM->second.end(), |
| 144 | back_inserter(imVal)); |
| 145 | |
| 146 | std::ostringstream imData; |
| 147 | for (auto& aByte : imVal) |
| 148 | { |
| 149 | imData << std::setw(2) << std::setfill('0') << std::hex |
| 150 | << static_cast<int>(aByte); |
| 151 | } |
| 152 | |
| 153 | return imData.str(); |
| 154 | } |
| 155 | |
| 156 | std::string Worker::getHWVersion(const types::IPZVpdMap& parsedVpd) const |
| 157 | { |
| 158 | if (parsedVpd.empty()) |
| 159 | { |
| 160 | throw std::runtime_error("Empty VPD map. Can't Extract HW value"); |
| 161 | } |
| 162 | |
| 163 | const auto& itrToVINI = parsedVpd.find("VINI"); |
| 164 | if (itrToVINI == parsedVpd.end()) |
| 165 | { |
| 166 | throw DataException("VINI record missing."); |
| 167 | } |
| 168 | |
| 169 | const auto& itrToHW = (itrToVINI->second).find("HW"); |
| 170 | if (itrToHW == (itrToVINI->second).end()) |
| 171 | { |
| 172 | throw DataException("HW keyword missing."); |
| 173 | } |
| 174 | |
| 175 | types::BinaryVector hwVal; |
| 176 | std::copy(itrToHW->second.begin(), itrToHW->second.end(), |
| 177 | back_inserter(hwVal)); |
| 178 | |
| 179 | // The planar pass only comes from the LSB of the HW keyword, |
| 180 | // where as the MSB is used for other purposes such as signifying clock |
| 181 | // termination. |
| 182 | hwVal[0] = 0x00; |
| 183 | |
| 184 | std::ostringstream hwString; |
| 185 | for (auto& aByte : hwVal) |
| 186 | { |
| 187 | hwString << std::setw(2) << std::setfill('0') << std::hex |
| 188 | << static_cast<int>(aByte); |
| 189 | } |
| 190 | |
| 191 | return hwString.str(); |
| 192 | } |
| 193 | |
| 194 | void Worker::fillVPDMap(const std::string& vpdFilePath, |
| 195 | types::VPDMapVariant& vpdMap) |
| 196 | { |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 197 | if (vpdFilePath.empty()) |
| 198 | { |
| 199 | throw std::runtime_error("Invalid file path passed to fillVPDMap API."); |
| 200 | } |
| 201 | |
| 202 | if (!std::filesystem::exists(vpdFilePath)) |
| 203 | { |
| 204 | throw std::runtime_error("Can't Find physical file"); |
| 205 | } |
| 206 | |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 207 | std::shared_ptr<Parser> vpdParser = |
| 208 | std::make_shared<Parser>(vpdFilePath, m_parsedJson); |
| 209 | vpdMap = vpdParser->parse(); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void Worker::getSystemJson(std::string& systemJson, |
| 213 | const types::VPDMapVariant& parsedVpdMap) |
| 214 | { |
| 215 | if (auto pVal = std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 216 | { |
| 217 | std::string hwKWdValue = getHWVersion(*pVal); |
| 218 | if (hwKWdValue.empty()) |
| 219 | { |
| 220 | throw DataException("HW value fetched is empty."); |
| 221 | } |
| 222 | |
| 223 | const std::string& imKwdValue = getIMValue(*pVal); |
| 224 | if (imKwdValue.empty()) |
| 225 | { |
| 226 | throw DataException("IM value fetched is empty."); |
| 227 | } |
| 228 | |
| 229 | auto itrToIM = config::systemType.find(imKwdValue); |
| 230 | if (itrToIM == config::systemType.end()) |
| 231 | { |
| 232 | throw DataException("IM keyword does not map to any system type"); |
| 233 | } |
| 234 | |
| 235 | const types::HWVerList hwVersionList = itrToIM->second.second; |
| 236 | if (!hwVersionList.empty()) |
| 237 | { |
| 238 | transform(hwKWdValue.begin(), hwKWdValue.end(), hwKWdValue.begin(), |
| 239 | ::toupper); |
| 240 | |
| 241 | auto itrToHW = |
| 242 | std::find_if(hwVersionList.begin(), hwVersionList.end(), |
| 243 | [&hwKWdValue](const auto& aPair) { |
| 244 | return aPair.first == hwKWdValue; |
| 245 | }); |
| 246 | |
| 247 | if (itrToHW != hwVersionList.end()) |
| 248 | { |
| 249 | if (!(*itrToHW).second.empty()) |
| 250 | { |
| 251 | systemJson += (*itrToIM).first + "_" + (*itrToHW).second + |
| 252 | ".json"; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | systemJson += (*itrToIM).first + ".json"; |
| 257 | } |
| 258 | return; |
| 259 | } |
| 260 | } |
| 261 | systemJson += itrToIM->second.first + ".json"; |
| 262 | return; |
| 263 | } |
| 264 | |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 265 | throw DataException( |
| 266 | "Invalid VPD type returned from Parser. Can't get system JSON."); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static void setEnvAndReboot(const std::string& key, const std::string& value) |
| 270 | { |
| 271 | // set env and reboot and break. |
| 272 | commonUtility::executeCmd("/sbin/fw_setenv", key, value); |
| 273 | logging::logMessage("Rebooting BMC to pick up new device tree"); |
| 274 | |
| 275 | // make dbus call to reboot |
| 276 | auto bus = sdbusplus::bus::new_default_system(); |
| 277 | auto method = bus.new_method_call( |
| 278 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 279 | "org.freedesktop.systemd1.Manager", "Reboot"); |
| 280 | bus.call_noreply(method); |
| 281 | } |
| 282 | |
| 283 | void Worker::setJsonSymbolicLink(const std::string& i_systemJson) |
| 284 | { |
| 285 | std::error_code l_ec; |
| 286 | l_ec.clear(); |
Sunny Srivastava | adff788 | 2025-03-13 11:41:05 +0530 | [diff] [blame] | 287 | |
| 288 | // Check if symlink file path exists and if the JSON at this location is a |
| 289 | // symlink. |
| 290 | if (m_isSymlinkPresent && |
| 291 | std::filesystem::is_symlink(INVENTORY_JSON_SYM_LINK, l_ec)) |
| 292 | { // Don't care about exception in "is_symlink". Will continue with creation |
| 293 | // of symlink. |
| 294 | |
| 295 | const auto& l_symlinkFilePth = |
| 296 | std::filesystem::read_symlink(INVENTORY_JSON_SYM_LINK, l_ec); |
| 297 | |
| 298 | if (l_ec) |
| 299 | { |
| 300 | logging::logMessage( |
| 301 | "Can't read existing symlink. Error =" + l_ec.message() + |
| 302 | "Trying removal of symlink and creation of new symlink."); |
| 303 | } |
| 304 | |
| 305 | // If currently set JSON is the required one. No further processing |
| 306 | // required. |
| 307 | if (i_systemJson == l_symlinkFilePth) |
| 308 | { |
| 309 | // Correct symlink already set. |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | if (!std::filesystem::remove(INVENTORY_JSON_SYM_LINK, l_ec)) |
| 314 | { |
| 315 | // No point going further. If removal fails for existing symlink, |
| 316 | // create will anyways throw. |
| 317 | throw std::runtime_error( |
| 318 | "Removal of symlink failed with Error = " + l_ec.message() + |
| 319 | ". Can't proceed with create_symlink."); |
| 320 | } |
| 321 | } |
| 322 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 323 | if (!std::filesystem::exists(VPD_SYMLIMK_PATH, l_ec)) |
| 324 | { |
| 325 | if (l_ec) |
| 326 | { |
| 327 | throw std::runtime_error( |
| 328 | "File system call to exist failed with error = " + |
| 329 | l_ec.message()); |
| 330 | } |
| 331 | |
| 332 | // implies it is a fresh boot/factory reset. |
| 333 | // Create the directory for hosting the symlink |
| 334 | if (!std::filesystem::create_directories(VPD_SYMLIMK_PATH, l_ec)) |
| 335 | { |
| 336 | if (l_ec) |
| 337 | { |
| 338 | throw std::runtime_error( |
| 339 | "File system call to create directory failed with error = " + |
| 340 | l_ec.message()); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // create a new symlink based on the system |
| 346 | std::filesystem::create_symlink(i_systemJson, INVENTORY_JSON_SYM_LINK, |
| 347 | l_ec); |
| 348 | |
| 349 | if (l_ec) |
| 350 | { |
| 351 | throw std::runtime_error( |
| 352 | "create_symlink system call failed with error: " + l_ec.message()); |
| 353 | } |
| 354 | |
| 355 | // If the flow is at this point implies the symlink was not present there. |
| 356 | // Considering this as factory reset. |
| 357 | m_isFactoryResetDone = true; |
| 358 | } |
| 359 | |
| 360 | void Worker::setDeviceTreeAndJson() |
| 361 | { |
| 362 | // JSON is madatory for processing of this API. |
| 363 | if (m_parsedJson.empty()) |
| 364 | { |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 365 | throw JsonException("System config JSON is empty", m_configJsonPath); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | types::VPDMapVariant parsedVpdMap; |
| 369 | fillVPDMap(SYSTEM_VPD_FILE_PATH, parsedVpdMap); |
| 370 | |
| 371 | // Implies it is default JSON. |
| 372 | std::string systemJson{JSON_ABSOLUTE_PATH_PREFIX}; |
| 373 | |
| 374 | // ToDo: Need to check if INVENTORY_JSON_SYM_LINK pointing to correct system |
| 375 | // This is required to support movement from rainier to Blue Ridge on the |
| 376 | // fly. |
| 377 | |
Sunny Srivastava | adff788 | 2025-03-13 11:41:05 +0530 | [diff] [blame] | 378 | getSystemJson(systemJson, parsedVpdMap); |
| 379 | |
| 380 | if (!systemJson.compare(JSON_ABSOLUTE_PATH_PREFIX)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 381 | { |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 382 | throw DataException( |
| 383 | "No system JSON found corresponding to IM read from VPD."); |
Sunny Srivastava | adff788 | 2025-03-13 11:41:05 +0530 | [diff] [blame] | 384 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 385 | |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 386 | uint16_t l_errCode = 0; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 387 | |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 388 | // re-parse the JSON once appropriate JSON has been selected. |
| 389 | m_parsedJson = jsonUtility::getParsedJson(systemJson, l_errCode); |
| 390 | |
| 391 | if (l_errCode) |
Sunny Srivastava | adff788 | 2025-03-13 11:41:05 +0530 | [diff] [blame] | 392 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 393 | throw(JsonException( |
| 394 | "JSON parsing failed for file [ " + systemJson + |
| 395 | " ], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode), |
| 396 | systemJson)); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | std::string devTreeFromJson; |
| 400 | if (m_parsedJson.contains("devTree")) |
| 401 | { |
| 402 | devTreeFromJson = m_parsedJson["devTree"]; |
| 403 | |
| 404 | if (devTreeFromJson.empty()) |
| 405 | { |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 406 | EventLogger::createSyncPel( |
| 407 | types::ErrorType::JsonFailure, types::SeverityType::Error, |
| 408 | __FILE__, __FUNCTION__, 0, |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 409 | "Mandatory value for device tree missing from JSON[" + |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 410 | systemJson + "]", |
| 411 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | auto fitConfigVal = readFitConfigValue(); |
| 416 | |
| 417 | if (devTreeFromJson.empty() || |
| 418 | fitConfigVal.find(devTreeFromJson) != std::string::npos) |
| 419 | { // Skipping setting device tree as either devtree info is missing from |
| 420 | // Json or it is rightly set. |
| 421 | |
Sunny Srivastava | adff788 | 2025-03-13 11:41:05 +0530 | [diff] [blame] | 422 | setJsonSymbolicLink(systemJson); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 423 | |
| 424 | if (isSystemVPDOnDBus() && |
| 425 | jsonUtility::isBackupAndRestoreRequired(m_parsedJson)) |
| 426 | { |
| 427 | performBackupAndRestore(parsedVpdMap); |
| 428 | } |
| 429 | |
| 430 | // proceed to publish system VPD. |
| 431 | publishSystemVPD(parsedVpdMap); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | setEnvAndReboot("fitconfig", devTreeFromJson); |
| 436 | exit(EXIT_SUCCESS); |
| 437 | } |
| 438 | |
| 439 | void Worker::populateIPZVPDpropertyMap( |
| 440 | types::InterfaceMap& interfacePropMap, |
| 441 | const types::IPZKwdValueMap& keyordValueMap, |
| 442 | const std::string& interfaceName) |
| 443 | { |
| 444 | types::PropertyMap propertyValueMap; |
| 445 | for (const auto& kwdVal : keyordValueMap) |
| 446 | { |
| 447 | auto kwd = kwdVal.first; |
| 448 | |
| 449 | if (kwd[0] == '#') |
| 450 | { |
| 451 | kwd = std::string("PD_") + kwd[1]; |
| 452 | } |
| 453 | else if (isdigit(kwd[0])) |
| 454 | { |
| 455 | kwd = std::string("N_") + kwd; |
| 456 | } |
| 457 | |
| 458 | types::BinaryVector value(kwdVal.second.begin(), kwdVal.second.end()); |
| 459 | propertyValueMap.emplace(move(kwd), move(value)); |
| 460 | } |
| 461 | |
| 462 | if (!propertyValueMap.empty()) |
| 463 | { |
| 464 | interfacePropMap.emplace(interfaceName, propertyValueMap); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void Worker::populateKwdVPDpropertyMap(const types::KeywordVpdMap& keyordVPDMap, |
| 469 | types::InterfaceMap& interfaceMap) |
| 470 | { |
| 471 | for (const auto& kwdValMap : keyordVPDMap) |
| 472 | { |
| 473 | types::PropertyMap propertyValueMap; |
| 474 | auto kwd = kwdValMap.first; |
| 475 | |
| 476 | if (kwd[0] == '#') |
| 477 | { |
| 478 | kwd = std::string("PD_") + kwd[1]; |
| 479 | } |
| 480 | else if (isdigit(kwd[0])) |
| 481 | { |
| 482 | kwd = std::string("N_") + kwd; |
| 483 | } |
| 484 | |
| 485 | if (auto keywordValue = get_if<types::BinaryVector>(&kwdValMap.second)) |
| 486 | { |
| 487 | types::BinaryVector value((*keywordValue).begin(), |
| 488 | (*keywordValue).end()); |
| 489 | propertyValueMap.emplace(move(kwd), move(value)); |
| 490 | } |
| 491 | else if (auto keywordValue = get_if<std::string>(&kwdValMap.second)) |
| 492 | { |
| 493 | types::BinaryVector value((*keywordValue).begin(), |
| 494 | (*keywordValue).end()); |
| 495 | propertyValueMap.emplace(move(kwd), move(value)); |
| 496 | } |
| 497 | else if (auto keywordValue = get_if<size_t>(&kwdValMap.second)) |
| 498 | { |
| 499 | if (kwd == "MemorySizeInKB") |
| 500 | { |
| 501 | types::PropertyMap memProp; |
| 502 | memProp.emplace(move(kwd), ((*keywordValue))); |
| 503 | interfaceMap.emplace("xyz.openbmc_project.Inventory.Item.Dimm", |
| 504 | move(memProp)); |
| 505 | continue; |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | logging::logMessage( |
| 510 | "Unknown Keyword =" + kwd + " found in keyword VPD map"); |
| 511 | continue; |
| 512 | } |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | logging::logMessage( |
| 517 | "Unknown variant type found in keyword VPD map."); |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | if (!propertyValueMap.empty()) |
| 522 | { |
| 523 | vpdSpecificUtility::insertOrMerge( |
| 524 | interfaceMap, constants::kwdVpdInf, move(propertyValueMap)); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | void Worker::populateInterfaces(const nlohmann::json& interfaceJson, |
| 530 | types::InterfaceMap& interfaceMap, |
| 531 | const types::VPDMapVariant& parsedVpdMap) |
| 532 | { |
| 533 | for (const auto& interfacesPropPair : interfaceJson.items()) |
| 534 | { |
| 535 | const std::string& interface = interfacesPropPair.key(); |
| 536 | types::PropertyMap propertyMap; |
| 537 | |
| 538 | for (const auto& propValuePair : interfacesPropPair.value().items()) |
| 539 | { |
| 540 | const std::string property = propValuePair.key(); |
| 541 | |
| 542 | if (propValuePair.value().is_boolean()) |
| 543 | { |
| 544 | propertyMap.emplace(property, |
| 545 | propValuePair.value().get<bool>()); |
| 546 | } |
| 547 | else if (propValuePair.value().is_string()) |
| 548 | { |
| 549 | if (property.compare("LocationCode") == 0 && |
| 550 | interface.compare("com.ibm.ipzvpd.Location") == 0) |
| 551 | { |
| 552 | std::string value = |
| 553 | vpdSpecificUtility::getExpandedLocationCode( |
| 554 | propValuePair.value().get<std::string>(), |
| 555 | parsedVpdMap); |
| 556 | propertyMap.emplace(property, value); |
| 557 | |
| 558 | auto l_locCodeProperty = propertyMap; |
| 559 | vpdSpecificUtility::insertOrMerge( |
| 560 | interfaceMap, |
| 561 | std::string(constants::xyzLocationCodeInf), |
| 562 | move(l_locCodeProperty)); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | propertyMap.emplace( |
| 567 | property, propValuePair.value().get<std::string>()); |
| 568 | } |
| 569 | } |
| 570 | else if (propValuePair.value().is_array()) |
| 571 | { |
| 572 | try |
| 573 | { |
| 574 | propertyMap.emplace( |
| 575 | property, |
| 576 | propValuePair.value().get<types::BinaryVector>()); |
| 577 | } |
| 578 | catch (const nlohmann::detail::type_error& e) |
| 579 | { |
| 580 | std::cerr << "Type exception: " << e.what() << "\n"; |
| 581 | } |
| 582 | } |
| 583 | else if (propValuePair.value().is_number()) |
| 584 | { |
| 585 | // For now assume the value is a size_t. In the future it would |
| 586 | // be nice to come up with a way to get the type from the JSON. |
| 587 | propertyMap.emplace(property, |
| 588 | propValuePair.value().get<size_t>()); |
| 589 | } |
| 590 | else if (propValuePair.value().is_object()) |
| 591 | { |
| 592 | const std::string& record = |
| 593 | propValuePair.value().value("recordName", ""); |
| 594 | const std::string& keyword = |
| 595 | propValuePair.value().value("keywordName", ""); |
| 596 | const std::string& encoding = |
| 597 | propValuePair.value().value("encoding", ""); |
| 598 | |
| 599 | if (auto ipzVpdMap = |
| 600 | std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 601 | { |
| 602 | if (!record.empty() && !keyword.empty() && |
| 603 | (*ipzVpdMap).count(record) && |
| 604 | (*ipzVpdMap).at(record).count(keyword)) |
| 605 | { |
| 606 | auto encoded = vpdSpecificUtility::encodeKeyword( |
| 607 | ((*ipzVpdMap).at(record).at(keyword)), encoding); |
| 608 | propertyMap.emplace(property, encoded); |
| 609 | } |
| 610 | } |
| 611 | else if (auto kwdVpdMap = |
| 612 | std::get_if<types::KeywordVpdMap>(&parsedVpdMap)) |
| 613 | { |
| 614 | if (!keyword.empty() && (*kwdVpdMap).count(keyword)) |
| 615 | { |
| 616 | if (auto kwValue = std::get_if<types::BinaryVector>( |
| 617 | &(*kwdVpdMap).at(keyword))) |
| 618 | { |
| 619 | auto encodedValue = |
| 620 | vpdSpecificUtility::encodeKeyword( |
| 621 | std::string((*kwValue).begin(), |
| 622 | (*kwValue).end()), |
| 623 | encoding); |
| 624 | |
| 625 | propertyMap.emplace(property, encodedValue); |
| 626 | } |
| 627 | else if (auto kwValue = std::get_if<std::string>( |
| 628 | &(*kwdVpdMap).at(keyword))) |
| 629 | { |
| 630 | auto encodedValue = |
| 631 | vpdSpecificUtility::encodeKeyword( |
| 632 | std::string((*kwValue).begin(), |
| 633 | (*kwValue).end()), |
| 634 | encoding); |
| 635 | |
| 636 | propertyMap.emplace(property, encodedValue); |
| 637 | } |
| 638 | else if (auto uintValue = std::get_if<size_t>( |
| 639 | &(*kwdVpdMap).at(keyword))) |
| 640 | { |
| 641 | propertyMap.emplace(property, *uintValue); |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | logging::logMessage( |
| 646 | "Unknown keyword found, Keywrod = " + keyword); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | vpdSpecificUtility::insertOrMerge(interfaceMap, interface, |
| 653 | move(propertyMap)); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | bool Worker::isCPUIOGoodOnly(const std::string& i_pgKeyword) |
| 658 | { |
| 659 | const unsigned char l_io[] = { |
| 660 | 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, |
| 661 | 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF, 0xE7, 0xF9, 0xFF}; |
| 662 | |
| 663 | // EQ0 index (in PG keyword) starts at 97 (with offset starting from 0). |
| 664 | // Each EQ carries 3 bytes of data. Totally there are 8 EQs. If all EQs' |
| 665 | // value equals 0xE7F9FF, then the cpu has no good cores and its treated as |
| 666 | // IO. |
| 667 | if (memcmp(l_io, i_pgKeyword.data() + constants::INDEX_OF_EQ0_IN_PG, |
| 668 | constants::SIZE_OF_8EQ_IN_PG) == 0) |
| 669 | { |
| 670 | return true; |
| 671 | } |
| 672 | |
| 673 | // The CPU is not an IO |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | bool Worker::primeInventory(const std::string& i_vpdFilePath) |
| 678 | { |
| 679 | if (i_vpdFilePath.empty()) |
| 680 | { |
| 681 | logging::logMessage("Empty VPD file path given"); |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | if (m_parsedJson.empty()) |
| 686 | { |
| 687 | logging::logMessage("Empty JSON detected for " + i_vpdFilePath); |
| 688 | return false; |
| 689 | } |
| 690 | else if (!m_parsedJson["frus"].contains(i_vpdFilePath)) |
| 691 | { |
| 692 | logging::logMessage("File " + i_vpdFilePath + |
| 693 | ", is not found in the system config JSON file."); |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | types::ObjectMap l_objectInterfaceMap; |
| 698 | for (const auto& l_Fru : m_parsedJson["frus"][i_vpdFilePath]) |
| 699 | { |
| 700 | types::InterfaceMap l_interfaces; |
| 701 | sdbusplus::message::object_path l_fruObjectPath(l_Fru["inventoryPath"]); |
| 702 | |
| 703 | if (l_Fru.contains("ccin")) |
| 704 | { |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | if (l_Fru.contains("noprime") && l_Fru.value("noprime", false)) |
| 709 | { |
| 710 | continue; |
| 711 | } |
| 712 | |
Souvik Roy | 6a9553c | 2025-02-07 01:16:32 -0600 | [diff] [blame] | 713 | // Reset data under PIM for this FRU only if the FRU is not synthesized |
| 714 | // and we handle it's Present property. |
| 715 | if (isPresentPropertyHandlingRequired(l_Fru)) |
| 716 | { |
| 717 | // Clear data under PIM if already exists. |
| 718 | vpdSpecificUtility::resetDataUnderPIM( |
| 719 | std::string(l_Fru["inventoryPath"]), l_interfaces); |
| 720 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 721 | |
| 722 | // Add extra interfaces mentioned in the Json config file |
| 723 | if (l_Fru.contains("extraInterfaces")) |
| 724 | { |
| 725 | populateInterfaces(l_Fru["extraInterfaces"], l_interfaces, |
| 726 | std::monostate{}); |
| 727 | } |
| 728 | |
| 729 | types::PropertyMap l_propertyValueMap; |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 730 | |
Souvik Roy | 6a9553c | 2025-02-07 01:16:32 -0600 | [diff] [blame] | 731 | // Update Present property for this FRU only if we handle Present |
| 732 | // property for the FRU. |
| 733 | if (isPresentPropertyHandlingRequired(l_Fru)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 734 | { |
Souvik Roy | 6a9553c | 2025-02-07 01:16:32 -0600 | [diff] [blame] | 735 | l_propertyValueMap.emplace("Present", false); |
| 736 | |
| 737 | // TODO: Present based on file will be taken care in future. |
| 738 | // By default present is set to false for FRU at the time of |
| 739 | // priming. Once collection goes through, it will be set to true in |
| 740 | // that flow. |
| 741 | /*if (std::filesystem::exists(i_vpdFilePath)) |
| 742 | { |
| 743 | l_propertyValueMap["Present"] = true; |
| 744 | }*/ |
| 745 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 746 | |
| 747 | vpdSpecificUtility::insertOrMerge(l_interfaces, |
| 748 | "xyz.openbmc_project.Inventory.Item", |
| 749 | move(l_propertyValueMap)); |
| 750 | |
| 751 | if (l_Fru.value("inherit", true) && |
| 752 | m_parsedJson.contains("commonInterfaces")) |
| 753 | { |
| 754 | populateInterfaces(m_parsedJson["commonInterfaces"], l_interfaces, |
| 755 | std::monostate{}); |
| 756 | } |
| 757 | |
| 758 | processFunctionalProperty(l_Fru["inventoryPath"], l_interfaces); |
| 759 | processEnabledProperty(l_Fru["inventoryPath"], l_interfaces); |
| 760 | |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 761 | // Emplace the default state of FRU VPD collection |
| 762 | types::PropertyMap l_fruCollectionProperty = { |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 763 | {"Status", constants::vpdCollectionNotStarted}}; |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 764 | |
| 765 | vpdSpecificUtility::insertOrMerge(l_interfaces, |
| 766 | constants::vpdCollectionInterface, |
| 767 | std::move(l_fruCollectionProperty)); |
| 768 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 769 | l_objectInterfaceMap.emplace(std::move(l_fruObjectPath), |
| 770 | std::move(l_interfaces)); |
| 771 | } |
| 772 | |
| 773 | // Notify PIM |
| 774 | if (!dbusUtility::callPIM(move(l_objectInterfaceMap))) |
| 775 | { |
| 776 | logging::logMessage("Call to PIM failed for VPD file " + i_vpdFilePath); |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | return true; |
| 781 | } |
| 782 | |
| 783 | void Worker::processEmbeddedAndSynthesizedFrus(const nlohmann::json& singleFru, |
| 784 | types::InterfaceMap& interfaces) |
| 785 | { |
| 786 | // embedded property(true or false) says whether the subfru is embedded |
| 787 | // into the parent fru (or) not. VPD sets Present property only for |
| 788 | // embedded frus. If the subfru is not an embedded FRU, the subfru may |
| 789 | // or may not be physically present. Those non embedded frus will always |
| 790 | // have Present=false irrespective of its physical presence or absence. |
| 791 | // Eg: nvme drive in nvme slot is not an embedded FRU. So don't set |
| 792 | // Present to true for such sub frus. |
| 793 | // Eg: ethernet port is embedded into bmc card. So set Present to true |
| 794 | // for such sub frus. Also donot populate present property for embedded |
| 795 | // subfru which is synthesized. Currently there is no subfru which are |
| 796 | // both embedded and synthesized. But still the case is handled here. |
| 797 | |
| 798 | // Check if its required to handle presence for this FRU. |
| 799 | if (singleFru.value("handlePresence", true)) |
| 800 | { |
| 801 | types::PropertyMap presProp; |
| 802 | presProp.emplace("Present", true); |
| 803 | vpdSpecificUtility::insertOrMerge( |
| 804 | interfaces, "xyz.openbmc_project.Inventory.Item", move(presProp)); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void Worker::processExtraInterfaces(const nlohmann::json& singleFru, |
| 809 | types::InterfaceMap& interfaces, |
| 810 | const types::VPDMapVariant& parsedVpdMap) |
| 811 | { |
| 812 | populateInterfaces(singleFru["extraInterfaces"], interfaces, parsedVpdMap); |
| 813 | if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 814 | { |
| 815 | if (singleFru["extraInterfaces"].contains( |
| 816 | "xyz.openbmc_project.Inventory.Item.Cpu")) |
| 817 | { |
| 818 | auto itrToRec = (*ipzVpdMap).find("CP00"); |
| 819 | if (itrToRec == (*ipzVpdMap).end()) |
| 820 | { |
| 821 | return; |
| 822 | } |
| 823 | |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 824 | const std::string pgKeywordValue{ |
| 825 | vpdSpecificUtility::getKwVal(itrToRec->second, "PG")}; |
| 826 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 827 | if (!pgKeywordValue.empty()) |
| 828 | { |
| 829 | if (isCPUIOGoodOnly(pgKeywordValue)) |
| 830 | { |
| 831 | interfaces["xyz.openbmc_project.Inventory.Item"] |
| 832 | ["PrettyName"] = "IO Module"; |
| 833 | } |
| 834 | } |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 835 | else |
| 836 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 837 | throw DataException(std::string(__FUNCTION__) + |
| 838 | "Failed to get value for keyword PG"); |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 839 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void Worker::processCopyRecordFlag(const nlohmann::json& singleFru, |
| 845 | const types::VPDMapVariant& parsedVpdMap, |
| 846 | types::InterfaceMap& interfaces) |
| 847 | { |
| 848 | if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 849 | { |
| 850 | for (const auto& record : singleFru["copyRecords"]) |
| 851 | { |
| 852 | const std::string& recordName = record; |
| 853 | if ((*ipzVpdMap).find(recordName) != (*ipzVpdMap).end()) |
| 854 | { |
| 855 | populateIPZVPDpropertyMap(interfaces, |
| 856 | (*ipzVpdMap).at(recordName), |
| 857 | constants::ipzVpdInf + recordName); |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | void Worker::processInheritFlag(const types::VPDMapVariant& parsedVpdMap, |
| 864 | types::InterfaceMap& interfaces) |
| 865 | { |
| 866 | if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 867 | { |
| 868 | for (const auto& [recordName, kwdValueMap] : *ipzVpdMap) |
| 869 | { |
| 870 | populateIPZVPDpropertyMap(interfaces, kwdValueMap, |
| 871 | constants::ipzVpdInf + recordName); |
| 872 | } |
| 873 | } |
| 874 | else if (auto kwdVpdMap = std::get_if<types::KeywordVpdMap>(&parsedVpdMap)) |
| 875 | { |
| 876 | populateKwdVPDpropertyMap(*kwdVpdMap, interfaces); |
| 877 | } |
| 878 | |
| 879 | if (m_parsedJson.contains("commonInterfaces")) |
| 880 | { |
| 881 | populateInterfaces(m_parsedJson["commonInterfaces"], interfaces, |
| 882 | parsedVpdMap); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | bool Worker::processFruWithCCIN(const nlohmann::json& singleFru, |
| 887 | const types::VPDMapVariant& parsedVpdMap) |
| 888 | { |
| 889 | if (auto ipzVPDMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 890 | { |
| 891 | auto itrToRec = (*ipzVPDMap).find("VINI"); |
| 892 | if (itrToRec == (*ipzVPDMap).end()) |
| 893 | { |
| 894 | return false; |
| 895 | } |
| 896 | |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 897 | std::string ccinFromVpd{ |
| 898 | vpdSpecificUtility::getKwVal(itrToRec->second, "CC")}; |
| 899 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 900 | if (ccinFromVpd.empty()) |
| 901 | { |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | transform(ccinFromVpd.begin(), ccinFromVpd.end(), ccinFromVpd.begin(), |
| 906 | ::toupper); |
| 907 | |
| 908 | std::vector<std::string> ccinList; |
| 909 | for (std::string ccin : singleFru["ccin"]) |
| 910 | { |
| 911 | transform(ccin.begin(), ccin.end(), ccin.begin(), ::toupper); |
| 912 | ccinList.push_back(ccin); |
| 913 | } |
| 914 | |
| 915 | if (ccinList.empty()) |
| 916 | { |
| 917 | return false; |
| 918 | } |
| 919 | |
| 920 | if (find(ccinList.begin(), ccinList.end(), ccinFromVpd) == |
| 921 | ccinList.end()) |
| 922 | { |
| 923 | return false; |
| 924 | } |
| 925 | } |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | void Worker::processFunctionalProperty(const std::string& i_inventoryObjPath, |
| 930 | types::InterfaceMap& io_interfaces) |
| 931 | { |
| 932 | if (!dbusUtility::isChassisPowerOn()) |
| 933 | { |
| 934 | std::array<const char*, 1> l_operationalStatusInf = { |
| 935 | constants::operationalStatusInf}; |
| 936 | |
| 937 | auto mapperObjectMap = dbusUtility::getObjectMap( |
| 938 | i_inventoryObjPath, l_operationalStatusInf); |
| 939 | |
| 940 | // If the object has been found. Check if it is under PIM. |
| 941 | if (mapperObjectMap.size() != 0) |
| 942 | { |
| 943 | for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap) |
| 944 | { |
| 945 | if (l_serviceName == constants::pimServiceName) |
| 946 | { |
| 947 | // The object is already under PIM. No need to process |
| 948 | // again. Retain the old value. |
| 949 | return; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | // Implies value is not there in D-Bus. Populate it with default |
| 955 | // value "true". |
| 956 | types::PropertyMap l_functionalProp; |
| 957 | l_functionalProp.emplace("Functional", true); |
| 958 | vpdSpecificUtility::insertOrMerge(io_interfaces, |
| 959 | constants::operationalStatusInf, |
| 960 | move(l_functionalProp)); |
| 961 | } |
| 962 | |
| 963 | // if chassis is power on. Functional property should be there on D-Bus. |
| 964 | // Don't process. |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | void Worker::processEnabledProperty(const std::string& i_inventoryObjPath, |
| 969 | types::InterfaceMap& io_interfaces) |
| 970 | { |
| 971 | if (!dbusUtility::isChassisPowerOn()) |
| 972 | { |
| 973 | std::array<const char*, 1> l_enableInf = {constants::enableInf}; |
| 974 | |
| 975 | auto mapperObjectMap = |
| 976 | dbusUtility::getObjectMap(i_inventoryObjPath, l_enableInf); |
| 977 | |
| 978 | // If the object has been found. Check if it is under PIM. |
| 979 | if (mapperObjectMap.size() != 0) |
| 980 | { |
| 981 | for (const auto& [l_serviceName, l_interfaceLsit] : mapperObjectMap) |
| 982 | { |
| 983 | if (l_serviceName == constants::pimServiceName) |
| 984 | { |
| 985 | // The object is already under PIM. No need to process |
| 986 | // again. Retain the old value. |
| 987 | return; |
| 988 | } |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | // Implies value is not there in D-Bus. Populate it with default |
| 993 | // value "true". |
| 994 | types::PropertyMap l_enabledProp; |
| 995 | l_enabledProp.emplace("Enabled", true); |
| 996 | vpdSpecificUtility::insertOrMerge(io_interfaces, constants::enableInf, |
| 997 | move(l_enabledProp)); |
| 998 | } |
| 999 | |
| 1000 | // if chassis is power on. Enabled property should be there on D-Bus. |
| 1001 | // Don't process. |
| 1002 | return; |
| 1003 | } |
| 1004 | |
| 1005 | void Worker::populateDbus(const types::VPDMapVariant& parsedVpdMap, |
| 1006 | types::ObjectMap& objectInterfaceMap, |
| 1007 | const std::string& vpdFilePath) |
| 1008 | { |
| 1009 | if (vpdFilePath.empty()) |
| 1010 | { |
| 1011 | throw std::runtime_error( |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1012 | std::string(__FUNCTION__) + |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1013 | "Invalid parameter passed to populateDbus API."); |
| 1014 | } |
| 1015 | |
| 1016 | // JSON config is mandatory for processing of "if". Add "else" for any |
| 1017 | // processing without config JSON. |
| 1018 | if (!m_parsedJson.empty()) |
| 1019 | { |
| 1020 | types::InterfaceMap interfaces; |
| 1021 | |
| 1022 | for (const auto& aFru : m_parsedJson["frus"][vpdFilePath]) |
| 1023 | { |
| 1024 | const auto& inventoryPath = aFru["inventoryPath"]; |
| 1025 | sdbusplus::message::object_path fruObjectPath(inventoryPath); |
| 1026 | if (aFru.contains("ccin")) |
| 1027 | { |
| 1028 | if (!processFruWithCCIN(aFru, parsedVpdMap)) |
| 1029 | { |
| 1030 | continue; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (aFru.value("inherit", true)) |
| 1035 | { |
| 1036 | processInheritFlag(parsedVpdMap, interfaces); |
| 1037 | } |
| 1038 | |
| 1039 | // If specific record needs to be copied. |
| 1040 | if (aFru.contains("copyRecords")) |
| 1041 | { |
| 1042 | processCopyRecordFlag(aFru, parsedVpdMap, interfaces); |
| 1043 | } |
| 1044 | |
| 1045 | if (aFru.contains("extraInterfaces")) |
| 1046 | { |
| 1047 | // Process extra interfaces w.r.t a FRU. |
| 1048 | processExtraInterfaces(aFru, interfaces, parsedVpdMap); |
| 1049 | } |
| 1050 | |
| 1051 | // Process FRUS which are embedded in the parent FRU and whose VPD |
| 1052 | // will be synthesized. |
| 1053 | if ((aFru.value("embedded", true)) && |
| 1054 | (!aFru.value("synthesized", false))) |
| 1055 | { |
| 1056 | processEmbeddedAndSynthesizedFrus(aFru, interfaces); |
| 1057 | } |
| 1058 | |
| 1059 | processFunctionalProperty(inventoryPath, interfaces); |
| 1060 | processEnabledProperty(inventoryPath, interfaces); |
| 1061 | |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1062 | // Update collection status as successful |
| 1063 | types::PropertyMap l_collectionProperty = { |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 1064 | {"Status", constants::vpdCollectionCompleted}}; |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1065 | |
| 1066 | vpdSpecificUtility::insertOrMerge(interfaces, |
| 1067 | constants::vpdCollectionInterface, |
| 1068 | std::move(l_collectionProperty)); |
| 1069 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1070 | objectInterfaceMap.emplace(std::move(fruObjectPath), |
| 1071 | std::move(interfaces)); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Patrick Williams | 43fedab | 2025-02-03 14:28:05 -0500 | [diff] [blame] | 1076 | std::string Worker::createAssetTagString( |
| 1077 | const types::VPDMapVariant& i_parsedVpdMap) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1078 | { |
| 1079 | std::string l_assetTag; |
| 1080 | |
| 1081 | // system VPD will be in IPZ format. |
| 1082 | if (auto l_parsedVpdMap = std::get_if<types::IPZVpdMap>(&i_parsedVpdMap)) |
| 1083 | { |
| 1084 | auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS); |
| 1085 | if (l_itrToVsys != (*l_parsedVpdMap).end()) |
| 1086 | { |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 1087 | const std::string l_tmKwdValue{vpdSpecificUtility::getKwVal( |
| 1088 | l_itrToVsys->second, constants::kwdTM)}; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1089 | |
Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 1090 | if (l_tmKwdValue.empty()) |
| 1091 | { |
| 1092 | throw std::runtime_error( |
| 1093 | std::string("Failed to get value for keyword [") + |
| 1094 | constants::kwdTM + |
| 1095 | std::string("] while creating Asset tag.")); |
| 1096 | } |
| 1097 | |
| 1098 | const std::string l_seKwdValue{vpdSpecificUtility::getKwVal( |
| 1099 | l_itrToVsys->second, constants::kwdSE)}; |
| 1100 | |
| 1101 | if (l_seKwdValue.empty()) |
| 1102 | { |
| 1103 | throw std::runtime_error( |
| 1104 | std::string("Failed to get value for keyword [") + |
| 1105 | constants::kwdSE + |
| 1106 | std::string("] while creating Asset tag.")); |
| 1107 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1108 | |
| 1109 | l_assetTag = std::string{"Server-"} + l_tmKwdValue + |
| 1110 | std::string{"-"} + l_seKwdValue; |
| 1111 | } |
| 1112 | else |
| 1113 | { |
| 1114 | throw std::runtime_error( |
| 1115 | "VSYS record not found in parsed VPD map to create Asset tag."); |
| 1116 | } |
| 1117 | } |
| 1118 | else |
| 1119 | { |
| 1120 | throw std::runtime_error( |
| 1121 | "Invalid VPD type recieved to create Asset tag."); |
| 1122 | } |
| 1123 | |
| 1124 | return l_assetTag; |
| 1125 | } |
| 1126 | |
| 1127 | void Worker::publishSystemVPD(const types::VPDMapVariant& parsedVpdMap) |
| 1128 | { |
| 1129 | types::ObjectMap objectInterfaceMap; |
| 1130 | |
| 1131 | if (std::get_if<types::IPZVpdMap>(&parsedVpdMap)) |
| 1132 | { |
| 1133 | populateDbus(parsedVpdMap, objectInterfaceMap, SYSTEM_VPD_FILE_PATH); |
| 1134 | |
| 1135 | try |
| 1136 | { |
| 1137 | if (m_isFactoryResetDone) |
| 1138 | { |
| 1139 | const auto& l_assetTag = createAssetTagString(parsedVpdMap); |
| 1140 | |
| 1141 | auto l_itrToSystemPath = objectInterfaceMap.find( |
| 1142 | sdbusplus::message::object_path(constants::systemInvPath)); |
| 1143 | if (l_itrToSystemPath == objectInterfaceMap.end()) |
| 1144 | { |
| 1145 | throw std::runtime_error( |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 1146 | "Asset tag update failed. System Path not found in object map."); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | types::PropertyMap l_assetTagProperty; |
| 1150 | l_assetTagProperty.emplace("AssetTag", l_assetTag); |
| 1151 | |
| 1152 | (l_itrToSystemPath->second) |
| 1153 | .emplace(constants::assetTagInf, |
| 1154 | std::move(l_assetTagProperty)); |
| 1155 | } |
| 1156 | } |
| 1157 | catch (const std::exception& l_ex) |
| 1158 | { |
| 1159 | EventLogger::createSyncPel( |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 1160 | EventLogger::getErrorType(l_ex), types::SeverityType::Warning, |
| 1161 | __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex), |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1162 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
| 1163 | } |
| 1164 | |
| 1165 | // Notify PIM |
| 1166 | if (!dbusUtility::callPIM(move(objectInterfaceMap))) |
| 1167 | { |
| 1168 | throw std::runtime_error("Call to PIM failed for system VPD"); |
| 1169 | } |
| 1170 | } |
| 1171 | else |
| 1172 | { |
| 1173 | throw DataException("Invalid format of parsed VPD map."); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | bool Worker::processPreAction(const std::string& i_vpdFilePath, |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1178 | const std::string& i_flagToProcess, |
| 1179 | uint16_t& i_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1180 | { |
| 1181 | if (i_vpdFilePath.empty() || i_flagToProcess.empty()) |
| 1182 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1183 | i_errCode = error_code::INVALID_INPUT_PARAMETER; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | if ((!jsonUtility::executeBaseAction(m_parsedJson, "preAction", |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1188 | i_vpdFilePath, i_flagToProcess, |
| 1189 | i_errCode)) && |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1190 | (i_flagToProcess.compare("collection") == constants::STR_CMP_SUCCESS)) |
| 1191 | { |
| 1192 | // TODO: Need a way to delete inventory object from Dbus and persisted |
| 1193 | // data section in case any FRU is not present or there is any |
| 1194 | // problem in collecting it. Once it has been deleted, it can be |
| 1195 | // re-created in the flow of priming the inventory. This needs to be |
| 1196 | // done either here or in the exception section of "parseAndPublishVPD" |
| 1197 | // API. Any failure in the process of collecting FRU will land up in the |
| 1198 | // excpetion of "parseAndPublishVPD". |
| 1199 | |
| 1200 | // If the FRU is not there, clear the VINI/CCIN data. |
| 1201 | // Enity manager probes for this keyword to look for this |
| 1202 | // FRU, now if the data is persistent on BMC and FRU is |
| 1203 | // removed this can lead to ambiguity. Hence clearing this |
| 1204 | // Keyword if FRU is absent. |
| 1205 | const auto& inventoryPath = |
| 1206 | m_parsedJson["frus"][i_vpdFilePath].at(0).value("inventoryPath", |
| 1207 | ""); |
| 1208 | |
| 1209 | if (!inventoryPath.empty()) |
| 1210 | { |
| 1211 | types::ObjectMap l_pimObjMap{ |
| 1212 | {inventoryPath, |
| 1213 | {{constants::kwdVpdInf, |
| 1214 | {{constants::kwdCCIN, types::BinaryVector{}}}}}}}; |
| 1215 | |
| 1216 | if (!dbusUtility::callPIM(std::move(l_pimObjMap))) |
| 1217 | { |
| 1218 | logging::logMessage( |
| 1219 | "Call to PIM failed for file " + i_vpdFilePath); |
| 1220 | } |
| 1221 | } |
| 1222 | else |
| 1223 | { |
| 1224 | logging::logMessage( |
| 1225 | "Inventory path is empty in Json for file " + i_vpdFilePath); |
| 1226 | } |
| 1227 | |
| 1228 | return false; |
| 1229 | } |
| 1230 | return true; |
| 1231 | } |
| 1232 | |
| 1233 | bool Worker::processPostAction( |
| 1234 | const std::string& i_vpdFruPath, const std::string& i_flagToProcess, |
| 1235 | const std::optional<types::VPDMapVariant> i_parsedVpd) |
| 1236 | { |
| 1237 | if (i_vpdFruPath.empty() || i_flagToProcess.empty()) |
| 1238 | { |
| 1239 | logging::logMessage( |
| 1240 | "Invalid input parameter. Abort processing post action"); |
| 1241 | return false; |
| 1242 | } |
| 1243 | |
| 1244 | // Check if post action tag is to be triggered in the flow of collection |
| 1245 | // based on some CCIN value? |
| 1246 | if (m_parsedJson["frus"][i_vpdFruPath] |
| 1247 | .at(0)["postAction"][i_flagToProcess] |
| 1248 | .contains("ccin")) |
| 1249 | { |
| 1250 | if (!i_parsedVpd.has_value()) |
| 1251 | { |
| 1252 | logging::logMessage("Empty VPD Map"); |
| 1253 | return false; |
| 1254 | } |
| 1255 | |
| 1256 | // CCIN match is required to process post action for this FRU as it |
| 1257 | // contains the flag. |
| 1258 | if (!vpdSpecificUtility::findCcinInVpd( |
| 1259 | m_parsedJson["frus"][i_vpdFruPath].at( |
| 1260 | 0)["postAction"]["collection"], |
| 1261 | i_parsedVpd.value())) |
| 1262 | { |
| 1263 | // If CCIN is not found, implies post action processing is not |
| 1264 | // required for this FRU. Let the flow continue. |
| 1265 | return true; |
| 1266 | } |
| 1267 | } |
| 1268 | |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1269 | uint16_t l_errCode = 0; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1270 | if (!jsonUtility::executeBaseAction(m_parsedJson, "postAction", |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1271 | i_vpdFruPath, i_flagToProcess, |
| 1272 | l_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1273 | { |
| 1274 | logging::logMessage( |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1275 | "Execution of post action failed for path: " + i_vpdFruPath + |
| 1276 | " . Reason: " + vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1277 | |
| 1278 | // If post action was required and failed only in that case return |
| 1279 | // false. In all other case post action is considered passed. |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
| 1283 | return true; |
| 1284 | } |
| 1285 | |
| 1286 | types::VPDMapVariant Worker::parseVpdFile(const std::string& i_vpdFilePath) |
| 1287 | { |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1288 | try |
| 1289 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1290 | if (i_vpdFilePath.empty()) |
| 1291 | { |
| 1292 | throw std::runtime_error( |
| 1293 | std::string(__FUNCTION__) + |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1294 | " Empty VPD file path passed. Abort processing"); |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1295 | } |
| 1296 | |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1297 | bool isPreActionRequired = false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1298 | if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath, |
| 1299 | "preAction", "collection")) |
| 1300 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1301 | isPreActionRequired = true; |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1302 | uint16_t l_errCode = 0; |
| 1303 | if (!processPreAction(i_vpdFilePath, "collection", l_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1304 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1305 | if (l_errCode == error_code::DEVICE_NOT_PRESENT) |
| 1306 | { |
| 1307 | logging::logMessage( |
| 1308 | vpdSpecificUtility::getErrCodeMsg(l_errCode) + |
| 1309 | i_vpdFilePath); |
| 1310 | // Presence pin has been read successfully and has been read |
| 1311 | // as false, so this is not a failure case, hence returning |
| 1312 | // empty variant so that pre action is not marked as failed. |
| 1313 | return types::VPDMapVariant{}; |
| 1314 | } |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1315 | throw std::runtime_error( |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1316 | std::string(__FUNCTION__) + |
| 1317 | " Pre-Action failed with error: " + |
| 1318 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | if (!std::filesystem::exists(i_vpdFilePath)) |
| 1323 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1324 | if (isPreActionRequired) |
| 1325 | { |
| 1326 | throw std::runtime_error( |
| 1327 | std::string(__FUNCTION__) + " Could not find file path " + |
| 1328 | i_vpdFilePath + "Skipping parser trigger for the EEPROM"); |
| 1329 | } |
| 1330 | return types::VPDMapVariant{}; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | std::shared_ptr<Parser> vpdParser = |
| 1334 | std::make_shared<Parser>(i_vpdFilePath, m_parsedJson); |
| 1335 | |
| 1336 | types::VPDMapVariant l_parsedVpd = vpdParser->parse(); |
| 1337 | |
| 1338 | // Before returning, as collection is over, check if FRU qualifies for |
| 1339 | // any post action in the flow of collection. |
| 1340 | // Note: Don't change the order, post action needs to be processed only |
| 1341 | // after collection for FRU is successfully done. |
| 1342 | if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath, |
| 1343 | "postAction", "collection")) |
| 1344 | { |
| 1345 | if (!processPostAction(i_vpdFilePath, "collection", l_parsedVpd)) |
| 1346 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1347 | // Post action was required but failed while executing. |
| 1348 | // Behaviour can be undefined. |
| 1349 | EventLogger::createSyncPel( |
| 1350 | types::ErrorType::InternalFailure, |
| 1351 | types::SeverityType::Warning, __FILE__, __FUNCTION__, 0, |
| 1352 | std::string("Required post action failed for path [" + |
| 1353 | i_vpdFilePath + "]"), |
| 1354 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | return l_parsedVpd; |
| 1359 | } |
| 1360 | catch (std::exception& l_ex) |
| 1361 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1362 | uint16_t l_errCode = 0; |
Souvik Roy | 37c6bef | 2025-07-17 00:55:59 -0500 | [diff] [blame] | 1363 | std::string l_exMsg{ |
| 1364 | std::string(__FUNCTION__) + " : VPD parsing failed for " + |
| 1365 | i_vpdFilePath + " due to error: " + l_ex.what()}; |
| 1366 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1367 | // If post fail action is required, execute it. |
| 1368 | if (jsonUtility::isActionRequired(m_parsedJson, i_vpdFilePath, |
Sunny Srivastava | 4c16438 | 2025-01-28 03:17:33 -0600 | [diff] [blame] | 1369 | "postFailAction", "collection")) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1370 | { |
| 1371 | if (!jsonUtility::executePostFailAction(m_parsedJson, i_vpdFilePath, |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1372 | "collection", l_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1373 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1374 | l_exMsg += ". Post fail action also failed. Error : " + |
| 1375 | vpdSpecificUtility::getErrCodeMsg(l_errCode) + |
| 1376 | " Aborting collection for this FRU."; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
Souvik Roy | 37c6bef | 2025-07-17 00:55:59 -0500 | [diff] [blame] | 1380 | if (typeid(l_ex) == typeid(DataException)) |
| 1381 | { |
| 1382 | throw DataException(l_exMsg); |
| 1383 | } |
| 1384 | else if (typeid(l_ex) == typeid(EccException)) |
| 1385 | { |
| 1386 | throw EccException(l_exMsg); |
| 1387 | } |
| 1388 | throw std::runtime_error(l_exMsg); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Patrick Williams | 43fedab | 2025-02-03 14:28:05 -0500 | [diff] [blame] | 1392 | std::tuple<bool, std::string> Worker::parseAndPublishVPD( |
| 1393 | const std::string& i_vpdFilePath) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1394 | { |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1395 | std::string l_inventoryPath{}; |
| 1396 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1397 | try |
| 1398 | { |
| 1399 | m_semaphore.acquire(); |
| 1400 | |
| 1401 | // Thread launched. |
| 1402 | m_mutex.lock(); |
| 1403 | m_activeCollectionThreadCount++; |
| 1404 | m_mutex.unlock(); |
| 1405 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1406 | uint16_t l_errCode = 0; |
| 1407 | |
| 1408 | // Set CollectionStatus as InProgress. Since it's an intermediate state |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1409 | // D-bus set-property call is good enough to update the status. |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1410 | l_inventoryPath = jsonUtility::getInventoryObjPathFromJson( |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1411 | m_parsedJson, i_vpdFilePath, l_errCode); |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1412 | |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1413 | if (!l_inventoryPath.empty()) |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1414 | { |
Sunny Srivastava | 995e1c2 | 2025-08-28 03:13:00 -0500 | [diff] [blame] | 1415 | // save time stamp under PIM. |
| 1416 | vpdSpecificUtility::saveTimeStampInPim(l_inventoryPath, |
| 1417 | "StartTime"); |
| 1418 | |
RekhaAparna01 | c532b18 | 2025-02-19 19:56:49 -0600 | [diff] [blame] | 1419 | if (!dbusUtility::writeDbusProperty( |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1420 | jsonUtility::getServiceName(m_parsedJson, l_inventoryPath), |
| 1421 | l_inventoryPath, constants::vpdCollectionInterface, |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 1422 | "Status", |
RekhaAparna01 | c532b18 | 2025-02-19 19:56:49 -0600 | [diff] [blame] | 1423 | types::DbusVariantType{constants::vpdCollectionInProgress})) |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1424 | { |
| 1425 | logging::logMessage( |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 1426 | "Unable to set collection Status as InProgress for " + |
RekhaAparna01 | c532b18 | 2025-02-19 19:56:49 -0600 | [diff] [blame] | 1427 | i_vpdFilePath + ". Error : " + "DBus write failed"); |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1428 | } |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1429 | } |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1430 | else if (l_errCode) |
| 1431 | { |
| 1432 | logging::logMessage( |
| 1433 | "Failed to get inventory path for FRU [" + i_vpdFilePath + |
| 1434 | "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1435 | } |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1436 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1437 | const types::VPDMapVariant& parsedVpdMap = parseVpdFile(i_vpdFilePath); |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1438 | if (!std::holds_alternative<std::monostate>(parsedVpdMap)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1439 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1440 | types::ObjectMap objectInterfaceMap; |
| 1441 | populateDbus(parsedVpdMap, objectInterfaceMap, i_vpdFilePath); |
| 1442 | |
Sunny Srivastava | 995e1c2 | 2025-08-28 03:13:00 -0500 | [diff] [blame] | 1443 | // save end time stamp under PIM. |
| 1444 | vpdSpecificUtility::saveTimeStampInPim(l_inventoryPath, |
| 1445 | "CompletedTime"); |
| 1446 | |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 1447 | // Notify PIM |
| 1448 | if (!dbusUtility::callPIM(move(objectInterfaceMap))) |
| 1449 | { |
| 1450 | throw std::runtime_error( |
| 1451 | std::string(__FUNCTION__) + |
| 1452 | "Call to PIM failed while publishing VPD."); |
| 1453 | } |
| 1454 | } |
| 1455 | else |
| 1456 | { |
| 1457 | logging::logMessage("Empty parsedVpdMap recieved for path [" + |
| 1458 | i_vpdFilePath + "]. Check PEL for reason."); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | catch (const std::exception& ex) |
| 1462 | { |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 1463 | setCollectionStatusProperty(i_vpdFilePath, |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 1464 | constants::vpdCollectionFailed); |
Priyanga Ramasamy | 1aad783 | 2024-12-12 22:13:52 -0600 | [diff] [blame] | 1465 | |
Sunny Srivastava | 995e1c2 | 2025-08-28 03:13:00 -0500 | [diff] [blame] | 1466 | // save end time stamp under PIM. |
| 1467 | vpdSpecificUtility::saveTimeStampInPim(l_inventoryPath, |
| 1468 | "CompletedTime"); |
| 1469 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1470 | // handle all the exceptions internally. Return only true/false |
| 1471 | // based on status of execution. |
| 1472 | if (typeid(ex) == std::type_index(typeid(DataException))) |
| 1473 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1474 | uint16_t l_errCode = 0; |
Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 1475 | // In case of pass1 planar, VPD can be corrupted on PCIe cards. Skip |
| 1476 | // logging error for these cases. |
| 1477 | if (vpdSpecificUtility::isPass1Planar()) |
| 1478 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1479 | std::string l_invPath = |
| 1480 | jsonUtility::getInventoryObjPathFromJson( |
| 1481 | m_parsedJson, i_vpdFilePath, l_errCode); |
| 1482 | |
| 1483 | if (l_errCode != 0) |
| 1484 | { |
| 1485 | logging::logMessage( |
| 1486 | "Failed to get inventory object path from JSON for FRU [" + |
| 1487 | i_vpdFilePath + "], error: " + |
| 1488 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1489 | } |
| 1490 | |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1491 | const std::string& l_invPathLeafValue = |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1492 | sdbusplus::message::object_path(l_invPath).filename(); |
Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 1493 | |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1494 | if ((l_invPathLeafValue.find("pcie_card", 0) != |
| 1495 | std::string::npos)) |
Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 1496 | { |
| 1497 | // skip logging any PEL for PCIe cards on pass 1 planar. |
| 1498 | return std::make_tuple(false, i_vpdFilePath); |
| 1499 | } |
| 1500 | } |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1501 | } |
Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 1502 | |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1503 | EventLogger::createSyncPel( |
Souvik Roy | 37c6bef | 2025-07-17 00:55:59 -0500 | [diff] [blame] | 1504 | EventLogger::getErrorType(ex), |
| 1505 | (typeid(ex) == typeid(DataException)) || |
| 1506 | (typeid(ex) == typeid(EccException)) |
| 1507 | ? types::SeverityType::Warning |
| 1508 | : types::SeverityType::Informational, |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1509 | __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(ex), |
| 1510 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1511 | |
| 1512 | // TODO: Figure out a way to clear data in case of any failure at |
| 1513 | // runtime. |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 1514 | |
| 1515 | // set present property to false for any error case. In future this will |
| 1516 | // be replaced by presence logic. |
Souvik Roy | 6a9553c | 2025-02-07 01:16:32 -0600 | [diff] [blame] | 1517 | // Update Present property for this FRU only if we handle Present |
| 1518 | // property for the FRU. |
| 1519 | if (isPresentPropertyHandlingRequired( |
| 1520 | m_parsedJson["frus"][i_vpdFilePath].at(0))) |
| 1521 | { |
| 1522 | setPresentProperty(i_vpdFilePath, false); |
| 1523 | } |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 1524 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1525 | m_semaphore.release(); |
| 1526 | return std::make_tuple(false, i_vpdFilePath); |
| 1527 | } |
| 1528 | m_semaphore.release(); |
| 1529 | return std::make_tuple(true, i_vpdFilePath); |
| 1530 | } |
| 1531 | |
Sunny Srivastava | 6161175 | 2025-02-04 00:29:33 -0600 | [diff] [blame] | 1532 | bool Worker::skipPathForCollection(const std::string& i_vpdFilePath) |
| 1533 | { |
| 1534 | if (i_vpdFilePath.empty()) |
| 1535 | { |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | // skip processing of system VPD again as it has been already collected. |
| 1540 | if (i_vpdFilePath == SYSTEM_VPD_FILE_PATH) |
| 1541 | { |
| 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | if (dbusUtility::isChassisPowerOn()) |
| 1546 | { |
| 1547 | // If chassis is powered on, skip collecting FRUs which are |
| 1548 | // powerOffOnly. |
| 1549 | if (jsonUtility::isFruPowerOffOnly(m_parsedJson, i_vpdFilePath)) |
| 1550 | { |
| 1551 | return true; |
| 1552 | } |
| 1553 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1554 | uint16_t l_errCode = 0; |
| 1555 | std::string l_invPath = jsonUtility::getInventoryObjPathFromJson( |
| 1556 | m_parsedJson, i_vpdFilePath, l_errCode); |
| 1557 | |
| 1558 | if (l_errCode) |
| 1559 | { |
| 1560 | logging::logMessage( |
| 1561 | "Failed to get inventory path from JSON for FRU [" + |
| 1562 | i_vpdFilePath + |
| 1563 | "], error : " + vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1564 | |
| 1565 | return false; |
| 1566 | } |
| 1567 | |
Sunny Srivastava | 6161175 | 2025-02-04 00:29:33 -0600 | [diff] [blame] | 1568 | const std::string& l_invPathLeafValue = |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 1569 | sdbusplus::message::object_path(l_invPath).filename(); |
Sunny Srivastava | 6161175 | 2025-02-04 00:29:33 -0600 | [diff] [blame] | 1570 | |
| 1571 | if ((l_invPathLeafValue.find("pcie_card", 0) != std::string::npos)) |
| 1572 | { |
| 1573 | return true; |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | return false; |
| 1578 | } |
| 1579 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1580 | void Worker::collectFrusFromJson() |
| 1581 | { |
| 1582 | // A parsed JSON file should be present to pick FRUs EEPROM paths |
| 1583 | if (m_parsedJson.empty()) |
| 1584 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1585 | throw JsonException( |
| 1586 | std::string(__FUNCTION__) + |
| 1587 | ": Config JSON is mandatory for processing of FRUs through this API.", |
| 1588 | m_configJsonPath); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | const nlohmann::json& listOfFrus = |
| 1592 | m_parsedJson["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 1593 | |
| 1594 | for (const auto& itemFRUS : listOfFrus.items()) |
| 1595 | { |
| 1596 | const std::string& vpdFilePath = itemFRUS.key(); |
| 1597 | |
Sunny Srivastava | 6161175 | 2025-02-04 00:29:33 -0600 | [diff] [blame] | 1598 | if (skipPathForCollection(vpdFilePath)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1599 | { |
| 1600 | continue; |
| 1601 | } |
| 1602 | |
Souvik Roy | 1f4c8f8 | 2025-01-23 00:37:43 -0600 | [diff] [blame] | 1603 | try |
| 1604 | { |
| 1605 | std::thread{[vpdFilePath, this]() { |
| 1606 | const auto& l_parseResult = parseAndPublishVPD(vpdFilePath); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1607 | |
Souvik Roy | 1f4c8f8 | 2025-01-23 00:37:43 -0600 | [diff] [blame] | 1608 | m_mutex.lock(); |
| 1609 | m_activeCollectionThreadCount--; |
| 1610 | m_mutex.unlock(); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1611 | |
Souvik Roy | 1f4c8f8 | 2025-01-23 00:37:43 -0600 | [diff] [blame] | 1612 | if (!m_activeCollectionThreadCount) |
| 1613 | { |
| 1614 | m_isAllFruCollected = true; |
| 1615 | } |
| 1616 | }}.detach(); |
| 1617 | } |
| 1618 | catch (const std::exception& l_ex) |
| 1619 | { |
| 1620 | // add vpdFilePath(EEPROM path) to failed list |
| 1621 | m_failedEepromPaths.push_front(vpdFilePath); |
| 1622 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | // ToDo: Move the API under IBM_SYSTEM |
| 1627 | void Worker::performBackupAndRestore(types::VPDMapVariant& io_srcVpdMap) |
| 1628 | { |
| 1629 | try |
| 1630 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1631 | uint16_t l_errCode = 0; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1632 | std::string l_backupAndRestoreCfgFilePath = |
| 1633 | m_parsedJson.value("backupRestoreConfigPath", ""); |
| 1634 | |
| 1635 | nlohmann::json l_backupAndRestoreCfgJsonObj = |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1636 | jsonUtility::getParsedJson(l_backupAndRestoreCfgFilePath, |
| 1637 | l_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1638 | |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1639 | if (l_errCode) |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1640 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1641 | throw JsonException( |
| 1642 | "JSON parsing failed for file [ " + |
| 1643 | l_backupAndRestoreCfgFilePath + " ], error : " + |
| 1644 | vpdSpecificUtility::getErrCodeMsg(l_errCode), |
| 1645 | l_backupAndRestoreCfgFilePath); |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 1646 | } |
| 1647 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1648 | // check if either of "source" or "destination" has inventory path. |
| 1649 | // this indicates that this sytem has System VPD on hardware |
| 1650 | // and other copy on D-Bus (BMC cache). |
| 1651 | if (!l_backupAndRestoreCfgJsonObj.empty() && |
| 1652 | ((l_backupAndRestoreCfgJsonObj.contains("source") && |
| 1653 | l_backupAndRestoreCfgJsonObj["source"].contains( |
| 1654 | "inventoryPath")) || |
| 1655 | (l_backupAndRestoreCfgJsonObj.contains("destination") && |
| 1656 | l_backupAndRestoreCfgJsonObj["destination"].contains( |
| 1657 | "inventoryPath")))) |
| 1658 | { |
| 1659 | BackupAndRestore l_backupAndRestoreObj(m_parsedJson); |
| 1660 | auto [l_srcVpdVariant, |
| 1661 | l_dstVpdVariant] = l_backupAndRestoreObj.backupAndRestore(); |
| 1662 | |
| 1663 | // ToDo: Revisit is this check is required or not. |
| 1664 | if (auto l_srcVpdMap = |
| 1665 | std::get_if<types::IPZVpdMap>(&l_srcVpdVariant); |
| 1666 | l_srcVpdMap && !(*l_srcVpdMap).empty()) |
| 1667 | { |
| 1668 | io_srcVpdMap = std::move(l_srcVpdVariant); |
| 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | catch (const std::exception& l_ex) |
| 1673 | { |
| 1674 | EventLogger::createSyncPel( |
Sunny Srivastava | 043955d | 2025-01-21 18:04:49 +0530 | [diff] [blame] | 1675 | EventLogger::getErrorType(l_ex), types::SeverityType::Warning, |
Sunny Srivastava | 15a189a | 2025-02-26 16:53:19 +0530 | [diff] [blame] | 1676 | __FILE__, __FUNCTION__, 0, |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1677 | std::string( |
| 1678 | "Exception caught while backup and restore VPD keyword's.") + |
Sunny Srivastava | 15a189a | 2025-02-26 16:53:19 +0530 | [diff] [blame] | 1679 | EventLogger::getErrorMsg(l_ex), |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1680 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | void Worker::deleteFruVpd(const std::string& i_dbusObjPath) |
| 1685 | { |
| 1686 | if (i_dbusObjPath.empty()) |
| 1687 | { |
| 1688 | throw std::runtime_error("Given DBus object path is empty."); |
| 1689 | } |
| 1690 | |
| 1691 | const std::string& l_fruPath = |
| 1692 | jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath); |
| 1693 | |
| 1694 | try |
| 1695 | { |
| 1696 | auto l_presentPropValue = dbusUtility::readDbusProperty( |
| 1697 | constants::pimServiceName, i_dbusObjPath, |
| 1698 | constants::inventoryItemInf, "Present"); |
| 1699 | |
| 1700 | if (auto l_value = std::get_if<bool>(&l_presentPropValue)) |
| 1701 | { |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1702 | uint16_t l_errCode = 0; |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1703 | // check if FRU's Present property is handled by vpd-manager |
| 1704 | const auto& l_isFruPresenceHandled = |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1705 | jsonUtility::isFruPresenceHandled(m_parsedJson, l_fruPath, |
| 1706 | l_errCode); |
| 1707 | |
| 1708 | if (l_errCode) |
| 1709 | { |
| 1710 | throw std::runtime_error( |
| 1711 | "Failed to check if FRU's presence is handled, reason: " + |
| 1712 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1713 | } |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1714 | |
| 1715 | if (!(*l_value) && l_isFruPresenceHandled) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1716 | { |
| 1717 | throw std::runtime_error("Given FRU is not present"); |
| 1718 | } |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1719 | else if (*l_value && !l_isFruPresenceHandled) |
| 1720 | { |
| 1721 | throw std::runtime_error( |
| 1722 | "Given FRU is present and its presence is not handled by vpd-manager."); |
| 1723 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1724 | else |
| 1725 | { |
| 1726 | if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath, |
| 1727 | "preAction", "deletion")) |
| 1728 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1729 | uint16_t l_errCode = 0; |
| 1730 | if (!processPreAction(l_fruPath, "deletion", l_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1731 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 1732 | std::string l_msg = "Pre action failed"; |
| 1733 | if (l_errCode) |
| 1734 | { |
| 1735 | l_msg += |
| 1736 | " Reason: " + |
| 1737 | vpdSpecificUtility::getErrCodeMsg(l_errCode); |
| 1738 | } |
| 1739 | throw std::runtime_error(l_msg); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | std::vector<std::string> l_interfaceList{ |
| 1744 | constants::operationalStatusInf}; |
| 1745 | |
| 1746 | types::MapperGetSubTree l_subTreeMap = |
| 1747 | dbusUtility::getObjectSubTree(i_dbusObjPath, 0, |
| 1748 | l_interfaceList); |
| 1749 | |
| 1750 | types::ObjectMap l_objectMap; |
| 1751 | |
| 1752 | // Updates VPD specific interfaces property value under PIM for |
| 1753 | // sub FRUs. |
| 1754 | for (const auto& [l_objectPath, l_serviceInterfaceMap] : |
| 1755 | l_subTreeMap) |
| 1756 | { |
| 1757 | types::InterfaceMap l_interfaceMap; |
| 1758 | vpdSpecificUtility::resetDataUnderPIM(l_objectPath, |
| 1759 | l_interfaceMap); |
| 1760 | l_objectMap.emplace(l_objectPath, |
| 1761 | std::move(l_interfaceMap)); |
| 1762 | } |
| 1763 | |
| 1764 | types::InterfaceMap l_interfaceMap; |
| 1765 | vpdSpecificUtility::resetDataUnderPIM(i_dbusObjPath, |
| 1766 | l_interfaceMap); |
| 1767 | |
| 1768 | l_objectMap.emplace(i_dbusObjPath, std::move(l_interfaceMap)); |
| 1769 | |
| 1770 | if (!dbusUtility::callPIM(std::move(l_objectMap))) |
| 1771 | { |
| 1772 | throw std::runtime_error("Call to PIM failed."); |
| 1773 | } |
| 1774 | |
| 1775 | if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath, |
| 1776 | "postAction", "deletion")) |
| 1777 | { |
| 1778 | if (!processPostAction(l_fruPath, "deletion")) |
| 1779 | { |
| 1780 | throw std::runtime_error("Post action failed"); |
| 1781 | } |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | else |
| 1786 | { |
| 1787 | logging::logMessage( |
| 1788 | "Can't process delete VPD for FRU [" + i_dbusObjPath + |
| 1789 | "] as unable to read present property"); |
| 1790 | return; |
| 1791 | } |
| 1792 | |
| 1793 | logging::logMessage( |
| 1794 | "Successfully completed deletion of FRU VPD for " + i_dbusObjPath); |
| 1795 | } |
| 1796 | catch (const std::exception& l_ex) |
| 1797 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1798 | uint16_t l_errCode = 0; |
| 1799 | std::string l_errMsg = |
| 1800 | "Failed to delete VPD for FRU : " + i_dbusObjPath + |
| 1801 | " error: " + std::string(l_ex.what()); |
| 1802 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1803 | if (jsonUtility::isActionRequired(m_parsedJson, l_fruPath, |
| 1804 | "postFailAction", "deletion")) |
| 1805 | { |
| 1806 | if (!jsonUtility::executePostFailAction(m_parsedJson, l_fruPath, |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1807 | "deletion", l_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1808 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1809 | l_errMsg += ". Post fail action also failed, error : " + |
| 1810 | vpdSpecificUtility::getErrCodeMsg(l_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 1814 | logging::logMessage(l_errMsg); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1815 | } |
| 1816 | } |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 1817 | |
| 1818 | void Worker::setPresentProperty(const std::string& i_vpdPath, |
| 1819 | const bool& i_value) |
| 1820 | { |
| 1821 | try |
| 1822 | { |
| 1823 | if (i_vpdPath.empty()) |
| 1824 | { |
| 1825 | throw std::runtime_error( |
| 1826 | "Path is empty. Can't set present property"); |
| 1827 | } |
| 1828 | |
| 1829 | types::ObjectMap l_objectInterfaceMap; |
| 1830 | |
| 1831 | // If the given path is EEPROM path. |
| 1832 | if (m_parsedJson["frus"].contains(i_vpdPath)) |
| 1833 | { |
| 1834 | for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath]) |
| 1835 | { |
| 1836 | sdbusplus::message::object_path l_fruObjectPath( |
| 1837 | l_Fru["inventoryPath"]); |
| 1838 | |
| 1839 | types::PropertyMap l_propertyValueMap; |
| 1840 | l_propertyValueMap.emplace("Present", i_value); |
| 1841 | |
| 1842 | types::InterfaceMap l_interfaces; |
| 1843 | vpdSpecificUtility::insertOrMerge(l_interfaces, |
| 1844 | constants::inventoryItemInf, |
| 1845 | move(l_propertyValueMap)); |
| 1846 | |
| 1847 | l_objectInterfaceMap.emplace(std::move(l_fruObjectPath), |
| 1848 | std::move(l_interfaces)); |
| 1849 | } |
| 1850 | } |
| 1851 | else |
| 1852 | { |
| 1853 | // consider it as an inventory path. |
| 1854 | if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0) |
| 1855 | { |
| 1856 | throw std::runtime_error( |
| 1857 | "Invalid inventory path: " + i_vpdPath); |
| 1858 | } |
| 1859 | |
| 1860 | types::PropertyMap l_propertyValueMap; |
| 1861 | l_propertyValueMap.emplace("Present", i_value); |
| 1862 | |
| 1863 | types::InterfaceMap l_interfaces; |
| 1864 | vpdSpecificUtility::insertOrMerge(l_interfaces, |
| 1865 | constants::inventoryItemInf, |
| 1866 | move(l_propertyValueMap)); |
| 1867 | |
| 1868 | l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces)); |
| 1869 | } |
| 1870 | |
| 1871 | // Notify PIM |
| 1872 | if (!dbusUtility::callPIM(move(l_objectInterfaceMap))) |
| 1873 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1874 | throw DbusException( |
| 1875 | std::string(__FUNCTION__) + |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 1876 | "Call to PIM failed while setting present property for path " + |
| 1877 | i_vpdPath); |
| 1878 | } |
| 1879 | } |
| 1880 | catch (const std::exception& l_ex) |
| 1881 | { |
Sunny Srivastava | 4c509c2 | 2025-03-25 12:43:40 +0530 | [diff] [blame] | 1882 | EventLogger::createSyncPel( |
| 1883 | EventLogger::getErrorType(l_ex), types::SeverityType::Warning, |
| 1884 | __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex), |
| 1885 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
Sunny Srivastava | d159bb4 | 2025-01-09 11:13:50 +0530 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 1889 | void Worker::performVpdRecollection() |
| 1890 | { |
| 1891 | try |
| 1892 | { |
| 1893 | // Check if system config JSON is present |
| 1894 | if (m_parsedJson.empty()) |
| 1895 | { |
| 1896 | throw std::runtime_error( |
| 1897 | "System config json object is empty, can't process recollection."); |
| 1898 | } |
| 1899 | |
| 1900 | const auto& l_frusReplaceableAtStandby = |
| 1901 | jsonUtility::getListOfFrusReplaceableAtStandby(m_parsedJson); |
| 1902 | |
| 1903 | for (const auto& l_fruInventoryPath : l_frusReplaceableAtStandby) |
| 1904 | { |
| 1905 | // ToDo: Add some logic/trace to know the flow to |
| 1906 | // collectSingleFruVpd has been directed via |
| 1907 | // performVpdRecollection. |
| 1908 | collectSingleFruVpd(l_fruInventoryPath); |
| 1909 | } |
| 1910 | return; |
| 1911 | } |
| 1912 | |
| 1913 | catch (const std::exception& l_ex) |
| 1914 | { |
| 1915 | // TODO Log PEL |
| 1916 | logging::logMessage( |
| 1917 | "VPD recollection failed with error: " + std::string(l_ex.what())); |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | void Worker::collectSingleFruVpd( |
| 1922 | const sdbusplus::message::object_path& i_dbusObjPath) |
| 1923 | { |
Anupama B R | 48f297b | 2025-08-13 04:29:06 -0500 | [diff] [blame] | 1924 | std::string l_fruPath{}; |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 1925 | try |
| 1926 | { |
| 1927 | // Check if system config JSON is present |
| 1928 | if (m_parsedJson.empty()) |
| 1929 | { |
| 1930 | logging::logMessage( |
| 1931 | "System config JSON object not present. Single FRU VPD collection is not performed for " + |
| 1932 | std::string(i_dbusObjPath)); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
| 1936 | // Get FRU path for the given D-bus object path from JSON |
Anupama B R | 48f297b | 2025-08-13 04:29:06 -0500 | [diff] [blame] | 1937 | l_fruPath = |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 1938 | jsonUtility::getFruPathFromJson(m_parsedJson, i_dbusObjPath); |
| 1939 | |
| 1940 | if (l_fruPath.empty()) |
| 1941 | { |
| 1942 | logging::logMessage( |
| 1943 | "D-bus object path not present in JSON. Single FRU VPD collection is not performed for " + |
| 1944 | std::string(i_dbusObjPath)); |
| 1945 | return; |
| 1946 | } |
| 1947 | |
| 1948 | // Check if host is up and running |
| 1949 | if (dbusUtility::isHostRunning()) |
| 1950 | { |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 1951 | uint16_t l_errCode = 0; |
| 1952 | bool isFruReplaceableAtRuntime = |
| 1953 | jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath, |
| 1954 | l_errCode); |
| 1955 | |
| 1956 | if (l_errCode) |
| 1957 | { |
| 1958 | logging::logMessage( |
| 1959 | "Failed to check if FRU is replaceable at runtime for FRU : [" + |
| 1960 | std::string(i_dbusObjPath) + "], error : " + |
| 1961 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1962 | return; |
| 1963 | } |
| 1964 | |
| 1965 | if (!isFruReplaceableAtRuntime) |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 1966 | { |
| 1967 | logging::logMessage( |
| 1968 | "Given FRU is not replaceable at host runtime. Single FRU VPD collection is not performed for " + |
| 1969 | std::string(i_dbusObjPath)); |
| 1970 | return; |
| 1971 | } |
| 1972 | } |
| 1973 | else if (dbusUtility::isBMCReady()) |
| 1974 | { |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1975 | uint16_t l_errCode = 0; |
| 1976 | bool isFruReplaceableAtStandby = |
| 1977 | jsonUtility::isFruReplaceableAtStandby(m_parsedJson, l_fruPath, |
| 1978 | l_errCode); |
| 1979 | |
| 1980 | if (l_errCode) |
| 1981 | { |
| 1982 | logging::logMessage( |
| 1983 | "Error while checking if FRU is replaceable at standby for FRU [" + |
| 1984 | std::string(i_dbusObjPath) + "], error : " + |
| 1985 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1986 | } |
| 1987 | |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 1988 | l_errCode = 0; |
| 1989 | bool isFruReplaceableAtRuntime = |
| 1990 | jsonUtility::isFruReplaceableAtRuntime(m_parsedJson, l_fruPath, |
| 1991 | l_errCode); |
| 1992 | |
| 1993 | if (l_errCode) |
| 1994 | { |
| 1995 | logging::logMessage( |
| 1996 | "Failed to check if FRU is replaceable at runtime for FRU : [" + |
| 1997 | std::string(i_dbusObjPath) + "], error : " + |
| 1998 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | if (!isFruReplaceableAtStandby && (!isFruReplaceableAtRuntime)) |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 2003 | { |
| 2004 | logging::logMessage( |
| 2005 | "Given FRU is neither replaceable at standby nor replaceable at runtime. Single FRU VPD collection is not performed for " + |
| 2006 | std::string(i_dbusObjPath)); |
| 2007 | return; |
| 2008 | } |
| 2009 | } |
| 2010 | |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2011 | // Set collection Status as InProgress. Since it's an intermediate state |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 2012 | // D-bus set-property call is good enough to update the status. |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2013 | const std::string& l_collStatusProp = "Status"; |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 2014 | |
| 2015 | if (!dbusUtility::writeDbusProperty( |
| 2016 | jsonUtility::getServiceName(m_parsedJson, |
| 2017 | std::string(i_dbusObjPath)), |
| 2018 | std::string(i_dbusObjPath), constants::vpdCollectionInterface, |
| 2019 | l_collStatusProp, |
| 2020 | types::DbusVariantType{constants::vpdCollectionInProgress})) |
| 2021 | { |
| 2022 | logging::logMessage( |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2023 | "Unable to set collection Status as InProgress for " + |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 2024 | std::string(i_dbusObjPath) + |
| 2025 | ". Continue single FRU VPD collection."); |
| 2026 | } |
| 2027 | |
| 2028 | // Parse VPD |
| 2029 | types::VPDMapVariant l_parsedVpd = parseVpdFile(l_fruPath); |
| 2030 | |
| 2031 | // If l_parsedVpd is pointing to std::monostate |
| 2032 | if (l_parsedVpd.index() == 0) |
| 2033 | { |
| 2034 | throw std::runtime_error( |
| 2035 | "VPD parsing failed for " + std::string(i_dbusObjPath)); |
| 2036 | } |
| 2037 | |
| 2038 | // Get D-bus object map from worker class |
| 2039 | types::ObjectMap l_dbusObjectMap; |
| 2040 | populateDbus(l_parsedVpd, l_dbusObjectMap, l_fruPath); |
| 2041 | |
| 2042 | if (l_dbusObjectMap.empty()) |
| 2043 | { |
| 2044 | throw std::runtime_error( |
| 2045 | "Failed to create D-bus object map. Single FRU VPD collection failed for " + |
| 2046 | std::string(i_dbusObjPath)); |
| 2047 | } |
| 2048 | |
| 2049 | // Call PIM's Notify method |
| 2050 | if (!dbusUtility::callPIM(move(l_dbusObjectMap))) |
| 2051 | { |
| 2052 | throw std::runtime_error( |
| 2053 | "Notify PIM failed. Single FRU VPD collection failed for " + |
| 2054 | std::string(i_dbusObjPath)); |
| 2055 | } |
| 2056 | } |
| 2057 | catch (const std::exception& l_error) |
| 2058 | { |
Anupama B R | 48f297b | 2025-08-13 04:29:06 -0500 | [diff] [blame] | 2059 | setCollectionStatusProperty(l_fruPath, constants::vpdCollectionFailed); |
Sunny Srivastava | 380efbb | 2025-04-25 10:28:30 +0530 | [diff] [blame] | 2060 | // TODO: Log PEL |
| 2061 | logging::logMessage(std::string(l_error.what())); |
| 2062 | } |
| 2063 | } |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2064 | |
| 2065 | void Worker::setCollectionStatusProperty( |
| 2066 | const std::string& i_vpdPath, const std::string& i_value) const noexcept |
| 2067 | { |
| 2068 | try |
| 2069 | { |
| 2070 | if (i_vpdPath.empty()) |
| 2071 | { |
| 2072 | throw std::runtime_error( |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2073 | "Given path is empty. Can't set collection Status property"); |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | types::ObjectMap l_objectInterfaceMap; |
| 2077 | |
| 2078 | if (m_parsedJson["frus"].contains(i_vpdPath)) |
| 2079 | { |
| 2080 | for (const auto& l_Fru : m_parsedJson["frus"][i_vpdPath]) |
| 2081 | { |
| 2082 | sdbusplus::message::object_path l_fruObjectPath( |
| 2083 | l_Fru["inventoryPath"]); |
| 2084 | |
| 2085 | types::PropertyMap l_propertyValueMap; |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2086 | l_propertyValueMap.emplace("Status", i_value); |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2087 | |
| 2088 | types::InterfaceMap l_interfaces; |
| 2089 | vpdSpecificUtility::insertOrMerge( |
| 2090 | l_interfaces, constants::vpdCollectionInterface, |
| 2091 | move(l_propertyValueMap)); |
| 2092 | |
| 2093 | l_objectInterfaceMap.emplace(std::move(l_fruObjectPath), |
| 2094 | std::move(l_interfaces)); |
| 2095 | } |
| 2096 | } |
| 2097 | else |
| 2098 | { |
| 2099 | // consider it as an inventory path. |
| 2100 | if (i_vpdPath.find(constants::pimPath) != constants::VALUE_0) |
| 2101 | { |
| 2102 | throw std::runtime_error( |
| 2103 | "Invalid inventory path: " + i_vpdPath + |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2104 | ". Can't set collection Status property"); |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | types::PropertyMap l_propertyValueMap; |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2108 | l_propertyValueMap.emplace("Status", i_value); |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2109 | |
| 2110 | types::InterfaceMap l_interfaces; |
| 2111 | vpdSpecificUtility::insertOrMerge(l_interfaces, |
| 2112 | constants::vpdCollectionInterface, |
| 2113 | move(l_propertyValueMap)); |
| 2114 | |
| 2115 | l_objectInterfaceMap.emplace(i_vpdPath, std::move(l_interfaces)); |
| 2116 | } |
| 2117 | |
| 2118 | // Notify PIM |
| 2119 | if (!dbusUtility::callPIM(move(l_objectInterfaceMap))) |
| 2120 | { |
| 2121 | throw DbusException( |
| 2122 | std::string(__FUNCTION__) + |
Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 2123 | "Call to PIM failed while setting collection Status property for path " + |
Anupama B R | 24691d2 | 2025-05-21 08:14:15 -0500 | [diff] [blame] | 2124 | i_vpdPath); |
| 2125 | } |
| 2126 | } |
| 2127 | catch (const std::exception& l_ex) |
| 2128 | { |
| 2129 | EventLogger::createSyncPel( |
| 2130 | EventLogger::getErrorType(l_ex), types::SeverityType::Warning, |
| 2131 | __FILE__, __FUNCTION__, 0, EventLogger::getErrorMsg(l_ex), |
| 2132 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
| 2133 | } |
| 2134 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 2135 | } // namespace vpd |