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