1 #include "globalhandler.hpp"
2 
3 #include <ipmid/api.h>
4 
5 #include <ipmid/utils.hpp>
6 #include <phosphor-logging/elog-errors.hpp>
7 #include <phosphor-logging/log.hpp>
8 #include <string>
9 #include <xyz/openbmc_project/Common/error.hpp>
10 #include <xyz/openbmc_project/State/BMC/server.hpp>
11 
12 static constexpr auto bmcStateRoot = "/xyz/openbmc_project/state";
13 static constexpr auto bmcStateIntf = "xyz.openbmc_project.State.BMC";
14 static constexpr auto reqTransition = "RequestedBMCTransition";
15 static constexpr auto match = "bmc0";
16 
17 using namespace phosphor::logging;
18 using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
19 
20 void register_netfn_global_functions() __attribute__((constructor));
21 
22 void resetBMC()
23 {
24     sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
25 
26     auto bmcStateObj =
27         ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot, match);
28 
29     auto service = ipmi::getService(bus, bmcStateIntf, bmcStateObj.first);
30 
31     ipmi::setDbusProperty(bus, service, bmcStateObj.first, bmcStateIntf,
32                           reqTransition,
33                           convertForMessage(BMC::Transition::Reboot));
34 }
35 
36 ipmi_ret_t ipmi_global_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
37                              ipmi_request_t request, ipmi_response_t response,
38                              ipmi_data_len_t data_len, ipmi_context_t context)
39 {
40     try
41     {
42         resetBMC();
43     }
44     catch (std::exception& e)
45     {
46         log<level::ERR>(e.what());
47         return IPMI_CC_UNSPECIFIED_ERROR;
48     }
49 
50     // Status code.
51     ipmi_ret_t rc = IPMI_CC_OK;
52     *data_len = 0;
53     return rc;
54 }
55 
56 void register_netfn_global_functions()
57 {
58     // Cold Reset
59     ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL,
60                            ipmi_global_reset, PRIVILEGE_ADMIN);
61 
62     // <Warm Reset>
63     ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL,
64                            ipmi_global_reset, PRIVILEGE_ADMIN);
65 
66     return;
67 }
68