Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <sstream> |
| 4 | #include <fstream> |
| 5 | #include <stdexcept> |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 6 | #include <openssl/sha.h> |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include "xyz/openbmc_project/Common/error.hpp" |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 10 | #include "config.h" |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 11 | #include "version.hpp" |
| 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace software |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 21 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 22 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 23 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 24 | std::string Version::getValue(const std::string& manifestFilePath, |
| 25 | std::string key) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 26 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 27 | key = key + "="; |
| 28 | auto keySize = key.length(); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 29 | |
| 30 | if (manifestFilePath.empty()) |
| 31 | { |
| 32 | log<level::ERR>("Error MANIFESTFilePath is empty"); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 33 | elog<InvalidArgument>( |
| 34 | Argument::ARGUMENT_NAME("manifestFilePath"), |
| 35 | Argument::ARGUMENT_VALUE(manifestFilePath.c_str())); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 38 | std::string value{}; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 39 | std::ifstream efile; |
| 40 | std::string line; |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 41 | efile.exceptions(std::ifstream::failbit | |
| 42 | std::ifstream::badbit | |
| 43 | std::ifstream::eofbit); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 44 | |
| 45 | // Too many GCC bugs (53984, 66145) to do this the right way... |
| 46 | try |
| 47 | { |
| 48 | efile.open(manifestFilePath); |
| 49 | while (getline(efile, line)) |
| 50 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 51 | if (line.compare(0, keySize, key) == 0) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 52 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 53 | value = line.substr(keySize); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 54 | break; |
| 55 | } |
| 56 | } |
| 57 | efile.close(); |
| 58 | } |
| 59 | catch (const std::exception& e) |
| 60 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 61 | log<level::ERR>("Error in reading MANIFEST file"); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 64 | return value; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::string Version::getId(const std::string& version) |
| 68 | { |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 69 | |
| 70 | if (version.empty()) |
| 71 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 72 | log<level::ERR>("Error version is empty"); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 73 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"), |
| 74 | Argument::ARGUMENT_VALUE(version.c_str())); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Saqib Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 77 | unsigned char digest[SHA512_DIGEST_LENGTH]; |
| 78 | SHA512_CTX ctx; |
| 79 | SHA512_Init(&ctx); |
| 80 | SHA512_Update(&ctx, version.c_str(), strlen(version.c_str())); |
| 81 | SHA512_Final(digest, &ctx); |
| 82 | char mdString[SHA512_DIGEST_LENGTH*2+1]; |
| 83 | for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) |
| 84 | { |
| 85 | snprintf(&mdString[i*2], 3, "%02x", (unsigned int)digest[i]); |
| 86 | } |
| 87 | |
| 88 | // Only need 8 hex digits. |
| 89 | std::string hexId = std::string(mdString); |
| 90 | return (hexId.substr(0, 8)); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 93 | std::string Version::getBMCVersion(const std::string& releaseFilePath) |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 94 | { |
| 95 | std::string versionKey = "VERSION_ID="; |
| 96 | std::string version{}; |
| 97 | std::ifstream efile; |
| 98 | std::string line; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 99 | efile.open(releaseFilePath); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 100 | |
| 101 | while (getline(efile, line)) |
| 102 | { |
| 103 | if (line.substr(0, versionKey.size()).find(versionKey) != |
| 104 | std::string::npos) |
| 105 | { |
| 106 | std::size_t pos = line.find_first_of('"') + 1; |
| 107 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | efile.close(); |
| 112 | |
| 113 | if (version.empty()) |
| 114 | { |
| 115 | log<level::ERR>("Error BMC current version is empty"); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 116 | elog<InternalFailure>(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return version; |
| 120 | } |
| 121 | |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 122 | bool Version::isFunctional() |
| 123 | { |
| 124 | return versionStr == getBMCVersion(OS_RELEASE_FILE); |
| 125 | } |
| 126 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 127 | void Delete::delete_() |
| 128 | { |
| 129 | if (parent.eraseCallback) |
| 130 | { |
| 131 | parent.eraseCallback(parent.getId(parent.version())); |
| 132 | } |
| 133 | } |
| 134 | |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 135 | } // namespace manager |
| 136 | } // namespace software |
| 137 | } // namepsace phosphor |