blob: 5e65e07811a58289365179501d2d0eeae8c7bcf4 [file] [log] [blame]
Steve Foreman4f0d1de2021-09-20 14:06:32 -07001// Copyright 2022 Google LLC
Willy Tua2056e92021-10-10 13:36:16 -07002//
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.
Michael Shen8d618532023-10-25 09:14:07 +000014
Patrick Venturef085d912019-03-15 08:50:00 -070015#include "handler.hpp"
16
Patrick Williams444b5ea2023-05-19 13:56:42 -050017#include "bm_config.h"
18
Nikhil Namjoshi8ec41062022-10-24 21:07:25 +000019#include "bmc_mode_enum.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070020#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070021#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070022#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070023
Willy Tu3b1b4272021-03-02 17:58:10 -080024#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070025#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080026#include <mtd/mtd-abi.h>
27#include <mtd/mtd-user.h>
28#include <sys/ioctl.h>
29#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070030
Patrick Williams444b5ea2023-05-19 13:56:42 -050031#include <nlohmann/json.hpp>
32#include <phosphor-logging/elog-errors.hpp>
33#include <phosphor-logging/log.hpp>
34#include <sdbusplus/bus.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000035#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050036#include <xyz/openbmc_project/Common/error.hpp>
37
Patrick Venturebb90d4f2019-03-15 13:42:06 -070038#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070039#include <cstdio>
40#include <filesystem>
41#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070042#include <map>
Patrick Ventured2037c62019-03-15 10:29:47 -070043#include <sstream>
44#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080045#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070046#include <tuple>
Steve Foreman4f0d1de2021-09-20 14:06:32 -070047#include <variant>
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000048
Patrick Venturef085d912019-03-15 08:50:00 -070049#ifndef NCSI_IF_NAME
50#define NCSI_IF_NAME eth0
51#endif
52
53// To deal with receiving a string without quotes.
54#define QUOTE(name) #name
55#define STR(macro) QUOTE(macro)
56#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
57
William A. Kennington III8d3d46a2021-07-13 12:35:35 -070058namespace ipmi
59{
60std::uint8_t getChannelByName(const std::string& chName);
61}
62
Patrick Venturef085d912019-03-15 08:50:00 -070063namespace google
64{
65namespace ipmi
66{
Patrick Venture07f85152019-03-15 21:36:56 -070067using Json = nlohmann::json;
68using namespace phosphor::logging;
69using InternalFailure =
70 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070071
Hao Zhou15d4d212023-07-11 20:18:04 +000072uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs)
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000073{
74#if BARE_METAL
75 return static_cast<uint8_t>(BmcMode::BM_MODE);
76#else
Brandon Kim3f3ca032023-03-17 18:49:00 +000077 std::error_code ec;
Hao Zhou4baf41c2023-07-06 22:34:04 +000078
Hao Zhou15d4d212023-07-11 20:18:04 +000079 if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec))
Brandon Kim3f3ca032023-03-17 18:49:00 +000080 {
Michael Shen8d618532023-10-25 09:14:07 +000081 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +000082 stderr,
Michael Shen8d618532023-10-25 09:14:07 +000083 "{} exists so we acked cleaning done and must be in BM mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +000084 bmDriveCleaningDoneAckFlagPath);
Brandon Kim3f3ca032023-03-17 18:49:00 +000085 return static_cast<uint8_t>(BmcMode::BM_MODE);
86 }
87
Hao Zhou15d4d212023-07-11 20:18:04 +000088 if (fs->exists(bmDriveCleaningDoneFlagPath, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +000089 {
Hao Zhou15d4d212023-07-11 20:18:04 +000090 fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath,
Hao Zhou4baf41c2023-07-06 22:34:04 +000091 ec);
Michael Shen8d618532023-10-25 09:14:07 +000092 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +000093 stderr,
Michael Shen8d618532023-10-25 09:14:07 +000094 "{} exists so we just finished cleaning and must be in BM mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +000095 bmDriveCleaningDoneFlagPath);
96 return static_cast<uint8_t>(BmcMode::BM_MODE);
97 }
98
Hao Zhou15d4d212023-07-11 20:18:04 +000099 if (fs->exists(BM_SIGNAL_PATH, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +0000100 {
Hao Zhou15d4d212023-07-11 20:18:04 +0000101 if (!fs->exists(bmDriveCleaningFlagPath, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +0000102 {
Hao Zhou15d4d212023-07-11 20:18:04 +0000103 fs->create(bmDriveCleaningFlagPath);
Hao Zhou4baf41c2023-07-06 22:34:04 +0000104 }
105
Michael Shen8d618532023-10-25 09:14:07 +0000106 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +0000107 stderr,
Michael Shen8d618532023-10-25 09:14:07 +0000108 "{} exists and no done/ack flag, we must be in BM cleaning mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +0000109 BM_SIGNAL_PATH);
110 return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE);
111 }
112
Michael Shen8d618532023-10-25 09:14:07 +0000113 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +0000114 stderr,
115 "Unable to find any BM state files so we must not be in BM mode\n");
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +0000116 return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
117#endif
118}
119
120uint8_t Handler::getBmcMode()
121{
122 // BM_CLEANING_MODE is not implemented yet
Hao Zhou15d4d212023-07-11 20:18:04 +0000123 return isBmcInBareMetalMode(this->getFs());
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +0000124}
125
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700126std::tuple<std::uint8_t, std::string>
127 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -0700128{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700129 if (intf.empty())
130 {
131 intf = NCSI_IF_NAME_STR;
132 }
133 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -0700134}
135
Patrick Ventured2037c62019-03-15 10:29:47 -0700136std::int64_t Handler::getRxPackets(const std::string& name) const
137{
138 std::ostringstream opath;
139 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
140 std::string path = opath.str();
141
142 // Minor sanity & security check (of course, I'm less certain if unicode
143 // comes into play here.
144 //
145 // Basically you can't easily inject ../ or /../ into the path below.
146 if (name.find("/") != std::string::npos)
147 {
Michael Shen8d618532023-10-25 09:14:07 +0000148 stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name);
Michael Shene5a06672022-06-20 05:08:32 +0000149 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700150 }
151
152 std::error_code ec;
Hao Zhou15d4d212023-07-11 20:18:04 +0000153 if (!this->getFs()->exists(path, ec))
Patrick Ventured2037c62019-03-15 10:29:47 -0700154 {
Michael Shen8d618532023-10-25 09:14:07 +0000155 stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000156 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700157 }
158 // We're uninterested in the state of ec.
159
160 int64_t count = 0;
161 std::ifstream ifs;
162 ifs.exceptions(std::ifstream::failbit);
163 try
164 {
165 ifs.open(path);
166 ifs >> count;
167 }
168 catch (std::ios_base::failure& fail)
169 {
Michael Shene5a06672022-06-20 05:08:32 +0000170 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventured2037c62019-03-15 10:29:47 -0700171 }
172
173 return count;
174}
175
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700176VersionTuple Handler::getCpldVersion(unsigned int id) const
177{
178 std::ostringstream opath;
179 opath << "/run/cpld" << id << ".version";
180 // Check for file
181
182 std::error_code ec;
Hao Zhou15d4d212023-07-11 20:18:04 +0000183 if (!this->getFs()->exists(opath.str(), ec))
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700184 {
Michael Shen8d618532023-10-25 09:14:07 +0000185 stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str());
Michael Shene5a06672022-06-20 05:08:32 +0000186 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700187 }
188 // We're uninterested in the state of ec.
189
190 // If file exists, read.
191 std::ifstream ifs;
192 ifs.exceptions(std::ifstream::failbit);
193 std::string value;
194 try
195 {
196 ifs.open(opath.str());
197 ifs >> value;
198 }
199 catch (std::ios_base::failure& fail)
200 {
Michael Shene5a06672022-06-20 05:08:32 +0000201 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700202 }
203
204 // If value parses as expected, return version.
205 VersionTuple version = std::make_tuple(0, 0, 0, 0);
206
Patrick Williams444b5ea2023-05-19 13:56:42 -0500207 int num_fields = std::sscanf(value.c_str(),
208 "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
209 &std::get<0>(version), &std::get<1>(version),
210 &std::get<2>(version), &std::get<3>(version));
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700211 if (num_fields == 0)
212 {
Michael Shen8d618532023-10-25 09:14:07 +0000213 stdplus::print(stderr, "Invalid version.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000214 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700215 }
216
217 return version;
218}
219
Patrick Ventureaa374122019-03-15 15:09:10 -0700220static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
221static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
222static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
223static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
224static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
225
226void Handler::psuResetDelay(std::uint32_t delay) const
227{
Patrick Ventureaa374122019-03-15 15:09:10 -0700228 std::ofstream ofs;
229 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
230 if (!ofs.good())
231 {
Michael Shen8d618532023-10-25 09:14:07 +0000232 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000233 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700234 }
235
236 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
237 if (ofs.fail())
238 {
Michael Shen8d618532023-10-25 09:14:07 +0000239 stdplus::print(stderr, "Write failed\n");
Patrick Ventureaa374122019-03-15 15:09:10 -0700240 ofs.close();
Michael Shene5a06672022-06-20 05:08:32 +0000241 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700242 }
243
244 // Write succeeded, please continue.
245 ofs.flush();
246 ofs.close();
247
248 auto bus = sdbusplus::bus::new_default();
249 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
250 SYSTEMD_INTERFACE, "StartUnit");
251
252 method.append(PSU_HARDRESET_TARGET);
253 method.append("replace");
254
255 try
256 {
257 bus.call_noreply(method);
258 }
259 catch (const sdbusplus::exception::SdBusError& ex)
260 {
261 log<level::ERR>("Failed to call PSU hard reset");
Michael Shene5a06672022-06-20 05:08:32 +0000262 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700263 }
264}
265
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800266static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
267
268void Handler::psuResetOnShutdown() const
269{
270 std::ofstream ofs;
271 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
272 if (!ofs.good())
273 {
Michael Shen8d618532023-10-25 09:14:07 +0000274 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000275 throw IpmiException(::ipmi::ccUnspecifiedError);
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800276 }
277 ofs.close();
278}
279
Willy Tu3b1b4272021-03-02 17:58:10 -0800280uint32_t Handler::getFlashSize()
281{
282 mtd_info_t info;
283 int fd = open("/dev/mtd0", O_RDONLY);
284 int err = ioctl(fd, MEMGETINFO, &info);
285 close(fd);
286
287 if (err)
288 {
Michael Shene5a06672022-06-20 05:08:32 +0000289 throw IpmiException(::ipmi::ccUnspecifiedError);
Willy Tu3b1b4272021-03-02 17:58:10 -0800290 }
291 return info.size;
292}
293
Patrick Ventureab650002019-03-16 09:08:47 -0700294std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700295{
Patrick Ventureab650002019-03-16 09:08:47 -0700296 // Check if we support this Entity ID.
297 auto it = _entityIdToName.find(id);
298 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700299 {
Patrick Ventureab650002019-03-16 09:08:47 -0700300 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
Michael Shene5a06672022-06-20 05:08:32 +0000301 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venture07f85152019-03-15 21:36:56 -0700302 }
303
Patrick Ventureab650002019-03-16 09:08:47 -0700304 std::string entityName;
305 try
Patrick Venture07f85152019-03-15 21:36:56 -0700306 {
Patrick Ventureab650002019-03-16 09:08:47 -0700307 // Parse the JSON config file.
308 if (!_entityConfigParsed)
309 {
310 _entityConfig = parseConfig(_configFile);
311 _entityConfigParsed = true;
312 }
313
314 // Find the "entity id:entity instance" mapping to entity name.
315 entityName = readNameFromConfig(it->second, instance, _entityConfig);
316 if (entityName.empty())
317 {
Michael Shene5a06672022-06-20 05:08:32 +0000318 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventureab650002019-03-16 09:08:47 -0700319 }
320 }
321 catch (InternalFailure& e)
322 {
Michael Shene5a06672022-06-20 05:08:32 +0000323 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venture07f85152019-03-15 21:36:56 -0700324 }
325
Patrick Ventureab650002019-03-16 09:08:47 -0700326 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700327}
328
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800329std::string Handler::getMachineName()
330{
331 const char* path = "/etc/os-release";
332 std::ifstream ifs(path);
333 if (ifs.fail())
334 {
Michael Shen8d618532023-10-25 09:14:07 +0000335 stdplus::print(stderr, "Failed to open: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000336 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800337 }
338
339 std::string line;
340 while (true)
341 {
342 std::getline(ifs, line);
343 if (ifs.eof())
344 {
Michael Shen8d618532023-10-25 09:14:07 +0000345 stdplus::print(stderr,
346 "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000347 throw IpmiException(::ipmi::ccInvalidCommand);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800348 }
349 if (ifs.fail())
350 {
Michael Shen8d618532023-10-25 09:14:07 +0000351 stdplus::print(stderr, "Failed to read: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000352 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800353 }
354 std::string_view lineView(line);
355 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
356 if (lineView.substr(0, prefix.size()) != prefix)
357 {
358 continue;
359 }
360 lineView.remove_prefix(prefix.size());
361 lineView.remove_prefix(
362 std::min(lineView.find_first_not_of('"'), lineView.size()));
363 lineView.remove_suffix(
364 lineView.size() - 1 -
365 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
366 return std::string(lineView);
367 }
368}
369
linyuny8cfa4c42021-06-16 13:53:08 -0700370static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
371static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
372
373void Handler::hostPowerOffDelay(std::uint32_t delay) const
374{
375 // Set time delay
376 std::ofstream ofs;
377 ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
378 if (!ofs.good())
379 {
Michael Shen8d618532023-10-25 09:14:07 +0000380 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000381 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700382 }
383
384 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
385 ofs.close();
386 if (ofs.fail())
387 {
Michael Shen8d618532023-10-25 09:14:07 +0000388 stdplus::print(stderr, "Write failed\n");
Michael Shene5a06672022-06-20 05:08:32 +0000389 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700390 }
391
392 // Write succeeded, please continue.
393 auto bus = sdbusplus::bus::new_default();
394 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
395 SYSTEMD_INTERFACE, "StartUnit");
396
397 method.append(HOST_POWEROFF_TARGET);
398 method.append("replace");
399
400 try
401 {
402 bus.call_noreply(method);
403 }
404 catch (const sdbusplus::exception::SdBusError& ex)
405 {
406 log<level::ERR>("Failed to call Power Off",
407 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000408 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700409 }
410}
411
Patrick Ventureab650002019-03-16 09:08:47 -0700412std::string readNameFromConfig(const std::string& type, uint8_t instance,
413 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700414{
415 static const std::vector<Json> empty{};
416 std::vector<Json> readings = config.value(type, empty);
417 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700418
Patrick Venture07f85152019-03-15 21:36:56 -0700419 for (const auto& j : readings)
420 {
421 uint8_t instanceNum = j.value("instance", 0);
422 // Not the instance we're interested in
423 if (instanceNum != instance)
424 {
425 continue;
426 }
427
428 // Found the instance we're interested in
429 name = j.value("name", "");
430
431 break;
432 }
Patrick Ventureab650002019-03-16 09:08:47 -0700433
Patrick Venture07f85152019-03-15 21:36:56 -0700434 return name;
435}
436
Patrick Venture49f23ad2019-03-16 11:59:55 -0700437void Handler::buildI2cPcieMapping()
438{
439 _pcie_i2c_map = buildPcieMap();
440}
441
442size_t Handler::getI2cPcieMappingSize() const
443{
444 return _pcie_i2c_map.size();
445}
446
447std::tuple<std::uint32_t, std::string>
448 Handler::getI2cEntry(unsigned int entry) const
449{
450 return _pcie_i2c_map[entry];
451}
452
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700453namespace
454{
455
456static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
457static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
458static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
459
460// C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
461using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
462using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
463using NamedArrayOfAnyTypeLists =
464 std::vector<std::pair<std::string, AnyTypeList>>;
465using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
466 std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
467
468} // namespace
469
Patrick Williams65371222023-05-19 02:31:40 -0500470sdbusplus::bus_t Handler::getDbus() const
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700471{
472 return sdbusplus::bus::new_default();
473}
474
Hao Zhou15d4d212023-07-11 20:18:04 +0000475const std::unique_ptr<FileSystemInterface>& Handler::getFs() const
476{
477 return this->fsPtr;
478}
479
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700480uint32_t Handler::accelOobDeviceCount() const
481{
482 ArrayOfObjectPathsAndTieredAnyTypeLists data;
483
484 try
485 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000486 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700487 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
488 "org.freedesktop.DBus.ObjectManager",
489 "GetManagedObjects");
490 bus.call(method).read(data);
491 }
492 catch (const sdbusplus::exception::SdBusError& ex)
493 {
494 log<level::ERR>(
495 "Failed to call GetManagedObjects on com.google.custom_accel",
496 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000497 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700498 }
499
500 return data.size();
501}
502
503std::string Handler::accelOobDeviceName(size_t index) const
504{
505 ArrayOfObjectPathsAndTieredAnyTypeLists data;
506
507 try
508 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000509 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700510 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
511 "org.freedesktop.DBus.ObjectManager",
512 "GetManagedObjects");
513 bus.call(method).read(data);
514 }
515 catch (const sdbusplus::exception::SdBusError& ex)
516 {
517 log<level::ERR>(
518 "Failed to call GetManagedObjects on com.google.custom_accel",
519 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000520 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700521 }
522
523 if (index >= data.size())
524 {
525 log<level::WARNING>(
526 "Requested index is larger than the number of entries.",
527 entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000528 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700529 }
530
531 std::string_view name(data[index].first.str);
532 if (!name.starts_with(ACCEL_OOB_ROOT))
533 {
Michael Shene5a06672022-06-20 05:08:32 +0000534 throw IpmiException(::ipmi::ccInvalidCommand);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700535 }
536 name.remove_prefix(ACCEL_OOB_ROOT.length());
537 return std::string(name);
538}
539
540uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
541 uint8_t num_bytes) const
542{
543 static constexpr char ACCEL_OOB_METHOD[] = "Read";
544
545 std::string object_name(ACCEL_OOB_ROOT);
546 object_name.append(name);
547
Michael Shen0e928ac2022-06-20 05:21:52 +0000548 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700549 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
550 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
551 method.append(address, static_cast<uint64_t>(num_bytes));
552
553 std::vector<uint8_t> bytes;
554
555 try
556 {
557 bus.call(method).read(bytes);
558 }
559 catch (const sdbusplus::exception::SdBusError& ex)
560 {
561 log<level::ERR>("Failed to call Read on com.google.custom_accel",
562 entry("WHAT=%s", ex.what()),
563 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
564 entry("DBUS_OBJECT=%s", object_name.c_str()),
565 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
566 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
567 entry("DBUS_ARG_ADDRESS=%016llx", address),
568 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
Michael Shene5a06672022-06-20 05:08:32 +0000569 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700570 }
571
572 if (bytes.size() < num_bytes)
573 {
574 log<level::ERR>(
575 "Call to Read on com.google.custom_accel didn't return the expected"
576 " number of bytes.",
577 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
578 entry("DBUS_OBJECT=%s", object_name.c_str()),
579 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
580 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
581 entry("DBUS_ARG_ADDRESS=%016llx", address),
582 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
583 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000584 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700585 }
586
587 if (bytes.size() > sizeof(uint64_t))
588 {
589 log<level::ERR>(
590 "Call to Read on com.google.custom_accel returned more than 8B.",
591 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
592 entry("DBUS_OBJECT=%s", object_name.c_str()),
593 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
594 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
595 entry("DBUS_ARG_ADDRESS=%016llx", address),
596 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
597 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000598 throw IpmiException(::ipmi::ccReqDataTruncated);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700599 }
600
601 uint64_t data = 0;
602 for (size_t i = 0; i < num_bytes; ++i)
603 {
604 data = (data << 8) | bytes[i];
605 }
606
607 return data;
608}
609
610void Handler::accelOobWrite(std::string_view name, uint64_t address,
611 uint8_t num_bytes, uint64_t data) const
612{
613 static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
614
615 std::string object_name(ACCEL_OOB_ROOT);
616 object_name.append(name);
617
618 if (num_bytes > sizeof(data))
619 {
620 log<level::ERR>(
621 "Call to Write on com.google.custom_accel requested more than 8B.",
622 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
623 entry("DBUS_OBJECT=%s", object_name.c_str()),
624 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
625 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
626 entry("DBUS_ARG_ADDRESS=%016llx", address),
627 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
628 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000629 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700630 }
631
632 std::vector<uint8_t> bytes;
633 bytes.reserve(num_bytes);
634 for (size_t i = 0; i < num_bytes; ++i)
635 {
636 bytes.emplace_back(data & 0xff);
637 data >>= 8;
638 }
639
640 try
641 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000642 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700643 auto method =
644 bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
645 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
646 method.append(address, bytes);
647 bus.call_noreply(method);
648 }
649 catch (const sdbusplus::exception::SdBusError& ex)
650 {
651 log<level::ERR>("Failed to call Write on com.google.custom_accel",
652 entry("WHAT=%s", ex.what()),
653 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
654 entry("DBUS_OBJECT=%s", object_name.c_str()),
655 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
656 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
657 entry("DBUS_ARG_ADDRESS=%016llx", address),
658 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
659 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000660 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700661 }
662}
663
Willy Tu6c71b0f2021-10-10 13:34:41 -0700664std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
665{
666 return bifurcationHelper.get().getBifurcation(index).value_or(
667 std::vector<uint8_t>{});
668}
669
John Wediga92d0e62023-06-29 10:43:47 -0700670static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target";
671
672void Handler::linuxBootDone() const
673{
Hao Zhou15d4d212023-07-11 20:18:04 +0000674 if (isBmcInBareMetalMode(this->fsPtr) !=
675 static_cast<uint8_t>(BmcMode::BM_MODE))
John Wediga92d0e62023-06-29 10:43:47 -0700676 {
677 return;
678 }
679
680 log<level::INFO>("LinuxBootDone: Disabling IPMI");
681
682 // Start the bare metal active systemd target.
683 auto bus = sdbusplus::bus::new_default();
684 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
685 SYSTEMD_INTERFACE, "StartUnit");
686
687 method.append(BARE_METAL_TARGET);
688 method.append("replace");
689
690 try
691 {
692 bus.call_noreply(method);
693 }
694 catch (const sdbusplus::exception::SdBusError& ex)
695 {
696 log<level::ERR>("Failed to start bare metal active systemd target",
697 entry("WHAT=%s", ex.what()));
698 throw IpmiException(::ipmi::ccUnspecifiedError);
699 }
700}
701
Patrick Venturef085d912019-03-15 08:50:00 -0700702} // namespace ipmi
703} // namespace google