blob: 04f40c46750799019887568215128690a51360e0 [file] [log] [blame]
Eddie James6d873712017-09-01 11:29:07 -05001#include "config.h"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05002
Gunnar Mills392f2942017-04-12 11:04:37 -05003#include "version.hpp"
4
Gunnar Millsb0ce9962018-09-07 13:39:10 -05005#include "xyz/openbmc_project/Common/error.hpp"
6
7#include <openssl/sha.h>
8
Gunnar Millsb0ce9962018-09-07 13:39:10 -05009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
Adriana Kobylak58aa7502020-06-08 11:12:11 -050011
12#include <fstream>
13#include <iostream>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050014#include <sstream>
15#include <stdexcept>
16#include <string>
17
Gunnar Mills392f2942017-04-12 11:04:37 -050018namespace phosphor
19{
20namespace software
21{
22namespace manager
23{
24
25using namespace phosphor::logging;
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050026using Argument = xyz::openbmc_project::Common::InvalidArgument;
27using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Mills392f2942017-04-12 11:04:37 -050028
Gunnar Millscebd1022017-04-17 16:10:15 -050029std::string Version::getValue(const std::string& manifestFilePath,
30 std::string key)
Gunnar Mills392f2942017-04-12 11:04:37 -050031{
Gunnar Millscebd1022017-04-17 16:10:15 -050032 key = key + "=";
33 auto keySize = key.length();
Gunnar Mills392f2942017-04-12 11:04:37 -050034
35 if (manifestFilePath.empty())
36 {
37 log<level::ERR>("Error MANIFESTFilePath is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050038 elog<InvalidArgument>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060039 Argument::ARGUMENT_NAME("manifestFilePath"),
40 Argument::ARGUMENT_VALUE(manifestFilePath.c_str()));
Gunnar Mills392f2942017-04-12 11:04:37 -050041 }
42
Gunnar Millscebd1022017-04-17 16:10:15 -050043 std::string value{};
Gunnar Mills392f2942017-04-12 11:04:37 -050044 std::ifstream efile;
45 std::string line;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060046 efile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050047 std::ifstream::eofbit);
Gunnar Mills392f2942017-04-12 11:04:37 -050048
49 // Too many GCC bugs (53984, 66145) to do this the right way...
50 try
51 {
52 efile.open(manifestFilePath);
53 while (getline(efile, line))
54 {
Lei YU5a7363b2019-10-18 16:50:59 +080055 if (!line.empty() && line.back() == '\r')
56 {
57 // If the manifest has CRLF line terminators, e.g. is created on
58 // Windows, the line will contain \r at the end, remove it.
59 line.pop_back();
60 }
Gunnar Millscebd1022017-04-17 16:10:15 -050061 if (line.compare(0, keySize, key) == 0)
Gunnar Mills392f2942017-04-12 11:04:37 -050062 {
Gunnar Millscebd1022017-04-17 16:10:15 -050063 value = line.substr(keySize);
Gunnar Mills392f2942017-04-12 11:04:37 -050064 break;
65 }
66 }
67 efile.close();
68 }
69 catch (const std::exception& e)
70 {
Gunnar Millscebd1022017-04-17 16:10:15 -050071 log<level::ERR>("Error in reading MANIFEST file");
Gunnar Mills392f2942017-04-12 11:04:37 -050072 }
73
Gunnar Millscebd1022017-04-17 16:10:15 -050074 return value;
Gunnar Mills392f2942017-04-12 11:04:37 -050075}
76
77std::string Version::getId(const std::string& version)
78{
Gunnar Mills392f2942017-04-12 11:04:37 -050079
80 if (version.empty())
81 {
Gunnar Millscebd1022017-04-17 16:10:15 -050082 log<level::ERR>("Error version is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050083 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"),
84 Argument::ARGUMENT_VALUE(version.c_str()));
Gunnar Mills392f2942017-04-12 11:04:37 -050085 }
86
Saqib Khan26a960d2017-09-19 14:23:28 -050087 unsigned char digest[SHA512_DIGEST_LENGTH];
88 SHA512_CTX ctx;
89 SHA512_Init(&ctx);
90 SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
91 SHA512_Final(digest, &ctx);
Adriana Kobylak2285fe02018-02-27 15:36:59 -060092 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
Saqib Khan26a960d2017-09-19 14:23:28 -050093 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
94 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060095 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan26a960d2017-09-19 14:23:28 -050096 }
97
98 // Only need 8 hex digits.
99 std::string hexId = std::string(mdString);
100 return (hexId.substr(0, 8));
Gunnar Mills392f2942017-04-12 11:04:37 -0500101}
102
Vijay Khemkab7c062e2019-09-18 17:15:57 -0700103std::string Version::getBMCMachine(const std::string& releaseFilePath)
104{
105 std::string machineKey = "OPENBMC_TARGET_MACHINE=";
106 std::string machine{};
107 std::ifstream efile(releaseFilePath);
108 std::string line;
109
110 while (getline(efile, line))
111 {
112 if (line.substr(0, machineKey.size()).find(machineKey) !=
113 std::string::npos)
114 {
115 std::size_t pos = line.find_first_of('"') + 1;
116 machine = line.substr(pos, line.find_last_of('"') - pos);
117 break;
118 }
119 }
120
121 if (machine.empty())
122 {
123 log<level::ERR>("Unable to find OPENBMC_TARGET_MACHINE");
124 elog<InternalFailure>();
125 }
126
127 return machine;
128}
129
Chanh Nguyen1fd6ddd2021-01-06 11:09:09 +0700130std::string Version::getBMCExtendedVersion(const std::string& releaseFilePath)
131{
132 std::string extendedVersionKey = "EXTENDED_VERSION=";
133 std::string extendedVersionValue{};
134 std::string extendedVersion{};
135 std::ifstream efile(releaseFilePath);
136 std::string line;
137
138 while (getline(efile, line))
139 {
140 if (line.substr(0, extendedVersionKey.size())
141 .find(extendedVersionKey) != std::string::npos)
142 {
143 extendedVersionValue = line.substr(extendedVersionKey.size());
144 std::size_t pos = extendedVersionValue.find_first_of('"') + 1;
145 extendedVersion = extendedVersionValue.substr(
146 pos, extendedVersionValue.find_last_of('"') - pos);
147 break;
148 }
149 }
150
151 return extendedVersion;
152}
153
Saqib Khan1eef62d2017-08-10 15:29:34 -0500154std::string Version::getBMCVersion(const std::string& releaseFilePath)
Saqib Khanba239882017-05-26 08:41:54 -0500155{
156 std::string versionKey = "VERSION_ID=";
Adriana Kobylak2a3d9f52020-05-06 10:46:32 -0500157 std::string versionValue{};
Saqib Khanba239882017-05-26 08:41:54 -0500158 std::string version{};
159 std::ifstream efile;
160 std::string line;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500161 efile.open(releaseFilePath);
Saqib Khanba239882017-05-26 08:41:54 -0500162
163 while (getline(efile, line))
164 {
165 if (line.substr(0, versionKey.size()).find(versionKey) !=
166 std::string::npos)
167 {
Adriana Kobylak2a3d9f52020-05-06 10:46:32 -0500168 // Support quoted and unquoted values
169 // 1. Remove the versionKey so that we process the value only.
170 versionValue = line.substr(versionKey.size());
171
172 // 2. Look for a starting quote, then increment the position by 1 to
173 // skip the quote character. If no quote is found,
174 // find_first_of() returns npos (-1), which by adding +1 sets pos
175 // to 0 (beginning of unquoted string).
176 std::size_t pos = versionValue.find_first_of('"') + 1;
177
178 // 3. Look for ending quote, then decrease the position by pos to
179 // get the size of the string up to before the ending quote. If
180 // no quote is found, find_last_of() returns npos (-1), and pos
181 // is 0 for the unquoted case, so substr() is called with a len
182 // parameter of npos (-1) which according to the documentation
183 // indicates to use all characters until the end of the string.
184 version =
185 versionValue.substr(pos, versionValue.find_last_of('"') - pos);
Saqib Khanba239882017-05-26 08:41:54 -0500186 break;
187 }
188 }
189 efile.close();
190
191 if (version.empty())
192 {
193 log<level::ERR>("Error BMC current version is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -0500194 elog<InternalFailure>();
Saqib Khanba239882017-05-26 08:41:54 -0500195 }
196
197 return version;
198}
199
Eddie James6d873712017-09-01 11:29:07 -0500200bool Version::isFunctional()
201{
202 return versionStr == getBMCVersion(OS_RELEASE_FILE);
203}
204
Saqib Khanee13e832017-10-23 12:53:11 -0500205void Delete::delete_()
206{
207 if (parent.eraseCallback)
208 {
209 parent.eraseCallback(parent.getId(parent.version()));
210 }
211}
212
Gunnar Mills392f2942017-04-12 11:04:37 -0500213} // namespace manager
214} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500215} // namespace phosphor