Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 1 | #include "file_io_type_pel.hpp" |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/utils.hpp" |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 6 | #include <libpldm/base.h> |
| 7 | #include <libpldm/file_io.h> |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | #include <systemd/sd-bus.h> |
| 10 | #include <unistd.h> |
| 11 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 13 | #include <sdbusplus/server.hpp> |
| 14 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
| 15 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 16 | #include <exception> |
| 17 | #include <filesystem> |
Matt Spinler | 39d8c7a | 2020-03-05 11:28:59 -0600 | [diff] [blame] | 18 | #include <fstream> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 19 | #include <iostream> |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 20 | #include <vector> |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 21 | |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 22 | PHOSPHOR_LOG2_USING; |
| 23 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 24 | namespace pldm |
| 25 | { |
| 26 | namespace responder |
| 27 | { |
Matt Spinler | 39d8c7a | 2020-03-05 11:28:59 -0600 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 29 | |
| 30 | namespace detail |
| 31 | { |
Matt Spinler | 39d8c7a | 2020-03-05 11:28:59 -0600 | [diff] [blame] | 32 | /** |
| 33 | * @brief Finds the Entry::Level value for the severity of the PEL |
| 34 | * passed in. |
| 35 | * |
| 36 | * The severity byte is at offset 10 in the User Header section, |
| 37 | * which is always after the 48 byte Private Header section. |
| 38 | * |
| 39 | * @param[in] pelFileName - The file containing the PEL |
| 40 | * |
| 41 | * @return Entry::Level - The severity value for the Entry |
| 42 | */ |
| 43 | Entry::Level getEntryLevelFromPEL(const std::string& pelFileName) |
| 44 | { |
| 45 | const std::map<uint8_t, Entry::Level> severityMap{ |
| 46 | {0x00, Entry::Level::Informational}, // Informational event |
| 47 | {0x10, Entry::Level::Warning}, // Recoverable error |
| 48 | {0x20, Entry::Level::Warning}, // Predictive error |
| 49 | {0x40, Entry::Level::Error}, // Unrecoverable error |
| 50 | {0x50, Entry::Level::Error}, // Critical error |
| 51 | {0x60, Entry::Level::Error}, // Error from a diagnostic test |
| 52 | {0x70, Entry::Level::Warning} // Recoverable symptom |
| 53 | }; |
| 54 | |
| 55 | const size_t severityOffset = 0x3A; |
| 56 | |
| 57 | size_t size = 0; |
| 58 | if (fs::exists(pelFileName)) |
| 59 | { |
| 60 | size = fs::file_size(pelFileName); |
| 61 | } |
| 62 | |
| 63 | if (size > severityOffset) |
| 64 | { |
| 65 | std::ifstream pel{pelFileName}; |
| 66 | if (pel.good()) |
| 67 | { |
| 68 | pel.seekg(severityOffset); |
| 69 | |
| 70 | uint8_t sev; |
| 71 | pel.read(reinterpret_cast<char*>(&sev), 1); |
| 72 | |
| 73 | // Get the type |
| 74 | sev = sev & 0xF0; |
| 75 | |
| 76 | auto entry = severityMap.find(sev); |
| 77 | if (entry != severityMap.end()) |
| 78 | { |
| 79 | return entry->second; |
| 80 | } |
| 81 | } |
| 82 | else |
| 83 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 84 | error("Unable to open PEL file {PEL_FILE_NAME}", "PEL_FILE_NAME", |
| 85 | pelFileName); |
Matt Spinler | 39d8c7a | 2020-03-05 11:28:59 -0600 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | return Entry::Level::Error; |
| 90 | } |
| 91 | } // namespace detail |
| 92 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 93 | int PelHandler::readIntoMemory(uint32_t offset, uint32_t& length, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 94 | uint64_t address, |
| 95 | oem_platform::Handler* /*oemPlatformHandler*/) |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 96 | { |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 97 | static constexpr auto logObjPath = "/xyz/openbmc_project/logging"; |
| 98 | static constexpr auto logInterface = "org.open_power.Logging.PEL"; |
| 99 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 100 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 101 | |
| 102 | try |
| 103 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 104 | auto service = pldm::utils::DBusHandler().getService(logObjPath, |
| 105 | logInterface); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 106 | auto method = bus.new_method_call(service.c_str(), logObjPath, |
| 107 | logInterface, "GetPEL"); |
| 108 | method.append(fileHandle); |
vkaverap@in.ibm.com | 9138c20 | 2023-05-19 07:50:47 -0500 | [diff] [blame] | 109 | auto reply = bus.call( |
| 110 | method, |
| 111 | std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count()); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 112 | sdbusplus::message::unix_fd fd{}; |
| 113 | reply.read(fd); |
| 114 | auto rc = transferFileData(fd, true, offset, length, address); |
| 115 | return rc; |
| 116 | } |
| 117 | catch (const std::exception& e) |
| 118 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 119 | error( |
| 120 | "GetPEL D-Bus call failed, PEL id = 0x{FILE_HANDLE}, error ={ERR_EXCEP}", |
| 121 | "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what()); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 122 | return PLDM_ERROR; |
| 123 | } |
| 124 | |
| 125 | return PLDM_SUCCESS; |
Deepak Kodihalli | f6d3a83 | 2019-11-19 07:00:29 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 128 | int PelHandler::read(uint32_t offset, uint32_t& length, Response& response, |
| 129 | oem_platform::Handler* /*oemPlatformHandler*/) |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 130 | { |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 131 | static constexpr auto logObjPath = "/xyz/openbmc_project/logging"; |
| 132 | static constexpr auto logInterface = "org.open_power.Logging.PEL"; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 133 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 134 | |
| 135 | try |
| 136 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 137 | auto service = pldm::utils::DBusHandler().getService(logObjPath, |
| 138 | logInterface); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 139 | auto method = bus.new_method_call(service.c_str(), logObjPath, |
| 140 | logInterface, "GetPEL"); |
| 141 | method.append(fileHandle); |
vkaverap@in.ibm.com | 9138c20 | 2023-05-19 07:50:47 -0500 | [diff] [blame] | 142 | auto reply = bus.call( |
| 143 | method, |
| 144 | std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count()); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 145 | sdbusplus::message::unix_fd fd{}; |
| 146 | reply.read(fd); |
Pavithra | ba0738b | 2020-01-29 08:47:51 -0600 | [diff] [blame] | 147 | |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 148 | off_t fileSize = lseek(fd, 0, SEEK_END); |
| 149 | if (fileSize == -1) |
| 150 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 151 | error("file seek failed"); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 152 | return PLDM_ERROR; |
| 153 | } |
| 154 | if (offset >= fileSize) |
| 155 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 156 | error( |
| 157 | "Offset exceeds file size, OFFSET={OFFSET} FILE_SIZE={FILE_SIZE}", |
| 158 | "OFFSET", offset, "FILE_SIZE", fileSize); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 159 | return PLDM_DATA_OUT_OF_RANGE; |
| 160 | } |
| 161 | if (offset + length > fileSize) |
| 162 | { |
| 163 | length = fileSize - offset; |
| 164 | } |
| 165 | auto rc = lseek(fd, offset, SEEK_SET); |
| 166 | if (rc == -1) |
| 167 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 168 | error("file seek failed"); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 169 | return PLDM_ERROR; |
| 170 | } |
| 171 | size_t currSize = response.size(); |
| 172 | response.resize(currSize + length); |
| 173 | auto filePos = reinterpret_cast<char*>(response.data()); |
| 174 | filePos += currSize; |
| 175 | rc = ::read(fd, filePos, length); |
| 176 | if (rc == -1) |
| 177 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 178 | error("file read failed"); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 179 | return PLDM_ERROR; |
| 180 | } |
| 181 | if (rc != length) |
| 182 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 183 | error( |
| 184 | "mismatch between number of characters to read and the length read, LENGTH={LEN} COUNT={CNT}", |
| 185 | "LEN", length, "CNT", rc); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 186 | return PLDM_ERROR; |
| 187 | } |
| 188 | } |
| 189 | catch (const std::exception& e) |
| 190 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 191 | error( |
| 192 | "GetPEL D-Bus call failed on PEL ID 0x{FILE_HANDLE}, error ={ERR_EXCEP}", |
| 193 | "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what()); |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 194 | return PLDM_ERROR; |
| 195 | } |
| 196 | return PLDM_SUCCESS; |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 197 | } |
| 198 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 199 | int PelHandler::writeFromMemory(uint32_t offset, uint32_t length, |
Sampa Misra | 6950850 | 2020-09-08 00:08:21 -0500 | [diff] [blame] | 200 | uint64_t address, |
| 201 | oem_platform::Handler* /*oemPlatformHandler*/) |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 202 | { |
Sampa Misra | f5087a4 | 2019-12-03 04:51:36 -0600 | [diff] [blame] | 203 | char tmpFile[] = "/tmp/pel.XXXXXX"; |
| 204 | int fd = mkstemp(tmpFile); |
| 205 | if (fd == -1) |
| 206 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 207 | error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno); |
Sampa Misra | f5087a4 | 2019-12-03 04:51:36 -0600 | [diff] [blame] | 208 | return PLDM_ERROR; |
| 209 | } |
| 210 | close(fd); |
| 211 | fs::path path(tmpFile); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 212 | |
| 213 | auto rc = transferFileData(path, false, offset, length, address); |
| 214 | if (rc == PLDM_SUCCESS) |
| 215 | { |
| 216 | rc = storePel(path.string()); |
| 217 | } |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 218 | return rc; |
| 219 | } |
| 220 | |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 221 | int PelHandler::fileAck(uint8_t /*fileStatus*/) |
| 222 | { |
| 223 | static constexpr auto logObjPath = "/xyz/openbmc_project/logging"; |
| 224 | static constexpr auto logInterface = "org.open_power.Logging.PEL"; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 225 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 226 | |
| 227 | try |
| 228 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 229 | auto service = pldm::utils::DBusHandler().getService(logObjPath, |
| 230 | logInterface); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 231 | auto method = bus.new_method_call(service.c_str(), logObjPath, |
| 232 | logInterface, "HostAck"); |
| 233 | method.append(fileHandle); |
| 234 | bus.call_noreply(method); |
| 235 | } |
| 236 | catch (const std::exception& e) |
| 237 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 238 | error( |
| 239 | "HostAck D-Bus call failed on PEL ID 0x{FILE_HANDLE}, error ={ERR_EXCEP}", |
| 240 | "FILE_HANDLE", lg2::hex, fileHandle, "ERR_EXCEP", e.what()); |
Deepak Kodihalli | 2da1bfe | 2019-12-14 08:28:09 -0600 | [diff] [blame] | 241 | return PLDM_ERROR; |
| 242 | } |
| 243 | |
| 244 | return PLDM_SUCCESS; |
| 245 | } |
| 246 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 247 | int PelHandler::storePel(std::string&& pelFileName) |
| 248 | { |
| 249 | static constexpr auto logObjPath = "/xyz/openbmc_project/logging"; |
| 250 | static constexpr auto logInterface = "xyz.openbmc_project.Logging.Create"; |
| 251 | |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 252 | auto& bus = pldm::utils::DBusHandler::getBus(); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 253 | |
| 254 | try |
| 255 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 256 | auto service = pldm::utils::DBusHandler().getService(logObjPath, |
| 257 | logInterface); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 258 | using namespace sdbusplus::xyz::openbmc_project::Logging::server; |
| 259 | std::map<std::string, std::string> addlData{}; |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 260 | auto severity = |
| 261 | sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage( |
Matt Spinler | 39d8c7a | 2020-03-05 11:28:59 -0600 | [diff] [blame] | 262 | detail::getEntryLevelFromPEL(pelFileName)); |
| 263 | addlData.emplace("RAWPEL", std::move(pelFileName)); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 264 | |
| 265 | auto method = bus.new_method_call(service.c_str(), logObjPath, |
| 266 | logInterface, "Create"); |
| 267 | method.append("xyz.openbmc_project.Host.Error.Event", severity, |
| 268 | addlData); |
| 269 | bus.call_noreply(method); |
| 270 | } |
| 271 | catch (const std::exception& e) |
| 272 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 273 | error("failed to make a d-bus call to PEL daemon, ERROR={ERR_EXCEP}", |
| 274 | "ERR_EXCEP", e.what()); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 275 | return PLDM_ERROR; |
| 276 | } |
| 277 | |
| 278 | return PLDM_SUCCESS; |
| 279 | } |
| 280 | |
George Liu | d37b495 | 2021-06-28 15:14:29 +0800 | [diff] [blame] | 281 | int PelHandler::write(const char* buffer, uint32_t offset, uint32_t& length, |
| 282 | oem_platform::Handler* /*oemPlatformHandler*/) |
| 283 | { |
| 284 | int rc = PLDM_SUCCESS; |
| 285 | |
| 286 | if (offset > 0) |
| 287 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 288 | error("Offset is non zero"); |
George Liu | d37b495 | 2021-06-28 15:14:29 +0800 | [diff] [blame] | 289 | return PLDM_ERROR; |
| 290 | } |
| 291 | |
| 292 | char tmpFile[] = "/tmp/pel.XXXXXX"; |
| 293 | auto fd = mkstemp(tmpFile); |
| 294 | if (fd == -1) |
| 295 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 296 | error("failed to create a temporary pel, ERROR={ERR}", "ERR", errno); |
George Liu | d37b495 | 2021-06-28 15:14:29 +0800 | [diff] [blame] | 297 | return PLDM_ERROR; |
| 298 | } |
| 299 | |
| 300 | size_t written = 0; |
| 301 | do |
| 302 | { |
| 303 | if ((rc = ::write(fd, buffer, length - written)) == -1) |
| 304 | { |
| 305 | break; |
| 306 | } |
| 307 | written += rc; |
| 308 | buffer += rc; |
| 309 | } while (rc && written < length); |
| 310 | close(fd); |
| 311 | |
| 312 | if (rc == -1) |
| 313 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 314 | error("file write failed, ERROR={ERR}, LENGTH={LEN}, OFFSET={OFFSET}", |
| 315 | "ERR", errno, "LEN", length, "OFFSET", offset); |
George Liu | d37b495 | 2021-06-28 15:14:29 +0800 | [diff] [blame] | 316 | fs::remove(tmpFile); |
| 317 | return PLDM_ERROR; |
| 318 | } |
| 319 | |
| 320 | if (written == length) |
| 321 | { |
| 322 | fs::path path(tmpFile); |
| 323 | rc = storePel(path.string()); |
| 324 | if (rc != PLDM_SUCCESS) |
| 325 | { |
Riya Dixit | 49cfb13 | 2023-03-02 04:26:53 -0600 | [diff] [blame] | 326 | error("save PEL failed, ERROR = {RC} tmpFile = {TMP_FILE}", "RC", |
| 327 | rc, "TMP_FILE", tmpFile); |
George Liu | d37b495 | 2021-06-28 15:14:29 +0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | return rc; |
| 332 | } |
| 333 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 334 | } // namespace responder |
| 335 | } // namespace pldm |