blob: adaaeb9dfa9f154016abe78b383985e63b39aafc [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
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 Mills392f2942017-04-12 11:04:37 -050017namespace phosphor
18{
19namespace software
20{
21namespace manager
22{
23
24using namespace phosphor::logging;
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050025using Argument = xyz::openbmc_project::Common::InvalidArgument;
26using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Mills392f2942017-04-12 11:04:37 -050027
Gunnar Millscebd1022017-04-17 16:10:15 -050028std::string Version::getValue(const std::string& manifestFilePath,
29 std::string key)
Gunnar Mills392f2942017-04-12 11:04:37 -050030{
Gunnar Millscebd1022017-04-17 16:10:15 -050031 key = key + "=";
32 auto keySize = key.length();
Gunnar Mills392f2942017-04-12 11:04:37 -050033
34 if (manifestFilePath.empty())
35 {
36 log<level::ERR>("Error MANIFESTFilePath is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050037 elog<InvalidArgument>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060038 Argument::ARGUMENT_NAME("manifestFilePath"),
39 Argument::ARGUMENT_VALUE(manifestFilePath.c_str()));
Gunnar Mills392f2942017-04-12 11:04:37 -050040 }
41
Gunnar Millscebd1022017-04-17 16:10:15 -050042 std::string value{};
Gunnar Mills392f2942017-04-12 11:04:37 -050043 std::ifstream efile;
44 std::string line;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060045 efile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050046 std::ifstream::eofbit);
Gunnar Mills392f2942017-04-12 11:04:37 -050047
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 Millscebd1022017-04-17 16:10:15 -050054 if (line.compare(0, keySize, key) == 0)
Gunnar Mills392f2942017-04-12 11:04:37 -050055 {
Gunnar Millscebd1022017-04-17 16:10:15 -050056 value = line.substr(keySize);
Gunnar Mills392f2942017-04-12 11:04:37 -050057 break;
58 }
59 }
60 efile.close();
61 }
62 catch (const std::exception& e)
63 {
Gunnar Millscebd1022017-04-17 16:10:15 -050064 log<level::ERR>("Error in reading MANIFEST file");
Gunnar Mills392f2942017-04-12 11:04:37 -050065 }
66
Gunnar Millscebd1022017-04-17 16:10:15 -050067 return value;
Gunnar Mills392f2942017-04-12 11:04:37 -050068}
69
70std::string Version::getId(const std::string& version)
71{
Gunnar Mills392f2942017-04-12 11:04:37 -050072
73 if (version.empty())
74 {
Gunnar Millscebd1022017-04-17 16:10:15 -050075 log<level::ERR>("Error version is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -050076 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Version"),
77 Argument::ARGUMENT_VALUE(version.c_str()));
Gunnar Mills392f2942017-04-12 11:04:37 -050078 }
79
Saqib Khan26a960d2017-09-19 14:23:28 -050080 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 Kobylak2285fe02018-02-27 15:36:59 -060085 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
Saqib Khan26a960d2017-09-19 14:23:28 -050086 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
87 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -060088 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan26a960d2017-09-19 14:23:28 -050089 }
90
91 // Only need 8 hex digits.
92 std::string hexId = std::string(mdString);
93 return (hexId.substr(0, 8));
Gunnar Mills392f2942017-04-12 11:04:37 -050094}
95
Vijay Khemkab7c062e2019-09-18 17:15:57 -070096std::string Version::getBMCMachine(const std::string& releaseFilePath)
97{
98 std::string machineKey = "OPENBMC_TARGET_MACHINE=";
99 std::string machine{};
100 std::ifstream efile(releaseFilePath);
101 std::string line;
102
103 while (getline(efile, line))
104 {
105 if (line.substr(0, machineKey.size()).find(machineKey) !=
106 std::string::npos)
107 {
108 std::size_t pos = line.find_first_of('"') + 1;
109 machine = line.substr(pos, line.find_last_of('"') - pos);
110 break;
111 }
112 }
113
114 if (machine.empty())
115 {
116 log<level::ERR>("Unable to find OPENBMC_TARGET_MACHINE");
117 elog<InternalFailure>();
118 }
119
120 return machine;
121}
122
Saqib Khan1eef62d2017-08-10 15:29:34 -0500123std::string Version::getBMCVersion(const std::string& releaseFilePath)
Saqib Khanba239882017-05-26 08:41:54 -0500124{
125 std::string versionKey = "VERSION_ID=";
126 std::string version{};
127 std::ifstream efile;
128 std::string line;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500129 efile.open(releaseFilePath);
Saqib Khanba239882017-05-26 08:41:54 -0500130
131 while (getline(efile, line))
132 {
133 if (line.substr(0, versionKey.size()).find(versionKey) !=
134 std::string::npos)
135 {
136 std::size_t pos = line.find_first_of('"') + 1;
137 version = line.substr(pos, line.find_last_of('"') - pos);
138 break;
139 }
140 }
141 efile.close();
142
143 if (version.empty())
144 {
145 log<level::ERR>("Error BMC current version is empty");
Gunnar Millsaae1b2b2017-10-06 13:42:39 -0500146 elog<InternalFailure>();
Saqib Khanba239882017-05-26 08:41:54 -0500147 }
148
149 return version;
150}
151
Eddie James6d873712017-09-01 11:29:07 -0500152bool Version::isFunctional()
153{
154 return versionStr == getBMCVersion(OS_RELEASE_FILE);
155}
156
Saqib Khanee13e832017-10-23 12:53:11 -0500157void Delete::delete_()
158{
159 if (parent.eraseCallback)
160 {
161 parent.eraseCallback(parent.getId(parent.version()));
162 }
163}
164
Gunnar Mills392f2942017-04-12 11:04:37 -0500165} // namespace manager
166} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500167} // namespace phosphor