blob: 0b9803652b0bf6a1da1b12e2c899211a365ba356 [file] [log] [blame]
Tom Josephbe5eaa12017-07-12 19:54:44 +05301#include "dcmihandler.hpp"
Patrick Williams37af7332016-09-02 21:21:42 -05002#include "host-ipmid/ipmid-api.h"
Tom Josephbe5eaa12017-07-12 19:54:44 +05303#include <phosphor-logging/elog-errors.hpp>
Chris Austen1810bec2015-10-13 12:12:39 -05004#include <stdio.h>
5#include <string.h>
6#include <stdint.h>
Tom Josephbe5eaa12017-07-12 19:54:44 +05307#include "utils.hpp"
8#include "xyz/openbmc_project/Common/error.hpp"
9
10using namespace phosphor::logging;
11using InternalFailure =
12 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austen1810bec2015-10-13 12:12:39 -050013
14void register_netfn_dcmi_functions() __attribute__((constructor));
15
16
17ipmi_ret_t ipmi_dcmi_get_power_limit(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
18 ipmi_request_t request, ipmi_response_t response,
19 ipmi_data_len_t data_len, ipmi_context_t context)
20{
21 ipmi_ret_t rc = IPMI_DCMI_CC_NO_ACTIVE_POWER_LIMIT;
22
23 // dcmi-v1-5-rev-spec.pdf 6.6.2.
24 // This is good enough for OpenBMC support for OpenPOWER based systems
25 // TODO research if more is needed
26 uint8_t data_response[] = { 0xDC, 0x00, 0x00, 0x01, 0x00, 0x00,
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28 0x00, 0x01};
29
30
31
32 printf("IPMI DCMI_GET_POWER_LEVEL\n");
33
34 memcpy(response, data_response, sizeof(data_response));
35 *data_len = sizeof(data_response);
36
37 return rc;
38}
39
Tom Josephbe5eaa12017-07-12 19:54:44 +053040namespace dcmi
41{
42
43void readAssetTagObjectTree(dcmi::assettag::ObjectTree& objectTree)
44{
45 static constexpr auto mapperBusName = "xyz.openbmc_project.ObjectMapper";
46 static constexpr auto mapperObjPath = "/xyz/openbmc_project/object_mapper";
47 static constexpr auto mapperIface = "xyz.openbmc_project.ObjectMapper";
48 static constexpr auto inventoryRoot = "/xyz/openbmc_project/inventory/";
49
50 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
51 auto depth = 0;
52
53 auto mapperCall = bus.new_method_call(mapperBusName,
54 mapperObjPath,
55 mapperIface,
56 "GetSubTree");
57
58 mapperCall.append(inventoryRoot);
59 mapperCall.append(depth);
60 mapperCall.append(std::vector<std::string>({dcmi::assetTagIntf}));
61
62 auto mapperReply = bus.call(mapperCall);
63 if (mapperReply.is_method_error())
64 {
65 log<level::ERR>("Error in mapper call");
66 elog<InternalFailure>();
67 }
68
69 mapperReply.read(objectTree);
70
71 if (objectTree.empty())
72 {
73 log<level::ERR>("AssetTag property is not populated");
74 elog<InternalFailure>();
75 }
76}
77
78std::string readAssetTag()
79{
80 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
81 dcmi::assettag::ObjectTree objectTree;
82
83 // Read the object tree with the inventory root to figure out the object
84 // that has implemented the Asset tag interface.
85 readAssetTagObjectTree(objectTree);
86
87 auto method = bus.new_method_call(
88 (objectTree.begin()->second.begin()->first).c_str(),
89 (objectTree.begin()->first).c_str(),
90 dcmi::propIntf,
91 "Get");
92 method.append(dcmi::assetTagIntf);
93 method.append(dcmi::assetTagProp);
94
95 auto reply = bus.call(method);
96 if (reply.is_method_error())
97 {
98 log<level::ERR>("Error in reading asset tag");
99 elog<InternalFailure>();
100 }
101
102 sdbusplus::message::variant<std::string> assetTag;
103 reply.read(assetTag);
104
105 return assetTag.get<std::string>();
106}
107
108} // namespace dcmi
Chris Austen1810bec2015-10-13 12:12:39 -0500109
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530110ipmi_ret_t getAssetTag(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
111 ipmi_request_t request, ipmi_response_t response,
112 ipmi_data_len_t data_len, ipmi_context_t context)
113{
114 auto requestData = reinterpret_cast<const dcmi::GetAssetTagRequest*>
115 (request);
116 std::vector<uint8_t> outPayload(sizeof(dcmi::GetAssetTagResponse));
117 auto responseData = reinterpret_cast<dcmi::GetAssetTagResponse*>
118 (outPayload.data());
119
120 if (requestData->groupID != dcmi::groupExtId)
121 {
122 *data_len = 0;
123 return IPMI_CC_INVALID_FIELD_REQUEST;
124 }
125
126 // Verify offset to read and number of bytes to read are not exceeding the
127 // range.
128 if ((requestData->offset > dcmi::assetTagMaxOffset) ||
129 (requestData->bytes > dcmi::maxBytes) ||
130 ((requestData->offset + requestData->bytes) > dcmi::assetTagMaxSize))
131 {
132 *data_len = 0;
133 return IPMI_CC_PARM_OUT_OF_RANGE;
134 }
135
136 std::string assetTag;
137
138 try
139 {
140 assetTag = dcmi::readAssetTag();
141 }
142 catch (InternalFailure& e)
143 {
144 *data_len = 0;
145 return IPMI_CC_UNSPECIFIED_ERROR;
146 }
147
148 responseData->groupID = dcmi::groupExtId;
149
150 // Return if the asset tag is not populated.
151 if (!assetTag.size())
152 {
153 responseData->tagLength = 0;
154 memcpy(response, outPayload.data(), outPayload.size());
155 *data_len = outPayload.size();
156 return IPMI_CC_OK;
157 }
158
159 // If the asset tag is longer than 63 bytes, restrict it to 63 bytes to suit
160 // Get Asset Tag command.
161 if (assetTag.size() > dcmi::assetTagMaxSize)
162 {
163 assetTag.resize(dcmi::assetTagMaxSize);
164 }
165
166 // If the requested offset is beyond the asset tag size.
167 if (requestData->offset >= assetTag.size())
168 {
169 *data_len = 0;
170 return IPMI_CC_PARM_OUT_OF_RANGE;
171 }
172
173 auto returnData = assetTag.substr(requestData->offset, requestData->bytes);
174
175 responseData->tagLength = assetTag.size();
176
177 memcpy(response, outPayload.data(), outPayload.size());
178 memcpy(static_cast<uint8_t*>(response) + outPayload.size(),
179 returnData.data(), returnData.size());
180 *data_len = outPayload.size() + returnData.size();
181
182 return IPMI_CC_OK;
183}
184
Tom Joseph545dd232017-07-12 20:20:49 +0530185ipmi_ret_t setAssetTag(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
186 ipmi_request_t request, ipmi_response_t response,
187 ipmi_data_len_t data_len, ipmi_context_t context)
188{
189 auto requestData = reinterpret_cast<const dcmi::SetAssetTagRequest*>
190 (request);
191 std::vector<uint8_t> outPayload(sizeof(dcmi::SetAssetTagResponse));
192 auto responseData = reinterpret_cast<dcmi::SetAssetTagResponse*>
193 (outPayload.data());
194
195 if (requestData->groupID != dcmi::groupExtId)
196 {
197 *data_len = 0;
198 return IPMI_CC_INVALID_FIELD_REQUEST;
199 }
200
201 // Verify offset to read and number of bytes to read are not exceeding the
202 // range.
203 if ((requestData->offset > dcmi::assetTagMaxOffset) ||
204 (requestData->bytes > dcmi::maxBytes) ||
205 ((requestData->offset + requestData->bytes) > dcmi::assetTagMaxSize))
206 {
207 *data_len = 0;
208 return IPMI_CC_PARM_OUT_OF_RANGE;
209 }
210
211 std::string assetTag;
212
213 try
214 {
215 assetTag = dcmi::readAssetTag();
216
217 if (requestData->offset > assetTag.size())
218 {
219 *data_len = 0;
220 return IPMI_CC_PARM_OUT_OF_RANGE;
221 }
222
223 assetTag.replace(requestData->offset,
224 assetTag.size() - requestData->offset,
225 static_cast<const char*>(request) +
226 sizeof(dcmi::SetAssetTagRequest),
227 requestData->bytes);
228
229 dcmi::writeAssetTag(assetTag);
230
231 responseData->groupID = dcmi::groupExtId;
232 responseData->tagLength = assetTag.size();
233 memcpy(response, outPayload.data(), outPayload.size());
234 *data_len = outPayload.size();
235
236 return IPMI_CC_OK;
237 }
238 catch (InternalFailure& e)
239 {
240 *data_len = 0;
241 return IPMI_CC_UNSPECIFIED_ERROR;
242 }
243}
244
Chris Austen1810bec2015-10-13 12:12:39 -0500245void register_netfn_dcmi_functions()
246{
Tom05732372016-09-06 17:21:23 +0530247 // <Get Power Limit>
Chris Austen1810bec2015-10-13 12:12:39 -0500248 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_GRPEXT, IPMI_CMD_DCMI_GET_POWER);
Tom05732372016-09-06 17:21:23 +0530249 ipmi_register_callback(NETFUN_GRPEXT, IPMI_CMD_DCMI_GET_POWER, NULL, ipmi_dcmi_get_power_limit,
250 PRIVILEGE_USER);
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530251
252 // <Get Asset Tag>
253 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_GRPEXT, IPMI_CMD_DCMI_GET_ASSET_TAG);
254 ipmi_register_callback(NETFUN_GRPEXT, IPMI_CMD_DCMI_GET_ASSET_TAG, NULL, getAssetTag,
255 PRIVILEGE_USER);
Tom Joseph545dd232017-07-12 20:20:49 +0530256
257 // <Set Asset Tag>
258 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_GRPEXT, IPMI_CMD_DCMI_SET_ASSET_TAG);
259 ipmi_register_callback(NETFUN_GRPEXT, IPMI_CMD_DCMI_SET_ASSET_TAG, NULL, setAssetTag,
260 PRIVILEGE_OPERATOR);
Chris Austen1810bec2015-10-13 12:12:39 -0500261 return;
262}
Tom05732372016-09-06 17:21:23 +0530263// 956379