blob: b57650a93726e94cdc1e9b072c17ae23e9e6a80f [file] [log] [blame]
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05001#include "config.h"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05002
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05003#include "item_updater.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05004
Gunnar Millsf6ed5892018-09-07 17:08:02 -05005#include "xyz/openbmc_project/Common/error.hpp"
6
Gunnar Millsf6ed5892018-09-07 17:08:02 -05007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/log.hpp>
Adriana Kobylak2d8fa222017-03-15 12:34:32 -05009
Brad Bishop8facccf2020-11-04 09:44:58 -050010#include <filesystem>
11
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050012namespace openpower
13{
14namespace software
15{
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -050016namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -050017{
Lei YUa9ac9272019-02-22 16:38:35 +080018namespace server = sdbusplus::xyz::openbmc_project::Software::server;
19namespace fs = std::filesystem;
20
Eddie James13fc66a2017-08-31 15:36:44 -050021using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Adriana Kobylakb66ac3a2017-03-28 13:33:20 -050022using namespace phosphor::logging;
23
Lei YUa9ac9272019-02-22 16:38:35 +080024void ItemUpdater::createActivation(sdbusplus::message::message& m)
25{
26 using SVersion = server::Version;
27 using VersionPurpose = SVersion::VersionPurpose;
Lei YUa9ac9272019-02-22 16:38:35 +080028
29 sdbusplus::message::object_path objPath;
Patrick Williams573552a2020-06-05 16:21:05 -050030 std::map<std::string, std::map<std::string, std::variant<std::string>>>
Lei YUa9ac9272019-02-22 16:38:35 +080031 interfaces;
32 m.read(objPath, interfaces);
33
34 std::string path(std::move(objPath));
35 std::string filePath;
36 auto purpose = VersionPurpose::Unknown;
37 std::string version;
38
39 for (const auto& intf : interfaces)
40 {
41 if (intf.first == VERSION_IFACE)
42 {
43 for (const auto& property : intf.second)
44 {
45 if (property.first == "Purpose")
46 {
47 // Only process the Host and System images
48 auto value = SVersion::convertVersionPurposeFromString(
Patrick Williams550f31b2020-05-13 11:15:24 -050049 std::get<std::string>(property.second));
Lei YUa9ac9272019-02-22 16:38:35 +080050
51 if (value == VersionPurpose::Host ||
52 value == VersionPurpose::System)
53 {
54 purpose = value;
55 }
56 }
57 else if (property.first == "Version")
58 {
Patrick Williams550f31b2020-05-13 11:15:24 -050059 version = std::get<std::string>(property.second);
Lei YUa9ac9272019-02-22 16:38:35 +080060 }
61 }
62 }
63 else if (intf.first == FILEPATH_IFACE)
64 {
65 for (const auto& property : intf.second)
66 {
67 if (property.first == "Path")
68 {
Patrick Williams550f31b2020-05-13 11:15:24 -050069 filePath = std::get<std::string>(property.second);
Lei YUa9ac9272019-02-22 16:38:35 +080070 }
71 }
72 }
73 }
74 if ((filePath.empty()) || (purpose == VersionPurpose::Unknown))
75 {
76 return;
77 }
78
79 // Version id is the last item in the path
80 auto pos = path.rfind("/");
81 if (pos == std::string::npos)
82 {
83 log<level::ERR>("No version id found in object path",
84 entry("OBJPATH=%s", path.c_str()));
85 return;
86 }
87
88 auto versionId = path.substr(pos + 1);
89
90 if (activations.find(versionId) == activations.end())
91 {
92 // Determine the Activation state by processing the given image dir.
93 auto activationState = server::Activation::Activations::Invalid;
94 AssociationList associations = {};
95 if (validateImage(filePath))
96 {
97 activationState = server::Activation::Activations::Ready;
98 // Create an association to the host inventory item
99 associations.emplace_back(std::make_tuple(
100 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
101 HOST_INVENTORY_PATH));
102 }
103
104 fs::path manifestPath(filePath);
105 manifestPath /= MANIFEST_FILE;
106 std::string extendedVersion =
107 (Version::getValue(
108 manifestPath.string(),
109 std::map<std::string, std::string>{{"extended_version", ""}}))
110 .begin()
111 ->second;
112
113 auto activation = createActivationObject(
114 path, versionId, extendedVersion, activationState, associations);
115 activations.emplace(versionId, std::move(activation));
116
117 auto versionPtr =
118 createVersionObject(path, versionId, version, purpose, filePath);
119 versions.emplace(versionId, std::move(versionPtr));
120 }
121 return;
122}
123
Lei YUf3ce4332019-02-21 14:09:49 +0800124void ItemUpdater::createActiveAssociation(const std::string& path)
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500125{
Lei YUf3ce4332019-02-21 14:09:49 +0800126 assocs.emplace_back(
127 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
128 associations(assocs);
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500129}
130
Adriana Kobylak3c810372020-07-15 16:47:03 -0500131void ItemUpdater::createUpdateableAssociation(const std::string& path)
132{
133 assocs.emplace_back(std::make_tuple(UPDATEABLE_FWD_ASSOCIATION,
134 UPDATEABLE_REV_ASSOCIATION, path));
135 associations(assocs);
136}
137
Lei YUf3ce4332019-02-21 14:09:49 +0800138void ItemUpdater::updateFunctionalAssociation(const std::string& versionId)
Saqib Khan7254f0e2017-04-10 21:45:37 -0500139{
Lei YUf3ce4332019-02-21 14:09:49 +0800140 std::string path = std::string{SOFTWARE_OBJPATH} + '/' + versionId;
141 // remove all functional associations
142 for (auto iter = assocs.begin(); iter != assocs.end();)
Saqib Khan4c5d7442017-07-18 00:43:52 -0500143 {
Lei YUf3ce4332019-02-21 14:09:49 +0800144 if ((std::get<0>(*iter)).compare(FUNCTIONAL_FWD_ASSOCIATION) == 0)
Saqib Khan4c5d7442017-07-18 00:43:52 -0500145 {
Lei YUf3ce4332019-02-21 14:09:49 +0800146 iter = assocs.erase(iter);
Saqib Khan4c5d7442017-07-18 00:43:52 -0500147 }
Lei YUf3ce4332019-02-21 14:09:49 +0800148 else
Saqib Khan2be9ba92017-09-26 22:44:10 -0500149 {
Lei YUf3ce4332019-02-21 14:09:49 +0800150 ++iter;
Saqib Khan2be9ba92017-09-26 22:44:10 -0500151 }
Saqib Khan4c5d7442017-07-18 00:43:52 -0500152 }
Lei YUf3ce4332019-02-21 14:09:49 +0800153 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
154 FUNCTIONAL_REV_ASSOCIATION, path));
155 associations(assocs);
Saqib Khan7254f0e2017-04-10 21:45:37 -0500156}
157
Lei YUf3ce4332019-02-21 14:09:49 +0800158void ItemUpdater::removeAssociation(const std::string& path)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500159{
Lei YUf3ce4332019-02-21 14:09:49 +0800160 for (auto iter = assocs.begin(); iter != assocs.end();)
Saqib Khana8ade7e2017-04-12 10:27:56 -0500161 {
Lei YUf3ce4332019-02-21 14:09:49 +0800162 if ((std::get<2>(*iter)).compare(path) == 0)
163 {
164 iter = assocs.erase(iter);
165 associations(assocs);
166 }
167 else
168 {
169 ++iter;
170 }
171 }
172}
173
174bool ItemUpdater::erase(std::string entryId)
175{
176 if (isVersionFunctional(entryId) && isChassisOn())
177 {
178 log<level::ERR>(("Error: Version " + entryId +
179 " is currently active and running on the host."
180 " Unable to remove.")
181 .c_str());
182 return false;
183 }
184
185 // Removing entry in versions map
186 auto it = versions.find(entryId);
187 if (it == versions.end())
188 {
189 log<level::ERR>(("Error: Failed to find version " + entryId +
190 " in item updater versions map."
191 " Unable to remove.")
192 .c_str());
Saqib Khana8ade7e2017-04-12 10:27:56 -0500193 }
194 else
195 {
Lei YUf3ce4332019-02-21 14:09:49 +0800196 versions.erase(entryId);
Saqib Khana8ade7e2017-04-12 10:27:56 -0500197 }
Saqib Khana8ade7e2017-04-12 10:27:56 -0500198
Lei YUf3ce4332019-02-21 14:09:49 +0800199 // Removing entry in activations map
200 auto ita = activations.find(entryId);
201 if (ita == activations.end())
Adriana Kobylak0bd65052018-07-09 10:55:56 -0500202 {
Lei YUf3ce4332019-02-21 14:09:49 +0800203 log<level::ERR>(("Error: Failed to find version " + entryId +
204 " in item updater activations map."
205 " Unable to remove.")
206 .c_str());
Adriana Kobylak0bd65052018-07-09 10:55:56 -0500207 }
Lei YUf3ce4332019-02-21 14:09:49 +0800208 else
Michael Tritzbb31f022017-11-22 11:51:29 -0600209 {
Lei YUf3ce4332019-02-21 14:09:49 +0800210 removeAssociation(ita->second->path);
211 activations.erase(entryId);
Michael Tritzbb31f022017-11-22 11:51:29 -0600212 }
Eddie James13fc66a2017-08-31 15:36:44 -0500213 return true;
214}
215
216bool ItemUpdater::isChassisOn()
217{
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600218 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
219 MAPPER_INTERFACE, "GetObject");
Eddie James13fc66a2017-08-31 15:36:44 -0500220
221 mapperCall.append(CHASSIS_STATE_PATH,
222 std::vector<std::string>({CHASSIS_STATE_OBJ}));
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500223
224 std::map<std::string, std::vector<std::string>> mapperResponse;
225
226 try
227 {
228 auto mapperResponseMsg = bus.call(mapperCall);
229 mapperResponseMsg.read(mapperResponse);
230 if (mapperResponse.empty())
231 {
232 log<level::ERR>("Invalid Response from mapper");
233 elog<InternalFailure>();
234 }
235 }
Patrick Williams7b5685d2021-09-02 09:32:14 -0500236 catch (const sdbusplus::exception::exception& e)
Eddie James13fc66a2017-08-31 15:36:44 -0500237 {
238 log<level::ERR>("Error in Mapper call");
239 elog<InternalFailure>();
240 }
Eddie James13fc66a2017-08-31 15:36:44 -0500241
242 auto method = bus.new_method_call((mapperResponse.begin()->first).c_str(),
243 CHASSIS_STATE_PATH,
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600244 SYSTEMD_PROPERTY_INTERFACE, "Get");
Eddie James13fc66a2017-08-31 15:36:44 -0500245 method.append(CHASSIS_STATE_OBJ, "CurrentPowerState");
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500246
Patrick Williams212102e2020-05-13 17:50:50 -0500247 std::variant<std::string> currentChassisState;
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500248
249 try
250 {
251 auto response = bus.call(method);
252 response.read(currentChassisState);
Patrick Williams550f31b2020-05-13 11:15:24 -0500253 auto strParam = std::get<std::string>(currentChassisState);
Adriana Kobylakb8cb0cc2019-05-31 09:58:04 -0500254 return (strParam != CHASSIS_STATE_OFF);
255 }
Patrick Williams7b5685d2021-09-02 09:32:14 -0500256 catch (const sdbusplus::exception::exception& e)
Eddie James13fc66a2017-08-31 15:36:44 -0500257 {
258 log<level::ERR>("Error in fetching current Chassis State",
Gunnar Mills850d5f62017-10-19 17:04:38 -0500259 entry("MAPPERRESPONSE=%s",
Eddie James13fc66a2017-08-31 15:36:44 -0500260 (mapperResponse.begin()->first).c_str()));
261 elog<InternalFailure>();
262 }
Eddie James13fc66a2017-08-31 15:36:44 -0500263}
264
Adriana Kobylakbefe5ce2017-04-05 15:57:44 -0500265} // namespace updater
Adriana Kobylak2d8fa222017-03-15 12:34:32 -0500266} // namespace software
267} // namespace openpower