Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 1 | // Copyright 2021 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 15 | #include "util.hpp" |
| 16 | |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 17 | #include <cstdint> |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 18 | #include <cstdio> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 19 | #include <filesystem> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <nlohmann/json.hpp> |
| 22 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 23 | #include <regex> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 24 | #include <string> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 25 | #include <tuple> |
| 26 | #include <vector> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 27 | #include <xyz/openbmc_project/Common/error.hpp> |
| 28 | |
| 29 | namespace google |
| 30 | { |
| 31 | namespace ipmi |
| 32 | { |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 33 | namespace fs = std::filesystem; |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 34 | using namespace phosphor::logging; |
| 35 | using InternalFailure = |
| 36 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 37 | |
| 38 | nlohmann::json parseConfig(const std::string& file) |
| 39 | { |
| 40 | std::ifstream jsonFile(file); |
| 41 | if (!jsonFile.is_open()) |
| 42 | { |
| 43 | log<level::ERR>("Entity association JSON file not found"); |
| 44 | elog<InternalFailure>(); |
| 45 | } |
| 46 | |
| 47 | auto data = nlohmann::json::parse(jsonFile, nullptr, false); |
| 48 | if (data.is_discarded()) |
| 49 | { |
| 50 | log<level::ERR>("Entity association JSON parser failure"); |
| 51 | elog<InternalFailure>(); |
| 52 | } |
| 53 | |
| 54 | return data; |
| 55 | } |
| 56 | |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 57 | std::string readPropertyFile(const std::string& fileName) |
| 58 | { |
| 59 | std::ifstream ifs(fileName); |
| 60 | std::string contents; |
| 61 | |
| 62 | if (!ifs.is_open()) |
| 63 | { |
Yunyun Lin | 8659851 | 2022-02-11 10:41:56 -0800 | [diff] [blame] | 64 | auto msg = std::string("Unable to open file ") + fileName.c_str(); |
| 65 | log<level::DEBUG>(msg.c_str()); |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
| 69 | if (ifs >> contents) |
| 70 | { |
| 71 | // If the last character is a null terminator; remove it. |
| 72 | if (!contents.empty()) |
| 73 | { |
| 74 | char const& back = contents.back(); |
| 75 | if (back == '\0') |
| 76 | contents.pop_back(); |
| 77 | } |
| 78 | |
| 79 | return contents; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | std::fprintf(stderr, "Unable to read file %s.\n", fileName.c_str()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return ""; |
| 88 | } |
| 89 | |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 90 | std::vector<std::tuple<std::uint32_t, std::string>> buildPcieMap() |
| 91 | { |
| 92 | std::vector<std::tuple<std::uint32_t, std::string>> pcie_i2c_map; |
| 93 | |
| 94 | // Build a vector with i2c bus to pcie slot mapping. |
| 95 | // Iterate through all the devices under "/sys/bus/i2c/devices". |
| 96 | for (const auto& i2c_dev : fs::directory_iterator("/sys/bus/i2c/devices")) |
| 97 | { |
| 98 | std::string i2c_dev_path = i2c_dev.path(); |
| 99 | std::smatch i2c_dev_string_number; |
| 100 | std::regex e("(i2c-)(\\d+)"); |
| 101 | |
| 102 | // Check if the device has "i2c-" in its path. |
| 103 | if (std::regex_search(i2c_dev_path, i2c_dev_string_number, e)) |
| 104 | { |
| 105 | // Check if the i2c device has "pcie-slot" file under "of-node" dir. |
| 106 | std::string pcie_slot_path = i2c_dev_path + "/of_node/pcie-slot"; |
| 107 | std::string pcie_slot; |
| 108 | |
| 109 | // Read the "pcie-slot" name from the "pcie-slot" file. |
| 110 | pcie_slot = readPropertyFile(pcie_slot_path); |
| 111 | if (pcie_slot.empty()) |
| 112 | { |
| 113 | continue; |
| 114 | } |
| 115 | std::string pcie_slot_name; |
| 116 | std::string pcie_slot_full_path; |
| 117 | |
| 118 | // Append the "pcie-slot" name to dts base. |
| 119 | pcie_slot_full_path.append("/proc/device-tree"); |
| 120 | pcie_slot_full_path.append(pcie_slot); |
| 121 | |
| 122 | // Read the "label" which contains the pcie slot name. |
| 123 | pcie_slot_full_path.append("/label"); |
| 124 | pcie_slot_name = readPropertyFile(pcie_slot_full_path); |
| 125 | |
| 126 | if (pcie_slot_name.empty()) |
| 127 | { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | // Get the i2c bus number from the i2c device path. |
| 132 | std::uint32_t i2c_bus_number = |
| 133 | i2c_dev_string_number[2].matched |
| 134 | ? std::stoi(i2c_dev_string_number[2]) |
| 135 | : 0; |
| 136 | // Store the i2c bus number and the pcie slot name in the vector. |
| 137 | pcie_i2c_map.push_back( |
| 138 | std::make_tuple(i2c_bus_number, pcie_slot_name)); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return pcie_i2c_map; |
| 143 | } |
| 144 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 145 | } // namespace ipmi |
| 146 | } // namespace google |