blob: 4d27e6eeac1db2df6d9ae9a62836db42d547c402 [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
Patrick Ventureca99ef52019-10-20 14:00:50 -070017#include "storagecommands.hpp"
18
19#include "commandutils.hpp"
20#include "ipmi_to_redfish_hooks.hpp"
21#include "sdrutils.hpp"
22
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070023#include <boost/algorithm/string.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070024#include <boost/container/flat_map.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080025#include <boost/process.hpp>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070026#include <filesystem>
Patrick Venturee8767d22019-09-25 16:54:16 -070027#include <functional>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070028#include <iostream>
James Feist2a265d52019-04-08 11:16:27 -070029#include <ipmid/api.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080030#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070031#include <phosphor-logging/log.hpp>
32#include <sdbusplus/message/types.hpp>
33#include <sdbusplus/timer.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080034#include <stdexcept>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070035#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070036
Patrick Venture9ce789f2019-10-17 09:09:39 -070037static constexpr bool DEBUG = false;
38
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070039namespace intel_oem::ipmi::sel
40{
41static const std::filesystem::path selLogDir = "/var/log";
42static const std::string selLogFilename = "ipmi_sel";
43
44static int getFileTimestamp(const std::filesystem::path& file)
45{
46 struct stat st;
47
48 if (stat(file.c_str(), &st) >= 0)
49 {
50 return st.st_mtime;
51 }
52 return ::ipmi::sel::invalidTimeStamp;
53}
54
55namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070056{
57static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
58
59void save()
60{
61 // open the file, creating it if necessary
62 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
63 if (fd < 0)
64 {
65 std::cerr << "Failed to open file\n";
66 return;
67 }
68
69 // update the file timestamp to the current time
70 if (futimens(fd, NULL) < 0)
71 {
72 std::cerr << "Failed to update timestamp: "
73 << std::string(strerror(errno));
74 }
75 close(fd);
76}
77
78int get()
79{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070080 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070081}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070082} // namespace erase_time
83} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070084
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070085namespace ipmi
86{
87
88namespace storage
89{
90
Jason M. Billse2d1aee2018-10-03 15:57:18 -070091constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070092constexpr static const size_t maxFruSdrNameSize = 16;
93using ManagedObjectType = boost::container::flat_map<
94 sdbusplus::message::object_path,
95 boost::container::flat_map<
96 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
97using ManagedEntry = std::pair<
98 sdbusplus::message::object_path,
99 boost::container::flat_map<
100 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
101
James Feist3bcba452018-12-20 12:31:03 -0800102constexpr static const char* fruDeviceServiceName =
103 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700104constexpr static const char* entityManagerServiceName =
105 "xyz.openbmc_project.EntityManager";
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700106constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700107
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700108// event direction is bit[7] of eventType where 1b = Deassertion event
109constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800110
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700111static std::vector<uint8_t> fruCache;
112static uint8_t cacheBus = 0xFF;
113static uint8_t cacheAddr = 0XFF;
114
115std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
116
117// we unfortunately have to build a map of hashes in case there is a
118// collision to verify our dev-id
119boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
120
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700121void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700122
123bool writeFru()
124{
Vernon Mauery15419dd2019-05-24 09:40:30 -0700125 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
126 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700127 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
128 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
129 writeFru.append(cacheBus, cacheAddr, fruCache);
130 try
131 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700132 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700133 }
134 catch (sdbusplus::exception_t&)
135 {
136 // todo: log sel?
137 phosphor::logging::log<phosphor::logging::level::ERR>(
138 "error writing fru");
139 return false;
140 }
141 return true;
142}
143
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700144void createTimer()
145{
146 if (cacheTimer == nullptr)
147 {
148 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
149 }
150}
151
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700152ipmi_ret_t replaceCacheFru(uint8_t devId)
153{
154 static uint8_t lastDevId = 0xFF;
155
156 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
157 if (lastDevId == devId && timerRunning)
158 {
159 return IPMI_CC_OK; // cache already up to date
160 }
161 // if timer is running, stop it and writeFru manually
162 else if (timerRunning)
163 {
164 cacheTimer->stop();
165 writeFru();
166 }
167
Vernon Mauery15419dd2019-05-24 09:40:30 -0700168 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
169 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700170 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
171 "GetManagedObjects");
172 ManagedObjectType frus;
173 try
174 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700175 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700176 resp.read(frus);
177 }
178 catch (sdbusplus::exception_t&)
179 {
180 phosphor::logging::log<phosphor::logging::level::ERR>(
181 "replaceCacheFru: error getting managed objects");
182 return IPMI_CC_RESPONSE_ERROR;
183 }
184
185 deviceHashes.clear();
186
187 // hash the object paths to create unique device id's. increment on
188 // collision
189 std::hash<std::string> hasher;
190 for (const auto& fru : frus)
191 {
192 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
193 if (fruIface == fru.second.end())
194 {
195 continue;
196 }
197
198 auto busFind = fruIface->second.find("BUS");
199 auto addrFind = fruIface->second.find("ADDRESS");
200 if (busFind == fruIface->second.end() ||
201 addrFind == fruIface->second.end())
202 {
203 phosphor::logging::log<phosphor::logging::level::INFO>(
204 "fru device missing Bus or Address",
205 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
206 continue;
207 }
208
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700209 uint8_t fruBus = std::get<uint32_t>(busFind->second);
210 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700211
212 uint8_t fruHash = 0;
213 if (fruBus != 0 || fruAddr != 0)
214 {
215 fruHash = hasher(fru.first.str);
216 // can't be 0xFF based on spec, and 0 is reserved for baseboard
217 if (fruHash == 0 || fruHash == 0xFF)
218 {
219 fruHash = 1;
220 }
221 }
222 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
223
224 bool emplacePassed = false;
225 while (!emplacePassed)
226 {
227 auto resp = deviceHashes.emplace(fruHash, newDev);
228 emplacePassed = resp.second;
229 if (!emplacePassed)
230 {
231 fruHash++;
232 // can't be 0xFF based on spec, and 0 is reserved for
233 // baseboard
234 if (fruHash == 0XFF)
235 {
236 fruHash = 0x1;
237 }
238 }
239 }
240 }
241 auto deviceFind = deviceHashes.find(devId);
242 if (deviceFind == deviceHashes.end())
243 {
244 return IPMI_CC_SENSOR_INVALID;
245 }
246
247 fruCache.clear();
Vernon Mauery15419dd2019-05-24 09:40:30 -0700248 sdbusplus::message::message getRawFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700249 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
250 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
251 cacheBus = deviceFind->second.first;
252 cacheAddr = deviceFind->second.second;
253 getRawFru.append(cacheBus, cacheAddr);
254 try
255 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700256 sdbusplus::message::message getRawResp = dbus->call(getRawFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700257 getRawResp.read(fruCache);
258 }
259 catch (sdbusplus::exception_t&)
260 {
261 lastDevId = 0xFF;
262 cacheBus = 0xFF;
263 cacheAddr = 0xFF;
264 return IPMI_CC_RESPONSE_ERROR;
265 }
266
267 lastDevId = devId;
268 return IPMI_CC_OK;
269}
270
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000271/** @brief implements the read FRU data command
272 * @param fruDeviceId - FRU Device ID
273 * @param fruInventoryOffset - FRU Inventory Offset to write
274 * @param countToRead - Count to read
275 *
276 * @returns ipmi completion code plus response data
277 * - countWritten - Count written
278 */
279ipmi::RspType<uint8_t, // Count
280 std::vector<uint8_t> // Requested data
281 >
282 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
283 uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700284{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000285 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700286 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000287 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700288 }
289
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000290 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700291
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000292 if (status != ipmi::ccSuccess)
293 {
294 return ipmi::response(status);
295 }
296
297 size_t fromFruByteLen = 0;
298 if (countToRead + fruInventoryOffset < fruCache.size())
299 {
300 fromFruByteLen = countToRead;
301 }
302 else if (fruCache.size() > fruInventoryOffset)
303 {
304 fromFruByteLen = fruCache.size() - fruInventoryOffset;
305 }
306 else
307 {
308 return ipmi::responseInvalidFieldRequest();
309 }
310
311 std::vector<uint8_t> requestedData;
312
313 requestedData.insert(
314 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
315 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
316
317 return ipmi::responseSuccess(countToRead, requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700318}
319
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000320/** @brief implements the write FRU data command
321 * @param fruDeviceId - FRU Device ID
322 * @param fruInventoryOffset - FRU Inventory Offset to write
323 * @param dataToWrite - Data to write
324 *
325 * @returns ipmi completion code plus response data
326 * - countWritten - Count written
327 */
328ipmi::RspType<uint8_t>
329 ipmiStorageWriteFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
330 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700331{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000332 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700333 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000334 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700335 }
336
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000337 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700338
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000339 ipmi::Cc status = replaceCacheFru(fruDeviceId);
340 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700341 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000342 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700343 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000344 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700345 if (fruCache.size() < lastWriteAddr)
346 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000347 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700348 }
349
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000350 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
351 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700352
353 bool atEnd = false;
354
355 if (fruCache.size() >= sizeof(FRUHeader))
356 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700357 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
358
359 int lastRecordStart = std::max(
360 header->internalOffset,
361 std::max(header->chassisOffset,
362 std::max(header->boardOffset, header->productOffset)));
363 // TODO: Handle Multi-Record FRUs?
364
365 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
366
367 // get the length of the area in multiples of 8 bytes
368 if (lastWriteAddr > (lastRecordStart + 1))
369 {
370 // second byte in record area is the length
371 int areaLength(fruCache[lastRecordStart + 1]);
372 areaLength *= 8; // it is in multiples of 8 bytes
373
374 if (lastWriteAddr >= (areaLength + lastRecordStart))
375 {
376 atEnd = true;
377 }
378 }
379 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000380 uint8_t countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700381 if (atEnd)
382 {
383 // cancel timer, we're at the end so might as well send it
384 cacheTimer->stop();
385 if (!writeFru())
386 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000387 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700388 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000389 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700390 }
391 else
392 {
393 // start a timer, if no further data is sent in cacheTimeoutSeconds
394 // seconds, check to see if it is valid
395 createTimer();
396 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
397 std::chrono::seconds(cacheTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000398 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700399 }
400
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000401 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700402}
403
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000404/** @brief implements the get FRU inventory area info command
405 * @param fruDeviceId - FRU Device ID
406 *
407 * @returns IPMI completion code plus response data
408 * - inventorySize - Number of possible allocation units
409 * - accessType - Allocation unit size in bytes.
410 */
411ipmi::RspType<uint16_t, // inventorySize
412 uint8_t> // accessType
413 ipmiStorageGetFruInvAreaInfo(uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700414{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000415 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700416 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000417 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700418 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700419
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000420 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700421
422 if (status != IPMI_CC_OK)
423 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000424 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700425 }
426
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000427 constexpr uint8_t accessType =
428 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700429
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000430 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700431}
432
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700433ipmi_ret_t getFruSdrCount(size_t& count)
434{
435 ipmi_ret_t ret = replaceCacheFru(0);
436 if (ret != IPMI_CC_OK)
437 {
438 return ret;
439 }
440 count = deviceHashes.size();
441 return IPMI_CC_OK;
442}
443
444ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
445{
446 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
447 if (ret != IPMI_CC_OK)
448 {
449 return ret;
450 }
451 if (deviceHashes.size() < index)
452 {
453 return IPMI_CC_INVALID_FIELD_REQUEST;
454 }
455 auto device = deviceHashes.begin() + index;
456 uint8_t& bus = device->second.first;
457 uint8_t& address = device->second.second;
458
459 ManagedObjectType frus;
460
Vernon Mauery15419dd2019-05-24 09:40:30 -0700461 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
462 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700463 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
464 "GetManagedObjects");
465 try
466 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700467 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700468 resp.read(frus);
469 }
470 catch (sdbusplus::exception_t&)
471 {
472 return IPMI_CC_RESPONSE_ERROR;
473 }
474 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
475 auto fru =
476 std::find_if(frus.begin(), frus.end(),
477 [bus, address, &fruData](ManagedEntry& entry) {
478 auto findFruDevice =
479 entry.second.find("xyz.openbmc_project.FruDevice");
480 if (findFruDevice == entry.second.end())
481 {
482 return false;
483 }
484 fruData = &(findFruDevice->second);
485 auto findBus = findFruDevice->second.find("BUS");
486 auto findAddress =
487 findFruDevice->second.find("ADDRESS");
488 if (findBus == findFruDevice->second.end() ||
489 findAddress == findFruDevice->second.end())
490 {
491 return false;
492 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700493 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700494 {
495 return false;
496 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700497 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700498 {
499 return false;
500 }
501 return true;
502 });
503 if (fru == frus.end())
504 {
505 return IPMI_CC_RESPONSE_ERROR;
506 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700507
508 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
509 ManagedObjectType entities;
510
511 try
512 {
513 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
514
515 sdbusplus::message::message getObjects = dbus->new_method_call(
516 entityManagerServiceName, "/", "org.freedesktop.DBus.ObjectManager",
517 "GetManagedObjects");
518
519 sdbusplus::message::message resp = dbus->call(getObjects);
520 resp.read(entities);
521
522 auto entity = std::find_if(
523 entities.begin(), entities.end(),
524 [bus, address, &entityData](ManagedEntry& entry) {
525 auto findFruDevice = entry.second.find(
526 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
527 if (findFruDevice == entry.second.end())
528 {
529 return false;
530 }
531
532 // Integer fields added via Entity-Manager json are uint64_ts by
533 // default.
534 auto findBus = findFruDevice->second.find("Bus");
535 auto findAddress = findFruDevice->second.find("Address");
536
537 if (findBus == findFruDevice->second.end() ||
538 findAddress == findFruDevice->second.end())
539 {
540 return false;
541 }
542 if ((std::get<uint64_t>(findBus->second) != bus) ||
543 (std::get<uint64_t>(findAddress->second) != address))
544 {
545 return false;
546 }
547
548 auto findIpmiDevice = entry.second.find(
549 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
550 if (findIpmiDevice == entry.second.end())
551 {
552 return false;
553 }
554
555 entityData = &(findIpmiDevice->second);
556 return true;
557 });
558
559 if (entity == entities.end())
560 {
561 if constexpr (DEBUG)
562 {
563 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
564 "not found for Fru\n");
565 }
566 }
567 }
568 catch (const std::exception& e)
569 {
570 std::fprintf(
571 stderr,
572 "Search for FruDevice+Ipmi Decorator Interface excepted: '%s'\n",
573 e.what());
574 }
575
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700576 std::string name;
577 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
578 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
579 if (findProductName != fruData->end())
580 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700581 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700582 }
583 else if (findBoardName != fruData->end())
584 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700585 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700586 }
587 else
588 {
589 name = "UNKNOWN";
590 }
591 if (name.size() > maxFruSdrNameSize)
592 {
593 name = name.substr(0, maxFruSdrNameSize);
594 }
595 size_t sizeDiff = maxFruSdrNameSize - name.size();
596
597 resp.header.record_id_lsb = 0x0; // calling code is to implement these
598 resp.header.record_id_msb = 0x0;
599 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700600 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700601 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
602 resp.key.deviceAddress = 0x20;
603 resp.key.fruID = device->first;
604 resp.key.accessLun = 0x80; // logical / physical fru device
605 resp.key.channelNumber = 0x0;
606 resp.body.reserved = 0x0;
607 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700608 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700609
610 uint8_t entityID = 0;
611 uint8_t entityInstance = 0x1;
612
613 if (entityData)
614 {
615 auto entityIdProperty = entityData->find("EntityId");
616 auto entityInstanceProperty = entityData->find("EntityInstance");
617
618 if (entityIdProperty != entityData->end())
619 {
620 entityID = static_cast<uint8_t>(
621 std::get<uint64_t>(entityIdProperty->second));
622 }
623 if (entityInstanceProperty != entityData->end())
624 {
625 entityInstance = static_cast<uint8_t>(
626 std::get<uint64_t>(entityInstanceProperty->second));
627 }
628 }
629
630 resp.body.entityID = entityID;
631 resp.body.entityInstance = entityInstance;
632
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700633 resp.body.oem = 0x0;
634 resp.body.deviceIDLen = name.size();
635 name.copy(resp.body.deviceID, name.size());
636
637 return IPMI_CC_OK;
638}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700639
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700640static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800641{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700642 // Loop through the directory looking for ipmi_sel log files
643 for (const std::filesystem::directory_entry& dirEnt :
644 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800645 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700646 std::string filename = dirEnt.path().filename();
647 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800648 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700649 // If we find an ipmi_sel log file, save the path
650 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
651 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800652 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800653 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700654 // As the log files rotate, they are appended with a ".#" that is higher for
655 // the older logs. Since we don't expect more than 10 log files, we
656 // can just sort the list to get them in order from newest to oldest
657 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800658
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700659 return !selLogFiles.empty();
660}
661
662static int countSELEntries()
663{
664 // Get the list of ipmi_sel log files
665 std::vector<std::filesystem::path> selLogFiles;
666 if (!getSELLogFiles(selLogFiles))
667 {
668 return 0;
669 }
670 int numSELEntries = 0;
671 // Loop through each log file and count the number of logs
672 for (const std::filesystem::path& file : selLogFiles)
673 {
674 std::ifstream logStream(file);
675 if (!logStream.is_open())
676 {
677 continue;
678 }
679
680 std::string line;
681 while (std::getline(logStream, line))
682 {
683 numSELEntries++;
684 }
685 }
686 return numSELEntries;
687}
688
689static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700690 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700691 std::string& entry)
692{
693 // Record ID is the first entry field following the timestamp. It is
694 // preceded by a space and followed by a comma
695 std::string search = " " + std::to_string(recordID) + ",";
696
697 // Loop through the ipmi_sel log entries
698 for (const std::filesystem::path& file : selLogFiles)
699 {
700 std::ifstream logStream(file);
701 if (!logStream.is_open())
702 {
703 continue;
704 }
705
706 while (std::getline(logStream, entry))
707 {
708 // Check if the record ID matches
709 if (entry.find(search) != std::string::npos)
710 {
711 return true;
712 }
713 }
714 }
715 return false;
716}
717
718static uint16_t
719 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700720 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700721{
722 uint16_t nextRecordID = recordID + 1;
723 std::string entry;
724 if (findSELEntry(nextRecordID, selLogFiles, entry))
725 {
726 return nextRecordID;
727 }
728 else
729 {
730 return ipmi::sel::lastEntry;
731 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800732}
733
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700734static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800735{
736 for (unsigned int i = 0; i < hexStr.size(); i += 2)
737 {
738 try
739 {
740 data.push_back(static_cast<uint8_t>(
741 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
742 }
743 catch (std::invalid_argument& e)
744 {
745 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
746 return -1;
747 }
748 catch (std::out_of_range& e)
749 {
750 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
751 return -1;
752 }
753 }
754 return 0;
755}
756
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700757ipmi::RspType<uint8_t, // SEL version
758 uint16_t, // SEL entry count
759 uint16_t, // free space
760 uint32_t, // last add timestamp
761 uint32_t, // last erase timestamp
762 uint8_t> // operation support
763 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800764{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700765 constexpr uint8_t selVersion = ipmi::sel::selVersion;
766 uint16_t entries = countSELEntries();
767 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
768 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
769 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
770 constexpr uint8_t operationSupport =
771 intel_oem::ipmi::sel::selOperationSupport;
772 constexpr uint16_t freeSpace =
773 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800774
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700775 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
776 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800777}
778
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700779using systemEventType = std::tuple<
780 uint32_t, // Timestamp
781 uint16_t, // Generator ID
782 uint8_t, // EvM Rev
783 uint8_t, // Sensor Type
784 uint8_t, // Sensor Number
785 uint7_t, // Event Type
786 bool, // Event Direction
787 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
788using oemTsEventType = std::tuple<
789 uint32_t, // Timestamp
790 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
791using oemEventType =
792 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800793
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700794ipmi::RspType<uint16_t, // Next Record ID
795 uint16_t, // Record ID
796 uint8_t, // Record Type
797 std::variant<systemEventType, oemTsEventType,
798 oemEventType>> // Record Content
799 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
800 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800801{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700802 // Only support getting the entire SEL record. If a partial size or non-zero
803 // offset is requested, return an error
804 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800805 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700806 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800807 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800808
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700809 // Check the reservation ID if one is provided or required (only if the
810 // offset is non-zero)
811 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800812 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700813 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800814 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700815 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800816 }
817 }
818
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700819 // Get the ipmi_sel log files
820 std::vector<std::filesystem::path> selLogFiles;
821 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800822 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700823 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800824 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800825
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700826 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800827
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800828 if (targetID == ipmi::sel::firstEntry)
829 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700830 // The first entry will be at the top of the oldest log file
831 std::ifstream logStream(selLogFiles.back());
832 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800833 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700834 return ipmi::responseUnspecifiedError();
835 }
836
837 if (!std::getline(logStream, targetEntry))
838 {
839 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800840 }
841 }
842 else if (targetID == ipmi::sel::lastEntry)
843 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700844 // The last entry will be at the bottom of the newest log file
845 std::ifstream logStream(selLogFiles.front());
846 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800847 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700848 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800849 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700850
851 std::string line;
852 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800853 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700854 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800855 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800856 }
857 else
858 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700859 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800860 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700861 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800862 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800863 }
864
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700865 // The format of the ipmi_sel message is "<Timestamp>
866 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
867 // First get the Timestamp
868 size_t space = targetEntry.find_first_of(" ");
869 if (space == std::string::npos)
870 {
871 return ipmi::responseUnspecifiedError();
872 }
873 std::string entryTimestamp = targetEntry.substr(0, space);
874 // Then get the log contents
875 size_t entryStart = targetEntry.find_first_not_of(" ", space);
876 if (entryStart == std::string::npos)
877 {
878 return ipmi::responseUnspecifiedError();
879 }
880 std::string_view entry(targetEntry);
881 entry.remove_prefix(entryStart);
882 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700883 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700884 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700886 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700887 {
888 return ipmi::responseUnspecifiedError();
889 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700890 std::string& recordIDStr = targetEntryFields[0];
891 std::string& recordTypeStr = targetEntryFields[1];
892 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700893
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700894 uint16_t recordID;
895 uint8_t recordType;
896 try
897 {
898 recordID = std::stoul(recordIDStr);
899 recordType = std::stoul(recordTypeStr, nullptr, 16);
900 }
901 catch (const std::invalid_argument&)
902 {
903 return ipmi::responseUnspecifiedError();
904 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700905 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700906 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700907 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700908 {
909 return ipmi::responseUnspecifiedError();
910 }
911
912 if (recordType == intel_oem::ipmi::sel::systemEvent)
913 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700914 // Get the timestamp
915 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700916 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700917
918 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
919 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
920 {
921 timestamp = std::mktime(&timeStruct);
922 }
923
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700924 // Set the event message revision
925 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
926
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700927 uint16_t generatorID = 0;
928 uint8_t sensorType = 0;
929 uint8_t sensorNum = 0xFF;
930 uint7_t eventType = 0;
931 bool eventDir = 0;
932 // System type events should have six fields
933 if (targetEntryFields.size() >= 6)
934 {
935 std::string& generatorIDStr = targetEntryFields[3];
936 std::string& sensorPath = targetEntryFields[4];
937 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700938
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700939 // Get the generator ID
940 try
941 {
942 generatorID = std::stoul(generatorIDStr, nullptr, 16);
943 }
944 catch (const std::invalid_argument&)
945 {
946 std::cerr << "Invalid Generator ID\n";
947 }
948
949 // Get the sensor type, sensor number, and event type for the sensor
950 sensorType = getSensorTypeFromPath(sensorPath);
951 sensorNum = getSensorNumberFromPath(sensorPath);
952 eventType = getSensorEventTypeFromPath(sensorPath);
953
954 // Get the event direction
955 try
956 {
957 eventDir = std::stoul(eventDirStr) ? 0 : 1;
958 }
959 catch (const std::invalid_argument&)
960 {
961 std::cerr << "Invalid Event Direction\n";
962 }
963 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700964
965 // Only keep the eventData bytes that fit in the record
966 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
967 std::copy_n(eventDataBytes.begin(),
968 std::min(eventDataBytes.size(), eventData.size()),
969 eventData.begin());
970
971 return ipmi::responseSuccess(
972 nextRecordID, recordID, recordType,
973 systemEventType{timestamp, generatorID, evmRev, sensorType,
974 sensorNum, eventType, eventDir, eventData});
975 }
976 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
977 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
978 {
979 // Get the timestamp
980 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700981 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700982
983 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
984 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
985 {
986 timestamp = std::mktime(&timeStruct);
987 }
988
989 // Only keep the bytes that fit in the record
990 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
991 std::copy_n(eventDataBytes.begin(),
992 std::min(eventDataBytes.size(), eventData.size()),
993 eventData.begin());
994
995 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
996 oemTsEventType{timestamp, eventData});
997 }
Patrick Venturec5136aa2019-10-04 20:39:31 -0700998 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700999 {
1000 // Only keep the bytes that fit in the record
1001 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1002 std::copy_n(eventDataBytes.begin(),
1003 std::min(eventDataBytes.size(), eventData.size()),
1004 eventData.begin());
1005
1006 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1007 eventData);
1008 }
1009
1010 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001011}
1012
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001013ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1014 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1015 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1016 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1017 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001018{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001019 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1020 // added
1021 cancelSELReservation();
1022
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001023 // Send this request to the Redfish hooks to log it as a Redfish message
1024 // instead. There is no need to add it to the SEL, so just return success.
1025 intel_oem::ipmi::sel::checkRedfishHooks(
1026 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1027 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001028
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001029 uint16_t responseID = 0xFFFF;
1030 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001031}
1032
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001033ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1034 uint16_t reservationID,
1035 const std::array<uint8_t, 3>& clr,
1036 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001037{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001038 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001039 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001040 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001041 }
1042
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001043 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1044 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001045 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001046 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001047 }
1048
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001049 // Erasure status cannot be fetched, so always return erasure status as
1050 // `erase completed`.
1051 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001052 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001053 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001054 }
1055
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001056 // Check that initiate erase is correct
1057 if (eraseOperation != ipmi::sel::initiateErase)
1058 {
1059 return ipmi::responseInvalidFieldRequest();
1060 }
1061
1062 // Per the IPMI spec, need to cancel any reservation when the SEL is
1063 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001064 cancelSELReservation();
1065
Jason M. Bills7944c302019-03-20 15:24:05 -07001066 // Save the erase time
1067 intel_oem::ipmi::sel::erase_time::save();
1068
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001069 // Clear the SEL by deleting the log files
1070 std::vector<std::filesystem::path> selLogFiles;
1071 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001072 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001073 for (const std::filesystem::path& file : selLogFiles)
1074 {
1075 std::error_code ec;
1076 std::filesystem::remove(file, ec);
1077 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001078 }
1079
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001080 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001081 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1082 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001083 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1084 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1085 rsyslogReload.append("rsyslog.service", "replace");
1086 try
1087 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001088 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001089 }
1090 catch (sdbusplus::exception_t& e)
1091 {
1092 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1093 }
1094
1095 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001096}
1097
Jason M. Bills1a474622019-06-14 14:51:33 -07001098ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1099{
1100 struct timespec selTime = {};
1101
1102 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1103 {
1104 return ipmi::responseUnspecifiedError();
1105 }
1106
1107 return ipmi::responseSuccess(selTime.tv_sec);
1108}
1109
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001110ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001111{
1112 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001113 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001114}
1115
James Feist74c50c62019-08-14 14:18:41 -07001116std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1117{
1118 std::vector<uint8_t> resp;
1119 if (index == 0)
1120 {
1121 Type12Record bmc = {};
1122 bmc.header.record_id_lsb = recordId;
1123 bmc.header.record_id_msb = recordId >> 8;
1124 bmc.header.sdr_version = ipmiSdrVersion;
1125 bmc.header.record_type = 0x12;
1126 bmc.header.record_length = 0x1b;
1127 bmc.slaveAddress = 0x20;
1128 bmc.channelNumber = 0;
1129 bmc.powerStateNotification = 0;
1130 bmc.deviceCapabilities = 0xBF;
1131 bmc.reserved = 0;
1132 bmc.entityID = 0x2E;
1133 bmc.entityInstance = 1;
1134 bmc.oem = 0;
1135 bmc.typeLengthCode = 0xD0;
1136 std::string bmcName = "Basbrd Mgmt Ctlr";
1137 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1138 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1139 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1140 }
1141 else if (index == 1)
1142 {
1143 Type12Record me = {};
1144 me.header.record_id_lsb = recordId;
1145 me.header.record_id_msb = recordId >> 8;
1146 me.header.sdr_version = ipmiSdrVersion;
1147 me.header.record_type = 0x12;
1148 me.header.record_length = 0x16;
1149 me.slaveAddress = 0x2C;
1150 me.channelNumber = 6;
1151 me.powerStateNotification = 0x24;
1152 me.deviceCapabilities = 0x21;
1153 me.reserved = 0;
1154 me.entityID = 0x2E;
1155 me.entityInstance = 2;
1156 me.oem = 0;
1157 me.typeLengthCode = 0xCB;
1158 std::string meName = "Mgmt Engine";
1159 std::copy(meName.begin(), meName.end(), me.name);
1160 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1161 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1162 }
1163 else
1164 {
1165 throw std::runtime_error("getType12SDRs:: Illegal index " +
1166 std::to_string(index));
1167 }
1168
1169 return resp;
1170}
1171
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001172void registerStorageFunctions()
1173{
1174 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001175 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1176 ipmi::storage::cmdGetFruInventoryAreaInfo,
1177 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001178 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001179 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1180 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1181 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001182
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001183 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001184 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1185 ipmi::storage::cmdWriteFruData,
1186 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001187
1188 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001189 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001190 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1191 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001192
1193 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001194 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001195 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1196 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001197
1198 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001199 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001200 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001201 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001202
1203 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001204 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1205 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1206 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001207
Jason M. Bills1a474622019-06-14 14:51:33 -07001208 // <Get SEL Time>
1209 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001210 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1211 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001212
Jason M. Billscac97a52019-01-30 14:43:46 -08001213 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001214 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1215 ipmi::storage::cmdSetSelTime,
1216 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001217}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001218} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001219} // namespace ipmi