1 #include "config.h"
2 
3 #include "systemintfcmds.hpp"
4 
5 #include "host-cmd-manager.hpp"
6 #include "host-interface.hpp"
7 
8 #include <ipmid-host/cmd.hpp>
9 #include <ipmid/api.hpp>
10 
11 #include <cstring>
12 
13 void register_netfn_app_functions() __attribute__((constructor));
14 
15 using namespace sdbusplus::xyz::openbmc_project::Control::server;
16 
17 // For accessing Host command manager
18 using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>;
19 extern cmdManagerPtr& ipmid_get_host_cmd_manager();
20 
21 //-------------------------------------------------------------------
22 // Called by Host post response from Get_Message_Flags
23 //-------------------------------------------------------------------
24 ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t,
25                                ipmi_response_t response,
26                                ipmi_data_len_t data_len, ipmi_context_t)
27 {
28     ipmi_ret_t rc = IPMI_CC_OK;
29 
30     struct oem_sel_timestamped oem_sel = {};
31     *data_len = sizeof(struct oem_sel_timestamped);
32 
33     // either id[0] -or- id[1] can be filled in. We will use id[0]
34     oem_sel.id[0] = SEL_OEM_ID_0;
35     oem_sel.id[1] = SEL_OEM_ID_0;
36     oem_sel.type = SEL_RECORD_TYPE_OEM;
37 
38     // Following 3 bytes are from IANA Manufactre_Id field. See below
39     oem_sel.manuf_id[0] = 0x41;
40     oem_sel.manuf_id[1] = 0xA7;
41     oem_sel.manuf_id[2] = 0x00;
42 
43     // per IPMI spec NetFuntion for OEM
44     oem_sel.netfun = 0x3A;
45 
46     // Read from the Command Manager queue. What gets returned is a
47     // pair of <command, data> that can be directly used here
48     auto hostCmd = ipmid_get_host_cmd_manager()->getNextCommand();
49     oem_sel.cmd = hostCmd.first;
50     oem_sel.data[0] = hostCmd.second;
51 
52     // All '0xFF' since unused.
53     std::memset(&oem_sel.data[1], 0xFF, 3);
54 
55     // Pack the actual response
56     std::memcpy(response, &oem_sel, *data_len);
57     return rc;
58 }
59 
60 //---------------------------------------------------------------------
61 // Called by Host on seeing a SMS_ATN bit set. Return a hardcoded
62 // value of 0x0 to indicate Event Message Buffer is not supported
63 //-------------------------------------------------------------------
64 ipmi::RspType<uint8_t> ipmiAppGetMessageFlags()
65 {
66     // From IPMI spec V2.0 for Get Message Flags Command :
67     // bit:[1] from LSB : 1b = Event Message Buffer Full.
68     // Return as 0 if Event Message Buffer is not supported,
69     // or when the Event Message buffer is disabled.
70     // This path is used to communicate messages to the host
71     // from within the phosphor::host::command::Manager
72     constexpr uint8_t setEventMsgBufferNotSupported = 0x0;
73     return ipmi::responseSuccess(setEventMsgBufferNotSupported);
74 }
75 
76 ipmi::RspType<bool,    // Receive Message Queue Interrupt Enabled
77               bool,    // Event Message Buffer Full Interrupt Enabled
78               bool,    // Event Message Buffer Enabled
79               bool,    // System Event Logging Enabled
80               uint1_t, // Reserved
81               bool,    // OEM 0 enabled
82               bool,    // OEM 1 enabled
83               bool     // OEM 2 enabled
84               >
85     ipmiAppGetBMCGlobalEnable()
86 {
87     return ipmi::responseSuccess(true, false, false, true, 0, false, false,
88                                  false);
89 }
90 
91 ipmi::RspType<> ipmiAppSetBMCGlobalEnable(
92     ipmi::Context::ptr ctx, bool receiveMessageQueueInterruptEnabled,
93     bool eventMessageBufferFullInterruptEnabled, bool eventMessageBufferEnabled,
94     bool systemEventLogEnable, uint1_t reserved, bool OEM0Enabled,
95     bool OEM1Enabled, bool OEM2Enabled)
96 {
97     ipmi::ChannelInfo chInfo;
98 
99     if (ipmi::getChannelInfo(ctx->channel, chInfo) != ipmi::ccSuccess)
100     {
101         phosphor::logging::log<phosphor::logging::level::ERR>(
102             "Failed to get Channel Info",
103             phosphor::logging::entry("CHANNEL=%d", ctx->channel));
104         return ipmi::responseUnspecifiedError();
105     }
106 
107     if (chInfo.mediumType !=
108         static_cast<uint8_t>(ipmi::EChannelMediumType::systemInterface))
109     {
110         phosphor::logging::log<phosphor::logging::level::ERR>(
111             "Error - supported only in system interface");
112         return ipmi::responseCommandNotAvailable();
113     }
114 
115     // Recv Message Queue and SEL are enabled by default.
116     // Event Message buffer are disabled by default (not supported).
117     // Any request that try to change the mask will be rejected
118     if (!receiveMessageQueueInterruptEnabled || !systemEventLogEnable ||
119         eventMessageBufferFullInterruptEnabled || eventMessageBufferEnabled ||
120         OEM0Enabled || OEM1Enabled || OEM2Enabled || reserved)
121     {
122         return ipmi::responseInvalidFieldRequest();
123     }
124 
125     return ipmi::responseSuccess();
126 }
127 
128 namespace
129 {
130 // Static storage to keep the object alive during process life
131 std::unique_ptr<phosphor::host::command::Host> host
132     __attribute__((init_priority(101)));
133 std::unique_ptr<sdbusplus::server::manager_t> objManager
134     __attribute__((init_priority(101)));
135 } // namespace
136 
137 void register_netfn_app_functions()
138 {
139     // <Read Event Message Buffer>
140     ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL,
141                            ipmi_app_read_event, SYSTEM_INTERFACE);
142 
143     // <Set BMC Global Enables>
144     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
145                           ipmi::app::cmdSetBmcGlobalEnables,
146                           ipmi::Privilege::Admin, ipmiAppSetBMCGlobalEnable);
147 
148     // <Get BMC Global Enables>
149     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
150                           ipmi::app::cmdGetBmcGlobalEnables,
151                           ipmi::Privilege::User, ipmiAppGetBMCGlobalEnable);
152 
153     // <Get Message Flags>
154     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
155                           ipmi::app::cmdGetMessageFlags, ipmi::Privilege::Admin,
156                           ipmiAppGetMessageFlags);
157 
158     // Create new xyz.openbmc_project.host object on the bus
159     auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
160 
161     std::unique_ptr<sdbusplus::asio::connection>& sdbusp =
162         ipmid_get_sdbus_plus_handler();
163 
164     // Add sdbusplus ObjectManager.
165     objManager = std::make_unique<sdbusplus::server::manager_t>(
166         *sdbusp, CONTROL_HOST_OBJ_MGR);
167 
168     host = std::make_unique<phosphor::host::command::Host>(*sdbusp,
169                                                            objPath.c_str());
170     sdbusp->request_name(CONTROL_HOST_BUSNAME);
171 
172     return;
173 }
174