| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 1 | #include "config.h" | 
|  | 2 |  | 
| Adriana Kobylak | 70ca242 | 2018-09-06 14:23:38 -0500 | [diff] [blame] | 3 | #include "utils.hpp" | 
|  | 4 |  | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 5 | #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 Kobylak | 70ca242 | 2018-09-06 14:23:38 -0500 | [diff] [blame] | 10 | #if OPENSSL_VERSION_NUMBER < 0x10100000L | 
|  | 11 |  | 
|  | 12 | #include <string.h> | 
|  | 13 |  | 
|  | 14 | static 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 |  | 
|  | 25 | EVP_MD_CTX* EVP_MD_CTX_new(void) | 
|  | 26 | { | 
|  | 27 | return (EVP_MD_CTX*)OPENSSL_zalloc(sizeof(EVP_MD_CTX)); | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | void 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 YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 37 |  | 
|  | 38 | namespace utils | 
|  | 39 | { | 
|  | 40 |  | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 41 | using namespace phosphor::logging; | 
|  | 42 |  | 
|  | 43 | constexpr auto HIOMAPD_PATH = "/xyz/openbmc_project/Hiomapd"; | 
|  | 44 | constexpr auto HIOMAPD_INTERFACE = "xyz.openbmc_project.Hiomapd.Control"; | 
|  | 45 |  | 
|  | 46 | using InternalFailure = | 
|  | 47 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; | 
|  | 48 |  | 
|  | 49 | std::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 Williams | 7b5685d | 2021-09-02 09:32:14 -0500 | [diff] [blame] | 70 | catch (const sdbusplus::exception::exception& ex) | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 71 | { | 
|  | 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 |  | 
|  | 79 | void 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 Williams | 7b5685d | 2021-09-02 09:32:14 -0500 | [diff] [blame] | 89 | catch (const sdbusplus::exception::exception& e) | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 90 | { | 
|  | 91 | log<level::ERR>("Error in mboxd suspend call", | 
|  | 92 | entry("ERROR=%s", e.what())); | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | void 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 Williams | 7b5685d | 2021-09-02 09:32:14 -0500 | [diff] [blame] | 108 | catch (const sdbusplus::exception::exception& e) | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 109 | { | 
|  | 110 | log<level::ERR>("Error in mboxd suspend call", | 
|  | 111 | entry("ERROR=%s", e.what())); | 
|  | 112 | } | 
|  | 113 | } | 
|  | 114 |  | 
| Adriana Kobylak | 56a4677 | 2022-02-25 16:37:37 +0000 | [diff] [blame] | 115 | void clearHMCManaged(sdbusplus::bus::bus& bus) | 
|  | 116 | { | 
|  | 117 | constexpr auto biosConfigPath = "/xyz/openbmc_project/bios_config/manager"; | 
|  | 118 | constexpr auto biosConfigIntf = "xyz.openbmc_project.BIOSConfig.Manager"; | 
|  | 119 | constexpr auto dbusAttrType = | 
|  | 120 | "xyz.openbmc_project.BIOSConfig.Manager.AttributeType.Enumeration"; | 
|  | 121 | constexpr auto dbusAttrName = "pvm_hmc_managed"; | 
|  | 122 |  | 
|  | 123 | using PendingAttributesType = std::vector<std::pair< | 
|  | 124 | std::string, std::tuple<std::string, std::variant<std::string>>>>; | 
|  | 125 | PendingAttributesType pendingAttributes; | 
|  | 126 | pendingAttributes.emplace_back(std::make_pair( | 
|  | 127 | dbusAttrName, std::make_tuple(dbusAttrType, "Disabled"))); | 
|  | 128 |  | 
|  | 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()), | 
|  | 142 | entry("ATTRIBUTE=%s", dbusAttrName)); | 
|  | 143 | return; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 |  | 
| Adriana Kobylak | 267c413 | 2022-02-25 20:00:07 +0000 | [diff] [blame] | 147 | void deleteAllErrorLogs(sdbusplus::bus::bus& bus) | 
|  | 148 | { | 
|  | 149 | constexpr auto loggingPath = "/xyz/openbmc_project/logging"; | 
|  | 150 | constexpr auto deleteAllIntf = "xyz.openbmc_project.Collection.DeleteAll"; | 
|  | 151 |  | 
|  | 152 | auto service = getService(bus, loggingPath, deleteAllIntf); | 
|  | 153 | auto method = bus.new_method_call(service.c_str(), loggingPath, | 
|  | 154 | deleteAllIntf, "DeleteAll"); | 
|  | 155 |  | 
|  | 156 | try | 
|  | 157 | { | 
|  | 158 | bus.call_noreply(method); | 
|  | 159 | } | 
|  | 160 | catch (const sdbusplus::exception::exception& e) | 
|  | 161 | { | 
|  | 162 | log<level::ERR>("Error deleting all error logs", | 
|  | 163 | entry("ERROR=%s", e.what())); | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 |  | 
| Lei YU | e499446 | 2019-03-14 14:41:53 +0800 | [diff] [blame] | 167 | } // namespace utils |