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