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 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 11 | |
| 12 | #include <fstream> |
| 13 | #include <iostream> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 14 | #include <sstream> |
| 15 | #include <stdexcept> |
| 16 | #include <string> |
| 17 | |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 18 | namespace phosphor |
| 19 | { |
| 20 | namespace software |
| 21 | { |
| 22 | namespace manager |
| 23 | { |
| 24 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 25 | PHOSPHOR_LOG2_USING; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 27 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 29 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 30 | std::string Version::getValue(const std::string& manifestFilePath, |
| 31 | std::string key) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 32 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 33 | key = key + "="; |
| 34 | auto keySize = key.length(); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 35 | |
| 36 | if (manifestFilePath.empty()) |
| 37 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 38 | error("ManifestFilePath is empty."); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 39 | elog<InvalidArgument>( |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 40 | Argument::ARGUMENT_NAME("manifestFilePath"), |
| 41 | Argument::ARGUMENT_VALUE(manifestFilePath.c_str())); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 44 | std::string value{}; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 45 | std::ifstream efile; |
| 46 | std::string line; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 47 | efile.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 48 | std::ifstream::eofbit); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 49 | |
| 50 | // Too many GCC bugs (53984, 66145) to do this the right way... |
| 51 | try |
| 52 | { |
| 53 | efile.open(manifestFilePath); |
| 54 | while (getline(efile, line)) |
| 55 | { |
Lei YU | 5a7363b | 2019-10-18 16:50:59 +0800 | [diff] [blame] | 56 | if (!line.empty() && line.back() == '\r') |
| 57 | { |
| 58 | // If the manifest has CRLF line terminators, e.g. is created on |
| 59 | // Windows, the line will contain \r at the end, remove it. |
| 60 | line.pop_back(); |
| 61 | } |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 62 | if (line.compare(0, keySize, key) == 0) |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 63 | { |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 64 | value = line.substr(keySize); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 65 | break; |
| 66 | } |
| 67 | } |
| 68 | efile.close(); |
| 69 | } |
| 70 | catch (const std::exception& e) |
| 71 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 72 | error("Error occurred when reading MANIFEST file: {ERROR}", "KEY", key, |
| 73 | "ERROR", e); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Gunnar Mills | cebd102 | 2017-04-17 16:10:15 -0500 | [diff] [blame] | 76 | return value; |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | std::string Version::getId(const std::string& version) |
| 80 | { |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 81 | |
| 82 | if (version.empty()) |
| 83 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 84 | error("Version is empty."); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 85 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"), |
| 86 | Argument::ARGUMENT_VALUE(version.c_str())); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Saqib Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 89 | unsigned char digest[SHA512_DIGEST_LENGTH]; |
| 90 | SHA512_CTX ctx; |
| 91 | SHA512_Init(&ctx); |
| 92 | SHA512_Update(&ctx, version.c_str(), strlen(version.c_str())); |
| 93 | SHA512_Final(digest, &ctx); |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 94 | char mdString[SHA512_DIGEST_LENGTH * 2 + 1]; |
Saqib Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 95 | for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) |
| 96 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 97 | snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]); |
Saqib Khan | 26a960d | 2017-09-19 14:23:28 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Only need 8 hex digits. |
| 101 | std::string hexId = std::string(mdString); |
| 102 | return (hexId.substr(0, 8)); |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Vijay Khemka | b7c062e | 2019-09-18 17:15:57 -0700 | [diff] [blame] | 105 | std::string Version::getBMCMachine(const std::string& releaseFilePath) |
| 106 | { |
| 107 | std::string machineKey = "OPENBMC_TARGET_MACHINE="; |
| 108 | std::string machine{}; |
| 109 | std::ifstream efile(releaseFilePath); |
| 110 | std::string line; |
| 111 | |
| 112 | while (getline(efile, line)) |
| 113 | { |
| 114 | if (line.substr(0, machineKey.size()).find(machineKey) != |
| 115 | std::string::npos) |
| 116 | { |
| 117 | std::size_t pos = line.find_first_of('"') + 1; |
| 118 | machine = line.substr(pos, line.find_last_of('"') - pos); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (machine.empty()) |
| 124 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 125 | error("Unable to find OPENBMC_TARGET_MACHINE"); |
Vijay Khemka | b7c062e | 2019-09-18 17:15:57 -0700 | [diff] [blame] | 126 | elog<InternalFailure>(); |
| 127 | } |
| 128 | |
| 129 | return machine; |
| 130 | } |
| 131 | |
Chanh Nguyen | 1fd6ddd | 2021-01-06 11:09:09 +0700 | [diff] [blame] | 132 | std::string Version::getBMCExtendedVersion(const std::string& releaseFilePath) |
| 133 | { |
| 134 | std::string extendedVersionKey = "EXTENDED_VERSION="; |
| 135 | std::string extendedVersionValue{}; |
| 136 | std::string extendedVersion{}; |
| 137 | std::ifstream efile(releaseFilePath); |
| 138 | std::string line; |
| 139 | |
| 140 | while (getline(efile, line)) |
| 141 | { |
| 142 | if (line.substr(0, extendedVersionKey.size()) |
| 143 | .find(extendedVersionKey) != std::string::npos) |
| 144 | { |
| 145 | extendedVersionValue = line.substr(extendedVersionKey.size()); |
| 146 | std::size_t pos = extendedVersionValue.find_first_of('"') + 1; |
| 147 | extendedVersion = extendedVersionValue.substr( |
| 148 | pos, extendedVersionValue.find_last_of('"') - pos); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return extendedVersion; |
| 154 | } |
| 155 | |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 156 | std::string Version::getBMCVersion(const std::string& releaseFilePath) |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 157 | { |
| 158 | std::string versionKey = "VERSION_ID="; |
Adriana Kobylak | 2a3d9f5 | 2020-05-06 10:46:32 -0500 | [diff] [blame] | 159 | std::string versionValue{}; |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 160 | std::string version{}; |
| 161 | std::ifstream efile; |
| 162 | std::string line; |
Saqib Khan | 1eef62d | 2017-08-10 15:29:34 -0500 | [diff] [blame] | 163 | efile.open(releaseFilePath); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 164 | |
| 165 | while (getline(efile, line)) |
| 166 | { |
| 167 | if (line.substr(0, versionKey.size()).find(versionKey) != |
| 168 | std::string::npos) |
| 169 | { |
Adriana Kobylak | 2a3d9f5 | 2020-05-06 10:46:32 -0500 | [diff] [blame] | 170 | // Support quoted and unquoted values |
| 171 | // 1. Remove the versionKey so that we process the value only. |
| 172 | versionValue = line.substr(versionKey.size()); |
| 173 | |
| 174 | // 2. Look for a starting quote, then increment the position by 1 to |
| 175 | // skip the quote character. If no quote is found, |
| 176 | // find_first_of() returns npos (-1), which by adding +1 sets pos |
| 177 | // to 0 (beginning of unquoted string). |
| 178 | std::size_t pos = versionValue.find_first_of('"') + 1; |
| 179 | |
| 180 | // 3. Look for ending quote, then decrease the position by pos to |
| 181 | // get the size of the string up to before the ending quote. If |
| 182 | // no quote is found, find_last_of() returns npos (-1), and pos |
| 183 | // is 0 for the unquoted case, so substr() is called with a len |
| 184 | // parameter of npos (-1) which according to the documentation |
| 185 | // indicates to use all characters until the end of the string. |
| 186 | version = |
| 187 | versionValue.substr(pos, versionValue.find_last_of('"') - pos); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } |
| 191 | efile.close(); |
| 192 | |
| 193 | if (version.empty()) |
| 194 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 195 | error("BMC current version is empty"); |
Gunnar Mills | aae1b2b | 2017-10-06 13:42:39 -0500 | [diff] [blame] | 196 | elog<InternalFailure>(); |
Saqib Khan | ba23988 | 2017-05-26 08:41:54 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return version; |
| 200 | } |
| 201 | |
Eddie James | 6d87371 | 2017-09-01 11:29:07 -0500 | [diff] [blame] | 202 | bool Version::isFunctional() |
| 203 | { |
| 204 | return versionStr == getBMCVersion(OS_RELEASE_FILE); |
| 205 | } |
| 206 | |
Saqib Khan | ee13e83 | 2017-10-23 12:53:11 -0500 | [diff] [blame] | 207 | void Delete::delete_() |
| 208 | { |
| 209 | if (parent.eraseCallback) |
| 210 | { |
| 211 | parent.eraseCallback(parent.getId(parent.version())); |
| 212 | } |
| 213 | } |
| 214 | |
Gunnar Mills | 392f294 | 2017-04-12 11:04:37 -0500 | [diff] [blame] | 215 | } // namespace manager |
| 216 | } // namespace software |
Gunnar Mills | fa34e02 | 2018-09-04 10:05:45 -0500 | [diff] [blame] | 217 | } // namespace phosphor |