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"); |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 33 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"), |
| 34 | Argument::ARGUMENT_VALUE(version.c_str())); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 35 | } |
| 36 | |
Saqib Khan | 2308b8b | 2017-09-19 15:33:06 -0500 | [diff] [blame] | 37 | unsigned char digest[SHA512_DIGEST_LENGTH]; |
| 38 | SHA512_CTX ctx; |
| 39 | SHA512_Init(&ctx); |
| 40 | SHA512_Update(&ctx, version.c_str(), strlen(version.c_str())); |
| 41 | SHA512_Final(digest, &ctx); |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 42 | char mdString[SHA512_DIGEST_LENGTH * 2 + 1]; |
Saqib Khan | 2308b8b | 2017-09-19 15:33:06 -0500 | [diff] [blame] | 43 | for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) |
| 44 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 45 | snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]); |
Saqib Khan | 2308b8b | 2017-09-19 15:33:06 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Only need 8 hex digits. |
| 49 | std::string hexId = std::string(mdString); |
| 50 | return (hexId.substr(0, 8)); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 53 | std::map<std::string, std::string> |
| 54 | Version::getValue(const std::string& filePath, |
| 55 | std::map<std::string, std::string> keys) |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 56 | { |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 57 | if (filePath.empty()) |
| 58 | { |
| 59 | log<level::ERR>("Error filePath is empty"); |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 60 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"), |
| 61 | Argument::ARGUMENT_VALUE(filePath.c_str())); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | std::ifstream efile; |
| 65 | std::string line; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 66 | efile.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Gunnar Mills | a93a07b | 2017-09-21 15:40:09 -0500 | [diff] [blame] | 67 | std::ifstream::eofbit); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 68 | |
| 69 | try |
| 70 | { |
| 71 | efile.open(filePath); |
| 72 | while (getline(efile, line)) |
| 73 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 74 | for (auto& key : keys) |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 75 | { |
| 76 | auto value = key.first + "="; |
| 77 | auto keySize = value.length(); |
| 78 | if (line.compare(0, keySize, value) == 0) |
| 79 | { |
| 80 | key.second = line.substr(keySize); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | efile.close(); |
| 86 | } |
| 87 | catch (const std::exception& e) |
| 88 | { |
Saqib Khan | e53222d | 2017-08-19 16:57:24 -0500 | [diff] [blame] | 89 | if (!efile.eof()) |
| 90 | { |
| 91 | log<level::ERR>("Error in reading file"); |
| 92 | } |
| 93 | efile.close(); |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return keys; |
| 97 | } |
| 98 | |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 99 | void Delete::delete_() |
| 100 | { |
| 101 | if (parent.eraseCallback) |
| 102 | { |
| 103 | parent.eraseCallback(parent.getId(parent.version())); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void Version::updateDeleteInterface(sdbusplus::message::message& msg) |
| 108 | { |
| 109 | std::string interface, chassisState; |
| 110 | std::map<std::string, sdbusplus::message::variant<std::string>> properties; |
| 111 | |
| 112 | msg.read(interface, properties); |
| 113 | |
| 114 | for (const auto& p : properties) |
| 115 | { |
| 116 | if (p.first == "CurrentPowerState") |
| 117 | { |
William A. Kennington III | af07acf | 2018-11-06 15:15:28 -0800 | [diff] [blame] | 118 | chassisState = |
| 119 | sdbusplus::message::variant_ns::get<std::string>(p.second); |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 120 | } |
| 121 | } |
Adriana Kobylak | 90fdc70 | 2018-08-29 09:38:42 -0500 | [diff] [blame] | 122 | if (chassisState.empty()) |
| 123 | { |
| 124 | // The chassis power state property did not change, return. |
| 125 | return; |
| 126 | } |
Saqib Khan | 7f80e0b | 2017-10-22 11:29:07 -0500 | [diff] [blame] | 127 | |
| 128 | if ((parent.isVersionFunctional(this->versionId)) && |
| 129 | (chassisState != CHASSIS_STATE_OFF)) |
| 130 | { |
| 131 | if (deleteObject) |
| 132 | { |
| 133 | deleteObject.reset(nullptr); |
| 134 | } |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | if (!deleteObject) |
| 139 | { |
| 140 | deleteObject = std::make_unique<Delete>(bus, objPath, *this); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Saqib Khan | 167601b | 2017-06-18 23:33:46 -0500 | [diff] [blame] | 145 | } // namespace updater |
| 146 | } // namespace software |
| 147 | } // namespace openpower |