blob: 54f5368760431696be82d50e8aa70ae6a01353b3 [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07002// Copyright (c) 2017-2019 Intel Corporation
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07003//
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. Bills1d4d54d2019-04-23 11:26:11 -070017#include <boost/algorithm/string.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070018#include <boost/container/flat_map.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080019#include <boost/process.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070020#include <commandutils.hpp>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070021#include <filesystem>
Patrick Venturee8767d22019-09-25 16:54:16 -070022#include <functional>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070023#include <iostream>
Jason M. Bills99b78ec2019-01-18 10:42:18 -080024#include <ipmi_to_redfish_hooks.hpp>
James Feist2a265d52019-04-08 11:16:27 -070025#include <ipmid/api.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080026#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070027#include <phosphor-logging/log.hpp>
28#include <sdbusplus/message/types.hpp>
29#include <sdbusplus/timer.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080030#include <sdrutils.hpp>
31#include <stdexcept>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070032#include <storagecommands.hpp>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070033#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070034
Patrick Venture9ce789f2019-10-17 09:09:39 -070035static constexpr bool DEBUG = false;
36
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070037namespace intel_oem::ipmi::sel
38{
39static const std::filesystem::path selLogDir = "/var/log";
40static const std::string selLogFilename = "ipmi_sel";
41
42static int getFileTimestamp(const std::filesystem::path& file)
43{
44 struct stat st;
45
46 if (stat(file.c_str(), &st) >= 0)
47 {
48 return st.st_mtime;
49 }
50 return ::ipmi::sel::invalidTimeStamp;
51}
52
53namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070054{
55static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
56
57void save()
58{
59 // open the file, creating it if necessary
60 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
61 if (fd < 0)
62 {
63 std::cerr << "Failed to open file\n";
64 return;
65 }
66
67 // update the file timestamp to the current time
68 if (futimens(fd, NULL) < 0)
69 {
70 std::cerr << "Failed to update timestamp: "
71 << std::string(strerror(errno));
72 }
73 close(fd);
74}
75
76int get()
77{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070078 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070079}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070080} // namespace erase_time
81} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070082
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070083namespace ipmi
84{
85
86namespace storage
87{
88
Jason M. Billse2d1aee2018-10-03 15:57:18 -070089constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070090constexpr static const size_t maxFruSdrNameSize = 16;
91using ManagedObjectType = boost::container::flat_map<
92 sdbusplus::message::object_path,
93 boost::container::flat_map<
94 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
95using ManagedEntry = std::pair<
96 sdbusplus::message::object_path,
97 boost::container::flat_map<
98 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
99
James Feist3bcba452018-12-20 12:31:03 -0800100constexpr static const char* fruDeviceServiceName =
101 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700102constexpr static const char* entityManagerServiceName =
103 "xyz.openbmc_project.EntityManager";
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700104constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700105
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700106// event direction is bit[7] of eventType where 1b = Deassertion event
107constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800108
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700109static std::vector<uint8_t> fruCache;
110static uint8_t cacheBus = 0xFF;
111static uint8_t cacheAddr = 0XFF;
112
113std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
114
115// we unfortunately have to build a map of hashes in case there is a
116// collision to verify our dev-id
117boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
118
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700119void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700120
121bool writeFru()
122{
Vernon Mauery15419dd2019-05-24 09:40:30 -0700123 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
124 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700125 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
126 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
127 writeFru.append(cacheBus, cacheAddr, fruCache);
128 try
129 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700130 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700131 }
132 catch (sdbusplus::exception_t&)
133 {
134 // todo: log sel?
135 phosphor::logging::log<phosphor::logging::level::ERR>(
136 "error writing fru");
137 return false;
138 }
139 return true;
140}
141
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700142void createTimer()
143{
144 if (cacheTimer == nullptr)
145 {
146 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
147 }
148}
149
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700150ipmi_ret_t replaceCacheFru(uint8_t devId)
151{
152 static uint8_t lastDevId = 0xFF;
153
154 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
155 if (lastDevId == devId && timerRunning)
156 {
157 return IPMI_CC_OK; // cache already up to date
158 }
159 // if timer is running, stop it and writeFru manually
160 else if (timerRunning)
161 {
162 cacheTimer->stop();
163 writeFru();
164 }
165
Vernon Mauery15419dd2019-05-24 09:40:30 -0700166 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
167 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700168 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
169 "GetManagedObjects");
170 ManagedObjectType frus;
171 try
172 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700173 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700174 resp.read(frus);
175 }
176 catch (sdbusplus::exception_t&)
177 {
178 phosphor::logging::log<phosphor::logging::level::ERR>(
179 "replaceCacheFru: error getting managed objects");
180 return IPMI_CC_RESPONSE_ERROR;
181 }
182
183 deviceHashes.clear();
184
185 // hash the object paths to create unique device id's. increment on
186 // collision
187 std::hash<std::string> hasher;
188 for (const auto& fru : frus)
189 {
190 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
191 if (fruIface == fru.second.end())
192 {
193 continue;
194 }
195
196 auto busFind = fruIface->second.find("BUS");
197 auto addrFind = fruIface->second.find("ADDRESS");
198 if (busFind == fruIface->second.end() ||
199 addrFind == fruIface->second.end())
200 {
201 phosphor::logging::log<phosphor::logging::level::INFO>(
202 "fru device missing Bus or Address",
203 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
204 continue;
205 }
206
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700207 uint8_t fruBus = std::get<uint32_t>(busFind->second);
208 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700209
210 uint8_t fruHash = 0;
211 if (fruBus != 0 || fruAddr != 0)
212 {
213 fruHash = hasher(fru.first.str);
214 // can't be 0xFF based on spec, and 0 is reserved for baseboard
215 if (fruHash == 0 || fruHash == 0xFF)
216 {
217 fruHash = 1;
218 }
219 }
220 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
221
222 bool emplacePassed = false;
223 while (!emplacePassed)
224 {
225 auto resp = deviceHashes.emplace(fruHash, newDev);
226 emplacePassed = resp.second;
227 if (!emplacePassed)
228 {
229 fruHash++;
230 // can't be 0xFF based on spec, and 0 is reserved for
231 // baseboard
232 if (fruHash == 0XFF)
233 {
234 fruHash = 0x1;
235 }
236 }
237 }
238 }
239 auto deviceFind = deviceHashes.find(devId);
240 if (deviceFind == deviceHashes.end())
241 {
242 return IPMI_CC_SENSOR_INVALID;
243 }
244
245 fruCache.clear();
Vernon Mauery15419dd2019-05-24 09:40:30 -0700246 sdbusplus::message::message getRawFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700247 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
248 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
249 cacheBus = deviceFind->second.first;
250 cacheAddr = deviceFind->second.second;
251 getRawFru.append(cacheBus, cacheAddr);
252 try
253 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700254 sdbusplus::message::message getRawResp = dbus->call(getRawFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700255 getRawResp.read(fruCache);
256 }
257 catch (sdbusplus::exception_t&)
258 {
259 lastDevId = 0xFF;
260 cacheBus = 0xFF;
261 cacheAddr = 0xFF;
262 return IPMI_CC_RESPONSE_ERROR;
263 }
264
265 lastDevId = devId;
266 return IPMI_CC_OK;
267}
268
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000269/** @brief implements the read FRU data command
270 * @param fruDeviceId - FRU Device ID
271 * @param fruInventoryOffset - FRU Inventory Offset to write
272 * @param countToRead - Count to read
273 *
274 * @returns ipmi completion code plus response data
275 * - countWritten - Count written
276 */
277ipmi::RspType<uint8_t, // Count
278 std::vector<uint8_t> // Requested data
279 >
280 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
281 uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700282{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000283 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700284 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000285 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700286 }
287
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000288 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700289
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000290 if (status != ipmi::ccSuccess)
291 {
292 return ipmi::response(status);
293 }
294
295 size_t fromFruByteLen = 0;
296 if (countToRead + fruInventoryOffset < fruCache.size())
297 {
298 fromFruByteLen = countToRead;
299 }
300 else if (fruCache.size() > fruInventoryOffset)
301 {
302 fromFruByteLen = fruCache.size() - fruInventoryOffset;
303 }
304 else
305 {
306 return ipmi::responseInvalidFieldRequest();
307 }
308
309 std::vector<uint8_t> requestedData;
310
311 requestedData.insert(
312 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
313 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
314
315 return ipmi::responseSuccess(countToRead, requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700316}
317
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000318/** @brief implements the write FRU data command
319 * @param fruDeviceId - FRU Device ID
320 * @param fruInventoryOffset - FRU Inventory Offset to write
321 * @param dataToWrite - Data to write
322 *
323 * @returns ipmi completion code plus response data
324 * - countWritten - Count written
325 */
326ipmi::RspType<uint8_t>
327 ipmiStorageWriteFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
328 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700329{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000330 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700331 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000332 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700333 }
334
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000335 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700336
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000337 ipmi::Cc status = replaceCacheFru(fruDeviceId);
338 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700339 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000340 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700341 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000342 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700343 if (fruCache.size() < lastWriteAddr)
344 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000345 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700346 }
347
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000348 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
349 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700350
351 bool atEnd = false;
352
353 if (fruCache.size() >= sizeof(FRUHeader))
354 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700355 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
356
357 int lastRecordStart = std::max(
358 header->internalOffset,
359 std::max(header->chassisOffset,
360 std::max(header->boardOffset, header->productOffset)));
361 // TODO: Handle Multi-Record FRUs?
362
363 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
364
365 // get the length of the area in multiples of 8 bytes
366 if (lastWriteAddr > (lastRecordStart + 1))
367 {
368 // second byte in record area is the length
369 int areaLength(fruCache[lastRecordStart + 1]);
370 areaLength *= 8; // it is in multiples of 8 bytes
371
372 if (lastWriteAddr >= (areaLength + lastRecordStart))
373 {
374 atEnd = true;
375 }
376 }
377 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000378 uint8_t countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700379 if (atEnd)
380 {
381 // cancel timer, we're at the end so might as well send it
382 cacheTimer->stop();
383 if (!writeFru())
384 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000385 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700386 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000387 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700388 }
389 else
390 {
391 // start a timer, if no further data is sent in cacheTimeoutSeconds
392 // seconds, check to see if it is valid
393 createTimer();
394 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
395 std::chrono::seconds(cacheTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000396 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700397 }
398
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000399 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700400}
401
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000402/** @brief implements the get FRU inventory area info command
403 * @param fruDeviceId - FRU Device ID
404 *
405 * @returns IPMI completion code plus response data
406 * - inventorySize - Number of possible allocation units
407 * - accessType - Allocation unit size in bytes.
408 */
409ipmi::RspType<uint16_t, // inventorySize
410 uint8_t> // accessType
411 ipmiStorageGetFruInvAreaInfo(uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700412{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000413 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700414 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000415 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700416 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700417
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000418 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700419
420 if (status != IPMI_CC_OK)
421 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000422 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700423 }
424
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000425 constexpr uint8_t accessType =
426 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700427
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000428 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700429}
430
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700431ipmi_ret_t getFruSdrCount(size_t& count)
432{
433 ipmi_ret_t ret = replaceCacheFru(0);
434 if (ret != IPMI_CC_OK)
435 {
436 return ret;
437 }
438 count = deviceHashes.size();
439 return IPMI_CC_OK;
440}
441
442ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
443{
444 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
445 if (ret != IPMI_CC_OK)
446 {
447 return ret;
448 }
449 if (deviceHashes.size() < index)
450 {
451 return IPMI_CC_INVALID_FIELD_REQUEST;
452 }
453 auto device = deviceHashes.begin() + index;
454 uint8_t& bus = device->second.first;
455 uint8_t& address = device->second.second;
456
457 ManagedObjectType frus;
458
Vernon Mauery15419dd2019-05-24 09:40:30 -0700459 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
460 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700461 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
462 "GetManagedObjects");
463 try
464 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700465 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700466 resp.read(frus);
467 }
468 catch (sdbusplus::exception_t&)
469 {
470 return IPMI_CC_RESPONSE_ERROR;
471 }
472 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
473 auto fru =
474 std::find_if(frus.begin(), frus.end(),
475 [bus, address, &fruData](ManagedEntry& entry) {
476 auto findFruDevice =
477 entry.second.find("xyz.openbmc_project.FruDevice");
478 if (findFruDevice == entry.second.end())
479 {
480 return false;
481 }
482 fruData = &(findFruDevice->second);
483 auto findBus = findFruDevice->second.find("BUS");
484 auto findAddress =
485 findFruDevice->second.find("ADDRESS");
486 if (findBus == findFruDevice->second.end() ||
487 findAddress == findFruDevice->second.end())
488 {
489 return false;
490 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700491 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700492 {
493 return false;
494 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700495 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700496 {
497 return false;
498 }
499 return true;
500 });
501 if (fru == frus.end())
502 {
503 return IPMI_CC_RESPONSE_ERROR;
504 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700505
506 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
507 ManagedObjectType entities;
508
509 try
510 {
511 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
512
513 sdbusplus::message::message getObjects = dbus->new_method_call(
514 entityManagerServiceName, "/", "org.freedesktop.DBus.ObjectManager",
515 "GetManagedObjects");
516
517 sdbusplus::message::message resp = dbus->call(getObjects);
518 resp.read(entities);
519
520 auto entity = std::find_if(
521 entities.begin(), entities.end(),
522 [bus, address, &entityData](ManagedEntry& entry) {
523 auto findFruDevice = entry.second.find(
524 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
525 if (findFruDevice == entry.second.end())
526 {
527 return false;
528 }
529
530 // Integer fields added via Entity-Manager json are uint64_ts by
531 // default.
532 auto findBus = findFruDevice->second.find("Bus");
533 auto findAddress = findFruDevice->second.find("Address");
534
535 if (findBus == findFruDevice->second.end() ||
536 findAddress == findFruDevice->second.end())
537 {
538 return false;
539 }
540 if ((std::get<uint64_t>(findBus->second) != bus) ||
541 (std::get<uint64_t>(findAddress->second) != address))
542 {
543 return false;
544 }
545
546 auto findIpmiDevice = entry.second.find(
547 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
548 if (findIpmiDevice == entry.second.end())
549 {
550 return false;
551 }
552
553 entityData = &(findIpmiDevice->second);
554 return true;
555 });
556
557 if (entity == entities.end())
558 {
559 if constexpr (DEBUG)
560 {
561 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
562 "not found for Fru\n");
563 }
564 }
565 }
566 catch (const std::exception& e)
567 {
568 std::fprintf(
569 stderr,
570 "Search for FruDevice+Ipmi Decorator Interface excepted: '%s'\n",
571 e.what());
572 }
573
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700574 std::string name;
575 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
576 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
577 if (findProductName != fruData->end())
578 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700579 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700580 }
581 else if (findBoardName != fruData->end())
582 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700583 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700584 }
585 else
586 {
587 name = "UNKNOWN";
588 }
589 if (name.size() > maxFruSdrNameSize)
590 {
591 name = name.substr(0, maxFruSdrNameSize);
592 }
593 size_t sizeDiff = maxFruSdrNameSize - name.size();
594
595 resp.header.record_id_lsb = 0x0; // calling code is to implement these
596 resp.header.record_id_msb = 0x0;
597 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700598 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700599 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
600 resp.key.deviceAddress = 0x20;
601 resp.key.fruID = device->first;
602 resp.key.accessLun = 0x80; // logical / physical fru device
603 resp.key.channelNumber = 0x0;
604 resp.body.reserved = 0x0;
605 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700606 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700607
608 uint8_t entityID = 0;
609 uint8_t entityInstance = 0x1;
610
611 if (entityData)
612 {
613 auto entityIdProperty = entityData->find("EntityId");
614 auto entityInstanceProperty = entityData->find("EntityInstance");
615
616 if (entityIdProperty != entityData->end())
617 {
618 entityID = static_cast<uint8_t>(
619 std::get<uint64_t>(entityIdProperty->second));
620 }
621 if (entityInstanceProperty != entityData->end())
622 {
623 entityInstance = static_cast<uint8_t>(
624 std::get<uint64_t>(entityInstanceProperty->second));
625 }
626 }
627
628 resp.body.entityID = entityID;
629 resp.body.entityInstance = entityInstance;
630
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700631 resp.body.oem = 0x0;
632 resp.body.deviceIDLen = name.size();
633 name.copy(resp.body.deviceID, name.size());
634
635 return IPMI_CC_OK;
636}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700637
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700638static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800639{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700640 // Loop through the directory looking for ipmi_sel log files
641 for (const std::filesystem::directory_entry& dirEnt :
642 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800643 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700644 std::string filename = dirEnt.path().filename();
645 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800646 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700647 // If we find an ipmi_sel log file, save the path
648 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
649 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800650 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800651 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700652 // As the log files rotate, they are appended with a ".#" that is higher for
653 // the older logs. Since we don't expect more than 10 log files, we
654 // can just sort the list to get them in order from newest to oldest
655 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800656
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700657 return !selLogFiles.empty();
658}
659
660static int countSELEntries()
661{
662 // Get the list of ipmi_sel log files
663 std::vector<std::filesystem::path> selLogFiles;
664 if (!getSELLogFiles(selLogFiles))
665 {
666 return 0;
667 }
668 int numSELEntries = 0;
669 // Loop through each log file and count the number of logs
670 for (const std::filesystem::path& file : selLogFiles)
671 {
672 std::ifstream logStream(file);
673 if (!logStream.is_open())
674 {
675 continue;
676 }
677
678 std::string line;
679 while (std::getline(logStream, line))
680 {
681 numSELEntries++;
682 }
683 }
684 return numSELEntries;
685}
686
687static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700688 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700689 std::string& entry)
690{
691 // Record ID is the first entry field following the timestamp. It is
692 // preceded by a space and followed by a comma
693 std::string search = " " + std::to_string(recordID) + ",";
694
695 // Loop through the ipmi_sel log entries
696 for (const std::filesystem::path& file : selLogFiles)
697 {
698 std::ifstream logStream(file);
699 if (!logStream.is_open())
700 {
701 continue;
702 }
703
704 while (std::getline(logStream, entry))
705 {
706 // Check if the record ID matches
707 if (entry.find(search) != std::string::npos)
708 {
709 return true;
710 }
711 }
712 }
713 return false;
714}
715
716static uint16_t
717 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700718 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700719{
720 uint16_t nextRecordID = recordID + 1;
721 std::string entry;
722 if (findSELEntry(nextRecordID, selLogFiles, entry))
723 {
724 return nextRecordID;
725 }
726 else
727 {
728 return ipmi::sel::lastEntry;
729 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800730}
731
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700732static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800733{
734 for (unsigned int i = 0; i < hexStr.size(); i += 2)
735 {
736 try
737 {
738 data.push_back(static_cast<uint8_t>(
739 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
740 }
741 catch (std::invalid_argument& e)
742 {
743 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
744 return -1;
745 }
746 catch (std::out_of_range& e)
747 {
748 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
749 return -1;
750 }
751 }
752 return 0;
753}
754
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700755ipmi::RspType<uint8_t, // SEL version
756 uint16_t, // SEL entry count
757 uint16_t, // free space
758 uint32_t, // last add timestamp
759 uint32_t, // last erase timestamp
760 uint8_t> // operation support
761 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800762{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700763 constexpr uint8_t selVersion = ipmi::sel::selVersion;
764 uint16_t entries = countSELEntries();
765 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
766 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
767 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
768 constexpr uint8_t operationSupport =
769 intel_oem::ipmi::sel::selOperationSupport;
770 constexpr uint16_t freeSpace =
771 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800772
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700773 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
774 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800775}
776
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700777using systemEventType = std::tuple<
778 uint32_t, // Timestamp
779 uint16_t, // Generator ID
780 uint8_t, // EvM Rev
781 uint8_t, // Sensor Type
782 uint8_t, // Sensor Number
783 uint7_t, // Event Type
784 bool, // Event Direction
785 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
786using oemTsEventType = std::tuple<
787 uint32_t, // Timestamp
788 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
789using oemEventType =
790 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800791
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700792ipmi::RspType<uint16_t, // Next Record ID
793 uint16_t, // Record ID
794 uint8_t, // Record Type
795 std::variant<systemEventType, oemTsEventType,
796 oemEventType>> // Record Content
797 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
798 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800799{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700800 // Only support getting the entire SEL record. If a partial size or non-zero
801 // offset is requested, return an error
802 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800803 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700804 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800805 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800806
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700807 // Check the reservation ID if one is provided or required (only if the
808 // offset is non-zero)
809 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800810 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700811 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800812 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700813 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800814 }
815 }
816
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700817 // Get the ipmi_sel log files
818 std::vector<std::filesystem::path> selLogFiles;
819 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800820 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700821 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800822 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800823
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700824 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800825
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800826 if (targetID == ipmi::sel::firstEntry)
827 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700828 // The first entry will be at the top of the oldest log file
829 std::ifstream logStream(selLogFiles.back());
830 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800831 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700832 return ipmi::responseUnspecifiedError();
833 }
834
835 if (!std::getline(logStream, targetEntry))
836 {
837 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800838 }
839 }
840 else if (targetID == ipmi::sel::lastEntry)
841 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700842 // The last entry will be at the bottom of the newest log file
843 std::ifstream logStream(selLogFiles.front());
844 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800845 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700846 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800847 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700848
849 std::string line;
850 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800851 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700852 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800853 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800854 }
855 else
856 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700857 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800858 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700859 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800860 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800861 }
862
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700863 // The format of the ipmi_sel message is "<Timestamp>
864 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
865 // First get the Timestamp
866 size_t space = targetEntry.find_first_of(" ");
867 if (space == std::string::npos)
868 {
869 return ipmi::responseUnspecifiedError();
870 }
871 std::string entryTimestamp = targetEntry.substr(0, space);
872 // Then get the log contents
873 size_t entryStart = targetEntry.find_first_not_of(" ", space);
874 if (entryStart == std::string::npos)
875 {
876 return ipmi::responseUnspecifiedError();
877 }
878 std::string_view entry(targetEntry);
879 entry.remove_prefix(entryStart);
880 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700881 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700882 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700883 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700884 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885 {
886 return ipmi::responseUnspecifiedError();
887 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700888 std::string& recordIDStr = targetEntryFields[0];
889 std::string& recordTypeStr = targetEntryFields[1];
890 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700891
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700892 uint16_t recordID;
893 uint8_t recordType;
894 try
895 {
896 recordID = std::stoul(recordIDStr);
897 recordType = std::stoul(recordTypeStr, nullptr, 16);
898 }
899 catch (const std::invalid_argument&)
900 {
901 return ipmi::responseUnspecifiedError();
902 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700903 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700904 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700905 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700906 {
907 return ipmi::responseUnspecifiedError();
908 }
909
910 if (recordType == intel_oem::ipmi::sel::systemEvent)
911 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700912 // Get the timestamp
913 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700914 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700915
916 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
917 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
918 {
919 timestamp = std::mktime(&timeStruct);
920 }
921
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700922 // Set the event message revision
923 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
924
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700925 uint16_t generatorID = 0;
926 uint8_t sensorType = 0;
927 uint8_t sensorNum = 0xFF;
928 uint7_t eventType = 0;
929 bool eventDir = 0;
930 // System type events should have six fields
931 if (targetEntryFields.size() >= 6)
932 {
933 std::string& generatorIDStr = targetEntryFields[3];
934 std::string& sensorPath = targetEntryFields[4];
935 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700936
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700937 // Get the generator ID
938 try
939 {
940 generatorID = std::stoul(generatorIDStr, nullptr, 16);
941 }
942 catch (const std::invalid_argument&)
943 {
944 std::cerr << "Invalid Generator ID\n";
945 }
946
947 // Get the sensor type, sensor number, and event type for the sensor
948 sensorType = getSensorTypeFromPath(sensorPath);
949 sensorNum = getSensorNumberFromPath(sensorPath);
950 eventType = getSensorEventTypeFromPath(sensorPath);
951
952 // Get the event direction
953 try
954 {
955 eventDir = std::stoul(eventDirStr) ? 0 : 1;
956 }
957 catch (const std::invalid_argument&)
958 {
959 std::cerr << "Invalid Event Direction\n";
960 }
961 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700962
963 // Only keep the eventData bytes that fit in the record
964 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
965 std::copy_n(eventDataBytes.begin(),
966 std::min(eventDataBytes.size(), eventData.size()),
967 eventData.begin());
968
969 return ipmi::responseSuccess(
970 nextRecordID, recordID, recordType,
971 systemEventType{timestamp, generatorID, evmRev, sensorType,
972 sensorNum, eventType, eventDir, eventData});
973 }
974 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
975 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
976 {
977 // Get the timestamp
978 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700979 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700980
981 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
982 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
983 {
984 timestamp = std::mktime(&timeStruct);
985 }
986
987 // Only keep the bytes that fit in the record
988 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
989 std::copy_n(eventDataBytes.begin(),
990 std::min(eventDataBytes.size(), eventData.size()),
991 eventData.begin());
992
993 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
994 oemTsEventType{timestamp, eventData});
995 }
Patrick Venturec5136aa2019-10-04 20:39:31 -0700996 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700997 {
998 // Only keep the bytes that fit in the record
999 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1000 std::copy_n(eventDataBytes.begin(),
1001 std::min(eventDataBytes.size(), eventData.size()),
1002 eventData.begin());
1003
1004 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1005 eventData);
1006 }
1007
1008 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001009}
1010
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001011ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1012 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1013 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1014 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1015 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001016{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001017 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1018 // added
1019 cancelSELReservation();
1020
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001021 // Send this request to the Redfish hooks to log it as a Redfish message
1022 // instead. There is no need to add it to the SEL, so just return success.
1023 intel_oem::ipmi::sel::checkRedfishHooks(
1024 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1025 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001026
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001027 uint16_t responseID = 0xFFFF;
1028 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001029}
1030
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001031ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1032 uint16_t reservationID,
1033 const std::array<uint8_t, 3>& clr,
1034 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001035{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001036 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001037 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001038 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001039 }
1040
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001041 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1042 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001043 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001044 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001045 }
1046
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001047 // Erasure status cannot be fetched, so always return erasure status as
1048 // `erase completed`.
1049 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001050 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001051 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001052 }
1053
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001054 // Check that initiate erase is correct
1055 if (eraseOperation != ipmi::sel::initiateErase)
1056 {
1057 return ipmi::responseInvalidFieldRequest();
1058 }
1059
1060 // Per the IPMI spec, need to cancel any reservation when the SEL is
1061 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001062 cancelSELReservation();
1063
Jason M. Bills7944c302019-03-20 15:24:05 -07001064 // Save the erase time
1065 intel_oem::ipmi::sel::erase_time::save();
1066
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001067 // Clear the SEL by deleting the log files
1068 std::vector<std::filesystem::path> selLogFiles;
1069 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001070 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001071 for (const std::filesystem::path& file : selLogFiles)
1072 {
1073 std::error_code ec;
1074 std::filesystem::remove(file, ec);
1075 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001076 }
1077
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001078 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001079 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1080 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001081 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1082 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1083 rsyslogReload.append("rsyslog.service", "replace");
1084 try
1085 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001086 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001087 }
1088 catch (sdbusplus::exception_t& e)
1089 {
1090 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1091 }
1092
1093 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001094}
1095
Jason M. Bills1a474622019-06-14 14:51:33 -07001096ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1097{
1098 struct timespec selTime = {};
1099
1100 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1101 {
1102 return ipmi::responseUnspecifiedError();
1103 }
1104
1105 return ipmi::responseSuccess(selTime.tv_sec);
1106}
1107
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001108ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001109{
1110 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001111 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001112}
1113
James Feist74c50c62019-08-14 14:18:41 -07001114std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1115{
1116 std::vector<uint8_t> resp;
1117 if (index == 0)
1118 {
1119 Type12Record bmc = {};
1120 bmc.header.record_id_lsb = recordId;
1121 bmc.header.record_id_msb = recordId >> 8;
1122 bmc.header.sdr_version = ipmiSdrVersion;
1123 bmc.header.record_type = 0x12;
1124 bmc.header.record_length = 0x1b;
1125 bmc.slaveAddress = 0x20;
1126 bmc.channelNumber = 0;
1127 bmc.powerStateNotification = 0;
1128 bmc.deviceCapabilities = 0xBF;
1129 bmc.reserved = 0;
1130 bmc.entityID = 0x2E;
1131 bmc.entityInstance = 1;
1132 bmc.oem = 0;
1133 bmc.typeLengthCode = 0xD0;
1134 std::string bmcName = "Basbrd Mgmt Ctlr";
1135 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1136 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1137 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1138 }
1139 else if (index == 1)
1140 {
1141 Type12Record me = {};
1142 me.header.record_id_lsb = recordId;
1143 me.header.record_id_msb = recordId >> 8;
1144 me.header.sdr_version = ipmiSdrVersion;
1145 me.header.record_type = 0x12;
1146 me.header.record_length = 0x16;
1147 me.slaveAddress = 0x2C;
1148 me.channelNumber = 6;
1149 me.powerStateNotification = 0x24;
1150 me.deviceCapabilities = 0x21;
1151 me.reserved = 0;
1152 me.entityID = 0x2E;
1153 me.entityInstance = 2;
1154 me.oem = 0;
1155 me.typeLengthCode = 0xCB;
1156 std::string meName = "Mgmt Engine";
1157 std::copy(meName.begin(), meName.end(), me.name);
1158 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1159 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1160 }
1161 else
1162 {
1163 throw std::runtime_error("getType12SDRs:: Illegal index " +
1164 std::to_string(index));
1165 }
1166
1167 return resp;
1168}
1169
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001170void registerStorageFunctions()
1171{
1172 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001173 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1174 ipmi::storage::cmdGetFruInventoryAreaInfo,
1175 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001176 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001177 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1178 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1179 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001180
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001181 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001182 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1183 ipmi::storage::cmdWriteFruData,
1184 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001185
1186 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001187 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001188 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1189 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001190
1191 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001192 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001193 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1194 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001195
1196 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001197 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001198 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001199 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001200
1201 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001202 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1203 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1204 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001205
Jason M. Bills1a474622019-06-14 14:51:33 -07001206 // <Get SEL Time>
1207 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001208 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1209 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001210
Jason M. Billscac97a52019-01-30 14:43:46 -08001211 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001212 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1213 ipmi::storage::cmdSetSelTime,
1214 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001215}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001216} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001217} // namespace ipmi