blob: c716af68c7d8e75ef661e75942da553eeeecf0e2 [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.
Patrick Venturef085d912019-03-15 08:50:00 -070014
15#include "handler.hpp"
16
Patrick Ventured2037c62019-03-15 10:29:47 -070017#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070018#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070019#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070020
Willy Tu3b1b4272021-03-02 17:58:10 -080021#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070022#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080023#include <mtd/mtd-abi.h>
24#include <mtd/mtd-user.h>
25#include <sys/ioctl.h>
26#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070027
Patrick Venturebb90d4f2019-03-15 13:42:06 -070028#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070029#include <cstdio>
30#include <filesystem>
31#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070032#include <map>
33#include <nlohmann/json.hpp>
34#include <phosphor-logging/elog-errors.hpp>
Patrick Ventureaa374122019-03-15 15:09:10 -070035#include <phosphor-logging/log.hpp>
36#include <sdbusplus/bus.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070037#include <sstream>
38#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080039#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070040#include <tuple>
Patrick Venture07f85152019-03-15 21:36:56 -070041#include <xyz/openbmc_project/Common/error.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070042
Patrick Venturef085d912019-03-15 08:50:00 -070043#ifndef NCSI_IF_NAME
44#define NCSI_IF_NAME eth0
45#endif
46
47// To deal with receiving a string without quotes.
48#define QUOTE(name) #name
49#define STR(macro) QUOTE(macro)
50#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
51
William A. Kennington III8d3d46a2021-07-13 12:35:35 -070052namespace ipmi
53{
54std::uint8_t getChannelByName(const std::string& chName);
55}
56
Patrick Venturef085d912019-03-15 08:50:00 -070057namespace google
58{
59namespace ipmi
60{
Patrick Ventured2037c62019-03-15 10:29:47 -070061namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070062using Json = nlohmann::json;
63using namespace phosphor::logging;
64using InternalFailure =
65 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070066
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070067std::tuple<std::uint8_t, std::string>
68 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -070069{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070070 if (intf.empty())
71 {
72 intf = NCSI_IF_NAME_STR;
73 }
74 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -070075}
76
Patrick Ventured2037c62019-03-15 10:29:47 -070077std::int64_t Handler::getRxPackets(const std::string& name) const
78{
79 std::ostringstream opath;
80 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
81 std::string path = opath.str();
82
83 // Minor sanity & security check (of course, I'm less certain if unicode
84 // comes into play here.
85 //
86 // Basically you can't easily inject ../ or /../ into the path below.
87 if (name.find("/") != std::string::npos)
88 {
89 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
90 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
91 }
92
93 std::error_code ec;
94 if (!fs::exists(path, ec))
95 {
96 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
97 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
98 }
99 // We're uninterested in the state of ec.
100
101 int64_t count = 0;
102 std::ifstream ifs;
103 ifs.exceptions(std::ifstream::failbit);
104 try
105 {
106 ifs.open(path);
107 ifs >> count;
108 }
109 catch (std::ios_base::failure& fail)
110 {
111 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
112 }
113
114 return count;
115}
116
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700117VersionTuple Handler::getCpldVersion(unsigned int id) const
118{
119 std::ostringstream opath;
120 opath << "/run/cpld" << id << ".version";
121 // Check for file
122
123 std::error_code ec;
124 if (!fs::exists(opath.str(), ec))
125 {
126 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
127 opath.str().c_str());
128 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
129 }
130 // We're uninterested in the state of ec.
131
132 // If file exists, read.
133 std::ifstream ifs;
134 ifs.exceptions(std::ifstream::failbit);
135 std::string value;
136 try
137 {
138 ifs.open(opath.str());
139 ifs >> value;
140 }
141 catch (std::ios_base::failure& fail)
142 {
143 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
144 }
145
146 // If value parses as expected, return version.
147 VersionTuple version = std::make_tuple(0, 0, 0, 0);
148
149 int num_fields =
150 std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
151 &std::get<0>(version), &std::get<1>(version),
152 &std::get<2>(version), &std::get<3>(version));
153 if (num_fields == 0)
154 {
155 std::fprintf(stderr, "Invalid version.\n");
156 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
157 }
158
159 return version;
160}
161
Patrick Ventureaa374122019-03-15 15:09:10 -0700162static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
163static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
164static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
165static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
166static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
167
168void Handler::psuResetDelay(std::uint32_t delay) const
169{
Patrick Ventureaa374122019-03-15 15:09:10 -0700170 std::ofstream ofs;
171 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
172 if (!ofs.good())
173 {
174 std::fprintf(stderr, "Unable to open file for output.\n");
175 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
176 }
177
178 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
179 if (ofs.fail())
180 {
181 std::fprintf(stderr, "Write failed\n");
182 ofs.close();
183 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
184 }
185
186 // Write succeeded, please continue.
187 ofs.flush();
188 ofs.close();
189
190 auto bus = sdbusplus::bus::new_default();
191 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
192 SYSTEMD_INTERFACE, "StartUnit");
193
194 method.append(PSU_HARDRESET_TARGET);
195 method.append("replace");
196
197 try
198 {
199 bus.call_noreply(method);
200 }
201 catch (const sdbusplus::exception::SdBusError& ex)
202 {
203 log<level::ERR>("Failed to call PSU hard reset");
204 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
205 }
206}
207
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800208static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
209
210void Handler::psuResetOnShutdown() const
211{
212 std::ofstream ofs;
213 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
214 if (!ofs.good())
215 {
216 std::fprintf(stderr, "Unable to open file for output.\n");
217 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
218 }
219 ofs.close();
220}
221
Willy Tu3b1b4272021-03-02 17:58:10 -0800222uint32_t Handler::getFlashSize()
223{
224 mtd_info_t info;
225 int fd = open("/dev/mtd0", O_RDONLY);
226 int err = ioctl(fd, MEMGETINFO, &info);
227 close(fd);
228
229 if (err)
230 {
231 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
232 }
233 return info.size;
234}
235
Patrick Ventureab650002019-03-16 09:08:47 -0700236std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700237{
Patrick Ventureab650002019-03-16 09:08:47 -0700238 // Check if we support this Entity ID.
239 auto it = _entityIdToName.find(id);
240 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700241 {
Patrick Ventureab650002019-03-16 09:08:47 -0700242 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
243 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
Patrick Venture07f85152019-03-15 21:36:56 -0700244 }
245
Patrick Ventureab650002019-03-16 09:08:47 -0700246 std::string entityName;
247 try
Patrick Venture07f85152019-03-15 21:36:56 -0700248 {
Patrick Ventureab650002019-03-16 09:08:47 -0700249 // Parse the JSON config file.
250 if (!_entityConfigParsed)
251 {
252 _entityConfig = parseConfig(_configFile);
253 _entityConfigParsed = true;
254 }
255
256 // Find the "entity id:entity instance" mapping to entity name.
257 entityName = readNameFromConfig(it->second, instance, _entityConfig);
258 if (entityName.empty())
259 {
260 throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST);
261 }
262 }
263 catch (InternalFailure& e)
264 {
265 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
Patrick Venture07f85152019-03-15 21:36:56 -0700266 }
267
Patrick Ventureab650002019-03-16 09:08:47 -0700268 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700269}
270
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800271std::string Handler::getMachineName()
272{
273 const char* path = "/etc/os-release";
274 std::ifstream ifs(path);
275 if (ifs.fail())
276 {
277 std::fprintf(stderr, "Failed to open: %s\n", path);
278 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
279 }
280
281 std::string line;
282 while (true)
283 {
284 std::getline(ifs, line);
285 if (ifs.eof())
286 {
287 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
288 path);
289 throw IpmiException(IPMI_CC_INVALID);
290 }
291 if (ifs.fail())
292 {
293 std::fprintf(stderr, "Failed to read: %s\n", path);
294 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
295 }
296 std::string_view lineView(line);
297 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
298 if (lineView.substr(0, prefix.size()) != prefix)
299 {
300 continue;
301 }
302 lineView.remove_prefix(prefix.size());
303 lineView.remove_prefix(
304 std::min(lineView.find_first_not_of('"'), lineView.size()));
305 lineView.remove_suffix(
306 lineView.size() - 1 -
307 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
308 return std::string(lineView);
309 }
310}
311
linyuny8cfa4c42021-06-16 13:53:08 -0700312static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
313static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
314
315void Handler::hostPowerOffDelay(std::uint32_t delay) const
316{
317 // Set time delay
318 std::ofstream ofs;
319 ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
320 if (!ofs.good())
321 {
322 std::fprintf(stderr, "Unable to open file for output.\n");
323 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
324 }
325
326 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
327 ofs.close();
328 if (ofs.fail())
329 {
330 std::fprintf(stderr, "Write failed\n");
331 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
332 }
333
334 // Write succeeded, please continue.
335 auto bus = sdbusplus::bus::new_default();
336 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
337 SYSTEMD_INTERFACE, "StartUnit");
338
339 method.append(HOST_POWEROFF_TARGET);
340 method.append("replace");
341
342 try
343 {
344 bus.call_noreply(method);
345 }
346 catch (const sdbusplus::exception::SdBusError& ex)
347 {
348 log<level::ERR>("Failed to call Power Off",
349 entry("WHAT=%s", ex.what()));
350 throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
351 }
352}
353
Patrick Ventureab650002019-03-16 09:08:47 -0700354std::string readNameFromConfig(const std::string& type, uint8_t instance,
355 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700356{
357 static const std::vector<Json> empty{};
358 std::vector<Json> readings = config.value(type, empty);
359 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700360
Patrick Venture07f85152019-03-15 21:36:56 -0700361 for (const auto& j : readings)
362 {
363 uint8_t instanceNum = j.value("instance", 0);
364 // Not the instance we're interested in
365 if (instanceNum != instance)
366 {
367 continue;
368 }
369
370 // Found the instance we're interested in
371 name = j.value("name", "");
372
373 break;
374 }
Patrick Ventureab650002019-03-16 09:08:47 -0700375
Patrick Venture07f85152019-03-15 21:36:56 -0700376 return name;
377}
378
Patrick Venture49f23ad2019-03-16 11:59:55 -0700379void Handler::buildI2cPcieMapping()
380{
381 _pcie_i2c_map = buildPcieMap();
382}
383
384size_t Handler::getI2cPcieMappingSize() const
385{
386 return _pcie_i2c_map.size();
387}
388
389std::tuple<std::uint32_t, std::string>
390 Handler::getI2cEntry(unsigned int entry) const
391{
392 return _pcie_i2c_map[entry];
393}
394
Patrick Venturef085d912019-03-15 08:50:00 -0700395} // namespace ipmi
396} // namespace google