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