xref: /openbmc/google-ipmi-sys/handler.cpp (revision 8d618532)
14f0d1de6SSteve Foreman // Copyright 2022 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
14*8d618532SMichael Shen 
15f085d91dSPatrick Venture #include "handler.hpp"
16f085d91dSPatrick Venture 
17444b5ea4SPatrick Williams #include "bm_config.h"
18444b5ea4SPatrick Williams 
198ec4106bSNikhil Namjoshi #include "bmc_mode_enum.hpp"
20d2037c6aSPatrick Venture #include "errors.hpp"
21c87de558SPatrick Venture #include "handler_impl.hpp"
22ab650004SPatrick Venture #include "util.hpp"
23d2037c6aSPatrick Venture 
243b1b427cSWilly Tu #include <fcntl.h>
25d2037c6aSPatrick Venture #include <ipmid/api.h>
263b1b427cSWilly Tu #include <mtd/mtd-abi.h>
273b1b427cSWilly Tu #include <mtd/mtd-user.h>
283b1b427cSWilly Tu #include <sys/ioctl.h>
293b1b427cSWilly Tu #include <unistd.h>
30d2037c6aSPatrick Venture 
31444b5ea4SPatrick Williams #include <nlohmann/json.hpp>
32444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp>
33444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp>
34444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp>
35*8d618532SMichael Shen #include <stdplus/print.hpp>
36444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp>
37444b5ea4SPatrick Williams 
38bb90d4fdSPatrick Venture #include <cinttypes>
39d2037c6aSPatrick Venture #include <cstdio>
40d2037c6aSPatrick Venture #include <filesystem>
41d2037c6aSPatrick Venture #include <fstream>
4207f85150SPatrick Venture #include <map>
43d2037c6aSPatrick Venture #include <sstream>
44d2037c6aSPatrick Venture #include <string>
4529f35bceSWilliam A. Kennington III #include <string_view>
46d2037c6aSPatrick Venture #include <tuple>
474f0d1de6SSteve Foreman #include <variant>
485e70dc8cSNikhil Namjoshi 
49f085d91dSPatrick Venture #ifndef NCSI_IF_NAME
50f085d91dSPatrick Venture #define NCSI_IF_NAME eth0
51f085d91dSPatrick Venture #endif
52f085d91dSPatrick Venture 
53f085d91dSPatrick Venture // To deal with receiving a string without quotes.
54f085d91dSPatrick Venture #define QUOTE(name) #name
55f085d91dSPatrick Venture #define STR(macro) QUOTE(macro)
56f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
57f085d91dSPatrick Venture 
588d3d46a2SWilliam A. Kennington III namespace ipmi
598d3d46a2SWilliam A. Kennington III {
608d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName);
618d3d46a2SWilliam A. Kennington III }
628d3d46a2SWilliam A. Kennington III 
63f085d91dSPatrick Venture namespace google
64f085d91dSPatrick Venture {
65f085d91dSPatrick Venture namespace ipmi
66f085d91dSPatrick Venture {
6707f85150SPatrick Venture using Json = nlohmann::json;
6807f85150SPatrick Venture using namespace phosphor::logging;
6907f85150SPatrick Venture using InternalFailure =
7007f85150SPatrick Venture     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
71f085d91dSPatrick Venture 
7215d4d21cSHao Zhou uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs)
735e70dc8cSNikhil Namjoshi {
745e70dc8cSNikhil Namjoshi #if BARE_METAL
755e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::BM_MODE);
765e70dc8cSNikhil Namjoshi #else
773f3ca035SBrandon Kim     std::error_code ec;
784baf41c8SHao Zhou 
7915d4d21cSHao Zhou     if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec))
803f3ca035SBrandon Kim     {
81*8d618532SMichael Shen         stdplus::print(
824baf41c8SHao Zhou             stderr,
83*8d618532SMichael Shen             "{} exists so we acked cleaning done and must be in BM mode\n",
844baf41c8SHao Zhou             bmDriveCleaningDoneAckFlagPath);
853f3ca035SBrandon Kim         return static_cast<uint8_t>(BmcMode::BM_MODE);
863f3ca035SBrandon Kim     }
873f3ca035SBrandon Kim 
8815d4d21cSHao Zhou     if (fs->exists(bmDriveCleaningDoneFlagPath, ec))
894baf41c8SHao Zhou     {
9015d4d21cSHao Zhou         fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath,
914baf41c8SHao Zhou                    ec);
92*8d618532SMichael Shen         stdplus::print(
934baf41c8SHao Zhou             stderr,
94*8d618532SMichael Shen             "{} exists so we just finished cleaning and must be in BM mode\n",
954baf41c8SHao Zhou             bmDriveCleaningDoneFlagPath);
964baf41c8SHao Zhou         return static_cast<uint8_t>(BmcMode::BM_MODE);
974baf41c8SHao Zhou     }
984baf41c8SHao Zhou 
9915d4d21cSHao Zhou     if (fs->exists(BM_SIGNAL_PATH, ec))
1004baf41c8SHao Zhou     {
10115d4d21cSHao Zhou         if (!fs->exists(bmDriveCleaningFlagPath, ec))
1024baf41c8SHao Zhou         {
10315d4d21cSHao Zhou             fs->create(bmDriveCleaningFlagPath);
1044baf41c8SHao Zhou         }
1054baf41c8SHao Zhou 
106*8d618532SMichael Shen         stdplus::print(
1074baf41c8SHao Zhou             stderr,
108*8d618532SMichael Shen             "{} exists and no done/ack flag, we must be in BM cleaning mode\n",
1093f3ca035SBrandon Kim             BM_SIGNAL_PATH);
1104baf41c8SHao Zhou         return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE);
1114baf41c8SHao Zhou     }
1124baf41c8SHao Zhou 
113*8d618532SMichael Shen     stdplus::print(
1144baf41c8SHao Zhou         stderr,
1154baf41c8SHao Zhou         "Unable to find any BM state files so we must not be in BM mode\n");
1165e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
1175e70dc8cSNikhil Namjoshi #endif
1185e70dc8cSNikhil Namjoshi }
1195e70dc8cSNikhil Namjoshi 
1205e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode()
1215e70dc8cSNikhil Namjoshi {
1225e70dc8cSNikhil Namjoshi     // BM_CLEANING_MODE is not implemented yet
12315d4d21cSHao Zhou     return isBmcInBareMetalMode(this->getFs());
1245e70dc8cSNikhil Namjoshi }
1255e70dc8cSNikhil Namjoshi 
126b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string>
127b69209b4SWilliam A. Kennington III     Handler::getEthDetails(std::string intf) const
128f085d91dSPatrick Venture {
129b69209b4SWilliam A. Kennington III     if (intf.empty())
130b69209b4SWilliam A. Kennington III     {
131b69209b4SWilliam A. Kennington III         intf = NCSI_IF_NAME_STR;
132b69209b4SWilliam A. Kennington III     }
133b69209b4SWilliam A. Kennington III     return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
134f085d91dSPatrick Venture }
135f085d91dSPatrick Venture 
136d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const
137d2037c6aSPatrick Venture {
138d2037c6aSPatrick Venture     std::ostringstream opath;
139d2037c6aSPatrick Venture     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
140d2037c6aSPatrick Venture     std::string path = opath.str();
141d2037c6aSPatrick Venture 
142d2037c6aSPatrick Venture     // Minor sanity & security check (of course, I'm less certain if unicode
143d2037c6aSPatrick Venture     // comes into play here.
144d2037c6aSPatrick Venture     //
145d2037c6aSPatrick Venture     // Basically you can't easily inject ../ or /../ into the path below.
146d2037c6aSPatrick Venture     if (name.find("/") != std::string::npos)
147d2037c6aSPatrick Venture     {
148*8d618532SMichael Shen         stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name);
149e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
150d2037c6aSPatrick Venture     }
151d2037c6aSPatrick Venture 
152d2037c6aSPatrick Venture     std::error_code ec;
15315d4d21cSHao Zhou     if (!this->getFs()->exists(path, ec))
154d2037c6aSPatrick Venture     {
155*8d618532SMichael Shen         stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path);
156e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
157d2037c6aSPatrick Venture     }
158d2037c6aSPatrick Venture     // We're uninterested in the state of ec.
159d2037c6aSPatrick Venture 
160d2037c6aSPatrick Venture     int64_t count = 0;
161d2037c6aSPatrick Venture     std::ifstream ifs;
162d2037c6aSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
163d2037c6aSPatrick Venture     try
164d2037c6aSPatrick Venture     {
165d2037c6aSPatrick Venture         ifs.open(path);
166d2037c6aSPatrick Venture         ifs >> count;
167d2037c6aSPatrick Venture     }
168d2037c6aSPatrick Venture     catch (std::ios_base::failure& fail)
169d2037c6aSPatrick Venture     {
170e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
171d2037c6aSPatrick Venture     }
172d2037c6aSPatrick Venture 
173d2037c6aSPatrick Venture     return count;
174d2037c6aSPatrick Venture }
175d2037c6aSPatrick Venture 
176bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const
177bb90d4fdSPatrick Venture {
178bb90d4fdSPatrick Venture     std::ostringstream opath;
179bb90d4fdSPatrick Venture     opath << "/run/cpld" << id << ".version";
180bb90d4fdSPatrick Venture     // Check for file
181bb90d4fdSPatrick Venture 
182bb90d4fdSPatrick Venture     std::error_code ec;
18315d4d21cSHao Zhou     if (!this->getFs()->exists(opath.str(), ec))
184bb90d4fdSPatrick Venture     {
185*8d618532SMichael Shen         stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str());
186e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
187bb90d4fdSPatrick Venture     }
188bb90d4fdSPatrick Venture     // We're uninterested in the state of ec.
189bb90d4fdSPatrick Venture 
190bb90d4fdSPatrick Venture     // If file exists, read.
191bb90d4fdSPatrick Venture     std::ifstream ifs;
192bb90d4fdSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
193bb90d4fdSPatrick Venture     std::string value;
194bb90d4fdSPatrick Venture     try
195bb90d4fdSPatrick Venture     {
196bb90d4fdSPatrick Venture         ifs.open(opath.str());
197bb90d4fdSPatrick Venture         ifs >> value;
198bb90d4fdSPatrick Venture     }
199bb90d4fdSPatrick Venture     catch (std::ios_base::failure& fail)
200bb90d4fdSPatrick Venture     {
201e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
202bb90d4fdSPatrick Venture     }
203bb90d4fdSPatrick Venture 
204bb90d4fdSPatrick Venture     // If value parses as expected, return version.
205bb90d4fdSPatrick Venture     VersionTuple version = std::make_tuple(0, 0, 0, 0);
206bb90d4fdSPatrick Venture 
207444b5ea4SPatrick Williams     int num_fields = std::sscanf(value.c_str(),
208444b5ea4SPatrick Williams                                  "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
209bb90d4fdSPatrick Venture                                  &std::get<0>(version), &std::get<1>(version),
210bb90d4fdSPatrick Venture                                  &std::get<2>(version), &std::get<3>(version));
211bb90d4fdSPatrick Venture     if (num_fields == 0)
212bb90d4fdSPatrick Venture     {
213*8d618532SMichael Shen         stdplus::print(stderr, "Invalid version.\n");
214e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
215bb90d4fdSPatrick Venture     }
216bb90d4fdSPatrick Venture 
217bb90d4fdSPatrick Venture     return version;
218bb90d4fdSPatrick Venture }
219bb90d4fdSPatrick Venture 
220aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
221aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
222aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
223aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
224aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
225aa374120SPatrick Venture 
226aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const
227aa374120SPatrick Venture {
228aa374120SPatrick Venture     std::ofstream ofs;
229aa374120SPatrick Venture     ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
230aa374120SPatrick Venture     if (!ofs.good())
231aa374120SPatrick Venture     {
232*8d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
233e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
234aa374120SPatrick Venture     }
235aa374120SPatrick Venture 
236aa374120SPatrick Venture     ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
237aa374120SPatrick Venture     if (ofs.fail())
238aa374120SPatrick Venture     {
239*8d618532SMichael Shen         stdplus::print(stderr, "Write failed\n");
240aa374120SPatrick Venture         ofs.close();
241e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
242aa374120SPatrick Venture     }
243aa374120SPatrick Venture 
244aa374120SPatrick Venture     // Write succeeded, please continue.
245aa374120SPatrick Venture     ofs.flush();
246aa374120SPatrick Venture     ofs.close();
247aa374120SPatrick Venture 
248aa374120SPatrick Venture     auto bus = sdbusplus::bus::new_default();
249aa374120SPatrick Venture     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
250aa374120SPatrick Venture                                       SYSTEMD_INTERFACE, "StartUnit");
251aa374120SPatrick Venture 
252aa374120SPatrick Venture     method.append(PSU_HARDRESET_TARGET);
253aa374120SPatrick Venture     method.append("replace");
254aa374120SPatrick Venture 
255aa374120SPatrick Venture     try
256aa374120SPatrick Venture     {
257aa374120SPatrick Venture         bus.call_noreply(method);
258aa374120SPatrick Venture     }
259aa374120SPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
260aa374120SPatrick Venture     {
261aa374120SPatrick Venture         log<level::ERR>("Failed to call PSU hard reset");
262e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
263aa374120SPatrick Venture     }
264aa374120SPatrick Venture }
265aa374120SPatrick Venture 
266ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
267ac4a16f7SShounak Mitra 
268ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const
269ac4a16f7SShounak Mitra {
270ac4a16f7SShounak Mitra     std::ofstream ofs;
271ac4a16f7SShounak Mitra     ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
272ac4a16f7SShounak Mitra     if (!ofs.good())
273ac4a16f7SShounak Mitra     {
274*8d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
275e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
276ac4a16f7SShounak Mitra     }
277ac4a16f7SShounak Mitra     ofs.close();
278ac4a16f7SShounak Mitra }
279ac4a16f7SShounak Mitra 
2803b1b427cSWilly Tu uint32_t Handler::getFlashSize()
2813b1b427cSWilly Tu {
2823b1b427cSWilly Tu     mtd_info_t info;
2833b1b427cSWilly Tu     int fd = open("/dev/mtd0", O_RDONLY);
2843b1b427cSWilly Tu     int err = ioctl(fd, MEMGETINFO, &info);
2853b1b427cSWilly Tu     close(fd);
2863b1b427cSWilly Tu 
2873b1b427cSWilly Tu     if (err)
2883b1b427cSWilly Tu     {
289e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
2903b1b427cSWilly Tu     }
2913b1b427cSWilly Tu     return info.size;
2923b1b427cSWilly Tu }
2933b1b427cSWilly Tu 
294ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
29507f85150SPatrick Venture {
296ab650004SPatrick Venture     // Check if we support this Entity ID.
297ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
298ab650004SPatrick Venture     if (it == _entityIdToName.end())
29907f85150SPatrick Venture     {
300ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
301e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
30207f85150SPatrick Venture     }
30307f85150SPatrick Venture 
304ab650004SPatrick Venture     std::string entityName;
305ab650004SPatrick Venture     try
30607f85150SPatrick Venture     {
307ab650004SPatrick Venture         // Parse the JSON config file.
308ab650004SPatrick Venture         if (!_entityConfigParsed)
309ab650004SPatrick Venture         {
310ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
311ab650004SPatrick Venture             _entityConfigParsed = true;
31207f85150SPatrick Venture         }
31307f85150SPatrick Venture 
314ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
315ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
316ab650004SPatrick Venture         if (entityName.empty())
317ab650004SPatrick Venture         {
318e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidFieldRequest);
319ab650004SPatrick Venture         }
320ab650004SPatrick Venture     }
321ab650004SPatrick Venture     catch (InternalFailure& e)
322ab650004SPatrick Venture     {
323e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
32407f85150SPatrick Venture     }
32507f85150SPatrick Venture 
326ab650004SPatrick Venture     return entityName;
327ab650004SPatrick Venture }
328ab650004SPatrick Venture 
32929f35bceSWilliam A. Kennington III std::string Handler::getMachineName()
33029f35bceSWilliam A. Kennington III {
33129f35bceSWilliam A. Kennington III     const char* path = "/etc/os-release";
33229f35bceSWilliam A. Kennington III     std::ifstream ifs(path);
33329f35bceSWilliam A. Kennington III     if (ifs.fail())
33429f35bceSWilliam A. Kennington III     {
335*8d618532SMichael Shen         stdplus::print(stderr, "Failed to open: {}\n", path);
336e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
33729f35bceSWilliam A. Kennington III     }
33829f35bceSWilliam A. Kennington III 
33929f35bceSWilliam A. Kennington III     std::string line;
34029f35bceSWilliam A. Kennington III     while (true)
34129f35bceSWilliam A. Kennington III     {
34229f35bceSWilliam A. Kennington III         std::getline(ifs, line);
34329f35bceSWilliam A. Kennington III         if (ifs.eof())
34429f35bceSWilliam A. Kennington III         {
345*8d618532SMichael Shen             stdplus::print(stderr,
346*8d618532SMichael Shen                            "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path);
347e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidCommand);
34829f35bceSWilliam A. Kennington III         }
34929f35bceSWilliam A. Kennington III         if (ifs.fail())
35029f35bceSWilliam A. Kennington III         {
351*8d618532SMichael Shen             stdplus::print(stderr, "Failed to read: {}\n", path);
352e5a06675SMichael Shen             throw IpmiException(::ipmi::ccUnspecifiedError);
35329f35bceSWilliam A. Kennington III         }
35429f35bceSWilliam A. Kennington III         std::string_view lineView(line);
35529f35bceSWilliam A. Kennington III         constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
35629f35bceSWilliam A. Kennington III         if (lineView.substr(0, prefix.size()) != prefix)
35729f35bceSWilliam A. Kennington III         {
35829f35bceSWilliam A. Kennington III             continue;
35929f35bceSWilliam A. Kennington III         }
36029f35bceSWilliam A. Kennington III         lineView.remove_prefix(prefix.size());
36129f35bceSWilliam A. Kennington III         lineView.remove_prefix(
36229f35bceSWilliam A. Kennington III             std::min(lineView.find_first_not_of('"'), lineView.size()));
36329f35bceSWilliam A. Kennington III         lineView.remove_suffix(
36429f35bceSWilliam A. Kennington III             lineView.size() - 1 -
36529f35bceSWilliam A. Kennington III             std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
36629f35bceSWilliam A. Kennington III         return std::string(lineView);
36729f35bceSWilliam A. Kennington III     }
36829f35bceSWilliam A. Kennington III }
36929f35bceSWilliam A. Kennington III 
3708cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
3718cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
3728cfa4c44Slinyuny 
3738cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const
3748cfa4c44Slinyuny {
3758cfa4c44Slinyuny     // Set time delay
3768cfa4c44Slinyuny     std::ofstream ofs;
3778cfa4c44Slinyuny     ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
3788cfa4c44Slinyuny     if (!ofs.good())
3798cfa4c44Slinyuny     {
380*8d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
381e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3828cfa4c44Slinyuny     }
3838cfa4c44Slinyuny 
3848cfa4c44Slinyuny     ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
3858cfa4c44Slinyuny     ofs.close();
3868cfa4c44Slinyuny     if (ofs.fail())
3878cfa4c44Slinyuny     {
388*8d618532SMichael Shen         stdplus::print(stderr, "Write failed\n");
389e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3908cfa4c44Slinyuny     }
3918cfa4c44Slinyuny 
3928cfa4c44Slinyuny     // Write succeeded, please continue.
3938cfa4c44Slinyuny     auto bus = sdbusplus::bus::new_default();
3948cfa4c44Slinyuny     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
3958cfa4c44Slinyuny                                       SYSTEMD_INTERFACE, "StartUnit");
3968cfa4c44Slinyuny 
3978cfa4c44Slinyuny     method.append(HOST_POWEROFF_TARGET);
3988cfa4c44Slinyuny     method.append("replace");
3998cfa4c44Slinyuny 
4008cfa4c44Slinyuny     try
4018cfa4c44Slinyuny     {
4028cfa4c44Slinyuny         bus.call_noreply(method);
4038cfa4c44Slinyuny     }
4048cfa4c44Slinyuny     catch (const sdbusplus::exception::SdBusError& ex)
4058cfa4c44Slinyuny     {
4068cfa4c44Slinyuny         log<level::ERR>("Failed to call Power Off",
4078cfa4c44Slinyuny                         entry("WHAT=%s", ex.what()));
408e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4098cfa4c44Slinyuny     }
4108cfa4c44Slinyuny }
4118cfa4c44Slinyuny 
412ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
413ab650004SPatrick Venture                                const Json& config)
41407f85150SPatrick Venture {
41507f85150SPatrick Venture     static const std::vector<Json> empty{};
41607f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
41707f85150SPatrick Venture     std::string name = "";
418ab650004SPatrick Venture 
41907f85150SPatrick Venture     for (const auto& j : readings)
42007f85150SPatrick Venture     {
42107f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
42207f85150SPatrick Venture         // Not the instance we're interested in
42307f85150SPatrick Venture         if (instanceNum != instance)
42407f85150SPatrick Venture         {
42507f85150SPatrick Venture             continue;
42607f85150SPatrick Venture         }
42707f85150SPatrick Venture 
42807f85150SPatrick Venture         // Found the instance we're interested in
42907f85150SPatrick Venture         name = j.value("name", "");
43007f85150SPatrick Venture 
43107f85150SPatrick Venture         break;
43207f85150SPatrick Venture     }
433ab650004SPatrick Venture 
43407f85150SPatrick Venture     return name;
43507f85150SPatrick Venture }
43607f85150SPatrick Venture 
43749f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
43849f23ad9SPatrick Venture {
43949f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
44049f23ad9SPatrick Venture }
44149f23ad9SPatrick Venture 
44249f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
44349f23ad9SPatrick Venture {
44449f23ad9SPatrick Venture     return _pcie_i2c_map.size();
44549f23ad9SPatrick Venture }
44649f23ad9SPatrick Venture 
44749f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
44849f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
44949f23ad9SPatrick Venture {
45049f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
45149f23ad9SPatrick Venture }
45249f23ad9SPatrick Venture 
4534f0d1de6SSteve Foreman namespace
4544f0d1de6SSteve Foreman {
4554f0d1de6SSteve Foreman 
4564f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
4574f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
4584f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
4594f0d1de6SSteve Foreman 
4604f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
4614f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
4624f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
4634f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists =
4644f0d1de6SSteve Foreman     std::vector<std::pair<std::string, AnyTypeList>>;
4654f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
4664f0d1de6SSteve Foreman     std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
4674f0d1de6SSteve Foreman 
4684f0d1de6SSteve Foreman } // namespace
4694f0d1de6SSteve Foreman 
47065371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const
4714f0d1de6SSteve Foreman {
4724f0d1de6SSteve Foreman     return sdbusplus::bus::new_default();
4734f0d1de6SSteve Foreman }
4744f0d1de6SSteve Foreman 
47515d4d21cSHao Zhou const std::unique_ptr<FileSystemInterface>& Handler::getFs() const
47615d4d21cSHao Zhou {
47715d4d21cSHao Zhou     return this->fsPtr;
47815d4d21cSHao Zhou }
47915d4d21cSHao Zhou 
4804f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const
4814f0d1de6SSteve Foreman {
4824f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
4834f0d1de6SSteve Foreman 
4844f0d1de6SSteve Foreman     try
4854f0d1de6SSteve Foreman     {
4860e928ac6SMichael Shen         auto bus = getDbus();
4874f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
4884f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
4894f0d1de6SSteve Foreman                                           "GetManagedObjects");
4904f0d1de6SSteve Foreman         bus.call(method).read(data);
4914f0d1de6SSteve Foreman     }
4924f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
4934f0d1de6SSteve Foreman     {
4944f0d1de6SSteve Foreman         log<level::ERR>(
4954f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
4964f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
497e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4984f0d1de6SSteve Foreman     }
4994f0d1de6SSteve Foreman 
5004f0d1de6SSteve Foreman     return data.size();
5014f0d1de6SSteve Foreman }
5024f0d1de6SSteve Foreman 
5034f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const
5044f0d1de6SSteve Foreman {
5054f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
5064f0d1de6SSteve Foreman 
5074f0d1de6SSteve Foreman     try
5084f0d1de6SSteve Foreman     {
5090e928ac6SMichael Shen         auto bus = getDbus();
5104f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
5114f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
5124f0d1de6SSteve Foreman                                           "GetManagedObjects");
5134f0d1de6SSteve Foreman         bus.call(method).read(data);
5144f0d1de6SSteve Foreman     }
5154f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
5164f0d1de6SSteve Foreman     {
5174f0d1de6SSteve Foreman         log<level::ERR>(
5184f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
5194f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
520e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5214f0d1de6SSteve Foreman     }
5224f0d1de6SSteve Foreman 
5234f0d1de6SSteve Foreman     if (index >= data.size())
5244f0d1de6SSteve Foreman     {
5254f0d1de6SSteve Foreman         log<level::WARNING>(
5264f0d1de6SSteve Foreman             "Requested index is larger than the number of entries.",
5274f0d1de6SSteve Foreman             entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
528e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
5294f0d1de6SSteve Foreman     }
5304f0d1de6SSteve Foreman 
5314f0d1de6SSteve Foreman     std::string_view name(data[index].first.str);
5324f0d1de6SSteve Foreman     if (!name.starts_with(ACCEL_OOB_ROOT))
5334f0d1de6SSteve Foreman     {
534e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidCommand);
5354f0d1de6SSteve Foreman     }
5364f0d1de6SSteve Foreman     name.remove_prefix(ACCEL_OOB_ROOT.length());
5374f0d1de6SSteve Foreman     return std::string(name);
5384f0d1de6SSteve Foreman }
5394f0d1de6SSteve Foreman 
5404f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
5414f0d1de6SSteve Foreman                                uint8_t num_bytes) const
5424f0d1de6SSteve Foreman {
5434f0d1de6SSteve Foreman     static constexpr char ACCEL_OOB_METHOD[] = "Read";
5444f0d1de6SSteve Foreman 
5454f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
5464f0d1de6SSteve Foreman     object_name.append(name);
5474f0d1de6SSteve Foreman 
5480e928ac6SMichael Shen     auto bus = getDbus();
5494f0d1de6SSteve Foreman     auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
5504f0d1de6SSteve Foreman                                       ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
5514f0d1de6SSteve Foreman     method.append(address, static_cast<uint64_t>(num_bytes));
5524f0d1de6SSteve Foreman 
5534f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
5544f0d1de6SSteve Foreman 
5554f0d1de6SSteve Foreman     try
5564f0d1de6SSteve Foreman     {
5574f0d1de6SSteve Foreman         bus.call(method).read(bytes);
5584f0d1de6SSteve Foreman     }
5594f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
5604f0d1de6SSteve Foreman     {
5614f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Read on com.google.custom_accel",
5624f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
5634f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5644f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
5654f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5664f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5674f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
5684f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
569e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5704f0d1de6SSteve Foreman     }
5714f0d1de6SSteve Foreman 
5724f0d1de6SSteve Foreman     if (bytes.size() < num_bytes)
5734f0d1de6SSteve Foreman     {
5744f0d1de6SSteve Foreman         log<level::ERR>(
5754f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel didn't return the expected"
5764f0d1de6SSteve Foreman             " number of bytes.",
5774f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5784f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5794f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5804f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5814f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5824f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5834f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
584e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5854f0d1de6SSteve Foreman     }
5864f0d1de6SSteve Foreman 
5874f0d1de6SSteve Foreman     if (bytes.size() > sizeof(uint64_t))
5884f0d1de6SSteve Foreman     {
5894f0d1de6SSteve Foreman         log<level::ERR>(
5904f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel returned more than 8B.",
5914f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5924f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5934f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5944f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5954f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5964f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5974f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
598e5a06675SMichael Shen         throw IpmiException(::ipmi::ccReqDataTruncated);
5994f0d1de6SSteve Foreman     }
6004f0d1de6SSteve Foreman 
6014f0d1de6SSteve Foreman     uint64_t data = 0;
6024f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
6034f0d1de6SSteve Foreman     {
6044f0d1de6SSteve Foreman         data = (data << 8) | bytes[i];
6054f0d1de6SSteve Foreman     }
6064f0d1de6SSteve Foreman 
6074f0d1de6SSteve Foreman     return data;
6084f0d1de6SSteve Foreman }
6094f0d1de6SSteve Foreman 
6104f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address,
6114f0d1de6SSteve Foreman                             uint8_t num_bytes, uint64_t data) const
6124f0d1de6SSteve Foreman {
6134f0d1de6SSteve Foreman     static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
6144f0d1de6SSteve Foreman 
6154f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
6164f0d1de6SSteve Foreman     object_name.append(name);
6174f0d1de6SSteve Foreman 
6184f0d1de6SSteve Foreman     if (num_bytes > sizeof(data))
6194f0d1de6SSteve Foreman     {
6204f0d1de6SSteve Foreman         log<level::ERR>(
6214f0d1de6SSteve Foreman             "Call to Write on com.google.custom_accel requested more than 8B.",
6224f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
6234f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
6244f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
6254f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
6264f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
6274f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
6284f0d1de6SSteve Foreman             entry("DBUS_ARG_DATA=%016llx", data));
629e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
6304f0d1de6SSteve Foreman     }
6314f0d1de6SSteve Foreman 
6324f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
6334f0d1de6SSteve Foreman     bytes.reserve(num_bytes);
6344f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
6354f0d1de6SSteve Foreman     {
6364f0d1de6SSteve Foreman         bytes.emplace_back(data & 0xff);
6374f0d1de6SSteve Foreman         data >>= 8;
6384f0d1de6SSteve Foreman     }
6394f0d1de6SSteve Foreman 
6404f0d1de6SSteve Foreman     try
6414f0d1de6SSteve Foreman     {
6420e928ac6SMichael Shen         auto bus = getDbus();
6434f0d1de6SSteve Foreman         auto method =
6444f0d1de6SSteve Foreman             bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
6454f0d1de6SSteve Foreman                                 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
6464f0d1de6SSteve Foreman         method.append(address, bytes);
6474f0d1de6SSteve Foreman         bus.call_noreply(method);
6484f0d1de6SSteve Foreman     }
6494f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
6504f0d1de6SSteve Foreman     {
6514f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Write on com.google.custom_accel",
6524f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
6534f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
6544f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
6554f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
6564f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
6574f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
6584f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
6594f0d1de6SSteve Foreman                         entry("DBUS_ARG_DATA=%016llx", data));
660e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
6614f0d1de6SSteve Foreman     }
6624f0d1de6SSteve Foreman }
6634f0d1de6SSteve Foreman 
6646c71b0f9SWilly Tu std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
6656c71b0f9SWilly Tu {
6666c71b0f9SWilly Tu     return bifurcationHelper.get().getBifurcation(index).value_or(
6676c71b0f9SWilly Tu         std::vector<uint8_t>{});
6686c71b0f9SWilly Tu }
6696c71b0f9SWilly Tu 
670a92d0e6bSJohn Wedig static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target";
671a92d0e6bSJohn Wedig 
672a92d0e6bSJohn Wedig void Handler::linuxBootDone() const
673a92d0e6bSJohn Wedig {
67415d4d21cSHao Zhou     if (isBmcInBareMetalMode(this->fsPtr) !=
67515d4d21cSHao Zhou         static_cast<uint8_t>(BmcMode::BM_MODE))
676a92d0e6bSJohn Wedig     {
677a92d0e6bSJohn Wedig         return;
678a92d0e6bSJohn Wedig     }
679a92d0e6bSJohn Wedig 
680a92d0e6bSJohn Wedig     log<level::INFO>("LinuxBootDone: Disabling IPMI");
681a92d0e6bSJohn Wedig 
682a92d0e6bSJohn Wedig     // Start the bare metal active systemd target.
683a92d0e6bSJohn Wedig     auto bus = sdbusplus::bus::new_default();
684a92d0e6bSJohn Wedig     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
685a92d0e6bSJohn Wedig                                       SYSTEMD_INTERFACE, "StartUnit");
686a92d0e6bSJohn Wedig 
687a92d0e6bSJohn Wedig     method.append(BARE_METAL_TARGET);
688a92d0e6bSJohn Wedig     method.append("replace");
689a92d0e6bSJohn Wedig 
690a92d0e6bSJohn Wedig     try
691a92d0e6bSJohn Wedig     {
692a92d0e6bSJohn Wedig         bus.call_noreply(method);
693a92d0e6bSJohn Wedig     }
694a92d0e6bSJohn Wedig     catch (const sdbusplus::exception::SdBusError& ex)
695a92d0e6bSJohn Wedig     {
696a92d0e6bSJohn Wedig         log<level::ERR>("Failed to start bare metal active systemd target",
697a92d0e6bSJohn Wedig                         entry("WHAT=%s", ex.what()));
698a92d0e6bSJohn Wedig         throw IpmiException(::ipmi::ccUnspecifiedError);
699a92d0e6bSJohn Wedig     }
700a92d0e6bSJohn Wedig }
701a92d0e6bSJohn Wedig 
702f085d91dSPatrick Venture } // namespace ipmi
703f085d91dSPatrick Venture } // namespace google
704