blob: 90a97d4b634759b40ee13d0eacd041f82c65cd15 [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "config.h"
2
3#include "item_updater.hpp"
4
5#include "images.hpp"
6#include "serialize.hpp"
7#include "version.hpp"
8#include "xyz/openbmc_project/Software/Version/server.hpp"
9
10#include <elog-errors.hpp>
11#include <experimental/filesystem>
Saqib Khan35e83f32017-05-22 11:37:32 -050012#include <fstream>
Gunnar Millsb0ce9962018-09-07 13:39:10 -050013#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
Adriana Kobylak204e1e72018-01-24 16:00:05 -060015#include <queue>
Adriana Kobylakb77551c2017-10-27 12:46:23 -050016#include <set>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050017#include <string>
Saqib Khandcbfa042017-09-18 13:08:39 -050018#include <xyz/openbmc_project/Software/Version/error.hpp>
Gunnar Millsec1b41c2017-05-02 12:20:36 -050019
20namespace phosphor
21{
22namespace software
23{
24namespace updater
25{
26
Gunnar Mills2ce7da22017-05-04 15:37:56 -050027// When you see server:: you know we're referencing our base class
28namespace server = sdbusplus::xyz::openbmc_project::Software::server;
Michael Tritz0129d922017-08-10 19:33:46 -050029namespace control = sdbusplus::xyz::openbmc_project::Control::server;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050030
31using namespace phosphor::logging;
Saqib Khandcbfa042017-09-18 13:08:39 -050032using namespace sdbusplus::xyz::openbmc_project::Software::Version::Error;
Jayanth Othayoth2ab9b102018-02-21 05:27:47 -060033using namespace phosphor::software::image;
Saqib Khan35e83f32017-05-22 11:37:32 -050034namespace fs = std::experimental::filesystem;
35
Patrick Williamse75d10f2017-05-30 16:56:32 -050036void ItemUpdater::createActivation(sdbusplus::message::message& msg)
Gunnar Millsec1b41c2017-05-02 12:20:36 -050037{
Saqib Khan84a0e692017-06-28 17:27:01 -050038
39 using SVersion = server::Version;
40 using VersionPurpose = SVersion::VersionPurpose;
Gunnar Mills9a782242017-08-22 16:23:15 -050041 using VersionClass = phosphor::software::manager::Version;
Saqib Khan84a0e692017-06-28 17:27:01 -050042 namespace mesg = sdbusplus::message;
43 namespace variant_ns = mesg::variant_ns;
44
45 mesg::object_path objPath;
46 auto purpose = VersionPurpose::Unknown;
Saqib Khan705f1bf2017-06-09 23:58:38 -050047 std::string version;
Adriana Kobylak2285fe02018-02-27 15:36:59 -060048 std::map<std::string, std::map<std::string, mesg::variant<std::string>>>
49 interfaces;
Patrick Williamse75d10f2017-05-30 16:56:32 -050050 msg.read(objPath, interfaces);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050051 std::string path(std::move(objPath));
Saqib Khan19177d32017-06-20 08:11:49 -050052 std::string filePath;
Gunnar Mills2ce7da22017-05-04 15:37:56 -050053
54 for (const auto& intf : interfaces)
55 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050056 if (intf.first == VERSION_IFACE)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050057 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050058 for (const auto& property : intf.second)
Gunnar Mills2ce7da22017-05-04 15:37:56 -050059 {
Saqib Khan705f1bf2017-06-09 23:58:38 -050060 if (property.first == "Purpose")
Gunnar Mills2ce7da22017-05-04 15:37:56 -050061 {
Saqib Khan84a0e692017-06-28 17:27:01 -050062 auto value = SVersion::convertVersionPurposeFromString(
Adriana Kobylak2285fe02018-02-27 15:36:59 -060063 variant_ns::get<std::string>(property.second));
Saqib Khan84a0e692017-06-28 17:27:01 -050064 if (value == VersionPurpose::BMC ||
65 value == VersionPurpose::System)
66 {
67 purpose = value;
68 }
Saqib Khan705f1bf2017-06-09 23:58:38 -050069 }
70 else if (property.first == "Version")
71 {
Saqib Khan84a0e692017-06-28 17:27:01 -050072 version = variant_ns::get<std::string>(property.second);
Gunnar Mills2ce7da22017-05-04 15:37:56 -050073 }
74 }
75 }
Saqib Khan19177d32017-06-20 08:11:49 -050076 else if (intf.first == FILEPATH_IFACE)
77 {
78 for (const auto& property : intf.second)
79 {
80 if (property.first == "Path")
81 {
Saqib Khan84a0e692017-06-28 17:27:01 -050082 filePath = variant_ns::get<std::string>(property.second);
Saqib Khan19177d32017-06-20 08:11:49 -050083 }
84 }
85 }
Gunnar Mills2ce7da22017-05-04 15:37:56 -050086 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -060087 if (version.empty() || 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",
Adriana Kobylak596466b2018-02-13 14:48:53 -060098 entry("OBJPATH=%s", path.c_str()));
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 =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600109 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
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600116 associations.emplace_back(
117 std::make_tuple(ACTIVATION_FWD_ASSOCIATION,
118 ACTIVATION_REV_ASSOCIATION, bmcInventoryPath));
Saqib Khan35e83f32017-05-22 11:37:32 -0500119 }
Gunnar Millsb60add12017-08-24 16:41:42 -0500120
Saqib Khanee13e832017-10-23 12:53:11 -0500121 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600122 versionId,
123 std::make_unique<Activation>(bus, path, *this, versionId,
124 activationState, associations)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500125
Saqib Khanee13e832017-10-23 12:53:11 -0500126 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600127 bus, path, version, purpose, filePath,
128 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500129 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600130 std::make_unique<phosphor::software::manager::Delete>(bus, path,
131 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500132 versions.insert(std::make_pair(versionId, std::move(versionPtr)));
Gunnar Mills2ce7da22017-05-04 15:37:56 -0500133 }
Patrick Williamse75d10f2017-05-30 16:56:32 -0500134 return;
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500135}
136
Saqib Khanba239882017-05-26 08:41:54 -0500137void ItemUpdater::processBMCImage()
138{
Gunnar Mills88e8a322017-09-13 11:09:28 -0500139 using VersionClass = phosphor::software::manager::Version;
Lei YU269bff32018-08-21 15:21:40 +0800140
141 // Check MEDIA_DIR and create if it does not exist
142 try
143 {
144 if (!fs::is_directory(MEDIA_DIR))
145 {
146 fs::create_directory(MEDIA_DIR);
147 }
148 }
149 catch (const fs::filesystem_error& e)
150 {
151 log<level::ERR>("Failed to prepare dir", entry("ERR=%s", e.what()));
152 return;
153 }
154
Gunnar Mills88e8a322017-09-13 11:09:28 -0500155 // Read os-release from /etc/ to get the functional BMC version
156 auto functionalVersion = VersionClass::getBMCVersion(OS_RELEASE_FILE);
157
Saqib Khan1eef62d2017-08-10 15:29:34 -0500158 // Read os-release from folders under /media/ to get
159 // BMC Software Versions.
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500160 for (const auto& iter : fs::directory_iterator(MEDIA_DIR))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500161 {
162 auto activationState = server::Activation::Activations::Active;
Saqib Khan6fab70d2017-09-07 00:13:50 -0500163 static const auto BMC_RO_PREFIX_LEN = strlen(BMC_ROFS_PREFIX);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500164
165 // Check if the BMC_RO_PREFIXis the prefix of the iter.path
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600166 if (0 ==
167 iter.path().native().compare(0, BMC_RO_PREFIX_LEN, BMC_ROFS_PREFIX))
Saqib Khan1eef62d2017-08-10 15:29:34 -0500168 {
Saqib Khan021c3652017-09-26 12:11:02 -0500169 // The versionId is extracted from the path
170 // for example /media/ro-2a1022fe.
171 auto id = iter.path().native().substr(BMC_RO_PREFIX_LEN);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500172 auto osRelease = iter.path() / OS_RELEASE_FILE;
173 if (!fs::is_regular_file(osRelease))
174 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600175 log<level::ERR>(
176 "Failed to read osRelease",
177 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan021c3652017-09-26 12:11:02 -0500178 ItemUpdater::erase(id);
179 continue;
Saqib Khan1eef62d2017-08-10 15:29:34 -0500180 }
Gunnar Mills88e8a322017-09-13 11:09:28 -0500181 auto version = VersionClass::getBMCVersion(osRelease);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500182 if (version.empty())
183 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600184 log<level::ERR>(
185 "Failed to read version from osRelease",
186 entry("FILENAME=%s", osRelease.string().c_str()));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500187 activationState = server::Activation::Activations::Invalid;
188 }
Saqib Khan021c3652017-09-26 12:11:02 -0500189
Saqib Khan1eef62d2017-08-10 15:29:34 -0500190 auto purpose = server::Version::VersionPurpose::BMC;
191 auto path = fs::path(SOFTWARE_OBJPATH) / id;
192
Lei YU269bff32018-08-21 15:21:40 +0800193 // Create functional association if this is the functional
194 // version
Gunnar Mills88e8a322017-09-13 11:09:28 -0500195 if (version.compare(functionalVersion) == 0)
196 {
197 createFunctionalAssociation(path);
198 }
199
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500200 AssociationList associations = {};
201
202 if (activationState == server::Activation::Activations::Active)
203 {
204 // Create an association to the BMC inventory item
205 associations.emplace_back(std::make_tuple(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600206 ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
207 bmcInventoryPath));
Gunnar Mills43b25cd2017-09-07 13:19:34 -0500208
209 // Create an active association since this image is active
210 createActiveAssociation(path);
211 }
212
Adriana Kobylakee590c72017-09-26 15:16:06 -0500213 // Create Version instance for this version.
214 auto versionPtr = std::make_unique<VersionClass>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600215 bus, path, version, purpose, "",
216 std::bind(&ItemUpdater::erase, this, std::placeholders::_1));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500217 auto isVersionFunctional = versionPtr->isFunctional();
Michael Tritz4254bec2017-10-03 17:18:22 -0500218 if (!isVersionFunctional)
219 {
Saqib Khanee13e832017-10-23 12:53:11 -0500220 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600221 std::make_unique<phosphor::software::manager::Delete>(
222 bus, path, *versionPtr);
Michael Tritz4254bec2017-10-03 17:18:22 -0500223 }
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600224 versions.insert(std::make_pair(id, std::move(versionPtr)));
Michael Tritz4254bec2017-10-03 17:18:22 -0500225
Saqib Khanee13e832017-10-23 12:53:11 -0500226 // Create Activation instance for this version.
227 activations.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600228 id, std::make_unique<Activation>(
229 bus, path, *this, id, activationState, associations)));
Saqib Khan1eef62d2017-08-10 15:29:34 -0500230
Lei YU269bff32018-08-21 15:21:40 +0800231 // If Active, create RedundancyPriority instance for this
232 // version.
Saqib Khan1eef62d2017-08-10 15:29:34 -0500233 if (activationState == server::Activation::Activations::Active)
234 {
235 uint8_t priority = std::numeric_limits<uint8_t>::max();
236 if (!restoreFromFile(id, priority))
237 {
Adriana Kobylakee590c72017-09-26 15:16:06 -0500238 if (isVersionFunctional)
239 {
240 priority = 0;
241 }
242 else
243 {
244 log<level::ERR>("Unable to restore priority from file.",
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600245 entry("VERSIONID=%s", id.c_str()));
Adriana Kobylakee590c72017-09-26 15:16:06 -0500246 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500247 }
248 activations.find(id)->second->redundancyPriority =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600249 std::make_unique<RedundancyPriority>(
250 bus, path, *(activations.find(id)->second), priority,
251 false);
Saqib Khan1eef62d2017-08-10 15:29:34 -0500252 }
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 }
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600278
279 mirrorUbootToAlt();
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 {
Lei YU0f88b5a2018-08-21 15:28:53 +0800289 if (it->second->isFunctional() && ACTIVE_BMC_MAX_ALLOWED > 1)
Eddie James6d873712017-09-01 11:29:07 -0500290 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600291 log<level::ERR>("Error: Version is currently running on the BMC. "
292 "Unable to remove.",
293 entry("VERSIONID=%s", entryId.c_str()));
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500294 return;
Eddie James6d873712017-09-01 11:29:07 -0500295 }
296
297 // Delete ReadOnly partitions if it's not active
298 removeReadOnlyPartition(entryId);
299 removeFile(entryId);
Saqib Khanee13e832017-10-23 12:53:11 -0500300
301 // Removing entry in versions map
302 this->versions.erase(entryId);
Eddie James6d873712017-09-01 11:29:07 -0500303 }
304 else
305 {
306 // Delete ReadOnly partitions even if we can't find the version
307 removeReadOnlyPartition(entryId);
308 removeFile(entryId);
309
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600310 log<level::ERR>("Error: Failed to find version in item updater "
311 "versions map. Unable to remove.",
312 entry("VERSIONID=%s", entryId.c_str()));
Eddie James6d873712017-09-01 11:29:07 -0500313 }
Saqib Khan1eef62d2017-08-10 15:29:34 -0500314
Lei YU56aaf452018-06-21 16:09:44 +0800315 helper.clearEntry(entryId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500316
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500317 // Removing entry in activations map
318 auto ita = activations.find(entryId);
319 if (ita == activations.end())
320 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600321 log<level::ERR>("Error: Failed to find version in item updater "
322 "activations map. Unable to remove.",
323 entry("VERSIONID=%s", entryId.c_str()));
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500324 }
Saqib Khanee13e832017-10-23 12:53:11 -0500325 else
326 {
327 this->activations.erase(entryId);
328 }
Saqib Khan49446ae2017-10-02 10:54:20 -0500329 ItemUpdater::resetUbootEnvVars();
Saqib Khanee13e832017-10-23 12:53:11 -0500330 return;
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500331}
332
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500333void ItemUpdater::deleteAll()
334{
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600335 std::vector<std::string> deletableVersions;
336
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500337 for (const auto& versionIt : versions)
338 {
339 if (!versionIt.second->isFunctional())
340 {
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600341 deletableVersions.push_back(versionIt.first);
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500342 }
343 }
344
Adriana Kobylak83cd21f2018-02-28 15:48:48 -0600345 for (const auto& deletableIt : deletableVersions)
346 {
347 ItemUpdater::erase(deletableIt);
348 }
349
Lei YU56aaf452018-06-21 16:09:44 +0800350 helper.cleanup();
Michael Tritzbc1bf3a2017-09-18 16:38:23 -0500351}
352
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600353ItemUpdater::ActivationStatus
354 ItemUpdater::validateSquashFSImage(const std::string& filePath)
Saqib Khan35e83f32017-05-22 11:37:32 -0500355{
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500356 bool invalid = false;
Saqib Khan35e83f32017-05-22 11:37:32 -0500357
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500358 for (auto& bmcImage : bmcImages)
Saqib Khan35e83f32017-05-22 11:37:32 -0500359 {
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500360 fs::path file(filePath);
361 file /= bmcImage;
362 std::ifstream efile(file.c_str());
363 if (efile.good() != 1)
364 {
365 log<level::ERR>("Failed to find the BMC image.",
Gunnar Mills9a782242017-08-22 16:23:15 -0500366 entry("IMAGE=%s", bmcImage.c_str()));
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500367 invalid = true;
368 }
Saqib Khan35e83f32017-05-22 11:37:32 -0500369 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500370
371 if (invalid)
Saqib Khan35e83f32017-05-22 11:37:32 -0500372 {
Saqib Khan35e83f32017-05-22 11:37:32 -0500373 return ItemUpdater::ActivationStatus::invalid;
374 }
Michael Tritzb1cfdf92017-08-14 14:33:30 -0500375
376 return ItemUpdater::ActivationStatus::ready;
Saqib Khan35e83f32017-05-22 11:37:32 -0500377}
378
Adriana Kobylakbbcb7be2018-07-17 15:47:34 -0500379void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
380{
381 storeToFile(versionId, value);
382 helper.setEntry(versionId, value);
383}
384
Saqib Khanb9da6632017-09-13 09:48:37 -0500385void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500386{
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500387 std::map<std::string, uint8_t> priorityMap;
388
389 // Insert the requested version and priority, it may not exist yet.
390 priorityMap.insert(std::make_pair(versionId, value));
391
Saqib Khan4c1aec02017-07-06 11:46:13 -0500392 for (const auto& intf : activations)
393 {
Gunnar Mills9a782242017-08-22 16:23:15 -0500394 if (intf.second->redundancyPriority)
Saqib Khan4c1aec02017-07-06 11:46:13 -0500395 {
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500396 priorityMap.insert(std::make_pair(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600397 intf.first, intf.second->redundancyPriority.get()->priority()));
Saqib Khan4c1aec02017-07-06 11:46:13 -0500398 }
399 }
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500400
401 // Lambda function to compare 2 priority values, use <= to allow duplicates
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600402 typedef std::function<bool(std::pair<std::string, uint8_t>,
403 std::pair<std::string, uint8_t>)>
404 cmpPriority;
405 cmpPriority cmpPriorityFunc =
406 [](std::pair<std::string, uint8_t> priority1,
407 std::pair<std::string, uint8_t> priority2) {
408 return priority1.second <= priority2.second;
409 };
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500410
411 // Sort versions by ascending priority
412 std::set<std::pair<std::string, uint8_t>, cmpPriority> prioritySet(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600413 priorityMap.begin(), priorityMap.end(), cmpPriorityFunc);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500414
415 auto freePriorityValue = value;
416 for (auto& element : prioritySet)
417 {
418 if (element.first == versionId)
419 {
420 continue;
421 }
422 if (element.second == freePriorityValue)
423 {
424 ++freePriorityValue;
425 auto it = activations.find(element.first);
426 it->second->redundancyPriority.get()->sdbusPriority(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600427 freePriorityValue);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500428 }
429 }
430
431 auto lowestVersion = prioritySet.begin()->first;
432 if (value == prioritySet.begin()->second)
433 {
434 lowestVersion = versionId;
435 }
436 updateUbootEnvVars(lowestVersion);
Saqib Khan4c1aec02017-07-06 11:46:13 -0500437}
438
Michael Tritz37a59042017-07-12 13:44:53 -0500439void ItemUpdater::reset()
440{
Lei YU56aaf452018-06-21 16:09:44 +0800441 helper.factoryReset();
Michael Tritz37a59042017-07-12 13:44:53 -0500442
443 log<level::INFO>("BMC factory reset will take effect upon reboot.");
Michael Tritz37a59042017-07-12 13:44:53 -0500444}
445
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500446void ItemUpdater::removeReadOnlyPartition(std::string versionId)
447{
Lei YU56aaf452018-06-21 16:09:44 +0800448 helper.removeVersion(versionId);
Leonel Gonzalez3526ef72017-07-07 14:38:25 -0500449}
450
Michael Tritz0129d922017-08-10 19:33:46 -0500451bool ItemUpdater::fieldModeEnabled(bool value)
452{
453 // enabling field mode is intended to be one way: false -> true
454 if (value && !control::FieldMode::fieldModeEnabled())
455 {
456 control::FieldMode::fieldModeEnabled(value);
457
Lei YU56aaf452018-06-21 16:09:44 +0800458 helper.enableFieldMode();
Michael Tritz0129d922017-08-10 19:33:46 -0500459 }
460
461 return control::FieldMode::fieldModeEnabled();
462}
463
464void ItemUpdater::restoreFieldModeStatus()
465{
Michael Tritzff0b4212017-10-24 17:38:09 -0500466 std::ifstream input("/dev/mtd/u-boot-env");
Michael Tritz0129d922017-08-10 19:33:46 -0500467 std::string envVar;
468 std::getline(input, envVar);
469
Gunnar Mills9a782242017-08-22 16:23:15 -0500470 if (envVar.find("fieldmode=true") != std::string::npos)
Michael Tritz0129d922017-08-10 19:33:46 -0500471 {
472 ItemUpdater::fieldModeEnabled(true);
473 }
474}
475
Gunnar Millsb60add12017-08-24 16:41:42 -0500476void ItemUpdater::setBMCInventoryPath()
477{
Gunnar Millsb60add12017-08-24 16:41:42 -0500478 auto depth = 0;
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600479 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
480 MAPPER_INTERFACE, "GetSubTreePaths");
Gunnar Millsb60add12017-08-24 16:41:42 -0500481
Adriana Kobylak1254c622017-12-07 12:24:56 -0600482 mapperCall.append(INVENTORY_PATH);
Gunnar Millsb60add12017-08-24 16:41:42 -0500483 mapperCall.append(depth);
Adriana Kobylak1254c622017-12-07 12:24:56 -0600484 std::vector<std::string> filter = {BMC_INVENTORY_INTERFACE};
Gunnar Millsb60add12017-08-24 16:41:42 -0500485 mapperCall.append(filter);
486
Ed Tanous87c78172018-08-10 12:51:53 -0700487 try
488 {
489 auto response = bus.call(mapperCall);
490
491 using ObjectPaths = std::vector<std::string>;
492 ObjectPaths result;
493 response.read(result);
494
495 if (!result.empty())
496 {
497 bmcInventoryPath = result.front();
498 }
499 }
500 catch (const sdbusplus::exception::SdBusError& e)
Gunnar Millsb60add12017-08-24 16:41:42 -0500501 {
502 log<level::ERR>("Error in mapper GetSubTreePath");
503 return;
504 }
505
Adriana Kobylak1254c622017-12-07 12:24:56 -0600506 return;
Gunnar Millsb60add12017-08-24 16:41:42 -0500507}
508
Gunnar Millsf10b2322017-09-21 15:31:55 -0500509void ItemUpdater::createActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500510{
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600511 assocs.emplace_back(
512 std::make_tuple(ACTIVE_FWD_ASSOCIATION, ACTIVE_REV_ASSOCIATION, path));
Gunnar Millsded875d2017-08-28 16:44:52 -0500513 associations(assocs);
514}
515
Gunnar Mills88e8a322017-09-13 11:09:28 -0500516void ItemUpdater::createFunctionalAssociation(const std::string& path)
517{
518 assocs.emplace_back(std::make_tuple(FUNCTIONAL_FWD_ASSOCIATION,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600519 FUNCTIONAL_REV_ASSOCIATION, path));
Gunnar Mills88e8a322017-09-13 11:09:28 -0500520 associations(assocs);
521}
522
Gunnar Millsf10b2322017-09-21 15:31:55 -0500523void ItemUpdater::removeActiveAssociation(const std::string& path)
Gunnar Millsded875d2017-08-28 16:44:52 -0500524{
525 for (auto iter = assocs.begin(); iter != assocs.end();)
526 {
Gunnar Mills88e8a322017-09-13 11:09:28 -0500527 // Since there could be multiple associations to the same path,
528 // only remove ones that have an active forward association.
529 if ((std::get<0>(*iter)).compare(ACTIVE_FWD_ASSOCIATION) == 0 &&
530 (std::get<2>(*iter)).compare(path) == 0)
Gunnar Millsded875d2017-08-28 16:44:52 -0500531 {
532 iter = assocs.erase(iter);
533 associations(assocs);
534 }
535 else
536 {
537 ++iter;
538 }
539 }
540}
541
Saqib Khanb9da6632017-09-13 09:48:37 -0500542bool ItemUpdater::isLowestPriority(uint8_t value)
543{
544 for (const auto& intf : activations)
545 {
Gunnar Millsd16bcbd2017-10-08 16:50:42 -0500546 if (intf.second->redundancyPriority)
Saqib Khanb9da6632017-09-13 09:48:37 -0500547 {
548 if (intf.second->redundancyPriority.get()->priority() < value)
549 {
550 return false;
551 }
552 }
553 }
554 return true;
555}
556
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500557void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
558{
Lei YU56aaf452018-06-21 16:09:44 +0800559 helper.updateUbootVersionId(versionId);
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500560}
561
Saqib Khan49446ae2017-10-02 10:54:20 -0500562void ItemUpdater::resetUbootEnvVars()
563{
564 decltype(activations.begin()->second->redundancyPriority.get()->priority())
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600565 lowestPriority = std::numeric_limits<uint8_t>::max();
Saqib Khan49446ae2017-10-02 10:54:20 -0500566 decltype(activations.begin()->second->versionId) lowestPriorityVersion;
567 for (const auto& intf : activations)
568 {
569 if (!intf.second->redundancyPriority.get())
570 {
571 // Skip this version if the redundancyPriority is not initialized.
572 continue;
573 }
574
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600575 if (intf.second->redundancyPriority.get()->priority() <= lowestPriority)
Saqib Khan49446ae2017-10-02 10:54:20 -0500576 {
577 lowestPriority = intf.second->redundancyPriority.get()->priority();
578 lowestPriorityVersion = intf.second->versionId;
579 }
580 }
581
Saqib Khanf0382c32017-10-24 13:36:22 -0500582 // Update the U-boot environment variable to point to the lowest priority
Adriana Kobylakb77551c2017-10-27 12:46:23 -0500583 updateUbootEnvVars(lowestPriorityVersion);
Saqib Khan49446ae2017-10-02 10:54:20 -0500584}
585
Adriana Kobylaka6963592018-09-07 14:13:29 -0500586void ItemUpdater::freeSpace(Activation& caller)
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600587{
588 // Versions with the highest priority in front
589 std::priority_queue<std::pair<int, std::string>,
590 std::vector<std::pair<int, std::string>>,
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600591 std::less<std::pair<int, std::string>>>
592 versionsPQ;
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600593
594 std::size_t count = 0;
595 for (const auto& iter : activations)
596 {
597 if ((iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600598 server::Activation::Activations::Active) ||
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600599 (iter.second.get()->activation() ==
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600600 server::Activation::Activations::Failed))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600601 {
602 count++;
603 // Don't put the functional version on the queue since we can't
604 // remove the "running" BMC version.
Lei YU0f88b5a2018-08-21 15:28:53 +0800605 // If ACTIVE_BMC_MAX_ALLOWED <= 1, there is only one active BMC,
606 // so remove functional version as well.
Adriana Kobylaka6963592018-09-07 14:13:29 -0500607 // Don't delete the the Activation object that called this function.
608 if ((versions.find(iter.second->versionId)
609 ->second->isFunctional() &&
610 ACTIVE_BMC_MAX_ALLOWED > 1) ||
611 (iter.second->versionId == caller.versionId))
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600612 {
613 continue;
614 }
Adriana Kobylaka6963592018-09-07 14:13:29 -0500615
616 // Failed activations don't have priority, assign them a large value
617 // for sorting purposes.
618 auto priority = 999;
619 if (iter.second.get()->activation() ==
620 server::Activation::Activations::Active)
621 {
622 priority = iter.second->redundancyPriority.get()->priority();
623 }
624
625 versionsPQ.push(std::make_pair(priority, iter.second->versionId));
Adriana Kobylak204e1e72018-01-24 16:00:05 -0600626 }
627 }
628
629 // If the number of BMC versions is over ACTIVE_BMC_MAX_ALLOWED -1,
630 // remove the highest priority one(s).
631 while ((count >= ACTIVE_BMC_MAX_ALLOWED) && (!versionsPQ.empty()))
632 {
633 erase(versionsPQ.top().second);
634 versionsPQ.pop();
635 count--;
636 }
637}
638
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600639void ItemUpdater::mirrorUbootToAlt()
640{
Lei YU56aaf452018-06-21 16:09:44 +0800641 helper.mirrorAlt();
Eddie Jameseaa1ee02018-03-01 10:09:10 -0600642}
643
Gunnar Millsec1b41c2017-05-02 12:20:36 -0500644} // namespace updater
645} // namespace software
646} // namespace phosphor