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 Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 17 | #include <nlohmann/json.hpp> |
| 18 | #include <phosphor-logging/elog-errors.hpp> |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 19 | #include <stdplus/print.hpp> |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 20 | #include <xyz/openbmc_project/Common/error.hpp> |
| 21 | |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 22 | #include <cstdint> |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 23 | #include <cstdio> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 24 | #include <filesystem> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 25 | #include <fstream> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 26 | #include <regex> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 27 | #include <string> |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 28 | #include <tuple> |
| 29 | #include <vector> |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 30 | |
| 31 | namespace google |
| 32 | { |
| 33 | namespace ipmi |
| 34 | { |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 35 | namespace fs = std::filesystem; |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 36 | using namespace phosphor::logging; |
| 37 | using InternalFailure = |
| 38 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 39 | |
| 40 | nlohmann::json parseConfig(const std::string& file) |
| 41 | { |
| 42 | std::ifstream jsonFile(file); |
| 43 | if (!jsonFile.is_open()) |
| 44 | { |
| 45 | log<level::ERR>("Entity association JSON file not found"); |
| 46 | elog<InternalFailure>(); |
| 47 | } |
| 48 | |
| 49 | auto data = nlohmann::json::parse(jsonFile, nullptr, false); |
| 50 | if (data.is_discarded()) |
| 51 | { |
| 52 | log<level::ERR>("Entity association JSON parser failure"); |
| 53 | elog<InternalFailure>(); |
| 54 | } |
| 55 | |
| 56 | return data; |
| 57 | } |
| 58 | |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 59 | std::string readPropertyFile(const std::string& fileName) |
| 60 | { |
| 61 | std::ifstream ifs(fileName); |
| 62 | std::string contents; |
| 63 | |
| 64 | if (!ifs.is_open()) |
| 65 | { |
Yunyun Lin | 8659851 | 2022-02-11 10:41:56 -0800 | [diff] [blame] | 66 | auto msg = std::string("Unable to open file ") + fileName.c_str(); |
| 67 | log<level::DEBUG>(msg.c_str()); |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 68 | } |
| 69 | else |
| 70 | { |
| 71 | if (ifs >> contents) |
| 72 | { |
| 73 | // If the last character is a null terminator; remove it. |
| 74 | if (!contents.empty()) |
| 75 | { |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 76 | const char& back = contents.back(); |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 77 | if (back == '\0') |
| 78 | contents.pop_back(); |
| 79 | } |
| 80 | |
| 81 | return contents; |
| 82 | } |
| 83 | else |
| 84 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 85 | stdplus::print(stderr, "Unable to read file {}.\n", fileName); |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | return ""; |
| 90 | } |
| 91 | |
Patrick Venture | 3fb7429 | 2019-03-16 11:32:16 -0700 | [diff] [blame] | 92 | std::vector<std::tuple<std::uint32_t, std::string>> buildPcieMap() |
| 93 | { |
| 94 | std::vector<std::tuple<std::uint32_t, std::string>> pcie_i2c_map; |
| 95 | |
| 96 | // Build a vector with i2c bus to pcie slot mapping. |
| 97 | // Iterate through all the devices under "/sys/bus/i2c/devices". |
| 98 | for (const auto& i2c_dev : fs::directory_iterator("/sys/bus/i2c/devices")) |
| 99 | { |
| 100 | std::string i2c_dev_path = i2c_dev.path(); |
| 101 | std::smatch i2c_dev_string_number; |
| 102 | std::regex e("(i2c-)(\\d+)"); |
| 103 | |
| 104 | // Check if the device has "i2c-" in its path. |
| 105 | if (std::regex_search(i2c_dev_path, i2c_dev_string_number, e)) |
| 106 | { |
| 107 | // Check if the i2c device has "pcie-slot" file under "of-node" dir. |
| 108 | std::string pcie_slot_path = i2c_dev_path + "/of_node/pcie-slot"; |
| 109 | std::string pcie_slot; |
| 110 | |
| 111 | // Read the "pcie-slot" name from the "pcie-slot" file. |
| 112 | pcie_slot = readPropertyFile(pcie_slot_path); |
| 113 | if (pcie_slot.empty()) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | std::string pcie_slot_name; |
| 118 | std::string pcie_slot_full_path; |
| 119 | |
| 120 | // Append the "pcie-slot" name to dts base. |
| 121 | pcie_slot_full_path.append("/proc/device-tree"); |
| 122 | pcie_slot_full_path.append(pcie_slot); |
| 123 | |
| 124 | // Read the "label" which contains the pcie slot name. |
| 125 | pcie_slot_full_path.append("/label"); |
| 126 | pcie_slot_name = readPropertyFile(pcie_slot_full_path); |
| 127 | |
| 128 | if (pcie_slot_name.empty()) |
| 129 | { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | // Get the i2c bus number from the i2c device path. |
| 134 | std::uint32_t i2c_bus_number = |
| 135 | i2c_dev_string_number[2].matched |
| 136 | ? std::stoi(i2c_dev_string_number[2]) |
| 137 | : 0; |
| 138 | // Store the i2c bus number and the pcie slot name in the vector. |
| 139 | pcie_i2c_map.push_back( |
| 140 | std::make_tuple(i2c_bus_number, pcie_slot_name)); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return pcie_i2c_map; |
| 145 | } |
| 146 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 147 | } // namespace ipmi |
| 148 | } // namespace google |