blob: c89ad8277134824cbb4d592b12570297b802510a [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>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070022#include <iostream>
Jason M. Bills99b78ec2019-01-18 10:42:18 -080023#include <ipmi_to_redfish_hooks.hpp>
James Feist2a265d52019-04-08 11:16:27 -070024#include <ipmid/api.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080025#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070026#include <phosphor-logging/log.hpp>
27#include <sdbusplus/message/types.hpp>
28#include <sdbusplus/timer.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080029#include <sdrutils.hpp>
30#include <stdexcept>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070031#include <storagecommands.hpp>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070032#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070033
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070034namespace intel_oem::ipmi::sel
35{
36static const std::filesystem::path selLogDir = "/var/log";
37static const std::string selLogFilename = "ipmi_sel";
38
39static int getFileTimestamp(const std::filesystem::path& file)
40{
41 struct stat st;
42
43 if (stat(file.c_str(), &st) >= 0)
44 {
45 return st.st_mtime;
46 }
47 return ::ipmi::sel::invalidTimeStamp;
48}
49
50namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070051{
52static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
53
54void save()
55{
56 // open the file, creating it if necessary
57 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
58 if (fd < 0)
59 {
60 std::cerr << "Failed to open file\n";
61 return;
62 }
63
64 // update the file timestamp to the current time
65 if (futimens(fd, NULL) < 0)
66 {
67 std::cerr << "Failed to update timestamp: "
68 << std::string(strerror(errno));
69 }
70 close(fd);
71}
72
73int get()
74{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070075 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070076}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070077} // namespace erase_time
78} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070079
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070080namespace ipmi
81{
82
83namespace storage
84{
85
Jason M. Billse2d1aee2018-10-03 15:57:18 -070086constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070087constexpr static const size_t maxFruSdrNameSize = 16;
88using ManagedObjectType = boost::container::flat_map<
89 sdbusplus::message::object_path,
90 boost::container::flat_map<
91 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
92using ManagedEntry = std::pair<
93 sdbusplus::message::object_path,
94 boost::container::flat_map<
95 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
96
James Feist3bcba452018-12-20 12:31:03 -080097constexpr static const char* fruDeviceServiceName =
98 "xyz.openbmc_project.FruDevice";
Jason M. Billse2d1aee2018-10-03 15:57:18 -070099constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700100
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700101// event direction is bit[7] of eventType where 1b = Deassertion event
102constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800103
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700104static std::vector<uint8_t> fruCache;
105static uint8_t cacheBus = 0xFF;
106static uint8_t cacheAddr = 0XFF;
107
108std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
109
110// we unfortunately have to build a map of hashes in case there is a
111// collision to verify our dev-id
112boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
113
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700114void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700115
116bool writeFru()
117{
Vernon Mauery15419dd2019-05-24 09:40:30 -0700118 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
119 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700120 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
121 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
122 writeFru.append(cacheBus, cacheAddr, fruCache);
123 try
124 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700125 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700126 }
127 catch (sdbusplus::exception_t&)
128 {
129 // todo: log sel?
130 phosphor::logging::log<phosphor::logging::level::ERR>(
131 "error writing fru");
132 return false;
133 }
134 return true;
135}
136
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700137void createTimer()
138{
139 if (cacheTimer == nullptr)
140 {
141 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
142 }
143}
144
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700145ipmi_ret_t replaceCacheFru(uint8_t devId)
146{
147 static uint8_t lastDevId = 0xFF;
148
149 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
150 if (lastDevId == devId && timerRunning)
151 {
152 return IPMI_CC_OK; // cache already up to date
153 }
154 // if timer is running, stop it and writeFru manually
155 else if (timerRunning)
156 {
157 cacheTimer->stop();
158 writeFru();
159 }
160
Vernon Mauery15419dd2019-05-24 09:40:30 -0700161 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
162 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700163 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
164 "GetManagedObjects");
165 ManagedObjectType frus;
166 try
167 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700168 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700169 resp.read(frus);
170 }
171 catch (sdbusplus::exception_t&)
172 {
173 phosphor::logging::log<phosphor::logging::level::ERR>(
174 "replaceCacheFru: error getting managed objects");
175 return IPMI_CC_RESPONSE_ERROR;
176 }
177
178 deviceHashes.clear();
179
180 // hash the object paths to create unique device id's. increment on
181 // collision
182 std::hash<std::string> hasher;
183 for (const auto& fru : frus)
184 {
185 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
186 if (fruIface == fru.second.end())
187 {
188 continue;
189 }
190
191 auto busFind = fruIface->second.find("BUS");
192 auto addrFind = fruIface->second.find("ADDRESS");
193 if (busFind == fruIface->second.end() ||
194 addrFind == fruIface->second.end())
195 {
196 phosphor::logging::log<phosphor::logging::level::INFO>(
197 "fru device missing Bus or Address",
198 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
199 continue;
200 }
201
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700202 uint8_t fruBus = std::get<uint32_t>(busFind->second);
203 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700204
205 uint8_t fruHash = 0;
206 if (fruBus != 0 || fruAddr != 0)
207 {
208 fruHash = hasher(fru.first.str);
209 // can't be 0xFF based on spec, and 0 is reserved for baseboard
210 if (fruHash == 0 || fruHash == 0xFF)
211 {
212 fruHash = 1;
213 }
214 }
215 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
216
217 bool emplacePassed = false;
218 while (!emplacePassed)
219 {
220 auto resp = deviceHashes.emplace(fruHash, newDev);
221 emplacePassed = resp.second;
222 if (!emplacePassed)
223 {
224 fruHash++;
225 // can't be 0xFF based on spec, and 0 is reserved for
226 // baseboard
227 if (fruHash == 0XFF)
228 {
229 fruHash = 0x1;
230 }
231 }
232 }
233 }
234 auto deviceFind = deviceHashes.find(devId);
235 if (deviceFind == deviceHashes.end())
236 {
237 return IPMI_CC_SENSOR_INVALID;
238 }
239
240 fruCache.clear();
Vernon Mauery15419dd2019-05-24 09:40:30 -0700241 sdbusplus::message::message getRawFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700242 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
243 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
244 cacheBus = deviceFind->second.first;
245 cacheAddr = deviceFind->second.second;
246 getRawFru.append(cacheBus, cacheAddr);
247 try
248 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700249 sdbusplus::message::message getRawResp = dbus->call(getRawFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700250 getRawResp.read(fruCache);
251 }
252 catch (sdbusplus::exception_t&)
253 {
254 lastDevId = 0xFF;
255 cacheBus = 0xFF;
256 cacheAddr = 0xFF;
257 return IPMI_CC_RESPONSE_ERROR;
258 }
259
260 lastDevId = devId;
261 return IPMI_CC_OK;
262}
263
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000264/** @brief implements the read FRU data command
265 * @param fruDeviceId - FRU Device ID
266 * @param fruInventoryOffset - FRU Inventory Offset to write
267 * @param countToRead - Count to read
268 *
269 * @returns ipmi completion code plus response data
270 * - countWritten - Count written
271 */
272ipmi::RspType<uint8_t, // Count
273 std::vector<uint8_t> // Requested data
274 >
275 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
276 uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700277{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000278 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700279 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000280 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700281 }
282
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000283 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700284
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000285 if (status != ipmi::ccSuccess)
286 {
287 return ipmi::response(status);
288 }
289
290 size_t fromFruByteLen = 0;
291 if (countToRead + fruInventoryOffset < fruCache.size())
292 {
293 fromFruByteLen = countToRead;
294 }
295 else if (fruCache.size() > fruInventoryOffset)
296 {
297 fromFruByteLen = fruCache.size() - fruInventoryOffset;
298 }
299 else
300 {
301 return ipmi::responseInvalidFieldRequest();
302 }
303
304 std::vector<uint8_t> requestedData;
305
306 requestedData.insert(
307 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
308 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
309
310 return ipmi::responseSuccess(countToRead, requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700311}
312
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000313/** @brief implements the write FRU data command
314 * @param fruDeviceId - FRU Device ID
315 * @param fruInventoryOffset - FRU Inventory Offset to write
316 * @param dataToWrite - Data to write
317 *
318 * @returns ipmi completion code plus response data
319 * - countWritten - Count written
320 */
321ipmi::RspType<uint8_t>
322 ipmiStorageWriteFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
323 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700324{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000325 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700326 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000327 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700328 }
329
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000330 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700331
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000332 ipmi::Cc status = replaceCacheFru(fruDeviceId);
333 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700334 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000335 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700336 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000337 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700338 if (fruCache.size() < lastWriteAddr)
339 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000340 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700341 }
342
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000343 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
344 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700345
346 bool atEnd = false;
347
348 if (fruCache.size() >= sizeof(FRUHeader))
349 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700350 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
351
352 int lastRecordStart = std::max(
353 header->internalOffset,
354 std::max(header->chassisOffset,
355 std::max(header->boardOffset, header->productOffset)));
356 // TODO: Handle Multi-Record FRUs?
357
358 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
359
360 // get the length of the area in multiples of 8 bytes
361 if (lastWriteAddr > (lastRecordStart + 1))
362 {
363 // second byte in record area is the length
364 int areaLength(fruCache[lastRecordStart + 1]);
365 areaLength *= 8; // it is in multiples of 8 bytes
366
367 if (lastWriteAddr >= (areaLength + lastRecordStart))
368 {
369 atEnd = true;
370 }
371 }
372 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000373 uint8_t countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700374 if (atEnd)
375 {
376 // cancel timer, we're at the end so might as well send it
377 cacheTimer->stop();
378 if (!writeFru())
379 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000380 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700381 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000382 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700383 }
384 else
385 {
386 // start a timer, if no further data is sent in cacheTimeoutSeconds
387 // seconds, check to see if it is valid
388 createTimer();
389 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
390 std::chrono::seconds(cacheTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000391 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700392 }
393
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000394 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700395}
396
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000397/** @brief implements the get FRU inventory area info command
398 * @param fruDeviceId - FRU Device ID
399 *
400 * @returns IPMI completion code plus response data
401 * - inventorySize - Number of possible allocation units
402 * - accessType - Allocation unit size in bytes.
403 */
404ipmi::RspType<uint16_t, // inventorySize
405 uint8_t> // accessType
406 ipmiStorageGetFruInvAreaInfo(uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700407{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000408 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700409 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000410 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700411 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700412
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000413 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700414
415 if (status != IPMI_CC_OK)
416 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000417 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700418 }
419
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000420 constexpr uint8_t accessType =
421 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700422
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000423 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700424}
425
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700426ipmi_ret_t getFruSdrCount(size_t& count)
427{
428 ipmi_ret_t ret = replaceCacheFru(0);
429 if (ret != IPMI_CC_OK)
430 {
431 return ret;
432 }
433 count = deviceHashes.size();
434 return IPMI_CC_OK;
435}
436
437ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
438{
439 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
440 if (ret != IPMI_CC_OK)
441 {
442 return ret;
443 }
444 if (deviceHashes.size() < index)
445 {
446 return IPMI_CC_INVALID_FIELD_REQUEST;
447 }
448 auto device = deviceHashes.begin() + index;
449 uint8_t& bus = device->second.first;
450 uint8_t& address = device->second.second;
451
452 ManagedObjectType frus;
453
Vernon Mauery15419dd2019-05-24 09:40:30 -0700454 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
455 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700456 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
457 "GetManagedObjects");
458 try
459 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700460 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700461 resp.read(frus);
462 }
463 catch (sdbusplus::exception_t&)
464 {
465 return IPMI_CC_RESPONSE_ERROR;
466 }
467 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
468 auto fru =
469 std::find_if(frus.begin(), frus.end(),
470 [bus, address, &fruData](ManagedEntry& entry) {
471 auto findFruDevice =
472 entry.second.find("xyz.openbmc_project.FruDevice");
473 if (findFruDevice == entry.second.end())
474 {
475 return false;
476 }
477 fruData = &(findFruDevice->second);
478 auto findBus = findFruDevice->second.find("BUS");
479 auto findAddress =
480 findFruDevice->second.find("ADDRESS");
481 if (findBus == findFruDevice->second.end() ||
482 findAddress == findFruDevice->second.end())
483 {
484 return false;
485 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700486 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700487 {
488 return false;
489 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700490 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700491 {
492 return false;
493 }
494 return true;
495 });
496 if (fru == frus.end())
497 {
498 return IPMI_CC_RESPONSE_ERROR;
499 }
500 std::string name;
501 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
502 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
503 if (findProductName != fruData->end())
504 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700505 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700506 }
507 else if (findBoardName != fruData->end())
508 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700509 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700510 }
511 else
512 {
513 name = "UNKNOWN";
514 }
515 if (name.size() > maxFruSdrNameSize)
516 {
517 name = name.substr(0, maxFruSdrNameSize);
518 }
519 size_t sizeDiff = maxFruSdrNameSize - name.size();
520
521 resp.header.record_id_lsb = 0x0; // calling code is to implement these
522 resp.header.record_id_msb = 0x0;
523 resp.header.sdr_version = ipmiSdrVersion;
524 resp.header.record_type = 0x11; // FRU Device Locator
525 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
526 resp.key.deviceAddress = 0x20;
527 resp.key.fruID = device->first;
528 resp.key.accessLun = 0x80; // logical / physical fru device
529 resp.key.channelNumber = 0x0;
530 resp.body.reserved = 0x0;
531 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700532 resp.body.deviceTypeModifier = 0x0;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700533 resp.body.entityID = 0x0;
534 resp.body.entityInstance = 0x1;
535 resp.body.oem = 0x0;
536 resp.body.deviceIDLen = name.size();
537 name.copy(resp.body.deviceID, name.size());
538
539 return IPMI_CC_OK;
540}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700541
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700542static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800543{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700544 // Loop through the directory looking for ipmi_sel log files
545 for (const std::filesystem::directory_entry& dirEnt :
546 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800547 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700548 std::string filename = dirEnt.path().filename();
549 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800550 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700551 // If we find an ipmi_sel log file, save the path
552 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
553 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800554 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800555 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700556 // As the log files rotate, they are appended with a ".#" that is higher for
557 // the older logs. Since we don't expect more than 10 log files, we
558 // can just sort the list to get them in order from newest to oldest
559 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800560
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700561 return !selLogFiles.empty();
562}
563
564static int countSELEntries()
565{
566 // Get the list of ipmi_sel log files
567 std::vector<std::filesystem::path> selLogFiles;
568 if (!getSELLogFiles(selLogFiles))
569 {
570 return 0;
571 }
572 int numSELEntries = 0;
573 // Loop through each log file and count the number of logs
574 for (const std::filesystem::path& file : selLogFiles)
575 {
576 std::ifstream logStream(file);
577 if (!logStream.is_open())
578 {
579 continue;
580 }
581
582 std::string line;
583 while (std::getline(logStream, line))
584 {
585 numSELEntries++;
586 }
587 }
588 return numSELEntries;
589}
590
591static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700592 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700593 std::string& entry)
594{
595 // Record ID is the first entry field following the timestamp. It is
596 // preceded by a space and followed by a comma
597 std::string search = " " + std::to_string(recordID) + ",";
598
599 // Loop through the ipmi_sel log entries
600 for (const std::filesystem::path& file : selLogFiles)
601 {
602 std::ifstream logStream(file);
603 if (!logStream.is_open())
604 {
605 continue;
606 }
607
608 while (std::getline(logStream, entry))
609 {
610 // Check if the record ID matches
611 if (entry.find(search) != std::string::npos)
612 {
613 return true;
614 }
615 }
616 }
617 return false;
618}
619
620static uint16_t
621 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700622 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700623{
624 uint16_t nextRecordID = recordID + 1;
625 std::string entry;
626 if (findSELEntry(nextRecordID, selLogFiles, entry))
627 {
628 return nextRecordID;
629 }
630 else
631 {
632 return ipmi::sel::lastEntry;
633 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800634}
635
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700636static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800637{
638 for (unsigned int i = 0; i < hexStr.size(); i += 2)
639 {
640 try
641 {
642 data.push_back(static_cast<uint8_t>(
643 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
644 }
645 catch (std::invalid_argument& e)
646 {
647 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
648 return -1;
649 }
650 catch (std::out_of_range& e)
651 {
652 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
653 return -1;
654 }
655 }
656 return 0;
657}
658
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700659ipmi::RspType<uint8_t, // SEL version
660 uint16_t, // SEL entry count
661 uint16_t, // free space
662 uint32_t, // last add timestamp
663 uint32_t, // last erase timestamp
664 uint8_t> // operation support
665 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800666{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700667 constexpr uint8_t selVersion = ipmi::sel::selVersion;
668 uint16_t entries = countSELEntries();
669 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
670 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
671 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
672 constexpr uint8_t operationSupport =
673 intel_oem::ipmi::sel::selOperationSupport;
674 constexpr uint16_t freeSpace =
675 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800676
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700677 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
678 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800679}
680
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700681using systemEventType = std::tuple<
682 uint32_t, // Timestamp
683 uint16_t, // Generator ID
684 uint8_t, // EvM Rev
685 uint8_t, // Sensor Type
686 uint8_t, // Sensor Number
687 uint7_t, // Event Type
688 bool, // Event Direction
689 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
690using oemTsEventType = std::tuple<
691 uint32_t, // Timestamp
692 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
693using oemEventType =
694 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800695
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700696ipmi::RspType<uint16_t, // Next Record ID
697 uint16_t, // Record ID
698 uint8_t, // Record Type
699 std::variant<systemEventType, oemTsEventType,
700 oemEventType>> // Record Content
701 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
702 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800703{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700704 // Only support getting the entire SEL record. If a partial size or non-zero
705 // offset is requested, return an error
706 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800707 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700708 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800709 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800710
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700711 // Check the reservation ID if one is provided or required (only if the
712 // offset is non-zero)
713 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800714 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700715 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800716 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700717 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800718 }
719 }
720
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700721 // Get the ipmi_sel log files
722 std::vector<std::filesystem::path> selLogFiles;
723 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800724 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700725 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800726 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800727
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700728 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800729
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800730 if (targetID == ipmi::sel::firstEntry)
731 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700732 // The first entry will be at the top of the oldest log file
733 std::ifstream logStream(selLogFiles.back());
734 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800735 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700736 return ipmi::responseUnspecifiedError();
737 }
738
739 if (!std::getline(logStream, targetEntry))
740 {
741 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800742 }
743 }
744 else if (targetID == ipmi::sel::lastEntry)
745 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700746 // The last entry will be at the bottom of the newest log file
747 std::ifstream logStream(selLogFiles.front());
748 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800749 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700750 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800751 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700752
753 std::string line;
754 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800755 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700756 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800757 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800758 }
759 else
760 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700761 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800762 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700763 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800764 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800765 }
766
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700767 // The format of the ipmi_sel message is "<Timestamp>
768 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
769 // First get the Timestamp
770 size_t space = targetEntry.find_first_of(" ");
771 if (space == std::string::npos)
772 {
773 return ipmi::responseUnspecifiedError();
774 }
775 std::string entryTimestamp = targetEntry.substr(0, space);
776 // Then get the log contents
777 size_t entryStart = targetEntry.find_first_not_of(" ", space);
778 if (entryStart == std::string::npos)
779 {
780 return ipmi::responseUnspecifiedError();
781 }
782 std::string_view entry(targetEntry);
783 entry.remove_prefix(entryStart);
784 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700785 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700786 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700787 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700788 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700789 {
790 return ipmi::responseUnspecifiedError();
791 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700792 std::string& recordIDStr = targetEntryFields[0];
793 std::string& recordTypeStr = targetEntryFields[1];
794 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700795
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700796 uint16_t recordID;
797 uint8_t recordType;
798 try
799 {
800 recordID = std::stoul(recordIDStr);
801 recordType = std::stoul(recordTypeStr, nullptr, 16);
802 }
803 catch (const std::invalid_argument&)
804 {
805 return ipmi::responseUnspecifiedError();
806 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700807 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700808 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700809 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700810 {
811 return ipmi::responseUnspecifiedError();
812 }
813
814 if (recordType == intel_oem::ipmi::sel::systemEvent)
815 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700816 // Get the timestamp
817 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700818 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700819
820 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
821 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
822 {
823 timestamp = std::mktime(&timeStruct);
824 }
825
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700826 // Set the event message revision
827 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
828
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700829 uint16_t generatorID = 0;
830 uint8_t sensorType = 0;
831 uint8_t sensorNum = 0xFF;
832 uint7_t eventType = 0;
833 bool eventDir = 0;
834 // System type events should have six fields
835 if (targetEntryFields.size() >= 6)
836 {
837 std::string& generatorIDStr = targetEntryFields[3];
838 std::string& sensorPath = targetEntryFields[4];
839 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700840
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700841 // Get the generator ID
842 try
843 {
844 generatorID = std::stoul(generatorIDStr, nullptr, 16);
845 }
846 catch (const std::invalid_argument&)
847 {
848 std::cerr << "Invalid Generator ID\n";
849 }
850
851 // Get the sensor type, sensor number, and event type for the sensor
852 sensorType = getSensorTypeFromPath(sensorPath);
853 sensorNum = getSensorNumberFromPath(sensorPath);
854 eventType = getSensorEventTypeFromPath(sensorPath);
855
856 // Get the event direction
857 try
858 {
859 eventDir = std::stoul(eventDirStr) ? 0 : 1;
860 }
861 catch (const std::invalid_argument&)
862 {
863 std::cerr << "Invalid Event Direction\n";
864 }
865 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700866
867 // Only keep the eventData bytes that fit in the record
868 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
869 std::copy_n(eventDataBytes.begin(),
870 std::min(eventDataBytes.size(), eventData.size()),
871 eventData.begin());
872
873 return ipmi::responseSuccess(
874 nextRecordID, recordID, recordType,
875 systemEventType{timestamp, generatorID, evmRev, sensorType,
876 sensorNum, eventType, eventDir, eventData});
877 }
878 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
879 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
880 {
881 // Get the timestamp
882 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700883 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700884
885 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
886 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
887 {
888 timestamp = std::mktime(&timeStruct);
889 }
890
891 // Only keep the bytes that fit in the record
892 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
893 std::copy_n(eventDataBytes.begin(),
894 std::min(eventDataBytes.size(), eventData.size()),
895 eventData.begin());
896
897 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
898 oemTsEventType{timestamp, eventData});
899 }
900 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst &&
901 recordType <= intel_oem::ipmi::sel::oemEventLast)
902 {
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