blob: 2b812a33b11193c669d4fb7ce50666644d55cbb3 [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>
James Feist25690252019-12-23 12:25:49 -080030#include <ipmid/message.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080031#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070032#include <phosphor-logging/log.hpp>
33#include <sdbusplus/message/types.hpp>
34#include <sdbusplus/timer.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080035#include <stdexcept>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070036#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070037
Patrick Venture9ce789f2019-10-17 09:09:39 -070038static constexpr bool DEBUG = false;
39
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070040namespace intel_oem::ipmi::sel
41{
42static const std::filesystem::path selLogDir = "/var/log";
43static const std::string selLogFilename = "ipmi_sel";
44
45static int getFileTimestamp(const std::filesystem::path& file)
46{
47 struct stat st;
48
49 if (stat(file.c_str(), &st) >= 0)
50 {
51 return st.st_mtime;
52 }
53 return ::ipmi::sel::invalidTimeStamp;
54}
55
56namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070057{
58static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
59
60void save()
61{
62 // open the file, creating it if necessary
63 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
64 if (fd < 0)
65 {
66 std::cerr << "Failed to open file\n";
67 return;
68 }
69
70 // update the file timestamp to the current time
71 if (futimens(fd, NULL) < 0)
72 {
73 std::cerr << "Failed to update timestamp: "
74 << std::string(strerror(errno));
75 }
76 close(fd);
77}
78
79int get()
80{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070081 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070082}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070083} // namespace erase_time
84} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070085
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070086namespace ipmi
87{
88
89namespace storage
90{
91
Jason M. Billse2d1aee2018-10-03 15:57:18 -070092constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070093constexpr static const size_t maxFruSdrNameSize = 16;
94using ManagedObjectType = boost::container::flat_map<
95 sdbusplus::message::object_path,
96 boost::container::flat_map<
97 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
98using ManagedEntry = std::pair<
99 sdbusplus::message::object_path,
100 boost::container::flat_map<
101 std::string, boost::container::flat_map<std::string, DbusVariant>>>;
102
James Feist3bcba452018-12-20 12:31:03 -0800103constexpr static const char* fruDeviceServiceName =
104 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700105constexpr static const char* entityManagerServiceName =
106 "xyz.openbmc_project.EntityManager";
James Feist25690252019-12-23 12:25:49 -0800107constexpr static const size_t cacheTimeoutSeconds = 30;
108constexpr static const size_t writeTimeoutSeconds = 10;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700109
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700110// event direction is bit[7] of eventType where 1b = Deassertion event
111constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800112
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700113static std::vector<uint8_t> fruCache;
114static uint8_t cacheBus = 0xFF;
115static uint8_t cacheAddr = 0XFF;
116
James Feist25690252019-12-23 12:25:49 -0800117static uint8_t writeBus = 0xFF;
118static uint8_t writeAddr = 0XFF;
119
120std::unique_ptr<phosphor::Timer> writeTimer = nullptr;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700121std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
122
James Feist25690252019-12-23 12:25:49 -0800123ManagedObjectType frus;
124
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700125// we unfortunately have to build a map of hashes in case there is a
126// collision to verify our dev-id
127boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
128
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700129void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700130
131bool writeFru()
132{
James Feist25690252019-12-23 12:25:49 -0800133 if (writeBus == 0xFF && writeAddr == 0xFF)
134 {
135 return true;
136 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700137 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
138 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700139 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
140 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
James Feist25690252019-12-23 12:25:49 -0800141 writeFru.append(writeBus, writeAddr, fruCache);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700142 try
143 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700144 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700145 }
146 catch (sdbusplus::exception_t&)
147 {
148 // todo: log sel?
149 phosphor::logging::log<phosphor::logging::level::ERR>(
150 "error writing fru");
151 return false;
152 }
James Feist25690252019-12-23 12:25:49 -0800153 writeBus = 0xFF;
154 writeAddr = 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700155 return true;
156}
157
James Feist25690252019-12-23 12:25:49 -0800158void createTimers()
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700159{
James Feist25690252019-12-23 12:25:49 -0800160
161 writeTimer = std::make_unique<phosphor::Timer>(writeFru);
162 cacheTimer = std::make_unique<phosphor::Timer>([]() { return; });
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700163}
164
James Feist25690252019-12-23 12:25:49 -0800165ipmi::Cc replaceCacheFru(ipmi::Context::ptr ctx, uint8_t devId)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700166{
167 static uint8_t lastDevId = 0xFF;
168
169 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
170 if (lastDevId == devId && timerRunning)
171 {
172 return IPMI_CC_OK; // cache already up to date
173 }
174 // if timer is running, stop it and writeFru manually
175 else if (timerRunning)
176 {
177 cacheTimer->stop();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700178 }
179
James Feist25690252019-12-23 12:25:49 -0800180 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
181 std::chrono::seconds(cacheTimeoutSeconds)));
182
183 boost::system::error_code ec;
184
185 frus = ctx->bus->yield_method_call<ManagedObjectType>(
186 ctx->yield, ec, fruDeviceServiceName, "/",
187 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
188 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700189 {
190 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feist25690252019-12-23 12:25:49 -0800191 "GetMangagedObjects for getSensorMap failed",
192 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
193
194 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700195 }
196
197 deviceHashes.clear();
198
199 // hash the object paths to create unique device id's. increment on
200 // collision
201 std::hash<std::string> hasher;
202 for (const auto& fru : frus)
203 {
204 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
205 if (fruIface == fru.second.end())
206 {
207 continue;
208 }
209
210 auto busFind = fruIface->second.find("BUS");
211 auto addrFind = fruIface->second.find("ADDRESS");
212 if (busFind == fruIface->second.end() ||
213 addrFind == fruIface->second.end())
214 {
215 phosphor::logging::log<phosphor::logging::level::INFO>(
216 "fru device missing Bus or Address",
217 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
218 continue;
219 }
220
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700221 uint8_t fruBus = std::get<uint32_t>(busFind->second);
222 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700223
224 uint8_t fruHash = 0;
225 if (fruBus != 0 || fruAddr != 0)
226 {
227 fruHash = hasher(fru.first.str);
228 // can't be 0xFF based on spec, and 0 is reserved for baseboard
229 if (fruHash == 0 || fruHash == 0xFF)
230 {
231 fruHash = 1;
232 }
233 }
234 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
235
236 bool emplacePassed = false;
237 while (!emplacePassed)
238 {
239 auto resp = deviceHashes.emplace(fruHash, newDev);
240 emplacePassed = resp.second;
241 if (!emplacePassed)
242 {
243 fruHash++;
244 // can't be 0xFF based on spec, and 0 is reserved for
245 // baseboard
246 if (fruHash == 0XFF)
247 {
248 fruHash = 0x1;
249 }
250 }
251 }
252 }
253 auto deviceFind = deviceHashes.find(devId);
254 if (deviceFind == deviceHashes.end())
255 {
256 return IPMI_CC_SENSOR_INVALID;
257 }
258
259 fruCache.clear();
James Feist25690252019-12-23 12:25:49 -0800260 ec.clear();
261
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700262 cacheBus = deviceFind->second.first;
263 cacheAddr = deviceFind->second.second;
James Feist25690252019-12-23 12:25:49 -0800264
265 fruCache = ctx->bus->yield_method_call<std::vector<uint8_t>>(
266 ctx->yield, ec, fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
267 "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus,
268 cacheAddr);
269 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700270 {
James Feist25690252019-12-23 12:25:49 -0800271 phosphor::logging::log<phosphor::logging::level::ERR>(
272 "Couldn't get raw fru",
273 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
274
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700275 lastDevId = 0xFF;
276 cacheBus = 0xFF;
277 cacheAddr = 0xFF;
James Feist25690252019-12-23 12:25:49 -0800278 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700279 }
280
281 lastDevId = devId;
James Feist25690252019-12-23 12:25:49 -0800282 return ipmi::ccSuccess;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700283}
284
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000285/** @brief implements the read FRU data command
286 * @param fruDeviceId - FRU Device ID
287 * @param fruInventoryOffset - FRU Inventory Offset to write
288 * @param countToRead - Count to read
289 *
290 * @returns ipmi completion code plus response data
291 * - countWritten - Count written
292 */
293ipmi::RspType<uint8_t, // Count
294 std::vector<uint8_t> // Requested data
295 >
James Feist25690252019-12-23 12:25:49 -0800296 ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
297 uint16_t fruInventoryOffset, uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700298{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000299 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700300 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000301 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700302 }
303
James Feist25690252019-12-23 12:25:49 -0800304 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700305
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000306 if (status != ipmi::ccSuccess)
307 {
308 return ipmi::response(status);
309 }
310
311 size_t fromFruByteLen = 0;
312 if (countToRead + fruInventoryOffset < fruCache.size())
313 {
314 fromFruByteLen = countToRead;
315 }
316 else if (fruCache.size() > fruInventoryOffset)
317 {
318 fromFruByteLen = fruCache.size() - fruInventoryOffset;
319 }
320 else
321 {
srikanta mondal92108382020-02-27 18:53:20 +0000322 return ipmi::responseReqDataLenExceeded();
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000323 }
324
325 std::vector<uint8_t> requestedData;
326
327 requestedData.insert(
328 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
329 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
330
Patrick Venture70b17f92019-10-28 20:01:53 -0700331 return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
332 requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700333}
334
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000335/** @brief implements the write FRU data command
336 * @param fruDeviceId - FRU Device ID
337 * @param fruInventoryOffset - FRU Inventory Offset to write
338 * @param dataToWrite - Data to write
339 *
340 * @returns ipmi completion code plus response data
341 * - countWritten - Count written
342 */
343ipmi::RspType<uint8_t>
James Feist25690252019-12-23 12:25:49 -0800344 ipmiStorageWriteFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
345 uint16_t fruInventoryOffset,
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000346 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700347{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000348 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700349 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000350 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700351 }
352
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000353 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700354
James Feist25690252019-12-23 12:25:49 -0800355 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000356 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700357 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000358 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700359 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000360 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700361 if (fruCache.size() < lastWriteAddr)
362 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000363 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700364 }
365
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000366 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
367 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700368
369 bool atEnd = false;
370
371 if (fruCache.size() >= sizeof(FRUHeader))
372 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700373 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
374
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800375 int areaLength = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700376 int lastRecordStart = std::max(
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800377 {header->internalOffset, header->chassisOffset, header->boardOffset,
378 header->productOffset, header->multiRecordOffset});
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700379 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
380
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800381 if (header->multiRecordOffset)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700382 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800383 // This FRU has a MultiRecord Area
384 uint8_t endOfList = 0;
385 // Walk the MultiRecord headers until the last record
386 while (!endOfList)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700387 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800388 // The MSB in the second byte of the MultiRecord header signals
389 // "End of list"
390 endOfList = fruCache[lastRecordStart + 1] & 0x80;
391 // Third byte in the MultiRecord header is the length
392 areaLength = fruCache[lastRecordStart + 2];
393 // This length is in bytes (not 8 bytes like other headers)
394 areaLength += 5; // The length omits the 5 byte header
395 if (!endOfList)
396 {
397 // Next MultiRecord header
398 lastRecordStart += areaLength;
399 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700400 }
401 }
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800402 else
403 {
404 // This FRU does not have a MultiRecord Area
405 // Get the length of the area in multiples of 8 bytes
406 if (lastWriteAddr > (lastRecordStart + 1))
407 {
408 // second byte in record area is the length
409 areaLength = fruCache[lastRecordStart + 1];
410 areaLength *= 8; // it is in multiples of 8 bytes
411 }
412 }
413 if (lastWriteAddr >= (areaLength + lastRecordStart))
414 {
415 atEnd = true;
416 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700417 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000418 uint8_t countWritten = 0;
James Feist25690252019-12-23 12:25:49 -0800419
420 writeBus = cacheBus;
421 writeAddr = cacheAddr;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700422 if (atEnd)
423 {
424 // cancel timer, we're at the end so might as well send it
James Feist25690252019-12-23 12:25:49 -0800425 writeTimer->stop();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700426 if (!writeFru())
427 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000428 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700429 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000430 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700431 }
432 else
433 {
James Feist25690252019-12-23 12:25:49 -0800434 // start a timer, if no further data is sent to check to see if it is
435 // valid
436 writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
437 std::chrono::seconds(writeTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000438 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700439 }
440
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000441 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700442}
443
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000444/** @brief implements the get FRU inventory area info command
445 * @param fruDeviceId - FRU Device ID
446 *
447 * @returns IPMI completion code plus response data
448 * - inventorySize - Number of possible allocation units
449 * - accessType - Allocation unit size in bytes.
450 */
451ipmi::RspType<uint16_t, // inventorySize
452 uint8_t> // accessType
James Feist25690252019-12-23 12:25:49 -0800453 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700454{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000455 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700456 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000457 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700458 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700459
James Feist25690252019-12-23 12:25:49 -0800460 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700461
462 if (status != IPMI_CC_OK)
463 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000464 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700465 }
466
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000467 constexpr uint8_t accessType =
468 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700469
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000470 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700471}
472
James Feist25690252019-12-23 12:25:49 -0800473ipmi_ret_t getFruSdrCount(ipmi::Context::ptr ctx, size_t& count)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700474{
James Feist25690252019-12-23 12:25:49 -0800475 ipmi_ret_t ret = replaceCacheFru(ctx, 0);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700476 if (ret != IPMI_CC_OK)
477 {
478 return ret;
479 }
480 count = deviceHashes.size();
481 return IPMI_CC_OK;
482}
483
James Feist25690252019-12-23 12:25:49 -0800484ipmi_ret_t getFruSdrs(ipmi::Context::ptr ctx, size_t index,
485 get_sdr::SensorDataFruRecord& resp)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700486{
James Feist25690252019-12-23 12:25:49 -0800487 ipmi_ret_t ret = replaceCacheFru(ctx, 0); // this will update the hash list
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700488 if (ret != IPMI_CC_OK)
489 {
490 return ret;
491 }
492 if (deviceHashes.size() < index)
493 {
494 return IPMI_CC_INVALID_FIELD_REQUEST;
495 }
496 auto device = deviceHashes.begin() + index;
497 uint8_t& bus = device->second.first;
498 uint8_t& address = device->second.second;
499
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700500 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
501 auto fru =
502 std::find_if(frus.begin(), frus.end(),
503 [bus, address, &fruData](ManagedEntry& entry) {
504 auto findFruDevice =
505 entry.second.find("xyz.openbmc_project.FruDevice");
506 if (findFruDevice == entry.second.end())
507 {
508 return false;
509 }
510 fruData = &(findFruDevice->second);
511 auto findBus = findFruDevice->second.find("BUS");
512 auto findAddress =
513 findFruDevice->second.find("ADDRESS");
514 if (findBus == findFruDevice->second.end() ||
515 findAddress == findFruDevice->second.end())
516 {
517 return false;
518 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700519 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700520 {
521 return false;
522 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700523 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700524 {
525 return false;
526 }
527 return true;
528 });
529 if (fru == frus.end())
530 {
531 return IPMI_CC_RESPONSE_ERROR;
532 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700533
James Feist25690252019-12-23 12:25:49 -0800534#ifdef USING_ENTITY_MANAGER_DECORATORS
535
Patrick Venture9ce789f2019-10-17 09:09:39 -0700536 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700537
James Feist25690252019-12-23 12:25:49 -0800538 // todo: this should really use caching, this is a very inefficient lookup
539 boost::system::error_code ec;
540 ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
541 ctx->yield, ec, entityManagerServiceName, "/",
542 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
543
544 if (ec)
Patrick Venture9ce789f2019-10-17 09:09:39 -0700545 {
James Feist25690252019-12-23 12:25:49 -0800546 phosphor::logging::log<phosphor::logging::level::ERR>(
547 "GetMangagedObjects for getSensorMap failed",
548 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
Patrick Venture9ce789f2019-10-17 09:09:39 -0700549
James Feist25690252019-12-23 12:25:49 -0800550 return ipmi::ccResponseError;
551 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700552
James Feist25690252019-12-23 12:25:49 -0800553 auto entity = std::find_if(
554 entities.begin(), entities.end(),
555 [bus, address, &entityData](ManagedEntry& entry) {
556 auto findFruDevice = entry.second.find(
557 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
558 if (findFruDevice == entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700559 {
James Feist25690252019-12-23 12:25:49 -0800560 return false;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700561 }
James Feist25690252019-12-23 12:25:49 -0800562
563 // Integer fields added via Entity-Manager json are uint64_ts by
564 // default.
565 auto findBus = findFruDevice->second.find("Bus");
566 auto findAddress = findFruDevice->second.find("Address");
567
568 if (findBus == findFruDevice->second.end() ||
569 findAddress == findFruDevice->second.end())
570 {
571 return false;
572 }
573 if ((std::get<uint64_t>(findBus->second) != bus) ||
574 (std::get<uint64_t>(findAddress->second) != address))
575 {
576 return false;
577 }
578
579 // At this point we found the device entry and should return
580 // true.
581 auto findIpmiDevice = entry.second.find(
582 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
583 if (findIpmiDevice != entry.second.end())
584 {
585 entityData = &(findIpmiDevice->second);
586 }
587
588 return true;
589 });
590
591 if (entity == entities.end())
592 {
593 if constexpr (DEBUG)
594 {
595 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
596 "not found for Fru\n");
Patrick Venture9ce789f2019-10-17 09:09:39 -0700597 }
598 }
James Feist25690252019-12-23 12:25:49 -0800599
600#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700601
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700602 std::string name;
603 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
604 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
605 if (findProductName != fruData->end())
606 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700607 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700608 }
609 else if (findBoardName != fruData->end())
610 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700611 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700612 }
613 else
614 {
615 name = "UNKNOWN";
616 }
617 if (name.size() > maxFruSdrNameSize)
618 {
619 name = name.substr(0, maxFruSdrNameSize);
620 }
621 size_t sizeDiff = maxFruSdrNameSize - name.size();
622
623 resp.header.record_id_lsb = 0x0; // calling code is to implement these
624 resp.header.record_id_msb = 0x0;
625 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700626 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700627 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
628 resp.key.deviceAddress = 0x20;
629 resp.key.fruID = device->first;
630 resp.key.accessLun = 0x80; // logical / physical fru device
631 resp.key.channelNumber = 0x0;
632 resp.body.reserved = 0x0;
633 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700634 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700635
636 uint8_t entityID = 0;
637 uint8_t entityInstance = 0x1;
638
James Feist25690252019-12-23 12:25:49 -0800639#ifdef USING_ENTITY_MANAGER_DECORATORS
Patrick Venture9ce789f2019-10-17 09:09:39 -0700640 if (entityData)
641 {
642 auto entityIdProperty = entityData->find("EntityId");
643 auto entityInstanceProperty = entityData->find("EntityInstance");
644
645 if (entityIdProperty != entityData->end())
646 {
647 entityID = static_cast<uint8_t>(
648 std::get<uint64_t>(entityIdProperty->second));
649 }
650 if (entityInstanceProperty != entityData->end())
651 {
652 entityInstance = static_cast<uint8_t>(
653 std::get<uint64_t>(entityInstanceProperty->second));
654 }
655 }
James Feist25690252019-12-23 12:25:49 -0800656#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700657
658 resp.body.entityID = entityID;
659 resp.body.entityInstance = entityInstance;
660
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700661 resp.body.oem = 0x0;
662 resp.body.deviceIDLen = name.size();
663 name.copy(resp.body.deviceID, name.size());
664
665 return IPMI_CC_OK;
666}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700667
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700668static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800669{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700670 // Loop through the directory looking for ipmi_sel log files
671 for (const std::filesystem::directory_entry& dirEnt :
672 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800673 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700674 std::string filename = dirEnt.path().filename();
675 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800676 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700677 // If we find an ipmi_sel log file, save the path
678 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
679 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800680 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800681 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700682 // As the log files rotate, they are appended with a ".#" that is higher for
683 // the older logs. Since we don't expect more than 10 log files, we
684 // can just sort the list to get them in order from newest to oldest
685 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800686
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700687 return !selLogFiles.empty();
688}
689
690static int countSELEntries()
691{
692 // Get the list of ipmi_sel log files
693 std::vector<std::filesystem::path> selLogFiles;
694 if (!getSELLogFiles(selLogFiles))
695 {
696 return 0;
697 }
698 int numSELEntries = 0;
699 // Loop through each log file and count the number of logs
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 std::string line;
709 while (std::getline(logStream, line))
710 {
711 numSELEntries++;
712 }
713 }
714 return numSELEntries;
715}
716
717static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700718 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700719 std::string& entry)
720{
721 // Record ID is the first entry field following the timestamp. It is
722 // preceded by a space and followed by a comma
723 std::string search = " " + std::to_string(recordID) + ",";
724
725 // Loop through the ipmi_sel log entries
726 for (const std::filesystem::path& file : selLogFiles)
727 {
728 std::ifstream logStream(file);
729 if (!logStream.is_open())
730 {
731 continue;
732 }
733
734 while (std::getline(logStream, entry))
735 {
736 // Check if the record ID matches
737 if (entry.find(search) != std::string::npos)
738 {
739 return true;
740 }
741 }
742 }
743 return false;
744}
745
746static uint16_t
747 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700748 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700749{
750 uint16_t nextRecordID = recordID + 1;
751 std::string entry;
752 if (findSELEntry(nextRecordID, selLogFiles, entry))
753 {
754 return nextRecordID;
755 }
756 else
757 {
758 return ipmi::sel::lastEntry;
759 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800760}
761
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700762static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800763{
764 for (unsigned int i = 0; i < hexStr.size(); i += 2)
765 {
766 try
767 {
768 data.push_back(static_cast<uint8_t>(
769 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
770 }
771 catch (std::invalid_argument& e)
772 {
773 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
774 return -1;
775 }
776 catch (std::out_of_range& e)
777 {
778 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
779 return -1;
780 }
781 }
782 return 0;
783}
784
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700785ipmi::RspType<uint8_t, // SEL version
786 uint16_t, // SEL entry count
787 uint16_t, // free space
788 uint32_t, // last add timestamp
789 uint32_t, // last erase timestamp
790 uint8_t> // operation support
791 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800792{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700793 constexpr uint8_t selVersion = ipmi::sel::selVersion;
794 uint16_t entries = countSELEntries();
795 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
796 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
797 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
798 constexpr uint8_t operationSupport =
799 intel_oem::ipmi::sel::selOperationSupport;
800 constexpr uint16_t freeSpace =
801 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800802
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700803 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
804 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800805}
806
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700807using systemEventType = std::tuple<
808 uint32_t, // Timestamp
809 uint16_t, // Generator ID
810 uint8_t, // EvM Rev
811 uint8_t, // Sensor Type
812 uint8_t, // Sensor Number
813 uint7_t, // Event Type
814 bool, // Event Direction
815 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
816using oemTsEventType = std::tuple<
817 uint32_t, // Timestamp
818 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
819using oemEventType =
820 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800821
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700822ipmi::RspType<uint16_t, // Next Record ID
823 uint16_t, // Record ID
824 uint8_t, // Record Type
825 std::variant<systemEventType, oemTsEventType,
826 oemEventType>> // Record Content
827 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
828 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800829{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700830 // Only support getting the entire SEL record. If a partial size or non-zero
831 // offset is requested, return an error
832 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800833 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700834 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800835 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800836
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700837 // Check the reservation ID if one is provided or required (only if the
838 // offset is non-zero)
839 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800840 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700841 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800842 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700843 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800844 }
845 }
846
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700847 // Get the ipmi_sel log files
848 std::vector<std::filesystem::path> selLogFiles;
849 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800850 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700851 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800852 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800853
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700854 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800855
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800856 if (targetID == ipmi::sel::firstEntry)
857 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700858 // The first entry will be at the top of the oldest log file
859 std::ifstream logStream(selLogFiles.back());
860 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800861 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700862 return ipmi::responseUnspecifiedError();
863 }
864
865 if (!std::getline(logStream, targetEntry))
866 {
867 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800868 }
869 }
870 else if (targetID == ipmi::sel::lastEntry)
871 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700872 // The last entry will be at the bottom of the newest log file
873 std::ifstream logStream(selLogFiles.front());
874 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800875 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700876 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800877 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700878
879 std::string line;
880 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800881 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700882 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800883 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800884 }
885 else
886 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700887 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800888 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700889 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800890 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800891 }
892
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700893 // The format of the ipmi_sel message is "<Timestamp>
894 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
895 // First get the Timestamp
896 size_t space = targetEntry.find_first_of(" ");
897 if (space == std::string::npos)
898 {
899 return ipmi::responseUnspecifiedError();
900 }
901 std::string entryTimestamp = targetEntry.substr(0, space);
902 // Then get the log contents
903 size_t entryStart = targetEntry.find_first_not_of(" ", space);
904 if (entryStart == std::string::npos)
905 {
906 return ipmi::responseUnspecifiedError();
907 }
908 std::string_view entry(targetEntry);
909 entry.remove_prefix(entryStart);
910 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700911 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700912 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700913 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700914 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700915 {
916 return ipmi::responseUnspecifiedError();
917 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700918 std::string& recordIDStr = targetEntryFields[0];
919 std::string& recordTypeStr = targetEntryFields[1];
920 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700921
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700922 uint16_t recordID;
923 uint8_t recordType;
924 try
925 {
926 recordID = std::stoul(recordIDStr);
927 recordType = std::stoul(recordTypeStr, nullptr, 16);
928 }
929 catch (const std::invalid_argument&)
930 {
931 return ipmi::responseUnspecifiedError();
932 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700933 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700934 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700935 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700936 {
937 return ipmi::responseUnspecifiedError();
938 }
939
940 if (recordType == intel_oem::ipmi::sel::systemEvent)
941 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700942 // Get the timestamp
943 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700944 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700945
946 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
947 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
948 {
949 timestamp = std::mktime(&timeStruct);
950 }
951
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700952 // Set the event message revision
953 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
954
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700955 uint16_t generatorID = 0;
956 uint8_t sensorType = 0;
957 uint8_t sensorNum = 0xFF;
958 uint7_t eventType = 0;
959 bool eventDir = 0;
960 // System type events should have six fields
961 if (targetEntryFields.size() >= 6)
962 {
963 std::string& generatorIDStr = targetEntryFields[3];
964 std::string& sensorPath = targetEntryFields[4];
965 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700966
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700967 // Get the generator ID
968 try
969 {
970 generatorID = std::stoul(generatorIDStr, nullptr, 16);
971 }
972 catch (const std::invalid_argument&)
973 {
974 std::cerr << "Invalid Generator ID\n";
975 }
976
977 // Get the sensor type, sensor number, and event type for the sensor
978 sensorType = getSensorTypeFromPath(sensorPath);
979 sensorNum = getSensorNumberFromPath(sensorPath);
980 eventType = getSensorEventTypeFromPath(sensorPath);
981
982 // Get the event direction
983 try
984 {
985 eventDir = std::stoul(eventDirStr) ? 0 : 1;
986 }
987 catch (const std::invalid_argument&)
988 {
989 std::cerr << "Invalid Event Direction\n";
990 }
991 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700992
993 // Only keep the eventData bytes that fit in the record
994 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
995 std::copy_n(eventDataBytes.begin(),
996 std::min(eventDataBytes.size(), eventData.size()),
997 eventData.begin());
998
999 return ipmi::responseSuccess(
1000 nextRecordID, recordID, recordType,
1001 systemEventType{timestamp, generatorID, evmRev, sensorType,
1002 sensorNum, eventType, eventDir, eventData});
1003 }
1004 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
1005 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
1006 {
1007 // Get the timestamp
1008 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -07001009 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001010
1011 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1012 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1013 {
1014 timestamp = std::mktime(&timeStruct);
1015 }
1016
1017 // Only keep the bytes that fit in the record
1018 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
1019 std::copy_n(eventDataBytes.begin(),
1020 std::min(eventDataBytes.size(), eventData.size()),
1021 eventData.begin());
1022
1023 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1024 oemTsEventType{timestamp, eventData});
1025 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001026 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001027 {
1028 // Only keep the bytes that fit in the record
1029 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1030 std::copy_n(eventDataBytes.begin(),
1031 std::min(eventDataBytes.size(), eventData.size()),
1032 eventData.begin());
1033
1034 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1035 eventData);
1036 }
1037
1038 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001039}
1040
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001041ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1042 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1043 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1044 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1045 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001046{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001047 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1048 // added
1049 cancelSELReservation();
1050
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001051 // Send this request to the Redfish hooks to log it as a Redfish message
1052 // instead. There is no need to add it to the SEL, so just return success.
1053 intel_oem::ipmi::sel::checkRedfishHooks(
1054 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1055 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001056
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001057 uint16_t responseID = 0xFFFF;
1058 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001059}
1060
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001061ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1062 uint16_t reservationID,
1063 const std::array<uint8_t, 3>& clr,
1064 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001065{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001066 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001067 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001068 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001069 }
1070
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001071 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1072 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001073 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001074 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001075 }
1076
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001077 // Erasure status cannot be fetched, so always return erasure status as
1078 // `erase completed`.
1079 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001080 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001081 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001082 }
1083
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001084 // Check that initiate erase is correct
1085 if (eraseOperation != ipmi::sel::initiateErase)
1086 {
1087 return ipmi::responseInvalidFieldRequest();
1088 }
1089
1090 // Per the IPMI spec, need to cancel any reservation when the SEL is
1091 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001092 cancelSELReservation();
1093
Jason M. Bills7944c302019-03-20 15:24:05 -07001094 // Save the erase time
1095 intel_oem::ipmi::sel::erase_time::save();
1096
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001097 // Clear the SEL by deleting the log files
1098 std::vector<std::filesystem::path> selLogFiles;
1099 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001100 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001101 for (const std::filesystem::path& file : selLogFiles)
1102 {
1103 std::error_code ec;
1104 std::filesystem::remove(file, ec);
1105 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001106 }
1107
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001108 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001109 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1110 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001111 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1112 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1113 rsyslogReload.append("rsyslog.service", "replace");
1114 try
1115 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001116 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001117 }
1118 catch (sdbusplus::exception_t& e)
1119 {
1120 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1121 }
1122
1123 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001124}
1125
Jason M. Bills1a474622019-06-14 14:51:33 -07001126ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1127{
1128 struct timespec selTime = {};
1129
1130 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1131 {
1132 return ipmi::responseUnspecifiedError();
1133 }
1134
1135 return ipmi::responseSuccess(selTime.tv_sec);
1136}
1137
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001138ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001139{
1140 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001141 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001142}
1143
James Feist74c50c62019-08-14 14:18:41 -07001144std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1145{
1146 std::vector<uint8_t> resp;
1147 if (index == 0)
1148 {
1149 Type12Record bmc = {};
1150 bmc.header.record_id_lsb = recordId;
1151 bmc.header.record_id_msb = recordId >> 8;
1152 bmc.header.sdr_version = ipmiSdrVersion;
1153 bmc.header.record_type = 0x12;
1154 bmc.header.record_length = 0x1b;
1155 bmc.slaveAddress = 0x20;
1156 bmc.channelNumber = 0;
1157 bmc.powerStateNotification = 0;
1158 bmc.deviceCapabilities = 0xBF;
1159 bmc.reserved = 0;
1160 bmc.entityID = 0x2E;
1161 bmc.entityInstance = 1;
1162 bmc.oem = 0;
1163 bmc.typeLengthCode = 0xD0;
1164 std::string bmcName = "Basbrd Mgmt Ctlr";
1165 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1166 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1167 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1168 }
1169 else if (index == 1)
1170 {
1171 Type12Record me = {};
1172 me.header.record_id_lsb = recordId;
1173 me.header.record_id_msb = recordId >> 8;
1174 me.header.sdr_version = ipmiSdrVersion;
1175 me.header.record_type = 0x12;
1176 me.header.record_length = 0x16;
1177 me.slaveAddress = 0x2C;
1178 me.channelNumber = 6;
1179 me.powerStateNotification = 0x24;
1180 me.deviceCapabilities = 0x21;
1181 me.reserved = 0;
1182 me.entityID = 0x2E;
1183 me.entityInstance = 2;
1184 me.oem = 0;
1185 me.typeLengthCode = 0xCB;
1186 std::string meName = "Mgmt Engine";
1187 std::copy(meName.begin(), meName.end(), me.name);
1188 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1189 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1190 }
1191 else
1192 {
1193 throw std::runtime_error("getType12SDRs:: Illegal index " +
1194 std::to_string(index));
1195 }
1196
1197 return resp;
1198}
1199
Yong Lifee5e4c2020-01-17 19:36:29 +08001200std::vector<uint8_t> getNMDiscoverySDR(uint16_t index, uint16_t recordId)
1201{
1202 std::vector<uint8_t> resp;
1203 if (index == 0)
1204 {
1205 NMDiscoveryRecord nm = {};
1206 nm.header.record_id_lsb = recordId;
1207 nm.header.record_id_msb = recordId >> 8;
1208 nm.header.sdr_version = ipmiSdrVersion;
1209 nm.header.record_type = 0xC0;
1210 nm.header.record_length = 0xB;
1211 nm.oemID0 = 0x57;
1212 nm.oemID1 = 0x1;
1213 nm.oemID2 = 0x0;
1214 nm.subType = 0x0D;
1215 nm.version = 0x1;
1216 nm.slaveAddress = 0x2C;
1217 nm.channelNumber = 0x60;
1218 nm.healthEventSensor = 0x19;
1219 nm.exceptionEventSensor = 0x18;
1220 nm.operationalCapSensor = 0x1A;
1221 nm.thresholdExceededSensor = 0x1B;
1222
1223 uint8_t* nmPtr = reinterpret_cast<uint8_t*>(&nm);
1224 resp.insert(resp.end(), nmPtr, nmPtr + sizeof(NMDiscoveryRecord));
1225 }
1226 else
1227 {
1228 throw std::runtime_error("getNMDiscoverySDR:: Illegal index " +
1229 std::to_string(index));
1230 }
1231
1232 return resp;
1233}
1234
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001235void registerStorageFunctions()
1236{
James Feist25690252019-12-23 12:25:49 -08001237 createTimers();
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001238 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001239 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1240 ipmi::storage::cmdGetFruInventoryAreaInfo,
1241 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001242 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001243 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1244 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1245 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001246
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001247 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001248 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1249 ipmi::storage::cmdWriteFruData,
1250 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001251
1252 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001253 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001254 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1255 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001256
1257 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001258 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001259 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1260 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001261
1262 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001263 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001264 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001265 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001266
1267 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001268 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1269 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1270 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001271
Jason M. Bills1a474622019-06-14 14:51:33 -07001272 // <Get SEL Time>
1273 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001274 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1275 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001276
Jason M. Billscac97a52019-01-30 14:43:46 -08001277 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001278 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1279 ipmi::storage::cmdSetSelTime,
1280 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001281}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001282} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001283} // namespace ipmi