blob: 236073312e8633df2816512ec812f91b4aba09c1 [file] [log] [blame]
Sampa Misra18967162020-01-14 02:31:41 -06001#include "file_io_type_dump.hpp"
2
3#include "utils.hpp"
4#include "xyz/openbmc_project/Common/error.hpp"
5
6#include <stdint.h>
7#include <systemd/sd-bus.h>
8#include <unistd.h>
9
10#include <exception>
11#include <filesystem>
12#include <iostream>
13#include <sdbusplus/server.hpp>
14#include <xyz/openbmc_project/Dump/NewDump/server.hpp>
15
16#include "libpldm/base.h"
17#include "oem/ibm/libpldm/file_io.h"
18
19namespace pldm
20{
21namespace responder
22{
23
24static constexpr auto nbdInterface = "/dev/nbd1";
25
26int DumpHandler::fd = -1;
27
28int DumpHandler::newFileAvailable(uint64_t length)
29{
30 static constexpr auto dumpObjPath = "/xyz/openbmc_project/dump";
31 static constexpr auto dumpInterface = "xyz.openbmc_project.Dump.NewDump";
32
33 auto& bus = pldm::utils::DBusHandler::getBus();
34
35 try
36 {
37 auto service =
38 pldm::utils::DBusHandler().getService(dumpObjPath, dumpInterface);
39 using namespace sdbusplus::xyz::openbmc_project::Dump::server;
40 auto method = bus.new_method_call(service.c_str(), dumpObjPath,
41 dumpInterface, "Notify");
42 method.append(
43 sdbusplus::xyz::openbmc_project::Dump::server::convertForMessage(
44 NewDump::DumpType::System),
45 fileHandle, length);
46 bus.call_noreply(method);
47 }
48 catch (const std::exception& e)
49 {
50 std::cerr << "failed to make a d-bus call to DUMP manager, ERROR="
51 << e.what() << "\n";
52 return PLDM_ERROR;
53 }
54
55 return PLDM_SUCCESS;
56}
57
58int DumpHandler::writeFromMemory(uint32_t offset, uint32_t length,
59 uint64_t address)
60{
61 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE;
62
63 if (DumpHandler::fd == -1)
64 {
65 DumpHandler::fd = open(nbdInterface, flags);
66 if (DumpHandler::fd == -1)
67 {
68 std::cerr << "NBD file does not exist at " << nbdInterface
69 << " ERROR=" << errno << "\n";
70 return PLDM_ERROR;
71 }
72 }
73 return transferFileData(DumpHandler::fd, false, offset, length, address);
74}
75
76int DumpHandler::write(const char* buffer, uint32_t offset, uint32_t& length)
77{
78 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE;
79 if (DumpHandler::fd == -1)
80 {
81 DumpHandler::fd = open(nbdInterface, flags);
82 if (DumpHandler::fd == -1)
83 {
84 std::cerr << "NBD file does not exist at " << nbdInterface
85 << " ERROR=" << errno << "\n";
86 return PLDM_ERROR;
87 }
88 }
89
90 int rc = lseek(DumpHandler::fd, offset, SEEK_SET);
91 if (rc == -1)
92 {
93 std::cerr << "lseek failed, ERROR=" << errno << ", OFFSET=" << offset
94 << "\n";
95 return PLDM_ERROR;
96 }
97 rc = ::write(DumpHandler::fd, buffer, length);
98 if (rc == -1)
99 {
100 std::cerr << "file write failed, ERROR=" << errno
101 << ", LENGTH=" << length << ", OFFSET=" << offset << "\n";
102 return PLDM_ERROR;
103 }
104 length = rc;
105
106 return PLDM_SUCCESS;
107}
108
109} // namespace responder
110} // namespace pldm