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