blob: 7b41d8d1416fde1420e97f152bcf53c26f56e5d5 [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07002// Copyright (c) 2017-2019 Intel Corporation
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Patrick Ventureca99ef52019-10-20 14:00:50 -070017#include "storagecommands.hpp"
18
19#include "commandutils.hpp"
20#include "ipmi_to_redfish_hooks.hpp"
21#include "sdrutils.hpp"
22
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070023#include <boost/algorithm/string.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070024#include <boost/container/flat_map.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080025#include <boost/process.hpp>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070026#include <filesystem>
Patrick Venturee8767d22019-09-25 16:54:16 -070027#include <functional>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070028#include <iostream>
James Feist2a265d52019-04-08 11:16:27 -070029#include <ipmid/api.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080030#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070031#include <phosphor-logging/log.hpp>
32#include <sdbusplus/message/types.hpp>
33#include <sdbusplus/timer.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080034#include <stdexcept>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070035#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070036
Patrick Venture9ce789f2019-10-17 09:09:39 -070037static constexpr bool DEBUG = false;
38
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070039namespace intel_oem::ipmi::sel
40{
41static const std::filesystem::path selLogDir = "/var/log";
42static const std::string selLogFilename = "ipmi_sel";
43
44static int getFileTimestamp(const std::filesystem::path& file)
45{
46 struct stat st;
47
48 if (stat(file.c_str(), &st) >= 0)
49 {
50 return st.st_mtime;
51 }
52 return ::ipmi::sel::invalidTimeStamp;
53}
54
55namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070056{
57static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
58
59void save()
60{
61 // open the file, creating it if necessary
62 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
63 if (fd < 0)
64 {
65 std::cerr << "Failed to open file\n";
66 return;
67 }
68
69 // update the file timestamp to the current time
70 if (futimens(fd, NULL) < 0)
71 {
72 std::cerr << "Failed to update timestamp: "
73 << std::string(strerror(errno));
74 }
75 close(fd);
76}
77
78int get()
79{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070080 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070081}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070082} // namespace erase_time
83} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070084
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070085namespace ipmi
86{
87
88namespace storage
89{
90
Jason M. Billse2d1aee2018-10-03 15:57:18 -070091constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070092constexpr static const size_t maxFruSdrNameSize = 16;
93using ManagedObjectType = boost::container::flat_map<
94 sdbusplus::message::object_path,
95 boost::container::flat_map<
96 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
97using ManagedEntry = std::pair<
98 sdbusplus::message::object_path,
99 boost::container::flat_map<
100 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
101
James Feist3bcba452018-12-20 12:31:03 -0800102constexpr static const char* fruDeviceServiceName =
103 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700104constexpr static const char* entityManagerServiceName =
105 "xyz.openbmc_project.EntityManager";
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700106constexpr static const size_t cacheTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700107
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700108// event direction is bit[7] of eventType where 1b = Deassertion event
109constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800110
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700111static std::vector<uint8_t> fruCache;
112static uint8_t cacheBus = 0xFF;
113static uint8_t cacheAddr = 0XFF;
114
115std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
116
117// we unfortunately have to build a map of hashes in case there is a
118// collision to verify our dev-id
119boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
120
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700121void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700122
123bool writeFru()
124{
Vernon Mauery15419dd2019-05-24 09:40:30 -0700125 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
126 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700127 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
128 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
129 writeFru.append(cacheBus, cacheAddr, fruCache);
130 try
131 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700132 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700133 }
134 catch (sdbusplus::exception_t&)
135 {
136 // todo: log sel?
137 phosphor::logging::log<phosphor::logging::level::ERR>(
138 "error writing fru");
139 return false;
140 }
141 return true;
142}
143
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700144void createTimer()
145{
146 if (cacheTimer == nullptr)
147 {
148 cacheTimer = std::make_unique<phosphor::Timer>(writeFru);
149 }
150}
151
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700152ipmi_ret_t replaceCacheFru(uint8_t devId)
153{
154 static uint8_t lastDevId = 0xFF;
155
156 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
157 if (lastDevId == devId && timerRunning)
158 {
159 return IPMI_CC_OK; // cache already up to date
160 }
161 // if timer is running, stop it and writeFru manually
162 else if (timerRunning)
163 {
164 cacheTimer->stop();
165 writeFru();
166 }
167
Vernon Mauery15419dd2019-05-24 09:40:30 -0700168 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
169 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700170 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
171 "GetManagedObjects");
172 ManagedObjectType frus;
173 try
174 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700175 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700176 resp.read(frus);
177 }
178 catch (sdbusplus::exception_t&)
179 {
180 phosphor::logging::log<phosphor::logging::level::ERR>(
181 "replaceCacheFru: error getting managed objects");
182 return IPMI_CC_RESPONSE_ERROR;
183 }
184
185 deviceHashes.clear();
186
187 // hash the object paths to create unique device id's. increment on
188 // collision
189 std::hash<std::string> hasher;
190 for (const auto& fru : frus)
191 {
192 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
193 if (fruIface == fru.second.end())
194 {
195 continue;
196 }
197
198 auto busFind = fruIface->second.find("BUS");
199 auto addrFind = fruIface->second.find("ADDRESS");
200 if (busFind == fruIface->second.end() ||
201 addrFind == fruIface->second.end())
202 {
203 phosphor::logging::log<phosphor::logging::level::INFO>(
204 "fru device missing Bus or Address",
205 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
206 continue;
207 }
208
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700209 uint8_t fruBus = std::get<uint32_t>(busFind->second);
210 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700211
212 uint8_t fruHash = 0;
213 if (fruBus != 0 || fruAddr != 0)
214 {
215 fruHash = hasher(fru.first.str);
216 // can't be 0xFF based on spec, and 0 is reserved for baseboard
217 if (fruHash == 0 || fruHash == 0xFF)
218 {
219 fruHash = 1;
220 }
221 }
222 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
223
224 bool emplacePassed = false;
225 while (!emplacePassed)
226 {
227 auto resp = deviceHashes.emplace(fruHash, newDev);
228 emplacePassed = resp.second;
229 if (!emplacePassed)
230 {
231 fruHash++;
232 // can't be 0xFF based on spec, and 0 is reserved for
233 // baseboard
234 if (fruHash == 0XFF)
235 {
236 fruHash = 0x1;
237 }
238 }
239 }
240 }
241 auto deviceFind = deviceHashes.find(devId);
242 if (deviceFind == deviceHashes.end())
243 {
244 return IPMI_CC_SENSOR_INVALID;
245 }
246
247 fruCache.clear();
Vernon Mauery15419dd2019-05-24 09:40:30 -0700248 sdbusplus::message::message getRawFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700249 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
250 "xyz.openbmc_project.FruDeviceManager", "GetRawFru");
251 cacheBus = deviceFind->second.first;
252 cacheAddr = deviceFind->second.second;
253 getRawFru.append(cacheBus, cacheAddr);
254 try
255 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700256 sdbusplus::message::message getRawResp = dbus->call(getRawFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700257 getRawResp.read(fruCache);
258 }
259 catch (sdbusplus::exception_t&)
260 {
261 lastDevId = 0xFF;
262 cacheBus = 0xFF;
263 cacheAddr = 0xFF;
264 return IPMI_CC_RESPONSE_ERROR;
265 }
266
267 lastDevId = devId;
268 return IPMI_CC_OK;
269}
270
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000271/** @brief implements the read FRU data command
272 * @param fruDeviceId - FRU Device ID
273 * @param fruInventoryOffset - FRU Inventory Offset to write
274 * @param countToRead - Count to read
275 *
276 * @returns ipmi completion code plus response data
277 * - countWritten - Count written
278 */
279ipmi::RspType<uint8_t, // Count
280 std::vector<uint8_t> // Requested data
281 >
282 ipmiStorageReadFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
283 uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700284{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000285 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700286 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000287 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700288 }
289
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000290 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700291
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000292 if (status != ipmi::ccSuccess)
293 {
294 return ipmi::response(status);
295 }
296
297 size_t fromFruByteLen = 0;
298 if (countToRead + fruInventoryOffset < fruCache.size())
299 {
300 fromFruByteLen = countToRead;
301 }
302 else if (fruCache.size() > fruInventoryOffset)
303 {
304 fromFruByteLen = fruCache.size() - fruInventoryOffset;
305 }
306 else
307 {
308 return ipmi::responseInvalidFieldRequest();
309 }
310
311 std::vector<uint8_t> requestedData;
312
313 requestedData.insert(
314 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
315 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
316
Patrick Venture70b17f92019-10-28 20:01:53 -0700317 return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
318 requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700319}
320
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000321/** @brief implements the write FRU data command
322 * @param fruDeviceId - FRU Device ID
323 * @param fruInventoryOffset - FRU Inventory Offset to write
324 * @param dataToWrite - Data to write
325 *
326 * @returns ipmi completion code plus response data
327 * - countWritten - Count written
328 */
329ipmi::RspType<uint8_t>
330 ipmiStorageWriteFruData(uint8_t fruDeviceId, uint16_t fruInventoryOffset,
331 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700332{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000333 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700334 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000335 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700336 }
337
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000338 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700339
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000340 ipmi::Cc status = replaceCacheFru(fruDeviceId);
341 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700342 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000343 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700344 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000345 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700346 if (fruCache.size() < lastWriteAddr)
347 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000348 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700349 }
350
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000351 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
352 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700353
354 bool atEnd = false;
355
356 if (fruCache.size() >= sizeof(FRUHeader))
357 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700358 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
359
360 int lastRecordStart = std::max(
361 header->internalOffset,
362 std::max(header->chassisOffset,
363 std::max(header->boardOffset, header->productOffset)));
364 // TODO: Handle Multi-Record FRUs?
365
366 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
367
368 // get the length of the area in multiples of 8 bytes
369 if (lastWriteAddr > (lastRecordStart + 1))
370 {
371 // second byte in record area is the length
372 int areaLength(fruCache[lastRecordStart + 1]);
373 areaLength *= 8; // it is in multiples of 8 bytes
374
375 if (lastWriteAddr >= (areaLength + lastRecordStart))
376 {
377 atEnd = true;
378 }
379 }
380 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000381 uint8_t countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700382 if (atEnd)
383 {
384 // cancel timer, we're at the end so might as well send it
385 cacheTimer->stop();
386 if (!writeFru())
387 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000388 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700389 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000390 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700391 }
392 else
393 {
394 // start a timer, if no further data is sent in cacheTimeoutSeconds
395 // seconds, check to see if it is valid
396 createTimer();
397 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
398 std::chrono::seconds(cacheTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000399 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700400 }
401
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000402 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700403}
404
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000405/** @brief implements the get FRU inventory area info command
406 * @param fruDeviceId - FRU Device ID
407 *
408 * @returns IPMI completion code plus response data
409 * - inventorySize - Number of possible allocation units
410 * - accessType - Allocation unit size in bytes.
411 */
412ipmi::RspType<uint16_t, // inventorySize
413 uint8_t> // accessType
414 ipmiStorageGetFruInvAreaInfo(uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700415{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000416 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700417 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000418 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700419 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700420
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000421 ipmi::Cc status = replaceCacheFru(fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700422
423 if (status != IPMI_CC_OK)
424 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000425 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700426 }
427
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000428 constexpr uint8_t accessType =
429 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700430
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000431 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700432}
433
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700434ipmi_ret_t getFruSdrCount(size_t& count)
435{
436 ipmi_ret_t ret = replaceCacheFru(0);
437 if (ret != IPMI_CC_OK)
438 {
439 return ret;
440 }
441 count = deviceHashes.size();
442 return IPMI_CC_OK;
443}
444
445ipmi_ret_t getFruSdrs(size_t index, get_sdr::SensorDataFruRecord& resp)
446{
447 ipmi_ret_t ret = replaceCacheFru(0); // this will update the hash list
448 if (ret != IPMI_CC_OK)
449 {
450 return ret;
451 }
452 if (deviceHashes.size() < index)
453 {
454 return IPMI_CC_INVALID_FIELD_REQUEST;
455 }
456 auto device = deviceHashes.begin() + index;
457 uint8_t& bus = device->second.first;
458 uint8_t& address = device->second.second;
459
460 ManagedObjectType frus;
461
Vernon Mauery15419dd2019-05-24 09:40:30 -0700462 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
463 sdbusplus::message::message getObjects = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700464 fruDeviceServiceName, "/", "org.freedesktop.DBus.ObjectManager",
465 "GetManagedObjects");
466 try
467 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700468 sdbusplus::message::message resp = dbus->call(getObjects);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700469 resp.read(frus);
470 }
471 catch (sdbusplus::exception_t&)
472 {
473 return IPMI_CC_RESPONSE_ERROR;
474 }
475 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
476 auto fru =
477 std::find_if(frus.begin(), frus.end(),
478 [bus, address, &fruData](ManagedEntry& entry) {
479 auto findFruDevice =
480 entry.second.find("xyz.openbmc_project.FruDevice");
481 if (findFruDevice == entry.second.end())
482 {
483 return false;
484 }
485 fruData = &(findFruDevice->second);
486 auto findBus = findFruDevice->second.find("BUS");
487 auto findAddress =
488 findFruDevice->second.find("ADDRESS");
489 if (findBus == findFruDevice->second.end() ||
490 findAddress == findFruDevice->second.end())
491 {
492 return false;
493 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700494 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700495 {
496 return false;
497 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700498 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700499 {
500 return false;
501 }
502 return true;
503 });
504 if (fru == frus.end())
505 {
506 return IPMI_CC_RESPONSE_ERROR;
507 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700508
509 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
510 ManagedObjectType entities;
511
512 try
513 {
514 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
515
516 sdbusplus::message::message getObjects = dbus->new_method_call(
517 entityManagerServiceName, "/", "org.freedesktop.DBus.ObjectManager",
518 "GetManagedObjects");
519
520 sdbusplus::message::message resp = dbus->call(getObjects);
521 resp.read(entities);
522
523 auto entity = std::find_if(
524 entities.begin(), entities.end(),
525 [bus, address, &entityData](ManagedEntry& entry) {
526 auto findFruDevice = entry.second.find(
527 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
528 if (findFruDevice == entry.second.end())
529 {
530 return false;
531 }
532
533 // Integer fields added via Entity-Manager json are uint64_ts by
534 // default.
535 auto findBus = findFruDevice->second.find("Bus");
536 auto findAddress = findFruDevice->second.find("Address");
537
538 if (findBus == findFruDevice->second.end() ||
539 findAddress == findFruDevice->second.end())
540 {
541 return false;
542 }
543 if ((std::get<uint64_t>(findBus->second) != bus) ||
544 (std::get<uint64_t>(findAddress->second) != address))
545 {
546 return false;
547 }
548
Patrick Ventureb4b020c2019-10-21 19:44:38 -0700549 // At this point we found the device entry and should return
550 // true.
Patrick Venture9ce789f2019-10-17 09:09:39 -0700551 auto findIpmiDevice = entry.second.find(
552 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
Patrick Ventureb4b020c2019-10-21 19:44:38 -0700553 if (findIpmiDevice != entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700554 {
Patrick Ventureb4b020c2019-10-21 19:44:38 -0700555 entityData = &(findIpmiDevice->second);
Patrick Venture9ce789f2019-10-17 09:09:39 -0700556 }
557
Patrick Venture9ce789f2019-10-17 09:09:39 -0700558 return true;
559 });
560
561 if (entity == entities.end())
562 {
563 if constexpr (DEBUG)
564 {
565 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
566 "not found for Fru\n");
567 }
568 }
569 }
570 catch (const std::exception& e)
571 {
572 std::fprintf(
573 stderr,
574 "Search for FruDevice+Ipmi Decorator Interface excepted: '%s'\n",
575 e.what());
576 }
577
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700578 std::string name;
579 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
580 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
581 if (findProductName != fruData->end())
582 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700583 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700584 }
585 else if (findBoardName != fruData->end())
586 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700587 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700588 }
589 else
590 {
591 name = "UNKNOWN";
592 }
593 if (name.size() > maxFruSdrNameSize)
594 {
595 name = name.substr(0, maxFruSdrNameSize);
596 }
597 size_t sizeDiff = maxFruSdrNameSize - name.size();
598
599 resp.header.record_id_lsb = 0x0; // calling code is to implement these
600 resp.header.record_id_msb = 0x0;
601 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700602 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700603 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
604 resp.key.deviceAddress = 0x20;
605 resp.key.fruID = device->first;
606 resp.key.accessLun = 0x80; // logical / physical fru device
607 resp.key.channelNumber = 0x0;
608 resp.body.reserved = 0x0;
609 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700610 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700611
612 uint8_t entityID = 0;
613 uint8_t entityInstance = 0x1;
614
615 if (entityData)
616 {
617 auto entityIdProperty = entityData->find("EntityId");
618 auto entityInstanceProperty = entityData->find("EntityInstance");
619
620 if (entityIdProperty != entityData->end())
621 {
622 entityID = static_cast<uint8_t>(
623 std::get<uint64_t>(entityIdProperty->second));
624 }
625 if (entityInstanceProperty != entityData->end())
626 {
627 entityInstance = static_cast<uint8_t>(
628 std::get<uint64_t>(entityInstanceProperty->second));
629 }
630 }
631
632 resp.body.entityID = entityID;
633 resp.body.entityInstance = entityInstance;
634
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700635 resp.body.oem = 0x0;
636 resp.body.deviceIDLen = name.size();
637 name.copy(resp.body.deviceID, name.size());
638
639 return IPMI_CC_OK;
640}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700641
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700642static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800643{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700644 // Loop through the directory looking for ipmi_sel log files
645 for (const std::filesystem::directory_entry& dirEnt :
646 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800647 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700648 std::string filename = dirEnt.path().filename();
649 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800650 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700651 // If we find an ipmi_sel log file, save the path
652 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
653 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800654 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800655 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700656 // As the log files rotate, they are appended with a ".#" that is higher for
657 // the older logs. Since we don't expect more than 10 log files, we
658 // can just sort the list to get them in order from newest to oldest
659 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800660
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700661 return !selLogFiles.empty();
662}
663
664static int countSELEntries()
665{
666 // Get the list of ipmi_sel log files
667 std::vector<std::filesystem::path> selLogFiles;
668 if (!getSELLogFiles(selLogFiles))
669 {
670 return 0;
671 }
672 int numSELEntries = 0;
673 // Loop through each log file and count the number of logs
674 for (const std::filesystem::path& file : selLogFiles)
675 {
676 std::ifstream logStream(file);
677 if (!logStream.is_open())
678 {
679 continue;
680 }
681
682 std::string line;
683 while (std::getline(logStream, line))
684 {
685 numSELEntries++;
686 }
687 }
688 return numSELEntries;
689}
690
691static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700692 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700693 std::string& entry)
694{
695 // Record ID is the first entry field following the timestamp. It is
696 // preceded by a space and followed by a comma
697 std::string search = " " + std::to_string(recordID) + ",";
698
699 // Loop through the ipmi_sel log entries
700 for (const std::filesystem::path& file : selLogFiles)
701 {
702 std::ifstream logStream(file);
703 if (!logStream.is_open())
704 {
705 continue;
706 }
707
708 while (std::getline(logStream, entry))
709 {
710 // Check if the record ID matches
711 if (entry.find(search) != std::string::npos)
712 {
713 return true;
714 }
715 }
716 }
717 return false;
718}
719
720static uint16_t
721 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700722 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700723{
724 uint16_t nextRecordID = recordID + 1;
725 std::string entry;
726 if (findSELEntry(nextRecordID, selLogFiles, entry))
727 {
728 return nextRecordID;
729 }
730 else
731 {
732 return ipmi::sel::lastEntry;
733 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800734}
735
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700736static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800737{
738 for (unsigned int i = 0; i < hexStr.size(); i += 2)
739 {
740 try
741 {
742 data.push_back(static_cast<uint8_t>(
743 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
744 }
745 catch (std::invalid_argument& e)
746 {
747 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
748 return -1;
749 }
750 catch (std::out_of_range& e)
751 {
752 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
753 return -1;
754 }
755 }
756 return 0;
757}
758
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700759ipmi::RspType<uint8_t, // SEL version
760 uint16_t, // SEL entry count
761 uint16_t, // free space
762 uint32_t, // last add timestamp
763 uint32_t, // last erase timestamp
764 uint8_t> // operation support
765 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800766{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700767 constexpr uint8_t selVersion = ipmi::sel::selVersion;
768 uint16_t entries = countSELEntries();
769 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
770 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
771 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
772 constexpr uint8_t operationSupport =
773 intel_oem::ipmi::sel::selOperationSupport;
774 constexpr uint16_t freeSpace =
775 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800776
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700777 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
778 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800779}
780
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700781using systemEventType = std::tuple<
782 uint32_t, // Timestamp
783 uint16_t, // Generator ID
784 uint8_t, // EvM Rev
785 uint8_t, // Sensor Type
786 uint8_t, // Sensor Number
787 uint7_t, // Event Type
788 bool, // Event Direction
789 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
790using oemTsEventType = std::tuple<
791 uint32_t, // Timestamp
792 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
793using oemEventType =
794 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800795
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700796ipmi::RspType<uint16_t, // Next Record ID
797 uint16_t, // Record ID
798 uint8_t, // Record Type
799 std::variant<systemEventType, oemTsEventType,
800 oemEventType>> // Record Content
801 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
802 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800803{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700804 // Only support getting the entire SEL record. If a partial size or non-zero
805 // offset is requested, return an error
806 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800807 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700808 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800809 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800810
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700811 // Check the reservation ID if one is provided or required (only if the
812 // offset is non-zero)
813 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800814 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700815 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800816 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700817 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800818 }
819 }
820
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700821 // Get the ipmi_sel log files
822 std::vector<std::filesystem::path> selLogFiles;
823 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800824 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700825 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800826 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800827
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700828 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800829
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800830 if (targetID == ipmi::sel::firstEntry)
831 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700832 // The first entry will be at the top of the oldest log file
833 std::ifstream logStream(selLogFiles.back());
834 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800835 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700836 return ipmi::responseUnspecifiedError();
837 }
838
839 if (!std::getline(logStream, targetEntry))
840 {
841 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800842 }
843 }
844 else if (targetID == ipmi::sel::lastEntry)
845 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700846 // The last entry will be at the bottom of the newest log file
847 std::ifstream logStream(selLogFiles.front());
848 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800849 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700850 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800851 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700852
853 std::string line;
854 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800855 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700856 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800857 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800858 }
859 else
860 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700861 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800862 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700863 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800864 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800865 }
866
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700867 // The format of the ipmi_sel message is "<Timestamp>
868 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
869 // First get the Timestamp
870 size_t space = targetEntry.find_first_of(" ");
871 if (space == std::string::npos)
872 {
873 return ipmi::responseUnspecifiedError();
874 }
875 std::string entryTimestamp = targetEntry.substr(0, space);
876 // Then get the log contents
877 size_t entryStart = targetEntry.find_first_not_of(" ", space);
878 if (entryStart == std::string::npos)
879 {
880 return ipmi::responseUnspecifiedError();
881 }
882 std::string_view entry(targetEntry);
883 entry.remove_prefix(entryStart);
884 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700886 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700887 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700888 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700889 {
890 return ipmi::responseUnspecifiedError();
891 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700892 std::string& recordIDStr = targetEntryFields[0];
893 std::string& recordTypeStr = targetEntryFields[1];
894 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700895
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700896 uint16_t recordID;
897 uint8_t recordType;
898 try
899 {
900 recordID = std::stoul(recordIDStr);
901 recordType = std::stoul(recordTypeStr, nullptr, 16);
902 }
903 catch (const std::invalid_argument&)
904 {
905 return ipmi::responseUnspecifiedError();
906 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700907 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700908 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700909 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700910 {
911 return ipmi::responseUnspecifiedError();
912 }
913
914 if (recordType == intel_oem::ipmi::sel::systemEvent)
915 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700916 // Get the timestamp
917 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700918 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700919
920 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
921 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
922 {
923 timestamp = std::mktime(&timeStruct);
924 }
925
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700926 // Set the event message revision
927 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
928
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700929 uint16_t generatorID = 0;
930 uint8_t sensorType = 0;
931 uint8_t sensorNum = 0xFF;
932 uint7_t eventType = 0;
933 bool eventDir = 0;
934 // System type events should have six fields
935 if (targetEntryFields.size() >= 6)
936 {
937 std::string& generatorIDStr = targetEntryFields[3];
938 std::string& sensorPath = targetEntryFields[4];
939 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700940
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700941 // Get the generator ID
942 try
943 {
944 generatorID = std::stoul(generatorIDStr, nullptr, 16);
945 }
946 catch (const std::invalid_argument&)
947 {
948 std::cerr << "Invalid Generator ID\n";
949 }
950
951 // Get the sensor type, sensor number, and event type for the sensor
952 sensorType = getSensorTypeFromPath(sensorPath);
953 sensorNum = getSensorNumberFromPath(sensorPath);
954 eventType = getSensorEventTypeFromPath(sensorPath);
955
956 // Get the event direction
957 try
958 {
959 eventDir = std::stoul(eventDirStr) ? 0 : 1;
960 }
961 catch (const std::invalid_argument&)
962 {
963 std::cerr << "Invalid Event Direction\n";
964 }
965 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700966
967 // Only keep the eventData bytes that fit in the record
968 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
969 std::copy_n(eventDataBytes.begin(),
970 std::min(eventDataBytes.size(), eventData.size()),
971 eventData.begin());
972
973 return ipmi::responseSuccess(
974 nextRecordID, recordID, recordType,
975 systemEventType{timestamp, generatorID, evmRev, sensorType,
976 sensorNum, eventType, eventDir, eventData});
977 }
978 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
979 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
980 {
981 // Get the timestamp
982 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700983 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700984
985 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
986 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
987 {
988 timestamp = std::mktime(&timeStruct);
989 }
990
991 // Only keep the bytes that fit in the record
992 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
993 std::copy_n(eventDataBytes.begin(),
994 std::min(eventDataBytes.size(), eventData.size()),
995 eventData.begin());
996
997 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
998 oemTsEventType{timestamp, eventData});
999 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001000 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001001 {
1002 // Only keep the bytes that fit in the record
1003 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1004 std::copy_n(eventDataBytes.begin(),
1005 std::min(eventDataBytes.size(), eventData.size()),
1006 eventData.begin());
1007
1008 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1009 eventData);
1010 }
1011
1012 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001013}
1014
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001015ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1016 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1017 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1018 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1019 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001020{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001021 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1022 // added
1023 cancelSELReservation();
1024
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001025 // Send this request to the Redfish hooks to log it as a Redfish message
1026 // instead. There is no need to add it to the SEL, so just return success.
1027 intel_oem::ipmi::sel::checkRedfishHooks(
1028 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1029 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001030
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001031 uint16_t responseID = 0xFFFF;
1032 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001033}
1034
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001035ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1036 uint16_t reservationID,
1037 const std::array<uint8_t, 3>& clr,
1038 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001039{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001040 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001041 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001042 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001043 }
1044
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001045 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1046 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001047 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001048 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001049 }
1050
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001051 // Erasure status cannot be fetched, so always return erasure status as
1052 // `erase completed`.
1053 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001054 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001055 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001056 }
1057
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001058 // Check that initiate erase is correct
1059 if (eraseOperation != ipmi::sel::initiateErase)
1060 {
1061 return ipmi::responseInvalidFieldRequest();
1062 }
1063
1064 // Per the IPMI spec, need to cancel any reservation when the SEL is
1065 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001066 cancelSELReservation();
1067
Jason M. Bills7944c302019-03-20 15:24:05 -07001068 // Save the erase time
1069 intel_oem::ipmi::sel::erase_time::save();
1070
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001071 // Clear the SEL by deleting the log files
1072 std::vector<std::filesystem::path> selLogFiles;
1073 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001074 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001075 for (const std::filesystem::path& file : selLogFiles)
1076 {
1077 std::error_code ec;
1078 std::filesystem::remove(file, ec);
1079 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001080 }
1081
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001082 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001083 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1084 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001085 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1086 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1087 rsyslogReload.append("rsyslog.service", "replace");
1088 try
1089 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001090 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001091 }
1092 catch (sdbusplus::exception_t& e)
1093 {
1094 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1095 }
1096
1097 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001098}
1099
Jason M. Bills1a474622019-06-14 14:51:33 -07001100ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1101{
1102 struct timespec selTime = {};
1103
1104 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1105 {
1106 return ipmi::responseUnspecifiedError();
1107 }
1108
1109 return ipmi::responseSuccess(selTime.tv_sec);
1110}
1111
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001112ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001113{
1114 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001115 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001116}
1117
James Feist74c50c62019-08-14 14:18:41 -07001118std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1119{
1120 std::vector<uint8_t> resp;
1121 if (index == 0)
1122 {
1123 Type12Record bmc = {};
1124 bmc.header.record_id_lsb = recordId;
1125 bmc.header.record_id_msb = recordId >> 8;
1126 bmc.header.sdr_version = ipmiSdrVersion;
1127 bmc.header.record_type = 0x12;
1128 bmc.header.record_length = 0x1b;
1129 bmc.slaveAddress = 0x20;
1130 bmc.channelNumber = 0;
1131 bmc.powerStateNotification = 0;
1132 bmc.deviceCapabilities = 0xBF;
1133 bmc.reserved = 0;
1134 bmc.entityID = 0x2E;
1135 bmc.entityInstance = 1;
1136 bmc.oem = 0;
1137 bmc.typeLengthCode = 0xD0;
1138 std::string bmcName = "Basbrd Mgmt Ctlr";
1139 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1140 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1141 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1142 }
1143 else if (index == 1)
1144 {
1145 Type12Record me = {};
1146 me.header.record_id_lsb = recordId;
1147 me.header.record_id_msb = recordId >> 8;
1148 me.header.sdr_version = ipmiSdrVersion;
1149 me.header.record_type = 0x12;
1150 me.header.record_length = 0x16;
1151 me.slaveAddress = 0x2C;
1152 me.channelNumber = 6;
1153 me.powerStateNotification = 0x24;
1154 me.deviceCapabilities = 0x21;
1155 me.reserved = 0;
1156 me.entityID = 0x2E;
1157 me.entityInstance = 2;
1158 me.oem = 0;
1159 me.typeLengthCode = 0xCB;
1160 std::string meName = "Mgmt Engine";
1161 std::copy(meName.begin(), meName.end(), me.name);
1162 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1163 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1164 }
1165 else
1166 {
1167 throw std::runtime_error("getType12SDRs:: Illegal index " +
1168 std::to_string(index));
1169 }
1170
1171 return resp;
1172}
1173
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001174void registerStorageFunctions()
1175{
1176 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001177 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1178 ipmi::storage::cmdGetFruInventoryAreaInfo,
1179 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001180 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001181 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1182 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1183 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001184
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001185 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001186 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1187 ipmi::storage::cmdWriteFruData,
1188 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001189
1190 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001191 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001192 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1193 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001194
1195 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001196 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001197 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1198 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001199
1200 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001201 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001202 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001203 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001204
1205 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001206 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1207 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1208 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001209
Jason M. Bills1a474622019-06-14 14:51:33 -07001210 // <Get SEL Time>
1211 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001212 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1213 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001214
Jason M. Billscac97a52019-01-30 14:43:46 -08001215 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001216 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1217 ipmi::storage::cmdSetSelTime,
1218 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001219}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001220} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001221} // namespace ipmi