1 #include "version.hpp"
2 
3 #include "item_updater.hpp"
4 #include "xyz/openbmc_project/Common/error.hpp"
5 
6 #include <openssl/evp.h>
7 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/log.hpp>
10 
11 #include <fstream>
12 #include <iostream>
13 #include <sstream>
14 #include <stdexcept>
15 #include <string>
16 
17 namespace openpower
18 {
19 namespace software
20 {
21 namespace updater
22 {
23 
24 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25 using namespace phosphor::logging;
26 using Argument = xyz::openbmc_project::Common::InvalidArgument;
27 
28 using EVP_MD_CTX_Ptr =
29     std::unique_ptr<EVP_MD_CTX, decltype(&::EVP_MD_CTX_free)>;
30 
getId(const std::string & version)31 std::string Version::getId(const std::string& version)
32 {
33     if (version.empty())
34     {
35         log<level::ERR>("Error version is empty");
36         return {};
37     }
38 
39     std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
40     EVP_MD_CTX_Ptr ctx(EVP_MD_CTX_new(), &::EVP_MD_CTX_free);
41 
42     EVP_DigestInit(ctx.get(), EVP_sha512());
43     EVP_DigestUpdate(ctx.get(), version.c_str(), strlen(version.c_str()));
44     EVP_DigestFinal(ctx.get(), digest.data(), nullptr);
45 
46     // We are only using the first 8 characters.
47     char mdString[9];
48     snprintf(mdString, sizeof(mdString), "%02x%02x%02x%02x",
49              (unsigned int)digest[0], (unsigned int)digest[1],
50              (unsigned int)digest[2], (unsigned int)digest[3]);
51 
52     return mdString;
53 }
54 
getValue(const std::string & filePath,std::map<std::string,std::string> keys)55 std::map<std::string, std::string> Version::getValue(
56     const std::string& filePath, std::map<std::string, std::string> keys)
57 {
58     if (filePath.empty())
59     {
60         log<level::ERR>("Error filePath is empty");
61         elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
62                               Argument::ARGUMENT_VALUE(filePath.c_str()));
63     }
64 
65     std::ifstream efile;
66     std::string line;
67     efile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
68                      std::ifstream::eofbit);
69 
70     try
71     {
72         efile.open(filePath);
73         while (getline(efile, line))
74         {
75             for (auto& key : keys)
76             {
77                 auto value = key.first + "=";
78                 auto keySize = value.length();
79                 if (line.compare(0, keySize, value) == 0)
80                 {
81                     key.second = line.substr(keySize);
82                     break;
83                 }
84             }
85         }
86         efile.close();
87     }
88     catch (const std::exception& e)
89     {
90         if (!efile.eof())
91         {
92             log<level::ERR>("Error in reading file");
93         }
94         efile.close();
95     }
96 
97     return keys;
98 }
99 
100 std::pair<std::string, std::string>
getVersions(const std::string & versionPart)101     Version::getVersions(const std::string& versionPart)
102 {
103     // versionPart contains strings like below:
104     // open-power-romulus-v2.2-rc1-48-g268344f-dirty
105     //     buildroot-2018.11.1-7-g5d7cc8c
106     //     skiboot-v6.2
107     std::istringstream iss(versionPart);
108     std::string line;
109     std::string version;
110     std::stringstream ss;
111     std::string extendedVersion;
112 
113     if (!std::getline(iss, line))
114     {
115         log<level::ERR>("Unable to read from version",
116                         entry("VERSION=%s", versionPart.c_str()));
117         return {};
118     }
119     version = line;
120 
121     while (std::getline(iss, line))
122     {
123         // Each line starts with a tab, let's trim it
124         line.erase(line.begin(),
125                    std::find_if(line.begin(), line.end(),
126                                 [](int c) { return !std::isspace(c); }));
127         ss << line << ',';
128     }
129     extendedVersion = ss.str();
130 
131     // Erase the last ',', if there is one
132     if (!extendedVersion.empty())
133     {
134         extendedVersion.pop_back();
135     }
136     return {version, extendedVersion};
137 }
138 
delete_()139 void Delete::delete_()
140 {
141     if (parent.eraseCallback)
142     {
143         parent.eraseCallback(parent.getId(parent.version()));
144     }
145 }
146 
updateDeleteInterface(sdbusplus::message_t & msg)147 void Version::updateDeleteInterface(sdbusplus::message_t& msg)
148 {
149     std::string interface, chassisState;
150     std::map<std::string, std::variant<std::string>> properties;
151 
152     msg.read(interface, properties);
153 
154     for (const auto& p : properties)
155     {
156         if (p.first == "CurrentPowerState")
157         {
158             chassisState = std::get<std::string>(p.second);
159         }
160     }
161     if (chassisState.empty())
162     {
163         // The chassis power state property did not change, return.
164         return;
165     }
166 
167     if ((parent.isVersionFunctional(this->versionId)) &&
168         (chassisState != CHASSIS_STATE_OFF))
169     {
170         if (deleteObject)
171         {
172             deleteObject.reset(nullptr);
173         }
174     }
175     else
176     {
177         if (!deleteObject)
178         {
179             deleteObject = std::make_unique<Delete>(bus, objPath, *this);
180         }
181     }
182 }
183 
184 } // namespace updater
185 } // namespace software
186 } // namespace openpower
187