SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "manager.hpp" |
| 4 | |
SunnySrivastava1984 | d076da8 | 2020-03-05 05:33:35 -0600 | [diff] [blame] | 5 | #include "editor_impl.hpp" |
Sunny Srivastava | 6c71c9d | 2021-04-15 04:43:54 -0500 | [diff] [blame] | 6 | #include "ibm_vpd_utils.hpp" |
SunnySrivastava1984 | e12b181 | 2020-05-26 02:23:11 -0500 | [diff] [blame] | 7 | #include "ipz_parser.hpp" |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 8 | #include "reader_impl.hpp" |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 9 | #include "vpd_exceptions.hpp" |
| 10 | |
| 11 | #include <phosphor-logging/elog-errors.hpp> |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 12 | |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 13 | using namespace openpower::vpd::constants; |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 14 | using namespace openpower::vpd::inventory; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 15 | using namespace openpower::vpd::manager::editor; |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 16 | using namespace openpower::vpd::manager::reader; |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 17 | using namespace std; |
| 18 | using namespace openpower::vpd::parser; |
| 19 | using namespace openpower::vpd::exceptions; |
| 20 | using namespace phosphor::logging; |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 21 | |
| 22 | namespace openpower |
| 23 | { |
| 24 | namespace vpd |
| 25 | { |
| 26 | namespace manager |
| 27 | { |
| 28 | Manager::Manager(sdbusplus::bus::bus&& bus, const char* busName, |
Priyanga Ramasamy | 9d14934 | 2020-07-16 23:41:26 +0530 | [diff] [blame] | 29 | const char* objPath, const char* /*iFace*/) : |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 30 | ServerObject<ManagerIface>(bus, objPath), |
| 31 | _bus(std::move(bus)), _manager(_bus, objPath) |
| 32 | { |
| 33 | _bus.request_name(busName); |
| 34 | } |
| 35 | |
| 36 | void Manager::run() |
| 37 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 38 | try |
| 39 | { |
| 40 | processJSON(); |
| 41 | } |
| 42 | catch (const std::exception& e) |
| 43 | { |
| 44 | std::cerr << e.what() << "\n"; |
| 45 | } |
| 46 | |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 47 | while (true) |
| 48 | { |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 49 | _bus.process_discard(); |
| 50 | |
| 51 | // wait for event |
| 52 | _bus.wait(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void Manager::processJSON() |
| 57 | { |
Santosh Puranik | 0246a4d | 2020-11-04 16:57:39 +0530 | [diff] [blame] | 58 | std::ifstream json(INVENTORY_JSON_SYM_LINK, std::ios::binary); |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 59 | |
| 60 | if (!json) |
| 61 | { |
| 62 | throw std::runtime_error("json file not found"); |
| 63 | } |
| 64 | |
| 65 | jsonFile = nlohmann::json::parse(json); |
| 66 | if (jsonFile.find("frus") == jsonFile.end()) |
| 67 | { |
| 68 | throw std::runtime_error("frus group not found in json"); |
| 69 | } |
| 70 | |
| 71 | const nlohmann::json& groupFRUS = |
| 72 | jsonFile["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 73 | for (const auto& itemFRUS : groupFRUS.items()) |
| 74 | { |
| 75 | const std::vector<nlohmann::json>& groupEEPROM = |
| 76 | itemFRUS.value().get_ref<const nlohmann::json::array_t&>(); |
| 77 | for (const auto& itemEEPROM : groupEEPROM) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 78 | { |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 79 | bool isMotherboard = false; |
| 80 | if (itemEEPROM["extraInterfaces"].find( |
| 81 | "xyz.openbmc_project.Inventory.Item.Board.Motherboard") != |
| 82 | itemEEPROM["extraInterfaces"].end()) |
| 83 | { |
| 84 | isMotherboard = true; |
| 85 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 86 | frus.emplace(itemEEPROM["inventoryPath"] |
| 87 | .get_ref<const nlohmann::json::string_t&>(), |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 88 | std::make_pair(itemFRUS.key(), isMotherboard)); |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 89 | |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 90 | if (itemEEPROM["extraInterfaces"].find(IBM_LOCATION_CODE_INF) != |
Alpana Kumari | 920408d | 2020-05-14 00:07:03 -0500 | [diff] [blame] | 91 | itemEEPROM["extraInterfaces"].end()) |
| 92 | { |
| 93 | fruLocationCode.emplace( |
Alpana Kumari | 414d5ae | 2021-03-04 21:06:35 +0000 | [diff] [blame] | 94 | itemEEPROM["extraInterfaces"][IBM_LOCATION_CODE_INF] |
Alpana Kumari | 920408d | 2020-05-14 00:07:03 -0500 | [diff] [blame] | 95 | ["LocationCode"] |
| 96 | .get_ref<const nlohmann::json::string_t&>(), |
| 97 | itemEEPROM["inventoryPath"] |
| 98 | .get_ref<const nlohmann::json::string_t&>()); |
| 99 | } |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 100 | |
| 101 | if (itemEEPROM.value("isReplaceable", false)) |
| 102 | { |
| 103 | replaceableFrus.emplace_back(itemFRUS.key()); |
| 104 | } |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void Manager::writeKeyword(const sdbusplus::message::object_path path, |
| 110 | const std::string recordName, |
| 111 | const std::string keyword, const Binary value) |
| 112 | { |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 113 | try |
| 114 | { |
| 115 | if (frus.find(path) == frus.end()) |
| 116 | { |
| 117 | throw std::runtime_error("Inventory path not found"); |
| 118 | } |
| 119 | |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 120 | inventory::Path vpdFilePath = frus.find(path)->second.first; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 121 | |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 122 | // instantiate editor class to update the data |
Alpana Kumari | 920408d | 2020-05-14 00:07:03 -0500 | [diff] [blame] | 123 | EditorImpl edit(vpdFilePath, jsonFile, recordName, keyword, path); |
PriyangaRamasamy | c0a534f | 2020-08-24 21:29:18 +0530 | [diff] [blame] | 124 | edit.updateKeyword(value, true); |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 125 | |
SunnySrivastava1984 | 4330654 | 2020-04-01 02:50:20 -0500 | [diff] [blame] | 126 | // if it is a motehrboard FRU need to check for location expansion |
| 127 | if (frus.find(path)->second.second) |
| 128 | { |
| 129 | if (recordName == "VCEN" && (keyword == "FC" || keyword == "SE")) |
| 130 | { |
| 131 | edit.expandLocationCode("fcs"); |
| 132 | } |
| 133 | else if (recordName == "VSYS" && |
| 134 | (keyword == "TM" || keyword == "SE")) |
| 135 | { |
| 136 | edit.expandLocationCode("mts"); |
| 137 | } |
| 138 | } |
| 139 | |
SunnySrivastava1984 | 6d8314d | 2020-05-15 09:34:58 -0500 | [diff] [blame] | 140 | return; |
SunnySrivastava1984 | f6d541e | 2020-02-04 12:50:40 -0600 | [diff] [blame] | 141 | } |
| 142 | catch (const std::exception& e) |
| 143 | { |
| 144 | std::cerr << e.what() << std::endl; |
| 145 | } |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 146 | } |
| 147 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 148 | ListOfPaths |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 149 | Manager::getFRUsByUnexpandedLocationCode(const LocationCode locationCode, |
| 150 | const NodeNumber nodeNumber) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 151 | { |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 152 | ReaderImpl read; |
| 153 | return read.getFrusAtLocation(locationCode, nodeNumber, fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 154 | } |
| 155 | |
SunnySrivastava1984 | 1356d7e | 2020-04-24 04:29:35 -0500 | [diff] [blame] | 156 | ListOfPaths |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 157 | Manager::getFRUsByExpandedLocationCode(const LocationCode locationCode) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 158 | { |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 159 | ReaderImpl read; |
| 160 | return read.getFRUsByExpandedLocationCode(locationCode, fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 161 | } |
| 162 | |
SunnySrivastava1984 | fb5815a | 2020-04-24 08:03:52 -0500 | [diff] [blame] | 163 | LocationCode Manager::getExpandedLocationCode(const LocationCode locationCode, |
| 164 | const NodeNumber nodeNumber) |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 165 | { |
SunnySrivastava1984 | bca5aaa | 2020-04-21 05:31:04 -0500 | [diff] [blame] | 166 | ReaderImpl read; |
| 167 | return read.getExpandedLocationCode(locationCode, nodeNumber, |
| 168 | fruLocationCode); |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 169 | } |
SunnySrivastava1984 | de3c60d | 2020-02-03 10:34:33 -0600 | [diff] [blame] | 170 | |
SunnySrivastava1984 | 9a19554 | 2020-09-07 06:04:50 -0500 | [diff] [blame] | 171 | void Manager::performVPDRecollection() |
| 172 | { |
| 173 | // get list of FRUs replaceable at standby |
| 174 | for (const auto& item : replaceableFrus) |
| 175 | { |
| 176 | const vector<nlohmann::json>& groupEEPROM = jsonFile["frus"][item]; |
| 177 | const nlohmann::json& singleFru = groupEEPROM[0]; |
| 178 | |
| 179 | const string& inventoryPath = |
| 180 | singleFru["inventoryPath"] |
| 181 | .get_ref<const nlohmann::json::string_t&>(); |
| 182 | |
| 183 | if ((singleFru.find("devAddress") == singleFru.end()) || |
| 184 | (singleFru.find("driverType") == singleFru.end()) || |
| 185 | (singleFru.find("busType") == singleFru.end())) |
| 186 | { |
| 187 | // The FRUs is marked for replacement but missing mandatory |
| 188 | // fields for recollection. Skip to another replaceable fru. |
| 189 | log<level::ERR>( |
| 190 | "Recollection Failed as mandatory field missing in Json", |
| 191 | entry("ERROR=%s", |
| 192 | ("Recollection failed for " + inventoryPath).c_str())); |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | string str = "echo "; |
| 197 | string deviceAddress = singleFru["devAddress"]; |
| 198 | const string& driverType = singleFru["driverType"]; |
| 199 | const string& busType = singleFru["busType"]; |
| 200 | |
| 201 | // devTreeStatus flag is present in json as false to mention |
| 202 | // that the EEPROM is not mentioned in device tree. If this flag |
| 203 | // is absent consider the value to be true, i.e EEPROM is |
| 204 | // mentioned in device tree |
| 205 | if (!singleFru.value("devTreeStatus", true)) |
| 206 | { |
| 207 | auto pos = deviceAddress.find('-'); |
| 208 | if (pos != string::npos) |
| 209 | { |
| 210 | string busNum = deviceAddress.substr(0, pos); |
| 211 | deviceAddress = |
| 212 | "0x" + deviceAddress.substr(pos + 1, string::npos); |
| 213 | |
| 214 | string deleteDevice = str + deviceAddress + " > /sys/bus/" + |
| 215 | busType + "/devices/" + busType + "-" + |
| 216 | busNum + "/delete_device"; |
| 217 | executeCmd(deleteDevice); |
| 218 | |
| 219 | string addDevice = str + driverType + " " + deviceAddress + |
| 220 | " > /sys/bus/" + busType + "/devices/" + |
| 221 | busType + "-" + busNum + "/new_device"; |
| 222 | executeCmd(addDevice); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | log<level::ERR>( |
| 227 | "Wrong format of device address in Json", |
| 228 | entry( |
| 229 | "ERROR=%s", |
| 230 | ("Recollection failed for " + inventoryPath).c_str())); |
| 231 | continue; |
| 232 | } |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | string cmd = str + deviceAddress + " > /sys/bus/" + busType + |
| 237 | "/drivers/" + driverType; |
| 238 | |
| 239 | executeCmd(cmd + "/unbind"); |
| 240 | executeCmd(cmd + "/bind"); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
SunnySrivastava1984 | b59fd09 | 2020-02-03 09:58:56 -0600 | [diff] [blame] | 245 | } // namespace manager |
| 246 | } // namespace vpd |
| 247 | } // namespace openpower |