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