blob: f1b98e07357e68d3ba9fd3fa2e2e3c387ddd6d58 [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07002// Copyright (c) 2017-2019 Intel Corporation
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Patrick Ventureca99ef52019-10-20 14:00:50 -070017#include "storagecommands.hpp"
18
19#include "commandutils.hpp"
20#include "ipmi_to_redfish_hooks.hpp"
21#include "sdrutils.hpp"
Patrick Venturec2a07d42020-05-30 16:35:03 -070022#include "types.hpp"
Patrick Ventureca99ef52019-10-20 14:00:50 -070023
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070024#include <boost/algorithm/string.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070025#include <boost/container/flat_map.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080026#include <boost/process.hpp>
James Feist2a265d52019-04-08 11:16:27 -070027#include <ipmid/api.hpp>
James Feist25690252019-12-23 12:25:49 -080028#include <ipmid/message.hpp>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080029#include <phosphor-ipmi-host/selutility.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070030#include <phosphor-logging/log.hpp>
31#include <sdbusplus/message/types.hpp>
32#include <sdbusplus/timer.hpp>
James Feistfcd2d3a2020-05-28 10:38:15 -070033
34#include <filesystem>
35#include <functional>
36#include <iostream>
Jason M. Billsc04e2e72018-11-28 15:15:56 -080037#include <stdexcept>
Jason M. Bills52aaa7d2019-05-08 15:21:39 -070038#include <string_view>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070039
Patrick Venture9ce789f2019-10-17 09:09:39 -070040static constexpr bool DEBUG = false;
41
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070042namespace intel_oem::ipmi::sel
43{
44static const std::filesystem::path selLogDir = "/var/log";
45static const std::string selLogFilename = "ipmi_sel";
46
47static int getFileTimestamp(const std::filesystem::path& file)
48{
49 struct stat st;
50
51 if (stat(file.c_str(), &st) >= 0)
52 {
53 return st.st_mtime;
54 }
55 return ::ipmi::sel::invalidTimeStamp;
56}
57
58namespace erase_time
Jason M. Bills7944c302019-03-20 15:24:05 -070059{
60static constexpr const char* selEraseTimestamp = "/var/lib/ipmi/sel_erase_time";
61
62void save()
63{
64 // open the file, creating it if necessary
65 int fd = open(selEraseTimestamp, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
66 if (fd < 0)
67 {
68 std::cerr << "Failed to open file\n";
69 return;
70 }
71
72 // update the file timestamp to the current time
73 if (futimens(fd, NULL) < 0)
74 {
75 std::cerr << "Failed to update timestamp: "
76 << std::string(strerror(errno));
77 }
78 close(fd);
79}
80
81int get()
82{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070083 return getFileTimestamp(selEraseTimestamp);
Jason M. Bills7944c302019-03-20 15:24:05 -070084}
Jason M. Bills1d4d54d2019-04-23 11:26:11 -070085} // namespace erase_time
86} // namespace intel_oem::ipmi::sel
Jason M. Bills7944c302019-03-20 15:24:05 -070087
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070088namespace ipmi
89{
90
91namespace storage
92{
93
Jason M. Billse2d1aee2018-10-03 15:57:18 -070094constexpr static const size_t maxMessageSize = 64;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070095constexpr static const size_t maxFruSdrNameSize = 16;
James Feiste4f710d2020-05-20 15:50:30 -070096using ObjectType = boost::container::flat_map<
97 std::string, boost::container::flat_map<std::string, DbusVariant>>;
98using ManagedObjectType =
99 boost::container::flat_map<sdbusplus::message::object_path, ObjectType>;
100using ManagedEntry = std::pair<sdbusplus::message::object_path, ObjectType>;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700101
James Feist3bcba452018-12-20 12:31:03 -0800102constexpr static const char* fruDeviceServiceName =
103 "xyz.openbmc_project.FruDevice";
Patrick Venture9ce789f2019-10-17 09:09:39 -0700104constexpr static const char* entityManagerServiceName =
105 "xyz.openbmc_project.EntityManager";
James Feist25690252019-12-23 12:25:49 -0800106constexpr static const size_t writeTimeoutSeconds = 10;
Anoop S358e7df2020-05-05 16:43:34 +0000107constexpr static const char* chassisTypeRackMount = "23";
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700108
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700109// event direction is bit[7] of eventType where 1b = Deassertion event
110constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800111
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700112static std::vector<uint8_t> fruCache;
113static uint8_t cacheBus = 0xFF;
114static uint8_t cacheAddr = 0XFF;
James Feiste4f710d2020-05-20 15:50:30 -0700115static uint8_t lastDevId = 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700116
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;
James Feiste4f710d2020-05-20 15:50:30 -0700121static std::vector<sdbusplus::bus::match::match> fruMatches;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700122
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 writeTimer = std::make_unique<phosphor::Timer>(writeFru);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700161}
162
James Feiste4f710d2020-05-20 15:50:30 -0700163void recalculateHashes()
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700164{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700165
166 deviceHashes.clear();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700167 // hash the object paths to create unique device id's. increment on
168 // collision
169 std::hash<std::string> hasher;
170 for (const auto& fru : frus)
171 {
172 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
173 if (fruIface == fru.second.end())
174 {
175 continue;
176 }
177
178 auto busFind = fruIface->second.find("BUS");
179 auto addrFind = fruIface->second.find("ADDRESS");
180 if (busFind == fruIface->second.end() ||
181 addrFind == fruIface->second.end())
182 {
183 phosphor::logging::log<phosphor::logging::level::INFO>(
184 "fru device missing Bus or Address",
185 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
186 continue;
187 }
188
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700189 uint8_t fruBus = std::get<uint32_t>(busFind->second);
190 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Anoop S358e7df2020-05-05 16:43:34 +0000191 auto chassisFind = fruIface->second.find("CHASSIS_TYPE");
192 std::string chassisType;
193 if (chassisFind != fruIface->second.end())
194 {
195 chassisType = std::get<std::string>(chassisFind->second);
196 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700197
198 uint8_t fruHash = 0;
Anoop S358e7df2020-05-05 16:43:34 +0000199 if (chassisType.compare(chassisTypeRackMount) != 0)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700200 {
201 fruHash = hasher(fru.first.str);
202 // can't be 0xFF based on spec, and 0 is reserved for baseboard
203 if (fruHash == 0 || fruHash == 0xFF)
204 {
205 fruHash = 1;
206 }
207 }
208 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
209
210 bool emplacePassed = false;
211 while (!emplacePassed)
212 {
213 auto resp = deviceHashes.emplace(fruHash, newDev);
214 emplacePassed = resp.second;
215 if (!emplacePassed)
216 {
217 fruHash++;
218 // can't be 0xFF based on spec, and 0 is reserved for
219 // baseboard
220 if (fruHash == 0XFF)
221 {
222 fruHash = 0x1;
223 }
224 }
225 }
226 }
James Feiste4f710d2020-05-20 15:50:30 -0700227}
228
229void replaceCacheFru(const std::shared_ptr<sdbusplus::asio::connection>& bus,
230 boost::asio::yield_context& yield,
231 const std::optional<std::string>& path = std::nullopt)
232{
233 boost::system::error_code ec;
234
235 frus = bus->yield_method_call<ManagedObjectType>(
236 yield, ec, fruDeviceServiceName, "/",
237 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
238 if (ec)
239 {
240 phosphor::logging::log<phosphor::logging::level::ERR>(
241 "GetMangagedObjects for getSensorMap failed",
242 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
243
244 return;
245 }
246 recalculateHashes();
247}
248
249ipmi::Cc getFru(ipmi::Context::ptr ctx, uint8_t devId)
250{
251 if (lastDevId == devId && devId != 0xFF)
252 {
253 return ipmi::ccSuccess;
254 }
255
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700256 auto deviceFind = deviceHashes.find(devId);
257 if (deviceFind == deviceHashes.end())
258 {
259 return IPMI_CC_SENSOR_INVALID;
260 }
261
262 fruCache.clear();
James Feist25690252019-12-23 12:25:49 -0800263
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700264 cacheBus = deviceFind->second.first;
265 cacheAddr = deviceFind->second.second;
James Feist25690252019-12-23 12:25:49 -0800266
James Feiste4f710d2020-05-20 15:50:30 -0700267 boost::system::error_code ec;
268
James Feist25690252019-12-23 12:25:49 -0800269 fruCache = ctx->bus->yield_method_call<std::vector<uint8_t>>(
270 ctx->yield, ec, fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
271 "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus,
272 cacheAddr);
273 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700274 {
James Feist25690252019-12-23 12:25:49 -0800275 phosphor::logging::log<phosphor::logging::level::ERR>(
276 "Couldn't get raw fru",
277 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
278
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700279 cacheBus = 0xFF;
280 cacheAddr = 0xFF;
James Feist25690252019-12-23 12:25:49 -0800281 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700282 }
283
284 lastDevId = devId;
James Feist25690252019-12-23 12:25:49 -0800285 return ipmi::ccSuccess;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700286}
287
James Feiste4f710d2020-05-20 15:50:30 -0700288void writeFruIfRunning()
289{
290 if (!writeTimer->isRunning())
291 {
292 return;
293 }
294 writeTimer->stop();
295 writeFru();
296}
297
298void startMatch(void)
299{
300 if (fruMatches.size())
301 {
302 return;
303 }
304
305 fruMatches.reserve(2);
306
307 auto bus = getSdBus();
308 fruMatches.emplace_back(*bus,
309 "type='signal',arg0path='/xyz/openbmc_project/"
310 "FruDevice/',member='InterfacesAdded'",
311 [](sdbusplus::message::message& message) {
312 sdbusplus::message::object_path path;
313 ObjectType object;
314 try
315 {
316 message.read(path, object);
317 }
318 catch (sdbusplus::exception_t&)
319 {
320 return;
321 }
322 auto findType = object.find(
323 "xyz.openbmc_project.FruDevice");
324 if (findType == object.end())
325 {
326 return;
327 }
328 writeFruIfRunning();
329 frus[path] = object;
330 recalculateHashes();
331 lastDevId = 0xFF;
332 });
333
334 fruMatches.emplace_back(*bus,
335 "type='signal',arg0path='/xyz/openbmc_project/"
336 "FruDevice/',member='InterfacesRemoved'",
337 [](sdbusplus::message::message& message) {
338 sdbusplus::message::object_path path;
339 std::set<std::string> interfaces;
340 try
341 {
342 message.read(path, interfaces);
343 }
344 catch (sdbusplus::exception_t&)
345 {
346 return;
347 }
348 auto findType = interfaces.find(
349 "xyz.openbmc_project.FruDevice");
350 if (findType == interfaces.end())
351 {
352 return;
353 }
354 writeFruIfRunning();
355 frus.erase(path);
356 recalculateHashes();
357 lastDevId = 0xFF;
358 });
359
360 // call once to populate
361 boost::asio::spawn(*getIoContext(), [](boost::asio::yield_context yield) {
362 replaceCacheFru(getSdBus(), yield);
363 });
364}
365
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000366/** @brief implements the read FRU data command
367 * @param fruDeviceId - FRU Device ID
368 * @param fruInventoryOffset - FRU Inventory Offset to write
369 * @param countToRead - Count to read
370 *
371 * @returns ipmi completion code plus response data
372 * - countWritten - Count written
373 */
374ipmi::RspType<uint8_t, // Count
375 std::vector<uint8_t> // Requested data
376 >
James Feist25690252019-12-23 12:25:49 -0800377 ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
378 uint16_t fruInventoryOffset, uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700379{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000380 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700381 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000382 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700383 }
384
James Feiste4f710d2020-05-20 15:50:30 -0700385 ipmi::Cc status = getFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700386
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000387 if (status != ipmi::ccSuccess)
388 {
389 return ipmi::response(status);
390 }
391
392 size_t fromFruByteLen = 0;
393 if (countToRead + fruInventoryOffset < fruCache.size())
394 {
395 fromFruByteLen = countToRead;
396 }
397 else if (fruCache.size() > fruInventoryOffset)
398 {
399 fromFruByteLen = fruCache.size() - fruInventoryOffset;
400 }
401 else
402 {
srikanta mondal92108382020-02-27 18:53:20 +0000403 return ipmi::responseReqDataLenExceeded();
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000404 }
405
406 std::vector<uint8_t> requestedData;
407
408 requestedData.insert(
409 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
410 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
411
Patrick Venture70b17f92019-10-28 20:01:53 -0700412 return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
413 requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700414}
415
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000416/** @brief implements the write FRU data command
417 * @param fruDeviceId - FRU Device ID
418 * @param fruInventoryOffset - FRU Inventory Offset to write
419 * @param dataToWrite - Data to write
420 *
421 * @returns ipmi completion code plus response data
422 * - countWritten - Count written
423 */
424ipmi::RspType<uint8_t>
James Feist25690252019-12-23 12:25:49 -0800425 ipmiStorageWriteFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
426 uint16_t fruInventoryOffset,
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000427 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700428{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000429 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700430 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000431 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700432 }
433
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000434 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700435
James Feiste4f710d2020-05-20 15:50:30 -0700436 ipmi::Cc status = getFru(ctx, fruDeviceId);
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000437 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700438 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000439 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700440 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000441 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700442 if (fruCache.size() < lastWriteAddr)
443 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000444 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700445 }
446
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000447 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
448 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700449
450 bool atEnd = false;
451
452 if (fruCache.size() >= sizeof(FRUHeader))
453 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700454 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
455
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800456 int areaLength = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700457 int lastRecordStart = std::max(
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800458 {header->internalOffset, header->chassisOffset, header->boardOffset,
459 header->productOffset, header->multiRecordOffset});
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700460 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
461
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800462 if (header->multiRecordOffset)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700463 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800464 // This FRU has a MultiRecord Area
465 uint8_t endOfList = 0;
466 // Walk the MultiRecord headers until the last record
467 while (!endOfList)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700468 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800469 // The MSB in the second byte of the MultiRecord header signals
470 // "End of list"
471 endOfList = fruCache[lastRecordStart + 1] & 0x80;
472 // Third byte in the MultiRecord header is the length
473 areaLength = fruCache[lastRecordStart + 2];
474 // This length is in bytes (not 8 bytes like other headers)
475 areaLength += 5; // The length omits the 5 byte header
476 if (!endOfList)
477 {
478 // Next MultiRecord header
479 lastRecordStart += areaLength;
480 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700481 }
482 }
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800483 else
484 {
485 // This FRU does not have a MultiRecord Area
486 // Get the length of the area in multiples of 8 bytes
487 if (lastWriteAddr > (lastRecordStart + 1))
488 {
489 // second byte in record area is the length
490 areaLength = fruCache[lastRecordStart + 1];
491 areaLength *= 8; // it is in multiples of 8 bytes
492 }
493 }
494 if (lastWriteAddr >= (areaLength + lastRecordStart))
495 {
496 atEnd = true;
497 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700498 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000499 uint8_t countWritten = 0;
James Feist25690252019-12-23 12:25:49 -0800500
501 writeBus = cacheBus;
502 writeAddr = cacheAddr;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700503 if (atEnd)
504 {
505 // cancel timer, we're at the end so might as well send it
James Feist25690252019-12-23 12:25:49 -0800506 writeTimer->stop();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700507 if (!writeFru())
508 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000509 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700510 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000511 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700512 }
513 else
514 {
James Feist25690252019-12-23 12:25:49 -0800515 // start a timer, if no further data is sent to check to see if it is
516 // valid
517 writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
518 std::chrono::seconds(writeTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000519 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700520 }
521
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000522 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700523}
524
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000525/** @brief implements the get FRU inventory area info command
526 * @param fruDeviceId - FRU Device ID
527 *
528 * @returns IPMI completion code plus response data
529 * - inventorySize - Number of possible allocation units
530 * - accessType - Allocation unit size in bytes.
531 */
532ipmi::RspType<uint16_t, // inventorySize
533 uint8_t> // accessType
James Feist25690252019-12-23 12:25:49 -0800534 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700535{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000536 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700537 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000538 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700539 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700540
James Feiste4f710d2020-05-20 15:50:30 -0700541 getFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700542
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000543 constexpr uint8_t accessType =
544 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700545
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000546 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700547}
548
James Feist25690252019-12-23 12:25:49 -0800549ipmi_ret_t getFruSdrCount(ipmi::Context::ptr ctx, size_t& count)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700550{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700551 count = deviceHashes.size();
552 return IPMI_CC_OK;
553}
554
James Feist25690252019-12-23 12:25:49 -0800555ipmi_ret_t getFruSdrs(ipmi::Context::ptr ctx, size_t index,
556 get_sdr::SensorDataFruRecord& resp)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700557{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700558 if (deviceHashes.size() < index)
559 {
560 return IPMI_CC_INVALID_FIELD_REQUEST;
561 }
562 auto device = deviceHashes.begin() + index;
563 uint8_t& bus = device->second.first;
564 uint8_t& address = device->second.second;
565
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700566 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
567 auto fru =
568 std::find_if(frus.begin(), frus.end(),
569 [bus, address, &fruData](ManagedEntry& entry) {
570 auto findFruDevice =
571 entry.second.find("xyz.openbmc_project.FruDevice");
572 if (findFruDevice == entry.second.end())
573 {
574 return false;
575 }
576 fruData = &(findFruDevice->second);
577 auto findBus = findFruDevice->second.find("BUS");
578 auto findAddress =
579 findFruDevice->second.find("ADDRESS");
580 if (findBus == findFruDevice->second.end() ||
581 findAddress == findFruDevice->second.end())
582 {
583 return false;
584 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700585 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700586 {
587 return false;
588 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700589 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700590 {
591 return false;
592 }
593 return true;
594 });
595 if (fru == frus.end())
596 {
597 return IPMI_CC_RESPONSE_ERROR;
598 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700599
James Feist25690252019-12-23 12:25:49 -0800600#ifdef USING_ENTITY_MANAGER_DECORATORS
601
Patrick Venture9ce789f2019-10-17 09:09:39 -0700602 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700603
James Feist25690252019-12-23 12:25:49 -0800604 // todo: this should really use caching, this is a very inefficient lookup
605 boost::system::error_code ec;
606 ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
607 ctx->yield, ec, entityManagerServiceName, "/",
608 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
609
610 if (ec)
Patrick Venture9ce789f2019-10-17 09:09:39 -0700611 {
James Feist25690252019-12-23 12:25:49 -0800612 phosphor::logging::log<phosphor::logging::level::ERR>(
613 "GetMangagedObjects for getSensorMap failed",
614 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
Patrick Venture9ce789f2019-10-17 09:09:39 -0700615
James Feist25690252019-12-23 12:25:49 -0800616 return ipmi::ccResponseError;
617 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700618
James Feist25690252019-12-23 12:25:49 -0800619 auto entity = std::find_if(
620 entities.begin(), entities.end(),
621 [bus, address, &entityData](ManagedEntry& entry) {
622 auto findFruDevice = entry.second.find(
623 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
624 if (findFruDevice == entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700625 {
James Feist25690252019-12-23 12:25:49 -0800626 return false;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700627 }
James Feist25690252019-12-23 12:25:49 -0800628
629 // Integer fields added via Entity-Manager json are uint64_ts by
630 // default.
631 auto findBus = findFruDevice->second.find("Bus");
632 auto findAddress = findFruDevice->second.find("Address");
633
634 if (findBus == findFruDevice->second.end() ||
635 findAddress == findFruDevice->second.end())
636 {
637 return false;
638 }
639 if ((std::get<uint64_t>(findBus->second) != bus) ||
640 (std::get<uint64_t>(findAddress->second) != address))
641 {
642 return false;
643 }
644
645 // At this point we found the device entry and should return
646 // true.
647 auto findIpmiDevice = entry.second.find(
648 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
649 if (findIpmiDevice != entry.second.end())
650 {
651 entityData = &(findIpmiDevice->second);
652 }
653
654 return true;
655 });
656
657 if (entity == entities.end())
658 {
659 if constexpr (DEBUG)
660 {
661 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
662 "not found for Fru\n");
Patrick Venture9ce789f2019-10-17 09:09:39 -0700663 }
664 }
James Feist25690252019-12-23 12:25:49 -0800665
666#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700667
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700668 std::string name;
669 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
670 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
671 if (findProductName != fruData->end())
672 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700673 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700674 }
675 else if (findBoardName != fruData->end())
676 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700677 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700678 }
679 else
680 {
681 name = "UNKNOWN";
682 }
683 if (name.size() > maxFruSdrNameSize)
684 {
685 name = name.substr(0, maxFruSdrNameSize);
686 }
687 size_t sizeDiff = maxFruSdrNameSize - name.size();
688
689 resp.header.record_id_lsb = 0x0; // calling code is to implement these
690 resp.header.record_id_msb = 0x0;
691 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700692 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700693 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
694 resp.key.deviceAddress = 0x20;
695 resp.key.fruID = device->first;
696 resp.key.accessLun = 0x80; // logical / physical fru device
697 resp.key.channelNumber = 0x0;
698 resp.body.reserved = 0x0;
699 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700700 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700701
702 uint8_t entityID = 0;
703 uint8_t entityInstance = 0x1;
704
James Feist25690252019-12-23 12:25:49 -0800705#ifdef USING_ENTITY_MANAGER_DECORATORS
Patrick Venture9ce789f2019-10-17 09:09:39 -0700706 if (entityData)
707 {
708 auto entityIdProperty = entityData->find("EntityId");
709 auto entityInstanceProperty = entityData->find("EntityInstance");
710
711 if (entityIdProperty != entityData->end())
712 {
713 entityID = static_cast<uint8_t>(
714 std::get<uint64_t>(entityIdProperty->second));
715 }
716 if (entityInstanceProperty != entityData->end())
717 {
718 entityInstance = static_cast<uint8_t>(
719 std::get<uint64_t>(entityInstanceProperty->second));
720 }
721 }
James Feist25690252019-12-23 12:25:49 -0800722#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700723
724 resp.body.entityID = entityID;
725 resp.body.entityInstance = entityInstance;
726
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700727 resp.body.oem = 0x0;
728 resp.body.deviceIDLen = name.size();
729 name.copy(resp.body.deviceID, name.size());
730
731 return IPMI_CC_OK;
732}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700733
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700734static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800735{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700736 // Loop through the directory looking for ipmi_sel log files
737 for (const std::filesystem::directory_entry& dirEnt :
738 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800739 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700740 std::string filename = dirEnt.path().filename();
741 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800742 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700743 // If we find an ipmi_sel log file, save the path
744 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
745 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800746 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800747 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700748 // As the log files rotate, they are appended with a ".#" that is higher for
749 // the older logs. Since we don't expect more than 10 log files, we
750 // can just sort the list to get them in order from newest to oldest
751 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800752
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700753 return !selLogFiles.empty();
754}
755
756static int countSELEntries()
757{
758 // Get the list of ipmi_sel log files
759 std::vector<std::filesystem::path> selLogFiles;
760 if (!getSELLogFiles(selLogFiles))
761 {
762 return 0;
763 }
764 int numSELEntries = 0;
765 // Loop through each log file and count the number of logs
766 for (const std::filesystem::path& file : selLogFiles)
767 {
768 std::ifstream logStream(file);
769 if (!logStream.is_open())
770 {
771 continue;
772 }
773
774 std::string line;
775 while (std::getline(logStream, line))
776 {
777 numSELEntries++;
778 }
779 }
780 return numSELEntries;
781}
782
783static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700784 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700785 std::string& entry)
786{
787 // Record ID is the first entry field following the timestamp. It is
788 // preceded by a space and followed by a comma
789 std::string search = " " + std::to_string(recordID) + ",";
790
791 // Loop through the ipmi_sel log entries
792 for (const std::filesystem::path& file : selLogFiles)
793 {
794 std::ifstream logStream(file);
795 if (!logStream.is_open())
796 {
797 continue;
798 }
799
800 while (std::getline(logStream, entry))
801 {
802 // Check if the record ID matches
803 if (entry.find(search) != std::string::npos)
804 {
805 return true;
806 }
807 }
808 }
809 return false;
810}
811
812static uint16_t
813 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700814 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700815{
816 uint16_t nextRecordID = recordID + 1;
817 std::string entry;
818 if (findSELEntry(nextRecordID, selLogFiles, entry))
819 {
820 return nextRecordID;
821 }
822 else
823 {
824 return ipmi::sel::lastEntry;
825 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800826}
827
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700828static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800829{
830 for (unsigned int i = 0; i < hexStr.size(); i += 2)
831 {
832 try
833 {
834 data.push_back(static_cast<uint8_t>(
835 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
836 }
837 catch (std::invalid_argument& e)
838 {
839 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
840 return -1;
841 }
842 catch (std::out_of_range& e)
843 {
844 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
845 return -1;
846 }
847 }
848 return 0;
849}
850
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700851ipmi::RspType<uint8_t, // SEL version
852 uint16_t, // SEL entry count
853 uint16_t, // free space
854 uint32_t, // last add timestamp
855 uint32_t, // last erase timestamp
856 uint8_t> // operation support
857 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800858{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700859 constexpr uint8_t selVersion = ipmi::sel::selVersion;
860 uint16_t entries = countSELEntries();
861 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
862 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
863 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
864 constexpr uint8_t operationSupport =
865 intel_oem::ipmi::sel::selOperationSupport;
866 constexpr uint16_t freeSpace =
867 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800868
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700869 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
870 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800871}
872
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700873using systemEventType = std::tuple<
874 uint32_t, // Timestamp
875 uint16_t, // Generator ID
876 uint8_t, // EvM Rev
877 uint8_t, // Sensor Type
878 uint8_t, // Sensor Number
879 uint7_t, // Event Type
880 bool, // Event Direction
881 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
882using oemTsEventType = std::tuple<
883 uint32_t, // Timestamp
884 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
885using oemEventType =
886 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800887
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700888ipmi::RspType<uint16_t, // Next Record ID
889 uint16_t, // Record ID
890 uint8_t, // Record Type
891 std::variant<systemEventType, oemTsEventType,
892 oemEventType>> // Record Content
893 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
894 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800895{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700896 // Only support getting the entire SEL record. If a partial size or non-zero
897 // offset is requested, return an error
898 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800899 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700900 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800901 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800902
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700903 // Check the reservation ID if one is provided or required (only if the
904 // offset is non-zero)
905 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800906 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700907 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800908 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700909 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800910 }
911 }
912
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700913 // Get the ipmi_sel log files
914 std::vector<std::filesystem::path> selLogFiles;
915 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800916 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700917 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800918 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800919
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700920 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800921
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800922 if (targetID == ipmi::sel::firstEntry)
923 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700924 // The first entry will be at the top of the oldest log file
925 std::ifstream logStream(selLogFiles.back());
926 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800927 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700928 return ipmi::responseUnspecifiedError();
929 }
930
931 if (!std::getline(logStream, targetEntry))
932 {
933 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800934 }
935 }
936 else if (targetID == ipmi::sel::lastEntry)
937 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700938 // The last entry will be at the bottom of the newest log file
939 std::ifstream logStream(selLogFiles.front());
940 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800941 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700942 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800943 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700944
945 std::string line;
946 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800947 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700948 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800949 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800950 }
951 else
952 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700953 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800954 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700955 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800956 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800957 }
958
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700959 // The format of the ipmi_sel message is "<Timestamp>
960 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
961 // First get the Timestamp
962 size_t space = targetEntry.find_first_of(" ");
963 if (space == std::string::npos)
964 {
965 return ipmi::responseUnspecifiedError();
966 }
967 std::string entryTimestamp = targetEntry.substr(0, space);
968 // Then get the log contents
969 size_t entryStart = targetEntry.find_first_not_of(" ", space);
970 if (entryStart == std::string::npos)
971 {
972 return ipmi::responseUnspecifiedError();
973 }
974 std::string_view entry(targetEntry);
975 entry.remove_prefix(entryStart);
976 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700977 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700978 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700979 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700980 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700981 {
982 return ipmi::responseUnspecifiedError();
983 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700984 std::string& recordIDStr = targetEntryFields[0];
985 std::string& recordTypeStr = targetEntryFields[1];
986 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700987
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700988 uint16_t recordID;
989 uint8_t recordType;
990 try
991 {
992 recordID = std::stoul(recordIDStr);
993 recordType = std::stoul(recordTypeStr, nullptr, 16);
994 }
995 catch (const std::invalid_argument&)
996 {
997 return ipmi::responseUnspecifiedError();
998 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700999 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001000 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07001001 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001002 {
1003 return ipmi::responseUnspecifiedError();
1004 }
1005
1006 if (recordType == intel_oem::ipmi::sel::systemEvent)
1007 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001008 // Get the timestamp
1009 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -07001010 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001011
1012 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1013 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1014 {
1015 timestamp = std::mktime(&timeStruct);
1016 }
1017
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001018 // Set the event message revision
1019 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
1020
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07001021 uint16_t generatorID = 0;
1022 uint8_t sensorType = 0;
Johnathan Mantey308c3a82020-07-22 11:50:54 -07001023 uint16_t sensorAndLun = 0;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07001024 uint8_t sensorNum = 0xFF;
1025 uint7_t eventType = 0;
1026 bool eventDir = 0;
1027 // System type events should have six fields
1028 if (targetEntryFields.size() >= 6)
1029 {
1030 std::string& generatorIDStr = targetEntryFields[3];
1031 std::string& sensorPath = targetEntryFields[4];
1032 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001033
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07001034 // Get the generator ID
1035 try
1036 {
1037 generatorID = std::stoul(generatorIDStr, nullptr, 16);
1038 }
1039 catch (const std::invalid_argument&)
1040 {
1041 std::cerr << "Invalid Generator ID\n";
1042 }
1043
1044 // Get the sensor type, sensor number, and event type for the sensor
1045 sensorType = getSensorTypeFromPath(sensorPath);
Johnathan Mantey308c3a82020-07-22 11:50:54 -07001046 sensorAndLun = getSensorNumberFromPath(sensorPath);
1047 sensorNum = static_cast<uint8_t>(sensorAndLun);
1048 generatorID |= sensorAndLun >> 8;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -07001049 eventType = getSensorEventTypeFromPath(sensorPath);
1050
1051 // Get the event direction
1052 try
1053 {
1054 eventDir = std::stoul(eventDirStr) ? 0 : 1;
1055 }
1056 catch (const std::invalid_argument&)
1057 {
1058 std::cerr << "Invalid Event Direction\n";
1059 }
1060 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001061
1062 // Only keep the eventData bytes that fit in the record
1063 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
1064 std::copy_n(eventDataBytes.begin(),
1065 std::min(eventDataBytes.size(), eventData.size()),
1066 eventData.begin());
1067
1068 return ipmi::responseSuccess(
1069 nextRecordID, recordID, recordType,
1070 systemEventType{timestamp, generatorID, evmRev, sensorType,
1071 sensorNum, eventType, eventDir, eventData});
1072 }
1073 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
1074 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
1075 {
1076 // Get the timestamp
1077 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -07001078 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001079
1080 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1081 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1082 {
1083 timestamp = std::mktime(&timeStruct);
1084 }
1085
1086 // Only keep the bytes that fit in the record
1087 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
1088 std::copy_n(eventDataBytes.begin(),
1089 std::min(eventDataBytes.size(), eventData.size()),
1090 eventData.begin());
1091
1092 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1093 oemTsEventType{timestamp, eventData});
1094 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001095 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001096 {
1097 // Only keep the bytes that fit in the record
1098 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1099 std::copy_n(eventDataBytes.begin(),
1100 std::min(eventDataBytes.size(), eventData.size()),
1101 eventData.begin());
1102
1103 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1104 eventData);
1105 }
1106
1107 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001108}
1109
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001110ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1111 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1112 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1113 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1114 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001115{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001116 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1117 // added
1118 cancelSELReservation();
1119
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001120 // Send this request to the Redfish hooks to log it as a Redfish message
1121 // instead. There is no need to add it to the SEL, so just return success.
1122 intel_oem::ipmi::sel::checkRedfishHooks(
1123 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1124 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001125
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001126 uint16_t responseID = 0xFFFF;
1127 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001128}
1129
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001130ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1131 uint16_t reservationID,
1132 const std::array<uint8_t, 3>& clr,
1133 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001134{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001135 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001136 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001137 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001138 }
1139
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001140 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1141 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001142 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001143 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001144 }
1145
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001146 // Erasure status cannot be fetched, so always return erasure status as
1147 // `erase completed`.
1148 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001149 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001150 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001151 }
1152
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001153 // Check that initiate erase is correct
1154 if (eraseOperation != ipmi::sel::initiateErase)
1155 {
1156 return ipmi::responseInvalidFieldRequest();
1157 }
1158
1159 // Per the IPMI spec, need to cancel any reservation when the SEL is
1160 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001161 cancelSELReservation();
1162
Jason M. Bills7944c302019-03-20 15:24:05 -07001163 // Save the erase time
1164 intel_oem::ipmi::sel::erase_time::save();
1165
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001166 // Clear the SEL by deleting the log files
1167 std::vector<std::filesystem::path> selLogFiles;
1168 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001169 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001170 for (const std::filesystem::path& file : selLogFiles)
1171 {
1172 std::error_code ec;
1173 std::filesystem::remove(file, ec);
1174 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001175 }
1176
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001177 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001178 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1179 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001180 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1181 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1182 rsyslogReload.append("rsyslog.service", "replace");
1183 try
1184 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001185 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001186 }
1187 catch (sdbusplus::exception_t& e)
1188 {
1189 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1190 }
1191
1192 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001193}
1194
Jason M. Bills1a474622019-06-14 14:51:33 -07001195ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1196{
1197 struct timespec selTime = {};
1198
1199 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1200 {
1201 return ipmi::responseUnspecifiedError();
1202 }
1203
1204 return ipmi::responseSuccess(selTime.tv_sec);
1205}
1206
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001207ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001208{
1209 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001210 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001211}
1212
James Feist74c50c62019-08-14 14:18:41 -07001213std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1214{
1215 std::vector<uint8_t> resp;
1216 if (index == 0)
1217 {
1218 Type12Record bmc = {};
1219 bmc.header.record_id_lsb = recordId;
1220 bmc.header.record_id_msb = recordId >> 8;
1221 bmc.header.sdr_version = ipmiSdrVersion;
1222 bmc.header.record_type = 0x12;
1223 bmc.header.record_length = 0x1b;
1224 bmc.slaveAddress = 0x20;
1225 bmc.channelNumber = 0;
1226 bmc.powerStateNotification = 0;
1227 bmc.deviceCapabilities = 0xBF;
1228 bmc.reserved = 0;
1229 bmc.entityID = 0x2E;
1230 bmc.entityInstance = 1;
1231 bmc.oem = 0;
1232 bmc.typeLengthCode = 0xD0;
1233 std::string bmcName = "Basbrd Mgmt Ctlr";
1234 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1235 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1236 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1237 }
1238 else if (index == 1)
1239 {
1240 Type12Record me = {};
1241 me.header.record_id_lsb = recordId;
1242 me.header.record_id_msb = recordId >> 8;
1243 me.header.sdr_version = ipmiSdrVersion;
1244 me.header.record_type = 0x12;
1245 me.header.record_length = 0x16;
1246 me.slaveAddress = 0x2C;
1247 me.channelNumber = 6;
1248 me.powerStateNotification = 0x24;
1249 me.deviceCapabilities = 0x21;
1250 me.reserved = 0;
1251 me.entityID = 0x2E;
1252 me.entityInstance = 2;
1253 me.oem = 0;
1254 me.typeLengthCode = 0xCB;
1255 std::string meName = "Mgmt Engine";
1256 std::copy(meName.begin(), meName.end(), me.name);
1257 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1258 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1259 }
1260 else
1261 {
1262 throw std::runtime_error("getType12SDRs:: Illegal index " +
1263 std::to_string(index));
1264 }
1265
1266 return resp;
1267}
1268
Yong Lifee5e4c2020-01-17 19:36:29 +08001269std::vector<uint8_t> getNMDiscoverySDR(uint16_t index, uint16_t recordId)
1270{
1271 std::vector<uint8_t> resp;
1272 if (index == 0)
1273 {
1274 NMDiscoveryRecord nm = {};
1275 nm.header.record_id_lsb = recordId;
1276 nm.header.record_id_msb = recordId >> 8;
1277 nm.header.sdr_version = ipmiSdrVersion;
1278 nm.header.record_type = 0xC0;
1279 nm.header.record_length = 0xB;
1280 nm.oemID0 = 0x57;
1281 nm.oemID1 = 0x1;
1282 nm.oemID2 = 0x0;
1283 nm.subType = 0x0D;
1284 nm.version = 0x1;
1285 nm.slaveAddress = 0x2C;
1286 nm.channelNumber = 0x60;
1287 nm.healthEventSensor = 0x19;
1288 nm.exceptionEventSensor = 0x18;
1289 nm.operationalCapSensor = 0x1A;
1290 nm.thresholdExceededSensor = 0x1B;
1291
1292 uint8_t* nmPtr = reinterpret_cast<uint8_t*>(&nm);
1293 resp.insert(resp.end(), nmPtr, nmPtr + sizeof(NMDiscoveryRecord));
1294 }
1295 else
1296 {
1297 throw std::runtime_error("getNMDiscoverySDR:: Illegal index " +
1298 std::to_string(index));
1299 }
1300
1301 return resp;
1302}
1303
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001304void registerStorageFunctions()
1305{
James Feist25690252019-12-23 12:25:49 -08001306 createTimers();
James Feiste4f710d2020-05-20 15:50:30 -07001307 startMatch();
1308
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001309 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001310 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1311 ipmi::storage::cmdGetFruInventoryAreaInfo,
1312 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001313 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001314 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1315 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1316 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001317
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001318 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001319 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1320 ipmi::storage::cmdWriteFruData,
1321 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001322
1323 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001324 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001325 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1326 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001327
1328 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001329 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001330 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1331 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001332
1333 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001334 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001335 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001336 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001337
1338 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001339 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1340 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1341 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001342
Jason M. Bills1a474622019-06-14 14:51:33 -07001343 // <Get SEL Time>
1344 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001345 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1346 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001347
Jason M. Billscac97a52019-01-30 14:43:46 -08001348 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001349 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1350 ipmi::storage::cmdSetSelTime,
1351 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001352}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001353} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001354} // namespace ipmi