blob: 9032055fd7e8564e71ac49bf09c5540617500fa1 [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.
Patrick Venturef085d912019-03-15 08:50:00 -070014#include "handler.hpp"
15
Patrick Williams444b5ea2023-05-19 13:56:42 -050016#include "bm_config.h"
17
Nikhil Namjoshi8ec41062022-10-24 21:07:25 +000018#include "bmc_mode_enum.hpp"
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
Willy Tu3b1b4272021-03-02 17:58:10 -080023#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070024#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080025#include <mtd/mtd-abi.h>
26#include <mtd/mtd-user.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070029
Patrick Williams444b5ea2023-05-19 13:56:42 -050030#include <nlohmann/json.hpp>
31#include <phosphor-logging/elog-errors.hpp>
32#include <phosphor-logging/log.hpp>
33#include <sdbusplus/bus.hpp>
34#include <xyz/openbmc_project/Common/error.hpp>
35
Patrick Venturebb90d4f2019-03-15 13:42:06 -070036#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070037#include <cstdio>
38#include <filesystem>
39#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070040#include <map>
Patrick Ventured2037c62019-03-15 10:29:47 -070041#include <sstream>
42#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080043#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070044#include <tuple>
Steve Foreman4f0d1de2021-09-20 14:06:32 -070045#include <variant>
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000046
Patrick Venturef085d912019-03-15 08:50:00 -070047#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
William A. Kennington III8d3d46a2021-07-13 12:35:35 -070056namespace ipmi
57{
58std::uint8_t getChannelByName(const std::string& chName);
59}
60
Patrick Venturef085d912019-03-15 08:50:00 -070061namespace google
62{
63namespace ipmi
64{
Patrick Ventured2037c62019-03-15 10:29:47 -070065namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070066using Json = nlohmann::json;
67using namespace phosphor::logging;
68using InternalFailure =
69 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070070
Hao Zhou4baf41c2023-07-06 22:34:04 +000071static constexpr auto bmDriveCleaningFlagPath = "/run/bm-drive-cleaning.flag";
72static constexpr auto bmDriveCleaningDoneFlagPath =
73 "/run/bm-drive-cleaning-done.flag";
74static constexpr auto bmDriveCleaningDoneAckFlagPath =
75 "/run/bm-drive-cleaning-done-ack.flag";
76
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000077uint8_t isBmcInBareMetalMode()
78{
79#if BARE_METAL
80 return static_cast<uint8_t>(BmcMode::BM_MODE);
81#else
Brandon Kim3f3ca032023-03-17 18:49:00 +000082 std::error_code ec;
Hao Zhou4baf41c2023-07-06 22:34:04 +000083
84 if (fs::exists(bmDriveCleaningDoneAckFlagPath, ec))
Brandon Kim3f3ca032023-03-17 18:49:00 +000085 {
Hao Zhou4baf41c2023-07-06 22:34:04 +000086 std::fprintf(
87 stderr,
88 "%s exists so we acked cleaning done and must be in BM mode\n",
89 bmDriveCleaningDoneAckFlagPath);
Brandon Kim3f3ca032023-03-17 18:49:00 +000090 return static_cast<uint8_t>(BmcMode::BM_MODE);
91 }
92
Hao Zhou4baf41c2023-07-06 22:34:04 +000093 if (fs::exists(bmDriveCleaningDoneFlagPath, ec))
94 {
95 fs::rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath,
96 ec);
97 std::fprintf(
98 stderr,
99 "%s exists so we just finished cleaning and must be in BM mode\n",
100 bmDriveCleaningDoneFlagPath);
101 return static_cast<uint8_t>(BmcMode::BM_MODE);
102 }
103
104 if (fs::exists(BM_SIGNAL_PATH, ec))
105 {
106 if (!fs::exists(bmDriveCleaningFlagPath, ec))
107 {
108 std::ofstream ofs;
109 ofs.open(bmDriveCleaningFlagPath, std::ofstream::out);
110 ofs.close();
111 }
112
113 std::fprintf(
114 stderr,
115 "%s exists and no done/ack flag, we must be in BM cleaning mode\n",
116 BM_SIGNAL_PATH);
117 return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE);
118 }
119
120 std::fprintf(
121 stderr,
122 "Unable to find any BM state files so we must not be in BM mode\n");
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +0000123 return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
124#endif
125}
126
127uint8_t Handler::getBmcMode()
128{
129 // BM_CLEANING_MODE is not implemented yet
130 return isBmcInBareMetalMode();
131}
132
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700133std::tuple<std::uint8_t, std::string>
134 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -0700135{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700136 if (intf.empty())
137 {
138 intf = NCSI_IF_NAME_STR;
139 }
140 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -0700141}
142
Patrick Ventured2037c62019-03-15 10:29:47 -0700143std::int64_t Handler::getRxPackets(const std::string& name) const
144{
145 std::ostringstream opath;
146 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
147 std::string path = opath.str();
148
149 // Minor sanity & security check (of course, I'm less certain if unicode
150 // comes into play here.
151 //
152 // Basically you can't easily inject ../ or /../ into the path below.
153 if (name.find("/") != std::string::npos)
154 {
155 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000156 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700157 }
158
159 std::error_code ec;
160 if (!fs::exists(path, ec))
161 {
162 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000163 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700164 }
165 // We're uninterested in the state of ec.
166
167 int64_t count = 0;
168 std::ifstream ifs;
169 ifs.exceptions(std::ifstream::failbit);
170 try
171 {
172 ifs.open(path);
173 ifs >> count;
174 }
175 catch (std::ios_base::failure& fail)
176 {
Michael Shene5a06672022-06-20 05:08:32 +0000177 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventured2037c62019-03-15 10:29:47 -0700178 }
179
180 return count;
181}
182
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700183VersionTuple Handler::getCpldVersion(unsigned int id) const
184{
185 std::ostringstream opath;
186 opath << "/run/cpld" << id << ".version";
187 // Check for file
188
189 std::error_code ec;
190 if (!fs::exists(opath.str(), ec))
191 {
192 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
193 opath.str().c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000194 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700195 }
196 // We're uninterested in the state of ec.
197
198 // If file exists, read.
199 std::ifstream ifs;
200 ifs.exceptions(std::ifstream::failbit);
201 std::string value;
202 try
203 {
204 ifs.open(opath.str());
205 ifs >> value;
206 }
207 catch (std::ios_base::failure& fail)
208 {
Michael Shene5a06672022-06-20 05:08:32 +0000209 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700210 }
211
212 // If value parses as expected, return version.
213 VersionTuple version = std::make_tuple(0, 0, 0, 0);
214
Patrick Williams444b5ea2023-05-19 13:56:42 -0500215 int num_fields = std::sscanf(value.c_str(),
216 "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
217 &std::get<0>(version), &std::get<1>(version),
218 &std::get<2>(version), &std::get<3>(version));
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700219 if (num_fields == 0)
220 {
221 std::fprintf(stderr, "Invalid version.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000222 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700223 }
224
225 return version;
226}
227
Patrick Ventureaa374122019-03-15 15:09:10 -0700228static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
229static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
230static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
231static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
232static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
233
234void Handler::psuResetDelay(std::uint32_t delay) const
235{
Patrick Ventureaa374122019-03-15 15:09:10 -0700236 std::ofstream ofs;
237 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
238 if (!ofs.good())
239 {
240 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000241 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700242 }
243
244 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
245 if (ofs.fail())
246 {
247 std::fprintf(stderr, "Write failed\n");
248 ofs.close();
Michael Shene5a06672022-06-20 05:08:32 +0000249 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700250 }
251
252 // Write succeeded, please continue.
253 ofs.flush();
254 ofs.close();
255
256 auto bus = sdbusplus::bus::new_default();
257 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
258 SYSTEMD_INTERFACE, "StartUnit");
259
260 method.append(PSU_HARDRESET_TARGET);
261 method.append("replace");
262
263 try
264 {
265 bus.call_noreply(method);
266 }
267 catch (const sdbusplus::exception::SdBusError& ex)
268 {
269 log<level::ERR>("Failed to call PSU hard reset");
Michael Shene5a06672022-06-20 05:08:32 +0000270 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700271 }
272}
273
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800274static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
275
276void Handler::psuResetOnShutdown() const
277{
278 std::ofstream ofs;
279 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
280 if (!ofs.good())
281 {
282 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000283 throw IpmiException(::ipmi::ccUnspecifiedError);
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800284 }
285 ofs.close();
286}
287
Willy Tu3b1b4272021-03-02 17:58:10 -0800288uint32_t Handler::getFlashSize()
289{
290 mtd_info_t info;
291 int fd = open("/dev/mtd0", O_RDONLY);
292 int err = ioctl(fd, MEMGETINFO, &info);
293 close(fd);
294
295 if (err)
296 {
Michael Shene5a06672022-06-20 05:08:32 +0000297 throw IpmiException(::ipmi::ccUnspecifiedError);
Willy Tu3b1b4272021-03-02 17:58:10 -0800298 }
299 return info.size;
300}
301
Patrick Ventureab650002019-03-16 09:08:47 -0700302std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700303{
Patrick Ventureab650002019-03-16 09:08:47 -0700304 // Check if we support this Entity ID.
305 auto it = _entityIdToName.find(id);
306 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700307 {
Patrick Ventureab650002019-03-16 09:08:47 -0700308 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
Michael Shene5a06672022-06-20 05:08:32 +0000309 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venture07f85152019-03-15 21:36:56 -0700310 }
311
Patrick Ventureab650002019-03-16 09:08:47 -0700312 std::string entityName;
313 try
Patrick Venture07f85152019-03-15 21:36:56 -0700314 {
Patrick Ventureab650002019-03-16 09:08:47 -0700315 // Parse the JSON config file.
316 if (!_entityConfigParsed)
317 {
318 _entityConfig = parseConfig(_configFile);
319 _entityConfigParsed = true;
320 }
321
322 // Find the "entity id:entity instance" mapping to entity name.
323 entityName = readNameFromConfig(it->second, instance, _entityConfig);
324 if (entityName.empty())
325 {
Michael Shene5a06672022-06-20 05:08:32 +0000326 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventureab650002019-03-16 09:08:47 -0700327 }
328 }
329 catch (InternalFailure& e)
330 {
Michael Shene5a06672022-06-20 05:08:32 +0000331 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venture07f85152019-03-15 21:36:56 -0700332 }
333
Patrick Ventureab650002019-03-16 09:08:47 -0700334 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700335}
336
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800337std::string Handler::getMachineName()
338{
339 const char* path = "/etc/os-release";
340 std::ifstream ifs(path);
341 if (ifs.fail())
342 {
343 std::fprintf(stderr, "Failed to open: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000344 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800345 }
346
347 std::string line;
348 while (true)
349 {
350 std::getline(ifs, line);
351 if (ifs.eof())
352 {
353 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
354 path);
Michael Shene5a06672022-06-20 05:08:32 +0000355 throw IpmiException(::ipmi::ccInvalidCommand);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800356 }
357 if (ifs.fail())
358 {
359 std::fprintf(stderr, "Failed to read: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000360 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800361 }
362 std::string_view lineView(line);
363 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
364 if (lineView.substr(0, prefix.size()) != prefix)
365 {
366 continue;
367 }
368 lineView.remove_prefix(prefix.size());
369 lineView.remove_prefix(
370 std::min(lineView.find_first_not_of('"'), lineView.size()));
371 lineView.remove_suffix(
372 lineView.size() - 1 -
373 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
374 return std::string(lineView);
375 }
376}
377
linyuny8cfa4c42021-06-16 13:53:08 -0700378static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
379static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
380
381void Handler::hostPowerOffDelay(std::uint32_t delay) const
382{
383 // Set time delay
384 std::ofstream ofs;
385 ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
386 if (!ofs.good())
387 {
388 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000389 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700390 }
391
392 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
393 ofs.close();
394 if (ofs.fail())
395 {
396 std::fprintf(stderr, "Write failed\n");
Michael Shene5a06672022-06-20 05:08:32 +0000397 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700398 }
399
400 // Write succeeded, please continue.
401 auto bus = sdbusplus::bus::new_default();
402 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
403 SYSTEMD_INTERFACE, "StartUnit");
404
405 method.append(HOST_POWEROFF_TARGET);
406 method.append("replace");
407
408 try
409 {
410 bus.call_noreply(method);
411 }
412 catch (const sdbusplus::exception::SdBusError& ex)
413 {
414 log<level::ERR>("Failed to call Power Off",
415 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000416 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700417 }
418}
419
Patrick Ventureab650002019-03-16 09:08:47 -0700420std::string readNameFromConfig(const std::string& type, uint8_t instance,
421 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700422{
423 static const std::vector<Json> empty{};
424 std::vector<Json> readings = config.value(type, empty);
425 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700426
Patrick Venture07f85152019-03-15 21:36:56 -0700427 for (const auto& j : readings)
428 {
429 uint8_t instanceNum = j.value("instance", 0);
430 // Not the instance we're interested in
431 if (instanceNum != instance)
432 {
433 continue;
434 }
435
436 // Found the instance we're interested in
437 name = j.value("name", "");
438
439 break;
440 }
Patrick Ventureab650002019-03-16 09:08:47 -0700441
Patrick Venture07f85152019-03-15 21:36:56 -0700442 return name;
443}
444
Patrick Venture49f23ad2019-03-16 11:59:55 -0700445void Handler::buildI2cPcieMapping()
446{
447 _pcie_i2c_map = buildPcieMap();
448}
449
450size_t Handler::getI2cPcieMappingSize() const
451{
452 return _pcie_i2c_map.size();
453}
454
455std::tuple<std::uint32_t, std::string>
456 Handler::getI2cEntry(unsigned int entry) const
457{
458 return _pcie_i2c_map[entry];
459}
460
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700461namespace
462{
463
464static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
465static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
466static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
467
468// C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
469using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
470using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
471using NamedArrayOfAnyTypeLists =
472 std::vector<std::pair<std::string, AnyTypeList>>;
473using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
474 std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
475
476} // namespace
477
Patrick Williams65371222023-05-19 02:31:40 -0500478sdbusplus::bus_t Handler::getDbus() const
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700479{
480 return sdbusplus::bus::new_default();
481}
482
483uint32_t Handler::accelOobDeviceCount() const
484{
485 ArrayOfObjectPathsAndTieredAnyTypeLists data;
486
487 try
488 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000489 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700490 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
491 "org.freedesktop.DBus.ObjectManager",
492 "GetManagedObjects");
493 bus.call(method).read(data);
494 }
495 catch (const sdbusplus::exception::SdBusError& ex)
496 {
497 log<level::ERR>(
498 "Failed to call GetManagedObjects on com.google.custom_accel",
499 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000500 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700501 }
502
503 return data.size();
504}
505
506std::string Handler::accelOobDeviceName(size_t index) const
507{
508 ArrayOfObjectPathsAndTieredAnyTypeLists data;
509
510 try
511 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000512 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700513 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
514 "org.freedesktop.DBus.ObjectManager",
515 "GetManagedObjects");
516 bus.call(method).read(data);
517 }
518 catch (const sdbusplus::exception::SdBusError& ex)
519 {
520 log<level::ERR>(
521 "Failed to call GetManagedObjects on com.google.custom_accel",
522 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000523 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700524 }
525
526 if (index >= data.size())
527 {
528 log<level::WARNING>(
529 "Requested index is larger than the number of entries.",
530 entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000531 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700532 }
533
534 std::string_view name(data[index].first.str);
535 if (!name.starts_with(ACCEL_OOB_ROOT))
536 {
Michael Shene5a06672022-06-20 05:08:32 +0000537 throw IpmiException(::ipmi::ccInvalidCommand);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700538 }
539 name.remove_prefix(ACCEL_OOB_ROOT.length());
540 return std::string(name);
541}
542
543uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
544 uint8_t num_bytes) const
545{
546 static constexpr char ACCEL_OOB_METHOD[] = "Read";
547
548 std::string object_name(ACCEL_OOB_ROOT);
549 object_name.append(name);
550
Michael Shen0e928ac2022-06-20 05:21:52 +0000551 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700552 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
553 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
554 method.append(address, static_cast<uint64_t>(num_bytes));
555
556 std::vector<uint8_t> bytes;
557
558 try
559 {
560 bus.call(method).read(bytes);
561 }
562 catch (const sdbusplus::exception::SdBusError& ex)
563 {
564 log<level::ERR>("Failed to call Read on com.google.custom_accel",
565 entry("WHAT=%s", ex.what()),
566 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
567 entry("DBUS_OBJECT=%s", object_name.c_str()),
568 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
569 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
570 entry("DBUS_ARG_ADDRESS=%016llx", address),
571 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
Michael Shene5a06672022-06-20 05:08:32 +0000572 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700573 }
574
575 if (bytes.size() < num_bytes)
576 {
577 log<level::ERR>(
578 "Call to Read on com.google.custom_accel didn't return the expected"
579 " number of bytes.",
580 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
581 entry("DBUS_OBJECT=%s", object_name.c_str()),
582 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
583 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
584 entry("DBUS_ARG_ADDRESS=%016llx", address),
585 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
586 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000587 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700588 }
589
590 if (bytes.size() > sizeof(uint64_t))
591 {
592 log<level::ERR>(
593 "Call to Read on com.google.custom_accel returned more than 8B.",
594 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
595 entry("DBUS_OBJECT=%s", object_name.c_str()),
596 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
597 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
598 entry("DBUS_ARG_ADDRESS=%016llx", address),
599 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
600 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000601 throw IpmiException(::ipmi::ccReqDataTruncated);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700602 }
603
604 uint64_t data = 0;
605 for (size_t i = 0; i < num_bytes; ++i)
606 {
607 data = (data << 8) | bytes[i];
608 }
609
610 return data;
611}
612
613void Handler::accelOobWrite(std::string_view name, uint64_t address,
614 uint8_t num_bytes, uint64_t data) const
615{
616 static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
617
618 std::string object_name(ACCEL_OOB_ROOT);
619 object_name.append(name);
620
621 if (num_bytes > sizeof(data))
622 {
623 log<level::ERR>(
624 "Call to Write on com.google.custom_accel requested more than 8B.",
625 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
626 entry("DBUS_OBJECT=%s", object_name.c_str()),
627 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
628 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
629 entry("DBUS_ARG_ADDRESS=%016llx", address),
630 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
631 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000632 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700633 }
634
635 std::vector<uint8_t> bytes;
636 bytes.reserve(num_bytes);
637 for (size_t i = 0; i < num_bytes; ++i)
638 {
639 bytes.emplace_back(data & 0xff);
640 data >>= 8;
641 }
642
643 try
644 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000645 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700646 auto method =
647 bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
648 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
649 method.append(address, bytes);
650 bus.call_noreply(method);
651 }
652 catch (const sdbusplus::exception::SdBusError& ex)
653 {
654 log<level::ERR>("Failed to call Write on com.google.custom_accel",
655 entry("WHAT=%s", ex.what()),
656 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
657 entry("DBUS_OBJECT=%s", object_name.c_str()),
658 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
659 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
660 entry("DBUS_ARG_ADDRESS=%016llx", address),
661 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
662 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000663 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700664 }
665}
666
Willy Tu6c71b0f2021-10-10 13:34:41 -0700667std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
668{
669 return bifurcationHelper.get().getBifurcation(index).value_or(
670 std::vector<uint8_t>{});
671}
672
John Wediga92d0e62023-06-29 10:43:47 -0700673static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target";
674
675void Handler::linuxBootDone() const
676{
677 if (isBmcInBareMetalMode() != static_cast<uint8_t>(BmcMode::BM_MODE))
678 {
679 return;
680 }
681
682 log<level::INFO>("LinuxBootDone: Disabling IPMI");
683
684 // Start the bare metal active systemd target.
685 auto bus = sdbusplus::bus::new_default();
686 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
687 SYSTEMD_INTERFACE, "StartUnit");
688
689 method.append(BARE_METAL_TARGET);
690 method.append("replace");
691
692 try
693 {
694 bus.call_noreply(method);
695 }
696 catch (const sdbusplus::exception::SdBusError& ex)
697 {
698 log<level::ERR>("Failed to start bare metal active systemd target",
699 entry("WHAT=%s", ex.what()));
700 throw IpmiException(::ipmi::ccUnspecifiedError);
701 }
702}
703
Patrick Venturef085d912019-03-15 08:50:00 -0700704} // namespace ipmi
705} // namespace google