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. 14f085d91dSPatrick Venture #include "handler.hpp" 15f085d91dSPatrick Venture 16444b5ea4SPatrick Williams #include "bm_config.h" 17444b5ea4SPatrick Williams 188ec4106bSNikhil Namjoshi #include "bmc_mode_enum.hpp" 19d2037c6aSPatrick Venture #include "errors.hpp" 20c87de558SPatrick Venture #include "handler_impl.hpp" 21ab650004SPatrick Venture #include "util.hpp" 22d2037c6aSPatrick Venture 233b1b427cSWilly Tu #include <fcntl.h> 24d2037c6aSPatrick Venture #include <ipmid/api.h> 253b1b427cSWilly Tu #include <mtd/mtd-abi.h> 263b1b427cSWilly Tu #include <mtd/mtd-user.h> 273b1b427cSWilly Tu #include <sys/ioctl.h> 283b1b427cSWilly Tu #include <unistd.h> 29d2037c6aSPatrick Venture 30444b5ea4SPatrick Williams #include <nlohmann/json.hpp> 31444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp> 32444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp> 33444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp> 34444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp> 35444b5ea4SPatrick Williams 36bb90d4fdSPatrick Venture #include <cinttypes> 37d2037c6aSPatrick Venture #include <cstdio> 38d2037c6aSPatrick Venture #include <filesystem> 39d2037c6aSPatrick Venture #include <fstream> 4007f85150SPatrick Venture #include <map> 41d2037c6aSPatrick Venture #include <sstream> 42d2037c6aSPatrick Venture #include <string> 4329f35bceSWilliam A. Kennington III #include <string_view> 44d2037c6aSPatrick Venture #include <tuple> 454f0d1de6SSteve Foreman #include <variant> 465e70dc8cSNikhil Namjoshi 47f085d91dSPatrick Venture #ifndef NCSI_IF_NAME 48f085d91dSPatrick Venture #define NCSI_IF_NAME eth0 49f085d91dSPatrick Venture #endif 50f085d91dSPatrick Venture 51f085d91dSPatrick Venture // To deal with receiving a string without quotes. 52f085d91dSPatrick Venture #define QUOTE(name) #name 53f085d91dSPatrick Venture #define STR(macro) QUOTE(macro) 54f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) 55f085d91dSPatrick Venture 568d3d46a2SWilliam A. Kennington III namespace ipmi 578d3d46a2SWilliam A. Kennington III { 588d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName); 598d3d46a2SWilliam A. Kennington III } 608d3d46a2SWilliam A. Kennington III 61f085d91dSPatrick Venture namespace google 62f085d91dSPatrick Venture { 63f085d91dSPatrick Venture namespace ipmi 64f085d91dSPatrick Venture { 6507f85150SPatrick Venture using Json = nlohmann::json; 6607f85150SPatrick Venture using namespace phosphor::logging; 6707f85150SPatrick Venture using InternalFailure = 6807f85150SPatrick Venture sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 69f085d91dSPatrick Venture 70*15d4d21cSHao Zhou uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs) 715e70dc8cSNikhil Namjoshi { 725e70dc8cSNikhil Namjoshi #if BARE_METAL 735e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::BM_MODE); 745e70dc8cSNikhil Namjoshi #else 753f3ca035SBrandon Kim std::error_code ec; 764baf41c8SHao Zhou 77*15d4d21cSHao Zhou if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec)) 783f3ca035SBrandon Kim { 794baf41c8SHao Zhou std::fprintf( 804baf41c8SHao Zhou stderr, 814baf41c8SHao Zhou "%s exists so we acked cleaning done and must be in BM mode\n", 824baf41c8SHao Zhou bmDriveCleaningDoneAckFlagPath); 833f3ca035SBrandon Kim return static_cast<uint8_t>(BmcMode::BM_MODE); 843f3ca035SBrandon Kim } 853f3ca035SBrandon Kim 86*15d4d21cSHao Zhou if (fs->exists(bmDriveCleaningDoneFlagPath, ec)) 874baf41c8SHao Zhou { 88*15d4d21cSHao Zhou fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath, 894baf41c8SHao Zhou ec); 904baf41c8SHao Zhou std::fprintf( 914baf41c8SHao Zhou stderr, 924baf41c8SHao Zhou "%s exists so we just finished cleaning and must be in BM mode\n", 934baf41c8SHao Zhou bmDriveCleaningDoneFlagPath); 944baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_MODE); 954baf41c8SHao Zhou } 964baf41c8SHao Zhou 97*15d4d21cSHao Zhou if (fs->exists(BM_SIGNAL_PATH, ec)) 984baf41c8SHao Zhou { 99*15d4d21cSHao Zhou if (!fs->exists(bmDriveCleaningFlagPath, ec)) 1004baf41c8SHao Zhou { 101*15d4d21cSHao Zhou fs->create(bmDriveCleaningFlagPath); 1024baf41c8SHao Zhou } 1034baf41c8SHao Zhou 1044baf41c8SHao Zhou std::fprintf( 1054baf41c8SHao Zhou stderr, 1064baf41c8SHao Zhou "%s exists and no done/ack flag, we must be in BM cleaning mode\n", 1073f3ca035SBrandon Kim BM_SIGNAL_PATH); 1084baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE); 1094baf41c8SHao Zhou } 1104baf41c8SHao Zhou 1114baf41c8SHao Zhou std::fprintf( 1124baf41c8SHao Zhou stderr, 1134baf41c8SHao Zhou "Unable to find any BM state files so we must not be in BM mode\n"); 1145e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::NON_BM_MODE); 1155e70dc8cSNikhil Namjoshi #endif 1165e70dc8cSNikhil Namjoshi } 1175e70dc8cSNikhil Namjoshi 1185e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode() 1195e70dc8cSNikhil Namjoshi { 1205e70dc8cSNikhil Namjoshi // BM_CLEANING_MODE is not implemented yet 121*15d4d21cSHao Zhou return isBmcInBareMetalMode(this->getFs()); 1225e70dc8cSNikhil Namjoshi } 1235e70dc8cSNikhil Namjoshi 124b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string> 125b69209b4SWilliam A. Kennington III Handler::getEthDetails(std::string intf) const 126f085d91dSPatrick Venture { 127b69209b4SWilliam A. Kennington III if (intf.empty()) 128b69209b4SWilliam A. Kennington III { 129b69209b4SWilliam A. Kennington III intf = NCSI_IF_NAME_STR; 130b69209b4SWilliam A. Kennington III } 131b69209b4SWilliam A. Kennington III return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf)); 132f085d91dSPatrick Venture } 133f085d91dSPatrick Venture 134d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const 135d2037c6aSPatrick Venture { 136d2037c6aSPatrick Venture std::ostringstream opath; 137d2037c6aSPatrick Venture opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 138d2037c6aSPatrick Venture std::string path = opath.str(); 139d2037c6aSPatrick Venture 140d2037c6aSPatrick Venture // Minor sanity & security check (of course, I'm less certain if unicode 141d2037c6aSPatrick Venture // comes into play here. 142d2037c6aSPatrick Venture // 143d2037c6aSPatrick Venture // Basically you can't easily inject ../ or /../ into the path below. 144d2037c6aSPatrick Venture if (name.find("/") != std::string::npos) 145d2037c6aSPatrick Venture { 146d2037c6aSPatrick Venture std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str()); 147e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 148d2037c6aSPatrick Venture } 149d2037c6aSPatrick Venture 150d2037c6aSPatrick Venture std::error_code ec; 151*15d4d21cSHao Zhou if (!this->getFs()->exists(path, ec)) 152d2037c6aSPatrick Venture { 153d2037c6aSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); 154e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 155d2037c6aSPatrick Venture } 156d2037c6aSPatrick Venture // We're uninterested in the state of ec. 157d2037c6aSPatrick Venture 158d2037c6aSPatrick Venture int64_t count = 0; 159d2037c6aSPatrick Venture std::ifstream ifs; 160d2037c6aSPatrick Venture ifs.exceptions(std::ifstream::failbit); 161d2037c6aSPatrick Venture try 162d2037c6aSPatrick Venture { 163d2037c6aSPatrick Venture ifs.open(path); 164d2037c6aSPatrick Venture ifs >> count; 165d2037c6aSPatrick Venture } 166d2037c6aSPatrick Venture catch (std::ios_base::failure& fail) 167d2037c6aSPatrick Venture { 168e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 169d2037c6aSPatrick Venture } 170d2037c6aSPatrick Venture 171d2037c6aSPatrick Venture return count; 172d2037c6aSPatrick Venture } 173d2037c6aSPatrick Venture 174bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const 175bb90d4fdSPatrick Venture { 176bb90d4fdSPatrick Venture std::ostringstream opath; 177bb90d4fdSPatrick Venture opath << "/run/cpld" << id << ".version"; 178bb90d4fdSPatrick Venture // Check for file 179bb90d4fdSPatrick Venture 180bb90d4fdSPatrick Venture std::error_code ec; 181*15d4d21cSHao Zhou if (!this->getFs()->exists(opath.str(), ec)) 182bb90d4fdSPatrick Venture { 183bb90d4fdSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", 184bb90d4fdSPatrick Venture opath.str().c_str()); 185e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 186bb90d4fdSPatrick Venture } 187bb90d4fdSPatrick Venture // We're uninterested in the state of ec. 188bb90d4fdSPatrick Venture 189bb90d4fdSPatrick Venture // If file exists, read. 190bb90d4fdSPatrick Venture std::ifstream ifs; 191bb90d4fdSPatrick Venture ifs.exceptions(std::ifstream::failbit); 192bb90d4fdSPatrick Venture std::string value; 193bb90d4fdSPatrick Venture try 194bb90d4fdSPatrick Venture { 195bb90d4fdSPatrick Venture ifs.open(opath.str()); 196bb90d4fdSPatrick Venture ifs >> value; 197bb90d4fdSPatrick Venture } 198bb90d4fdSPatrick Venture catch (std::ios_base::failure& fail) 199bb90d4fdSPatrick Venture { 200e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 201bb90d4fdSPatrick Venture } 202bb90d4fdSPatrick Venture 203bb90d4fdSPatrick Venture // If value parses as expected, return version. 204bb90d4fdSPatrick Venture VersionTuple version = std::make_tuple(0, 0, 0, 0); 205bb90d4fdSPatrick Venture 206444b5ea4SPatrick Williams int num_fields = std::sscanf(value.c_str(), 207444b5ea4SPatrick Williams "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, 208bb90d4fdSPatrick Venture &std::get<0>(version), &std::get<1>(version), 209bb90d4fdSPatrick Venture &std::get<2>(version), &std::get<3>(version)); 210bb90d4fdSPatrick Venture if (num_fields == 0) 211bb90d4fdSPatrick Venture { 212bb90d4fdSPatrick Venture std::fprintf(stderr, "Invalid version.\n"); 213e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 214bb90d4fdSPatrick Venture } 215bb90d4fdSPatrick Venture 216bb90d4fdSPatrick Venture return version; 217bb90d4fdSPatrick Venture } 218bb90d4fdSPatrick Venture 219aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; 220aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 221aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 222aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 223aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; 224aa374120SPatrick Venture 225aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const 226aa374120SPatrick Venture { 227aa374120SPatrick Venture std::ofstream ofs; 228aa374120SPatrick Venture ofs.open(TIME_DELAY_FILENAME, std::ofstream::out); 229aa374120SPatrick Venture if (!ofs.good()) 230aa374120SPatrick Venture { 231aa374120SPatrick Venture std::fprintf(stderr, "Unable to open file for output.\n"); 232e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 233aa374120SPatrick Venture } 234aa374120SPatrick Venture 235aa374120SPatrick Venture ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl; 236aa374120SPatrick Venture if (ofs.fail()) 237aa374120SPatrick Venture { 238aa374120SPatrick Venture std::fprintf(stderr, "Write failed\n"); 239aa374120SPatrick Venture ofs.close(); 240e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 241aa374120SPatrick Venture } 242aa374120SPatrick Venture 243aa374120SPatrick Venture // Write succeeded, please continue. 244aa374120SPatrick Venture ofs.flush(); 245aa374120SPatrick Venture ofs.close(); 246aa374120SPatrick Venture 247aa374120SPatrick Venture auto bus = sdbusplus::bus::new_default(); 248aa374120SPatrick Venture auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 249aa374120SPatrick Venture SYSTEMD_INTERFACE, "StartUnit"); 250aa374120SPatrick Venture 251aa374120SPatrick Venture method.append(PSU_HARDRESET_TARGET); 252aa374120SPatrick Venture method.append("replace"); 253aa374120SPatrick Venture 254aa374120SPatrick Venture try 255aa374120SPatrick Venture { 256aa374120SPatrick Venture bus.call_noreply(method); 257aa374120SPatrick Venture } 258aa374120SPatrick Venture catch (const sdbusplus::exception::SdBusError& ex) 259aa374120SPatrick Venture { 260aa374120SPatrick Venture log<level::ERR>("Failed to call PSU hard reset"); 261e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 262aa374120SPatrick Venture } 263aa374120SPatrick Venture } 264aa374120SPatrick Venture 265ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5"; 266ac4a16f7SShounak Mitra 267ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const 268ac4a16f7SShounak Mitra { 269ac4a16f7SShounak Mitra std::ofstream ofs; 270ac4a16f7SShounak Mitra ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out); 271ac4a16f7SShounak Mitra if (!ofs.good()) 272ac4a16f7SShounak Mitra { 273ac4a16f7SShounak Mitra std::fprintf(stderr, "Unable to open file for output.\n"); 274e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 275ac4a16f7SShounak Mitra } 276ac4a16f7SShounak Mitra ofs.close(); 277ac4a16f7SShounak Mitra } 278ac4a16f7SShounak Mitra 2793b1b427cSWilly Tu uint32_t Handler::getFlashSize() 2803b1b427cSWilly Tu { 2813b1b427cSWilly Tu mtd_info_t info; 2823b1b427cSWilly Tu int fd = open("/dev/mtd0", O_RDONLY); 2833b1b427cSWilly Tu int err = ioctl(fd, MEMGETINFO, &info); 2843b1b427cSWilly Tu close(fd); 2853b1b427cSWilly Tu 2863b1b427cSWilly Tu if (err) 2873b1b427cSWilly Tu { 288e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 2893b1b427cSWilly Tu } 2903b1b427cSWilly Tu return info.size; 2913b1b427cSWilly Tu } 2923b1b427cSWilly Tu 293ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) 29407f85150SPatrick Venture { 295ab650004SPatrick Venture // Check if we support this Entity ID. 296ab650004SPatrick Venture auto it = _entityIdToName.find(id); 297ab650004SPatrick Venture if (it == _entityIdToName.end()) 29807f85150SPatrick Venture { 299ab650004SPatrick Venture log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); 300e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 30107f85150SPatrick Venture } 30207f85150SPatrick Venture 303ab650004SPatrick Venture std::string entityName; 304ab650004SPatrick Venture try 30507f85150SPatrick Venture { 306ab650004SPatrick Venture // Parse the JSON config file. 307ab650004SPatrick Venture if (!_entityConfigParsed) 308ab650004SPatrick Venture { 309ab650004SPatrick Venture _entityConfig = parseConfig(_configFile); 310ab650004SPatrick Venture _entityConfigParsed = true; 31107f85150SPatrick Venture } 31207f85150SPatrick Venture 313ab650004SPatrick Venture // Find the "entity id:entity instance" mapping to entity name. 314ab650004SPatrick Venture entityName = readNameFromConfig(it->second, instance, _entityConfig); 315ab650004SPatrick Venture if (entityName.empty()) 316ab650004SPatrick Venture { 317e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 318ab650004SPatrick Venture } 319ab650004SPatrick Venture } 320ab650004SPatrick Venture catch (InternalFailure& e) 321ab650004SPatrick Venture { 322e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 32307f85150SPatrick Venture } 32407f85150SPatrick Venture 325ab650004SPatrick Venture return entityName; 326ab650004SPatrick Venture } 327ab650004SPatrick Venture 32829f35bceSWilliam A. Kennington III std::string Handler::getMachineName() 32929f35bceSWilliam A. Kennington III { 33029f35bceSWilliam A. Kennington III const char* path = "/etc/os-release"; 33129f35bceSWilliam A. Kennington III std::ifstream ifs(path); 33229f35bceSWilliam A. Kennington III if (ifs.fail()) 33329f35bceSWilliam A. Kennington III { 33429f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to open: %s\n", path); 335e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 33629f35bceSWilliam A. Kennington III } 33729f35bceSWilliam A. Kennington III 33829f35bceSWilliam A. Kennington III std::string line; 33929f35bceSWilliam A. Kennington III while (true) 34029f35bceSWilliam A. Kennington III { 34129f35bceSWilliam A. Kennington III std::getline(ifs, line); 34229f35bceSWilliam A. Kennington III if (ifs.eof()) 34329f35bceSWilliam A. Kennington III { 34429f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n", 34529f35bceSWilliam A. Kennington III path); 346e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidCommand); 34729f35bceSWilliam A. Kennington III } 34829f35bceSWilliam A. Kennington III if (ifs.fail()) 34929f35bceSWilliam A. Kennington III { 35029f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to read: %s\n", path); 351e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 35229f35bceSWilliam A. Kennington III } 35329f35bceSWilliam A. Kennington III std::string_view lineView(line); 35429f35bceSWilliam A. Kennington III constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE="; 35529f35bceSWilliam A. Kennington III if (lineView.substr(0, prefix.size()) != prefix) 35629f35bceSWilliam A. Kennington III { 35729f35bceSWilliam A. Kennington III continue; 35829f35bceSWilliam A. Kennington III } 35929f35bceSWilliam A. Kennington III lineView.remove_prefix(prefix.size()); 36029f35bceSWilliam A. Kennington III lineView.remove_prefix( 36129f35bceSWilliam A. Kennington III std::min(lineView.find_first_not_of('"'), lineView.size())); 36229f35bceSWilliam A. Kennington III lineView.remove_suffix( 36329f35bceSWilliam A. Kennington III lineView.size() - 1 - 36429f35bceSWilliam A. Kennington III std::min(lineView.find_last_not_of('"'), lineView.size() - 1)); 36529f35bceSWilliam A. Kennington III return std::string(lineView); 36629f35bceSWilliam A. Kennington III } 36729f35bceSWilliam A. Kennington III } 36829f35bceSWilliam A. Kennington III 3698cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay"; 3708cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target"; 3718cfa4c44Slinyuny 3728cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const 3738cfa4c44Slinyuny { 3748cfa4c44Slinyuny // Set time delay 3758cfa4c44Slinyuny std::ofstream ofs; 3768cfa4c44Slinyuny ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out); 3778cfa4c44Slinyuny if (!ofs.good()) 3788cfa4c44Slinyuny { 3798cfa4c44Slinyuny std::fprintf(stderr, "Unable to open file for output.\n"); 380e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3818cfa4c44Slinyuny } 3828cfa4c44Slinyuny 3838cfa4c44Slinyuny ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl; 3848cfa4c44Slinyuny ofs.close(); 3858cfa4c44Slinyuny if (ofs.fail()) 3868cfa4c44Slinyuny { 3878cfa4c44Slinyuny std::fprintf(stderr, "Write failed\n"); 388e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3898cfa4c44Slinyuny } 3908cfa4c44Slinyuny 3918cfa4c44Slinyuny // Write succeeded, please continue. 3928cfa4c44Slinyuny auto bus = sdbusplus::bus::new_default(); 3938cfa4c44Slinyuny auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 3948cfa4c44Slinyuny SYSTEMD_INTERFACE, "StartUnit"); 3958cfa4c44Slinyuny 3968cfa4c44Slinyuny method.append(HOST_POWEROFF_TARGET); 3978cfa4c44Slinyuny method.append("replace"); 3988cfa4c44Slinyuny 3998cfa4c44Slinyuny try 4008cfa4c44Slinyuny { 4018cfa4c44Slinyuny bus.call_noreply(method); 4028cfa4c44Slinyuny } 4038cfa4c44Slinyuny catch (const sdbusplus::exception::SdBusError& ex) 4048cfa4c44Slinyuny { 4058cfa4c44Slinyuny log<level::ERR>("Failed to call Power Off", 4068cfa4c44Slinyuny entry("WHAT=%s", ex.what())); 407e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 4088cfa4c44Slinyuny } 4098cfa4c44Slinyuny } 4108cfa4c44Slinyuny 411ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance, 412ab650004SPatrick Venture const Json& config) 41307f85150SPatrick Venture { 41407f85150SPatrick Venture static const std::vector<Json> empty{}; 41507f85150SPatrick Venture std::vector<Json> readings = config.value(type, empty); 41607f85150SPatrick Venture std::string name = ""; 417ab650004SPatrick Venture 41807f85150SPatrick Venture for (const auto& j : readings) 41907f85150SPatrick Venture { 42007f85150SPatrick Venture uint8_t instanceNum = j.value("instance", 0); 42107f85150SPatrick Venture // Not the instance we're interested in 42207f85150SPatrick Venture if (instanceNum != instance) 42307f85150SPatrick Venture { 42407f85150SPatrick Venture continue; 42507f85150SPatrick Venture } 42607f85150SPatrick Venture 42707f85150SPatrick Venture // Found the instance we're interested in 42807f85150SPatrick Venture name = j.value("name", ""); 42907f85150SPatrick Venture 43007f85150SPatrick Venture break; 43107f85150SPatrick Venture } 432ab650004SPatrick Venture 43307f85150SPatrick Venture return name; 43407f85150SPatrick Venture } 43507f85150SPatrick Venture 43649f23ad9SPatrick Venture void Handler::buildI2cPcieMapping() 43749f23ad9SPatrick Venture { 43849f23ad9SPatrick Venture _pcie_i2c_map = buildPcieMap(); 43949f23ad9SPatrick Venture } 44049f23ad9SPatrick Venture 44149f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const 44249f23ad9SPatrick Venture { 44349f23ad9SPatrick Venture return _pcie_i2c_map.size(); 44449f23ad9SPatrick Venture } 44549f23ad9SPatrick Venture 44649f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string> 44749f23ad9SPatrick Venture Handler::getI2cEntry(unsigned int entry) const 44849f23ad9SPatrick Venture { 44949f23ad9SPatrick Venture return _pcie_i2c_map[entry]; 45049f23ad9SPatrick Venture } 45149f23ad9SPatrick Venture 4524f0d1de6SSteve Foreman namespace 4534f0d1de6SSteve Foreman { 4544f0d1de6SSteve Foreman 4554f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/"; 4564f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel"; 4574f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR"; 4584f0d1de6SSteve Foreman 4594f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects() 4604f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>; 4614f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>; 4624f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists = 4634f0d1de6SSteve Foreman std::vector<std::pair<std::string, AnyTypeList>>; 4644f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector< 4654f0d1de6SSteve Foreman std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>; 4664f0d1de6SSteve Foreman 4674f0d1de6SSteve Foreman } // namespace 4684f0d1de6SSteve Foreman 46965371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const 4704f0d1de6SSteve Foreman { 4714f0d1de6SSteve Foreman return sdbusplus::bus::new_default(); 4724f0d1de6SSteve Foreman } 4734f0d1de6SSteve Foreman 474*15d4d21cSHao Zhou const std::unique_ptr<FileSystemInterface>& Handler::getFs() const 475*15d4d21cSHao Zhou { 476*15d4d21cSHao Zhou return this->fsPtr; 477*15d4d21cSHao Zhou } 478*15d4d21cSHao Zhou 4794f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const 4804f0d1de6SSteve Foreman { 4814f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 4824f0d1de6SSteve Foreman 4834f0d1de6SSteve Foreman try 4844f0d1de6SSteve Foreman { 4850e928ac6SMichael Shen auto bus = getDbus(); 4864f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 4874f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 4884f0d1de6SSteve Foreman "GetManagedObjects"); 4894f0d1de6SSteve Foreman bus.call(method).read(data); 4904f0d1de6SSteve Foreman } 4914f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 4924f0d1de6SSteve Foreman { 4934f0d1de6SSteve Foreman log<level::ERR>( 4944f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 4954f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 496e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 4974f0d1de6SSteve Foreman } 4984f0d1de6SSteve Foreman 4994f0d1de6SSteve Foreman return data.size(); 5004f0d1de6SSteve Foreman } 5014f0d1de6SSteve Foreman 5024f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const 5034f0d1de6SSteve Foreman { 5044f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 5054f0d1de6SSteve Foreman 5064f0d1de6SSteve Foreman try 5074f0d1de6SSteve Foreman { 5080e928ac6SMichael Shen auto bus = getDbus(); 5094f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 5104f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 5114f0d1de6SSteve Foreman "GetManagedObjects"); 5124f0d1de6SSteve Foreman bus.call(method).read(data); 5134f0d1de6SSteve Foreman } 5144f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 5154f0d1de6SSteve Foreman { 5164f0d1de6SSteve Foreman log<level::ERR>( 5174f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 5184f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 519e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5204f0d1de6SSteve Foreman } 5214f0d1de6SSteve Foreman 5224f0d1de6SSteve Foreman if (index >= data.size()) 5234f0d1de6SSteve Foreman { 5244f0d1de6SSteve Foreman log<level::WARNING>( 5254f0d1de6SSteve Foreman "Requested index is larger than the number of entries.", 5264f0d1de6SSteve Foreman entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size())); 527e5a06675SMichael Shen throw IpmiException(::ipmi::ccParmOutOfRange); 5284f0d1de6SSteve Foreman } 5294f0d1de6SSteve Foreman 5304f0d1de6SSteve Foreman std::string_view name(data[index].first.str); 5314f0d1de6SSteve Foreman if (!name.starts_with(ACCEL_OOB_ROOT)) 5324f0d1de6SSteve Foreman { 533e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidCommand); 5344f0d1de6SSteve Foreman } 5354f0d1de6SSteve Foreman name.remove_prefix(ACCEL_OOB_ROOT.length()); 5364f0d1de6SSteve Foreman return std::string(name); 5374f0d1de6SSteve Foreman } 5384f0d1de6SSteve Foreman 5394f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address, 5404f0d1de6SSteve Foreman uint8_t num_bytes) const 5414f0d1de6SSteve Foreman { 5424f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_METHOD[] = "Read"; 5434f0d1de6SSteve Foreman 5444f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 5454f0d1de6SSteve Foreman object_name.append(name); 5464f0d1de6SSteve Foreman 5470e928ac6SMichael Shen auto bus = getDbus(); 5484f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 5494f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD); 5504f0d1de6SSteve Foreman method.append(address, static_cast<uint64_t>(num_bytes)); 5514f0d1de6SSteve Foreman 5524f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 5534f0d1de6SSteve Foreman 5544f0d1de6SSteve Foreman try 5554f0d1de6SSteve Foreman { 5564f0d1de6SSteve Foreman bus.call(method).read(bytes); 5574f0d1de6SSteve Foreman } 5584f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 5594f0d1de6SSteve Foreman { 5604f0d1de6SSteve Foreman log<level::ERR>("Failed to call Read on com.google.custom_accel", 5614f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 5624f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5634f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5644f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5654f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5664f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5674f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes)); 568e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5694f0d1de6SSteve Foreman } 5704f0d1de6SSteve Foreman 5714f0d1de6SSteve Foreman if (bytes.size() < num_bytes) 5724f0d1de6SSteve Foreman { 5734f0d1de6SSteve Foreman log<level::ERR>( 5744f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel didn't return the expected" 5754f0d1de6SSteve Foreman " number of bytes.", 5764f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5774f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5784f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5794f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5804f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5814f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 5824f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 583e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5844f0d1de6SSteve Foreman } 5854f0d1de6SSteve Foreman 5864f0d1de6SSteve Foreman if (bytes.size() > sizeof(uint64_t)) 5874f0d1de6SSteve Foreman { 5884f0d1de6SSteve Foreman log<level::ERR>( 5894f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel returned more than 8B.", 5904f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5914f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5924f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5934f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5944f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5954f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 5964f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 597e5a06675SMichael Shen throw IpmiException(::ipmi::ccReqDataTruncated); 5984f0d1de6SSteve Foreman } 5994f0d1de6SSteve Foreman 6004f0d1de6SSteve Foreman uint64_t data = 0; 6014f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 6024f0d1de6SSteve Foreman { 6034f0d1de6SSteve Foreman data = (data << 8) | bytes[i]; 6044f0d1de6SSteve Foreman } 6054f0d1de6SSteve Foreman 6064f0d1de6SSteve Foreman return data; 6074f0d1de6SSteve Foreman } 6084f0d1de6SSteve Foreman 6094f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address, 6104f0d1de6SSteve Foreman uint8_t num_bytes, uint64_t data) const 6114f0d1de6SSteve Foreman { 6124f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_METHOD = "Write"; 6134f0d1de6SSteve Foreman 6144f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 6154f0d1de6SSteve Foreman object_name.append(name); 6164f0d1de6SSteve Foreman 6174f0d1de6SSteve Foreman if (num_bytes > sizeof(data)) 6184f0d1de6SSteve Foreman { 6194f0d1de6SSteve Foreman log<level::ERR>( 6204f0d1de6SSteve Foreman "Call to Write on com.google.custom_accel requested more than 8B.", 6214f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 6224f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 6234f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 6244f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 6254f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 6264f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 6274f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 628e5a06675SMichael Shen throw IpmiException(::ipmi::ccParmOutOfRange); 6294f0d1de6SSteve Foreman } 6304f0d1de6SSteve Foreman 6314f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 6324f0d1de6SSteve Foreman bytes.reserve(num_bytes); 6334f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 6344f0d1de6SSteve Foreman { 6354f0d1de6SSteve Foreman bytes.emplace_back(data & 0xff); 6364f0d1de6SSteve Foreman data >>= 8; 6374f0d1de6SSteve Foreman } 6384f0d1de6SSteve Foreman 6394f0d1de6SSteve Foreman try 6404f0d1de6SSteve Foreman { 6410e928ac6SMichael Shen auto bus = getDbus(); 6424f0d1de6SSteve Foreman auto method = 6434f0d1de6SSteve Foreman bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 6444f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data()); 6454f0d1de6SSteve Foreman method.append(address, bytes); 6464f0d1de6SSteve Foreman bus.call_noreply(method); 6474f0d1de6SSteve Foreman } 6484f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 6494f0d1de6SSteve Foreman { 6504f0d1de6SSteve Foreman log<level::ERR>("Failed to call Write on com.google.custom_accel", 6514f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 6524f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 6534f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 6544f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 6554f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 6564f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 6574f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 6584f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 659e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 6604f0d1de6SSteve Foreman } 6614f0d1de6SSteve Foreman } 6624f0d1de6SSteve Foreman 6636c71b0f9SWilly Tu std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index) 6646c71b0f9SWilly Tu { 6656c71b0f9SWilly Tu return bifurcationHelper.get().getBifurcation(index).value_or( 6666c71b0f9SWilly Tu std::vector<uint8_t>{}); 6676c71b0f9SWilly Tu } 6686c71b0f9SWilly Tu 669a92d0e6bSJohn Wedig static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target"; 670a92d0e6bSJohn Wedig 671a92d0e6bSJohn Wedig void Handler::linuxBootDone() const 672a92d0e6bSJohn Wedig { 673*15d4d21cSHao Zhou if (isBmcInBareMetalMode(this->fsPtr) != 674*15d4d21cSHao Zhou static_cast<uint8_t>(BmcMode::BM_MODE)) 675a92d0e6bSJohn Wedig { 676a92d0e6bSJohn Wedig return; 677a92d0e6bSJohn Wedig } 678a92d0e6bSJohn Wedig 679a92d0e6bSJohn Wedig log<level::INFO>("LinuxBootDone: Disabling IPMI"); 680a92d0e6bSJohn Wedig 681a92d0e6bSJohn Wedig // Start the bare metal active systemd target. 682a92d0e6bSJohn Wedig auto bus = sdbusplus::bus::new_default(); 683a92d0e6bSJohn Wedig auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 684a92d0e6bSJohn Wedig SYSTEMD_INTERFACE, "StartUnit"); 685a92d0e6bSJohn Wedig 686a92d0e6bSJohn Wedig method.append(BARE_METAL_TARGET); 687a92d0e6bSJohn Wedig method.append("replace"); 688a92d0e6bSJohn Wedig 689a92d0e6bSJohn Wedig try 690a92d0e6bSJohn Wedig { 691a92d0e6bSJohn Wedig bus.call_noreply(method); 692a92d0e6bSJohn Wedig } 693a92d0e6bSJohn Wedig catch (const sdbusplus::exception::SdBusError& ex) 694a92d0e6bSJohn Wedig { 695a92d0e6bSJohn Wedig log<level::ERR>("Failed to start bare metal active systemd target", 696a92d0e6bSJohn Wedig entry("WHAT=%s", ex.what())); 697a92d0e6bSJohn Wedig throw IpmiException(::ipmi::ccUnspecifiedError); 698a92d0e6bSJohn Wedig } 699a92d0e6bSJohn Wedig } 700a92d0e6bSJohn Wedig 701f085d91dSPatrick Venture } // namespace ipmi 702f085d91dSPatrick Venture } // namespace google 703