Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 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 | |
| 17 | #pragma once |
Lei YU | b07851c | 2025-03-13 02:36:19 +0000 | [diff] [blame] | 18 | #include <systemd/sd-journal.h> |
| 19 | |
Lei YU | 9916d41 | 2025-02-06 11:51:18 +0000 | [diff] [blame] | 20 | #include <sdbusplus/asio/connection.hpp> |
| 21 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 22 | #include <filesystem> |
Lei YU | 9916d41 | 2025-02-06 11:51:18 +0000 | [diff] [blame] | 23 | #include <string> |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 24 | |
Patrick Williams | 69f7fa3 | 2023-05-10 07:50:41 -0500 | [diff] [blame] | 25 | static constexpr const char* ipmiSelObject = "xyz.openbmc_project.Logging.IPMI"; |
| 26 | static constexpr const char* ipmiSelPath = "/xyz/openbmc_project/Logging/IPMI"; |
| 27 | static constexpr const char* ipmiSelAddInterface = |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 28 | "xyz.openbmc_project.Logging.IPMI"; |
| 29 | |
| 30 | // ID string generated using journalctl to include in the MESSAGE_ID field for |
| 31 | // SEL entries. Helps with filtering SEL entries in the journal. |
Patrick Williams | 69f7fa3 | 2023-05-10 07:50:41 -0500 | [diff] [blame] | 32 | static constexpr const char* selMessageId = "b370836ccf2f4850ac5bee185b77893a"; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 33 | static constexpr int selPriority = 5; // notice |
| 34 | static constexpr uint8_t selSystemType = 0x02; |
| 35 | static constexpr uint16_t selBMCGenID = 0x0020; |
| 36 | static constexpr uint16_t selInvalidRecID = |
| 37 | std::numeric_limits<uint16_t>::max(); |
| 38 | static constexpr size_t selEvtDataMaxSize = 3; |
| 39 | static constexpr size_t selOemDataMaxSize = 13; |
Zhikui Ren | 25b26e1 | 2020-06-26 20:18:19 -0700 | [diff] [blame] | 40 | static constexpr uint8_t selEvtDataUnspecified = 0xFF; |
Jason M. Bills | 5e049d3 | 2018-10-19 12:59:38 -0700 | [diff] [blame] | 41 | |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 42 | static const std::filesystem::path selLogDir = "/var/log"; |
| 43 | static const std::string selLogFilename = "ipmi_sel"; |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 44 | #ifdef SEL_LOGGER_ENABLE_SEL_DELETE |
| 45 | static const std::string nextRecordFilename = "next_records"; |
Lei YU | b07851c | 2025-03-13 02:36:19 +0000 | [diff] [blame] | 46 | uint16_t getNewRecordId(); |
Lei YU | 3abf2b0 | 2025-03-21 05:51:37 +0000 | [diff] [blame] | 47 | #else |
| 48 | unsigned int getNewRecordId(); |
Jonico Eustaquio | 9fa224c | 2024-01-10 13:08:52 -0600 | [diff] [blame] | 49 | #endif |
Jason M. Bills | c4a336f | 2019-04-23 10:43:10 -0700 | [diff] [blame] | 50 | |
Lei YU | 9916d41 | 2025-02-06 11:51:18 +0000 | [diff] [blame] | 51 | void toHexStr(const std::vector<uint8_t>& data, std::string& hexStr); |
| 52 | |
Jason M. Bills | 97be353 | 2018-11-02 13:09:16 -0700 | [diff] [blame] | 53 | template <typename... T> |
Lei YU | 9916d41 | 2025-02-06 11:51:18 +0000 | [diff] [blame] | 54 | uint16_t selAddSystemRecord( |
| 55 | [[maybe_unused]] std::shared_ptr<sdbusplus::asio::connection> conn, |
| 56 | [[maybe_unused]] const std::string& message, const std::string& path, |
Patrick Williams | a58ea68 | 2025-02-01 08:22:21 -0500 | [diff] [blame] | 57 | const std::vector<uint8_t>& selData, const bool& assert, |
Lei YU | 9916d41 | 2025-02-06 11:51:18 +0000 | [diff] [blame] | 58 | const uint16_t& genId, [[maybe_unused]] T&&... metadata) |
| 59 | { |
| 60 | // Only 3 bytes of SEL event data are allowed in a system record |
| 61 | if (selData.size() > selEvtDataMaxSize) |
| 62 | { |
| 63 | throw std::invalid_argument("Event data too large"); |
| 64 | } |
| 65 | std::string selDataStr; |
| 66 | toHexStr(selData, selDataStr); |
| 67 | |
| 68 | #ifdef SEL_LOGGER_SEND_TO_LOGGING_SERVICE |
| 69 | sdbusplus::message_t AddToLog = conn->new_method_call( |
| 70 | "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging", |
| 71 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 72 | |
| 73 | std::string journalMsg( |
| 74 | message + " from " + path + ": " + |
| 75 | " RecordType=" + std::to_string(selSystemType) + |
| 76 | ", GeneratorID=" + std::to_string(genId) + |
| 77 | ", EventDir=" + std::to_string(assert) + ", EventData=" + selDataStr); |
| 78 | |
| 79 | AddToLog.append( |
| 80 | journalMsg, "xyz.openbmc_project.Logging.Entry.Level.Informational", |
| 81 | std::map<std::string, std::string>( |
| 82 | {{"SENSOR_PATH", path}, |
| 83 | {"GENERATOR_ID", std::to_string(genId)}, |
| 84 | {"RECORD_TYPE", std::to_string(selSystemType)}, |
| 85 | {"EVENT_DIR", std::to_string(assert)}, |
| 86 | {"SENSOR_DATA", selDataStr}})); |
| 87 | conn->call(AddToLog); |
| 88 | return 0; |
| 89 | #else |
| 90 | unsigned int recordId = getNewRecordId(); |
| 91 | if (recordId < selInvalidRecID) |
| 92 | { |
| 93 | sd_journal_send( |
| 94 | "MESSAGE=%s", message.c_str(), "PRIORITY=%i", selPriority, |
| 95 | "MESSAGE_ID=%s", selMessageId, "IPMI_SEL_RECORD_ID=%d", recordId, |
| 96 | "IPMI_SEL_RECORD_TYPE=%x", selSystemType, |
| 97 | "IPMI_SEL_GENERATOR_ID=%x", genId, "IPMI_SEL_SENSOR_PATH=%s", |
| 98 | path.c_str(), "IPMI_SEL_EVENT_DIR=%x", assert, "IPMI_SEL_DATA=%s", |
| 99 | selDataStr.c_str(), std::forward<T>(metadata)..., NULL); |
| 100 | } |
| 101 | return recordId; |
| 102 | #endif |
| 103 | } |