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