xref: /openbmc/google-ipmi-sys/ipmi.cpp (revision ac730af2)
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ipmi.hpp"
18 
19 #include "cable.hpp"
20 #include "commands.hpp"
21 #include "cpld.hpp"
22 #include "entity_name.hpp"
23 #include "eth.hpp"
24 #include "flash_size.hpp"
25 #include "handler.hpp"
26 #include "host_power_off.hpp"
27 #include "machine_name.hpp"
28 #include "pcie_i2c.hpp"
29 #include "psu.hpp"
30 
31 #include <ipmid/api.h>
32 
33 #include <cstdint>
34 #include <cstdio>
35 
36 namespace google
37 {
38 namespace ipmi
39 {
40 
41 ipmi_ret_t handleSysCommand(HandlerInterface* handler, ipmi_cmd_t,
42                             const uint8_t* reqBuf, uint8_t* replyCmdBuf,
43                             size_t* dataLen)
44 {
45     // Verify it's at least as long as it needs to be for a subcommand.
46     if ((*dataLen) < 1)
47     {
48         std::fprintf(stderr, "*dataLen too small: %u\n",
49                      static_cast<uint32_t>(*dataLen));
50         return IPMI_CC_REQ_DATA_LEN_INVALID;
51     }
52 
53     switch (reqBuf[0])
54     {
55         case SysCableCheck:
56             return cableCheck(reqBuf, replyCmdBuf, dataLen, handler);
57         case SysCpldVersion:
58             return cpldVersion(reqBuf, replyCmdBuf, dataLen, handler);
59         case SysGetEthDevice:
60             return getEthDevice(reqBuf, replyCmdBuf, dataLen, handler);
61         case SysPsuHardReset:
62             return psuHardReset(reqBuf, replyCmdBuf, dataLen, handler);
63         case SysPcieSlotCount:
64             return pcieSlotCount(reqBuf, replyCmdBuf, dataLen, handler);
65         case SysPcieSlotI2cBusMapping:
66             return pcieSlotI2cBusMapping(reqBuf, replyCmdBuf, dataLen, handler);
67         case SysEntityName:
68             return getEntityName(reqBuf, replyCmdBuf, dataLen, handler);
69         case SysMachineName:
70             return getMachineName(reqBuf, replyCmdBuf, dataLen, handler);
71         case SysPsuHardResetOnShutdown:
72             return psuHardResetOnShutdown(reqBuf, replyCmdBuf, dataLen,
73                                           handler);
74         case SysGetFlashSize:
75             return getFlashSize(reqBuf, replyCmdBuf, dataLen, handler);
76         case SysHostPowerOff:
77             return hostPowerOff(reqBuf, replyCmdBuf, dataLen, handler);
78         default:
79             std::fprintf(stderr, "Invalid subcommand: 0x%x\n", reqBuf[0]);
80             return IPMI_CC_INVALID;
81     }
82 }
83 
84 } // namespace ipmi
85 } // namespace google
86