blob: 1887581a43a0fc94d0a9fd7003c69a1a5673c8b7 [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"
Patrick Venturec2a07d42020-05-30 16:35:03 -070022#include "types.hpp"
Patrick Ventureca99ef52019-10-20 14:00:50 -070023
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070024#include <boost/algorithm/string.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070025#include <boost/container/flat_map.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080026#include <boost/process.hpp>
James Feist2a265d52019-04-08 11:16:27 -070027#include <ipmid/api.hpp>
James Feist25690252019-12-23 12:25:49 -080028#include <ipmid/message.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080029#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070030#include <phosphor-logging/log.hpp>
31#include <sdbusplus/message/types.hpp>
32#include <sdbusplus/timer.hpp>
James Feistfcd2d3a2020-05-28 10:38:15 -070033
34#include <filesystem>
35#include <functional>
36#include <iostream>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080037#include <stdexcept>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070038#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070039
Patrick Venture9ce789f2019-10-17 09:09:39 -070040static constexpr bool DEBUG = false;
41
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070042namespace intel_oem::ipmi::sel
43{
44static const std::filesystem::path selLogDir = "/var/log";
45static const std::string selLogFilename = "ipmi_sel";
46
47static int getFileTimestamp(const std::filesystem::path& file)
48{
49 struct stat st;
50
51 if (stat(file.c_str(), &st) >= 0)
52 {
53 return st.st_mtime;
54 }
55 return ::ipmi::sel::invalidTimeStamp;
56}
57
58namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070059{
60static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
61
62void save()
63{
64 // open the file, creating it if necessary
65 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
66 if (fd < 0)
67 {
68 std::cerr << "Failed to open file\n";
69 return;
70 }
71
72 // update the file timestamp to the current time
73 if (futimens(fd, NULL) < 0)
74 {
75 std::cerr << "Failed to update timestamp: "
76 << std::string(strerror(errno));
77 }
78 close(fd);
79}
80
81int get()
82{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070083 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070084}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070085} // namespace erase_time
86} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070087
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070088namespace ipmi
89{
90
91namespace storage
92{
93
Jason M. Billse2d1aee2018-10-03 15:57:18 -070094constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070095constexpr static const size_t maxFruSdrNameSize = 16;
96using ManagedObjectType = boost::container::flat_map<
97 sdbusplus::message::object_path,
98 boost::container::flat_map<
99 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
100using ManagedEntry = std::pair<
101 sdbusplus::message::object_path,
102 boost::container::flat_map<
103 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
104
James Feist3bcba452018-12-20 12:31:03 -0800105constexpr static const char* fruDeviceServiceName =
106 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700107constexpr static const char* entityManagerServiceName =
108 "xyz.openbmc_project.EntityManager";
James Feist25690252019-12-23 12:25:49 -0800109constexpr static const size_t cacheTimeoutSeconds = 30;
110constexpr static const size_t writeTimeoutSeconds = 10;
Anoop S358e7df2020-05-05 16:43:34 +0000111constexpr static const char* chassisTypeRackMount = "23";
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700112
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700113// event direction is bit[7] of eventType where 1b = Deassertion event
114constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800115
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700116static std::vector<uint8_t> fruCache;
117static uint8_t cacheBus = 0xFF;
118static uint8_t cacheAddr = 0XFF;
119
James Feist25690252019-12-23 12:25:49 -0800120static uint8_t writeBus = 0xFF;
121static uint8_t writeAddr = 0XFF;
122
123std::unique_ptr<phosphor::Timer> writeTimer = nullptr;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700124std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
125
James Feist25690252019-12-23 12:25:49 -0800126ManagedObjectType frus;
127
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700128// we unfortunately have to build a map of hashes in case there is a
129// collision to verify our dev-id
130boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
131
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700132void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700133
134bool writeFru()
135{
James Feist25690252019-12-23 12:25:49 -0800136 if (writeBus == 0xFF && writeAddr == 0xFF)
137 {
138 return true;
139 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700140 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
141 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700142 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
143 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
James Feist25690252019-12-23 12:25:49 -0800144 writeFru.append(writeBus, writeAddr, fruCache);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700145 try
146 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700147 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700148 }
149 catch (sdbusplus::exception_t&)
150 {
151 // todo: log sel?
152 phosphor::logging::log<phosphor::logging::level::ERR>(
153 "error writing fru");
154 return false;
155 }
James Feist25690252019-12-23 12:25:49 -0800156 writeBus = 0xFF;
157 writeAddr = 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700158 return true;
159}
160
James Feist25690252019-12-23 12:25:49 -0800161void createTimers()
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700162{
James Feist25690252019-12-23 12:25:49 -0800163
164 writeTimer = std::make_unique<phosphor::Timer>(writeFru);
165 cacheTimer = std::make_unique<phosphor::Timer>([]() { return; });
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700166}
167
James Feist25690252019-12-23 12:25:49 -0800168ipmi::Cc replaceCacheFru(ipmi::Context::ptr ctx, uint8_t devId)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700169{
170 static uint8_t lastDevId = 0xFF;
171
172 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
173 if (lastDevId == devId && timerRunning)
174 {
175 return IPMI_CC_OK; // cache already up to date
176 }
177 // if timer is running, stop it and writeFru manually
178 else if (timerRunning)
179 {
180 cacheTimer->stop();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700181 }
182
James Feist25690252019-12-23 12:25:49 -0800183 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
184 std::chrono::seconds(cacheTimeoutSeconds)));
185
186 boost::system::error_code ec;
187
188 frus = ctx->bus->yield_method_call<ManagedObjectType>(
189 ctx->yield, ec, fruDeviceServiceName, "/",
190 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
191 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700192 {
193 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feist25690252019-12-23 12:25:49 -0800194 "GetMangagedObjects for getSensorMap failed",
195 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
196
197 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700198 }
199
200 deviceHashes.clear();
201
202 // hash the object paths to create unique device id's. increment on
203 // collision
204 std::hash<std::string> hasher;
205 for (const auto& fru : frus)
206 {
207 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
208 if (fruIface == fru.second.end())
209 {
210 continue;
211 }
212
213 auto busFind = fruIface->second.find("BUS");
214 auto addrFind = fruIface->second.find("ADDRESS");
215 if (busFind == fruIface->second.end() ||
216 addrFind == fruIface->second.end())
217 {
218 phosphor::logging::log<phosphor::logging::level::INFO>(
219 "fru device missing Bus or Address",
220 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
221 continue;
222 }
223
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700224 uint8_t fruBus = std::get<uint32_t>(busFind->second);
225 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Anoop S358e7df2020-05-05 16:43:34 +0000226 auto chassisFind = fruIface->second.find("CHASSIS_TYPE");
227 std::string chassisType;
228 if (chassisFind != fruIface->second.end())
229 {
230 chassisType = std::get<std::string>(chassisFind->second);
231 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700232
233 uint8_t fruHash = 0;
Anoop S358e7df2020-05-05 16:43:34 +0000234 if (chassisType.compare(chassisTypeRackMount) != 0)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700235 {
236 fruHash = hasher(fru.first.str);
237 // can't be 0xFF based on spec, and 0 is reserved for baseboard
238 if (fruHash == 0 || fruHash == 0xFF)
239 {
240 fruHash = 1;
241 }
242 }
243 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
244
245 bool emplacePassed = false;
246 while (!emplacePassed)
247 {
248 auto resp = deviceHashes.emplace(fruHash, newDev);
249 emplacePassed = resp.second;
250 if (!emplacePassed)
251 {
252 fruHash++;
253 // can't be 0xFF based on spec, and 0 is reserved for
254 // baseboard
255 if (fruHash == 0XFF)
256 {
257 fruHash = 0x1;
258 }
259 }
260 }
261 }
262 auto deviceFind = deviceHashes.find(devId);
263 if (deviceFind == deviceHashes.end())
264 {
265 return IPMI_CC_SENSOR_INVALID;
266 }
267
268 fruCache.clear();
James Feist25690252019-12-23 12:25:49 -0800269 ec.clear();
270
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700271 cacheBus = deviceFind->second.first;
272 cacheAddr = deviceFind->second.second;
James Feist25690252019-12-23 12:25:49 -0800273
274 fruCache = ctx->bus->yield_method_call<std::vector<uint8_t>>(
275 ctx->yield, ec, fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
276 "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus,
277 cacheAddr);
278 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700279 {
James Feist25690252019-12-23 12:25:49 -0800280 phosphor::logging::log<phosphor::logging::level::ERR>(
281 "Couldn't get raw fru",
282 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
283
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700284 lastDevId = 0xFF;
285 cacheBus = 0xFF;
286 cacheAddr = 0xFF;
James Feist25690252019-12-23 12:25:49 -0800287 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700288 }
289
290 lastDevId = devId;
James Feist25690252019-12-23 12:25:49 -0800291 return ipmi::ccSuccess;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700292}
293
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000294/** @brief implements the read FRU data command
295 * @param fruDeviceId - FRU Device ID
296 * @param fruInventoryOffset - FRU Inventory Offset to write
297 * @param countToRead - Count to read
298 *
299 * @returns ipmi completion code plus response data
300 * - countWritten - Count written
301 */
302ipmi::RspType<uint8_t, // Count
303 std::vector<uint8_t> // Requested data
304 >
James Feist25690252019-12-23 12:25:49 -0800305 ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
306 uint16_t fruInventoryOffset, uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700307{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000308 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700309 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000310 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700311 }
312
James Feist25690252019-12-23 12:25:49 -0800313 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700314
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000315 if (status != ipmi::ccSuccess)
316 {
317 return ipmi::response(status);
318 }
319
320 size_t fromFruByteLen = 0;
321 if (countToRead + fruInventoryOffset < fruCache.size())
322 {
323 fromFruByteLen = countToRead;
324 }
325 else if (fruCache.size() > fruInventoryOffset)
326 {
327 fromFruByteLen = fruCache.size() - fruInventoryOffset;
328 }
329 else
330 {
srikanta mondal92108382020-02-27 18:53:20 +0000331 return ipmi::responseReqDataLenExceeded();
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000332 }
333
334 std::vector<uint8_t> requestedData;
335
336 requestedData.insert(
337 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
338 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
339
Patrick Venture70b17f92019-10-28 20:01:53 -0700340 return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
341 requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700342}
343
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000344/** @brief implements the write FRU data command
345 * @param fruDeviceId - FRU Device ID
346 * @param fruInventoryOffset - FRU Inventory Offset to write
347 * @param dataToWrite - Data to write
348 *
349 * @returns ipmi completion code plus response data
350 * - countWritten - Count written
351 */
352ipmi::RspType<uint8_t>
James Feist25690252019-12-23 12:25:49 -0800353 ipmiStorageWriteFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
354 uint16_t fruInventoryOffset,
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000355 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700356{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000357 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700358 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000359 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700360 }
361
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000362 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700363
James Feist25690252019-12-23 12:25:49 -0800364 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000365 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700366 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000367 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700368 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000369 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700370 if (fruCache.size() < lastWriteAddr)
371 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000372 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700373 }
374
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000375 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
376 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700377
378 bool atEnd = false;
379
380 if (fruCache.size() >= sizeof(FRUHeader))
381 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700382 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
383
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800384 int areaLength = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700385 int lastRecordStart = std::max(
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800386 {header->internalOffset, header->chassisOffset, header->boardOffset,
387 header->productOffset, header->multiRecordOffset});
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700388 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
389
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800390 if (header->multiRecordOffset)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700391 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800392 // This FRU has a MultiRecord Area
393 uint8_t endOfList = 0;
394 // Walk the MultiRecord headers until the last record
395 while (!endOfList)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700396 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800397 // The MSB in the second byte of the MultiRecord header signals
398 // "End of list"
399 endOfList = fruCache[lastRecordStart + 1] & 0x80;
400 // Third byte in the MultiRecord header is the length
401 areaLength = fruCache[lastRecordStart + 2];
402 // This length is in bytes (not 8 bytes like other headers)
403 areaLength += 5; // The length omits the 5 byte header
404 if (!endOfList)
405 {
406 // Next MultiRecord header
407 lastRecordStart += areaLength;
408 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700409 }
410 }
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800411 else
412 {
413 // This FRU does not have a MultiRecord Area
414 // Get the length of the area in multiples of 8 bytes
415 if (lastWriteAddr > (lastRecordStart + 1))
416 {
417 // second byte in record area is the length
418 areaLength = fruCache[lastRecordStart + 1];
419 areaLength *= 8; // it is in multiples of 8 bytes
420 }
421 }
422 if (lastWriteAddr >= (areaLength + lastRecordStart))
423 {
424 atEnd = true;
425 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700426 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000427 uint8_t countWritten = 0;
James Feist25690252019-12-23 12:25:49 -0800428
429 writeBus = cacheBus;
430 writeAddr = cacheAddr;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700431 if (atEnd)
432 {
433 // cancel timer, we're at the end so might as well send it
James Feist25690252019-12-23 12:25:49 -0800434 writeTimer->stop();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700435 if (!writeFru())
436 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000437 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700438 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000439 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700440 }
441 else
442 {
James Feist25690252019-12-23 12:25:49 -0800443 // start a timer, if no further data is sent to check to see if it is
444 // valid
445 writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
446 std::chrono::seconds(writeTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000447 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700448 }
449
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000450 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700451}
452
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000453/** @brief implements the get FRU inventory area info command
454 * @param fruDeviceId - FRU Device ID
455 *
456 * @returns IPMI completion code plus response data
457 * - inventorySize - Number of possible allocation units
458 * - accessType - Allocation unit size in bytes.
459 */
460ipmi::RspType<uint16_t, // inventorySize
461 uint8_t> // accessType
James Feist25690252019-12-23 12:25:49 -0800462 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700463{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000464 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700465 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000466 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700467 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700468
James Feist25690252019-12-23 12:25:49 -0800469 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700470
471 if (status != IPMI_CC_OK)
472 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000473 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700474 }
475
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000476 constexpr uint8_t accessType =
477 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700478
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000479 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700480}
481
James Feist25690252019-12-23 12:25:49 -0800482ipmi_ret_t getFruSdrCount(ipmi::Context::ptr ctx, size_t& count)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700483{
James Feist25690252019-12-23 12:25:49 -0800484 ipmi_ret_t ret = replaceCacheFru(ctx, 0);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700485 if (ret != IPMI_CC_OK)
486 {
487 return ret;
488 }
489 count = deviceHashes.size();
490 return IPMI_CC_OK;
491}
492
James Feist25690252019-12-23 12:25:49 -0800493ipmi_ret_t getFruSdrs(ipmi::Context::ptr ctx, size_t index,
494 get_sdr::SensorDataFruRecord& resp)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700495{
James Feist25690252019-12-23 12:25:49 -0800496 ipmi_ret_t ret = replaceCacheFru(ctx, 0); // this will update the hash list
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700497 if (ret != IPMI_CC_OK)
498 {
499 return ret;
500 }
501 if (deviceHashes.size() < index)
502 {
503 return IPMI_CC_INVALID_FIELD_REQUEST;
504 }
505 auto device = deviceHashes.begin() + index;
506 uint8_t& bus = device->second.first;
507 uint8_t& address = device->second.second;
508
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700509 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
510 auto fru =
511 std::find_if(frus.begin(), frus.end(),
512 [bus, address, &fruData](ManagedEntry& entry) {
513 auto findFruDevice =
514 entry.second.find("xyz.openbmc_project.FruDevice");
515 if (findFruDevice == entry.second.end())
516 {
517 return false;
518 }
519 fruData = &(findFruDevice->second);
520 auto findBus = findFruDevice->second.find("BUS");
521 auto findAddress =
522 findFruDevice->second.find("ADDRESS");
523 if (findBus == findFruDevice->second.end() ||
524 findAddress == findFruDevice->second.end())
525 {
526 return false;
527 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700528 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700529 {
530 return false;
531 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700532 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700533 {
534 return false;
535 }
536 return true;
537 });
538 if (fru == frus.end())
539 {
540 return IPMI_CC_RESPONSE_ERROR;
541 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700542
James Feist25690252019-12-23 12:25:49 -0800543#ifdef USING_ENTITY_MANAGER_DECORATORS
544
Patrick Venture9ce789f2019-10-17 09:09:39 -0700545 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700546
James Feist25690252019-12-23 12:25:49 -0800547 // todo: this should really use caching, this is a very inefficient lookup
548 boost::system::error_code ec;
549 ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
550 ctx->yield, ec, entityManagerServiceName, "/",
551 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
552
553 if (ec)
Patrick Venture9ce789f2019-10-17 09:09:39 -0700554 {
James Feist25690252019-12-23 12:25:49 -0800555 phosphor::logging::log<phosphor::logging::level::ERR>(
556 "GetMangagedObjects for getSensorMap failed",
557 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
Patrick Venture9ce789f2019-10-17 09:09:39 -0700558
James Feist25690252019-12-23 12:25:49 -0800559 return ipmi::ccResponseError;
560 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700561
James Feist25690252019-12-23 12:25:49 -0800562 auto entity = std::find_if(
563 entities.begin(), entities.end(),
564 [bus, address, &entityData](ManagedEntry& entry) {
565 auto findFruDevice = entry.second.find(
566 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
567 if (findFruDevice == entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700568 {
James Feist25690252019-12-23 12:25:49 -0800569 return false;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700570 }
James Feist25690252019-12-23 12:25:49 -0800571
572 // Integer fields added via Entity-Manager json are uint64_ts by
573 // default.
574 auto findBus = findFruDevice->second.find("Bus");
575 auto findAddress = findFruDevice->second.find("Address");
576
577 if (findBus == findFruDevice->second.end() ||
578 findAddress == findFruDevice->second.end())
579 {
580 return false;
581 }
582 if ((std::get<uint64_t>(findBus->second) != bus) ||
583 (std::get<uint64_t>(findAddress->second) != address))
584 {
585 return false;
586 }
587
588 // At this point we found the device entry and should return
589 // true.
590 auto findIpmiDevice = entry.second.find(
591 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
592 if (findIpmiDevice != entry.second.end())
593 {
594 entityData = &(findIpmiDevice->second);
595 }
596
597 return true;
598 });
599
600 if (entity == entities.end())
601 {
602 if constexpr (DEBUG)
603 {
604 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
605 "not found for Fru\n");
Patrick Venture9ce789f2019-10-17 09:09:39 -0700606 }
607 }
James Feist25690252019-12-23 12:25:49 -0800608
609#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700610
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700611 std::string name;
612 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
613 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
614 if (findProductName != fruData->end())
615 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700616 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700617 }
618 else if (findBoardName != fruData->end())
619 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700620 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700621 }
622 else
623 {
624 name = "UNKNOWN";
625 }
626 if (name.size() > maxFruSdrNameSize)
627 {
628 name = name.substr(0, maxFruSdrNameSize);
629 }
630 size_t sizeDiff = maxFruSdrNameSize - name.size();
631
632 resp.header.record_id_lsb = 0x0; // calling code is to implement these
633 resp.header.record_id_msb = 0x0;
634 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700635 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700636 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
637 resp.key.deviceAddress = 0x20;
638 resp.key.fruID = device->first;
639 resp.key.accessLun = 0x80; // logical / physical fru device
640 resp.key.channelNumber = 0x0;
641 resp.body.reserved = 0x0;
642 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700643 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700644
645 uint8_t entityID = 0;
646 uint8_t entityInstance = 0x1;
647
James Feist25690252019-12-23 12:25:49 -0800648#ifdef USING_ENTITY_MANAGER_DECORATORS
Patrick Venture9ce789f2019-10-17 09:09:39 -0700649 if (entityData)
650 {
651 auto entityIdProperty = entityData->find("EntityId");
652 auto entityInstanceProperty = entityData->find("EntityInstance");
653
654 if (entityIdProperty != entityData->end())
655 {
656 entityID = static_cast<uint8_t>(
657 std::get<uint64_t>(entityIdProperty->second));
658 }
659 if (entityInstanceProperty != entityData->end())
660 {
661 entityInstance = static_cast<uint8_t>(
662 std::get<uint64_t>(entityInstanceProperty->second));
663 }
664 }
James Feist25690252019-12-23 12:25:49 -0800665#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700666
667 resp.body.entityID = entityID;
668 resp.body.entityInstance = entityInstance;
669
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700670 resp.body.oem = 0x0;
671 resp.body.deviceIDLen = name.size();
672 name.copy(resp.body.deviceID, name.size());
673
674 return IPMI_CC_OK;
675}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700676
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700677static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800678{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700679 // Loop through the directory looking for ipmi_sel log files
680 for (const std::filesystem::directory_entry& dirEnt :
681 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800682 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700683 std::string filename = dirEnt.path().filename();
684 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800685 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700686 // If we find an ipmi_sel log file, save the path
687 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
688 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800689 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800690 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700691 // As the log files rotate, they are appended with a ".#" that is higher for
692 // the older logs. Since we don't expect more than 10 log files, we
693 // can just sort the list to get them in order from newest to oldest
694 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800695
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700696 return !selLogFiles.empty();
697}
698
699static int countSELEntries()
700{
701 // Get the list of ipmi_sel log files
702 std::vector<std::filesystem::path> selLogFiles;
703 if (!getSELLogFiles(selLogFiles))
704 {
705 return 0;
706 }
707 int numSELEntries = 0;
708 // Loop through each log file and count the number of logs
709 for (const std::filesystem::path& file : selLogFiles)
710 {
711 std::ifstream logStream(file);
712 if (!logStream.is_open())
713 {
714 continue;
715 }
716
717 std::string line;
718 while (std::getline(logStream, line))
719 {
720 numSELEntries++;
721 }
722 }
723 return numSELEntries;
724}
725
726static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700727 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700728 std::string& entry)
729{
730 // Record ID is the first entry field following the timestamp. It is
731 // preceded by a space and followed by a comma
732 std::string search = " " + std::to_string(recordID) + ",";
733
734 // Loop through the ipmi_sel log entries
735 for (const std::filesystem::path& file : selLogFiles)
736 {
737 std::ifstream logStream(file);
738 if (!logStream.is_open())
739 {
740 continue;
741 }
742
743 while (std::getline(logStream, entry))
744 {
745 // Check if the record ID matches
746 if (entry.find(search) != std::string::npos)
747 {
748 return true;
749 }
750 }
751 }
752 return false;
753}
754
755static uint16_t
756 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700757 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700758{
759 uint16_t nextRecordID = recordID + 1;
760 std::string entry;
761 if (findSELEntry(nextRecordID, selLogFiles, entry))
762 {
763 return nextRecordID;
764 }
765 else
766 {
767 return ipmi::sel::lastEntry;
768 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800769}
770
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700771static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800772{
773 for (unsigned int i = 0; i < hexStr.size(); i += 2)
774 {
775 try
776 {
777 data.push_back(static_cast<uint8_t>(
778 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
779 }
780 catch (std::invalid_argument& e)
781 {
782 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
783 return -1;
784 }
785 catch (std::out_of_range& e)
786 {
787 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
788 return -1;
789 }
790 }
791 return 0;
792}
793
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700794ipmi::RspType<uint8_t, // SEL version
795 uint16_t, // SEL entry count
796 uint16_t, // free space
797 uint32_t, // last add timestamp
798 uint32_t, // last erase timestamp
799 uint8_t> // operation support
800 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800801{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700802 constexpr uint8_t selVersion = ipmi::sel::selVersion;
803 uint16_t entries = countSELEntries();
804 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
805 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
806 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
807 constexpr uint8_t operationSupport =
808 intel_oem::ipmi::sel::selOperationSupport;
809 constexpr uint16_t freeSpace =
810 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800811
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700812 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
813 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800814}
815
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700816using systemEventType = std::tuple<
817 uint32_t, // Timestamp
818 uint16_t, // Generator ID
819 uint8_t, // EvM Rev
820 uint8_t, // Sensor Type
821 uint8_t, // Sensor Number
822 uint7_t, // Event Type
823 bool, // Event Direction
824 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
825using oemTsEventType = std::tuple<
826 uint32_t, // Timestamp
827 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
828using oemEventType =
829 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800830
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700831ipmi::RspType<uint16_t, // Next Record ID
832 uint16_t, // Record ID
833 uint8_t, // Record Type
834 std::variant<systemEventType, oemTsEventType,
835 oemEventType>> // Record Content
836 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
837 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800838{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700839 // Only support getting the entire SEL record. If a partial size or non-zero
840 // offset is requested, return an error
841 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800842 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700843 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800844 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800845
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700846 // Check the reservation ID if one is provided or required (only if the
847 // offset is non-zero)
848 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800849 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700850 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800851 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700852 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800853 }
854 }
855
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700856 // Get the ipmi_sel log files
857 std::vector<std::filesystem::path> selLogFiles;
858 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800859 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700860 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800861 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800862
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700863 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800864
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800865 if (targetID == ipmi::sel::firstEntry)
866 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700867 // The first entry will be at the top of the oldest log file
868 std::ifstream logStream(selLogFiles.back());
869 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800870 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700871 return ipmi::responseUnspecifiedError();
872 }
873
874 if (!std::getline(logStream, targetEntry))
875 {
876 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800877 }
878 }
879 else if (targetID == ipmi::sel::lastEntry)
880 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700881 // The last entry will be at the bottom of the newest log file
882 std::ifstream logStream(selLogFiles.front());
883 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800884 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800886 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700887
888 std::string line;
889 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800890 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700891 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800892 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800893 }
894 else
895 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700896 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800897 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700898 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800899 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800900 }
901
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700902 // The format of the ipmi_sel message is "<Timestamp>
903 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
904 // First get the Timestamp
905 size_t space = targetEntry.find_first_of(" ");
906 if (space == std::string::npos)
907 {
908 return ipmi::responseUnspecifiedError();
909 }
910 std::string entryTimestamp = targetEntry.substr(0, space);
911 // Then get the log contents
912 size_t entryStart = targetEntry.find_first_not_of(" ", space);
913 if (entryStart == std::string::npos)
914 {
915 return ipmi::responseUnspecifiedError();
916 }
917 std::string_view entry(targetEntry);
918 entry.remove_prefix(entryStart);
919 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700920 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700921 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700922 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700923 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700924 {
925 return ipmi::responseUnspecifiedError();
926 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700927 std::string& recordIDStr = targetEntryFields[0];
928 std::string& recordTypeStr = targetEntryFields[1];
929 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700930
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700931 uint16_t recordID;
932 uint8_t recordType;
933 try
934 {
935 recordID = std::stoul(recordIDStr);
936 recordType = std::stoul(recordTypeStr, nullptr, 16);
937 }
938 catch (const std::invalid_argument&)
939 {
940 return ipmi::responseUnspecifiedError();
941 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700942 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700943 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700944 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700945 {
946 return ipmi::responseUnspecifiedError();
947 }
948
949 if (recordType == intel_oem::ipmi::sel::systemEvent)
950 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700951 // Get the timestamp
952 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700953 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700954
955 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
956 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
957 {
958 timestamp = std::mktime(&timeStruct);
959 }
960
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700961 // Set the event message revision
962 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
963
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700964 uint16_t generatorID = 0;
965 uint8_t sensorType = 0;
966 uint8_t sensorNum = 0xFF;
967 uint7_t eventType = 0;
968 bool eventDir = 0;
969 // System type events should have six fields
970 if (targetEntryFields.size() >= 6)
971 {
972 std::string& generatorIDStr = targetEntryFields[3];
973 std::string& sensorPath = targetEntryFields[4];
974 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700975
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700976 // Get the generator ID
977 try
978 {
979 generatorID = std::stoul(generatorIDStr, nullptr, 16);
980 }
981 catch (const std::invalid_argument&)
982 {
983 std::cerr << "Invalid Generator ID\n";
984 }
985
986 // Get the sensor type, sensor number, and event type for the sensor
987 sensorType = getSensorTypeFromPath(sensorPath);
988 sensorNum = getSensorNumberFromPath(sensorPath);
989 eventType = getSensorEventTypeFromPath(sensorPath);
990
991 // Get the event direction
992 try
993 {
994 eventDir = std::stoul(eventDirStr) ? 0 : 1;
995 }
996 catch (const std::invalid_argument&)
997 {
998 std::cerr << "Invalid Event Direction\n";
999 }
1000 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001001
1002 // Only keep the eventData bytes that fit in the record
1003 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
1004 std::copy_n(eventDataBytes.begin(),
1005 std::min(eventDataBytes.size(), eventData.size()),
1006 eventData.begin());
1007
1008 return ipmi::responseSuccess(
1009 nextRecordID, recordID, recordType,
1010 systemEventType{timestamp, generatorID, evmRev, sensorType,
1011 sensorNum, eventType, eventDir, eventData});
1012 }
1013 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
1014 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
1015 {
1016 // Get the timestamp
1017 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -07001018 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001019
1020 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1021 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1022 {
1023 timestamp = std::mktime(&timeStruct);
1024 }
1025
1026 // Only keep the bytes that fit in the record
1027 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
1028 std::copy_n(eventDataBytes.begin(),
1029 std::min(eventDataBytes.size(), eventData.size()),
1030 eventData.begin());
1031
1032 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1033 oemTsEventType{timestamp, eventData});
1034 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001035 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001036 {
1037 // Only keep the bytes that fit in the record
1038 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1039 std::copy_n(eventDataBytes.begin(),
1040 std::min(eventDataBytes.size(), eventData.size()),
1041 eventData.begin());
1042
1043 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1044 eventData);
1045 }
1046
1047 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001048}
1049
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001050ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1051 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1052 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1053 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1054 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001055{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001056 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1057 // added
1058 cancelSELReservation();
1059
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001060 // Send this request to the Redfish hooks to log it as a Redfish message
1061 // instead. There is no need to add it to the SEL, so just return success.
1062 intel_oem::ipmi::sel::checkRedfishHooks(
1063 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1064 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001065
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001066 uint16_t responseID = 0xFFFF;
1067 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001068}
1069
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001070ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1071 uint16_t reservationID,
1072 const std::array<uint8_t, 3>& clr,
1073 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001074{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001075 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001076 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001077 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001078 }
1079
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001080 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1081 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001082 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001083 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001084 }
1085
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001086 // Erasure status cannot be fetched, so always return erasure status as
1087 // `erase completed`.
1088 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001089 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001090 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001091 }
1092
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001093 // Check that initiate erase is correct
1094 if (eraseOperation != ipmi::sel::initiateErase)
1095 {
1096 return ipmi::responseInvalidFieldRequest();
1097 }
1098
1099 // Per the IPMI spec, need to cancel any reservation when the SEL is
1100 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001101 cancelSELReservation();
1102
Jason M. Bills7944c302019-03-20 15:24:05 -07001103 // Save the erase time
1104 intel_oem::ipmi::sel::erase_time::save();
1105
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001106 // Clear the SEL by deleting the log files
1107 std::vector<std::filesystem::path> selLogFiles;
1108 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001109 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001110 for (const std::filesystem::path& file : selLogFiles)
1111 {
1112 std::error_code ec;
1113 std::filesystem::remove(file, ec);
1114 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001115 }
1116
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001117 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001118 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1119 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001120 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1121 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1122 rsyslogReload.append("rsyslog.service", "replace");
1123 try
1124 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001125 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001126 }
1127 catch (sdbusplus::exception_t& e)
1128 {
1129 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1130 }
1131
1132 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001133}
1134
Jason M. Bills1a474622019-06-14 14:51:33 -07001135ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1136{
1137 struct timespec selTime = {};
1138
1139 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1140 {
1141 return ipmi::responseUnspecifiedError();
1142 }
1143
1144 return ipmi::responseSuccess(selTime.tv_sec);
1145}
1146
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001147ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001148{
1149 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001150 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001151}
1152
James Feist74c50c62019-08-14 14:18:41 -07001153std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1154{
1155 std::vector<uint8_t> resp;
1156 if (index == 0)
1157 {
1158 Type12Record bmc = {};
1159 bmc.header.record_id_lsb = recordId;
1160 bmc.header.record_id_msb = recordId >> 8;
1161 bmc.header.sdr_version = ipmiSdrVersion;
1162 bmc.header.record_type = 0x12;
1163 bmc.header.record_length = 0x1b;
1164 bmc.slaveAddress = 0x20;
1165 bmc.channelNumber = 0;
1166 bmc.powerStateNotification = 0;
1167 bmc.deviceCapabilities = 0xBF;
1168 bmc.reserved = 0;
1169 bmc.entityID = 0x2E;
1170 bmc.entityInstance = 1;
1171 bmc.oem = 0;
1172 bmc.typeLengthCode = 0xD0;
1173 std::string bmcName = "Basbrd Mgmt Ctlr";
1174 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1175 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1176 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1177 }
1178 else if (index == 1)
1179 {
1180 Type12Record me = {};
1181 me.header.record_id_lsb = recordId;
1182 me.header.record_id_msb = recordId >> 8;
1183 me.header.sdr_version = ipmiSdrVersion;
1184 me.header.record_type = 0x12;
1185 me.header.record_length = 0x16;
1186 me.slaveAddress = 0x2C;
1187 me.channelNumber = 6;
1188 me.powerStateNotification = 0x24;
1189 me.deviceCapabilities = 0x21;
1190 me.reserved = 0;
1191 me.entityID = 0x2E;
1192 me.entityInstance = 2;
1193 me.oem = 0;
1194 me.typeLengthCode = 0xCB;
1195 std::string meName = "Mgmt Engine";
1196 std::copy(meName.begin(), meName.end(), me.name);
1197 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1198 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1199 }
1200 else
1201 {
1202 throw std::runtime_error("getType12SDRs:: Illegal index " +
1203 std::to_string(index));
1204 }
1205
1206 return resp;
1207}
1208
Yong Lifee5e4c2020-01-17 19:36:29 +08001209std::vector<uint8_t> getNMDiscoverySDR(uint16_t index, uint16_t recordId)
1210{
1211 std::vector<uint8_t> resp;
1212 if (index == 0)
1213 {
1214 NMDiscoveryRecord nm = {};
1215 nm.header.record_id_lsb = recordId;
1216 nm.header.record_id_msb = recordId >> 8;
1217 nm.header.sdr_version = ipmiSdrVersion;
1218 nm.header.record_type = 0xC0;
1219 nm.header.record_length = 0xB;
1220 nm.oemID0 = 0x57;
1221 nm.oemID1 = 0x1;
1222 nm.oemID2 = 0x0;
1223 nm.subType = 0x0D;
1224 nm.version = 0x1;
1225 nm.slaveAddress = 0x2C;
1226 nm.channelNumber = 0x60;
1227 nm.healthEventSensor = 0x19;
1228 nm.exceptionEventSensor = 0x18;
1229 nm.operationalCapSensor = 0x1A;
1230 nm.thresholdExceededSensor = 0x1B;
1231
1232 uint8_t* nmPtr = reinterpret_cast<uint8_t*>(&nm);
1233 resp.insert(resp.end(), nmPtr, nmPtr + sizeof(NMDiscoveryRecord));
1234 }
1235 else
1236 {
1237 throw std::runtime_error("getNMDiscoverySDR:: Illegal index " +
1238 std::to_string(index));
1239 }
1240
1241 return resp;
1242}
1243
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001244void registerStorageFunctions()
1245{
James Feist25690252019-12-23 12:25:49 -08001246 createTimers();
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001247 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001248 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1249 ipmi::storage::cmdGetFruInventoryAreaInfo,
1250 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001251 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001252 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1253 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1254 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001255
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001256 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001257 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1258 ipmi::storage::cmdWriteFruData,
1259 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001260
1261 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001262 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001263 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1264 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001265
1266 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001267 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001268 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1269 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001270
1271 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001272 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001273 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001274 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001275
1276 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001277 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1278 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1279 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001280
Jason M. Bills1a474622019-06-14 14:51:33 -07001281 // <Get SEL Time>
1282 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001283 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1284 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001285
Jason M. Billscac97a52019-01-30 14:43:46 -08001286 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001287 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1288 ipmi::storage::cmdSetSelTime,
1289 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001290}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001291} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001292} // namespace ipmi