blob: ec239559d24a71bfa9e2227bd3146b0624174bc2 [file] [log] [blame]
Saqib Khan167601b2017-06-18 23:33:46 -05001#include "version.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05002
Leonel Gonzalez9c8adfa2017-07-12 11:08:40 -05003#include "item_updater.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05004#include "xyz/openbmc_project/Common/error.hpp"
5
Saqib Khan2308b8b2017-09-19 15:33:06 -05006#include <openssl/sha.h>
Saqib Khan167601b2017-06-18 23:33:46 -05007
Gunnar Millsf6ed5892018-09-07 17:08:02 -05008#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/log.hpp>
Brad Bishop8facccf2020-11-04 09:44:58 -050010
11#include <fstream>
12#include <iostream>
Gunnar Millsf6ed5892018-09-07 17:08:02 -050013#include <sstream>
14#include <stdexcept>
15#include <string>
16
Saqib Khan167601b2017-06-18 23:33:46 -050017namespace openpower
18{
19namespace software
20{
21namespace updater
22{
23
24using namespace sdbusplus::xyz::openbmc_project::Common::Error;
25using namespace phosphor::logging;
Gunnar Millsa93a07b2017-09-21 15:40:09 -050026using Argument = xyz::openbmc_project::Common::InvalidArgument;
Saqib Khan167601b2017-06-18 23:33:46 -050027
28std::string Version::getId(const std::string& version)
29{
Saqib Khan167601b2017-06-18 23:33:46 -050030
31 if (version.empty())
32 {
33 log<level::ERR>("Error version is empty");
Lei YU91add6d2019-03-01 14:23:40 +080034 return {};
Saqib Khan167601b2017-06-18 23:33:46 -050035 }
36
Saqib Khan2308b8b2017-09-19 15:33:06 -050037 unsigned char digest[SHA512_DIGEST_LENGTH];
38 SHA512_CTX ctx;
39 SHA512_Init(&ctx);
40 SHA512_Update(&ctx, version.c_str(), strlen(version.c_str()));
41 SHA512_Final(digest, &ctx);
Adriana Kobylak70dcb632018-02-27 15:46:52 -060042 char mdString[SHA512_DIGEST_LENGTH * 2 + 1];
Saqib Khan2308b8b2017-09-19 15:33:06 -050043 for (int i = 0; i < SHA512_DIGEST_LENGTH; i++)
44 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060045 snprintf(&mdString[i * 2], 3, "%02x", (unsigned int)digest[i]);
Saqib Khan2308b8b2017-09-19 15:33:06 -050046 }
47
48 // Only need 8 hex digits.
49 std::string hexId = std::string(mdString);
50 return (hexId.substr(0, 8));
Saqib Khan167601b2017-06-18 23:33:46 -050051}
52
Adriana Kobylak70dcb632018-02-27 15:46:52 -060053std::map<std::string, std::string>
54 Version::getValue(const std::string& filePath,
55 std::map<std::string, std::string> keys)
Saqib Khan167601b2017-06-18 23:33:46 -050056{
Saqib Khan167601b2017-06-18 23:33:46 -050057 if (filePath.empty())
58 {
59 log<level::ERR>("Error filePath is empty");
Gunnar Millsa93a07b2017-09-21 15:40:09 -050060 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FilePath"),
61 Argument::ARGUMENT_VALUE(filePath.c_str()));
Saqib Khan167601b2017-06-18 23:33:46 -050062 }
63
64 std::ifstream efile;
65 std::string line;
Adriana Kobylak70dcb632018-02-27 15:46:52 -060066 efile.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Gunnar Millsa93a07b2017-09-21 15:40:09 -050067 std::ifstream::eofbit);
Saqib Khan167601b2017-06-18 23:33:46 -050068
69 try
70 {
71 efile.open(filePath);
72 while (getline(efile, line))
73 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060074 for (auto& key : keys)
Saqib Khan167601b2017-06-18 23:33:46 -050075 {
76 auto value = key.first + "=";
77 auto keySize = value.length();
78 if (line.compare(0, keySize, value) == 0)
79 {
80 key.second = line.substr(keySize);
81 break;
82 }
83 }
84 }
85 efile.close();
86 }
87 catch (const std::exception& e)
88 {
Saqib Khane53222d2017-08-19 16:57:24 -050089 if (!efile.eof())
90 {
91 log<level::ERR>("Error in reading file");
92 }
93 efile.close();
Saqib Khan167601b2017-06-18 23:33:46 -050094 }
95
96 return keys;
97}
98
Lei YUdec8cf92019-02-21 17:47:05 +080099std::pair<std::string, std::string>
100 Version::getVersions(const std::string& versionPart)
101{
102 // versionPart contains strings like below:
103 // open-power-romulus-v2.2-rc1-48-g268344f-dirty
104 // buildroot-2018.11.1-7-g5d7cc8c
105 // skiboot-v6.2
106 std::istringstream iss(versionPart);
107 std::string line;
108 std::string version;
109 std::stringstream ss;
110 std::string extendedVersion;
111
112 if (!std::getline(iss, line))
113 {
114 log<level::ERR>("Unable to read from version",
115 entry("VERSION=%s", versionPart.c_str()));
116 return {};
117 }
118 version = line;
119
120 while (std::getline(iss, line))
121 {
122 // Each line starts with a tab, let's trim it
123 line.erase(line.begin(),
124 std::find_if(line.begin(), line.end(),
125 [](int c) { return !std::isspace(c); }));
126 ss << line << ',';
127 }
128 extendedVersion = ss.str();
129
130 // Erase the last ',', if there is one
131 if (!extendedVersion.empty())
132 {
133 extendedVersion.pop_back();
134 }
135 return {version, extendedVersion};
136}
137
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500138void Delete::delete_()
139{
140 if (parent.eraseCallback)
141 {
142 parent.eraseCallback(parent.getId(parent.version()));
143 }
144}
145
146void Version::updateDeleteInterface(sdbusplus::message::message& msg)
147{
148 std::string interface, chassisState;
Patrick Williams212102e2020-05-13 17:50:50 -0500149 std::map<std::string, std::variant<std::string>> properties;
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500150
151 msg.read(interface, properties);
152
153 for (const auto& p : properties)
154 {
155 if (p.first == "CurrentPowerState")
156 {
Patrick Williams550f31b2020-05-13 11:15:24 -0500157 chassisState = std::get<std::string>(p.second);
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500158 }
159 }
Adriana Kobylak90fdc702018-08-29 09:38:42 -0500160 if (chassisState.empty())
161 {
162 // The chassis power state property did not change, return.
163 return;
164 }
Saqib Khan7f80e0b2017-10-22 11:29:07 -0500165
166 if ((parent.isVersionFunctional(this->versionId)) &&
167 (chassisState != CHASSIS_STATE_OFF))
168 {
169 if (deleteObject)
170 {
171 deleteObject.reset(nullptr);
172 }
173 }
174 else
175 {
176 if (!deleteObject)
177 {
178 deleteObject = std::make_unique<Delete>(bus, objPath, *this);
179 }
180 }
181}
182
Saqib Khan167601b2017-06-18 23:33:46 -0500183} // namespace updater
184} // namespace software
185} // namespace openpower