blob: 4f1bc565dc9273d2f0abc331b7acadc5e6ff4fd9 [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
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800202static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
203
204void Handler::psuResetOnShutdown() const
205{
206 std::ofstream ofs;
207 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
208 if (!ofs.good())
209 {
210 std::fprintf(stderr, "Unable to open file for output.\n");
211 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
212 }
213 ofs.close();
214}
215
Patrick Ventureab650002019-03-16 09:08:47 -0700216std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700217{
Patrick Ventureab650002019-03-16 09:08:47 -0700218 // Check if we support this Entity ID.
219 auto it = _entityIdToName.find(id);
220 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700221 {
Patrick Ventureab650002019-03-16 09:08:47 -0700222 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
223 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
Patrick Venture07f85152019-03-15 21:36:56 -0700224 }
225
Patrick Ventureab650002019-03-16 09:08:47 -0700226 std::string entityName;
227 try
Patrick Venture07f85152019-03-15 21:36:56 -0700228 {
Patrick Ventureab650002019-03-16 09:08:47 -0700229 // Parse the JSON config file.
230 if (!_entityConfigParsed)
231 {
232 _entityConfig = parseConfig(_configFile);
233 _entityConfigParsed = true;
234 }
235
236 // Find the "entity id:entity instance" mapping to entity name.
237 entityName = readNameFromConfig(it->second, instance, _entityConfig);
238 if (entityName.empty())
239 {
240 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
241 }
242 }
243 catch (InternalFailure& e)
244 {
245 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
Patrick Venture07f85152019-03-15 21:36:56 -0700246 }
247
Patrick Ventureab650002019-03-16 09:08:47 -0700248 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700249}
250
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800251std::string Handler::getMachineName()
252{
253 const char* path = "/etc/os-release";
254 std::ifstream ifs(path);
255 if (ifs.fail())
256 {
257 std::fprintf(stderr, "Failed to open: %s\n", path);
258 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
259 }
260
261 std::string line;
262 while (true)
263 {
264 std::getline(ifs, line);
265 if (ifs.eof())
266 {
267 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
268 path);
269 throw IpmiException(IPMI_CC_INVALID);
270 }
271 if (ifs.fail())
272 {
273 std::fprintf(stderr, "Failed to read: %s\n", path);
274 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
275 }
276 std::string_view lineView(line);
277 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
278 if (lineView.substr(0, prefix.size()) != prefix)
279 {
280 continue;
281 }
282 lineView.remove_prefix(prefix.size());
283 lineView.remove_prefix(
284 std::min(lineView.find_first_not_of('"'), lineView.size()));
285 lineView.remove_suffix(
286 lineView.size() - 1 -
287 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
288 return std::string(lineView);
289 }
290}
291
Patrick Ventureab650002019-03-16 09:08:47 -0700292std::string readNameFromConfig(const std::string& type, uint8_t instance,
293 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700294{
295 static const std::vector<Json> empty{};
296 std::vector<Json> readings = config.value(type, empty);
297 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700298
Patrick Venture07f85152019-03-15 21:36:56 -0700299 for (const auto& j : readings)
300 {
301 uint8_t instanceNum = j.value("instance", 0);
302 // Not the instance we're interested in
303 if (instanceNum != instance)
304 {
305 continue;
306 }
307
308 // Found the instance we're interested in
309 name = j.value("name", "");
310
311 break;
312 }
Patrick Ventureab650002019-03-16 09:08:47 -0700313
Patrick Venture07f85152019-03-15 21:36:56 -0700314 return name;
315}
316
Patrick Venture49f23ad2019-03-16 11:59:55 -0700317void Handler::buildI2cPcieMapping()
318{
319 _pcie_i2c_map = buildPcieMap();
320}
321
322size_t Handler::getI2cPcieMappingSize() const
323{
324 return _pcie_i2c_map.size();
325}
326
327std::tuple<std::uint32_t, std::string>
328 Handler::getI2cEntry(unsigned int entry) const
329{
330 return _pcie_i2c_map[entry];
331}
332
Patrick Venturef085d912019-03-15 08:50:00 -0700333} // namespace ipmi
334} // namespace google