blob: 9c73b6a65291ce720f5d72db9237dc0d524a44d2 [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;
Anoop S358e7df2020-05-05 16:43:34 +0000109constexpr static const char* chassisTypeRackMount = "23";
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700110
Jason M. Bills4ed6f2c2019-04-02 12:21:25 -0700111// event direction is bit[7] of eventType where 1b = Deassertion event
112constexpr static const uint8_t deassertionEvent = 0x80;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800113
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700114static std::vector<uint8_t> fruCache;
115static uint8_t cacheBus = 0xFF;
116static uint8_t cacheAddr = 0XFF;
117
James Feist25690252019-12-23 12:25:49 -0800118static uint8_t writeBus = 0xFF;
119static uint8_t writeAddr = 0XFF;
120
121std::unique_ptr<phosphor::Timer> writeTimer = nullptr;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700122std::unique_ptr<phosphor::Timer> cacheTimer = nullptr;
123
James Feist25690252019-12-23 12:25:49 -0800124ManagedObjectType frus;
125
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700126// we unfortunately have to build a map of hashes in case there is a
127// collision to verify our dev-id
128boost::container::flat_map<uint8_t, std::pair<uint8_t, uint8_t>> deviceHashes;
129
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700130void registerStorageFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700131
132bool writeFru()
133{
James Feist25690252019-12-23 12:25:49 -0800134 if (writeBus == 0xFF && writeAddr == 0xFF)
135 {
136 return true;
137 }
Vernon Mauery15419dd2019-05-24 09:40:30 -0700138 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
139 sdbusplus::message::message writeFru = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700140 fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
141 "xyz.openbmc_project.FruDeviceManager", "WriteFru");
James Feist25690252019-12-23 12:25:49 -0800142 writeFru.append(writeBus, writeAddr, fruCache);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700143 try
144 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700145 sdbusplus::message::message writeFruResp = dbus->call(writeFru);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700146 }
147 catch (sdbusplus::exception_t&)
148 {
149 // todo: log sel?
150 phosphor::logging::log<phosphor::logging::level::ERR>(
151 "error writing fru");
152 return false;
153 }
James Feist25690252019-12-23 12:25:49 -0800154 writeBus = 0xFF;
155 writeAddr = 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700156 return true;
157}
158
James Feist25690252019-12-23 12:25:49 -0800159void createTimers()
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700160{
James Feist25690252019-12-23 12:25:49 -0800161
162 writeTimer = std::make_unique<phosphor::Timer>(writeFru);
163 cacheTimer = std::make_unique<phosphor::Timer>([]() { return; });
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700164}
165
James Feist25690252019-12-23 12:25:49 -0800166ipmi::Cc replaceCacheFru(ipmi::Context::ptr ctx, uint8_t devId)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700167{
168 static uint8_t lastDevId = 0xFF;
169
170 bool timerRunning = (cacheTimer != nullptr) && !cacheTimer->isExpired();
171 if (lastDevId == devId && timerRunning)
172 {
173 return IPMI_CC_OK; // cache already up to date
174 }
175 // if timer is running, stop it and writeFru manually
176 else if (timerRunning)
177 {
178 cacheTimer->stop();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700179 }
180
James Feist25690252019-12-23 12:25:49 -0800181 cacheTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
182 std::chrono::seconds(cacheTimeoutSeconds)));
183
184 boost::system::error_code ec;
185
186 frus = ctx->bus->yield_method_call<ManagedObjectType>(
187 ctx->yield, ec, fruDeviceServiceName, "/",
188 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
189 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700190 {
191 phosphor::logging::log<phosphor::logging::level::ERR>(
James Feist25690252019-12-23 12:25:49 -0800192 "GetMangagedObjects for getSensorMap failed",
193 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
194
195 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700196 }
197
198 deviceHashes.clear();
199
200 // hash the object paths to create unique device id's. increment on
201 // collision
202 std::hash<std::string> hasher;
203 for (const auto& fru : frus)
204 {
205 auto fruIface = fru.second.find("xyz.openbmc_project.FruDevice");
206 if (fruIface == fru.second.end())
207 {
208 continue;
209 }
210
211 auto busFind = fruIface->second.find("BUS");
212 auto addrFind = fruIface->second.find("ADDRESS");
213 if (busFind == fruIface->second.end() ||
214 addrFind == fruIface->second.end())
215 {
216 phosphor::logging::log<phosphor::logging::level::INFO>(
217 "fru device missing Bus or Address",
218 phosphor::logging::entry("FRU=%s", fru.first.str.c_str()));
219 continue;
220 }
221
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700222 uint8_t fruBus = std::get<uint32_t>(busFind->second);
223 uint8_t fruAddr = std::get<uint32_t>(addrFind->second);
Anoop S358e7df2020-05-05 16:43:34 +0000224 auto chassisFind = fruIface->second.find("CHASSIS_TYPE");
225 std::string chassisType;
226 if (chassisFind != fruIface->second.end())
227 {
228 chassisType = std::get<std::string>(chassisFind->second);
229 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700230
231 uint8_t fruHash = 0;
Anoop S358e7df2020-05-05 16:43:34 +0000232 if (chassisType.compare(chassisTypeRackMount) != 0)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700233 {
234 fruHash = hasher(fru.first.str);
235 // can't be 0xFF based on spec, and 0 is reserved for baseboard
236 if (fruHash == 0 || fruHash == 0xFF)
237 {
238 fruHash = 1;
239 }
240 }
241 std::pair<uint8_t, uint8_t> newDev(fruBus, fruAddr);
242
243 bool emplacePassed = false;
244 while (!emplacePassed)
245 {
246 auto resp = deviceHashes.emplace(fruHash, newDev);
247 emplacePassed = resp.second;
248 if (!emplacePassed)
249 {
250 fruHash++;
251 // can't be 0xFF based on spec, and 0 is reserved for
252 // baseboard
253 if (fruHash == 0XFF)
254 {
255 fruHash = 0x1;
256 }
257 }
258 }
259 }
260 auto deviceFind = deviceHashes.find(devId);
261 if (deviceFind == deviceHashes.end())
262 {
263 return IPMI_CC_SENSOR_INVALID;
264 }
265
266 fruCache.clear();
James Feist25690252019-12-23 12:25:49 -0800267 ec.clear();
268
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700269 cacheBus = deviceFind->second.first;
270 cacheAddr = deviceFind->second.second;
James Feist25690252019-12-23 12:25:49 -0800271
272 fruCache = ctx->bus->yield_method_call<std::vector<uint8_t>>(
273 ctx->yield, ec, fruDeviceServiceName, "/xyz/openbmc_project/FruDevice",
274 "xyz.openbmc_project.FruDeviceManager", "GetRawFru", cacheBus,
275 cacheAddr);
276 if (ec)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700277 {
James Feist25690252019-12-23 12:25:49 -0800278 phosphor::logging::log<phosphor::logging::level::ERR>(
279 "Couldn't get raw fru",
280 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
281
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700282 lastDevId = 0xFF;
283 cacheBus = 0xFF;
284 cacheAddr = 0xFF;
James Feist25690252019-12-23 12:25:49 -0800285 return ipmi::ccResponseError;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700286 }
287
288 lastDevId = devId;
James Feist25690252019-12-23 12:25:49 -0800289 return ipmi::ccSuccess;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700290}
291
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000292/** @brief implements the read FRU data command
293 * @param fruDeviceId - FRU Device ID
294 * @param fruInventoryOffset - FRU Inventory Offset to write
295 * @param countToRead - Count to read
296 *
297 * @returns ipmi completion code plus response data
298 * - countWritten - Count written
299 */
300ipmi::RspType<uint8_t, // Count
301 std::vector<uint8_t> // Requested data
302 >
James Feist25690252019-12-23 12:25:49 -0800303 ipmiStorageReadFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
304 uint16_t fruInventoryOffset, uint8_t countToRead)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700305{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000306 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700307 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000308 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700309 }
310
James Feist25690252019-12-23 12:25:49 -0800311 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700312
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000313 if (status != ipmi::ccSuccess)
314 {
315 return ipmi::response(status);
316 }
317
318 size_t fromFruByteLen = 0;
319 if (countToRead + fruInventoryOffset < fruCache.size())
320 {
321 fromFruByteLen = countToRead;
322 }
323 else if (fruCache.size() > fruInventoryOffset)
324 {
325 fromFruByteLen = fruCache.size() - fruInventoryOffset;
326 }
327 else
328 {
srikanta mondal92108382020-02-27 18:53:20 +0000329 return ipmi::responseReqDataLenExceeded();
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000330 }
331
332 std::vector<uint8_t> requestedData;
333
334 requestedData.insert(
335 requestedData.begin(), fruCache.begin() + fruInventoryOffset,
336 fruCache.begin() + fruInventoryOffset + fromFruByteLen);
337
Patrick Venture70b17f92019-10-28 20:01:53 -0700338 return ipmi::responseSuccess(static_cast<uint8_t>(requestedData.size()),
339 requestedData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700340}
341
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000342/** @brief implements the write FRU data command
343 * @param fruDeviceId - FRU Device ID
344 * @param fruInventoryOffset - FRU Inventory Offset to write
345 * @param dataToWrite - Data to write
346 *
347 * @returns ipmi completion code plus response data
348 * - countWritten - Count written
349 */
350ipmi::RspType<uint8_t>
James Feist25690252019-12-23 12:25:49 -0800351 ipmiStorageWriteFruData(ipmi::Context::ptr ctx, uint8_t fruDeviceId,
352 uint16_t fruInventoryOffset,
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000353 std::vector<uint8_t>& dataToWrite)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700354{
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000355 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700356 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000357 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700358 }
359
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000360 size_t writeLen = dataToWrite.size();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700361
James Feist25690252019-12-23 12:25:49 -0800362 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000363 if (status != ipmi::ccSuccess)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700364 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000365 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700366 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000367 int lastWriteAddr = fruInventoryOffset + writeLen;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700368 if (fruCache.size() < lastWriteAddr)
369 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000370 fruCache.resize(fruInventoryOffset + writeLen);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700371 }
372
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000373 std::copy(dataToWrite.begin(), dataToWrite.begin() + writeLen,
374 fruCache.begin() + fruInventoryOffset);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700375
376 bool atEnd = false;
377
378 if (fruCache.size() >= sizeof(FRUHeader))
379 {
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700380 FRUHeader* header = reinterpret_cast<FRUHeader*>(fruCache.data());
381
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800382 int areaLength = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700383 int lastRecordStart = std::max(
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800384 {header->internalOffset, header->chassisOffset, header->boardOffset,
385 header->productOffset, header->multiRecordOffset});
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700386 lastRecordStart *= 8; // header starts in are multiples of 8 bytes
387
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800388 if (header->multiRecordOffset)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700389 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800390 // This FRU has a MultiRecord Area
391 uint8_t endOfList = 0;
392 // Walk the MultiRecord headers until the last record
393 while (!endOfList)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700394 {
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800395 // The MSB in the second byte of the MultiRecord header signals
396 // "End of list"
397 endOfList = fruCache[lastRecordStart + 1] & 0x80;
398 // Third byte in the MultiRecord header is the length
399 areaLength = fruCache[lastRecordStart + 2];
400 // This length is in bytes (not 8 bytes like other headers)
401 areaLength += 5; // The length omits the 5 byte header
402 if (!endOfList)
403 {
404 // Next MultiRecord header
405 lastRecordStart += areaLength;
406 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700407 }
408 }
Peter Lundgrenc59391f2019-11-19 14:26:15 -0800409 else
410 {
411 // This FRU does not have a MultiRecord Area
412 // Get the length of the area in multiples of 8 bytes
413 if (lastWriteAddr > (lastRecordStart + 1))
414 {
415 // second byte in record area is the length
416 areaLength = fruCache[lastRecordStart + 1];
417 areaLength *= 8; // it is in multiples of 8 bytes
418 }
419 }
420 if (lastWriteAddr >= (areaLength + lastRecordStart))
421 {
422 atEnd = true;
423 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700424 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000425 uint8_t countWritten = 0;
James Feist25690252019-12-23 12:25:49 -0800426
427 writeBus = cacheBus;
428 writeAddr = cacheAddr;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700429 if (atEnd)
430 {
431 // cancel timer, we're at the end so might as well send it
James Feist25690252019-12-23 12:25:49 -0800432 writeTimer->stop();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700433 if (!writeFru())
434 {
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000435 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700436 }
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000437 countWritten = std::min(fruCache.size(), static_cast<size_t>(0xFF));
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700438 }
439 else
440 {
James Feist25690252019-12-23 12:25:49 -0800441 // start a timer, if no further data is sent to check to see if it is
442 // valid
443 writeTimer->start(std::chrono::duration_cast<std::chrono::microseconds>(
444 std::chrono::seconds(writeTimeoutSeconds)));
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000445 countWritten = 0;
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700446 }
447
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +0000448 return ipmi::responseSuccess(countWritten);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700449}
450
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000451/** @brief implements the get FRU inventory area info command
452 * @param fruDeviceId - FRU Device ID
453 *
454 * @returns IPMI completion code plus response data
455 * - inventorySize - Number of possible allocation units
456 * - accessType - Allocation unit size in bytes.
457 */
458ipmi::RspType<uint16_t, // inventorySize
459 uint8_t> // accessType
James Feist25690252019-12-23 12:25:49 -0800460 ipmiStorageGetFruInvAreaInfo(ipmi::Context::ptr ctx, uint8_t fruDeviceId)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700461{
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000462 if (fruDeviceId == 0xFF)
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700463 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000464 return ipmi::responseInvalidFieldRequest();
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700465 }
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700466
James Feist25690252019-12-23 12:25:49 -0800467 ipmi::Cc status = replaceCacheFru(ctx, fruDeviceId);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700468
469 if (status != IPMI_CC_OK)
470 {
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000471 return ipmi::response(status);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700472 }
473
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000474 constexpr uint8_t accessType =
475 static_cast<uint8_t>(GetFRUAreaAccessType::byte);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700476
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +0000477 return ipmi::responseSuccess(fruCache.size(), accessType);
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700478}
479
James Feist25690252019-12-23 12:25:49 -0800480ipmi_ret_t getFruSdrCount(ipmi::Context::ptr ctx, size_t& count)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700481{
James Feist25690252019-12-23 12:25:49 -0800482 ipmi_ret_t ret = replaceCacheFru(ctx, 0);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700483 if (ret != IPMI_CC_OK)
484 {
485 return ret;
486 }
487 count = deviceHashes.size();
488 return IPMI_CC_OK;
489}
490
James Feist25690252019-12-23 12:25:49 -0800491ipmi_ret_t getFruSdrs(ipmi::Context::ptr ctx, size_t index,
492 get_sdr::SensorDataFruRecord& resp)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700493{
James Feist25690252019-12-23 12:25:49 -0800494 ipmi_ret_t ret = replaceCacheFru(ctx, 0); // this will update the hash list
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700495 if (ret != IPMI_CC_OK)
496 {
497 return ret;
498 }
499 if (deviceHashes.size() < index)
500 {
501 return IPMI_CC_INVALID_FIELD_REQUEST;
502 }
503 auto device = deviceHashes.begin() + index;
504 uint8_t& bus = device->second.first;
505 uint8_t& address = device->second.second;
506
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700507 boost::container::flat_map<std::string, DbusVariant>* fruData = nullptr;
508 auto fru =
509 std::find_if(frus.begin(), frus.end(),
510 [bus, address, &fruData](ManagedEntry& entry) {
511 auto findFruDevice =
512 entry.second.find("xyz.openbmc_project.FruDevice");
513 if (findFruDevice == entry.second.end())
514 {
515 return false;
516 }
517 fruData = &(findFruDevice->second);
518 auto findBus = findFruDevice->second.find("BUS");
519 auto findAddress =
520 findFruDevice->second.find("ADDRESS");
521 if (findBus == findFruDevice->second.end() ||
522 findAddress == findFruDevice->second.end())
523 {
524 return false;
525 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700526 if (std::get<uint32_t>(findBus->second) != bus)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700527 {
528 return false;
529 }
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700530 if (std::get<uint32_t>(findAddress->second) != address)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700531 {
532 return false;
533 }
534 return true;
535 });
536 if (fru == frus.end())
537 {
538 return IPMI_CC_RESPONSE_ERROR;
539 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700540
James Feist25690252019-12-23 12:25:49 -0800541#ifdef USING_ENTITY_MANAGER_DECORATORS
542
Patrick Venture9ce789f2019-10-17 09:09:39 -0700543 boost::container::flat_map<std::string, DbusVariant>* entityData = nullptr;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700544
James Feist25690252019-12-23 12:25:49 -0800545 // todo: this should really use caching, this is a very inefficient lookup
546 boost::system::error_code ec;
547 ManagedObjectType entities = ctx->bus->yield_method_call<ManagedObjectType>(
548 ctx->yield, ec, entityManagerServiceName, "/",
549 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
550
551 if (ec)
Patrick Venture9ce789f2019-10-17 09:09:39 -0700552 {
James Feist25690252019-12-23 12:25:49 -0800553 phosphor::logging::log<phosphor::logging::level::ERR>(
554 "GetMangagedObjects for getSensorMap failed",
555 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
Patrick Venture9ce789f2019-10-17 09:09:39 -0700556
James Feist25690252019-12-23 12:25:49 -0800557 return ipmi::ccResponseError;
558 }
Patrick Venture9ce789f2019-10-17 09:09:39 -0700559
James Feist25690252019-12-23 12:25:49 -0800560 auto entity = std::find_if(
561 entities.begin(), entities.end(),
562 [bus, address, &entityData](ManagedEntry& entry) {
563 auto findFruDevice = entry.second.find(
564 "xyz.openbmc_project.Inventory.Decorator.FruDevice");
565 if (findFruDevice == entry.second.end())
Patrick Venture9ce789f2019-10-17 09:09:39 -0700566 {
James Feist25690252019-12-23 12:25:49 -0800567 return false;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700568 }
James Feist25690252019-12-23 12:25:49 -0800569
570 // Integer fields added via Entity-Manager json are uint64_ts by
571 // default.
572 auto findBus = findFruDevice->second.find("Bus");
573 auto findAddress = findFruDevice->second.find("Address");
574
575 if (findBus == findFruDevice->second.end() ||
576 findAddress == findFruDevice->second.end())
577 {
578 return false;
579 }
580 if ((std::get<uint64_t>(findBus->second) != bus) ||
581 (std::get<uint64_t>(findAddress->second) != address))
582 {
583 return false;
584 }
585
586 // At this point we found the device entry and should return
587 // true.
588 auto findIpmiDevice = entry.second.find(
589 "xyz.openbmc_project.Inventory.Decorator.Ipmi");
590 if (findIpmiDevice != entry.second.end())
591 {
592 entityData = &(findIpmiDevice->second);
593 }
594
595 return true;
596 });
597
598 if (entity == entities.end())
599 {
600 if constexpr (DEBUG)
601 {
602 std::fprintf(stderr, "Ipmi or FruDevice Decorator interface "
603 "not found for Fru\n");
Patrick Venture9ce789f2019-10-17 09:09:39 -0700604 }
605 }
James Feist25690252019-12-23 12:25:49 -0800606
607#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700608
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700609 std::string name;
610 auto findProductName = fruData->find("BOARD_PRODUCT_NAME");
611 auto findBoardName = fruData->find("PRODUCT_PRODUCT_NAME");
612 if (findProductName != fruData->end())
613 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700614 name = std::get<std::string>(findProductName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700615 }
616 else if (findBoardName != fruData->end())
617 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700618 name = std::get<std::string>(findBoardName->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700619 }
620 else
621 {
622 name = "UNKNOWN";
623 }
624 if (name.size() > maxFruSdrNameSize)
625 {
626 name = name.substr(0, maxFruSdrNameSize);
627 }
628 size_t sizeDiff = maxFruSdrNameSize - name.size();
629
630 resp.header.record_id_lsb = 0x0; // calling code is to implement these
631 resp.header.record_id_msb = 0x0;
632 resp.header.sdr_version = ipmiSdrVersion;
Patrick Venture73d01352019-10-11 18:32:59 -0700633 resp.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700634 resp.header.record_length = sizeof(resp.body) + sizeof(resp.key) - sizeDiff;
635 resp.key.deviceAddress = 0x20;
636 resp.key.fruID = device->first;
637 resp.key.accessLun = 0x80; // logical / physical fru device
638 resp.key.channelNumber = 0x0;
639 resp.body.reserved = 0x0;
640 resp.body.deviceType = 0x10;
James Feist4f86d1f2019-04-03 10:30:26 -0700641 resp.body.deviceTypeModifier = 0x0;
Patrick Venture9ce789f2019-10-17 09:09:39 -0700642
643 uint8_t entityID = 0;
644 uint8_t entityInstance = 0x1;
645
James Feist25690252019-12-23 12:25:49 -0800646#ifdef USING_ENTITY_MANAGER_DECORATORS
Patrick Venture9ce789f2019-10-17 09:09:39 -0700647 if (entityData)
648 {
649 auto entityIdProperty = entityData->find("EntityId");
650 auto entityInstanceProperty = entityData->find("EntityInstance");
651
652 if (entityIdProperty != entityData->end())
653 {
654 entityID = static_cast<uint8_t>(
655 std::get<uint64_t>(entityIdProperty->second));
656 }
657 if (entityInstanceProperty != entityData->end())
658 {
659 entityInstance = static_cast<uint8_t>(
660 std::get<uint64_t>(entityInstanceProperty->second));
661 }
662 }
James Feist25690252019-12-23 12:25:49 -0800663#endif
Patrick Venture9ce789f2019-10-17 09:09:39 -0700664
665 resp.body.entityID = entityID;
666 resp.body.entityInstance = entityInstance;
667
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700668 resp.body.oem = 0x0;
669 resp.body.deviceIDLen = name.size();
670 name.copy(resp.body.deviceID, name.size());
671
672 return IPMI_CC_OK;
673}
Jason M. Billse2d1aee2018-10-03 15:57:18 -0700674
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700675static bool getSELLogFiles(std::vector<std::filesystem::path>& selLogFiles)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800676{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700677 // Loop through the directory looking for ipmi_sel log files
678 for (const std::filesystem::directory_entry& dirEnt :
679 std::filesystem::directory_iterator(intel_oem::ipmi::sel::selLogDir))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800680 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700681 std::string filename = dirEnt.path().filename();
682 if (boost::starts_with(filename, intel_oem::ipmi::sel::selLogFilename))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800683 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700684 // If we find an ipmi_sel log file, save the path
685 selLogFiles.emplace_back(intel_oem::ipmi::sel::selLogDir /
686 filename);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800687 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800688 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700689 // As the log files rotate, they are appended with a ".#" that is higher for
690 // the older logs. Since we don't expect more than 10 log files, we
691 // can just sort the list to get them in order from newest to oldest
692 std::sort(selLogFiles.begin(), selLogFiles.end());
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800693
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700694 return !selLogFiles.empty();
695}
696
697static int countSELEntries()
698{
699 // Get the list of ipmi_sel log files
700 std::vector<std::filesystem::path> selLogFiles;
701 if (!getSELLogFiles(selLogFiles))
702 {
703 return 0;
704 }
705 int numSELEntries = 0;
706 // Loop through each log file and count the number of logs
707 for (const std::filesystem::path& file : selLogFiles)
708 {
709 std::ifstream logStream(file);
710 if (!logStream.is_open())
711 {
712 continue;
713 }
714
715 std::string line;
716 while (std::getline(logStream, line))
717 {
718 numSELEntries++;
719 }
720 }
721 return numSELEntries;
722}
723
724static bool findSELEntry(const int recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700725 const std::vector<std::filesystem::path>& selLogFiles,
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700726 std::string& entry)
727{
728 // Record ID is the first entry field following the timestamp. It is
729 // preceded by a space and followed by a comma
730 std::string search = " " + std::to_string(recordID) + ",";
731
732 // Loop through the ipmi_sel log entries
733 for (const std::filesystem::path& file : selLogFiles)
734 {
735 std::ifstream logStream(file);
736 if (!logStream.is_open())
737 {
738 continue;
739 }
740
741 while (std::getline(logStream, entry))
742 {
743 // Check if the record ID matches
744 if (entry.find(search) != std::string::npos)
745 {
746 return true;
747 }
748 }
749 }
750 return false;
751}
752
753static uint16_t
754 getNextRecordID(const uint16_t recordID,
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700755 const std::vector<std::filesystem::path>& selLogFiles)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700756{
757 uint16_t nextRecordID = recordID + 1;
758 std::string entry;
759 if (findSELEntry(nextRecordID, selLogFiles, entry))
760 {
761 return nextRecordID;
762 }
763 else
764 {
765 return ipmi::sel::lastEntry;
766 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800767}
768
Patrick Ventureff7e15b2019-09-25 16:48:26 -0700769static int fromHexStr(const std::string& hexStr, std::vector<uint8_t>& data)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800770{
771 for (unsigned int i = 0; i < hexStr.size(); i += 2)
772 {
773 try
774 {
775 data.push_back(static_cast<uint8_t>(
776 std::stoul(hexStr.substr(i, 2), nullptr, 16)));
777 }
778 catch (std::invalid_argument& e)
779 {
780 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
781 return -1;
782 }
783 catch (std::out_of_range& e)
784 {
785 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
786 return -1;
787 }
788 }
789 return 0;
790}
791
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700792ipmi::RspType<uint8_t, // SEL version
793 uint16_t, // SEL entry count
794 uint16_t, // free space
795 uint32_t, // last add timestamp
796 uint32_t, // last erase timestamp
797 uint8_t> // operation support
798 ipmiStorageGetSELInfo()
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800799{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700800 constexpr uint8_t selVersion = ipmi::sel::selVersion;
801 uint16_t entries = countSELEntries();
802 uint32_t addTimeStamp = intel_oem::ipmi::sel::getFileTimestamp(
803 intel_oem::ipmi::sel::selLogDir / intel_oem::ipmi::sel::selLogFilename);
804 uint32_t eraseTimeStamp = intel_oem::ipmi::sel::erase_time::get();
805 constexpr uint8_t operationSupport =
806 intel_oem::ipmi::sel::selOperationSupport;
807 constexpr uint16_t freeSpace =
808 0xffff; // Spec indicates that more than 64kB is free
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800809
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700810 return ipmi::responseSuccess(selVersion, entries, freeSpace, addTimeStamp,
811 eraseTimeStamp, operationSupport);
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800812}
813
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700814using systemEventType = std::tuple<
815 uint32_t, // Timestamp
816 uint16_t, // Generator ID
817 uint8_t, // EvM Rev
818 uint8_t, // Sensor Type
819 uint8_t, // Sensor Number
820 uint7_t, // Event Type
821 bool, // Event Direction
822 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
823using oemTsEventType = std::tuple<
824 uint32_t, // Timestamp
825 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
826using oemEventType =
827 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800828
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700829ipmi::RspType<uint16_t, // Next Record ID
830 uint16_t, // Record ID
831 uint8_t, // Record Type
832 std::variant<systemEventType, oemTsEventType,
833 oemEventType>> // Record Content
834 ipmiStorageGetSELEntry(uint16_t reservationID, uint16_t targetID,
835 uint8_t offset, uint8_t size)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800836{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700837 // Only support getting the entire SEL record. If a partial size or non-zero
838 // offset is requested, return an error
839 if (offset != 0 || size != ipmi::sel::entireRecord)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800840 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700841 return ipmi::responseRetBytesUnavailable();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800842 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800843
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700844 // Check the reservation ID if one is provided or required (only if the
845 // offset is non-zero)
846 if (reservationID != 0 || offset != 0)
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800847 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700848 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800849 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700850 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800851 }
852 }
853
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700854 // Get the ipmi_sel log files
855 std::vector<std::filesystem::path> selLogFiles;
856 if (!getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800857 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700858 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800859 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800860
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700861 std::string targetEntry;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800862
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800863 if (targetID == ipmi::sel::firstEntry)
864 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700865 // The first entry will be at the top of the oldest log file
866 std::ifstream logStream(selLogFiles.back());
867 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800868 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700869 return ipmi::responseUnspecifiedError();
870 }
871
872 if (!std::getline(logStream, targetEntry))
873 {
874 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800875 }
876 }
877 else if (targetID == ipmi::sel::lastEntry)
878 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700879 // The last entry will be at the bottom of the newest log file
880 std::ifstream logStream(selLogFiles.front());
881 if (!logStream.is_open())
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800882 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700883 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800884 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700885
886 std::string line;
887 while (std::getline(logStream, line))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800888 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700889 targetEntry = line;
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800890 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800891 }
892 else
893 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700894 if (!findSELEntry(targetID, selLogFiles, targetEntry))
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800895 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700896 return ipmi::responseSensorInvalid();
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800897 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -0800898 }
899
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700900 // The format of the ipmi_sel message is "<Timestamp>
901 // <ID>,<Type>,<EventData>,[<Generator ID>,<Path>,<Direction>]".
902 // First get the Timestamp
903 size_t space = targetEntry.find_first_of(" ");
904 if (space == std::string::npos)
905 {
906 return ipmi::responseUnspecifiedError();
907 }
908 std::string entryTimestamp = targetEntry.substr(0, space);
909 // Then get the log contents
910 size_t entryStart = targetEntry.find_first_not_of(" ", space);
911 if (entryStart == std::string::npos)
912 {
913 return ipmi::responseUnspecifiedError();
914 }
915 std::string_view entry(targetEntry);
916 entry.remove_prefix(entryStart);
917 // Use split to separate the entry into its fields
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700918 std::vector<std::string> targetEntryFields;
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700919 boost::split(targetEntryFields, entry, boost::is_any_of(","),
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700920 boost::token_compress_on);
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700921 if (targetEntryFields.size() < 3)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700922 {
923 return ipmi::responseUnspecifiedError();
924 }
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700925 std::string& recordIDStr = targetEntryFields[0];
926 std::string& recordTypeStr = targetEntryFields[1];
927 std::string& eventDataStr = targetEntryFields[2];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700928
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700929 uint16_t recordID;
930 uint8_t recordType;
931 try
932 {
933 recordID = std::stoul(recordIDStr);
934 recordType = std::stoul(recordTypeStr, nullptr, 16);
935 }
936 catch (const std::invalid_argument&)
937 {
938 return ipmi::responseUnspecifiedError();
939 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700940 uint16_t nextRecordID = getNextRecordID(recordID, selLogFiles);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700941 std::vector<uint8_t> eventDataBytes;
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700942 if (fromHexStr(eventDataStr, eventDataBytes) < 0)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700943 {
944 return ipmi::responseUnspecifiedError();
945 }
946
947 if (recordType == intel_oem::ipmi::sel::systemEvent)
948 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700949 // Get the timestamp
950 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -0700951 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700952
953 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
954 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
955 {
956 timestamp = std::mktime(&timeStruct);
957 }
958
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700959 // Set the event message revision
960 uint8_t evmRev = intel_oem::ipmi::sel::eventMsgRev;
961
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700962 uint16_t generatorID = 0;
963 uint8_t sensorType = 0;
964 uint8_t sensorNum = 0xFF;
965 uint7_t eventType = 0;
966 bool eventDir = 0;
967 // System type events should have six fields
968 if (targetEntryFields.size() >= 6)
969 {
970 std::string& generatorIDStr = targetEntryFields[3];
971 std::string& sensorPath = targetEntryFields[4];
972 std::string& eventDirStr = targetEntryFields[5];
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700973
Jason M. Bills1a2fbdd2019-05-10 09:05:37 -0700974 // Get the generator ID
975 try
976 {
977 generatorID = std::stoul(generatorIDStr, nullptr, 16);
978 }
979 catch (const std::invalid_argument&)
980 {
981 std::cerr << "Invalid Generator ID\n";
982 }
983
984 // Get the sensor type, sensor number, and event type for the sensor
985 sensorType = getSensorTypeFromPath(sensorPath);
986 sensorNum = getSensorNumberFromPath(sensorPath);
987 eventType = getSensorEventTypeFromPath(sensorPath);
988
989 // Get the event direction
990 try
991 {
992 eventDir = std::stoul(eventDirStr) ? 0 : 1;
993 }
994 catch (const std::invalid_argument&)
995 {
996 std::cerr << "Invalid Event Direction\n";
997 }
998 }
Jason M. Bills1d4d54d2019-04-23 11:26:11 -0700999
1000 // Only keep the eventData bytes that fit in the record
1001 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
1002 std::copy_n(eventDataBytes.begin(),
1003 std::min(eventDataBytes.size(), eventData.size()),
1004 eventData.begin());
1005
1006 return ipmi::responseSuccess(
1007 nextRecordID, recordID, recordType,
1008 systemEventType{timestamp, generatorID, evmRev, sensorType,
1009 sensorNum, eventType, eventDir, eventData});
1010 }
1011 else if (recordType >= intel_oem::ipmi::sel::oemTsEventFirst &&
1012 recordType <= intel_oem::ipmi::sel::oemTsEventLast)
1013 {
1014 // Get the timestamp
1015 std::tm timeStruct = {};
Jason M. Bills52aaa7d2019-05-08 15:21:39 -07001016 std::istringstream entryStream(entryTimestamp);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001017
1018 uint32_t timestamp = ipmi::sel::invalidTimeStamp;
1019 if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
1020 {
1021 timestamp = std::mktime(&timeStruct);
1022 }
1023
1024 // Only keep the bytes that fit in the record
1025 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize> eventData{};
1026 std::copy_n(eventDataBytes.begin(),
1027 std::min(eventDataBytes.size(), eventData.size()),
1028 eventData.begin());
1029
1030 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1031 oemTsEventType{timestamp, eventData});
1032 }
Patrick Venturec5136aa2019-10-04 20:39:31 -07001033 else if (recordType >= intel_oem::ipmi::sel::oemEventFirst)
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001034 {
1035 // Only keep the bytes that fit in the record
1036 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize> eventData{};
1037 std::copy_n(eventDataBytes.begin(),
1038 std::min(eventDataBytes.size(), eventData.size()),
1039 eventData.begin());
1040
1041 return ipmi::responseSuccess(nextRecordID, recordID, recordType,
1042 eventData);
1043 }
1044
1045 return ipmi::responseUnspecifiedError();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001046}
1047
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001048ipmi::RspType<uint16_t> ipmiStorageAddSELEntry(
1049 uint16_t recordID, uint8_t recordType, uint32_t timestamp,
1050 uint16_t generatorID, uint8_t evmRev, uint8_t sensorType, uint8_t sensorNum,
1051 uint8_t eventType, uint8_t eventData1, uint8_t eventData2,
1052 uint8_t eventData3)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001053{
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001054 // Per the IPMI spec, need to cancel any reservation when a SEL entry is
1055 // added
1056 cancelSELReservation();
1057
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001058 // Send this request to the Redfish hooks to log it as a Redfish message
1059 // instead. There is no need to add it to the SEL, so just return success.
1060 intel_oem::ipmi::sel::checkRedfishHooks(
1061 recordID, recordType, timestamp, generatorID, evmRev, sensorType,
1062 sensorNum, eventType, eventData1, eventData2, eventData3);
Jason M. Bills99b78ec2019-01-18 10:42:18 -08001063
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001064 uint16_t responseID = 0xFFFF;
1065 return ipmi::responseSuccess(responseID);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001066}
1067
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001068ipmi::RspType<uint8_t> ipmiStorageClearSEL(ipmi::Context::ptr ctx,
1069 uint16_t reservationID,
1070 const std::array<uint8_t, 3>& clr,
1071 uint8_t eraseOperation)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001072{
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001073 if (!checkSELReservation(reservationID))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001074 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001075 return ipmi::responseInvalidReservationId();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001076 }
1077
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001078 static constexpr std::array<uint8_t, 3> clrExpected = {'C', 'L', 'R'};
1079 if (clr != clrExpected)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001080 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001081 return ipmi::responseInvalidFieldRequest();
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001082 }
1083
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001084 // Erasure status cannot be fetched, so always return erasure status as
1085 // `erase completed`.
1086 if (eraseOperation == ipmi::sel::getEraseStatus)
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001087 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001088 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001089 }
1090
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001091 // Check that initiate erase is correct
1092 if (eraseOperation != ipmi::sel::initiateErase)
1093 {
1094 return ipmi::responseInvalidFieldRequest();
1095 }
1096
1097 // Per the IPMI spec, need to cancel any reservation when the SEL is
1098 // cleared
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001099 cancelSELReservation();
1100
Jason M. Bills7944c302019-03-20 15:24:05 -07001101 // Save the erase time
1102 intel_oem::ipmi::sel::erase_time::save();
1103
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001104 // Clear the SEL by deleting the log files
1105 std::vector<std::filesystem::path> selLogFiles;
1106 if (getSELLogFiles(selLogFiles))
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001107 {
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001108 for (const std::filesystem::path& file : selLogFiles)
1109 {
1110 std::error_code ec;
1111 std::filesystem::remove(file, ec);
1112 }
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001113 }
1114
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001115 // Reload rsyslog so it knows to start new log files
Vernon Mauery15419dd2019-05-24 09:40:30 -07001116 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
1117 sdbusplus::message::message rsyslogReload = dbus->new_method_call(
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001118 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1119 "org.freedesktop.systemd1.Manager", "ReloadUnit");
1120 rsyslogReload.append("rsyslog.service", "replace");
1121 try
1122 {
Vernon Mauery15419dd2019-05-24 09:40:30 -07001123 sdbusplus::message::message reloadResponse = dbus->call(rsyslogReload);
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001124 }
1125 catch (sdbusplus::exception_t& e)
1126 {
1127 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1128 }
1129
1130 return ipmi::responseSuccess(ipmi::sel::eraseComplete);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001131}
1132
Jason M. Bills1a474622019-06-14 14:51:33 -07001133ipmi::RspType<uint32_t> ipmiStorageGetSELTime()
1134{
1135 struct timespec selTime = {};
1136
1137 if (clock_gettime(CLOCK_REALTIME, &selTime) < 0)
1138 {
1139 return ipmi::responseUnspecifiedError();
1140 }
1141
1142 return ipmi::responseSuccess(selTime.tv_sec);
1143}
1144
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001145ipmi::RspType<> ipmiStorageSetSELTime(uint32_t selTime)
Jason M. Billscac97a52019-01-30 14:43:46 -08001146{
1147 // Set SEL Time is not supported
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001148 return ipmi::responseInvalidCommand();
Jason M. Billscac97a52019-01-30 14:43:46 -08001149}
1150
James Feist74c50c62019-08-14 14:18:41 -07001151std::vector<uint8_t> getType12SDRs(uint16_t index, uint16_t recordId)
1152{
1153 std::vector<uint8_t> resp;
1154 if (index == 0)
1155 {
1156 Type12Record bmc = {};
1157 bmc.header.record_id_lsb = recordId;
1158 bmc.header.record_id_msb = recordId >> 8;
1159 bmc.header.sdr_version = ipmiSdrVersion;
1160 bmc.header.record_type = 0x12;
1161 bmc.header.record_length = 0x1b;
1162 bmc.slaveAddress = 0x20;
1163 bmc.channelNumber = 0;
1164 bmc.powerStateNotification = 0;
1165 bmc.deviceCapabilities = 0xBF;
1166 bmc.reserved = 0;
1167 bmc.entityID = 0x2E;
1168 bmc.entityInstance = 1;
1169 bmc.oem = 0;
1170 bmc.typeLengthCode = 0xD0;
1171 std::string bmcName = "Basbrd Mgmt Ctlr";
1172 std::copy(bmcName.begin(), bmcName.end(), bmc.name);
1173 uint8_t* bmcPtr = reinterpret_cast<uint8_t*>(&bmc);
1174 resp.insert(resp.end(), bmcPtr, bmcPtr + sizeof(Type12Record));
1175 }
1176 else if (index == 1)
1177 {
1178 Type12Record me = {};
1179 me.header.record_id_lsb = recordId;
1180 me.header.record_id_msb = recordId >> 8;
1181 me.header.sdr_version = ipmiSdrVersion;
1182 me.header.record_type = 0x12;
1183 me.header.record_length = 0x16;
1184 me.slaveAddress = 0x2C;
1185 me.channelNumber = 6;
1186 me.powerStateNotification = 0x24;
1187 me.deviceCapabilities = 0x21;
1188 me.reserved = 0;
1189 me.entityID = 0x2E;
1190 me.entityInstance = 2;
1191 me.oem = 0;
1192 me.typeLengthCode = 0xCB;
1193 std::string meName = "Mgmt Engine";
1194 std::copy(meName.begin(), meName.end(), me.name);
1195 uint8_t* mePtr = reinterpret_cast<uint8_t*>(&me);
1196 resp.insert(resp.end(), mePtr, mePtr + sizeof(Type12Record));
1197 }
1198 else
1199 {
1200 throw std::runtime_error("getType12SDRs:: Illegal index " +
1201 std::to_string(index));
1202 }
1203
1204 return resp;
1205}
1206
Yong Lifee5e4c2020-01-17 19:36:29 +08001207std::vector<uint8_t> getNMDiscoverySDR(uint16_t index, uint16_t recordId)
1208{
1209 std::vector<uint8_t> resp;
1210 if (index == 0)
1211 {
1212 NMDiscoveryRecord nm = {};
1213 nm.header.record_id_lsb = recordId;
1214 nm.header.record_id_msb = recordId >> 8;
1215 nm.header.sdr_version = ipmiSdrVersion;
1216 nm.header.record_type = 0xC0;
1217 nm.header.record_length = 0xB;
1218 nm.oemID0 = 0x57;
1219 nm.oemID1 = 0x1;
1220 nm.oemID2 = 0x0;
1221 nm.subType = 0x0D;
1222 nm.version = 0x1;
1223 nm.slaveAddress = 0x2C;
1224 nm.channelNumber = 0x60;
1225 nm.healthEventSensor = 0x19;
1226 nm.exceptionEventSensor = 0x18;
1227 nm.operationalCapSensor = 0x1A;
1228 nm.thresholdExceededSensor = 0x1B;
1229
1230 uint8_t* nmPtr = reinterpret_cast<uint8_t*>(&nm);
1231 resp.insert(resp.end(), nmPtr, nmPtr + sizeof(NMDiscoveryRecord));
1232 }
1233 else
1234 {
1235 throw std::runtime_error("getNMDiscoverySDR:: Illegal index " +
1236 std::to_string(index));
1237 }
1238
1239 return resp;
1240}
1241
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001242void registerStorageFunctions()
1243{
James Feist25690252019-12-23 12:25:49 -08001244 createTimers();
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001245 // <Get FRU Inventory Area Info>
jayaprakash Mutyalad33acd62019-05-17 19:37:25 +00001246 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1247 ipmi::storage::cmdGetFruInventoryAreaInfo,
1248 ipmi::Privilege::User, ipmiStorageGetFruInvAreaInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001249 // <READ FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001250 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1251 ipmi::storage::cmdReadFruData, ipmi::Privilege::User,
1252 ipmiStorageReadFruData);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001253
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001254 // <WRITE FRU Data>
jayaprakash Mutyala5f4194e2019-05-20 16:17:01 +00001255 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1256 ipmi::storage::cmdWriteFruData,
1257 ipmi::Privilege::Operator, ipmiStorageWriteFruData);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001258
1259 // <Get SEL Info>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001260 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001261 ipmi::storage::cmdGetSelInfo, ipmi::Privilege::User,
1262 ipmiStorageGetSELInfo);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001263
1264 // <Get SEL Entry>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001265 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001266 ipmi::storage::cmdGetSelEntry, ipmi::Privilege::User,
1267 ipmiStorageGetSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001268
1269 // <Add SEL Entry>
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001270 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001271 ipmi::storage::cmdAddSelEntry,
Jason M. Bills6dd8f042019-04-11 10:39:02 -07001272 ipmi::Privilege::Operator, ipmiStorageAddSELEntry);
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001273
1274 // <Clear SEL>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001275 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1276 ipmi::storage::cmdClearSel, ipmi::Privilege::Operator,
1277 ipmiStorageClearSEL);
Jason M. Billscac97a52019-01-30 14:43:46 -08001278
Jason M. Bills1a474622019-06-14 14:51:33 -07001279 // <Get SEL Time>
1280 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
Jason M. Bills542498e2019-06-24 16:57:42 -07001281 ipmi::storage::cmdGetSelTime, ipmi::Privilege::User,
1282 ipmiStorageGetSELTime);
Jason M. Bills1a474622019-06-14 14:51:33 -07001283
Jason M. Billscac97a52019-01-30 14:43:46 -08001284 // <Set SEL Time>
Jason M. Bills1d4d54d2019-04-23 11:26:11 -07001285 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
1286 ipmi::storage::cmdSetSelTime,
1287 ipmi::Privilege::Operator, ipmiStorageSetSELTime);
Jason M. Billse2d1aee2018-10-03 15:57:18 -07001288}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001289} // namespace storage
Jason M. Billsc04e2e72018-11-28 15:15:56 -08001290} // namespace ipmi