blob: 04b24bfd53799b832594a74becbe2d41bc106104 [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
41using sdbusplus::exception::SdBusError;
42using namespace phosphor::logging;
43
44constexpr auto HIOMAPD_PATH = "/xyz/openbmc_project/Hiomapd";
45constexpr auto HIOMAPD_INTERFACE = "xyz.openbmc_project.Hiomapd.Control";
46
47using InternalFailure =
48 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
49
50std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
51 const std::string& intf)
52{
53 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
54 MAPPER_INTERFACE, "GetObject");
55
56 mapper.append(path, std::vector<std::string>({intf}));
57 try
58 {
59 auto mapperResponseMsg = bus.call(mapper);
60
61 std::vector<std::pair<std::string, std::vector<std::string>>>
62 mapperResponse;
63 mapperResponseMsg.read(mapperResponse);
64 if (mapperResponse.empty())
65 {
66 log<level::ERR>("Error reading mapper response");
67 throw std::runtime_error("Error reading mapper response");
68 }
69 return mapperResponse[0].first;
70 }
71 catch (const sdbusplus::exception::SdBusError& ex)
72 {
73 log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
74 entry("PATH=%s", path.c_str()),
75 entry("INTERFACE=%s", intf.c_str()));
76 throw std::runtime_error("Mapper call failed");
77 }
78}
79
80void hiomapdSuspend(sdbusplus::bus::bus& bus)
81{
82 auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
83 auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
84 HIOMAPD_INTERFACE, "Suspend");
85
86 try
87 {
88 bus.call_noreply(method);
89 }
90 catch (const SdBusError& e)
91 {
92 log<level::ERR>("Error in mboxd suspend call",
93 entry("ERROR=%s", e.what()));
94 }
95}
96
97void hiomapdResume(sdbusplus::bus::bus& bus)
98{
99 auto service = getService(bus, HIOMAPD_PATH, HIOMAPD_INTERFACE);
100 auto method = bus.new_method_call(service.c_str(), HIOMAPD_PATH,
101 HIOMAPD_INTERFACE, "Resume");
102
103 method.append(true); // Indicate PNOR is modified
104
105 try
106 {
107 bus.call_noreply(method);
108 }
109 catch (const SdBusError& e)
110 {
111 log<level::ERR>("Error in mboxd suspend call",
112 entry("ERROR=%s", e.what()));
113 }
114}
115
116} // namespace utils