Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 1 | #include "version.hpp" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 2 | |
Leonel Gonzalez | 9c8adfa | 2017-07-12 11:08:40 -0500 | [diff] [blame] | 3 | #include "item_updater.hpp" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 6 | #include <openssl/evp.h> |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 7 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame] | 10 | |
| 11 | #include <fstream> |
| 12 | #include <iostream> |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 13 | #include <sstream> |
| 14 | #include <stdexcept> |
| 15 | #include <string> |
| 16 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 17 | namespace openpower |
| 18 | { |
| 19 | namespace software |
| 20 | { |
| 21 | namespace updater |
| 22 | { |
| 23 | |
| 24 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 25 | using namespace phosphor::logging; |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 26 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 27 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 28 | using EVP_MD_CTX_Ptr = |
| 29 | std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>; |
| 30 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 31 | std::string Version::getId(const std::string& version) |
| 32 | { |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 33 | if (version.empty()) |
| 34 | { |
| 35 | log<level::ERR>("Error version is empty"); |
Lei YU | 91add6d | 2019-03-01 14:23:40 +0800 | [diff] [blame] | 36 | return {}; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 39 | std::array<unsigned char, EVP_MAX_MD_SIZE> digest{}; |
| 40 | EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free); |
Saqib Khan | 2308b8b | 2017-09-19 15:33:06 -0500 | [diff] [blame] | 41 | |
Adriana Kobylak | 5e0b237 | 2021-12-08 21:59:30 +0000 | [diff] [blame] | 42 | EVP_DigestInit(ctx.get(), EVP_sha512()); |
| 43 | EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str())); |
| 44 | EVP_DigestFinal(ctx.get(), digest.data(), nullptr); |
| 45 | |
| 46 | // We are only using the first 8 characters. |
| 47 | char mdString[9]; |
| 48 | snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x", |
| 49 | (unsigned int)digest[0], (unsigned int)digest[1], |
| 50 | (unsigned int)digest[2], (unsigned int)digest[3]); |
| 51 | |
| 52 | return mdString; |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 55 | std::map<std::string, std::string> |
| 56 | Version::getValue(const std::string& filePath, |
| 57 | std::map<std::string, std::string> keys) |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 58 | { |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 59 | if (filePath.empty()) |
| 60 | { |
| 61 | log<level::ERR>("Error filePath is empty"); |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 62 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"), |
| 63 | Argument::ARGUMENT_VALUE(filePath.c_str())); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::ifstream efile; |
| 67 | std::string line; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 68 | efile.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 69 | std::ifstream::eofbit); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 70 | |
| 71 | try |
| 72 | { |
| 73 | efile.open(filePath); |
| 74 | while (getline(efile, line)) |
| 75 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 76 | for (auto& key : keys) |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 77 | { |
| 78 | auto value = key.first + "="; |
| 79 | auto keySize = value.length(); |
| 80 | if (line.compare(0, keySize, value) == 0) |
| 81 | { |
| 82 | key.second = line.substr(keySize); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | efile.close(); |
| 88 | } |
| 89 | catch (const std::exception& e) |
| 90 | { |
Saqib Khan | e53222d | 2017-08-19 16:57:24 -0500 | [diff] [blame] | 91 | if (!efile.eof()) |
| 92 | { |
| 93 | log<level::ERR>("Error in reading file"); |
| 94 | } |
| 95 | efile.close(); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return keys; |
| 99 | } |
| 100 | |
Lei YU | dec8cf9 | 2019-02-21 17:47:05 +0800 | [diff] [blame] | 101 | std::pair<std::string, std::string> |
| 102 | Version::getVersions(const std::string& versionPart) |
| 103 | { |
| 104 | // versionPart contains strings like below: |
| 105 | // open-power-romulus-v2.2-rc1-48-g268344f-dirty |
| 106 | // buildroot-2018.11.1-7-g5d7cc8c |
| 107 | // skiboot-v6.2 |
| 108 | std::istringstream iss(versionPart); |
| 109 | std::string line; |
| 110 | std::string version; |
| 111 | std::stringstream ss; |
| 112 | std::string extendedVersion; |
| 113 | |
| 114 | if (!std::getline(iss, line)) |
| 115 | { |
| 116 | log<level::ERR>("Unable to read from version", |
| 117 | entry("VERSION=%s", versionPart.c_str())); |
| 118 | return {}; |
| 119 | } |
| 120 | version = line; |
| 121 | |
| 122 | while (std::getline(iss, line)) |
| 123 | { |
| 124 | // Each line starts with a tab, let's trim it |
| 125 | line.erase(line.begin(), |
| 126 | std::find_if(line.begin(), line.end(), |
| 127 | [](int c) { return !std::isspace(c); })); |
| 128 | ss << line << ','; |
| 129 | } |
| 130 | extendedVersion = ss.str(); |
| 131 | |
| 132 | // Erase the last ',', if there is one |
| 133 | if (!extendedVersion.empty()) |
| 134 | { |
| 135 | extendedVersion.pop_back(); |
| 136 | } |
| 137 | return {version, extendedVersion}; |
| 138 | } |
| 139 | |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 140 | void Delete::delete_() |
| 141 | { |
| 142 | if (parent.eraseCallback) |
| 143 | { |
| 144 | parent.eraseCallback(parent.getId(parent.version())); |
| 145 | } |
| 146 | } |
| 147 | |
Patrick Williams | 0dea199 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 148 | void Version::updateDeleteInterface(sdbusplus::message_t& msg) |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 149 | { |
| 150 | std::string interface, chassisState; |
Patrick Williams | 212102e | 2020-05-13 17:50:50 -0500 | [diff] [blame] | 151 | std::map<std::string, std::variant<std::string>> properties; |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 152 | |
| 153 | msg.read(interface, properties); |
| 154 | |
| 155 | for (const auto& p : properties) |
| 156 | { |
| 157 | if (p.first == "CurrentPowerState") |
| 158 | { |
Patrick Williams | 550f31b | 2020-05-13 11:15:24 -0500 | [diff] [blame] | 159 | chassisState = std::get<std::string>(p.second); |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
Adriana Kobylak | 90fdc70 | 2018-08-29 09:38:42 -0500 | [diff] [blame] | 162 | if (chassisState.empty()) |
| 163 | { |
| 164 | // The chassis power state property did not change, return. |
| 165 | return; |
| 166 | } |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 167 | |
| 168 | if ((parent.isVersionFunctional(this->versionId)) && |
| 169 | (chassisState != CHASSIS_STATE_OFF)) |
| 170 | { |
| 171 | if (deleteObject) |
| 172 | { |
| 173 | deleteObject.reset(nullptr); |
| 174 | } |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | if (!deleteObject) |
| 179 | { |
| 180 | deleteObject = std::make_unique<Delete>(bus, objPath, *this); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 185 | } // namespace updater |
| 186 | } // namespace software |
| 187 | } // namespace openpower |