blob: c883682189531d84b38e5d83795d1917218defbb [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
2// Copyright (c) 2017 2018 Intel Corporation
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
Jason M. Bills6d9c83f2019-02-08 14:02:19 -080017#include <ipmid/api.h>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070018
19#include <boost/container/flat_map.hpp>
20#include <commandutils.hpp>
21#include <iostream>
22#include <phosphor-logging/log.hpp>
23#include <sdbusplus/message/types.hpp>
24#include <sdbusplus/timer.hpp>
25#include <storagecommands.hpp>
26
27namespace ipmi
28{
29
30namespace storage
31{
32
Jason M. Billse2d1aee2018-10-03 15:57:18 -070033constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070034constexpr static const size_t maxFruSdrNameSize = 16;
35using ManagedObjectType = boost::container::flat_map<
36 sdbusplus::message::object_path,
37 boost::container::flat_map<
38 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
39using ManagedEntry = std::pair<
40 sdbusplus::message::object_path,
41 boost::container::flat_map<
42 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
43
James Feist3bcba452018-12-20 12:31:03 -080044constexpr static const char* fruDeviceServiceName =
45 "xyz.openbmc_project.FruDevice";
Jason M. Billse2d1aee2018-10-03 15:57:18 -070046constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070047
48static std::vector<uint8_t> fruCache;
49static uint8_t cacheBus = 0xFF;
50static uint8_t cacheAddr = 0XFF;
51
52std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
53
54// we unfortunately have to build a map of hashes in case there is a
55// collision to verify our dev-id
56boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
57
Jason M. Billse2d1aee2018-10-03 15:57:18 -070058void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070059static sdbusplus::bus::bus dbus(ipmid_get_sd_bus_connection());
60
61bool writeFru()
62{
63 sdbusplus::message::message writeFru = dbus.new_method_call(
64 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
65 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
66 writeFru.append(cacheBus, cacheAddr, fruCache);
67 try
68 {
69 sdbusplus::message::message writeFruResp = dbus.call(writeFru);
70 }
71 catch (sdbusplus::exception_t&)
72 {
73 // todo: log sel?
74 phosphor::logging::log<phosphor::logging::level::ERR>(
75 "error writing fru");
76 return false;
77 }
78 return true;
79}
80
Jason M. Billse2d1aee2018-10-03 15:57:18 -070081void createTimer()
82{
83 if (cacheTimer == nullptr)
84 {
85 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
86 }
87}
88
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070089ipmi_ret_t replaceCacheFru(uint8_t devId)
90{
91 static uint8_t lastDevId = 0xFF;
92
93 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
94 if (lastDevId == devId && timerRunning)
95 {
96 return IPMI_CC_OK; // cache already up to date
97 }
98 // if timer is running, stop it and writeFru manually
99 else if (timerRunning)
100 {
101 cacheTimer->stop();
102 writeFru();
103 }
104
105 sdbusplus::message::message getObjects = dbus.new_method_call(
106 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
107 "GetManagedObjects");
108 ManagedObjectType frus;
109 try
110 {
111 sdbusplus::message::message resp = dbus.call(getObjects);
112 resp.read(frus);
113 }
114 catch (sdbusplus::exception_t&)
115 {
116 phosphor::logging::log<phosphor::logging::level::ERR>(
117 "replaceCacheFru: error getting managed objects");
118 return IPMI_CC_RESPONSE_ERROR;
119 }
120
121 deviceHashes.clear();
122
123 // hash the object paths to create unique device id's. increment on
124 // collision
125 std::hash<std::string> hasher;
126 for (const auto& fru : frus)
127 {
128 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
129 if (fruIface == fru.second.end())
130 {
131 continue;
132 }
133
134 auto busFind = fruIface->second.find("BUS");
135 auto addrFind = fruIface->second.find("ADDRESS");
136 if (busFind == fruIface->second.end() ||
137 addrFind == fruIface->second.end())
138 {
139 phosphor::logging::log<phosphor::logging::level::INFO>(
140 "fru device missing Bus or Address",
141 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
142 continue;
143 }
144
145 uint8_t fruBus =
146 sdbusplus::message::variant_ns::get<uint32_t>(busFind->second);
147 uint8_t fruAddr =
148 sdbusplus::message::variant_ns::get<uint32_t>(addrFind->second);
149
150 uint8_t fruHash = 0;
151 if (fruBus != 0 || fruAddr != 0)
152 {
153 fruHash = hasher(fru.first.str);
154 // can't be 0xFF based on spec, and 0 is reserved for baseboard
155 if (fruHash == 0 || fruHash == 0xFF)
156 {
157 fruHash = 1;
158 }
159 }
160 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
161
162 bool emplacePassed = false;
163 while (!emplacePassed)
164 {
165 auto resp = deviceHashes.emplace(fruHash, newDev);
166 emplacePassed = resp.second;
167 if (!emplacePassed)
168 {
169 fruHash++;
170 // can't be 0xFF based on spec, and 0 is reserved for
171 // baseboard
172 if (fruHash == 0XFF)
173 {
174 fruHash = 0x1;
175 }
176 }
177 }
178 }
179 auto deviceFind = deviceHashes.find(devId);
180 if (deviceFind == deviceHashes.end())
181 {
182 return IPMI_CC_SENSOR_INVALID;
183 }
184
185 fruCache.clear();
186 sdbusplus::message::message getRawFru = dbus.new_method_call(
187 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
188 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
189 cacheBus = deviceFind->second.first;
190 cacheAddr = deviceFind->second.second;
191 getRawFru.append(cacheBus, cacheAddr);
192 try
193 {
194 sdbusplus::message::message getRawResp = dbus.call(getRawFru);
195 getRawResp.read(fruCache);
196 }
197 catch (sdbusplus::exception_t&)
198 {
199 lastDevId = 0xFF;
200 cacheBus = 0xFF;
201 cacheAddr = 0xFF;
202 return IPMI_CC_RESPONSE_ERROR;
203 }
204
205 lastDevId = devId;
206 return IPMI_CC_OK;
207}
208
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700209ipmi_ret_t ipmiStorageReadFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
210 ipmi_request_t request,
211 ipmi_response_t response,
212 ipmi_data_len_t dataLen,
213 ipmi_context_t context)
214{
215 if (*dataLen != 4)
216 {
217 *dataLen = 0;
218 return IPMI_CC_REQ_DATA_LEN_INVALID;
219 }
220 *dataLen = 0; // default to 0 in case of an error
221
222 auto req = static_cast<GetFRUAreaReq*>(request);
223
224 if (req->countToRead > maxMessageSize - 1)
225 {
226 return IPMI_CC_INVALID_FIELD_REQUEST;
227 }
228 ipmi_ret_t status = replaceCacheFru(req->fruDeviceID);
229
230 if (status != IPMI_CC_OK)
231 {
232 return status;
233 }
234
235 size_t fromFRUByteLen = 0;
236 if (req->countToRead + req->fruInventoryOffset < fruCache.size())
237 {
238 fromFRUByteLen = req->countToRead;
239 }
240 else if (fruCache.size() > req->fruInventoryOffset)
241 {
242 fromFRUByteLen = fruCache.size() - req->fruInventoryOffset;
243 }
244 size_t padByteLen = req->countToRead - fromFRUByteLen;
245 uint8_t* respPtr = static_cast<uint8_t*>(response);
246 *respPtr = req->countToRead;
247 std::copy(fruCache.begin() + req->fruInventoryOffset,
248 fruCache.begin() + req->fruInventoryOffset + fromFRUByteLen,
249 ++respPtr);
250 // if longer than the fru is requested, fill with 0xFF
251 if (padByteLen)
252 {
253 respPtr += fromFRUByteLen;
254 std::fill(respPtr, respPtr + padByteLen, 0xFF);
255 }
256 *dataLen = fromFRUByteLen + 1;
257
258 return IPMI_CC_OK;
259}
260
261ipmi_ret_t ipmiStorageWriteFRUData(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
262 ipmi_request_t request,
263 ipmi_response_t response,
264 ipmi_data_len_t dataLen,
265 ipmi_context_t context)
266{
267 if (*dataLen < 4 ||
268 *dataLen >=
269 0xFF + 3) // count written return is one byte, so limit to one byte
270 // of data after the three request data bytes
271 {
272 *dataLen = 0;
273 return IPMI_CC_REQ_DATA_LEN_INVALID;
274 }
275
276 auto req = static_cast<WriteFRUDataReq*>(request);
277 size_t writeLen = *dataLen - 3;
278 *dataLen = 0; // default to 0 in case of an error
279
280 ipmi_ret_t status = replaceCacheFru(req->fruDeviceID);
281 if (status != IPMI_CC_OK)
282 {
283 return status;
284 }
285 int lastWriteAddr = req->fruInventoryOffset + writeLen;
286 if (fruCache.size() < lastWriteAddr)
287 {
288 fruCache.resize(req->fruInventoryOffset + writeLen);
289 }
290
291 std::copy(req->data, req->data + writeLen,
292 fruCache.begin() + req->fruInventoryOffset);
293
294 bool atEnd = false;
295
296 if (fruCache.size() >= sizeof(FRUHeader))
297 {
298
299 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
300
301 int lastRecordStart = std::max(
302 header->internalOffset,
303 std::max(header->chassisOffset,
304 std::max(header->boardOffset, header->productOffset)));
305 // TODO: Handle Multi-Record FRUs?
306
307 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
308
309 // get the length of the area in multiples of 8 bytes
310 if (lastWriteAddr > (lastRecordStart + 1))
311 {
312 // second byte in record area is the length
313 int areaLength(fruCache[lastRecordStart + 1]);
314 areaLength *= 8; // it is in multiples of 8 bytes
315
316 if (lastWriteAddr >= (areaLength + lastRecordStart))
317 {
318 atEnd = true;
319 }
320 }
321 }
322 uint8_t* respPtr = static_cast<uint8_t*>(response);
323 if (atEnd)
324 {
325 // cancel timer, we're at the end so might as well send it
326 cacheTimer->stop();
327 if (!writeFru())
328 {
329 return IPMI_CC_INVALID_FIELD_REQUEST;
330 }
331 *respPtr = std::min(fruCache.size(), static_cast<size_t>(0xFF));
332 }
333 else
334 {
335 // start a timer, if no further data is sent in cacheTimeoutSeconds
336 // seconds, check to see if it is valid
337 createTimer();
338 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
339 std::chrono::seconds(cacheTimeoutSeconds)));
340 *respPtr = 0;
341 }
342
343 *dataLen = 1;
344
345 return IPMI_CC_OK;
346}
347
348ipmi_ret_t ipmiStorageGetFRUInvAreaInfo(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
349 ipmi_request_t request,
350 ipmi_response_t response,
351 ipmi_data_len_t dataLen,
352 ipmi_context_t context)
353{
354 if (*dataLen != 1)
355 {
356 *dataLen = 0;
357 return IPMI_CC_REQ_DATA_LEN_INVALID;
358 }
359 *dataLen = 0; // default to 0 in case of an error
360
361 uint8_t reqDev = *(static_cast<uint8_t*>(request));
362 if (reqDev == 0xFF)
363 {
364 return IPMI_CC_INVALID_FIELD_REQUEST;
365 }
366 ipmi_ret_t status = replaceCacheFru(reqDev);
367
368 if (status != IPMI_CC_OK)
369 {
370 return status;
371 }
372
373 GetFRUAreaResp* respPtr = static_cast<GetFRUAreaResp*>(response);
374 respPtr->inventorySizeLSB = fruCache.size() & 0xFF;
375 respPtr->inventorySizeMSB = fruCache.size() >> 8;
376 respPtr->accessType = static_cast<uint8_t>(GetFRUAreaAccessType::byte);
377
378 *dataLen = sizeof(GetFRUAreaResp);
379 return IPMI_CC_OK;
380}
381
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700382ipmi_ret_t getFruSdrCount(size_t& count)
383{
384 ipmi_ret_t ret = replaceCacheFru(0);
385 if (ret != IPMI_CC_OK)
386 {
387 return ret;
388 }
389 count = deviceHashes.size();
390 return IPMI_CC_OK;
391}
392
393ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
394{
395 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
396 if (ret != IPMI_CC_OK)
397 {
398 return ret;
399 }
400 if (deviceHashes.size() < index)
401 {
402 return IPMI_CC_INVALID_FIELD_REQUEST;
403 }
404 auto device = deviceHashes.begin() + index;
405 uint8_t& bus = device->second.first;
406 uint8_t& address = device->second.second;
407
408 ManagedObjectType frus;
409
410 sdbusplus::message::message getObjects = dbus.new_method_call(
411 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
412 "GetManagedObjects");
413 try
414 {
415 sdbusplus::message::message resp = dbus.call(getObjects);
416 resp.read(frus);
417 }
418 catch (sdbusplus::exception_t&)
419 {
420 return IPMI_CC_RESPONSE_ERROR;
421 }
422 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
423 auto fru =
424 std::find_if(frus.begin(), frus.end(),
425 [bus, address, &fruData](ManagedEntry& entry) {
426 auto findFruDevice =
427 entry.second.find("xyz.openbmc_project.FruDevice");
428 if (findFruDevice == entry.second.end())
429 {
430 return false;
431 }
432 fruData = &(findFruDevice->second);
433 auto findBus = findFruDevice->second.find("BUS");
434 auto findAddress =
435 findFruDevice->second.find("ADDRESS");
436 if (findBus == findFruDevice->second.end() ||
437 findAddress == findFruDevice->second.end())
438 {
439 return false;
440 }
441 if (sdbusplus::message::variant_ns::get<uint32_t>(
442 findBus->second) != bus)
443 {
444 return false;
445 }
446 if (sdbusplus::message::variant_ns::get<uint32_t>(
447 findAddress->second) != address)
448 {
449 return false;
450 }
451 return true;
452 });
453 if (fru == frus.end())
454 {
455 return IPMI_CC_RESPONSE_ERROR;
456 }
457 std::string name;
458 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
459 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
460 if (findProductName != fruData->end())
461 {
462 name = sdbusplus::message::variant_ns::get<std::string>(
463 findProductName->second);
464 }
465 else if (findBoardName != fruData->end())
466 {
467 name = sdbusplus::message::variant_ns::get<std::string>(
468 findBoardName->second);
469 }
470 else
471 {
472 name = "UNKNOWN";
473 }
474 if (name.size() > maxFruSdrNameSize)
475 {
476 name = name.substr(0, maxFruSdrNameSize);
477 }
478 size_t sizeDiff = maxFruSdrNameSize - name.size();
479
480 resp.header.record_id_lsb = 0x0; // calling code is to implement these
481 resp.header.record_id_msb = 0x0;
482 resp.header.sdr_version = ipmiSdrVersion;
483 resp.header.record_type = 0x11; // FRU Device Locator
484 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
485 resp.key.deviceAddress = 0x20;
486 resp.key.fruID = device->first;
487 resp.key.accessLun = 0x80; // logical / physical fru device
488 resp.key.channelNumber = 0x0;
489 resp.body.reserved = 0x0;
490 resp.body.deviceType = 0x10;
491 resp.body.entityID = 0x0;
492 resp.body.entityInstance = 0x1;
493 resp.body.oem = 0x0;
494 resp.body.deviceIDLen = name.size();
495 name.copy(resp.body.deviceID, name.size());
496
497 return IPMI_CC_OK;
498}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700499
500void registerStorageFunctions()
501{
502 // <Get FRU Inventory Area Info>
503 ipmiPrintAndRegister(
504 NETFUN_STORAGE,
505 static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdGetFRUInvAreaInfo),
506 NULL, ipmiStorageGetFRUInvAreaInfo, PRIVILEGE_OPERATOR);
507
508 // <Add READ FRU Data
509 ipmiPrintAndRegister(
510 NETFUN_STORAGE,
511 static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdReadFRUData), NULL,
512 ipmiStorageReadFRUData, PRIVILEGE_OPERATOR);
513
514 // <Add WRITE FRU Data
515 ipmiPrintAndRegister(
516 NETFUN_STORAGE,
517 static_cast<ipmi_cmd_t>(IPMINetfnStorageCmds::ipmiCmdWriteFRUData),
518 NULL, ipmiStorageWriteFRUData, PRIVILEGE_OPERATOR);
519}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700520} // namespace storage
521} // namespace ipmi