blob: 33f4096ad9cb22e768c9661aff717d98d2435d0b [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
Michael Tritz4254bec2017-10-03 17:18:22 -0500122 auto activationPtr = std::make_unique<Activation>(
123 bus,
124 path,
125 *this,
126 versionId,
127 activationState,
128 associations);
129
130 activationPtr->deleteObject =
131 std::make_unique<Delete>(bus, path, *activationPtr);
132
133 activations.insert(std::make_pair(versionId, std::move(activationPtr)));
134
Saqib Khan705f1bf2017-06-09 23:58:38 -0500135 versions.insert(std::make_pair(
136 versionId,
Gunnar Mills9a782242017-08-22 16:23:15 -0500137 std::make_unique<VersionClass>(
Saqib Khan705f1bf2017-06-09 23:58:38 -0500138 bus,
139 path,
140 version,
141 purpose,
Eddie James9440f492017-08-30 11:34:16 -0500142 filePath)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500143 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500144 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500145}
146
Saqib Khanba239882017-05-26 08:41:54 -0500147void ItemUpdater::processBMCImage()
148{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500149 using VersionClass = phosphor::software::manager::Version;
150 // Read os-release from /etc/ to get the functional BMC version
151 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
152
Saqib Khan1eef62d2017-08-10 15:29:34 -0500153 // Read os-release from folders under /media/ to get
154 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500155 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500156 {
157 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500158 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500159
160 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
161 if (0 == iter.path().native().compare(0, BMC_RO_PREFIX_LEN,
Saqib Khan6fab70d2017-09-07 00:13:50 -0500162 BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500163 {
Saqib Khan021c3652017-09-26 12:11:02 -0500164 // The versionId is extracted from the path
165 // for example /media/ro-2a1022fe.
166 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500167 auto osRelease = iter.path() / OS_RELEASE_FILE;
168 if (!fs::is_regular_file(osRelease))
169 {
Gunnar Mills2ad1b552017-10-19 15:58:52 -0500170 log<level::ERR>("Failed to read osRelease",
171 entry("FILENAME=%s", osRelease.string()));
Saqib Khan021c3652017-09-26 12:11:02 -0500172 ItemUpdater::erase(id);
173 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500174 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500175 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500176 if (version.empty())
177 {
178 log<level::ERR>("Failed to read version from osRelease",
179 entry("FILENAME=%s", osRelease.string()));
180 activationState = server::Activation::Activations::Invalid;
181 }
Saqib Khan021c3652017-09-26 12:11:02 -0500182
Saqib Khan1eef62d2017-08-10 15:29:34 -0500183 auto purpose = server::Version::VersionPurpose::BMC;
184 auto path = fs::path(SOFTWARE_OBJPATH) / id;
185
Gunnar Mills88e8a322017-09-13 11:09:28 -0500186 // Create functional association if this is the functional version
187 if (version.compare(functionalVersion) == 0)
188 {
189 createFunctionalAssociation(path);
190 }
191
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500192 AssociationList associations = {};
193
194 if (activationState == server::Activation::Activations::Active)
195 {
196 // Create an association to the BMC inventory item
197 associations.emplace_back(std::make_tuple(
198 ACTIVATION_FWD_ASSOCIATION,
199 ACTIVATION_REV_ASSOCIATION,
200 bmcInventoryPath));
201
202 // Create an active association since this image is active
203 createActiveAssociation(path);
204 }
205
Adriana Kobylakee590c72017-09-26 15:16:06 -0500206 // Create Version instance for this version.
207 auto versionPtr = std::make_unique<VersionClass>(
208 bus,
209 path,
210 version,
211 purpose,
212 "");
213 auto isVersionFunctional = versionPtr->isFunctional();
214 versions.insert(std::make_pair(
215 id,
216 std::move(versionPtr)));
217
Saqib Khan1eef62d2017-08-10 15:29:34 -0500218 // Create Activation instance for this version.
Michael Tritz4254bec2017-10-03 17:18:22 -0500219 auto activationPtr = std::make_unique<Activation>(
220 bus,
221 path,
222 *this,
223 id,
224 activationState,
225 associations);
226
227 // Add Delete() if this isn't the functional version
228 if (!isVersionFunctional)
229 {
230 activationPtr->deleteObject =
231 std::make_unique<Delete>(bus, path, *activationPtr);
232 }
233
234 activations.insert(std::make_pair(id, std::move(activationPtr)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500235
236 // If Active, create RedundancyPriority instance for this version.
237 if (activationState == server::Activation::Activations::Active)
238 {
239 uint8_t priority = std::numeric_limits<uint8_t>::max();
240 if (!restoreFromFile(id, priority))
241 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500242 if (isVersionFunctional)
243 {
244 priority = 0;
245 }
246 else
247 {
248 log<level::ERR>("Unable to restore priority from file.",
249 entry("VERSIONID=%s", id));
250 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500251 }
252 activations.find(id)->second->redundancyPriority =
253 std::make_unique<RedundancyPriority>(
Saqib Khanba239882017-05-26 08:41:54 -0500254 bus,
255 path,
Saqib Khan1eef62d2017-08-10 15:29:34 -0500256 *(activations.find(id)->second),
257 priority);
258 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500259 }
260 }
Saqib Khandcbfa042017-09-18 13:08:39 -0500261
262 // If there is no ubi volume for bmc version then read the /etc/os-release
263 // and create rofs-<versionId> under /media
264 if (activations.size() == 0)
265 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500266 auto version = VersionClass::getBMCVersion(OS_RELEASE_FILE);
Saqib Khandcbfa042017-09-18 13:08:39 -0500267 auto id = phosphor::software::manager::Version::getId(version);
268 auto versionFileDir = BMC_ROFS_PREFIX + id + "/etc/";
269 try
270 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500271 if (!fs::is_directory(versionFileDir))
Saqib Khandcbfa042017-09-18 13:08:39 -0500272 {
273 fs::create_directories(versionFileDir);
274 }
275 auto versionFilePath = BMC_ROFS_PREFIX + id + OS_RELEASE_FILE;
276 fs::create_directory_symlink(OS_RELEASE_FILE, versionFilePath);
277 ItemUpdater::processBMCImage();
278 }
279 catch (const std::exception& e)
280 {
281 log<level::ERR>(e.what());
282 }
283 }
Saqib Khanba239882017-05-26 08:41:54 -0500284 return;
285}
286
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500287void ItemUpdater::erase(std::string entryId)
288{
Eddie James6d873712017-09-01 11:29:07 -0500289 // Find entry in versions map
290 auto it = versions.find(entryId);
291 if (it != versions.end())
292 {
293 if (it->second->isFunctional())
294 {
295 log<level::ERR>(("Error: Version " + entryId + \
296 " is currently running on the BMC." \
297 " Unable to remove.").c_str());
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500298 return;
Eddie James6d873712017-09-01 11:29:07 -0500299 }
300
301 // Delete ReadOnly partitions if it's not active
302 removeReadOnlyPartition(entryId);
303 removeFile(entryId);
304 }
305 else
306 {
307 // Delete ReadOnly partitions even if we can't find the version
308 removeReadOnlyPartition(entryId);
309 removeFile(entryId);
310
311 log<level::ERR>(("Error: Failed to find version " + entryId + \
312 " in item updater versions map." \
313 " Unable to remove.").c_str());
314 return;
315 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500316
317 // Remove the priority environment variable.
318 auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
319 auto method = bus.new_method_call(
320 SYSTEMD_BUSNAME,
321 SYSTEMD_PATH,
322 SYSTEMD_INTERFACE,
323 "StartUnit");
324 method.append(serviceFile, "replace");
325 bus.call_noreply(method);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500326
327 // Removing entry in versions map
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500328 this->versions.erase(entryId);
329
330 // Removing entry in activations map
331 auto ita = activations.find(entryId);
332 if (ita == activations.end())
333 {
334 log<level::ERR>(("Error: Failed to find version " + entryId + \
Gunnar Mills9a782242017-08-22 16:23:15 -0500335 " in item updater activations map." \
336 " Unable to remove.").c_str());
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500337 return;
338 }
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500339
340 this->activations.erase(entryId);
Saqib Khan49446ae2017-10-02 10:54:20 -0500341 ItemUpdater::resetUbootEnvVars();
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500342}
343
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500344void ItemUpdater::deleteAll()
345{
346 std::vector<std::string> deletableVersions;
347
348 for (const auto& versionIt : versions)
349 {
350 if (!versionIt.second->isFunctional())
351 {
352 deletableVersions.push_back(versionIt.first);
353 }
354 }
355
356 for (const auto& deletableIt : deletableVersions)
357 {
358 ItemUpdater::erase(deletableIt);
359 }
360
361 // Remove any volumes that do not match current versions.
362 auto method = bus.new_method_call(
363 SYSTEMD_BUSNAME,
364 SYSTEMD_PATH,
365 SYSTEMD_INTERFACE,
366 "StartUnit");
367 method.append("obmc-flash-bmc-cleanup.service", "replace");
368 bus.call_noreply(method);
369}
370
Saqib Khan35e83f32017-05-22 11:37:32 -0500371ItemUpdater::ActivationStatus ItemUpdater::validateSquashFSImage(
Gunnar Mills9a782242017-08-22 16:23:15 -0500372 const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500373{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500374 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500375
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500376 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500377 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500378 fs::path file(filePath);
379 file /= bmcImage;
380 std::ifstream efile(file.c_str());
381 if (efile.good() != 1)
382 {
383 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500384 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500385 invalid = true;
386 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500387 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500388
389 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500390 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500391 return ItemUpdater::ActivationStatus::invalid;
392 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500393
394 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500395}
396
Saqib Khanb9da6632017-09-13 09:48:37 -0500397void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500398{
399 //TODO openbmc/openbmc#1896 Improve the performance of this function
400 for (const auto& intf : activations)
401 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500402 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500403 {
Saqib Khanb9da6632017-09-13 09:48:37 -0500404 if (intf.second->redundancyPriority.get()->priority() == value &&
405 intf.second->versionId != versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500406 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500407 intf.second->redundancyPriority.get()->priority(value + 1);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500408 }
409 }
410 }
411}
412
Michael Tritz37a59042017-07-12 13:44:53 -0500413void ItemUpdater::reset()
414{
415 // Mark the read-write partition for recreation upon reboot.
416 auto method = bus.new_method_call(
417 SYSTEMD_BUSNAME,
418 SYSTEMD_PATH,
419 SYSTEMD_INTERFACE,
420 "StartUnit");
Michael Tritz0129d922017-08-10 19:33:46 -0500421 method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
Michael Tritz37a59042017-07-12 13:44:53 -0500422 bus.call_noreply(method);
423
424 log<level::INFO>("BMC factory reset will take effect upon reboot.");
425
426 return;
427}
428
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500429void ItemUpdater::removeReadOnlyPartition(std::string versionId)
430{
431 auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId +
432 ".service";
433
434 // Remove the read-only partitions.
435 auto method = bus.new_method_call(
436 SYSTEMD_BUSNAME,
437 SYSTEMD_PATH,
438 SYSTEMD_INTERFACE,
439 "StartUnit");
440 method.append(serviceFile, "replace");
441 bus.call_noreply(method);
442}
443
Michael Tritz0129d922017-08-10 19:33:46 -0500444bool ItemUpdater::fieldModeEnabled(bool value)
445{
446 // enabling field mode is intended to be one way: false -> true
447 if (value && !control::FieldMode::fieldModeEnabled())
448 {
449 control::FieldMode::fieldModeEnabled(value);
450
451 auto method = bus.new_method_call(
452 SYSTEMD_BUSNAME,
453 SYSTEMD_PATH,
454 SYSTEMD_INTERFACE,
455 "StartUnit");
456 method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
Gunnar Mills9a782242017-08-22 16:23:15 -0500457 "replace");
Michael Tritz0129d922017-08-10 19:33:46 -0500458 bus.call_noreply(method);
459
460 method = bus.new_method_call(
461 SYSTEMD_BUSNAME,
462 SYSTEMD_PATH,
463 SYSTEMD_INTERFACE,
464 "StopUnit");
465 method.append("usr-local.mount", "replace");
466 bus.call_noreply(method);
467
468 std::vector<std::string> usrLocal = {"usr-local.mount"};
469
470 method = bus.new_method_call(
471 SYSTEMD_BUSNAME,
472 SYSTEMD_PATH,
473 SYSTEMD_INTERFACE,
474 "MaskUnitFiles");
475 method.append(usrLocal, false, true);
476 bus.call_noreply(method);
477 }
478
479 return control::FieldMode::fieldModeEnabled();
480}
481
482void ItemUpdater::restoreFieldModeStatus()
483{
Michael Tritzff0b4212017-10-24 17:38:09 -0500484 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500485 std::string envVar;
486 std::getline(input, envVar);
487
Gunnar Mills9a782242017-08-22 16:23:15 -0500488 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500489 {
490 ItemUpdater::fieldModeEnabled(true);
491 }
492}
493
Gunnar Millsb60add12017-08-24 16:41:42 -0500494void ItemUpdater::setBMCInventoryPath()
495{
496 //TODO: openbmc/openbmc#1786 - Get the BMC path by looking for objects
497 // that implement the BMC inventory interface
498 auto depth = 0;
499 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
500 MAPPER_PATH,
501 MAPPER_INTERFACE,
502 "GetSubTreePaths");
503
504 mapperCall.append(CHASSIS_INVENTORY_PATH);
505 mapperCall.append(depth);
506
507 // TODO: openbmc/openbmc#2226 - Add Inventory Item filter when
508 // mapper is fixed.
509 std::vector<std::string> filter = {};
510 mapperCall.append(filter);
511
512 auto response = bus.call(mapperCall);
513 if (response.is_method_error())
514 {
515 log<level::ERR>("Error in mapper GetSubTreePath");
516 return;
517 }
518
519 using ObjectPaths = std::vector<std::string>;
520 ObjectPaths result;
521 response.read(result);
522
523 if (result.empty())
524 {
525 log<level::ERR>("Invalid response from mapper");
526 return;
527 }
528
529 for (auto& iter : result)
530 {
531 const auto& path = iter;
532 if (path.substr(path.find_last_of('/') + 1).compare("bmc") == 0)
533 {
534 bmcInventoryPath = path;
535 return;
536 }
537 }
538}
539
Gunnar Millsf10b2322017-09-21 15:31:55 -0500540void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500541{
542 assocs.emplace_back(std::make_tuple(ACTIVE_FWD_ASSOCIATION,
543 ACTIVE_REV_ASSOCIATION,
544 path));
545 associations(assocs);
546}
547
Gunnar Mills88e8a322017-09-13 11:09:28 -0500548void ItemUpdater::createFunctionalAssociation(const std::string& path)
549{
550 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
551 FUNCTIONAL_REV_ASSOCIATION,
552 path));
553 associations(assocs);
554}
555
Gunnar Millsf10b2322017-09-21 15:31:55 -0500556void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500557{
558 for (auto iter = assocs.begin(); iter != assocs.end();)
559 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500560 // Since there could be multiple associations to the same path,
561 // only remove ones that have an active forward association.
562 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
563 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500564 {
565 iter = assocs.erase(iter);
566 associations(assocs);
567 }
568 else
569 {
570 ++iter;
571 }
572 }
573}
574
Saqib Khanb9da6632017-09-13 09:48:37 -0500575bool ItemUpdater::isLowestPriority(uint8_t value)
576{
577 for (const auto& intf : activations)
578 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500579 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500580 {
581 if (intf.second->redundancyPriority.get()->priority() < value)
582 {
583 return false;
584 }
585 }
586 }
587 return true;
588}
589
Saqib Khan49446ae2017-10-02 10:54:20 -0500590void ItemUpdater::resetUbootEnvVars()
591{
592 decltype(activations.begin()->second->redundancyPriority.get()->priority())
593 lowestPriority = std::numeric_limits<uint8_t>::max();
594 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
595 for (const auto& intf : activations)
596 {
597 if (!intf.second->redundancyPriority.get())
598 {
599 // Skip this version if the redundancyPriority is not initialized.
600 continue;
601 }
602
603 if (intf.second->redundancyPriority.get()->priority()
604 <= lowestPriority)
605 {
606 lowestPriority = intf.second->redundancyPriority.get()->priority();
607 lowestPriorityVersion = intf.second->versionId;
608 }
609 }
610
Saqib Khanf0382c32017-10-24 13:36:22 -0500611 // Update the U-boot environment variable to point to the lowest priority
612 auto it = activations.find(lowestPriorityVersion);
613 it->second->updateUbootEnvVars();
Saqib Khan49446ae2017-10-02 10:54:20 -0500614}
615
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500616} // namespace updater
617} // namespace software
618} // namespace phosphor