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; |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 120 | vpdVector = getVpdDataInVector(js, constants::systemVpdFilePath); |
girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 121 | ParserInterface* parser = ParserFactory::getParser( |
| 122 | vpdVector, invPath, constants::systemVpdFilePath, 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 | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 132 | std::string err = vpdPath + |
| 133 | " 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 | { |
| 344 | if (!(ex.value().is_null())) |
| 345 | { |
Priyanga Ramasamy | dacaa47 | 2021-10-07 12:22:41 -0500 | [diff] [blame] | 346 | // TODO: Remove this if condition check once inventory json is |
| 347 | // updated with xyz location code interface. |
| 348 | if (ex.key() == "com.ibm.ipzvpd.Location") |
| 349 | { |
| 350 | getExtraInterfaceProperties( |
| 351 | invPath, |
| 352 | "xyz.openbmc_project.Inventory.Decorator.LocationCode", |
| 353 | ex.value(), js); |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | getExtraInterfaceProperties(invPath, ex.key(), ex.value(), |
| 358 | js); |
| 359 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 360 | } |
PriyangaRamasamy | 8cc5b15 | 2021-06-03 13:05:17 -0500 | [diff] [blame] | 361 | if ((ex.key().find("Item") != string::npos) && |
| 362 | (ex.value().is_null())) |
| 363 | { |
| 364 | js.emplace("type", ex.key()); |
| 365 | } |
| 366 | subOutput.insert(js.begin(), js.end()); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 367 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 368 | } |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 369 | return subOutput; |
| 370 | } |
| 371 | |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 372 | json VpdTool::getPresentPropJson(const std::string& invPath) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 373 | { |
| 374 | std::variant<bool> response; |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 375 | std::string presence = "Unknown"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 376 | |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 377 | try |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 378 | { |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 379 | makeDBusCall(invPath, "xyz.openbmc_project.Inventory.Item", "Present") |
| 380 | .read(response); |
| 381 | |
| 382 | if (auto pVal = get_if<bool>(&response)) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 383 | { |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 384 | presence = *pVal ? "true" : "false"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 385 | } |
| 386 | } |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 387 | catch (const sdbusplus::exception::SdBusError& e) |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 388 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 389 | presence = "Unknown"; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 390 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 391 | |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 392 | json js; |
| 393 | js.emplace("Present", presence); |
| 394 | return js; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | json VpdTool::parseInvJson(const json& jsObject, char flag, string fruPath) |
| 398 | { |
| 399 | json output = json::object({}); |
| 400 | bool validObject = false; |
| 401 | |
| 402 | if (jsObject.find("frus") == jsObject.end()) |
| 403 | { |
| 404 | throw runtime_error("Frus missing in Inventory json"); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | for (const auto& itemFRUS : jsObject["frus"].items()) |
| 409 | { |
| 410 | for (auto itemEEPROM : itemFRUS.value()) |
| 411 | { |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 412 | json subOutput = json::object({}); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 413 | try |
| 414 | { |
| 415 | if (flag == 'O') |
| 416 | { |
| 417 | if (itemEEPROM.find("inventoryPath") == |
| 418 | itemEEPROM.end()) |
| 419 | { |
| 420 | throw runtime_error("Inventory Path not found"); |
| 421 | } |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 422 | else if (itemEEPROM.at("inventoryPath") == fruPath) |
| 423 | { |
| 424 | validObject = true; |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 425 | subOutput = interfaceDecider(itemEEPROM); |
| 426 | json presentJs = getPresentPropJson( |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 427 | "/xyz/openbmc_project/inventory" + fruPath); |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 428 | subOutput.insert(presentJs.begin(), |
| 429 | presentJs.end()); |
| 430 | output.emplace(fruPath, subOutput); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 431 | return output; |
| 432 | } |
| 433 | } |
| 434 | else |
| 435 | { |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 436 | subOutput = interfaceDecider(itemEEPROM); |
| 437 | json presentJs = getPresentPropJson( |
| 438 | "/xyz/openbmc_project/inventory" + |
Priyanga Ramasamy | d90aadb | 2023-03-28 12:25:14 -0500 | [diff] [blame] | 439 | string(itemEEPROM.at("inventoryPath"))); |
Priyanga Ramasamy | 0086dd1 | 2021-09-28 10:31:50 -0500 | [diff] [blame] | 440 | subOutput.insert(presentJs.begin(), presentJs.end()); |
| 441 | output.emplace(string(itemEEPROM.at("inventoryPath")), |
| 442 | subOutput); |
| 443 | } |
| 444 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 445 | catch (const exception& e) |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 446 | { |
| 447 | cerr << e.what(); |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | if ((flag == 'O') && (!validObject)) |
| 452 | { |
| 453 | throw runtime_error( |
| 454 | "Invalid object path. Refer --dumpInventory/-i option."); |
| 455 | } |
| 456 | } |
| 457 | return output; |
| 458 | } |
| 459 | |
| 460 | void VpdTool::dumpInventory(const nlohmann::basic_json<>& jsObject) |
| 461 | { |
| 462 | char flag = 'I'; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 463 | json output = json::array({}); |
| 464 | output.emplace_back(parseInvJson(jsObject, flag, "")); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 465 | debugger(output); |
| 466 | } |
| 467 | |
| 468 | void VpdTool::dumpObject(const nlohmann::basic_json<>& jsObject) |
| 469 | { |
| 470 | char flag = 'O'; |
PriyangaRamasamy | cdf943c | 2020-03-18 02:25:30 +0530 | [diff] [blame] | 471 | json output = json::array({}); |
Santosh Puranik | d5d52bb | 2020-07-28 15:08:10 +0530 | [diff] [blame] | 472 | output.emplace_back(parseInvJson(jsObject, flag, fruPath)); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 473 | debugger(output); |
| 474 | } |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 475 | |
| 476 | void VpdTool::readKeyword() |
| 477 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 478 | const std::string& kw = getDbusNameForThisKw(keyword); |
| 479 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 480 | string interface = "com.ibm.ipzvpd."; |
| 481 | variant<Binary> response; |
| 482 | |
| 483 | try |
| 484 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 485 | makeDBusCall(INVENTORY_PATH + fruPath, interface + recordName, kw) |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 486 | .read(response); |
| 487 | |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 488 | string printableVal{}; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 489 | if (auto vec = get_if<Binary>(&response)) |
| 490 | { |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 491 | printableVal = getPrintableValue(*vec); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 492 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 493 | |
| 494 | if (!value.empty()) |
| 495 | { |
| 496 | if (copyStringToFile(printableVal)) |
| 497 | { |
| 498 | std::cout << "Value read is saved in the file " << value |
| 499 | << std::endl; |
| 500 | return; |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | std::cerr << "Error while saving the read value in file. " |
| 505 | "Displaying the read value on console" |
| 506 | << std::endl; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | json output = json::object({}); |
| 511 | json kwVal = json::object({}); |
PriyangaRamasamy | 887a42a | 2020-09-03 00:33:57 +0530 | [diff] [blame] | 512 | kwVal.emplace(keyword, printableVal); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 513 | |
| 514 | output.emplace(fruPath, kwVal); |
| 515 | |
| 516 | debugger(output); |
| 517 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 518 | catch (const json::exception& e) |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 519 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 520 | std::cout << "Keyword Value: " << keyword << std::endl; |
| 521 | std::cout << e.what() << std::endl; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 522 | } |
| 523 | } |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 524 | |
| 525 | int VpdTool::updateKeyword() |
| 526 | { |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 527 | Binary val; |
| 528 | |
| 529 | if (std::filesystem::exists(value)) |
| 530 | { |
| 531 | if (!fileToVector(val)) |
| 532 | { |
| 533 | std::cout << "Keyword " << keyword << " update failed." |
| 534 | << std::endl; |
| 535 | return 1; |
| 536 | } |
| 537 | } |
| 538 | else |
| 539 | { |
| 540 | val = toBinary(value); |
| 541 | } |
| 542 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 543 | auto bus = sdbusplus::bus::new_default(); |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 544 | auto properties = bus.new_method_call(BUSNAME, OBJPATH, IFACE, |
| 545 | "WriteKeyword"); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 546 | properties.append(static_cast<sdbusplus::message::object_path>(fruPath)); |
| 547 | properties.append(recordName); |
| 548 | properties.append(keyword); |
| 549 | properties.append(val); |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 550 | |
| 551 | // When there is a request to write 10K bytes, there occurs a delay in dbus |
| 552 | // call which leads to dbus timeout exception. To avoid such exceptions |
| 553 | // increase the timeout period from default 25 seconds to 60 seconds. |
| 554 | auto timeoutInMicroSeconds = 60 * 1000000L; |
| 555 | auto result = bus.call(properties, timeoutInMicroSeconds); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 556 | |
| 557 | if (result.is_method_error()) |
| 558 | { |
| 559 | throw runtime_error("Get api failed"); |
| 560 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 561 | std::cout << "Data updated successfully " << std::endl; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 562 | return 0; |
| 563 | } |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 564 | |
| 565 | void VpdTool::forceReset(const nlohmann::basic_json<>& jsObject) |
| 566 | { |
| 567 | for (const auto& itemFRUS : jsObject["frus"].items()) |
| 568 | { |
| 569 | for (const auto& itemEEPROM : itemFRUS.value().items()) |
| 570 | { |
| 571 | string fru = itemEEPROM.value().at("inventoryPath"); |
| 572 | |
| 573 | fs::path fruCachePath = INVENTORY_MANAGER_CACHE; |
| 574 | fruCachePath += INVENTORY_PATH; |
| 575 | fruCachePath += fru; |
| 576 | |
| 577 | try |
| 578 | { |
| 579 | for (const auto& it : fs::directory_iterator(fruCachePath)) |
| 580 | { |
| 581 | if (fs::is_regular_file(it.status())) |
| 582 | { |
| 583 | fs::remove(it); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | catch (const fs::filesystem_error& e) |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 588 | {} |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 592 | cout.flush(); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 593 | string udevRemove = "udevadm trigger -c remove -s \"*nvmem*\" -v"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 594 | int returnCode = system(udevRemove.c_str()); |
| 595 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 596 | |
| 597 | string invManagerRestart = |
| 598 | "systemctl restart xyz.openbmc_project.Inventory.Manager.service"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 599 | returnCode = system(invManagerRestart.c_str()); |
| 600 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 601 | |
Santosh Puranik | 6c7a84e | 2022-03-09 13:42:18 +0530 | [diff] [blame] | 602 | string sysVpdRestart = "systemctl restart system-vpd.service"; |
| 603 | returnCode = system(sysVpdRestart.c_str()); |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 604 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 605 | |
| 606 | string udevAdd = "udevadm trigger -c add -s \"*nvmem*\" -v"; |
PriyangaRamasamy | 6ee637a | 2021-02-12 04:49:02 -0600 | [diff] [blame] | 607 | returnCode = system(udevAdd.c_str()); |
| 608 | printReturnCode(returnCode); |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 609 | } |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 610 | |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 611 | int VpdTool::updateHardware(const uint32_t offset) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 612 | { |
| 613 | int rc = 0; |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 614 | Binary val; |
| 615 | if (std::filesystem::exists(value)) |
| 616 | { |
| 617 | if (!fileToVector(val)) |
| 618 | { |
| 619 | std::cout << "Keyword " << keyword << " update failed." |
| 620 | << std::endl; |
| 621 | return 1; |
| 622 | } |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | val = toBinary(value); |
| 627 | } |
| 628 | |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 629 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
| 630 | try |
| 631 | { |
| 632 | auto json = nlohmann::json::parse(inventoryJson); |
| 633 | EditorImpl edit(fruPath, json, recordName, keyword); |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 634 | |
| 635 | edit.updateKeyword(val, offset, false); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 636 | } |
Patrick Williams | 8e15b93 | 2021-10-06 13:04:22 -0500 | [diff] [blame] | 637 | catch (const json::parse_error& ex) |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 638 | { |
| 639 | throw(VpdJsonException("Json Parsing failed", INVENTORY_JSON_SYM_LINK)); |
| 640 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 641 | std::cout << "Data updated successfully " << std::endl; |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 642 | return rc; |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 643 | } |
| 644 | |
Priyanga Ramasamy | c99a0b0 | 2022-06-08 14:53:39 -0500 | [diff] [blame] | 645 | void VpdTool::readKwFromHw(const uint32_t& startOffset) |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 646 | { |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 647 | ifstream inventoryJson(INVENTORY_JSON_SYM_LINK); |
| 648 | auto jsonFile = nlohmann::json::parse(inventoryJson); |
| 649 | |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 650 | Binary completeVPDFile; |
| 651 | completeVPDFile.resize(65504); |
| 652 | fstream vpdFileStream; |
| 653 | vpdFileStream.open(fruPath, |
| 654 | std::ios::in | std::ios::out | std::ios::binary); |
| 655 | |
| 656 | vpdFileStream.seekg(startOffset, ios_base::cur); |
| 657 | vpdFileStream.read(reinterpret_cast<char*>(&completeVPDFile[0]), 65504); |
| 658 | completeVPDFile.resize(vpdFileStream.gcount()); |
| 659 | vpdFileStream.clear(std::ios_base::eofbit); |
| 660 | |
| 661 | if (completeVPDFile.empty()) |
| 662 | { |
| 663 | throw std::runtime_error("Invalid File"); |
| 664 | } |
Sunny Srivastava | f31a91b | 2022-06-09 08:11:29 -0500 | [diff] [blame] | 665 | |
| 666 | const std::string& inventoryPath = |
| 667 | jsonFile["frus"][fruPath][0]["inventoryPath"]; |
| 668 | |
girik | 18bb985 | 2022-11-16 05:48:13 -0600 | [diff] [blame] | 669 | uint32_t vpdStartOffset = 0; |
| 670 | for (const auto& item : jsonFile["frus"][fruPath]) |
| 671 | { |
| 672 | if (item.find("offset") != item.end()) |
| 673 | { |
| 674 | vpdStartOffset = item["offset"]; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | Impl obj(completeVPDFile, (constants::pimPath + inventoryPath), fruPath, |
| 679 | vpdStartOffset); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 680 | std::string keywordVal = obj.readKwFromHw(recordName, keyword); |
| 681 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 682 | keywordVal = getPrintableValue(keywordVal); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 683 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 684 | if (keywordVal.empty()) |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 685 | { |
| 686 | std::cerr << "The given keyword " << keyword << " or record " |
| 687 | << recordName |
| 688 | << " or both are not present in the given FRU path " |
| 689 | << fruPath << std::endl; |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 690 | return; |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 691 | } |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 692 | |
| 693 | if (!value.empty()) |
| 694 | { |
| 695 | if (copyStringToFile(keywordVal)) |
| 696 | { |
| 697 | std::cout << "Value read is saved in the file " << value |
| 698 | << std::endl; |
| 699 | return; |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | std::cerr |
| 704 | << "Error while saving the read value in file. Displaying " |
| 705 | "the read value on console" |
| 706 | << std::endl; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | json output = json::object({}); |
| 711 | json kwVal = json::object({}); |
| 712 | kwVal.emplace(keyword, keywordVal); |
| 713 | output.emplace(fruPath, kwVal); |
| 714 | debugger(output); |
Priyanga Ramasamy | 3803131 | 2021-10-07 16:39:13 -0500 | [diff] [blame] | 715 | } |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 716 | |
| 717 | void VpdTool::printFixSystemVPDOption(UserOption option) |
| 718 | { |
| 719 | switch (option) |
| 720 | { |
| 721 | case VpdTool::EXIT: |
| 722 | cout << "\nEnter 0 => To exit successfully : "; |
| 723 | break; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 724 | case VpdTool::BACKUP_DATA_FOR_ALL: |
| 725 | 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] | 726 | "mismatching record-keyword pairs"; |
| 727 | break; |
| 728 | case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 729 | cout << "\nEnter 2 => If you choose the data on primary for all " |
| 730 | "mismatching record-keyword pairs"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 731 | break; |
| 732 | case VpdTool::MORE_OPTIONS: |
| 733 | cout << "\nEnter 3 => If you wish to explore more options"; |
| 734 | break; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 735 | case VpdTool::BACKUP_DATA_FOR_CURRENT: |
| 736 | cout << "\nEnter 4 => If you choose the data on backup as the " |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 737 | "right value"; |
| 738 | break; |
| 739 | case VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 740 | cout << "\nEnter 5 => If you choose the data on primary as the " |
| 741 | "right value"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 742 | break; |
| 743 | case VpdTool::NEW_VALUE_ON_BOTH: |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 744 | cout << "\nEnter 6 => If you wish to enter a new value to update " |
| 745 | "both on backup and primary"; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 746 | break; |
| 747 | case VpdTool::SKIP_CURRENT: |
| 748 | cout << "\nEnter 7 => If you wish to skip the above " |
| 749 | "record-keyword pair"; |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 754 | void VpdTool::getSystemDataFromCache(IntfPropMap& svpdBusData) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 755 | { |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 756 | const auto vsys = getAllDBusProperty<GetAllResultType>( |
| 757 | constants::pimIntf, |
| 758 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 759 | "com.ibm.ipzvpd.VSYS"); |
| 760 | svpdBusData.emplace("VSYS", vsys); |
| 761 | |
| 762 | const auto vcen = getAllDBusProperty<GetAllResultType>( |
| 763 | constants::pimIntf, |
| 764 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 765 | "com.ibm.ipzvpd.VCEN"); |
| 766 | svpdBusData.emplace("VCEN", vcen); |
| 767 | |
| 768 | const auto lxr0 = getAllDBusProperty<GetAllResultType>( |
| 769 | constants::pimIntf, |
| 770 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 771 | "com.ibm.ipzvpd.LXR0"); |
| 772 | svpdBusData.emplace("LXR0", lxr0); |
| 773 | |
| 774 | const auto util = getAllDBusProperty<GetAllResultType>( |
| 775 | constants::pimIntf, |
| 776 | "/xyz/openbmc_project/inventory/system/chassis/motherboard", |
| 777 | "com.ibm.ipzvpd.UTIL"); |
| 778 | svpdBusData.emplace("UTIL", util); |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | int VpdTool::fixSystemVPD() |
| 782 | { |
| 783 | std::string outline(191, '='); |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 784 | cout << "\nRestorable record-keyword pairs and their data on backup & " |
| 785 | "primary.\n\n" |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 786 | << outline << std::endl; |
| 787 | |
| 788 | cout << left << setw(6) << "S.No" << left << setw(8) << "Record" << left |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 789 | << setw(9) << "Keyword" << left << setw(75) << "Data On Backup" << left |
| 790 | << setw(75) << "Data On Primary" << left << setw(14) |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 791 | << "Data Mismatch\n" |
| 792 | << outline << std::endl; |
| 793 | |
| 794 | int num = 0; |
| 795 | |
| 796 | // Get system VPD data in map |
| 797 | unordered_map<string, DbusPropertyMap> vpdMap; |
| 798 | json js; |
| 799 | getVPDInMap(constants::systemVpdFilePath, vpdMap, js, |
| 800 | constants::pimPath + |
| 801 | static_cast<std::string>(constants::SYSTEM_OBJECT)); |
| 802 | |
| 803 | // Get system VPD D-Bus Data in a map |
| 804 | IntfPropMap svpdBusData; |
| 805 | getSystemDataFromCache(svpdBusData); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 806 | |
| 807 | for (const auto& recordKw : svpdKwdMap) |
| 808 | { |
| 809 | string record = recordKw.first; |
| 810 | |
| 811 | // Extract specific record data from the svpdBusData map. |
| 812 | const auto& rec = svpdBusData.find(record); |
| 813 | |
| 814 | if (rec == svpdBusData.end()) |
| 815 | { |
| 816 | std::cerr << record << " not a part of critical system VPD records." |
| 817 | << std::endl; |
| 818 | continue; |
| 819 | } |
| 820 | |
| 821 | const auto& recData = svpdBusData.find(record)->second; |
| 822 | |
| 823 | string busStr{}, hwValStr{}; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 824 | |
Priyanga Ramasamy | 952d6c5 | 2022-11-07 07:20:24 -0600 | [diff] [blame] | 825 | for (const auto& keywordInfo : recordKw.second) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 826 | { |
Priyanga Ramasamy | 952d6c5 | 2022-11-07 07:20:24 -0600 | [diff] [blame] | 827 | const auto& keyword = get<0>(keywordInfo); |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 828 | string mismatch = "NO"; // no mismatch |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 829 | string hardwareValue{}; |
| 830 | auto recItr = vpdMap.find(record); |
| 831 | |
| 832 | if (recItr != vpdMap.end()) |
| 833 | { |
| 834 | DbusPropertyMap& kwValMap = recItr->second; |
| 835 | auto kwItr = kwValMap.find(keyword); |
| 836 | if (kwItr != kwValMap.end()) |
| 837 | { |
| 838 | hardwareValue = kwItr->second; |
| 839 | } |
| 840 | } |
| 841 | |
Priyanga Ramasamy | 6d5e734 | 2022-10-14 07:17:25 -0500 | [diff] [blame] | 842 | inventory::Value kwValue; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 843 | for (auto& kwData : recData) |
| 844 | { |
| 845 | if (kwData.first == keyword) |
| 846 | { |
| 847 | kwValue = kwData.second; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 852 | if (keyword != "SE") // SE to display in Hex string only |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 853 | { |
| 854 | ostringstream hwValStream; |
| 855 | hwValStream << "0x"; |
| 856 | hwValStr = hwValStream.str(); |
| 857 | |
| 858 | for (uint16_t byte : hardwareValue) |
| 859 | { |
| 860 | hwValStream << setfill('0') << setw(2) << hex << byte; |
| 861 | hwValStr = hwValStream.str(); |
| 862 | } |
| 863 | |
| 864 | if (const auto value = get_if<Binary>(&kwValue)) |
| 865 | { |
GiridhariKrishnan | 6363910 | 2023-03-02 05:55:47 -0600 | [diff] [blame] | 866 | busStr = hexString(*value); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 867 | } |
| 868 | if (busStr != hwValStr) |
| 869 | { |
| 870 | mismatch = "YES"; |
| 871 | } |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | if (const auto value = get_if<Binary>(&kwValue)) |
| 876 | { |
| 877 | busStr = getPrintableValue(*value); |
| 878 | } |
| 879 | if (busStr != hardwareValue) |
| 880 | { |
| 881 | mismatch = "YES"; |
| 882 | } |
| 883 | hwValStr = hardwareValue; |
| 884 | } |
| 885 | recKwData.push_back( |
| 886 | make_tuple(++num, record, keyword, busStr, hwValStr, mismatch)); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 887 | |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 888 | std::string splitLine(191, '-'); |
| 889 | cout << left << setw(6) << num << left << setw(8) << record << left |
| 890 | << setw(9) << keyword << left << setw(75) << setfill(' ') |
| 891 | << busStr << left << setw(75) << setfill(' ') << hwValStr |
| 892 | << left << setw(14) << mismatch << '\n' |
| 893 | << splitLine << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | parseSVPDOptions(js); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | void VpdTool::parseSVPDOptions(const nlohmann::json& json) |
| 901 | { |
| 902 | do |
| 903 | { |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 904 | printFixSystemVPDOption(VpdTool::BACKUP_DATA_FOR_ALL); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 905 | printFixSystemVPDOption(VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL); |
| 906 | printFixSystemVPDOption(VpdTool::MORE_OPTIONS); |
| 907 | printFixSystemVPDOption(VpdTool::EXIT); |
| 908 | |
| 909 | int option = 0; |
| 910 | cin >> option; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 911 | |
| 912 | std::string outline(191, '='); |
| 913 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 914 | |
| 915 | if (json.find("frus") == json.end()) |
| 916 | { |
| 917 | throw runtime_error("Frus not found in json"); |
| 918 | } |
| 919 | |
| 920 | bool mismatchFound = false; |
| 921 | |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 922 | if (option == VpdTool::BACKUP_DATA_FOR_ALL) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 923 | { |
| 924 | for (const auto& data : recKwData) |
| 925 | { |
| 926 | if (get<5>(data) == "YES") |
| 927 | { |
| 928 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 929 | get<1>(data), get<2>(data)); |
| 930 | edit.updateKeyword(toBinary(get<3>(data)), 0, true); |
| 931 | mismatchFound = true; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | if (mismatchFound) |
| 936 | { |
| 937 | cout << "\nData updated successfully for all mismatching " |
| 938 | "record-keyword pairs by choosing their corresponding " |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 939 | "data from backup. Exit successfully.\n" |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 940 | << endl; |
| 941 | } |
| 942 | else |
| 943 | { |
| 944 | cout << "\nNo mismatch found for any of the above mentioned " |
| 945 | "record-keyword pair. Exit successfully.\n"; |
| 946 | } |
| 947 | |
| 948 | exit(0); |
| 949 | } |
| 950 | else if (option == VpdTool::SYSTEM_BACKPLANE_DATA_FOR_ALL) |
| 951 | { |
| 952 | for (const auto& data : recKwData) |
| 953 | { |
| 954 | if (get<5>(data) == "YES") |
| 955 | { |
| 956 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 957 | get<1>(data), get<2>(data)); |
| 958 | edit.updateKeyword(toBinary(get<4>(data)), 0, true); |
| 959 | mismatchFound = true; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | if (mismatchFound) |
| 964 | { |
| 965 | cout << "\nData updated successfully for all mismatching " |
| 966 | "record-keyword pairs by choosing their corresponding " |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 967 | "data from primary VPD.\n" |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 968 | << endl; |
| 969 | } |
| 970 | else |
| 971 | { |
| 972 | cout << "\nNo mismatch found for any of the above mentioned " |
| 973 | "record-keyword pair. Exit successfully.\n"; |
| 974 | } |
| 975 | |
| 976 | exit(0); |
| 977 | } |
| 978 | else if (option == VpdTool::MORE_OPTIONS) |
| 979 | { |
| 980 | cout << "\nIterate through all restorable record-keyword pairs\n"; |
| 981 | |
| 982 | for (const auto& data : recKwData) |
| 983 | { |
| 984 | do |
| 985 | { |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 986 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 987 | |
| 988 | cout << left << setw(6) << "S.No" << left << setw(8) |
| 989 | << "Record" << left << setw(9) << "Keyword" << left |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 990 | << setw(75) << setfill(' ') << "Backup Data" << left |
| 991 | << setw(75) << setfill(' ') << "Primary Data" << left |
| 992 | << setw(14) << "Data Mismatch" << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 993 | |
| 994 | cout << left << setw(6) << get<0>(data) << left << setw(8) |
| 995 | << get<1>(data) << left << setw(9) << get<2>(data) |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 996 | << left << setw(75) << setfill(' ') << get<3>(data) |
| 997 | << left << setw(75) << setfill(' ') << get<4>(data) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 998 | << left << setw(14) << get<5>(data); |
| 999 | |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1000 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1001 | |
| 1002 | if (get<5>(data) == "NO") |
| 1003 | { |
| 1004 | cout << "\nNo mismatch found.\n"; |
| 1005 | printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH); |
| 1006 | printFixSystemVPDOption(VpdTool::SKIP_CURRENT); |
| 1007 | printFixSystemVPDOption(VpdTool::EXIT); |
| 1008 | } |
| 1009 | else |
| 1010 | { |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1011 | printFixSystemVPDOption( |
| 1012 | VpdTool::BACKUP_DATA_FOR_CURRENT); |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1013 | printFixSystemVPDOption( |
| 1014 | VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT); |
| 1015 | printFixSystemVPDOption(VpdTool::NEW_VALUE_ON_BOTH); |
| 1016 | printFixSystemVPDOption(VpdTool::SKIP_CURRENT); |
| 1017 | printFixSystemVPDOption(VpdTool::EXIT); |
| 1018 | } |
| 1019 | |
| 1020 | cin >> option; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1021 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1022 | |
| 1023 | EditorImpl edit(constants::systemVpdFilePath, json, |
| 1024 | get<1>(data), get<2>(data)); |
| 1025 | |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1026 | if (option == VpdTool::BACKUP_DATA_FOR_CURRENT) |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1027 | { |
| 1028 | edit.updateKeyword(toBinary(get<3>(data)), 0, true); |
| 1029 | cout << "\nData updated successfully.\n"; |
| 1030 | break; |
| 1031 | } |
| 1032 | else if (option == |
| 1033 | VpdTool::SYSTEM_BACKPLANE_DATA_FOR_CURRENT) |
| 1034 | { |
| 1035 | edit.updateKeyword(toBinary(get<4>(data)), 0, true); |
| 1036 | cout << "\nData updated successfully.\n"; |
| 1037 | break; |
| 1038 | } |
| 1039 | else if (option == VpdTool::NEW_VALUE_ON_BOTH) |
| 1040 | { |
| 1041 | string value; |
Priyanga Ramasamy | 2494223 | 2023-01-05 04:54:59 -0600 | [diff] [blame] | 1042 | cout << "\nEnter the new value to update on both " |
| 1043 | "primary & backup. Value should be in ASCII or " |
| 1044 | "in HEX(prefixed with 0x) : "; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1045 | cin >> value; |
Priyanga Ramasamy | 0ac1085 | 2022-10-26 05:35:42 -0500 | [diff] [blame] | 1046 | cout << '\n' << outline << endl; |
Priyanga Ramasamy | 43ffcf7 | 2022-06-08 14:10:11 -0500 | [diff] [blame] | 1047 | |
| 1048 | edit.updateKeyword(toBinary(value), 0, true); |
| 1049 | cout << "\nData updated successfully.\n"; |
| 1050 | break; |
| 1051 | } |
| 1052 | else if (option == VpdTool::SKIP_CURRENT) |
| 1053 | { |
| 1054 | cout << "\nSkipped the above record-keyword pair. " |
| 1055 | "Continue to the next available pair.\n"; |
| 1056 | break; |
| 1057 | } |
| 1058 | else if (option == VpdTool::EXIT) |
| 1059 | { |
| 1060 | cout << "\nExit successfully\n"; |
| 1061 | exit(0); |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | cout << "\nProvide a valid option. Retrying for the " |
| 1066 | "current record-keyword pair\n"; |
| 1067 | } |
| 1068 | } while (1); |
| 1069 | } |
| 1070 | exit(0); |
| 1071 | } |
| 1072 | else if (option == VpdTool::EXIT) |
| 1073 | { |
| 1074 | cout << "\nExit successfully"; |
| 1075 | exit(0); |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | cout << "\nProvide a valid option. Retry."; |
| 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | } while (true); |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | int VpdTool::cleanSystemVPD() |
| 1087 | { |
| 1088 | try |
| 1089 | { |
| 1090 | // Get system VPD hardware data in map |
| 1091 | unordered_map<string, DbusPropertyMap> vpdMap; |
| 1092 | json js; |
| 1093 | getVPDInMap(constants::systemVpdFilePath, vpdMap, js, |
| 1094 | constants::pimPath + |
| 1095 | static_cast<std::string>(constants::SYSTEM_OBJECT)); |
| 1096 | |
| 1097 | RecKwValMap kwdsToBeUpdated; |
| 1098 | |
| 1099 | for (auto recordMap : svpdKwdMap) |
| 1100 | { |
| 1101 | const auto& record = recordMap.first; |
| 1102 | std::unordered_map<std::string, Binary> kwDefault; |
| 1103 | for (auto keywordMap : recordMap.second) |
| 1104 | { |
| 1105 | // Skip those keywords which cannot be reset at manufacturing |
| 1106 | if (!std::get<3>(keywordMap)) |
| 1107 | { |
| 1108 | continue; |
| 1109 | } |
| 1110 | const auto& keyword = std::get<0>(keywordMap); |
| 1111 | |
| 1112 | // Get hardware value for this keyword from vpdMap |
| 1113 | Binary hardwareValue; |
| 1114 | |
| 1115 | auto recItr = vpdMap.find(record); |
| 1116 | |
| 1117 | if (recItr != vpdMap.end()) |
| 1118 | { |
| 1119 | DbusPropertyMap& kwValMap = recItr->second; |
| 1120 | auto kwItr = kwValMap.find(keyword); |
| 1121 | if (kwItr != kwValMap.end()) |
| 1122 | { |
| 1123 | hardwareValue = toBinary(kwItr->second); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | // compare hardware value with the keyword's default value |
| 1128 | auto defaultValue = std::get<1>(keywordMap); |
| 1129 | if (hardwareValue != defaultValue) |
| 1130 | { |
| 1131 | EditorImpl edit(constants::systemVpdFilePath, js, record, |
| 1132 | keyword); |
| 1133 | edit.updateKeyword(defaultValue, 0, true); |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
Priyanga Ramasamy | 5629fbc | 2023-03-01 08:17:19 -0600 | [diff] [blame] | 1138 | std::cout << "\n The critical keywords from system backplane VPD has " |
| 1139 | "been reset successfully." |
| 1140 | << std::endl; |
Priyanga Ramasamy | 124ae6c | 2022-10-18 12:46:14 -0500 | [diff] [blame] | 1141 | } |
| 1142 | catch (const std::exception& e) |
| 1143 | { |
| 1144 | std::cerr << e.what(); |
| 1145 | std::cerr |
| 1146 | << "\nManufacturing reset on system vpd keywords is unsuccessful"; |
| 1147 | } |
| 1148 | return 0; |
Patrick Williams | c78d887 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 1149 | } |