blob: 27b6b79363191933eda97084183e478c41505476 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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 Ventureab650002019-03-16 09:08:47 -070015#include "util.hpp"
16
Patrick Williams444b5ea2023-05-19 13:56:42 -050017#include <nlohmann/json.hpp>
18#include <phosphor-logging/elog-errors.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000019#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050020#include <xyz/openbmc_project/Common/error.hpp>
21
Patrick Venture3fb74292019-03-16 11:32:16 -070022#include <cstdint>
Patrick Venture8d5c9ce2019-03-16 11:20:12 -070023#include <cstdio>
Patrick Venture3fb74292019-03-16 11:32:16 -070024#include <filesystem>
Patrick Ventureab650002019-03-16 09:08:47 -070025#include <fstream>
Patrick Venture3fb74292019-03-16 11:32:16 -070026#include <regex>
Patrick Ventureab650002019-03-16 09:08:47 -070027#include <string>
Patrick Venture3fb74292019-03-16 11:32:16 -070028#include <tuple>
29#include <vector>
Patrick Ventureab650002019-03-16 09:08:47 -070030
31namespace google
32{
33namespace ipmi
34{
Patrick Venture3fb74292019-03-16 11:32:16 -070035namespace fs = std::filesystem;
Patrick Ventureab650002019-03-16 09:08:47 -070036using namespace phosphor::logging;
37using InternalFailure =
38 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
39
40nlohmann::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 Venture8d5c9ce2019-03-16 11:20:12 -070059std::string readPropertyFile(const std::string& fileName)
60{
61 std::ifstream ifs(fileName);
62 std::string contents;
63
64 if (!ifs.is_open())
65 {
Yunyun Lin86598512022-02-11 10:41:56 -080066 auto msg = std::string("Unable to open file ") + fileName.c_str();
67 log<level::DEBUG>(msg.c_str());
Patrick Venture8d5c9ce2019-03-16 11:20:12 -070068 }
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 Williams444b5ea2023-05-19 13:56:42 -050076 const char& back = contents.back();
Patrick Venture8d5c9ce2019-03-16 11:20:12 -070077 if (back == '\0')
78 contents.pop_back();
79 }
80
81 return contents;
82 }
83 else
84 {
Michael Shen8d618532023-10-25 09:14:07 +000085 stdplus::print(stderr, "Unable to read file {}.\n", fileName);
Patrick Venture8d5c9ce2019-03-16 11:20:12 -070086 }
87 }
88
89 return "";
90}
91
Patrick Venture3fb74292019-03-16 11:32:16 -070092std::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 Ventureab650002019-03-16 09:08:47 -0700147} // namespace ipmi
148} // namespace google