blob: f7a1194f0afdddded9531d6086532141e2270ab7 [file] [log] [blame]
Lei YUe4994462019-03-14 14:41:53 +08001#include "config.h"
2
Adriana Kobylak70ca2422018-09-06 14:23:38 -05003#include "utils.hpp"
4
Lei YUe4994462019-03-14 14:41:53 +08005#include <phosphor-logging/elog-errors.hpp>
6#include <phosphor-logging/elog.hpp>
7#include <phosphor-logging/log.hpp>
8#include <xyz/openbmc_project/Common/error.hpp>
9
Adriana Kobylak70ca2422018-09-06 14:23:38 -050010#if OPENSSL_VERSION_NUMBER < 0x10100000L
11
12#include <string.h>
13
14static void* OPENSSL_zalloc(size_t num)
15{
16 void* ret = OPENSSL_malloc(num);
17
18 if (ret != NULL)
19 {
20 memset(ret, 0, num);
21 }
22 return ret;
23}
24
25EVP_MD_CTX* EVP_MD_CTX_new(void)
26{
27 return (EVP_MD_CTX*)OPENSSL_zalloc(sizeof(EVP_MD_CTX));
28}
29
30void EVP_MD_CTX_free(EVP_MD_CTX* ctx)
31{
32 EVP_MD_CTX_cleanup(ctx);
33 OPENSSL_free(ctx);
34}
35
36#endif // OPENSSL_VERSION_NUMBER < 0x10100000L
Lei YUe4994462019-03-14 14:41:53 +080037
38namespace utils
39{
40
Lei YUe4994462019-03-14 14:41:53 +080041using namespace phosphor::logging;
42
43constexpr auto HIOMAPD_PATH = "/xyz/openbmc_project/Hiomapd";
44constexpr auto HIOMAPD_INTERFACE = "xyz.openbmc_project.Hiomapd.Control";
45
46using InternalFailure =
47 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
48
49std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
50 const std::string& intf)
51{
52 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
53 MAPPER_INTERFACE, "GetObject");
54
55 mapper.append(path, std::vector<std::string>({intf}));
56 try
57 {
58 auto mapperResponseMsg = bus.call(mapper);
59
60 std::vector<std::pair<std::string, std::vector<std::string>>>
61 mapperResponse;
62 mapperResponseMsg.read(mapperResponse);
63 if (mapperResponse.empty())
64 {
65 log<level::ERR>("Error reading mapper response");
66 throw std::runtime_error("Error reading mapper response");
67 }
68 return mapperResponse[0].first;
69 }
Patrick Williams7b5685d2021-09-02 09:32:14 -050070 catch (const sdbusplus::exception::exception& ex)
Lei YUe4994462019-03-14 14:41:53 +080071 {
72 log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
73 entry("PATH=%s", path.c_str()),
74 entry("INTERFACE=%s", intf.c_str()));
75 throw std::runtime_error("Mapper call failed");
76 }
77}
78
79void hiomapdSuspend(sdbusplus::bus::bus& bus)
80{
81 auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
82 auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
83 HIOMAPD_INTERFACE, "Suspend");
84
85 try
86 {
87 bus.call_noreply(method);
88 }
Patrick Williams7b5685d2021-09-02 09:32:14 -050089 catch (const sdbusplus::exception::exception& e)
Lei YUe4994462019-03-14 14:41:53 +080090 {
91 log<level::ERR>("Error in mboxd suspend call",
92 entry("ERROR=%s", e.what()));
93 }
94}
95
96void hiomapdResume(sdbusplus::bus::bus& bus)
97{
98 auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
99 auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
100 HIOMAPD_INTERFACE, "Resume");
101
102 method.append(true); // Indicate PNOR is modified
103
104 try
105 {
106 bus.call_noreply(method);
107 }
Patrick Williams7b5685d2021-09-02 09:32:14 -0500108 catch (const sdbusplus::exception::exception& e)
Lei YUe4994462019-03-14 14:41:53 +0800109 {
110 log<level::ERR>("Error in mboxd suspend call",
111 entry("ERROR=%s", e.what()));
112 }
113}
114
Adriana Kobylakf9a72a72022-05-20 14:52:29 +0000115void setPendingAttributes(sdbusplus::bus::bus& bus, const std::string& attrName,
116 const std::string& attrValue)
Adriana Kobylak56a46772022-02-25 16:37:37 +0000117{
118 constexpr auto biosConfigPath = "/xyz/openbmc_project/bios_config/manager";
119 constexpr auto biosConfigIntf = "xyz.openbmc_project.BIOSConfig.Manager";
120 constexpr auto dbusAttrType =
121 "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration";
Adriana Kobylak56a46772022-02-25 16:37:37 +0000122
123 using PendingAttributesType = std::vector<std::pair<
124 std::string, std::tuple<std::string, std::variant<std::string>>>>;
125 PendingAttributesType pendingAttributes;
Adriana Kobylakf9a72a72022-05-20 14:52:29 +0000126 pendingAttributes.emplace_back(
127 std::make_pair(attrName, std::make_tuple(dbusAttrType, attrValue)));
Adriana Kobylak56a46772022-02-25 16:37:37 +0000128
129 try
130 {
131 auto service = getService(bus, biosConfigPath, biosConfigIntf);
132 auto method = bus.new_method_call(service.c_str(), biosConfigPath,
133 SYSTEMD_PROPERTY_INTERFACE, "Set");
134 method.append(biosConfigIntf, "PendingAttributes",
135 std::variant<PendingAttributesType>(pendingAttributes));
136 bus.call(method);
137 }
138 catch (const sdbusplus::exception::exception& e)
139 {
140 log<level::ERR>("Error setting the bios attribute",
141 entry("ERROR=%s", e.what()),
Adriana Kobylakf9a72a72022-05-20 14:52:29 +0000142 entry("ATTRIBUTE=%s", attrName.c_str()),
143 entry("ATTRIBUTE_VALUE=%s", attrValue.c_str()));
Adriana Kobylak56a46772022-02-25 16:37:37 +0000144 return;
145 }
146}
147
Adriana Kobylakf9a72a72022-05-20 14:52:29 +0000148void clearHMCManaged(sdbusplus::bus::bus& bus)
149{
150 setPendingAttributes(bus, "pvm_hmc_managed", "Disabled");
151}
152
153void setClearNvram(sdbusplus::bus::bus& bus)
154{
155 setPendingAttributes(bus, "pvm_clear_nvram", "Enabled");
156}
157
Adriana Kobylak267c4132022-02-25 20:00:07 +0000158void deleteAllErrorLogs(sdbusplus::bus::bus& bus)
159{
160 constexpr auto loggingPath = "/xyz/openbmc_project/logging";
161 constexpr auto deleteAllIntf = "xyz.openbmc_project.Collection.DeleteAll";
162
163 auto service = getService(bus, loggingPath, deleteAllIntf);
164 auto method = bus.new_method_call(service.c_str(), loggingPath,
165 deleteAllIntf, "DeleteAll");
166
167 try
168 {
169 bus.call_noreply(method);
170 }
171 catch (const sdbusplus::exception::exception& e)
172 {
173 log<level::ERR>("Error deleting all error logs",
174 entry("ERROR=%s", e.what()));
175 }
176}
177
Lei YUe4994462019-03-14 14:41:53 +0800178} // namespace utils