blob: f3989803cd0837793147f2d4ae35cb29a799d034 [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 {
322 return ipmi::responseInvalidFieldRequest();
323 }
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
375 int lastRecordStart = std::max(
376 header->internalOffset,
377 std::max(header->chassisOffset,
378 std::max(header->boardOffset, header->productOffset)));
379 // TODO: Handle Multi-Record FRUs?
380
381 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
382
383 // get the length of the area in multiples of 8 bytes
384 if (lastWriteAddr > (lastRecordStart + 1))
385 {
386 // second byte in record area is the length
387 int areaLength(fruCache[lastRecordStart + 1]);
388 areaLength *= 8; // it is in multiples of 8 bytes
389
390 if (lastWriteAddr >= (areaLength + lastRecordStart))
391 {
392 atEnd = true;
393 }
394 }
395 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000396 uint8_t countWritten = 0;
James Feist25690252019-12-23 12:25:49 -0800397
398 writeBus = cacheBus;
399 writeAddr = cacheAddr;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700400 if (atEnd)
401 {
402 // cancel timer, we're at the end so might as well send it
James Feist25690252019-12-23 12:25:49 -0800403 writeTimer->stop();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700404 if (!writeFru())
405 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000406 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700407 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000408 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700409 }
410 else
411 {
James Feist25690252019-12-23 12:25:49 -0800412 // start a timer, if no further data is sent to check to see if it is
413 // valid
414 writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
415 std::chrono::seconds(writeTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000416 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700417 }
418
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000419 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700420}
421
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000422/** @brief implements the get FRU inventory area info command
423 * @param fruDeviceId - FRU Device ID
424 *
425 * @returns IPMI completion code plus response data
426 * - inventorySize - Number of possible allocation units
427 * - accessType - Allocation unit size in bytes.
428 */
429ipmi::RspType<uint16_t, // inventorySize
430 uint8_t> // accessType
James Feist25690252019-12-23 12:25:49 -0800431 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700432{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000433 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700434 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000435 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700436 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700437
James Feist25690252019-12-23 12:25:49 -0800438 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700439
440 if (status != IPMI_CC_OK)
441 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000442 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700443 }
444
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000445 constexpr uint8_t accessType =
446 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700447
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000448 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700449}
450
James Feist25690252019-12-23 12:25:49 -0800451ipmi_ret_t getFruSdrCount(ipmi::Context::ptr ctx, size_t& count)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700452{
James Feist25690252019-12-23 12:25:49 -0800453 ipmi_ret_t ret = replaceCacheFru(ctx, 0);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700454 if (ret != IPMI_CC_OK)
455 {
456 return ret;
457 }
458 count = deviceHashes.size();
459 return IPMI_CC_OK;
460}
461
James Feist25690252019-12-23 12:25:49 -0800462ipmi_ret_t getFruSdrs(ipmi::Context::ptr ctx, size_t index,
463 get_sdr::SensorDataFruRecord& resp)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700464{
James Feist25690252019-12-23 12:25:49 -0800465 ipmi_ret_t ret = replaceCacheFru(ctx, 0); // this will update the hash list
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700466 if (ret != IPMI_CC_OK)
467 {
468 return ret;
469 }
470 if (deviceHashes.size() < index)
471 {
472 return IPMI_CC_INVALID_FIELD_REQUEST;
473 }
474 auto device = deviceHashes.begin() + index;
475 uint8_t& bus = device->second.first;
476 uint8_t& address = device->second.second;
477
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700478 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
479 auto fru =
480 std::find_if(frus.begin(), frus.end(),
481 [bus, address, &fruData](ManagedEntry& entry) {
482 auto findFruDevice =
483 entry.second.find("xyz.openbmc_project.FruDevice");
484 if (findFruDevice == entry.second.end())
485 {
486 return false;
487 }
488 fruData = &(findFruDevice->second);
489 auto findBus = findFruDevice->second.find("BUS");
490 auto findAddress =
491 findFruDevice->second.find("ADDRESS");
492 if (findBus == findFruDevice->second.end() ||
493 findAddress == findFruDevice->second.end())
494 {
495 return false;
496 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700497 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700498 {
499 return false;
500 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700501 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700502 {
503 return false;
504 }
505 return true;
506 });
507 if (fru == frus.end())
508 {
509 return IPMI_CC_RESPONSE_ERROR;
510 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700511
James Feist25690252019-12-23 12:25:49 -0800512#ifdef USING_ENTITY_MANAGER_DECORATORS
513
Patrick Venture9ce789f2019-10-17 09:09:39 -0700514 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700515
James Feist25690252019-12-23 12:25:49 -0800516 // todo: this should really use caching, this is a very inefficient lookup
517 boost::system::error_code ec;
518 ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
519 ctx->yield, ec, entityManagerServiceName, "/",
520 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
521
522 if (ec)
Patrick Venture9ce789f2019-10-17 09:09:39 -0700523 {
James Feist25690252019-12-23 12:25:49 -0800524 phosphor::logging::log<phosphor::logging::level::ERR>(
525 "GetMangagedObjects for getSensorMap failed",
526 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
Patrick Venture9ce789f2019-10-17 09:09:39 -0700527
James Feist25690252019-12-23 12:25:49 -0800528 return ipmi::ccResponseError;
529 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700530
James Feist25690252019-12-23 12:25:49 -0800531 auto entity = std::find_if(
532 entities.begin(), entities.end(),
533 [bus, address, &entityData](ManagedEntry& entry) {
534 auto findFruDevice = entry.second.find(
535 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
536 if (findFruDevice == entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700537 {
James Feist25690252019-12-23 12:25:49 -0800538 return false;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700539 }
James Feist25690252019-12-23 12:25:49 -0800540
541 // Integer fields added via Entity-Manager json are uint64_ts by
542 // default.
543 auto findBus = findFruDevice->second.find("Bus");
544 auto findAddress = findFruDevice->second.find("Address");
545
546 if (findBus == findFruDevice->second.end() ||
547 findAddress == findFruDevice->second.end())
548 {
549 return false;
550 }
551 if ((std::get<uint64_t>(findBus->second) != bus) ||
552 (std::get<uint64_t>(findAddress->second) != address))
553 {
554 return false;
555 }
556
557 // At this point we found the device entry and should return
558 // true.
559 auto findIpmiDevice = entry.second.find(
560 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
561 if (findIpmiDevice != entry.second.end())
562 {
563 entityData = &(findIpmiDevice->second);
564 }
565
566 return true;
567 });
568
569 if (entity == entities.end())
570 {
571 if constexpr (DEBUG)
572 {
573 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
574 "not found for Fru\n");
Patrick Venture9ce789f2019-10-17 09:09:39 -0700575 }
576 }
James Feist25690252019-12-23 12:25:49 -0800577
578#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700579
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700580 std::string name;
581 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
582 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
583 if (findProductName != fruData->end())
584 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700585 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700586 }
587 else if (findBoardName != fruData->end())
588 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700589 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700590 }
591 else
592 {
593 name = "UNKNOWN";
594 }
595 if (name.size() > maxFruSdrNameSize)
596 {
597 name = name.substr(0, maxFruSdrNameSize);
598 }
599 size_t sizeDiff = maxFruSdrNameSize - name.size();
600
601 resp.header.record_id_lsb = 0x0; // calling code is to implement these
602 resp.header.record_id_msb = 0x0;
603 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700604 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700605 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
606 resp.key.deviceAddress = 0x20;
607 resp.key.fruID = device->first;
608 resp.key.accessLun = 0x80; // logical / physical fru device
609 resp.key.channelNumber = 0x0;
610 resp.body.reserved = 0x0;
611 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700612 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700613
614 uint8_t entityID = 0;
615 uint8_t entityInstance = 0x1;
616
James Feist25690252019-12-23 12:25:49 -0800617#ifdef USING_ENTITY_MANAGER_DECORATORS
Patrick Venture9ce789f2019-10-17 09:09:39 -0700618 if (entityData)
619 {
620 auto entityIdProperty = entityData->find("EntityId");
621 auto entityInstanceProperty = entityData->find("EntityInstance");
622
623 if (entityIdProperty != entityData->end())
624 {
625 entityID = static_cast<uint8_t>(
626 std::get<uint64_t>(entityIdProperty->second));
627 }
628 if (entityInstanceProperty != entityData->end())
629 {
630 entityInstance = static_cast<uint8_t>(
631 std::get<uint64_t>(entityInstanceProperty->second));
632 }
633 }
James Feist25690252019-12-23 12:25:49 -0800634#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700635
636 resp.body.entityID = entityID;
637 resp.body.entityInstance = entityInstance;
638
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700639 resp.body.oem = 0x0;
640 resp.body.deviceIDLen = name.size();
641 name.copy(resp.body.deviceID, name.size());
642
643 return IPMI_CC_OK;
644}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700645
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700646static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800647{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700648 // Loop through the directory looking for ipmi_sel log files
649 for (const std::filesystem::directory_entry& dirEnt :
650 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800651 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700652 std::string filename = dirEnt.path().filename();
653 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800654 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700655 // If we find an ipmi_sel log file, save the path
656 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
657 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800658 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800659 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700660 // As the log files rotate, they are appended with a ".#" that is higher for
661 // the older logs. Since we don't expect more than 10 log files, we
662 // can just sort the list to get them in order from newest to oldest
663 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800664
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700665 return !selLogFiles.empty();
666}
667
668static int countSELEntries()
669{
670 // Get the list of ipmi_sel log files
671 std::vector<std::filesystem::path> selLogFiles;
672 if (!getSELLogFiles(selLogFiles))
673 {
674 return 0;
675 }
676 int numSELEntries = 0;
677 // Loop through each log file and count the number of logs
678 for (const std::filesystem::path& file : selLogFiles)
679 {
680 std::ifstream logStream(file);
681 if (!logStream.is_open())
682 {
683 continue;
684 }
685
686 std::string line;
687 while (std::getline(logStream, line))
688 {
689 numSELEntries++;
690 }
691 }
692 return numSELEntries;
693}
694
695static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700696 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700697 std::string& entry)
698{
699 // Record ID is the first entry field following the timestamp. It is
700 // preceded by a space and followed by a comma
701 std::string search = " " + std::to_string(recordID) + ",";
702
703 // Loop through the ipmi_sel log entries
704 for (const std::filesystem::path& file : selLogFiles)
705 {
706 std::ifstream logStream(file);
707 if (!logStream.is_open())
708 {
709 continue;
710 }
711
712 while (std::getline(logStream, entry))
713 {
714 // Check if the record ID matches
715 if (entry.find(search) != std::string::npos)
716 {
717 return true;
718 }
719 }
720 }
721 return false;
722}
723
724static uint16_t
725 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700726 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700727{
728 uint16_t nextRecordID = recordID + 1;
729 std::string entry;
730 if (findSELEntry(nextRecordID, selLogFiles, entry))
731 {
732 return nextRecordID;
733 }
734 else
735 {
736 return ipmi::sel::lastEntry;
737 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800738}
739
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700740static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800741{
742 for (unsigned int i = 0; i < hexStr.size(); i += 2)
743 {
744 try
745 {
746 data.push_back(static_cast<uint8_t>(
747 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
748 }
749 catch (std::invalid_argument& e)
750 {
751 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
752 return -1;
753 }
754 catch (std::out_of_range& e)
755 {
756 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
757 return -1;
758 }
759 }
760 return 0;
761}
762
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700763ipmi::RspType<uint8_t, // SEL version
764 uint16_t, // SEL entry count
765 uint16_t, // free space
766 uint32_t, // last add timestamp
767 uint32_t, // last erase timestamp
768 uint8_t> // operation support
769 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800770{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700771 constexpr uint8_t selVersion = ipmi::sel::selVersion;
772 uint16_t entries = countSELEntries();
773 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
774 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
775 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
776 constexpr uint8_t operationSupport =
777 intel_oem::ipmi::sel::selOperationSupport;
778 constexpr uint16_t freeSpace =
779 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800780
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700781 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
782 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800783}
784
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700785using systemEventType = std::tuple<
786 uint32_t, // Timestamp
787 uint16_t, // Generator ID
788 uint8_t, // EvM Rev
789 uint8_t, // Sensor Type
790 uint8_t, // Sensor Number
791 uint7_t, // Event Type
792 bool, // Event Direction
793 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
794using oemTsEventType = std::tuple<
795 uint32_t, // Timestamp
796 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
797using oemEventType =
798 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800799
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700800ipmi::RspType<uint16_t, // Next Record ID
801 uint16_t, // Record ID
802 uint8_t, // Record Type
803 std::variant<systemEventType, oemTsEventType,
804 oemEventType>> // Record Content
805 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
806 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800807{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700808 // Only support getting the entire SEL record. If a partial size or non-zero
809 // offset is requested, return an error
810 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800811 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700812 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800813 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800814
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700815 // Check the reservation ID if one is provided or required (only if the
816 // offset is non-zero)
817 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800818 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700819 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800820 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700821 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800822 }
823 }
824
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700825 // Get the ipmi_sel log files
826 std::vector<std::filesystem::path> selLogFiles;
827 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800828 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700829 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800830 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800831
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700832 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800833
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800834 if (targetID == ipmi::sel::firstEntry)
835 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700836 // The first entry will be at the top of the oldest log file
837 std::ifstream logStream(selLogFiles.back());
838 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800839 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700840 return ipmi::responseUnspecifiedError();
841 }
842
843 if (!std::getline(logStream, targetEntry))
844 {
845 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800846 }
847 }
848 else if (targetID == ipmi::sel::lastEntry)
849 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700850 // The last entry will be at the bottom of the newest log file
851 std::ifstream logStream(selLogFiles.front());
852 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800853 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700854 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800855 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700856
857 std::string line;
858 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800859 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700860 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800861 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800862 }
863 else
864 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700865 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800866 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700867 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800868 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800869 }
870
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700871 // The format of the ipmi_sel message is "<Timestamp>
872 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
873 // First get the Timestamp
874 size_t space = targetEntry.find_first_of(" ");
875 if (space == std::string::npos)
876 {
877 return ipmi::responseUnspecifiedError();
878 }
879 std::string entryTimestamp = targetEntry.substr(0, space);
880 // Then get the log contents
881 size_t entryStart = targetEntry.find_first_not_of(" ", space);
882 if (entryStart == std::string::npos)
883 {
884 return ipmi::responseUnspecifiedError();
885 }
886 std::string_view entry(targetEntry);
887 entry.remove_prefix(entryStart);
888 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700889 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700890 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700891 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700892 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700893 {
894 return ipmi::responseUnspecifiedError();
895 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700896 std::string& recordIDStr = targetEntryFields[0];
897 std::string& recordTypeStr = targetEntryFields[1];
898 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700899
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700900 uint16_t recordID;
901 uint8_t recordType;
902 try
903 {
904 recordID = std::stoul(recordIDStr);
905 recordType = std::stoul(recordTypeStr, nullptr, 16);
906 }
907 catch (const std::invalid_argument&)
908 {
909 return ipmi::responseUnspecifiedError();
910 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700911 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700912 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700913 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700914 {
915 return ipmi::responseUnspecifiedError();
916 }
917
918 if (recordType == intel_oem::ipmi::sel::systemEvent)
919 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700920 // Get the timestamp
921 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700922 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700923
924 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
925 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
926 {
927 timestamp = std::mktime(&timeStruct);
928 }
929
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700930 // Set the event message revision
931 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
932
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700933 uint16_t generatorID = 0;
934 uint8_t sensorType = 0;
935 uint8_t sensorNum = 0xFF;
936 uint7_t eventType = 0;
937 bool eventDir = 0;
938 // System type events should have six fields
939 if (targetEntryFields.size() >= 6)
940 {
941 std::string& generatorIDStr = targetEntryFields[3];
942 std::string& sensorPath = targetEntryFields[4];
943 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700944
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700945 // Get the generator ID
946 try
947 {
948 generatorID = std::stoul(generatorIDStr, nullptr, 16);
949 }
950 catch (const std::invalid_argument&)
951 {
952 std::cerr << "Invalid Generator ID\n";
953 }
954
955 // Get the sensor type, sensor number, and event type for the sensor
956 sensorType = getSensorTypeFromPath(sensorPath);
957 sensorNum = getSensorNumberFromPath(sensorPath);
958 eventType = getSensorEventTypeFromPath(sensorPath);
959
960 // Get the event direction
961 try
962 {
963 eventDir = std::stoul(eventDirStr) ? 0 : 1;
964 }
965 catch (const std::invalid_argument&)
966 {
967 std::cerr << "Invalid Event Direction\n";
968 }
969 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700970
971 // Only keep the eventData bytes that fit in the record
972 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
973 std::copy_n(eventDataBytes.begin(),
974 std::min(eventDataBytes.size(), eventData.size()),
975 eventData.begin());
976
977 return ipmi::responseSuccess(
978 nextRecordID, recordID, recordType,
979 systemEventType{timestamp, generatorID, evmRev, sensorType,
980 sensorNum, eventType, eventDir, eventData});
981 }
982 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
983 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
984 {
985 // Get the timestamp
986 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700987 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700988
989 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
990 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
991 {
992 timestamp = std::mktime(&timeStruct);
993 }
994
995 // Only keep the bytes that fit in the record
996 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
997 std::copy_n(eventDataBytes.begin(),
998 std::min(eventDataBytes.size(), eventData.size()),
999 eventData.begin());
1000
1001 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1002 oemTsEventType{timestamp, eventData});
1003 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001004 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001005 {
1006 // Only keep the bytes that fit in the record
1007 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1008 std::copy_n(eventDataBytes.begin(),
1009 std::min(eventDataBytes.size(), eventData.size()),
1010 eventData.begin());
1011
1012 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1013 eventData);
1014 }
1015
1016 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001017}
1018
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001019ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1020 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1021 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1022 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1023 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001024{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001025 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1026 // added
1027 cancelSELReservation();
1028
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001029 // Send this request to the Redfish hooks to log it as a Redfish message
1030 // instead. There is no need to add it to the SEL, so just return success.
1031 intel_oem::ipmi::sel::checkRedfishHooks(
1032 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1033 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001034
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001035 uint16_t responseID = 0xFFFF;
1036 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001037}
1038
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001039ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1040 uint16_t reservationID,
1041 const std::array<uint8_t, 3>& clr,
1042 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001043{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001044 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001045 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001046 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001047 }
1048
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001049 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1050 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001051 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001052 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001053 }
1054
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001055 // Erasure status cannot be fetched, so always return erasure status as
1056 // `erase completed`.
1057 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001058 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001059 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001060 }
1061
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001062 // Check that initiate erase is correct
1063 if (eraseOperation != ipmi::sel::initiateErase)
1064 {
1065 return ipmi::responseInvalidFieldRequest();
1066 }
1067
1068 // Per the IPMI spec, need to cancel any reservation when the SEL is
1069 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001070 cancelSELReservation();
1071
Jason M. Bills7944c302019-03-20 15:24:05 -07001072 // Save the erase time
1073 intel_oem::ipmi::sel::erase_time::save();
1074
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001075 // Clear the SEL by deleting the log files
1076 std::vector<std::filesystem::path> selLogFiles;
1077 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001078 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001079 for (const std::filesystem::path& file : selLogFiles)
1080 {
1081 std::error_code ec;
1082 std::filesystem::remove(file, ec);
1083 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001084 }
1085
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001086 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001087 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1088 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001089 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1090 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1091 rsyslogReload.append("rsyslog.service", "replace");
1092 try
1093 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001094 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001095 }
1096 catch (sdbusplus::exception_t& e)
1097 {
1098 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1099 }
1100
1101 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001102}
1103
Jason M. Bills1a474622019-06-14 14:51:33 -07001104ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1105{
1106 struct timespec selTime = {};
1107
1108 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1109 {
1110 return ipmi::responseUnspecifiedError();
1111 }
1112
1113 return ipmi::responseSuccess(selTime.tv_sec);
1114}
1115
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001116ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001117{
1118 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001119 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001120}
1121
James Feist74c50c62019-08-14 14:18:41 -07001122std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1123{
1124 std::vector<uint8_t> resp;
1125 if (index == 0)
1126 {
1127 Type12Record bmc = {};
1128 bmc.header.record_id_lsb = recordId;
1129 bmc.header.record_id_msb = recordId >> 8;
1130 bmc.header.sdr_version = ipmiSdrVersion;
1131 bmc.header.record_type = 0x12;
1132 bmc.header.record_length = 0x1b;
1133 bmc.slaveAddress = 0x20;
1134 bmc.channelNumber = 0;
1135 bmc.powerStateNotification = 0;
1136 bmc.deviceCapabilities = 0xBF;
1137 bmc.reserved = 0;
1138 bmc.entityID = 0x2E;
1139 bmc.entityInstance = 1;
1140 bmc.oem = 0;
1141 bmc.typeLengthCode = 0xD0;
1142 std::string bmcName = "Basbrd Mgmt Ctlr";
1143 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1144 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1145 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1146 }
1147 else if (index == 1)
1148 {
1149 Type12Record me = {};
1150 me.header.record_id_lsb = recordId;
1151 me.header.record_id_msb = recordId >> 8;
1152 me.header.sdr_version = ipmiSdrVersion;
1153 me.header.record_type = 0x12;
1154 me.header.record_length = 0x16;
1155 me.slaveAddress = 0x2C;
1156 me.channelNumber = 6;
1157 me.powerStateNotification = 0x24;
1158 me.deviceCapabilities = 0x21;
1159 me.reserved = 0;
1160 me.entityID = 0x2E;
1161 me.entityInstance = 2;
1162 me.oem = 0;
1163 me.typeLengthCode = 0xCB;
1164 std::string meName = "Mgmt Engine";
1165 std::copy(meName.begin(), meName.end(), me.name);
1166 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1167 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1168 }
1169 else
1170 {
1171 throw std::runtime_error("getType12SDRs:: Illegal index " +
1172 std::to_string(index));
1173 }
1174
1175 return resp;
1176}
1177
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001178void registerStorageFunctions()
1179{
James Feist25690252019-12-23 12:25:49 -08001180 createTimers();
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001181 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001182 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1183 ipmi::storage::cmdGetFruInventoryAreaInfo,
1184 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001185 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001186 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1187 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1188 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001189
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001190 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001191 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1192 ipmi::storage::cmdWriteFruData,
1193 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001194
1195 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001196 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001197 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1198 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001199
1200 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001201 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001202 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1203 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001204
1205 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001206 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001207 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001208 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001209
1210 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001211 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1212 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1213 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001214
Jason M. Bills1a474622019-06-14 14:51:33 -07001215 // <Get SEL Time>
1216 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001217 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1218 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001219
Jason M. Billscac97a52019-01-30 14:43:46 -08001220 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001221 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1222 ipmi::storage::cmdSetSelTime,
1223 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001224}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001225} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001226} // namespace ipmi