blob: bb361f738c37c52332066fbe73c60647ac3a1525 [file] [log] [blame]
Saqib Khan35e83f32017-05-22 11:37:32 -05001#include <fstream>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05002#include <string>
Gunnar Mills2ce7da22017-05-04 15:37:56 -05003#include <phosphor-logging/log.hpp>
Saqib Khandcbfa042017-09-18 13:08:39 -05004#include <phosphor-logging/elog.hpp>
5#include <elog-errors.hpp>
6#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -05007#include "config.h"
Gunnar Mills2ce7da22017-05-04 15:37:56 -05008#include "item_updater.hpp"
9#include "xyz/openbmc_project/Software/Version/server.hpp"
Saqib Khan35e83f32017-05-22 11:37:32 -050010#include <experimental/filesystem>
Saqib Khan705f1bf2017-06-09 23:58:38 -050011#include "version.hpp"
Saqib Khan5d532672017-08-09 10:44:50 -050012#include "serialize.hpp"
Gunnar Millsec1b41c2017-05-02 12:20:36 -050013
14namespace phosphor
15{
16namespace software
17{
18namespace updater
19{
20
Gunnar Mills2ce7da22017-05-04 15:37:56 -050021// When you see server:: you know we're referencing our base class
22namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050023namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050024
25using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050026using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Saqib Khan35e83f32017-05-22 11:37:32 -050027namespace fs = std::experimental::filesystem;
28
Gunnar Mills9a782242017-08-22 16:23:15 -050029const std::vector<std::string> bmcImages = { "image-kernel",
30 "image-rofs",
31 "image-rwfs",
32 "image-u-boot" };
Gunnar Mills2ce7da22017-05-04 15:37:56 -050033
Patrick Williamse75d10f2017-05-30 16:56:32 -050034void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050035{
Saqib Khan84a0e692017-06-28 17:27:01 -050036
37 using SVersion = server::Version;
38 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050039 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050040 namespace mesg = sdbusplus::message;
41 namespace variant_ns = mesg::variant_ns;
42
43 mesg::object_path objPath;
44 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050045 std::string version;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050046 std::map<std::string,
Patrick Williamse75d10f2017-05-30 16:56:32 -050047 std::map<std::string,
Saqib Khan84a0e692017-06-28 17:27:01 -050048 mesg::variant<std::string>>> interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050049 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050050 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050051 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050052
53 for (const auto& intf : interfaces)
54 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050055 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050056 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050057 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050058 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050059 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050060 {
Saqib Khan84a0e692017-06-28 17:27:01 -050061 auto value = SVersion::convertVersionPurposeFromString(
Gunnar Mills9a782242017-08-22 16:23:15 -050062 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050063 if (value == VersionPurpose::BMC ||
64 value == VersionPurpose::System)
65 {
66 purpose = value;
67 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050068 }
69 else if (property.first == "Version")
70 {
Saqib Khan84a0e692017-06-28 17:27:01 -050071 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050072 }
73 }
74 }
Saqib Khan19177d32017-06-20 08:11:49 -050075 else if (intf.first == FILEPATH_IFACE)
76 {
77 for (const auto& property : intf.second)
78 {
79 if (property.first == "Path")
80 {
Saqib Khan84a0e692017-06-28 17:27:01 -050081 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050082 }
83 }
84 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050085 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050086 if (version.empty() ||
Saqib Khan19177d32017-06-20 08:11:49 -050087 filePath.empty() ||
Saqib Khan84a0e692017-06-28 17:27:01 -050088 purpose == VersionPurpose::Unknown)
Saqib Khan705f1bf2017-06-09 23:58:38 -050089 {
90 return;
91 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050092
93 // Version id is the last item in the path
94 auto pos = path.rfind("/");
95 if (pos == std::string::npos)
96 {
97 log<level::ERR>("No version id found in object path",
98 entry("OBJPATH=%s", path));
Patrick Williamse75d10f2017-05-30 16:56:32 -050099 return;
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500100 }
101
102 auto versionId = path.substr(pos + 1);
103
Patrick Williamse75d10f2017-05-30 16:56:32 -0500104 if (activations.find(versionId) == activations.end())
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500105 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500106 // Determine the Activation state by processing the given image dir.
107 auto activationState = server::Activation::Activations::Invalid;
Gunnar Mills9a782242017-08-22 16:23:15 -0500108 ItemUpdater::ActivationStatus result =
109 ItemUpdater::validateSquashFSImage(filePath);
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500110 AssociationList associations = {};
111
Saqib Khan35e83f32017-05-22 11:37:32 -0500112 if (result == ItemUpdater::ActivationStatus::ready)
113 {
114 activationState = server::Activation::Activations::Ready;
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500115 // Create an association to the BMC inventory item
116 associations.emplace_back(std::make_tuple(
117 ACTIVATION_FWD_ASSOCIATION,
118 ACTIVATION_REV_ASSOCIATION,
119 bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500120 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500121
Saqib Khan35e83f32017-05-22 11:37:32 -0500122 activations.insert(std::make_pair(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500123 versionId,
124 std::make_unique<Activation>(
125 bus,
126 path,
Saqib Khan4c1aec02017-07-06 11:46:13 -0500127 *this,
Saqib Khan35e83f32017-05-22 11:37:32 -0500128 versionId,
Gunnar Millsb60add12017-08-24 16:41:42 -0500129 activationState,
130 associations)));
Saqib Khan705f1bf2017-06-09 23:58:38 -0500131 versions.insert(std::make_pair(
132 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500133 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500134 bus,
135 path,
136 version,
137 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500138 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500139 }
Saqib Khan7b5010f2017-08-09 10:03:11 -0500140 else
141 {
142 log<level::INFO>("Software Object with the same version already exists",
Gunnar Mills9a782242017-08-22 16:23:15 -0500143 entry("VERSION_ID=%s", versionId));
Saqib Khan7b5010f2017-08-09 10:03:11 -0500144 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500145 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500146}
147
Saqib Khanba239882017-05-26 08:41:54 -0500148void ItemUpdater::processBMCImage()
149{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500150 using VersionClass = phosphor::software::manager::Version;
151 // Read os-release from /etc/ to get the functional BMC version
152 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
153
Saqib Khan1eef62d2017-08-10 15:29:34 -0500154 // Read os-release from folders under /media/ to get
155 // BMC Software Versions.
156 for(const auto& iter : fs::directory_iterator(MEDIA_DIR))
157 {
158 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500159 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500160
161 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
162 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500163 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500164 {
Saqib Khan021c3652017-09-26 12:11:02 -0500165 // The versionId is extracted from the path
166 // for example /media/ro-2a1022fe.
167 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500168 auto osRelease = iter.path() / OS_RELEASE_FILE;
169 if (!fs::is_regular_file(osRelease))
170 {
171 log<level::ERR>("Failed to read osRelease\n",
172 entry("FileName=%s", osRelease.string()));
Saqib Khan021c3652017-09-26 12:11:02 -0500173 ItemUpdater::erase(id);
174 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500175 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500176 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500177 if (version.empty())
178 {
179 log<level::ERR>("Failed to read version from osRelease",
180 entry("FILENAME=%s", osRelease.string()));
181 activationState = server::Activation::Activations::Invalid;
182 }
Saqib Khan021c3652017-09-26 12:11:02 -0500183
Saqib Khan1eef62d2017-08-10 15:29:34 -0500184 auto purpose = server::Version::VersionPurpose::BMC;
185 auto path = fs::path(SOFTWARE_OBJPATH) / id;
186
Gunnar Mills88e8a322017-09-13 11:09:28 -0500187 // Create functional association if this is the functional version
188 if (version.compare(functionalVersion) == 0)
189 {
190 createFunctionalAssociation(path);
191 }
192
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500193 AssociationList associations = {};
194
195 if (activationState == server::Activation::Activations::Active)
196 {
197 // Create an association to the BMC inventory item
198 associations.emplace_back(std::make_tuple(
199 ACTIVATION_FWD_ASSOCIATION,
200 ACTIVATION_REV_ASSOCIATION,
201 bmcInventoryPath));
202
203 // Create an active association since this image is active
204 createActiveAssociation(path);
205 }
206
Adriana Kobylakee590c72017-09-26 15:16:06 -0500207 // Create Version instance for this version.
208 auto versionPtr = std::make_unique<VersionClass>(
209 bus,
210 path,
211 version,
212 purpose,
213 "");
214 auto isVersionFunctional = versionPtr->isFunctional();
215 versions.insert(std::make_pair(
216 id,
217 std::move(versionPtr)));
218
Saqib Khan1eef62d2017-08-10 15:29:34 -0500219 // Create Activation instance for this version.
220 activations.insert(std::make_pair(
221 id,
222 std::make_unique<Activation>(
223 bus,
224 path,
225 *this,
226 id,
227 server::Activation::Activations::Active,
228 associations)));
229
230 // If Active, create RedundancyPriority instance for this version.
231 if (activationState == server::Activation::Activations::Active)
232 {
233 uint8_t priority = std::numeric_limits<uint8_t>::max();
234 if (!restoreFromFile(id, priority))
235 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500236 if (isVersionFunctional)
237 {
238 priority = 0;
239 }
240 else
241 {
242 log<level::ERR>("Unable to restore priority from file.",
243 entry("VERSIONID=%s", id));
244 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500245 }
246 activations.find(id)->second->redundancyPriority =
247 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500248 bus,
249 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500250 *(activations.find(id)->second),
251 priority);
252 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500253 }
254 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500255
256 // If there is no ubi volume for bmc version then read the /etc/os-release
257 // and create rofs-<versionId> under /media
258 if (activations.size() == 0)
259 {
260 auto version =
261 phosphor::software::manager::Version::
262 getBMCVersion(OS_RELEASE_FILE);
263 auto id = phosphor::software::manager::Version::getId(version);
264 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
265 try
266 {
267 if(!fs::is_directory(versionFileDir))
268 {
269 fs::create_directories(versionFileDir);
270 }
271 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
272 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
273 ItemUpdater::processBMCImage();
274 }
275 catch (const std::exception& e)
276 {
277 log<level::ERR>(e.what());
278 }
279 }
Saqib Khanba239882017-05-26 08:41:54 -0500280 return;
281}
282
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500283void ItemUpdater::erase(std::string entryId)
284{
Eddie James6d873712017-09-01 11:29:07 -0500285 // Find entry in versions map
286 auto it = versions.find(entryId);
287 if (it != versions.end())
288 {
289 if (it->second->isFunctional())
290 {
291 log<level::ERR>(("Error: Version " + entryId + \
292 " is currently running on the BMC." \
293 " Unable to remove.").c_str());
294 return;
295 }
296
297 // Delete ReadOnly partitions if it's not active
298 removeReadOnlyPartition(entryId);
299 removeFile(entryId);
300 }
301 else
302 {
303 // Delete ReadOnly partitions even if we can't find the version
304 removeReadOnlyPartition(entryId);
305 removeFile(entryId);
306
307 log<level::ERR>(("Error: Failed to find version " + entryId + \
308 " in item updater versions map." \
309 " Unable to remove.").c_str());
310 return;
311 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500312
313 // Remove the priority environment variable.
314 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
315 auto method = bus.new_method_call(
316 SYSTEMD_BUSNAME,
317 SYSTEMD_PATH,
318 SYSTEMD_INTERFACE,
319 "StartUnit");
320 method.append(serviceFile, "replace");
321 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500322
323 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500324 this->versions.erase(entryId);
325
326 // Removing entry in activations map
327 auto ita = activations.find(entryId);
328 if (ita == activations.end())
329 {
330 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500331 " in item updater activations map." \
332 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500333 return;
334 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500335
336 this->activations.erase(entryId);
337}
338
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500339void ItemUpdater::deleteAll()
340{
341 std::vector<std::string> deletableVersions;
342
343 for (const auto& versionIt : versions)
344 {
345 if (!versionIt.second->isFunctional())
346 {
347 deletableVersions.push_back(versionIt.first);
348 }
349 }
350
351 for (const auto& deletableIt : deletableVersions)
352 {
353 ItemUpdater::erase(deletableIt);
354 }
355
356 // Remove any volumes that do not match current versions.
357 auto method = bus.new_method_call(
358 SYSTEMD_BUSNAME,
359 SYSTEMD_PATH,
360 SYSTEMD_INTERFACE,
361 "StartUnit");
362 method.append("obmc-flash-bmc-cleanup.service", "replace");
363 bus.call_noreply(method);
364}
365
Saqib Khan35e83f32017-05-22 11:37:32 -0500366ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500367 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500368{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500369 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500370
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500371 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500372 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500373 fs::path file(filePath);
374 file /= bmcImage;
375 std::ifstream efile(file.c_str());
376 if (efile.good() != 1)
377 {
378 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500379 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500380 invalid = true;
381 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500382 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500383
384 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500385 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500386 return ItemUpdater::ActivationStatus::invalid;
387 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500388
389 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500390}
391
Saqib Khanb9da6632017-09-13 09:48:37 -0500392void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500393{
394 //TODO openbmc/openbmc#1896 Improve the performance of this function
395 for (const auto& intf : activations)
396 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500397 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500398 {
Saqib Khanb9da6632017-09-13 09:48:37 -0500399 if (intf.second->redundancyPriority.get()->priority() == value &&
400 intf.second->versionId != versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500401 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500402 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500403 }
404 }
405 }
406}
407
Michael Tritz37a59042017-07-12 13:44:53 -0500408void ItemUpdater::reset()
409{
410 // Mark the read-write partition for recreation upon reboot.
411 auto method = bus.new_method_call(
412 SYSTEMD_BUSNAME,
413 SYSTEMD_PATH,
414 SYSTEMD_INTERFACE,
415 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500416 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500417 bus.call_noreply(method);
418
419 log<level::INFO>("BMC factory reset will take effect upon reboot.");
420
421 return;
422}
423
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500424void ItemUpdater::removeReadOnlyPartition(std::string versionId)
425{
426 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
427 ".service";
428
429 // Remove the read-only partitions.
430 auto method = bus.new_method_call(
431 SYSTEMD_BUSNAME,
432 SYSTEMD_PATH,
433 SYSTEMD_INTERFACE,
434 "StartUnit");
435 method.append(serviceFile, "replace");
436 bus.call_noreply(method);
437}
438
Michael Tritz0129d922017-08-10 19:33:46 -0500439bool ItemUpdater::fieldModeEnabled(bool value)
440{
441 // enabling field mode is intended to be one way: false -> true
442 if (value && !control::FieldMode::fieldModeEnabled())
443 {
444 control::FieldMode::fieldModeEnabled(value);
445
446 auto method = bus.new_method_call(
447 SYSTEMD_BUSNAME,
448 SYSTEMD_PATH,
449 SYSTEMD_INTERFACE,
450 "StartUnit");
451 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500452 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500453 bus.call_noreply(method);
454
455 method = bus.new_method_call(
456 SYSTEMD_BUSNAME,
457 SYSTEMD_PATH,
458 SYSTEMD_INTERFACE,
459 "StopUnit");
460 method.append("usr-local.mount", "replace");
461 bus.call_noreply(method);
462
463 std::vector<std::string> usrLocal = {"usr-local.mount"};
464
465 method = bus.new_method_call(
466 SYSTEMD_BUSNAME,
467 SYSTEMD_PATH,
468 SYSTEMD_INTERFACE,
469 "MaskUnitFiles");
470 method.append(usrLocal, false, true);
471 bus.call_noreply(method);
472 }
473
474 return control::FieldMode::fieldModeEnabled();
475}
476
477void ItemUpdater::restoreFieldModeStatus()
478{
479 std::ifstream input("/run/fw_env");
480 std::string envVar;
481 std::getline(input, envVar);
482
Gunnar Mills9a782242017-08-22 16:23:15 -0500483 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500484 {
485 ItemUpdater::fieldModeEnabled(true);
486 }
487}
488
Gunnar Millsb60add12017-08-24 16:41:42 -0500489void ItemUpdater::setBMCInventoryPath()
490{
491 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
492 // that implement the BMC inventory interface
493 auto depth = 0;
494 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
495 MAPPER_PATH,
496 MAPPER_INTERFACE,
497 "GetSubTreePaths");
498
499 mapperCall.append(CHASSIS_INVENTORY_PATH);
500 mapperCall.append(depth);
501
502 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
503 // mapper is fixed.
504 std::vector<std::string> filter = {};
505 mapperCall.append(filter);
506
507 auto response = bus.call(mapperCall);
508 if (response.is_method_error())
509 {
510 log<level::ERR>("Error in mapper GetSubTreePath");
511 return;
512 }
513
514 using ObjectPaths = std::vector<std::string>;
515 ObjectPaths result;
516 response.read(result);
517
518 if (result.empty())
519 {
520 log<level::ERR>("Invalid response from mapper");
521 return;
522 }
523
524 for (auto& iter : result)
525 {
526 const auto& path = iter;
527 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
528 {
529 bmcInventoryPath = path;
530 return;
531 }
532 }
533}
534
Gunnar Millsf10b2322017-09-21 15:31:55 -0500535void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500536{
537 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
538 ACTIVE_REV_ASSOCIATION,
539 path));
540 associations(assocs);
541}
542
Gunnar Mills88e8a322017-09-13 11:09:28 -0500543void ItemUpdater::createFunctionalAssociation(const std::string& path)
544{
545 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
546 FUNCTIONAL_REV_ASSOCIATION,
547 path));
548 associations(assocs);
549}
550
Gunnar Millsf10b2322017-09-21 15:31:55 -0500551void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500552{
553 for (auto iter = assocs.begin(); iter != assocs.end();)
554 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500555 // Since there could be multiple associations to the same path,
556 // only remove ones that have an active forward association.
557 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
558 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500559 {
560 iter = assocs.erase(iter);
561 associations(assocs);
562 }
563 else
564 {
565 ++iter;
566 }
567 }
568}
569
Saqib Khanb9da6632017-09-13 09:48:37 -0500570bool ItemUpdater::isLowestPriority(uint8_t value)
571{
572 for (const auto& intf : activations)
573 {
574 if(intf.second->redundancyPriority)
575 {
576 if (intf.second->redundancyPriority.get()->priority() < value)
577 {
578 return false;
579 }
580 }
581 }
582 return true;
583}
584
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500585} // namespace updater
586} // namespace software
587} // namespace phosphor