blob: 1cafb03170cf4c65d371ac7dbbff78ce966ea744 [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.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500156 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500157 {
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 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500260 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500261 auto id = phosphor::software::manager::Version::getId(version);
262 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
263 try
264 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500265 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500266 {
267 fs::create_directories(versionFileDir);
268 }
269 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
270 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
271 ItemUpdater::processBMCImage();
272 }
273 catch (const std::exception& e)
274 {
275 log<level::ERR>(e.what());
276 }
277 }
Saqib Khanba239882017-05-26 08:41:54 -0500278 return;
279}
280
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500281void ItemUpdater::erase(std::string entryId)
282{
Eddie James6d873712017-09-01 11:29:07 -0500283 // Find entry in versions map
284 auto it = versions.find(entryId);
285 if (it != versions.end())
286 {
287 if (it->second->isFunctional())
288 {
289 log<level::ERR>(("Error: Version " + entryId + \
290 " is currently running on the BMC." \
291 " Unable to remove.").c_str());
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500292 return;
Eddie James6d873712017-09-01 11:29:07 -0500293 }
294
295 // Delete ReadOnly partitions if it's not active
296 removeReadOnlyPartition(entryId);
297 removeFile(entryId);
298 }
299 else
300 {
301 // Delete ReadOnly partitions even if we can't find the version
302 removeReadOnlyPartition(entryId);
303 removeFile(entryId);
304
305 log<level::ERR>(("Error: Failed to find version " + entryId + \
306 " in item updater versions map." \
307 " Unable to remove.").c_str());
308 return;
309 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500310
311 // Remove the priority environment variable.
312 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
313 auto method = bus.new_method_call(
314 SYSTEMD_BUSNAME,
315 SYSTEMD_PATH,
316 SYSTEMD_INTERFACE,
317 "StartUnit");
318 method.append(serviceFile, "replace");
319 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500320
321 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500322 this->versions.erase(entryId);
323
324 // Removing entry in activations map
325 auto ita = activations.find(entryId);
326 if (ita == activations.end())
327 {
328 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500329 " in item updater activations map." \
330 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500331 return;
332 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500333
334 this->activations.erase(entryId);
335}
336
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500337void ItemUpdater::deleteAll()
338{
339 std::vector<std::string> deletableVersions;
340
341 for (const auto& versionIt : versions)
342 {
343 if (!versionIt.second->isFunctional())
344 {
345 deletableVersions.push_back(versionIt.first);
346 }
347 }
348
349 for (const auto& deletableIt : deletableVersions)
350 {
351 ItemUpdater::erase(deletableIt);
352 }
353
354 // Remove any volumes that do not match current versions.
355 auto method = bus.new_method_call(
356 SYSTEMD_BUSNAME,
357 SYSTEMD_PATH,
358 SYSTEMD_INTERFACE,
359 "StartUnit");
360 method.append("obmc-flash-bmc-cleanup.service", "replace");
361 bus.call_noreply(method);
362}
363
Saqib Khan35e83f32017-05-22 11:37:32 -0500364ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500365 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500366{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500367 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500368
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500369 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500370 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500371 fs::path file(filePath);
372 file /= bmcImage;
373 std::ifstream efile(file.c_str());
374 if (efile.good() != 1)
375 {
376 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500377 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500378 invalid = true;
379 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500380 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500381
382 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500383 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500384 return ItemUpdater::ActivationStatus::invalid;
385 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500386
387 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500388}
389
Saqib Khanb9da6632017-09-13 09:48:37 -0500390void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500391{
392 //TODO openbmc/openbmc#1896 Improve the performance of this function
393 for (const auto& intf : activations)
394 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500395 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500396 {
Saqib Khanb9da6632017-09-13 09:48:37 -0500397 if (intf.second->redundancyPriority.get()->priority() == value &&
398 intf.second->versionId != versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500399 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500400 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500401 }
402 }
403 }
404}
405
Michael Tritz37a59042017-07-12 13:44:53 -0500406void ItemUpdater::reset()
407{
408 // Mark the read-write partition for recreation upon reboot.
409 auto method = bus.new_method_call(
410 SYSTEMD_BUSNAME,
411 SYSTEMD_PATH,
412 SYSTEMD_INTERFACE,
413 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500414 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500415 bus.call_noreply(method);
416
417 log<level::INFO>("BMC factory reset will take effect upon reboot.");
418
419 return;
420}
421
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500422void ItemUpdater::removeReadOnlyPartition(std::string versionId)
423{
424 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
425 ".service";
426
427 // Remove the read-only partitions.
428 auto method = bus.new_method_call(
429 SYSTEMD_BUSNAME,
430 SYSTEMD_PATH,
431 SYSTEMD_INTERFACE,
432 "StartUnit");
433 method.append(serviceFile, "replace");
434 bus.call_noreply(method);
435}
436
Michael Tritz0129d922017-08-10 19:33:46 -0500437bool ItemUpdater::fieldModeEnabled(bool value)
438{
439 // enabling field mode is intended to be one way: false -> true
440 if (value && !control::FieldMode::fieldModeEnabled())
441 {
442 control::FieldMode::fieldModeEnabled(value);
443
444 auto method = bus.new_method_call(
445 SYSTEMD_BUSNAME,
446 SYSTEMD_PATH,
447 SYSTEMD_INTERFACE,
448 "StartUnit");
449 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500450 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500451 bus.call_noreply(method);
452
453 method = bus.new_method_call(
454 SYSTEMD_BUSNAME,
455 SYSTEMD_PATH,
456 SYSTEMD_INTERFACE,
457 "StopUnit");
458 method.append("usr-local.mount", "replace");
459 bus.call_noreply(method);
460
461 std::vector<std::string> usrLocal = {"usr-local.mount"};
462
463 method = bus.new_method_call(
464 SYSTEMD_BUSNAME,
465 SYSTEMD_PATH,
466 SYSTEMD_INTERFACE,
467 "MaskUnitFiles");
468 method.append(usrLocal, false, true);
469 bus.call_noreply(method);
470 }
471
472 return control::FieldMode::fieldModeEnabled();
473}
474
475void ItemUpdater::restoreFieldModeStatus()
476{
477 std::ifstream input("/run/fw_env");
478 std::string envVar;
479 std::getline(input, envVar);
480
Gunnar Mills9a782242017-08-22 16:23:15 -0500481 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500482 {
483 ItemUpdater::fieldModeEnabled(true);
484 }
485}
486
Gunnar Millsb60add12017-08-24 16:41:42 -0500487void ItemUpdater::setBMCInventoryPath()
488{
489 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
490 // that implement the BMC inventory interface
491 auto depth = 0;
492 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
493 MAPPER_PATH,
494 MAPPER_INTERFACE,
495 "GetSubTreePaths");
496
497 mapperCall.append(CHASSIS_INVENTORY_PATH);
498 mapperCall.append(depth);
499
500 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
501 // mapper is fixed.
502 std::vector<std::string> filter = {};
503 mapperCall.append(filter);
504
505 auto response = bus.call(mapperCall);
506 if (response.is_method_error())
507 {
508 log<level::ERR>("Error in mapper GetSubTreePath");
509 return;
510 }
511
512 using ObjectPaths = std::vector<std::string>;
513 ObjectPaths result;
514 response.read(result);
515
516 if (result.empty())
517 {
518 log<level::ERR>("Invalid response from mapper");
519 return;
520 }
521
522 for (auto& iter : result)
523 {
524 const auto& path = iter;
525 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
526 {
527 bmcInventoryPath = path;
528 return;
529 }
530 }
531}
532
Gunnar Millsf10b2322017-09-21 15:31:55 -0500533void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500534{
535 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
536 ACTIVE_REV_ASSOCIATION,
537 path));
538 associations(assocs);
539}
540
Gunnar Mills88e8a322017-09-13 11:09:28 -0500541void ItemUpdater::createFunctionalAssociation(const std::string& path)
542{
543 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
544 FUNCTIONAL_REV_ASSOCIATION,
545 path));
546 associations(assocs);
547}
548
Gunnar Millsf10b2322017-09-21 15:31:55 -0500549void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500550{
551 for (auto iter = assocs.begin(); iter != assocs.end();)
552 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500553 // Since there could be multiple associations to the same path,
554 // only remove ones that have an active forward association.
555 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
556 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500557 {
558 iter = assocs.erase(iter);
559 associations(assocs);
560 }
561 else
562 {
563 ++iter;
564 }
565 }
566}
567
Saqib Khanb9da6632017-09-13 09:48:37 -0500568bool ItemUpdater::isLowestPriority(uint8_t value)
569{
570 for (const auto& intf : activations)
571 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500572 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500573 {
574 if (intf.second->redundancyPriority.get()->priority() < value)
575 {
576 return false;
577 }
578 }
579 }
580 return true;
581}
582
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500583} // namespace updater
584} // namespace software
585} // namespace phosphor