PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 1 | #include "vpd_tool_impl.hpp" |
| 2 | |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 3 | #include "impl.hpp" |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 4 | #include "parser_factory.hpp" |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 5 | #include "vpd_exceptions.hpp" |
| 6 | |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 9 | #include <cstdlib> |
| 10 | #include <filesystem> |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 11 | #include <iostream> |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 12 | #include <variant> |
| 13 | #include <vector> |
| 14 | |
| 15 | using namespace std; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 16 | using namespace openpower::vpd; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 17 | using namespace inventory; |
| 18 | using namespace openpower::vpd::manager::editor; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 19 | namespace fs = std::filesystem; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 20 | using json = nlohmann::json; |
| 21 | using namespace openpower::vpd::exceptions; |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 22 | using namespace openpower::vpd::parser; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 23 | using namespace openpower::vpd::parser::factory; |
| 24 | using namespace openpower::vpd::parser::interface; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 25 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 26 | bool VpdTool::fileToVector(Binary& data) |
| 27 | { |
| 28 | try |
| 29 | { |
| 30 | std::ifstream file(value, std::ifstream::in); |
| 31 | |
| 32 | if (file) |
| 33 | { |
| 34 | std::string line; |
| 35 | while (std::getline(file, line)) |
| 36 | { |
| 37 | std::istringstream iss(line); |
| 38 | std::string byteStr; |
| 39 | while (iss >> std::setw(2) >> std::hex >> byteStr) |
| 40 | { |
| 41 | uint8_t byte = strtoul(byteStr.c_str(), nullptr, 16); |
| 42 | data.emplace(data.end(), byte); |
| 43 | } |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | std::cerr << "Unable to open the given file " << value << std::endl; |
| 50 | } |
| 51 | } |
| 52 | catch (std::exception& e) |
| 53 | { |
| 54 | std::cerr << e.what(); |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | bool VpdTool::copyStringToFile(const std::string& input) |
| 60 | { |
| 61 | try |
| 62 | { |
| 63 | std::ofstream outFile(value, std::ofstream::out); |
| 64 | |
| 65 | if (outFile.is_open()) |
| 66 | { |
| 67 | std::string hexString = input; |
| 68 | if (input.substr(0, 2) == "0x") |
| 69 | { |
| 70 | // truncating prefix 0x |
| 71 | hexString = input.substr(2); |
| 72 | } |
| 73 | outFile.write(hexString.c_str(), hexString.length()); |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | std::cerr << "Error opening output file " << value << std::endl; |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | outFile.close(); |
| 82 | } |
| 83 | catch (std::exception& e) |
| 84 | { |
| 85 | std::cerr << e.what(); |
| 86 | return false; |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 91 | static void |
| 92 | getVPDInMap(const std::string& vpdPath, |
| 93 | std::unordered_map<std::string, DbusPropertyMap>& vpdMap, |
| 94 | json& js, const std::string& invPath) |
| 95 | { |
| 96 | auto jsonToParse = INVENTORY_JSON_DEFAULT; |
| 97 | if (fs::exists(INVENTORY_JSON_SYM_LINK)) |
| 98 | { |
| 99 | jsonToParse = INVENTORY_JSON_SYM_LINK; |
| 100 | } |
| 101 | |
| 102 | std::ifstream inventoryJson(jsonToParse); |
| 103 | if (!inventoryJson) |
| 104 | { |
| 105 | throw std::runtime_error("VPD JSON file not found"); |
| 106 | } |
| 107 | |
| 108 | try |
| 109 | { |
| 110 | js = json::parse(inventoryJson); |
| 111 | } |
| 112 | catch (const json::parse_error& ex) |
| 113 | { |
| 114 | throw std::runtime_error("VPD JSON parsing failed"); |
| 115 | } |
| 116 | |
| 117 | Binary vpdVector{}; |
| 118 | |
girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 119 | uint32_t vpdStartOffset = 0; |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 120 | vpdVector = getVpdDataInVector(js, vpdPath); |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 121 | ParserInterface* parser = |
| 122 | ParserFactory::getParser(vpdVector, invPath, vpdPath, vpdStartOffset); |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 123 | auto parseResult = parser->parse(); |
| 124 | ParserFactory::freeParser(parser); |
| 125 | |
| 126 | if (auto pVal = std::get_if<Store>(&parseResult)) |
| 127 | { |
| 128 | vpdMap = pVal->getVpdMap(); |
| 129 | } |
| 130 | else |
| 131 | { |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 132 | std::string err = |
| 133 | vpdPath + " is not of type IPZ VPD. Unable to parse the VPD."; |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 134 | throw std::runtime_error(err); |
| 135 | } |
| 136 | } |
| 137 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 138 | Binary VpdTool::toBinary(const std::string& value) |
| 139 | { |
| 140 | Binary val{}; |
| 141 | if (value.find("0x") == string::npos) |
| 142 | { |
| 143 | val.assign(value.begin(), value.end()); |
| 144 | } |
| 145 | else if (value.find("0x") != string::npos) |
| 146 | { |
| 147 | stringstream ss; |
| 148 | ss.str(value.substr(2)); |
| 149 | string byteStr{}; |
| 150 | |
Priyanga Ramasamy | ec912e6 | 2021-12-15 22:47:51 -0600 | [diff] [blame] | 151 | if (value.length() % 2 != 0) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 152 | { |
Priyanga Ramasamy | ec912e6 | 2021-12-15 22:47:51 -0600 | [diff] [blame] | 153 | throw runtime_error( |
| 154 | "VPD-TOOL write option accepts 2 digit hex numbers. (Eg. 0x1 " |
| 155 | "should be given as 0x01). Aborting the write operation."); |
| 156 | } |
| 157 | |
| 158 | if (value.find_first_not_of("0123456789abcdefABCDEF", 2) != |
| 159 | std::string::npos) |
| 160 | { |
| 161 | throw runtime_error("Provide a valid hexadecimal input."); |
| 162 | } |
| 163 | |
| 164 | while (ss >> setw(2) >> byteStr) |
| 165 | { |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 166 | uint8_t byte = strtoul(byteStr.c_str(), nullptr, 16); |
| 167 | |
| 168 | val.push_back(byte); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | else |
| 173 | { |
| 174 | throw runtime_error("The value to be updated should be either in ascii " |
| 175 | "or in hex. Refer --help option"); |
| 176 | } |
| 177 | return val; |
| 178 | } |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 179 | |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 180 | void VpdTool::printReturnCode(int returnCode) |
| 181 | { |
| 182 | if (returnCode) |
| 183 | { |
| 184 | cout << "\n Command failed with the return code " << returnCode |
| 185 | << ". Continuing the execution. " << endl; |
| 186 | } |
| 187 | } |
| 188 | |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 189 | void VpdTool::eraseInventoryPath(string& fru) |
| 190 | { |
| 191 | // Power supply frupath comes with INVENTORY_PATH appended in prefix. |
| 192 | // Stripping it off inorder to avoid INVENTORY_PATH duplication |
| 193 | // during getVINIProperties() execution. |
| 194 | fru.erase(0, sizeof(INVENTORY_PATH) - 1); |
| 195 | } |
| 196 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 197 | void VpdTool::debugger(json output) |
| 198 | { |
| 199 | cout << output.dump(4) << '\n'; |
| 200 | } |
| 201 | |
| 202 | auto VpdTool::makeDBusCall(const string& objectName, const string& interface, |
| 203 | const string& kw) |
| 204 | { |
| 205 | auto bus = sdbusplus::bus::new_default(); |
| 206 | auto properties = |
| 207 | bus.new_method_call(INVENTORY_MANAGER_SERVICE, objectName.c_str(), |
| 208 | "org.freedesktop.DBus.Properties", "Get"); |
| 209 | properties.append(interface); |
| 210 | properties.append(kw); |
| 211 | auto result = bus.call(properties); |
| 212 | |
| 213 | if (result.is_method_error()) |
| 214 | { |
| 215 | throw runtime_error("Get api failed"); |
| 216 | } |
| 217 | return result; |
| 218 | } |
| 219 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 220 | json VpdTool::getVINIProperties(string invPath) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 221 | { |
| 222 | variant<Binary> response; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 223 | json kwVal = json::object({}); |
| 224 | |
| 225 | vector<string> keyword{"CC", "SN", "PN", "FN", "DR"}; |
| 226 | string interface = "com.ibm.ipzvpd.VINI"; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 227 | string objectName = {}; |
| 228 | |
| 229 | if (invPath.find(INVENTORY_PATH) != string::npos) |
| 230 | { |
| 231 | objectName = invPath; |
| 232 | eraseInventoryPath(invPath); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | objectName = INVENTORY_PATH + invPath; |
| 237 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 238 | for (string kw : keyword) |
| 239 | { |
| 240 | try |
| 241 | { |
| 242 | makeDBusCall(objectName, interface, kw).read(response); |
| 243 | |
| 244 | if (auto vec = get_if<Binary>(&response)) |
| 245 | { |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 246 | string printableVal = getPrintableValue(*vec); |
| 247 | kwVal.emplace(kw, printableVal); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 248 | } |
| 249 | } |
Patrick Williams | 2eb0176 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 250 | catch (const sdbusplus::exception_t& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 251 | { |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 252 | if (string(e.name()) == |
| 253 | string("org.freedesktop.DBus.Error.UnknownObject")) |
| 254 | { |
| 255 | kwVal.emplace(invPath, json::object({})); |
| 256 | objFound = false; |
| 257 | break; |
| 258 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 262 | return kwVal; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 263 | } |
| 264 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 265 | void VpdTool::getExtraInterfaceProperties(const string& invPath, |
| 266 | const string& extraInterface, |
| 267 | const json& prop, json& output) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 268 | { |
| 269 | variant<string> response; |
| 270 | |
| 271 | string objectName = INVENTORY_PATH + invPath; |
| 272 | |
| 273 | for (const auto& itProp : prop.items()) |
| 274 | { |
| 275 | string kw = itProp.key(); |
| 276 | try |
| 277 | { |
| 278 | makeDBusCall(objectName, extraInterface, kw).read(response); |
| 279 | |
| 280 | if (auto str = get_if<string>(&response)) |
| 281 | { |
| 282 | output.emplace(kw, *str); |
| 283 | } |
| 284 | } |
Patrick Williams | 2eb0176 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 285 | catch (const sdbusplus::exception_t& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 286 | { |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 287 | if (std::string(e.name()) == |
| 288 | std::string("org.freedesktop.DBus.Error.UnknownObject")) |
| 289 | { |
| 290 | objFound = false; |
| 291 | break; |
| 292 | } |
| 293 | else if (std::string(e.name()) == |
| 294 | std::string("org.freedesktop.DBus.Error.UnknownProperty")) |
| 295 | { |
| 296 | output.emplace(kw, ""); |
| 297 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 298 | } |
| 299 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | json VpdTool::interfaceDecider(json& itemEEPROM) |
| 303 | { |
| 304 | if (itemEEPROM.find("inventoryPath") == itemEEPROM.end()) |
| 305 | { |
| 306 | throw runtime_error("Inventory Path not found"); |
| 307 | } |
| 308 | |
| 309 | if (itemEEPROM.find("extraInterfaces") == itemEEPROM.end()) |
| 310 | { |
| 311 | throw runtime_error("Extra Interfaces not found"); |
| 312 | } |
| 313 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 314 | json subOutput = json::object({}); |
Alpana Kumari | b6965f1 | 2020-06-01 00:32:21 -0500 | [diff] [blame] | 315 | fruType = "FRU"; |
| 316 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 317 | json j; |
| 318 | objFound = true; |
| 319 | string invPath = itemEEPROM.at("inventoryPath"); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 320 | |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 321 | j = getVINIProperties(invPath); |
| 322 | |
| 323 | if (objFound) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 324 | { |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 325 | subOutput.insert(j.begin(), j.end()); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 326 | json js; |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 327 | if (itemEEPROM.find("type") != itemEEPROM.end()) |
| 328 | { |
| 329 | fruType = itemEEPROM.at("type"); |
| 330 | } |
| 331 | js.emplace("TYPE", fruType); |
| 332 | |
| 333 | if (invPath.find("powersupply") != string::npos) |
| 334 | { |
| 335 | js.emplace("type", POWER_SUPPLY_TYPE_INTERFACE); |
| 336 | } |
| 337 | else if (invPath.find("fan") != string::npos) |
| 338 | { |
| 339 | js.emplace("type", FAN_INTERFACE); |
| 340 | } |
| 341 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 342 | for (const auto& ex : itemEEPROM["extraInterfaces"].items()) |
| 343 | { |
Priyanga Ramasamy | 051b34f | 2023-06-28 00:37:43 -0500 | [diff] [blame] | 344 | // Properties under Decorator.Asset interface are derived from VINI |
| 345 | // keywords. Displaying VINI keywords and skipping Decorator.Asset |
| 346 | // interface's properties will avoid duplicate entries in vpd-tool |
| 347 | // output. |
Priyanga Ramasamy | 2dfdbdd | 2023-08-18 10:07:37 +0000 | [diff] [blame] | 348 | if (ex.key() == "xyz.openbmc_project.Inventory.Decorator.Asset" && |
| 349 | itemEEPROM["extraInterfaces"].find(constants::kwdVpdInf) != |
| 350 | itemEEPROM["extraInterfaces"].end()) |
Priyanga Ramasamy | 051b34f | 2023-06-28 00:37:43 -0500 | [diff] [blame] | 351 | { |
| 352 | continue; |
| 353 | } |
| 354 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 355 | if (!(ex.value().is_null())) |
| 356 | { |
Priyanga Ramasamy | dacaa47 | 2021-10-07 12:22:41 -0500 | [diff] [blame] | 357 | // TODO: Remove this if condition check once inventory json is |
| 358 | // updated with xyz location code interface. |
| 359 | if (ex.key() == "com.ibm.ipzvpd.Location") |
| 360 | { |
| 361 | getExtraInterfaceProperties( |
| 362 | invPath, |
| 363 | "xyz.openbmc_project.Inventory.Decorator.LocationCode", |
| 364 | ex.value(), js); |
| 365 | } |
| 366 | else |
| 367 | { |
| 368 | getExtraInterfaceProperties(invPath, ex.key(), ex.value(), |
| 369 | js); |
| 370 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 371 | } |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 372 | if ((ex.key().find("Item") != string::npos) && |
| 373 | (ex.value().is_null())) |
| 374 | { |
| 375 | js.emplace("type", ex.key()); |
| 376 | } |
| 377 | subOutput.insert(js.begin(), js.end()); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 378 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 379 | } |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 380 | return subOutput; |
| 381 | } |
| 382 | |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 383 | json VpdTool::getPresentPropJson(const std::string& invPath) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 384 | { |
| 385 | std::variant<bool> response; |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 386 | std::string presence = "Unknown"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 387 | |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 388 | try |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 389 | { |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 390 | makeDBusCall(invPath, "xyz.openbmc_project.Inventory.Item", "Present") |
| 391 | .read(response); |
| 392 | |
| 393 | if (auto pVal = get_if<bool>(&response)) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 394 | { |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 395 | presence = *pVal ? "true" : "false"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 396 | } |
| 397 | } |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 398 | catch (const sdbusplus::exception::SdBusError& e) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 399 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 400 | presence = "Unknown"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 401 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 402 | |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 403 | json js; |
| 404 | js.emplace("Present", presence); |
| 405 | return js; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | json VpdTool::parseInvJson(const json& jsObject, char flag, string fruPath) |
| 409 | { |
| 410 | json output = json::object({}); |
| 411 | bool validObject = false; |
| 412 | |
| 413 | if (jsObject.find("frus") == jsObject.end()) |
| 414 | { |
| 415 | throw runtime_error("Frus missing in Inventory json"); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | for (const auto& itemFRUS : jsObject["frus"].items()) |
| 420 | { |
| 421 | for (auto itemEEPROM : itemFRUS.value()) |
| 422 | { |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 423 | json subOutput = json::object({}); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 424 | try |
| 425 | { |
| 426 | if (flag == 'O') |
| 427 | { |
| 428 | if (itemEEPROM.find("inventoryPath") == |
| 429 | itemEEPROM.end()) |
| 430 | { |
| 431 | throw runtime_error("Inventory Path not found"); |
| 432 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 433 | else if (itemEEPROM.at("inventoryPath") == fruPath) |
| 434 | { |
| 435 | validObject = true; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 436 | subOutput = interfaceDecider(itemEEPROM); |
| 437 | json presentJs = getPresentPropJson( |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 438 | "/xyz/openbmc_project/inventory" + fruPath); |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 439 | subOutput.insert(presentJs.begin(), |
| 440 | presentJs.end()); |
| 441 | output.emplace(fruPath, subOutput); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 442 | return output; |
| 443 | } |
| 444 | } |
| 445 | else |
| 446 | { |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 447 | subOutput = interfaceDecider(itemEEPROM); |
| 448 | json presentJs = getPresentPropJson( |
| 449 | "/xyz/openbmc_project/inventory" + |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 450 | string(itemEEPROM.at("inventoryPath"))); |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 451 | subOutput.insert(presentJs.begin(), presentJs.end()); |
| 452 | output.emplace(string(itemEEPROM.at("inventoryPath")), |
| 453 | subOutput); |
| 454 | } |
| 455 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 456 | catch (const exception& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 457 | { |
| 458 | cerr << e.what(); |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | if ((flag == 'O') && (!validObject)) |
| 463 | { |
| 464 | throw runtime_error( |
| 465 | "Invalid object path. Refer --dumpInventory/-i option."); |
| 466 | } |
| 467 | } |
| 468 | return output; |
| 469 | } |
| 470 | |
| 471 | void VpdTool::dumpInventory(const nlohmann::basic_json<>& jsObject) |
| 472 | { |
| 473 | char flag = 'I'; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 474 | json output = json::array({}); |
| 475 | output.emplace_back(parseInvJson(jsObject, flag, "")); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 476 | debugger(output); |
| 477 | } |
| 478 | |
| 479 | void VpdTool::dumpObject(const nlohmann::basic_json<>& jsObject) |
| 480 | { |
| 481 | char flag = 'O'; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 482 | json output = json::array({}); |
Santosh Puranik | d5d52bb | 2020-07-28 15:08:10 +0530 | [diff] [blame] | 483 | output.emplace_back(parseInvJson(jsObject, flag, fruPath)); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 484 | debugger(output); |
| 485 | } |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 486 | |
| 487 | void VpdTool::readKeyword() |
| 488 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 489 | const std::string& kw = getDbusNameForThisKw(keyword); |
| 490 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 491 | string interface = "com.ibm.ipzvpd."; |
| 492 | variant<Binary> response; |
| 493 | |
| 494 | try |
| 495 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 496 | makeDBusCall(INVENTORY_PATH + fruPath, interface + recordName, kw) |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 497 | .read(response); |
| 498 | |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 499 | string printableVal{}; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 500 | if (auto vec = get_if<Binary>(&response)) |
| 501 | { |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 502 | printableVal = getPrintableValue(*vec); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 503 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 504 | |
| 505 | if (!value.empty()) |
| 506 | { |
| 507 | if (copyStringToFile(printableVal)) |
| 508 | { |
| 509 | std::cout << "Value read is saved in the file " << value |
| 510 | << std::endl; |
| 511 | return; |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | std::cerr << "Error while saving the read value in file. " |
| 516 | "Displaying the read value on console" |
| 517 | << std::endl; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | json output = json::object({}); |
| 522 | json kwVal = json::object({}); |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 523 | kwVal.emplace(keyword, printableVal); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 524 | |
| 525 | output.emplace(fruPath, kwVal); |
| 526 | |
| 527 | debugger(output); |
| 528 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 529 | catch (const json::exception& e) |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 530 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 531 | std::cout << "Keyword Value: " << keyword << std::endl; |
| 532 | std::cout << e.what() << std::endl; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 533 | } |
| 534 | } |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 535 | |
| 536 | int VpdTool::updateKeyword() |
| 537 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 538 | Binary val; |
| 539 | |
| 540 | if (std::filesystem::exists(value)) |
| 541 | { |
| 542 | if (!fileToVector(val)) |
| 543 | { |
| 544 | std::cout << "Keyword " << keyword << " update failed." |
| 545 | << std::endl; |
| 546 | return 1; |
| 547 | } |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | val = toBinary(value); |
| 552 | } |
| 553 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 554 | auto bus = sdbusplus::bus::new_default(); |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 555 | auto properties = |
| 556 | bus.new_method_call(BUSNAME, OBJPATH, IFACE, "WriteKeyword"); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 557 | properties.append(static_cast<sdbusplus::message::object_path>(fruPath)); |
| 558 | properties.append(recordName); |
| 559 | properties.append(keyword); |
| 560 | properties.append(val); |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 561 | |
| 562 | // When there is a request to write 10K bytes, there occurs a delay in dbus |
| 563 | // call which leads to dbus timeout exception. To avoid such exceptions |
| 564 | // increase the timeout period from default 25 seconds to 60 seconds. |
| 565 | auto timeoutInMicroSeconds = 60 * 1000000L; |
| 566 | auto result = bus.call(properties, timeoutInMicroSeconds); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 567 | |
| 568 | if (result.is_method_error()) |
| 569 | { |
| 570 | throw runtime_error("Get api failed"); |
| 571 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 572 | std::cout << "Data updated successfully " << std::endl; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 573 | return 0; |
| 574 | } |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 575 | |
| 576 | void VpdTool::forceReset(const nlohmann::basic_json<>& jsObject) |
| 577 | { |
| 578 | for (const auto& itemFRUS : jsObject["frus"].items()) |
| 579 | { |
| 580 | for (const auto& itemEEPROM : itemFRUS.value().items()) |
| 581 | { |
| 582 | string fru = itemEEPROM.value().at("inventoryPath"); |
| 583 | |
| 584 | fs::path fruCachePath = INVENTORY_MANAGER_CACHE; |
| 585 | fruCachePath += INVENTORY_PATH; |
| 586 | fruCachePath += fru; |
| 587 | |
| 588 | try |
| 589 | { |
| 590 | for (const auto& it : fs::directory_iterator(fruCachePath)) |
| 591 | { |
| 592 | if (fs::is_regular_file(it.status())) |
| 593 | { |
| 594 | fs::remove(it); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | catch (const fs::filesystem_error& e) |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 599 | {} |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 603 | cout.flush(); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 604 | string udevRemove = "udevadm trigger -c remove -s \"*nvmem*\" -v"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 605 | int returnCode = system(udevRemove.c_str()); |
| 606 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 607 | |
| 608 | string invManagerRestart = |
| 609 | "systemctl restart xyz.openbmc_project.Inventory.Manager.service"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 610 | returnCode = system(invManagerRestart.c_str()); |
| 611 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 612 | |
Santosh Puranik | 6c7a84e | 2022-03-09 13:42:18 +0530 | [diff] [blame] | 613 | string sysVpdRestart = "systemctl restart system-vpd.service"; |
| 614 | returnCode = system(sysVpdRestart.c_str()); |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 615 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 616 | |
| 617 | string udevAdd = "udevadm trigger -c add -s \"*nvmem*\" -v"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 618 | returnCode = system(udevAdd.c_str()); |
| 619 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 620 | } |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 621 | |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 622 | int VpdTool::updateHardware(const uint32_t offset) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 623 | { |
| 624 | int rc = 0; |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 625 | Binary val; |
| 626 | if (std::filesystem::exists(value)) |
| 627 | { |
| 628 | if (!fileToVector(val)) |
| 629 | { |
| 630 | std::cout << "Keyword " << keyword << " update failed." |
| 631 | << std::endl; |
| 632 | return 1; |
| 633 | } |
| 634 | } |
| 635 | else |
| 636 | { |
| 637 | val = toBinary(value); |
| 638 | } |
| 639 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 640 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
| 641 | try |
| 642 | { |
| 643 | auto json = nlohmann::json::parse(inventoryJson); |
| 644 | EditorImpl edit(fruPath, json, recordName, keyword); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 645 | |
| 646 | edit.updateKeyword(val, offset, false); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 647 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 648 | catch (const json::parse_error& ex) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 649 | { |
| 650 | throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK)); |
| 651 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 652 | std::cout << "Data updated successfully " << std::endl; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 653 | return rc; |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 654 | } |
| 655 | |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 656 | void VpdTool::readKwFromHw(const uint32_t& startOffset) |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 657 | { |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 658 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
| 659 | auto jsonFile = nlohmann::json::parse(inventoryJson); |
girik | bab2bed | 2023-05-15 04:00:17 -0500 | [diff] [blame] | 660 | std::string inventoryPath; |
| 661 | |
| 662 | if (jsonFile["frus"].contains(fruPath)) |
| 663 | { |
| 664 | uint32_t vpdStartOffset = 0; |
| 665 | |
| 666 | for (const auto& item : jsonFile["frus"][fruPath]) |
| 667 | { |
| 668 | if (item.find("offset") != item.end()) |
| 669 | { |
| 670 | vpdStartOffset = item["offset"]; |
| 671 | break; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if ((startOffset != vpdStartOffset)) |
| 676 | { |
| 677 | std::cerr << "Invalid offset, please correct the offset" << endl; |
| 678 | std::cerr << "Recommended Offset is: " << vpdStartOffset << endl; |
| 679 | return; |
| 680 | } |
| 681 | inventoryPath = jsonFile["frus"][fruPath][0]["inventoryPath"]; |
| 682 | } |
| 683 | |
Priyanga Ramasamy | 4170bda | 2023-07-19 09:25:36 +0000 | [diff] [blame] | 684 | Binary completeVPDFile; |
| 685 | fstream vpdFileStream; |
| 686 | |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 687 | vpdFileStream.exceptions(std::ifstream::badbit | std::ifstream::failbit); |
| 688 | try |
| 689 | { |
| 690 | vpdFileStream.open(fruPath, |
| 691 | std::ios::in | std::ios::out | std::ios::binary); |
Priyanga Ramasamy | 4170bda | 2023-07-19 09:25:36 +0000 | [diff] [blame] | 692 | |
| 693 | auto vpdFileSize = std::min(std::filesystem::file_size(fruPath), |
| 694 | constants::MAX_VPD_SIZE); |
| 695 | if (vpdFileSize == 0) |
| 696 | { |
| 697 | std::cerr << "File size is 0 for " << fruPath << std::endl; |
| 698 | throw std::runtime_error("File size is 0."); |
| 699 | } |
| 700 | |
| 701 | completeVPDFile.resize(vpdFileSize); |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 702 | vpdFileStream.seekg(startOffset, ios_base::cur); |
Priyanga Ramasamy | 4170bda | 2023-07-19 09:25:36 +0000 | [diff] [blame] | 703 | vpdFileStream.read(reinterpret_cast<char*>(&completeVPDFile[0]), |
| 704 | vpdFileSize); |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 705 | vpdFileStream.clear(std::ios_base::eofbit); |
| 706 | } |
Priyanga Ramasamy | 4170bda | 2023-07-19 09:25:36 +0000 | [diff] [blame] | 707 | catch (const std::system_error& fail) |
jinuthomas | 45d5497 | 2023-07-03 04:36:29 -0500 | [diff] [blame] | 708 | { |
| 709 | std::cerr << "Exception in file handling [" << fruPath |
| 710 | << "] error : " << fail.what(); |
| 711 | std::cerr << "Stream file size = " << vpdFileStream.gcount() |
| 712 | << std::endl; |
| 713 | throw; |
| 714 | } |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 715 | |
| 716 | if (completeVPDFile.empty()) |
| 717 | { |
| 718 | throw std::runtime_error("Invalid File"); |
| 719 | } |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 720 | |
girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 721 | Impl obj(completeVPDFile, (constants::pimPath + inventoryPath), fruPath, |
girik | bab2bed | 2023-05-15 04:00:17 -0500 | [diff] [blame] | 722 | startOffset); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 723 | std::string keywordVal = obj.readKwFromHw(recordName, keyword); |
| 724 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 725 | keywordVal = getPrintableValue(keywordVal); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 726 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 727 | if (keywordVal.empty()) |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 728 | { |
| 729 | std::cerr << "The given keyword " << keyword << " or record " |
| 730 | << recordName |
| 731 | << " or both are not present in the given FRU path " |
| 732 | << fruPath << std::endl; |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 733 | return; |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 734 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 735 | |
| 736 | if (!value.empty()) |
| 737 | { |
| 738 | if (copyStringToFile(keywordVal)) |
| 739 | { |
| 740 | std::cout << "Value read is saved in the file " << value |
| 741 | << std::endl; |
| 742 | return; |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | std::cerr |
| 747 | << "Error while saving the read value in file. Displaying " |
| 748 | "the read value on console" |
| 749 | << std::endl; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | json output = json::object({}); |
| 754 | json kwVal = json::object({}); |
| 755 | kwVal.emplace(keyword, keywordVal); |
| 756 | output.emplace(fruPath, kwVal); |
| 757 | debugger(output); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 758 | } |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 759 | |
| 760 | void VpdTool::printFixSystemVPDOption(UserOption option) |
| 761 | { |
| 762 | switch (option) |
| 763 | { |
| 764 | case VpdTool::EXIT: |
| 765 | cout << "\nEnter 0 => To exit successfully : "; |
| 766 | break; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 767 | case VpdTool::BACKUP_DATA_FOR_ALL: |
| 768 | cout << "\n\nEnter 1 => If you choose the data on backup for all " |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 769 | "mismatching record-keyword pairs"; |
| 770 | break; |
| 771 | case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 772 | cout << "\nEnter 2 => If you choose the data on primary for all " |
| 773 | "mismatching record-keyword pairs"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 774 | break; |
| 775 | case VpdTool::MORE_OPTIONS: |
| 776 | cout << "\nEnter 3 => If you wish to explore more options"; |
| 777 | break; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 778 | case VpdTool::BACKUP_DATA_FOR_CURRENT: |
| 779 | cout << "\nEnter 4 => If you choose the data on backup as the " |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 780 | "right value"; |
| 781 | break; |
| 782 | case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 783 | cout << "\nEnter 5 => If you choose the data on primary as the " |
| 784 | "right value"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 785 | break; |
| 786 | case VpdTool::NEW_VALUE_ON_BOTH: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 787 | cout << "\nEnter 6 => If you wish to enter a new value to update " |
| 788 | "both on backup and primary"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 789 | break; |
| 790 | case VpdTool::SKIP_CURRENT: |
| 791 | cout << "\nEnter 7 => If you wish to skip the above " |
| 792 | "record-keyword pair"; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 797 | void VpdTool::getSystemDataFromCache(IntfPropMap& svpdBusData) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 798 | { |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 799 | const auto vsys = getAllDBusProperty<GetAllResultType>( |
| 800 | constants::pimIntf, |
| 801 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 802 | "com.ibm.ipzvpd.VSYS"); |
| 803 | svpdBusData.emplace("VSYS", vsys); |
| 804 | |
| 805 | const auto vcen = getAllDBusProperty<GetAllResultType>( |
| 806 | constants::pimIntf, |
| 807 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 808 | "com.ibm.ipzvpd.VCEN"); |
| 809 | svpdBusData.emplace("VCEN", vcen); |
| 810 | |
| 811 | const auto lxr0 = getAllDBusProperty<GetAllResultType>( |
| 812 | constants::pimIntf, |
| 813 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 814 | "com.ibm.ipzvpd.LXR0"); |
| 815 | svpdBusData.emplace("LXR0", lxr0); |
| 816 | |
| 817 | const auto util = getAllDBusProperty<GetAllResultType>( |
| 818 | constants::pimIntf, |
| 819 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 820 | "com.ibm.ipzvpd.UTIL"); |
| 821 | svpdBusData.emplace("UTIL", util); |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | int VpdTool::fixSystemVPD() |
| 825 | { |
| 826 | std::string outline(191, '='); |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 827 | cout << "\nRestorable record-keyword pairs and their data on backup & " |
| 828 | "primary.\n\n" |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 829 | << outline << std::endl; |
| 830 | |
| 831 | cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 832 | << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left |
| 833 | << setw(75) << "Data On Primary" << left << setw(14) |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 834 | << "Data Mismatch\n" |
| 835 | << outline << std::endl; |
| 836 | |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 837 | uint8_t num = 0; |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 838 | |
| 839 | // Get system VPD data in map |
| 840 | unordered_map<string, DbusPropertyMap> vpdMap; |
| 841 | json js; |
| 842 | getVPDInMap(constants::systemVpdFilePath, vpdMap, js, |
| 843 | constants::pimPath + |
| 844 | static_cast<std::string>(constants::SYSTEM_OBJECT)); |
| 845 | |
| 846 | // Get system VPD D-Bus Data in a map |
| 847 | IntfPropMap svpdBusData; |
| 848 | getSystemDataFromCache(svpdBusData); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 849 | |
| 850 | for (const auto& recordKw : svpdKwdMap) |
| 851 | { |
| 852 | string record = recordKw.first; |
| 853 | |
| 854 | // Extract specific record data from the svpdBusData map. |
| 855 | const auto& rec = svpdBusData.find(record); |
| 856 | |
| 857 | if (rec == svpdBusData.end()) |
| 858 | { |
| 859 | std::cerr << record << " not a part of critical system VPD records." |
| 860 | << std::endl; |
| 861 | continue; |
| 862 | } |
| 863 | |
| 864 | const auto& recData = svpdBusData.find(record)->second; |
| 865 | |
| 866 | string busStr{}, hwValStr{}; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 867 | |
Priyanga Ramasamy | 952d6c5 | 2022-11-07 07:20:24 -0600 | [diff] [blame] | 868 | for (const auto& keywordInfo : recordKw.second) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 869 | { |
Priyanga Ramasamy | 952d6c5 | 2022-11-07 07:20:24 -0600 | [diff] [blame] | 870 | const auto& keyword = get<0>(keywordInfo); |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 871 | string mismatch = "NO"; // no mismatch |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 872 | string hardwareValue{}; |
| 873 | auto recItr = vpdMap.find(record); |
| 874 | |
| 875 | if (recItr != vpdMap.end()) |
| 876 | { |
| 877 | DbusPropertyMap& kwValMap = recItr->second; |
| 878 | auto kwItr = kwValMap.find(keyword); |
| 879 | if (kwItr != kwValMap.end()) |
| 880 | { |
| 881 | hardwareValue = kwItr->second; |
| 882 | } |
| 883 | } |
| 884 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 885 | inventory::Value kwValue; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 886 | for (auto& kwData : recData) |
| 887 | { |
| 888 | if (kwData.first == keyword) |
| 889 | { |
| 890 | kwValue = kwData.second; |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 895 | if (keyword != "SE") // SE to display in Hex string only |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 896 | { |
| 897 | ostringstream hwValStream; |
| 898 | hwValStream << "0x"; |
| 899 | hwValStr = hwValStream.str(); |
| 900 | |
| 901 | for (uint16_t byte : hardwareValue) |
| 902 | { |
| 903 | hwValStream << setfill('0') << setw(2) << hex << byte; |
| 904 | hwValStr = hwValStream.str(); |
| 905 | } |
| 906 | |
| 907 | if (const auto value = get_if<Binary>(&kwValue)) |
| 908 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 909 | busStr = hexString(*value); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 910 | } |
| 911 | if (busStr != hwValStr) |
| 912 | { |
| 913 | mismatch = "YES"; |
| 914 | } |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | if (const auto value = get_if<Binary>(&kwValue)) |
| 919 | { |
| 920 | busStr = getPrintableValue(*value); |
| 921 | } |
| 922 | if (busStr != hardwareValue) |
| 923 | { |
| 924 | mismatch = "YES"; |
| 925 | } |
| 926 | hwValStr = hardwareValue; |
| 927 | } |
| 928 | recKwData.push_back( |
| 929 | make_tuple(++num, record, keyword, busStr, hwValStr, mismatch)); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 930 | |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 931 | std::string splitLine(191, '-'); |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 932 | cout << left << setw(6) << static_cast<int>(num) << left << setw(8) |
| 933 | << record << left << setw(9) << keyword << left << setw(75) |
| 934 | << setfill(' ') << busStr << left << setw(75) << setfill(' ') |
| 935 | << hwValStr << left << setw(14) << mismatch << '\n' |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 936 | << splitLine << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 937 | } |
| 938 | } |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 939 | parseSVPDOptions(js, std::string()); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 940 | return 0; |
| 941 | } |
| 942 | |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 943 | void VpdTool::parseSVPDOptions(const nlohmann::json& json, |
| 944 | const std::string& backupEEPROMPath) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 945 | { |
| 946 | do |
| 947 | { |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 948 | printFixSystemVPDOption(VpdTool::BACKUP_DATA_FOR_ALL); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 949 | printFixSystemVPDOption(VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL); |
| 950 | printFixSystemVPDOption(VpdTool::MORE_OPTIONS); |
| 951 | printFixSystemVPDOption(VpdTool::EXIT); |
| 952 | |
| 953 | int option = 0; |
| 954 | cin >> option; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 955 | |
| 956 | std::string outline(191, '='); |
| 957 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 958 | |
| 959 | if (json.find("frus") == json.end()) |
| 960 | { |
| 961 | throw runtime_error("Frus not found in json"); |
| 962 | } |
| 963 | |
| 964 | bool mismatchFound = false; |
| 965 | |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 966 | if (option == VpdTool::BACKUP_DATA_FOR_ALL) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 967 | { |
| 968 | for (const auto& data : recKwData) |
| 969 | { |
| 970 | if (get<5>(data) == "YES") |
| 971 | { |
| 972 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 973 | get<1>(data), get<2>(data)); |
| 974 | edit.updateKeyword(toBinary(get<3>(data)), 0, true); |
| 975 | mismatchFound = true; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | if (mismatchFound) |
| 980 | { |
| 981 | cout << "\nData updated successfully for all mismatching " |
| 982 | "record-keyword pairs by choosing their corresponding " |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 983 | "data from backup. Exit successfully.\n" |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 984 | << endl; |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | cout << "\nNo mismatch found for any of the above mentioned " |
| 989 | "record-keyword pair. Exit successfully.\n"; |
| 990 | } |
| 991 | |
| 992 | exit(0); |
| 993 | } |
| 994 | else if (option == VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL) |
| 995 | { |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 996 | std::string hardwarePath = constants::systemVpdFilePath; |
| 997 | if (!backupEEPROMPath.empty()) |
| 998 | { |
| 999 | hardwarePath = backupEEPROMPath; |
| 1000 | } |
| 1001 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1002 | for (const auto& data : recKwData) |
| 1003 | { |
| 1004 | if (get<5>(data) == "YES") |
| 1005 | { |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1006 | std::string record = get<1>(data), keyword = get<2>(data); |
| 1007 | |
| 1008 | if (!backupEEPROMPath.empty()) |
| 1009 | { |
| 1010 | getBackupRecordKeyword(record, keyword); |
| 1011 | } |
| 1012 | |
| 1013 | EditorImpl edit(hardwarePath, json, record, keyword); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1014 | edit.updateKeyword(toBinary(get<4>(data)), 0, true); |
| 1015 | mismatchFound = true; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (mismatchFound) |
| 1020 | { |
| 1021 | cout << "\nData updated successfully for all mismatching " |
| 1022 | "record-keyword pairs by choosing their corresponding " |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1023 | "data from primary VPD.\n" |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1024 | << endl; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | cout << "\nNo mismatch found for any of the above mentioned " |
| 1029 | "record-keyword pair. Exit successfully.\n"; |
| 1030 | } |
| 1031 | |
| 1032 | exit(0); |
| 1033 | } |
| 1034 | else if (option == VpdTool::MORE_OPTIONS) |
| 1035 | { |
| 1036 | cout << "\nIterate through all restorable record-keyword pairs\n"; |
| 1037 | |
| 1038 | for (const auto& data : recKwData) |
| 1039 | { |
| 1040 | do |
| 1041 | { |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1042 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1043 | |
| 1044 | cout << left << setw(6) << "S.No" << left << setw(8) |
| 1045 | << "Record" << left << setw(9) << "Keyword" << left |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1046 | << setw(75) << setfill(' ') << "Backup Data" << left |
| 1047 | << setw(75) << setfill(' ') << "Primary Data" << left |
| 1048 | << setw(14) << "Data Mismatch" << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1049 | |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1050 | cout << left << setw(6) << static_cast<int>(get<0>(data)) |
| 1051 | << left << setw(8) << get<1>(data) << left << setw(9) |
| 1052 | << get<2>(data) << left << setw(75) << setfill(' ') |
| 1053 | << get<3>(data) << left << setw(75) << setfill(' ') |
| 1054 | << get<4>(data) << left << setw(14) << get<5>(data); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1055 | |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1056 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1057 | |
| 1058 | if (get<5>(data) == "NO") |
| 1059 | { |
| 1060 | cout << "\nNo mismatch found.\n"; |
| 1061 | printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH); |
| 1062 | printFixSystemVPDOption(VpdTool::SKIP_CURRENT); |
| 1063 | printFixSystemVPDOption(VpdTool::EXIT); |
| 1064 | } |
| 1065 | else |
| 1066 | { |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1067 | printFixSystemVPDOption( |
| 1068 | VpdTool::BACKUP_DATA_FOR_CURRENT); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1069 | printFixSystemVPDOption( |
| 1070 | VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT); |
| 1071 | printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH); |
| 1072 | printFixSystemVPDOption(VpdTool::SKIP_CURRENT); |
| 1073 | printFixSystemVPDOption(VpdTool::EXIT); |
| 1074 | } |
| 1075 | |
| 1076 | cin >> option; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1077 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1078 | |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1079 | if (option == VpdTool::BACKUP_DATA_FOR_CURRENT) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1080 | { |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1081 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 1082 | get<1>(data), get<2>(data)); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1083 | edit.updateKeyword(toBinary(get<3>(data)), 0, true); |
| 1084 | cout << "\nData updated successfully.\n"; |
| 1085 | break; |
| 1086 | } |
| 1087 | else if (option == |
| 1088 | VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT) |
| 1089 | { |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1090 | std::string hardwarePath = constants::systemVpdFilePath; |
| 1091 | std::string record = get<1>(data); |
| 1092 | std::string keyword = get<2>(data); |
| 1093 | |
| 1094 | if (!backupEEPROMPath.empty()) |
| 1095 | { |
| 1096 | hardwarePath = backupEEPROMPath; |
| 1097 | getBackupRecordKeyword(record, keyword); |
| 1098 | } |
| 1099 | |
| 1100 | EditorImpl edit(hardwarePath, json, record, keyword); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1101 | edit.updateKeyword(toBinary(get<4>(data)), 0, true); |
| 1102 | cout << "\nData updated successfully.\n"; |
| 1103 | break; |
| 1104 | } |
| 1105 | else if (option == VpdTool::NEW_VALUE_ON_BOTH) |
| 1106 | { |
| 1107 | string value; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1108 | cout << "\nEnter the new value to update on both " |
| 1109 | "primary & backup. Value should be in ASCII or " |
| 1110 | "in HEX(prefixed with 0x) : "; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1111 | cin >> value; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1112 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1113 | |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1114 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 1115 | get<1>(data), get<2>(data)); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1116 | edit.updateKeyword(toBinary(value), 0, true); |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1117 | |
| 1118 | if (!backupEEPROMPath.empty()) |
| 1119 | { |
| 1120 | std::string record = get<1>(data); |
| 1121 | std::string keyword = get<2>(data); |
| 1122 | |
| 1123 | getBackupRecordKeyword(record, keyword); |
| 1124 | EditorImpl edit(backupEEPROMPath, json, record, |
| 1125 | keyword); |
| 1126 | edit.updateKeyword(toBinary(value), 0, true); |
| 1127 | } |
| 1128 | |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1129 | cout << "\nData updated successfully.\n"; |
| 1130 | break; |
| 1131 | } |
| 1132 | else if (option == VpdTool::SKIP_CURRENT) |
| 1133 | { |
| 1134 | cout << "\nSkipped the above record-keyword pair. " |
| 1135 | "Continue to the next available pair.\n"; |
| 1136 | break; |
| 1137 | } |
| 1138 | else if (option == VpdTool::EXIT) |
| 1139 | { |
| 1140 | cout << "\nExit successfully\n"; |
| 1141 | exit(0); |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | cout << "\nProvide a valid option. Retrying for the " |
| 1146 | "current record-keyword pair\n"; |
| 1147 | } |
| 1148 | } while (1); |
| 1149 | } |
| 1150 | exit(0); |
| 1151 | } |
| 1152 | else if (option == VpdTool::EXIT) |
| 1153 | { |
| 1154 | cout << "\nExit successfully"; |
| 1155 | exit(0); |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | cout << "\nProvide a valid option. Retry."; |
| 1160 | continue; |
| 1161 | } |
| 1162 | |
| 1163 | } while (true); |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | int VpdTool::cleanSystemVPD() |
| 1167 | { |
| 1168 | try |
| 1169 | { |
| 1170 | // Get system VPD hardware data in map |
| 1171 | unordered_map<string, DbusPropertyMap> vpdMap; |
| 1172 | json js; |
| 1173 | getVPDInMap(constants::systemVpdFilePath, vpdMap, js, |
| 1174 | constants::pimPath + |
| 1175 | static_cast<std::string>(constants::SYSTEM_OBJECT)); |
| 1176 | |
| 1177 | RecKwValMap kwdsToBeUpdated; |
| 1178 | |
| 1179 | for (auto recordMap : svpdKwdMap) |
| 1180 | { |
| 1181 | const auto& record = recordMap.first; |
| 1182 | std::unordered_map<std::string, Binary> kwDefault; |
| 1183 | for (auto keywordMap : recordMap.second) |
| 1184 | { |
| 1185 | // Skip those keywords which cannot be reset at manufacturing |
| 1186 | if (!std::get<3>(keywordMap)) |
| 1187 | { |
| 1188 | continue; |
| 1189 | } |
| 1190 | const auto& keyword = std::get<0>(keywordMap); |
| 1191 | |
| 1192 | // Get hardware value for this keyword from vpdMap |
| 1193 | Binary hardwareValue; |
| 1194 | |
| 1195 | auto recItr = vpdMap.find(record); |
| 1196 | |
| 1197 | if (recItr != vpdMap.end()) |
| 1198 | { |
| 1199 | DbusPropertyMap& kwValMap = recItr->second; |
| 1200 | auto kwItr = kwValMap.find(keyword); |
| 1201 | if (kwItr != kwValMap.end()) |
| 1202 | { |
| 1203 | hardwareValue = toBinary(kwItr->second); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | // compare hardware value with the keyword's default value |
| 1208 | auto defaultValue = std::get<1>(keywordMap); |
| 1209 | if (hardwareValue != defaultValue) |
| 1210 | { |
| 1211 | EditorImpl edit(constants::systemVpdFilePath, js, record, |
| 1212 | keyword); |
| 1213 | edit.updateKeyword(defaultValue, 0, true); |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 1218 | std::cout << "\n The critical keywords from system backplane VPD has " |
| 1219 | "been reset successfully." |
| 1220 | << std::endl; |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 1221 | } |
| 1222 | catch (const std::exception& e) |
| 1223 | { |
| 1224 | std::cerr << e.what(); |
| 1225 | std::cerr |
| 1226 | << "\nManufacturing reset on system vpd keywords is unsuccessful"; |
| 1227 | } |
| 1228 | return 0; |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 1229 | } |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1230 | |
| 1231 | int VpdTool::fixSystemBackupVPD(const std::string& backupEepromPath, |
| 1232 | const std::string& backupInvPath) |
| 1233 | { |
| 1234 | std::string outline(191, '='); |
| 1235 | cout << "\nRestorable record-keyword pairs and their data on backup & " |
| 1236 | "primary.\n\n" |
| 1237 | << outline << std::endl; |
| 1238 | |
| 1239 | cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left |
| 1240 | << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left |
| 1241 | << setw(75) << "Data On Primary" << left << setw(14) |
| 1242 | << "Data Mismatch\n" |
| 1243 | << outline << std::endl; |
| 1244 | |
| 1245 | uint8_t num = 0; |
| 1246 | // Get system VPD data in map |
| 1247 | unordered_map<string, DbusPropertyMap> systemVPDMap; |
| 1248 | json js; |
| 1249 | getVPDInMap(constants::systemVpdFilePath, systemVPDMap, js, |
| 1250 | constants::pimPath + |
| 1251 | static_cast<std::string>(constants::SYSTEM_OBJECT)); |
| 1252 | |
| 1253 | // Get backup VPD data in map |
| 1254 | unordered_map<string, DbusPropertyMap> backupVPDMap; |
| 1255 | getVPDInMap(backupEepromPath, backupVPDMap, js, |
| 1256 | constants::pimPath + backupInvPath); |
| 1257 | |
| 1258 | for (const auto& recordKw : svpdKwdMap) |
| 1259 | { |
| 1260 | const std::string& primaryRecord = recordKw.first; |
| 1261 | |
| 1262 | std::string primaryValStr{}, backupValStr{}; |
| 1263 | |
| 1264 | for (const auto& keywordInfo : recordKw.second) |
| 1265 | { |
| 1266 | const auto& primaryKeyword = get<0>(keywordInfo); |
| 1267 | const auto& bkRecord = get<4>(keywordInfo); |
| 1268 | const auto& bkKeyword = get<5>(keywordInfo); |
| 1269 | string mismatch = "NO"; |
| 1270 | string primaryValue{}; |
| 1271 | string backupValue{}; |
| 1272 | |
| 1273 | // Find keyword value for system VPD (primary VPD) |
| 1274 | auto primaryRecItr = systemVPDMap.find(primaryRecord); |
| 1275 | if (primaryRecItr != systemVPDMap.end()) |
| 1276 | { |
| 1277 | DbusPropertyMap& primaryKwValMap = primaryRecItr->second; |
| 1278 | auto kwItr = primaryKwValMap.find(primaryKeyword); |
| 1279 | if (kwItr != primaryKwValMap.end()) |
| 1280 | { |
| 1281 | primaryValue = kwItr->second; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | // Find keyword value for backup VPD |
| 1286 | auto bkRecItr = backupVPDMap.find(bkRecord); |
| 1287 | if (bkRecItr != backupVPDMap.end()) |
| 1288 | { |
| 1289 | DbusPropertyMap& bkKwValMap = bkRecItr->second; |
| 1290 | auto kwItr = bkKwValMap.find(bkKeyword); |
| 1291 | if (kwItr != bkKwValMap.end()) |
| 1292 | { |
| 1293 | backupValue = kwItr->second; |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | // SE to display in hex string only |
| 1298 | if (primaryKeyword != "SE") |
| 1299 | { |
| 1300 | ostringstream hwValStream; |
| 1301 | hwValStream << "0x"; |
| 1302 | primaryValStr = hwValStream.str(); |
| 1303 | |
| 1304 | for (uint16_t byte : primaryValue) |
| 1305 | { |
| 1306 | hwValStream << setfill('0') << setw(2) << hex << byte; |
| 1307 | primaryValStr = hwValStream.str(); |
| 1308 | } |
| 1309 | |
| 1310 | hwValStream.str(std::string()); |
| 1311 | hwValStream << "0x"; |
| 1312 | backupValStr = hwValStream.str(); |
| 1313 | |
| 1314 | for (uint16_t byte : backupValue) |
| 1315 | { |
| 1316 | hwValStream << setfill('0') << setw(2) << hex << byte; |
| 1317 | backupValStr = hwValStream.str(); |
| 1318 | } |
| 1319 | if (primaryValStr != backupValStr) |
| 1320 | { |
| 1321 | mismatch = "YES"; |
| 1322 | } |
| 1323 | } |
| 1324 | else |
| 1325 | { |
| 1326 | if (primaryValue != backupValue) |
| 1327 | { |
| 1328 | mismatch = "YES"; |
| 1329 | } |
| 1330 | |
| 1331 | primaryValStr = primaryValue; |
| 1332 | backupValStr = backupValue; |
| 1333 | } |
| 1334 | |
Patrick Williams | 08dc31c | 2024-08-16 15:21:06 -0400 | [diff] [blame] | 1335 | recKwData.push_back( |
| 1336 | make_tuple(++num, primaryRecord, primaryKeyword, backupValStr, |
| 1337 | primaryValStr, mismatch)); |
priyaram | 0eb8cac | 2023-09-20 09:16:46 +0530 | [diff] [blame] | 1338 | |
| 1339 | std::string splitLine(191, '-'); |
| 1340 | cout << left << setw(6) << static_cast<int>(num) << left << setw(8) |
| 1341 | << primaryRecord << left << setw(9) << primaryKeyword << left |
| 1342 | << setw(75) << setfill(' ') << backupValStr << left << setw(75) |
| 1343 | << setfill(' ') << primaryValStr << left << setw(14) |
| 1344 | << mismatch << '\n' |
| 1345 | << splitLine << endl; |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | parseSVPDOptions(js, backupEepromPath); |
| 1350 | return 0; |
| 1351 | } |