1 #include "config.h"
2 
3 #include "systemintfcmds.hpp"
4 
5 #include "host-cmd-manager.hpp"
6 #include "host-interface.hpp"
7 
8 #include <host-ipmid/ipmid-api.h>
9 #include <mapper.h>
10 #include <stdio.h>
11 
12 #include <host-ipmid/ipmid-host-cmd.hpp>
13 
14 void register_netfn_app_functions() __attribute__((constructor));
15 
16 using namespace sdbusplus::xyz::openbmc_project::Control::server;
17 
18 // For accessing Host command manager
19 using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>;
20 extern cmdManagerPtr& ipmid_get_host_cmd_manager();
21 
22 //-------------------------------------------------------------------
23 // Called by Host post response from Get_Message_Flags
24 //-------------------------------------------------------------------
25 ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
26                                ipmi_request_t request, ipmi_response_t response,
27                                ipmi_data_len_t data_len, ipmi_context_t context)
28 {
29     ipmi_ret_t rc = IPMI_CC_OK;
30 
31     struct oem_sel_timestamped oem_sel = {0};
32     *data_len = sizeof(struct oem_sel_timestamped);
33 
34     // either id[0] -or- id[1] can be filled in. We will use id[0]
35     oem_sel.id[0] = SEL_OEM_ID_0;
36     oem_sel.id[1] = SEL_OEM_ID_0;
37     oem_sel.type = SEL_RECORD_TYPE_OEM;
38 
39     // Following 3 bytes are from IANA Manufactre_Id field. See below
40     oem_sel.manuf_id[0] = 0x41;
41     oem_sel.manuf_id[1] = 0xA7;
42     oem_sel.manuf_id[2] = 0x00;
43 
44     // per IPMI spec NetFuntion for OEM
45     oem_sel.netfun = 0x3A;
46 
47     // Read from the Command Manager queue. What gets returned is a
48     // pair of <command, data> that can be directly used here
49     auto hostCmd = ipmid_get_host_cmd_manager()->getNextCommand();
50     oem_sel.cmd = hostCmd.first;
51     oem_sel.data[0] = hostCmd.second;
52 
53     // All '0xFF' since unused.
54     memset(&oem_sel.data[1], 0xFF, 3);
55 
56     // Pack the actual response
57     memcpy(response, &oem_sel, *data_len);
58     return rc;
59 }
60 
61 //---------------------------------------------------------------------
62 // Called by Host on seeing a SMS_ATN bit set. Return a hardcoded
63 // value of 0x2 indicating we need Host read some data.
64 //-------------------------------------------------------------------
65 ipmi_ret_t ipmi_app_get_msg_flags(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
66                                   ipmi_request_t request,
67                                   ipmi_response_t response,
68                                   ipmi_data_len_t data_len,
69                                   ipmi_context_t context)
70 {
71     // Generic return from IPMI commands.
72     ipmi_ret_t rc = IPMI_CC_OK;
73 
74     // From IPMI spec V2.0 for Get Message Flags Command :
75     // bit:[1] from LSB : 1b = Event Message Buffer Full.
76     // Return as 0 if Event Message Buffer is not supported,
77     // or when the Event Message buffer is disabled.
78     // TODO. For now. assume its not disabled and send "0x2" anyway:
79 
80     uint8_t set_event_msg_buffer_full = 0x2;
81     *data_len = sizeof(set_event_msg_buffer_full);
82 
83     // Pack the actual response
84     memcpy(response, &set_event_msg_buffer_full, *data_len);
85 
86     return rc;
87 }
88 
89 ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
90                                            ipmi_request_t request,
91                                            ipmi_response_t response,
92                                            ipmi_data_len_t data_len,
93                                            ipmi_context_t context)
94 {
95     ipmi_ret_t rc = IPMI_CC_OK;
96     *data_len = 0;
97 
98     // Event and message logging enabled by default so return for now
99 #ifdef __IPMI_DEBUG__
100     printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
101 #endif
102 
103     return rc;
104 }
105 
106 namespace
107 {
108 // Static storage to keep the object alive during process life
109 std::unique_ptr<phosphor::host::command::Host> host
110     __attribute__((init_priority(101)));
111 std::unique_ptr<sdbusplus::server::manager::manager> objManager
112     __attribute__((init_priority(101)));
113 } // namespace
114 
115 #include <unistd.h>
116 void register_netfn_app_functions()
117 {
118 
119     // <Read Event Message Buffer>
120     ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL,
121                            ipmi_app_read_event, SYSTEM_INTERFACE);
122 
123     // <Set BMC Global Enables>
124     ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
125                            ipmi_app_set_bmc_global_enables, SYSTEM_INTERFACE);
126 
127     // <Get Message Flags>
128     ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL,
129                            ipmi_app_get_msg_flags, SYSTEM_INTERFACE);
130 
131     // Create new xyz.openbmc_project.host object on the bus
132     auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
133 
134     // Add sdbusplus ObjectManager.
135     auto& sdbusPlusHandler = ipmid_get_sdbus_plus_handler();
136     objManager = std::make_unique<sdbusplus::server::manager::manager>(
137         *sdbusPlusHandler, CONTROL_HOST_OBJ_MGR);
138 
139     host = std::make_unique<phosphor::host::command::Host>(*sdbusPlusHandler,
140                                                            objPath.c_str());
141     sdbusPlusHandler->request_name(CONTROL_HOST_BUSNAME);
142 
143     return;
144 }
145