blob: 2c9629719eda47b5998780abb4e4b76ba83f9b47 [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
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070035namespace intel_oem::ipmi::sel
36{
37static const std::filesystem::path selLogDir = "/var/log";
38static const std::string selLogFilename = "ipmi_sel";
39
40static int getFileTimestamp(const std::filesystem::path& file)
41{
42 struct stat st;
43
44 if (stat(file.c_str(), &st) >= 0)
45 {
46 return st.st_mtime;
47 }
48 return ::ipmi::sel::invalidTimeStamp;
49}
50
51namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070052{
53static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
54
55void save()
56{
57 // open the file, creating it if necessary
58 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
59 if (fd < 0)
60 {
61 std::cerr << "Failed to open file\n";
62 return;
63 }
64
65 // update the file timestamp to the current time
66 if (futimens(fd, NULL) < 0)
67 {
68 std::cerr << "Failed to update timestamp: "
69 << std::string(strerror(errno));
70 }
71 close(fd);
72}
73
74int get()
75{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070076 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070077}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070078} // namespace erase_time
79} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070080
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070081namespace ipmi
82{
83
84namespace storage
85{
86
Jason M. Billse2d1aee2018-10-03 15:57:18 -070087constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070088constexpr static const size_t maxFruSdrNameSize = 16;
89using ManagedObjectType = boost::container::flat_map<
90 sdbusplus::message::object_path,
91 boost::container::flat_map<
92 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
93using ManagedEntry = std::pair<
94 sdbusplus::message::object_path,
95 boost::container::flat_map<
96 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
97
James Feist3bcba452018-12-20 12:31:03 -080098constexpr static const char* fruDeviceServiceName =
99 "xyz.openbmc_project.FruDevice";
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700100constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700101
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700102// event direction is bit[7] of eventType where 1b = Deassertion event
103constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800104
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700105static std::vector<uint8_t> fruCache;
106static uint8_t cacheBus = 0xFF;
107static uint8_t cacheAddr = 0XFF;
108
109std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
110
111// we unfortunately have to build a map of hashes in case there is a
112// collision to verify our dev-id
113boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
114
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700115void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700116
117bool writeFru()
118{
Vernon Mauery15419dd2019-05-24 09:40:30 -0700119 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
120 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700121 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
122 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
123 writeFru.append(cacheBus, cacheAddr, fruCache);
124 try
125 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700126 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700127 }
128 catch (sdbusplus::exception_t&)
129 {
130 // todo: log sel?
131 phosphor::logging::log<phosphor::logging::level::ERR>(
132 "error writing fru");
133 return false;
134 }
135 return true;
136}
137
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700138void createTimer()
139{
140 if (cacheTimer == nullptr)
141 {
142 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
143 }
144}
145
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700146ipmi_ret_t replaceCacheFru(uint8_t devId)
147{
148 static uint8_t lastDevId = 0xFF;
149
150 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
151 if (lastDevId == devId && timerRunning)
152 {
153 return IPMI_CC_OK; // cache already up to date
154 }
155 // if timer is running, stop it and writeFru manually
156 else if (timerRunning)
157 {
158 cacheTimer->stop();
159 writeFru();
160 }
161
Vernon Mauery15419dd2019-05-24 09:40:30 -0700162 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
163 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700164 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
165 "GetManagedObjects");
166 ManagedObjectType frus;
167 try
168 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700169 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700170 resp.read(frus);
171 }
172 catch (sdbusplus::exception_t&)
173 {
174 phosphor::logging::log<phosphor::logging::level::ERR>(
175 "replaceCacheFru: error getting managed objects");
176 return IPMI_CC_RESPONSE_ERROR;
177 }
178
179 deviceHashes.clear();
180
181 // hash the object paths to create unique device id's. increment on
182 // collision
183 std::hash<std::string> hasher;
184 for (const auto& fru : frus)
185 {
186 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
187 if (fruIface == fru.second.end())
188 {
189 continue;
190 }
191
192 auto busFind = fruIface->second.find("BUS");
193 auto addrFind = fruIface->second.find("ADDRESS");
194 if (busFind == fruIface->second.end() ||
195 addrFind == fruIface->second.end())
196 {
197 phosphor::logging::log<phosphor::logging::level::INFO>(
198 "fru device missing Bus or Address",
199 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
200 continue;
201 }
202
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700203 uint8_t fruBus = std::get<uint32_t>(busFind->second);
204 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700205
206 uint8_t fruHash = 0;
207 if (fruBus != 0 || fruAddr != 0)
208 {
209 fruHash = hasher(fru.first.str);
210 // can't be 0xFF based on spec, and 0 is reserved for baseboard
211 if (fruHash == 0 || fruHash == 0xFF)
212 {
213 fruHash = 1;
214 }
215 }
216 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
217
218 bool emplacePassed = false;
219 while (!emplacePassed)
220 {
221 auto resp = deviceHashes.emplace(fruHash, newDev);
222 emplacePassed = resp.second;
223 if (!emplacePassed)
224 {
225 fruHash++;
226 // can't be 0xFF based on spec, and 0 is reserved for
227 // baseboard
228 if (fruHash == 0XFF)
229 {
230 fruHash = 0x1;
231 }
232 }
233 }
234 }
235 auto deviceFind = deviceHashes.find(devId);
236 if (deviceFind == deviceHashes.end())
237 {
238 return IPMI_CC_SENSOR_INVALID;
239 }
240
241 fruCache.clear();
Vernon Mauery15419dd2019-05-24 09:40:30 -0700242 sdbusplus::message::message getRawFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700243 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
244 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
245 cacheBus = deviceFind->second.first;
246 cacheAddr = deviceFind->second.second;
247 getRawFru.append(cacheBus, cacheAddr);
248 try
249 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700250 sdbusplus::message::message getRawResp = dbus->call(getRawFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700251 getRawResp.read(fruCache);
252 }
253 catch (sdbusplus::exception_t&)
254 {
255 lastDevId = 0xFF;
256 cacheBus = 0xFF;
257 cacheAddr = 0xFF;
258 return IPMI_CC_RESPONSE_ERROR;
259 }
260
261 lastDevId = devId;
262 return IPMI_CC_OK;
263}
264
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000265/** @brief implements the read FRU data command
266 * @param fruDeviceId - FRU Device ID
267 * @param fruInventoryOffset - FRU Inventory Offset to write
268 * @param countToRead - Count to read
269 *
270 * @returns ipmi completion code plus response data
271 * - countWritten - Count written
272 */
273ipmi::RspType<uint8_t, // Count
274 std::vector<uint8_t> // Requested data
275 >
276 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
277 uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700278{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000279 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700280 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000281 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700282 }
283
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000284 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700285
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000286 if (status != ipmi::ccSuccess)
287 {
288 return ipmi::response(status);
289 }
290
291 size_t fromFruByteLen = 0;
292 if (countToRead + fruInventoryOffset < fruCache.size())
293 {
294 fromFruByteLen = countToRead;
295 }
296 else if (fruCache.size() > fruInventoryOffset)
297 {
298 fromFruByteLen = fruCache.size() - fruInventoryOffset;
299 }
300 else
301 {
302 return ipmi::responseInvalidFieldRequest();
303 }
304
305 std::vector<uint8_t> requestedData;
306
307 requestedData.insert(
308 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
309 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
310
311 return ipmi::responseSuccess(countToRead, requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700312}
313
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000314/** @brief implements the write FRU data command
315 * @param fruDeviceId - FRU Device ID
316 * @param fruInventoryOffset - FRU Inventory Offset to write
317 * @param dataToWrite - Data to write
318 *
319 * @returns ipmi completion code plus response data
320 * - countWritten - Count written
321 */
322ipmi::RspType<uint8_t>
323 ipmiStorageWriteFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
324 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700325{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000326 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700327 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000328 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700329 }
330
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000331 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700332
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000333 ipmi::Cc status = replaceCacheFru(fruDeviceId);
334 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700335 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000336 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700337 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000338 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700339 if (fruCache.size() < lastWriteAddr)
340 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000341 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700342 }
343
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000344 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
345 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700346
347 bool atEnd = false;
348
349 if (fruCache.size() >= sizeof(FRUHeader))
350 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700351 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
352
353 int lastRecordStart = std::max(
354 header->internalOffset,
355 std::max(header->chassisOffset,
356 std::max(header->boardOffset, header->productOffset)));
357 // TODO: Handle Multi-Record FRUs?
358
359 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
360
361 // get the length of the area in multiples of 8 bytes
362 if (lastWriteAddr > (lastRecordStart + 1))
363 {
364 // second byte in record area is the length
365 int areaLength(fruCache[lastRecordStart + 1]);
366 areaLength *= 8; // it is in multiples of 8 bytes
367
368 if (lastWriteAddr >= (areaLength + lastRecordStart))
369 {
370 atEnd = true;
371 }
372 }
373 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000374 uint8_t countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700375 if (atEnd)
376 {
377 // cancel timer, we're at the end so might as well send it
378 cacheTimer->stop();
379 if (!writeFru())
380 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000381 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700382 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000383 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700384 }
385 else
386 {
387 // start a timer, if no further data is sent in cacheTimeoutSeconds
388 // seconds, check to see if it is valid
389 createTimer();
390 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
391 std::chrono::seconds(cacheTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000392 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700393 }
394
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000395 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700396}
397
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000398/** @brief implements the get FRU inventory area info command
399 * @param fruDeviceId - FRU Device ID
400 *
401 * @returns IPMI completion code plus response data
402 * - inventorySize - Number of possible allocation units
403 * - accessType - Allocation unit size in bytes.
404 */
405ipmi::RspType<uint16_t, // inventorySize
406 uint8_t> // accessType
407 ipmiStorageGetFruInvAreaInfo(uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700408{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000409 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700410 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000411 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700412 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700413
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000414 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700415
416 if (status != IPMI_CC_OK)
417 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000418 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700419 }
420
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000421 constexpr uint8_t accessType =
422 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700423
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000424 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700425}
426
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700427ipmi_ret_t getFruSdrCount(size_t& count)
428{
429 ipmi_ret_t ret = replaceCacheFru(0);
430 if (ret != IPMI_CC_OK)
431 {
432 return ret;
433 }
434 count = deviceHashes.size();
435 return IPMI_CC_OK;
436}
437
438ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
439{
440 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
441 if (ret != IPMI_CC_OK)
442 {
443 return ret;
444 }
445 if (deviceHashes.size() < index)
446 {
447 return IPMI_CC_INVALID_FIELD_REQUEST;
448 }
449 auto device = deviceHashes.begin() + index;
450 uint8_t& bus = device->second.first;
451 uint8_t& address = device->second.second;
452
453 ManagedObjectType frus;
454
Vernon Mauery15419dd2019-05-24 09:40:30 -0700455 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
456 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700457 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
458 "GetManagedObjects");
459 try
460 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700461 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700462 resp.read(frus);
463 }
464 catch (sdbusplus::exception_t&)
465 {
466 return IPMI_CC_RESPONSE_ERROR;
467 }
468 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
469 auto fru =
470 std::find_if(frus.begin(), frus.end(),
471 [bus, address, &fruData](ManagedEntry& entry) {
472 auto findFruDevice =
473 entry.second.find("xyz.openbmc_project.FruDevice");
474 if (findFruDevice == entry.second.end())
475 {
476 return false;
477 }
478 fruData = &(findFruDevice->second);
479 auto findBus = findFruDevice->second.find("BUS");
480 auto findAddress =
481 findFruDevice->second.find("ADDRESS");
482 if (findBus == findFruDevice->second.end() ||
483 findAddress == findFruDevice->second.end())
484 {
485 return false;
486 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700487 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700488 {
489 return false;
490 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700491 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700492 {
493 return false;
494 }
495 return true;
496 });
497 if (fru == frus.end())
498 {
499 return IPMI_CC_RESPONSE_ERROR;
500 }
501 std::string name;
502 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
503 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
504 if (findProductName != fruData->end())
505 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700506 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700507 }
508 else if (findBoardName != fruData->end())
509 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700510 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700511 }
512 else
513 {
514 name = "UNKNOWN";
515 }
516 if (name.size() > maxFruSdrNameSize)
517 {
518 name = name.substr(0, maxFruSdrNameSize);
519 }
520 size_t sizeDiff = maxFruSdrNameSize - name.size();
521
522 resp.header.record_id_lsb = 0x0; // calling code is to implement these
523 resp.header.record_id_msb = 0x0;
524 resp.header.sdr_version = ipmiSdrVersion;
525 resp.header.record_type = 0x11; // FRU Device Locator
526 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
527 resp.key.deviceAddress = 0x20;
528 resp.key.fruID = device->first;
529 resp.key.accessLun = 0x80; // logical / physical fru device
530 resp.key.channelNumber = 0x0;
531 resp.body.reserved = 0x0;
532 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700533 resp.body.deviceTypeModifier = 0x0;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700534 resp.body.entityID = 0x0;
535 resp.body.entityInstance = 0x1;
536 resp.body.oem = 0x0;
537 resp.body.deviceIDLen = name.size();
538 name.copy(resp.body.deviceID, name.size());
539
540 return IPMI_CC_OK;
541}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700542
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700543static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800544{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700545 // Loop through the directory looking for ipmi_sel log files
546 for (const std::filesystem::directory_entry& dirEnt :
547 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800548 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700549 std::string filename = dirEnt.path().filename();
550 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800551 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700552 // If we find an ipmi_sel log file, save the path
553 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
554 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800555 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800556 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700557 // As the log files rotate, they are appended with a ".#" that is higher for
558 // the older logs. Since we don't expect more than 10 log files, we
559 // can just sort the list to get them in order from newest to oldest
560 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800561
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700562 return !selLogFiles.empty();
563}
564
565static int countSELEntries()
566{
567 // Get the list of ipmi_sel log files
568 std::vector<std::filesystem::path> selLogFiles;
569 if (!getSELLogFiles(selLogFiles))
570 {
571 return 0;
572 }
573 int numSELEntries = 0;
574 // Loop through each log file and count the number of logs
575 for (const std::filesystem::path& file : selLogFiles)
576 {
577 std::ifstream logStream(file);
578 if (!logStream.is_open())
579 {
580 continue;
581 }
582
583 std::string line;
584 while (std::getline(logStream, line))
585 {
586 numSELEntries++;
587 }
588 }
589 return numSELEntries;
590}
591
592static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700593 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700594 std::string& entry)
595{
596 // Record ID is the first entry field following the timestamp. It is
597 // preceded by a space and followed by a comma
598 std::string search = " " + std::to_string(recordID) + ",";
599
600 // Loop through the ipmi_sel log entries
601 for (const std::filesystem::path& file : selLogFiles)
602 {
603 std::ifstream logStream(file);
604 if (!logStream.is_open())
605 {
606 continue;
607 }
608
609 while (std::getline(logStream, entry))
610 {
611 // Check if the record ID matches
612 if (entry.find(search) != std::string::npos)
613 {
614 return true;
615 }
616 }
617 }
618 return false;
619}
620
621static uint16_t
622 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700623 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700624{
625 uint16_t nextRecordID = recordID + 1;
626 std::string entry;
627 if (findSELEntry(nextRecordID, selLogFiles, entry))
628 {
629 return nextRecordID;
630 }
631 else
632 {
633 return ipmi::sel::lastEntry;
634 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800635}
636
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700637static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800638{
639 for (unsigned int i = 0; i < hexStr.size(); i += 2)
640 {
641 try
642 {
643 data.push_back(static_cast<uint8_t>(
644 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
645 }
646 catch (std::invalid_argument& e)
647 {
648 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
649 return -1;
650 }
651 catch (std::out_of_range& e)
652 {
653 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
654 return -1;
655 }
656 }
657 return 0;
658}
659
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700660ipmi::RspType<uint8_t, // SEL version
661 uint16_t, // SEL entry count
662 uint16_t, // free space
663 uint32_t, // last add timestamp
664 uint32_t, // last erase timestamp
665 uint8_t> // operation support
666 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800667{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700668 constexpr uint8_t selVersion = ipmi::sel::selVersion;
669 uint16_t entries = countSELEntries();
670 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
671 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
672 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
673 constexpr uint8_t operationSupport =
674 intel_oem::ipmi::sel::selOperationSupport;
675 constexpr uint16_t freeSpace =
676 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800677
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700678 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
679 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800680}
681
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700682using systemEventType = std::tuple<
683 uint32_t, // Timestamp
684 uint16_t, // Generator ID
685 uint8_t, // EvM Rev
686 uint8_t, // Sensor Type
687 uint8_t, // Sensor Number
688 uint7_t, // Event Type
689 bool, // Event Direction
690 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
691using oemTsEventType = std::tuple<
692 uint32_t, // Timestamp
693 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
694using oemEventType =
695 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800696
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700697ipmi::RspType<uint16_t, // Next Record ID
698 uint16_t, // Record ID
699 uint8_t, // Record Type
700 std::variant<systemEventType, oemTsEventType,
701 oemEventType>> // Record Content
702 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
703 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800704{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700705 // Only support getting the entire SEL record. If a partial size or non-zero
706 // offset is requested, return an error
707 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800708 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700709 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800710 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800711
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700712 // Check the reservation ID if one is provided or required (only if the
713 // offset is non-zero)
714 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800715 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700716 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800717 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700718 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800719 }
720 }
721
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700722 // Get the ipmi_sel log files
723 std::vector<std::filesystem::path> selLogFiles;
724 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800725 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700726 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800727 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800728
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700729 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800730
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800731 if (targetID == ipmi::sel::firstEntry)
732 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700733 // The first entry will be at the top of the oldest log file
734 std::ifstream logStream(selLogFiles.back());
735 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800736 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700737 return ipmi::responseUnspecifiedError();
738 }
739
740 if (!std::getline(logStream, targetEntry))
741 {
742 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800743 }
744 }
745 else if (targetID == ipmi::sel::lastEntry)
746 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700747 // The last entry will be at the bottom of the newest log file
748 std::ifstream logStream(selLogFiles.front());
749 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800750 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700751 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800752 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700753
754 std::string line;
755 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800756 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700757 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800758 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800759 }
760 else
761 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700762 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800763 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700764 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800765 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800766 }
767
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700768 // The format of the ipmi_sel message is "<Timestamp>
769 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
770 // First get the Timestamp
771 size_t space = targetEntry.find_first_of(" ");
772 if (space == std::string::npos)
773 {
774 return ipmi::responseUnspecifiedError();
775 }
776 std::string entryTimestamp = targetEntry.substr(0, space);
777 // Then get the log contents
778 size_t entryStart = targetEntry.find_first_not_of(" ", space);
779 if (entryStart == std::string::npos)
780 {
781 return ipmi::responseUnspecifiedError();
782 }
783 std::string_view entry(targetEntry);
784 entry.remove_prefix(entryStart);
785 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700786 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700787 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700788 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700789 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700790 {
791 return ipmi::responseUnspecifiedError();
792 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700793 std::string& recordIDStr = targetEntryFields[0];
794 std::string& recordTypeStr = targetEntryFields[1];
795 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700796
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700797 uint16_t recordID;
798 uint8_t recordType;
799 try
800 {
801 recordID = std::stoul(recordIDStr);
802 recordType = std::stoul(recordTypeStr, nullptr, 16);
803 }
804 catch (const std::invalid_argument&)
805 {
806 return ipmi::responseUnspecifiedError();
807 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700808 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700809 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700810 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700811 {
812 return ipmi::responseUnspecifiedError();
813 }
814
815 if (recordType == intel_oem::ipmi::sel::systemEvent)
816 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700817 // Get the timestamp
818 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700819 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700820
821 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
822 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
823 {
824 timestamp = std::mktime(&timeStruct);
825 }
826
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700827 // Set the event message revision
828 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
829
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700830 uint16_t generatorID = 0;
831 uint8_t sensorType = 0;
832 uint8_t sensorNum = 0xFF;
833 uint7_t eventType = 0;
834 bool eventDir = 0;
835 // System type events should have six fields
836 if (targetEntryFields.size() >= 6)
837 {
838 std::string& generatorIDStr = targetEntryFields[3];
839 std::string& sensorPath = targetEntryFields[4];
840 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700841
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700842 // Get the generator ID
843 try
844 {
845 generatorID = std::stoul(generatorIDStr, nullptr, 16);
846 }
847 catch (const std::invalid_argument&)
848 {
849 std::cerr << "Invalid Generator ID\n";
850 }
851
852 // Get the sensor type, sensor number, and event type for the sensor
853 sensorType = getSensorTypeFromPath(sensorPath);
854 sensorNum = getSensorNumberFromPath(sensorPath);
855 eventType = getSensorEventTypeFromPath(sensorPath);
856
857 // Get the event direction
858 try
859 {
860 eventDir = std::stoul(eventDirStr) ? 0 : 1;
861 }
862 catch (const std::invalid_argument&)
863 {
864 std::cerr << "Invalid Event Direction\n";
865 }
866 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700867
868 // Only keep the eventData bytes that fit in the record
869 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
870 std::copy_n(eventDataBytes.begin(),
871 std::min(eventDataBytes.size(), eventData.size()),
872 eventData.begin());
873
874 return ipmi::responseSuccess(
875 nextRecordID, recordID, recordType,
876 systemEventType{timestamp, generatorID, evmRev, sensorType,
877 sensorNum, eventType, eventDir, eventData});
878 }
879 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
880 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
881 {
882 // Get the timestamp
883 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700884 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885
886 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
887 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
888 {
889 timestamp = std::mktime(&timeStruct);
890 }
891
892 // Only keep the bytes that fit in the record
893 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
894 std::copy_n(eventDataBytes.begin(),
895 std::min(eventDataBytes.size(), eventData.size()),
896 eventData.begin());
897
898 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
899 oemTsEventType{timestamp, eventData});
900 }
Patrick Venturec5136aa2019-10-04 20:39:31 -0700901 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700902 {
903 // Only keep the bytes that fit in the record
904 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
905 std::copy_n(eventDataBytes.begin(),
906 std::min(eventDataBytes.size(), eventData.size()),
907 eventData.begin());
908
909 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
910 eventData);
911 }
912
913 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800914}
915
Jason M. Bills6dd8f042019-04-11 10:39:02 -0700916ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
917 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
918 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
919 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
920 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800921{
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800922 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
923 // added
924 cancelSELReservation();
925
Jason M. Bills6dd8f042019-04-11 10:39:02 -0700926 // Send this request to the Redfish hooks to log it as a Redfish message
927 // instead. There is no need to add it to the SEL, so just return success.
928 intel_oem::ipmi::sel::checkRedfishHooks(
929 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
930 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -0800931
Jason M. Bills6dd8f042019-04-11 10:39:02 -0700932 uint16_t responseID = 0xFFFF;
933 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800934}
935
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700936ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
937 uint16_t reservationID,
938 const std::array<uint8_t, 3>& clr,
939 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800940{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700941 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800942 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700943 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800944 }
945
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700946 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
947 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800948 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700949 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800950 }
951
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700952 // Erasure status cannot be fetched, so always return erasure status as
953 // `erase completed`.
954 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800955 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700956 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800957 }
958
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700959 // Check that initiate erase is correct
960 if (eraseOperation != ipmi::sel::initiateErase)
961 {
962 return ipmi::responseInvalidFieldRequest();
963 }
964
965 // Per the IPMI spec, need to cancel any reservation when the SEL is
966 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800967 cancelSELReservation();
968
Jason M. Bills7944c302019-03-20 15:24:05 -0700969 // Save the erase time
970 intel_oem::ipmi::sel::erase_time::save();
971
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700972 // Clear the SEL by deleting the log files
973 std::vector<std::filesystem::path> selLogFiles;
974 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800975 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700976 for (const std::filesystem::path& file : selLogFiles)
977 {
978 std::error_code ec;
979 std::filesystem::remove(file, ec);
980 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800981 }
982
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700983 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -0700984 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
985 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700986 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
987 "org.freedesktop.systemd1.Manager", "ReloadUnit");
988 rsyslogReload.append("rsyslog.service", "replace");
989 try
990 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700991 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700992 }
993 catch (sdbusplus::exception_t& e)
994 {
995 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
996 }
997
998 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800999}
1000
Jason M. Bills1a474622019-06-14 14:51:33 -07001001ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1002{
1003 struct timespec selTime = {};
1004
1005 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1006 {
1007 return ipmi::responseUnspecifiedError();
1008 }
1009
1010 return ipmi::responseSuccess(selTime.tv_sec);
1011}
1012
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001013ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001014{
1015 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001016 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001017}
1018
James Feist74c50c62019-08-14 14:18:41 -07001019std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1020{
1021 std::vector<uint8_t> resp;
1022 if (index == 0)
1023 {
1024 Type12Record bmc = {};
1025 bmc.header.record_id_lsb = recordId;
1026 bmc.header.record_id_msb = recordId >> 8;
1027 bmc.header.sdr_version = ipmiSdrVersion;
1028 bmc.header.record_type = 0x12;
1029 bmc.header.record_length = 0x1b;
1030 bmc.slaveAddress = 0x20;
1031 bmc.channelNumber = 0;
1032 bmc.powerStateNotification = 0;
1033 bmc.deviceCapabilities = 0xBF;
1034 bmc.reserved = 0;
1035 bmc.entityID = 0x2E;
1036 bmc.entityInstance = 1;
1037 bmc.oem = 0;
1038 bmc.typeLengthCode = 0xD0;
1039 std::string bmcName = "Basbrd Mgmt Ctlr";
1040 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1041 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1042 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1043 }
1044 else if (index == 1)
1045 {
1046 Type12Record me = {};
1047 me.header.record_id_lsb = recordId;
1048 me.header.record_id_msb = recordId >> 8;
1049 me.header.sdr_version = ipmiSdrVersion;
1050 me.header.record_type = 0x12;
1051 me.header.record_length = 0x16;
1052 me.slaveAddress = 0x2C;
1053 me.channelNumber = 6;
1054 me.powerStateNotification = 0x24;
1055 me.deviceCapabilities = 0x21;
1056 me.reserved = 0;
1057 me.entityID = 0x2E;
1058 me.entityInstance = 2;
1059 me.oem = 0;
1060 me.typeLengthCode = 0xCB;
1061 std::string meName = "Mgmt Engine";
1062 std::copy(meName.begin(), meName.end(), me.name);
1063 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1064 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1065 }
1066 else
1067 {
1068 throw std::runtime_error("getType12SDRs:: Illegal index " +
1069 std::to_string(index));
1070 }
1071
1072 return resp;
1073}
1074
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001075void registerStorageFunctions()
1076{
1077 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001078 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1079 ipmi::storage::cmdGetFruInventoryAreaInfo,
1080 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001081 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001082 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1083 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1084 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001085
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001086 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001087 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1088 ipmi::storage::cmdWriteFruData,
1089 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001090
1091 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001092 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001093 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1094 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001095
1096 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001097 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001098 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1099 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001100
1101 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001102 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001103 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001104 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001105
1106 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001107 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1108 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1109 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001110
Jason M. Bills1a474622019-06-14 14:51:33 -07001111 // <Get SEL Time>
1112 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001113 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1114 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001115
Jason M. Billscac97a52019-01-30 14:43:46 -08001116 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001117 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1118 ipmi::storage::cmdSetSelTime,
1119 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001120}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001121} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001122} // namespace ipmi