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