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