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