blob: e4c59fc1616555e39e98aaf4b2c6d0af81cc35ee [file] [log] [blame]
Patrick Venture123b5c02019-03-05 14:01:00 -08001/*
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_handler.hpp"
18
19#include "ipmi_errors.hpp"
20
21#include <fcntl.h>
22#include <linux/ipmi.h>
23#include <linux/ipmi_msgdefs.h>
24#include <sys/ioctl.h>
25
26#include <array>
27#include <cstdint>
28#include <cstring>
29#include <sstream>
30#include <string>
31#include <vector>
32
Patrick Venture1470bec2019-03-06 07:33:12 -080033namespace ipmiblob
Patrick Venture123b5c02019-03-05 14:01:00 -080034{
35
36void IpmiHandler::open()
37{
38 const int device = 0;
39 const std::vector<std::string> formats = {"/dev/ipmi", "/dev/ipmi/",
40 "/dev/ipmidev/"};
41
42 for (const auto& format : formats)
43 {
44 std::ostringstream path;
45 path << format << device;
46
47 fd = sys->open(path.str().c_str(), O_RDWR);
48 if (fd < 0)
49 {
50 continue;
51 }
52 break;
53 }
54
55 if (fd < 0)
56 {
57 throw IpmiException("Unable to open any ipmi devices");
58 }
59}
60
61std::vector<std::uint8_t>
62 IpmiHandler::sendPacket(std::vector<std::uint8_t>& data)
63{
64 if (fd < 0)
65 {
66 open();
67 }
68
69 constexpr int ipmiOEMNetFn = 46;
70 constexpr int ipmiOEMLun = 0;
71 /* /openbmc/phosphor-host-ipmid/blob/master/host-ipmid/oemopenbmc.hpp */
72 constexpr int ipmiOEMBlobCmd = 128;
73 constexpr int fifteenMs = 15 * 1000;
74 constexpr int ipmiReadTimeout = fifteenMs;
75 constexpr int ipmiResponseBufferLen = IPMI_MAX_MSG_LENGTH;
76 constexpr int ipmiOk = 0;
77
78 /* We have a handle to the IPMI device. */
79 std::array<std::uint8_t, ipmiResponseBufferLen> responseBuffer;
80
81 /* Build address. */
82 struct ipmi_system_interface_addr systemAddress;
83 systemAddress.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
84 systemAddress.channel = IPMI_BMC_CHANNEL;
85 systemAddress.lun = ipmiOEMLun;
86
87 /* Build request. */
88 struct ipmi_req request;
89 std::memset(&request, 0, sizeof(request));
90 request.addr = reinterpret_cast<unsigned char*>(&systemAddress);
91 request.addr_len = sizeof(systemAddress);
92 request.msgid = sequence++;
93 request.msg.data = reinterpret_cast<unsigned char*>(data.data());
94 request.msg.data_len = data.size();
95 request.msg.netfn = ipmiOEMNetFn;
96 request.msg.cmd = ipmiOEMBlobCmd;
97
98 struct ipmi_recv reply;
99 reply.addr = reinterpret_cast<unsigned char*>(&systemAddress);
100 reply.addr_len = sizeof(systemAddress);
101 reply.msg.data = reinterpret_cast<unsigned char*>(responseBuffer.data());
102 reply.msg.data_len = responseBuffer.size();
103
104 /* Try to send request. */
105 int rc = sys->ioctl(fd, IPMICTL_SEND_COMMAND, &request);
106 if (rc < 0)
107 {
108 throw IpmiException("Unable to send IPMI request.");
109 }
110
111 /* Could use sdeventplus, but for only one type of event is it worth it? */
112 struct pollfd pfd;
113 pfd.fd = fd;
114 pfd.events = POLLIN;
115
116 do
117 {
118 rc = sys->poll(&pfd, 1, ipmiReadTimeout);
119 if (rc < 0)
120 {
121 if (errno == EINTR)
122 {
123 continue;
124 }
125 throw IpmiException("Error occurred.");
126 }
127 else if (rc == 0)
128 {
129 throw IpmiException("Timeout waiting for reply.");
130 }
131
132 /* Yay, happy case! */
133 rc = sys->ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &reply);
134 if (rc < 0)
135 {
136 throw IpmiException("Unable to read reply.");
137 }
138
139 if (request.msgid != reply.msgid)
140 {
141 std::fprintf(stderr, "Received wrong message, trying again.\n");
142 }
143 } while (request.msgid != reply.msgid);
144
145 if (responseBuffer[0] != ipmiOk)
146 {
147 throw IpmiException(static_cast<int>(responseBuffer[0]));
148 }
149
150 std::vector<std::uint8_t> returning;
151 auto dataLen = reply.msg.data_len - 1;
152
153 returning.insert(returning.begin(), responseBuffer.begin() + 1,
154 responseBuffer.begin() + dataLen + 1);
155
156 for (const auto& byte : returning)
157 {
158 std::fprintf(stderr, "0x%02x ", byte);
159 }
160 std::fprintf(stderr, "\n");
161
162 return returning;
163}
164
Patrick Venture1470bec2019-03-06 07:33:12 -0800165} // namespace ipmiblob