blob: f9a964df455f2ffdd67b74b97795a00fcc31cd85 [file] [log] [blame]
Patrick Venturef085d912019-03-15 08:50:00 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "handler.hpp"
18
Patrick Ventured2037c62019-03-15 10:29:47 -070019#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070020#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070021#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070022
23#include <ipmid/api.h>
24
Patrick Venturebb90d4f2019-03-15 13:42:06 -070025#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070026#include <cstdio>
27#include <filesystem>
28#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070029#include <map>
30#include <nlohmann/json.hpp>
31#include <phosphor-logging/elog-errors.hpp>
Patrick Ventureaa374122019-03-15 15:09:10 -070032#include <phosphor-logging/log.hpp>
33#include <sdbusplus/bus.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070034#include <sstream>
35#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080036#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070037#include <tuple>
Patrick Venture07f85152019-03-15 21:36:56 -070038#include <xyz/openbmc_project/Common/error.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070039
Patrick Venturef085d912019-03-15 08:50:00 -070040// The phosphor-host-ipmi daemon requires a configuration that maps
41// the if_name to the IPMI LAN channel. However, that doesn't strictly
42// define which is meant to be used for NCSI.
43#ifndef NCSI_IPMI_CHANNEL
44#define NCSI_IPMI_CHANNEL 1
45#endif
46
47#ifndef NCSI_IF_NAME
48#define NCSI_IF_NAME eth0
49#endif
50
51// To deal with receiving a string without quotes.
52#define QUOTE(name) #name
53#define STR(macro) QUOTE(macro)
54#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
55
56namespace google
57{
58namespace ipmi
59{
Patrick Ventured2037c62019-03-15 10:29:47 -070060namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070061using Json = nlohmann::json;
62using namespace phosphor::logging;
63using InternalFailure =
64 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070065
66std::tuple<std::uint8_t, std::string> Handler::getEthDetails() const
67{
68 return std::make_tuple(NCSI_IPMI_CHANNEL, NCSI_IF_NAME_STR);
69}
70
Patrick Ventured2037c62019-03-15 10:29:47 -070071std::int64_t Handler::getRxPackets(const std::string& name) const
72{
73 std::ostringstream opath;
74 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
75 std::string path = opath.str();
76
77 // Minor sanity & security check (of course, I'm less certain if unicode
78 // comes into play here.
79 //
80 // Basically you can't easily inject ../ or /../ into the path below.
81 if (name.find("/") != std::string::npos)
82 {
83 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
84 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
85 }
86
87 std::error_code ec;
88 if (!fs::exists(path, ec))
89 {
90 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
91 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
92 }
93 // We're uninterested in the state of ec.
94
95 int64_t count = 0;
96 std::ifstream ifs;
97 ifs.exceptions(std::ifstream::failbit);
98 try
99 {
100 ifs.open(path);
101 ifs >> count;
102 }
103 catch (std::ios_base::failure& fail)
104 {
105 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
106 }
107
108 return count;
109}
110
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700111VersionTuple Handler::getCpldVersion(unsigned int id) const
112{
113 std::ostringstream opath;
114 opath << "/run/cpld" << id << ".version";
115 // Check for file
116
117 std::error_code ec;
118 if (!fs::exists(opath.str(), ec))
119 {
120 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
121 opath.str().c_str());
122 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
123 }
124 // We're uninterested in the state of ec.
125
126 // If file exists, read.
127 std::ifstream ifs;
128 ifs.exceptions(std::ifstream::failbit);
129 std::string value;
130 try
131 {
132 ifs.open(opath.str());
133 ifs >> value;
134 }
135 catch (std::ios_base::failure& fail)
136 {
137 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
138 }
139
140 // If value parses as expected, return version.
141 VersionTuple version = std::make_tuple(0, 0, 0, 0);
142
143 int num_fields =
144 std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
145 &std::get<0>(version), &std::get<1>(version),
146 &std::get<2>(version), &std::get<3>(version));
147 if (num_fields == 0)
148 {
149 std::fprintf(stderr, "Invalid version.\n");
150 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
151 }
152
153 return version;
154}
155
Patrick Ventureaa374122019-03-15 15:09:10 -0700156static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
157static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
158static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
159static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
160static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
161
162void Handler::psuResetDelay(std::uint32_t delay) const
163{
Patrick Ventureaa374122019-03-15 15:09:10 -0700164 std::ofstream ofs;
165 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
166 if (!ofs.good())
167 {
168 std::fprintf(stderr, "Unable to open file for output.\n");
169 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
170 }
171
172 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
173 if (ofs.fail())
174 {
175 std::fprintf(stderr, "Write failed\n");
176 ofs.close();
177 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
178 }
179
180 // Write succeeded, please continue.
181 ofs.flush();
182 ofs.close();
183
184 auto bus = sdbusplus::bus::new_default();
185 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
186 SYSTEMD_INTERFACE, "StartUnit");
187
188 method.append(PSU_HARDRESET_TARGET);
189 method.append("replace");
190
191 try
192 {
193 bus.call_noreply(method);
194 }
195 catch (const sdbusplus::exception::SdBusError& ex)
196 {
197 log<level::ERR>("Failed to call PSU hard reset");
198 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
199 }
200}
201
Patrick Ventureab650002019-03-16 09:08:47 -0700202std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700203{
Patrick Ventureab650002019-03-16 09:08:47 -0700204 // Check if we support this Entity ID.
205 auto it = _entityIdToName.find(id);
206 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700207 {
Patrick Ventureab650002019-03-16 09:08:47 -0700208 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
209 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
Patrick Venture07f85152019-03-15 21:36:56 -0700210 }
211
Patrick Ventureab650002019-03-16 09:08:47 -0700212 std::string entityName;
213 try
Patrick Venture07f85152019-03-15 21:36:56 -0700214 {
Patrick Ventureab650002019-03-16 09:08:47 -0700215 // Parse the JSON config file.
216 if (!_entityConfigParsed)
217 {
218 _entityConfig = parseConfig(_configFile);
219 _entityConfigParsed = true;
220 }
221
222 // Find the "entity id:entity instance" mapping to entity name.
223 entityName = readNameFromConfig(it->second, instance, _entityConfig);
224 if (entityName.empty())
225 {
226 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
227 }
228 }
229 catch (InternalFailure& e)
230 {
231 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
Patrick Venture07f85152019-03-15 21:36:56 -0700232 }
233
Patrick Ventureab650002019-03-16 09:08:47 -0700234 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700235}
236
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800237std::string Handler::getMachineName()
238{
239 const char* path = "/etc/os-release";
240 std::ifstream ifs(path);
241 if (ifs.fail())
242 {
243 std::fprintf(stderr, "Failed to open: %s\n", path);
244 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
245 }
246
247 std::string line;
248 while (true)
249 {
250 std::getline(ifs, line);
251 if (ifs.eof())
252 {
253 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
254 path);
255 throw IpmiException(IPMI_CC_INVALID);
256 }
257 if (ifs.fail())
258 {
259 std::fprintf(stderr, "Failed to read: %s\n", path);
260 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
261 }
262 std::string_view lineView(line);
263 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
264 if (lineView.substr(0, prefix.size()) != prefix)
265 {
266 continue;
267 }
268 lineView.remove_prefix(prefix.size());
269 lineView.remove_prefix(
270 std::min(lineView.find_first_not_of('"'), lineView.size()));
271 lineView.remove_suffix(
272 lineView.size() - 1 -
273 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
274 return std::string(lineView);
275 }
276}
277
Patrick Ventureab650002019-03-16 09:08:47 -0700278std::string readNameFromConfig(const std::string& type, uint8_t instance,
279 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700280{
281 static const std::vector<Json> empty{};
282 std::vector<Json> readings = config.value(type, empty);
283 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700284
Patrick Venture07f85152019-03-15 21:36:56 -0700285 for (const auto& j : readings)
286 {
287 uint8_t instanceNum = j.value("instance", 0);
288 // Not the instance we're interested in
289 if (instanceNum != instance)
290 {
291 continue;
292 }
293
294 // Found the instance we're interested in
295 name = j.value("name", "");
296
297 break;
298 }
Patrick Ventureab650002019-03-16 09:08:47 -0700299
Patrick Venture07f85152019-03-15 21:36:56 -0700300 return name;
301}
302
Patrick Venture49f23ad2019-03-16 11:59:55 -0700303void Handler::buildI2cPcieMapping()
304{
305 _pcie_i2c_map = buildPcieMap();
306}
307
308size_t Handler::getI2cPcieMappingSize() const
309{
310 return _pcie_i2c_map.size();
311}
312
313std::tuple<std::uint32_t, std::string>
314 Handler::getI2cEntry(unsigned int entry) const
315{
316 return _pcie_i2c_map[entry];
317}
318
Patrick Venturef085d912019-03-15 08:50:00 -0700319} // namespace ipmi
320} // namespace google