blob: a534ddf1371fddf43f60471b90598799571a38d8 [file] [log] [blame]
Jayanth Othayothda9b5832021-11-05 04:19:43 -05001#include "phal_service_actions.hpp"
2
3#include <attributes_info.H>
4#include <fmt/format.h>
5
6#include <phosphor-logging/log.hpp>
7
8namespace openpower
9{
10namespace pels
11{
12namespace phal
13{
14using namespace phosphor::logging;
15
16/**
17 * @brief Helper function to get EntrySeverity based on
18 * the given GardType
19 *
20 * @param[in] guardType openpower gard type
21 *
22 * @return EntrySeverity on success
23 * Empty optional on failure
24 */
25std::optional<EntrySeverity> getEntrySeverityType(const std::string& guardType)
26{
27 if ((guardType == "GARD_Unrecoverable") || (guardType == "GARD_Fatal"))
28 {
29 return EntrySeverity::Critical;
30 }
31 else if (guardType == "GARD_User_Manual")
32 {
33 return EntrySeverity::Manual;
34 }
35 else if (guardType == "GARD_Predictive")
36 {
37 return EntrySeverity::Warning;
38 }
39 else
40 {
41 log<level::ERR>(
42 fmt::format("GUARD: Unsupported GardType [{}] was given ",
43 "to get the hardware isolation entry severity type",
44 guardType.c_str())
45 .c_str());
46 }
47 return std::nullopt;
48}
49
50/**
51 * @brief Helper function to create guard records.
52 *
53 * User need to fill the JSON callouts array with below keywords/data
54 * "Entitypath": entity path of the hardware from the PHAL device tree.
55 * "Guardtype": The hardware isolation severity which is defined in
56 * xyz.openbmc_project.HardwareIsolation.Entry
57 * "Guarded": boolean, true to create gurad records.
58 *
59 * @param[in] jsonCallouts - The array of JSON callouts, or an empty object.
60 * @param[in] path - The BMC error log object path
61 * @param[in] dataIface - The DataInterface object
62 */
63void createGuardRecords(const nlohmann::json& jsonCallouts,
64 const std::string& path,
65 const DataInterfaceBase& dataIface)
66{
67 if (jsonCallouts.empty())
68 {
69 return;
70 }
71
72 if (!jsonCallouts.is_array())
73 {
74 log<level::ERR>("GUARD: Callout JSON isn't an array");
75 return;
76 }
77 for (const auto& _callout : jsonCallouts)
78 {
79 try
80 {
81 // Check Callout data conatains Guarded requests.
82 if (!_callout.contains("Guarded"))
83 {
84 continue;
85 }
86 if (!_callout.at("Guarded").get<bool>())
87 {
88 continue;
89 }
90 // Get Entity path required for guard D-bus method
91 // "CreateWithEntityPath"
92 if (!_callout.contains("EntityPath"))
93 {
94 log<level::ERR>(
95 "GUARD: Callout data, missing EntityPath information");
96 continue;
97 }
98 using EntityPath = std::vector<uint8_t>;
99
100 auto entityPath = _callout.at("EntityPath").get<EntityPath>();
101
102 std::stringstream ss;
103 for (uint32_t a = 0; a < sizeof(ATTR_PHYS_BIN_PATH_Type); a++)
104 {
105 ss << " 0x" << std::hex << static_cast<int>(entityPath[a]);
106 }
107 std::string s = ss.str();
108 log<level::INFO>(fmt::format("GUARD :({})", s).c_str());
109
110 // Get Guard type
111 auto severity = EntrySeverity::Warning;
112 if (!_callout.contains("GuardType"))
113 {
114 log<level::ERR>(
115 "GUARD: doesn't have Severity, setting to warning");
116 }
117 else
118 {
119 auto guardType = _callout.at("GuardType").get<std::string>();
120 // convert GuardType to severity type.
121 auto sType = getEntrySeverityType(guardType);
122 if (sType.has_value())
123 {
124 severity = sType.value();
125 }
126 }
127 // Create guard record
128 auto type = sdbusplus::xyz::openbmc_project::HardwareIsolation::
129 server::convertForMessage(severity);
130 dataIface.createGuardRecord(entityPath, type, path);
131 }
132 catch (const std::exception& e)
133 {
134 log<level::INFO>(
135 fmt::format("GUARD: Failed entry creation exception:({})",
136 e.what())
137 .c_str());
138 }
139 }
140}
141
142void createServiceActions(const nlohmann::json& jsonCallouts,
143 const std::string& path,
144 const DataInterfaceBase& dataIface)
145{
146 // Create Guard records.
147 createGuardRecords(jsonCallouts, path, dataIface);
148}
149} // namespace phal
150} // namespace pels
151} // namespace openpower