1 #include "file_io_type_progress_src.hpp"
2 
3 #include "common/utils.hpp"
4 
5 #include <iostream>
6 
7 namespace pldm
8 {
9 
10 namespace responder
11 {
12 
13 int ProgressCodeHandler::setRawBootProperty(
14     const std::tuple<uint64_t, std::vector<uint8_t>>& progressCodeBuffer)
15 {
16     static constexpr auto RawObjectPath =
17         "/xyz/openbmc_project/state/boot/raw0";
18     static constexpr auto RawInterface = "xyz.openbmc_project.State.Boot.Raw";
19     static constexpr auto FreedesktopInterface =
20         "org.freedesktop.DBus.Properties";
21     static constexpr auto RawProperty = "Value";
22     static constexpr auto SetMethod = "Set";
23 
24     auto& bus = pldm::utils::DBusHandler::getBus();
25 
26     try
27     {
28         auto service =
29             pldm::utils::DBusHandler().getService(RawObjectPath, RawInterface);
30         auto method = bus.new_method_call(service.c_str(), RawObjectPath,
31                                           FreedesktopInterface, SetMethod);
32         method.append(RawInterface, RawProperty,
33                       std::variant<std::tuple<uint64_t, std::vector<uint8_t>>>(
34                           progressCodeBuffer));
35 
36         bus.call_noreply(method);
37     }
38     catch (const std::exception& e)
39     {
40         std::cerr << "failed to make a d-bus call to host-postd daemon, ERROR="
41                   << e.what() << "\n";
42         return PLDM_ERROR;
43     }
44 
45     return PLDM_SUCCESS;
46 }
47 
48 int ProgressCodeHandler::write(const char* buffer, uint32_t /*offset*/,
49                                uint32_t& length,
50                                oem_platform::Handler* /*oemPlatformHandler*/)
51 {
52     static constexpr auto StartOffset = 40;
53     static constexpr auto EndOffset = 48;
54     if (buffer != nullptr)
55     {
56         // read the data from the pointed location
57         std::vector<uint8_t> secondaryCode(buffer, buffer + length);
58 
59         // Get the primary code from the offset 40 bytes in the received buffer
60 
61         std::vector<uint8_t> primaryCodeArray(
62             secondaryCode.begin() + StartOffset,
63             secondaryCode.begin() + EndOffset);
64         uint64_t primaryCode = 0;
65 
66         // form a uint64_t using uint8_t[8]
67         for (int i = 0; i < 8; i++)
68             primaryCode |= (uint64_t)primaryCodeArray[i] << 8 * i;
69 
70         std::cout << "Primary Progress Code in Hex : " << std::hex
71                   << primaryCode << std::endl;
72 
73         return setRawBootProperty(std::make_tuple(primaryCode, secondaryCode));
74     }
75     return PLDM_ERROR;
76 }
77 
78 } // namespace responder
79 } // namespace pldm
80