1 #include "systemintfcmds.h"
2 #include "host-ipmid/ipmid-api.h"
3 #include "host-ipmid/ipmid-host-cmd.hpp"
4 #include "config.h"
5 #include "host-cmd-manager.hpp"
6 #include "host-interface.hpp"
7 
8 #include <stdio.h>
9 #include <mapper.h>
10 
11 void register_netfn_app_functions() __attribute__((constructor));
12 
13 using namespace sdbusplus::xyz::openbmc_project::Control::server;
14 
15 // For accessing Host command manager
16 using cmdManagerPtr = std::unique_ptr<phosphor::host::command::Manager>;
17 extern cmdManagerPtr& ipmid_get_host_cmd_manager();
18 
19 //-------------------------------------------------------------------
20 // Called by Host post response from Get_Message_Flags
21 //-------------------------------------------------------------------
22 ipmi_ret_t ipmi_app_read_event(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
23         ipmi_request_t request, ipmi_response_t response,
24         ipmi_data_len_t data_len, ipmi_context_t context)
25 {
26     ipmi_ret_t rc = IPMI_CC_OK;
27 
28     printf("IPMI APP READ EVENT command received\n");
29 
30     struct oem_sel_timestamped oem_sel = {0};
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     memset(&oem_sel.data[1], 0xFF, 3);
54 
55     // Pack the actual response
56     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 0x2 indicating we need Host read some data.
63 //-------------------------------------------------------------------
64 ipmi_ret_t ipmi_app_get_msg_flags(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
65                              ipmi_request_t request, ipmi_response_t response,
66                              ipmi_data_len_t data_len, ipmi_context_t context)
67 {
68     // Generic return from IPMI commands.
69     ipmi_ret_t rc = IPMI_CC_OK;
70 
71     printf("IPMI APP GET MSG FLAGS returning with [bit:2] set\n");
72 
73     // From IPMI spec V2.0 for Get Message Flags Command :
74     // bit:[1] from LSB : 1b = Event Message Buffer Full.
75     // Return as 0 if Event Message Buffer is not supported,
76     // or when the Event Message buffer is disabled.
77     // TODO. For now. assume its not disabled and send "0x2" anyway:
78 
79     uint8_t set_event_msg_buffer_full = 0x2;
80     *data_len = sizeof(set_event_msg_buffer_full);
81 
82     // Pack the actual response
83     memcpy(response, &set_event_msg_buffer_full, *data_len);
84 
85     return rc;
86 }
87 
88 ipmi_ret_t ipmi_app_set_bmc_global_enables(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
89                              ipmi_request_t request, ipmi_response_t response,
90                              ipmi_data_len_t data_len, ipmi_context_t context)
91 {
92     ipmi_ret_t rc = IPMI_CC_OK;
93     *data_len = 0;
94 
95     // Event and message logging enabled by default so return for now
96     printf("IPMI APP SET BMC GLOBAL ENABLES Ignoring for now\n");
97 
98     return rc;
99 }
100 
101 namespace {
102 // Static storage to keep the object alive during process life
103 std::unique_ptr<phosphor::host::command::Host> host
104                 __attribute__((init_priority(101)));
105 std::unique_ptr<sdbusplus::server::manager::manager> objManager
106                 __attribute__((init_priority(101)));
107 }
108 
109 #include <unistd.h>
110 void register_netfn_app_functions()
111 {
112 
113     // <Read Event Message Buffer>
114     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_READ_EVENT);
115     ipmi_register_callback(NETFUN_APP, IPMI_CMD_READ_EVENT, NULL, ipmi_app_read_event,
116                            SYSTEM_INTERFACE);
117 
118     // <Set BMC Global Enables>
119     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP,
120                                             IPMI_CMD_SET_BMC_GLOBAL_ENABLES);
121     ipmi_register_callback(NETFUN_APP, IPMI_CMD_SET_BMC_GLOBAL_ENABLES, NULL,
122                            ipmi_app_set_bmc_global_enables, SYSTEM_INTERFACE);
123 
124     // <Get Message Flags>
125     printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS);
126     ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_MSG_FLAGS, NULL, ipmi_app_get_msg_flags,
127                            SYSTEM_INTERFACE);
128 
129     // Create new xyz.openbmc_project.host object on the bus
130     auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
131 
132     // Add sdbusplus ObjectManager.
133     auto& sdbusPlusHandler = ipmid_get_sdbus_plus_handler();
134     objManager = std::make_unique<sdbusplus::server::manager::manager>(
135                         *sdbusPlusHandler,
136                          CONTROL_HOST_OBJ_MGR);
137 
138     host = std::make_unique<phosphor::host::command::Host>(
139                             *sdbusPlusHandler, objPath.c_str());
140     sdbusPlusHandler->request_name(CONTROL_HOST_BUSNAME);
141 
142     return;
143 }
144