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