1 #include "platform_oem_ibm.hpp"
2 
3 #include "common/utils.hpp"
4 #include "libpldmresponder/pdr.hpp"
5 
6 #include <libpldm/oem/ibm/platform.h>
7 
8 #include <phosphor-logging/lg2.hpp>
9 #include <xyz/openbmc_project/Common/error.hpp>
10 #include <xyz/openbmc_project/State/Boot/Progress/client.hpp>
11 
12 PHOSPHOR_LOG2_USING;
13 
14 namespace pldm
15 {
16 namespace responder
17 {
18 namespace platform
19 {
sendBiosAttributeUpdateEvent(uint8_t eid,pldm::InstanceIdDb * instanceIdDb,const std::vector<uint16_t> & handles,pldm::requester::Handler<pldm::requester::Request> * handler)20 int sendBiosAttributeUpdateEvent(
21     uint8_t eid, pldm::InstanceIdDb* instanceIdDb,
22     const std::vector<uint16_t>& handles,
23     pldm::requester::Handler<pldm::requester::Request>* handler)
24 {
25     using BootProgress =
26         sdbusplus::client::xyz::openbmc_project::state::boot::Progress<>;
27 
28     constexpr auto hostStatePath = "/xyz/openbmc_project/state/host0";
29     constexpr auto hostStateProperty = "BootProgress";
30 
31     try
32     {
33         auto propVal = pldm::utils::DBusHandler().getDbusPropertyVariant(
34             hostStatePath, hostStateProperty, BootProgress::interface);
35 
36         using Stages = BootProgress::ProgressStages;
37         auto currHostState = sdbusplus::message::convert_from_string<Stages>(
38                                  std::get<std::string>(propVal))
39                                  .value();
40 
41         if (currHostState != Stages::SystemInitComplete &&
42             currHostState != Stages::OSRunning &&
43             currHostState != Stages::SystemSetup)
44         {
45             return PLDM_SUCCESS;
46         }
47     }
48     catch (
49         const sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound&)
50     {
51         /* Exception is expected to happen in the case when state manager is
52          * started after pldm, this is expected to happen in reboot case
53          * where host is considered to be up. As host is up pldm is expected
54          * to send attribute update event to host so this is not an error
55          * case */
56     }
57     catch (const sdbusplus::exception_t& e)
58     {
59         error(
60             "Error in getting current remote terminus state, error - '{ERROR}' Continue sending the bios attribute update event ...",
61             "ERROR", e);
62     }
63 
64     auto instanceId = instanceIdDb->next(eid);
65 
66     std::vector<uint8_t> requestMsg(
67         sizeof(pldm_msg_hdr) + sizeof(pldm_bios_attribute_update_event_req) -
68             1 + (handles.size() * sizeof(uint16_t)),
69         0);
70 
71     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
72 
73     auto rc = encode_bios_attribute_update_event_req(
74         instanceId, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION, TERMINUS_ID,
75         handles.size(), reinterpret_cast<const uint8_t*>(handles.data()),
76         requestMsg.size() - sizeof(pldm_msg_hdr), request);
77     if (rc != PLDM_SUCCESS)
78     {
79         error(
80             "Failed to encode BIOS Attribute update event message, response code '{RC}'",
81             "RC", lg2::hex, rc);
82         instanceIdDb->free(eid, instanceId);
83         return rc;
84     }
85 
86     auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
87                                                   const pldm_msg* response,
88                                                   size_t respMsgLen) {
89         if (response == nullptr || !respMsgLen)
90         {
91             error("Failed to receive response for platform event message");
92             return;
93         }
94         uint8_t completionCode{};
95         uint8_t status{};
96         auto rc = decode_platform_event_message_resp(response, respMsgLen,
97                                                      &completionCode, &status);
98         if (rc || completionCode)
99         {
100             error(
101                 "Failed to decode BIOS Attribute update platform event message response with response code '{RC}' and completion code '{CC}'",
102                 "RC", rc, "CC", completionCode);
103         }
104     };
105     rc = handler->registerRequest(
106         eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
107         std::move(requestMsg), std::move(platformEventMessageResponseHandler));
108     if (rc)
109     {
110         error(
111             "Failed to send BIOS Attribute update the platform event message, response code '{RC}'",
112             "RC", rc);
113     }
114 
115     return rc;
116 }
117 
118 } // namespace platform
119 
120 } // namespace responder
121 
122 } // namespace pldm
123